From c25201d259c40107c856e62d4ece098ba17f837d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=B7=8D=E5=B3=B0?= Date: Mon, 26 Oct 2026 22:45:27 +0800 Subject: [PATCH] qoder 0.2.38: a reply names the model-list entry it was sent with (#413) Qoder and Qoder CN chat replies name the model-list entry the request was built from (m.name), in JSON and every SSE chunk, usage included. A blank name, or one equal to the slot key, keeps the requested id. No extra model-list fetch per request; keys, X-Model-Key and model_config unchanged. Run at the reviewed head 6f9813f7bb44ea243007c1da2495d69de6be2de9: - bun test in packages/qoder: 266 pass, 0 fail. - model-identity.test.mjs against main's index.mjs: 28 of 28 fail; at the head they pass. - Merges cleanly onto main; npm has 0.2.37, this is 0.2.38. Not tried: a real Qoder account. Closes #412 once the reporter confirms. --- packages/qoder/README.md | 15 ++++ packages/qoder/index.mjs | 10 ++- packages/qoder/model-identity.test.mjs | 102 +++++++++++++++++++++++++ packages/qoder/package.json | 2 +- 4 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 packages/qoder/model-identity.test.mjs diff --git a/packages/qoder/README.md b/packages/qoder/README.md index e1c1c55..c5183ed 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -166,6 +166,21 @@ itself. ## Models +Chat responses report the display name from the same model-list entry +used to build the request, in both JSON completions and every SSE chunk +(including usage). A blank name or a name equal to the slot key falls +back to the requested model ID. This shared behavior applies to Qoder, +Qoder CN, QwenWork and QwenWork Enterprise, including tier labels. + +Reporting the name does not fetch the model list again. The existing +cache and model-list refresh behavior remain in place: a model missing +from the cache still forces a refresh, and normal list refreshes update +the configuration and name together. Model-list keys, API IDs, +`X-Model-Key` and `model_config` are unchanged; slots with the same display +name remain independently selectable. The name describes the catalog +entry used for the request, not an attestation of the backend that +executed it. + The `provider.models` hook reads the account's own list, as Qoder's client asks for it (`/algo/api/v2/model/list`). It keeps the enabled models of the listing's `app` scene, the list Qoder's app shows and the one a request diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 95cdd44..ca9c647 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -1685,7 +1685,7 @@ async function opened(res, tools) { return { it, first: await it.next() } } -async function answer({ it, first }, chat) { +async function answer({ it, first }, chat, servedName = chat.model) { const id = "chatcmpl-" + hexID() const created = Math.floor(Date.now() / 1000) if (first.value?.error) return errorResponse(first.value.error) @@ -1708,12 +1708,12 @@ async function answer({ it, first }, chat) { } if (reasoning) msg.reasoning_content = reasoning if (calls.length) msg.tool_calls = calls - return Response.json({ id, object: "chat.completion", created, model: chat.model, choices: [{ index: 0, message: msg, finish_reason: stop }], ...(usage ? { usage } : {}) }) + return Response.json({ id, object: "chat.completion", created, model: servedName, choices: [{ index: 0, message: msg, finish_reason: stop }], ...(usage ? { usage } : {}) }) } const enc = new TextEncoder() const chunk = (delta, finish_reason = null, extra = {}) => - enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model: chat.model, choices: [{ index: 0, delta, finish_reason }], ...extra })}\n\n`) + enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model: servedName, choices: [{ index: 0, delta, finish_reason }], ...extra })}\n\n`) let pending = first let began = false const stream = new ReadableStream({ @@ -2555,6 +2555,8 @@ const makePlugin = (site) => async ({ client }) => { return errorResponse({ status: 502, message: e?.message ?? String(e) }) } if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` }) + const name = m.name?.trim() + const servedName = name && name !== m.key ? name : chat.model const api = siteOf(site, cred).api const CHAT_URL = api + CHAT_PATH const task = taskOf(sessionOf(chat, named), chat) @@ -2582,7 +2584,7 @@ const makePlugin = (site) => async ({ client }) => { const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error // an answered call moves its task on, as Qoder's client moves it if (!f) task.answered = true - if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat) + if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat, servedName) try { if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f) } catch (e) { diff --git a/packages/qoder/model-identity.test.mjs b/packages/qoder/model-identity.test.mjs new file mode 100644 index 0000000..b86d4d4 --- /dev/null +++ b/packages/qoder/model-identity.test.mjs @@ -0,0 +1,102 @@ +// Request-time labels preserve stable routing IDs on the shared Qoder protocol. +import { afterEach, expect, test } from 'bun:test' +import { QoderAuthPlugin, QoderCNAuthPlugin, QwenWorkAuthPlugin, QwenWorkEnterpriseAuthPlugin, _internal } from './index.mjs' +const originalFetch = globalThis.fetch +afterEach(() => { globalThis.fetch = originalFetch }) +const catalog = { kmodel_latest: 'Kimi-K3', gmodel: 'GLM-5.3', qmodel_latest: 'Qwen3.8-Max', qmodel_38max: 'Qwen3.8-Max', dfmodel: 'DeepSeek-Flash', mmodel: 'MiniMax-M3.1-Flash', ultimate: 'Ultimate', future_slot: 'Future Model' } +async function fixture(create) { + const names = { ...catalog } + let available = true + let listingCalls = 0 + const sent = [] + globalThis.fetch = async (url, init) => { + if (new URL(String(url)).pathname.endsWith('/model/list')) { + listingCalls++ + if (!available) return new Response('', { status: 502 }) + const models = Object.entries(names).map(([key, display_name]) => ({ key, display_name, enable: true, max_input_tokens: 131072 })) + return Response.json({ chat: models, qwork: models }) + } + if (String(url).includes('agent_chat_generation')) { + sent.push({ key: new Headers(init.headers).get('X-Model-Key'), body: JSON.parse(_internal.decodeBody(init.body)) }) + const chunk = { model: 'auto', choices: [{ delta: { content: 'OK' }, finish_reason: 'stop' }], usage: { prompt_tokens: 100, completion_tokens: 10 } } + return new Response('data: ' + JSON.stringify({ body: JSON.stringify(chunk) }) + '\n\ndata: ' + JSON.stringify({ body: '[DONE]' }) + '\n\n', { headers: { 'content-type': 'text/event-stream' } }) + } + throw new Error('Unexpected upstream URL') + } + const auth = { type: 'oauth', access: 'test-token', refresh: 'test-refresh', expires: Date.now() + 3600000, uid: 'test-user', machineId: 'test-machine' } + if (create === QwenWorkEnterpriseAuthPlugin) auth.enterpriseHost = 'acme.example.com' + const hooks = await create({ client: { auth: { set: async () => {} } } }) + const loader = await hooks.auth.loader(async () => auth) + const ask = (model, stream = false) => loader.fetch('https://test.invalid/chat/completions', { method: 'POST', body: JSON.stringify({ model, stream, messages: [{ role: 'user', content: 'OK' }] }) }) + return { ask, sent, names, hooks, auth, listingCalls: () => listingCalls, refresh: () => hooks.provider.models({ models: {} }, { auth }), fail: () => { available = false } } +} +for (const [site, create] of [['qoder', QoderAuthPlugin], ['qoder-cn', QoderCNAuthPlugin], ['qwenwork', QwenWorkAuthPlugin], ['qwenwork-enterprise', QwenWorkEnterpriseAuthPlugin]]) { + for (const stream of [false, true]) { + test(`${site}: all model labels, duplicate labels and tiers; stream=${stream}`, async () => { + const f = await fixture(create) + for (const [key, name] of Object.entries(catalog)) { + const response = await f.ask(key, stream) + expect(response.status).toBe(200) + const events = stream ? (await response.text()).split('\n').filter(x => x.startsWith('data: ') && x !== 'data: [DONE]').map(x => JSON.parse(x.slice(6))) : [await response.json()] + expect(events.every(x => x.model === name)).toBe(true) + expect(events.at(-1).usage).toMatchObject({ prompt_tokens: 100, completion_tokens: 10 }) + expect(f.sent.at(-1).key).toBe(key) + expect(JSON.stringify(f.sent.at(-1).body)).toContain(key) + } + expect(Object.keys(await f.hooks.provider.models({ models: {} }, { auth: f.auth }))).toEqual(Object.keys(catalog)) + }) + } + test(`${site}: a cached chat does not fetch the model list again`, async () => { + const f = await fixture(create) + await f.refresh() + expect(f.listingCalls()).toBe(1) + for (let i = 0; i < 2; i++) { + expect((await (await f.ask('gmodel')).json()).model).toBe('GLM-5.3') + expect(f.listingCalls()).toBe(1) + } + }) + test(`${site}: the reported name and model_config follow the same cached entry`, async () => { + const f = await fixture(create) + expect((await (await f.ask('gmodel')).json()).model).toBe('GLM-5.3') + expect(f.listingCalls()).toBe(1) + f.names.gmodel = 'GLM-Next' + expect((await (await f.ask('gmodel')).json()).model).toBe('GLM-5.3') + expect(f.sent.at(-1).body.model_config.display_name).toBe('GLM-5.3') + expect(f.listingCalls()).toBe(1) + await f.refresh() + expect((await (await f.ask('gmodel')).json()).model).toBe('GLM-Next') + expect(f.sent.at(-1).body.model_config.display_name).toBe('GLM-Next') + expect(f.listingCalls()).toBe(2) + }) + test(`${site}: a missing cached model still forces exactly one refresh`, async () => { + const f = await fixture(create) + await f.refresh() + f.names.new_slot = 'New Model' + expect((await (await f.ask('new_slot')).json()).model).toBe('New Model') + expect(f.listingCalls()).toBe(2) + expect(f.sent.at(-1).key).toBe('new_slot') + expect(f.sent.at(-1).body.model_config.display_name).toBe('New Model') + }) + test(`${site}: empty, missing or unchanged names fall back to the slot ID`, async () => { + const f = await fixture(create) + for (const name of ['', ' ', undefined, 'gmodel']) { + f.names.gmodel = name + await f.refresh() + expect((await (await f.ask('gmodel')).json()).model).toBe('gmodel') + } + f.names.gmodel = ' GLM-Next ' + await f.refresh() + expect((await (await f.ask('gmodel')).json()).model).toBe('GLM-Next') + }) + test(`${site}: a failed normal refresh keeps the request's cached entry`, async () => { + const f = await fixture(create) + await f.refresh() + f.fail() + await f.refresh() + const response = await f.ask('gmodel') + expect(response.status).toBe(200) + expect((await response.json()).model).toBe('GLM-5.3') + expect(f.sent.at(-1).body.model_config.display_name).toBe('GLM-5.3') + expect(f.listingCalls()).toBe(2) + }) +} diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 7880fcc..3c7001d 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.37", + "version": "0.2.38", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)", "type": "module", "main": "./index.mjs",