Files
plugins/packages/page-harnessweight/lib/brands.mjs
T
webkuborandwebkubor 186b642f5a page-harnessweight 0.4.0: weigh each agent harness behind the gateway (#922)
A page plugin that compares the agent harnesses behind magpie's gateway by agent: input tokens, cache hits, failures, latency and estimated tool-definition size. A local service on 127.0.0.1:3496 reads magpie's usage ledger, and a read-only gateway hook writes tool counts, sizes and names to <config>/harnessweight/tools.jsonl.

Since the review at e0a0ae97 (7c6cd626a, then main merged in):
- Port 3492 -> 3496 (window-warmup has 3492; 3496 is used by no other package).
- Config dir: input.directory, then a cwd probe, then an absolute XDG_CONFIG_HOME, then ~/.config/magpie. A relative XDG is ignored.
- tools.jsonl is append-only. Past 8 MiB it is renamed to tools.jsonl.1. Age is checked at most hourly. Appends from two hosts are serialized by a cross-process lock that is skipped after 100 ms.
- MAGPIE_ADDR is honoured; the UI has English and Chinese (zh-TW and ja read English); preview PNGs dropped; /%E0 returns 400.

Run at 961b20e63c7e00e1dcca723bc2aa1d302f5dd9a3, in WSL (magpie-desktop, Node 22.23.4):
- node --test test/*.test.mjs in the package: 60 pass, 0 fail.
- bun scripts/check.mjs: clean (registry.json ok; the package has hooks, so check skips it).
- Probes against createHandler:
  - Origin null without the token: 403. With the token: 200.
  - A foreign Origin, a loopback Origin on another port, a file:// Origin, a rebound Host and a localhost Host on another port: all 403.
  - The index HTML carries the token and no ACAO.
  - ..%2f: 400. /%E0: 400.
- Rollover with 40-tool rows (1306 B each), 9.5 MiB written: the file peaked at 8,388,590 B (cap 8,388,608) with one rename to .1. Append p50 was 1.35 ms, p99 9.2 ms and max 24 ms.
- Portable dir: the hook's cwd probe and the service's input.directory gave the same tools.jsonl.
- Broken by hand:
  - Size rotation turned off: two rollover tests went red.
  - The Host check removed: the service test "other hosts, other origins and path traversal are refused" went red.

Co-authored-by: webkubor <29485642+webkubor@users.noreply.github.com>
2026-11-10 17:37:33 +08:00

91 lines
3.5 KiB
JavaScript

// Each agent's display name and icon, exactly as magpie shows them. magpie's
// gateway already knows both: GET /v1/magpie/usage lists every agent with a
// name ("Claude Code") and an icon key ("claudecode-color"). The icon files
// live in app/icons/, named by that key; a key with no file falls back to the
// agent's initial in the page.
import { readFile, readdir } from "node:fs/promises"
import { dirname, join } from "node:path"
import { fileURLToPath } from "node:url"
const ICONS = join(dirname(fileURLToPath(import.meta.url)), "..", "app", "icons")
const TTL_MS = 10 * 60e3
export function gatewayUrl(options = {}) {
const explicit = options.gatewayUrl || process.env.MAGPIE_GATEWAY_URL
if (explicit) return String(explicit).trim().replace(/\/+$/, "")
// MAGPIE_ADDR is the host's host:port, not a URL. Only wildcard bind
// addresses become IPv4 loopback; an explicit IPv6 listener stays IPv6.
const addr = String(process.env.MAGPIE_ADDR || "").trim().replace(/^(0\.0\.0\.0|\[::\])(:|$)/, "127.0.0.1$2")
if (/^(\[[0-9a-f:.]+\]|[\w.-]+):\d+$/i.test(addr)) return `http://${addr}`
return "http://127.0.0.1:3425"
}
export function createBrands(options = {}, fetchImpl = globalThis.fetch) {
let at = 0
let map = new Map()
let files = null
let pending = null
async function iconFiles() {
if (!files) {
files = new Map()
for (const f of await readdir(ICONS).catch(() => [])) {
const dot = f.lastIndexOf(".")
if (dot > 0 && /\.(svg|png|webp)$/i.test(f)) files.set(f.slice(0, dot), f)
}
}
return files
}
async function refresh() {
if (Date.now() - at < TTL_MS) return
if (pending) return pending
pending = (async () => {
const have = await iconFiles()
try {
const res = await fetchImpl(`${gatewayUrl(options)}/v1/magpie/usage?period=30d`, { signal: AbortSignal.timeout(3000) })
if (!res.ok) throw new Error(`HTTP ${res.status}`)
const body = await res.json()
const next = new Map()
for (const g of body.agents || []) {
if (!g || !g.id) continue
const key = typeof g.icon === "string" && g.icon !== "generic" ? g.icon : ""
next.set(String(g.id), { name: String(g.name || g.id), icon: have.get(key) || null })
}
map = next
} catch {
// the gateway is down or older: keep what we had, retry sooner
at = Date.now() - TTL_MS + 30e3
return
}
at = Date.now()
})().finally(() => { pending = null })
return pending
}
// the brand for an agent id: magpie's, else the id itself and an icon file
// whose name is the id, else no icon
function of(agent) {
const b = map.get(agent)
if (b) return b
const f = files?.get(String(agent).toLowerCase()) || files?.get(`${String(agent).toLowerCase()}-color`)
return { name: agent, icon: f || null }
}
// an icon as a data: URI, so the page needs no request for it (in magpie's
// sandboxed frame, images the script adds after load do not show)
const uris = new Map()
const TYPES = { svg: "image/svg+xml", png: "image/png", webp: "image/webp" }
async function dataUri(file) {
if (!file) return null
if (!uris.has(file)) {
const buf = await readFile(join(ICONS, file)).catch(() => null)
const ext = file.slice(file.lastIndexOf(".") + 1).toLowerCase()
uris.set(file, buf ? `data:${TYPES[ext]};base64,${buf.toString("base64")}` : null)
}
return uris.get(file)
}
return { refresh, of, dataUri }
}