CryptoSarom on X (tweet 2116797183207035245, the tray panel's Usage tab):
"能否支持合并用量和额度显示? 有时候更关心某个模型还能用多少 ... 同个供应商
同模型可以合并 ... 或者单个路由组单独显示用量和额度. 有需要关心账号限制
的时候可以进详情再去看".
What it does:
- A Usage card of several accounts has "All N accounts" under its head:
each window the accounts share, one figure and a thin bar, and how
many couldn't be read. The tray panel's group of several has the same
strip above its cards (at most 3 windows). Each account stays below it
as it was: the details are where they were.
- A routing group's row (Routing › groups) has a line for each provider
among its members: "WorkBuddy AI, 4 accounts: Credits 355 / 1,500
credits · 23.7% used · 1 couldn't be read", following Used/Left.
The design, and why:
- Only what reads alike goes together (mergeQuotas). Counted windows of
one name and unit add their amounts and limits, the share is of the
sum; wallets add balances in one unit. A window said only as a share
is the mean over accounts on one plan, whose allowances are the same
size, so the mean is the share of their allowance together; two plans
are never averaged, and a share is never added to a count. Text-only
windows are left out; unlimited only when every account read says so.
- An account whose reading failed is unknown, not 0 or 100: left out of
the figure and counted ("1 couldn't be read"). A window an earlier
reading (asOf) stands in for, past its reset, is left out the same way
and counted in its tooltip. No single reset is claimed for the whole;
the tooltip says when the first of them starts again.
- Only what two or more accounts have goes together: one account's
window is already below it.
- No switch: the strip is additive and quiet (no gradient, no coloured
stripe; the pill grey of the page, the accent bar the cards use), and
it names no account, so Hide accounts holds.
- The code is in quotaall.js, imported when a provider first has two
accounts (quotaAllJS) or a routing group's member has readings, and
each place is drawn again once it is in. app.js has the loader and the
two call sites; three comments in the quota code were cut to keep the
core inside its budget (1,533,096 of 1,533,105), no raise.
Run, at this tree (rebased on origin/main df0320156):
- mote (macOS 27), node --test: usage-merge (7/7), usage-all-accounts
(14/14: Chromium and WebKit; en, zh, zh-TW, ja, de; light and dark for
en and zh; Usage page at 440px, tray panel at 360x760, Routing), i18n-keys
(4/4), old-webkit (6/6): 31 of 31 ok.
- mote, go test -count=1 ./internal/gui: TestCoreScriptBudget ok (app.js
1,311,777, core 1,533,096); one red, TestMainWindowCorners/macOS_26_SDK,
#3949 (macOS 27, AppKit), no Go changed here.
- Screenshots of all three places in both engines, every language,
light and dark (en, zh), looked at.
- Not run: the whole GUI suite. mote had four other sessions' full GUI
suites running (1h46m+, load 57 to 240) and the WSL box three, so
per the one-suite-a-machine rule it waited and wasn't run.
Broken by hand (red, then put back):
- quotaall.js counting a failed account as read and dropping the
two-account rule: usage-merge 4 of 7 red (share vs count, failed is
unknown, plans apart, balances/unlimited). Before the move to
quotaall.js, dropping the kind from the key and the stale skip each
turned their tests red too.
- the loader not drawing again once quotaall.js is in:
usage-all-accounts 14 of 14 red (both engines, every language).
- usage-merge.test.cjs is red on the old code: quotaall.js isn't there.
Not tried: real accounts (the figures come from fixtures: WorkBuddy AI
credits, Codex Plus and Pro, Claude Max ×2, Kimi); the real tray app on
macOS, Windows or WebKitGTK; a slow first load of quotaall.js (the strip
comes in after the cards on its first draw, measured only on a local
server).
119 lines
5.8 KiB
JavaScript
119 lines
5.8 KiB
JavaScript
// A provider's accounts together (CryptoSarom on X: how much of a model is
|
|
// left matters more than which account holds it). mergeQuotas, the shipped
|
|
// function, puts together only what reads alike: counted windows of one
|
|
// name and unit add their amounts and limits, wallets their balances, and a
|
|
// window said only as a share is the mean of accounts on one plan. A share
|
|
// is never added to a count, two units never meet, two plans' shares
|
|
// never meet, and an account whose reading failed is unknown, not 0 or
|
|
// 100. A window an earlier reading stands in for, past its reset, is left
|
|
// out and counted. Needs no browser.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const vm = require("node:vm");
|
|
const { test } = require("node:test");
|
|
|
|
const source = fs.readFileSync(process.env.MAGPIE_APP_JS || path.resolve(__dirname, "../assets/app.js"), "utf8").replace(/\r\n/g, "\n");
|
|
function section(start, end) {
|
|
const a = source.indexOf(start), b = source.indexOf(end, a);
|
|
assert(a >= 0 && b > a, `missing ${start}`);
|
|
return source.slice(a, b);
|
|
}
|
|
// quotaall.js, which app.js loads once a provider has two accounts
|
|
const merging = fs.readFileSync(path.resolve(__dirname, "../assets/quotaall.js"), "utf8").replace(/\r\n/g, "\n");
|
|
const ctx = vm.createContext({});
|
|
ctx.window = ctx;
|
|
vm.runInContext(`
|
|
${section("function to100(", "\n")}
|
|
${merging}
|
|
`, ctx);
|
|
const merge = (subs, now) => JSON.parse(JSON.stringify(ctx.mergeQuotas(JSON.parse(JSON.stringify(subs)), now)));
|
|
const H = 3600e3, now = Date.parse("2026-11-01T12:00:00Z");
|
|
const iso = (ms) => new Date(ms).toISOString();
|
|
const acct = (user, windows, extra) => ({ provider: "workbuddy-ai", name: "WorkBuddy AI", user, plan: "Pro", windows, ...extra });
|
|
const by = (r, name) => r.windows.filter((w) => w.name === name);
|
|
|
|
test("counted windows of one unit add up; the share is of the sum", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Credits", amount: 100, limit: 500, unit: "credits", used: 20 }]),
|
|
acct("b", [{ name: "Credits", amount: 250, limit: 500, unit: "credits", used: 50 }]),
|
|
acct("c", [{ name: "Credits", amount: 5, limit: 500, unit: "credits", used: 1 }]),
|
|
], now);
|
|
assert.equal(r.accounts, 3);
|
|
assert.equal(r.unknown, 0);
|
|
const [w] = by(r, "Credits");
|
|
assert.equal(w.amount, 355);
|
|
assert.equal(w.limit, 1500);
|
|
assert.ok(Math.abs(w.used - (100 * 355) / 1500) < 1e-9);
|
|
assert.equal(w.accounts, 3);
|
|
assert.equal(w.how, "c");
|
|
});
|
|
|
|
test("a share is never added to a count, nor one unit to another", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Monthly", amount: 100, limit: 500, unit: "credits" }, { name: "Tokens", amount: 1, limit: 10, unit: "M tokens" }]),
|
|
acct("b", [{ name: "Monthly", used: 40 }, { name: "Tokens", amount: 3, limit: 10, unit: "requests" }]),
|
|
], now);
|
|
// each kind has one account only: nothing goes together
|
|
assert.deepEqual(r.windows, []);
|
|
});
|
|
|
|
test("an account whose reading failed is unknown, left out of the figure", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", used: 20, resetsAt: iso(now + H) }]),
|
|
acct("b", [{ name: "5 hours", used: 60, resetsAt: iso(now + 2 * H) }]),
|
|
acct("c", [], { error: "401 Unauthorized" }),
|
|
], now);
|
|
assert.equal(r.accounts, 2);
|
|
assert.equal(r.unknown, 1);
|
|
const [w] = by(r, "5 hours");
|
|
assert.equal(w.used, 40, "the mean of the two read, the failed one neither 0 nor 100");
|
|
assert.equal(w.accounts, 2);
|
|
assert.equal(w.firstReset, iso(now + H));
|
|
assert.equal(w.resetsAt, undefined, "no single reset is claimed for the whole");
|
|
});
|
|
|
|
test("shares are averaged within a plan only", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", used: 10 }], { plan: "Plus" }),
|
|
acct("b", [{ name: "5 hours", used: 30 }], { plan: "Plus" }),
|
|
acct("c", [{ name: "5 hours", used: 90 }], { plan: "Pro" }),
|
|
], now);
|
|
const ws = by(r, "5 hours");
|
|
assert.equal(ws.length, 1, "the Pro account alone is not merged into the Plus mean");
|
|
assert.equal(ws[0].used, 20);
|
|
assert.equal(ws[0].plan, "Plus", "named with its plan when the accounts have several");
|
|
const one = merge([acct("a", [{ name: "5 hours", used: 10 }], { plan: "Plus" }), acct("b", [{ name: "5 hours", used: 30 }], { plan: "Pro" })], now);
|
|
assert.deepEqual(one.windows, []);
|
|
});
|
|
|
|
test("a stale window past its reset is left out and counted", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Daily", used: 10, resetsAt: iso(now + H) }]),
|
|
acct("b", [{ name: "Daily", used: 50, resetsAt: iso(now + H) }]),
|
|
acct("c", [{ name: "Daily", used: 99, resetsAt: iso(now - H) }], { asOf: iso(now - 3 * H) }),
|
|
], now);
|
|
const [w] = by(r, "Daily");
|
|
assert.equal(w.used, 30);
|
|
assert.equal(w.unknown, 1);
|
|
});
|
|
|
|
test("balances add up; unlimited only when every account is; text is left out", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "Wallet", balance: 12.5, unit: "USD" }, { name: "Chat", unlimited: true }, { name: "Overages", text: true, used: 100 }]),
|
|
acct("b", [{ name: "Wallet", balance: 7.5, unit: "USD" }, { name: "Chat", unlimited: true }, { name: "Overages", text: true, used: 100 }]),
|
|
acct("c", [{ name: "Wallet", balance: 1, unit: "credits" }, { name: "Chat", used: 30 }]),
|
|
], now);
|
|
assert.deepEqual(by(r, "Wallet").map((w) => [w.balance, w.unit]), [[20, "USD"]]);
|
|
assert.deepEqual(by(r, "Chat").filter((w) => w.unlimited), [], "one account has a limit");
|
|
assert.deepEqual(by(r, "Overages"), []);
|
|
});
|
|
|
|
test("windows keep their family and pool, apart by them", () => {
|
|
const r = merge([
|
|
acct("a", [{ name: "5 hours", pool: "Gemini", used: 10 }, { name: "5 hours", pool: "Claude & GPT", used: 70 }]),
|
|
acct("b", [{ name: "5 hours", pool: "Gemini", used: 30 }, { name: "5 hours", pool: "Claude & GPT", used: 90 }]),
|
|
], now);
|
|
assert.deepEqual(r.windows.map((w) => [w.pool, w.used]), [["Gemini", 20], ["Claude & GPT", 80]]);
|
|
});
|