Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/workbench/extensions/agent/AgentPanelRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,15 @@ function dispatchDrag(
return event.defaultPrevented
}

function dispatchPaste(target: Element, files: File[]): boolean {
const event = new Event('paste', { bubbles: true, cancelable: true })
Object.defineProperty(event, 'clipboardData', {
value: { files, getData: () => '' }
})
target.dispatchEvent(event)
return event.defaultPrevented
}

function fileOfSize(name: string, size: number, type: string): File {
const file = new File(['x'], name, { type })
Object.defineProperty(file, 'size', { value: size })
Expand Down Expand Up @@ -1274,6 +1283,34 @@ describe('AgentPanelRoot attach flow', () => {
await vi.waitFor(() => expect(uploaded).toEqual(['huge.png']))
})

it('attaches an image pasted into the focused composer', async () => {
const uploaded = stubUploadFetch()
renderWithSelectedTarget()
await nextTick()

const image = new File(['image'], 'clipboard.png', { type: 'image/png' })
const textbox = screen.getByRole('textbox')
textbox.focus()

expect(dispatchPaste(textbox, [image])).toBe(true)
expect(
within(await screen.findByTestId('composer-asset-section')).getByText(
'clipboard.png'
)
).toBeInTheDocument()
await vi.waitFor(() => expect(uploaded).toEqual(['clipboard.png']))
})

it('leaves clipboard files outside the composer to the canvas', async () => {
stubUploadFetch()
renderWithSelectedTarget()
await nextTick()

const image = new File(['image'], 'canvas.png', { type: 'image/png' })
expect(dispatchPaste(document.body, [image])).toBe(false)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue: This test passes whether or not the composer guard exists, so the PR's central mechanism is uncovered.

I verified it rather than eyeballing it. On cac5412, I deleted the guard in AgentPanelRoot.vue:1267, reducing it to if (!(target instanceof Element)) return, and re-ran both new tests:

pnpm test:unit src/workbench/extensions/agent/AgentPanelRoot.test.ts \
  -t "clipboard|pasted into the focused composer"
→ Tests  2 passed | 242 skipped   (identical to the unmodified run)

The reason is the dispatch target. document.body is an ancestor of the element carrying @paste.capture, not a descendant, so the handler never runs for this event — the guard is never consulted and defaultPrevented is trivially false. The assertion holds for a reason unrelated to what it's meant to prove.

That matters because closest('#agent-composer') is doing real work: it's a magic string matched against an id defined in a different component (Composer.vue:332), with no type relationship between them. A rename there is exactly the regression this test should catch, and today it wouldn't.

Dispatching on something inside the panel but outside the composer covers it — the message list, or the panel root itself. With the guard intact that stays false; with it removed it flips to true and the test fails, which is the signal you want.

expect(screen.queryByText('canvas.png')).not.toBeInTheDocument()
})

it('uploads a dropped video above 20MB when the server permits it', async () => {
getServerFeature.mockReturnValue(100 * 1024 * 1024)
const uploaded = stubUploadFetch()
Expand Down
13 changes: 13 additions & 0 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,18 @@ function onPanelDrop(event: DragEvent): void {
event.preventDefault()
void attachment.addFiles(files)
}

function onPanelPaste(event: ClipboardEvent): void {
const target = event.target
if (!(target instanceof Element) || !target.closest('#agent-composer')) return

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: The summary frames this as pasting images "instead of letting the canvas create a Load Image node" — was the canvas actually claiming these pastes?

The document-level handler in usePaste.ts:200 bails immediately via shouldIgnoreCopyPaste(e.target), and that helper returns true for target.isContentEditable (eventHelpers.ts:68-72). With focus in the composer the paste target is the ProseMirror contenteditable, so the canvas path should already have been declining it before this PR.

If that's right, the change is purely additive — it adds attaching, rather than winning a conflict — and the framing in the description (and in the E2E step 3, "verify no Load Image node is added") describes a race that wasn't happening. Not a code problem either way; I'd just rather the recorded motivation match the mechanism, since the next person to touch paste ownership will read this description first.

If you have seen a Load Image node appear from a composer paste, that's more interesting than the fix — it would mean shouldIgnoreCopyPaste is missing the composer in some state, and that gap would still be open for non-attachable files, which this handler returns early on without claiming.

const files = Array.from(event.clipboardData?.files ?? []).filter(
isAgentAttachable
)
if (files.length === 0) return
event.preventDefault()
event.stopPropagation()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: In the capture phase this stops the event before the composer's own editor ever sees it. Is dropping the accompanying text intended when the clipboard carries both?

The composer is a ProseMirror contenteditable (Composer.vue → InlinePromptEditor.vue), so @paste.capture + stopPropagation() means ProseMirror's paste handling is bypassed entirely whenever clipboardData.files has at least one attachable entry. For an image-only paste that's exactly right.

The case I'm unsure about is a mixed payload. Copying a cell range out of Sheets or Excel puts both a rendered image and text/html/text/plain on the clipboard; same for some rich editors. After this change, pasting that into the composer silently attaches a PNG of the selection and inserts none of the text — where today the user gets the text. Both behaviors are defensible, but the current one is implicit.

If attaching is the intent, no change needed beyond maybe a note here. If text should still land when it's present, gating on files.length > 0 && !event.clipboardData?.getData('text/plain') — or letting the event through and only attaching — would preserve it.

void attachment.addFiles(files)
}
</script>

<template>
Expand All @@ -1272,6 +1284,7 @@ function onPanelDrop(event: DragEvent): void {
@dragleave="onPanelDragLeave"
@dragover="onPanelDragOver"
@drop="onPanelDrop"
@paste.capture="onPanelPaste"
>
<input
ref="fileInput"
Expand Down
Loading