@magpie-community/opencode-qoder-auth 0.2.74 → 0.2.75

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 +2 -1
  2. package/index.mjs +69 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -162,7 +162,8 @@ itself.
162
162
  - an MD5 signature;
163
163
  - the machine id and the client's headers.
164
164
  - `Cosy-Version`: the worker runtime version that Qoder's desktop app
165
- ships (1.1.69 in app 0.4.8, since 0.2.23, #281), because
165
+ ships (1.1.96-hotfix.8 in app 0.5.20, since 0.2.75, #1560; before
166
+ that 1.1.69 in app 0.4.8, from 0.2.23, #281), because
166
167
  `Cosy-ClientType` says the desktop app.
167
168
 
168
169
  **Reading the reply**
package/index.mjs CHANGED
@@ -48,10 +48,14 @@ const MODELS_PATH = "/algo/api/v2/model/list?Encode=1"
48
48
  // talks through — the worker runtime bundled in the app — because the
49
49
  // envelope also says Cosy-ClientType: 10 (desktop) and carries that app's
50
50
  // machine identity; the CLI's channel publishes a different, later number
51
- // that no desktop build runs. Taken from app 0.4.8 (build 79743374,
52
- // 2026-10-14), whose transport config names 1.1.69. Raise it when the app
53
- // moves, not when the CLI does.
54
- const COSY_VERSION = "1.1.69"
51
+ // that no desktop build runs. Taken from app 0.5.20 (both feeds, Qoder
52
+ // and Qoder CN, 2026-11-27), whose bundled worker runtime's
53
+ // runtime-info.json names 1.1.96-hotfix.8, the number its envelope and its
54
+ // chat's business.version say. 0.4.8 named 1.1.69: sending that, every
55
+ // request of one Qoder CN account was queued again after each wait, while
56
+ // the app answered the same account at once (plugins#1560). Raise it when
57
+ // the app moves, not when the CLI does.
58
+ const COSY_VERSION = "1.1.96-hotfix.8"
55
59
  const SIGN_IN_TIMEOUT = 15 * 60 * 1000
56
60
  const REFRESH_LEAD = 5 * 60 * 1000 // a chat token this close to its end is refreshed before a request
57
61
  const RENEW_LEAD = 10 * 60 * 1000 // and this close, magpie renews it ahead of time (auth.refresh)
@@ -1543,6 +1547,13 @@ function modelConfig(m, thinking) {
1543
1547
  // 0.4.3 and QwenWork 1.0.46 build their record without it, so a site that
1544
1548
  // ships such a client says businessModelKey: false.
1545
1549
  //
1550
+ // APP_CHAT is what Qoder's desktop app (0.5.20, its worker runtime's
1551
+ // remote chat body) says of every chat request it sends, whatever the
1552
+ // prompt: a streamed free-input turn, version "3". It goes with
1553
+ // COSY_VERSION, so only to the sites that speak as that app; QwenWork and
1554
+ // Lingma, with versions of their own, send their own clients' bodies.
1555
+ const APP_CHAT = { stream: true, chat_task: "FREE_INPUT", is_reply: true, is_retry: false, source: 1, version: "3", aliyun_user_type: "" }
1556
+
1546
1557
  // qoderBody is the plaintext agent_chat_generation takes for a chat
1547
1558
  // completion request, for model m, in the session the agent named (named,
1548
1559
  // "" when none) and the task it is a call of (taskOf's, a new one when
@@ -1572,6 +1583,8 @@ function qoderBody(chat, m, named = "", task, site) {
1572
1583
  request_set_id: task.id,
1573
1584
  chat_record_id: request,
1574
1585
  session_id: session,
1586
+ // what the app's request says of itself beside its version
1587
+ ...(site?.cosyVersion ? {} : APP_CHAT),
1575
1588
  parameters: params,
1576
1589
  business: { product: site?.businessProduct ?? "app", version: site?.cosyVersion ?? COSY_VERSION, type: "agent", id: task.id, name: task.name ?? "", begin_at: task.begin, stage: task.answered ? "processing" : "start", ...(site?.businessModelKey === false ? {} : { model_key: m.key }) },
1577
1590
  agent_id: "agent_common",
@@ -1800,6 +1813,9 @@ const QUEUE = {
1800
1813
  // Qoder's client waits it (#807).
1801
1814
  stuck: 5 * 60 * 1000,
1802
1815
  now: () => Date.now(),
1816
+ // a line for magpie's log (the plugin host's stderr, app.log's
1817
+ // "plugin: " lines): each step of a queued request's wait
1818
+ log: (line) => console.warn(line),
1803
1819
  // every calls fn each ms until the returned stop is called
1804
1820
  every: (fn, ms) => {
1805
1821
  const t = setInterval(fn, ms)
@@ -1830,6 +1846,23 @@ function queuePause(seconds) {
1830
1846
 
1831
1847
  class QueueEnd extends Error {}
1832
1848
 
1849
+ // queueSays is a queue state as Qoder gave it, its fields by Qoder's names,
1850
+ // for magpie's log and the refusal a wait ends with: what the queue said
1851
+ // (plugins#1560: the reporter had no way to see it).
1852
+ const QUEUE_FIELDS = ["isQueued", "serviceAvailable", "queueType", "queueCount", "retryAfterSeconds", "waitTime", "subCode", "modelKey"]
1853
+ function queueSays(st) {
1854
+ if (!st || typeof st !== "object") return "nothing of the queue"
1855
+ const said = QUEUE_FIELDS.filter((k) => st[k] !== undefined && st[k] !== null && st[k] !== "").map((k) => `${k} ${st[k]}`)
1856
+ return said.length ? said.join(", ") : "nothing of the queue"
1857
+ }
1858
+
1859
+ // queueLog logs a step of the wait of ask, the request of site
1860
+ function queueLog(site, ask, text) {
1861
+ try {
1862
+ QUEUE.log(`${site?.id ?? "qoder"}: ${ask.modelKey} in request set ${String(ask.requestSetId).slice(0, 8)}: ${text}`)
1863
+ } catch {}
1864
+ }
1865
+
1833
1866
  // queueState is the queue's state in a status reply, found as Qoder's
1834
1867
  // client finds it: the object, or one under data/result/message/body, as
1835
1868
  // an object or a JSON string, that says isQueued or serviceAvailable.
@@ -1852,7 +1885,7 @@ function queueState(text) {
1852
1885
  if (isQueued !== undefined || serviceAvailable !== undefined) {
1853
1886
  const num = (v) => (v == null || v === "" || !Number.isFinite(Number(v)) ? undefined : Number(v))
1854
1887
  const str = (v) => (typeof v === "string" && v ? v : undefined)
1855
- return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds), queueCount: num(o.queueCount), subCode: str(o.subCode) }
1888
+ return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds), queueCount: num(o.queueCount), waitTime: num(o.waitTime), subCode: str(o.subCode) }
1856
1889
  }
1857
1890
  for (const k of ["data", "result", "message", "body"]) {
1858
1891
  const v = o[k]
@@ -1941,6 +1974,7 @@ function stuckKey(st) {
1941
1974
  // (false). A wait ended by QUEUE.stuck sets ask.stuck. A cancelled request
1942
1975
  // throws.
1943
1976
  async function queueWait(api, cred, ask, first, signal, deadline, site) {
1977
+ const began = QUEUE.now()
1944
1978
  let q = first ?? {}
1945
1979
  let asked = false
1946
1980
  let failed = 0
@@ -1948,21 +1982,31 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
1948
1982
  let since = QUEUE.now()
1949
1983
  for (;;) {
1950
1984
  if (signal?.aborted) throw signal.reason ?? new DOMException("aborted", "AbortError")
1951
- if (QUEUE.now() >= deadline) return false
1985
+ const ranOut = () => (queueLog(site, ask, `the wait ran out after ${Math.round((QUEUE.now() - began) / 1000)}s`), false)
1986
+ if (QUEUE.now() >= deadline) return ranOut()
1952
1987
  if (asked) await QUEUE.pause(Math.min(queuePause(q.retryAfterSeconds), Math.max(0, deadline - QUEUE.now())), signal)
1953
1988
  asked = true
1954
- if (QUEUE.now() >= deadline) return false
1989
+ if (QUEUE.now() >= deadline) return ranOut()
1955
1990
  let st
1956
1991
  try {
1957
1992
  st = await queueStatus(api, cred, { ...ask, modelKey: q.modelKey ?? ask.modelKey, queueType: q.queueType ?? ask.queueType }, signal, deadline, site)
1958
1993
  } catch (e) {
1959
1994
  if (signal?.aborted) throw signal.reason ?? e
1960
- if (e instanceof QueueEnd) return false
1961
- if (++failed >= QUEUE.failures) return false
1995
+ if (e instanceof QueueEnd) {
1996
+ queueLog(site, ask, `the wait ends: ${e.message}`)
1997
+ return false
1998
+ }
1999
+ queueLog(site, ask, `queue status ask failed: ${e?.message ?? e}`)
2000
+ if (++failed >= QUEUE.failures) {
2001
+ queueLog(site, ask, `the wait ends: ${failed} status asks failed in a row`)
2002
+ return false
2003
+ }
1962
2004
  q = { ...q, retryAfterSeconds: undefined }
1963
2005
  continue
1964
2006
  }
1965
2007
  failed = 0
2008
+ ask.last = st
2009
+ queueLog(site, ask, `queue status: ${queueSays(st)}${queueReady(st) ? "; through, sending it again" : ""}`)
1966
2010
  q = { ...st, modelKey: st.modelKey ?? q.modelKey, queueType: st.queueType ?? q.queueType }
1967
2011
  const key = stuckKey(q)
1968
2012
  if (!key || key !== same) {
@@ -1970,6 +2014,7 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
1970
2014
  since = QUEUE.now()
1971
2015
  } else if (QUEUE.now() - since >= QUEUE.stuck && !queueReady(st)) {
1972
2016
  ask.stuck = true
2017
+ queueLog(site, ask, `the wait ends: the queue hasn't moved in ${Math.round(QUEUE.stuck / 60_000)} minutes with nothing ahead`)
1973
2018
  return false
1974
2019
  }
1975
2020
  if (queueReady(st)) {
@@ -1977,6 +2022,7 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
1977
2022
  return QUEUE.now() < deadline
1978
2023
  }
1979
2024
  if (st.serviceAvailable === false || st.isQueued === true) continue
2025
+ queueLog(site, ask, "the wait ends: the status is neither queued nor through")
1980
2026
  return false
1981
2027
  }
1982
2028
  }
@@ -3985,7 +4031,20 @@ const makePlugin = (site) => async ({ client }) => {
3985
4031
  void release(task, lease)
3986
4032
  return r
3987
4033
  }
3988
- if (waited >= QUEUE.recoveries || fallback) return giveUp(errorResponse(f))
4034
+ const said = siteOf(site, cred)
4035
+ queueLog(said, ask, `queued (${QUEUED}) on send ${sent + 1}: ${queueSays(f.queue)}`)
4036
+ if (fallback) {
4037
+ queueLog(said, ask, "not waited: another candidate follows")
4038
+ return giveUp(errorResponse(f))
4039
+ }
4040
+ // the queue said it was through each time, and each resend was
4041
+ // queued again: said in the refusal, with the queue's last status,
4042
+ // so the reason is readable where the error is (plugins#1560)
4043
+ if (waited >= QUEUE.recoveries) {
4044
+ const again = `Qoder's queue let it through ${waited} times and queued each resend again; its last status: ${queueSays(ask.last)}`
4045
+ queueLog(said, ask, `the wait ends: ${again}`)
4046
+ return giveUp(errorResponse({ ...f, message: `${f.message} (${again})` }))
4047
+ }
3989
4048
  if (chat.stream && !held) {
3990
4049
  held = holdQueued(f, init.signal)
3991
4050
  signal = held.signal
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.74",
3
+ "version": "0.2.75",
4
4
  "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains",
5
5
  "type": "module",
6
6
  "main": "./opencode.mjs",