diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 1d7bc80..77ed87e 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -1708,9 +1708,8 @@ const makePlugin = (site) => async ({ client }) => { try { m = (await models(cred)).find((x) => x.key === chat.model) ?? (await models(cred, true)).find((x) => x.key === chat.model) } catch (e) { - // the built-in answered any failure to read the list 400 - // (QoderModelOf), a refused one included, marking nothing - return errorResponse({ status: 400, message: e.message }) + if (e instanceof SignInGone || e?.expired) return signedInError(e) + return errorResponse({ status: 502, message: e?.message ?? String(e) }) } if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` }) const CHAT_URL = siteOf(site, cred).api + CHAT_PATH diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 85b0832..2b131fb 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.18", + "version": "0.2.19", "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 13554b6..8c339c3 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -181,18 +181,30 @@ 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" }) }) -test("a model list Qoder won't give is a 400, as the built-in's QoderModelOf answered, the account unmarked", async () => { +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" } globalThis.fetch = async () => new Response("no", { status }) const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } }) const l = await hooks.auth.loader(async () => auth) const res = await l.fetch(API_CHAT, ASK) - expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([400, null]) + expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([502, null]) expect((await res.json()).error.message).toBe(`Qoder models: HTTP ${status}: no`) } }) +test("a network failure fetching the model list is a 502 so magpie can fall back", async () => { + const auth = { ...account(), machineId: "m1" } + globalThis.fetch = async () => { + throw new Error("getaddrinfo ENOTFOUND api3.qoder.sh") + } + const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } }) + const l = await hooks.auth.loader(async () => auth) + const res = await l.fetch(API_CHAT, ASK) + expect([res.status, res.headers.get("X-Magpie-Sign-In")]).toEqual([502, null]) + expect((await res.json()).error.message).toBe("getaddrinfo ENOTFOUND api3.qoder.sh") +}) + // the loader's fetch for an account whose job token is due, the refresh answered by refresh() async function due(refresh, fields = {}) { const auth = { ...account(), machineId: "m1", expires: 0, ...fields }