@magpie-community/opencode-qoder-auth 0.2.37 → 0.2.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +15 -0
  2. package/index.mjs +27 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -166,6 +166,21 @@ itself.
166
166
 
167
167
  ## Models
168
168
 
169
+ Chat responses report the display name from the same model-list entry
170
+ used to build the request, in both JSON completions and every SSE chunk
171
+ (including usage). A blank name or a name equal to the slot key falls
172
+ back to the requested model ID. This shared behavior applies to Qoder,
173
+ Qoder CN, QwenWork and QwenWork Enterprise, including tier labels.
174
+
175
+ Reporting the name does not fetch the model list again. The existing
176
+ cache and model-list refresh behavior remain in place: a model missing
177
+ from the cache still forces a refresh, and normal list refreshes update
178
+ the configuration and name together. Model-list keys, API IDs,
179
+ `X-Model-Key` and `model_config` are unchanged; slots with the same display
180
+ name remain independently selectable. The name describes the catalog
181
+ entry used for the request, not an attestation of the backend that
182
+ executed it.
183
+
169
184
  The `provider.models` hook reads the account's own list, as Qoder's client
170
185
  asks for it (`/algo/api/v2/model/list`). It keeps the enabled models of the
171
186
  listing's `app` scene, the list Qoder's app shows and the one a request
package/index.mjs CHANGED
@@ -1131,8 +1131,9 @@ const errorResponse = ({ status, message, signIn = status === 401 ? "kept" : und
1131
1131
  // the request's requestSetId, the model and the plan's queueType, at once
1132
1132
  // and then every retryAfterSeconds (held to 0.5s..30s, 30s when unsaid),
1133
1133
  // while the state says isQueued or serviceAvailable false. Once isQueued is
1134
- // false it waits that state's retryAfterSeconds, if any, and sends the same
1135
- // request again, request_set_id and all, up to 10 times. Three failed asks
1134
+ // false it waits that state's retryAfterSeconds, if any, and sends the
1135
+ // request again in the same request_set_id under a new request_id and
1136
+ // chat_record_id, up to 10 times; the refused call's stream is closed. Three failed asks
1136
1137
  // in a row, a 404 (no queue there) or a state with neither field end the
1137
1138
  // wait. Qoder's client waits up to an hour; here the whole of it is held
1138
1139
  // under QUEUE.budget from the request's start, since nothing reaches the
@@ -1685,7 +1686,7 @@ async function opened(res, tools) {
1685
1686
  return { it, first: await it.next() }
1686
1687
  }
1687
1688
 
1688
- async function answer({ it, first }, chat) {
1689
+ async function answer({ it, first }, chat, servedName = chat.model) {
1689
1690
  const id = "chatcmpl-" + hexID()
1690
1691
  const created = Math.floor(Date.now() / 1000)
1691
1692
  if (first.value?.error) return errorResponse(first.value.error)
@@ -1708,12 +1709,12 @@ async function answer({ it, first }, chat) {
1708
1709
  }
1709
1710
  if (reasoning) msg.reasoning_content = reasoning
1710
1711
  if (calls.length) msg.tool_calls = calls
1711
- return Response.json({ id, object: "chat.completion", created, model: chat.model, choices: [{ index: 0, message: msg, finish_reason: stop }], ...(usage ? { usage } : {}) })
1712
+ return Response.json({ id, object: "chat.completion", created, model: servedName, choices: [{ index: 0, message: msg, finish_reason: stop }], ...(usage ? { usage } : {}) })
1712
1713
  }
1713
1714
 
1714
1715
  const enc = new TextEncoder()
1715
1716
  const chunk = (delta, finish_reason = null, extra = {}) =>
1716
- enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model: chat.model, choices: [{ index: 0, delta, finish_reason }], ...extra })}\n\n`)
1717
+ enc.encode(`data: ${JSON.stringify({ id, object: "chat.completion.chunk", created, model: servedName, choices: [{ index: 0, delta, finish_reason }], ...extra })}\n\n`)
1717
1718
  let pending = first
1718
1719
  let began = false
1719
1720
  const stream = new ReadableStream({
@@ -2555,16 +2556,26 @@ const makePlugin = (site) => async ({ client }) => {
2555
2556
  return errorResponse({ status: 502, message: e?.message ?? String(e) })
2556
2557
  }
2557
2558
  if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` })
2559
+ const name = m.name?.trim()
2560
+ const servedName = name && name !== m.key ? name : chat.model
2558
2561
  const api = siteOf(site, cred).api
2559
2562
  const CHAT_URL = api + CHAT_PATH
2560
2563
  const task = taskOf(sessionOf(chat, named), chat)
2561
2564
  const body = qoderBody(chat, m, named, task, siteOf(site, cred))
2562
- const wire = encodeBody(JSON.stringify(body))
2563
- // Qoder's queue is waited out (queueWait) and the same request, its
2564
- // request_set_id the one the queue knows, sent again
2565
+ // Qoder's queue is waited out (queueWait) and the request sent again
2566
+ // under the request_set_id the queue knows, with a new request_id and
2567
+ // chat_record_id, as Qoder's client sends it (its o4i before each
2568
+ // resend). On a real qoder.com account (plugins#427) the free models
2569
+ // are queued in p3 for RESOURCE_LIMIT, about 30s at a time, and a
2570
+ // resend can be queued again before it is answered.
2565
2571
  const deadline = QUEUE.now() + QUEUE.budget
2566
2572
  const ask = { requestSetId: body.request_set_id, requestId: body.request_id, sessionId: body.session_id, modelKey: m.key }
2567
2573
  for (let waited = 0; ; waited++) {
2574
+ if (waited) {
2575
+ body.request_id = body.chat_record_id = randomUUID()
2576
+ ask.requestId = body.request_id
2577
+ }
2578
+ const wire = encodeBody(JSON.stringify(body))
2568
2579
  const headers = {
2569
2580
  ...cosyHeaders(CHAT_URL, cred, wire, siteOf(site, cred)),
2570
2581
  Accept: "text/event-stream",
@@ -2572,17 +2583,23 @@ const makePlugin = (site) => async ({ client }) => {
2572
2583
  "X-Model-Key": m.key,
2573
2584
  "X-Model-Source": m.source,
2574
2585
  }
2586
+ // a refused call's stream is closed: Qoder keeps a 10605 stream open
2587
+ // after its one event, and a connection held open while the call is
2588
+ // waited out and sent again is one more the account holds
2589
+ const call = new AbortController()
2590
+ const signal = init.signal ? AbortSignal.any([init.signal, call.signal]) : call.signal
2575
2591
  let res, open
2576
2592
  try {
2577
- res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: init.signal })
2593
+ res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal })
2578
2594
  if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
2579
2595
  } catch (e) {
2580
2596
  return errorResponse({ status: 502, message: e.message })
2581
2597
  }
2582
2598
  const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
2599
+ if (f) call.abort()
2583
2600
  // an answered call moves its task on, as Qoder's client moves it
2584
2601
  if (!f) task.answered = true
2585
- if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat)
2602
+ if (!f?.queue || waited >= QUEUE.recoveries) return f ? errorResponse(f) : answer(open, chat, servedName)
2586
2603
  try {
2587
2604
  if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f)
2588
2605
  } catch (e) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.37",
3
+ "version": "0.2.39",
4
4
  "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)",
5
5
  "type": "module",
6
6
  "main": "./index.mjs",