feat(chat): step through every chat image in the preview overlay, and copy the image

Merged
AV
1mo ago
+1881-262

feat(chat): step through every chat image in the preview overlay, and copy the image

Branch chat-image-gallery, forked from main at d456295848. Three commits, head d1a25df9fc.

Summary

Opening any image in a chat (a thumbnail in a sent message, or an image the agent read, captured or received) now opens one preview overlay for the whole conversation. Previous and Next buttons sit on the scrim edges, Left and Right arrow keys do the same, and a small "3 of 12" count shows where you are. Stepping past either end wraps around to the other end, and the buttons and the count stay hidden when a chat has a single image. The sequence covers every image in the transcript in reading order, across turns; PDFs and code files are not in it and keep today's behaviour.

The overlay also gets a Copy image button next to Download and Close. It puts the image itself on the clipboard, pasteable into Slack, Figma or another chat, shows a check for 1.5 s on success and a toast on failure. ⌘C / Ctrl+C in the overlay does the same unless text is selected. Pending images in the composer get the same Previous/Next as their own short sequence.

How the gallery is built

  • One overlay per chat pane. ChatImageGalleryProvider wraps the transcript. It holds the ordered image list and the open image; thumbnails call open(id) instead of owning an overlay. No store is shared across panes.
  • Derived from transcript data, not mounted components. galleryImages(rows) walks the same rows the transcript renders: a user message's attachment tags, an agent message's or tool call's image attachments, and pending outbox prompts last. Non-images are excluded and each row contributes a path once, so a run that reads the same file twice gives one entry. Whole-session loading renders only rows near the viewport, so an image's thumbnail may not be mounted; the list does not depend on it.
  • Stable ids. An entry's id is the item's transcript key plus the path. A user message keeps its client id when the pending prompt is echoed, and a tool image is owned by the tool call that carried it, so the id survives a lone tool call growing into a run. If the open image leaves the list (a discarded prompt), the overlay closes.
  • The overlay loads bytes itself. useChatImageSource is the one read path for thumbnails and the overlay: the same filesystem.readFile query with the same input, so a mounted thumbnail and the overlay share one cached result. A file over the 5 MB thumbnail cap gets the 25 MB read only once the overlay shows it, and a failed read says "Couldn't load " in the overlay instead of swapping the tile for a badge. The neighbours of the open image are fetched as it opens so stepping is instant.
  • Overlay API. ImagePreviewOverlay gains optional onPrevious, onNext, position, onCopy, fallback and focusOnClose, and src may be null while bytes load. The four existing callers (markdown ImageView, RecordAttachments, the New Workspace AttachmentCard, the composer preview) pass nothing new and are unchanged.
  • Keyboard and focus. The key listener stays on window in the capture phase; arrows are stopped there so neither the transcript nor the composer under the overlay moves. The dialog takes focus on open, Tab wraps inside it, and on close focus returns to the thumbnail of the image last shown, or to the one that opened the overlay if that is the only one mounted. No slide animation between images, a plain swap.
  • Composer. A pill click now also hands useAttachmentPreview the full pending list (onAttachmentClick(attachment, attachments) in @superset/chat-ui), so it steps through the composer's images in pill order as a separate sequence.

Copy path chosen, and why

Renderer first: copyImageToClipboard fetches the data or object URL the overlay already shows (no second file read), re-encodes anything that is not already PNG through a canvas because Chromium's clipboard takes only image/png, and writes it with navigator.clipboard.write(new ClipboardItem(...)). The page clipboard refuses a write when the document lacks focus, so useCopyImageToClipboard falls back to a new external.copyImage procedure that writes the PNG with Electron's clipboard.writeImage. Verified in the dev app: after a copy, osascript -e 'clipboard info' listed «class PNGf» and the dumped PNG was 480×200, the image shown.

⌘C goes through the Edit menu's Copy command in the app, which reaches the page as a copy event rather than a keydown, so the overlay handles that event. Text selected inside the overlay still copies as text; a selection left in the transcript behind it does not win over the image.

A copy confirms itself: an "Image copied" toast with the passed-check icon from the PR checks list, a short white flash over the image, and the Copy button turning into a green check with a small pop. The flash and pop are skipped under reduced motion.

The overlay marks itself data-state="open", so the existing rule that turns off the window's drag strips while a dialog is open applies to it. Before, the corner buttons overlapped the 48px drag strip and sometimes ignored hover and clicks.

Test plan

Automated (all green, Bun 1.4.2 with --isolate):

  • galleryImages.test.ts: ordering from transcript rows with user and agent images interleaved across turns, a tool run that repeats a path, an outbox prompt last; non-image attachments, untyped tags and rows without images excluded.
  • ImagePreviewOverlay.test.tsx: edge buttons step and stay enabled at the ends; buttons, count and arrow keys off for one image; arrow keys step from either end; arrows do not reach listeners under the overlay; copy success shows the check; copy failure keeps the button and toasts; ⌘C copies unless text is selected; null src shows the fallback and disables copy; focus enters the dialog and returns to the opener.
  • ChatImageGalleryProvider.test.tsx: a thumbnail opens its image and the overlay steps through every image, mounted or not; stepping past either end wraps; neighbours are prefetched across the wrap; closing returns focus to the thumbnail of the image shown.
  • copyImageToClipboard.test.ts: PNG passes through as is; a rejecting writer fails the copy.
  • Updated: AttachmentImage (over-cap placeholder opens the overlay from the larger read; a failed larger read reports in the overlay), PreviewableImage, AttachmentRail, useAttachmentPreview (steps through pending images only, in pill order).
  • Whole ChatSession + composer suites: 290 pass. packages/chat-ui PromptInput: 20 pass.
  • tsc --noEmit for apps/desktop and packages/chat-ui: exit 0. biome check --write --unsafe on touched files. bun run check:i18n clean, with the three new strings translated in all 16 locales.

Live, over CDP against this worktree's dev app (RENDERER_REMOTE_DEBUG_PORT=9223 bun dev), screenshots in /tmp/chat-image-gallery-shots/:

  1. Chat with three images across two turns: two user attachments in turn 1, one image the agent read with its Read tool in turn 2 (hash-named file under .superset/attachments).
  2. Clicked the first thumbnail: overlay at "1 of 3", focus on the dialog (06).
  3. ArrowRight → "2 of 3" (07); ArrowRight → "3 of 3", the agent's image (200×420) loaded by the overlay itself (08); clicked Previous → "2 of 3" (09). After the wrap change: ArrowLeft from "1 of 3" → "3 of 3" (16), ArrowRight from 3 → 1, and the Previous button from 1 → 3.
  4. Copy button → "Copied" check, no toast, clipboard holds «class PNGf» (10); cleared the clipboard, ⌘C → PNG again; the dumped PNG is 480×200.
  5. Escape → overlay gone, focus on "Preview green-wide.png", the thumbnail of the image last shown.
  6. Composer: two pending pills, clicked the second → "2 of 2" (11); ArrowLeft → "1 of 2" (12); Escape returns focus to the pill.

Open questions

  • The edge buttons sit at left-4 / right-4 over the scrim and can overlap the edges of a very wide image; the corner buttons already behave this way. Worth a look on a small window.
  • Prefetching covers one neighbour each side. If stepping fast through many large files feels laggy, widen to two.

Summary by cubic

Replaces the per-image preview overlay with a single chat-wide gallery: opening any thumbnail steps through every image in the conversation with Previous/Next buttons, arrow keys, a position count, and a new Copy image button. Stepping past either end wraps around.

New Features

  • Gallery order follows the transcript reading order; PDFs and code files keep the old behavior.
  • Copy button puts the image on the clipboard (non-PNG re-encoded via canvas), with a check on success and a toast on failure; ⌘C/Ctrl+C does the same unless text is selected.
  • Composer pills step through pending images in pill order as a separate sequence.
  • Previous/Next and arrow keys wrap between ends; controls stay hidden for a single image.

Implementation

  • Gallery is derived from transcript rows so it works even when thumbnails are not mounted.
  • Thumbnails and overlay share one cached file read; over-cap files read at full size only in the overlay, and failures show "Couldn't load ".
  • Copy falls back to an Electron main-process route when the page clipboard refuses.
  • Focus enters the dialog on open, Tab wraps, and returns to the thumbnail last shown on close.
  • Overlay marks data-state="open" so the window drag strips release clicks to the corner buttons.

Summary by CodeRabbit

  • New Features
    • Open transcript images in a shared gallery and navigate through images with on-screen controls or keyboard shortcuts.
    • Browse pending image attachments in order; non-image attachments are skipped.
    • Copy or download images from previews, including full-size images. Previews show loading states and filename-specific errors.
  • Improvements
    • Closing a gallery restores focus to the displayed image’s thumbnail when available.
    • Gallery navigation and image-loading messages are available in multiple languages.