diff --git a/packages/qoder/README.md b/packages/qoder/README.md index 5be43a1..98ae11a 100644 --- a/packages/qoder/README.md +++ b/packages/qoder/README.md @@ -125,6 +125,7 @@ itself. | A quota (codes 110, 112–119, 122) | 429 | | Busy or queued for the model (code 10605) | 503, with `Retry-After` from Qoder's `retryAfterSeconds` (30s when it gives none); 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 | | A failure after the answer has started | The stream ends with an error | diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index e3e0c8a..fb0e101 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -849,6 +849,14 @@ const QUOTA_CODES = new Set(["110", "112", "113", "114", "115", "116", "117", "1 // supported, input too long, too many media, a custom model's service const REQUEST_CODES = new Set(["406", "416", "430", "80411", "80412", "100400", "100401", "100403"]) const BUSY_WAIT = 30 +// UPSTREAM_FAILED is Qoder saying the model's own vendor behind it failed: +// "Error in upstream response", which Qoder sends as a 400 (SlouchyQuill507 +// on Discord, qoder-cn). Nothing is wrong with the request or the account, +// so it is a 502, which magpie asks again or of another account, not a +// 400 that ends the turn as the client's fault. A request too long for the +// model fails the same way, and magpie tells that one apart by its size +// (tooLongUnsaid), at 502 as at 400. +const UPSTREAM_FAILED = /\berror in upstream response\b/i function failure(status, text) { if (status < 400 || status > 599) status = 502 @@ -894,6 +902,7 @@ function failure(status, text) { } if (QUOTA_CODES.has(code)) return { status: 429, message: "usage limit reached: " + msg, signIn: "" } if (REQUEST_CODES.has(code)) return { status: 400, message: `Qoder refused the request (${code}): ${msg}`, signIn: "" } + if (UPSTREAM_FAILED.test(msg)) return { status: 502, message: msg, signIn: "" } // a refused chat is the built-in's 401, which never marked the account // lapsed: only a refused refresh did (errorResponse says kept). Qoder's // own words go with it: a 401 right after signing in isn't a lapsed diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 8cfb2b2..b950d5b 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.23", + "version": "0.2.24", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/usage.test.mjs b/packages/qoder/usage.test.mjs index 8c339c3..0879031 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -181,6 +181,33 @@ test("other refusals keep their status and say nothing of the sign-in", async () expect(_internal.failure(400, JSON.stringify({ message: "bad", details: { error: { message: "effort" } } }))).toEqual({ status: 400, message: "bad: effort" }) }) +// SlouchyQuill507 (Discord, qoder-cn): Qoder answers a failure of the model's +// own vendor behind it with a 400 "Error in upstream response". It is no +// fault of the request or the account, so it reaches magpie as a 502, which +// it asks again or of another account. A 400 that is the request's fault +// stays a 400. +test("Qoder's 400 \"Error in upstream response\" is a 502, the account unmarked", async () => { + const UPSTREAM = "Error in upstream response" + const sse = (v) => new Response(`data: ${JSON.stringify(v)}\n\n`, { headers: { "Content-Type": "text/event-stream" } }) + for (const [how, reply] of [ + ["an HTTP 400 with Qoder's JSON", () => new Response(JSON.stringify({ message: UPSTREAM }), { status: 400 })], + ["an HTTP 400 in plain text", () => new Response(UPSTREAM, { status: 400 })], + ["a 400 in the stream's envelope", () => sse({ headers: {}, body: JSON.stringify({ message: UPSTREAM }), statusCodeValue: 400, statusCode: "BAD_REQUEST" })], + ["a 400 in the envelope as plain text", () => sse({ headers: {}, body: UPSTREAM, statusCodeValue: 400, statusCode: "BAD_REQUEST" })], + ]) { + const { fetch } = await chat(LISTING, reply) + const res = await fetch(API_CHAT, ASK) + expect([how, res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([how, 502, null]) + expect((await res.json()).error.message).toBe(UPSTREAM) + } + expect(_internal.failure(400, JSON.stringify({ code: "500", message: UPSTREAM }))).toEqual({ status: 502, message: UPSTREAM, signIn: "" }) + // a request Qoder refuses as the request's fault stays a 400 + expect(_internal.failure(400, JSON.stringify({ message: "invalid request: messages is empty" }))).toEqual({ status: 400, message: "invalid request: messages is empty" }) + expect(_internal.failure(400, JSON.stringify({ code: "416", message: "input too long" })).status).toBe(400) + const { fetch } = await chat(LISTING, () => new Response(JSON.stringify({ message: "invalid request" }), { status: 400 })) + expect((await fetch(API_CHAT, ASK)).status).toBe(400) +}) + test("a model list Qoder won't give is a 502, the account unmarked", async () => { for (const status of [401, 403, 500]) { const auth = { ...account(), machineId: "m1" }