diff --git a/packages/qoder/README.md b/packages/qoder/README.md index c5183ed..36aab47 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -158,7 +158,7 @@ itself. |---|---| | A refused sign-in (no code, or code 105) | 401 | | A quota (codes 110, 112–119, 122) | 429 | -| 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 | +| 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 | | A refusal of the request itself (codes 406, 416, 430, 80411, 80412, 100400–100403) | 400, with Qoder's words | | "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 | | 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 of its turn to `/api/v2/service/ask/finish`; the plugin doesn't, and Qoder releases that lease on its own TTL. `queue.test.mjs` has the wait. +Since **0.2.40** the wait is only for the last candidate: when magpie says +another account, provider or routing group member follows this request +(its `x-magpie-fallback: 1` header, which the plugin host keeps from Qoder), +a queued request is answered at once with the 503 above, e.g. "Qoder queued +the request for dfmodel (this plan's queue: p3, 3 ahead); try again in +30s", and magpie asks the next instead of keeping the agent in Qoder's +queue. + For a bounded offline reproduction, run `packages/qoder/busy.test.mjs` from #185 in an isolated temporary HOME. It compares plain queue JSON, BOM-prefixed JSON and known message wrappers, keeping explicit login and diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 5221ba2..8f3550d 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -897,6 +897,10 @@ function sessionOf(chat, named = "") { // conversation by, from the chat.headers hook to the loader's fetch. It // goes no further: nothing of the request's headers is sent to Qoder. const SESSION = "x-magpie-qoder-session" +// FALLBACK is "1" when magpie has another candidate after this request +// (another account, provider or routing group member): a queued request is +// then answered at once rather than waited out +const FALLBACK = "x-magpie-fallback" // namedSession is the session a chat.headers hook is told, "" when it // names none. magpie, with no session header from the agent, makes one of @@ -1090,7 +1094,8 @@ function failure(status, text) { const n = Number(queue?.retryAfterSeconds) const wait = Number.isFinite(n) && n > 0 ? Math.ceil(n) : BUSY_WAIT const model = queue?.modelKey ? ` for ${queue.modelKey}` : "" - const tier = queue?.queueType ? ` (this plan's queue: ${queue.queueType})` : "" + const ahead = Number(queue?.queueCount) > 0 ? `, ${Math.floor(Number(queue.queueCount))} ahead` : "" + const tier = queue?.queueType ? ` (this plan's queue: ${queue.queueType}${ahead})` : ahead ? ` (${ahead.slice(2)})` : "" const what = queue?.isQueued === true ? "queued the request" : "is busy" return { status: 503, message: `Qoder ${what}${model}${tier}; try again in ${wait}s`, retryAfter: wait, signIn: "", queue: queue ?? {} } } @@ -2547,7 +2552,7 @@ const makePlugin = (site) => async ({ client }) => { }) // ask is the answer to a chat completion on the live account cred. - const ask = async (chat, cred, init, named = "") => { + const ask = async (chat, cred, init, named = "", fallback = false) => { let m try { m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model) @@ -2599,7 +2604,10 @@ const makePlugin = (site) => async ({ client }) => { if (f) call.abort() // an answered call moves its task on, as Qoder's client moves it if (!f) task.answered = true - if (!f?.queue || waited >= QUEUE.recoveries) return f ? errorResponse(f) : answer(open, chat, servedName) + // with another account, provider or group member after this one + // (magpie's X-Magpie-Fallback), a queued request isn't waited out: + // its 503 goes back at once, saying the queue, and the next is asked + if (!f?.queue || waited >= QUEUE.recoveries || fallback) return f ? errorResponse(f) : answer(open, chat, servedName) try { if (!(await queueWait(api, cred, ask, f.queue, init.signal, deadline, siteOf(site, cred)))) return errorResponse(f) } catch (e) { @@ -2673,7 +2681,7 @@ const makePlugin = (site) => async ({ client }) => { } const h = new Headers(input instanceof Request ? input.headers : undefined) new Headers(init.headers ?? {}).forEach((v, k) => h.set(k, v)) - return signed(await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? ""), renewed(cred)) + return signed(await ask(chat, cred, init, h.get(SESSION)?.trim().slice(0, 128) ?? "", h.get(FALLBACK) === "1"), renewed(cred)) }, } }, diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 39751b2..7ac8c0a 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.39", + "version": "0.2.40", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/queue.test.mjs b/packages/qoder/queue.test.mjs index 4fa2ed6..632b060 100644 --- a/packages/qoder/queue.test.mjs +++ b/packages/qoder/queue.test.mjs @@ -147,13 +147,30 @@ test("a wait held past the budget is the 503 with Retry-After it was, the reques const res = await fetch(API_CHAT, ask()) expect(res.status).toBe(503) expect(res.headers.get("Retry-After")).toBe("30") - expect((await res.json()).error.message).toBe("Qoder queued the request for dfmodel (this plan's queue: p3); try again in 30s") + expect((await res.json()).error.message).toBe("Qoder queued the request for dfmodel (this plan's queue: p3, 3 ahead); try again in 30s") expect(sent.chats.length).toBe(1) // 30s pauses, the last cut to what was left of the budget expect(pauses.reduce((a, b) => a + b, 0)).toBe(QUEUE.budget) expect(Math.max(...pauses)).toBe(30_000) }) +// With another candidate after it (magpie's X-Magpie-Fallback: the model's +// next account or provider, a routing group's next member), a queued +// request isn't waited out: its 503 goes back at once, with where it stood +// in the queue, and magpie asks the next (axuhere on X: a free model held +// at "预计等待超过 10 分钟" while another provider could answer). The last +// candidate isn't told, and waits as Qoder's client does (above). +test("told another candidate follows, a queued request is answered at once with the queue's reason", async () => { + const { fetch, sent } = await loader({ chats: () => refused(), statuses: () => status(QUEUED) }) + const res = await fetch(API_CHAT, { ...ask(), headers: { "x-magpie-fallback": "1" } }) + expect(res.status).toBe(503) + expect(res.headers.get("Retry-After")).toBe("30") + expect((await res.json()).error.message).toBe("Qoder queued the request for dfmodel (this plan's queue: p3, 3 ahead); try again in 30s") + expect(sent.chats.length).toBe(1) + expect(sent.statuses.length).toBe(0) + expect(pauses).toEqual([]) +}) + test("the pause is the queue's retryAfterSeconds held to 0.5s..30s, and 30s when it says none", async () => { const states = [ { ...QUEUED, retryAfterSeconds: 0.1 },