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.
157 lines
6.5 KiB
JavaScript
157 lines
6.5 KiB
JavaScript
(function (root) {
|
||
const WORDS = {
|
||
zh: {
|
||
title: "账号用量",
|
||
subtitle: "先看账号,再展开查看它使用的模型",
|
||
periods: { today: "今天", "7d": "7 天", "30d": "30 天", all: "全部" },
|
||
refresh: "刷新用量",
|
||
period: "统计周期",
|
||
summary: "用量汇总",
|
||
accounts: "账号",
|
||
providers: "供应商",
|
||
calls: "调用",
|
||
tokens: "输入 + 输出 Token",
|
||
tokensWithCache: "输入 + 输出 + 缓存 Token",
|
||
tokensWithoutCache: "输入 + 输出 Token",
|
||
includeCache: "Token 含缓存",
|
||
includeCacheHint: "切换是否把缓存 Token 计入总量、排序和占比",
|
||
cost: "估算费用",
|
||
models: "模型",
|
||
detail: "账号明细",
|
||
search: "搜索账号、供应商或模型",
|
||
sort: "排序",
|
||
sortBy: { tokens: "按 Token", cost: "按费用", calls: "按调用", name: "按名称" },
|
||
expand: "展开全部",
|
||
collapse: "收起全部",
|
||
columns: {
|
||
identity: "供应商 / 账号",
|
||
calls: "调用",
|
||
tokens: "Token",
|
||
share: "占比",
|
||
cost: "费用",
|
||
status: "状态",
|
||
},
|
||
model: "模型",
|
||
token: "Token",
|
||
modelTokensWithCache: "Token + 缓存",
|
||
modelTokensWithoutCache: "Token",
|
||
allOk: "正常",
|
||
failed: "失败",
|
||
unpriced: "次未定价",
|
||
noCalls: "这个周期还没有账号用量",
|
||
noMatch: "没有匹配的账号",
|
||
noCallsHint: "通过 Magpie 网关产生调用后,这里会按账号汇总。",
|
||
noMatchHint: "试试搜索其他账号、供应商或模型。",
|
||
noData: "没有读到用量数据",
|
||
reload: "重新加载",
|
||
firstRead: "首次读取约需数秒",
|
||
source: "只读聚合",
|
||
updated: "更新于",
|
||
input: "入",
|
||
output: "出",
|
||
cache: "缓存",
|
||
cacheHit: "缓存命中率",
|
||
cacheUnknown: (calls) => `${calls} 次未统计缓存`,
|
||
share: { tokens: "Token 占比", cost: "费用占比", calls: "调用占比", name: "Token 占比" },
|
||
unpricedOnly: "未定价",
|
||
unpricedCalls: (n) => `${n} 次未定价`,
|
||
noTime: "无时间",
|
||
justUsed: "刚刚使用",
|
||
usedRecently: "最近使用",
|
||
minutesAgo: (n) => `${n} 分钟前`,
|
||
hoursAgo: (n) => `${n} 小时前`,
|
||
readFailed: "无法读取用量数据",
|
||
refreshFailed: "刷新失败,仍显示上次数据",
|
||
apiKeyNote: "API key 调用,不计入账号",
|
||
accountsAndModels: (a, m) => `${a} 个账号 · ${m} 个模型`,
|
||
modelsOnly: (m) => `${m} 个模型`,
|
||
providerModels: (p, m) => `${p} 个供应商 · ${m} 个模型`,
|
||
hostNotes: (items) => `含 ${items}`,
|
||
noUsage: "这个周期还没有用量:Magpie 网关记录第一次调用后会显示在这里。",
|
||
modelDetails: (count) => `${count} 个账号的模型明细暂时无法读取,账号汇总仍完整。`,
|
||
serviceTimeout: "数据请求超过 30 秒。请检查 Magpie 网关是否可访问。",
|
||
localService: (port) => `本地用量服务没有在运行(127.0.0.1:${port})`,
|
||
},
|
||
en: {
|
||
title: "Account usage",
|
||
subtitle: "Start with each account, then expand it to see the models it used",
|
||
periods: { today: "Today", "7d": "7 days", "30d": "30 days", all: "All" },
|
||
refresh: "Refresh usage",
|
||
period: "Period",
|
||
summary: "Usage summary",
|
||
accounts: "Accounts",
|
||
providers: "providers",
|
||
calls: "Calls",
|
||
tokens: "Input + output tokens",
|
||
tokensWithCache: "Input + output + cache tokens",
|
||
tokensWithoutCache: "Input + output tokens",
|
||
includeCache: "Tokens include cache",
|
||
includeCacheHint: "Include cache tokens in totals, sorting and shares",
|
||
cost: "Estimated cost",
|
||
models: "models",
|
||
detail: "Account details",
|
||
search: "Search accounts, providers or models",
|
||
sort: "Sort",
|
||
sortBy: { tokens: "By tokens", cost: "By cost", calls: "By calls", name: "By name" },
|
||
expand: "Expand all",
|
||
collapse: "Collapse all",
|
||
columns: {
|
||
identity: "Provider / account",
|
||
calls: "Calls",
|
||
tokens: "Tokens",
|
||
share: "Share",
|
||
cost: "Cost",
|
||
status: "Status",
|
||
},
|
||
model: "Model",
|
||
token: "Token",
|
||
modelTokensWithCache: "Token + cache",
|
||
modelTokensWithoutCache: "Token",
|
||
allOk: "OK",
|
||
failed: "failed",
|
||
unpriced: "unpriced",
|
||
noCalls: "No account usage in this period",
|
||
noMatch: "No matching account",
|
||
noCallsHint: "Usage will be grouped by account after calls pass through the Magpie gateway.",
|
||
noMatchHint: "Try another account, provider or model.",
|
||
noData: "Could not read usage data",
|
||
reload: "Reload",
|
||
firstRead: "The first read may take a few seconds",
|
||
source: "Read-only aggregate",
|
||
updated: "Updated at",
|
||
input: "In",
|
||
output: "Out",
|
||
cache: "Cache",
|
||
cacheHit: "Cache hit",
|
||
cacheUnknown: (calls) => `${calls} cache-uncounted call${calls === 1 ? "" : "s"}`,
|
||
share: { tokens: "Token share", cost: "Cost share", calls: "Call share", name: "Token share" },
|
||
unpricedOnly: "Not priced",
|
||
unpricedCalls: (n) => `${n} unpriced`,
|
||
noTime: "no time",
|
||
justUsed: "used just now",
|
||
usedRecently: "used recently",
|
||
minutesAgo: (n) => `${n} min ago`,
|
||
hoursAgo: (n) => `${n} hr ago`,
|
||
readFailed: "Could not read usage data",
|
||
refreshFailed: "Refresh failed; showing the previous data",
|
||
apiKeyNote: "API key calls, not counted as accounts",
|
||
accountsAndModels: (a, m) =>
|
||
`${a} ${a === 1 ? "account" : "accounts"} · ${m} ${m === 1 ? "model" : "models"}`,
|
||
modelsOnly: (m) => `${m} ${m === 1 ? "model" : "models"}`,
|
||
providerModels: (p, m) =>
|
||
`${p} ${p === 1 ? "provider" : "providers"} · ${m} ${m === 1 ? "model" : "models"}`,
|
||
hostNotes: (items) => `Includes ${items}`,
|
||
noUsage: "No usage in this period yet: Magpie shows it after the gateway records its first call.",
|
||
modelDetails: (count) => `Model details for ${count} ${count === 1 ? "account is" : "accounts are"} temporarily unavailable; account totals are complete.`,
|
||
serviceTimeout: "The data request took more than 30 seconds. Check the Magpie gateway.",
|
||
localService: (port) => `The local usage service is not running (127.0.0.1:${port})`,
|
||
},
|
||
};
|
||
|
||
root.UsageI18n = {
|
||
get(lang) {
|
||
return String(lang || "").toLowerCase().startsWith("zh") ? WORDS.zh : WORDS.en;
|
||
},
|
||
};
|
||
})(globalThis);
|