feat(desktop): ::preview renders the page live inside the message, not just a rail-opener card
The first cut of the core ::preview consumer rendered the classic preview-attachment card — a button into the right rail we already had, which made the directive indistinguishable from an ordinary preview link. Now the directive shows the thing itself: the workspace HTML file renders in a sandboxed srcdoc iframe inline in the assistant message (opaque origin, allow-scripts only — no reach into the app, its storage, or the bridge), with an optional height attribute clamped to 120-1200px and the classic card kept below as the rail escape hatch. The frame waits for turn settle before reading the file (mid-stream it is often mid-write), resolves relative paths against the session's own cwd, and falls back to the plain card for non-HTML targets and remote gateways (no local file door there).
This commit is contained in:
committed by
brooklyn!
parent
8f2ddc9676
commit
8425f8286b
@@ -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  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; "
|
||||
|
||||
@@ -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 ? <PreviewAttachment source="explicit-link" target={attrs.file} /> : null)
|
||||
render: ({ attrs, streaming }) => <InlinePreviewDirective attrs={attrs} streaming={streaming} />
|
||||
} satisfies TranscriptDirectiveContribution
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
@@ -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<Record<string, string>>
|
||||
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 ? <PreviewAttachment source="explicit-link" target={file} /> : null
|
||||
}
|
||||
|
||||
return <InlineHtmlFrame file={file} height={directiveFrameHeight(attrs.height)} streaming={streaming} />
|
||||
}
|
||||
|
||||
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<string | null>(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 <PreviewAttachment source="explicit-link" target={file} />
|
||||
}
|
||||
|
||||
return (
|
||||
<span className="my-2 grid w-full max-w-160 gap-2">
|
||||
{doc === null ? (
|
||||
<span
|
||||
className="grid w-full animate-pulse place-items-center rounded-lg border border-(--ui-stroke-tertiary) text-[0.75rem] text-muted-foreground"
|
||||
style={{ height }}
|
||||
>
|
||||
{t.preview.opening}
|
||||
</span>
|
||||
) : (
|
||||
<span
|
||||
className="relative block w-full overflow-hidden rounded-lg border border-(--ui-stroke-tertiary)"
|
||||
style={{ height }}
|
||||
>
|
||||
<iframe
|
||||
className="absolute inset-0 size-full border-0 bg-transparent"
|
||||
loading="lazy"
|
||||
sandbox="allow-scripts"
|
||||
srcDoc={doc}
|
||||
style={{ colorScheme: isDark ? 'dark' : 'light' }}
|
||||
title={file}
|
||||
/>
|
||||
<ScrollGate />
|
||||
</span>
|
||||
)}
|
||||
<PreviewAttachment source="explicit-link" target={file} />
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -382,8 +382,12 @@ Rules the host enforces so the surface stays safe:
|
||||
with your slug (`myplugin-board`, not `board`).
|
||||
|
||||
Core ships one directive as the reference consumer: `::preview{file="…"}`
|
||||
renders the standard preview-attachment card for a workspace file. Tell the
|
||||
agent about your directive in a skill (that's how it learns to emit it).
|
||||
renders the workspace HTML file **live inside the message** — a sandboxed
|
||||
`srcdoc` iframe with an opaque origin (scripts run; no reach into the app,
|
||||
its storage, or the bridge), optional `height="480"`, with the classic
|
||||
preview card below it as the rail escape hatch. Non-HTML targets and remote
|
||||
gateways fall back to the card alone. Tell the agent about your directive in
|
||||
a skill (that's how it learns to emit it).
|
||||
|
||||
### Mount-scoped chrome (`Contribute`)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user