diff --git a/packages/day-card/README.md b/packages/day-card/README.md index 5926aff..c27f14b 100644 --- a/packages/day-card/README.md +++ b/packages/day-card/README.md @@ -1,14 +1,14 @@ # @magpie-community/page-day-card -A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. Its foot credits [nowdex](https://nowdex.app), whose share card the receipt's design follows. **Background** picks what the paper lies on: nothing (the paper alone, a transparent PNG), teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. +A page in [magpie](https://usemagpie.ai)'s window that prints a day of your AI usage (or a week, a month or all of it) as a receipt, a "Daily AI statement" on paper over a backdrop of your choice. It has the day's API-equivalent cost, your streak, requests, errors and the day against your average day before it, then each agent and its models with their logos and costs, and the day's tokens: input, output and the cache hit rate. Its foot credits [nowdex](https://nowdex.app), whose share card the receipt's design follows. **Background** picks what the paper lies on: nothing (the paper alone, a transparent PNG), teal, fifteen patterns (gingham, graph paper, kraft, cyanotype, seigaiha waves, xuan paper and more), four landscape photos, or a picture of your own (pick one, drop it on the page or paste it). A slider sets the margin around the paper; a photo also has blur and dim. The choice is kept in the plugin's options. **Copy image** puts the card on the clipboard; **Save PNG** saves it in Downloads. -Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. Pick an earlier day with the arrows or the date's menu. The card is shown as large as fits under the toolbar, so a whole day is on one screen; the picture you copy or save is full size. +Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @magpie-community/page-day-card`. It then has a tab, **Day card** (titled in the window's language: 每日用量卡, 1日の使用量カード, Tageskarte), under Plugins in the sidebar, in the app and in `magpie web`. The switch at the left picks **Day**, **Week** (the seven days ending on the date shown), **Month** (the thirty) or **All** (every day with calls, up to today); a week's or a month's card has its period, the days that had calls and the cost of one such day in place of the streak against an average day. Pick an earlier day, or the date a week or month ends on, with the arrows (which step a whole week or month) or the date's menu. The card is shown as large as fits under the toolbar, so a whole day is on one screen; the picture you copy or save is full size. -It needs a magpie with the `usage:day` read and `magpie.image` (see `engines.magpie`). An older magpie refuses the read, and the page says to update. +It needs a magpie with the `usage:day` read and `magpie.image` (see `engines.magpie`). An older magpie refuses the read, and the page says to update; one from before the week, month and all-time spans (a `from` in `usage:day`) shows days only, and says to update when you pick another. ## What it reads -Only `usage:day`, the one permission its `package.json` declares (`magpie.permissions`): a day's totals, each agent's and model's, the days before it that had calls, and the agents' and providers' logos. Nothing in it is a key, an account, a session or a prompt. +Only `usage:day`, the one permission its `package.json` declares (`magpie.permissions`): a day's (or a span's) totals, each agent's and model's, the days before it that had calls, and the agents' and providers' logos. Nothing in it is a key, an account, a session or a prompt. Like every plugin page, it runs in a sandboxed frame with no network (`connect-src 'none'`) and no inline scripts. The card is drawn on a canvas, the same in a light or dark theme, and its PNG goes to magpie through `_magpie/bridge.js`. In `magpie web` the browser copies or downloads it itself. @@ -27,4 +27,4 @@ Like every plugin page, it runs in a sandboxed frame with no network (`connect-s bun test packages/day-card ``` -中文:在 magpie 窗口里加一个「每日用量卡」页面,把某一天的用量打印成一张小票(Daily AI Statement):当天按 API 价计的费用、连续使用天数、请求数、错误数、与此前日均的倍数,每个 Agent 及其模型(带图标和费用),以及输入、输出 token 和缓存命中率。小票底部注明设计灵感来自 [nowdex](https://nowdex.app)。「背景」可以换成 15 种图案、4 张风景照片或自己的图片(选择、拖入或粘贴),边距可调,照片还可调模糊和压暗。可以「复制图片」到剪贴板,或「保存 PNG」到下载文件夹。只读取声明过的 `usage:day`,运行在无网络的沙盒 frame 里。 +中文:在 magpie 窗口里加一个「每日用量卡」页面,把某一天(或最近 7 天、30 天、全部)的用量打印成一张小票(Daily AI Statement):当天按 API 价计的费用、连续使用天数、请求数、错误数、与此前日均的倍数,每个 Agent 及其模型(带图标和费用),以及输入、输出 token 和缓存命中率。小票底部注明设计灵感来自 [nowdex](https://nowdex.app)。「背景」可以换成 15 种图案、4 张风景照片或自己的图片(选择、拖入或粘贴),边距可调,照片还可调模糊和压暗。左上角的开关切换「日 / 周 / 月 / 全部」。可以「复制图片」到剪贴板,或「保存 PNG」到下载文件夹。只读取声明过的 `usage:day`,运行在无网络的沙盒 frame 里。 diff --git a/packages/day-card/day-card.test.mjs b/packages/day-card/day-card.test.mjs index 0e0498f..ece48e6 100644 --- a/packages/day-card/day-card.test.mjs +++ b/packages/day-card/day-card.test.mjs @@ -119,6 +119,27 @@ test("the receipt: title, the day, its cost, then its parts", () => { for (const lang of Object.keys(V.TEXT)) expect(V.card(day, lang, {}, now).credit).toContain("nowdex.app") }) +test("a week, a month or all time is a receipt of its own", () => { + const span = { ...day, from: "2026-11-03", days: [{ date: "2026-11-03", calls: 5, tokens: 10, cost: 1 }, { date: "2026-11-07", calls: 5, tokens: 10, cost: 1 }, { date: "2026-11-08", calls: 5, tokens: 10, cost: 1 }, { date: "2026-11-09", calls: 5, tokens: 10, cost: 1 }] } + const w = V.card(span, "en", { span: "week" }, now) + expect(w.title).toBe("WEEKLY AI STATEMENT") + expect(w.head[0]).toEqual({ label: "PERIOD", value: "NOV 3, 2026 – NOV 9, 2026" }) + // 4 of the 7 days had calls, and $12.345 over them + expect(w.stats.slice(-2)).toEqual([{ label: "ACTIVE DAYS", value: "4 / 7" }, { label: "AVG ACTIVE DAY", value: "$3.09" }]) + expect(w.stats.map((s) => s.label)).not.toContain("VS AVERAGE DAY") + expect(V.card(span, "en", { span: "month" }, now).title).toBe("MONTHLY AI STATEMENT") + const a = V.card(span, "zh", { span: "all" }, now) + expect(a.title).toBe("累计 AI 账单") + expect(a.stats.slice(-2)[0]).toEqual({ label: "活跃天数", value: "4 天" }) + expect(V.card({ ...span, calls: 0 }, "en", { span: "week" }, now).empty).toBe("No calls in this period.") + // a magpie that answers a day (no from) draws a day, never a week's title + expect(V.card(day, "en", { span: "week" }, now).title).toBe("DAILY AI STATEMENT") + // what the read is asked, and the steps of the arrows + expect([V.spanFrom("day", "2026-11-09"), V.spanFrom("week", "2026-11-09"), V.spanFrom("month", "2026-11-09"), V.spanFrom("all", "2026-11-09")]).toEqual(["", "2026-11-03", "2026-10-11", "all"]) + expect([V.stepDate("week", "2026-11-09", -1, "2026-11-11"), V.stepDate("week", "2026-11-09", 1, "2026-11-11"), V.stepDate("day", "2026-11-09", 1, "2026-11-11")]).toEqual(["2026-11-02", "2026-11-11", "2026-11-10"]) + expect([V.fileName("2026-11-09"), V.fileName("2026-11-09", "week", "2026-11-03"), V.fileName("2026-11-09", "all", "2020-01-01")]).toEqual(["magpie 2026-11-09", "magpie 2026-11-03 to 2026-11-09", "magpie all 2026-11-09"]) +}) + test("agents and their models by cost, each with what was left out", () => { const withCost = { ...day, agents: day.agents.map((a) => ({ ...a, models: a.models.map((m, i) => ({ ...m, cost: [1, 9][i] ?? 0 })) })) } const c = V.card(withCost, "en", {}, now) diff --git a/packages/day-card/package.json b/packages/day-card/package.json index 66f81b8..cf03715 100644 --- a/packages/day-card/package.json +++ b/packages/day-card/package.json @@ -1,7 +1,7 @@ { "name": "@magpie-community/page-day-card", - "version": "0.2.3", - "description": "A page in magpie's window that draws a day of your AI usage as a card: tokens, cost, each agent and its models, your streak and your average day. Copy it as a picture or save it as a PNG", + "version": "0.3.0", + "description": "A page in magpie's window that draws a day, week, month or all of your AI usage as a card: tokens, cost, each agent and its models, your streak and your average day. Copy it as a picture or save it as a PNG", "files": ["page", "README.md"], "keywords": ["magpie", "magpie-page", "usage", "tokens", "cost", "card"], "license": "MIT", diff --git a/packages/day-card/page/app.js b/packages/day-card/page/app.js index 2139944..af340b4 100644 --- a/packages/day-card/page/app.js +++ b/packages/day-card/page/app.js @@ -1,5 +1,5 @@ // The page: reads usage:day (the only read package.json declares) for the -// day picked, draws it on a canvas as a receipt, and hands that canvas's +// day picked, or the week, month or all time ending on it, draws it on a canvas as a receipt, and hands that canvas's // PNG to magpie.image to copy or save. (async () => { const V = globalThis.DayView; @@ -8,7 +8,7 @@ const theme = await magpie.ready; let lang = theme.lang; const zone = (() => { try { return Intl.DateTimeFormat().resolvedOptions().timeZone || ""; } catch { return ""; } })(); - const state = { date: "", day: null, icons: new Map(), loading: 0, drawn: 0, bg: B.norm(null) }; + const state = { date: "", span: "day", day: null, icons: new Map(), loading: 0, drawn: 0, bg: B.norm(null) }; // ---- words ---- function words() { @@ -331,7 +331,7 @@ const n = ++state.drawn; const bg = state.bg; await fonts; - const c = V.card(day, lang); + const c = V.card(day, lang, { span: state.span }); const icons = new Map(); const [img] = await Promise.all([ backdrop(bg), @@ -404,8 +404,18 @@ async function load(date) { const n = ++state.loading; try { - const day = await magpie.read("usage:day", { date: date || "", tz: zone }); + const span = state.span; + const params = { date: date || "", tz: zone }; + if (span !== "day") { + // a week or a month ends on a date, so today's is worked out here + const end = date || state.day?.today || new Date().toLocaleDateString("en-CA"); + params.date = end; + params.from = V.spanFrom(span, end); + } + const day = await magpie.read("usage:day", params); if (n !== state.loading) return; + // a magpie from before spans answers one day and no `from` + if (span !== "day" && !day.from) throw new Error("usage:day from"); state.day = day; state.date = day.date; status(""); @@ -413,7 +423,7 @@ if (n !== state.loading) return; const m = String(e?.message || e); // a magpie from before usage:day refuses it as a read it doesn't have - status(/usage:day/.test(m) ? V.text(lang).update : `${V.text(lang).failed}: ${m}`, true, true); + status(m === "usage:day from" ? V.text(lang).updateSpan : /usage:day/.test(m) ? V.text(lang).update : `${V.text(lang).failed}: ${m}`, true, true); } paintBar(); await draw(); @@ -421,10 +431,19 @@ function paintBar() { const d = state.day; - $("date-text").textContent = d ? V.dateLabel(d.date, lang) : "…"; - $("next").disabled = !d || d.date >= d.today; - $("today").disabled = !d || d.date === d.today; - $("prev").disabled = !d; + const t = V.text(lang); + const span = state.span; + $("date-text").textContent = !d ? "…" : span === "all" ? t.allTime : span === "day" || !d.from ? V.dateLabel(d.date, lang) : `${V.dateLabel(d.from, lang, true)} – ${V.dateLabel(d.date, lang, true)}`; + const all = span === "all"; + $("next").disabled = !d || all || d.date >= d.today; + $("today").disabled = !d || all || d.date === d.today; + $("prev").disabled = !d || all; + $("date").disabled = all; + for (const [id, key] of [["prev", span === "day" ? "prev" : "earlier"], ["next", span === "day" ? "next" : "later"]]) { + $(id).setAttribute("aria-label", t[key]); + $(id).title = t[key]; + } + document.querySelectorAll("#spans [data-span]").forEach((b) => b.setAttribute("aria-checked", String(b.dataset.span === span))); } // ---- the day menu: the days that had calls, newest first ---- @@ -472,9 +491,16 @@ document.addEventListener("click", (e) => { if (!e.target.closest(".pick")) closeMenu(); }); document.addEventListener("keydown", (e) => { if (e.key === "Escape") closeMenu(); }); - $("prev").addEventListener("click", () => state.date && load(V.shift(state.date, -1))); - $("next").addEventListener("click", () => state.date && state.day && state.date < state.day.today && load(V.shift(state.date, 1))); + $("prev").addEventListener("click", () => state.date && load(V.stepDate(state.span, state.date, -1, state.day?.today || state.date))); + $("next").addEventListener("click", () => state.date && state.day && state.date < state.day.today && load(V.stepDate(state.span, state.date, 1, state.day.today))); $("today").addEventListener("click", () => load("")); + $("spans").addEventListener("click", (e) => { + const b = e.target.closest("[data-span]"); + if (!b || b.dataset.span === state.span) return; + state.span = b.dataset.span; + paintBar(); + load(state.date === state.day?.today ? "" : state.date); + }); // ---- the backdrop: a pattern, a photo from bg/, or the reader's picture ---- const images = new Map(); @@ -732,7 +758,7 @@ const b = $("save"); b.disabled = true; try { - const r = await magpie.image.save(await png(), V.fileName(state.date)); + const r = await magpie.image.save(await png(), V.fileName(state.date, state.span, state.day?.from)); if (r?.path) status(`${V.text(lang).saved} ${r.path}`); } catch (e) { status(`${V.text(lang).shareFailed}: ${e?.message || e}`, true); diff --git a/packages/day-card/page/index.html b/packages/day-card/page/index.html index 041b145..d352784 100644 --- a/packages/day-card/page/index.html +++ b/packages/day-card/page/index.html @@ -13,6 +13,12 @@