diff --git a/packages/kiro/index.mjs b/packages/kiro/index.mjs index f2e2725..b5e8129 100644 --- a/packages/kiro/index.mjs +++ b/packages/kiro/index.mjs @@ -414,7 +414,10 @@ function goG(x) { // usageOf is the allowance Get-Usage-Limits tells, as magpie's built-in // said it: the account's email, the credits of each allowance, the month's and a free trial's -// while it runs. +// while it runs. Each window carries its count (amount, limit, unit), so +// magpie can say what is left of it when the user shows what is left +// (#408): display alone is the vendor's text, which magpie can't turn +// round. function usageOf(l) { const at = (secs) => (secs > 0 ? new Date(Math.trunc(secs) * 1000).toISOString() : undefined) const count = (used, limit) => used.toFixed(2).replace(/0+$/, "").replace(/\.$/, "") + " / " + goG(limit) @@ -424,13 +427,14 @@ function usageOf(l) { const t = u?.freeTrialInfo if (t && String(t.freeTrialStatus ?? "").toUpperCase() === "ACTIVE" && num(t.usageLimitWithPrecision) > 0) { const used = num(t.currentUsageWithPrecision), limit = num(t.usageLimitWithPrecision) - windows.push({ name: "Free trial", used: (100 * used) / limit, resetsAt: at(num(t.freeTrialExpiry)), display: count(used, limit) }) + windows.push({ name: "Free trial", used: (100 * used) / limit, resetsAt: at(num(t.freeTrialExpiry)), display: count(used, limit), + amount: used, limit, unit: "credits" }) } const used = num(u?.currentUsageWithPrecision), limit = num(u?.usageLimitWithPrecision) if (limit <= 0) continue const reset = num(u?.nextDateReset) || num(l?.nextDateReset) windows.push({ name: u?.displayNamePlural || u?.displayName || "Credits", used: (100 * used) / limit, resetsAt: at(reset), - display: count(used, limit), span: 30 * 24 * 3600 }) + display: count(used, limit), amount: used, limit, unit: "credits", span: 30 * 24 * 3600 }) } const email = l?.userInfo?.email return { plan: planName(l?.subscriptionInfo?.subscriptionTitle), ...(typeof email === "string" && email ? { user: email } : {}), windows } diff --git a/packages/kiro/package.json b/packages/kiro/package.json index 24265b4..e94349a 100644 --- a/packages/kiro/package.json +++ b/packages/kiro/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kiro-auth", - "version": "0.1.16", + "version": "0.1.17", "description": "Kiro subscriptions (Free, Pro, Pro+, Power) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/kiro/usage.test.mjs b/packages/kiro/usage.test.mjs index 291d917..c1a475d 100644 --- a/packages/kiro/usage.test.mjs +++ b/packages/kiro/usage.test.mjs @@ -45,8 +45,10 @@ test("a free account's credits and trial", async () => { plan: "Kiro Free", user: "me@example.com", windows: [ - { name: "Free trial", used: 20, resetsAt: new Date(1791000000_000).toISOString(), display: "100 / 500" }, - { name: "Credits", used: 25, resetsAt: new Date(1790000000_000).toISOString(), display: "12.5 / 50", span: 30 * 24 * 3600 }, + { name: "Free trial", used: 20, resetsAt: new Date(1791000000_000).toISOString(), display: "100 / 500", + amount: 100, limit: 500, unit: "credits" }, + { name: "Credits", used: 25, resetsAt: new Date(1790000000_000).toISOString(), display: "12.5 / 50", + amount: 12.5, limit: 50, unit: "credits", span: 30 * 24 * 3600 }, ], signIn: "kept", }) @@ -70,6 +72,8 @@ test("names, resets and counts as Go says them", () => { ["Credits", "150.1 / 100", new Date(1800000000_000).toISOString()], ]) expect(windows[1].used).toBeCloseTo(150.1) // the host clamps it to 100; Go's built-in didn't + // #408: the count goes along, so magpie's "left" shows 0 / 100, not the used text + expect(windows.map((w) => [w.amount, w.limit, w.unit])).toEqual([[1.005, 1000000, "credits"], [150.1, 100, "credits"]]) }) test("Kiro's refusal is the card's error", async () => {