@magpie-community/opencode-qoder-auth 0.2.38 → 0.2.40

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 +9 -1
  2. package/index.mjs +33 -10
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -158,7 +158,7 @@ itself.
158
158
  |---|---|
159
159
  | A refused sign-in (no code, or code 105) | 401 |
160
160
  | A quota (codes 110, 112–119, 122) | 429 |
161
- | 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; past the wait, 503 with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none). The sign-in is untouched |
161
+ | 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; 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 |
162
162
  | A refusal of the request itself (codes 406, 416, 430, 80411, 80412, 100400–100403) | 400, with Qoder's words |
163
163
  | "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 |
164
164
  | A failure before the answer starts | Qoder's status |
@@ -427,6 +427,14 @@ cancelled request end the wait early. Qoder's client also reports the end
427
427
  of its turn to `/api/v2/service/ask/finish`; the plugin doesn't, and
428
428
  Qoder releases that lease on its own TTL. `queue.test.mjs` has the wait.
429
429
 
430
+ Since **0.2.40** the wait is only for the last candidate: when magpie says
431
+ another account, provider or routing group member follows this request
432
+ (its `x-magpie-fallback: 1` header, which the plugin host keeps from Qoder),
433
+ a queued request is answered at once with the 503 above, e.g. "Qoder queued
434
+ the request for dfmodel (this plan's queue: p3, 3 ahead); try again in
435
+ 30s", and magpie asks the next instead of keeping the agent in Qoder's
436
+ queue.
437
+
430
438
  For a bounded offline reproduction, run `packages/qoder/busy.test.mjs`
431
439
  from #185 in an isolated temporary HOME. It compares plain queue JSON,
432
440
  BOM-prefixed JSON and known message wrappers, keeping explicit login and
package/index.mjs CHANGED
@@ -897,6 +897,10 @@ function sessionOf(chat, named = "") {
897
897
  // conversation by, from the chat.headers hook to the loader's fetch. It
898
898
  // goes no further: nothing of the request's headers is sent to Qoder.
899
899
  const SESSION = "x-magpie-qoder-session"
900
+ // FALLBACK is "1" when magpie has another candidate after this request
901
+ // (another account, provider or routing group member): a queued request is
902
+ // then answered at once rather than waited out
903
+ const FALLBACK = "x-magpie-fallback"
900
904
 
901
905
  // namedSession is the session a chat.headers hook is told, "" when it
902
906
  // names none. magpie, with no session header from the agent, makes one of
@@ -1090,7 +1094,8 @@ function failure(status, text) {
1090
1094
  const n = Number(queue?.retryAfterSeconds)
1091
1095
  const wait = Number.isFinite(n) && n > 0 ? Math.ceil(n) : BUSY_WAIT
1092
1096
  const model = queue?.modelKey ? ` for ${queue.modelKey}` : ""
1093
- const tier = queue?.queueType ? ` (this plan's queue: ${queue.queueType})` : ""
1097
+ const ahead = Number(queue?.queueCount) > 0 ? `, ${Math.floor(Number(queue.queueCount))} ahead` : ""
1098
+ const tier = queue?.queueType ? ` (this plan's queue: ${queue.queueType}${ahead})` : ahead ? ` (${ahead.slice(2)})` : ""
1094
1099
  const what = queue?.isQueued === true ? "queued the request" : "is busy"
1095
1100
  return { status: 503, message: `Qoder ${what}${model}${tier}; try again in ${wait}s`, retryAfter: wait, signIn: "", queue: queue ?? {} }
1096
1101
  }
@@ -1131,8 +1136,9 @@ const errorResponse = ({ status, message, signIn = status === 401 ? "kept" : und
1131
1136
  // the request's requestSetId, the model and the plan's queueType, at once
1132
1137
  // and then every retryAfterSeconds (held to 0.5s..30s, 30s when unsaid),
1133
1138
  // 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
1139
+ // false it waits that state's retryAfterSeconds, if any, and sends the
1140
+ // request again in the same request_set_id under a new request_id and
1141
+ // chat_record_id, up to 10 times; the refused call's stream is closed. Three failed asks
1136
1142
  // in a row, a 404 (no queue there) or a state with neither field end the
1137
1143
  // wait. Qoder's client waits up to an hour; here the whole of it is held
1138
1144
  // under QUEUE.budget from the request's start, since nothing reaches the
@@ -2546,7 +2552,7 @@ const makePlugin = (site) => async ({ client }) => {
2546
2552
  })
2547
2553
 
2548
2554
  // ask is the answer to a chat completion on the live account cred.
2549
- const ask = async (chat, cred, init, named = "") => {
2555
+ const ask = async (chat, cred, init, named = "", fallback = false) => {
2550
2556
  let m
2551
2557
  try {
2552
2558
  m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model)
@@ -2561,12 +2567,20 @@ const makePlugin = (site) => async ({ client }) => {
2561
2567
  const CHAT_URL = api + CHAT_PATH
2562
2568
  const task = taskOf(sessionOf(chat, named), chat)
2563
2569
  const body = qoderBody(chat, m, named, task, siteOf(site, cred))
2564
- const wire = encodeBody(JSON.stringify(body))
2565
- // Qoder's queue is waited out (queueWait) and the same request, its
2566
- // request_set_id the one the queue knows, sent again
2570
+ // Qoder's queue is waited out (queueWait) and the request sent again
2571
+ // under the request_set_id the queue knows, with a new request_id and
2572
+ // chat_record_id, as Qoder's client sends it (its o4i before each
2573
+ // resend). On a real qoder.com account (plugins#427) the free models
2574
+ // are queued in p3 for RESOURCE_LIMIT, about 30s at a time, and a
2575
+ // resend can be queued again before it is answered.
2567
2576
  const deadline = QUEUE.now() + QUEUE.budget
2568
2577
  const ask = { requestSetId: body.request_set_id, requestId: body.request_id, sessionId: body.session_id, modelKey: m.key }
2569
2578
  for (let waited = 0; ; waited++) {
2579
+ if (waited) {
2580
+ body.request_id = body.chat_record_id = randomUUID()
2581
+ ask.requestId = body.request_id
2582
+ }
2583
+ const wire = encodeBody(JSON.stringify(body))
2570
2584
  const headers = {
2571
2585
  ...cosyHeaders(CHAT_URL, cred, wire, siteOf(site, cred)),
2572
2586
  Accept: "text/event-stream",
@@ -2574,17 +2588,26 @@ const makePlugin = (site) => async ({ client }) => {
2574
2588
  "X-Model-Key": m.key,
2575
2589
  "X-Model-Source": m.source,
2576
2590
  }
2591
+ // a refused call's stream is closed: Qoder keeps a 10605 stream open
2592
+ // after its one event, and a connection held open while the call is
2593
+ // waited out and sent again is one more the account holds
2594
+ const call = new AbortController()
2595
+ const signal = init.signal ? AbortSignal.any([init.signal, call.signal]) : call.signal
2577
2596
  let res, open
2578
2597
  try {
2579
- res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: init.signal })
2598
+ res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal })
2580
2599
  if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
2581
2600
  } catch (e) {
2582
2601
  return errorResponse({ status: 502, message: e.message })
2583
2602
  }
2584
2603
  const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
2604
+ if (f) call.abort()
2585
2605
  // an answered call moves its task on, as Qoder's client moves it
2586
2606
  if (!f) task.answered = true
2587
- if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat, servedName)
2607
+ // with another account, provider or group member after this one
2608
+ // (magpie's X-Magpie-Fallback), a queued request isn't waited out:
2609
+ // its 503 goes back at once, saying the queue, and the next is asked
2610
+ if (!f?.queue || waited >= QUEUE.recoveries || fallback) return f ? errorResponse(f) : answer(open, chat, servedName)
2588
2611
  try {
2589
2612
  if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f)
2590
2613
  } catch (e) {
@@ -2658,7 +2681,7 @@ const makePlugin = (site) => async ({ client }) => {
2658
2681
  }
2659
2682
  const h = new Headers(input instanceof Request ? input.headers : undefined)
2660
2683
  new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v))
2661
- return signed(await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? ""), renewed(cred))
2684
+ return signed(await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? "", h.get(FALLBACK) === "1"), renewed(cred))
2662
2685
  },
2663
2686
  }
2664
2687
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@magpie-community/opencode-qoder-auth",
3
- "version": "0.2.38",
3
+ "version": "0.2.40",
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",