text
stringlengths
0
1.47k
const onMessage = (event: MessageEvent) => {
const report = event.data as { type?: unknown; token?: unknown; height?: unknown } | null
if (report?.type !== HEIGHT_MESSAGE_TYPE || report.token !== 'streaming-preview') return
if (typeof report.height !== 'number' || !Number.isFinite(report.height)) return
setContentHeight(Math.max(0, Math.ceil(report.height)))
}
addEventListener('message', onMessage)
return () => removeEventListener('message', onMessage)
}, [])
// The shell loads ONCE per streaming call; updates flow over postMessage
// and sync incrementally, so unchanged components persist and new ones
// float in instead of the whole frame reloading per flush.
const doc = useMemo(() => buildStreamShellDoc({
...resolveTheme(),
reportToken: 'streaming-preview',
}), [])
useEffect(() => {
if (!loaded || !hasContent) return
frameRef.current?.contentWindow?.postMessage({
type: STREAM_MESSAGE_TYPE,
token: 'streaming-preview',
fragment: preview,
}, '*')
}, [loaded, hasContent, preview])
return (
<div style={wrapStyle}>
<div style={labelStyle}>
{hasContent ? 'Visualize · streaming preview' : 'Visualize · composing…'}
</div>
<iframe
ref={frameRef}
sandbox="allow-scripts"
referrerPolicy="no-referrer"
title="Visualization streaming preview"
srcDoc={doc}
style={{ ...frameStyle, height: hasContent ? Math.min(contentHeight, PREVIEW_MAX_HEIGHT) : 0 }}
onLoad={() => setLoaded(true)}
/>
</div>
)
}
/**
* Dock entry: mounts the preview exactly while the streaming partial carries
* a `visualize` tool-call block.
*/
export function StreamingPreview({ session }: StreamingPreviewProps) {
// Defensive guard mirroring the official dock rows: a host passing no
// session must degrade to an empty dock instead of crashing.
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const blocks = session?.partial?.blocks
if (blocks === undefined) return null
// Last matching block: a turn can contain several visualize calls; only the
// most recent one can still be streaming.
let argsRaw: string | undefined
for (const block of blocks) {
if (block.kind === 'tool-call' && block.name === VISUALIZE_TOOL_NAME) argsRaw = block.argsRaw
}
if (argsRaw === undefined) return null
return <Preview argsRaw={argsRaw} />
}
# ===== file: src/client/TurnCards.tsx =====
/**
* A turn's settled visualizations, shown again under its final answer.
*
* From dsh 0.1.2 the Web chat folds a finished turn's process — every tool
* row, visualize cards included — behind its duration summary whenever the
* reader's work-details mode folds (the default). A visualization is the
* turn's output rather than its process, so this re-shows the turn's cards on
* the turn tail, the seat the host's own deliverables use for the same
* reason. The toolview card stays inside the fold as the call's own record.
*
* The cards come from a state-only Conversation Definition that collects each
* successful `visualize` result's persisted meta per turn; nothing here reads
* the session log directly or imports host runtime code.
*/
import type { CSSProperties } from 'react'
import type {
ConversationLocationData, ConversationNodeDefinition, TurnLocation,
} from '@deepseek-ai/dsh-client-ui-conversation/client'
import type { HostObservable, InjectFace, PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots'
// Type-only: pulls the `conversation.chat.turnTail` SlotMap declaration.
import type {} from '@deepseek-ai/dsh-client-ui-chat/client'
import { visualizeMetaFrom, type VisualizeMeta } from '../fragment.ts'
import { Frame } from './VisualizeCard.tsx'
/** Definition kind and Turn data key; namespaced by package to stay unique. */
export const TURN_CARDS_KEY = 'dsh-visualize'
/** One settled card, keyed by the call that produced it. */
export interface TurnCard {
readonly callId: string
readonly seq: number
readonly meta: VisualizeMeta
}
/** Per-turn value published on the Turn Location. */
export interface VisualizeTurnData {