Repository navigation
Expand file tree
/
Copy pathmonitor.js
More file actions
178 lines (162 loc) · 8.06 KB
/
Copy pathmonitor.js
File metadata and controls
178 lines (162 loc) · 8.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Resource usage: top-bar meters and the Task manager page. One poller feeds both.
import { S, agentChip, ctxPct, findSession, fmtTokens, isSubAgent, portChips, projectColor, renderSidebar, sessionTitle, shellLabel, showSession, updateAgentStates, sessionOnMachine } from "./app.js";
import { $, api, esc } from "./ui.js";
const POLL_MS = 2000;
const HISTORY = 60; // samples kept for sparklines (2 minutes)
const hist = { cpu: [], mem: [], gpu: [] };
let stats = null;
let pageEl = null;
export function startMonitor() {
poll();
setInterval(() => !document.hidden && poll(), POLL_MS);
}
async function poll() {
try {
stats = await api("/api/stats");
} catch {
return;
}
const s = stats.system;
push("cpu", s.cpu);
push("mem", (100 * s.mem_used) / s.mem_total);
if (s.gpu) push("gpu", s.gpu.util);
renderMeters();
updatePorts();
updateAgents();
if (pageEl && !pageEl.hidden) renderMonitor(pageEl);
}
// dev servers etc. listening in a terminal: chips in its pane header
function updatePorts() {
const next = {};
for (const [sid, u] of Object.entries(stats.sessions)) if (u.ports?.length) next[sid] = u.ports;
if (JSON.stringify(next) === JSON.stringify(S.ports)) return;
S.ports = next;
for (const el of document.querySelectorAll(".pane[data-sid] .pane-head .ports")) {
el.innerHTML = portChips(el.closest(".pane").dataset.sid);
}
}
// AI coding agents (Claude Code, Codex, ...) running in a terminal: header chip, sidebar badge
function updateAgents() {
const next = {};
for (const [sid, u] of Object.entries(stats.sessions)) if (u.agent) next[sid] = u.agent;
const before = JSON.stringify([S.agents, S.agentState]);
S.agents = next;
updateAgentStates();
if (JSON.stringify([S.agents, S.agentState]) === before) return;
// AI agents nav link: how many run, amber when one needs you
const count = $(".sb-agents .sb-count");
const here = Object.keys(next).filter(sessionOnMachine); // the machine picked in the sidebar
const needs = here.filter((sid) => S.agentState[sid] === "approval" && !isSubAgent(sid)).length;
count.hidden = !here.length;
count.textContent = needs ? `${needs} need${needs > 1 ? "" : "s"} you` : here.length;
count.classList.toggle("needs", !!needs);
for (const pane of document.querySelectorAll(".pane[data-sid]")) {
pane.classList.toggle("ai", !!next[pane.dataset.sid]);
const el = pane.querySelector(".pane-head .agent");
if (el) el.innerHTML = agentChip(pane.dataset.sid);
}
renderSidebar();
}
function push(k, v) {
hist[k].push(v);
if (hist[k].length > HISTORY) hist[k].shift();
}
export function fmtBytes(b) {
return b >= 2 ** 30 ? `${(b / 2 ** 30).toFixed(1)} GB` : `${Math.round(b / 2 ** 20)} MB`;
}
const level = (pct) => (pct >= 85 ? "high" : pct >= 60 ? "mid" : "");
function meter(label, pct, text, title) {
return `<span class="meter" data-level="${level(pct)}" title="${esc(title)}">
<span class="m-label">${label}</span><span class="m-bar"><i style="width:${Math.min(100, pct).toFixed(1)}%"></i></span><span class="m-val">${esc(text)}</span>
</span>`;
}
function renderMeters() {
const el = $("#sysmon");
if (!el || !stats) return;
const s = stats.system;
const memPct = (100 * s.mem_used) / s.mem_total;
el.innerHTML =
meter("CPU", s.cpu, `${Math.round(s.cpu)}%`, `CPU ${s.cpu.toFixed(1)}%`) +
meter("RAM", memPct, `${(s.mem_used / 2 ** 30).toFixed(1)}/${Math.round(s.mem_total / 2 ** 30)}G`, `Memory ${fmtBytes(s.mem_used)} of ${fmtBytes(s.mem_total)} (${Math.round(memPct)}%)`) +
(s.gpu ? meter("GPU", s.gpu.util, `${Math.round(s.gpu.util)}%`, `${s.gpu.name}: ${s.gpu.util}% · VRAM ${fmtBytes(s.gpu.mem_used)} of ${fmtBytes(s.gpu.mem_total)}`) : "") +
ctxMeter();
}
// context window of the agent in the focused terminal (Claude Code, Codex, Devin)
function ctxMeter() {
const a = S.focused && S.agents[S.focused];
const pct = ctxPct(a);
if (pct === null) return "";
const c = a.context;
return meter("CTX", pct, `${fmtTokens(c.used)}/${fmtTokens(c.window)}`,
`${a.label} context: ${fmtTokens(c.used)} of ${fmtTokens(c.window)} tokens (${pct}%)${c.estimated ? ", window estimated" : ""}`);
}
function spark(values) {
if (values.length < 2) return `<svg class="spark" viewBox="0 0 100 32" preserveAspectRatio="none"></svg>`;
const step = 100 / (HISTORY - 1);
const x0 = 100 - step * (values.length - 1);
const pts = values.map((v, i) => `${(x0 + i * step).toFixed(2)},${(31 - (Math.min(100, v) / 100) * 30).toFixed(2)}`);
return `<svg class="spark" viewBox="0 0 100 32" preserveAspectRatio="none" aria-hidden="true">
<polygon points="${x0.toFixed(2)},32 ${pts.join(" ")} 100,32" /><polyline points="${pts.join(" ")}" />
</svg>`;
}
function card(label, value, sub, key, pct) {
return `<div class="card mon-card" data-level="${level(pct)}">
<div class="mon-label">${label}</div>
<div class="mon-value">${esc(value)}</div>
<div class="mon-sub">${esc(sub)}</div>
${key ? spark(hist[key]) : ""}
</div>`;
}
export function renderMonitor(el) {
pageEl = el;
if (!el.dataset.wired) {
el.dataset.wired = "1";
el.addEventListener("click", (e) => {
const port = e.target.closest("[data-port]")?.dataset.port;
if (port) return window.open(`http://localhost:${port}`, "_blank", "noopener");
const sid = e.target.closest("tr[data-sid]")?.dataset.sid;
if (sid) showSession(sid);
});
}
const head = `<div class="page-head"><div><h1>Task manager</h1>
<p>Live usage of this machine and of each running terminal (its shell plus every process started from it). Updates every 2 seconds.</p></div></div>`;
if (!stats) {
el.innerHTML = `${head}<p class="faint">Loading…</p>`;
return;
}
const s = stats.system;
const memPct = (100 * s.mem_used) / s.mem_total;
const cards = [
card("CPU", `${s.cpu.toFixed(0)}%`, `${navigator.hardwareConcurrency || "?"} logical cores`, "cpu", s.cpu),
card("Memory", `${fmtBytes(s.mem_used)} / ${fmtBytes(s.mem_total)}`, `${Math.round(memPct)}% in use`, "mem", memPct),
s.gpu
? card("GPU", `${Math.round(s.gpu.util)}%`, `${s.gpu.name} · VRAM ${fmtBytes(s.gpu.mem_used)} / ${fmtBytes(s.gpu.mem_total)}`, "gpu", s.gpu.util)
: card("GPU", "n/a", "No NVIDIA GPU found (usage comes from nvidia-smi)", null, 0),
].join("");
const rows = Object.entries(stats.sessions)
.map(([sid, u]) => ({ sid, u, s: findSession(sid) }))
.filter((r) => r.s)
.sort((a, b) => b.u.cpu - a.u.cpu || b.u.mem - a.u.mem);
const total = rows.reduce((t, r) => ({ cpu: t.cpu + r.u.cpu, mem: t.mem + r.u.mem, procs: t.procs + r.u.procs }), { cpu: 0, mem: 0, procs: 0 });
const body = rows.length
? rows
.map(
({ sid, u, s: sess }) => `<tr data-sid="${sid}" title="Open terminal">
<td><span class="proj-dot" style="background:${projectColor(sess.project)}"></span>${esc(sessionTitle(sess))}<span class="faint"> · ${esc(shellLabel(sess.shell))}</span></td>
<td class="faint">${esc(sess.project.name)}</td>
<td class="num"><span class="cell-bar" data-level="${level(u.cpu)}"><i style="width:${Math.min(100, u.cpu)}%"></i></span>${u.cpu.toFixed(1)}%</td>
<td class="num">${fmtBytes(u.mem)}</td>
<td class="num">${u.procs}</td>
<td class="mono">${u.agent ? agentChip(sid) : esc((u.top || "").replace(/\.exe$/i, "")) || '<span class="faint" title="No processes besides the shell">—</span>'}</td>
<td>${portChips(sid)}</td>
</tr>`,
)
.join("")
: `<tr><td colspan="7" class="faint empty-row">No running terminals. A terminal starts when you open it.</td></tr>`;
el.innerHTML = `${head}<div class="mon-cards">${cards}</div>
<div class="card mon-table"><table>
<thead><tr><th>Terminal</th><th>Project</th><th class="num">CPU</th><th class="num">Memory</th><th class="num">Processes</th><th>Busiest process</th><th>Ports</th></tr></thead>
<tbody>${body}</tbody>
${rows.length ? `<tfoot><tr><td>All terminals</td><td></td><td class="num">${total.cpu.toFixed(1)}%</td><td class="num">${fmtBytes(total.mem)}</td><td class="num">${total.procs}</td><td></td><td></td></tr></tfoot>` : ""}
</table></div>`;
}