diff --git a/packages/zcode/index.mjs b/packages/zcode/index.mjs index fa6e383..f024bfa 100644 --- a/packages/zcode/index.mjs +++ b/packages/zcode/index.mjs @@ -282,13 +282,18 @@ const isNum = (v) => typeof v === "number" && Number.isFinite(v) // limitWindows are /api/monitor/usage/quota/limit's limits as windows: what // is used of the whole when both are told (the whole less what remains, or -// the current value), else the percentage given. +// the current value), else the percentage given. TIME_LIMIT is the month's +// MCP tool calls, which ZCode shows but never stops the models on, so it is +// set aside, as is a limit whose whole is told as 0: no cap (an older +// plan's), which the vendor may still give as 100% used. function limitWindows(d) { const out = [] for (const x of d?.limits ?? []) { const span = spanOf(Math.trunc(Number(x?.unit) || 0), x?.number) const w = { name: windowName(span), used: 0 } + if (String(x?.type ?? "").toUpperCase() === "TIME_LIMIT") (w.name = "MCP · Month"), (w.aside = true) if (isNum(x?.percentage)) w.used = x.percentage + if (x?.usage === 0) (w.used = 0), (w.aside = true) if (isNum(x?.usage) && x.usage > 0) { const total = x.usage if (isNum(x.remaining)) { diff --git a/packages/zcode/package.json b/packages/zcode/package.json index 4b72977..21fbb0a 100644 --- a/packages/zcode/package.json +++ b/packages/zcode/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-zcode-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "OpenCode provider plugin: signs in to Z.ai's or BigModel's (智谱) GLM Coding Plan as ZCode does, including team seats and ZCode's Start Plan", "type": "module", "main": "./index.mjs", diff --git a/packages/zcode/usage.test.mjs b/packages/zcode/usage.test.mjs index 402dce4..e63b4a2 100644 --- a/packages/zcode/usage.test.mjs +++ b/packages/zcode/usage.test.mjs @@ -78,7 +78,7 @@ test("windows, names and terms as Go reads them", () => { { name: "Weekly", used: 25, display: "200 / 800", span: 604800 }, { name: "0 hours", used: 7.5, span: 1800 }, { name: "3 days", used: 0, span: 3 * 86400 }, - { name: "Monthly", used: 0, span: 30 * 86400 }, + { name: "Monthly", used: 0, aside: true, span: 30 * 86400 }, // a whole of 0: no cap { name: "Credits", used: 0 }, ]) // Beijing times: the next renewal, auto or not; else the valid span's end @@ -91,6 +91,32 @@ test("windows, names and terms as Go reads them", () => { expect(_internal.termOf([{ status: "VALID", valid: "2026-09-18 - 2026-10-18" }])).toEqual({ until: "2026-10-17T16:00:00.000Z", renew: "off" }) }) +// Go's TestZCodeNoMonthlyCap (#366): the month's MCP tool calls are named +// and set aside as the built-in's are, so an older plan's uncapped month, +// told as 100% used, never reads as the account run out +test("the month's MCP calls: named and set aside, an uncapped month nothing used", () => { + const reset = Date.now() + 3 * 3600_000 + const month = Date.now() + 20 * 86400_000 + expect(_internal.limitWindows({ level: "pro", limits: [ + { type: "TOKENS_LIMIT", unit: 3, number: 5, percentage: 0, nextResetTime: reset }, + { type: "TIME_LIMIT", unit: 5, number: 1, usage: 0, currentValue: 0, remaining: 0, percentage: 100, nextResetTime: month }, + ] })).toEqual([ + { name: "5 hours", used: 0, resetsAt: new Date(reset).toISOString(), span: 5 * 3600 }, + { name: "MCP · Month", used: 0, aside: true, resetsAt: new Date(month).toISOString(), span: 30 * 86400 }, + ]) + // capped and used up: still shown as used, still aside + const ws = _internal.limitWindows({ limits: [ + { type: "TOKENS_LIMIT", unit: 3, number: 5, percentage: 10, nextResetTime: reset }, + { type: "TOKENS_LIMIT", unit: 6, number: 1, percentage: 30, nextResetTime: month }, + { type: "TIME_LIMIT", unit: 5, number: 1, usage: 100, currentValue: 100, remaining: 0, percentage: 100, nextResetTime: month }, + ] }) + expect(ws.map((w) => [w.name, w.used, !!w.aside, w.display])).toEqual([ + ["5 hours", 10, false, undefined], + ["Weekly", 30, false, undefined], + ["MCP · Month", 100, true, "100 / 100"], + ]) +}) + // a Start Plan with one bucket, GLM-5.1's tokens for the day, a quarter used function startBalance(now, status) { return { server_time: now, plans: [{ plan_id: "zai-start-plan", user_plan_id: "up1", name: "Start Plan", status, ends_at: now + 7 * 86400,