From 9a719ca8bf5e7626db42fc5ccf3cb9ca9a03fc1f Mon Sep 17 00:00:00 2001 From: ryan-di Date: Mon, 5 Oct 2026 11:21:31 +0800 Subject: [PATCH 01/28] feat(agent): stage assets separately from inline prompt references --- .../fixtures/components/AgentPanel.ts | 29 +- .../tests/agent/agentAttachmentUpload.spec.ts | 10 +- .../agent/agentBulkAttachmentComposer.spec.ts | 5 + .../tests/agent/agentInlineReferences.spec.ts | 49 +- browser_tests/tests/agent/agentPanel.spec.ts | 10 +- ...ndependent-assets-and-prompt-references.md | 46 ++ docs/adr/README.md | 1 + src/locales/en/main.json | 2 + src/platform/assets/utils/assetDragUtil.ts | 2 +- .../extensions/agent/AgentPanelRoot.test.ts | 355 ++++++++-- .../extensions/agent/AgentPanelRoot.vue | 33 +- .../agent/components/agent/AgentPanel.test.ts | 2 +- .../agent/components/agent/AgentPanel.vue | 2 +- .../agent/Composer.assetTray.test.ts | 616 ++++++++++++++++++ .../agent/Composer.inlineReferences.test.ts | 72 +- .../agent/components/agent/Composer.test.ts | 236 ++++--- .../agent/components/agent/Composer.vue | 126 ++-- .../agent/Composer.workflowClipboard.test.ts | 47 +- .../agent/composer/AssetHoverPreview.vue | 35 + .../agent/composer/AssetThumbnail.vue | 57 ++ .../components/agent/composer/AssetTray.vue | 117 ++++ .../agent/composer/AttachmentChip.test.ts | 21 +- .../agent/composer/AttachmentChip.vue | 77 +-- .../agent/composer/InlineAssetReference.vue | 61 ++ .../agent/composer/InlinePromptEditor.vue | 133 +++- .../composer/inlineReferenceChipStyles.ts | 20 + .../agent/useAgentDraftSubmission.test.ts | 38 +- .../agent/useAgentDraftSubmission.ts | 35 +- .../agent/useAgentMentionPicker.ts | 30 +- .../composables/agent/useAttachment.test.ts | 2 +- .../agent/useAttachment.transport.test.ts | 2 +- .../agent/composables/agent/useAttachment.ts | 2 +- .../composables/agent/useComposer.test.ts | 18 +- .../agent/composables/agent/useComposer.ts | 18 +- .../stores/agent/agentComposerStore.test.ts | 72 +- .../agent/stores/agent/agentComposerStore.ts | 137 ++-- .../agent/types/composerAttachment.ts | 7 + .../extensions/agent/types/composerPrompt.ts | 2 +- 38 files changed, 1982 insertions(+), 545 deletions(-) create mode 100644 docs/adr/AGENT-COMPOSER-0040-independent-assets-and-prompt-references.md create mode 100644 src/workbench/extensions/agent/components/agent/Composer.assetTray.test.ts create mode 100644 src/workbench/extensions/agent/components/agent/composer/AssetHoverPreview.vue create mode 100644 src/workbench/extensions/agent/components/agent/composer/AssetThumbnail.vue create mode 100644 src/workbench/extensions/agent/components/agent/composer/AssetTray.vue create mode 100644 src/workbench/extensions/agent/components/agent/composer/InlineAssetReference.vue create mode 100644 src/workbench/extensions/agent/components/agent/composer/inlineReferenceChipStyles.ts create mode 100644 src/workbench/extensions/agent/types/composerAttachment.ts diff --git a/browser_tests/fixtures/components/AgentPanel.ts b/browser_tests/fixtures/components/AgentPanel.ts index 1f613987e4e..cb998aef251 100644 --- a/browser_tests/fixtures/components/AgentPanel.ts +++ b/browser_tests/fixtures/components/AgentPanel.ts @@ -21,6 +21,7 @@ export class AgentPanel { public readonly workflowPicker: Locator public readonly fileInput: Locator public readonly composerAssetSection: Locator + public readonly scrollAssetsRight: Locator public readonly attachmentChips: Locator public readonly composer: Locator public readonly composerPromptArea: Locator @@ -64,6 +65,9 @@ export class AgentPanel { }) this.fileInput = this.root.getByTestId('agent-file-input') this.composerAssetSection = this.root.getByTestId('composer-asset-section') + this.scrollAssetsRight = this.root.getByRole('button', { + name: enMessages.g.scrollRight + }) this.attachmentChips = this.root.getByTestId('agent-attachment-chip') this.composer = this.root.getByRole('textbox', { name: /^Describe ideas/ }) this.composerPromptArea = this.root.getByTestId('composer-inline-input') @@ -86,13 +90,36 @@ export class AgentPanel { .getByRole('listitem') } + async scrollAssetsToEnd(): Promise { + const count = await this.attachmentChips.count() + for ( + let step = 0; + step < count && (await this.scrollAssetsRight.isEnabled()); + step++ + ) { + const target = await this.composerAssetSection.evaluate((element) => + Math.min( + element.scrollWidth - element.clientWidth, + element.scrollLeft + element.clientWidth + ) + ) + await this.scrollAssetsRight.click() + await expect + .poll(() => + this.composerAssetSection.evaluate((element) => element.scrollLeft) + ) + .toBeCloseTo(target, 0) + } + await expect(this.scrollAssetsRight).toBeDisabled() + } + activityRow(label: string): Locator { return this.activityRows.getByText(label, { exact: true }) } /** * The composer attachment carrying `name`. Matches on the chip's own - * attribute rather than its text, which truncates at `max-w-32`. + * attribute, including while the visible filename is hidden. * * `name` is a filename and may legitimately contain a quote or backslash, so * it is escaped for the double-quoted CSS string rather than interpolated diff --git a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts index f59fee1e7e4..a71501b0416 100644 --- a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts +++ b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts @@ -40,7 +40,9 @@ test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => { }) await uploadReceived await expect( - panel.getByTestId('composer-asset-section').getByText('pending.mp4') + panel + .getByTestId('composer-asset-section') + .getByRole('group', { name: 'pending.mp4' }) ).toBeVisible() await expect(send).toBeDisabled() @@ -51,9 +53,9 @@ test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => { await openButton.click() await expect(composer).toHaveText('Keep this draft') - await expect(panel.getByText('pending.mp4', { exact: true })).toHaveCount( - 0 - ) + await expect( + panel.getByRole('group', { name: 'pending.mp4' }) + ).toHaveCount(0) await expect(send).toBeEnabled() } finally { releaseUpload() diff --git a/browser_tests/tests/agent/agentBulkAttachmentComposer.spec.ts b/browser_tests/tests/agent/agentBulkAttachmentComposer.spec.ts index 75b5cef06f3..ad762f1f23f 100644 --- a/browser_tests/tests/agent/agentBulkAttachmentComposer.spec.ts +++ b/browser_tests/tests/agent/agentBulkAttachmentComposer.spec.ts @@ -41,6 +41,11 @@ test.describe('Agent composer bulk attachments', { tag: '@cloud' }, () => { ) await expect(agentPanel.attachmentChips).toHaveCount(ATTACHMENT_COUNT) + await expect(agentPanel.attachmentChips.last()).not.toBeInViewport({ + ratio: 1 + }) + await agentPanel.scrollAssetsToEnd() + await expect(agentPanel.attachmentChips.last()).toBeInViewport({ ratio: 1 }) await expect(agentPanel.sendButton).toBeEnabled() // ratio: 1 rather than the default ratio: 0 — a Send button clipped down to // a sliver is the FE-3202 symptom, and it satisfies ratio: 0. diff --git a/browser_tests/tests/agent/agentInlineReferences.spec.ts b/browser_tests/tests/agent/agentInlineReferences.spec.ts index 7deb81ebb51..e492b9dddab 100644 --- a/browser_tests/tests/agent/agentInlineReferences.spec.ts +++ b/browser_tests/tests/agent/agentInlineReferences.spec.ts @@ -16,7 +16,7 @@ test.use({ }) test( - 'keeps inline node and asset removal synchronized with the upper rows and Undo', + 'keeps asset attachments in the tray while inline references follow prompt editing and Undo', { tag: ['@cloud', '@ui'] }, async ({ page, workflowSelection, assetUpload }) => { await page.locator('#comfy-file-input').setInputFiles({ @@ -42,39 +42,50 @@ test( await panel .getByTestId('agent-file-input') .setInputFiles(assetPath('test_upload_image.png')) - const text = 'Adjust Color balance #12 to match test_upload_image.png ' - await expect(editor).toHaveText(text) + const tray = panel.getByTestId('composer-asset-section') + await expect( + tray.getByRole('group', { name: 'test_upload_image.png' }) + ).toBeVisible() + await expect(tray).not.toContainText('test_upload_image.png') + await expect(editor).toHaveText('Adjust Color balance #12 to match ') + await expect(panel.getByTestId('composer-node-section')).toHaveCount(0) await expect( panel.getByRole('button', { name: enMessages.agent.send, exact: true }) ).toBeDisabled() + await editor.pressSequentially('@test_upload') await panel - .getByTestId('composer-asset-section') - .getByRole('button', { name: enMessages.agent.remove, exact: true }) + .getByRole('menuitem', { name: 'test_upload_image.png', exact: true }) .click() - assetUpload.finish() - await expect(editor.getByTestId('asset-reference-chip')).toHaveCount(0) - await editor.press('ControlOrMeta+z') + const text = 'Adjust Color balance #12 to match test_upload_image.png ' await expect(editor).toHaveText(text) + assetUpload.finish() await expect( panel.getByRole('button', { name: enMessages.agent.send, exact: true }) ).toBeEnabled() + await editor.press('Backspace') + await editor.press('Backspace') + await expect(editor.getByTestId('asset-reference-chip')).toHaveCount(0) + await expect( + tray.getByRole('group', { name: 'test_upload_image.png' }) + ).toBeVisible() + await editor.press('ControlOrMeta+z') + await expect(editor.getByTestId('asset-reference-chip')).toHaveCount(1) + await editor.getByTestId('node-reference-chip').hover() await panel .getByRole('button', { name: 'Remove Color balance #12 reference' }) .click() - await expect(editor).toHaveText('Adjust to match test_upload_image.png ') + await expect(editor.getByTestId('node-reference-chip')).toHaveCount(0) await expect(panel.getByTestId('composer-node-section')).toHaveCount(0) await editor.press('ControlOrMeta+z') - await expect(editor).toHaveText(text) - await expect(panel.getByTestId('composer-node-section')).toHaveText( - 'Color balance' - ) + await expect(editor.getByTestId('node-reference-chip')).toHaveCount(1) + await expect(panel.getByTestId('composer-node-section')).toHaveCount(0) await editor.press('ControlOrMeta+a') await editor.press('ControlOrMeta+c') await expect .poll(() => page.evaluate(() => navigator.clipboard.readText())) .toBe( - 'Adjust @[Node: Color balance #12] to match @[Image: test_upload_image.png] ' + 'Adjust @[Node: Color balance #12] to match @[Image: test_upload_image.png]' ) await editor.press('ArrowRight') await page @@ -96,11 +107,19 @@ test( await editor.press('Backspace') await editor.press('ControlOrMeta+v') await expect(editor).toHaveText( - 'Adjust @[Node: Color balance #12] to match @[Image: test_upload_image.png] ' + 'Adjust @[Node: Color balance #12] to match @[Image: test_upload_image.png]' ) await expect(editor.getByTestId('node-reference-chip')).toHaveCount(0) await expect(editor.getByTestId('asset-reference-chip')).toHaveCount(0) await expect(panel.getByTestId('composer-node-section')).toHaveCount(0) + await expect( + tray.getByRole('group', { name: 'test_upload_image.png' }) + ).toBeVisible() + await tray.getByRole('group', { name: 'test_upload_image.png' }).hover() + await panel + .getByTestId('composer-asset-section') + .getByRole('button', { name: enMessages.agent.remove, exact: true }) + .click() await expect(panel.getByTestId('composer-asset-section')).toHaveCount(0) await expect( panel.getByRole('button', { name: enMessages.agent.switchWorkflow }) diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index c59d9351b0a..46d74e83c28 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -621,7 +621,9 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { }) expect((await uploadResponse).ok()).toBe(true) await expect( - panel.getByTestId('composer-asset-section').getByText('movie.mp4') + panel + .getByTestId('composer-asset-section') + .getByRole('group', { name: 'movie.mp4' }) ).toBeVisible() await expect.poll(() => uploadCount).toBe(1) @@ -633,9 +635,9 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { await expect( page.getByText('too-large.mp4 is larger than 24 MB') ).toBeVisible() - await expect(panel.getByText('too-large.mp4', { exact: true })).toHaveCount( - 0 - ) + await expect( + panel.getByRole('group', { name: 'too-large.mp4' }) + ).toHaveCount(0) await expect.poll(() => uploadCount).toBe(1) }) diff --git a/docs/adr/AGENT-COMPOSER-0040-independent-assets-and-prompt-references.md b/docs/adr/AGENT-COMPOSER-0040-independent-assets-and-prompt-references.md new file mode 100644 index 00000000000..67cc764e172 --- /dev/null +++ b/docs/adr/AGENT-COMPOSER-0040-independent-assets-and-prompt-references.md @@ -0,0 +1,46 @@ +# ADR-AGENT-COMPOSER-0040: Independent Assets and Prompt References + +Date: 2026-10-05 + +## Status + +Proposed + +## Context + +An asset added to the agent composer must be sent even without an inline +mention. Users may mention one asset repeatedly, delete or undo an occurrence, +and continue typing while an upload or send completes. Deriving attachments +from prompt references makes deletion remove the asset from the request and +makes repeated mentions duplicate it. + +## Decision + +The existing composer store owns the ordered included asset IDs and asset +metadata. Prompt references identify occurrences at text positions. Adding an +asset stages it; mentioning an included asset inserts an occurrence. Editor +history may restore occurrences only for assets still included in the tray. +Removing a tray item retires its identity and removes every occurrence; an +explicit new drop gets a fresh identity. + +Send snapshots retain both the included assets and the prompt. An untouched +failed draft restores both; newer user input wins. Local preview resources +remain owned by the store while included or held by a pending snapshot, and are +released when no longer retained. UI hover/focus highlighting and native scroll +measurements remain local to components and never change the draft. + +## Alternatives considered + +- Continue deriving attachments from references: cannot express an unmentioned + included asset or repeated references without changing send semantics. +- Keep a separate tray in a component: loses assets across remount and makes + upload completion, Undo and failed-send recovery depend on mounted UI. +- Create another persistent asset store: duplicates the existing composer's + submission lifecycle without a separate sharing requirement. + +## Consequences + +Attachment metadata is resolved through one owner when applying editor history. +Removing an inline occurrence keeps the included file. Send payloads contain +one entry per included asset while preserving every inline mention. Callers +stage and reference assets through distinct store commands. diff --git a/docs/adr/README.md b/docs/adr/README.md index a39c07929ea..0be818643d7 100644 --- a/docs/adr/README.md +++ b/docs/adr/README.md @@ -11,6 +11,7 @@ An Architecture Decision Record captures an important architectural decision mad | ADR | Title | Status | Date | | ---------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ---------- | ---------- | | [AGENT-BINDING-0035](AGENT-BINDING-0035-document-identity-gates-persisted-workflow-tab-bindings.md) | Document Identity Gates Persisted Workflow Tab Bindings | Proposed | 2026-09-19 | +| [AGENT-COMPOSER-0040](AGENT-COMPOSER-0040-independent-assets-and-prompt-references.md) | Independent Assets and Prompt References | Proposed | 2026-10-05 | | [AGENT-CONTEXT-0028](AGENT-CONTEXT-0028-separate-workflow-references-from-editor-tabs.md) | Separate Workflow References from Editor Tabs | Proposed | 2026-09-07 | | [AGENT-HISTORY-0038](AGENT-HISTORY-0038-wait-for-chat-and-workflow-readiness.md) | Wait for Chat and Workflow Readiness | Proposed | 2026-09-25 | | [AGENT-OBSERVABILITY-0034](AGENT-OBSERVABILITY-0034-frontend-journey-effect-contract.md) | Frontend Journey Effect Contract | Proposed | 2026-09-17 | diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 44f24aceff7..ee4390c1441 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -5505,7 +5505,9 @@ "focusNode": "Show on canvas", "focusNodeLabel": "Show {node} on canvas", "removeNodeLabel": "Remove {node} reference", + "removeAssetReference": "Remove {name} reference", "uploading": "Uploading", + "uploadingAttachments": "Uploading {count} attachment | Uploading {count} attachments", "noNodesToMention": "No nodes in this workflow", "friend": "there", "sendFailed": "Message failed to send", diff --git a/src/platform/assets/utils/assetDragUtil.ts b/src/platform/assets/utils/assetDragUtil.ts index 9487c359402..d7184304162 100644 --- a/src/platform/assets/utils/assetDragUtil.ts +++ b/src/platform/assets/utils/assetDragUtil.ts @@ -57,7 +57,7 @@ export function startAssetDrag( : { filename: asset.name, type: getAssetType(asset.tags), - display_name: asset.display_name + display_name: asset.display_name ?? undefined }), attachment_ref: getAssetUrlFilename(asset), media_kind: mediaKind, diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index f4913ee5d81..69481241407 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -54,6 +54,8 @@ import { useWorkflowTabActivityStore } from '@/stores/workflowTabActivityStore' import { useSidebarTabStore } from '@/stores/workspace/sidebarTabStore' import { useToastStore } from '@/platform/updates/common/toastStore' import { useAssetsStore } from '@/stores/assetsStore' +import type { AssetItem } from '@/platform/assets/schemas/assetSchema' +import { startAssetDrag } from '@/platform/assets/utils/assetDragUtil' import { getFilenameDetails } from '@/utils/formatUtil' import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore' import { StorageKeys } from '@/platform/workflow/persistence/base/storageKeys' @@ -2111,6 +2113,26 @@ function fileOfSize(name: string, size: number, type: string): File { return file } +function assetPanelDrag(displayName: string | null) { + const asset = { + id: 'library-source', + name: 'library.png', + hash: 'stored-library.png', + display_name: displayName, + tags: ['input'], + created_at: '2026-10-03T00:00:00Z', + updated_at: '2026-10-03T00:00:00Z' + } satisfies AssetItem + const dataTransfer = new DataTransfer() + const event = new DragEvent('dragstart', { cancelable: true }) + Object.defineProperty(event, 'dataTransfer', { value: dataTransfer }) + startAssetDrag(event, asset) + return { + types: Array.from(dataTransfer.types), + getData: (type: string) => dataTransfer.getData(type) + } +} + // DES-527 replaced the paperclip and @ buttons with a single + menu, so every // attach or node-mention gesture now starts by opening it. async function openAddMenu(): Promise { @@ -2356,6 +2378,109 @@ describe('AgentPanelRoot attach flow', () => { useAgentWorkflowTabBindingStore().bind('wf-42', tab.path) }) + it.for([ + { displayName: null, label: 'library.png' }, + { displayName: 'My library image', label: 'My library image' } + ])( + 'stages an assets-panel drop with display name $displayName from the real producer payload', + async ({ displayName, label }) => { + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + const store = useAgentComposerStore() + store.setText('Keep this draft') + await nextTick() + const target = screen.getByRole('button', { + name: i18n.global.t('agent.close') + }) + const data = assetPanelDrag(displayName) + + expect(dispatchDrag(target, 'dragover', data)).toBe(true) + expect(dispatchDrag(target, 'drop', data)).toBe(true) + expect( + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: label } + ) + ).toBeInTheDocument() + expect(store.attachments).toEqual([ + expect.objectContaining({ + name: label, + ref: 'stored-library.png', + previewUrl: 'http://localhost:3000/api/assets/library-source/content' + }) + ]) + expect(store.prompt).toEqual({ text: 'Keep this draft', references: [] }) + expect(uploaded).toEqual([]) + } + ) + + it('allows a removed library asset to be explicitly dropped again while retaining retirement of its old identity', async () => { + stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + const store = useAgentComposerStore() + const target = screen.getByRole('textbox') + const data = assetPanelDrag('library.png') + expect(dispatchDrag(target, 'drop', data)).toBe(true) + const trayItem = await screen.findByRole('group', { name: 'library.png' }) + const oldAttachment = store.attachments[0] + assert.exists(oldAttachment) + store.referenceAttachment(oldAttachment.id) + const oldPrompt = store.prompt + await userEvent.hover(trayItem) + await userEvent.click( + within(trayItem).getByRole('button', { + name: i18n.global.t('agent.remove') + }) + ) + expect(store.attachments).toEqual([]) + + expect(dispatchDrag(target, 'drop', data)).toBe(true) + await screen.findByRole('group', { name: 'library.png' }) + const newAttachment = store.attachments[0] + assert.exists(newAttachment) + expect(newAttachment.id).not.toBe(oldAttachment.id) + store.updateAttachment(oldAttachment.id, { ref: 'too-late.png' }) + store.applyEditorPrompt(oldPrompt) + expect(store.attachments).toEqual([newAttachment]) + expect(newAttachment.ref).toBe('stored-library.png') + expect(store.prompt.references).toEqual([]) + expect(dispatchDrag(target, 'drop', data)).toBe(true) + expect(store.attachments).toHaveLength(1) + }) + + it('allows the same library asset to be dropped into a new draft after sending', async () => { + vi.stubGlobal( + 'fetch', + vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => + init?.method === 'POST' + ? json(202, { thread_id: 'th-1', message_id: 'm-1' }) + : json(200, agentThreadList()) + ) + ) + renderWithSelectedTarget() + await nextTick() + const store = useAgentComposerStore() + const target = screen.getByRole('textbox') + const data = assetPanelDrag('library.png') + expect(dispatchDrag(target, 'drop', data)).toBe(true) + await screen.findByRole('group', { name: 'library.png' }) + const previousId = store.attachments[0]?.id + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + expect(store.attachments).toEqual([]) + + expect(dispatchDrag(target, 'drop', data)).toBe(true) + await screen.findByRole('group', { name: 'library.png' }) + expect(store.attachments).toEqual([ + expect.objectContaining({ + name: 'library.png', + ref: 'stored-library.png' + }) + ]) + expect(store.attachments[0]?.id).not.toBe(previousId) + expect(store.prompt.references).toEqual([]) + }) + it('uploads a picked file, stages its ref, and forwards it on the next send', async () => { const messageBodies: unknown[] = [] const fetchMock = vi.fn(async (url: string, init?: RequestInit) => { @@ -2391,8 +2516,9 @@ describe('AgentPanelRoot attach flow', () => { await userEvent.upload(input, file) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) ).toBeInTheDocument() @@ -2402,7 +2528,7 @@ describe('AgentPanelRoot attach flow', () => { expect(messageBodies).toHaveLength(1) expect(messageBodies[0]).toMatchObject({ - content: '@[Image: cat.png] make it pop', + content: 'make it pop', attachments: ['uploaded_cat.png'] }) expect(useTelemetry()!.trackAgentMessageSent).toHaveBeenCalledWith({ @@ -2509,8 +2635,9 @@ describe('AgentPanelRoot attach flow', () => { ) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'movie.mp4' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'movie.mp4' } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual(['movie.mp4'])) @@ -2592,7 +2719,9 @@ describe('AgentPanelRoot attach flow', () => { detail: 'movie.mp4 is larger than 24 MB' }) ) - expect(screen.queryByText('movie.mp4')).not.toBeInTheDocument() + expect( + screen.queryByRole('group', { name: 'movie.mp4' }) + ).not.toBeInTheDocument() expect(telemetry.trackAgentAttachButtonClicked).not.toHaveBeenCalled() }) @@ -2606,8 +2735,9 @@ describe('AgentPanelRoot attach flow', () => { dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [image] }) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'huge.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'huge.png' } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual(['huge.png'])) @@ -2625,8 +2755,9 @@ describe('AgentPanelRoot attach flow', () => { ).toBe(true) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'movie.mp4' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'movie.mp4' } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual(['movie.mp4'])) @@ -2657,8 +2788,9 @@ describe('AgentPanelRoot attach flow', () => { ).toBe(true) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - name + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: name } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual([name])) @@ -2680,8 +2812,9 @@ describe('AgentPanelRoot attach flow', () => { await userEvent.paste(clipboard) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'image.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'image.png' } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual(['image.png'])) @@ -2701,8 +2834,9 @@ describe('AgentPanelRoot attach flow', () => { await userEvent.paste(clipboard) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'image.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'image.png' } ) ).toBeInTheDocument() expect(screen.getByRole('textbox')).toHaveTextContent( @@ -2887,7 +3021,7 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) }) - it('lets a removed upload finish without reattaching until Undo', async () => { + it('keeps a removed tray upload retired after completion and stale editor history', async () => { const signals: AbortSignal[] = [] let finishUpload: (response: Response) => void = () => {} const upload = new Promise((resolve) => { @@ -2926,13 +3060,7 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) expect(composer.attachments).toEqual([]) composer.applyEditorPrompt(prompt) - expect(composer.attachments).toEqual([ - expect.objectContaining({ - name: 'cat.png', - ref: 'uploaded-cat.png', - uploading: false - }) - ]) + expect(composer.attachments).toEqual([]) }) it('uses the server limit for audio rejection copy', async () => { @@ -2968,26 +3096,51 @@ describe('AgentPanelRoot attach flow', () => { ) }) - it('shows the asset drop target during a trusted drag and clears it on leave', async () => { - stubUploadFetch() - renderWithSelectedTarget() - await nextTick() - const target = screen.getByRole('textbox') - const data = { - types: ['application/x-comfy-asset-info', 'text/uri-list'] - } + it.for([{ withAsset: false }, { withAsset: true }])( + 'shows panel-wide drop feedback across child transitions while preserving the composer (tray=$withAsset)', + async ({ withAsset }) => { + stubUploadFetch() + renderWithSelectedTarget() + const store = useAgentComposerStore() + store.setText('Keep this draft') + if (withAsset) + store.addAttachment({ id: 'kept', name: 'kept.png', ref: 'kept.png' }) + await nextTick() + const prompt = store.prompt + const header = screen.getByRole('button', { + name: i18n.global.t('agent.close') + }) + const composer = screen.getByTestId('composer-input-box') + const data = { + types: ['application/x-comfy-asset-info', 'text/uri-list'] + } + expect(screen.queryByRole('status')).not.toBeInTheDocument() - dispatchDrag(target, 'dragenter', data) - await nextTick() + dispatchDrag(header, 'dragenter', data) + await nextTick() - const dropTarget = screen.getByRole('status') - expect(dropTarget).toHaveTextContent('Drag and drop assets here') + const dropTarget = screen.getByRole('status') + expect(dropTarget).toHaveTextContent('Drag and drop assets here') + expect(composer).not.toContainElement(dropTarget) + expect(screen.getByRole('textbox')).toHaveTextContent('Keep this draft') + expect(store.prompt).toEqual(prompt) + if (withAsset) + expect( + screen.getByRole('group', { name: 'kept.png' }) + ).toBeInTheDocument() - dispatchDrag(dropTarget, 'dragleave', data) - await nextTick() + dispatchDrag(composer, 'dragenter', data) + dispatchDrag(header, 'dragleave', data) + await nextTick() + expect(screen.getByRole('status')).toBe(dropTarget) - expect(screen.queryByRole('status')).not.toBeInTheDocument() - }) + dispatchDrag(composer, 'dragleave', data) + await nextTick() + expect(screen.queryByRole('status')).not.toBeInTheDocument() + expect(store.prompt).toEqual(prompt) + expect(store.attachments).toHaveLength(withAsset ? 1 : 0) + } + ) it('rejects URI-only drags without showing or claiming the asset target', async () => { const uploaded = stubUploadFetch() @@ -3067,8 +3220,9 @@ describe('AgentPanelRoot attach flow', () => { expect(dropTarget).not.toBeInTheDocument() expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - filename + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: filename } ) ).toBeInTheDocument() @@ -3078,7 +3232,7 @@ describe('AgentPanelRoot attach flow', () => { expect(messageBodies).toHaveLength(1) expect(messageBodies[0]).toMatchObject({ - content: `@[${mime === 'image/png' ? 'Image' : 'Video'}: ${filename}] describe this`, + content: 'describe this', attachments: [`uploaded_${filename}`] }) } @@ -3128,13 +3282,15 @@ describe('AgentPanelRoot attach flow', () => { expect(dispatchDrag(target, 'drop', dragData)).toBe(true) expect(dispatchDrag(target, 'drop', dragData)).toBe(true) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - filename + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: filename } ) ).toBeInTheDocument() expect( - within(screen.getByTestId('composer-asset-section')).getAllByText( - filename + within(screen.getByTestId('composer-asset-section')).getAllByRole( + 'group', + { name: filename } ) ).toHaveLength(1) expect( @@ -3197,8 +3353,9 @@ describe('AgentPanelRoot attach flow', () => { expect(dispatchDrag(target, 'drop', dragData)).toBe(true) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'gen.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'gen.png' } ) ).toBeInTheDocument() expect( @@ -3262,19 +3419,24 @@ describe('AgentPanelRoot attach flow', () => { stubUploadFetch() renderWithSelectedTarget() await nextTick() - const target = screen.getByRole('textbox') + const target = screen.getByRole('button', { + name: i18n.global.t('agent.close') + }) const workflow = new File(['{}'], 'flow.json', { type: 'application/json' }) expect(dispatchDrag(target, 'drop', { files: [workflow] })).toBe(false) - expect(screen.queryByText('flow.json')).not.toBeInTheDocument() + expect( + screen.queryByRole('group', { name: 'flow.json' }) + ).not.toBeInTheDocument() const asset = new File(['x'], 'cat.png', { type: 'image/png' }) expect(dispatchDrag(target, 'drop', { files: [asset] })).toBe(true) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) ).toBeInTheDocument() }) @@ -3295,8 +3457,9 @@ describe('AgentPanelRoot attach flow', () => { dispatchDrag(screen.getByRole('textbox'), 'drop', { files }) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) ).toBeInTheDocument() await vi.waitFor(() => expect(uploaded).toEqual(['cat.png'])) @@ -3399,7 +3562,9 @@ describe('AgentPanelRoot attach flow', () => { // The rejected upload's chip leaves; only a committed one stays settled. await vi.waitFor(() => - expect(screen.queryByText('bad.png')).not.toBeInTheDocument() + expect( + screen.queryByRole('group', { name: 'bad.png' }) + ).not.toBeInTheDocument() ) await vi.waitFor(() => expect( @@ -3412,8 +3577,9 @@ describe('AgentPanelRoot attach flow', () => { expect(telemetry.trackAgentAttachButtonClicked).not.toHaveBeenCalled() } else { expect( - within(screen.getByTestId('composer-asset-section')).getByText( - 'good.png' + within(screen.getByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'good.png' } ) ).toBeInTheDocument() await vi.waitFor(() => @@ -3454,8 +3620,9 @@ describe('AgentPanelRoot attach flow', () => { ) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) ).toBeInTheDocument() expect( @@ -3506,8 +3673,9 @@ describe('AgentPanelRoot attach flow', () => { screen.getByTestId('agent-file-input'), file ) - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) await sendFromComposer('second message') @@ -3582,8 +3750,9 @@ describe('AgentPanelRoot attach flow', () => { file ) expect( - within(await screen.findByTestId('composer-asset-section')).getByText( - 'cat.png' + within(await screen.findByTestId('composer-asset-section')).getByRole( + 'group', + { name: 'cat.png' } ) ).toBeInTheDocument() @@ -3591,7 +3760,9 @@ describe('AgentPanelRoot attach flow', () => { vi.mocked(reportError).mockClear() failUpload() await vi.waitFor(() => - expect(screen.queryByText('cat.png')).not.toBeInTheDocument() + expect( + screen.queryByRole('group', { name: 'cat.png' }) + ).not.toBeInTheDocument() ) expect(revoke).toHaveBeenCalledTimes(1) // A rejected file is the user's to fix; raising the server-error overlay @@ -3631,7 +3802,7 @@ describe('AgentPanelRoot attach flow', () => { revoke.mockRestore() }) - it('keeps a dismissed durable preview available for Undo until the editor unmounts', async () => { + it('keeps a durable tray preview through inline deletion, Undo and unmount', async () => { const revoke = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => {}) vi.stubGlobal( 'fetch', @@ -3661,7 +3832,9 @@ describe('AgentPanelRoot attach flow', () => { file ) const assetSection = await screen.findByTestId('composer-asset-section') - expect(within(assetSection).getByText('cat.png')).toBeInTheDocument() + expect( + within(assetSection).getByRole('group', { name: 'cat.png' }) + ).toBeInTheDocument() await waitFor(() => expect( within(assetSection).getByRole('img', { name: 'cat.png' }) @@ -3669,10 +3842,17 @@ describe('AgentPanelRoot attach flow', () => { ) expect(revoke).toHaveBeenCalledExactlyOnceWith('blob:mock-url') - await userEvent.click( - screen.getByRole('button', { name: i18n.global.t('agent.remove') }) - ) - expect(screen.queryByText('cat.png')).not.toBeInTheDocument() + const composer = useAgentComposerStore() + composer.referenceAttachment(composer.attachments[0].id) + await screen.findByTestId('asset-reference-chip') + await userEvent.click(screen.getByRole('textbox')) + await userEvent.keyboard('{Control>}a{/Control}{Backspace}') + expect(screen.queryByTestId('asset-reference-chip')).not.toBeInTheDocument() + expect( + within(screen.getByTestId('composer-asset-section')).getByRole('group', { + name: 'cat.png' + }) + ).toBeVisible() screen.getByRole('textbox').focus() await userEvent.keyboard('{Control>}z{/Control}') expect( @@ -3681,6 +3861,9 @@ describe('AgentPanelRoot attach flow', () => { { name: 'cat.png' } ) ).toHaveAttribute('src', '/api/view?filename=uploaded_cat.png&type=input') + expect(screen.getByTestId('asset-reference-chip')).toHaveTextContent( + 'cat.png' + ) expect(revoke).toHaveBeenCalledExactlyOnceWith('blob:mock-url') view.unmount() expect(revoke).toHaveBeenCalledTimes(1) @@ -9917,6 +10100,11 @@ describe('AgentPanelRoot workflow binding', () => { ] }) composer.setNodes([{ id: '12', title: 'KSampler' }]) + composer.setInsertionPoint({ + textOffset: composer.draft.length, + referenceIndex: composer.prompt.references.length + }) + composer.referenceAttachment('upload-1') const originalDraft = composer.draft const originalReferences = [...composer.workflowReferences] await userEvent.click(screen.getByRole('button', { name: 'Send' })) @@ -10232,6 +10420,31 @@ describe('AgentPanelRoot workflow binding', () => { expect([...state.selectedItems]).toEqual([rootTwin]) }) + it('lists target nodes loaded after opening the reference menu', async () => { + makeTab('wf-42') + const state = setupNodeSelectionCanvas() + showRootGraph(state) + renderWithSelectedTarget() + useAgentPanelStore().isOpen = true + + const textbox = screen.getByRole('textbox') + await userEvent.click(textbox) + await userEvent.paste('Use @') + showRootGraph(state, state.nodes) + await userEvent.click(screen.getByRole('menuitem', { name: 'Nodes' })) + + expect(screen.getByRole('menuitem', { name: 'VAE Decode' })).toBeVisible() + await userEvent.click(screen.getByRole('menuitem', { name: 'KSampler' })) + expect(useAgentComposerStore().nodes).toEqual([ + { id: '12', locatorId: '12', title: 'KSampler' } + ]) + await userEvent.paste('next') + expect(useAgentComposerStore().draft).toBe('Use next') + expect(screen.getByTestId('node-reference-chip')).toHaveTextContent( + 'KSampler' + ) + }) + it('excludes referenced nodes from the mention picker', async () => { makeTab() setupNodeSelectionCanvas() diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 7a7f0f817ae..ef162a7c217 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -11,7 +11,6 @@ import { onBeforeUnmount, onMounted, provide, - readonly, ref, watch } from 'vue' @@ -1570,11 +1569,9 @@ function onNewChat(source?: 'new_chat_button' | 'history_delete'): void { else agentPanelStore.startFollowingVisibleWorkflow() } -const panelRef = ref>() const fileInput = ref() const assetDragActive = ref(false) let assetDragDepth = 0 -provide('agentAssetDragActive', readonly(assetDragActive)) let selectingNodes = false let nodeSelectionCanvas: LGraphCanvas | undefined @@ -1805,14 +1802,14 @@ async function attachDroppedAsset(event: DragEvent): Promise { } if (asset.ref && asset.kind !== 'other') { - return ( - panelRef.value?.addAttachment({ - id: `asset:${asset.ref}`, - name: asset.name, - ref: asset.ref, - previewUrl: asset.previewUrl - }) ?? false - ) + if (composerStore.attachments.some((item) => item.ref === asset.ref)) + return false + return composerStore.addAttachment({ + id: `asset:${crypto.randomUUID()}`, + name: asset.name, + ref: asset.ref, + previewUrl: asset.previewUrl + }) } const result = await attachment.addDeferredFile(asset.name, async () => { @@ -1860,7 +1857,7 @@ async function onPanelDrop(event: DragEvent): Promise {
{ @change="onFilesPicked" /> { +
+
{ }) import { i18n } from '@/i18n' -import type { ComposerAttachment } from '../../composables/agent/useComposer' +import type { ComposerAttachment } from '../../types/composerAttachment' import { toTurnId } from '../../schemas/agentApiSchema' import type { WorkflowReference } from '../../types/workflowReference' import type { AgentStarterPromptAttribution } from '../../utils/starterPrompts' diff --git a/src/workbench/extensions/agent/components/agent/AgentPanel.vue b/src/workbench/extensions/agent/components/agent/AgentPanel.vue index b049581d0e7..a5da059f33f 100644 --- a/src/workbench/extensions/agent/components/agent/AgentPanel.vue +++ b/src/workbench/extensions/agent/components/agent/AgentPanel.vue @@ -28,7 +28,7 @@ import type { WorkflowReferenceOption } from '../../types/workflowReference' import type { TurnId } from '../../schemas/agentApiSchema' -import type { ComposerAttachment } from '../../composables/agent/useComposer' +import type { ComposerAttachment } from '../../types/composerAttachment' import type { SelectedNode } from '../../composables/agent/useCanvasSelection' import { DEFAULT_AGENT_PAYWALL_PRESENTATION } from '@/workbench/extensions/agent/services/agent/agentPaywallPresentation' import type { diff --git a/src/workbench/extensions/agent/components/agent/Composer.assetTray.test.ts b/src/workbench/extensions/agent/components/agent/Composer.assetTray.test.ts new file mode 100644 index 00000000000..9dad4487e4c --- /dev/null +++ b/src/workbench/extensions/agent/components/agent/Composer.assetTray.test.ts @@ -0,0 +1,616 @@ +import userEvent from '@testing-library/user-event' +import { + fireEvent, + render, + screen, + waitFor, + within +} from '@testing-library/vue' +import { describe, expect, it, vi } from 'vitest' +import { defineComponent, h, nextTick } from 'vue' + +import { i18n } from '@/i18n' + +import { useAgentComposerStore } from '../../stores/agent/agentComposerStore' +import Composer from './Composer.vue' +import { setupInlinePromptEditorDom } from './composer/inlinePromptEditorTestSetup' + +setupInlinePromptEditorDom() + +const asset = { + id: 'source', + name: 'source.png', + ref: 'uploaded-source.png', + previewUrl: 'https://example.com/source.png' +} + +function setup() { + const store = useAgentComposerStore() + store.setNodeScope('workflow-a') + const send = vi.fn() + const Host = defineComponent({ + setup: () => () => + h(Composer, { + hasWorkflowTarget: true, + selectionTags: store.nodes, + onSend: send + }) + }) + const view = render(Host, { + global: { plugins: [i18n], directives: { tooltip: () => {} } } + }) + return { ...view, store, send, editor: screen.getByRole('textbox') } +} + +function trackResizes() { + const callbacks = new Map void>>() + vi.stubGlobal( + 'ResizeObserver', + class implements ResizeObserver { + private targets = new Set() + private notify: () => void + constructor(callback: ResizeObserverCallback) { + this.notify = () => callback([], this) + } + observe(target: Element) { + this.targets.add(target) + const listeners = callbacks.get(target) ?? new Set<() => void>() + listeners.add(this.notify) + callbacks.set(target, listeners) + } + unobserve(target: Element) { + callbacks.get(target)?.delete(this.notify) + this.targets.delete(target) + } + disconnect() { + for (const target of this.targets) this.unobserve(target) + } + } + ) + return (target: Element) => + callbacks.get(target)?.forEach((notify) => notify()) +} + +function setTrayGeometry( + tray: HTMLElement, + width: number, + contentWidth: number +) { + const dimensions = { width, contentWidth } + let left = 0 + Object.defineProperties(tray, { + clientWidth: { get: () => dimensions.width }, + scrollWidth: { get: () => dimensions.contentWidth }, + scrollLeft: { + get: () => { + left = Math.min( + left, + Math.max(0, dimensions.contentWidth - dimensions.width) + ) + return left + }, + set: (value: number) => { + left = value + } + }, + scrollTo: { + value: (options: ScrollToOptions) => { + tray.scrollLeft = options.left ?? tray.scrollLeft + tray.dispatchEvent(new Event('scroll')) + } + } + }) + return dimensions +} + +describe('composer asset tray', () => { + it('announces all pending uploads outside the scroll viewport and preserves the draft on Enter', async () => { + const user = userEvent.setup() + const { store, editor, send } = setup() + const status = screen.getByTestId('composer-upload-status') + expect(status).toBeEmptyDOMElement() + expect(status).toHaveAttribute('aria-live', 'polite') + expect(status).toHaveAttribute('aria-atomic', 'true') + store.addAttachment({ + id: 'first', + name: 'first.png', + ref: '', + uploading: true + }) + store.addAttachment({ + id: 'second', + name: 'second.png', + ref: '', + uploading: true + }) + await user.type(editor, 'Keep my draft') + expect(status).toHaveTextContent('Uploading 2 attachments') + expect( + within(screen.getByTestId('composer-asset-section')).queryByTestId( + 'composer-upload-status' + ) + ).not.toBeInTheDocument() + expect(screen.getByRole('button', { name: 'Send' })).toBeDisabled() + await user.keyboard('{Enter}') + expect(send).not.toHaveBeenCalled() + expect(store.draft).toBe('Keep my draft') + store.updateAttachment('first', { ref: 'first.png', uploading: false }) + await waitFor(() => + expect(status).toHaveTextContent('Uploading 1 attachment') + ) + store.removeAttachment('second') + await waitFor(() => expect(status).toBeEmptyDOMElement()) + expect(screen.getByTestId('composer-upload-status')).toBe(status) + expect(screen.getByRole('button', { name: 'Send' })).toBeEnabled() + await user.keyboard('{Enter}') + expect(send).toHaveBeenCalledExactlyOnceWith('Keep my draft', [ + expect.objectContaining({ id: 'first', ref: 'first.png' }) + ]) + }) + + it('offers bounded page scrolling without changing the draft or included assets', async () => { + const resize = trackResizes() + const { store, editor } = setup() + const assets = Array.from({ length: 8 }, (_, index) => ({ + id: `asset-${index}`, + name: `asset-${index}.png`, + ref: `asset-${index}.png` + })) + store.replaceDraft({ + text: '', + attachments: assets, + workflowReferences: [] + }) + await userEvent.type(editor, 'Keep this draft') + const tray = screen.getByTestId('composer-asset-section') + setTrayGeometry(tray, 250, 720) + resize(tray) + await fireEvent.scroll(tray) + + const previous = screen.getByRole('button', { name: 'Scroll Left' }) + const next = screen.getByRole('button', { name: 'Scroll Right' }) + expect(previous).toBeDisabled() + expect(next).toBeEnabled() + await userEvent.click(next) + expect(tray.scrollLeft).toBe(250) + expect(previous).toBeEnabled() + expect(next).toBeEnabled() + await userEvent.keyboard('{Enter}') + expect(tray.scrollLeft).toBe(470) + expect(next).toBeDisabled() + await userEvent.click(previous) + expect(tray.scrollLeft).toBe(220) + expect(next).toBeEnabled() + expect(store.draft).toBe('Keep this draft') + expect(store.attachments).toEqual(assets) + }) + + it('updates overflow controls when the tray is resized', async () => { + const resize = trackResizes() + const { store } = setup() + store.addAttachment(asset) + store.addAttachment({ id: 'other', name: 'other.png', ref: 'other.png' }) + await nextTick() + const tray = screen.getByTestId('composer-asset-section') + const dimensions = setTrayGeometry(tray, 240, 192) + resize(tray) + await nextTick() + expect(screen.queryByRole('button', { name: 'Scroll Right' })).toBeNull() + + dimensions.width = 160 + resize(tray) + expect( + await screen.findByRole('button', { name: 'Scroll Right' }) + ).toBeEnabled() + + dimensions.width = 240 + resize(tray) + await nextTick() + expect(screen.queryByRole('button', { name: 'Scroll Right' })).toBeNull() + expect(screen.queryByRole('button', { name: 'Scroll Left' })).toBeNull() + }) + + it('clears overflow controls when removing assets makes the tray fit', async () => { + const resize = trackResizes() + const { store } = setup() + store.addAttachment(asset) + store.addAttachment({ id: 'other', name: 'other.png', ref: 'other.png' }) + await nextTick() + const tray = screen.getByTestId('composer-asset-section') + const dimensions = setTrayGeometry(tray, 160, 192) + resize(tray) + expect( + await screen.findByRole('button', { name: 'Scroll Right' }) + ).toBeEnabled() + + await userEvent.click(screen.getByRole('button', { name: 'Scroll Right' })) + expect(tray.scrollLeft).toBe(32) + + dimensions.contentWidth = 104 + await userEvent.click( + within(screen.getByRole('group', { name: 'other.png' })).getByRole( + 'button', + { name: 'Remove' } + ) + ) + + await waitFor(() => + expect(screen.queryByRole('button', { name: 'Scroll Right' })).toBeNull() + ) + expect(store.attachments).toEqual([asset]) + + dimensions.contentWidth = 192 + store.addAttachment({ id: 'new', name: 'new.png', ref: 'new.png' }) + expect( + await screen.findByRole('button', { name: 'Scroll Right' }) + ).toBeEnabled() + expect(screen.getByRole('button', { name: 'Scroll Left' })).toBeDisabled() + store.removeAttachment('new') + store.removeAttachment(asset.id) + await nextTick() + expect(screen.queryByTestId('composer-asset-section')).toBeNull() + }) + + it('stages assets without changing the prompt, and sends unmentioned assets', async () => { + const { store, editor, send } = setup() + await userEvent.type(editor, 'Inspect these images') + store.addAttachment(asset) + await screen.findByRole('img', { name: asset.name }) + + expect(editor).toHaveTextContent('Inspect these images') + expect(screen.queryByTestId('asset-reference-chip')).not.toBeInTheDocument() + expect(store.draft).toBe('Inspect these images') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + expect(send).toHaveBeenCalledWith('Inspect these images', [asset]) + }) + + it.for([ + { + via: 'pointer', + pick: () => + userEvent.click(screen.getByRole('menuitem', { name: asset.name })) + }, + { via: 'keyboard', pick: () => userEvent.keyboard('{Enter}') } + ])( + 'inserts a matching asset at the caret via $via and retains the tray', + async ({ pick }) => { + const { store, editor, send } = setup() + store.addAttachment(asset) + store.addAttachment({ id: 'other', name: 'other.png', ref: 'other.png' }) + await userEvent.type(editor, 'Use @source') + const menu = screen.getByRole('menu', { name: 'Add to prompt' }) + expect( + within(menu).queryByRole('menuitem', { name: 'other.png' }) + ).toBeNull() + await pick() + await screen.findByTestId('asset-reference-chip') + expect(editor.textContent).toBe('Use source.png ') + expect(store.attachments).toEqual([ + asset, + { id: 'other', name: 'other.png', ref: 'other.png' } + ]) + expect( + within(screen.getByTestId('composer-asset-section')).getByRole( + 'group', + { name: asset.name } + ) + ).toBeVisible() + await userEvent.keyboard('as the subject') + expect(editor.textContent).toBe('Use source.png as the subject') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + expect(send).toHaveBeenCalledWith( + 'Use @[Image: source.png] as the subject', + [asset, { id: 'other', name: 'other.png', ref: 'other.png' }] + ) + } + ) + + it('keeps included assets through inline deletion, Undo and remount', async () => { + const { store, editor, unmount } = setup() + store.addAttachment(asset) + await userEvent.type(editor, 'Use @source') + await userEvent.keyboard('{Enter}{Backspace}{Backspace}') + await waitFor(() => + expect( + screen.queryByTestId('asset-reference-chip') + ).not.toBeInTheDocument() + ) + expect(store.attachments).toEqual([asset]) + await userEvent.keyboard('{Control>}z{/Control}') + await screen.findByTestId('asset-reference-chip') + expect(store.attachments).toEqual([asset]) + unmount() + setup() + expect( + within(screen.getByTestId('composer-asset-section')).getByRole('group', { + name: asset.name + }) + ).toBeVisible() + expect(screen.getByTestId('asset-reference-chip')).toHaveTextContent( + asset.name + ) + }) + + it('allows repeated asset mentions, deletes only one, and sends the attachment once', async () => { + const { store, editor, send } = setup() + store.addAttachment(asset) + await userEvent.type(editor, 'Use @source') + await userEvent.keyboard('{Enter}') + await userEvent.keyboard('then @source') + expect(screen.getByRole('menuitem', { name: asset.name })).toBeVisible() + await userEvent.keyboard('{Enter}') + await userEvent.keyboard('and @source') + await userEvent.keyboard('{Enter}') + await waitFor(() => + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(3) + ) + expect(editor.textContent).toBe( + 'Use source.png then source.png and source.png ' + ) + + await userEvent.keyboard('{Backspace}{Backspace}') + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(2) + expect(store.attachments).toEqual([asset]) + await userEvent.keyboard('{Control>}z{/Control}') + await waitFor(() => + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(3) + ) + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + expect(send).toHaveBeenCalledWith( + 'Use @[Image: source.png] then @[Image: source.png] and @[Image: source.png]', + [asset] + ) + }) + + it('removes only the chosen inline asset with its hover button, retains the tray, and supports Undo and continued typing', async () => { + const user = userEvent.setup() + const { store, editor } = setup() + store.addAttachment(asset) + await user.type(editor, 'Use @source') + await user.keyboard('{Enter}then @source{Enter}after') + const references = await screen.findAllByTestId('asset-reference-chip') + expect(references).toHaveLength(2) + await user.hover(references[0]) + const trayItem = within( + screen.getByTestId('composer-asset-section') + ).getByRole('group', { name: asset.name }) + expect(trayItem).toHaveAttribute('data-highlighted', 'true') + await user.click( + within(references[0]).getByRole('button', { + name: 'Remove source.png reference' + }) + ) + + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(1) + expect(editor.textContent).toBe('Use then source.png after') + expect(store.attachments).toEqual([asset]) + expect(editor).toHaveFocus() + expect(trayItem).not.toHaveAttribute('data-highlighted') + await user.keyboard('{Control>}z{/Control}') + await waitFor(() => + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(2) + ) + expect(editor.textContent).toBe('Use source.png then source.png after') + expect(trayItem).not.toHaveAttribute('data-highlighted') + await user.keyboard('continued ') + expect(editor).toHaveTextContent('continued') + expect(store.attachments).toEqual([asset]) + }) + + it('keeps typing uninterrupted after asset insertion and highlights its tray item on inline hover without opening a preview', async () => { + vi.useFakeTimers() + const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }) + const { store, editor } = setup() + store.addAttachment(asset) + await user.type(editor, 'Use @source') + await user.keyboard('{Enter}') + const chip = screen.getByTestId('asset-reference-chip') + + chip.dispatchEvent( + new PointerEvent('pointerenter', { pointerType: 'mouse' }) + ) + await nextTick() + await vi.advanceTimersByTimeAsync(300) + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + expect(editor).toHaveFocus() + await user.keyboard('and keep typing') + expect(editor.textContent).toBe('Use source.png and keep typing') + + await user.hover(chip) + await vi.advanceTimersByTimeAsync(300) + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + const trayItem = within( + screen.getByTestId('composer-asset-section') + ).getByRole('group', { name: asset.name }) + expect(trayItem).toHaveAttribute('data-highlighted', 'true') + expect(editor).toHaveFocus() + + await user.unhover(chip) + expect(trayItem).not.toHaveAttribute('data-highlighted') + expect(store.attachments).toEqual([asset]) + }) + + it('highlights the tray while an inline asset has focus or hover, without opening a preview', async () => { + vi.useFakeTimers() + const user = userEvent.setup({ advanceTimers: vi.advanceTimersByTime }) + const { store, editor } = setup() + store.addAttachment(asset) + await user.type(editor, '@source') + await user.keyboard('{Enter}') + + await user.tab() + const chip = screen.getByTestId('asset-reference-chip') + expect(chip).toHaveFocus() + await vi.advanceTimersByTimeAsync(300) + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + const trayItem = within( + screen.getByTestId('composer-asset-section') + ).getByRole('group', { name: asset.name }) + expect(trayItem).toHaveAttribute('data-highlighted', 'true') + + await user.hover(chip) + await user.tab({ shift: true }) + expect(editor).toHaveFocus() + expect(trayItem).toHaveAttribute('data-highlighted', 'true') + await user.unhover(chip) + expect(trayItem).not.toHaveAttribute('data-highlighted') + }) + + it('maps repeated inline references to their tray asset and clears only the activity that ended', async () => { + const user = userEvent.setup() + const { store, editor } = setup() + const other = { id: 'other', name: 'other.png', ref: 'other.png' } + store.addAttachment(asset) + store.addAttachment(other) + await user.type(editor, '@source') + await user.keyboard('{Enter}then @source{Enter}and @other{Enter}') + const references = screen.getAllByTestId('asset-reference-chip') + expect(references).toHaveLength(3) + const tray = within(screen.getByTestId('composer-asset-section')) + const sourceItem = tray.getByRole('group', { name: asset.name }) + const otherItem = tray.getByRole('group', { name: other.name }) + + await user.tab() + expect(references[0]).toHaveFocus() + await user.hover(references[1]) + expect(sourceItem).toHaveAttribute('data-highlighted', 'true') + expect(otherItem).not.toHaveAttribute('data-highlighted') + await user.unhover(references[1]) + expect(sourceItem).toHaveAttribute('data-highlighted', 'true') + + await user.hover(references[2]) + expect(sourceItem).toHaveAttribute('data-highlighted', 'true') + expect(otherItem).toHaveAttribute('data-highlighted', 'true') + await user.unhover(references[2]) + expect(sourceItem).toHaveAttribute('data-highlighted', 'true') + expect(otherItem).not.toHaveAttribute('data-highlighted') + await user.click(editor) + expect(sourceItem).not.toHaveAttribute('data-highlighted') + expect(store.attachments).toEqual([asset, other]) + }) + + it('retains the highlight from a hovered occurrence when another focused occurrence is removed', async () => { + const user = userEvent.setup() + const { store, editor } = setup() + store.addAttachment(asset) + await user.type(editor, '@source') + await user.keyboard('{Enter}then @source{Enter}') + const references = screen.getAllByTestId('asset-reference-chip') + const trayItem = within( + screen.getByTestId('composer-asset-section') + ).getByRole('group', { name: asset.name }) + + await user.tab() + expect(references[0]).toHaveFocus() + await user.hover(references[1]) + await user.tab() + expect( + within(references[0]).getByRole('button', { + name: 'Remove source.png reference' + }) + ).toHaveFocus() + await user.keyboard('{Enter}') + + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(1) + expect(editor).toHaveFocus() + expect(trayItem).toHaveAttribute('data-highlighted', 'true') + await user.unhover(references[1]) + expect(trayItem).not.toHaveAttribute('data-highlighted') + expect(store.attachments).toEqual([asset]) + }) + + it('opens a large asset preview from tray hover and closes it on leave or unmount', async () => { + const user = userEvent.setup() + const { store, editor, unmount } = setup() + store.addAttachment(asset) + await user.type(editor, '@source') + await user.keyboard('{Enter}') + await screen.findByTestId('asset-reference-chip') + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + + const trigger = screen.getByTestId('agent-attachment-chip') + await user.hover(trigger) + const preview = await screen.findByRole('tooltip', { name: asset.name }) + expect( + within(preview).getByRole('img', { name: asset.name }) + ).toHaveAttribute('src', asset.previewUrl) + await user.unhover(trigger) + await user.pointer({ + target: screen.getByRole('button', { name: 'Send' }), + coords: { clientX: 500, clientY: 500 } + }) + await waitFor(() => + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + ) + + await user.hover(trigger) + await screen.findByRole('tooltip', { name: asset.name }) + unmount() + expect( + screen.queryByRole('tooltip', { name: asset.name }) + ).not.toBeInTheDocument() + }) + + it('inserts an asset in the middle of a sentence and preserves the following text', async () => { + const { store, editor } = setup() + store.addAttachment(asset) + await userEvent.type(editor, 'Use as background') + await userEvent.keyboard('{Control>}a{/Control}{ArrowLeft}{ArrowRight>4/}') + await userEvent.paste('@source') + await userEvent.keyboard('{Enter}') + + await screen.findByTestId('asset-reference-chip') + expect(editor.textContent).toBe('Use source.png as background') + expect(store.prompt.text).toBe('Use as background') + }) + + it('retires a tray asset and its inline reference despite late upload completion or Undo', async () => { + const { store, editor } = setup() + store.addAttachment({ ...asset, uploading: true, ref: '' }) + await userEvent.type(editor, 'Use @source') + await userEvent.keyboard('{Enter}') + await userEvent.keyboard('and @source') + await userEvent.keyboard('{Enter}') + await waitFor(() => + expect(screen.getAllByTestId('asset-reference-chip')).toHaveLength(2) + ) + await userEvent.click( + within(screen.getByTestId('composer-asset-section')).getByRole('button', { + name: 'Remove' + }) + ) + store.updateAttachment(asset.id, { uploading: false, ref: asset.ref }) + await waitFor(() => + expect( + screen.queryByTestId('composer-asset-section') + ).not.toBeInTheDocument() + ) + await userEvent.click(editor) + await userEvent.keyboard('{Control>}z{/Control}') + expect(store.attachments).toEqual([]) + expect(screen.queryByTestId('asset-reference-chip')).not.toBeInTheDocument() + }) + + it('shows nodes only inline without adding an asset', async () => { + const { store } = setup() + store.setNodes([{ id: '361', title: 'Save Image' }]) + await screen.findByTestId('node-reference-chip') + expect( + screen.queryByTestId('composer-node-section') + ).not.toBeInTheDocument() + expect( + screen.queryByTestId('composer-asset-section') + ).not.toBeInTheDocument() + expect(screen.getByRole('textbox')).toHaveTextContent('Save Image #361') + }) +}) diff --git a/src/workbench/extensions/agent/components/agent/Composer.inlineReferences.test.ts b/src/workbench/extensions/agent/components/agent/Composer.inlineReferences.test.ts index d736c5ce6ab..64e149e44d6 100644 --- a/src/workbench/extensions/agent/components/agent/Composer.inlineReferences.test.ts +++ b/src/workbench/extensions/agent/components/agent/Composer.inlineReferences.test.ts @@ -45,59 +45,28 @@ function renderComposer() { describe('inline node and asset references', () => { beforeEach(() => vi.useRealTimers()) - it('keeps upper-row removal and Undo synchronized with inline references', async () => { + it('keeps inline node deletion and Undo synchronized with selected context', async () => { const { store, selected, editor, send } = renderComposer() await userEvent.type(editor, 'Use ') selected.value = [{ id: '12', title: 'KSampler' }] - await waitFor(() => expect(editor.textContent).toBe('Use KSampler #12 ')) - await userEvent.keyboard('with ') + await screen.findByTestId('node-reference-chip') + await userEvent.keyboard('{Backspace}{Backspace}') + expect(store.nodes).toEqual([]) + expect(editor.textContent).toBe('Use ') + await userEvent.keyboard('{Control>}z{/Control}') + await screen.findByTestId('node-reference-chip') + expect(store.nodes).toEqual([{ id: '12', title: 'KSampler' }]) + expect( + screen.queryByTestId('composer-node-section') + ).not.toBeInTheDocument() + await userEvent.keyboard(' with ') store.addAttachment({ id: 'image', name: 'source.png', ref: 'uploaded.png' }) - await waitFor(() => - expect(editor.textContent).toBe('Use KSampler #12 with source.png ') - ) - - await userEvent.click( - screen.getByRole('button', { name: 'Remove KSampler #12 reference' }) - ) - await waitFor(() => - expect( - screen.queryByTestId('composer-node-section') - ).not.toBeInTheDocument() - ) - expect(editor.textContent).toBe('Use with source.png ') - expect(store.nodes).toEqual([]) - editor.focus() - await userEvent.keyboard('{Control>}z{/Control}') - await waitFor(() => - expect(screen.getByTestId('composer-node-section')).toHaveTextContent( - 'KSampler' - ) - ) - expect(editor.textContent).toBe('Use KSampler #12 with source.png ') - - await userEvent.click( - within(screen.getByTestId('composer-asset-section')).getByRole('button', { - name: 'Remove' - }) - ) - await waitFor(() => - expect( - screen.queryByTestId('composer-asset-section') - ).not.toBeInTheDocument() - ) - expect(editor.textContent).toBe('Use KSampler #12 with ') - editor.focus() - await userEvent.keyboard('{Control>}z{/Control}') - await waitFor(() => - expect(screen.getByTestId('composer-asset-section')).toHaveTextContent( - 'source.png' - ) - ) - expect(editor.textContent).toBe('Use KSampler #12 with source.png ') + store.referenceAttachment('image') + await screen.findByTestId('asset-reference-chip') await userEvent.click(screen.getByRole('button', { name: 'Send' })) expect(send).toHaveBeenCalledWith( 'Use @[Node: KSampler #12] with @[Image: source.png]', @@ -114,6 +83,7 @@ describe('inline node and asset references', () => { ref: '', uploading: true }) + store.referenceAttachment('image') await screen.findByTestId('asset-reference-chip') await userEvent.click( within(screen.getByTestId('composer-asset-section')).getByRole('button', { @@ -131,15 +101,8 @@ describe('inline node and asset references', () => { ).not.toBeInTheDocument() editor.focus() await userEvent.keyboard('{Control>}z{/Control}') - await screen.findByTestId('asset-reference-chip') - expect(store.attachments).toEqual([ - { - id: 'image', - name: 'pending.png', - ref: 'complete.png', - uploading: false - } - ]) + expect(screen.queryByTestId('asset-reference-chip')).not.toBeInTheDocument() + expect(store.attachments).toEqual([]) }) it('deletes a node from the sentence without restaging the selected canvas node', async () => { @@ -166,6 +129,7 @@ describe('inline node and asset references', () => { selected.value = [{ id: '12', title: 'KSampler' }] await screen.findByTestId('node-reference-chip') store.addAttachment({ id: 'image', name: 'source.png', ref: 'source.png' }) + store.referenceAttachment('image') await screen.findByTestId('asset-reference-chip') await user.keyboard('{Control>}a{/Control}') const clipboard = await user.copy() diff --git a/src/workbench/extensions/agent/components/agent/Composer.test.ts b/src/workbench/extensions/agent/components/agent/Composer.test.ts index b37bcafd6f5..edfe9cd9d20 100644 --- a/src/workbench/extensions/agent/components/agent/Composer.test.ts +++ b/src/workbench/extensions/agent/components/agent/Composer.test.ts @@ -202,16 +202,33 @@ describe('Composer', () => { ).toBeTruthy() }) - it('hides the empty-composer hint once typing begins', async () => { - mount() - const box = screen.getByRole('textbox') + it.for([{ withAsset: false }, { withAsset: true }])( + 'hides the hint while typing and restores it after clearing (tray=$withAsset)', + async ({ withAsset }) => { + const store = useAgentComposerStore() + if (withAsset) + store.addAttachment({ id: 'asset', name: 'cat.png', ref: 'cat.png' }) + const { emitted } = mount() + const box = screen.getByRole('textbox') + expect( + screen.getByRole('button', { name: 'mention nodes' }) + ).toBeVisible() - await userEvent.click(box) - await userEvent.paste('hello') + await userEvent.click(box) + await userEvent.paste('hello') - expect(useAgentComposerStore().draft).toBe('hello') - expect(screen.queryByRole('button', { name: 'mention nodes' })).toBeNull() - }) + expect(store.draft).toBe('hello') + expect(screen.queryByRole('button', { name: 'mention nodes' })).toBeNull() + + await userEvent.keyboard('{Control>}a{/Control}{Backspace}') + expect(store.draft).toBe('') + await userEvent.click( + screen.getByRole('button', { name: 'mention nodes' }) + ) + expect(emitted().selectNodes).toHaveLength(1) + expect(store.attachments).toHaveLength(withAsset ? 1 : 0) + } + ) it('enters graph selection mode from the empty-composer hint', async () => { const getMentionNodes = vi.fn(() => []) @@ -910,6 +927,40 @@ describe('Composer', () => { ]) }) + it('loads current nodes when entering or re-entering the Nodes submenu', async () => { + let nodes: typeof NODES = [] + mount({ getMentionNodes: () => nodes }) + await openReferenceRoot() + + nodes = [NODES[0]] + await userEvent.click(screen.getByRole('menuitem', { name: 'Nodes' })) + expect(screen.getByRole('menuitem', { name: 'KSampler' })).toBeVisible() + + await userEvent.click(screen.getByRole('menuitem', { name: 'Back' })) + nodes = [NODES[2]] + await userEvent.click(screen.getByRole('menuitem', { name: 'Nodes' })) + expect(screen.getByRole('menuitem', { name: 'VAE Decode' })).toBeVisible() + expect(screen.queryByRole('menuitem', { name: 'KSampler' })).toBeNull() + expect(useAgentComposerStore().draft).toBe('@') + }) + + it('filters current nodes after the graph changes with the submenu open', async () => { + let nodes = [NODES[0]] + const { emitted } = mount({ getMentionNodes: () => nodes }) + await openReferenceSection('Nodes') + + nodes = [NODES[2]] + await userEvent.click(screen.getByRole('textbox')) + await userEvent.paste('vae') + + expect(screen.getByRole('menuitem', { name: 'VAE Decode' })).toBeVisible() + expect(screen.queryByRole('menuitem', { name: 'KSampler' })).toBeNull() + expect(useAgentComposerStore().draft).toBe('@vae') + await userEvent.keyboard('{Enter}') + expect(emitted().mentionPick).toEqual([[NODES[2]]]) + expect(emitted().send).toBeUndefined() + }) + // Re-picking a staged node is a no-op, so it drops out of the list. it('hides nodes already in the basket', async () => { mount({ @@ -926,11 +977,11 @@ describe('Composer', () => { expect(labels).toEqual(['Back', 'KSampler #7', 'VAE Decode']) }) - // The staged node is only hidden from the picker, not from the duplicate - // check - its chip must still show the id that tells it apart from the - // same-titled node still in the graph. it('keeps disambiguating a staged node against its graph twin', async () => { const twin = { id: '11', title: NODES[0].title } + const store = useAgentComposerStore() + store.setNodeScope('workflow-a') + store.setNodes([NODES[0]]) mount({ getMentionNodes: () => [...NODES, twin], selectionTags: [NODES[0]] @@ -938,7 +989,12 @@ describe('Composer', () => { await openReferenceSection('Nodes') - expect(screen.getByText(`#${NODES[0].id}`)).toBeInTheDocument() + expect(screen.getByTestId('node-reference-chip')).toHaveTextContent( + 'KSampler #5' + ) + expect( + screen.queryByTestId('composer-node-section') + ).not.toBeInTheDocument() }) it('keeps type-to-filter behavior inside the selected reference type', async () => { @@ -1334,7 +1390,10 @@ describe('Composer', () => { it.for(['pointer', 'Enter', 'Space'])( 'opens a staged workflow with %s without consuming the draft', async (interaction) => { - useAgentComposerStore().setText('Keep this prompt') + const store = useAgentComposerStore() + store.setText('Keep this prompt') + store.setNodeScope('workflow-a') + store.setNodes([{ id: '5', title: 'KSampler' }]) const { emitted } = mount({ workflowReferences: [ { id: 'wf-1', name: 'Water world', textOffset: 0 } @@ -1351,9 +1410,10 @@ describe('Composer', () => { expect(emitted().openReferenceWorkflow).toEqual([['wf-1', 'Water world']]) expect(emitted().removeWorkflowReference).toBeUndefined() expect(emitted().send).toBeUndefined() - expect(useAgentComposerStore().draft).toBe('Keep this prompt') - expect(screen.getByTestId('composer-node-section')).toHaveTextContent( - 'KSampler' + expect(store.draft).toBe('Keep this prompt ') + expect(store.nodes).toEqual([{ id: '5', title: 'KSampler' }]) + expect(screen.getByTestId('node-reference-chip')).toHaveTextContent( + 'KSampler #5' ) } ) @@ -1517,20 +1577,20 @@ describe('Composer', () => { expect(useAgentComposerStore().draft).toBe('tex') }) - it('keeps selected nodes in a dedicated section above the inline prompt', () => { + it('keeps selected nodes inline alongside workflow references', () => { + const store = useAgentComposerStore() + store.setNodeScope('workflow-a') + store.setNodes([{ id: '5', title: 'KSampler' }]) mount({ - selectionTags: [{ id: '5', title: 'KSampler' }], + selectionTags: store.nodes, workflowReferences: [{ id: 'wf-1', name: 'Water world', textOffset: 0 }] }) - - const nodeSection = screen.getByTestId('composer-node-section') - const inlineInput = screen.getByTestId('composer-inline-input') - - expect(nodeSection).toHaveClass('border-b', 'p-3') - expect(nodeSection).toHaveTextContent('KSampler') - expect(nodeSection).not.toContainElement(screen.getByRole('textbox')) - expect(inlineInput).not.toHaveTextContent('KSampler') - expect(inlineInput).toHaveTextContent('Water world') + expect( + screen.queryByTestId('composer-node-section') + ).not.toBeInTheDocument() + expect(screen.getByRole('textbox')).toHaveTextContent( + 'Water worldKSampler #5' + ) }) it('keeps added assets in a separate padded section above the prompt', async () => { @@ -1583,9 +1643,11 @@ describe('Composer', () => { screen.getByRole('button', { name: 'Open Water world' }) ) expect(onOpenReferenceWorkflow).toHaveBeenCalledWith('wf-1', 'Water world') - expect(screen.getByTestId('composer-asset-section')).toHaveTextContent( - 'cat.png' - ) + expect( + within(screen.getByTestId('composer-asset-section')).getByRole('group', { + name: 'cat.png' + }) + ).toBeVisible() }) it('keeps the empty prompt hint visible when only nodes are selected', () => { @@ -1645,81 +1707,51 @@ describe('Composer', () => { expect(getMentionNodes).not.toHaveBeenCalled() }) - it('hides the id on a uniquely named selection chip', () => { - mount({ selectionTags: [{ id: '5', title: 'KSampler' }] }) - - expect(screen.getByText('KSampler')).toBeInTheDocument() - expect(screen.queryByText('#5')).not.toBeInTheDocument() - }) - - it('emits removeTag when a selection chip is removed', async () => { - const { emitted } = mount({ - selectionTags: [{ id: '5', title: 'KSampler' }] - }) - - await userEvent.click( - screen.getByRole('button', { name: 'Remove KSampler #5 reference' }) - ) - - expect(emitted().removeTag).toEqual([['5']]) - }) - - it('builds the remove tooltip for a selection chip', () => { - mount({ selectionTags: [{ id: '5', title: 'KSampler' }] }) - - const removeButton = screen.getByRole('button', { - name: 'Remove KSampler #5 reference' - }) - expect(tooltipBindings.get(removeButton)).toMatchObject({ value: 'Remove' }) - }) - - it('renders a selection chip label as non-interactive context', () => { - mount({ selectionTags: [{ id: '5', title: 'KSampler' }] }) - - expect(screen.getByText('KSampler')).toBeVisible() - expect( - screen.queryByRole('button', { name: 'Show KSampler #5 on canvas' }) - ).toBeNull() - }) - - // The remove button sits outside the focus trigger; removing a chip must not - // also fly the canvas to the node being removed. - it('removes a selection chip without focusing its node', async () => { - const { emitted } = mount({ - selectionTags: [{ id: '5', title: 'KSampler' }] - }) - - await userEvent.click( - screen.getByRole('button', { name: 'Remove KSampler #5 reference' }) - ) - - expect(emitted().removeTag).toEqual([['5']]) - expect(emitted().focusTag).toBeUndefined() - }) - - it('shows the id on a lone selection chip with a graph title twin', () => { - const selected = { id: '5', title: 'KSampler' } - mount({ - selectionTags: [selected], - getMentionNodes: () => [selected, { id: '7', title: 'KSampler' }] - }) - - expect(screen.getByText('KSampler')).toBeInTheDocument() - expect(screen.getByText('#5')).toBeInTheDocument() - expect(screen.queryByText('#7')).not.toBeInTheDocument() - }) - - it('shows ids when selection chips have duplicate titles', () => { - mount({ - selectionTags: [ + it.for([ + { + label: 'unique title', + nodes: [{ id: '5', title: 'KSampler' }], + labels: ['KSampler #5'] + }, + { + label: 'duplicate titles', + nodes: [ { id: '5', title: 'KSampler' }, { id: '7', title: 'KSampler' } - ] - }) + ], + labels: ['KSampler #5', 'KSampler #7'] + } + ])( + 'identifies inline nodes with $label by their IDs', + ({ nodes, labels }) => { + const store = useAgentComposerStore() + store.setNodeScope('workflow-a') + store.setNodes(nodes) + mount({ selectionTags: store.nodes }) + expect( + screen + .getAllByTestId('node-reference-chip') + .map((chip) => chip.textContent) + ).toEqual(labels) + expect( + screen.queryByTestId('composer-node-section') + ).not.toBeInTheDocument() + expect( + screen.queryByRole('button', { name: 'Show KSampler #5 on canvas' }) + ).toBeNull() + } + ) - expect(screen.getAllByText('KSampler')).toHaveLength(2) - expect(screen.getByText('#5')).toBeInTheDocument() - expect(screen.getByText('#7')).toBeInTheDocument() + it('removes an inline node without focusing its canvas node', async () => { + const store = useAgentComposerStore() + store.setNodeScope('workflow-a') + store.setNodes([{ id: '5', title: 'KSampler' }]) + const { emitted } = mount({ selectionTags: store.nodes }) + await userEvent.click(screen.getByRole('textbox')) + await userEvent.keyboard('{Control>}a{/Control}{Backspace}') + expect(emitted().removeTag).toEqual([['5']]) + expect(emitted().focusTag).toBeUndefined() + expect(store.nodes).toEqual([]) }) it('renders an attachment preview and removes it from the composer', async () => { diff --git a/src/workbench/extensions/agent/components/agent/Composer.vue b/src/workbench/extensions/agent/components/agent/Composer.vue index 1e3c9472935..f468471f66b 100644 --- a/src/workbench/extensions/agent/components/agent/Composer.vue +++ b/src/workbench/extensions/agent/components/agent/Composer.vue @@ -12,7 +12,6 @@ import { } from 'reka-ui' import { computed, - inject, nextTick, onMounted, onUnmounted, @@ -20,11 +19,9 @@ import { useTemplateRef, watch } from 'vue' -import type { Ref } from 'vue' import { useI18n } from 'vue-i18n' import Button from '@/components/ui/button/Button.vue' -import Tag from '@/components/chip/Tag.vue' import AccessibleTooltip from '@/components/ui/tooltip/AccessibleTooltip.vue' import { buildTooltipConfig } from '@/composables/useTooltipConfig' import { registerEscapeOverride } from '@/platform/keybindings/escapeOverride' @@ -35,10 +32,9 @@ import { composerPromptForSend } from '../../utils/composerPrompt' import type { AgentStarterPromptAttribution } from '../../utils/starterPrompts' import { useAgentMentionPicker } from '../../composables/agent/useAgentMentionPicker' import { useWorkflowReferencePicker } from '../../composables/agent/useWorkflowReferencePicker' -import type { ComposerAttachment } from '../../composables/agent/useComposer' +import type { ComposerAttachment } from '../../types/composerAttachment' import { useComposer } from '../../composables/agent/useComposer' import type { SelectedNode } from '../../composables/agent/useCanvasSelection' -import { selectedNodeKey } from '../../composables/agent/useCanvasSelection' import type { PromptSnapshot, WorkflowReference, @@ -47,7 +43,8 @@ import type { } from '../../types/workflowReference' import { cn } from '@comfyorg/tailwind-utils' -import AttachmentChip from './composer/AttachmentChip.vue' +import AssetTray from './composer/AssetTray.vue' +import AssetThumbnail from './composer/AssetThumbnail.vue' import RunModePopover from './composer/RunModePopover.vue' const { @@ -99,11 +96,6 @@ const emit = defineEmits<{ }>() const { t } = useI18n() -const assetDragActive = inject>>( - 'agentAssetDragActive', - ref(false) -) - const duplicateIdClass = 'shrink-0 rounded-full bg-interface-menu-keybind-surface-default px-1 py-0.5 font-mono text-xs/4 text-base-foreground' @@ -146,9 +138,16 @@ const composer = useComposer({ const editorRef = useTemplateRef>('editorRef') const { workflowReferences } = composer +const showPlaceholderHint = computed( + () => !composer.draft.value && !composer.prompt.value.references.length +) const workflowSubmenuOpen = ref(false) const addMenuOpen = ref(false) +const highlightedAssetIds = ref([]) +const uploadingAttachmentCount = computed( + () => composer.attachments.value.filter((item) => item.uploading).length +) const { eligibleWorkflows, selectWorkflow } = useWorkflowReferencePicker({ editor: () => editorRef.value, @@ -166,7 +165,6 @@ const { mentionVisible, mentionHasResults, graphDupes, - tagDupes, syncMention, pickMention, isNodeReferenceDisabled, @@ -180,11 +178,13 @@ const { editor: () => editorRef.value, selectionTags: () => selectionTags, workflows: () => eligibleWorkflows.value, + assets: () => composer.attachments.value, nodeReferenceDisabledReason: () => nodeReferenceDisabledReason, workflowSelecting: () => workflowSelecting, getMentionNodes: () => getMentionNodes(), selectWorkflow, pickNode: (node) => emit('mentionPick', node), + pickAsset: (asset) => composer.referenceAttachment(asset.id), requestWorkflows: () => emit('requestWorkflowReferences') }) @@ -343,7 +343,7 @@ defineExpose({ id="agent-composer" ref="composerContainerRef" data-testid="agent-composer" - class="relative flex flex-col rounded-lg border border-border-subtle bg-base-background" + class="relative flex min-w-0 flex-col rounded-lg border border-border-subtle bg-base-background" >