From e4c55f48713b00ca1bad0e87d72933d66bffcb15 Mon Sep 17 00:00:00 2001 From: snufkintomo Date: Fri, 21 Aug 2026 19:05:17 +0800 Subject: [PATCH 1/7] fix: end the stream at [DONE] and drop orphaned tool results Two independent bugs made Qoder turns hang or fail outright. 1. The SSE read loop never terminated on the sentinel. `data: [DONE]` broke only the inner line loop, so the outer loop kept awaiting reader.read() until the socket closed. Qoder's gateway holds the body open after the sentinel, so a reply that had already streamed in full produced no `done` event: the agent sat there with the complete answer buffered and no error, until something else timed the connection out. The sentinel also arrives wrapped in an envelope (`body: "[DONE]"`), which likewise only did `continue`, so both forms now end the loop. The reader is cancelled once the reply is complete instead of being left holding the connection. 2. Aborting a turn produced an invalid request. Assistant messages with stopReason error/aborted are skipped, but the tool result that followed was still emitted, leaving a `tool` message whose tool_call_id had no matching tool_calls entry. Upstreams reject that with "tool must follow a message with tool_calls" -- the same failure the null-content workaround above it already guards against -- so after one cancelled tool call, every later request in that session failed. Tool calls belonging to a dropped turn are now recorded and their results dropped with them. Both fixes are covered by regression tests that fail on the current code: the two stream tests time out at the 8s limit, and the transform tests produce an orphaned tool message. --- src/__tests__/stream.test.ts | 54 +++++++++++++++++++++++++++++++++ src/__tests__/transform.test.ts | 52 +++++++++++++++++++++++++++++++ src/stream.ts | 23 ++++++++++++-- src/transform.ts | 21 +++++++++++++ 4 files changed, 148 insertions(+), 2 deletions(-) diff --git a/src/__tests__/stream.test.ts b/src/__tests__/stream.test.ts index 268583a..7acc3de 100644 --- a/src/__tests__/stream.test.ts +++ b/src/__tests__/stream.test.ts @@ -290,4 +290,58 @@ describe("streamQoder", () => { expect(msg.content.find((c) => c.type === "toolCall")).toBeUndefined(); expect(msg.stopReason).toBe("stop"); }); + it("finishes when the gateway sends [DONE] but keeps the body open", async () => { + // Qoder's gateway does not always close the HTTP body after the sentinel. + // The read loop used to keep awaiting reader.read() until the socket went + // away, so a fully streamed reply never produced a done event and the + // agent appeared to hang with no error. + const sse = sseEnvelope(chunk({ content: "OK", role: "assistant" })) + sseEnvelope(finishChunk("stop")) + DONE_SSE; + let cancelled = false; + const body = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode(sse)); + // Deliberately never call controller.close(). + }, + cancel() { + cancelled = true; + }, + }); + globalThis.fetch = vi.fn( + async () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }), + ) as unknown as typeof fetch; + + const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); + const events = await consume(stream); + + const done = events.find((e) => e.type === "done"); + expect(done, "expected a done event even though the body stayed open").toBeDefined(); + const msg = (done as { message: AssistantMessage }).message; + expect(msg.stopReason).toBe("stop"); + const text = msg.content.find((c) => c.type === "text"); + expect(text && "text" in text ? text.text : "").toBe("OK"); + // The reader is released rather than left holding the connection. + expect(cancelled).toBe(true); + }); + + it("finishes on a bare 'data: [DONE]' line with the body left open", async () => { + // Same sentinel, unwrapped. + const sse = `${sseEnvelope(chunk({ content: "hi", role: "assistant" }))}data: [DONE]\n\n`; + const body = new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode(sse)); + }, + }); + globalThis.fetch = vi.fn( + async () => new Response(body, { status: 200, headers: { "content-type": "text/event-stream" } }), + ) as unknown as typeof fetch; + + const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" }); + const events = await consume(stream); + + const done = events.find((e) => e.type === "done"); + expect(done, "expected a done event for the bare sentinel").toBeDefined(); + const msg = (done as { message: AssistantMessage }).message; + const text = msg.content.find((c) => c.type === "text"); + expect(text && "text" in text ? text.text : "").toBe("hi"); + }); }); diff --git a/src/__tests__/transform.test.ts b/src/__tests__/transform.test.ts index 567f631..6b1365b 100644 --- a/src/__tests__/transform.test.ts +++ b/src/__tests__/transform.test.ts @@ -348,4 +348,56 @@ describe("transformMessagesForQoder", () => { expect(tool.role).toBe("tool"); expect(tool.tool_call_id).toBe("call_abc123"); }); + it("drops the tool result whose assistant turn was aborted", () => { + // Cancelling a turn mid-tool used to drop only the assistant message and + // keep its tool result, leaving a `tool` message with no matching + // tool_calls. Upstreams reject that with "tool must follow a message with + // tool_calls", so every later request in the session failed. + const msgs = [ + { role: "user", content: "go" }, + { + role: "assistant", + stopReason: "aborted", + content: [{ type: "toolCall", id: "call_gone", name: "bash", arguments: {} }], + }, + { role: "toolResult", toolCallId: "call_gone", content: "output" }, + { role: "user", content: "again" }, + ] as unknown as Message[]; + const result = transformMessagesForQoder(msgs); + + expect(result.map((m) => (m as { role: string }).role)).toEqual(["user", "user"]); + expect(result.some((m) => (m as { role: string }).role === "tool")).toBe(false); + }); + + it("keeps a healthy tool round-trip while dropping an aborted one", () => { + const msgs = [ + { + role: "assistant", + stopReason: "error", + content: [{ type: "toolCall", id: "call_bad", name: "x", arguments: {} }], + }, + { role: "toolResult", toolCallId: "call_bad", content: "r1" }, + { + role: "assistant", + content: [{ type: "toolCall", id: "call_good", name: "y", arguments: {} }], + }, + { role: "toolResult", toolCallId: "call_good", content: "r2" }, + ] as unknown as Message[]; + const result = transformMessagesForQoder(msgs); + + expect(result).toHaveLength(2); + const asst = result[0] as { tool_calls?: Array<{ id: string }> }; + const tool = result[1] as { role: string; tool_call_id: string }; + expect(asst.tool_calls?.map((t) => t.id)).toEqual(["call_good"]); + expect(tool.tool_call_id).toBe("call_good"); + + // Every tool message must have a declaring tool_calls entry. + const declared = new Set( + result.flatMap((m) => ((m as { tool_calls?: Array<{ id: string }> }).tool_calls ?? []).map((t) => t.id)), + ); + for (const m of result) { + const tm = m as { role: string; tool_call_id?: string }; + if (tm.role === "tool") expect(declared.has(tm.tool_call_id as string)).toBe(true); + } + }); }); diff --git a/src/stream.ts b/src/stream.ts index fd78f54..ae3cd6d 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -273,7 +273,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; @@ -290,6 +297,7 @@ export function streamQoder( const dataStr = line.substring(5).trim(); if (dataStr === "[DONE]") { + sawDone = true; break; } @@ -300,7 +308,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; @@ -472,6 +487,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(); } diff --git a/src/transform.ts b/src/transform.ts index 6933b72..5e06cc1 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -72,12 +72,33 @@ export function transformTools(tools: Tool[]): QoderTool[] { export function transformMessagesForQoder(messages: Message[]): QoderMessage[] { const normalizedMessages: QoderMessage[] = []; + // Dropping an assistant turn (below) also invalidates its tool calls: the + // result that follows would refer to a tool_calls entry that is no longer in + // the request, and upstreams reject that with "tool must follow a message + // with tool_calls". + const droppedToolCallIds = new Set(); + for (const msg of messages) { // Skip error or aborted messages if ( msg.role === "assistant" && ((msg as AssistantMessage).stopReason === "error" || (msg as AssistantMessage).stopReason === "aborted") ) { + const am = msg as AssistantMessage; + if (Array.isArray(am.content)) { + for (const block of am.content) { + if (block.type === "toolCall") { + const id = (block as ToolCall).id; + if (id) droppedToolCallIds.add(id); + } + } + } + continue; + } + + // Drop the result too, otherwise it refers to a tool_calls entry that is + // no longer in the request. + if (msg.role === "toolResult" && droppedToolCallIds.has((msg as ToolResultMessage).toolCallId)) { continue; } From b462207447100c91a54e9fe15c263ce9bce6e95c Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 08:16:36 +0800 Subject: [PATCH 2/7] feat(cn): expose upstream display_name as model id, drop key mapping tables MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The CN model picker used to translate Qoder's internal keys (qfmodel, qmodel_latest, ...) into hardcoded "friendly" ids (qwen3.6-flash, ...) via a manual mapping table, with the display label hardcoded alongside. Both sides rotted independently of upstream: when Qoder upgraded qfmodel to Qwen3.8-Flash the label stayed "Qwen 3.6 Flash", and new keys (qmodel_38max, q37fmodel) had no entry so their raw key leaked into the picker as the id. The catalog API already returns every key with its display_name, so the mapping table duplicates data the server provides and can only ever be stale. Rework: use the upstream display_name (whitespace-stripped) directly as the pi-visible Model.id, and read the request-time upstream key back from the cached model config at send time. No key<->friendlyId table is needed. - cosy.ts: remove qoderCNFriendlyModels, getQoderCNDirectModel, getQoderCNFriendlyModelInfo, toQoderCNFriendlyModel, prettifyQoderCNModelName. Add toQoderCNModelId(display) = display with whitespace stripped. - models.ts: updateQoderModelsCache now sets CN model id = toQoderCNModelId(display_name); the original key is still stored in configs (keyed by both key and id) and read back via getCachedModelConfig. Simplify the CN fallback in getCachedModelConfig (drop the stale reasoning set). staticCnModels ids/names rewritten to the display_name form. - stream.ts: the upstream key comes from modelConfig.key (the cached entry) instead of a reverse-lookup table; falls back to model.id for the international site, which already uses the key as id. - index.ts: modelsForProvider no longer re-maps CN models through a friendly transform — the cached/static ids are already the display name. Trade-off: a renamed/upgraded model changes its id, so a previously saved default model may no longer resolve after an upstream rename and the user reselects. This is expected: users should learn about new models. This is accepted. Tests updated for the new contract; e2e against the live CN catalog confirms id=Qwen3.8-Flash resolves to send key=qfmodel with no mapping table. --- src/__tests__/cosy.test.ts | 90 ++++++------------------------------ src/__tests__/models.test.ts | 4 +- src/cosy.ts | 70 ++++++---------------------- src/index.ts | 21 +++------ src/models.ts | 70 ++++++++++++---------------- src/stream.ts | 21 +++++---- 6 files changed, 77 insertions(+), 199 deletions(-) diff --git a/src/__tests__/cosy.test.ts b/src/__tests__/cosy.test.ts index 001c912..b1197c1 100644 --- a/src/__tests__/cosy.test.ts +++ b/src/__tests__/cosy.test.ts @@ -3,8 +3,6 @@ import { getQoderBaseUrl, getQoderCenterUrl, getQoderChatURL, - getQoderCNDirectModel, - getQoderCNFriendlyModelInfo, getQoderExchangeURL, getQoderManageUrl, getQoderMode, @@ -15,7 +13,7 @@ import { getQoderUserEmailFallback, getQoderUserInfoURL, isQoderCNMode, - toQoderCNFriendlyModel, + toQoderCNModelId, } from "../cosy.js"; // ── getQoderMode ────────────────────────────────────────────────────────── @@ -160,83 +158,23 @@ describe("getQoderUserEmailFallback", () => { }); }); -// ── getQoderCNDirectModel ───────────────────────────────────────────────── +// ── toQoderCNModelId ────────────────────────────────────────────────────── -describe("getQoderCNDirectModel", () => { - it("maps known model IDs to internal keys", () => { - expect(getQoderCNDirectModel("qoder-cn")).toBe("auto"); - expect(getQoderCNDirectModel("qwen3.7-max")).toBe("qmodel_latest"); - expect(getQoderCNDirectModel("qwen3.7-plus")).toBe("qmodel"); - expect(getQoderCNDirectModel("qwen3.6-plus")).toBe("qmodel"); - expect(getQoderCNDirectModel("qwen3.6-flash")).toBe("q36fmodel"); - expect(getQoderCNDirectModel("deepseek-v4-pro")).toBe("dmodel"); - expect(getQoderCNDirectModel("deepseek-v4-flash")).toBe("dfmodel"); - expect(getQoderCNDirectModel("glm-5.2")).toBe("gm51model"); - expect(getQoderCNDirectModel("glm-5.1")).toBe("gm51model"); - expect(getQoderCNDirectModel("kimi-k2.6")).toBe("kmodel"); - expect(getQoderCNDirectModel("minimax-m2.7")).toBe("mmodel"); - expect(getQoderCNDirectModel("minimax-m3")).toBe("mmodel"); +describe("toQoderCNModelId", () => { + it("strips whitespace from the upstream display_name", () => { + // The display name is used directly as the pi-visible model id, with + // whitespace removed so it stays a clean token for persistence keys and + // search. "Qwen3.8-Flash" has no spaces already. + expect(toQoderCNModelId("Qwen3.8-Flash")).toBe("Qwen3.8-Flash"); }); - it("returns the input ID for unknown models", () => { - expect(getQoderCNDirectModel("custom-model")).toBe("custom-model"); + it("collapses internal spaces", () => { + expect(toQoderCNModelId("Qwen 3.8 Max")).toBe("Qwen3.8Max"); + expect(toQoderCNModelId("DeepSeek V4 Pro")).toBe("DeepSeekV4Pro"); }); - it('defaults to "auto" when no input', () => { - expect(getQoderCNDirectModel()).toBe("auto"); - expect(getQoderCNDirectModel("")).toBe("auto"); - }); -}); - -// ── getQoderCNFriendlyModelInfo ─────────────────────────────────────────── - -describe("getQoderCNFriendlyModelInfo", () => { - it("returns known friendly info for mapped keys", () => { - const info = getQoderCNFriendlyModelInfo("qmodel_latest"); - expect(info.id).toBe("qwen3.7-max"); - expect(info.name).toBe("Qwen 3.7 Max · Qoder CN"); - }); - - it("returns auto mapping", () => { - const info = getQoderCNFriendlyModelInfo("auto"); - expect(info.id).toBe("auto"); - expect(info.name).toBe("Auto · Qoder CN"); - }); - - it("generates friendly name for unknown keys", () => { - const info = getQoderCNFriendlyModelInfo("my-custom-model", "My Custom Model"); - expect(info.id).toBe("my-custom-model"); - expect(info.name).toContain("Qoder CN"); - }); - - it("prettifies model names with version numbers", () => { - const info = getQoderCNFriendlyModelInfo("some-model", "Qwen3.7-New"); - expect(info.name).toContain("Qwen 3.7"); - expect(info.name).toContain("Qoder CN"); - }); -}); - -// ── toQoderCNFriendlyModel ──────────────────────────────────────────────── - -describe("toQoderCNFriendlyModel", () => { - it("maps known model ID to friendly version", () => { - const result = toQoderCNFriendlyModel({ id: "qmodel_latest", name: "Original Name" }); - expect(result.id).toBe("qwen3.7-max"); - expect(result.name).toBe("Qwen 3.7 Max · Qoder CN"); - }); - - it("preserves extra fields", () => { - const result = toQoderCNFriendlyModel({ id: "auto", name: "Auto", extra: "field" } as { - id: string; - name: string; - extra: string; - }); - expect(result.extra).toBe("field"); - }); - - it("handles unknown models by prettifying display name", () => { - const result = toQoderCNFriendlyModel({ id: "custom", name: "CustomModel V2-Pro" }); - expect(result.id).toBe("custom"); - expect(result.name).toContain("Qoder CN"); + it("falls back to a default when no display name is given", () => { + expect(toQoderCNModelId()).toBe("QoderCNModel"); + expect(toQoderCNModelId("")).toBe("QoderCNModel"); }); }); diff --git a/src/__tests__/models.test.ts b/src/__tests__/models.test.ts index 5e35c02..167c93e 100644 --- a/src/__tests__/models.test.ts +++ b/src/__tests__/models.test.ts @@ -43,8 +43,8 @@ describe("staticCnModels", () => { expect(staticCnModels.length).toBeGreaterThan(0); }); - it("has auto as first entry", () => { - expect(staticCnModels[0].id).toBe("auto"); + it("has Auto as first entry", () => { + expect(staticCnModels[0].id).toBe("Auto"); }); it("every CN model has required fields", () => { diff --git a/src/cosy.ts b/src/cosy.ts index f2a40b8..6bdd242 100644 --- a/src/cosy.ts +++ b/src/cosy.ts @@ -113,62 +113,20 @@ export function getQoderRefreshURL(mode?: string): string { return `${getQoderCenterUrl(mode)}/algo/api/v3/user/refresh_token`; } -export function getQoderCNDirectModel(modelID?: string): string { - return ( - { - "qoder-cn": "auto", - "qwen3.7-max": "qmodel_latest", - "qwen3.7-plus": "qmodel", - "qwen3.6-plus": "qmodel", - "qwen3.6-flash": "q36fmodel", - "deepseek-v4-pro": "dmodel", - "deepseek-v4-flash": "dfmodel", - "glm-5.2": "gm51model", - "glm-5.1": "gm51model", - "kimi-k2.6": "kmodel", - "minimax-m2.7": "mmodel", - "minimax-m3": "mmodel", - }[modelID || ""] || - modelID || - "auto" - ); -} - -const qoderCNFriendlyModels: Record = { - auto: { id: "auto", name: "Auto · Qoder CN" }, - "qoder-cn": { id: "qoder-cn", name: "Auto · Qoder CN" }, - qmodel_latest: { id: "qwen3.7-max", name: "Qwen 3.7 Max · Qoder CN" }, - qmodel: { id: "qwen3.7-plus", name: "Qwen 3.7 Plus · Qoder CN" }, - q36fmodel: { id: "qwen3.6-flash", name: "Qwen 3.6 Flash · Qoder CN" }, - qfmodel: { id: "qwen3.6-flash", name: "Qwen 3.6 Flash · Qoder CN" }, - dmodel: { id: "deepseek-v4-pro", name: "DeepSeek V4 Pro · Qoder CN" }, - dfmodel: { id: "deepseek-v4-flash", name: "DeepSeek V4 Flash · Qoder CN" }, - gm51model: { id: "glm-5.2", name: "GLM 5.2 · Qoder CN" }, - kmodel: { id: "kimi-k2.6", name: "Kimi K2.6 · Qoder CN" }, - mmodel: { id: "minimax-m2.7", name: "MiniMax M2.7 · Qoder CN" }, -}; - -function prettifyQoderCNModelName(name: string): string { - const pretty = (name || "Qoder CN Model") - .replace(/Qwen(\d)/g, "Qwen $1") - .replace(/Qwen([\d.]+)-/g, "Qwen $1 ") - .replace(/DeepSeek\s*V(\d)-/g, "DeepSeek V$1 ") - .replace(/\s+/g, " ") - .trim(); - return pretty.includes("Qoder CN") ? pretty : `${pretty} · Qoder CN`; -} - -export function getQoderCNFriendlyModelInfo(key: string, display?: string): { id: string; name: string } { - return qoderCNFriendlyModels[key] || { id: key, name: prettifyQoderCNModelName(display || key) }; -} - -export function toQoderCNFriendlyModel(model: T): T { - const info = getQoderCNFriendlyModelInfo(model.id, model.name); - return { - ...model, - id: info.id, - name: info.name, - }; +/** + * Derive the model `id` pi exposes in the picker from Qoder's `display_name`. + * + * The picker renders `Model.id`, not `Model.name`, so the display name is used + * directly as the id (with whitespace stripped so it stays a clean token for + * persistence keys, logs, and search). The original upstream `key` (e.g. + * `qfmodel`) is NOT used as the id: it is kept only as the request-time + * identifier, read back from the cached model config at send time. This drops + * the hardcoded key<->friendlyId mapping tables entirely — the label now + * tracks whatever upstream returns, so a renamed/upgraded model is shown + * correctly without a code change. + */ +export function toQoderCNModelId(displayName?: string): string { + return (displayName || "QoderCNModel").replace(/\s+/g, ""); } export function getQoderManageUrl(mode?: string): string { diff --git a/src/index.ts b/src/index.ts index 8a2c859..f0478de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,6 @@ import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai"; import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent"; -import { - getQoderBaseUrl, - getQoderMode, - getQoderUserEmailFallback, - isQoderCNMode, - toQoderCNFriendlyModel, -} from "./cosy.js"; +import { getQoderBaseUrl, getQoderMode, getQoderUserEmailFallback, isQoderCNMode } from "./cosy.js"; import { getCachedModels, isCacheStale, staticCnModels, staticModels, updateQoderModelsCache } from "./models.js"; import { autoLoginQoderFromEnvironment, @@ -29,14 +23,11 @@ function modelsForProvider(mode: string, providerID: string): Model[] { const cached = getCachedModels(mode); const modelsToUse = cached.length > 0 ? cached : isQoderCNMode(mode) ? staticCnModels : staticModels; - return modelsToUse.map((m) => { - const model = isQoderCNMode(mode) ? toQoderCNFriendlyModel(m) : m; - return { - ...model, - provider: providerID, - baseUrl: getQoderBaseUrl(mode), - }; - }) as unknown as Model[]; + return modelsToUse.map((m) => ({ + ...m, + provider: providerID, + baseUrl: getQoderBaseUrl(mode), + })) as unknown as Model[]; } function createQoderOAuth(providerID: string, mode: string): OAuthConfigWithUsage { diff --git a/src/models.ts b/src/models.ts index a94d11c..432726b 100644 --- a/src/models.ts +++ b/src/models.ts @@ -4,10 +4,10 @@ import { dirname, join } from "node:path"; import { buildAuthHeaders, getQoderBaseUrl, - getQoderCNFriendlyModelInfo, getQoderMode, getQoderModelListURL, isQoderCNMode, + toQoderCNModelId, } from "./cosy.js"; export const ZERO_COST = Object.freeze({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }); @@ -251,8 +251,8 @@ export const staticModels: QoderModelDef[] = [ export const staticCnModels: QoderModelDef[] = [ { - id: "auto", - name: "Auto · Qoder CN", + id: "Auto", + name: "Auto", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -265,8 +265,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN smart routing; live catalog reports 180K max input.", }, { - id: "qwen3.7-max", - name: "Qwen 3.7 Max · Qoder CN", + id: "Qwen3.7-Max", + name: "Qwen3.7-Max", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -279,8 +279,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN qmodel_latest; context options 200K/400K/1M.", }, { - id: "qwen3.7-plus", - name: "Qwen 3.7 Plus · Qoder CN", + id: "Qwen3.7-Plus", + name: "Qwen3.7-Plus", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -293,8 +293,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN qmodel; context options 200K/400K/1M.", }, { - id: "qwen3.6-flash", - name: "Qwen 3.6 Flash · Qoder CN", + id: "Qwen3.6-Flash", + name: "Qwen3.6-Flash", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -307,8 +307,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN q36fmodel; context options 200K/400K/1M.", }, { - id: "deepseek-v4-pro", - name: "DeepSeek V4 Pro · Qoder CN", + id: "DeepSeek-V4-Pro", + name: "DeepSeek-V4-Pro", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -321,8 +321,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN dmodel; context options 200K/400K/1M.", }, { - id: "deepseek-v4-flash", - name: "DeepSeek V4 Flash · Qoder CN", + id: "DeepSeek-V4-Flash", + name: "DeepSeek-V4-Flash", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -335,8 +335,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN dfmodel; context options 200K/400K/1M.", }, { - id: "glm-5.2", - name: "GLM 5.2 · Qoder CN", + id: "GLM-5.2", + name: "GLM-5.2", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -349,8 +349,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN gm51model; live catalog currently displays GLM-5.2 with 200K context.", }, { - id: "kimi-k2.6", - name: "Kimi K2.6 · Qoder CN", + id: "Kimi-K2.7-Code", + name: "Kimi-K2.7-Code", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -363,8 +363,8 @@ export const staticCnModels: QoderModelDef[] = [ description: "Qoder CN kmodel; context option 256K.", }, { - id: "minimax-m2.7", - name: "MiniMax M2.7 · Qoder CN", + id: "MiniMax-M2.7", + name: "MiniMax-M2.7", api: "qoder-api", provider: "qoder-cn", baseUrl: getQoderBaseUrl("cn"), @@ -407,29 +407,14 @@ export function getCachedModelConfig(modelKey: string, mode?: string): QoderMode } catch {} } + // No cached config. This only happens before the first successful catalog + // fetch (e.g. not yet logged in), in which case the request cannot succeed + // anyway. Return a minimal entry carrying the id as the key so callers have + // something to read; reasoning is unknown so default to false. if (isQoderCNMode(mode)) { - const reasoningModels = new Set([ - "qoder-cn", - "auto", - "qmodel_latest", - "qmodel", - "q36fmodel", - "qfmodel", - "dmodel", - "gm51model", - "kmodel", - "qwen3.7-max", - "qwen3.7-plus", - "qwen3.6-plus", - "qwen3.6-flash", - "deepseek-v4-pro", - "glm-5.2", - "glm-5.1", - "kimi-k2.6", - ]); return { key: modelKey, - is_reasoning: reasoningModels.has(modelKey), + is_reasoning: false, max_output_tokens: 32768, source: "system", }; @@ -526,7 +511,12 @@ export async function updateQoderModelsCache( const isVL = !!entry.is_vl; const isReasoning = !!entry.is_reasoning || !!entry.thinking_config; const supportsEffort = !!entry.thinking_config?.enabled?.efforts; - const modelInfo = isQoderCNMode(mode) ? getQoderCNFriendlyModelInfo(key, display) : { id: key, name: display }; + // CN models expose the upstream display_name (whitespace-stripped) as the + // pi-visible id; the original `key` is stored in `configs` and read back at + // request time, so no key<->friendlyId mapping table is needed. + const modelInfo = isQoderCNMode(mode) + ? { id: toQoderCNModelId(display), name: display } + : { id: key, name: display }; configs[key] = entry; if (modelInfo.id !== key) configs[modelInfo.id] = entry; diff --git a/src/stream.ts b/src/stream.ts index fd78f54..d92a9d8 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -15,7 +15,6 @@ import { buildAuthHeaders, getMachineId, getQoderChatURL, - getQoderCNDirectModel, getQoderMode, getQoderUserEmailFallback, isQoderCNMode, @@ -120,18 +119,20 @@ export function streamQoder( const email = cachedCreds?.email || getQoderUserEmailFallback(providerMode); const machineID = cachedCreds?.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"), + // 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, max_output_tokens: 32768, 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; From cc461cd145b9ecee0ace16d32e99df190077149c Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 10:11:37 +0800 Subject: [PATCH 3/7] refactor: drop redundant max_input_tokens, default context fallback to 200K max_input_tokens (180K) was only used to seed the context window before context_config options overrode it. Every live CN model exposes selectable contexts (200K/400K/1M) whose smallest option already exceeds 180K, so the seed only ever acted as a redundant floor and was never the final value. - updateQoderModelsCache: derive contextWindow from the largest context_config option directly; fall back to 200K when no context_config is present (was 180K via max_input_tokens). - staticCnModels: the Auto entry (the no-context fallback) now uses 200K too, matching the dynamic fallback; its description no longer references the stale 180K live-catalog note. --- src/models.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/models.ts b/src/models.ts index 432726b..923b620 100644 --- a/src/models.ts +++ b/src/models.ts @@ -260,9 +260,9 @@ export const staticCnModels: QoderModelDef[] = [ supportsEffort: false, input: ["text", "image"], cost: ZERO_COST, - contextWindow: 180000, + contextWindow: 200000, maxTokens: 32768, - description: "Qoder CN smart routing; live catalog reports 180K max input.", + description: "Qoder CN smart routing; fallback context window of 200K.", }, { id: "Qwen3.7-Max", @@ -497,13 +497,17 @@ export async function updateQoderModelsCache( if (!key || !entry.enable) continue; const display = entry.display_name || key; - let ctxLen = entry.max_input_tokens || 180000; + // The context window is the largest selectable context option the upstream + // catalog exposes (e.g. 1M when 200K/400K/1M are offered). Fall back to + // 200K when no context_config is present. `max_input_tokens` is not used: + // it is a flat base value (180K) that every catalog entry with selectable + // contexts already exceeds, so it only ever acted as a redundant floor. + let ctxLen = 200000; if (entry.context_config && typeof entry.context_config === "object") { for (const configVal of Object.values(entry.context_config)) { if (configVal && typeof configVal === "object" && typeof configVal.token_count === "number") { - const tc = configVal.token_count; - if (tc > ctxLen) { - ctxLen = tc; + if (configVal.token_count > ctxLen) { + ctxLen = configVal.token_count; } } } From 88cf414a8e4110f786f946020462e5b8bee1418e Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 17:27:07 +0800 Subject: [PATCH 4/7] feat(cn): map Qoder thinking_config to pi thinkingLevelMap for the picker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The catalog API returns each model's supported thinking efforts (thinking_config.enabled.efforts, e.g. low/medium/xhigh for qwen3.8), but the provider never populated Model.thinkingLevelMap, so pi's getSupportedThinkingLevels hid xhigh/max (they require a non-undefined map entry) and the picker topped out at "high" even for models that support xhigh. Parse thinking_config at catalog fetch time and build the map: - effort-based models (qwen3.8-max/flash, DeepSeek, GLM): each supported effort maps to its same-named pi level; unsupported levels pin to null (hidden). xhigh is now exposed. "off" is selectable when the catalog also advertises a "disabled" option. - toggle-only models (qwen3.7-max/plus: enabled/disabled, no efforts): every pi level is exposed and maps to "enabled" so picking any level turns thinking on; "off" selectable when "disabled" is advertised. - models without thinking_config (qwen3.7-flash): no map, pi falls back to its default level set. Qoder effort keys are already pi level names (low/medium/high/xhigh/max), so the mapping is by identity — no translation table. This is display-layer only: it fixes which thinking levels the picker shows. Sending the chosen effort upstream is a separate change that needs the exact request field from a qoder-cli capture; the map values ("low"/"xhigh"/ "enabled"/"disabled") are placeholders that will be mapped to the request shape once confirmed. --- src/models.ts | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/src/models.ts b/src/models.ts index 923b620..3aec41c 100644 --- a/src/models.ts +++ b/src/models.ts @@ -1,6 +1,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; +import type { ThinkingLevel, ThinkingLevelMap } from "@earendil-works/pi-ai"; import { buildAuthHeaders, getQoderBaseUrl, @@ -22,7 +23,10 @@ export interface QoderModelEntry { context_config?: Record; is_vl?: boolean; is_reasoning?: boolean; - thinking_config?: { enabled?: { efforts?: unknown } }; + thinking_config?: { + disabled?: unknown; + enabled?: { efforts?: Record; is_default?: boolean }; + }; source?: string; [key: string]: unknown; } @@ -35,6 +39,7 @@ export interface QoderModelDef { baseUrl: string; reasoning: boolean; supportsEffort: boolean; + thinkingLevelMap?: ThinkingLevelMap; input: ("text" | "image")[]; cost: typeof ZERO_COST; contextWindow: number; @@ -378,6 +383,52 @@ export const staticCnModels: QoderModelDef[] = [ }, ]; +/** pi thinking levels in display order (matches the pi-ai SDK this build targets). */ +const PI_THINKING_LEVELS: readonly ThinkingLevel[] = ["minimal", "low", "medium", "high", "xhigh"]; + +/** + * Map Qoder's `thinking_config` to pi's `thinkingLevelMap` so the TUI exposes + * the levels the upstream model actually supports. + * + * Qoder has two shapes: + * - effort-based: `thinking_config.enabled.efforts = { low, medium, xhigh, ... }` + * Each effort key is already a pi level name, so supported levels map to + * themselves and the rest are pinned to null (hidden in the picker). + * `xhigh`/`max` are only shown when the map carries them, otherwise the + * picker tops out at `high`. + * - toggle-based: `thinking_config.enabled` without `efforts` (only on/off). + * Every pi level is exposed and maps to "enabled" so a user picking any + * level turns thinking on; the exact effort sent upstream is decided at + * request time. + * Returns undefined for models that do not support thinking, so pi falls back + * to `reasoning: false`-style behavior (only `off`). + */ +function buildThinkingLevelMap(entry: QoderModelEntry): ThinkingLevelMap | undefined { + const tc = entry.thinking_config; + if (!tc) return undefined; + const efforts = tc.enabled?.efforts; + if (efforts && typeof efforts === "object") { + const supported = new Set(Object.keys(efforts)); + // `off` (disable thinking) is selectable when the catalog advertises a + // `disabled` option; otherwise pin it to null to hide it. + const map: ThinkingLevelMap = { off: tc.disabled ? "disabled" : null }; + for (const level of PI_THINKING_LEVELS) { + map[level] = supported.has(level) ? level : null; + } + return map; + } + // toggle-only (enabled/disabled, no efforts) — expose every level as "on". + // `off` is selectable when the catalog advertises `disabled`. + if (tc.enabled) { + const map: ThinkingLevelMap = { off: tc.disabled ? "disabled" : null }; + for (const level of PI_THINKING_LEVELS) { + map[level] = "enabled"; + } + return map; + } + return undefined; +} + export function getCachedModels(mode?: string): QoderModelDef[] { const cachePath = getQoderCachePath(mode); if (existsSync(cachePath)) { @@ -515,6 +566,7 @@ export async function updateQoderModelsCache( const isVL = !!entry.is_vl; const isReasoning = !!entry.is_reasoning || !!entry.thinking_config; const supportsEffort = !!entry.thinking_config?.enabled?.efforts; + const thinkingLevelMap = buildThinkingLevelMap(entry); // CN models expose the upstream display_name (whitespace-stripped) as the // pi-visible id; the original `key` is stored in `configs` and read back at // request time, so no key<->friendlyId mapping table is needed. @@ -533,6 +585,7 @@ export async function updateQoderModelsCache( baseUrl: getQoderBaseUrl(mode), reasoning: isReasoning, supportsEffort, + thinkingLevelMap, input: isVL ? ["text", "image"] : ["text"], cost: ZERO_COST, contextWindow: ctxLen, From 5393c24b7ef52f1c170964497f87d49fc36345f6 Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 18:57:56 +0800 Subject: [PATCH 5/7] feat: send reasoning_effort + enable_thinking to Qoder upstream MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The picker was fixed to show thinking levels, but the chosen level was never forwarded: stream.ts only used options.reasoning to toggle the local thinking parser, so every request let the model default-decide its reasoning effort. Reverse-engineered @qoder-ai/qodercli (bundle/qodercli.js) to confirm the request shape: the chat body's `parameters` object carries `reasoning_effort` ("none"|"low"|"medium"|"high"|"xhigh"|"max") and `enable_thinking` (bool) alongside `max_tokens`. qodercli's Zlr() sets reasoning_effort = zN(req) (reasoning.enabled===false ? "none" : reasoning.effort) and enable_thinking. Implementation follows the pi-ai OpenAI provider pattern: - clampThinkingLevel(model, options.reasoning) clamps the requested level to what the model advertises via thinkingLevelMap (returns "off" when unsupported or disabled); - map the clamped level to the upstream effort name via thinkingLevelMap, falling back to the pi level name (Qoder effort names already match pi); - effort-based models (thinking_config.enabled.efforts present, e.g. qwen3.8-max) get reasoning_effort + enable_thinking=true; - toggle-only models (qwen3.7-max: enabled/disabled, no efforts) get enable_thinking=true only — no reasoning_effort, since they accept no level; - no level / clamped to off: enable_thinking=false to explicitly disable. Qoder has no "minimal" effort; clampThinkingLevel handles stepping it down based on the model's thinkingLevelMap (minimal is pinned to null for effort models, so it clamps to the next supported level). --- src/stream.ts | 57 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 45 insertions(+), 12 deletions(-) diff --git a/src/stream.ts b/src/stream.ts index d92a9d8..e2e13cc 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -1,16 +1,17 @@ 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, @@ -173,6 +174,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 = { 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 = { request_id: crypto.randomUUID(), request_set_id: recordID, @@ -197,7 +230,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, From 112da4deaf0fbedb2e398dd3f20fa01abf3d1da1 Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 19:07:16 +0800 Subject: [PATCH 6/7] refactor: drop dead max_output_tokens, simplify maxTokens to 32K + pi cap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Qoder's /model/list catalog never returns max_output_tokens (the field does not exist on any entry — only max_input_tokens is present), so the per-model lookup `entry.max_output_tokens || 32768` was dead code that always fell back to the 32768 literal. - models.ts: remove max_output_tokens from QoderModelEntry; maxTokens is now a flat 32768 (matches qodercli's 32e3 fallback). - stream.ts: remove the now-unused maxOutputTokens variable and its always-true branch; maxTokens = 32768, capped lower only by options.maxTokens (e.g. pi compaction passes 40000). pi-ai's maxTokens setting still has an outlet. - stream.ts fallback modelConfig: drop the dead max_output_tokens: 32768 field. qodercli does conditionally send max_tokens (user setting → generation config → 32K fallback), but the field is not required by the upstream API. Keeping it gives pi-ai's options.maxTokens a place to flow through. --- src/models.ts | 3 +-- src/stream.ts | 8 +++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/models.ts b/src/models.ts index 3aec41c..de9827a 100644 --- a/src/models.ts +++ b/src/models.ts @@ -19,7 +19,6 @@ export interface QoderModelEntry { enable?: boolean; display_name?: string; max_input_tokens?: number; - max_output_tokens?: number; context_config?: Record; is_vl?: boolean; is_reasoning?: boolean; @@ -589,7 +588,7 @@ export async function updateQoderModelsCache( input: isVL ? ["text", "image"] : ["text"], cost: ZERO_COST, contextWindow: ctxLen, - maxTokens: entry.max_output_tokens || 32768, + maxTokens: 32768, }); } diff --git a/src/stream.ts b/src/stream.ts index e2e13cc..f94d068 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -128,7 +128,6 @@ export function streamQoder( const modelConfig = getCachedModelConfig(model.id, providerMode) || { key: model.id, is_reasoning: false, - max_output_tokens: 32768, source: "system", }; // Use the cached entry's original upstream key when available; fall back to @@ -136,7 +135,6 @@ export function streamQoder( const qoderModel = modelConfig.key || model.id; const isReasoning = !!modelConfig.is_reasoning; - const maxOutputTokens = modelConfig.max_output_tokens || 32768; const normalizedMessages = transformMessagesForQoder(context.messages); const systemText = context.systemPrompt || ""; @@ -163,10 +161,10 @@ export function streamQoder( ? `${stablePart}-${options.sessionId}` : `${stablePart}-${crypto.randomUUID()}`; + // Qoder's catalog exposes no per-model output cap (no max_output_tokens), + // so we default to 32K (matches qodercli's 32e3 fallback) and let pi cap it + // lower when the caller sets options.maxTokens (e.g. compaction at 40K). let maxTokens = 32768; - if (maxOutputTokens > 0) { - maxTokens = maxOutputTokens; - } if (options?.maxTokens && options.maxTokens < maxTokens) { maxTokens = options.maxTokens; } From 76c3b7201b0e2b773a6c912d1635afe3cbecaed6 Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 19:36:21 +0800 Subject: [PATCH 7/7] feat: raise max output tokens to 128K (documented upstream ceiling) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Aliyun Model Studio (the upstream behind Qoder's CN catalog) documents Max Output Length = 131072 for every model we expose, in both normal and thinking modes (thinking chain alone goes to 262144): qwen3.8-max/flash, qwen3.7-max/plus/flash — all 131072. The previous 32768 default (copied from qodercli's conservative 32e3) used only 1/4 of the supported output budget. That truncates reasoning chains in thinking mode (where the chain counts against output) and risks cutting long generations / compaction summaries. qodercli caps its own UI at 65536, still below the documented ceiling. - models.ts: introduce MAX_OUTPUT_TOKENS = 131072 as the single source of truth; all 24 staticModels entries + the lazy-loaded CN entry now reference it. Remove the dead max_output_tokens field from the CN placeholder entry. - stream.ts: maxTokens defaults to MAX_OUTPUT_TOKENS; still capped lower by options.maxTokens (e.g. pi compaction passes 40000). Verified against the real CN endpoint: max_tokens=131072 returns 200 / stop (accepted, not rejected); an xhigh reasoning run completes with finish_reason=stop (not truncated). Field placement + reasoning_effort send also confirmed working in the same request. --- src/models.ts | 64 ++++++++++++++++++++++++++++++--------------------- src/stream.ts | 12 ++++++---- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/src/models.ts b/src/models.ts index de9827a..f13019b 100644 --- a/src/models.ts +++ b/src/models.ts @@ -13,6 +13,19 @@ import { export const ZERO_COST = Object.freeze({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }); +/** + * Maximum output tokens sent per request. Aliyun Model Studio (the upstream + * behind Qoder's CN catalog) documents Max Output Length = 131072 for every + * model we expose (qwen3.8-max/flash, qwen3.7-max/plus/flash), in both normal + * and thinking modes (thinking chain alone goes up to 262144). The Qoder + * /model/list catalog does not return a per-model output cap, so this single + * constant is the source of truth for both static models and request sending. + * qodercli ships a conservative 32e3 default and caps its UI at 65536; we use + * the documented upstream ceiling so reasoning chains and long generations + * are not truncated. + */ +export const MAX_OUTPUT_TOKENS = 131072; + /** Shape of a single entry returned by the Qoder /model/list endpoint. */ export interface QoderModelEntry { key?: string; @@ -67,7 +80,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 180000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "ultimate", @@ -80,7 +93,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "performance", @@ -93,7 +106,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "efficient", @@ -106,7 +119,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 180000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "lite", @@ -119,7 +132,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 180000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "qmodel", @@ -132,7 +145,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "cmodel", @@ -145,7 +158,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "qmodel_preview", @@ -158,7 +171,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "qmodel_latest", @@ -171,7 +184,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "dmodel", @@ -184,7 +197,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "dfmodel", @@ -197,7 +210,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "gm51model", @@ -210,7 +223,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "kmodel", @@ -223,7 +236,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 256000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "kmodel_latest", @@ -236,7 +249,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, { id: "mmodel", @@ -249,7 +262,7 @@ export const staticModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }, ]; @@ -265,7 +278,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 200000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN smart routing; fallback context window of 200K.", }, { @@ -279,7 +292,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN qmodel_latest; context options 200K/400K/1M.", }, { @@ -293,7 +306,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN qmodel; context options 200K/400K/1M.", }, { @@ -307,7 +320,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN q36fmodel; context options 200K/400K/1M.", }, { @@ -321,7 +334,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN dmodel; context options 200K/400K/1M.", }, { @@ -335,7 +348,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 1000000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN dfmodel; context options 200K/400K/1M.", }, { @@ -349,7 +362,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 200000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN gm51model; live catalog currently displays GLM-5.2 with 200K context.", }, { @@ -363,7 +376,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text", "image"], cost: ZERO_COST, contextWindow: 256000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN kmodel; context option 256K.", }, { @@ -377,7 +390,7 @@ export const staticCnModels: QoderModelDef[] = [ input: ["text"], cost: ZERO_COST, contextWindow: 200000, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, description: "Qoder CN mmodel; live catalog reports 200K context.", }, ]; @@ -465,7 +478,6 @@ export function getCachedModelConfig(modelKey: string, mode?: string): QoderMode return { key: modelKey, is_reasoning: false, - max_output_tokens: 32768, source: "system", }; } @@ -588,7 +600,7 @@ export async function updateQoderModelsCache( input: isVL ? ["text", "image"] : ["text"], cost: ZERO_COST, contextWindow: ctxLen, - maxTokens: 32768, + maxTokens: MAX_OUTPUT_TOKENS, }); } diff --git a/src/stream.ts b/src/stream.ts index f94d068..3bf0022 100644 --- a/src/stream.ts +++ b/src/stream.ts @@ -20,7 +20,7 @@ import { getQoderUserEmailFallback, isQoderCNMode, } from "./cosy.js"; -import { getCachedModelConfig } from "./models.js"; +import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js"; import { getCachedCredentials } from "./oauth.js"; import { qoderEncodeBody } from "./qoder-encoding.js"; import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js"; @@ -161,10 +161,12 @@ export function streamQoder( ? `${stablePart}-${options.sessionId}` : `${stablePart}-${crypto.randomUUID()}`; - // Qoder's catalog exposes no per-model output cap (no max_output_tokens), - // so we default to 32K (matches qodercli's 32e3 fallback) and let pi cap it - // lower when the caller sets options.maxTokens (e.g. compaction at 40K). - let maxTokens = 32768; + // 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; }