visuals
HTML pages (html_preview, html_render) and native inline charts, positioned diagrams and sortable tables in agent replies.
The app opens the listing; nothing installs until an agent in your Plugins workspace has read the files and you enable the plugin. In a terminal: dvg install divergence/visuals@0.2.1
Visuals — official plugin
divergence/visuals provides publish_visual for interactive, native visual
replies. It ships enabled on a fresh install; an existing install keeps the
choice it has, and users can turn it off or on in Settings → Plugins. It starts
only when a reply first contains one of its fences and loads Pele only on the
first Mermaid fence, so a chat without fences pays nothing. Its only requested
permission is tools.provide.
HTML pages (T3 Code's html_preview and html_render)
html.ts holds T3's contract (pingdotgg/t3code b24f0fbb): the tool names,
descriptions, limits (HTML 1–512000 UTF-16 units, title 1–200, preview width
240–1600 with 728 by default, frame height 80–2000), the result shapes and
messages, the frame-height rule, and the theme bootstrap with Divergence's
colours. Both tools are direct, so in code mode they stay tools of their
own and the preview's PNG reaches the agent as an MCP image part.
html_previewsends the themed page tohost/web.preview. With a web view it answers T3's metadata as text and structured content plus the PNG. Without one it is an error,HtmlBackendUnavailable: <raw reason>, with no image, and no screenshot is made up.html_rendersaves the agent's page unchanged as ahtmlv1 transcript artifact (the host stamps the owner) and answers T3'shtmlRenderreference and message. Measuredheightsare not supplied.- Inline (
html-view.ts) the page is live (ui.webDocument): borderless in the reply, as wide as the column and as tall as the page up to the agent's height, with one small glass Open in panel control. The panel is a detail-pane tab (thedivergence/tabsservice): the page live again, its Source, Copy and Save (kernel/copy_text,kernel/save_text, both after a click).
The renderer is the system's WKWebView on macOS (crates/host/src/web), so
a page behaves as it would in Safari, with the Mac's fonts, the GPU, text
input, the clipboard and native pickers. The difference from T3 is that
pages are offline: remote URLs and local image paths never load
(missingImages lists the paths a page names). On Linux and Windows, which
have no web view in this version, the page says so and its source, copy and
save still work.
Version 2 supports responsive multi-panel compositions: bar (grouped/stacked/
percent; horizontal/vertical), line, area, scatter/bubble, pie/donut, heatmap,
histogram, range/timeline, waterfall and boxplot, plus metrics, text, typed tables
and positioned diagrams. document.ts defines the strict, bounded JSON schema;
rules.md is the agent-facing reference. Version 1 documents still replay.
Rendering and interaction
Rust/GPUI draws every shape, label, control and floating tooltip. A pinned,
DOM-free D3 math subset computes scales, numeric/UTC ticks and paths inside
QuickJS; there is no browser, JavaScript UI execution, graph auto-layout, network,
file, process or plugin-storage access. math.LICENSES.txt includes provenance
and licenses; scripts/bundle-visuals-math.mjs reproduces the readable bundle.
Charts use the native canvas's onResize({width,height}) logical-pixel measurement,
not a scaled viewBox. Text remains readable as panels grow or wrap. Compact
legends keep original domains, colors and percentage denominators when hidden.
Null breaks paths and stays explicitly missing in shared bar inspection.
Hover opens a clamped floating tooltip; click pins/unpins. Arrow keys, Home/End traverse observations, Enter/Space pin, Escape clears inspection. In the expanded overlay Escape unpins first, then closes. Tables retain stable raw row identity, sort nulls last in either direction, paginate and expose full selected values. Diagram node details remain accessible through a native picker.
State and cached geometry belong to each artifact's ctx.state, with separate
panels and inline/expanded inspection. Hover does not recompute paths. Native
resize reports are change-only and coalesced off-paint. No JavaScript frame loop
or render-time notification runs.
Persistence and compatibility
The host owns immutable transcript artifacts. ctx.artifacts.publish persists
the complete document independently of the surrounding tool result, including
discarded acknowledgements and later code-mode errors. The plugin registers
removable visual renderers for versions 1 and 2; Source retains original input
and Data retains all raw values, including hidden series and nulls.
This plugin requires the updated native host and canvas resize API. Older hosts without artifact support fail with an explicit update message. Invalid persisted documents keep their readable summary and expose a validation error.
Verification
node --import ./plugins/testing/register.mjs --test plugins/visuals/*.test.ts
pnpm -C plugins typecheck
pnpm -C plugins exec prettier --check 'visuals/**/*.{ts,json,md}'
Geometry tests use asymmetric data and independent numeric expectations; view tests exercise real native element factories and handlers. Native desktop checks and app-owned Metal captures supplement these tests; they do not use a browser.
Permissions in 0.2.1
Files
// Native artifact views: an always-visible visual, plus one keyboard-owned inspection overlay.
// Each artifact's ViewContext owns its state; neither artifacts nor panes share module globals.
import { absurd } from "effect/Function";
import { ui } from "divergence";
import type { Element, ViewContext } from "divergence";
import { newChartState, renderChart } from "./chart-view.ts";
import type { ChartState } from "./chart-view.ts";
import { diagramCommands, nodeAt } from "./diagram.ts";
import { richChartSchema, visualDocumentSchema } from "./document.ts";
import type { Diagram, Table, VisualDocument, VisualPanel } from "./document.ts";
import { formatValue } from "./plot.ts";
import { cellText, dataTable, pageRows } from "./table.ts";
interface TableState {
sort: { column: string; descending: boolean } | null;
page: number;
selected: number | null;
}
interface PanelState {
chart: ChartState;
node: string | null;
table: TableState;
data: TableState;
}
interface State {
document: VisualDocument;
panels: VisualPanel[];
expanded: boolean;
tab: "visual" | "data" | "source";
panel: string;
states: Record<string, PanelState>;
}
export interface VisualArtifact {
readonly owner: string;
readonly type: string;
readonly version: number;
readonly title: string;
readonly summary: string;
readonly data: unknown;
}
function stateFor(ctx: ViewContext, document: VisualDocument): State {
// View state is in-memory and private to this renderer, not persisted input.
const panels: VisualPanel[] =
document.version === 2
? document.panels
: [
{
id: "legacy",
span: 2,
body:
document.body.kind === "chart"
? richChartSchema.parse({
kind: "chart",
chart: document.body.chart,
x: { type: "category", values: document.body.categories },
series: document.body.series,
yLabel: document.body.yLabel,
})
: document.body,
},
];
const table = (): TableState => ({ sort: null, page: 0, selected: null });
const state: State = {
document,
panels,
expanded: false,
tab: "visual",
panel: panels[0]!.id,
states: Object.fromEntries(
panels.map((panel) => [panel.id, { chart: newChartState(), node: null, table: table(), data: table() }]),
),
};
ctx.state.visuals = state;
return state;
}
function change(ctx: ViewContext, state: State, action: () => void): void {
action();
ctx.state.visuals = state;
ctx.update();
}
function renderDiagram(
diagram: Diagram,
state: State,
panel: PanelState,
ctx: ViewContext,
id: string,
expanded: boolean,
): Element {
const selected = diagram.nodes.find((node) => node.id === panel.node);
return ui.column({ padding: 0, gap: "small", minWidth: 0 }, [
ui.canvas({
id: `${id}-diagram`,
width: "100%",
minWidth: 0,
height: expanded ? 360 : 240,
viewBox: { width: diagram.width, height: diagram.height },
commands: diagramCommands(diagram, panel.node),
cursor: "pointer",
onClick: ({ x, y }) =>
change(ctx, state, () => {
panel.node = nodeAt(diagram, x, y);
}),
}),
ui.select({
id: `${id}-node`,
placeholder: "Inspect a node…",
value: panel.node ?? "",
options: [
{ value: "", label: "Inspect a node…" },
...diagram.nodes.map((node) => ({ value: node.id, label: node.label })),
],
onChange: ({ value }) =>
change(ctx, state, () => {
panel.node = value || null;
}),
}),
selected
? ui.column({ padding: 0, gap: "small" }, [
ui.text(selected.label, { weight: 500 }),
ui.text(selected.detail ?? "No additional details."),
...diagram.edges
.filter((edge) => edge.from === selected.id || edge.to === selected.id)
.map((edge, index) =>
ui.text(`${edge.from} → ${edge.to}${edge.label ? `: ${edge.label}` : ""}`, { key: index, tone: "muted" }),
),
])
: ui.muted("Select a node in the diagram or use the node picker to read its details."),
]);
}
function renderTable(table: Table, tableState: TableState, state: State, ctx: ViewContext, id: string): Element {
const { rows, current, pages } = pageRows(table, tableState.sort, tableState.page);
const selectRow = (index: number) =>
change(ctx, state, () => {
tableState.selected = index;
});
const selected = tableState.selected === null ? undefined : table.rows[tableState.selected];
return ui.column({ padding: 0, gap: "small", minWidth: 0 }, [
ui.div({ width: "100%", minWidth: 0, overflowX: "scroll" }, [
ui.column({ padding: 0, gap: 0, minWidth: Math.max(320, table.columns.length * 140) }, [
ui.row(
{ padding: 0, gap: 0 },
table.columns.map((column) =>
ui.div({ flexGrow: 1, flexBasis: 0, minWidth: 140, padding: "small", key: column.key }, [
ui.button({
id: `${id}-sort-${column.key}`,
label: `${column.label}${tableState.sort?.column === column.key ? (tableState.sort.descending ? " ↓" : " ↑") : ""}`,
variant: "ghost",
small: true,
onClick: () =>
change(ctx, state, () => {
tableState.sort = {
column: column.key,
descending: tableState.sort?.column === column.key && !tableState.sort.descending,
};
tableState.page = 0;
}),
}),
]),
),
),
...rows.map(({ index, cells }) =>
ui.div(
{
id: `${id}-row-${index}`,
key: index,
padding: 0,
gap: 0,
focusable: true,
display: "flex",
flexDirection: "row",
background: tableState.selected === index ? "accent" : undefined,
hover: { background: tableState.selected === index ? "accent" : "hover" },
onClick: () => selectRow(index),
onKeyDown: ({ key }) => {
if (key === "enter" || key === "space") selectRow(index);
},
},
cells.map((cell, column) =>
ui.div({ key: column, flexGrow: 1, flexBasis: 0, minWidth: 140, padding: "small" }, [
ui.text(cellText(cell), { truncate: true, color: "foreground" }),
]),
),
),
),
]),
]),
rows.length === 0 ? ui.muted("No rows.") : null,
ui.row({ padding: 0, wrap: true, gap: "small" }, [
ui.button({
id: `${id}-previous`,
label: "Previous",
small: true,
disabled: current === 0,
onClick: () =>
change(ctx, state, () => {
tableState.page = current - 1;
}),
}),
ui.muted(`Page ${current + 1} of ${pages} · ${table.rows.length} rows`),
ui.button({
id: `${id}-next`,
label: "Next",
small: true,
disabled: current >= pages - 1,
onClick: () =>
change(ctx, state, () => {
tableState.page = current + 1;
}),
}),
]),
selected
? ui.column(
{ padding: 0, gap: "small" },
table.columns.map((column, index) =>
ui.text(`${column.label}: ${cellText(selected[index] ?? null)}`, { key: column.key }),
),
)
: ui.muted("Select a row to read complete cell values."),
]);
}
// The calm inline report: panels are not boxed; spacing and a quiet label separate them, and
// metrics sit side by side as one row of figures unless the author spans one across the row.
function panelBasis(panel: VisualPanel, columns: number): number | "100%" {
if (panel.span === 2) return "100%";
if (panel.body.kind === "metric") return 150;
return columns === 2 ? 300 : "100%";
}
// A metric's label already names it, so a title that only repeats the label is left out.
function panelTitle(panel: VisualPanel): string | undefined {
if (!panel.title) return undefined;
const same = (a: string, b: string) => a.trim().toLowerCase() === b.trim().toLowerCase();
return panel.body.kind === "metric" && same(panel.title, panel.body.label) ? undefined : panel.title;
}
function renderVisual(document: VisualDocument, state: State, ctx: ViewContext, expanded: boolean): Element {
const columns = document.version === 2 ? document.columns : 1;
return ui.row(
{ padding: 0, gap: 20, wrap: true, align: "start", minWidth: 0 },
state.panels.map((panel) => {
const title = panelTitle(panel);
return ui.div(
{
id: `${expanded ? "expanded" : "inline"}-panel-${panel.id}`,
key: panel.id,
flexGrow: 1,
flexShrink: 1,
flexBasis: panelBasis(panel, columns),
minWidth: 0,
},
[
ui.column({ padding: 0, gap: 8, minWidth: 0 }, [
title ? ui.text(title, { tone: "muted", weight: 500 }) : null,
renderPanel(panel, state, ctx, expanded),
panel.caption ? ui.text(panel.caption, { tone: "muted", size: "sm" }) : null,
]),
],
);
}),
);
}
function renderPanel(panel: VisualPanel, state: State, ctx: ViewContext, expanded: boolean): Element {
const body = panel.body;
const local = state.states[panel.id]!;
const id = `${expanded ? "expanded" : "inline"}${panel.id === "legacy" ? "" : `-${panel.id}`}`;
switch (body.kind) {
case "chart":
return renderChart(body, local.chart, ctx, `${id}-chart`, expanded);
case "diagram":
return renderDiagram(body, state, local, ctx, id, expanded);
case "table":
return renderTable(body, local.table, state, ctx, `${id}-table`);
case "text":
return ui.text(body.text);
case "metric":
// The figure first, its label under it, as in an editorial summary.
return ui.column({ padding: 0, gap: 2 }, [
ui.text(formatValue(body.value, body.format), { size: 28, weight: 500 }),
ui.text(body.label, { tone: "muted" }),
body.comparison
? ui.row({ padding: 0, gap: 6 }, [
ui.text(formatValue(body.comparison.value, body.comparison.format), {
tone:
body.comparison.tone === "positive"
? "success"
: body.comparison.tone === "negative"
? "danger"
: "muted",
weight: 500,
}),
ui.text(body.comparison.label, { tone: "muted" }),
])
: null,
]);
default:
return absurd(body);
}
}
export function renderArtifact(artifact: VisualArtifact, ctx: ViewContext): Element {
// Published artifacts are immutable; validate once per view/runtime lifetime.
const stored = ctx.state.visuals as State | undefined;
const parsed = stored
? { success: true as const, data: stored.document }
: visualDocumentSchema.safeParse(artifact.data);
if (
artifact.owner !== "visuals" ||
artifact.type !== "visual" ||
!parsed.success ||
artifact.version !== parsed.data.version
)
return ui.column({ padding: "small", gap: "small" }, [
ui.text(artifact.title, { weight: 500 }),
ui.text(artifact.summary),
ui.text(
`This visual cannot be rendered: ${parsed.success ? "unsupported owner, type or version" : parsed.error.issues.map((issue) => `${issue.path.join(".")}: ${issue.message}`).join("; ")}`,
{ tone: "danger" },
),
]);
const document = parsed.data;
const state = stored ?? stateFor(ctx, document);
const close = () =>
change(ctx, state, () => {
state.expanded = false;
Object.values(state.states).forEach((panel) => {
panel.chart.expanded.hover = panel.chart.expanded.pinned = null;
});
});
const escape = () => {
const pinned = Object.values(state.states).filter((panel) => panel.chart.expanded.pinned);
if (state.tab === "visual" && pinned.length)
change(ctx, state, () => {
pinned.forEach((panel) => {
panel.chart.expanded.pinned = panel.chart.expanded.hover = null;
});
});
else close();
};
const activePanel = state.panels.find((panel) => panel.id === state.panel)!;
const overlayContent = () =>
state.tab === "source"
? ui.code(JSON.stringify(artifact.data, null, 2), "json")
: state.tab === "data"
? ui.column({ padding: 0, gap: "small" }, [
state.panels.length > 1
? ui.select({
id: "data-panel",
value: state.panel,
options: state.panels.map((panel) => ({ value: panel.id, label: panel.title ?? panel.id })),
onChange: ({ value }) =>
change(ctx, state, () => {
state.panel = value;
}),
})
: null,
renderTable(
dataTable(document.version === 1 ? document : activePanel),
state.states[state.panel]!.data,
state,
ctx,
"data-table",
),
activePanel.body.kind === "diagram"
? ui.column({ padding: 0, gap: 0 }, [
ui.text("Connections", { weight: 500 }),
...activePanel.body.edges.map((edge, index) =>
ui.text(`${edge.from} → ${edge.to}${edge.label ? `: ${edge.label}` : ""}`, { key: index }),
),
])
: null,
])
: renderVisual(document, state, ctx, true);
return ui.column({ padding: "small", gap: 12, minWidth: 0 }, [
ui.row({ padding: 0, wrap: true, gap: "small", align: "center" }, [
ui.text(document.title, { weight: 500, size: 15 }),
ui.spacer(),
ui.button({
id: "expand-visual",
label: "Expand",
icon: "maximize-2",
variant: "ghost",
small: true,
onClick: () =>
change(ctx, state, () => {
state.expanded = true;
}),
}),
]),
// The takeaway reads as the lede, not a caption.
ui.text(document.summary),
renderVisual(document, state, ctx, false),
state.expanded
? ui.overlay({ id: "visual-overlay", open: true, width: 900, top: 0.06, keys: { escape }, onDismiss: close }, [
ui.column({ padding: "medium", gap: "small", minWidth: 0 }, [
ui.row({ padding: 0, wrap: true, gap: "small" }, [
ui.text(document.title, { weight: 500 }),
ui.spacer(),
...(["visual", "data", "source"] as const).map((tab) =>
ui.button({
id: `visual-tab-${tab}`,
label: `${tab[0]?.toUpperCase()}${tab.slice(1)}`,
active: state.tab === tab,
small: true,
onClick: () =>
change(ctx, state, () => {
state.tab = tab;
}),
}),
),
ui.button({ id: "close-visual", label: "Close", variant: "ghost", small: true, onClick: close }),
]),
ui.text(document.summary),
ui.div({ minWidth: 0, maxHeight: 520, overflowY: "scroll" }, [overlayContent()]),
]),
])
: null,
]);
}
No comments yet.