Merge branch 'main' into fix/qoder-cn-login-windows-build
This commit is contained in:
+14
-76
@@ -3,8 +3,6 @@ import {
|
|||||||
getQoderBaseUrl,
|
getQoderBaseUrl,
|
||||||
getQoderCenterUrl,
|
getQoderCenterUrl,
|
||||||
getQoderChatURL,
|
getQoderChatURL,
|
||||||
getQoderCNDirectModel,
|
|
||||||
getQoderCNFriendlyModelInfo,
|
|
||||||
getQoderExchangeURL,
|
getQoderExchangeURL,
|
||||||
getQoderManageUrl,
|
getQoderManageUrl,
|
||||||
getQoderMode,
|
getQoderMode,
|
||||||
@@ -15,7 +13,7 @@ import {
|
|||||||
getQoderUserEmailFallback,
|
getQoderUserEmailFallback,
|
||||||
getQoderUserInfoURL,
|
getQoderUserInfoURL,
|
||||||
isQoderCNMode,
|
isQoderCNMode,
|
||||||
toQoderCNFriendlyModel,
|
toQoderCNModelId,
|
||||||
} from "../cosy.js";
|
} from "../cosy.js";
|
||||||
|
|
||||||
// ── getQoderMode ──────────────────────────────────────────────────────────
|
// ── getQoderMode ──────────────────────────────────────────────────────────
|
||||||
@@ -160,83 +158,23 @@ describe("getQoderUserEmailFallback", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ── getQoderCNDirectModel ─────────────────────────────────────────────────
|
// ── toQoderCNModelId ──────────────────────────────────────────────────────
|
||||||
|
|
||||||
describe("getQoderCNDirectModel", () => {
|
describe("toQoderCNModelId", () => {
|
||||||
it("maps known model IDs to internal keys", () => {
|
it("strips whitespace from the upstream display_name", () => {
|
||||||
expect(getQoderCNDirectModel("qoder-cn")).toBe("auto");
|
// The display name is used directly as the pi-visible model id, with
|
||||||
expect(getQoderCNDirectModel("qwen3.7-max")).toBe("qmodel_latest");
|
// whitespace removed so it stays a clean token for persistence keys and
|
||||||
expect(getQoderCNDirectModel("qwen3.7-plus")).toBe("qmodel");
|
// search. "Qwen3.8-Flash" has no spaces already.
|
||||||
expect(getQoderCNDirectModel("qwen3.6-plus")).toBe("qmodel");
|
expect(toQoderCNModelId("Qwen3.8-Flash")).toBe("Qwen3.8-Flash");
|
||||||
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");
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns the input ID for unknown models", () => {
|
it("collapses internal spaces", () => {
|
||||||
expect(getQoderCNDirectModel("custom-model")).toBe("custom-model");
|
expect(toQoderCNModelId("Qwen 3.8 Max")).toBe("Qwen3.8Max");
|
||||||
|
expect(toQoderCNModelId("DeepSeek V4 Pro")).toBe("DeepSeekV4Pro");
|
||||||
});
|
});
|
||||||
|
|
||||||
it('defaults to "auto" when no input', () => {
|
it("falls back to a default when no display name is given", () => {
|
||||||
expect(getQoderCNDirectModel()).toBe("auto");
|
expect(toQoderCNModelId()).toBe("QoderCNModel");
|
||||||
expect(getQoderCNDirectModel("")).toBe("auto");
|
expect(toQoderCNModelId("")).toBe("QoderCNModel");
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
// ── 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");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -43,8 +43,8 @@ describe("staticCnModels", () => {
|
|||||||
expect(staticCnModels.length).toBeGreaterThan(0);
|
expect(staticCnModels.length).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("has auto as first entry", () => {
|
it("has Auto as first entry", () => {
|
||||||
expect(staticCnModels[0].id).toBe("auto");
|
expect(staticCnModels[0].id).toBe("Auto");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("every CN model has required fields", () => {
|
it("every CN model has required fields", () => {
|
||||||
|
|||||||
@@ -290,4 +290,58 @@ describe("streamQoder", () => {
|
|||||||
expect(msg.content.find((c) => c.type === "toolCall")).toBeUndefined();
|
expect(msg.content.find((c) => c.type === "toolCall")).toBeUndefined();
|
||||||
expect(msg.stopReason).toBe("stop");
|
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<Uint8Array>({
|
||||||
|
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<Uint8Array>({
|
||||||
|
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");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -348,4 +348,56 @@ describe("transformMessagesForQoder", () => {
|
|||||||
expect(tool.role).toBe("tool");
|
expect(tool.role).toBe("tool");
|
||||||
expect(tool.tool_call_id).toBe("call_abc123");
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
+14
-56
@@ -113,62 +113,20 @@ export function getQoderRefreshURL(mode?: string): string {
|
|||||||
return `${getQoderCenterUrl(mode)}/algo/api/v3/user/refresh_token`;
|
return `${getQoderCenterUrl(mode)}/algo/api/v3/user/refresh_token`;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getQoderCNDirectModel(modelID?: string): string {
|
/**
|
||||||
return (
|
* Derive the model `id` pi exposes in the picker from Qoder's `display_name`.
|
||||||
{
|
*
|
||||||
"qoder-cn": "auto",
|
* The picker renders `Model.id`, not `Model.name`, so the display name is used
|
||||||
"qwen3.7-max": "qmodel_latest",
|
* directly as the id (with whitespace stripped so it stays a clean token for
|
||||||
"qwen3.7-plus": "qmodel",
|
* persistence keys, logs, and search). The original upstream `key` (e.g.
|
||||||
"qwen3.6-plus": "qmodel",
|
* `qfmodel`) is NOT used as the id: it is kept only as the request-time
|
||||||
"qwen3.6-flash": "q36fmodel",
|
* identifier, read back from the cached model config at send time. This drops
|
||||||
"deepseek-v4-pro": "dmodel",
|
* the hardcoded key<->friendlyId mapping tables entirely — the label now
|
||||||
"deepseek-v4-flash": "dfmodel",
|
* tracks whatever upstream returns, so a renamed/upgraded model is shown
|
||||||
"glm-5.2": "gm51model",
|
* correctly without a code change.
|
||||||
"glm-5.1": "gm51model",
|
*/
|
||||||
"kimi-k2.6": "kmodel",
|
export function toQoderCNModelId(displayName?: string): string {
|
||||||
"minimax-m2.7": "mmodel",
|
return (displayName || "QoderCNModel").replace(/\s+/g, "");
|
||||||
"minimax-m3": "mmodel",
|
|
||||||
}[modelID || ""] ||
|
|
||||||
modelID ||
|
|
||||||
"auto"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const qoderCNFriendlyModels: Record<string, { id: string; name: string }> = {
|
|
||||||
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<T extends { id: string; name: string }>(model: T): T {
|
|
||||||
const info = getQoderCNFriendlyModelInfo(model.id, model.name);
|
|
||||||
return {
|
|
||||||
...model,
|
|
||||||
id: info.id,
|
|
||||||
name: info.name,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getQoderManageUrl(mode?: string): string {
|
export function getQoderManageUrl(mode?: string): string {
|
||||||
|
|||||||
+6
-15
@@ -1,12 +1,6 @@
|
|||||||
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
import type { Api, Model, OAuthCredentials } from "@earendil-works/pi-ai";
|
||||||
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
import type { ExtensionAPI, ProviderConfig } from "@earendil-works/pi-coding-agent";
|
||||||
import {
|
import { getQoderBaseUrl, getQoderMode, getQoderUserEmailFallback, isQoderCNMode } from "./cosy.js";
|
||||||
getQoderBaseUrl,
|
|
||||||
getQoderMode,
|
|
||||||
getQoderUserEmailFallback,
|
|
||||||
isQoderCNMode,
|
|
||||||
toQoderCNFriendlyModel,
|
|
||||||
} from "./cosy.js";
|
|
||||||
import { getCachedModels, isCacheStale, staticCnModels, staticModels, updateQoderModelsCache } from "./models.js";
|
import { getCachedModels, isCacheStale, staticCnModels, staticModels, updateQoderModelsCache } from "./models.js";
|
||||||
import {
|
import {
|
||||||
autoLoginQoderFromEnvironment,
|
autoLoginQoderFromEnvironment,
|
||||||
@@ -29,14 +23,11 @@ function modelsForProvider(mode: string, providerID: string): Model<Api>[] {
|
|||||||
const cached = getCachedModels(mode);
|
const cached = getCachedModels(mode);
|
||||||
const modelsToUse = cached.length > 0 ? cached : isQoderCNMode(mode) ? staticCnModels : staticModels;
|
const modelsToUse = cached.length > 0 ? cached : isQoderCNMode(mode) ? staticCnModels : staticModels;
|
||||||
|
|
||||||
return modelsToUse.map((m) => {
|
return modelsToUse.map((m) => ({
|
||||||
const model = isQoderCNMode(mode) ? toQoderCNFriendlyModel(m) : m;
|
...m,
|
||||||
return {
|
provider: providerID,
|
||||||
...model,
|
baseUrl: getQoderBaseUrl(mode),
|
||||||
provider: providerID,
|
})) as unknown as Model<Api>[];
|
||||||
baseUrl: getQoderBaseUrl(mode),
|
|
||||||
};
|
|
||||||
}) as unknown as Model<Api>[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createQoderOAuth(providerID: string, mode: string): OAuthConfigWithUsage {
|
function createQoderOAuth(providerID: string, mode: string): OAuthConfigWithUsage {
|
||||||
|
|||||||
+196
-138
@@ -1,28 +1,44 @@
|
|||||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||||
import { homedir } from "node:os";
|
import { homedir } from "node:os";
|
||||||
import { dirname, join } from "node:path";
|
import { dirname, join } from "node:path";
|
||||||
|
import type { ThinkingLevel, ThinkingLevelMap } from "@earendil-works/pi-ai";
|
||||||
import {
|
import {
|
||||||
buildAuthHeaders,
|
buildAuthHeaders,
|
||||||
getQoderBaseUrl,
|
getQoderBaseUrl,
|
||||||
getQoderCNFriendlyModelInfo,
|
|
||||||
getQoderMode,
|
getQoderMode,
|
||||||
getQoderModelListURL,
|
getQoderModelListURL,
|
||||||
isQoderCNMode,
|
isQoderCNMode,
|
||||||
|
toQoderCNModelId,
|
||||||
} from "./cosy.js";
|
} from "./cosy.js";
|
||||||
|
|
||||||
export const ZERO_COST = Object.freeze({ input: 0, output: 0, cacheRead: 0, cacheWrite: 0 });
|
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. */
|
/** Shape of a single entry returned by the Qoder /model/list endpoint. */
|
||||||
export interface QoderModelEntry {
|
export interface QoderModelEntry {
|
||||||
key?: string;
|
key?: string;
|
||||||
enable?: boolean;
|
enable?: boolean;
|
||||||
display_name?: string;
|
display_name?: string;
|
||||||
max_input_tokens?: number;
|
max_input_tokens?: number;
|
||||||
max_output_tokens?: number;
|
|
||||||
context_config?: Record<string, { token_count?: number; is_default?: boolean }>;
|
context_config?: Record<string, { token_count?: number; is_default?: boolean }>;
|
||||||
is_vl?: boolean;
|
is_vl?: boolean;
|
||||||
is_reasoning?: boolean;
|
is_reasoning?: boolean;
|
||||||
thinking_config?: { enabled?: { efforts?: unknown } };
|
thinking_config?: {
|
||||||
|
disabled?: unknown;
|
||||||
|
enabled?: { efforts?: Record<string, { is_default?: boolean }>; is_default?: boolean };
|
||||||
|
};
|
||||||
source?: string;
|
source?: string;
|
||||||
[key: string]: unknown;
|
[key: string]: unknown;
|
||||||
}
|
}
|
||||||
@@ -35,6 +51,7 @@ export interface QoderModelDef {
|
|||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
reasoning: boolean;
|
reasoning: boolean;
|
||||||
supportsEffort: boolean;
|
supportsEffort: boolean;
|
||||||
|
thinkingLevelMap?: ThinkingLevelMap;
|
||||||
input: ("text" | "image")[];
|
input: ("text" | "image")[];
|
||||||
cost: typeof ZERO_COST;
|
cost: typeof ZERO_COST;
|
||||||
contextWindow: number;
|
contextWindow: number;
|
||||||
@@ -63,7 +80,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 180000,
|
contextWindow: 180000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "ultimate",
|
id: "ultimate",
|
||||||
@@ -76,7 +93,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "performance",
|
id: "performance",
|
||||||
@@ -89,7 +106,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "efficient",
|
id: "efficient",
|
||||||
@@ -102,7 +119,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 180000,
|
contextWindow: 180000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "lite",
|
id: "lite",
|
||||||
@@ -115,7 +132,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text"],
|
input: ["text"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 180000,
|
contextWindow: 180000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "qmodel",
|
id: "qmodel",
|
||||||
@@ -128,7 +145,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "cmodel",
|
id: "cmodel",
|
||||||
@@ -141,7 +158,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "qmodel_preview",
|
id: "qmodel_preview",
|
||||||
@@ -154,7 +171,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "qmodel_latest",
|
id: "qmodel_latest",
|
||||||
@@ -167,7 +184,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "dmodel",
|
id: "dmodel",
|
||||||
@@ -180,7 +197,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "dfmodel",
|
id: "dfmodel",
|
||||||
@@ -193,7 +210,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "gm51model",
|
id: "gm51model",
|
||||||
@@ -206,7 +223,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "kmodel",
|
id: "kmodel",
|
||||||
@@ -219,7 +236,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 256000,
|
contextWindow: 256000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "kmodel_latest",
|
id: "kmodel_latest",
|
||||||
@@ -232,7 +249,7 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "mmodel",
|
id: "mmodel",
|
||||||
@@ -245,98 +262,14 @@ export const staticModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 1000000,
|
contextWindow: 1000000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const staticCnModels: QoderModelDef[] = [
|
export const staticCnModels: QoderModelDef[] = [
|
||||||
{
|
{
|
||||||
id: "auto",
|
id: "Auto",
|
||||||
name: "Auto · Qoder CN",
|
name: "Auto",
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: true,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text", "image"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 180000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN smart routing; live catalog reports 180K max input.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "qwen3.7-max",
|
|
||||||
name: "Qwen 3.7 Max · Qoder CN",
|
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: true,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text", "image"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 1000000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN qmodel_latest; context options 200K/400K/1M.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "qwen3.7-plus",
|
|
||||||
name: "Qwen 3.7 Plus · Qoder CN",
|
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: true,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 1000000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN qmodel; context options 200K/400K/1M.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "qwen3.6-flash",
|
|
||||||
name: "Qwen 3.6 Flash · Qoder CN",
|
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: true,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 1000000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN q36fmodel; context options 200K/400K/1M.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "deepseek-v4-pro",
|
|
||||||
name: "DeepSeek V4 Pro · Qoder CN",
|
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: true,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 1000000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN dmodel; context options 200K/400K/1M.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "deepseek-v4-flash",
|
|
||||||
name: "DeepSeek V4 Flash · Qoder CN",
|
|
||||||
api: "qoder-api",
|
|
||||||
provider: "qoder-cn",
|
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
|
||||||
reasoning: false,
|
|
||||||
supportsEffort: false,
|
|
||||||
input: ["text"],
|
|
||||||
cost: ZERO_COST,
|
|
||||||
contextWindow: 1000000,
|
|
||||||
maxTokens: 32768,
|
|
||||||
description: "Qoder CN dfmodel; context options 200K/400K/1M.",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "glm-5.2",
|
|
||||||
name: "GLM 5.2 · Qoder CN",
|
|
||||||
api: "qoder-api",
|
api: "qoder-api",
|
||||||
provider: "qoder-cn",
|
provider: "qoder-cn",
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
@@ -345,12 +278,96 @@ export const staticCnModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 200000,
|
contextWindow: 200000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN smart routing; fallback context window of 200K.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "Qwen3.7-Max",
|
||||||
|
name: "Qwen3.7-Max",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: true,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text", "image"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 1000000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN qmodel_latest; context options 200K/400K/1M.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "Qwen3.7-Plus",
|
||||||
|
name: "Qwen3.7-Plus",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: true,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 1000000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN qmodel; context options 200K/400K/1M.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "Qwen3.6-Flash",
|
||||||
|
name: "Qwen3.6-Flash",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: true,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 1000000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN q36fmodel; context options 200K/400K/1M.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "DeepSeek-V4-Pro",
|
||||||
|
name: "DeepSeek-V4-Pro",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: true,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 1000000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN dmodel; context options 200K/400K/1M.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "DeepSeek-V4-Flash",
|
||||||
|
name: "DeepSeek-V4-Flash",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: false,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 1000000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
|
description: "Qoder CN dfmodel; context options 200K/400K/1M.",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "GLM-5.2",
|
||||||
|
name: "GLM-5.2",
|
||||||
|
api: "qoder-api",
|
||||||
|
provider: "qoder-cn",
|
||||||
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
|
reasoning: true,
|
||||||
|
supportsEffort: false,
|
||||||
|
input: ["text", "image"],
|
||||||
|
cost: ZERO_COST,
|
||||||
|
contextWindow: 200000,
|
||||||
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
description: "Qoder CN gm51model; live catalog currently displays GLM-5.2 with 200K context.",
|
description: "Qoder CN gm51model; live catalog currently displays GLM-5.2 with 200K context.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "kimi-k2.6",
|
id: "Kimi-K2.7-Code",
|
||||||
name: "Kimi K2.6 · Qoder CN",
|
name: "Kimi-K2.7-Code",
|
||||||
api: "qoder-api",
|
api: "qoder-api",
|
||||||
provider: "qoder-cn",
|
provider: "qoder-cn",
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
@@ -359,12 +376,12 @@ export const staticCnModels: QoderModelDef[] = [
|
|||||||
input: ["text", "image"],
|
input: ["text", "image"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 256000,
|
contextWindow: 256000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
description: "Qoder CN kmodel; context option 256K.",
|
description: "Qoder CN kmodel; context option 256K.",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: "minimax-m2.7",
|
id: "MiniMax-M2.7",
|
||||||
name: "MiniMax M2.7 · Qoder CN",
|
name: "MiniMax-M2.7",
|
||||||
api: "qoder-api",
|
api: "qoder-api",
|
||||||
provider: "qoder-cn",
|
provider: "qoder-cn",
|
||||||
baseUrl: getQoderBaseUrl("cn"),
|
baseUrl: getQoderBaseUrl("cn"),
|
||||||
@@ -373,11 +390,57 @@ export const staticCnModels: QoderModelDef[] = [
|
|||||||
input: ["text"],
|
input: ["text"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: 200000,
|
contextWindow: 200000,
|
||||||
maxTokens: 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
description: "Qoder CN mmodel; live catalog reports 200K context.",
|
description: "Qoder CN mmodel; live catalog reports 200K context.",
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
/** 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[] {
|
export function getCachedModels(mode?: string): QoderModelDef[] {
|
||||||
const cachePath = getQoderCachePath(mode);
|
const cachePath = getQoderCachePath(mode);
|
||||||
if (existsSync(cachePath)) {
|
if (existsSync(cachePath)) {
|
||||||
@@ -407,30 +470,14 @@ export function getCachedModelConfig(modelKey: string, mode?: string): QoderMode
|
|||||||
} catch {}
|
} 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)) {
|
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 {
|
return {
|
||||||
key: modelKey,
|
key: modelKey,
|
||||||
is_reasoning: reasoningModels.has(modelKey),
|
is_reasoning: false,
|
||||||
max_output_tokens: 32768,
|
|
||||||
source: "system",
|
source: "system",
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -512,13 +559,17 @@ export async function updateQoderModelsCache(
|
|||||||
if (!key || !entry.enable) continue;
|
if (!key || !entry.enable) continue;
|
||||||
|
|
||||||
const display = entry.display_name || key;
|
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") {
|
if (entry.context_config && typeof entry.context_config === "object") {
|
||||||
for (const configVal of Object.values(entry.context_config)) {
|
for (const configVal of Object.values(entry.context_config)) {
|
||||||
if (configVal && typeof configVal === "object" && typeof configVal.token_count === "number") {
|
if (configVal && typeof configVal === "object" && typeof configVal.token_count === "number") {
|
||||||
const tc = configVal.token_count;
|
if (configVal.token_count > ctxLen) {
|
||||||
if (tc > ctxLen) {
|
ctxLen = configVal.token_count;
|
||||||
ctxLen = tc;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -526,7 +577,13 @@ export async function updateQoderModelsCache(
|
|||||||
const isVL = !!entry.is_vl;
|
const isVL = !!entry.is_vl;
|
||||||
const isReasoning = !!entry.is_reasoning || !!entry.thinking_config;
|
const isReasoning = !!entry.is_reasoning || !!entry.thinking_config;
|
||||||
const supportsEffort = !!entry.thinking_config?.enabled?.efforts;
|
const supportsEffort = !!entry.thinking_config?.enabled?.efforts;
|
||||||
const modelInfo = isQoderCNMode(mode) ? getQoderCNFriendlyModelInfo(key, display) : { id: key, name: display };
|
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.
|
||||||
|
const modelInfo = isQoderCNMode(mode)
|
||||||
|
? { id: toQoderCNModelId(display), name: display }
|
||||||
|
: { id: key, name: display };
|
||||||
|
|
||||||
configs[key] = entry;
|
configs[key] = entry;
|
||||||
if (modelInfo.id !== key) configs[modelInfo.id] = entry;
|
if (modelInfo.id !== key) configs[modelInfo.id] = entry;
|
||||||
@@ -539,10 +596,11 @@ export async function updateQoderModelsCache(
|
|||||||
baseUrl: getQoderBaseUrl(mode),
|
baseUrl: getQoderBaseUrl(mode),
|
||||||
reasoning: isReasoning,
|
reasoning: isReasoning,
|
||||||
supportsEffort,
|
supportsEffort,
|
||||||
|
thinkingLevelMap,
|
||||||
input: isVL ? ["text", "image"] : ["text"],
|
input: isVL ? ["text", "image"] : ["text"],
|
||||||
cost: ZERO_COST,
|
cost: ZERO_COST,
|
||||||
contextWindow: ctxLen,
|
contextWindow: ctxLen,
|
||||||
maxTokens: entry.max_output_tokens || 32768,
|
maxTokens: MAX_OUTPUT_TOKENS,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+85
-32
@@ -1,27 +1,27 @@
|
|||||||
import crypto from "node:crypto";
|
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 * 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 {
|
import {
|
||||||
buildAuthHeaders,
|
buildAuthHeaders,
|
||||||
getMachineId,
|
getMachineId,
|
||||||
getQoderChatURL,
|
getQoderChatURL,
|
||||||
getQoderCNDirectModel,
|
|
||||||
getQoderMode,
|
getQoderMode,
|
||||||
getQoderUserEmailFallback,
|
getQoderUserEmailFallback,
|
||||||
isQoderCNMode,
|
isQoderCNMode,
|
||||||
} from "./cosy.js";
|
} from "./cosy.js";
|
||||||
import { getCachedModelConfig } from "./models.js";
|
import { resolveQoderIdentity, getCachedCredentials } from "./oauth.js";
|
||||||
import { resolveQoderIdentity } from "./oauth.js";
|
import { getCachedModelConfig, MAX_OUTPUT_TOKENS } from "./models.js";
|
||||||
import { qoderEncodeBody } from "./qoder-encoding.js";
|
import { qoderEncodeBody } from "./qoder-encoding.js";
|
||||||
import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js";
|
import { stripThinkingTags, ThinkingTagParser } from "./thinking-parser.js";
|
||||||
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
import { transformMessagesForQoder, transformTools } from "./transform.js";
|
||||||
@@ -138,21 +138,21 @@ export function streamQoder(
|
|||||||
const email = ident.email || getQoderUserEmailFallback(providerMode);
|
const email = ident.email || getQoderUserEmailFallback(providerMode);
|
||||||
const machineID = ident.machineID || getMachineId();
|
const machineID = ident.machineID || getMachineId();
|
||||||
|
|
||||||
const qoderModel = isQoderCNMode(providerMode) ? getQoderCNDirectModel(model.id) : model.id;
|
// The model `id` pi exposes is the upstream display_name (whitespace
|
||||||
const modelConfig = getCachedModelConfig(qoderModel, providerMode) || {
|
// stripped) for CN, or the raw key for the international site. The
|
||||||
key: qoderModel,
|
// request-time upstream `key` is read back from the cached model config
|
||||||
is_reasoning:
|
// (which stores the original entry keyed by both `key` and `id`), so no
|
||||||
qoderModel === "ultimate" ||
|
// key<->friendlyId mapping table is needed here.
|
||||||
qoderModel === "performance" ||
|
const modelConfig = getCachedModelConfig(model.id, providerMode) || {
|
||||||
qoderModel.includes("dmodel") ||
|
key: model.id,
|
||||||
qoderModel.includes("dfmodel"),
|
is_reasoning: false,
|
||||||
max_output_tokens: 32768,
|
|
||||||
source: "system",
|
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 isReasoning = !!modelConfig.is_reasoning;
|
||||||
const maxOutputTokens = modelConfig.max_output_tokens || 32768;
|
|
||||||
|
|
||||||
const normalizedMessages = transformMessagesForQoder(context.messages);
|
const normalizedMessages = transformMessagesForQoder(context.messages);
|
||||||
// OMP may supply the system prompt as a single-element content array;
|
// OMP may supply the system prompt as a single-element content array;
|
||||||
@@ -182,10 +182,12 @@ export function streamQoder(
|
|||||||
? `${stablePart}-${options.sessionId}`
|
? `${stablePart}-${options.sessionId}`
|
||||||
: `${stablePart}-${crypto.randomUUID()}`;
|
: `${stablePart}-${crypto.randomUUID()}`;
|
||||||
|
|
||||||
let maxTokens = 32768;
|
// Qoder's catalog exposes no per-model output cap, so we use the
|
||||||
if (maxOutputTokens > 0) {
|
// documented upstream ceiling (MAX_OUTPUT_TOKENS = 131072, see models.ts)
|
||||||
maxTokens = maxOutputTokens;
|
// 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) {
|
if (options?.maxTokens && options.maxTokens < maxTokens) {
|
||||||
maxTokens = options.maxTokens;
|
maxTokens = options.maxTokens;
|
||||||
}
|
}
|
||||||
@@ -193,6 +195,38 @@ export function streamQoder(
|
|||||||
const toolsRaw = context.tools && context.tools.length > 0 ? transformTools(context.tools) : undefined;
|
const toolsRaw = context.tools && context.tools.length > 0 ? transformTools(context.tools) : undefined;
|
||||||
const recordID = stableChatRecordID(qoderModel, normalizedMessages, toolsRaw, maxTokens);
|
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> = {
|
const reqBody: Record<string, unknown> = {
|
||||||
request_id: crypto.randomUUID(),
|
request_id: crypto.randomUUID(),
|
||||||
request_set_id: recordID,
|
request_set_id: recordID,
|
||||||
@@ -217,7 +251,7 @@ export function streamQoder(
|
|||||||
system: "",
|
system: "",
|
||||||
messages: systemText ? [{ role: "system", content: systemText }, ...normalizedMessages] : normalizedMessages,
|
messages: systemText ? [{ role: "system", content: systemText }, ...normalizedMessages] : normalizedMessages,
|
||||||
tools: toolsRaw || [],
|
tools: toolsRaw || [],
|
||||||
parameters: { max_tokens: maxTokens },
|
parameters,
|
||||||
chat_context: {
|
chat_context: {
|
||||||
chatPrompt: "",
|
chatPrompt: "",
|
||||||
imageUrls: null,
|
imageUrls: null,
|
||||||
@@ -295,7 +329,14 @@ export function streamQoder(
|
|||||||
|
|
||||||
stream.push({ type: "start", partial: output });
|
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();
|
const { done, value } = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
|
|
||||||
@@ -312,6 +353,7 @@ export function streamQoder(
|
|||||||
|
|
||||||
const dataStr = line.substring(5).trim();
|
const dataStr = line.substring(5).trim();
|
||||||
if (dataStr === "[DONE]") {
|
if (dataStr === "[DONE]") {
|
||||||
|
sawDone = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -322,7 +364,14 @@ export function streamQoder(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const innerStr = envelope.body;
|
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);
|
const inner = JSON.parse(innerStr);
|
||||||
if (inner.id) output.responseId = inner.id as string;
|
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) {
|
if (thinkingParser) {
|
||||||
thinkingParser.finalize();
|
thinkingParser.finalize();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,12 +72,33 @@ export function transformTools(tools: Tool[]): QoderTool[] {
|
|||||||
export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
export function transformMessagesForQoder(messages: Message[]): QoderMessage[] {
|
||||||
const normalizedMessages: 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<string>();
|
||||||
|
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
// Skip error or aborted messages
|
// Skip error or aborted messages
|
||||||
if (
|
if (
|
||||||
msg.role === "assistant" &&
|
msg.role === "assistant" &&
|
||||||
((msg as AssistantMessage).stopReason === "error" || (msg as AssistantMessage).stopReason === "aborted")
|
((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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user