Files
yetone 8828f7731c page-usage 0.1.0: a page with calls, errors, tokens and cost at a glance
A new kind of package: a page in magpie's window (magpie.page), with
magpie.permissions ["usage:summary"] and nothing else. Today, 7 or 30
days; models by tokens, calls or cost; the picks are saved through
magpie.options.set (debounced past the bridge's once-a-second limit).
Theme from --magpie-* variables; en and zh text.

Run:
- bun test packages/usage: 6 tests (permissions are exactly what the
  scripts read; no fetch/XHR/WebSocket; no inline scripts; every local
  reference exists; what tiles and rows say).
- Sandbox magpie from yetone/magpie origin/main b03031738: `magpie plugin`
  lists "page Usage glance (reads usage:summary)". `magpie web` on
  127.0.0.1:3595 serves every page file under the sandbox CSP
  (connect-src 'none'); /api/plugin-pages/read usage:summary answers the
  sandbox's 10 calls; providers:list is refused 403. Headless Chromium and
  WebKit (playwright 1.62.0) opened the sidebar tab: tiles "Calls 10,
  Errors 0, Tokens 70, Cost $0", row "stub-1 70 · 10 calls", no console
  errors; picking 7 days wrote "period": "7d" to plugins.json.
2026-10-16 02:57:43 +08:00

107 lines
3.5 KiB
JavaScript

// The page: reads usage:summary (the only read package.json declares) for
// the period picked, and keeps the period and the sort in this plugin's
// own options so the page opens where it was left.
(async () => {
const V = globalThis.UsageView;
const $ = (id) => document.getElementById(id);
const theme = await magpie.ready;
let lang = theme.lang;
let opts = {};
try { opts = (await magpie.options.get()) || {}; } catch {}
const state = {
period: V.PERIODS.includes(opts.period) ? opts.period : "today",
sort: V.SORTS.includes(opts.sort) ? opts.sort : "tokens",
summary: null,
};
function words() {
const t = V.text(lang);
document.querySelectorAll("[data-t]").forEach((e) => { e.textContent = t[e.dataset.t]; });
}
function select(group, attr, value) {
$(group).querySelectorAll("button").forEach((b) => b.setAttribute("aria-selected", String(b.dataset[attr] === value)));
}
function paint() {
select("periods", "period", state.period);
select("sorts", "sort", state.sort);
const s = state.summary;
if (!s) return;
$("since").textContent = V.since(s, lang);
$("tiles").replaceChildren(...V.tiles(s, lang).map((t) => {
const d = document.createElement("div");
d.className = "tile" + (t.bad ? " bad" : "");
for (const [cls, v] of [["k", t.label], ["v", t.value], ["s", t.sub]]) {
const e = document.createElement("div");
e.className = cls;
e.textContent = v;
d.append(e);
}
return d;
}));
const rows = V.rows(s, state.sort, lang);
$("empty").hidden = rows.length > 0;
$("empty").textContent = V.text(lang).noCalls;
$("models").replaceChildren(...rows.map((r) => {
const li = document.createElement("li");
const name = document.createElement("span");
name.className = "name";
name.textContent = name.title = r.name;
const num = document.createElement("span");
num.className = "num";
num.textContent = `${r.value} · ${r.detail}`;
const bar = document.createElement("span");
bar.className = "bar";
const fill = document.createElement("i");
fill.style.width = `${Math.max(2, Math.round(r.share * 100))}%`;
bar.append(fill);
li.append(name, num, bar);
return li;
}));
}
async function load() {
try {
state.summary = await magpie.read("usage:summary", { period: state.period });
$("error").hidden = true;
} catch (e) {
$("error").hidden = false;
$("error").textContent = `${V.text(lang).failed}: ${e.message}`;
}
paint();
}
// options can be set once a second: a quick second click is saved with
// the next one
let saving = null;
function save() {
if (saving) return;
saving = setTimeout(async () => {
saving = null;
try { opts = await magpie.options.set({ ...opts, period: state.period, sort: state.sort }); } catch {}
}, 1100);
}
$("periods").addEventListener("click", (e) => {
const p = e.target.closest("button")?.dataset.period;
if (!p || p === state.period) return;
state.period = p;
paint();
load();
save();
});
$("sorts").addEventListener("click", (e) => {
const s = e.target.closest("button")?.dataset.sort;
if (!s || s === state.sort) return;
state.sort = s;
paint();
save();
});
magpie.onTheme((t) => { lang = t.lang; words(); paint(); });
// fresh numbers while the page is open
setInterval(load, 60_000);
words();
await load();
})();