diff --git a/docs/subsystems/gui-shell.md b/docs/subsystems/gui-shell.md index 6be1d9d4e..4639fc75a 100644 --- a/docs/subsystems/gui-shell.md +++ b/docs/subsystems/gui-shell.md @@ -216,10 +216,13 @@ first in its line, with a bar under it like an account row's (`foldQuota`, chen2652 on Discord): the Usage page's readings of its accounts (`cardOf`) put together by quotaall.js's `mergeQuotas`, the same as the Usage page's "All N accounts" strip, its tightest window that says a share as the figure -and the bar, every window and when the first of them resets on hover. Only +and the bar, then when that window first starts again, the soonest of the +accounts' ("renews in 2h", as an account's row says it; Sdsi134a on X), +every window and when the first of them resets on hover. Only when every account in it was read, none is free, and all are on one plan; -otherwise, and while the fold is open or a kept day is shown, it says -nothing of it and each account's row has its own. See +otherwise, while the fold is open or a kept day is shown, and while the +groups page's Hide plans is on (`plansHidden`, `magpie.routingGroupPlans`), +it says nothing of it and each account's row has its own. See `routing-fold-accounts-together.test.cjs`. A group in the group's heading (`li.rt-sub`, `subNode`) folds the same way @@ -246,8 +249,10 @@ routing went by, and each of its windows, with when each resets of its own (a remote's, or an account not read yet) shows its windows as its one line instead (`.wins`, cut to one line, all of it on hover). `cardOf` in [`plugins/routing/client.js`](../../internal/gui/assets/plugins/routing/client.js) matches -the route's account to app.js's `quotas`: a `subscription` card of this -computer (no `from`) whose provider is the account's `agent` (a plugin's +the route's account to app.js's `quotas`: a card of this computer (no +`from`) whose `kind` is `subscription` or none (`/api/usage/quotas` sends +this computer's cards with no kind; only a remote's are told one, so asking +for `subscription` alone matched none in the app) whose provider is the account's `agent` (a plugin's provider id) or its provider, and whose user is the account's, ignoring case. `winsOf` names them with `familyWindows`, `quotaText` and `resetInText`. Nothing new is read: opening Routing asks diff --git a/internal/gui/assets/plugins/routing/client.js b/internal/gui/assets/plugins/routing/client.js index fcfc9195b..14f1a051d 100644 --- a/internal/gui/assets/plugins/routing/client.js +++ b/internal/gui/assets/plugins/routing/client.js @@ -4812,7 +4812,11 @@ function routingPage(part) { function cardFor(w) { if (w.kind !== "account" || !Array.isArray(quotas)) return null; const user = (w.who || "").trim().toLowerCase(); - const mine = quotas.filter((q) => !q.from && q.kind === "subscription" && (q.provider === w.agent || q.provider === w.provider)); + // this computer's own cards come with no kind (SubscriptionQuota.Kind + // is "" here, only a remote magpie's are told one), so asking for + // "subscription" matched none of them in the app (Sdsi134a on X: a + // fold of three accounts on one plan said nothing of what was left) + const mine = quotas.filter((q) => !q.from && (q.kind || "subscription") === "subscription" && (q.provider === w.agent || q.provider === w.provider)); return mine.find((q) => (q.user || "").trim().toLowerCase() === user) || (mine.length === 1 && !mine[0].user ? mine[0] : null); } // cardError: why the Usage page could not read the account, when its @@ -4823,6 +4827,11 @@ function routingPage(part) { const q = cardFor(w); return q && !cardOf(w) && q.error ? String(q.error) : ""; } + // plansHidden: the groups page's Hide plans was clicked (kept in + // localStorage, so the Routing page's fold reads it too) + function plansHidden() { + try { return localStorage.getItem("magpie.routingGroupPlans") === "hidden"; } catch { return false; } + } // foldQuota: what a fold's accounts have left together, from the Usage // page's readings (mergeQuotas), as its strip of all of a provider's // accounts says it: only when every account in it was read, and all are @@ -4830,8 +4839,10 @@ function routingPage(part) { // have is left out (mergeQuotas). The tightest window that says a share // is the figure and the bar; the rest, and when the first of them // starts again, are on hover. Null when nothing adds up. + // Hide plans on the groups page (magpie.routingGroupPlans) folds these + // away too: the user asked for no plans on this page function foldQuota(ms) { - if (ms.length < 2 || ms.some((m) => m.w.free)) return null; + if (ms.length < 2 || ms.some((m) => m.w.free) || plansHidden()) return null; const cards = ms.map((m) => cardOf(m.w)); if (cards.some((q) => !q) || new Set(cards).size !== cards.length) return null; if (new Set(cards.map((q) => q.plan || "")).size > 1) return null; @@ -4843,8 +4854,11 @@ function routingPage(part) { const top = bar || ws[0]; const line = (w) => t(w.name) + " " + quotaText(w); const soon = ws.map((w) => w.firstReset).filter(Boolean).sort()[0]; + // when the figure's window starts again, the soonest of the accounts', + // as an account's own row says it + const back = top.firstReset ? Date.parse(top.firstReset) - now() : 0; return { - text: line(top), bar, + text: line(top) + (back > 0 ? " · " + t("renews in {d}", { d: dur(back) }) : ""), bar, tip: tipLines(...ws.map(line), t(top.how === "p" ? "The mean of {n} accounts on one plan" : top.how === "u" ? "Unlimited on each of {n} accounts" : "Added up over {n} accounts", { n: top.accounts }), soon && t("The first of them starts again {when}", { when: resetClock(new Date(soon)) })), @@ -6584,8 +6598,7 @@ function routingPage(part) { // the quota lines folded away from every group, as kept in this window // (Sdsi134a on X: with many plans the list grew long); shown, as before, // until the toolbar's Hide plans is clicked - let gPlansHid = false; - try { gPlansHid = localStorage.getItem("magpie.routingGroupPlans") === "hidden"; } catch {} + let gPlansHid = plansHidden(); function plansButton() { const b = el("button", "text rt-gplans", t(gPlansHid ? "Show plans" : "Hide plans")); b.type = "button"; diff --git a/internal/gui/tests/routing-fold-accounts-together.test.cjs b/internal/gui/tests/routing-fold-accounts-together.test.cjs index 89485ed7a..d71d3d319 100644 --- a/internal/gui/tests/routing-fold-accounts-together.test.cjs +++ b/internal/gui/tests/routing-fold-accounts-together.test.cjs @@ -7,6 +7,11 @@ // Accounts on different plans are not put together: their fold is as it // was. In every language, at a window's width and at 420px, in both // engines. ARTIFACT_DIR keeps a screenshot of each folded stage. +// The cards are as /api/usage/quotas sends this computer's: no kind +// (Sdsi134a on X: a kind asked for matched none of them, and the fold said +// nothing). The figure says when its window starts again, the soonest of +// the accounts', as an account's own row does; Hide plans on the groups +// page (magpie.routingGroupPlans) folds the figure and the bar away. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); const path = require("node:path"); @@ -25,7 +30,7 @@ const route = { id: 7, seq: 7, time: iso(now - 2000), agent: "claude", model: "m-1", provider: "same", done: true, status: 200, ms: 900, tokens: 1500, order: accts, tries: [{ id: accts[0].id, model: "m-1", start: iso(now - 2000), done: true, ms: 800, status: 200 }], }; -const card = (provider, user, plan, five, seven, reset) => ({ provider, name: provider, kind: "subscription", user, plan, windows: [ +const card = (provider, user, plan, five, seven, reset) => ({ provider, name: provider, user, plan, windows: [ { name: "5 hours", used: five, resetsAt: iso(now + reset * 60e3) }, { name: "7 days", used: seven, resetsAt: iso(now + 73 * 3600e3) }, ] }); @@ -58,13 +63,14 @@ function serve(lang) { }; } -// the fold's figure: the 5 hours window, the mean of 20, 40 and 60 +// the fold's figure: the 5 hours window, the mean of 20, 40 and 60, and +// when the first of them starts again (2h 10m) const words = { - en: ["5 hours 40% used", "The mean of 3 accounts on one plan"], - zh: ["5 小时 已用 40%", "同一套餐 3 个账号的平均值"], - "zh-TW": ["5 小時 已用 40%", "同一方案 3 個帳號的平均值"], - ja: ["5 時間 使用済み 40%", "同じプランの 3 アカウントの平均"], - de: ["5 Stunden 40% verbraucht", "Mittelwert von 3 Konten mit demselben Tarif"], + en: ["5 hours 40% used", "The mean of 3 accounts on one plan", /^renews in \S/], + zh: ["5 小时 已用 40%", "同一套餐 3 个账号的平均值", /\S后重置$/], + "zh-TW": ["5 小時 已用 40%", "同一方案 3 個帳號的平均值", /\S後重置$/], + ja: ["5 時間 使用済み 40%", "同じプランの 3 アカウントの平均", /\S 後に更新$/], + de: ["5 Stunden 40% verbraucht", "Mittelwert von 3 Konten mit demselben Tarif", /^erneuert sich in \S/], }; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { @@ -104,6 +110,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.ok(!s.open && s.bar, JSON.stringify(s)); assert.equal(s.fill, "40%"); assert.ok(s.text.startsWith(words[lang][0] + " · "), s.text); + assert.match(s.text.split(" · ")[1], words[lang][2], s.text); if (words[lang][1]) assert.ok(s.tip.includes(words[lang][1]), s.tip); assert.ok(s.tip.split("\n").length >= 3, "each window and how it was put together, on hover: " + s.tip); // no account is named in it @@ -117,6 +124,22 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // nothing runs past the list const over = await same.evaluate((e) => Math.max(e.scrollWidth - e.clientWidth, document.documentElement.scrollWidth - innerWidth)); assert.ok(over <= 1, `${lang} at ${width}: ${over}px past`); + // Hide plans: the figure and the bar go, the fold's count stays + await page.evaluate(() => localStorage.setItem("magpie.routingGroupPlans", "hidden")); + await same.click(); + await page.waitForFunction(() => document.querySelectorAll(".rt-accts > li.rt-fold.open").length === 1); + await same.click(); + await page.waitForFunction(() => document.querySelectorAll(".rt-accts > li.rt-fold:not(.open)").length === 2); + let h = await look(same); + assert.ok(!h.open && !h.bar && !h.text.includes(words[lang][0]), "hidden plans: " + JSON.stringify(h)); + // shown again: back + await page.evaluate(() => localStorage.setItem("magpie.routingGroupPlans", "shown")); + await same.click(); + await page.waitForFunction(() => document.querySelectorAll(".rt-accts > li.rt-fold.open").length === 1); + await same.click(); + await page.waitForFunction(([w]) => [...document.querySelectorAll(".rt-accts > li.rt-fold:not(.open) em")].some((e) => e.textContent.startsWith(w)), [words[lang][0]]); + h = await look(same); + assert.ok(!h.open && h.bar && h.fill === "40%", "shown again: " + JSON.stringify(h)); // open again: the figure and the bar go await same.click(); await page.waitForFunction(() => document.querySelectorAll(".rt-accts > li.rt-fold.open").length === 1);