diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index ea6f37c..c3e2850 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -2128,7 +2128,20 @@ function when(v) { return new Date(Math.trunc(n < 1e12 ? n * 1000 : n)).toISOString() } -// parseUsage is the plan and each pool of credits the envelope tells. +// SPENT_IN_TURN are the pools Qoder spends one after another before an +// account stops: the plan's credits, the account's own add-on credits and, +// on a team, the organisation's shared add-on credits (docs.qoder.com/Credits +// and account/teams/shared-add-on-credits: the soonest to expire first, the +// plan's before add-on on the same day). The envelope's own +// totalUsagePercentage counts them as one too. So the account has left what +// they have left together: a plan used up beside add-on credits is no +// account used up (magpie-community/plugins#454). +const SPENT_IN_TURN = new Set(["Credits", "Add-on credits", "Shared credits"]) + +// parseUsage is the plan and each pool of credits the envelope tells. When +// more than one of the pools spent in turn holds credits, "Total credits" +// is what they hold together, the one routing counts, and each pool is +// shown beside it, set aside. function parseUsage(env) { if (env.displayMode === "enterprise") return { plan: "Enterprise" } const u = env.qoderUsage @@ -2148,11 +2161,22 @@ function parseUsage(env) { 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"}` }) + const unit = b.unit || "credits" + const w = { name: b.name || name, used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${unit}` } + out.windows.push(w) + if (SPENT_IN_TURN.has(name)) turn.push({ w, used: Math.min(used, total), total, unit }) } + const turn = [] add(field("userQuota", "user_quota"), "Credits") add(field("addOnQuota", "add_on_quota"), "Add-on credits") add(field("orgResourcePackage", "org_resource_package"), "Shared credits") + // one count only when every pool counts in the same unit + if (turn.length > 1 && turn.every((p) => p.unit === turn[0].unit)) { + const used = turn.reduce((a, p) => a + p.used, 0) + const total = turn.reduce((a, p) => a + p.total, 0) + for (const p of turn) p.w.aside = true + out.windows.unshift({ name: "Total credits", used: Math.min(100, (100 * used) / total), display: `${gfmt(used)} / ${gfmt(total)} ${turn[0].unit}` }) + } const dedicated = field("dedicatedResourcePackages", "dedicated_resource_packages") for (const b of Array.isArray(dedicated) ? dedicated : []) add(b, "Dedicated credits") return out diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 7c9b64f..eaefb35 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.43", + "version": "0.2.44", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship (VPC)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/usage.test.mjs b/packages/qoder/usage.test.mjs index 0879031..f8e4bfd 100644 --- a/packages/qoder/usage.test.mjs +++ b/packages/qoder/usage.test.mjs @@ -60,9 +60,10 @@ test("a refused device token is rotated, saved, and the usage read with it", asy 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: "Total credits", used: (100 * 45) / 170, display: "45 / 170 credits" }, + { name: "Credits", used: 25, display: "25 / 100 credits", aside: true }, + { name: "Add-on credits", used: 20, display: "10 / 50 credits", aside: true }, + { name: "Shared credits", used: 50, display: "10 / 20 credits", aside: true }, { name: "Team", used: 20, display: "2 / 10 credits" }, ], signIn: "kept", @@ -124,6 +125,56 @@ test("snake_case fields, an expiry, units, and pools too thin to show", () => { expect(_internal.when(0)).toBeUndefined() }) +// magpie-community/plugins#454 (chansee97): a Qoder CN trial whose plan's +// 300 credits are spent, with 245 of 1,100 add-on credits used, read 0% left +// in Routing. Qoder spends the add-on credits once the plan's are gone, so +// the account has 855 of 1,400 left. +test("a spent plan beside add-on credits left is no account used up (#454)", () => { + const u = _internal.parseUsage({ + displayMode: "qoder", + qoderUsage: { + userType: "personal_professional_trial", + usageType: "credits", + isQuotaExceeded: false, + userQuota: { total: 300, used: 300, remaining: 0, percentage: 1, unit: "credits" }, + addOnQuota: { total: 1100, used: 245, remaining: 855, percentage: 0.22, unit: "credits" }, + }, + }) + expect(u.windows).toEqual([ + { name: "Total credits", used: (100 * 545) / 1400, display: "545 / 1400 credits" }, + { name: "Credits", used: 100, display: "300 / 300 credits", aside: true }, + { name: "Add-on credits", used: (100 * 245) / 1100, display: "245 / 1100 credits", aside: true }, + ]) + // what routing counts: the windows not set aside + const counted = u.windows.filter((w) => !w.aside) + expect(counted.map((w) => Math.round(100 - w.used))).toEqual([61]) +}) + +test("one pool alone is shown as before, with nothing set aside", () => { + // the owner's account (2026-10-28, read-only): a plan of 0 and add-on credits + const u = _internal.parseUsage({ + displayMode: "qoder", + qoderUsage: { + userType: "personal_standard", + totalUsagePercentage: 0.02, + userQuota: { total: 0, used: 0, remaining: 0, percentage: 0, unit: "credits" }, + addOnQuota: { total: 600, used: 7, remaining: 593, percentage: 0.02, unit: "credits" }, + }, + }) + expect(u.windows).toEqual([{ name: "Add-on credits", used: (100 * 7) / 600, display: "7 / 600 credits" }]) +}) + +test("pools in different units are not added up", () => { + const u = _internal.parseUsage({ + displayMode: "qoder", + qoderUsage: { userQuota: { total: 100, used: 100, unit: "tokens" }, addOnQuota: { total: 50, used: 0, unit: "credits" } }, + }) + expect(u.windows).toEqual([ + { name: "Credits", used: 100, display: "100 / 100 tokens" }, + { name: "Add-on credits", used: 0, display: "0 / 50 credits" }, + ]) +}) + // the loader's fetch, as OpenCode's engine calls it, against a listing and // a chat reply async function chat(listing, reply) {