Files
yetone 207880e1d8 agy-cli 0.1.92: usage shows each group as a pool and lists its models again
tongzhenbao on X: since moving to agy-cli, the usage card shows only three
rings ("Gemini …", "Gemini …", "Claude …"), and they want each model's
usage as before.

Before: the retired antigravity plugin sent one window per model
(fetchAvailableModels' quotaInfo, with family and pool) and the pools
from retrieveUserQuotaSummary, so magpie's card showed the pools
("Gemini 5h", "Gemini 7d", two lines a ring) and "Every model (n)"
listed the models. agy-cli sent only "Gemini · 7 days"-named windows with
no pool and no model, so magpie drew plain rings with the name cut short
and had no models to list.

Google's per-model figure is the pool's: the owner's last antigravity
read (quotas.json, 2026-11-11) gave every Gemini model 1.58104% with the
Gemini pool's 7 days, and every Claude/GPT model 6.7575% with Claude &
GPT's, to the digit. So nothing new is asked of Google; from agy's own
/usage:

- each bucket goes as its pool's window: { pool: "Gemini", name: "7 days" }
  (magpie's LineName is still "Gemini · 7 days", so quota history and a
  cap the user set keep their names);
- each model family a group names (familiesInGroup, from the catalog agy
  models listed) gets an aside window in that pool: its display name
  without the effort word, model = the family id, models = every id of
  it, family Gemini/Claude/GPT-OSS, reading the group's most used
  measured bucket with that bucket's span and reset. A model two groups
  name reads the one that stops it first. A group with no measured bucket
  gives its models no window; no catalog yet gives no model windows.

Every window stays aside, so routing is as before (magpie's allowanceOf
skips aside windows, and a pool's window holds only members that aren't).

Run (mica, mote over 18 load): HOME=$(mktemp -d) bun test in
packages/agy-cli: 41 pass, 0 fail.
Broken by hand: a model reading its group's first bucket instead of the
most used one; "each model family a group names has its own window in
the pool, at the pool's most used bucket" went red (1 fail), green again
restored.
Not tried: a real agy /usage read (the parse is the captured live report
of 11-15 and its agy models rows); magpie's rendering is checked in
magpie's GUI tests with this shape.
2026-11-18 08:22:40 +08:00

102 lines
4.3 KiB
JavaScript

// The runtime catalog `agy models` listed, so usage windows can name the ids
// the provider serves (usage.mjs stays import-free of index.mjs).
//
// rows(): [{ id, name, effort? }] — the shape `agy models` parses into
// (parseAgyModels), the saved catalog, and FALLBACK_MODELS.
//
// nameIndex(): display name ("Claude Opus 5.5", case- and space-insensitive)
// → the family id ("claude-opus-5-5") and every effort variant id agy listed
// in it, plus the bare family id itself even when agy lists no variant of
// that name (the catalog serves it as its default).
let rows = null
let index = new Map()
export function storeCatalogRows(list) {
rows = Array.isArray(list) ? list.filter(model => model && typeof model.id === "string" && model.id) : null
index = null
}
export function catalogRows() {
return rows ? [...rows] : null
}
const normalizeName = (value) => String(value ?? "").trim().toLowerCase().replace(/\s+/g, " ").replace(/[.,;]$/, "")
// `agy models` names its variants "Gemini 4 Hydrogen (High)" while usage
// groups say "Gemini 4 Hydrogen": match on the name without the effort
// suffix, too, so every variant of a family answers its display name.
const stripEffortSuffix = (name) => name.replace(/\s+\((?:high|medium|low|minimal|xhigh|max)\)$/i, "")
// In "Models within this group: A, B and C", the separator before "and" can
// be a comma (as agy prints today) or nothing at all.
function listedNames(description) {
const listed = /Models within this group:\s*(.+)/i.exec(String(description ?? ""))
if (!listed) return null
return listed[1]
.split(/,|\s+and\s+/i)
.map(normalizeName)
.filter(Boolean)
}
export function nameIndex() {
if (index) return index
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() || ""
const family = effort ? model.id.slice(0, -(effort.length + 1)) : model.id
if (!families.has(family)) families.set(family, new Set())
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)
}
}
for (const [family, ids] of families) {
// the bare id the catalog serves even when agy lists no variant of it
ids.add(family)
}
for (const [name, owners] of claims) {
// 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), name: shown.get(family) || family })
}
}
return index
}
// 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()
for (const family of familiesInGroup(group)) for (const id of family.ids) ids.add(id)
return [...ids].sort()
}