From 202ec68ffd7dfb3b899596037e17ceaabe06b964 Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 28 Oct 2026 10:29:31 +0800 Subject: [PATCH] qoder 0.2.44: a spent plan beside add-on credits left is no account used up (#454) chansee97's Qoder CN trial had its plan's 300 credits spent and 245 of 1,100 add-on credits used, and Routing read the account as 0% left: each pool was a window of its own, and routing counts the tightest. Qoder spends the pools one after another before an account stops (docs.qoder.com/Credits: soonest to expire first, the plan's before add-on on the same day; teams: then the organisation's shared add-on credits), and its own envelope's totalUsagePercentage counts them as one. So when more than one of the plan, add-on and shared pools holds credits in the same unit, "Total credits" is what they hold together (545 / 1400 here, 61% left) and the one routing counts, and each pool is shown beside it set aside, as CodeArts' buckets are under its total. One pool alone is shown as before. Dedicated packages are left as they were (no word from Qoder on their order). Run: bun test in packages/qoder (298 pass) and scripts/check.mjs, on mote. Broken by hand: the new tests against origin/main's index.mjs, red (#454's reads 0% left; the rotated-token test's windows differ). Read-only real read of the owner's Qoder account's /sash/api/v2/me/usage: plan total 0, add-on 7 / 600, which stays one window, unchanged. Not tried: a real account with both a plan and add-on credits (the owner's has no plan credits); the fields are the ones that read returns. --- packages/qoder/index.mjs | 28 +++++++++++++++-- packages/qoder/package.json | 2 +- packages/qoder/usage.test.mjs | 57 +++++++++++++++++++++++++++++++++-- 3 files changed, 81 insertions(+), 6 deletions(-) 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) {