diff --git a/browser_tests/tests/agent/agentAttachmentUpload.spec.ts b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts new file mode 100644 index 00000000000..e7e3160f701 --- /dev/null +++ b/browser_tests/tests/agent/agentAttachmentUpload.spec.ts @@ -0,0 +1,63 @@ +import { expect } from '@playwright/test' + +import enMessages from '@/locales/en/main.json' with { type: 'json' } + +import { agentTest as test } from '@e2e/tests/agent/agentPanelMocks' + +test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => { + test('closing the panel removes a pending upload and preserves a sendable draft', async ({ + comfyPage, + agentPanel + }) => { + const page = comfyPage.page + let releaseUpload = () => {} + const uploadHeld = new Promise((resolve) => { + releaseUpload = resolve + }) + await page.route('**/api/upload/image', async (route) => { + await uploadHeld + await route.abort() + }) + + try { + const openButton = page.getByRole('button', { + name: enMessages.agent.entryButton, + exact: true + }) + await openButton.click() + await agentPanel.selectWorkflow() + const panel = page.locator('#agent-panel-root') + const composer = panel.getByRole('textbox', { name: /^Describe ideas/ }) + const send = panel.getByRole('button', { name: 'Send', exact: true }) + await composer.fill('Keep this draft') + await expect(send).toBeEnabled() + + const uploadReceived = page.waitForRequest('**/api/upload/image') + await panel.getByTestId('agent-file-input').setInputFiles({ + name: 'pending.mp4', + mimeType: 'video/mp4', + buffer: Buffer.from('pending upload') + }) + await uploadReceived + await expect( + panel.getByTestId('composer-asset-section').getByText('pending.mp4') + ).toBeVisible() + await expect(send).toBeDisabled() + + await panel + .getByRole('button', { name: enMessages.g.close, exact: true }) + .click() + await expect(panel).toHaveCount(0) + await openButton.click() + + await expect(composer).toHaveText('Keep this draft') + await expect(panel.getByText('pending.mp4', { exact: true })).toHaveCount( + 0 + ) + await expect(send).toBeEnabled() + } finally { + releaseUpload() + await page.unrouteAll({ behavior: 'wait' }) + } + }) +}) diff --git a/browser_tests/tests/agent/agentPanel.spec.ts b/browser_tests/tests/agent/agentPanel.spec.ts index c81dc90426d..84ce5beeb2c 100644 --- a/browser_tests/tests/agent/agentPanel.spec.ts +++ b/browser_tests/tests/agent/agentPanel.spec.ts @@ -402,6 +402,62 @@ test.describe('In-App Agent panel', { tag: '@cloud' }, () => { .toBeLessThanOrEqual(1) }) + test('uses the server upload limit for Agent file attachments', async ({ + comfyPage, + agentPanel + }) => { + test.setTimeout(60_000) + const page = comfyPage.page + let uploadCount = 0 + await page.route('**/api/upload/image', async (route) => { + uploadCount += 1 + await route.fulfill({ + status: 200, + contentType: 'application/json', + body: JSON.stringify({ + name: 'uploaded_movie.mp4', + subfolder: '', + type: 'input' + }) + }) + }) + await page.evaluate(() => { + window.app!.api.serverFeatureFlags.value = { + ...window.app!.api.serverFeatureFlags.value, + max_upload_size: 24 * 1024 * 1024 + } + }) + + await agentPanel.open() + const panel = page.locator('#agent-panel-root') + const fileInput = panel.getByTestId('agent-file-input') + + const uploadResponse = page.waitForResponse('**/api/upload/image') + await fileInput.setInputFiles({ + name: 'movie.mp4', + mimeType: 'video/mp4', + buffer: Buffer.alloc(21 * 1024 * 1024) + }) + expect((await uploadResponse).ok()).toBe(true) + await expect( + panel.getByTestId('composer-asset-section').getByText('movie.mp4') + ).toBeVisible() + await expect.poll(() => uploadCount).toBe(1) + + await fileInput.setInputFiles({ + name: 'too-large.mp4', + mimeType: 'video/mp4', + buffer: Buffer.alloc(25 * 1024 * 1024) + }) + 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.poll(() => uploadCount).toBe(1) + }) + test('exits node selection when the active workflow changes', async ({ agentPanel, comfyPage diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 39e93d1f2f3..c4c36653315 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1090,6 +1090,40 @@ describe('AgentPanelRoot attach flow', () => { expect(screen.getByRole('button', { name: 'cat.png' })).toBeInTheDocument() }) + it('uses the submitted filename when the upload response omits a name', async () => { + const messageBodies: unknown[] = [] + vi.stubGlobal( + 'fetch', + vi.fn(async (url: string, init?: RequestInit) => { + if (url.endsWith('/api/upload/image')) { + return json(200, { subfolder: '', type: 'input' }) + } + messageBodies.push(JSON.parse(String(init?.body))) + return json(202, { thread_id: 'th-1', message_id: 'm-1' }) + }) + ) + + renderWithSelectedTarget() + + await openAddMenu() + await userEvent.click( + await screen.findByRole('menuitem', { + name: i18n.global.t('agent.attachFiles') + }) + ) + await userEvent.upload( + screen.getByTestId('agent-file-input'), + new File(['x'], 'cat.png', { type: 'image/png' }) + ) + + await userEvent.click(screen.getByRole('textbox')) + await userEvent.paste('describe it') + await userEvent.click(screen.getByRole('button', { name: 'Send' })) + + expect(messageBodies).toHaveLength(1) + expect(messageBodies[0]).toMatchObject({ attachments: ['cat.png'] }) + }) + it('uploads a picked video above 20MB when the server permits it', async () => { getServerFeature.mockReturnValue(100 * 1024 * 1024) const uploaded = stubUploadFetch() @@ -1187,13 +1221,13 @@ describe('AgentPanelRoot attach flow', () => { expect(useToastStore().messagesToAdd).toContainEqual( expect.objectContaining({ severity: 'warn', - detail: 'movie.mp4 is larger than 24MB' + detail: 'movie.mp4 is larger than 24 MB' }) ) expect(screen.queryByText('movie.mp4')).not.toBeInTheDocument() }) - it('keeps the image limit at 20MB when the server permits more', async () => { + it('uses a larger server limit for non-video attachments', async () => { getServerFeature.mockReturnValue(100 * 1024 * 1024) const uploaded = stubUploadFetch() renderWithSelectedTarget() @@ -1201,15 +1235,13 @@ describe('AgentPanelRoot attach flow', () => { const image = fileOfSize('huge.png', MAX_ATTACHMENT_BYTES + 1, 'image/png') dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [image] }) - await nextTick() - expect(uploaded).toEqual([]) - expect(useToastStore().messagesToAdd).toContainEqual( - expect.objectContaining({ - severity: 'warn', - detail: 'huge.png is larger than 20MB' - }) - ) + expect( + within(await screen.findByTestId('composer-asset-section')).getByText( + 'huge.png' + ) + ).toBeInTheDocument() + await vi.waitFor(() => expect(uploaded).toEqual(['huge.png'])) }) it('uploads a dropped video above 20MB when the server permits it', async () => { @@ -1301,13 +1333,142 @@ describe('AgentPanelRoot attach flow', () => { await vi.waitFor(() => expect(refresh).toHaveBeenCalled()) }) - it('keeps the 20MB limit for an oversize audio file', async () => { - getServerFeature.mockReturnValue(100 * 1024 * 1024) + it('refreshes the input asset library once for a dropped batch', async () => { + // Overlapping refreshes coalesce into the in-flight query without a + // trailing run, so an asset committing mid-refresh would be dropped. + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockResolvedValue(undefined) + + dispatchDrag(screen.getByRole('textbox'), 'drop', { + files: [ + new File(['x'], 'a.png', { type: 'image/png' }), + new File(['x'], 'b.png', { type: 'image/png' }), + new File(['x'], 'c.png', { type: 'image/png' }) + ] + }) + + await vi.waitFor(() => expect(uploaded).toHaveLength(3)) + await vi.waitFor(() => expect(refresh).toHaveBeenCalled()) + expect(refresh).toHaveBeenCalledOnce() + }) + + it('chains input asset refreshes across overlapping batches', async () => { + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + let releaseFirstRefresh: () => void = () => {} + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockImplementationOnce( + () => + new Promise((resolve) => { + releaseFirstRefresh = () => resolve(undefined) + }) + ) + .mockResolvedValue(undefined) + + const textbox = screen.getByRole('textbox') + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'a.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) + + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'b.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(uploaded).toEqual(['a.png', 'b.png'])) + await expect( + vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2), { + timeout: 250, + interval: 10 + }) + ).rejects.toThrow() + + releaseFirstRefresh() + await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) + }) + + it('keeps refreshing later batches after a refresh fails', async () => { + const uploaded = stubUploadFetch() + renderWithSelectedTarget() + await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockRejectedValueOnce(new Error('asset fetch failed')) + .mockResolvedValue(undefined) + + const textbox = screen.getByRole('textbox') + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'a.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledOnce()) + + dispatchDrag(textbox, 'drop', { + files: [new File(['x'], 'b.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(uploaded).toEqual(['a.png', 'b.png'])) + await vi.waitFor(() => expect(refresh).toHaveBeenCalledTimes(2)) + }) + + it('lets a removed upload finish without reattaching until Undo', async () => { + const signals: AbortSignal[] = [] + let finishUpload: (response: Response) => void = () => {} + const upload = new Promise((resolve) => { + finishUpload = resolve + }) + vi.stubGlobal( + 'fetch', + vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => { + if (!String(input).includes('/upload/')) + return json(200, agentThreadList()) + if (init?.signal) signals.push(init.signal) + return upload + }) + ) + renderWithSelectedTarget() + await nextTick() + const refresh = vi + .spyOn(useAssetsStore().inputAssets, 'loadNew') + .mockResolvedValue(undefined) + + dispatchDrag(screen.getByRole('textbox'), 'drop', { + files: [new File(['x'], 'cat.png', { type: 'image/png' })] + }) + await vi.waitFor(() => expect(signals).toHaveLength(1)) + const composer = useAgentComposerStore() + const prompt = composer.prompt + + await userEvent.click( + await screen.findByRole('button', { name: i18n.global.t('agent.remove') }) + ) + + expect(signals[0].aborted).toBe(false) + finishUpload( + json(200, { name: 'uploaded-cat.png', subfolder: '', type: 'input' }) + ) + 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 + }) + ]) + }) + + it('uses the server limit for audio rejection copy', async () => { + getServerFeature.mockReturnValue(24 * 1024 * 1024) const uploaded = stubUploadFetch() renderWithSelectedTarget() await nextTick() - const song = fileOfSize('big.mp3', MAX_ATTACHMENT_BYTES + 1, 'audio/mpeg') + const song = fileOfSize('big.mp3', 25 * 1024 * 1024, 'audio/mpeg') dispatchDrag(screen.getByRole('textbox'), 'drop', { files: [song] }) await nextTick() @@ -1315,7 +1476,7 @@ describe('AgentPanelRoot attach flow', () => { expect(useToastStore().messagesToAdd).toContainEqual( expect.objectContaining({ severity: 'warn', - detail: 'big.mp3 is larger than 20MB' + detail: 'big.mp3 is larger than 24 MB' }) ) }) @@ -1573,6 +1734,49 @@ describe('AgentPanelRoot attach flow', () => { ) }) + it('does not warn after closing the panel during a deferred asset fetch', async () => { + vi.stubGlobal( + 'fetch', + vi.fn((input) => { + const url = String(input) + if (url.includes('/api/view')) return new Promise(() => {}) + if (url.includes('/assets')) + return Promise.resolve( + json(200, { assets: [], total: 0, has_more: false }) + ) + if (url.includes('/workflows')) + return Promise.resolve( + json(200, { data: [], total: 0, has_more: false }) + ) + return Promise.resolve(json(200, agentThreadList())) + }) + ) + const { unmount } = renderWithSelectedTarget() + await nextTick() + const toast = useToastStore() + vi.useFakeTimers() + try { + dispatchDrag(screen.getByRole('textbox'), 'drop', { + types: ['application/x-comfy-asset-info', 'text/uri-list'], + getData: (type: string) => + type === 'application/x-comfy-asset-info' + ? JSON.stringify({ filename: 'gen.png', type: 'input' }) + : 'http://localhost/api/view?filename=gen.png' + }) + await nextTick() + expect( + screen.getByLabelText(i18n.global.t('agent.uploading')) + ).toBeInTheDocument() + + unmount() + await vi.advanceTimersByTimeAsync(60_000) + + expect(toast.messagesToAdd).toEqual([]) + } finally { + vi.useRealTimers() + } + }) + it('attaches dropped assets and leaves other files to the graph loader', async () => { // The graph loader only opens a dropped workflow while the drop is // unclaimed, so the panel must not claim files it cannot attach. diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 67d9e4a5905..b390e094f7e 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1,6 +1,7 @@