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
60 changes: 60 additions & 0 deletions browser_tests/tests/agent/agentAttachmentUpload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,66 @@ import { assetPath } from '@e2e/fixtures/utils/paths'
import { agentTest as test } from '@e2e/tests/agent/agentPanelMocks'

test.describe('Agent attachment cancellation', { tag: '@cloud' }, () => {
test(
'starting a new chat releases upload capacity for the next draft',
{
annotation: {
type: 'regression',
description:
'https://github.com/Comfy-Org/ComfyUI_frontend/pull/21588#discussion_r4378869451'
}
},
async ({ comfyPage, agentPanel }) => {
const page = comfyPage.page
let releaseUploads = () => {}
const uploadsHeld = new Promise<void>((resolve) => {
releaseUploads = resolve
})
const uploadsSettled = new Set<Promise<void>>()
let uploadCount = 0
function holdUpload(route: Route) {
uploadCount += 1
const uploadSettled = uploadsHeld.then(() => route.abort())
uploadsSettled.add(uploadSettled)
return uploadSettled
}
await page.route('**/api/upload/image', holdUpload)

try {
await agentPanel.open()
await agentPanel.fileInput.setInputFiles(
Array.from({ length: 3 }, (_, index) => ({
name: `old-draft-${index}.png`,
mimeType: 'image/png',
buffer: Buffer.from(`old draft ${index}`)
}))
)
await expect.poll(() => uploadCount).toBe(3)
await expect(agentPanel.attachmentChips).toHaveCount(3)

await agentPanel.root
.getByRole('button', {
name: enMessages.agent.newChat,
exact: true
})
.click()
await expect(agentPanel.attachmentChips).toHaveCount(0)
await agentPanel.fileInput.setInputFiles({
name: 'new-draft.png',
mimeType: 'image/png',
buffer: Buffer.from('new draft')
})

await expect(agentPanel.attachmentChip('new-draft.png')).toBeVisible()
await expect.poll(() => uploadCount).toBe(4)
} finally {
releaseUploads()
await Promise.all(uploadsSettled)
await page.unroute('**/api/upload/image', holdUpload)
}
}
)

test('closing the panel removes a pending upload and preserves a sendable draft', async ({
comfyPage,
agentPanel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from '@playwright/test'

import enMessages from '@/locales/en/main.json' with { type: 'json' }

import { waitForCloudApp } from '@e2e/fixtures/cloudAppFixture'
import { agentTest as test } from '@e2e/tests/agent/agentPanelMocks'

test.describe(
'Agent composer draft survives a page refresh',
{ tag: ['@cloud', '@ui'] },
() => {
test('restores an unsent draft after a reload, but not one discarded by New Chat', async ({
agentPanel,
page
}, testInfo) => {
await agentPanel.open()
const draft = 'Make the background a sunset over the ocean'

await test.step('an unsent draft is still in the composer after a reload', async () => {
await agentPanel.composer.fill(draft)
await expect(agentPanel.composer).toHaveText(draft)

await page.reload()
await waitForCloudApp(page)
await agentPanel.open()
await expect(agentPanel.composer).toHaveText(draft)
await testInfo.attach('draft-restored-after-reload', {
body: await agentPanel.root.screenshot({
path: testInfo.outputPath('draft-restored-after-reload.png')
}),
contentType: 'image/png'
})
})

await test.step('New Chat leaves the composer empty after a reload', async () => {
await agentPanel.root
.getByRole('button', { name: enMessages.agent.newChat })
.click()
await expect(agentPanel.composer).toHaveText('')

await page.reload()
await waitForCloudApp(page)
await agentPanel.open()
await expect(
agentPanel.root.getByText(enMessages.agent.placeholder, {
exact: true
})
).toBeVisible()
await expect(agentPanel.composer).toHaveText('')
await testInfo.attach('new-chat-empty-after-reload', {
body: await agentPanel.root.screenshot({
path: testInfo.outputPath('new-chat-empty-after-reload.png')
}),
contentType: 'image/png'
})
})
})
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test.describe(
'New chat targets the workflow tab on screen',
{ tag: ['@cloud', '@agent'] },
() => {
test('PM-1322: retargets to a tab the user switched to', async ({
test('PM-1322 / PM-2251: retargets to a tab the user switched to and starts with an empty composer', async ({
page,
workflowSelection
}, testInfo) => {
Expand All @@ -48,7 +48,7 @@ test.describe(
await initialComposer.press('Enter')
await expect.poll(() => workflowSelection.postedMessages.length).toBe(1)
await expect(initialComposer).toHaveText('Start in this workflow')
await initialComposer.fill('')
await initialComposer.fill('Only edit workflow A')

// Open a second workflow tab (B), then switch back to A so B is just
// another open tab rather than the newest/active one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ retain the target before further tab navigation, including a picker choice of
the already-visible workflow. Send retains before asynchronous preparation;
a picker retains only after successful selection. Failed, cancelled or
superseded picker operations do not commit. Success, failure, retry and removing
references do not resume following. Explicit target selection remains available; New Chat starts
following again unless its retained draft still contains node references.
references do not resume following. Explicit target selection remains available. New Chat
clears the unsent draft (text, attachments and staged node references), cancels pending uploads
and starts following again, so a new chat never carries input aimed at another workflow
(amended 2026-11-01 for PM-2251). Leaving a failed history selection with Back keeps the unsent
text and follows the visible workflow.

The panel store owns a single target-state union so it survives component
remounts. In `following`, the target derives directly from the editor's active
Expand Down Expand Up @@ -55,6 +58,8 @@ workflow is already known. Ordinary draft text does not commit a target.
### Positive

- Fresh chat entry and New Chat use the same default without an extra save.
- New Chat always starts empty; references staged for the previous chat are dropped rather
than carried to a workflow the user may have left.
- Explicit choices, failed sends and node references preserve the target.
- Mismatch feedback can reveal a different target without changing it.

Expand All @@ -64,6 +69,8 @@ workflow is already known. Ordinary draft text does not commit a target.
length and thread ID cannot replace the explicit policy.
- Existing tests that treat manual first selection as mandatory must instead
distinguish fresh following from a target committed by user input.
- A user who wanted to reuse staged references or text in a new chat must restage them;
the unchanged canvas selection can be restaged immediately.
- Explicit retargeting keeps the existing node-scope cleanup behavior; this
decision only changes passive tab following.

Expand Down
3 changes: 2 additions & 1 deletion src/platform/workflow/persistence/base/storageIO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,8 @@ export function clearAllWorkspaceStorage(): void {
StorageKeys.prefixes.agentThread,
StorageKeys.prefixes.agentWorkflowTabBindings,
StorageKeys.prefixes.agentChatTitles,
StorageKeys.prefixes.agentDeletedThreads
StorageKeys.prefixes.agentDeletedThreads,
StorageKeys.prefixes.agentComposerDraft
]
)
}
7 changes: 6 additions & 1 deletion src/platform/workflow/persistence/base/storageKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ export const StorageKeys = {
return `Comfy.Agent.DeletedThreads:${workspaceId}`
},

agentComposerDraft(workspaceId: string): string {
return `Comfy.Agent.ComposerDraft:${workspaceId}`
},

/**
* localStorage copies of tab pointers for cross-session restore.
* sessionStorage is per-tab (correct for in-session use) but lost
Expand All @@ -117,6 +121,7 @@ export const StorageKeys = {
agentThread: 'Comfy.Agent.ThreadId:',
agentWorkflowTabBindings: 'Comfy.Agent.WorkflowTabBindings:',
agentChatTitles: 'Comfy.Agent.ChatTitles:',
agentDeletedThreads: 'Comfy.Agent.DeletedThreads:'
agentDeletedThreads: 'Comfy.Agent.DeletedThreads:',
agentComposerDraft: 'Comfy.Agent.ComposerDraft:'
}
} as const
108 changes: 87 additions & 21 deletions src/workbench/extensions/agent/AgentPanelRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9414,7 +9414,7 @@ describe('AgentPanelRoot workflow binding', () => {
}
)

it('resumes following on New Chat after an explicit pick while preserving draft text', async () => {
it('resumes following on New Chat after an explicit pick and starts with an empty composer', async () => {
const {
target,
references: [other]
Expand Down Expand Up @@ -9444,7 +9444,7 @@ describe('AgentPanelRoot workflow binding', () => {
workflowStore.activeWorkflow = other
await nextTick()
expect(useAgentPanelStore().selectedWorkflow?.path).toBe(other.path)
expect(screen.getByRole('textbox')).toHaveTextContent('keep this draft')
expect(screen.getByRole('textbox')).toHaveTextContent('')
})

it('retains the first Send target through failure, reopen and retry until New Chat', async () => {
Expand Down Expand Up @@ -10049,6 +10049,46 @@ describe('AgentPanelRoot workflow binding', () => {
expect(workflowStore.activeWorkflow.path).toBe(other.path)
})

it('preserves a new unsent draft when Back leaves a failed history selection', async () => {
makeTab('wf-42')
mockMessagesEndpoint(
'wf-42',
[{ id: 'wf-42', name: 'current' }],
[
agentThread({
id: 'th-1',
title: 'Previous chat',
last_message_at: '2026-09-25T00:00:00Z'
})
]
)
const request = vi.mocked(fetch).getMockImplementation()
assert.exists(request)
vi.mocked(fetch).mockImplementation((input, init) =>
String(input).includes('/messages') && init?.method !== 'POST'
? Promise.resolve(json(500, {}))
: request(input, init)
)
renderWithSelectedTarget()
await userEvent.click(screen.getByRole('textbox'))
await userEvent.paste('Keep my new draft')
await userEvent.click(
screen.getByRole('button', {
name: i18n.global.t('agent.showChatHistory')
})
)
await userEvent.click(
await screen.findByRole('button', { name: 'Previous chat' })
)
await screen.findByRole('alert')

await userEvent.click(
screen.getByRole('button', { name: 'Back to previous chat' })
)

expect(screen.getByRole('textbox')).toHaveTextContent('Keep my new draft')
})

it('restores Current after remounting an interrupted return instead of reusing stale messages', async () => {
makeTab('wf-42')
mockMessagesEndpoint(
Expand Down Expand Up @@ -15491,33 +15531,35 @@ describe('AgentPanelRoot workflow binding', () => {
name: i18n.global.t('agent.switchWorkflow')
})
).toHaveTextContent('other')
expect(screen.getByRole('textbox')).toHaveTextContent('Keep this draft')
expect(screen.getByRole('textbox')).toHaveTextContent('')
workflowStore.activeWorkflow = target
await nextTick()
expect(useAgentPanelStore().selectedWorkflow?.path).toBe(target.path)
await userEvent.click(screen.getByRole('textbox'))
await userEvent.paste('Fresh prompt')
await userEvent.click(screen.getByRole('button', { name: 'Send' }))
await vi.waitFor(() => expect(bodies).toHaveLength(1))
expect(bodies[0]).toMatchObject({
content: 'Keep this draft',
content: 'Fresh prompt',
workflow_id: 'wf-42'
})
expect(bodies[0]).not.toHaveProperty('selection')
})

it.for(['current', 'other'] as const)(
'preserves carried node references through New Chat while viewing $0',
'drops staged node references on New Chat while viewing $0',
async (visibleTab) => {
const tabs = {
current: makeTab('wf-42'),
other: addTab('workflows/other.json')
}
const bodies = mockMessagesEndpoint('wf-42')
mockMessagesEndpoint('wf-42')
setupNodeSelectionCanvas()
render(AgentPanelRoot, { global: { plugins: [i18n] } })
useAgentPanelStore().isOpen = true
await openMentionPicker()
await userEvent.click(await screen.findByText('KSampler'))
await userEvent.paste('Keep these nodes')
await userEvent.paste('Edit these nodes')
workflowStore.activeWorkflow = tabs[visibleTab]
await nextTick()

Expand All @@ -15527,24 +15569,48 @@ describe('AgentPanelRoot workflow binding', () => {
screen.getByRole('button', {
name: i18n.global.t('agent.switchWorkflow')
})
).toHaveTextContent('current')
expect(screen.getByRole('textbox')).toHaveTextContent('Keep these nodes')
).toHaveTextContent(visibleTab)
expect(screen.getByRole('textbox')).toHaveTextContent('')
expect(
screen.getByRole('button', { name: 'Remove KSampler #12 reference' })
).toBeVisible()

workflowStore.activeWorkflow = addTab('workflows/third.json')
await nextTick()
await userEvent.click(screen.getByRole('button', { name: 'Send' }))
await vi.waitFor(() => expect(bodies).toHaveLength(1))
expect(bodies[0]).toMatchObject({
content: '@[Node: KSampler #12] Keep these nodes',
workflow_id: 'wf-42',
selection: { node_ids: ['12'], workflow_id: 'wf-42' }
})
screen.queryByRole('button', { name: 'Remove KSampler #12 reference' })
).toBeNull()
}
)

it('restages the unchanged canvas selection in a new chat', async () => {
makeTab('wf-42')
mockMessagesEndpoint('wf-42')
const state = setupNodeSelectionCanvas()
renderWithSelectedTarget()
useAgentPanelStore().isOpen = true

await enterNodeSelectionMode()
state.selectedItems.add(state.nodes[0])
syncFakeSelection()
await nextTick()
expect(
screen.getByRole('button', {
name: /Remove VAE Decode #\d+ reference/
})
).toBeVisible()

await userEvent.click(screen.getByRole('button', { name: 'New chat' }))
expect(
screen.queryByRole('button', {
name: /Remove VAE Decode #\d+ reference/
})
).toBeNull()

state.selectedItems.add(state.nodes[0])
syncFakeSelection()
await enterNodeSelectionMode()
expect(
screen.getByRole('button', {
name: /Remove VAE Decode #\d+ reference/
})
).toBeVisible()
})

it('clears old node references when selecting another workflow with the same node id', async () => {
const {
references: [other]
Expand Down
Loading
Loading