Files
plugins/packages/page-usage-tree/lib/data.mjs
T
CHENZUDE 808f7a184d page-usage-tree 0.1.0: usage as provider → account → model (CHENZUDE, #575)
New page plugin @magpie-community/page-usage-tree 0.1.0 by CHENZUDE: magpie's usage as a provider -> account -> model tree, served by a small server that reads the gateway's /v1/magpie/usage and /usage/requests.

The last round (f9e0907) gives the model row's calls cell the calls-cell class, so CALLS values sit under their right-aligned header, adds a test for it, and comments why the server's retry budget is 16 x 100ms.

What was run at f9e09079 (mote, macOS, sandbox HOME):
- In the package: node --test 50 pass; bun test 47 tests, 0 fail.
- f9e09079 merged onto main 8765cf8f: bun scripts/check.mjs ok; whole-repo bun test 6149 pass, 20 skip, 0 fail.
- Playwright against a fake gateway: CALLS header and value right edges 616/616 (1280px), 423/423 (900), 341/341 (760, 621) in Chromium and WebKit. Chromium 1280-620px, WebKit 900/760, both engines at 420px in en and zh: no clipped cell, no sideways scroll, no <select>, no page errors.
- Broken by hand: the old `model.errors ? "bad" : ""` puts values at 544 and 273 and turns "model calls sit under the right-aligned calls header" red (27 pass, 1 fail).

Not tried: a real magpie with real accounts, the desktop app, Windows and Linux.
2026-11-05 17:11:25 +08:00

172 lines
5.5 KiB
JavaScript

import { randomUUID } from "node:crypto";
import {
chmodSync,
mkdirSync,
readFileSync,
renameSync,
unlinkSync,
writeFileSync,
} from "node:fs";
import { homedir } from "node:os";
import { dirname, isAbsolute, join } from "node:path";
import { accountKey, aggregateGatewayUsage, PERIODS } from "./usage.mjs";
import {
gatewayAccountRequests,
gatewayUsage,
} from "./gateway.mjs";
export function cacheDir() {
if (process.env.MAGPIE_CACHE_DIR) return process.env.MAGPIE_CACHE_DIR;
if (isAbsolute(process.env.XDG_CACHE_HOME || "")) {
return join(process.env.XDG_CACHE_HOME, "magpie");
}
return join(homedir(), ".cache", "magpie");
}
// The page takes only what the gateway can tell it. Currency and number
// units are presentation choices passed by the launcher from this page's
// options; the gateway's costs are USD, which is magpie's default.
export function displaySettings(overrides = {}) {
return {
currency: overrides.currency === "cny" ? "cny" : "usd",
chineseUnits: overrides.chineseUnits === true,
lang: typeof overrides.lang === "string" && overrides.lang ? overrides.lang : "en",
usdCny: Number.isFinite(Number(overrides.usdCny)) && Number(overrides.usdCny) > 0
? Number(overrides.usdCny)
: 0,
theme: overrides.theme === "dark" || overrides.theme === "light" ? overrides.theme : "",
};
}
// Cost comes from the gateway in USD. The page only converts it for display,
// using the options Magpie's page bridge hands to the launcher. No settings
// or exchange-rate file is read here.
function normalizePeriod(period) {
return PERIODS.has(period) ? period : "today";
}
function cacheFile(period) {
return join(cacheDir(), "usage-tree", `${period}.json`);
}
// A read written by an older build can lack a field this page needs, so it
// is a miss rather than served as current.
const CACHE_VERSION = 6;
export function cacheUsable(data) {
return Boolean(data && Array.isArray(data.providers) && Array.isArray(data.accounts));
}
function readCached(period) {
try {
const raw = JSON.parse(readFileSync(cacheFile(period), "utf8"));
if (!raw?.data || raw.v !== CACHE_VERSION || !cacheUsable(raw.data)) return null;
return { data: raw.data, at: Number(raw.at) || 0 };
} catch {
return null;
}
}
function writeCached(period, data) {
let temporary = "";
try {
const file = cacheFile(period);
const directory = dirname(file);
mkdirSync(directory, { recursive: true, mode: 0o700 });
try {
chmodSync(directory, 0o700);
} catch {
// Windows ignores POSIX modes; the write itself is still useful.
}
const { stale, cached, ...keep } = data;
temporary = `${file}.${process.pid}.${randomUUID()}.tmp`;
writeFileSync(
temporary,
JSON.stringify({ v: CACHE_VERSION, at: Date.now(), data: keep }),
{ encoding: "utf8", mode: 0o600 },
);
renameSync(temporary, file);
} catch {
// A cache that cannot be written only costs the next read its wait.
} finally {
if (temporary) try { unlinkSync(temporary); } catch {}
}
}
// magpie's requests API can narrow by provider as well as account. A
// subscription id is unique in practice, but passing both keeps an API-key
// group from borrowing another provider's models.
async function requestsForAccounts(usage, period, options) {
const out = {};
const accounts = Array.isArray(usage?.accounts) ? usage.accounts : [];
await Promise.all(
accounts.map(async (item) => {
const account = String(item?.account || "");
if (!account) return;
const provider = String(item?.provider || "");
const key = accountKey(provider, account);
try {
out[key] = await gatewayAccountRequests(period, account, provider, options);
} catch {
// The account totals still come from the usage answer; only its
// model rows are missing.
out[key] = null;
}
}),
);
return out;
}
async function readGateway(period, options) {
const usage = await gatewayUsage(period, options);
// The usage answer carries the model table already. Only accounts whose
// models it cannot place need their own requests read; on a large ledger
// that is usually none of them.
const preview = aggregateGatewayUsage(usage, {}, { period });
const missing = preview.accounts.filter((account) => account.calls > 0 && !account.models.length);
const requestsByAccount = missing.length
? await requestsForAccounts(
{ ...usage, accounts: usage.accounts.filter((item) =>
missing.some((account) => account.provider === item.provider && account.account === item.account)) },
period,
options,
)
: {};
return aggregateGatewayUsage(usage, requestsByAccount, { period });
}
const refreshing = new Set();
export async function loadUsageData(period, options = {}) {
const wanted = normalizePeriod(period);
if (!options.fresh) {
const hit = readCached(wanted);
if (hit) {
const stale = Date.now() - hit.at >= 25_000;
if (stale) void refreshBehind(wanted, options);
return { ...hit.data, cached: true, stale };
}
}
return refresh(wanted, options);
}
function refreshBehind(period, options) {
if (refreshing.has(period)) return;
refreshing.add(period);
refresh(period, { ...options, fresh: true })
.catch(() => {})
.finally(() => refreshing.delete(period));
}
async function refresh(period, options = {}) {
const result = await readGateway(period, options);
result.source = "Magpie gateway usage";
writeCached(period, result);
return result;
}
export { PERIODS };