import { randomUUID } from "node:crypto"; import { access, mkdir, readdir, readFile, rm, stat, writeFile, } from "node:fs/promises"; import { homedir } from "node:os"; import path from "node:path"; import { defineTool, type ExtensionContext, keyHint, } from "@earendil-works/pi-coding-agent"; import { Text } from "@earendil-works/pi-tui"; import { Type } from "typebox"; import { type MockupRequest, type MockupResult, type MockupServer, startMockupServer, validateMockupRequest, } from "./mockup.js"; const DESCRIPTION = "Show HTML-fragment mockups in a browser for visual review. Returns accept or iterate, selected option IDs, notes, and annotations."; const CACHE_TTL_MS = 30 * 24 * 60 * 60 * 1000; const CACHE_LIMIT_BYTES = 256 * 1024 * 1024; const parameters = Type.Object({ title: Type.String({ description: "Short browser page title for this visual decision.", }), question: Type.String({ description: "The exact visual question the human should answer.", }), options: Type.Array( Type.Object({ id: Type.String({ description: "Stable option id. Letters, numbers, dot, underscore, dash only.", }), label: Type.String({ description: "Human-visible option label." }), description: Type.Optional( Type.String({ description: "One short sentence explaining the option.", }), ), html: Type.String({ description: "HTML fragment for this mockup option. Do not include , , , or . Scripts are allowed.", }), }), { minItems: 1, maxItems: 4, description: "One to four mockup options. One option is valid for reviewing a single screen.", }, ), multi: Type.Optional( Type.Boolean({ default: false, description: "Allow selecting multiple options.", }), ), }); export type MockupHistoryEntry = { iteration: number; question: string; options: Array<{ id: string; label: string }>; action: MockupResult["action"]; selected: string[]; notes: string; annotations: MockupResult["annotations"]; cacheManifest?: string; }; export type MockupToolDetails = MockupResult & { sessionId: string; chainId: string; iteration: number; cacheManifest?: string; cacheError?: string; history: MockupHistoryEntry[]; contextCapsule: string; }; type ActiveChain = { server: MockupServer; chainId: string; }; type RuntimeOptions = { cacheRoot?: string; openBrowser?: boolean; }; export function createMockupRuntime(options: RuntimeOptions = {}) { const active = new Map(); const contexts = new Map(); const cacheRoot = options.cacheRoot ?? mockupCacheRoot(); const remember = (ctx: ExtensionContext): string => { const sessionId = ctx.sessionManager.getSessionId(); contexts.set(sessionId, ctx); return sessionId; }; const abortAgent = (sessionId: string): boolean => { const ctx = contexts.get(sessionId); if (!ctx || ctx.isIdle()) return false; try { ctx.abort(); return true; } catch { return false; } }; const schedulePrune = (protectedSessionId?: string): void => { const timer = setTimeout(() => { void pruneMockupCache(cacheRoot, protectedSessionId).catch((error) => console.error("Mockup cache cleanup failed:", error), ); }, 0); timer.unref?.(); }; const tool = defineTool({ name: "mockup", label: "mockup", description: DESCRIPTION, promptSnippet: "Review HTML mockups in a browser", promptGuidelines: [ "Use mockup when visual comparison beats prose for UI, layout, styling, or interaction decisions.", "Pass HTML fragments only; never include , , , or .", "Provide 1-4 options; one option is valid when reviewing a single design.", "The tool owns browser review and feedback. Do not ask the user to repeat its result in chat.", "On iterate, revise the fragments and call mockup again. On accept, continue the design unless feedback requires another iteration.", ], parameters, renderCall(args, theme, context) { const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); const optionCount = Array.isArray(args.options) ? args.options.length : 0; const optionWord = `option${optionCount === 1 ? "" : "s"}`; let output = `${theme.fg("toolTitle", theme.bold("mockup"))} ${theme.fg("accent", args.title ?? "untitled")} ${theme.fg("toolOutput", `· ${optionCount} ${optionWord}${args.multi ? " · multi-select" : ""}`)}`; if (!context.expanded) { output += theme.fg( "dim", ` (${keyHint("app.tools.expand", "to expand")})`, ); } else { if (args.question) output += `\n${theme.fg("toolOutput", args.question)}`; for (const option of args.options ?? []) { const description = option.description ? theme.fg("dim", ` · ${option.description}`) : ""; output += `\n${theme.fg("accent", String(option.id))} ${theme.fg("toolOutput", option.label)}${description}`; } } text.setText(output); return text; }, renderResult(result, { expanded }, theme, context) { const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0); const details = result.details as Partial | undefined; if (!details) { const output = result.content .filter( (item): item is { type: "text"; text: string } => item.type === "text", ) .map((item) => item.text) .join("\n"); text.setText(theme.fg(context.isError ? "error" : "muted", output)); return text; } const action = details.action ?? "submitted"; const status = action === "accept" ? theme.fg("success", "✓ accepted") : theme.fg("warning", "↻ iterate"); const selected = (details.selected ?? []).join(", ") || "none"; const annotations = details.annotations ?? []; let output = `${status}${theme.fg("dim", ` · selected ${selected} · ${annotations.length} annotation${annotations.length === 1 ? "" : "s"}`)}`; if (!expanded) { output += theme.fg( "dim", ` (${keyHint("app.tools.expand", "to expand")})`, ); } else { if (details.notes) output += `\n${theme.fg("toolOutput", `notes: ${details.notes}`)}`; for (const annotation of annotations) { output += `\n${theme.fg("accent", annotation.optionId)} ${theme.fg("dim", `@ ${Math.round(annotation.x * 100)}%,${Math.round(annotation.y * 100)}%`)} ${theme.fg("toolOutput", annotation.note)}`; } if (details.history && details.history.length > 1) output += `\n${theme.fg("dim", `iterations: ${details.history.length}`)}`; if (details.cacheManifest) output += `\n${theme.fg("dim", `cache: ${details.cacheManifest}`)}`; else if (details.cacheError) output += `\n${theme.fg("warning", `cache unavailable: ${details.cacheError}`)}`; } text.setText(output); return text; }, async execute(toolCallId, params, signal, _onUpdate, ctx) { const request = validateMockupRequest(params); const sessionId = remember(ctx); const previous = latestMockupDetails(ctx); const continuing = previous?.sessionId === sessionId && previous.action === "iterate"; let chain = active.get(sessionId); const created = !chain; if (!chain) { chain = { chainId: continuing ? previous.chainId : randomUUID(), server: await startMockupServer(request, { openBrowser: options.openBrowser ?? process.env.PI_MOCKUP_NO_OPEN !== "1", abortAgent: () => abortAgent(sessionId), }), }; active.set(sessionId, chain); if (ctx.hasUI) ctx.ui.notify(`Mockup open: ${chain.server.url}`, "info"); } const iteration = continuing ? previous.iteration + 1 : 1; const history = continuing ? previous.history : []; const resultPromise = created ? chain.server.result : chain.server.present(request); try { const result = await abortable(resultPromise, signal, () => chain?.server.close("aborted"), ); let cacheManifest: string | undefined; let cacheError: string | undefined; try { cacheManifest = await persistMockupIteration(cacheRoot, { sessionId, sessionFile: ctx.sessionManager.getSessionFile(), chainId: chain.chainId, iteration, toolCallId, request, result, previousManifest: continuing ? previous.cacheManifest : undefined, }); } catch (error) { cacheError = error instanceof Error ? error.message : String(error); if (ctx.hasUI) ctx.ui.notify(`Mockup cache unavailable: ${cacheError}`, "warning"); } const current = historyEntry(iteration, request, result, cacheManifest); const nextHistory = [...history, current]; const contextCapsule = formatContextCapsule( chain.chainId, nextHistory, cacheError, ); const details: MockupToolDetails = { ...result, sessionId, chainId: chain.chainId, iteration, cacheManifest, cacheError, history: nextHistory, contextCapsule, }; if (result.action === "accept") { chain.server.close("accepted"); active.delete(sessionId); schedulePrune(sessionId); } return { content: [ { type: "text" as const, text: JSON.stringify({ action: result.action, selected: result.selected, notes: result.notes, annotations: result.annotations, cacheManifest, contextCapsule, }), }, ], details, }; } catch (error) { chain.server.close("failed"); active.delete(sessionId); schedulePrune(sessionId); throw error; } }, }); return { tool, remember, contextCapsule(ctx: ExtensionContext): string | undefined { const sessionId = remember(ctx); const details = latestMockupDetails(ctx); return details?.sessionId === sessionId ? details.contextCapsule : undefined; }, agentWorking(ctx: ExtensionContext): void { const sessionId = remember(ctx); active.get(sessionId)?.server.progress("Pi is working on the revision."); }, agentSettled(ctx: ExtensionContext): void { const sessionId = remember(ctx); active.get(sessionId)?.server.settled(); }, shutdown(ctx: ExtensionContext): void { const sessionId = ctx.sessionManager.getSessionId(); active.get(sessionId)?.server.close("session ended"); active.delete(sessionId); contexts.delete(sessionId); schedulePrune(); }, }; } function latestMockupDetails( ctx: ExtensionContext, ): MockupToolDetails | undefined { const branch = ctx.sessionManager.getBranch(); for (let index = branch.length - 1; index >= 0; index--) { const entry = branch[index] as unknown as Record; if (entry.type !== "message") continue; const message = entry.message; if (!isRecord(message)) continue; if (message.role !== "toolResult" || message.toolName !== "mockup") continue; const details = message.details; if (!isRecord(details) || typeof details.contextCapsule !== "string") return undefined; return details as unknown as MockupToolDetails; } return undefined; } function historyEntry( iteration: number, request: MockupRequest, result: MockupResult, cacheManifest?: string, ): MockupHistoryEntry { return { iteration, question: request.question, options: request.options.map(({ id, label }) => ({ id, label })), action: result.action, selected: result.selected, notes: result.notes, annotations: result.annotations, cacheManifest, }; } export function formatContextCapsule( chainId: string, history: MockupHistoryEntry[], cacheError?: string, ): string { const latest = history.at(-1); if (!latest) return ""; const lines = [ "Mockup context capsule", `Chain: ${chainId}`, `Current: iteration ${latest.iteration}, ${latest.action}`, `Question: ${latest.question}`, `Selected: ${selectedLabels(latest)}`, `Notes: ${latest.notes || "none"}`, ]; if (latest.annotations.length === 0) lines.push("Annotations: none"); else { lines.push("Annotations:"); for (const annotation of latest.annotations) lines.push( `- ${annotation.optionId} @ ${Math.round(annotation.x * 100)}%,${Math.round(annotation.y * 100)}%: ${annotation.note}`, ); } lines.push( "Rejected rationale: not recorded separately; never infer rejection from non-selection.", `Cache manifest: ${latest.cacheManifest ?? `unavailable${cacheError ? ` (${cacheError})` : ""}`}`, ); if (history.length > 1) { lines.push("Earlier iterations, newest first:"); for (const entry of history.slice(0, -1).reverse()) { lines.push( `- ${entry.iteration} ${entry.action}; selected ${selectedLabels(entry)}; notes: ${entry.notes || "none"}`, ); for (const annotation of entry.annotations) lines.push(` annotation ${annotation.optionId}: ${annotation.note}`); if (entry.cacheManifest) lines.push(` cache: ${entry.cacheManifest}`); } } return lines.join("\n"); } function selectedLabels(entry: MockupHistoryEntry): string { if (entry.selected.length === 0) return "none"; const labels = new Map( entry.options.map((option) => [option.id, option.label]), ); return entry.selected .map((id) => `${id} (${labels.get(id) ?? id})`) .join(", "); } export function mockupCacheRoot( platform: NodeJS.Platform = process.platform, env: NodeJS.ProcessEnv = process.env, home: string = homedir(), ): string { if (platform === "win32") return path.join( env.LOCALAPPDATA ?? path.join(home, "AppData", "Local"), "pi", "cache", "mockups", ); if (platform === "darwin") return path.join(home, "Library", "Caches", "pi", "mockups"); return path.join( env.XDG_CACHE_HOME ?? path.join(home, ".cache"), "pi", "mockups", ); } type PersistInput = { sessionId: string; sessionFile: string | undefined; chainId: string; iteration: number; toolCallId: string; request: MockupRequest; result: MockupResult; previousManifest?: string; }; export async function persistMockupIteration( root: string, input: PersistInput, ): Promise { const sessionDir = path.join(root, safeSegment(input.sessionId)); const iterationDir = path.join( sessionDir, safeSegment(input.chainId), `${String(input.iteration).padStart(3, "0")}-${safeSegment(input.toolCallId)}`, ); await mkdir(iterationDir, { recursive: true }); await Promise.all([ writeFile( path.join(sessionDir, "session.json"), `${JSON.stringify({ sessionId: input.sessionId, sessionFile: input.sessionFile }, null, 2)}\n`, ), writeFile( path.join(iterationDir, "request.json"), `${JSON.stringify(input.request, null, 2)}\n`, ), writeFile( path.join(iterationDir, "feedback.json"), `${JSON.stringify(input.result, null, 2)}\n`, ), ...input.request.options.map((option) => writeFile( path.join(iterationDir, `${safeSegment(option.id)}.html`), option.html, ), ), ]); const manifest = path.join(iterationDir, "manifest.json"); await writeFile( manifest, `${JSON.stringify( { version: 1, recordedAt: new Date().toISOString(), sessionId: input.sessionId, chainId: input.chainId, iteration: input.iteration, toolCallId: input.toolCallId, previousManifest: input.previousManifest, request: "request.json", feedback: "feedback.json", options: input.request.options.map((option) => ({ id: option.id, label: option.label, html: `${safeSegment(option.id)}.html`, })), }, null, 2, )}\n`, ); return manifest; } export async function pruneMockupCache( root: string, protectedSessionId?: string, now = Date.now(), policy: { ttlMs: number; maxBytes: number } = { ttlMs: CACHE_TTL_MS, maxBytes: CACHE_LIMIT_BYTES, }, ): Promise { const sessions = await directories(root); const candidates: Array<{ path: string; modified: number; bytes: number }> = []; for (const session of sessions) { if (session.name === safeSegment(protectedSessionId ?? "")) continue; const metadataPath = path.join(session.path, "session.json"); const metadata = await readJson(metadataPath); if ( isRecord(metadata) && typeof metadata.sessionFile === "string" && !(await exists(metadata.sessionFile)) ) { await rm(session.path, { recursive: true, force: true }); continue; } for (const chain of await directories(session.path)) { for (const iteration of await directories(chain.path)) { const info = await stat(iteration.path); if (now - info.mtimeMs >= policy.ttlMs) { await rm(iteration.path, { recursive: true, force: true }); continue; } candidates.push({ path: iteration.path, modified: info.mtimeMs, bytes: await directorySize(iteration.path), }); } } } let total = candidates.reduce((sum, candidate) => sum + candidate.bytes, 0); for (const candidate of candidates.sort( (left, right) => left.modified - right.modified, )) { if (total <= policy.maxBytes) break; await rm(candidate.path, { recursive: true, force: true }); total -= candidate.bytes; } } async function directories( root: string, ): Promise> { try { return (await readdir(root, { withFileTypes: true })) .filter((entry) => entry.isDirectory() && !entry.isSymbolicLink()) .map((entry) => ({ name: entry.name, path: path.join(root, entry.name), })); } catch (error) { if (isNotFound(error)) return []; throw error; } } async function directorySize(root: string): Promise { let bytes = 0; for (const entry of await readdir(root, { withFileTypes: true })) { if (entry.isSymbolicLink()) continue; const child = path.join(root, entry.name); if (entry.isDirectory()) bytes += await directorySize(child); else if (entry.isFile()) bytes += (await stat(child)).size; } return bytes; } async function readJson(file: string): Promise { try { return JSON.parse(await readFile(file, "utf8")); } catch (error) { if (isNotFound(error)) return undefined; throw error; } } async function exists(file: string): Promise { try { await access(file); return true; } catch { return false; } } function safeSegment(value: string): string { return value.replaceAll(/[^a-zA-Z0-9._-]/g, "_") || "unknown"; } function isRecord(value: unknown): value is Record { return !!value && typeof value === "object" && !Array.isArray(value); } function isNotFound(error: unknown): boolean { return isRecord(error) && error.code === "ENOENT"; } function abortable( promise: Promise, signal: AbortSignal | undefined, abort: () => void, ): Promise { if (!signal) return promise; if (signal.aborted) { abort(); return Promise.reject(new Error("mockup aborted")); } return new Promise((resolve, reject) => { const onAbort = () => { abort(); reject(new Error("mockup aborted")); }; signal.addEventListener("abort", onAbort, { once: true }); promise .then(resolve, reject) .finally(() => signal.removeEventListener("abort", onAbort)); }); }