diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 14813c51d..35b0a5a7a 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -17890,7 +17890,8 @@ function panelQuotaCard(q, first = false) { // a pool's 5-hour and weekly rings, every pool's (a third pool once left // Claude's out: 01huadalang on Discord), else three const fam = familyWindows(q.windows.filter((w) => !w.unlimited && !quotaNoShare(w))); - const tx = q.windows.filter((w) => quotaNoShare(w)).map((w) => t(w.name) + " " + quotaText(w)).join(" · "); + const says = (f) => q.windows.filter(f).map((w) => t(w.name) + " " + quotaText(w)).join(" · "); + const tx = says(quotaNoShare), wallets = says((w) => w.balance != null); if (!fam.length && (tx || q.windows.some((w) => w.unlimited))) { card.append(pqLine(tx || t("Unlimited"), q)); if (balance) card.append(balance); @@ -17937,6 +17938,9 @@ function panelQuotaCard(q, first = false) { rings.append(r); } card.append(rings); + // a wallet beside the rings (Kimi Code's booster, "Extra usage"): what + // is left on it, not only on the Usage page (apple_test_src on X) + if (wallets) card.append(el("span", "pq-sub", wallets)); if (balance) card.append(balance); // the first window's cycle, a thin line under the card (#651) const spark = ws[0] && !ws[0].tiers ? quotaSpark(q, ws[0]) : null; diff --git a/internal/gui/core_budget_test.go b/internal/gui/core_budget_test.go index e84ff35d0..ab9c4920b 100644 --- a/internal/gui/core_budget_test.go +++ b/internal/gui/core_budget_test.go @@ -17,8 +17,8 @@ import ( // never what pays for a change (#5918); the history of each number is in // `git log -L` of this file. const ( - appScriptBudget = 1_086_324 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had; +57 for each account of a provider's Usage card in a card of its own (normalcoder_ on X), past the 24 of room left - coreScriptBudget = 1_270_363 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to; +151 for the provider menu's "Only on this device" item and its four translations; +458 for Settings › About's Report a problem row and its translations; +113 for withScheme, the one rule every typed address goes through (eachg0 on X), cut to the shortest form that keeps it whole; +10 for the kept whole's tooltip (Musikrv77852 on X), the past-cycle line reused for it, no page left to move it to; +1,112 for Codex's Open button and its Open Codex with menu (Yink88564391 on X), packed to the shortest form, no page left to move them to; +168 for Discover's long lists drawn in part (the owner, 11-13), after the rest of its hook moved into plugingrid.js, imported when Plugins first draws; +296 for the cap toggle and Smart sentences that say what they do and the Sign in on another computer row (Wing8Aqua on X), packed to the shortest form, no page left to move them to; +333 for In turn's figure in a provider's editor and its words naming the message that goes round (Wing8Aqua on X), the figure itself in rotfig.js, imported when In turn is first shown, the hook packed to the shortest form; +236 for a lone capped Codex account's row saying its turns go to the fallbacks when Codex is connected (not_4O4 on X), the two helpers inlined, no page left to move it to; +717 for omp's advisor (Awad_Yoo on X, #6408): its three rows moved into ompadvisor.js, imported when an omp's settings show, and what is left is the import, the role's " · off" and the Settings line's name, packed to the shortest form; +468 for a plugin's own file import (auth.import) in the import sheet, in the plugin's words (marchen_cc on X), packed to one statement; importSay is read as the Providers rows draw, so it has no lazy page to go to, and nothing else was moved out; +180 for Claude Magpie's own Claude Code folder (xMopf7RF on X, #6459): its Advanced fold in claudemagpie.js, imported when it first shows, and what is left is the import and the fold's place in the Separate app line, 216 bytes packed to the shortest form, less the 36 of room there was; +81 for the Use Claude Magpie button on Claude Desktop's row (xMopf7RF on X), drawn by claudemagpie.js too, what is left the one line that places it (#2710); +225 for the Hermes row's Other profiles lines (Norethcy on X), which 1d60b165c took out while each profile had a row and c96f33d65 left with no place at all, packed to the shortest form; the dead hermes# Default note went with it, and the Agents rows draw it, so there is no page to move it to; +397 for a key's own price rate (tnt12231 on X): its lines, check and placeholder in keyrates.js, imported when a provider with two or more keys opens, and what is left is the import, the draft's copy, Advanced opening for it, the save's one line and proxyLines drawing another kind of line; +316 for an image or video model's chip saying what it makes from its right-click (iceloon on X): the chat chip's Makes… now shared with it, and the media chip's Copy model ID left out, packed to the shortest form; the provider's editor draws it, so there is no page to move it to; +67 for the Claude Code · Claude Magpie row saying it is on magpie through Claude Magpie (xMopf7RF on X), one line in connectLine with the shortest words that say it; the Agents rows draw it, so there is no page to move it to; +12 for AIGoCode's logo in pngIcons (MindfulReturn on X), the list every drawn icon is looked up in; +491 for each cost in the tray panel's and Requests' chart and ranking in its own provider's currency (onkashiko on X): ledProv and the provider passed through, packed to the shortest form; drawLedColumns and drawLedRank are shared by the panel, so there is no page to move it to; +326 for the Qwen AI platform's Token Plan sign-in (imthe99galaxy on X): its field, sign-in and wait in qianwenplan.js, imported when such a provider's editor opens, and what is left is the import and one quotaError line for its two refusals, packed to the shortest form; quotaError reads every card's error, so there is no page to move it to; +72 for Copy all keys (emilochen on X): its row in keyrates.js, imported when a provider with two or more keys opens through keyRateField, now told which of its parts to draw, and what is left is the one line that places it; +83 for the page saying it has gone from view, so what its Sessions and Usage pages keep goes in 30 s and not 7 minutes (P0 memory): folded into the visibilitychange listener every window has, packed to the shortest form, so there is no page to move it to; +270 for Providers' Select, to remove several at once (eiyuuzaki2333 on X): its rows, bar, removal and keys in pvpick.js, imported when Select is first clicked, and what is left is pvSel, the button's click and two lines in renderProviders, which draws the list, so there is no page to move it to; +81 for each account of a provider's Usage card in a card of its own (normalcoder_ on X): one line that makes the box, its brief look left to CSS, and renderQuotas, which draws every card, so there is no page to move it to + appScriptBudget = 1_086_341 // app.js: 1,080,614 at 8016205e3, with the 5,653 of room it had; +57 for each account of a provider's Usage card in a card of its own (normalcoder_ on X), past the 24 of room left; +17 for an allowance card's wallet line under its rings (apple_test_src on X), the one filter shared with its text line + coreScriptBudget = 1_270_380 // every core script at 8016205e3, the old measure 682 over its cap there (#5918); +367 for the model test on a chosen account (tmr11235), no page left to move it to; +151 for the provider menu's "Only on this device" item and its four translations; +458 for Settings › About's Report a problem row and its translations; +113 for withScheme, the one rule every typed address goes through (eachg0 on X), cut to the shortest form that keeps it whole; +10 for the kept whole's tooltip (Musikrv77852 on X), the past-cycle line reused for it, no page left to move it to; +1,112 for Codex's Open button and its Open Codex with menu (Yink88564391 on X), packed to the shortest form, no page left to move them to; +168 for Discover's long lists drawn in part (the owner, 11-13), after the rest of its hook moved into plugingrid.js, imported when Plugins first draws; +296 for the cap toggle and Smart sentences that say what they do and the Sign in on another computer row (Wing8Aqua on X), packed to the shortest form, no page left to move them to; +333 for In turn's figure in a provider's editor and its words naming the message that goes round (Wing8Aqua on X), the figure itself in rotfig.js, imported when In turn is first shown, the hook packed to the shortest form; +236 for a lone capped Codex account's row saying its turns go to the fallbacks when Codex is connected (not_4O4 on X), the two helpers inlined, no page left to move it to; +717 for omp's advisor (Awad_Yoo on X, #6408): its three rows moved into ompadvisor.js, imported when an omp's settings show, and what is left is the import, the role's " · off" and the Settings line's name, packed to the shortest form; +468 for a plugin's own file import (auth.import) in the import sheet, in the plugin's words (marchen_cc on X), packed to one statement; importSay is read as the Providers rows draw, so it has no lazy page to go to, and nothing else was moved out; +180 for Claude Magpie's own Claude Code folder (xMopf7RF on X, #6459): its Advanced fold in claudemagpie.js, imported when it first shows, and what is left is the import and the fold's place in the Separate app line, 216 bytes packed to the shortest form, less the 36 of room there was; +81 for the Use Claude Magpie button on Claude Desktop's row (xMopf7RF on X), drawn by claudemagpie.js too, what is left the one line that places it (#2710); +225 for the Hermes row's Other profiles lines (Norethcy on X), which 1d60b165c took out while each profile had a row and c96f33d65 left with no place at all, packed to the shortest form; the dead hermes# Default note went with it, and the Agents rows draw it, so there is no page to move it to; +397 for a key's own price rate (tnt12231 on X): its lines, check and placeholder in keyrates.js, imported when a provider with two or more keys opens, and what is left is the import, the draft's copy, Advanced opening for it, the save's one line and proxyLines drawing another kind of line; +316 for an image or video model's chip saying what it makes from its right-click (iceloon on X): the chat chip's Makes… now shared with it, and the media chip's Copy model ID left out, packed to the shortest form; the provider's editor draws it, so there is no page to move it to; +67 for the Claude Code · Claude Magpie row saying it is on magpie through Claude Magpie (xMopf7RF on X), one line in connectLine with the shortest words that say it; the Agents rows draw it, so there is no page to move it to; +12 for AIGoCode's logo in pngIcons (MindfulReturn on X), the list every drawn icon is looked up in; +491 for each cost in the tray panel's and Requests' chart and ranking in its own provider's currency (onkashiko on X): ledProv and the provider passed through, packed to the shortest form; drawLedColumns and drawLedRank are shared by the panel, so there is no page to move it to; +326 for the Qwen AI platform's Token Plan sign-in (imthe99galaxy on X): its field, sign-in and wait in qianwenplan.js, imported when such a provider's editor opens, and what is left is the import and one quotaError line for its two refusals, packed to the shortest form; quotaError reads every card's error, so there is no page to move it to; +72 for Copy all keys (emilochen on X): its row in keyrates.js, imported when a provider with two or more keys opens through keyRateField, now told which of its parts to draw, and what is left is the one line that places it; +83 for the page saying it has gone from view, so what its Sessions and Usage pages keep goes in 30 s and not 7 minutes (P0 memory): folded into the visibilitychange listener every window has, packed to the shortest form, so there is no page to move it to; +270 for Providers' Select, to remove several at once (eiyuuzaki2333 on X): its rows, bar, removal and keys in pvpick.js, imported when Select is first clicked, and what is left is pvSel, the button's click and two lines in renderProviders, which draws the list, so there is no page to move it to; +81 for each account of a provider's Usage card in a card of its own (normalcoder_ on X): one line that makes the box, its brief look left to CSS, and renderQuotas, which draws every card, so there is no page to move it to; +17 for Kimi Code's booster balance under an allowance card's rings in the tray panel (apple_test_src on X): the card's text line and the wallet line share one filter, packed to the shortest form; panelQuotaCard draws every card, so there is no page to move it to ) // codeBytes is how many bytes of b are code: its blank lines, its lines diff --git a/internal/gui/tests/panel-quota-wallet.test.cjs b/internal/gui/tests/panel-quota-wallet.test.cjs new file mode 100644 index 000000000..68ff7a6e1 --- /dev/null +++ b/internal/gui/tests/panel-quota-wallet.test.cjs @@ -0,0 +1,75 @@ +// apple_test_src on X: Kimi Code's booster (加油包) showed only on the +// Usage page. The tray panel's Allowances card drew its 5-hour and weekly +// rings and left out a wallet window beside them; it now says what is left +// on it under the rings, in the reader's language, inside a narrow panel. +// Real page, isolated responses: no vendor or account is used. +// MAGPIE_QUOTA_ASSETS points at older assets to see it fail. +const assert = require("node:assert/strict"); +const fs = require("node:fs/promises"); +const path = require("node:path"); +const { test } = require("node:test"); +const { chromium, webkit } = require("playwright"); +const assets = process.env.MAGPIE_QUOTA_ASSETS || path.resolve(__dirname, "../assets"); +const iso = n => new Date(n).toISOString(); +function fixtures() { + const now = Date.now(), readAt = iso(now - 60000); + return [{ + provider: "kimi-code", name: "Kimi Code", icon: "kimi", user: "k@example.com", plan: "Allegretto", readAt, windows: [ + { name: "5 hours", used: 30, resetsAt: iso(now + 2 * 3600000), credits: true }, + { name: "Week", used: 50, resetsAt: iso(now + 3 * 86400000), credits: true }, + { name: "Extra usage", balance: 98.5, unit: "CNY" }, + ], + }]; +} +function serve(lang, data) { + const settings = { theme: "light", lang, tray: "panel", quotaLeft: false, currency: "usd" }; + return async route => { + const url = new URL(route.request().url()), json = body => route.fulfill({ json: body }); + if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = ${JSON.stringify({ lang, theme: "light", web: true })};` }); + if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); + if (url.pathname === "/api/state" && url.searchParams.get("last")) return json({ agents: [], profiles: [], settings, last: true }); + if (url.pathname === "/api/state") { return json({ agents: [], profiles: [], settings }); } + if (url.pathname === "/api/settings") return json(settings); + if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } }); + if (url.pathname === "/api/usage/quotas") { return json(data.quotas); } + if (url.pathname === "/api/usage/quotas/history") return json([]); + if (url.pathname === "/api/usage") return json({ calls: 0, cost: 0, days: [], agents: [], models: [], keys: [] }); + if (url.pathname === "/api/groups") return json({ groups: [], models: [] }); + if (url.pathname === "/api/plugins") return json({ plugins: [] }); + if (url.pathname.startsWith("/api/")) return json({}); + const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname); + const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)]; + try { await route.fulfill({ body: await fs.readFile(file), contentType }); } + catch { await route.fulfill({ status: 404, body: "" }); } + }; +} +const card = '#panelQuota .pq-card[data-card="kimi-code|k@example.com"]'; +for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { + for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { + for (const width of [440, 360]) { + test(`${engine} ${lang} ${width}px: an allowance card says its booster's balance under its rings`, async (t) => { + const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); + t.after(() => browser.close()); + const page = await browser.newPage({ viewport: { width, height: 640 }, reducedMotion: "reduce" }); + page.setDefaultTimeout(5000); + page.setDefaultNavigationTimeout(60000); + const errors = []; + page.on("pageerror", e => errors.push(e.message)); + await page.addInitScript(() => { try { localStorage.setItem("magpie.panelTab", "usage"); } catch {} }); + await page.route("**/*", serve(lang, { quotas: fixtures() })); + await page.goto("http://magpie.test/?mode=panel"); + await page.locator(`${card} .pq-dial b`, { hasText: "30%" }).waitFor(); + const wallet = page.locator(`${card} .pq-rings + .pq-sub`); + await wallet.waitFor(); + const text = (await wallet.textContent()).trim(); + const want = await page.evaluate(() => t("Extra usage") + " " + t("{n} left", { n: "98.5 " + t("CNY") })); + assert.equal(text, want.trim()); + if (lang !== "en") assert.ok(!text.includes("Extra usage"), `said in ${lang}: ${text}`); + assert.ok(await page.locator(`${card} .pq-ring`).count() === 2, "both rings stay"); + const fits = await page.locator(card).evaluate(c => c.scrollWidth <= c.clientWidth + 1 && c.getBoundingClientRect().right <= innerWidth); + assert.ok(fits, "nothing runs out of the card"); + assert.deepEqual(errors, []); + }); + } + } +} diff --git a/internal/gui/trayusage.go b/internal/gui/trayusage.go index 4d764c7f2..a2be5fd06 100644 --- a/internal/gui/trayusage.go +++ b/internal/gui/trayusage.go @@ -329,6 +329,18 @@ func trayCardText(lang string, q provider.SubscriptionQuota, now time.Time, left } long = append(long, line) } + // a wallet beside the windows (Kimi Code's booster, "Extra usage"): + // what is left on it, as the card's line under its rings says it + for _, w := range q.Windows { + if w.Balance == nil { + continue + } + amt := strconv.FormatFloat(math.Round(*w.Balance*100)/100, 'f', -1, 64) + if u := pageWord(lang, w.Unit); u != "" { + amt += " " + u + } + long = append(long, trayWindowName(lang, w.Name)+" "+trayText(lang, "%s left", amt)) + } return strings.Join(short, " · "), q.Name + "\n" + strings.Join(long, "\n") + trayAsOf(lang, q) } diff --git a/internal/gui/trayusage_wallet_test.go b/internal/gui/trayusage_wallet_test.go new file mode 100644 index 000000000..11698257e --- /dev/null +++ b/internal/gui/trayusage_wallet_test.go @@ -0,0 +1,35 @@ +package gui + +import ( + "strings" + "testing" + "time" + + "github.com/yetone/magpie/internal/provider" +) + +// Kimi Code's booster (加油包) is a wallet window set aside beside the plan's +// windows. The menu bar's tooltip says what is left on it, as the panel's +// card does under its rings (apple_test_src on X: it showed only on the +// Usage page); the label stays the plan's percent. +func TestTrayTipSaysWalletLeft(t *testing.T) { + now := time.Now() + r5, r7 := now.Add(2*time.Hour), now.Add(72*time.Hour) + left := 98.5 + q := provider.SubscriptionQuota{Provider: "kimi-code", Name: "Kimi Code", User: "k@example.com", Windows: []provider.QuotaWindow{ + {Name: "5 hours", Used: 30, ResetsAt: &r5, Span: 5 * time.Hour}, + {Name: "Week", Used: 50, ResetsAt: &r7, Span: 7 * 24 * time.Hour}, + {Name: "Extra usage", Balance: &left, Unit: "CNY", Aside: true}, + }} + label, tip := trayCardText("en", q, now, false, false, nil) + if label != "30% · 50%" { + t.Fatalf("label: %q", label) + } + if !strings.Contains(tip, "\nExtra usage 98.5 CNY left") { + t.Fatalf("en tip: %q", tip) + } + _, tip = trayCardText("zh", q, now, false, false, nil) + if strings.Contains(tip, "Extra usage") || !strings.Contains(tip, "剩余 98.5") { + t.Fatalf("zh tip: %q", tip) + } +}