assistant-preview-cards.md 14 KB

Assistant File Preview Cards

Assistant file preview cards render inside the latest terminal assistant row, above message actions and before the per-turn change summary. Assistant body text keeps the existing Streamdown behavior: plain file-name mentions are not automatically rewritten into clickable links. A complete ::zcode-file-citation{...} directive is a separate, explicit projection and is rendered with the same file-link UI and opening behavior as a Markdown [label](path) link.

::zcode-file-citation{...} remains the canonical syntax emitted by prompts and Skills. The renderer also accepts the model compatibility form :zcode-file-citation{...} with one missing leading colon. This tolerance is limited to file citations: both forms share the same parameter parsing, path validation, body-link projection, and preview-card eligibility, while other directives such as ::code-comment{...} remain strict.

Scope

  • Preview card file types: Markdown, HTML, DOCX, XLSX, PPTX, PDF, MP4, MOV, WebM, M4V, MP3, WAV, M4A, OGG, Opus, FLAC, and WEBA.
  • Every preview card uses a 24 x 24 px leading icon for website, recognized-file, and fallback-file presentation, inside the existing card icon container.
  • The file type registry owns extension, subtitle, and opening behavior so another previewable file type can be added without changing the extraction pipeline.
  • Card extraction supports explicit Markdown file links and plain file path mentions. The shared extractor accepts Unicode file and directory names, paths containing spaces, Unix paths, and Windows drive-letter paths for every registered file type, including Markdown and HTML.
  • Local file references accept balanced ASCII quotes ("...", '...') and Chinese smart quotes (“...”, ‘...’) around citation parameters, Markdown destinations, and plain path mentions. Mismatched or unclosed quotes are kept as raw text and do not become file links, so malformed model output cannot silently change the target path.
  • Home-relative paths beginning with ~/ or ~\\ are resolved against the homedir reported by the current workspace Host. This applies consistently to preview cards, Assistant body links, citations, code comments, and local Markdown images. The renderer never expands ~ with its own process home; when the Host Home is unavailable, the path fails closed instead of being treated as a workspace-relative path.
  • Workspace-relative references are lexically normalized across / and \\ separators before they become cards. . segments are removed, .. segments may only cancel a preceding segment, and any traversal that would move above the workspace root is rejected before Host file I/O. Safe forms such as ./a/../report.pdf resolve to the normalized workspace path; unsafe forms such as foo/../../private.pdf do not generate a card.
  • Explicit absolute paths remain a separate, existing product capability. They are not joined to or authorized by the workspace-relative resolver; opening them continues to use the selected local or remote Host and its existing file access permissions.
  • Plain file-name mentions in Assistant body text are not auto-linked. Existing explicit Markdown-link handling remains outside this card feature and keeps its pre-existing behavior.
  • zcode-file-citation accepts path, purpose, and artifact_kind as its current common parameters. path is required; purpose is parsed but does not change presentation or opening behavior. Other syntactically valid parameters are tolerated and ignored by this projection.
  • A valid citation path is rendered in the Assistant body as a file link whose label is the path's leaf name. Citation body links are not limited to preview card extensions and do not perform an eager existence check.
  • Citation preview cards are limited to DOCX, XLSX, PPTX, PDF, video, and audio. The supported artifact_kind mapping is document -> docx, presentation -> pptx, workbook -> xlsx, video -> video, and audio -> audio; when artifact_kind is absent, the type is inferred from the path extension. An unknown or extension-incompatible artifact_kind suppresses only the citation card, not an otherwise valid body link.
  • A complete citation occupies one protected extraction range. Its inner path is not extracted again as a Markdown, HTML, or plain file reference, so citation card eligibility cannot be bypassed by the generic path regex.
  • During streaming, an unclosed citation on the final standalone line is hidden from the body until it closes. A valid-looking inline tail is treated the same way. If malformed output has already continued onto later prose, the raw text is retained so a missing } cannot swallow the rest of the answer. Code-fenced and inline-code examples are never projected.
  • Card candidates are ordered by reverse body occurrence, then deduplicated by resolved file path or URL.
  • At most the first 15 ordered candidates are checked. The Host checks those files concurrently, caches positive and negative results for one minute, and the UI renders at most the first 10 valid cards. Invalid candidates are not backfilled from positions after the first 15.
  • Renderer validation is keyed by the semantic card signature and the selected workspace file service. Re-rendering an equivalent card list with a new array identity reuses the settled validation result without another Host RPC; changing either the card signature or workspace file service starts a new validation.
  • Markdown and HTML cards require a matching active file in the current turn's fileChanges. Office, PDF, and video/audio cards do not use this gate.
  • Current-turn fileChanges requests are deduplicated above virtualized rows in SessionPane. Running-turn requests include the current projection revision in their key and share only the in-flight promise; the settled result is removed because that turn can still grow. Terminal-turn requests use sessionId + logEpoch + turnHeader rowId + entityId + fileChanges state, share both in-flight and successful results, and remain in the renderer-local LRU of at most 20 entries. A projection transition from active to reverted therefore cannot reuse the pre-rewind result. Failed requests are always removed.
  • File-reference extraction is memoized once per terminal turn text and workspace path. The Markdown/HTML fileChanges gate and final card builder consume the same extracted references; rebuilding an equivalent assistant-row array does not repeat Unicode/path parsing.

Projection and state boundary

Preview cards remain a UI-only projection from already delivered V4 rows. They do not add protocol rows, snapshot fields, replayable events, relay state, or agent-side metadata.

assistantText rows (desktop continuous / mobile replayable hydration)
  |
  +-- complete zcode-file-citation
  |     +-- body: render leaf-name link through existing MessageFileLink
  |     `-- cards: admit only compatible docx/xlsx/pptx/pdf references
  |
  +-- streaming / failed / non-tail row ----------------------> no cards
  |
  `-- latest complete|interrupted row
        |
        +-- join assistant text in body order once
        +-- extract file references once; reuse them for gate + card build
        +-- extract localhost candidates
        +-- fetch this turn's fileChanges with the turnHeader target when md/html is present
        |     +-- running: revision key + in-flight only
        |     +-- terminal: target + active|reverted key + settled success, shared LRU <= 20 entries
        |     `-- active md/html paths only
        +-- reverse occurrence order + dedupe
        +-- take first 15 candidates
        `-- workspace-scoped fileService.checkFilesExist()
              |
              +-- Host cache: workspace host instance + path, TTL 60 s
              +-- concurrent checks: at most 15
              `-- keep valid candidates, take first 10 -> render cards

The Host file service instance already belongs to one local or remote workspace host. Cache entries are therefore isolated by the host instance; renderer code must resolve the service with useWorkspaceServices(workspacePath, remoteSessionId, workspaceIdentity) so remote paths are checked by their remote workspace host.

The same workspace-scoped Host provides systemService.info().homedir for Home- relative Assistant paths. Desktop local uses the local Host Home; SSH/WSL/Docker and Web /remote use the attached remote Host Home. The Home result is cached per workspaceIdentity?.trim() || workspacePath plus remoteSessionId and refreshed when the Host service instance changes.

Workspace-scoped service selection identifies which Host performs file I/O; it does not by itself prove that an input path is under the workspace directory. That directory boundary is enforced earlier for relative Assistant card references by the shared lexical resolver. Rejected card candidates must never reach checkFilesExist, Preview Pane, Browser, or external-editor actions.

Citation body links deliberately remain outside this eager validation pipeline. They reuse the existing Markdown file-link click path, including its workspace scope, traversal rejection, Windows/UNC normalization, and click-time file stat. The one-minute checkFilesExist cache is used only when a citation becomes a preview-card candidate.

Current-turn fileChanges gate

Markdown and HTML are source-like formats that appear frequently in discussion. Their preview cards are shown only when the terminal row's turn reports an active fileChanges result containing the resolved path. A missing target, unavailable fetch function, reverted result, or failed request yields no Markdown or HTML cards. This failure does not suppress DOCX/XLSX/PPTX/PDF/video/audio cards.

The turn header's fileChanges.state is the authoritative rewind state. When it changes to reverted, the card projection immediately clears previously loaded Markdown/HTML paths and does not wait for a detail query to rediscover the state. The same primitive state is part of the shared terminal query key so the file summary panel cannot reuse an active Promise/result after rewind.

The query target must be the same turn's turnHeader row. The terminal assistantText row remains the body/card render anchor, but it is not a valid V4 fileChanges action target. If the turn header or its entity id is unavailable, the UI fails closed for Markdown/HTML cards instead of querying with an assistant row.

The gate compares normalized workspace paths. For a bare name, it may resolve to a fileChanges path only when that name identifies a matching file in the current turn; otherwise normal workspace-relative card resolution is used.

The shared cache lives above ConversationTimeline virtualization, so scrolling a completed turn out of view and back does not repeat the full-detail query. A running turn may expose a partial file summary; consumers therefore request it with an in-flight-only policy and refetch after the header enters a terminal state. This prevents an earlier projection revision from suppressing final Markdown/HTML cards or leaving a remounted file-change panel incomplete. It is recreated when the session, logEpoch, or fileChanges transport changes; failures are never retained. This cache is separate from the Host's one-minute file-existence cache because it stores turn diff results, including patch lines, instead of path existence booleans.

Opening behavior

  • DOCX, XLSX, PPTX, PDF, supported video/audio, and Markdown open in Preview Pane using a file source carrying workspacePath, optional workspaceIdentity, and optional workspaceRemoteSessionId.
  • After the latest assistant turn reaches complete, the renderer automatically opens the PPTX cards that survived the existing candidate limit and file existence validation. interrupted, streaming, failed, historical, and cold-resumed turns do not trigger automatic opening.
  • When one completed turn produces multiple visible PPTX cards, every card is opened as a separate right-side Code Viewer tab. Existing tabs are reused by the normal workspace-scoped source key, and the first PPTX card in the card order becomes the active tab. This is tabbed preview, not a new split-pane layout.
  • Local desktop HTML keeps the Browser side-pane behavior.
  • Remote workspace HTML on Desktop degrades from file:// Browser navigation to the file Preview Pane because the browser cannot safely resolve the host workspace path.
  • Web /remote does not project localhost/127.0.0.1 URL cards or HTML output cards. Without port forwarding or a URL proxy those URLs cannot reach the Desktop host, and Preview Pane would only show HTML source rather than the rendered page. Markdown, DOCX, XLSX, PPTX, PDF, and video/audio cards remain available.
  • Desktop localhost HTTP cards keep the Browser side-pane behavior. When a URL maps to a generated HTML file, the backing file must also pass the existence check.
  • Desktop local and Desktop remote workspaces support the automatic PPTX open because they expose a right-side Preview Pane. Web /remote keeps the cards visible, but does not auto-open when the current surface has no usable right- side Preview Pane.

Compatibility

  • Desktop stays on the existing desktop-continuous message path.
  • Web remote stays on web-remote-replayable; card projection runs only after row hydration and does not enter gap/snapshot recovery. Unsupported local URL and HTML candidates are filtered in renderer projection before the 15-card existence-check window, without adding protocol or Host state.
  • The fileChanges cache is renderer-local query state. It does not add relay, Host, snapshot, or replayable state and does not alter delivery semantics.
  • No service or cache key uses workspacePath as workspace identity. Remote routing continues to carry workspaceIdentity and remoteSessionId through the existing workspace service accessor.
  • Existing Preview Pane Office/PDF renderers are reused; this feature does not add another document parser.
  • Automatic opening is a renderer-local side effect. It does not add protocol rows, snapshot fields, relay state, or Agent metadata. A one-shot key scoped by workspace, session, log epoch, turn, and card signature prevents virtualized remounts and equivalent re-renders from reopening a tab after the user closes it.