diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index b48c3c5..9eeca9e 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -252,6 +252,15 @@ const MODELS = [ { id: "mmodel", name: "MiniMax-M3", context: 180_000 }, ].map((m) => ({ images: true, ...m })) +// freeOf reads whether the listing marks a model free, as Qoder's client +// reads it (internal/qoder/models.go): is_free true, or a price_factor (the +// credits a request costs, as a multiple) of 0, either in snake or camel case. +function freeOf(raw) { + const is = raw.is_free ?? raw.isFree + const price = raw.price_factor ?? raw.priceFactor + return is === true || (typeof price === "number" && price === 0) +} + // modelInfo reads one entry of the listing's "chat" array: its efforts and // default from thinking_config (is_reasoning alone when it has none). function modelInfo(raw) { @@ -265,6 +274,7 @@ function modelInfo(raw) { alwaysThinks: false, efforts: Array.isArray(raw.reasoning_efforts) ? [...raw.reasoning_efforts] : [], defaultEffort: "", + free: freeOf(raw), config: raw, } const tc = raw.thinking_config @@ -331,6 +341,8 @@ function runtimeModel(m) { }, release_date: "", variants: Object.fromEntries((m.efforts ?? []).map((e) => [e, { reasoningEffort: e }])), + // magpie's own: served at no cost to the plan's credits + free: !!m.free, } } @@ -478,9 +490,11 @@ function failure(status, text) { if (d?.error?.message) msg += ": " + d.error.message } catch {} msg ||= `HTTP ${status}` - if (status === 401 || status === 403) return { status: 401, message: "Qoder: the sign-in lapsed — sign in again" } + // only Qoder's 401 says the job token itself is refused; a 403 is kept as + // what it is, as magpie marks the account lapsed on a 401 + if (status === 401 || status === 403) return { status, message: "the sign-in lapsed — sign in again" } if (status === 429 || msg.toLowerCase().includes("quota")) return { status: 429, message: "usage limit reached: " + msg } - return { status, message: "Qoder: " + msg } + return { status, message: msg } } const errorResponse = ({ status, message }) => @@ -900,7 +914,7 @@ export async function QoderAuthPlugin({ client }) { } const signedInError = (e) => - errorResponse(e instanceof SignInGone ? { status: 401, message: e.message } : { status: 502, message: "Qoder: " + (e?.message ?? e) }) + errorResponse({ status: e instanceof SignInGone ? 401 : 502, message: String(e?.message ?? e).replace(/^Qoder: /, "") }) return { auth: { @@ -915,12 +929,12 @@ export async function QoderAuthPlugin({ client }) { async fetch(input, init = {}) { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url if (!/\/chat\/completions$/.test(new URL(url).pathname)) - return errorResponse({ status: 404, message: "Qoder: only chat completions are served" }) + return errorResponse({ status: 404, message: "only chat completions are served" }) let chat try { chat = JSON.parse(await bodyText(input, init)) } catch { - return errorResponse({ status: 400, message: "Qoder: a request that isn't JSON" }) + return errorResponse({ status: 400, message: "a request that isn't JSON" }) } let cred try { @@ -934,7 +948,7 @@ export async function QoderAuthPlugin({ client }) { } catch (e) { return errorResponse({ status: 502, message: e.message }) } - if (!m) return errorResponse({ status: 400, message: `Qoder: unknown or disabled model "${chat.model}"` }) + if (!m) return errorResponse({ status: 400, message: `unknown or disabled model "${chat.model}"` }) const wire = encodeBody(JSON.stringify(qoderBody(chat, m))) const headers = { ...cosyHeaders(CHAT_URL, cred, wire), @@ -947,7 +961,7 @@ export async function QoderAuthPlugin({ client }) { try { res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: init.signal }) } catch (e) { - return errorResponse({ status: 502, message: "Qoder: " + e.message }) + return errorResponse({ status: 502, message: e.message }) } if (!res.ok) return errorResponse(failure(res.status, (await res.text()).slice(0, 1 << 20))) return answer(res, chat) @@ -978,7 +992,7 @@ export async function QoderAuthPlugin({ client }) { const ms = await models(cred, true) if (!ms.length) return provider.models return Object.fromEntries( - ms.map((m) => [m.key, runtimeModel({ id: m.key, name: m.name, context: m.context, images: m.images, efforts: m.thinks ? m.efforts : [] })]), + ms.map((m) => [m.key, runtimeModel({ id: m.key, name: m.name, context: m.context, images: m.images, efforts: m.thinks ? m.efforts : [], free: m.free })]), ) } catch { return provider.models @@ -989,4 +1003,4 @@ export async function QoderAuthPlugin({ client }) { } // for tests -export const _internal = { parseUsage, gfmt, when } +export const _internal = { parseUsage, gfmt, when, failure, modelInfo } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 67233bf..6b698fa 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.1.1", + "version": "0.1.2", "description": "Qoder subscription in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/usage.test.mjs b/packages/qoder/usage.test.mjs index 352773d..c232937 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -43,6 +43,7 @@ async function plugin(serve) { } const json = (v, status = 200) => new Response(JSON.stringify(v), { status }) +const API_CHAT = "https://api3.qoder.sh/v1/chat/completions" test("a refused device token is rotated, saved, and the usage read with it", async () => { const p = await plugin((path, init) => { @@ -120,3 +121,56 @@ test("snake_case fields, an expiry, units, and pools too thin to show", () => { expect(_internal.when("1790812800")).toBe("2026-10-01T00:00:00.000Z") expect(_internal.when(0)).toBeUndefined() }) + +// the loader's fetch, as OpenCode's engine calls it, against a listing and +// a chat reply +async function chat(listing, reply) { + const auth = { ...account(), machineId: "m1", name: "One" } + globalThis.fetch = async (url) => { + const u = new URL(String(url)) + if (u.pathname.endsWith("/model/list")) return json(listing) + return reply() + } + const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } }) + const l = await hooks.auth.loader(async () => auth) + return { fetch: l.fetch, hooks, auth } +} + +const LISTING = { + chat: [ + { key: "qmodel", display_name: "Q", enable: true, max_input_tokens: 1000 }, + { key: "fmodel", display_name: "F", enable: true, is_free: true }, + { key: "pmodel", display_name: "P", enable: true, price_factor: 0 }, + { key: "cmodel", display_name: "C", enable: true, priceFactor: 0.5 }, + ], +} + +test("a refused chat is a 401 only when Qoder says 401; a 403 stays a 403", async () => { + for (const status of [401, 403]) { + const { fetch } = await chat(LISTING, () => new Response("nope", { status })) + const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "qmodel", messages: [{ role: "user", content: "hi" }] }) }) + expect(res.status).toBe(status) + expect((await res.json()).error.message).toBe("the sign-in lapsed — sign in again") + } +}) + +test("errors don't name Qoder, which magpie adds", async () => { + expect(_internal.failure(500, JSON.stringify({ message: "boom" }))).toEqual({ status: 500, message: "boom" }) + const { fetch } = await chat(LISTING, () => new Response("", { status: 500 })) + let res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "nomodel", messages: [] }) }) + expect((await res.json()).error.message).toBe('unknown or disabled model "nomodel"') + res = await fetch("https://api3.qoder.sh/v1/responses", { method: "POST", body: "{}" }) + expect((await res.json()).error.message).toBe("only chat completions are served") + const gone = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } }) + const l = await gone.auth.loader(async () => ({ type: "oauth" })) + res = await l.fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "qmodel", messages: [] }) }) + expect(res.status).toBe(401) + expect((await res.json()).error.message).toBe("not signed in") +}) + +test("a model Qoder lists free (is_free, or a price_factor of 0) is marked free", async () => { + const { hooks, auth } = await chat(LISTING, () => new Response("")) + const ms = await hooks.provider.models({ models: {} }, { auth }) + expect(Object.fromEntries(Object.entries(ms).map(([k, m]) => [k, m.free]))).toEqual({ qmodel: false, fmodel: true, pmodel: true, cmodel: false }) + expect(_internal.modelInfo({ key: "x", isFree: true }).free).toBe(true) +})