From b462207447100c91a54e9fe15c263ce9bce6e95c Mon Sep 17 00:00:00 2001 From: Loongphy Date: Fri, 28 Aug 2026 08:16:36 +0800 Subject: [PATCH] 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;