refactor(ui,chat-ui,desktop): one clipboard-file reader for every paste handler
Summary
- Adds
getClipboardFilestopackages/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:
| Handler | Read from |
|---|---|
packages/ui PromptInputTextarea | items only |
desktop TiptapPromptEditor | items only |
chat-ui ComposerBody (Lexical) | files only |
desktop MarkdownEditor | both, 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.
| Payload | files | items | itemsOnly | filesOnly | union(name:size) | union(+lastModified) |
|---|---|---|---|---|---|---|
| 3 PNGs (Finder copy) | 3 | 3 | 3 | 3 | 3 | 3 |
| png + pdf + txt | 3 | 3 | 3 | 3 | 3 | 3 |
| screenshot bytes | 1 | 1 | 1 | 1 | 1 | 2 |
| 3 images as raw bytes | 1 | 1 | 1 | 1 | 1 | 1 |
| image + text | 1 | 2 | 1 | 1 | 1 | 2 |
| same name + same size x2 | 2 | 2 | 2 | 2 | 2 | 2 |
| html + image | 1 | 2 | 1 | 1 | 1 | 1 |
Three conclusions:
itemsnever under-reported againstfiles. The multi-file paste bug
does not reproduce on any payload shape I could construct. The three
pre-existing implementations are equivalent in practice.- Never key clipboard de-duplication on
lastModified. Chromium
synthesizes a separateFileper accessor and stamps each withDate.now()
as it is created, so the same pasted screenshot came back as...223via
filesand...224viaitems. I had "strengthened" the key that way; it
duplicates the attachment. Reverted, and there is now a test for it. - 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 reachdocument, no path or
garbage inserted - Text paste still reaches the PTY
-
terminal-image-paste-fallback.tsis 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 thelastModifiedkey) bun run --cwd packages/ui typecheck,bun run --cwd packages/chat-ui typecheck,
bun run --cwd apps/desktop typecheckbunx 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. maxFilesandmaxFileSizeonPromptInputare ignored when a controller
provider is supplied, since the provider'saddskips 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, thefiles-only reader, and the mergedMarkdownEditorreader with one helper inpackages/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 thelastModifiedkey.
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.