diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index eb68ea0..b48c3c5 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -727,6 +727,116 @@ async function bodyText(input, init) { return typeof b === "string" ? b : new TextDecoder().decode(b) } +// ---- usage -------------------------------------------------------------------- +// +// The account's allowance as magpie's built-in Qoder account shows it +// (internal/provider/qoder_usage.go): the account pages' usage, asked with +// the device token (not the job token the chat runs on), which is rotated +// with its own refresh token once Qoder refuses it. + +class UsageStatus extends Error { + constructor(status) { + super(`qoder usage: upstream HTTP ${status}`) + this.status = status + } +} + +// fetchUsage is the usage envelope, {displayMode, qoderUsage}. +async function fetchUsage(deviceToken) { + if (!String(deviceToken ?? "").trim()) throw new Error("qoder usage: missing device token") + let res + try { + res = await fetch(OPENAPI + "/sash/api/v2/me/usage", { + headers: { Accept: "application/json", Authorization: `Bearer ${deviceToken}`, "Cosy-ClientType": "10", "User-Agent": "Qoder" }, + signal: AbortSignal.timeout(20_000), + }) + } catch (e) { + throw new Error(`qoder usage: request failed: ${e?.message ?? e}`) + } + if (res.status !== 200) throw new UsageStatus(res.status) + let env + try { + env = JSON.parse(await res.text()) + } catch (e) { + throw new Error(`qoder usage: decode response: ${e?.message ?? e}`) + } + if (env?.displayMode !== "qoder" && env?.displayMode !== "enterprise") throw new Error("qoder usage: unknown display mode") + if (env.displayMode === "qoder" && (env.qoderUsage === undefined || env.qoderUsage === null)) throw new Error("qoder usage: missing quota data") + return env +} + +// refreshDevice trades the device refresh token for a new pair; it rotates +// too, so the caller saves it. +async function refreshDevice(refresh) { + if (!String(refresh ?? "").trim()) throw new Error("qoder device token refresh: missing refresh token; sign in again") + let r + try { + r = await openapi("/api/v1/deviceToken/refresh", { method: "POST", body: { refresh_token: refresh } }) + } catch (e) { + throw new Error(`qoder device token refresh: request failed: ${e?.message ?? e}`) + } + if (r.status !== 200) { + const err = `qoder device token refresh: upstream HTTP ${r.status}` + // the device token serves only the account pages (usage); chat runs on + // the job token, so a refused one doesn't lapse the account + if (r.status === 401 || r.status === 403) + throw new Error(`Qoder usage is unavailable: Qoder refused the account-page sign-in (chat still works) — sign in again to see usage (${err})`) + throw new Error(err) + } + const token = r.json?.token || r.json?.device_token || "" + if (!String(token).trim() || !String(r.json?.refresh_token ?? "").trim()) throw new Error("qoder device token refresh: incomplete token pair") + return { token, refresh: r.json.refresh_token } +} + +// gfmt is a number as Go's %g writes it. +function gfmt(n) { + const [m, e] = n.toExponential().split("e") + const x = Number(e) + if (x < -4 || x >= 6) return `${m}e${x < 0 ? "-" : "+"}${String(Math.abs(x)).padStart(2, "0")}` + return String(n) +} + +const RFC3339 = /^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(Z|[+-]\d\d:\d\d)$/i + +// when is a time Qoder gives: an RFC 3339 string, else seconds or +// milliseconds, as a number or its text. +function when(v) { + if (typeof v === "string" && RFC3339.test(v) && !isNaN(Date.parse(v))) return new Date(v).toISOString() + const n = typeof v === "number" ? v : typeof v === "string" && v.trim() !== "" ? Number(v.trim()) : NaN + if (!isFinite(n) || n <= 0) return undefined + return new Date(Math.trunc(n < 1e12 ? n * 1000 : n)).toISOString() +} + +// parseUsage is the plan and each pool of credits the envelope tells. +function parseUsage(env) { + if (env.displayMode === "enterprise") return { plan: "Enterprise" } + const u = env.qoderUsage + if (env.displayMode !== "qoder" || !u || typeof u !== "object" || Array.isArray(u)) return { error: "Qoder: missing quota data" } + const field = (a, b) => (u[a] !== undefined ? u[a] : u[b]) + const out = { windows: [] } + const plan = field("userType", "user_type") + if (typeof plan === "string") out.plan = plan + const until = when(field("expiresAt", "expires_at")) + if (until) out.until = until + const num = (v) => v === undefined || v === null || typeof v === "number" + const str = (v) => v === undefined || v === null || typeof v === "string" + const add = (b, name) => { + if (b === null || typeof b !== "object" || Array.isArray(b)) return + if (![b.total, b.cap, b.used, b.remaining].every(num) || !str(b.name) || !str(b.unit)) return + const total = b.total ?? b.cap + if (total == null || total <= 0 || (b.used == null && b.remaining == null)) return + const used = b.used != null ? b.used : total - b.remaining + if (used < 0) return + out.windows.push({ name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${b.unit || "credits"}` }) + } + add(field("userQuota", "user_quota"), "Credits") + add(field("addOnQuota", "add_on_quota"), "Add-on credits") + add(field("orgResourcePackage", "org_resource_package"), "Shared credits") + const dedicated = field("dedicatedResourcePackages", "dedicated_resource_packages") + for (const b of Array.isArray(dedicated) ? dedicated : []) add(b, "Dedicated credits") + return out +} + export async function QoderAuthPlugin({ client }) { // serializes checking, rotating and saving tokens: a refresh token is // spent once, so two refreshes would spend it twice @@ -761,6 +871,34 @@ export async function QoderAuthPlugin({ client }) { return l } + // deviceToken is a device token newer than the one Qoder just refused: + // the one saved since, else a rotated one, saved. + const deviceToken = (getAuth, attempted) => + locked(async () => { + const a = await getAuth() + if (a?.deviceToken !== attempted) return a?.deviceToken + const dt = await refreshDevice(a.deviceRefresh) + await client.auth.set({ path: { id: ID }, body: { ...a, deviceToken: dt.token, deviceRefresh: dt.refresh } }) + return dt.token + }) + + // usage is the account's allowance, magpie's own hook + const usage = async (getAuth) => { + try { + const cred = await fresh(getAuth) + let env + try { + env = await fetchUsage(cred.deviceToken) + } catch (e) { + if (!(e instanceof UsageStatus) || (e.status !== 401 && e.status !== 403)) throw e + env = await fetchUsage(await deviceToken(getAuth, cred.deviceToken)) + } + return parseUsage(env) + } catch (e) { + return { error: e?.message ?? String(e) } + } + } + const signedInError = (e) => errorResponse(e instanceof SignInGone ? { status: 401, message: e.message } : { status: 502, message: "Qoder: " + (e?.message ?? e) }) @@ -817,6 +955,7 @@ export async function QoderAuthPlugin({ client }) { } }, methods: [{ type: "oauth", label: "Sign in with Qoder", authorize: deviceSignIn }], + usage, }, async config(config) { config.provider ??= {} @@ -848,3 +987,6 @@ export async function QoderAuthPlugin({ client }) { }, } } + +// for tests +export const _internal = { parseUsage, gfmt, when } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 1d65cbe..67233bf 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.1.0", + "version": "0.1.1", "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 new file mode 100644 index 0000000..352773d --- /dev/null +++ b/packages/qoder/usage.test.mjs @@ -0,0 +1,122 @@ +// auth.usage tells what magpie's built-in Qoder account shows +// (internal/provider/qoder_usage.go), against Qoder's replies as its tests +// give them (qoder_test.go). +import { afterEach, expect, test } from "bun:test" +import { QoderAuthPlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => (globalThis.fetch = real)) + +const USAGE = { + displayMode: "qoder", + qoderUsage: { + userType: "pro", + userQuota: { total: 100, used: 25 }, + addOnQuota: { cap: 50, remaining: 40 }, + orgResourcePackage: { total: 20, used: 10 }, + dedicatedResourcePackages: [{ name: "Team", total: 10, used: 2 }], + }, +} + +const account = () => ({ + type: "oauth", + access: "jt-one", + refresh: "rt-one", + expires: Date.now() + 3_600_000, + accountId: "one@x", + uid: "u1", + deviceToken: "dt-old", + deviceRefresh: "drt-old", +}) + +async function plugin(serve) { + let auth = account() + const seen = [] + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + seen.push(u.pathname) + return serve(u.pathname, init) + } + const client = { auth: { set: async ({ body }) => (auth = body) } } + const hooks = await QoderAuthPlugin({ client }) + return { usage: () => hooks.auth.usage(async () => auth), seen, auth: () => auth } +} + +const json = (v, status = 200) => new Response(JSON.stringify(v), { status }) + +test("a refused device token is rotated, saved, and the usage read with it", async () => { + const p = await plugin((path, init) => { + if (path === "/api/v1/deviceToken/refresh") { + expect(JSON.parse(init.body)).toEqual({ refresh_token: "drt-old" }) + return json({ device_token: "dt-new", refresh_token: "drt-new" }) + } + if (path === "/sash/api/v2/me/usage") { + expect(init.headers["Cosy-ClientType"]).toBe("10") + return init.headers.Authorization === "Bearer dt-new" ? json(USAGE) : new Response("", { status: 401 }) + } + return new Response("", { status: 404 }) + }) + expect(await p.usage()).toEqual({ + plan: "pro", + windows: [ + { name: "Credits", used: 25, display: "25 / 100 credits" }, + { name: "Add-on credits", used: 20, display: "10 / 50 credits" }, + { name: "Shared credits", used: 50, display: "10 / 20 credits" }, + { name: "Team", used: 20, display: "2 / 10 credits" }, + ], + }) + expect(p.seen).toEqual(["/sash/api/v2/me/usage", "/api/v1/deviceToken/refresh", "/sash/api/v2/me/usage"]) + expect(p.auth()).toMatchObject({ deviceToken: "dt-new", deviceRefresh: "drt-new", access: "jt-one", refresh: "rt-one" }) +}) + +test("a refused device refresh says usage is unavailable, chat still working", async () => { + const p = await plugin((path) => new Response("", { status: path === "/api/v1/deviceToken/refresh" ? 403 : 401 })) + expect(await p.usage()).toEqual({ + error: + "Qoder usage is unavailable: Qoder refused the account-page sign-in (chat still works) — sign in again to see usage (qoder device token refresh: upstream HTTP 403)", + }) + expect(p.auth().deviceToken).toBe("dt-old") +}) + +test("another failure is Qoder's status", async () => { + const p = await plugin(() => new Response("", { status: 500 })) + expect(await p.usage()).toEqual({ error: "qoder usage: upstream HTTP 500" }) +}) + +test("an enterprise account has a plan and no windows", async () => { + const p = await plugin(() => json({ displayMode: "enterprise" })) + expect(await p.usage()).toEqual({ plan: "Enterprise" }) +}) + +test("an unknown display mode, or no quota, is an error", async () => { + let p = await plugin(() => json({ displayMode: "team" })) + expect(await p.usage()).toEqual({ error: "qoder usage: unknown display mode" }) + p = await plugin(() => json({ displayMode: "qoder", qoderUsage: null })) + expect(await p.usage()).toEqual({ error: "qoder usage: missing quota data" }) +}) + +test("snake_case fields, an expiry, units, and pools too thin to show", () => { + const u = _internal.parseUsage({ + displayMode: "qoder", + qoderUsage: { + user_type: "Pro Trial", + expires_at: 1790812800, // seconds + user_quota: { total: 2000000, remaining: 500000, unit: "tokens" }, + add_on_quota: { total: 0, used: 0 }, // no total + org_resource_package: { total: 10 }, // neither used nor remaining + dedicated_resource_packages: [{ total: 5, used: 9 }, { total: 5, remaining: 9 }, { total: "5", used: 1 }], + }, + }) + expect(u).toEqual({ + plan: "Pro Trial", + until: "2026-10-01T00:00:00.000Z", + windows: [ + { name: "Credits", used: 75, display: "1.5e+06 / 2e+06 tokens" }, + { name: "Dedicated credits", used: 100, display: "9 / 5 credits" }, + ], + }) + expect(_internal.when("2026-10-01T08:00:00+08:00")).toBe("2026-10-01T00:00:00.000Z") + expect(_internal.when(1790812800000)).toBe("2026-10-01T00:00:00.000Z") + expect(_internal.when("1790812800")).toBe("2026-10-01T00:00:00.000Z") + expect(_internal.when(0)).toBeUndefined() +})