diff --git a/README.md b/README.md index d8d4003..f20e9dc 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ These add a page to magpie's window, in the app and in `magpie web`, under Plugi | Package | Page | Reads | |---|---|---| | [usage](packages/usage) | Usage glance: calls, errors, tokens and cost for today, 7 or 30 days, and the models behind them | `usage:summary` | +| [day-card](packages/day-card) | Day card: a day of your usage drawn as a card (tokens, cost, each agent and its models, streak, average day), copied as a picture or saved as a PNG | `usage:day` | | [powercontext](packages/powercontext) | Connection settings for an independently deployed PowerContext service | None | | [nowledge-mem](packages/nowledge-mem) | Connection settings, recall and capture switches, a real connection test, and the scope of what is sent | None | | [agent-office](packages/agent-office) | What are my agents doing: a real WebGL 3D office with six scenes, an isometric/2D office, grouped workbench, Today usage and recent conversations | no `magpie.read`; its host part reads local session files of Claude Code, Codex, Copilot CLI, Kimi Code, dsh, Gemini CLI, Qwen Code, Pi, omp, Qoder, WorkBuddy, CodeBuddy and Grok Build, including prompts, replies and tool commands (`text:false` keeps statuses and tool names only) | diff --git a/packages/day-card/README.md b/packages/day-card/README.md new file mode 100644 index 0000000..f60b11f --- /dev/null +++ b/packages/day-card/README.md @@ -0,0 +1,27 @@ +# @magpie-community/page-day-card + +A page in [magpie](https://usemagpie.ai)'s window that draws a day of your AI usage as a card. It shows the day's tokens, cost and calls, input, output, cache reads and the cache hit rate, then each agent with its models and logos, your streak and your average active day, and the last two weeks as small bars. **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. + +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. + +## 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. + +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 in magpie's theme, light or dark, and its PNG goes to magpie through `_magpie/bridge.js`. In `magpie web` the browser copies or downloads it itself. + +## Files + +- `page/index.html`, `page/style.css`: the page. +- `page/view.js`: what the card says, in English, 简体中文, 繁體中文, 日本語 and Deutsch, with no DOM. The test loads it. +- `page/app.js`: reads through the bridge, draws the card, copies and saves it, and refreshes today's every minute. + +## Test + +```sh +bun test packages/day-card +``` + +中文:在 magpie 窗口里加一个「每日用量卡」页面,把某一天的用量画成一张卡片:token、费用、调用次数、缓存命中率,每个 Agent 及其模型(带图标)、连续使用天数、活跃日均和最近两周的小柱状图。可以「复制图片」到剪贴板,或「保存 PNG」到下载文件夹。只读取声明过的 `usage:day`,运行在无网络的沙盒 frame 里。 diff --git a/packages/day-card/day-card.test.mjs b/packages/day-card/day-card.test.mjs new file mode 100644 index 0000000..3bea99e --- /dev/null +++ b/packages/day-card/day-card.test.mjs @@ -0,0 +1,179 @@ +import { expect, test } from "bun:test" +import { readFileSync, readdirSync } from "node:fs" +import { join } from "node:path" + +const dir = import.meta.dir +const page = join(dir, "page") +const read = (f) => readFileSync(join(page, f), "utf8") +const pkg = JSON.parse(readFileSync(join(dir, "package.json"), "utf8")) + +// view.js is a classic script, as the page loads it +const scope = {} +new Function("globalThis", read("view.js"))(scope) +const V = scope.DayView + +// usage:day as magpie answers it (internal/gui/pageday.go, usageapi.DayOf) +const day = { + date: "2026-11-09", today: "2026-11-09", + calls: 412, errors: 3, input: 1_200_000, output: 300_000, cacheRead: 8_000_000, cacheWrite: 500_000, reasoning: 0, + cost: 12.345, unpriced: 2, cacheUnknown: 0, + agents: [ + { id: "claude", name: "Claude Code", icon: "claude-color", calls: 300, input: 1_000_000, output: 200_000, cacheRead: 7_000_000, cacheWrite: 400_000, cost: 10, + models: [ + { model: "claude-opus-4-1", provider: "anthropic", providerName: "Anthropic", icon: "claude-color", input: 800_000, output: 150_000, cacheRead: 6_000_000, cacheWrite: 300_000 }, + { model: "claude-haiku-4-5", provider: "anthropic", providerName: "Anthropic", icon: "claude-color", input: 200_000, output: 50_000, cacheRead: 1_000_000, cacheWrite: 100_000 }, + ] }, + { id: "codex", name: "Codex", icon: "openai", calls: 100, input: 150_000, output: 90_000, cacheRead: 900_000, cacheWrite: 0, cost: 2.3, + models: [{ model: "gpt-5-codex", provider: "openai", icon: "openai", input: 150_000, output: 90_000, cacheRead: 900_000 }], moreModels: 3 }, + { id: "", name: "", icon: "generic", calls: 12, input: 50_000, output: 10_000, cacheRead: 100_000, cacheWrite: 100_000, cost: 0.04, models: [] }, + ], + days: [ + { date: "2026-11-01", calls: 10, tokens: 1000, cost: 1 }, + { date: "2026-11-06", calls: 10, tokens: 2000, cost: 2 }, + { date: "2026-11-07", calls: 10, tokens: 3000, cost: 3 }, + { date: "2026-11-08", calls: 10, tokens: 4000, cost: 4 }, + { date: "2026-11-09", calls: 412, tokens: 10_000_000, cost: 12.345 }, + ], + icons: {}, currency: "usd", rate: 7.1, +} + +test("asks for one read, usage:day, and nothing else", () => { + expect(pkg.magpie.permissions).toEqual(["usage:day"]) + const reads = new Set() + for (const f of readdirSync(page).filter((f) => f.endsWith(".js"))) { + for (const m of read(f).matchAll(/magpie\.read\(\s*"([^"]+)"/g)) reads.add(m[1]) + // no way out of the frame: magpie's policy refuses these anyway + expect(read(f)).not.toMatch(/\bfetch\(|XMLHttpRequest|WebSocket|sendBeacon|EventSource|importScripts/) + } + expect([...reads]).toEqual(["usage:day"]) +}) + +test("is a page magpie serves: its files in page/, scripts as files", () => { + expect(pkg.main).toBeUndefined() + expect(pkg.exports).toBeUndefined() + expect(pkg.engines.magpie).toMatch(/^>=0\.1\.\d+$/) + expect(Object.keys(pkg.magpie.page.titles).sort()).toEqual(["de", "ja", "zh", "zh-TW"]) + const html = read("index.html") + expect(html).toContain('') + // the policy runs no inline script + for (const m of html.matchAll(/ + + + +
+