diff --git a/agent/prompt_builder.py b/agent/prompt_builder.py index 6b24735d44..2bbab9ce8c 100644 --- a/agent/prompt_builder.py +++ b/agent/prompt_builder.py @@ -927,10 +927,10 @@ PLATFORM_HINTS = { "video play inline, and other files arrive as download links. You can " "also include image URLs in markdown format ![alt](url) and they " "render inline as photos. " - "To show a workspace file as a live inline preview card (an HTML page " - "you built, a report), put ::preview{file=\"path/to/file.html\"} alone " - "on its own line — desktop plugins can register more ::name{...} " - "directives like it. " + "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. " "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/app/contrib/controller.tsx b/apps/desktop/src/app/contrib/controller.tsx index c0b76919e8..ff5b21b56d 100644 --- a/apps/desktop/src/app/contrib/controller.tsx +++ b/apps/desktop/src/app/contrib/controller.tsx @@ -6,7 +6,7 @@ import { SessionDraftTitle } from '@/app/chat/session-draft-title' import { SessionStatusDot } from '@/app/chat/session-status-dot' import { PALETTE_AREA, type PaletteContribution, paletteToggle } from '@/app/command-palette/contrib' import { type StatusbarItem } from '@/app/shell/statusbar-controls' -import { PreviewAttachment } from '@/components/chat/preview-attachment' +import { InlinePreviewDirective } from '@/components/assistant-ui/inline-preview-directive' import { IdleMount } from '@/components/idle-mount' import { $layoutEditMode, toggleLayoutEditMode } from '@/components/pane-shell/edit-mode' import { allPaneIds, group, groupLeafIds, split } from '@/components/pane-shell/tree/model' @@ -277,15 +277,16 @@ registry.registerMany([ } satisfies PaletteContribution }, // The core `::preview{file="…"}` transcript directive — the model (or a - // skill) addresses the existing preview card deliberately instead of - // relying on artifact/fence heuristics. Also the reference consumer for + // skill) renders a workspace HTML file LIVE inside its own message + // (sandboxed srcdoc iframe; falls back to the classic preview card for + // non-HTML targets and remote gateways). Also the reference consumer for // the `transcript.directives` area plugins register into. { id: 'transcript.preview', area: TRANSCRIPT_DIRECTIVE_AREA, data: { name: 'preview', - render: ({ attrs }) => (attrs.file ? : null) + render: ({ attrs, streaming }) => } satisfies TranscriptDirectiveContribution }, { 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.ts new file mode 100644 index 0000000000..9f65458688 --- /dev/null +++ b/apps/desktop/src/components/assistant-ui/inline-preview-directive.test.ts @@ -0,0 +1,18 @@ +import { describe, expect, it } from 'vitest' + +import { directiveFrameHeight } from './inline-preview-directive' + +describe('directiveFrameHeight', () => { + it('defaults when absent or garbage', () => { + expect(directiveFrameHeight(undefined)).toBe(280) + expect(directiveFrameHeight('')).toBe(280) + expect(directiveFrameHeight('tall')).toBe(280) + expect(directiveFrameHeight('12.5')).toBe(280) + }) + + it('clamps to the sane band', () => { + expect(directiveFrameHeight('50')).toBe(120) + expect(directiveFrameHeight('480')).toBe(480) + expect(directiveFrameHeight('99999')).toBe(1200) + }) +}) diff --git a/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx b/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx new file mode 100644 index 0000000000..804296029c --- /dev/null +++ b/apps/desktop/src/components/assistant-ui/inline-preview-directive.tsx @@ -0,0 +1,135 @@ +import { useStore } from '@nanostores/react' +import { useEffect, 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. + * + * Non-HTML targets and remote gateways (no local file access) fall back to + * the standard preview-attachment card rather than a broken frame. + */ + +const MIN_HEIGHT = 120 +const MAX_HEIGHT = 1200 +const DEFAULT_HEIGHT = 280 + +export function directiveFrameHeight(raw: string | undefined): number { + if (!raw) { + return DEFAULT_HEIGHT + } + + const parsed = Number(raw) + + if (!Number.isInteger(parsed)) { + return DEFAULT_HEIGHT + } + + return Math.min(MAX_HEIGHT, Math.max(MIN_HEIGHT, parsed)) +} + +const HTML_FILE_RE = /\.(?:html?|xhtml)$/i + +export function InlinePreviewDirective({ + attrs, + streaming +}: { + attrs: Readonly> + streaming: boolean +}) { + const file = attrs.file ?? '' + + // Not renderable inline: hand the whole leaf to the classic card. Remote + // gateways lack a local-file door; non-HTML has nothing to frame. + if (!file || isRemoteGateway() || !HTML_FILE_RE.test(file)) { + return file ? : null + } + + return +} + +function InlineHtmlFrame({ file, height, streaming }: { file: string; height: number; streaming: boolean }) { + const { t } = useI18n() + const cwd = useStore(useSessionView().$cwd) + const isDark = useIsDark() + const [doc, setDoc] = useState(null) + const [failed, setFailed] = useState(false) + + // Resolve against THIS session's cwd (the file was written by its agent). + const resolved = localPreviewTarget(file, cwd || undefined) + const path = resolved?.path ?? null + + useEffect(() => { + // Wait for turn settle: mid-stream the file is often mid-write, and a + // half-written srcdoc renders as garbage that never self-corrects. + if (!path || streaming) { + return + } + + let alive = true + + void Promise.resolve(window.hermesDesktop?.readFileText(path)) + .then(result => { + if (!alive) { + return + } + + if (!result || result.binary || !result.text) { + setFailed(true) + } else { + setDoc(result.text) + } + }) + .catch(() => alive && setFailed(true)) + + return () => { + alive = false + } + }, [path, streaming]) + + if (!path || failed) { + return + } + + return ( + + {doc === null ? ( + + {t.preview.opening} + + ) : ( + +