diff --git a/packages/commandcode/CHANGELOG.md b/packages/commandcode/CHANGELOG.md new file mode 100644 index 0000000..6564b84 --- /dev/null +++ b/packages/commandcode/CHANGELOG.md @@ -0,0 +1,19 @@ +# Changelog + +## 0.1.20 + +- The Credits window of a Go, GOAT or Pro account says credits, not + dollars: "0.20 / 70.00 credits". Command Code counts those plans in + usage-value units ("on Go, GOAT and Pro, credits are usage-value units", + docs/resources/usage-limits, read 2026-10-31). GOAT's 70 credits a month + are the "up to $100" its pricing page now says on a $100 model like Ling + 3.1 Flash, and $70 on GLM-5.2; Pro's 80 are "up to $110". The 5-hour and + weekly caps billing/credits returns (GOAT's 14 and 35) are in the same + units, so the month stays 70 and 80, as the official CLI's table has it, + and the card no longer calls it a dollar pool (plugins #556). +- On those plans, bought and free credits (dollars at model cost) are said + beside the month ("+ $5.00 extra") and no longer added to a pool of + another unit. Max, Ultra and Teams Pro, where a credit is a dollar, keep + the CLI's pool in dollars. +- Provider is pay as you go: its $15 a month carries no credits, so its card + shows the balance and no longer a "$x / $15.00" month. diff --git a/packages/commandcode/index.mjs b/packages/commandcode/index.mjs index 3bdae8d..2050b7f 100644 --- a/packages/commandcode/index.mjs +++ b/packages/commandcode/index.mjs @@ -450,24 +450,34 @@ async function accountJSON(path, key, timeout = 15_000) { return res.json() } -// The plan names, by planId prefix, the longest first, with the dollars of -// credits each gives a month (the CLI's getPlanInfo; "individual-pro-v1" -// is the old Pro, $80 of them). +// The plan names, by planId prefix, the longest first, with the credits +// each gives a month (the CLI's getPlanInfo; "individual-pro-v1" is the +// old Pro, 80 of them), and whether those credits are usage-value units. +// On Go, GOAT and Pro a credit is not a dollar: Command Code's usage limits +// page (2026-10-31) says "on Go, GOAT and Pro, credits are usage-value +// units - full-allowance models like GLM-5.2 get $1 of usage per credit, +// and lower-allowance models draw proportionally more credits per dollar". +// GOAT's 70 credits are the "up to $100" its pricing page says on a $100 +// model, and $70 on GLM-5.2; Pro's 80 are "up to $110" (plugins #556). The +// windows billing/credits caps are counted in the same units (GOAT's 14 +// and 35). So the month of those plans is said in credits, not as a dollar +// pool none of their models spends. Provider is pay as you go, $15 a +// month for API access with no credits in it: no month of credits. const PLAN_NAMES = [ - ["individual-provider", "Provider", 15], - ["individual-pro-v1", "Pro", 80], - ["individual-goat", "GOAT", 70], + ["individual-provider", "Provider", 0], + ["individual-pro-v1", "Pro", 80, "units"], + ["individual-goat", "GOAT", 70, "units"], ["individual-ultra", "Ultra", 300], ["individual-max", "Max", 150], - ["individual-pro", "Pro", 30], - ["individual-go", "Go", 10], + ["individual-pro", "Pro", 30, "units"], + ["individual-go", "Go", 10, "units"], ["teams-pro", "Teams Pro", 40], ] const NO_PLAN = "No plan" function planOf(id) { const s = String(id ?? "").toLowerCase().replaceAll("_", "-") - return PLAN_NAMES.find(([p]) => s.startsWith(p)) ?? ["", "", 0] + return PLAN_NAMES.find(([p]) => s.startsWith(p)) ?? ["", "", 0, ""] } const planName = (id) => planOf(id)[1] @@ -539,6 +549,10 @@ function money(v) { return "$" + (v < 0 ? "-" : "") + s } +// credits is an amount of usage-value credits to the cent, as money +// rounds dollars, without the dollar sign. +const credits = (v) => money(v).replace("$", "") + // limits is the plan's 5-hour and weekly windows in a credits reply, // those it gives a cap. function limits(c) { @@ -557,19 +571,23 @@ function limits(c) { } // allowanceOf makes a credits reply into the plan's windows: the 5-hour -// and weekly limits, and the dollars as one more, used of the CLI's pool -// (the plan's month of credits, or what is left of it if more, and the -// bought and free ones) as its /usage bar is. The dollars, the plan's -// whole allowance, go second, after the 5-hour window: magpie's account -// line and menu bar show an account's first two windows, and with them -// last the Credits bar was never there (01huadalang on Discord). A reply with neither windows -// nor a plan tells a balance: a key's money, not an allowance. The month's -// credits come back at until, the end of the plan's period +// and weekly limits, and the month's credits as one more, used of the +// CLI's pool (the plan's month of credits, or what is left of it if more, +// and the bought and free ones) as its /usage bar is. The credits go +// second, after the 5-hour window: magpie's account line and menu bar show +// an account's first two windows, and with them last the Credits bar was +// never there (01huadalang on Discord). On a plan whose credits are +// usage-value units (Go, GOAT, Pro) the bar is the month's credits alone, +// said as credits, and the bought and free ones, dollars at model cost +// (the docs: "Buy extra credits at model cost"), are said beside it, not +// added to a pool of another unit (plugins #556). A reply with neither +// windows nor a plan tells a balance: a key's money, not an allowance. The +// month's credits come back at until, the end of the plan's period // (billing/subscriptions' currentPeriodEnd), when it is known // (yetone/magpie#2454). function allowanceOf(c, until) { const out = { windows: limits(c) } - const [, name, planMonthly] = planOf(c?.credits?.planId) + const [, name, planMonthly, unit] = planOf(c?.credits?.planId) if (name) out.plan = name let monthly = 0 let left = 0 @@ -582,9 +600,15 @@ function allowanceOf(c, until) { left += n known = true } - if (known && planMonthly > 0) { + const reset = until ? { resetsAt: until } : {} + if (known && planMonthly > 0 && unit === "units") { + const pool = Math.max(planMonthly, monthly) + const extra = left - monthly + const display = credits(pool - monthly) + " / " + credits(pool) + " credits" + (extra > 0 ? " + " + money(extra) + " extra" : "") + out.windows.splice(1, 0, { name: "Credits", used: Math.min(100, (100 * (pool - monthly)) / pool), display, ...reset }) + } else if (known && planMonthly > 0) { const pool = Math.max(planMonthly, monthly) + left - monthly - out.windows.splice(1, 0, { name: "Credits", used: Math.min(100, (100 * (pool - left)) / pool), display: money(pool - left) + " / " + money(pool), ...(until ? { resetsAt: until } : {}) }) + out.windows.splice(1, 0, { name: "Credits", used: Math.min(100, (100 * (pool - left)) / pool), display: money(pool - left) + " / " + money(pool), ...reset }) } else if (known && !out.windows.length) out.balance = money(left) return out } diff --git a/packages/commandcode/package.json b/packages/commandcode/package.json index e0caaeb..9efff44 100644 --- a/packages/commandcode/package.json +++ b/packages/commandcode/package.json @@ -1,11 +1,11 @@ { "name": "@magpie-community/opencode-commandcode-auth", - "version": "0.1.19", + "version": "0.1.20", "description": "Command Code plans (Pro, GOAT, Max, Ultra, Go, Teams) in OpenCode and magpie", "type": "module", "main": "./index.mjs", "exports": "./index.mjs", - "files": ["index.mjs", "README.md"], + "files": ["index.mjs", "README.md", "CHANGELOG.md"], "keywords": ["opencode", "opencode-plugin", "magpie", "commandcode"], "license": "MIT", "repository": { "type": "git", "url": "git+https://github.com/magpie-community/plugins.git", "directory": "packages/commandcode" } diff --git a/packages/commandcode/parity.test.mjs b/packages/commandcode/parity.test.mjs index 1163265..61f13ef 100644 --- a/packages/commandcode/parity.test.mjs +++ b/packages/commandcode/parity.test.mjs @@ -35,7 +35,7 @@ test("the Credits window renews when the plan's period ends", async () => { } const hooks = await CommandCodePlugin() const u = await hooks.auth.usage(async () => ({ type: "api", key: "k" })) - expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "$18.00 / $30.00", resetsAt: "2026-11-02T08:00:00.000Z" }]) + expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "18.00 / 30.00 credits", resetsAt: "2026-11-02T08:00:00.000Z" }]) expect(u.until).toBe("2026-11-02T08:00:00.000Z") }) @@ -48,7 +48,7 @@ test("no period end, no reset on the Credits window", async () => { } const hooks = await CommandCodePlugin() const u = await hooks.auth.usage(async () => ({ type: "api", key: "k" })) - expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "$6.00 / $10.00" }]) + expect(u.windows).toEqual([{ name: "Credits", used: 60, display: "6.00 / 10.00 credits" }]) }) // a list of every free id and two paid ones, as the Provider API gives it diff --git a/packages/commandcode/plan-credits.test.mjs b/packages/commandcode/plan-credits.test.mjs new file mode 100644 index 0000000..b2fa4ea --- /dev/null +++ b/packages/commandcode/plan-credits.test.mjs @@ -0,0 +1,75 @@ +// The month of credits each plan's card says (plugins #556). On Go, GOAT +// and Pro a credit is a usage-value unit, not a dollar: Command Code's +// usage limits page (2026-10-31) says GOAT's 14 credits in 5 hours and 35 +// in 7 days are "worth up to $20 / $50 of usage on a $100 model like Ling +// 3.1 Flash, or $14 / $35 on a $70 model like GLM-5.2", and Pro's 16 and +// 40 "up to $22 / $55 ... on a $110 model, or $16 / $40 on an $80 model". +// So the month is 70 credits on GOAT (up to $100) and 80 on Pro (up to +// $110), in the units billing/credits counts in, and the card says +// credits. Provider is pay as you go, with no credits in its $15. +import { afterEach, expect, test } from "bun:test" +import { readFileSync } from "node:fs" +import { join } from "node:path" +import { CommandCodePlugin, _internal } from "./index.mjs" + +const real = globalThis.fetch +afterEach(() => { + globalThis.fetch = real + _internal.subsSeen.clear() + _internal.extraSeen.clear() +}) + +const fixture = (name) => JSON.parse(readFileSync(join(import.meta.dir, "testdata", name), "utf8")) + +async function usage(sub, credits) { + _internal.subsSeen.clear() // a plan read is kept ten minutes + globalThis.fetch = async (url) => { + const path = new URL(String(url)).pathname + if (path === "/alpha/billing/subscriptions") return Response.json(sub) + if (path === "/alpha/billing/credits") return Response.json(credits) + return new Response("", { status: 404 }) + } + const hooks = await CommandCodePlugin() + return hooks.auth.usage(async () => ({ type: "api", key: "k" })) +} + +const plan = (planId) => ({ success: true, data: { planId, status: "active" } }) + +test("a real GOAT reply: its month is 70 credits, the units its windows count in, not a dollar pool", async () => { + const credits = fixture("credits-goat.json") + const sub = fixture("subscriptions-goat.json") + const u = await usage(sub, credits) + const month = u.windows.find((w) => w.name === "Credits") + expect(month).toEqual({ name: "Credits", used: (100 * (70 - 69.8033399)) / 70, display: "0.20 / 70.00 credits", resetsAt: "2026-10-28T16:16:42.000Z" }) + // the docs' GOAT windows: 14 and 35 credits for up to $20 and $50, and + // the month up to $100: 70 credits, the same units + expect(credits.windowLimits.fiveHour.cap).toBe(14) + expect(credits.windowLimits.weekly.cap).toBe(35) + expect((35 * 100) / 50).toBe(70) + expect(month.display).not.toContain("$") +}) + +test("Pro, Go and the old Pro say credits; Max, Ultra and Teams Pro, whose credit is a dollar, say dollars", async () => { + const shown = async (id, monthly) => (await usage(plan(id), { credits: { monthlyCredits: monthly } })).windows[0]?.display + expect(await shown("individual-pro-v1", 40)).toBe("40.00 / 80.00 credits") + expect(await shown("individual-go-v1", 4)).toBe("6.00 / 10.00 credits") + expect(await shown("individual-goat", 7)).toBe("63.00 / 70.00 credits") + expect(await shown("individual-max-monthly", 50)).toBe("$100.00 / $150.00") + expect(await shown("individual-ultra", 300)).toBe("$0.00 / $300.00") + expect(await shown("teams-pro", 10)).toBe("$30.00 / $40.00") +}) + +test("GOAT: bought and free credits, dollars at model cost, are said beside the month, not added to it", async () => { + const u = await usage(plan("individual-goat"), { credits: { monthlyCredits: 35, purchasedCredits: 4, freeCredits: 1 } }) + expect(u.windows).toEqual([{ name: "Credits", used: 50, display: "35.00 / 70.00 credits + $5.00 extra" }]) +}) + +test("Max keeps the CLI's pool, the month and the bought credits together, in dollars", async () => { + const u = await usage(plan("individual-max"), { credits: { monthlyCredits: 100, purchasedCredits: 10 } }) + expect(u.windows).toEqual([{ name: "Credits", used: (100 * 50) / 160, display: "$50.00 / $160.00" }]) +}) + +test("Provider is pay as you go: its card is the balance, not a $15 month", async () => { + const u = await usage(plan("individual-provider"), { credits: { monthlyCredits: 0, purchasedCredits: 7.25, freeCredits: 0 } }) + expect(u).toEqual({ signIn: "renewed", plan: "Provider", balance: "$7.25", windows: [] }) +}) diff --git a/packages/commandcode/usage.test.mjs b/packages/commandcode/usage.test.mjs index 99f2b8c..9208d17 100644 --- a/packages/commandcode/usage.test.mjs +++ b/packages/commandcode/usage.test.mjs @@ -69,7 +69,7 @@ test("Go: the month's $10 is the pool", async () => { "/alpha/billing/subscriptions": { data: { planId: "individual-go", status: "active" } }, "/alpha/billing/credits": { credits: { monthlyCredits: 4 } }, }) - expect(u).toEqual({ signIn: "renewed", plan: "Go", windows: [{ name: "Credits", used: 60, display: "$6.00 / $10.00" }] }) + expect(u).toEqual({ signIn: "renewed", plan: "Go", windows: [{ name: "Credits", used: 60, display: "6.00 / 10.00 credits" }] }) }) test("credits that can't be read keep the plan and say why", async () => { @@ -99,7 +99,7 @@ test("seconds and string amounts read as magpie reads them; a half cent goes to signIn: "renewed", plan: "Pro", until: new Date(1790000000 * 1000).toISOString(), - windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "$0.12 / $30.00", resetsAt: new Date(1790000000 * 1000).toISOString() }], + windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "0.12 / 30.00 credits", resetsAt: new Date(1790000000 * 1000).toISOString() }], }) }) @@ -126,7 +126,7 @@ test("a slow subscription: the windows go without it, and the next card has it", answer() await new Promise((r) => setTimeout(r, 20)) const next = await hooks.auth.usage(async () => api) - expect(next).toEqual({ signIn: "renewed", plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "$35.00 / $70.00" }] }) + expect(next).toEqual({ signIn: "renewed", plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "35.00 / 70.00 credits" }] }) }) test("a plan read is saved with the key, once", async () => { @@ -159,7 +159,7 @@ test("a plan saved stands in for a subscription slow to read: waited for a momen const t = Date.now() const u = await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT", planId: "individual-goat" } })) expect(Date.now() - t).toBeLessThan(3_000) - expect(u).toEqual({ signIn: "renewed", plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "$35.00 / $70.00" }] }) + expect(u).toEqual({ signIn: "renewed", plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "35.00 / 70.00 credits" }] }) answer() })