diff --git a/packages/agy-cli/README.en.md b/packages/agy-cli/README.en.md index 2bdcd85..f3ffe5f 100644 --- a/packages/agy-cli/README.en.md +++ b/packages/agy-cli/README.en.md @@ -228,7 +228,10 @@ magpie quota refresh agy-cli --json ``` The numbers come from agy's own `/usage` report for whichever account is signed -in to the CLI, broken down by model group. The plugin never writes Google tokens +in to the CLI, broken down by model group. Each group's models share its +5-hour and weekly limits, so magpie shows the groups, and under "Every model" +each model the group names, reading the group's most used limit (the one that +stops it; Google's own per-model figure was the group's). The plugin never writes Google tokens or sends them anywhere but Google; it always reports the account agy is signed in to. A window with no measurable data shows "Limits not available" rather than a fake 0% or 100%. diff --git a/packages/agy-cli/README.md b/packages/agy-cli/README.md index 2b6967b..da697b6 100644 --- a/packages/agy-cli/README.md +++ b/packages/agy-cli/README.md @@ -161,7 +161,9 @@ magpie quota refresh agy-cli --json ``` 额度数据来自 agy 自己的 `/usage` 报告,显示的是当前 agy 登录账号的订阅窗口,按模型组 -分别列出。插件不写 Google 令牌,也只把它发往 Google,查询的始终是 agy 当前登录的账号。没有可测量数据的窗口会显示 +分别列出。同一组的模型共用这一组的 5 小时和每周额度,所以 Magpie 显示各组,并在「全部模型」 +里列出组里的每个模型,读数是这一组用得最多的那个额度(先用完的那个;Google 自己给每个模型的 +读数就是组的读数)。插件不写 Google 令牌,也只把它发往 Google,查询的始终是 agy 当前登录的账号。没有可测量数据的窗口会显示 "Limits not available",而不是 0% 或 100%。 在 Antigravity 未开放的地区,agy 会在资格检查时拒绝 `/usage`("not currently available in diff --git a/packages/agy-cli/model-ids.mjs b/packages/agy-cli/model-ids.mjs index cd7e20c..5650ce0 100644 --- a/packages/agy-cli/model-ids.mjs +++ b/packages/agy-cli/model-ids.mjs @@ -44,6 +44,7 @@ export function nameIndex() { index = new Map() if (rows) { const families = new Map() + const shown = new Map() // family -> its display name const claims = new Map() // display name -> the families that use it for (const model of rows) { const effort = /-(high|medium|low|minimal|xhigh|max)$/i.exec(model.id)?.[1]?.toLowerCase() || "" @@ -52,6 +53,8 @@ export function nameIndex() { families.get(family).add(model.id) const name = normalizeName(model.name) if (!name) continue + // the family's name as agy shows it, its effort word left off + if (!shown.has(family)) shown.set(family, stripEffortSuffix(String(model.name).trim().replace(/\s+/g, " "))) for (const alias of [name, stripEffortSuffix(name)]) { if (!claims.has(alias)) claims.set(alias, new Set()) claims.get(alias).add(family) @@ -65,29 +68,34 @@ export function nameIndex() { // one name, two families: no match, rather than whichever came first if (owners.size !== 1) continue const [family] = owners - index.set(name, { family, ids: families.get(family) }) + index.set(name, { family, ids: families.get(family), name: shown.get(family) || family }) } } return index } -// modelsInGroup answers the ids to put on a usage window. Group names are -// matched against the catalog's display names; a name that matches nothing -// is left out, as is a group whose description names no model and whose own -// name isn't a catalog name either. -export function modelsInGroup(group) { +// familiesInGroup answers the model families a usage group covers, each +// { id, name, ids }: the family id, its display name without the effort +// word ("Claude Opus 5.5"), and every id of it the catalog serves. Group +// names are matched against the catalog's display names; a name that +// matches nothing is left out, as is a group whose description names no +// model and whose own name isn't a catalog name either. +export function familiesInGroup(group) { const index = nameIndex() if (!index.size) return [] + const found = new Map() + const add = entry => { if (entry && !found.has(entry.family)) found.set(entry.family, entry) } + for (const name of listedNames(group?.description) || []) add(index.get(name)) + if (!found.size) { + add(index.get(normalizeName((group?.displayName || group?.display_name || group?.name || "").replace(/\s+models$/i, "")))) + } + return [...found.values()].map(e => ({ id: e.family, name: e.name, ids: [...e.ids].sort() })) +} + +// modelsInGroup answers the ids to put on a usage window: every id of +// every family the group covers (familiesInGroup). +export function modelsInGroup(group) { const ids = new Set() - const names = listedNames(group?.description) || [] - for (const name of names) { - const entry = index.get(name) - if (entry) for (const id of entry.ids) ids.add(id) - } - if (!ids.size) { - const own = normalizeName((group?.displayName || group?.display_name || group?.name || "").replace(/\s+models$/i, "")) - const entry = index.get(own) - if (entry) for (const id of entry.ids) ids.add(id) - } + for (const family of familiesInGroup(group)) for (const id of family.ids) ids.add(id) return [...ids].sort() } diff --git a/packages/agy-cli/package.json b/packages/agy-cli/package.json index 5aae27a..77049d7 100644 --- a/packages/agy-cli/package.json +++ b/packages/agy-cli/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-agy-cli-auth", - "version": "0.1.91", + "version": "0.1.92", "description": "Magpie/OpenCode model provider that connects your local agy CLI through the Agent Client Protocol (agy acp), so Antigravity subscription models run through the real CLI", "type": "module", "main": "./opencode.mjs", diff --git a/packages/agy-cli/test/smoke.mjs b/packages/agy-cli/test/smoke.mjs index c286c9a..2f4a31b 100644 --- a/packages/agy-cli/test/smoke.mjs +++ b/packages/agy-cli/test/smoke.mjs @@ -724,9 +724,11 @@ const legacyModel = "agy-cli/gemini-test-low" { const usage = await plugin.auth.usage(async () => ({ key: "agy" }), {}) - check("auth.usage exposes measured CLI quota", usage.windows?.[0]?.used === 40 && usage.windows[0].name === "Gemini · 7 days") - check("auth.usage preserves explicit cadence and reset", usage.windows?.[0]?.span === 604800 && usage.windows[0].resetsAt === "2026-10-27T00:00:00.000Z") - check("auth.usage is display-only and keeps bridge auth", usage.windows?.[0]?.aside === true && usage.signIn === "kept") + // the pool's own window, beside any model's in it + const pw = usage.windows?.find(w => !w.model) + check("auth.usage exposes measured CLI quota", pw?.used === 40 && pw.pool === "Gemini" && pw.name === "7 days") + check("auth.usage preserves explicit cadence and reset", pw?.span === 604800 && pw.resetsAt === "2026-10-27T00:00:00.000Z") + check("auth.usage is display-only and keeps bridge auth", usage.windows?.every(w => w.aside === true) && usage.signIn === "kept") check("auth.usage does not invent Google account identity", !usage.user && !usage.plan) const calls = logs().filter(event => event.type === "usage") check("auth.usage uses a separate cleaned workspace", calls.length === 1 && !fs.existsSync(calls[0].cwd)) diff --git a/packages/agy-cli/test/usage.mjs b/packages/agy-cli/test/usage.mjs index 181b613..b2ae0b4 100644 --- a/packages/agy-cli/test/usage.mjs +++ b/packages/agy-cli/test/usage.mjs @@ -73,7 +73,11 @@ const LIVE_MODEL_ROWS = [ test("a live report names on each window the ids the listed catalog serves", () => { storeCatalogRows(LIVE_MODEL_ROWS) const result = parseUsageReport(structuredClone(LIVE_REPORT)) - assert.deepEqual(result.windows.map(w => w.models), [ + const pools = result.windows.filter(w => !w.model) + assert.deepEqual(pools.map(w => [w.pool, w.name]), [ + ["Gemini", "7 days"], ["Gemini", "5 hours"], ["Claude & GPT", "7 days"], ["Claude & GPT", "5 hours"], ["Gemini 4 Argon", "7 days"], + ]) + assert.deepEqual(pools.map(w => w.models), [ ["gemini-3.8-flash", "gemini-3.8-flash-high", "gemini-3.8-flash-low", "gemini-3.8-flash-medium", "gemini-4-hydrogen", "gemini-4-hydrogen-high", "gemini-4-hydrogen-low", "gemini-4-hydrogen-medium", "gemini-4-hydrogen-xhigh"], ["gemini-3.8-flash", "gemini-3.8-flash-high", "gemini-3.8-flash-low", "gemini-3.8-flash-medium", "gemini-4-hydrogen", "gemini-4-hydrogen-high", "gemini-4-hydrogen-low", "gemini-4-hydrogen-medium", "gemini-4-hydrogen-xhigh"], ["claude-opus-5-5", "claude-opus-5-5-high", "claude-opus-5-5-low", "claude-opus-5-5-medium", "claude-sonnet-5-5", "claude-sonnet-5-5-high", "claude-sonnet-5-5-low", "claude-sonnet-5-5-medium", "gpt-oss-120b", "gpt-oss-120b-medium"], @@ -81,12 +85,53 @@ test("a live report names on each window the ids the listed catalog serves", () // Argon's description names no model; its group name is a catalog name. ["gemini-4-argon", "gemini-4-argon-high", "gemini-4-argon-low", "gemini-4-argon-medium"], ]) - assert.deepEqual(result.windows.map(w => w.used), [0, 0, 66.394961, 0, 0]) + assert.deepEqual(pools.map(w => w.used), [0, 0, 66.394961, 0, 0]) // agy names a family in the description even though it lists no bare id. - assert.ok(result.windows[2].models.includes("gpt-oss-120b")) + assert.ok(pools[2].models.includes("gpt-oss-120b")) + assert.ok(result.windows.every(w => w.aside === true)) storeCatalogRows(null) }) +test("each model family a group names has its own window in the pool, at the pool's most used bucket", () => { + storeCatalogRows(LIVE_MODEL_ROWS) + const result = parseUsageReport(structuredClone(LIVE_REPORT)) + const own = result.windows.filter(w => w.model) + assert.deepEqual(own.map(w => [w.name, w.model, w.family, w.pool, w.used, w.span, w.resetsAt]), [ + ["Claude Opus 5.5", "claude-opus-5-5", "Claude", "Claude & GPT", 66.394961, 604800, "2026-11-20T09:01:10.000Z"], + ["Claude Sonnet 5.5", "claude-sonnet-5-5", "Claude", "Claude & GPT", 66.394961, 604800, "2026-11-20T09:01:10.000Z"], + ["Gemini 3.8 Flash", "gemini-3.8-flash", "Gemini", "Gemini", 0, 604800, "2026-11-22T11:19:33.000Z"], + ["Gemini 4 Argon", "gemini-4-argon", "Gemini", "Gemini 4 Argon", 0, 604800, "2026-11-22T11:19:33.000Z"], + ["Gemini 4 Hydrogen", "gemini-4-hydrogen", "Gemini", "Gemini", 0, 604800, "2026-11-22T11:19:33.000Z"], + ["GPT-OSS 120B", "gpt-oss-120b", "GPT-OSS", "Claude & GPT", 66.394961, 604800, "2026-11-20T09:01:10.000Z"], + ]) + // a model's window names every id of its family, effort levels too + assert.deepEqual(own[0].models, ["claude-opus-5-5", "claude-opus-5-5-high", "claude-opus-5-5-low", "claude-opus-5-5-medium"]) + // the models' windows come first, then the pools', as the antigravity plugin sent them + assert.ok(result.windows.slice(0, own.length).every(w => w.model)) + + // the 5 hours used more than the week: a model reads the 5 hours + const busy = structuredClone(LIVE_REPORT) + busy.command.data.groups[0].buckets[1].remaining_fraction = 0.25 + const flash = parseUsageReport(busy).windows.find(w => w.model === "gemini-3.8-flash") + assert.deepEqual([flash.used, flash.span, flash.resetsAt], [75, 18000, "2026-11-15T16:19:33.000Z"]) + + // a model two groups name reads the one that stops it first + const twice = structuredClone(LIVE_REPORT) + twice.command.data.groups[2].description = "Models within this group: Gemini 4 Argon, Gemini 3.8 Flash" + twice.command.data.groups[2].buckets[0].remaining_fraction = 0.4 + const again = parseUsageReport(twice).windows.find(w => w.model === "gemini-3.8-flash") + assert.deepEqual([again.pool, again.used], ["Gemini 4 Argon", 60]) + + // a pool whose buckets say no share: its models get no window, not a guess + const blind = structuredClone(LIVE_REPORT) + for (const b of blind.command.data.groups[1].buckets) delete b.remaining_fraction + assert.ok(!parseUsageReport(blind).windows.some(w => w.model && w.pool === "Claude & GPT")) + storeCatalogRows(null) + + // no catalog: the pools' windows alone + assert.ok(!parseUsageReport(structuredClone(LIVE_REPORT)).windows.some(w => w.model)) +}) + test("ids on windows follow the catalog, and names agy adds match nothing until it lists them", () => { storeCatalogRows(LIVE_MODEL_ROWS) // A renamed model: the description keeps the old name, the catalog the new. @@ -120,7 +165,7 @@ test("maps all groups/cadences to display-only Magpie windows, without invented group([bucket({ id: "gemini-argon-weekly", window: "weekly", remaining_fraction: 0.53 })], "Gemini 4 Argon"), ])) assert.equal(result.windows.length, 5) - assert.deepEqual(result.windows.map(w => w.name), ["Gemini · 5 hours", "Gemini · 7 days", "Claude & GPT · 5 hours", "Claude & GPT · 7 days", "Gemini 4 Argon · 7 days"]) + assert.deepEqual(result.windows.map(w => w.pool + " · " + w.name), ["Gemini · 5 hours", "Gemini · 7 days", "Claude & GPT · 5 hours", "Claude & GPT · 7 days", "Gemini 4 Argon · 7 days"]) assert.deepEqual(result.windows.map(w => w.used), [25, 50, 100, 0, 47]) assert.deepEqual(result.windows.map(w => w.span), [18000, 604800, 18000, 604800, 604800]) assert.ok(result.windows.every(w => w.aside === true)) @@ -895,9 +940,9 @@ test("an account agy refuses for its region has its quota read the way agy reads const result = await createUsageReader(() => f.executable, { ...f.options, quota })() assert.equal(result.error, undefined) assert.equal(result.signIn, "kept") - assert.deepEqual(result.windows.map(w => [w.name, w.used, w.resetsAt]), [ - ["Gemini · 7 days", 25, "2026-11-22T11:19:33.000Z"], - ["Gemini · 5 hours", 50, "2026-11-16T16:19:33.000Z"], + assert.deepEqual(result.windows.map(w => [w.pool, w.name, w.used, w.resetsAt]), [ + ["Gemini", "7 days", 25, "2026-11-22T11:19:33.000Z"], + ["Gemini", "5 hours", 50, "2026-11-16T16:19:33.000Z"], ]) // the two requests agy makes, as it makes them assert.deepEqual(api.calls.map(c => [c.method, c.url, c.body]), [ diff --git a/packages/agy-cli/usage.mjs b/packages/agy-cli/usage.mjs index a285bb7..34ca3e9 100644 --- a/packages/agy-cli/usage.mjs +++ b/packages/agy-cli/usage.mjs @@ -3,7 +3,7 @@ import fs from "node:fs/promises" import os from "node:os" import path from "node:path" import { agySpawnSpec, childEnv as agyChildEnv } from "./platform.mjs" -import { modelsInGroup } from "./model-ids.mjs" +import { familiesInGroup } from "./model-ids.mjs" import { cannotSignIn, isDead, noteDead, storedSignIn, storedToken } from "./signin.mjs" const DEFAULT_TIMEOUT_MS = 15_000 @@ -161,8 +161,28 @@ function cadence(bucket, id, name) { return { name: name || id } } +// vendorOf is a model's family as magpie's cards group them (Gemini, +// Claude, GPT-OSS), as the retired antigravity plugin named them. +function vendorOf(id, name) { + const l = id.toLowerCase() + if (l.startsWith("gemini")) return "Gemini" + if (l.startsWith("claude")) return "Claude" + if (l.startsWith("gpt-oss")) return "GPT-OSS" + return String(name ?? "").trim().split(" ")[0] || id.split("-")[0] +} + // agy's command report is not its token-usage object or a model-availability list. // Unknown quota is not evidence of exhaustion or a full allowance. +// +// Each group is a pool its models share: its windows go as the pool's +// ({ pool: "Gemini", name: "5 hours" }), and each model family the group +// names gets a window of its own in that pool, reading the pool's most +// used bucket, the one that stops it. That is what Google's own per-model +// reading said: the retired antigravity plugin's fetchAvailableModels +// read of 2026-11-11 gave every model of a pool the pool's figure, to the +// digit (each Gemini model 1.58104% with Gemini's 7 days). So magpie's +// card shows the pools, and "Every model" lists the models, as it did for +// the antigravity plugin. export function parseUsageReport(value) { let report try { report = typeof value === "string" ? JSON.parse(value) : value } catch { @@ -173,9 +193,13 @@ export function parseUsageReport(value) { } const groups = report.command.data?.groups const windows = [] + const models = new Map() // family id -> its window for (const group of Array.isArray(groups) ? groups : []) { const pool = (text(field(group, "displayName", "display_name")) || text(group?.name) || "Quota") .replace(/\s+models$/i, "").replace(/\s+and\s+/gi, " & ") + const families = familiesInGroup(group) + const ids = [...new Set(families.flatMap(f => f.ids))].sort() + let tightest for (const bucket of Array.isArray(group?.buckets) ? group.buckets : []) { const id = text(field(bucket, "bucketId", "bucket_id")) || text(bucket?.id) if (!id || bucket?.disabled === true) continue @@ -187,15 +211,34 @@ export function parseUsageReport(value) { ?? (remaining?.case === "remainingFraction" ? remaining.value : undefined) const known = typeof fraction === "number" && Number.isFinite(fraction) && fraction >= 0 && fraction <= 1 const resetsAt = resetTime(field(bucket, "resetTime", "reset_time")) - const ids = modelsInGroup(group) - windows.push({ - name: `${pool} · ${period.name}`, + const w = { + name: period.name, + pool, // This feature displays quota only; it must not change model routing. aside: true, ...(ids.length ? { models: ids } : {}), ...(known ? { used: Math.round((1 - fraction) * 1e8) / 1e6 } : { display: "Limits not available" }), ...(period.span ? { span: period.span } : {}), ...(resetsAt ? { resetsAt } : {}), + } + windows.push(w) + if (known && (!tightest || w.used > tightest.used)) tightest = w + } + if (!tightest) continue + for (const f of families) { + const had = models.get(f.id) + // a model two groups name reads the one that stops it first + if (had && had.used >= tightest.used) continue + models.set(f.id, { + name: f.name, + model: f.id, + family: vendorOf(f.id, f.name), + pool, + aside: true, + models: f.ids, + used: tightest.used, + ...(tightest.span ? { span: tightest.span } : {}), + ...(tightest.resetsAt ? { resetsAt: tightest.resetsAt } : {}), }) } } @@ -203,7 +246,7 @@ export function parseUsageReport(value) { throw new UsageError("Limits not available: agy returned no enabled, measured quota buckets.") } // /usage has no verified account or plan identity. Do not borrow one elsewhere. - return { windows } + return { windows: [...[...models.values()].sort((a, b) => a.name.localeCompare(b.name)), ...windows] } } function timeout(value) {