feat(maestro): web client (Vite + TS + xterm.js)

NAP-218. Browser UI for the prototype: catalog tree derived from
`metadata.label`, live updates over `WS /api/events`, search across
session name+label, spawn modal (with optional git worktree), and an
attach view that opens the daemon's per-session WS for xterm.js.

Framework choice: vanilla TypeScript. The interactivity surface is
small enough that pulling in a framework wouldn't pay back; xterm.js
does the heavy lifting. Easy to swap later without protocol impact.

Hash-based routing (`#/`, `#/attach/<id>`) keeps the registry's
static surface dumb — no SPA fallback needed; concurrent attaches
work by opening the same hash URL in another tab.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-25 09:57:11 +00:00
co-authored by Claude Opus 4.7
parent b68e39a9e5
commit e68c9b6d73
16 changed files with 2188 additions and 0 deletions
+171
View File
@@ -0,0 +1,171 @@
import { FitAddon } from "@xterm/addon-fit";
import { Terminal } from "@xterm/xterm";
import "@xterm/xterm/css/xterm.css";
import { getConnectInfo } from "./api.js";
import type { ClientControl, Uuid } from "./types.js";
interface AttachHandle {
dispose: () => void;
}
function statusMessage(host: HTMLElement, kind: "info" | "error", text: string): void {
host.innerHTML = `<div class="attach-status status-${kind}">${text}</div>`;
}
function streamUrl(daemonUrl: string, sessionId: Uuid, jwt: string): string {
let base = daemonUrl;
// Allow operators to give an http(s) URL; convert to ws(s).
if (base.startsWith("http://")) base = "ws://" + base.slice("http://".length);
else if (base.startsWith("https://")) base = "wss://" + base.slice("https://".length);
// Trim trailing slash.
base = base.replace(/\/+$/, "");
const encId = encodeURIComponent(sessionId);
const encJwt = encodeURIComponent(jwt);
return `${base}/sessions/${encId}/stream?token=${encJwt}`;
}
export async function renderAttach(
root: HTMLElement,
sessionId: Uuid,
): Promise<AttachHandle> {
root.innerHTML = "";
const shell = document.createElement("div");
shell.className = "attach";
shell.innerHTML = `
<header class="attach-header">
<a class="btn btn-ghost back" href="#/" aria-label="Back to catalog">←</a>
<span class="attach-title" title="${sessionId}">${sessionId.slice(0, 8)}</span>
<span class="attach-state" data-state="connecting">connecting…</span>
</header>
<div class="attach-body"></div>
`;
root.appendChild(shell);
const body = shell.querySelector(".attach-body") as HTMLDivElement;
const stateEl = shell.querySelector(".attach-state") as HTMLSpanElement;
const setState = (s: string, kind: "connecting" | "open" | "closed" | "error") => {
stateEl.textContent = s;
stateEl.dataset.state = kind;
};
statusMessage(body, "info", "Fetching connect info…");
let connect;
try {
connect = await getConnectInfo(sessionId);
} catch (e) {
setState("error", "error");
statusMessage(body, "error", `Failed to get connect info: ${(e as Error).message}`);
return { dispose: () => {} };
}
body.innerHTML = "";
const term = new Terminal({
cursorBlink: true,
fontFamily:
'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace',
fontSize: 13,
convertEol: false,
theme: {
background: "#101115",
foreground: "#e6e6e6",
cursor: "#e6e6e6",
},
scrollback: 5000,
allowProposedApi: true,
});
const fit = new FitAddon();
term.loadAddon(fit);
term.open(body);
const tryFit = () => {
try {
fit.fit();
} catch {
// ignore — fit fails if container is detached
}
};
tryFit();
const wsUrl = streamUrl(connect.daemon_url, sessionId, connect.jwt);
const ws = new WebSocket(wsUrl);
ws.binaryType = "arraybuffer";
const sendControl = (msg: ClientControl) => {
if (ws.readyState !== WebSocket.OPEN) return;
ws.send(JSON.stringify(msg));
};
const onResize = () => {
tryFit();
sendControl({ type: "Resize", cols: term.cols, rows: term.rows });
};
ws.onopen = () => {
setState("connected", "open");
sendControl({ type: "Resize", cols: term.cols, rows: term.rows });
term.focus();
};
ws.onmessage = (ev) => {
if (typeof ev.data === "string") {
// Text frames: ignore unknown control messages; we just need a parse.
try {
JSON.parse(ev.data) as ClientControl;
} catch {
// ignore
}
return;
}
if (ev.data instanceof ArrayBuffer) {
term.write(new Uint8Array(ev.data));
} else if (ev.data instanceof Blob) {
// Some browsers may default to Blob despite binaryType="arraybuffer".
ev.data.arrayBuffer().then((b) => term.write(new Uint8Array(b)));
}
};
ws.onerror = () => {
setState("error", "error");
};
ws.onclose = (ev) => {
setState(`closed (${ev.code})`, "closed");
};
// PTY input: every keystroke goes out as a binary frame.
const dataDisp = term.onData((data) => {
if (ws.readyState !== WebSocket.OPEN) return;
ws.send(new TextEncoder().encode(data));
});
const resizeDisp = term.onResize(({ cols, rows }) => {
sendControl({ type: "Resize", cols, rows });
});
window.addEventListener("resize", onResize);
// Keep focus + sizing correct when the visual viewport shifts (mobile keyboards).
const visual = window.visualViewport ?? null;
visual?.addEventListener("resize", onResize);
let disposed = false;
const dispose = () => {
if (disposed) return;
disposed = true;
window.removeEventListener("resize", onResize);
visual?.removeEventListener("resize", onResize);
dataDisp.dispose();
resizeDisp.dispose();
try {
ws.close();
} catch {
// ignore
}
term.dispose();
};
return { dispose };
}