text stringlengths 0 1.47k |
|---|
# ===== file: src/client/StreamingPreview.tsx ===== |
/** |
* Live streaming preview docked under the composer: while the model is still |
* generating a `visualize` call, the accumulated argument stream |
* (`assistant/chunk` tool-call deltas folded into the conversation snapshot's |
* `partial`) is parsed for the fragment prefix and rendered into the same |
* sandboxed frame the settled card uses — markup and style only, scripts |
* neutered until the call settles (a half-streamed script body is almost |
* never valid JavaScript). Renders nothing when no `visualize` call is |
* streaming; once the call settles the preview unmounts and the transcript |
* card takes over. |
*/ |
import { useEffect, useMemo, useRef, useState, type CSSProperties } from 'react' |
import type { PropsRuntime } from '@deepseek-ai/dsh-client-ui-slots' |
// Type-only: pulls the `conversation.input.dock` SlotMap declaration. |
import type {} from '@deepseek-ai/dsh-client-ui-conversation/client' |
import { extractStreamingFragment, trimStreamingScripts, VISUALIZE_TOOL_NAME } from '../fragment.ts' |
import { buildStreamShellDoc, HEIGHT_MESSAGE_TYPE, STREAM_MESSAGE_TYPE } from '../shell.ts' |
import { resolveTheme } from './theme.ts' |
/** Dock entry props: the InputZone owner share (live conversation snapshot). */ |
type StreamingPreviewProps = PropsRuntime<'conversation.input.dock'> |
/** |
* Flush interval for posting fragment updates into the persistent shell. |
* Updates are incremental DOM syncs, not reloads, so this can run near |
* per-delta speed; 150ms keeps the reveal continuous at negligible cost. |
*/ |
const FLUSH_MS = 150 |
/** |
* Preview height ceiling. The frame tracks the shell's *measured* content |
* height (its height reports), so invisible early markup — style blocks, |
* empty containers — keeps the frame collapsed and painted components grow |
* it; the cap stops a tall fragment from crowding out the composer. Reserving |
* space no paint has claimed reads as a layout bug, not as anticipation. |
*/ |
const PREVIEW_MAX_HEIGHT = 300 |
/** |
* The input dock spans the conversation view, not the composer column, so the |
* preview constrains itself to the composer's width family and centers. |
*/ |
const wrapStyle: CSSProperties = { margin: '6px auto 2px', maxWidth: 760, width: '100%' } |
const labelStyle: CSSProperties = { |
fontSize: 12, |
opacity: 0.65, |
margin: '0 0 4px', |
} |
const frameStyle: CSSProperties = { |
display: 'block', |
width: '100%', |
border: 0, |
background: 'transparent', |
transition: 'height 240ms ease', |
} |
/** |
* Throttle a fast-changing string: re-emits at most every {@link FLUSH_MS}, |
* with a trailing flush so the final value always lands. A plain trailing |
* debounce would starve under a continuous token stream — every delta would |
* reset the timer — so this flushes on the leading edge when the interval has |
* already elapsed. |
* @param value - the raw per-render value. |
* @returns the throttled value. |
*/ |
function useThrottled(value: string): string { |
const [shown, setShown] = useState(value) |
const lastFlush = useRef(0) |
useEffect(() => { |
const elapsed = Date.now() - lastFlush.current |
if (elapsed >= FLUSH_MS) { |
lastFlush.current = Date.now() |
setShown(value) |
return |
} |
const timer = setTimeout(() => { |
lastFlush.current = Date.now() |
setShown(value) |
}, FLUSH_MS - elapsed) |
return () => clearTimeout(timer) |
}, [value]) |
return shown |
} |
/** The mounted preview, split out so hooks only run while a call streams. */ |
function Preview({ argsRaw }: { argsRaw: string }) { |
const throttled = useThrottled(argsRaw) |
const fragment = extractStreamingFragment(throttled) |
const preview = fragment === undefined ? '' : trimStreamingScripts(fragment) |
const hasContent = preview.trim().length > 0 |
const frameRef = useRef<HTMLIFrameElement | null>(null) |
const [loaded, setLoaded] = useState(false) |
const [contentHeight, setContentHeight] = useState(0) |
// Follow the shell's height reports so the frame hugs what is actually |
// painted; the CSS transition turns each growth step into a smooth reveal. |
useEffect(() => { |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.