Merge branch 'main' into fix/qoder-cn-login-windows-build

This commit is contained in:
Simon Shi
2026-08-31 23:43:40 +08:00
committed by GitHub
9 changed files with 444 additions and 319 deletions
+85 -32
View File
@@ -1,27 +1,27 @@
import crypto from "node:crypto";
import type {
Api,
AssistantMessage,
AssistantMessageEventStream,
Context,
Model,
SimpleStreamOptions,
TextContent,
ThinkingContent,
ToolCall,
} from "@earendil-works/pi-ai";
import * as PiAi from "@earendil-works/pi-ai";
import {
type Api,
type AssistantMessage,
type AssistantMessageEventStream,
type Context,
clampThinkingLevel,
type Model,
type SimpleStreamOptions,
type TextContent,
type ThinkingContent,
type ToolCall,
} from "@earendil-works/pi-ai";
import {
buildAuthHeaders,
getMachineId,
getQoderChatURL,
getQoderCNDirectModel,
getQoderMode,
getQoderUserEmailFallback,
isQoderCNMode,
} from "./cosy.js";
import { getCachedModelConfig } from "./models.js";
import { resolveQoderIdentity } from "./oauth.js";
import { resolveQoderIdentity, getCachedCredentials } from "./oauth.js";
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js";
import { qoderEncodeBody } from "./qoder-encoding.js";
import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js";
import { transformMessagesForQoder, transformTools } from "./transform.js";
@@ -138,21 +138,21 @@ export function streamQoder(
const email = ident.email || getQoderUserEmailFallback(providerMode);
const machineID = ident.machineID || getMachineId();
const qoderModel = isQoderCNMode(providerMode) ? getQoderCNDirectModel(model.id) : model.id;
const modelConfig = getCachedModelConfig(qoderModel, providerMode) || {
key: qoderModel,
is_reasoning:
qoderModel === "ultimate" ||
qoderModel === "performance" ||
qoderModel.includes("dmodel") ||
qoderModel.includes("dfmodel"),
max_output_tokens: 32768,
// The model `id` pi exposes is the upstream display_name (whitespace
// stripped) for CN, or the raw key for the international site. The
// request-time upstream `key` is read back from the cached model config
// (which stores the original entry keyed by both `key` and `id`), so no
// key<->friendlyId mapping table is needed here.
const modelConfig = getCachedModelConfig(model.id, providerMode) || {
key: model.id,
is_reasoning: false,
source: "system",
};
modelConfig.key = qoderModel;
// Use the cached entry's original upstream key when available; fall back to
// the pi id (international site already uses the key as id).
const qoderModel = modelConfig.key || model.id;
const isReasoning = !!modelConfig.is_reasoning;
const maxOutputTokens = modelConfig.max_output_tokens || 32768;
const normalizedMessages = transformMessagesForQoder(context.messages);
// OMP may supply the system prompt as a single-element content array;
@@ -182,10 +182,12 @@ export function streamQoder(
? `${stablePart}-${options.sessionId}`
: `${stablePart}-${crypto.randomUUID()}`;
let maxTokens = 32768;
if (maxOutputTokens > 0) {
maxTokens = maxOutputTokens;
}
// Qoder's catalog exposes no per-model output cap, so we use the
// documented upstream ceiling (MAX_OUTPUT_TOKENS = 131072, see models.ts)
// and let pi cap it lower when the caller sets options.maxTokens (e.g.
// compaction at 40K). This avoids truncating reasoning chains / long
// generations that the 32K default would cut off.
let maxTokens = MAX_OUTPUT_TOKENS;
if (options?.maxTokens && options.maxTokens < maxTokens) {
maxTokens = options.maxTokens;
}
@@ -193,6 +195,38 @@ export function streamQoder(
const toolsRaw = context.tools && context.tools.length > 0 ? transformTools(context.tools) : undefined;
const recordID = stableChatRecordID(qoderModel, normalizedMessages, toolsRaw, maxTokens);
// Map pi's thinking level (options.reasoning) to Qoder's request fields.
// Confirmed from @qoder-ai/qodercli: the chat body carries `reasoning_effort`
// ("none"|"low"|"medium"|"high"|"xhigh"|"max") and `enable_thinking` (bool)
// inside `parameters`, alongside `max_tokens`.
//
// This mirrors the pattern the pi-ai OpenAI provider uses: clamp the
// requested level to what the model advertises via thinkingLevelMap, then
// map to the upstream effort name. clampThinkingLevel returns "off" when
// the level is unsupported or the user disabled thinking.
const requestedLevel = options?.reasoning;
const clamped = requestedLevel ? clampThinkingLevel(model, requestedLevel) : undefined;
const reasoningLevel = clamped === "off" ? undefined : clamped;
const parameters: Record<string, unknown> = { max_tokens: maxTokens };
if (reasoningLevel) {
parameters.enable_thinking = true;
// Effort-based models advertise concrete effort names in the map
// (low/medium/xhigh/max). Toggle-only models map every level to
// "enabled"/"disabled" and accept no effort value — only the on/off
// switch matters, so we send enable_thinking alone.
const mapped = model.thinkingLevelMap?.[reasoningLevel];
const effort = mapped && mapped !== "enabled" && mapped !== "disabled" ? mapped : reasoningLevel;
// Only send reasoning_effort when the upstream model actually exposes
// effort levels (thinking_config.enabled.efforts).
if (modelConfig?.thinking_config?.enabled?.efforts && typeof effort === "string") {
parameters.reasoning_effort = effort;
}
} else {
// No reasoning level selected (or clamped to off): explicitly disable
// thinking so the model does not reason by default.
parameters.enable_thinking = false;
}
const reqBody: Record<string, unknown> = {
request_id: crypto.randomUUID(),
request_set_id: recordID,
@@ -217,7 +251,7 @@ export function streamQoder(
system: "",
messages: systemText ? [{ role: "system", content: systemText }, ...normalizedMessages] : normalizedMessages,
tools: toolsRaw || [],
parameters: { max_tokens: maxTokens },
parameters,
chat_context: {
chatPrompt: "",
imageUrls: null,
@@ -295,7 +329,14 @@ export function streamQoder(
stream.push({ type: "start", partial: output });
while (true) {
// `data: [DONE]` is the end of the response. Break the read loop too, not
// just the line loop: Qoder's gateway keeps the HTTP body open after the
// sentinel, so waiting for `done` from reader.read() hung until the
// server or the OS eventually closed the socket. The full reply had
// already been streamed by then, so the agent looked stuck with no error.
let sawDone = false;
while (!sawDone) {
const { done, value } = await reader.read();
if (done) break;
@@ -312,6 +353,7 @@ export function streamQoder(
const dataStr = line.substring(5).trim();
if (dataStr === "[DONE]") {
sawDone = true;
break;
}
@@ -322,7 +364,14 @@ export function streamQoder(
}
const innerStr = envelope.body;
if (!innerStr || innerStr === "[DONE]") continue;
// The gateway sends the sentinel wrapped in an envelope
// (`body: "[DONE]"`) as well as bare, and both mean the reply is
// over, so both have to end the read loop.
if (innerStr === "[DONE]") {
sawDone = true;
break;
}
if (!innerStr) continue;
const inner = JSON.parse(innerStr);
if (inner.id) output.responseId = inner.id as string;
@@ -494,6 +543,10 @@ export function streamQoder(
}
}
// Stop reading and let the connection go once the reply is complete.
// Without this the body stays open until the server times it out.
await reader.cancel().catch(() => {});
if (thinkingParser) {
thinkingParser.finalize();
}