@magpie-community/opencode-qoder-auth 0.2.70 → 0.2.72

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 +13 -2
  2. package/index.mjs +47 -5
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -106,6 +106,11 @@ itself.
106
106
 
107
107
  - It writes the chat completion as Qoder's request:
108
108
  - Qoder's own system line comes before yours.
109
+ - Claude Code's identity line, `You are Claude Code, Anthropic's official
110
+ CLI for Claude.`, goes as the Claude Agent SDK's, `You are a Claude
111
+ agent, built on Anthropic's Claude Agent SDK.` (since 0.2.72): Qoder
112
+ answers a prompt with that line with 400 `Cannot find model`, whatever
113
+ the model. The rest of the prompt and your messages go as written.
109
114
  - Messages become text and image blocks.
110
115
  - Tool calls and results become OpenAI tool turns. Images a tool returned
111
116
  follow in a user turn.
@@ -193,7 +198,7 @@ itself.
193
198
  | A refused sign-in (no code, or code 105) | 401 |
194
199
  | A quota (codes 110, 112–119, 122) | 429 |
195
200
  | A model the account's listing has off while its usage says no credits are left (since 0.2.64, #880) | 429 usage limit reached, with the credits used and the model; a model off with credits left, or whose usage can't be read, stays 400 `unknown or disabled model`, and an id Qoder never served is that 400 at once |
196
- | Busy or queued for the model (code 10605) | Since 0.2.28 the request waits in Qoder's queue as Qoder's client does (below), then is sent again; since 0.2.62 a streamed request is answered 200 at once and kept alive with SSE comments for up to an hour, its outcome in the stream; past the wait, 503 with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none). Since 0.2.40, when magpie has another candidate after this request (`x-magpie-fallback: 1`), the 503 comes at once, saying the queue and how many are ahead, and magpie asks the next. The sign-in is untouched |
201
+ | Busy or queued for the model (code 10605) | Since 0.2.28 the request waits in Qoder's queue as Qoder's client does (below), then is sent again; since 0.2.62 a streamed request is answered 200 at once and kept alive with SSE comments for up to an hour (since 0.2.71, 5 minutes when the queue hasn't moved with nothing ahead), its outcome in the stream; past the wait, 503 with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none). Since 0.2.40, when magpie has another candidate after this request (`x-magpie-fallback: 1`), the 503 comes at once, saying the queue and how many are ahead, and magpie asks the next. The sign-in is untouched |
197
202
  | A refusal of the request itself (codes 406, 416, 430, 80411, 80412, 100400–100403) | 400, with Qoder's words |
198
203
  | "Error in upstream response", the model's own vendor behind Qoder failing, which Qoder sends as a 400 (since 0.2.24) | 502, so magpie asks again or another account; the sign-in is untouched |
199
204
  | "Range of max_tokens should be [1, N]" behind "Error in upstream response" (since 0.2.63) | Asked again once with max_tokens N, which later requests for that model are sent from the start; refused again, 400 with Qoder's words, the sign-in untouched. QoDuck (`lmodel`) is never sent more than 131072, and a model whose listing entry names `max_output_tokens` never more than that; a smaller max_tokens goes as asked |
@@ -596,7 +601,13 @@ SSE comment saying the queue (": Qoder queued the request for smodel
596
601
  on: a Codex client is told `response.in_progress`, the event its 300s idle
597
602
  timeout counts, and a Chat client gets the comment. What comes of the wait
598
603
  goes on in the same stream: the answer, or a refusal (the queue's own after
599
- the hour) as the stream's error. Nothing needs setting. A request that isn't
604
+ the hour) as the stream's error. Since **0.2.71** a wait with nothing ahead of
605
+ the request (`queueCount` 1 or none) whose queue state comes back the same
606
+ at every ask for 5 minutes ends there, with the queue's 503 and "the queue
607
+ hasn't moved in 5 minutes with nothing ahead" (plugins#1220: Qoder CN held
608
+ such a RESOURCE_LIMIT queue unchanged, and the agent heard only
609
+ keepalives); magpie can then rest the account or ask the next. A queue with
610
+ others ahead is still waited out. Nothing needs setting. A request that isn't
600
611
  streamed has nothing to keep its client alive with, so it still waits 4
601
612
  minutes at most before the 503. Claude Code takes a ping as life only 30
602
613
  times in a row, about 35 minutes, so a longer wait still ends there. Qoder's client also reports the end
package/index.mjs CHANGED
@@ -71,6 +71,17 @@ XcW+ML9FoCI6AOvOzwIDAQAB
71
71
 
72
72
  const QODER_SYS = "You are a Qoder agent. Use the instructions below and the tools available to you to assist the user."
73
73
 
74
+ // Qoder answers a system prompt that opens with Claude Code's identity
75
+ // line with 400 "Cannot find model", on any model; the same request with
76
+ // only that line swapped for the Claude Agent SDK's goes through (ByFanX
77
+ // on X, 11-24). asAgentSDK makes that swap, in what Qoder is sent only:
78
+ // the line is matched whatever its white space, and the rest of the
79
+ // prompt goes as written. Claude Code's SDK form (", running within the
80
+ // Claude Agent SDK.") is swapped the same way.
81
+ const CLAUDE_CODE_LINE = /You\s+are\s+Claude\s+Code,\s+Anthropic's\s+official\s+CLI\s+for\s+Claude(?:,\s+running\s+within\s+the\s+Claude\s+Agent\s+SDK)?\./g
82
+ const AGENT_SDK_LINE = "You are a Claude agent, built on Anthropic's Claude Agent SDK."
83
+ const asAgentSDK = (text) => text.replace(CLAUDE_CODE_LINE, AGENT_SDK_LINE)
84
+
74
85
  const hexID = () => randomUUID().replaceAll("-", "")
75
86
 
76
87
  // ---- the COSY envelope --------------------------------------------------------
@@ -1504,7 +1515,8 @@ function modelConfig(m, thinking) {
1504
1515
  // none is given). A site on this protocol with its own values (QwenWork)
1505
1516
  // passes itself last.
1506
1517
  function qoderBody(chat, m, named = "", task, site) {
1507
- const system = (chat.messages ?? []).filter((x) => x.role === "system" || x.role === "developer").map((x) => textOf(x.content)).filter(Boolean).join("\n\n")
1518
+ const given = (chat.messages ?? []).filter((x) => x.role === "system" || x.role === "developer").map((x) => textOf(x.content)).filter(Boolean).join("\n\n")
1519
+ const system = asAgentSDK(given)
1508
1520
  const tools = chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function" && t.function?.name) : []
1509
1521
  const baseSystem = site?.systemPrompt ?? QODER_SYS
1510
1522
  let sysText = system ? baseSystem + "\n\n" + system : baseSystem
@@ -1735,6 +1747,15 @@ const QUEUE = {
1735
1747
  // fail over from. A queued call answers its 10605 at once, and a
1736
1748
  // thinking model streams its reasoning, so a real answer starts sooner.
1737
1749
  silent: 120_000,
1750
+ // the longest a wait goes on with nothing ahead of the request
1751
+ // (queueCount 1 or none) and the queue's state the same at every ask.
1752
+ // Qoder CN once held such a RESOURCE_LIMIT queue unchanged, 1 in p4,
1753
+ // for every request from one hour on (plugins#1220): the hour's hold
1754
+ // then left the agent with keepalives only. After this it is the
1755
+ // queue's 503 with Retry-After, which magpie can rest the account on
1756
+ // or fail over from. A queue with others ahead is waited out as
1757
+ // Qoder's client waits it (#807).
1758
+ stuck: 5 * 60 * 1000,
1738
1759
  now: () => Date.now(),
1739
1760
  // every calls fn each ms until the returned stop is called
1740
1761
  every: (fn, ms) => {
@@ -1788,7 +1809,7 @@ function queueState(text) {
1788
1809
  if (isQueued !== undefined || serviceAvailable !== undefined) {
1789
1810
  const num = (v) => (v == null || v === "" || !Number.isFinite(Number(v)) ? undefined : Number(v))
1790
1811
  const str = (v) => (typeof v === "string" && v ? v : undefined)
1791
- return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds) }
1812
+ return { isQueued, serviceAvailable, modelKey: str(o.modelKey), queueType: str(o.queueType), retryAfterSeconds: num(o.retryAfterSeconds), queueCount: num(o.queueCount), subCode: str(o.subCode) }
1792
1813
  }
1793
1814
  for (const k of ["data", "result", "message", "body"]) {
1794
1815
  const v = o[k]
@@ -1820,13 +1841,25 @@ async function queueStatus(api, cred, { requestSetId, modelKey, queueType, reque
1820
1841
  return st
1821
1842
  }
1822
1843
 
1844
+ // stuckKey is what a queue's state says of the request's place in it, or
1845
+ // "" when others are ahead of it (a queue that moves while they are
1846
+ // served is waited out, QUEUE.stuck never ends it).
1847
+ function stuckKey(st) {
1848
+ const n = Number(st?.queueCount)
1849
+ if (Number.isFinite(n) && n > 1) return ""
1850
+ return JSON.stringify([st?.isQueued, st?.serviceAvailable, Number.isFinite(n) ? n : null, st?.queueType ?? null, st?.subCode ?? null, st?.retryAfterSeconds ?? null])
1851
+ }
1852
+
1823
1853
  // queueWait waits in Qoder's queue for the request, from the state its
1824
1854
  // refusal gave, until the queue lets it through (true) or the wait ends
1825
- // (false). A cancelled request throws.
1855
+ // (false). A wait ended by QUEUE.stuck sets ask.stuck. A cancelled request
1856
+ // throws.
1826
1857
  async function queueWait(api, cred, ask, first, signal, deadline, site) {
1827
1858
  let q = first ?? {}
1828
1859
  let asked = false
1829
1860
  let failed = 0
1861
+ let same = stuckKey(q)
1862
+ let since = QUEUE.now()
1830
1863
  for (;;) {
1831
1864
  if (signal?.aborted) throw signal.reason ?? new DOMException("aborted", "AbortError")
1832
1865
  if (QUEUE.now() >= deadline) return false
@@ -1845,6 +1878,14 @@ async function queueWait(api, cred, ask, first, signal, deadline, site) {
1845
1878
  }
1846
1879
  failed = 0
1847
1880
  q = { ...st, modelKey: st.modelKey ?? q.modelKey, queueType: st.queueType ?? q.queueType }
1881
+ const key = stuckKey(q)
1882
+ if (!key || key !== same) {
1883
+ same = key
1884
+ since = QUEUE.now()
1885
+ } else if (QUEUE.now() - since >= QUEUE.stuck && st.isQueued !== false) {
1886
+ ask.stuck = true
1887
+ return false
1888
+ }
1848
1889
  if (st.isQueued === false) {
1849
1890
  if (st.retryAfterSeconds > 0) await QUEUE.pause(Math.min(queuePause(st.retryAfterSeconds), Math.max(0, deadline - QUEUE.now())), signal)
1850
1891
  return QUEUE.now() < deadline
@@ -3858,7 +3899,8 @@ const makePlugin = (site) => async ({ client }) => {
3858
3899
  onHeld(held.response)
3859
3900
  }
3860
3901
  try {
3861
- if (!(await queueWait(api, cred, ask, f.queue, signal, deadline, siteOf(site, cred)))) return errorResponse(f)
3902
+ if (!(await queueWait(api, cred, ask, f.queue, signal, deadline, siteOf(site, cred))))
3903
+ return errorResponse(ask.stuck ? { ...f, message: `${f.message} (the queue hasn't moved in ${Math.round(QUEUE.stuck / 60_000)} minutes with nothing ahead)` } : f)
3862
3904
  } catch (e) {
3863
3905
  return errorResponse({ status: 502, message: e?.message ?? String(e) })
3864
3906
  }
@@ -4244,4 +4286,4 @@ export async function QwenWorkEnterpriseAuthPlugin({ client }) {
4244
4286
  export const protocol = { makePlugin, deviceSignIn, openapi, siteOf }
4245
4287
 
4246
4288
  // for tests
4247
- export const _internal = { spentOf, OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, qoderMessages, kimiModel, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
4289
+ export const _internal = { spentOf, OUTPUT_CAPS, LEARNED_CAPS, outputCap, fetchListing, accountIdentity, deviceRefreshBody, qwenworkClientHeaders, freeView, maskPhone, phoneLabelOf, realEmailOf, chatHeaders, machineOS, machineHostname, cosyHeaders, typedUnions, Splitter, campaignRuntime, RUNTIMES, campaignPage, campaignTarget, vpcInstance, vpcSite, dedicatedOrigin, dedicatedHost, dedicatedSite, lingmaRoots, lingmaDecode, lingmaSite, lingmaHeaders, LINGMA_MODELS, parseUsage, gfmt, when, failure, modelInfo, qoderBody, asAgentSDK, qoderMessages, kimiModel, cacheMarked, sessionOf, namedSession, taskOf, TASKS, TASK_MAX, TASK_IDLE, SESSION, SITES, decodeBody, events, QUEUE, queueState }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.70",
3
+ "version": "0.2.72",
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",