import type { ExtensionAPI, ExtensionContext } from "@earendil-works/pi-coding-agent"; import { open } from "node:fs/promises"; import { readOdkLinkConfigs } from "./config.ts"; import { registerConsoleExtension } from "./console-extension.ts"; import { createControlTcpTransport } from "./control-transport.ts"; import { aggregateDeskSnapshots, selectConsoleDesk } from "./desk-set.ts"; import { eventsFromMessage, promptFromMessage } from "./events.ts"; import { scanDirectorySessions, SessionDiscovery } from "./discovery.ts"; import { DeskReporter, type OdkLinkSnapshot } from "./reporter.ts"; import { SessionHost } from "./session-host.ts"; import { CurrentSessionReplay } from "./session-replay.ts"; import { createTcpTransport } from "./transport.ts"; const CONFIG_HINT = "set ODK_LINK_ADDRESS and ODK_LINK_TOKEN, or list desks in ~/.config/odk/desks.json, to report this machine"; /** Read only a bounded tail before it enters the reporter's own 300-event/1MiB retention pipeline. */ const MAX_DURABLE_SESSION_BYTES = 4 * 1024 * 1024; function workspaceName(cwd: string): string { const parts = cwd.split(/[/\\]/).filter((part) => part.length > 0); return parts.length > 0 ? (parts[parts.length - 1] ?? cwd) : cwd; } /** * What a running session is doing, in the card's own vocabulary: the tool and the * one argument that says what it is working on, as `read: src/example.js` does. * A tool whose arguments carry no such line is named and nothing more, so no * argument is echoed beyond the summary the desk shows. */ function toolSummary(event: { toolName?: unknown; args?: unknown }): string { const name = typeof event.toolName === "string" && event.toolName.length > 0 ? event.toolName : "tool"; const args = (event.args ?? {}) as Record; const hint = ["command", "path", "file", "pattern", "url", "query"] .map((key) => args[key]) .find((value): value is string => typeof value === "string" && value.trim().length > 0); return hint === undefined ? name : `${name}: ${hint.trim()}`; } /** * The current session manager points at its own trusted durable log. Read its * bounded tail in chronological order; other sessions never enter this path. */ async function durableSessionEvents(sessionFile: string | undefined): Promise> { if (!sessionFile) return []; try { const handle = await open(sessionFile, "r"); try { const stat = await handle.stat(); if (!stat.isFile() || stat.size <= 0) return []; const bytes = Math.min(stat.size, MAX_DURABLE_SESSION_BYTES); const tail = Buffer.alloc(bytes); await handle.read(tail, 0, bytes, stat.size - bytes); const text = tail.toString("utf8"); const lines = text.split("\n"); // A writer can have produced valid JSON before its terminating newline. // It is still an incomplete append and must wait for message_end instead // of being replayed now and then sent a second time live. if (!text.endsWith("\n")) lines.pop(); // A bounded tail can begin mid-record; that first fragment is never an entry. if (stat.size > bytes) lines.shift(); return lines.flatMap((line) => { if (!line.trim()) return []; try { const entry = JSON.parse(line) as { type?: unknown; id?: unknown; message?: unknown }; return entry.type === "message" && entry.message !== undefined ? eventsFromMessage(entry.message, entry.id) : []; } catch { return []; } }); } finally { await handle.close(); } } catch { return []; } } /** * Report this machine's Pi sessions and, with an independent credential, drive * Hosted Pi sessions over a separate Open DeskOS Link v2 control connection. */ export default function (pi: ExtensionAPI): void { const { configs, refusals } = readOdkLinkConfigs(); const reporters = configs.map((config) => new DeskReporter({ config, createTransport: createTcpTransport })); const endpoints = configs.map((config) => `${config.host}:${config.port}`); // This is one machine reporting, however many desks listen: discovery runs // once and every desk receives the same session, status, and event updates, // so one desk being unreachable never stops the others. const each = (run: (reporter: DeskReporter) => void): void => { for (const reporter of reporters) run(reporter); }; const aggregate = (): OdkLinkSnapshot | null => aggregateDeskSnapshots(reporters.map((reporter, index) => ({ ...reporter.snapshot(), endpoint: endpoints[index] ?? "desk" }))); const discovery = reporters.length === 0 ? null : new SessionDiscovery({ discover: () => scanDirectorySessions(), onSnapshot: (sessions) => each((reporter) => reporter.replaceDiscoveredSessions(sessions)), }); const replay = reporters.length === 0 ? null : new CurrentSessionReplay(durableSessionEvents); // A desk's voice agent can drive the session this process is, from inside this process: the // same lifecycle events, a private socket, and no new process or credential. It is created // here and bound only from session_start, so loading the extension binds nothing. // // It is opt-in, because an endpoint that lets something else hand this session work is a // capability rather than a default: a machine that wants one names the directory it belongs // in with ODK_SESSION_HOST_SOCKET, and a machine that does not is never reachable. const sessionHost = new SessionHost({ pi }); const sessionHostEnabled = (process.env.ODK_SESSION_HOST_SOCKET ?? "").length > 0; let currentSessionId = ""; let sessionGeneration = 0; let pendingTurn: { generation: number; sessionId: string; previousMessageId: string | undefined } | null = null; const lastMessage = (ctx: ExtensionContext) => ctx.sessionManager.getBranch().filter(entry => entry.type === "message").at(-1); let hostRefusal = ""; if (reporters.length > 0 || sessionHostEnabled) { function beginSession(ctx: Parameters[1]>[1]): string { if (currentSessionId.length > 0 && currentSessionId !== ctx.sessionManager.getSessionId()) { each((reporter) => reporter.markStatus(currentSessionId, "exited")); } pendingTurn = null; currentSessionId = ctx.sessionManager.getSessionId(); const name = ctx.sessionManager.getSessionName(); const timestamp = ctx.sessionManager.getHeader()?.timestamp; const startedAt = timestamp ? Date.parse(timestamp) : Number.NaN; each((reporter) => reporter.recordSession({ sessionId: currentSessionId, ...(name === undefined ? {} : { name }), cwd: ctx.cwd, workspaceName: workspaceName(ctx.cwd), status: ctx.isIdle() ? "settled" : "running", startedAt: Number.isFinite(startedAt) ? startedAt : Date.now(), })); return currentSessionId; } pi.on("session_start", async (event, ctx) => { const generation = ++sessionGeneration; const sessionId = beginSession(ctx); each((reporter) => reporter.start()); discovery?.start(); // A resumed/reloaded Pi can have a durable JSONL history that is longer // than its in-memory branch. Start the gate for every session; only the // resumed/reloaded/forked cases provide a durable tail. New sessions still // queue their first message_end event behind this empty replay boundary. const replayFile = ["resume", "reload", "fork"].includes((event as { reason?: string }).reason ?? "") ? ctx.sessionManager.getSessionFile?.() : undefined; await replay?.start(sessionId, replayFile, (id, events) => each((reporter) => reporter.recordEvents(id, events))); // A reload replaces the previous endpoint cleanly, so a desk never finds a // half-replaced one, and a machine that cannot spare an endpoint still reports. if (generation !== sessionGeneration) return; if (sessionHostEnabled) { const started = await sessionHost.start(ctx); if (generation !== sessionGeneration) return; const previous = refusals.indexOf(hostRefusal); if (previous >= 0) refusals.splice(previous, 1); hostRefusal = started.reason ? `current-session endpoint: ${started.reason}` : ""; if (hostRefusal) refusals.push(hostRefusal); } }); pi.on("session_info_changed", (event) => { if (currentSessionId.length > 0) each((reporter) => reporter.renameSession(currentSessionId, event.name)); }); pi.on("agent_start", (_event, ctx) => { pendingTurn = { generation: sessionGeneration, sessionId: ctx.sessionManager.getSessionId(), previousMessageId: lastMessage(ctx)?.id }; sessionHost.markRunning(); each((reporter) => reporter.markStatus(currentSessionId, "running")); }); pi.on("agent_settled", (event, ctx) => { const turn = pendingTurn; pendingTurn = null; sessionHost.markSettled(); each((reporter) => reporter.markStatus(currentSessionId, "settled")); if (!turn || event.aborted || turn.generation !== sessionGeneration || turn.sessionId !== currentSessionId || ctx.sessionManager.getSessionId() !== currentSessionId) return; // message_end runs before appendMessage. Only settled exposes this turn's // persisted identity, never the preceding leaf or a text-derived surrogate. const entry = lastMessage(ctx); if (!entry || entry.id === turn.previousMessageId || entry.message.role !== "assistant" || entry.message.stopReason !== "stop") return; const completed = eventsFromMessage(entry.message, entry.id).filter(value => value.kind === "assistant"); replay?.append(currentSessionId, completed, (id, fresh) => each(reporter => reporter.recordEvents(id, fresh))); }); // A turn runs several tools before its message ends, and until that message // the card would keep showing the prompt that started the turn. The tool that // is running now is the newest thing the session is doing, so it is what the // card states. pi.on("tool_execution_start", (event) => { if (currentSessionId.length === 0) return; const activity = toolSummary(event); sessionHost.observeActivity(activity); each((reporter) => reporter.recordActivity(currentSessionId, activity)); }); pi.on("message_end", (event) => { sessionHost.observeMessage(event.message); if (currentSessionId.length === 0) return; const prompt = promptFromMessage(event.message); if (prompt.length > 0) each((reporter) => reporter.recordSession({ sessionId: currentSessionId, latestGoal: prompt })); const events = eventsFromMessage(event.message); replay?.append(currentSessionId, events, (id, fresh) => each((reporter) => reporter.recordEvents(id, fresh))); }); pi.on("session_shutdown", async () => { sessionGeneration += 1; pendingTurn = null; replay?.invalidate(); discovery?.stop(); each((reporter) => reporter.markStatus(currentSessionId, "exited")); currentSessionId = ""; each((reporter) => reporter.disconnect()); await sessionHost.close(); }); } registerConsoleExtension({ pi, // The Console drives one desk: the one that issued a control credential, // unless the operator named the desk they mean. config: selectConsoleDesk(configs, process.env.ODK_LINK_CONSOLE_DESK ?? ""), desks: configs, refusals, createControlTransport: createControlTcpTransport, reporterSnapshot: aggregate, configHint: CONFIG_HINT, }); }