Adds packages/amp-usage (@magpie-community/page-amp-usage 0.1.0). It is a magpie page that reads Amp's local thread files and the gateway's usage.jsonl and shows per-day usage. The service listens on loopback only and requires a per-start service token on every API call. The page that carries the token is served only to a navigation (Sec-Fetch-Mode). The gateway log is read incrementally from a saved offset, whole lines only, and is re-indexed on rotation. The UI words are in ui/words.js (en, zh).
Also bumps packages/day-card to 0.3.1.
Round 3 (13c1f33) adds three things:
- a test that bytes before the gateway-log offset are never re-read;
- ui.test.mjs, which checks that every error code the server can return has words in each language, and that an unknown code falls back;
- words.js, moved out of app.js, served at /words.js and included in the pack.
What ran, at 13c1f33 merged with main 14187408, on mica:
- amp-usage `node --test`: 47/47.
- `bun test packages/amp-usage packages/day-card`: 65 pass, 0 fail.
- `bun scripts/check.mjs`, all packages: ok.
- `npm pack --dry-run`: amp-usage 0.1.0 has 19 files including ui/words.js; day-card 0.3.1 has 17 files.
- Root `bun test` with a temp HOME: 8362 pass, 0 fail (8382 tests, 590 files).
- Render smoke in Chromium and WebKit at 1200 and 420px, en and zh: words.js loads, both languages show their own words, no page errors, no horizontal overflow.
Broken by hand, each run with node and bun:
- collector.mjs:102 made to re-read the whole file every scan: the new offset test goes red.
- `const cut = buf.length`, so a partial line is taken: red.
- Raw error codes shown instead of their words (`w.err[code] || code`): red.
- A zh string deleted: red.
Not tried:
- A real Amp account or the real `amp` CLI.
- Changing the auto-refresh default from 300 to 60 fails no test. The author left it untested and round 2 called it optional.
Rounds 1 and 2 also ran the 1M-line log probe (rss steady at about 363MB, scans of 70 to 114ms after the first), the bad-record and paging probes, and the full render matrix including dark mode.
154 lines
7.1 KiB
JavaScript
154 lines
7.1 KiB
JavaScript
// The page's words in each language, and how a service error code reads.
|
|
// A classic script, so the page and the tests share one copy.
|
|
globalThis.AmpUsage = {
|
|
words: {
|
|
en: {
|
|
title: "Amp usage",
|
|
today: "Today",
|
|
h24: "24 hours",
|
|
d7: "7 days",
|
|
d30: "30 days",
|
|
all: "All",
|
|
custom: "Custom",
|
|
refresh: "Refresh",
|
|
auto: "Refresh automatically",
|
|
off: "Off",
|
|
seconds: (n) => `${n} s`,
|
|
minutes: (n) => `${n} min`,
|
|
hourly: "Tokens per hour",
|
|
daily: "Tokens per day",
|
|
byModel: "By model",
|
|
model: "Model",
|
|
calls: "Requests",
|
|
input: "Input",
|
|
output: "Output",
|
|
cacheRead: "Cache read",
|
|
cacheWrite: "Cache write",
|
|
hit: "Cache hit rate",
|
|
share: "Share",
|
|
tokens: "tokens",
|
|
reading: "Reading Amp usage…",
|
|
scanned: (t) => `Read at ${t}`,
|
|
remaining: (n) => `${n} more threads to export`,
|
|
empty: "No Amp usage found yet.",
|
|
emptyPeriod: "No Amp usage in this period.",
|
|
issues: (n) => `${n} records left out`,
|
|
noTime: "No reliable time",
|
|
inProgress: "Still running or just changed",
|
|
excluded: "Excluded in plugin options",
|
|
held: "Inconsistent, held back rather than guessed",
|
|
howTitle: "How this is counted",
|
|
how1: "Input, output, cache reads and cache writes are kept apart. Amp credits are not turned into dollars; Magpie estimates the cost in its own usage pages.",
|
|
how2: "A request with no time of its own is dated by when its turn was sent, so a turn that runs past midnight counts on the day it began.",
|
|
credit: "Parser reference: Tokscale · MIT",
|
|
err: {
|
|
ampNotFound:
|
|
"Amp CLI not found. Install Amp and run amp login, or set ampPath in the plugin options.",
|
|
ampFailed:
|
|
"An Amp command failed. Check amp login and network access in your terminal.",
|
|
ampTimeout: "An Amp command timed out. Refresh to try again.",
|
|
ampTooLarge:
|
|
"An Amp thread export was too large to read; it was skipped.",
|
|
ampInvalidJSON:
|
|
"Amp returned output this plugin cannot read. Update Amp.",
|
|
ampListFormat:
|
|
"Amp's thread list changed format. Update Amp and this plugin.",
|
|
ampExportMismatch:
|
|
"Amp exported a different thread than asked; it was skipped.",
|
|
ampBudget: "Reading Amp took its time limit. Refresh to continue.",
|
|
cacheMismatch:
|
|
"A cached Amp thread did not match; it will be exported again.",
|
|
localDirInvalid:
|
|
"Thread directories in the plugin options must be absolute paths.",
|
|
localDirUnreadable: "A local thread directory cannot be read.",
|
|
localDirMissing:
|
|
"A local thread directory in the plugin options was not found.",
|
|
localThreadInvalid:
|
|
"A local thread was unreadable, oversized or invalid; it was left out.",
|
|
localThreadConflict:
|
|
"Two local copies of one thread disagree; it was left out.",
|
|
fileUnreadable:
|
|
"A file of this plugin's own cannot be read. Check the plugin log.",
|
|
magpieRefused:
|
|
"Magpie refused the usage records. Check the gateway address and Magpie version.",
|
|
magpieAck:
|
|
"Magpie answered in a way this plugin did not expect. Records will be sent again safely.",
|
|
magpieUnreachable:
|
|
"Magpie could not be reached. Check that it is running and the gateway address.",
|
|
reconnect: "Reopen the Amp usage page to reconnect.",
|
|
serviceError: "The local service failed. Check the plugin log.",
|
|
unreachable: "The local service is not answering. Reopen the page.",
|
|
unexpected: "Something went wrong. Check the plugin log.",
|
|
},
|
|
},
|
|
zh: {
|
|
title: "Amp 用量",
|
|
today: "今天",
|
|
h24: "24 小时",
|
|
d7: "7 天",
|
|
d30: "30 天",
|
|
all: "全部",
|
|
custom: "自定义",
|
|
refresh: "刷新",
|
|
auto: "自动刷新",
|
|
off: "关闭",
|
|
seconds: (n) => `${n} 秒`,
|
|
minutes: (n) => `${n} 分钟`,
|
|
hourly: "每小时 Token",
|
|
daily: "每日 Token",
|
|
byModel: "按模型",
|
|
model: "模型",
|
|
calls: "请求",
|
|
input: "输入",
|
|
output: "输出",
|
|
cacheRead: "缓存读取",
|
|
cacheWrite: "缓存写入",
|
|
hit: "缓存命中率",
|
|
share: "占比",
|
|
tokens: "token",
|
|
reading: "正在读取 Amp 用量…",
|
|
scanned: (t) => `读取于 ${t}`,
|
|
remaining: (n) => `还有 ${n} 个线程待导出`,
|
|
empty: "还没有读到 Amp 用量。",
|
|
emptyPeriod: "这个时间段没有 Amp 用量。",
|
|
issues: (n) => `${n} 条记录未计入`,
|
|
noTime: "没有可靠时间",
|
|
inProgress: "仍在生成或刚更新",
|
|
excluded: "已在插件选项中排除",
|
|
held: "数据前后不一致,保留不导入",
|
|
howTitle: "统计口径",
|
|
how1: "输入、输出、缓存读取、缓存写入分别统计。Amp credits 不折算成美元,费用由 Magpie 在自己的用量页估算。",
|
|
how2: "没有自身时间的请求,按所在轮次的发送时间计日;跨过午夜的一轮记在开始那天。",
|
|
credit: "解析参考:Tokscale · MIT",
|
|
err: {
|
|
ampNotFound:
|
|
"未找到 Amp CLI。请安装 Amp 并运行 amp login,或在插件选项中设置 ampPath。",
|
|
ampFailed: "Amp 命令执行失败。请在终端中检查 amp login 和网络连接。",
|
|
ampTimeout: "Amp 命令超时,请刷新重试。",
|
|
ampTooLarge: "有一个 Amp 线程导出过大,无法读取,已跳过。",
|
|
ampInvalidJSON: "Amp 返回了无法解析的内容,请更新 Amp。",
|
|
ampListFormat: "Amp 线程列表格式已变化,请更新 Amp 和本插件。",
|
|
ampExportMismatch: "Amp 导出的线程与请求不符,已跳过。",
|
|
ampBudget: "读取 Amp 已达单次时间上限,请刷新继续。",
|
|
cacheMismatch: "有一个缓存的 Amp 线程不一致,将重新导出。",
|
|
localDirInvalid: "插件选项中的线程目录必须是绝对路径。",
|
|
localDirUnreadable: "无法读取本地线程目录。",
|
|
localDirMissing: "插件选项中的本地线程目录不存在。",
|
|
localThreadInvalid: "有本地线程无法读取、过大或格式无效,已排除。",
|
|
localThreadConflict: "同一线程的两份本地副本不一致,已排除。",
|
|
fileUnreadable: "无法读取插件自身的文件,请查看插件日志。",
|
|
magpieRefused: "Magpie 拒绝了用量记录。请检查网关地址和 Magpie 版本。",
|
|
magpieAck: "Magpie 的回复不符合预期,记录稍后会安全重发。",
|
|
magpieUnreachable:
|
|
"无法连接 Magpie。请确认它正在运行,并检查网关地址。",
|
|
reconnect: "请重新打开 Amp 用量页面以重新连接。",
|
|
serviceError: "本地服务出错,请查看插件日志。",
|
|
unreachable: "本地服务没有响应,请重新打开页面。",
|
|
unexpected: "出现错误,请查看插件日志。",
|
|
},
|
|
},
|
|
},
|
|
// Unknown codes, such as one from a newer service, read as the generic one.
|
|
error: (w, code) => w.err[code] || w.err.unexpected,
|
|
};
|