The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
feat(desktop): inline previews read as native widgets — content-sized, theme-bridged, interactive
Brooklyn Nicholsona1fea534548e
diff --git a/agent/prompt_builder.py b/agent/prompt_builder.pyindex 2bbab9ce8cc..7e2c5abe27f 100644--- a/agent/prompt_builder.py+++ b/agent/prompt_builder.py@@ -929,8 +929,17 @@ PLATFORM_HINTS = {"render inline as photos. ""To show an HTML file you wrote as a LIVE inline page right in your ""message, put ::preview{file=\"path/to/file.html\"} alone on its own "- "line (optional height=\"480\" in pixels) — desktop plugins can "- "register more ::name{...} directives like it. "+ "line — desktop plugins can register more ::name{...} directives like "+ "it. When the user asks for an inline widget, chart, or visualization "+ "(anything living IN the chat rather than a standalone page), design "+ "it as a native piece of the app by default: transparent background, "+ "colors from the provided theme tokens — var(--foreground), "+ "var(--muted-foreground), var(--accent), var(--border), var(--card) — "+ "the inherited app font, no body padding or margin, content flush "+ "left and filling the viewport width, no centering wrappers, decorative "+ "backdrops, or page chrome. The frame auto-sizes to the content. Only "+ "a standalone PAGE (a mockup, a poster, a game) should bring its own "+ "background and layout. ""When the user asks to add, enable, or authorize an MCP server (or a ""task clearly needs one that is missing), use the setup_mcp tool if ""it is available — it shows an inline consent card right in the chat; "diff --git a/apps/desktop/src/components/assistant-ui/inline-preview-directive.test.ts b/apps/desktop/src/components/assistant-ui/inline-preview-directive.test.tsindex 10f20511348..be5b55566b2 100644--- a/apps/desktop/src/components/assistant-ui/inline-preview-directive.test.ts+++ b/apps/desktop/src/components/assistant-ui/inline-preview-directive.test.ts@@ -1,6 +1,6 @@import { describe, expect, it } from 'vitest'-import { directiveFrameHeight, frameHeightFromMessage, withMeasurement } from './inline-preview-directive'+import { directiveFrameHeight, frameSizeFromMessage, themePrelude, withInlineChrome } from './inline-preview-directive'describe('directiveFrameHeight', () => {it('returns null (auto-size) when absent or garbage', () => {@@ -17,46 +17,79 @@ describe('directiveFrameHeight', () => {})})-describe('withMeasurement', () => {+describe('withInlineChrome', () => {+ const prelude = themePrelude({ '--foreground': '#eee' }, 'Inter')++ it('puts the theme prelude FIRST so page styles override it', () => {+ const doc = '<html><head><style>body{color:red}</style></head><body><h1>hi</h1></body></html>'+ const framed = withInlineChrome(doc, 'tok', prelude)++ expect(framed.startsWith(prelude)).toBe(true)+ expect(framed.indexOf(prelude)).toBeLessThan(framed.indexOf('color:red'))+ })+it('injects the measuring script before </body>', () => {const doc = '<html><body><h1>hi</h1></body></html>'- const framed = withMeasurement(doc, 'tok')+ const framed = withInlineChrome(doc, 'tok', prelude)- expect(framed.indexOf('<script>')).toBeGreaterThan(framed.indexOf('<h1>'))- expect(framed.indexOf('</script>')).toBeLessThan(framed.indexOf('</body>'))+ expect(framed.indexOf('postMessage')).toBeGreaterThan(framed.indexOf('<h1>'))+ expect(framed.indexOf('postMessage')).toBeLessThan(framed.indexOf('</body>'))expect(framed).toContain('"tok"')})- it('appends when there is no body close tag', () => {- const framed = withMeasurement('<h1>fragment</h1>', 'tok')+ it('appends the script when there is no body close tag', () => {+ const framed = withInlineChrome('<h1>fragment</h1>', 'tok', prelude)- expect(framed.startsWith('<h1>fragment</h1>')).toBe(true)+ expect(framed).toContain('<h1>fragment</h1>')expect(framed).toContain('postMessage')})})-describe('frameHeightFromMessage', () => {+describe('themePrelude', () => {+ it('carries resolved tokens, transparent background, and the app font', () => {+ const prelude = themePrelude({ '--foreground': 'oklch(0.9 0 0)', '--accent': '#7aa2f7' }, 'Inter, sans-serif')++ expect(prelude).toContain('--foreground:oklch(0.9 0 0)')+ expect(prelude).toContain('--accent:#7aa2f7')+ expect(prelude).toContain('background:transparent')+ expect(prelude).toContain('font-family:Inter, sans-serif')+ })++ it('omits the font rule when no font resolved', () => {+ expect(themePrelude({}, '')).not.toContain('font-family')+ })+})++describe('frameSizeFromMessage', () => {const msg = (over: Record<string, unknown> = {}) => ({type: 'hermes-inline-preview-size',token: 'tok',height: 500,+ width: 300,...over})- it('accepts our message with our token, clamped', () => {- expect(frameHeightFromMessage(msg(), 'tok')).toBe(500)- expect(frameHeightFromMessage(msg({ height: 12 }), 'tok')).toBe(120)- expect(frameHeightFromMessage(msg({ height: 5000 }), 'tok')).toBe(1200)- expect(frameHeightFromMessage(msg({ height: 500.7 }), 'tok')).toBe(501)+ it('accepts our message with our token, height clamped', () => {+ expect(frameSizeFromMessage(msg(), 'tok')).toEqual({ height: 500, width: 300 })+ expect(frameSizeFromMessage(msg({ height: 12 }), 'tok')?.height).toBe(120)+ expect(frameSizeFromMessage(msg({ height: 5000 }), 'tok')?.height).toBe(1200)+ expect(frameSizeFromMessage(msg({ height: 500.7 }), 'tok')?.height).toBe(501)+ })++ it('sanitizes width to 0 when missing or hostile', () => {+ expect(frameSizeFromMessage(msg({ width: undefined }), 'tok')?.width).toBe(0)+ expect(frameSizeFromMessage(msg({ width: 'wide' }), 'tok')?.width).toBe(0)+ expect(frameSizeFromMessage(msg({ width: Infinity }), 'tok')?.width).toBe(0)+ expect(frameSizeFromMessage(msg({ width: -10 }), 'tok')?.width).toBe(0)})it('rejects wrong type, wrong token, and hostile shapes', () => {- expect(frameHeightFromMessage(msg({ type: 'other' }), 'tok')).toBeNull()- expect(frameHeightFromMessage(msg({ token: 'stolen' }), 'tok')).toBeNull()- expect(frameHeightFromMessage(msg({ height: 'tall' }), 'tok')).toBeNull()- expect(frameHeightFromMessage(msg({ height: Infinity }), 'tok')).toBeNull()- expect(frameHeightFromMessage(msg({ height: -5 }), 'tok')).toBeNull()- expect(frameHeightFromMessage(null, 'tok')).toBeNull()- expect(frameHeightFromMessage('str', 'tok')).toBeNull()+ expect(frameSizeFromMessage(msg({ type: 'other' }), 'tok')).toBeNull()+ expect(frameSizeFromMessage(msg({ token: 'stolen' }), 'tok')).toBeNull()+ expect(frameSizeFromMessage(msg({ height: 'tall' }), 'tok')).toBeNull()+ expect(frameSizeFromMessage(msg({ height: Infinity }), 'tok')).toBeNull()+ expect(frameSizeFromMessage(msg({ height: -5 }), 'tok')).toBeNull()+ expect(frameSizeFromMessage(null, 'tok')).toBeNull()+ expect(frameSizeFromMessage('str', 'tok')).toBeNull()})})diff --git a/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx b/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsxindex 00f3fc09acc..b086c3f2fcb 100644--- a/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx+++ b/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx@@ -3,29 +3,34 @@ import { useEffect, useMemo, useRef, useState } from 'react'import { useSessionView } from '@/app/chat/session-view'import { PreviewAttachment } from '@/components/chat/preview-attachment'-import { ScrollGate } from '@/components/assistant-ui/embeds/scroll-gate'import { useIsDark } from '@/components/assistant-ui/embeds/use-is-dark'-import { useI18n } from '@/i18n'import { isRemoteGateway } from '@/lib/media'import { localPreviewTarget } from '@/lib/local-preview'/*** `::preview{file="…"}` — a workspace HTML file rendered LIVE inside the- * assistant message, bb-inline-vis style. A sandboxed iframe with an opaque- * origin (`sandbox="allow-scripts"`, deliberately no `allow-same-origin`):- * scripts run, but the document cannot reach the app, its cookies, storage,- * or the bridge. The doc arrives via `srcdoc` from a bridge file read, so- * single-file HTML (what agents generate) is fully live; relative sibling- * assets don't resolve in an opaque origin — the header's rail affordance- * covers multi-file apps with the full webview preview.+ * assistant message. A sandboxed iframe with an opaque origin+ * (`sandbox="allow-scripts"`, deliberately no `allow-same-origin`): scripts+ * run and the widget is fully interactive, but the document cannot reach the+ * app, its cookies, storage, or the bridge. The doc arrives via `srcdoc`+ * from a bridge file read, so single-file HTML (what agents generate) is+ * fully live; relative sibling assets don't resolve in an opaque origin.*- * HEIGHT IS CONTENT-DRIVEN. The opaque origin means the parent can't measure- * the document, but we own the srcdoc string — so a measuring script is- * injected that posts the document's scrollHeight up via postMessage (tagged- * with a per-mount token) on load and on every resize, and the frame tracks- * it within the clamp band. Full-viewport pages (100vh) measure exactly the- * height they're given, so they settle at the default instead of looping. An- * explicit `height="480"` attribute opts out of auto-sizing entirely.+ * SIZE IS CONTENT-DRIVEN. The opaque origin means the parent can't measure+ * the document, but we own the srcdoc string — an injected script posts the+ * content's size up via postMessage (tagged with a per-mount token). Height+ * tracks live within the clamp band; width adopts ONCE from the first+ * report, so a fixed-size widget shrink-wraps and sits left in the message+ * flow like an image, while a fluid page measures the full viewport and+ * stays column-wide. A `height="480"` attribute only sets the starting+ * height — measurement always wins.+ *+ * NATIVE BY DEFAULT. A theme prelude injects first: the app's resolved+ * theme tokens under friendly names (--foreground, --muted-foreground,+ * --accent, --border, --card), the app font, zero body margin/padding, and+ * a transparent background — so widget-shaped content reads as part of the+ * app. The page's own styles override all of it, so a full page keeps its+ * own design.** Non-HTML targets and remote gateways (no local file access) fall back to* the standard preview-attachment card rather than a broken frame.@@ -34,6 +39,8 @@ import { localPreviewTarget } from '@/lib/local-preview'const MIN_HEIGHT = 120const MAX_HEIGHT = 1200const DEFAULT_HEIGHT = 280+/** The transcript column cap the frame renders inside (`max-w-160` = 40rem). */+const MAX_COLUMN_WIDTH = 640/** Ignore sub-pixel/rounding churn so a vh-sized page can't oscillate. */const RESIZE_TOLERANCE = 4@@ -53,47 +60,112 @@ export function directiveFrameHeight(raw: string | undefined): number | null {const SIZE_MESSAGE_TYPE = 'hermes-inline-preview-size'-/** The script injected into the srcdoc that reports content height to the+/** Semantic tokens handed into the frame, resolved to concrete values from+ * the LIVE theme. Friendly names, not internal ones — this is the contract+ * reference HTML / skills write against (`var(--foreground)` etc.). */+const THEME_BRIDGE_TOKENS: Record<string, string> = {+ '--foreground': '--ui-text-primary',+ '--muted-foreground': '--ui-text-tertiary',+ '--accent': '--ui-accent',+ '--border': '--ui-stroke-tertiary',+ '--card': '--ui-bg-editor'+}++/** Resolve the bridge tokens + app font against the current document. */+export function collectThemeBridge(): { vars: Record<string, string>; font: string } {+ const vars: Record<string, string> = {}++ if (typeof document !== 'undefined') {+ const root = getComputedStyle(document.documentElement)++ for (const [alias, source] of Object.entries(THEME_BRIDGE_TOKENS)) {+ const value = root.getPropertyValue(source).trim()++ if (value) {+ vars[alias] = value+ }+ }+ }++ const font = typeof document === 'undefined' ? '' : getComputedStyle(document.body).fontFamily++ return { vars, font }+}++/**+ * The style prelude that makes an inline widget read as NATIVE: the app's+ * resolved theme tokens as CSS vars, the app font, no margin, and a+ * transparent background so the widget sits directly on the chat surface.+ * Injected FIRST, so the page's own styles override every default here — a+ * full page that wants its own look keeps it.+ */+export function themePrelude(vars: Record<string, string>, font: string): string {+ const tokens = Object.entries(vars)+ .map(([name, value]) => `${name}:${value}`)+ .join(';')++ const fontRule = font ? `font-family:${font};` : ''++ return (+ `<style>:root{${tokens}}` ++ `html,body{margin:0;padding:0;background:transparent;color:var(--foreground,inherit);${fontRule}}</style>`+ )+}++/** The script injected into the srcdoc that reports content size to the* parent. Runs inside the opaque origin, so postMessage is its only door —- * it can say "I am N pixels tall" and nothing else. */+ * it can say "I am N pixels" and nothing else. Height is the document+ * scrollHeight; width is the union of the body children's boxes (intrinsic+ * content width — the document itself always fills the viewport, so+ * scrollWidth would just echo the frame back). */export function measurementScript(token: string): string {return ('<script>(function(){var t=' +JSON.stringify(token) +- ';var last=0;function post(){var d=document.documentElement;var b=document.body;' ++ ';var lastH=0,lastW=0;function post(){var d=document.documentElement;var b=document.body;' +'var h=Math.max(d?d.scrollHeight:0,b?b.scrollHeight:0);' +- 'if(Math.abs(h-last)>1){last=h;parent.postMessage({type:' ++ 'var w=0;if(b){var kids=b.children;var L=Infinity,R=0;for(var i=0;i<kids.length;i++){' ++ 'var r=kids[i].getBoundingClientRect();if(r.width===0&&r.height===0)continue;' ++ 'if(r.left<L)L=r.left;if(r.right>R)R=r.right}' ++ 'if(R>L)w=R-L}' ++ 'w=Math.ceil(w);' ++ 'if(Math.abs(h-lastH)>1||Math.abs(w-lastW)>1){lastH=h;lastW=w;parent.postMessage({type:' +JSON.stringify(SIZE_MESSAGE_TYPE) +- ',token:t,height:h},"*")}}' ++ ',token:t,height:h,width:w},"*")}}' +'if(typeof ResizeObserver==="function"){var ro=new ResizeObserver(post);' +'ro.observe(document.documentElement);if(document.body)ro.observe(document.body)}' +'addEventListener("load",post);post()})()</script>')}-/** Inject the measuring script into a document string — before `</body>`- * when present so it runs after the page's own markup, appended otherwise. */-export function withMeasurement(doc: string, token: string): string {+/** Assemble the srcdoc: theme prelude first (so the page's own styles win),+ * measuring script before `</body>` when present so it runs after the+ * page's own markup, appended otherwise. */+export function withInlineChrome(doc: string, token: string, prelude: string): string {const script = measurementScript(token)const bodyClose = /<\/body\s*>/i.exec(doc)+ const framed = bodyClose ? doc.slice(0, bodyClose.index) + script + doc.slice(bodyClose.index) : doc + script- if (bodyClose) {- return doc.slice(0, bodyClose.index) + script + doc.slice(bodyClose.index)- }+ return prelude + framed+}- return doc + script+export interface FrameSizeReport {+ height: number+ /** Intrinsic content width, 0 when unmeasurable. */+ width: number}/** Parse a size report from the frame. Null unless it is OUR message type,* carries OUR token, and holds a sane finite height — anything inside the* sandbox can postMessage, so everything is validated before it moves the- * layout. Clamped to the band. */-export function frameHeightFromMessage(data: unknown, token: string): number | null {+ * layout. Height clamped to the band; width sanitized but uncapped (the+ * frame caps it against the column at render). */+export function frameSizeFromMessage(data: unknown, token: string): FrameSizeReport | null {if (typeof data !== 'object' || data === null) {return null}- const message = data as { type?: unknown; token?: unknown; height?: unknown }+ const message = data as { type?: unknown; token?: unknown; height?: unknown; width?: unknown }if (message.type !== SIZE_MESSAGE_TYPE || message.token !== token || typeof message.height !== 'number') {return null@@ -103,7 +175,15 @@ export function frameHeightFromMessage(data: unknown, token: string): number | nreturn null}- return Math.min(MAX_HEIGHT, Math.max(MIN_HEIGHT, Math.round(message.height)))+ const width =+ typeof message.width === 'number' && Number.isFinite(message.width) && message.width > 0+ ? Math.round(message.width)+ : 0++ return {+ height: Math.min(MAX_HEIGHT, Math.max(MIN_HEIGHT, Math.round(message.height))),+ width+ }}const HTML_FILE_RE = /\.(?:html?|xhtml)$/i@@ -123,26 +203,27 @@ export function InlinePreviewDirective({return file ? <PreviewAttachment source="explicit-link" target={file} /> : null}- return <InlineHtmlFrame file={file} fixedHeight={directiveFrameHeight(attrs.height)} streaming={streaming} />+ return <InlineHtmlFrame file={file} initialHeight={directiveFrameHeight(attrs.height)} streaming={streaming} />}function InlineHtmlFrame({file,- fixedHeight,+ initialHeight,streaming}: {file: string- /** Explicit `height` attribute — opts out of content-driven sizing. */- fixedHeight: number | null+ /** `height` attribute — the starting height only; measurement overrides. */+ initialHeight: number | nullstreaming: boolean}) {- const { t } = useI18n()const cwd = useStore(useSessionView().$cwd)const isDark = useIsDark()const [doc, setDoc] = useState<string | null>(null)const [failed, setFailed] = useState(false)const [measured, setMeasured] = useState<number | null>(null)- const heightRef = useRef<number>(fixedHeight ?? DEFAULT_HEIGHT)+ const heightRef = useRef<number>(initialHeight ?? DEFAULT_HEIGHT)+ const [contentWidth, setContentWidth] = useState<number | null>(null)+ const adoptedWidthRef = useRef<number | null>(null)// One token per mount: the message listener only trusts reports from the// document THIS mount injected, so two previews in one transcript (or a@@ -182,45 +263,65 @@ function InlineHtmlFrame({}, [path, streaming])useEffect(() => {- if (fixedHeight !== null) {- return- }-const onMessage = (event: MessageEvent) => {- const next = frameHeightFromMessage(event.data, token)+ const next = frameSizeFromMessage(event.data, token)- if (next !== null && Math.abs(next - heightRef.current) > RESIZE_TOLERANCE) {- heightRef.current = next- setMeasured(next)+ if (next === null) {+ return+ }++ if (Math.abs(next.height - heightRef.current) > RESIZE_TOLERANCE) {+ heightRef.current = next.height+ setMeasured(next.height)+ }++ // Width adopts ONCE, from the first report — measured at full column+ // width, so it is the content's intrinsic span. Tracking width live+ // would feedback-loop: %-width children reflow narrower every time+ // the frame shrinks, spiraling toward zero.+ if (adoptedWidthRef.current === null && next.width > 0) {+ adoptedWidthRef.current = next.width+ setContentWidth(next.width)}}window.addEventListener('message', onMessage)return () => window.removeEventListener('message', onMessage)- }, [fixedHeight, token])+ }, [token])++ // Resolved once per mount; theme switches remount the transcript anyway.+ const framedDoc = useMemo(() => {+ if (doc === null) {+ return null+ }- const framedDoc = useMemo(() => (doc === null ? null : withMeasurement(doc, token)), [doc, token])+ const { vars, font } = collectThemeBridge()++ return withInlineChrome(doc, token, themePrelude(vars, font))+ }, [doc, token])if (!path || failed) {return <PreviewAttachment source="explicit-link" target={file} />}- const height = fixedHeight ?? measured ?? DEFAULT_HEIGHT+ const height = measured ?? initialHeight ?? DEFAULT_HEIGHT+ // Left-aligned in the message flow, like an image: the frame is only as+ // wide as its content (capped at the column). Fluid pages measure the+ // full viewport and stay full-bleed.+ const width = contentWidth !== null ? Math.min(contentWidth, MAX_COLUMN_WIDTH) : undefinedreturn (- <span className="my-2 grid w-full max-w-160 gap-2">+ <span className="my-2 block w-full max-w-160">{framedDoc === null ? (<span- className="grid w-full animate-pulse place-items-center rounded-lg border border-(--ui-stroke-tertiary) text-[0.75rem] text-muted-foreground"+ className="block w-full animate-pulse rounded-md bg-[color-mix(in_srgb,currentColor_4%,transparent)]"style={{ height }}- >- {t.preview.opening}- </span>+ />) : (<span- className="relative block w-full overflow-hidden rounded-lg border border-(--ui-stroke-tertiary) transition-[height] duration-200"- style={{ height }}+ className="relative block max-w-full transition-[height] duration-200"+ style={{ height, width: width ?? '100%' }}><iframeclassName="absolute inset-0 size-full border-0 bg-transparent"@@ -230,10 +331,8 @@ function InlineHtmlFrame({style={{ colorScheme: isDark ? 'dark' : 'light' }}title={file}/>- <ScrollGate /></span>)}- <PreviewAttachment source="explicit-link" target={file} /></span>)}