Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
feat(shared): add inline visualization contracts
zszz3ce9746baee47
diff --git a/packages/shared/src/errors.ts b/packages/shared/src/errors.tsindex b316de887..f03883189 100644--- a/packages/shared/src/errors.ts+++ b/packages/shared/src/errors.ts@@ -136,6 +136,11 @@ export const ErrorCodes = {SUBAGENT_OUTPUT_TRUNCATED: "SUBAGENT_OUTPUT_TRUNCATED",WORKSPACE_REQUIRED: "WORKSPACE_REQUIRED",PATH_OUTSIDE_WORKSPACE: "PATH_OUTSIDE_WORKSPACE",+ /**+ * A write into the session visualization directory was not a direct-child+ * `.html` fragment. Only fragments can be rendered as inline visualizations.+ */+ VISUALIZATION_HTML_ONLY: "VISUALIZATION_HTML_ONLY",TOOL_NOT_FOUND: "TOOL_NOT_FOUND",TOOL_DENIED: "TOOL_DENIED",TOOL_TIMEOUT: "TOOL_TIMEOUT",diff --git a/packages/shared/src/gen-ui.test.ts b/packages/shared/src/gen-ui.test.tsnew file mode 100644index 000000000..14a36c7f2--- /dev/null+++ b/packages/shared/src/gen-ui.test.ts@@ -0,0 +1,118 @@+import { describe, expect, it } from "vitest";+import {+ genUiLocationFromPath,+ genUiLocationFromUrl,+ genUiUrl,+ parseGenUiReferenceLine,+ splitGenUiReferences,+} from "./gen-ui.js";++const PATH = "/Users/me/.pi-desktop/visualizations/sess_01-A/chart.html";++describe("gen-ui references", () => {+ it("maps an absolute visualization path to a data-root-independent URL", () => {+ const location = genUiLocationFromPath(PATH);+ expect(location).toEqual({ sessionId: "sess_01-A", fileName: "chart.html" });+ const url = genUiUrl(location!);+ expect(url).toBe("pi-genui://session/sess_01-A/chart.html");+ expect(genUiLocationFromUrl(url)).toEqual(location);+ expect(genUiLocationFromPath("C:\\data\\visualizations\\s1\\a.html")).toEqual({+ sessionId: "s1",+ fileName: "a.html",+ });+ });++ it("rejects paths and URLs outside the contract", () => {+ for (const path of [+ "/tmp/chart.html",+ "/data/visualizations/s1/chart.js",+ "/data/visualizations/s1/sub/chart.html",+ "/data/visualizations/s.1/chart.html",+ "/data/visualizations/s1/.hidden.html",+ ]) {+ expect(genUiLocationFromPath(path)).toBeNull();+ }+ for (const url of [+ "pi-genui://session/s1/../x.html",+ "pi-genui://session/s1/%2E%2E%2Fx.html",+ "pi-genui://other/s1/a.html",+ "pi-genui://session/s1/a.html?x=1",+ "https://session/s1/a.html",+ "pi-genui://session/s1/a/b.html",+ ]) {+ expect(genUiLocationFromUrl(url)).toBeNull();+ }+ });++ it("parses a standalone reference line with an optional title", () => {+ expect(parseGenUiReferenceLine(` ::visualize{"path":"${PATH}","title":" Chart "}`)).toEqual({+ path: PATH,+ title: "Chart",+ location: { sessionId: "sess_01-A", fileName: "chart.html" },+ });+ expect(parseGenUiReferenceLine('::visualize{"path":1}')).toBeNull();+ expect(parseGenUiReferenceLine("::visualize{not json}")).toBeNull();+ expect(parseGenUiReferenceLine(`see ::visualize{"path":"${PATH}"}`)).toBeNull();+ });++ it("splits references out of prose but never out of code blocks", () => {+ const line = `::visualize{"path":"${PATH}"}`;+ const markdown = [+ "Here is the chart.",+ "",+ "```text",+ line,+ "```",+ "",+ ` ${line}`,+ "",+ line,+ "",+ ].join("\n");+ const split = splitGenUiReferences(markdown);+ expect(split.references).toHaveLength(1);+ expect(split.content).toContain("```text\n" + line + "\n```");+ expect(split.content).toContain(` ${line}`);+ expect(split.content.split(line)).toHaveLength(3);+ });++ it("accepts a reference the model wrapped alone in the reply's closing code block", () => {+ const line = `::visualize{"path":"${PATH}","title":"Chart"}`;+ for (const fence of ["```text", "```", "~~~"]) {+ const close = fence.startsWith("~") ? "~~~" : "```";+ const split = splitGenUiReferences(["Done.", "", fence, line, close, ""].join("\n"));+ expect(split.references.map(({ path, title }) => ({ path, title }))).toEqual([+ { path: PATH, title: "Chart" },+ ]);+ expect(split.content).toBe("Done.");+ }+ // A closing block with anything besides the reference stays code.+ const mixed = ["Syntax:", "", "```text", "# example", line, "```"].join("\n");+ expect(splitGenUiReferences(mixed).references).toEqual([]);+ });++ it("follows CommonMark fence boundaries around references", () => {+ const line = `::visualize{"path":"${PATH}"}`;+ // A closing fence is not an opener: an indented reference after it stays code.+ const indentedAfterClose = ["```", "code", "```", ` ${line}`, "```"].join("\n");+ expect(splitGenUiReferences(indentedAfterClose).references).toEqual([]);+ // A fence line with an info string cannot close an open block.+ const infoInside = ["```", "code", "```js", line, "```", "after"].join("\n");+ expect(splitGenUiReferences(infoInside).references).toEqual([]);+ // Backticks are legal in a ~~~ info string.+ const tildeInfo = ["~~~ a`b", line, "~~~", "after"].join("\n");+ expect(splitGenUiReferences(tildeInfo).references).toEqual([]);+ });++ it("accepts Windows paths copied with single backslashes", () => {+ const raw = String.raw`::visualize{"path":"C:\Users\me\AppData\pi\visualizations\s1\chart.html","title":"T"}`;+ const reference = parseGenUiReferenceLine(raw);+ expect(reference?.path).toBe(String.raw`C:\Users\me\AppData\pi\visualizations\s1\chart.html`);+ expect(reference?.location).toEqual({ sessionId: "s1", fileName: "chart.html" });+ });++ it("leaves content untouched when there is no reference", () => {+ const markdown = "plain ::visualize text\n\n\n";+ expect(splitGenUiReferences(markdown)).toEqual({ content: markdown, references: [] });+ });+});diff --git a/packages/shared/src/gen-ui.ts b/packages/shared/src/gen-ui.tsnew file mode 100644index 000000000..1bd8d7cd2--- /dev/null+++ b/packages/shared/src/gen-ui.ts@@ -0,0 +1,241 @@+/**+ * Inline generative UI visualizations (ADR gen-ui-inline-visualization).+ *+ * The model writes an HTML fragment to `<data_dir>/visualizations/<sessionId>/`+ * and references it from its reply with a standalone `::visualize{...}` line.+ * The renderer maps the referenced absolute path to a `pi-genui://` URL that+ * Electron Main resolves against the current data directory, so references+ * survive data-directory relocation.+ */++/** Custom scheme Electron Main serves visualization documents from. */+export const GEN_UI_SCHEME = "pi-genui";++/** Directory under the data root that holds per-session visualization files. */+export const GEN_UI_DIRECTORY_NAME = "visualizations";++/** Built-in skill that teaches the model the visualization contract. */+export const GEN_UI_SKILL_ID = "pi-desktop/visualize";++/** Largest fragment Electron Main will serve. */+export const GEN_UI_MAX_FRAGMENT_BYTES = 2 * 1024 * 1024;++/** Guest → host: the guest document's content height in CSS pixels. */+export const GEN_UI_SIZE_MESSAGE = "pi-genui:size";+/** Host → guest: resolved theme variables to apply on the guest root. */+export const GEN_UI_THEME_MESSAGE = "pi-genui:theme";+/**+ * Guest → host: a wheel event the guest received. The guest is an+ * out-of-process frame, so the host page never sees its input; the host+ * replays this as a non-scrolling gesture signal for the transcript.+ */+export const GEN_UI_WHEEL_MESSAGE = "pi-genui:wheel";+/** Host → guest: which surface shows the guest (`inline` card or `expanded` dialog). */+export const GEN_UI_VIEW_MESSAGE = "pi-genui:view";+/**+ * Guest → host: the shell runtime is running. Sent from the shell `<head>`+ * before the fragment parses, so the host can post theme and view before+ * first paint instead of waiting for `load`.+ */+export const GEN_UI_READY_MESSAGE = "pi-genui:ready";+/**+ * Guest → host: a key the host acts on. The guest is out-of-process, so its+ * keydowns never reach the host window; only `Escape` is forwarded.+ */+export const GEN_UI_KEY_MESSAGE = "pi-genui:key";+/** Guest → host: Main served an error shell instead of the fragment. */+export const GEN_UI_ERROR_MESSAGE = "pi-genui:error";++/** Why Main could not serve a visualization; carried by `GEN_UI_ERROR_MESSAGE`. */+export const GEN_UI_ERROR_CODES = ["not-found", "too-large", "not-fragment", "runtime-unavailable"] as const;+export type GenUiErrorCode = (typeof GEN_UI_ERROR_CODES)[number];++export function isGenUiErrorCode(value: unknown): value is GenUiErrorCode {+ return typeof value === "string" && (GEN_UI_ERROR_CODES as readonly string[]).includes(value);+}++export const GEN_UI_MIN_HEIGHT = 120;+export const GEN_UI_INITIAL_HEIGHT = 240;+export const GEN_UI_MAX_HEIGHT = 2000;++const SESSION_ID_PATTERN = /^[A-Za-z0-9_-]{1,128}$/;+const FILE_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}\.html$/;++/** Session IDs use the same alphabet host-core accepts for scratch roots. */+export function isGenUiSessionId(value: string): boolean {+ return SESSION_ID_PATTERN.test(value);+}++/** A single path component ending in `.html`; no dot-dot or hidden names. */+export function isGenUiFileName(value: string): boolean {+ return FILE_NAME_PATTERN.test(value) && !value.includes("..");+}++export interface GenUiLocation {+ sessionId: string;+ fileName: string;+}++/**+ * Locate the trailing `visualizations/<sessionId>/<file>.html` of an absolute+ * path written by the model. Returns `null` for anything else.+ */+export function genUiLocationFromPath(path: string): GenUiLocation | null {+ const parts = path.replaceAll("\\", "/").split("/").filter(Boolean);+ if (parts.length < 3) return null;+ const [directory, sessionId, fileName] = parts.slice(-3);+ if (directory !== GEN_UI_DIRECTORY_NAME) return null;+ if (!isGenUiSessionId(sessionId) || !isGenUiFileName(fileName)) return null;+ return { sessionId, fileName };+}++/** Fixed URL host; the session ID lives in the path because standard+ * schemes lower-case the host. */+export const GEN_UI_HOST = "session";++/** `pi-genui://session/<sessionId>/<file>.html` */+export function genUiUrl(location: GenUiLocation): string {+ return `${GEN_UI_SCHEME}://${GEN_UI_HOST}/${location.sessionId}/${encodeURIComponent(location.fileName)}`;+}++/** Parse a `pi-genui:` URL back into its location, rejecting anything else. */+export function genUiLocationFromUrl(url: string): GenUiLocation | null {+ let parsed: URL;+ try {+ parsed = new URL(url);+ } catch {+ return null;+ }+ if (parsed.protocol !== `${GEN_UI_SCHEME}:`) return null;+ if (parsed.search || parsed.hash || parsed.username || parsed.password || parsed.port) return null;+ if (parsed.hostname !== GEN_UI_HOST) return null;+ const segments = parsed.pathname.split("/");+ if (segments.length !== 3 || segments[0] !== "") return null;+ const sessionId = segments[1];+ let fileName: string;+ try {+ fileName = decodeURIComponent(segments[2]);+ } catch {+ return null;+ }+ if (!isGenUiSessionId(sessionId) || !isGenUiFileName(fileName)) return null;+ return { sessionId, fileName };+}++export interface GenUiReference {+ /** Absolute path exactly as the model wrote it. */+ path: string;+ title?: string;+ location: GenUiLocation | null;+}++const REFERENCE_PREFIX = "::visualize";++/**+ * Parse the reference JSON. Models copying a Windows path often leave single+ * backslashes (`"C:\Users\..."`), which is invalid JSON; retry once with+ * every lone backslash doubled so the path survives literally.+ */+function parseReferenceJson(json: string): unknown {+ try {+ return JSON.parse(json);+ } catch {+ try {+ return JSON.parse(json.replace(/\\(?!["\\/])/g, "\\\\"));+ } catch {+ return null;+ }+ }+}++/** Parse one standalone `::visualize{...}` line; `null` when it is not one. */+export function parseGenUiReferenceLine(line: string): GenUiReference | null {+ const trimmed = line.trim();+ if (!trimmed.startsWith(`${REFERENCE_PREFIX}{`) || !trimmed.endsWith("}")) return null;+ const value = parseReferenceJson(trimmed.slice(REFERENCE_PREFIX.length));+ if (!value || typeof value !== "object" || Array.isArray(value)) return null;+ const record = value as Record<string, unknown>;+ if (typeof record.path !== "string" || record.path.length === 0) return null;+ const title =+ typeof record.title === "string" && record.title.trim() ? record.title.trim() : undefined;+ return { path: record.path, title, location: genUiLocationFromPath(record.path) };+}++export interface GenUiSplit {+ /** Message content with reference lines removed. */+ content: string;+ references: GenUiReference[];+}++/**+ * Split standalone reference lines out of completed assistant Markdown,+ * ignoring fenced (``` / ~~~) and indented code blocks.+ */+export function splitGenUiReferences(markdown: string): GenUiSplit {+ if (!markdown.includes(REFERENCE_PREFIX)) return { content: markdown, references: [] };+ const lines = markdown.split("\n");+ const kept: string[] = [];+ const references: GenUiReference[] = [];+ let fence: { marker: string; openIndex: number } | null = null;+ let lastBlock: FencedBlock | null = null;+ for (const line of lines) {+ if (fence) {+ const close = FENCE_CLOSE.exec(line);+ kept.push(line);+ if (close && close[1][0] === fence.marker[0] && close[1].length >= fence.marker.length) {+ lastBlock = { open: fence.openIndex, close: kept.length - 1 };+ fence = null;+ }+ continue;+ }+ const open = fenceOpen(line);+ if (open) {+ fence = { marker: open, openIndex: kept.length };+ kept.push(line);+ continue;+ }+ const indented = /^( {4}|\t)/.test(line);+ const reference = indented ? null : parseGenUiReferenceLine(line);+ if (reference) references.push(reference);+ else kept.push(line);+ }+ const closing = fence ? null : closingFencedReference(kept, lastBlock);+ if (closing) {+ kept.length = closing.start;+ references.push(closing.reference);+ }+ if (references.length === 0) return { content: markdown, references };+ return { content: kept.join("\n").replace(/\n{3,}/g, "\n\n").trimEnd(), references };+}++interface FencedBlock {+ open: number;+ close: number;+}++const FENCE_CLOSE = /^ {0,3}(`{3,}|~{3,})\s*$/;++/** CommonMark fence opener; a backtick fence's info string has no backticks. */+function fenceOpen(line: string): string | null {+ const match = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);+ if (!match) return null;+ if (match[1][0] === "`" && match[2].includes("`")) return null;+ return match[1];+}++/**+ * Models sometimes copy the reference into a code block. Code stays code in+ * general, but a reply that ends with a fenced block holding nothing except+ * one reference line meant that reference, so it is accepted there only.+ */+function closingFencedReference(+ lines: string[],+ block: FencedBlock | null,+): { start: number; reference: GenUiReference } | null {+ if (!block || block.close !== block.open + 2) return null;+ for (let index = block.close + 1; index < lines.length; index += 1) {+ if (lines[index].trim() !== "") return null;+ }+ const reference = parseGenUiReferenceLine(lines[block.open + 1]);+ return reference ? { start: block.open, reference } : null;+}diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.tsindex 9ad014dc8..ce210f32c 100644--- a/packages/shared/src/index.ts+++ b/packages/shared/src/index.ts@@ -56,6 +56,7 @@ export * from "./network-proxy.js";export * from "./attachment-limits.js";export * from "./attachment-formats.js";export * from "./speech.js";+export * from "./gen-ui.js";export * from "./image-generation.js";export * from "./font-size.js";export * from "./chat-content-width.js";