diff --git a/open-sse/services/usage/commandcode.js b/open-sse/services/usage/commandcode.js index 0f4e3065..1b86f635 100644 --- a/open-sse/services/usage/commandcode.js +++ b/open-sse/services/usage/commandcode.js @@ -157,19 +157,23 @@ export async function getCommandCodeUsage(apiKey, proxyOptions = null) { }); } - // Monthly credits consumed this billing period (from summary when present, - // else the credits object's monthlyCredits as a fallback). + // The credits API reports remaining balances (monthly/purchased/free), + // not a total. The official CLI renders the monthly line as + // `used = summary.totalCost`, `total = totalCost + remaining` — i.e. + // the plan ceiling is the sum of what was consumed and what is left. const monthlyUsed = typeof summary?.totalCredits === "number" ? summary.totalCredits - : typeof credits?.credits?.monthlyCredits === "number" - ? credits.credits.monthlyCredits + : typeof summary?.totalCost === "number" + ? summary.totalCost : 0; - const monthlyTotal = - typeof credits?.credits?.monthlyCredits === "number" - ? credits.credits.monthlyCredits - : 0; + const creditsObj = credits?.credits || {}; + const remaining = + Math.max(0, Number(creditsObj.monthlyCredits) || 0) + + Math.max(0, Number(creditsObj.purchasedCredits) || 0) + + Math.max(0, Number(creditsObj.freeCredits) || 0); + const monthlyTotal = monthlyUsed + remaining; if (monthlyTotal > 0 || monthlyUsed > 0) { quotas["Monthly credits"] = makeQuota({ diff --git a/tests/unit/commandcode-usage.test.js b/tests/unit/commandcode-usage.test.js index 60aa6821..22ade81b 100644 --- a/tests/unit/commandcode-usage.test.js +++ b/tests/unit/commandcode-usage.test.js @@ -113,9 +113,11 @@ describe("getUsageForProvider(commandcode)", () => { total: 6, resetAt: new Date(1786379982640).toISOString(), }); + // monthlyCredits/purchasedCredits/freeCredits are remaining balances; + // total = consumed (summary.totalCredits) + remaining, matching the CLI. expect(usage.quotas["Monthly credits"]).toMatchObject({ used: 0.1, - total: 9.9, + total: 10, }); }); @@ -194,8 +196,8 @@ describe("parseQuotaData(commandcode)", () => { }, "Monthly credits": { used: 0.1, - total: 9.9, - remainingPercentage: 98.99, + total: 10, + remainingPercentage: 99, resetAt: null, unit: "$", }, @@ -205,14 +207,11 @@ describe("parseQuotaData(commandcode)", () => { name: "5-hour window", used: 0.05, total: 3, - remainingPercentage: 98.33, - unit: "$", }); expect(rows[1]).toMatchObject({ name: "Monthly credits", used: 0.1, - total: 9.9, - remainingPercentage: 98.99, + total: 10, }); }); });