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.
This commit is contained in:
刘巍峰
2026-10-26 22:45:27 +08:00
committed by GitHub
parent 03f5952a8b
commit c25201d259
4 changed files with 124 additions and 5 deletions
+15
View File
@@ -166,6 +166,21 @@ itself.
## Models ## 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 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 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 listing's `app` scene, the list Qoder's app shows and the one a request
+6 -4
View File
@@ -1685,7 +1685,7 @@ async function opened(res, tools) {
return { it, first: await it.next() } 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 id = "chatcmpl-" + hexID()
const created = Math.floor(Date.now() / 1000) const created = Math.floor(Date.now() / 1000)
if (first.value?.error) return errorResponse(first.value.error) 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 (reasoning) msg.reasoning_content = reasoning
if (calls.length) msg.tool_calls = calls 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 enc = new TextEncoder()
const chunk = (delta, finish_reason = null, extra = {}) => 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 pending = first
let began = false let began = false
const stream = new ReadableStream({ const stream = new ReadableStream({
@@ -2555,6 +2555,8 @@ const makePlugin = (site) => async ({ client }) => {
return errorResponse({ status: 502, message: e?.message ?? String(e) }) return errorResponse({ status: 502, message: e?.message ?? String(e) })
} }
if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` }) 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 api = siteOf(site, cred).api
const CHAT_URL = api + CHAT_PATH const CHAT_URL = api + CHAT_PATH
const task = taskOf(sessionOf(chat, named), chat) 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 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 // an answered call moves its task on, as Qoder's client moves it
if (!f) task.answered = true 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 { try {
if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f) if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f)
} catch (e) { } catch (e) {
+102
View File
@@ -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)
})
}
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "@magpie-community/opencode-qoder-auth", "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)", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
"type": "module", "type": "module",
"main": "./index.mjs", "main": "./index.mjs",