Files
plugins/packages/page-usage-tree
CHENZUDE 1e060e6163 page-usage-tree 0.1.1: refresh warning only on a real failure, hit rate and model-detail fixes (#960)
page-usage-tree 0.1.1 (CHENZUDE):

- A failed refresh is kept on the cache entry the page reads, so
  "Refresh failed; showing the previous data" shows when a refresh threw
  (on 0.1.0 it was written to an entry already replaced and never shown).
  Only a loader that threw counts: an older disk cache marked stale does
  not, and a retry from a failed entry stays failed until a loader succeeds.
  The page takes the server's X-Usage-Tree-Refresh on every answer, quiet
  follow-ups included, so the warning clears once the gateway recovers.
- Hit rate uses magpie's hitPct rounding (0.9996 shows 99.96%, not 100%).
- A failed per-account model-detail read keeps the account totals and says
  model details are incomplete (zh, en). Disk cache v8.
- Not in the PR body: a /bridge.js shim served by the service (talks only to
  parent: hello, options.get/set, theme and visibility), request timeout
  10s -> 30s, narrow breakpoint 620px -> 700px, model split on narrow
  screens, "permissions": [] added (same as absent), zh warnings joined
  with ";".

Run at the reviewed head 74363201570d9f69866ac0e4afff7a2b898e6dab:
- WSL (magpie-desktop), fresh HOME: bun test packages/page-usage-tree
  62 pass 1 skip (parity needs a live magpie); node --test 68 pass 1 skip;
  bun scripts/check.mjs ok.
- Windows (xiaomi-gpu), fresh HOME and USERPROFILE: bun test
  packages/page-usage-tree exit 0; bun scripts/check.mjs ok. CI green.
- Playwright 1.62 Chromium and WebKit on WSL, sandbox server with fake
  data, 1280 and 420px, en and zh: a first open from a stale disk cache
  never shows the refresh warning; a failed manual refresh shows it, it
  stays while the gateway is down, and clears on the quiet follow-ups after
  recovery while the model-detail warning stays; no page errors, no
  horizontal scroll. The same probe on e3da32bed fails 3 checks (false
  warning on open, warning never clears, zh joined by a space).
- Broken by hand: counting data.stale as failed, a retry entry forgetting
  the failure, and a quiet load never clearing each turn a test red.
  Not covered by a test: dropping warningStateChanged (render also runs
  renderWarnings) and the zh join between two data warnings.
- Host/Origin/token handling on /api/tree unchanged from main; /bridge.js
  is static and behind the Host check. dist had only 0.1.0.
2026-11-11 15:18:33 +08:00
..

中文说明

在 Magpie 窗口中按 供应商 -> 账号 -> 模型 查看用量,逐级展开调用、错误、 Token、缓存和费用。它补充「用量速览」没有的账号层:数据由插件自己的本地服务 向 Magpie 网关查询,页面不直接读取 Magpie 的配置文件。

@magpie-community/page-usage-tree

English

A page in Magpie's window that shows usage as a three-level tree: provider -> account -> model. Each level expands into the next, with calls, errors, tokens, cache and cost at every level.

Usage Glance (@magpie-community/page-usage) shows totals and per-model usage from usage:summary. This page adds the provider -> account -> model tree, including calls, errors and cost for each account. usage:summary deliberately contains no per-account data, so this page asks Magpie's local gateway for the usage it already counted.

magpie plugin add @magpie-community/page-usage-tree

Architecture

  • page/index.html and page/boot.js are the launcher Magpie sandboxes. They read the theme, language, theme tokens and this plugin's own options, then navigate the frame to the local service. They never probe the service first: a plugin page's CSP uses connect-src 'none'.
  • ui/app.html, ui/app.js, ui/view.js, ui/i18n.js and ui/style.css are the page, served by the local service. It uses Magpie's own --magpie-* colours, font and control shapes.
  • index.mjs is the OpenCode v1 host part. The service runs inside Magpie's plugin host, so it stops when Magpie stops the plugin.
  • magpie.page.service lets the same page work through magpie web.
  • lib/gateway.mjs asks the gateway for GET /v1/magpie/usage?period=... and GET /v1/magpie/usage/requests?period=...&account=.... The gateway applies Magpie's own accounting and pricing before this page groups the answer.
  • No CLI is started, no providers.json, settings.json, fxrate.json or usage.jsonl is read, and there is no ledger fallback.
  • Each period's aggregate is cached under ~/.cache/magpie/usage-tree/<period>.json. A page shows the last read at once and the service reads again behind it. Refresh (fresh=1) reads past the cache. The page opens on today.
  • Calls and errors are Magpie's: a try handed on to another account is not a call of its own, and API-key calls are a clearly marked group, not an account. Session-file rows are included because the gateway counted them.
  • The page speaks Chinese and English. Every other language falls back to English, and the launcher follows the window's language too.

Currency and units

The gateway reports cost in USD, Magpie's default. The page shows $ unless Magpie's page bridge supplies its own display settings (for example currency, chineseUnits and an exchange rate), or this plugin's options explicitly set currency: "cny" and a usdCny rate. Only then does it convert for display. Token counts use K, M and B; Chinese 万 and 亿 are used only when the language is Chinese and chineseUnits is on.

The current Magpie page bridge exposes language and theme, but not the global Settings preferences. Until those fields are supplied by the bridge, the plugin's own options remain the supported fallback. Theme and visibility callbacks re-read those options once; no Magpie settings file is read, no configuration read is declared, and nothing is polled.

Token measure

Tokens include cache is on by default. It controls whether cache reads and writes are part of the token measure used by the summary, row values, token shares and token sorting. Turning it off leaves the input + output total visible while keeping cache in the detail line. The page remembers the choice. The detail line also shows Magpie's cache hit rate: cache reads over the counted prompt, with calls whose vendor reports no cache left out of the denominator. As in Magpie's own usage view, a row with no cache reads omits the rate; a row with cache-uncounted calls says how many calls were left out. A mixed row shows both notes. Hit-rate percentages use Magpie's rounding rule, so a rate below 100% is never displayed as a misleading 100%. If an account's per-model request read fails, the page keeps its account totals and says that model details are temporarily incomplete.

Security

  • Listens on 127.0.0.1 only.
  • Uses a random token for every /api/tree and /api/settings request.
  • Checks Host and Origin; the local sandbox's opaque origin is allowed for the page API. CORS preflight is answered for both page APIs.
  • Reads nothing from Magpie's configuration files. It only calls the local gateway.
  • Aggregation stays local; no data is uploaded.
  • If another copy of this plugin owns the port, /healthz says which version it is. The same build is reused. A different build, or another service, is reported as an occupied port and is never killed: the listener may be the plugin host that serves every plugin.

Options

{
  "port": 3494,
  "currency": "usd",
  "chineseUnits": false,
  "usdCny": 0
}

currency: "cny" also needs usdCny for a meaningful conversion. Without a rate the page keeps showing USD rather than inventing one.

Run and test

node serve.mjs --port 3494
node --test --test-force-exit test/*.test.mjs

test/parity.test.mjs compares the gateway aggregation with magpie usage 7d when a CLI and gateway are available. API-key groups are excluded from the account-count comparison because Magpie's accounts table does not list them.

test/lifecycle.test.mjs checks that the same build reuses a running service, that another build is reported and left running, and that a host on its way out releases the port.