diff --git a/.github/design-surfaces.json b/.github/design-surfaces.json index 4e675f46c574..6478d6da10b8 100644 --- a/.github/design-surfaces.json +++ b/.github/design-surfaces.json @@ -142,6 +142,21 @@ "paths": [ "webviews/src/agent-session/acpmux/session-info" ] + }, + "artifacts": { + "spec": "leo-workspace design/artifacts.md", + "aliases": ["artifact", "artifacts", "inline preview", "html preview", "svg preview", "render card"], + "paths": [ + "webviews/src/agent-session/acpmux/conversation/InlinePreview.tsx", + "webviews/src/agent-session/acpmux/conversation/previewDocument.ts", + "webviews/src/agent-session/acpmux/conversation/ArtifactPanel.tsx", + "webviews/src/agent-session/acpmux/conversation/OpenPreview.tsx", + "webviews/src/agent-session/acpmux/conversation/artifactPreview.ts", + "webviews/src/agent-session/acpmux/conversation/artifactSandbox.ts", + "webviews/src/agent-session/acpmux/conversation/artifactVersions.ts", + "webviews/src/agent-session/acpmux/conversation/RenderCard.tsx", + "webviews/src/agent-session/acpmux/conversation/renderCall.ts" + ] } } } diff --git a/webviews/src/agent-session/acpmux/conversation/InlinePreview.gallery.ts b/webviews/src/agent-session/acpmux/conversation/InlinePreview.gallery.ts new file mode 100644 index 000000000000..fffe9fb61ec3 --- /dev/null +++ b/webviews/src/agent-session/acpmux/conversation/InlinePreview.gallery.ts @@ -0,0 +1,122 @@ +// l10n-allow-file: gallery fixtures (sample prompts, replies and documents), not shipped UI. +// A reply's closed ```html or ```svg fence drawn inline as its document (InlinePreview.tsx), in a frame +// sandboxed with allow-scripts and no same-origin, behind a policy with no network. Code shows the +// source card (Copy stays); Open puts it in the side panel. An open fence stays plain code, and a +// document that carries script stays the code card; Open puts it in the side panel, where Run is. +import { agentPaneEntry } from "../../../gallery/format"; +import { assistant, chat, summary, user } from "../../../gallery/fixtures/acpmux"; +import type { PlayContext } from "../../../gallery/play"; + +const fence = (lang: string, code: string, close = true) => "```" + lang + "\n" + code + (close ? "\n```" : "\n"); + +const reply = (prompt: string, text: string, streaming = false) => + chat( + streaming + ? [user(prompt, 6), assistant(text, 0.5, { streaming: true })] + : [user(prompt, 6), assistant(text, 5), summary(5, { status: "completed", durationMs: 24_000 })], + ); + +const bars: [string, number][] = [ + ["Mon", 42], + ["Tue", 58], + ["Wed", 35], + ["Thu", 71], + ["Fri", 64], +]; + +/// A bar chart as one SVG image: bars, values and day labels, in the chat's text colour. +const barChart = ` + Builds per day + +${bars + .map(([day, value], index) => { + const x = 44 + index * 62; + const height = value * 2; + return ` + ${value} + ${day}`; + }) + .join("\n")} +`; + +const rows: [string, number, number][] = [ + ["1st year", 5, 7], + ["2nd year", 5, 9], + ["3rd year", 6, 8], +]; + +/// A study-guide section: a table of hours and the same numbers as a line chart, side by side. +const tableAndChart = ` + +Study hours by year + + +

Study hours by year

+
+ + + +${rows.map(([year, psych, neuro]) => ` `).join("\n")} + +
YearPsychNeuro
${year}${psych}${neuro}
+ + + + +${rows.map(([year], index) => ` ${year}`).join("\n")} + Psych + Neuro + +
+ +`; + +const counter = `Counter +`; + +export default agentPaneEntry({ + id: "agent-pane.inline-preview", + title: "Inline HTML and SVG preview", + area: "Agent pane", + height: 640, + covers: [ + "agent-session/acpmux/conversation/InlinePreview.tsx", + "agent-session/acpmux/conversation/previewDocument.ts", + ], + variants: { + "svg-bar-chart": { + note: "An SVG bar chart in a reply draws as the chart.", + snapshot: reply( + "Chart builds per day this week", + `Builds per day:\n\n${fence("svg", barChart)}\n\nThursday peaked at 71.`, + ), + }, + "html-table-chart": { + note: "An HTML section with a table and the same numbers as a line chart.", + snapshot: reply( + "Make a study-guide section for these hours", + `Here is the section:\n\n${fence("html", tableAndChart)}\n\nNeuro rises in the 2nd year.`, + ), + }, + code: { + note: "Code swaps the preview for the source card, which keeps Copy.", + snapshot: reply("Chart builds per day this week", `Builds per day:\n\n${fence("svg", barChart)}`), + play: async (ctx: PlayContext) => { + await ctx.click({ role: "button", name: "Code" }); + await ctx.waitFor(() => ctx.document.querySelector(".cv-codeblock")); + }, + }, + streaming: { + note: "A fence still arriving stays plain code: nothing renders until it closes.", + snapshot: reply("Chart builds per day this week", `Builds per day:\n\n${fence("svg", barChart, false)}`, true), + }, + scripted: { + note: "A document with script stays the code card; Open takes it to the side panel, where Run is.", + snapshot: reply("Make a counter", fence("html", counter)), + }, + }, +}); diff --git a/webviews/src/agent-session/acpmux/conversation/InlinePreview.tsx b/webviews/src/agent-session/acpmux/conversation/InlinePreview.tsx new file mode 100644 index 000000000000..3cb7271769c3 --- /dev/null +++ b/webviews/src/agent-session/acpmux/conversation/InlinePreview.tsx @@ -0,0 +1,221 @@ +// A closed ```html or ```svg fence in a reply, drawn as the document it holds (leo-workspace +// design/artifacts.md). The preview is an iframe sandboxed with `allow-scripts` and no +// `allow-same-origin`, so the document is an opaque origin that cannot reach the pane. Only markup +// with no script (artifactNeedsRun) previews, made inert first (previewDocument.ts: no refresh, no +// resource hints, no outbound link). In the app it draws in the native artifact frame +// (`cmux-agent://render/artifact`, or the shared page host's), which has its own policy with no +// network, reports its height and whose host refuses its navigation; a `srcdoc` frame would take the +// pane's policy, which runs no inline script, so it could not report a height. The gallery, which has +// no native frame, draws it in `srcdoc` behind the artifact policy with a nonce'd frame script. Any +// other page shows the source. The height follows the frame's report, accepted only from that +// frame's window, capped at 480 px (Expand lifts the cap), and the document scrolls past it. A document that +// carries script stays the code card, whose Open puts it in the side panel, where Run is (#26216). +// Code shows the source card, which keeps Copy. Markdown.tsx draws this only once the fence has +// closed: an open fence stays plain code, so no frame is made or reloaded per token. The frame mounts +// once the card nears the viewport, so a long chat does not start every frame at once. +import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; +import { useT } from "../i18n"; +import { useNearViewport } from "../useNearViewport"; +import { artifactFrameURL, artifactGallery, artifactNeedsRun } from "./artifactSandbox"; +import { CodeBlock, languageLabel } from "./CodeBlock"; +import { CopyButton } from "./CopyButton"; +import { OpenPreview } from "./OpenPreview"; +import { renderFrameCSS } from "./renderBase"; +import { useRenderFrame } from "./RenderCard"; +import { + INLINE_PREVIEW_EXPANDED_MAX_HEIGHT, + INLINE_PREVIEW_MAX_HEIGHT, + INLINE_PREVIEW_MIN_HEIGHT, + INLINE_PREVIEW_SANDBOX, + inlinePreviewDocument, + inlinePreviewHtml, + previewNonce, + reportedHeight, +} from "./previewDocument"; + +export function InlinePreview({ + kind, + code, + lang, + actions, +}: { + kind: "html" | "svg"; + code: string; + lang: string; + actions?: ReactNode; +}) { + const t = useT(); + const [showCode, setShowCode] = useState(false); + const [expanded, setExpanded] = useState(false); + // The frame left its document (its frame counts its loads): the card shows the source. + const [left, setLeft] = useState(false); + const scripted = useMemo(() => artifactNeedsRun(code), [code]); + // Only the native frame or the gallery can draw it; anywhere else (the dev server) shows the source. + const framed = artifactFrameURL() !== undefined || artifactGallery(); + if (scripted || left || !framed) return ; + const toggle = ( + + ); + if (showCode) + return ( + + {toggle} + {actions} + + } + /> + ); + return ( + setExpanded((value) => !value)} + onLeave={() => setLeft(true)} + head={ + <> + {toggle} + + + {actions} + + } + /> + ); +} + +function FrameCard({ + kind, + code, + lang, + expanded, + onExpand, + onLeave, + head, +}: { + kind: "html" | "svg"; + code: string; + lang: string; + expanded: boolean; + onExpand(): void; + onLeave(): void; + head: ReactNode; +}) { + const t = useT(); + const [card, setCard] = useState(null); + const near = useNearViewport(card); + const [height, setHeight] = useState(INLINE_PREVIEW_MIN_HEIGHT); + const max = expanded ? INLINE_PREVIEW_EXPANDED_MAX_HEIGHT : INLINE_PREVIEW_MAX_HEIGHT; + return ( +
+
+ {languageLabel(lang, t)} + {height > INLINE_PREVIEW_MAX_HEIGHT && ( + + )} + {head} +
+ {near && + (artifactFrameURL() ? ( + + ) : ( + + ))} +
+ ); +} + +/// The inert document in the native artifact frame, which writes it into itself under its own policy +/// and reports its height (useRenderFrame, as the side panel's Run does). +function NativeFrame({ + kind, + code, + max, + onHeight, +}: { + kind: "html" | "svg"; + code: string; + max: number; + onHeight(height: number): void; +}) { + const t = useT(); + const html = useMemo(() => inlinePreviewHtml(kind, code), [kind, code]); + const { frame, height } = useRenderFrame(html); + useEffect(() => onHeight(height), [onHeight, height]); + return ( +