diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index a1df7b5..afbbb0a 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -708,8 +708,13 @@ function failure(status, text) { } catch {} msg ||= STATUS_CODES[status] ?? `HTTP ${status}` // a refused chat is the built-in's 401, which never marked the account - // lapsed: only a refused refresh did (errorResponse says kept) - if (status === 401 || status === 403) return { status: 401, message: "the sign-in lapsed — sign in again" } + // 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 + // sign-in, and its reason is what says what is (Yayoi_no_yume on X). + if (status === 401 || status === 403) { + const said = msg && msg !== STATUS_CODES[status] ? ` (Qoder said ${status}: ${msg.slice(0, 300)})` : "" + return { status: 401, message: "the sign-in lapsed — sign in again" + said } + } if (status === 429 || msg.toLowerCase().includes("quota")) return { status: 429, message: "usage limit reached: " + msg } return { status, message: msg } } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index c980544..bf2d1b2 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.11", + "version": "0.2.12", "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 16870de..13554b6 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -151,12 +151,18 @@ const ASK = { method: "POST", body: JSON.stringify({ model: "qmodel", messages: test("a refused chat, 401 or 403, is the built-in's 401 and leaves the account unmarked", async () => { for (const status of [401, 403]) { - const { fetch } = await chat(LISTING, () => new Response("nope", { status })) + const { fetch } = await chat(LISTING, () => new Response("", { status })) const res = await fetch(API_CHAT, ASK) expect(res.status).toBe(401) expect(res.headers.get("X-Magpie-Sign-In")).toBe("kept") expect((await res.json()).error.message).toBe("the sign-in lapsed — sign in again") } + // what Qoder says of the refusal is kept: a 401 just after signing in + // has some other reason, and only Qoder's words tell it + const said = await chat(LISTING, () => new Response(JSON.stringify({ message: "device not trusted" }), { status: 401 })) + const r = await said.fetch(API_CHAT, ASK) + expect([r.status, r.headers.get("X-Magpie-Sign-In")]).toEqual([401, "kept"]) + expect((await r.json()).error.message).toBe("the sign-in lapsed — sign in again (Qoder said 401: device not trusted)") // refused in the stream, before any answer: the same const sse = (v) => new Response(`data: ${JSON.stringify(v)}\n\n`, { headers: { "Content-Type": "text/event-stream" } }) const { fetch } = await chat(LISTING, () => sse({ statusCodeValue: 403, body: "" }))