refactor(ui,chat-ui,desktop): one clipboard-file reader for every paste handler

Merged
KI
3mo ago
+182-45

Summary

  • Adds getClipboardFiles to packages/ui/src/lib/ and routes all four paste
    handlers through it, replacing three divergent implementations.
  • No behaviour change. This started as a fix for a reported "multipaste
    doesn't work when pasting multiple things at the same time" bug. I could not
    reproduce that bug, and the measurements below are the evidence.

Why / Context

Four handlers each read files off a DataTransfer their own way:

HandlerRead from
packages/ui PromptInputTextareaitems only
desktop TiptapPromptEditoritems only
chat-ui ComposerBody (Lexical)files only
desktop MarkdownEditorboth, de-duplicated

The merged form in MarkdownEditor arrived with #5283, when pasted images went
missing from the new-workspace prompt. It was never shared, so the other three
kept their own shapes.

What the measurements showed

Driven over CDP against the real desktop app: files written to the macOS
pasteboard with NSPasteboard.writeObjects, focused by a real click, pasted
with a real Cmd+V. All candidate implementations were run against the same
live paste event, so the comparison is exact.

PayloadfilesitemsitemsOnlyfilesOnlyunion(name:size)union(+lastModified)
3 PNGs (Finder copy)333333
png + pdf + txt333333
screenshot bytes111112
3 images as raw bytes111111
image + text121112
same name + same size x2222222
html + image121111

Three conclusions:

  1. items never under-reported against files. The multi-file paste bug
    does not reproduce on any payload shape I could construct. The three
    pre-existing implementations are equivalent in practice.
  2. Never key clipboard de-duplication on lastModified. Chromium
    synthesizes a separate File per accessor and stamps each with Date.now()
    as it is created, so the same pasted screenshot came back as ...223 via
    files and ...224 via items. I had "strengthened" the key that way; it
    duplicates the attachment. Reverted, and there is now a test for it.
  3. Copying several images as raw bytes yields one file to the page (3
    NSImages written, files.length === 1). That loss happens at the
    Chromium/macOS pasteboard boundary, so no JS handler can recover images 2..n.
    Not addressed here.

Before / after

The one defect I could reproduce end to end is the lastModified duplicate, so
that is what the before/after covers. Eight identical single-image pastes:

BEFORE (lastModified in key): per-paste cards [1,1,2,1,1,1,1,1]  total 9  duplicated 1/8
AFTER  (name:size:type):      per-paste cards [1,1,1,1,1,1,1,1]  total 8  duplicated 0/8

It is intermittent by nature: it fires only when the millisecond ticks between
the two accessor calls, which makes it the kind of duplicate that is painful to
chase in the wild.

Manual QA

Verified in the running desktop app (this worktree, Vite 4625), signed in, with
real pasteboard payloads and real Cmd+V.

New-workspace composer

  • Finder copy of 3 PNGs attaches 3 cards, correct thumbnails
  • Mixed png + pdf + txt attaches 3 cards (image card plus two file cards)
  • Screenshot bytes attach exactly one card, repeatedly (20 consecutive pastes)
  • Two files with identical name and identical byte size both attach
  • Image + text and HTML + image attach the image
  • Text-only paste still inserts text and attaches nothing

Terminal pane

  • 3-file paste is intercepted by the existing image-paste fallback: all 3
    files present in the payload, event does not reach document, no path or
    garbage inserted
  • Text paste still reaches the PTY
  • terminal-image-paste-fallback.ts is untouched by this PR

Testing

  • bun test packages/ui/src packages/chat-ui/src (41 pass)
  • 6 new tests in clipboard-files.test.ts; confirmed each fails against the
    implementation it guards (items-only, and the lastModified key)
  • bun run --cwd packages/ui typecheck, bun run --cwd packages/chat-ui typecheck,
    bun run --cwd apps/desktop typecheck
  • bunx biome check, bunx sherif
  • Pre-existing unrelated failures in
    apps/desktop/src/renderer/lib/terminal/font-settle.test.ts (fail on a clean
    checkout of that file, import nothing this PR touches)

Design decisions

  • Merge both lists rather than pick one. They agree today, so this is
    belt-and-braces. It cannot return fewer files than any implementation it
    replaces, which is the property worth having in shared code.
  • De-duplicate on name:size:type. The only fields stable across the two
    accessors. See conclusion 2.

Known limitations

  • Several images copied as raw bytes still arrive as one file. Upstream of this
    code.
  • maxFiles and maxFileSize on PromptInput are ignored when a controller
    provider is supplied, since the provider's add skips the local filters.
    Pre-existing, unrelated, not touched here.

Risks / Rollout

  • Risk: low. Pure consolidation, no behaviour change measured across seven
    payload shapes and four surfaces.
  • Rollback: revert the two commits; each handler's previous implementation is
    restored intact.

Summary by cubic

Consolidates all four paste handlers onto one shared getClipboardFiles helper and fixes an intermittent duplicate-attachment bug. MarkdownEditor previously de-duplicated on lastModified, which Chromium stamps anew per accessor, so roughly one in eight identical image pastes attached the image twice; the new helper keys on name:size:type instead.

Refactors

  • Replaces the two items-only readers, the files-only reader, and the merged MarkdownEditor reader with one helper in packages/ui/src/lib/.
  • Measured against a real macOS pasteboard, all implementations returned the same files, so this is consolidation, not a behavior change.
  • Adds 6 tests in clipboard-files.test.ts, including one that guards against the lastModified key.

Summary by CodeRabbit

  • Bug Fixes

    • Improved file attachment handling when pasting content into editors and prompt fields.
    • Prevented duplicate pasted files and ensured files are detected across supported clipboard formats.
    • Gracefully handles empty, unavailable, or non-file clipboard content.
  • Tests

    • Added coverage for clipboard file extraction, deduplication, fallback behavior, and invalid clipboard data.