@magpie-community/opencode-qoder-auth 0.2.38 → 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 (2) hide show
  1. package/index.mjs +22 -7
  2. package/package.json +1 -1
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
@@ -2561,12 +2562,20 @@ const makePlugin = (site) => async ({ client }) => {
2561
2562
  const CHAT_URL = api + CHAT_PATH
2562
2563
  const task = taskOf(sessionOf(chat, named), chat)
2563
2564
  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
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.
2567
2571
  const deadline = QUEUE.now() + QUEUE.budget
2568
2572
  const ask = { requestSetId: body.request_set_id, requestId: body.request_id, sessionId: body.session_id, modelKey: m.key }
2569
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))
2570
2579
  const headers = {
2571
2580
  ...cosyHeaders(CHAT_URL, cred, wire, siteOf(site, cred)),
2572
2581
  Accept: "text/event-stream",
@@ -2574,17 +2583,23 @@ const makePlugin = (site) => async ({ client }) => {
2574
2583
  "X-Model-Key": m.key,
2575
2584
  "X-Model-Source": m.source,
2576
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
2577
2591
  let res, open
2578
2592
  try {
2579
- 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 })
2580
2594
  if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : [])
2581
2595
  } catch (e) {
2582
2596
  return errorResponse({ status: 502, message: e.message })
2583
2597
  }
2584
2598
  const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error
2599
+ if (f) call.abort()
2585
2600
  // an answered call moves its task on, as Qoder's client moves it
2586
2601
  if (!f) task.answered = true
2587
- if (!f?.queue || waited >= QUEUE.recoveries) return f && !res.ok ? errorResponse(f) : answer(open, chat, servedName)
2602
+ if (!f?.queue || waited >= QUEUE.recoveries) return f ? errorResponse(f) : answer(open, chat, servedName)
2588
2603
  try {
2589
2604
  if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f)
2590
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.38",
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",