206 lines
9.1 KiB
HTML
206 lines
9.1 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
<title>CPU & disk chips</title>
|
|
<!--
|
|
conversation.header: two chips at the end of the conversation header's meta
|
|
line — its CPU and memory (perf.read's hub/perf/conversation/{sid}: live
|
|
CPU % · RAM while it runs, peak RAM · CPU time once it has ended) and what
|
|
it still keeps on disk (hub/perf/conversation/{sid}/storage, from 50 MB;
|
|
amber from 1 GB). Each opens the plugin's own page (Performance, Storage).
|
|
Polled while the conversation runs (CPU 5 s, disk 30 s), read once
|
|
otherwise; inline, sized both ways (resize(h, w)), and nothing at all
|
|
(setEmpty + resize(0, 0)) when there is nothing to say.
|
|
-->
|
|
<style>
|
|
* { box-sizing: border-box; }
|
|
html, body { margin: 0; padding: 0; background: transparent; overflow: hidden; }
|
|
body { font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; white-space: nowrap;
|
|
-webkit-font-smoothing: antialiased; }
|
|
#root { display: inline-flex; align-items: center; gap: 6px; height: 16px; vertical-align: top; }
|
|
#root:empty { display: none; }
|
|
.chip { display: inline-flex; align-items: center; gap: 4px; height: 16px; margin: 0; border: 0; border-radius: 999px;
|
|
padding: 0 6px; background: hsl(var(--muted)); color: hsl(var(--muted-foreground)); cursor: pointer;
|
|
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 10px; font-weight: 500;
|
|
line-height: 16px; font-variant-numeric: tabular-nums; transition: color .15s; }
|
|
.chip:hover { color: hsl(var(--foreground)); }
|
|
.chip:focus-visible { outline: 2px solid hsl(var(--ring, var(--primary))); outline-offset: 1px; }
|
|
.chip.warn { background: rgb(245 158 11 / .15); color: rgb(180 83 9); }
|
|
html.dark .chip.warn { color: rgb(252 211 77); }
|
|
svg.i { width: 10px; height: 10px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
|
|
flex-shrink: 0; }
|
|
@media (prefers-reduced-motion: reduce) { .chip { transition: none; } }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div id="root"></div>
|
|
<script>
|
|
(function () {
|
|
"use strict";
|
|
var A = window.aiAgentPlugin || {};
|
|
var init = A.init || {};
|
|
var root = document.getElementById("root");
|
|
var PID = A.id || "performance";
|
|
var HEIGHT = 16;
|
|
/** Below this a conversation's leftovers aren't worth a chip. */
|
|
var DISK_MIN = 50 * 1024 * 1024;
|
|
var DISK_WARN = 1024 * 1024 * 1024;
|
|
var first = init.update || {};
|
|
// reuseFrame: the host keeps this frame across conversation switches; the
|
|
// session comes in init.update / onUpdate (init.sessionId on older hosts).
|
|
var U = {}, sid = String(first.sessionId || init.sessionId || "");
|
|
var perf = null, disk = null, qPerf = null, qDisk = null, tPerf = null, tDisk = null;
|
|
|
|
var NS = "http://www.w3.org/2000/svg";
|
|
var ICONS = {
|
|
cpu: "M12 20v2M12 2v2M17 20v2M17 2v2M2 12h2M2 17h2M2 7h2M20 12h2M20 17h2M20 7h2M7 20v2M7 2v2M6 4h12a2 2 0 0 1 2 2v12a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2zM9 9h6v6H9z",
|
|
drive: "M22 12H2M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11zM6 16h.01M10 16h.01",
|
|
};
|
|
function svg(name) {
|
|
var n = document.createElementNS(NS, "svg");
|
|
n.setAttribute("viewBox", "0 0 24 24");
|
|
n.setAttribute("class", "i");
|
|
n.setAttribute("aria-hidden", "true");
|
|
var p = document.createElementNS(NS, "path");
|
|
p.setAttribute("d", ICONS[name]);
|
|
n.appendChild(p);
|
|
return n;
|
|
}
|
|
function fmtBytes(n) {
|
|
if (n == null) return "—";
|
|
var u = ["B", "KB", "MB", "GB", "TB"], v = n, i = 0;
|
|
while (v >= 1024 && i < u.length - 1) { v /= 1024; i++; }
|
|
return (v >= 100 || i < 2 ? v.toFixed(0) : v.toFixed(1)) + " " + u[i];
|
|
}
|
|
/** CPU in % of one core, like top ("35%", "240%"). */
|
|
function fmtPct(p) { return p == null ? "—" : (p >= 10 ? Math.round(p) : p.toFixed(1)) + "%"; }
|
|
/** CPU seconds as "42s" / "3.5 min" / "1.2 h". */
|
|
function fmtCpuTime(s) {
|
|
if (s == null) return "—";
|
|
if (s < 60) return (s < 10 ? s.toFixed(1) : Math.round(s)) + "s";
|
|
if (s < 3600) return (s / 60).toFixed(s < 600 ? 1 : 0) + " min";
|
|
return (s / 3600).toFixed(1) + " h";
|
|
}
|
|
function tilde(p) { return String(p).replace(/^\/(?:home|Users)\/[^/]+(?=\/|$)/, "~"); }
|
|
function go(page) { if (A.navigate) A.navigate("/plugins/" + PID + "/" + page); }
|
|
|
|
function chip(icon, text, title, page, warn) {
|
|
var b = document.createElement("button");
|
|
b.type = "button";
|
|
b.className = "chip" + (warn ? " warn" : "");
|
|
b.title = title;
|
|
b.setAttribute("aria-label", text + " — " + title.split("\n")[0]);
|
|
b.appendChild(svg(icon));
|
|
b.appendChild(document.createTextNode(text));
|
|
b.addEventListener("click", function () { go(page); });
|
|
return b;
|
|
}
|
|
|
|
function cpuChip() {
|
|
var d = perf && perf.perf;
|
|
if (!d || !d.tracked) return null;
|
|
var live = d.live;
|
|
var title = live
|
|
? "Now: " + fmtPct(live.cpu) + " CPU · " + fmtBytes(live.rss) + " over " + live.procs + " processes — total " +
|
|
fmtCpuTime(d.cpuSeconds) + " CPU, peak " + fmtBytes(d.peakRss)
|
|
: "Peak " + fmtBytes(d.peakRss) + " over up to " + d.peakProcs + " processes · " + fmtCpuTime(d.cpuSeconds) + " of CPU";
|
|
var text = live ? fmtPct(live.cpu) + " · " + fmtBytes(live.rss) : fmtBytes(d.peakRss) + " · " + fmtCpuTime(d.cpuSeconds);
|
|
return chip("cpu", text, title, "performance", false);
|
|
}
|
|
|
|
function diskChip() {
|
|
var d = disk;
|
|
if (!d || !(d.left >= DISK_MIN)) return null;
|
|
var kept = (d.items || []).filter(function (i) { return !i.gone && i.kind !== "transcript"; });
|
|
var ram = kept.some(function (i) { return i.ram; });
|
|
var title = ["Keeps " + fmtBytes(d.left) + " on disk" + (ram ? " (its scratchpad is in RAM)" : "") + ":"]
|
|
.concat(kept.map(function (i) { return fmtBytes(i.own) + " " + i.kind + " " + tilde(i.path); })).join("\n");
|
|
return chip("drive", fmtBytes(d.left), title, "storage", d.left >= DISK_WARN);
|
|
}
|
|
|
|
var lastW = -1, lastH = -1, empty = null;
|
|
function fit() {
|
|
var w = empty ? 0 : Math.ceil(root.getBoundingClientRect().width);
|
|
var h = empty ? 0 : HEIGHT;
|
|
if (w === lastW && h === lastH) return;
|
|
lastW = w; lastH = h;
|
|
if (A.resize) A.resize(h, w);
|
|
}
|
|
function render() {
|
|
var kids = [cpuChip(), diskChip()].filter(Boolean);
|
|
while (root.firstChild) root.removeChild(root.firstChild);
|
|
kids.forEach(function (k) { root.appendChild(k); });
|
|
var nowEmpty = !kids.length;
|
|
var changed = nowEmpty !== empty;
|
|
empty = nowEmpty;
|
|
// size first: the host shows the frame at the width it has just been told
|
|
fit();
|
|
if (changed && A.setEmpty) A.setEmpty(nowEmpty);
|
|
}
|
|
if (window.ResizeObserver) new ResizeObserver(fit).observe(root);
|
|
|
|
function read(payload, cb) {
|
|
if (A.query) return A.query("read", payload, cb);
|
|
var live = true;
|
|
function run() { return A.action("read", payload).then(function (x) { if (live) cb(x, {}); }, function (e) { if (live) cb(null, { error: String((e && e.message) || e) }); }); }
|
|
run();
|
|
return { stop: function () { live = false; }, refresh: run };
|
|
}
|
|
/** A poll skips a hidden tab; the last read after a run ends doesn't. */
|
|
function refresh(q, always) { if (q && (always || !document.hidden)) q.refresh(); }
|
|
/** Poll while it runs (CPU every 5 s, disk every 30 s, like the core chips
|
|
* did); a finished conversation is read once. */
|
|
function schedule() {
|
|
clearInterval(tPerf); clearInterval(tDisk);
|
|
tPerf = tDisk = null;
|
|
if (!U.running || !sid) return;
|
|
tPerf = setInterval(function () { refresh(qPerf); }, 5000);
|
|
tDisk = setInterval(function () { refresh(qDisk); }, 30000);
|
|
}
|
|
function start() {
|
|
if (qPerf) qPerf.stop();
|
|
if (qDisk) qDisk.stop();
|
|
qPerf = qDisk = null;
|
|
clearInterval(tPerf); clearInterval(tDisk);
|
|
tPerf = tDisk = null;
|
|
perf = disk = null;
|
|
render();
|
|
if (!sid) return;
|
|
var curSid = sid;
|
|
// Share the exact {view: "conversation", sid, series: ["10m", "life"]} key
|
|
// with conv-perf so both coalesce in batch-read / singleflight / cache.
|
|
// An error (a guest: perf.read is the owner's) keeps the last good answer, or nothing.
|
|
qPerf = read({ view: "conversation", sid: sid, series: ["10m", "life"] }, function (x) {
|
|
if (curSid !== sid) return;
|
|
if (x) perf = x;
|
|
render();
|
|
});
|
|
qDisk = read({ view: "conversationStorage", sid: sid }, function (x) {
|
|
if (curSid !== sid) return;
|
|
if (x) disk = x;
|
|
render();
|
|
});
|
|
}
|
|
var wasRunning = null;
|
|
function onUpdate(u) {
|
|
U = u && typeof u === "object" ? u : {};
|
|
if (Object.prototype.hasOwnProperty.call(U, "sessionId") && String(U.sessionId || "") !== sid) {
|
|
sid = String(U.sessionId || "");
|
|
wasRunning = null;
|
|
start();
|
|
}
|
|
if (wasRunning === true && !U.running) { refresh(qPerf, true); refresh(qDisk, true); } // its last numbers
|
|
wasRunning = !!U.running;
|
|
schedule();
|
|
}
|
|
start();
|
|
if (typeof A.onUpdate === "function") A.onUpdate(onUpdate);
|
|
onUpdate(init.update || {});
|
|
if (typeof A.onTheme === "function") A.onTheme(function () { render(); });
|
|
})();
|
|
</script>
|
|
</body>
|
|
</html>
|