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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions browser_tests/tests/agent/agentAttachmentUpload.spec.ts
Original file line number Diff line number Diff line change
@@ -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<void>((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' })
}
})
})
56 changes: 56 additions & 0 deletions browser_tests/tests/agent/agentPanel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,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 ({
comfyPage
}) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ after the last URL segment, which for a content URL is the literal `content`.
an asset onto the agent prompt did nothing while the panel was in list
view and worked in grid view.
5. A non-OK response is not a file, and the user is told. `fetchDroppedAsset`
returns `undefined` on `!response.ok` (status-based, never content-type
sniffing). The canvas drop handler, when a drop that carried the
rejects on `!response.ok` (status-based, never content-type sniffing), and
callers choose whether to surface or gracefully ignore that failure. The
canvas drop handler, when a drop that carried the
asset-info flavour yields no file, reports the failure through
`reportError` and shows a toast instead of a silent no-op. Drops that
never carried asset-info (plain links from elsewhere) stay silent.
Expand Down
2 changes: 2 additions & 0 deletions src/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5244,6 +5244,8 @@
"addFromAssets": "Drag in asset from asset panel",
"assetNotAttachable": "Images, video, audio, 3D models, and text files can be attached",
"attachmentTooLarge": "{name} is larger than {limit}",
"attachmentBatchLimit": "{count} file was not added because the upload queue limit is {limit}. | {count} files were not added because the upload queue limit is {limit}.",
"attachmentsTooLarge": "{count} file exceeds the upload size limit. | {count} files exceed the upload size limit.",
"attachmentUploadFailed": "{name} could not be uploaded",
"mention": "Mention a node",
"changeWorkflowForChat": "Change the workflow that the agent can edit",
Expand Down
25 changes: 25 additions & 0 deletions src/stores/assetsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,30 @@ export const useAssetsStore = defineStore('assets', () => {

const inputAssets = ref<PagedList<AssetItem>>(undefined!)
const outputAssets = ref<PagedList<AssetItem>>(undefined!)
let inputRefreshDirty = false
let inputRefresh: Promise<void> | undefined

function refreshInputAssets(): Promise<void> {
inputRefreshDirty = true
inputRefresh ??= (async () => {
for (;;) {
inputRefreshDirty = false
try {
await inputAssets.value.loadNew()
} catch {
// A coalesced request still owns a trailing pass even when the
// current refresh fails.
}
// The flag can change while the awaited refresh is in flight.
// oxlint-disable-next-line typescript/no-unnecessary-condition
if (inputRefreshDirty) continue

inputRefresh = undefined
return
}
})()
return inputRefresh
}
let assetsScope: EffectScope | undefined
watch(
() => flags.assetsEnabled,
Expand Down Expand Up @@ -913,6 +937,7 @@ export const useAssetsStore = defineStore('assets', () => {
return {
// States
inputAssets,
refreshInputAssets,
outputAssets,
invalidateAll,

Expand Down
99 changes: 95 additions & 4 deletions src/utils/__tests__/eventUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,96 @@
import { extractFilesFromDragEvent, getDroppedAsset } from '@/utils/eventUtils'
import {
extractFilesFromDragEvent,
fetchDroppedAsset,
fetchTrustedDroppedAsset,
getDroppedAsset
} from '@/utils/eventUtils'
import { beforeEach, describe, expect, it, vi } from 'vitest'

describe('eventUtils', () => {
it('rejects a declared oversized dropped asset before buffering its body', async () => {
const blob = vi.fn()
const cancel = vi.fn(async () => undefined)
vi.stubGlobal(
'fetch',
vi.fn(async () => ({
ok: true,
headers: new Headers({ 'Content-Length': '101' }),
body: { cancel },
blob
}))
)

await expect(
fetchDroppedAsset(
{ name: 'large.png', uri: '/api/view?filename=large.png' },
undefined,
100
)
).rejects.toThrow('Dropped asset exceeds 100 bytes')
expect(blob).not.toHaveBeenCalled()
expect(cancel).toHaveBeenCalledOnce()
})

it('cancels an unsuccessful dropped-asset response', async () => {
const cancel = vi.fn(async () => undefined)
vi.stubGlobal(
'fetch',
vi.fn(async () => ({
ok: false,
status: 503,
body: { cancel }
}))
)

await expect(
fetchDroppedAsset({
name: 'asset.png',
uri: '/api/view?filename=asset.png'
})
).rejects.toThrow('Dropped asset fetch failed with HTTP 503')
expect(cancel).toHaveBeenCalledOnce()
})

it.for([
'https://example.com/api/view?filename=private.png',
'/api/settings',
'data:text/plain,private'
])('rejects an untrusted dropped-asset URI: %s', async (uri) => {
const fetchSpy = vi.fn()
vi.stubGlobal('fetch', fetchSpy)

await expect(
fetchTrustedDroppedAsset({ name: 'asset.png', uri })
).resolves.toBeUndefined()
expect(fetchSpy).not.toHaveBeenCalled()
})

it('cancels an unknown-length stream once its byte limit is crossed', async () => {
let cancelled = false
const body = new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(new Uint8Array(60))
controller.enqueue(new Uint8Array(50))
},
cancel() {
cancelled = true
}
})
vi.stubGlobal(
'fetch',
vi.fn(async () => new Response(body, { status: 200 }))
)

await expect(
fetchDroppedAsset(
{ name: 'streamed.png', uri: '/api/view?filename=streamed.png' },
undefined,
100
)
).rejects.toThrow('Dropped asset exceeds 100 bytes')
expect(cancelled).toBe(true)
})

describe('extractFilesFromDragEvent', () => {
let fetchSpy: ReturnType<typeof vi.fn>

Expand Down Expand Up @@ -106,7 +195,7 @@ describe('eventUtils', () => {
})

it('should fetch URI and return as File when text/uri-list is present', async () => {
const uri = 'https://example.com/api/view?filename=test.png&type=input'
const uri = '/api/view?filename=test.png&type=input'
const imageBlob = new Blob([new Uint8Array([0x89, 0x50])], {
type: 'image/png'
})
Expand All @@ -126,7 +215,7 @@ describe('eventUtils', () => {
})

it('should handle text/x-moz-url type', async () => {
const uri = 'https://example.com/api/view?filename=test.png&type=input'
const uri = '/api/view?filename=test.png&type=input'
const imageBlob = new Blob([new Uint8Array([0x89, 0x50])], {
type: 'image/png'
})
Expand Down Expand Up @@ -188,7 +277,7 @@ describe('eventUtils', () => {
)

it('should return empty array when URI fetch fails', async () => {
const uri = 'https://example.com/api/view?filename=test.png&type=input'
const uri = '/api/view?filename=test.png&type=input'
fetchSpy.mockRejectedValue(new TypeError('Failed to fetch'))

const dataTransfer = new DataTransfer()
Expand Down Expand Up @@ -219,6 +308,7 @@ describe('eventUtils', () => {

expect(getDroppedAsset(dataTransfer)).toEqual({
name: 'My asset',
filename: 'asset.png',
uri: 'http://localhost/api/view?x=1',
ref: 'stored-asset.png',
kind: 'image',
Expand All @@ -239,6 +329,7 @@ describe('eventUtils', () => {

expect(getDroppedAsset(dataTransfer)).toEqual({
name: 'asset.mp4',
filename: 'asset.mp4',
uri: undefined,
ref: 'stored-asset.mp4',
kind: 'video',
Expand Down
Loading
Loading