import { useState } from "react"; import { api } from "../api/client"; /** * Operator "inspect" REPL: run a read-only tool against the desk's live Docker * sandbox. Calls POST /sessions/:id/inspect, which routes through the desk's * persistent worker so it executes with the SAME container, path translation, * and read guards the agent uses. Tools are whitelisted server-side * (read_file, search_files, list_files, terminal — no writes). */ type Tool = "terminal" | "search_files" | "read_file" | "list_files"; const TOOLS: { id: Tool; label: string; hint: string }[] = [ { id: "terminal", label: "terminal", hint: "Run a shell command in the sandbox (e.g. ls -la /workspace)" }, { id: "search_files", label: "search_files", hint: "Grep file contents (or names) under a path" }, { id: "read_file", label: "read_file", hint: "Read a file with line numbers" }, { id: "list_files", label: "list_files", hint: "List a directory" }, ]; const INPUT: React.CSSProperties = { fontSize: 12, fontFamily: "monospace", padding: "6px 8px", borderRadius: 6, background: "rgba(255,255,255,0.06)", border: "1px solid var(--card-border)", color: "var(--text)", outline: "none", }; const LABEL: React.CSSProperties = { fontSize: 10, color: "var(--text-dim)", marginBottom: 2 }; export function InspectPanel({ sessionId }: { sessionId: string }) { const [tool, setTool] = useState("terminal"); const [command, setCommand] = useState("ls -la /workspace"); const [pattern, setPattern] = useState(""); const [target, setTarget] = useState<"content" | "files">("content"); const [path, setPath] = useState("/workspace"); const [busy, setBusy] = useState(false); const [stopping, setStopping] = useState(false); const [output, setOutput] = useState(""); const [isError, setIsError] = useState(false); function buildArgs(): Record { switch (tool) { case "terminal": return { command }; case "search_files": return { pattern, target, path: path || "/workspace" }; case "read_file": return { path }; case "list_files": return { path: path || "/workspace" }; } } async function run() { setBusy(true); setIsError(false); setStopping(false); try { const r = await api.sessions.inspect(sessionId, tool, buildArgs()); if (r.ok) { setOutput(formatResult(r.result)); setIsError(false); } else { setOutput(r.error || "inspect failed"); setIsError(true); } } catch (e) { setOutput(e instanceof Error ? e.message : String(e)); setIsError(true); } finally { setBusy(false); setStopping(false); } } async function stop() { setStopping(true); try { await api.sessions.inspectStop(sessionId); } catch { /* the in-flight run() will surface any error when it returns */ } } const hint = TOOLS.find((t) => t.id === tool)?.hint || ""; return (
Run read-only tools against this desk's sandbox — same container & paths the agent sees. First call may take a few seconds while the worker warms up.
tool
{tool === "terminal" && (
command setCommand(e.target.value)} onKeyDown={(e) => e.key === "Enter" && run()} style={INPUT} spellCheck={false} />
)} {tool === "search_files" && ( <>
pattern setPattern(e.target.value)} onKeyDown={(e) => e.key === "Enter" && run()} style={INPUT} spellCheck={false} />
target
path setPath(e.target.value)} onKeyDown={(e) => e.key === "Enter" && run()} style={INPUT} spellCheck={false} />
)} {(tool === "read_file" || tool === "list_files") && (
path setPath(e.target.value)} onKeyDown={(e) => e.key === "Enter" && run()} style={INPUT} spellCheck={false} placeholder={tool === "read_file" ? "/workspace/team_files/notes.md" : "/workspace"} />
)} {busy && ( )}
{hint}
        {output || "— output will appear here —"}
      
); } /** Tool results arrive as JSON strings; pull out the human-readable field, else * pretty-print. Falls back to the raw string for plain output. */ function formatResult(raw: string | undefined): string { if (!raw) return "(empty)"; try { const obj = JSON.parse(raw); if (obj && typeof obj === "object") { const o = obj as Record; for (const k of ["output", "content", "results", "matches", "error", "stdout"]) { if (typeof o[k] === "string" && o[k]) return o[k] as string; } return JSON.stringify(obj, null, 2); } return String(obj); } catch { return raw; } }