From ce3b7f1fa4be299357d56e86141034210ff3ca02 Mon Sep 17 00:00:00 2001 From: heymemie <94387204+heymemie@users.noreply.github.com> Date: Tue, 10 Nov 2026 17:25:19 -0800 Subject: [PATCH 1/3] fix(agent): show a blank chat when switching to a tab with no chat (PM-2521) The workflow chat follow (#21512) opened a tab's latest chat but did nothing for a tab without one, so the panel kept the previous tab's chat and target on screen and the next prompt went to the wrong workflow. - A user tab switch to a saved tab with no chat, or to a new unsaved tab, now shows the blank new-chat screen targeting that tab. - A draft, a send in flight or an open workflow picker still holds the current chat, as before. Unsaved tabs the agent bound to a chat are still not followed. - The panel-mount follow keeps the previous behavior, so a chat whose tab is not active keeps its restore path. - New Blank Workflow reports the new tab as a user activation, so opening a new tab triggers the follow. --- src/composables/useCoreCommands.test.ts | 16 ++ src/composables/useCoreCommands.ts | 3 + .../extensions/agent/AgentPanelRoot.test.ts | 12 +- .../extensions/agent/AgentPanelRoot.vue | 14 +- .../agent/useAgentWorkflowChatFollow.test.ts | 180 +++++++++++++++--- .../agent/useAgentWorkflowChatFollow.ts | 54 +++++- 6 files changed, 237 insertions(+), 42 deletions(-) diff --git a/src/composables/useCoreCommands.test.ts b/src/composables/useCoreCommands.test.ts index 46f5d2db32a..f61eb79905c 100644 --- a/src/composables/useCoreCommands.test.ts +++ b/src/composables/useCoreCommands.test.ts @@ -337,6 +337,22 @@ describe('useCoreCommands', () => { }) }) + describe('NewBlankWorkflow command', () => { + it('reports the new tab as a user activation, so the agent chat follows it', async () => { + Object.assign(app.rootGraph, { _nodes: [] }) + const created = mockWorkflowStore.activeWorkflow + const newBlank = useCoreCommands().find( + (cmd) => cmd.id === 'Comfy.NewBlankWorkflow' + )! + + await newBlank.function() + + expect(mockWorkflowStore.reportUserActivation).toHaveBeenCalledWith( + created + ) + }) + }) + describe('PasteFromClipboardWithConnect command', () => { // An unresolved key renders as its own path, and this one reads as valid // English, so asserting the shipped string would pass either way. Only a diff --git a/src/composables/useCoreCommands.ts b/src/composables/useCoreCommands.ts index 93fa75cc36c..63a431a76e6 100644 --- a/src/composables/useCoreCommands.ts +++ b/src/composables/useCoreCommands.ts @@ -176,6 +176,9 @@ export function useCoreCommands(): ComfyCommand[] { function: async () => { const previousWorkflowHadNodes = app.rootGraph._nodes.length > 0 await workflowService.loadBlankWorkflow() + // A new tab is a tab the user switched to: the agent panel follows it. + const created = workflowStore.activeWorkflow + if (created) workflowStore.reportUserActivation(created) telemetry?.trackWorkflowCreated({ workflow_type: 'blank', previous_workflow_had_nodes: previousWorkflowHadNodes diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index a3f8596d76d..36857459976 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -5910,7 +5910,7 @@ describe('AgentPanelRoot history', () => { ) } - it('keeps the current chat when the active tab has no matching thread', async () => { + it('shows a blank chat for the active tab when it has no matching thread', async () => { const first = addTab('workflows/first.json') const second = addTab('workflows/second.json') workflowStore.activeWorkflow = second @@ -5935,8 +5935,9 @@ describe('AgentPanelRoot history', () => { await activateTabAsUser(second) await settleFollow() - expect(useAgentConversationStore().threadId).toBe('th-first') - expect(userPrompts()).toEqual(['Prompt in th-first']) + expect(useAgentConversationStore().threadId).toBeNull() + expect(userPrompts()).toEqual([]) + expect(useAgentPanelStore().selectedWorkflow?.path).toBe(second.path) expect(loadedThreads).toEqual(['th-first']) }) @@ -7266,7 +7267,12 @@ describe('AgentPanelRoot history', () => { await vi.waitFor(() => expect(userPrompts()).toEqual(['Prompt in th-open']) ) + // A draft holds the open chat on screen across the switch to a tab with + // no chat; clearing it afterwards does not re-run the follow. + await userEvent.type(screen.getByRole('textbox'), 'Draft') await activateTabAsUser(second) + await settleFollow() + await userEvent.clear(screen.getByRole('textbox')) await userEvent.click( await screen.findByRole('button', { diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index 16ca29b0e35..ecd83656663 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1929,9 +1929,19 @@ const workflowChatFollow = useAgentWorkflowChatFollow({ useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }), restoreThreadWorkflow, adoptWorkflowTarget: adoptFollowedWorkflow, - adoptRestoredWorkflowTab + adoptRestoredWorkflowTab, + startBlankChat: startBlankChatForTab }) +// Follow-only reset: the user is not engaged (no draft, send or selection), so +// there is nothing to cancel. Leaves `errorAttachRun` alone, so an error attach +// that follows into a tab with no chat still completes. +function startBlankChatForTab(): void { + provisionalTitle.value = undefined + newChat() + agentPanelStore.startFollowingVisibleWorkflow() +} + const { failure: followFailure, openingWorkflowName, @@ -2010,7 +2020,7 @@ if ( selectedTarget.value !== null && selectedTarget.value.path !== mountedOn.path ) - void followTab(mountedOn) + void followTab(mountedOn, { blankWhenNoChat: false }) function warnWorkflowChanged(): void { toast.warning(t('agent.errorContext.workflowChanged'), { duration: 5000 }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts index 1238f93e87f..74504fd52de 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts @@ -1,46 +1,65 @@ import { describe, expect, it, vi } from 'vitest' +import type { ComfyWorkflow } from '@/platform/workflow/management/stores/comfyWorkflow' import { createMockLoadedWorkflow } from '@/utils/__tests__/litegraphTestUtils' import type { ThreadListing } from '@/workbench/extensions/agent/services/agent/agentRestClient' +import type { ChatSession } from '@/workbench/extensions/agent/stores/agent/agentChatHistoryStore' import { useAgentWorkflowChatFollow } from './useAgentWorkflowChatFollow' +type Deps = Parameters[0] + +const portraitChat: ChatSession = { + id: 'th-portrait', + title: 'Portrait', + titleSource: 'server', + updatedAt: 1, + workflowId: 'wf-portrait' +} + +function savedWorkflow(path: string): ComfyWorkflow { + return createMockLoadedWorkflow({ path, isTemporary: false }) +} + +function unsavedWorkflow(path: string): ComfyWorkflow { + return createMockLoadedWorkflow({ path, isTemporary: true }) +} + +function setup(workflow: ComfyWorkflow, overrides: Partial = {}) { + const deps: Deps = { + activeWorkflow: () => workflow, + selectedTarget: () => null, + currentThreadId: () => 'th-other', + sessions: () => [portraitChat], + followsVisibleWorkflow: () => false, + hasDraft: () => false, + userEngaged: () => false, + cloudIdFor: () => 'wf-portrait', + refreshCloudWorkflowIds: async () => true, + refreshHistory: async () => 'complete', + loadThread: vi.fn(async () => true), + clearDraft: vi.fn(), + trackThreadStarted: vi.fn(), + restoreThreadWorkflow: () => true, + adoptWorkflowTarget: vi.fn(), + adoptRestoredWorkflowTab: vi.fn(), + startBlankChat: vi.fn(), + ...overrides + } + return { deps, follow: useAgentWorkflowChatFollow(deps) } +} + describe('useAgentWorkflowChatFollow', () => { it('does not open a chat for a follow retired while its history refresh settles', async () => { - const workflow = createMockLoadedWorkflow({ - path: 'workflows/portrait.json', - isTemporary: false - }) + const workflow = savedWorkflow('workflows/portrait.json') let resolveRefresh!: (outcome: ThreadListing['status']) => void - const loadThread = vi.fn(async () => true) - const follow = useAgentWorkflowChatFollow({ - activeWorkflow: () => workflow, - selectedTarget: () => null, + const { deps, follow } = setup(workflow, { currentThreadId: () => null, - sessions: () => [ - { - id: 'th-portrait', - title: 'Portrait', - titleSource: 'server', - updatedAt: 1, - workflowId: 'wf-portrait' - } - ], followsVisibleWorkflow: () => true, - hasDraft: () => false, - userEngaged: () => false, - cloudIdFor: () => 'wf-portrait', - refreshCloudWorkflowIds: async () => true, refreshHistory: () => new Promise((resolve) => { resolveRefresh = resolve - }), - loadThread, - clearDraft: vi.fn(), - trackThreadStarted: vi.fn(), - restoreThreadWorkflow: () => true, - adoptWorkflowTarget: vi.fn(), - adoptRestoredWorkflowTab: vi.fn() + }) }) const following = follow.followTab(workflow) @@ -50,6 +69,109 @@ describe('useAgentWorkflowChatFollow', () => { follow.retireFollow() await following - expect(loadThread).not.toHaveBeenCalled() + expect(deps.loadThread).not.toHaveBeenCalled() + }) + + it("opens the tab's latest chat", async () => { + const workflow = savedWorkflow('workflows/portrait.json') + const { deps, follow } = setup(workflow) + + await follow.followTab(workflow) + + expect(deps.loadThread).toHaveBeenCalledWith( + 'th-portrait', + expect.any(Function), + { keepOpenChatUntilLoaded: true } + ) + expect(deps.startBlankChat).not.toHaveBeenCalled() + }) + + it('shows a blank chat for a saved tab that has no chat', async () => { + const workflow = savedWorkflow('workflows/landscape.json') + const { deps, follow } = setup(workflow, { + cloudIdFor: () => 'wf-landscape' + }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).toHaveBeenCalledOnce() + expect(deps.loadThread).not.toHaveBeenCalled() + }) + + it('shows a blank chat for a saved tab the cloud listing has no record of', async () => { + const workflow = savedWorkflow('workflows/unsynced.json') + const { deps, follow } = setup(workflow, { cloudIdFor: () => undefined }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).toHaveBeenCalledOnce() + }) + + it('keeps the current chat for a tab with no chat when the panel mounts', async () => { + const workflow = savedWorkflow('workflows/landscape.json') + const { deps, follow } = setup(workflow, { + cloudIdFor: () => 'wf-landscape' + }) + + await follow.followTab(workflow, { blankWhenNoChat: false }) + + expect(deps.startBlankChat).not.toHaveBeenCalled() + }) + + it('keeps the current chat when the cloud listing fails', async () => { + const workflow = savedWorkflow('workflows/unsynced.json') + const { deps, follow } = setup(workflow, { + cloudIdFor: () => undefined, + refreshCloudWorkflowIds: async () => false + }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).not.toHaveBeenCalled() + }) + + it('shows a blank chat for a new, unsaved tab', async () => { + const workflow = unsavedWorkflow('workflows/Unsaved Workflow.json') + const { deps, follow } = setup(workflow, { cloudIdFor: () => undefined }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).toHaveBeenCalledOnce() + expect(deps.loadThread).not.toHaveBeenCalled() + }) + + it('leaves an unsaved tab the agent bound to a chat alone', async () => { + const workflow = unsavedWorkflow('workflows/second.json') + const { deps, follow } = setup(workflow, { cloudIdFor: () => 'wf-second' }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).not.toHaveBeenCalled() + expect(deps.loadThread).not.toHaveBeenCalled() + }) + + it('does not reset a blank chat that already follows the visible tab', async () => { + const workflow = unsavedWorkflow('workflows/Unsaved Workflow (2).json') + const { deps, follow } = setup(workflow, { + cloudIdFor: () => undefined, + currentThreadId: () => null, + followsVisibleWorkflow: () => true + }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).not.toHaveBeenCalled() + }) + + it('keeps the current chat while the user has a draft or a send in flight', async () => { + const workflow = unsavedWorkflow('workflows/Unsaved Workflow.json') + const { deps, follow } = setup(workflow, { + cloudIdFor: () => undefined, + userEngaged: () => true + }) + + await follow.followTab(workflow) + + expect(deps.startBlankChat).not.toHaveBeenCalled() }) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts index c9c3fd435c1..2992e6e192c 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts @@ -56,6 +56,8 @@ type FollowDeps = { ) => Promise | boolean adoptWorkflowTarget: (workflow: ComfyWorkflow, workflowId: string) => void adoptRestoredWorkflowTab: (workflowId: string) => void + /** Shows the blank new-chat screen, targeting the visible tab. */ + startBlankChat: () => void } function requestedState( @@ -133,7 +135,7 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { const target = deps.selectedTarget() const chatTargetsWorkflow = !deps.followsVisibleWorkflow() && target?.path === workflow.path - return workflow.isTemporary || deps.userEngaged() || chatTargetsWorkflow + return deps.userEngaged() || chatTargetsWorkflow } function latestChatFor(workflowId: string): string | undefined { @@ -179,13 +181,27 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { deps.trackThreadStarted() } + /** + * The tab's cloud workflow id; `null` when a complete listing has no record + * of it (so it can have no chat); `undefined` when the listing failed. + */ async function resolveWorkflowId( workflow: ComfyWorkflow - ): Promise { + ): Promise { const existing = deps.cloudIdFor(workflow) if (existing !== undefined) return existing - await deps.refreshCloudWorkflowIds() - return deps.cloudIdFor(workflow) + const listed = await deps.refreshCloudWorkflowIds() + return deps.cloudIdFor(workflow) ?? (listed ? null : undefined) + } + + /** + * A tab with no chat shows the blank new-chat screen for that tab, instead + * of leaving the previous tab's chat (and its workflow target) on screen. + */ + function showBlankChat(operation: FollowOperation): void { + if (!isCurrent(operation)) return + if (deps.currentThreadId() === null && deps.followsVisibleWorkflow()) return + deps.startBlankChat() } async function refreshForFollow( @@ -207,26 +223,48 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { async function openLatestChat( workflowId: string, - operation: FollowOperation + operation: FollowOperation, + blankWhenNoChat: boolean ): Promise { const latest = latestChatFor(workflowId) - if (latest !== undefined && latest !== deps.currentThreadId()) + if (latest === undefined) { + if (blankWhenNoChat) showBlankChat(operation) + } else if (latest !== deps.currentThreadId()) await openFollowedChat(latest, operation, workflowId) } - async function followTab(workflow: ComfyWorkflow): Promise { + /** + * `blankWhenNoChat`: a tab the user switched to that has no chat shows the + * blank chat. Off when the panel mounts, so a chat whose tab is not active + * keeps its restore path. + */ + async function followTab( + workflow: ComfyWorkflow, + { blankWhenNoChat = true } = {} + ): Promise { dispatch({ type: 'requested', workflow, heldBack: heldBack(workflow) }) const operation = state.value if (operation.status !== 'loading') return try { + // A temporary tab is never followed into a chat (one the agent created + // may be bound to one), but a fresh one with no binding has no chat. + if (workflow.isTemporary) { + if (blankWhenNoChat && deps.cloudIdFor(workflow) === undefined) + showBlankChat(operation) + return + } const workflowId = await resolveWorkflowId(workflow) if (workflowId === undefined || !isCurrent(operation)) return + if (workflowId === null) { + if (blankWhenNoChat) showBlankChat(operation) + return + } if ( !(await refreshForFollow(workflowId, operation)) || !isCurrent(operation) ) return - await openLatestChat(workflowId, operation) + await openLatestChat(workflowId, operation, blankWhenNoChat) } finally { dispatch({ type: 'finished', generation: operation.generation }) } From 7b80c738e6a5df29ddd0703795d60df2df7e8f05 Mon Sep 17 00:00:00 2001 From: heymemie <94387204+heymemie@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:36:16 -0800 Subject: [PATCH 2/3] refactor(agent): split followTab to clear fallow complexity finding Move the temporary-tab and saved-tab paths into their own functions so followTab only starts and finishes the follow. No behavior change. --- .../agent/useAgentWorkflowChatFollow.ts | 54 ++++++++++++------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts index 2992e6e192c..e397a4d917e 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts @@ -233,6 +233,38 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { await openFollowedChat(latest, operation, workflowId) } + /** + * A temporary tab is never followed into a chat (one the agent created may + * be bound to one), but a fresh one with no binding has no chat. + */ + function followTemporaryTab( + workflow: ComfyWorkflow, + operation: FollowOperation, + blankWhenNoChat: boolean + ): void { + if (blankWhenNoChat && deps.cloudIdFor(workflow) === undefined) + showBlankChat(operation) + } + + async function followSavedTab( + workflow: ComfyWorkflow, + operation: FollowOperation, + blankWhenNoChat: boolean + ): Promise { + const workflowId = await resolveWorkflowId(workflow) + if (workflowId === undefined || !isCurrent(operation)) return + if (workflowId === null) { + if (blankWhenNoChat) showBlankChat(operation) + return + } + if ( + !(await refreshForFollow(workflowId, operation)) || + !isCurrent(operation) + ) + return + await openLatestChat(workflowId, operation, blankWhenNoChat) + } + /** * `blankWhenNoChat`: a tab the user switched to that has no chat shows the * blank chat. Off when the panel mounts, so a chat whose tab is not active @@ -246,25 +278,9 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { const operation = state.value if (operation.status !== 'loading') return try { - // A temporary tab is never followed into a chat (one the agent created - // may be bound to one), but a fresh one with no binding has no chat. - if (workflow.isTemporary) { - if (blankWhenNoChat && deps.cloudIdFor(workflow) === undefined) - showBlankChat(operation) - return - } - const workflowId = await resolveWorkflowId(workflow) - if (workflowId === undefined || !isCurrent(operation)) return - if (workflowId === null) { - if (blankWhenNoChat) showBlankChat(operation) - return - } - if ( - !(await refreshForFollow(workflowId, operation)) || - !isCurrent(operation) - ) - return - await openLatestChat(workflowId, operation, blankWhenNoChat) + if (workflow.isTemporary) + followTemporaryTab(workflow, operation, blankWhenNoChat) + else await followSavedTab(workflow, operation, blankWhenNoChat) } finally { dispatch({ type: 'finished', generation: operation.generation }) } From 6cbab4882ddf22522a1e6ab1121d1d7469c9ed1e Mon Sep 17 00:00:00 2001 From: heymemie <94387204+heymemie@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:44:06 -0800 Subject: [PATCH 3/3] refactor(agent): drop comments and type the workflow id lookup Bring the blank-chat follow in line with the repo's no-comment rule and failure-as-data guidance. - Replace the string | null | undefined cloud id with a found / unlisted / failed lookup. Unlisted means a complete cloud listing has no record of the tab, so it can have no chat; failed keeps the current chat. - A temporary tab is never followed into a chat, since one the agent created may be bound to one; a fresh one with no cloud binding gets the blank chat. - blankWhenNoChat is off on panel mount so a chat whose tab is not active keeps its restore path. - startBlankChatForTab leaves errorAttachRun alone, so an error attach that follows into a tab with no chat still completes. The follow only runs when the user is not engaged, so there is nothing to cancel. - Fold the blank-chat follow tests into one table. --- src/composables/useCoreCommands.ts | 1 - .../extensions/agent/AgentPanelRoot.test.ts | 2 - .../extensions/agent/AgentPanelRoot.vue | 3 - .../agent/useAgentWorkflowChatFollow.test.ts | 156 ++++++++---------- .../agent/useAgentWorkflowChatFollow.ts | 47 ++---- 5 files changed, 88 insertions(+), 121 deletions(-) diff --git a/src/composables/useCoreCommands.ts b/src/composables/useCoreCommands.ts index 63a431a76e6..b29652780b9 100644 --- a/src/composables/useCoreCommands.ts +++ b/src/composables/useCoreCommands.ts @@ -176,7 +176,6 @@ export function useCoreCommands(): ComfyCommand[] { function: async () => { const previousWorkflowHadNodes = app.rootGraph._nodes.length > 0 await workflowService.loadBlankWorkflow() - // A new tab is a tab the user switched to: the agent panel follows it. const created = workflowStore.activeWorkflow if (created) workflowStore.reportUserActivation(created) telemetry?.trackWorkflowCreated({ diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 36857459976..6226319b0c6 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -7267,8 +7267,6 @@ describe('AgentPanelRoot history', () => { await vi.waitFor(() => expect(userPrompts()).toEqual(['Prompt in th-open']) ) - // A draft holds the open chat on screen across the switch to a tab with - // no chat; clearing it afterwards does not re-run the follow. await userEvent.type(screen.getByRole('textbox'), 'Draft') await activateTabAsUser(second) await settleFollow() diff --git a/src/workbench/extensions/agent/AgentPanelRoot.vue b/src/workbench/extensions/agent/AgentPanelRoot.vue index ecd83656663..e9ae3ed7c7f 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1933,9 +1933,6 @@ const workflowChatFollow = useAgentWorkflowChatFollow({ startBlankChat: startBlankChatForTab }) -// Follow-only reset: the user is not engaged (no draft, send or selection), so -// there is nothing to cancel. Leaves `errorAttachRun` alone, so an error attach -// that follows into a tab with no chat still completes. function startBlankChatForTab(): void { provisionalTitle.value = undefined newChat() diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts index 74504fd52de..d62168d0b61 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.test.ts @@ -86,92 +86,76 @@ describe('useAgentWorkflowChatFollow', () => { expect(deps.startBlankChat).not.toHaveBeenCalled() }) - it('shows a blank chat for a saved tab that has no chat', async () => { - const workflow = savedWorkflow('workflows/landscape.json') - const { deps, follow } = setup(workflow, { - cloudIdFor: () => 'wf-landscape' - }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).toHaveBeenCalledOnce() + it.for<{ + name: string + workflow: () => ComfyWorkflow + overrides: Partial + blankWhenNoChat?: boolean + blankChats: number + }>([ + { + name: 'shows a blank chat for a saved tab that has no chat', + workflow: () => savedWorkflow('workflows/landscape.json'), + overrides: { cloudIdFor: () => 'wf-landscape' }, + blankChats: 1 + }, + { + name: 'shows a blank chat for a saved tab the cloud listing has no record of', + workflow: () => savedWorkflow('workflows/unsynced.json'), + overrides: { cloudIdFor: () => undefined }, + blankChats: 1 + }, + { + name: 'shows a blank chat for a new, unsaved tab', + workflow: () => unsavedWorkflow('workflows/Unsaved Workflow.json'), + overrides: { cloudIdFor: () => undefined }, + blankChats: 1 + }, + { + name: 'keeps the current chat for a tab with no chat when the panel mounts', + workflow: () => savedWorkflow('workflows/landscape.json'), + overrides: { cloudIdFor: () => 'wf-landscape' }, + blankWhenNoChat: false, + blankChats: 0 + }, + { + name: 'keeps the current chat when the cloud listing fails', + workflow: () => savedWorkflow('workflows/unsynced.json'), + overrides: { + cloudIdFor: () => undefined, + refreshCloudWorkflowIds: async () => false + }, + blankChats: 0 + }, + { + name: 'leaves an unsaved tab the agent bound to a chat alone', + workflow: () => unsavedWorkflow('workflows/second.json'), + overrides: { cloudIdFor: () => 'wf-second' }, + blankChats: 0 + }, + { + name: 'does not reset a blank chat that already follows the visible tab', + workflow: () => unsavedWorkflow('workflows/Unsaved Workflow (2).json'), + overrides: { + cloudIdFor: () => undefined, + currentThreadId: () => null, + followsVisibleWorkflow: () => true + }, + blankChats: 0 + }, + { + name: 'keeps the current chat while the user has a draft or a send in flight', + workflow: () => unsavedWorkflow('workflows/Unsaved Workflow.json'), + overrides: { cloudIdFor: () => undefined, userEngaged: () => true }, + blankChats: 0 + } + ])('$name', async ({ overrides, blankWhenNoChat, blankChats, ...row }) => { + const workflow = row.workflow() + const { deps, follow } = setup(workflow, overrides) + + await follow.followTab(workflow, { blankWhenNoChat }) + + expect(deps.startBlankChat).toHaveBeenCalledTimes(blankChats) expect(deps.loadThread).not.toHaveBeenCalled() }) - - it('shows a blank chat for a saved tab the cloud listing has no record of', async () => { - const workflow = savedWorkflow('workflows/unsynced.json') - const { deps, follow } = setup(workflow, { cloudIdFor: () => undefined }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).toHaveBeenCalledOnce() - }) - - it('keeps the current chat for a tab with no chat when the panel mounts', async () => { - const workflow = savedWorkflow('workflows/landscape.json') - const { deps, follow } = setup(workflow, { - cloudIdFor: () => 'wf-landscape' - }) - - await follow.followTab(workflow, { blankWhenNoChat: false }) - - expect(deps.startBlankChat).not.toHaveBeenCalled() - }) - - it('keeps the current chat when the cloud listing fails', async () => { - const workflow = savedWorkflow('workflows/unsynced.json') - const { deps, follow } = setup(workflow, { - cloudIdFor: () => undefined, - refreshCloudWorkflowIds: async () => false - }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).not.toHaveBeenCalled() - }) - - it('shows a blank chat for a new, unsaved tab', async () => { - const workflow = unsavedWorkflow('workflows/Unsaved Workflow.json') - const { deps, follow } = setup(workflow, { cloudIdFor: () => undefined }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).toHaveBeenCalledOnce() - expect(deps.loadThread).not.toHaveBeenCalled() - }) - - it('leaves an unsaved tab the agent bound to a chat alone', async () => { - const workflow = unsavedWorkflow('workflows/second.json') - const { deps, follow } = setup(workflow, { cloudIdFor: () => 'wf-second' }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).not.toHaveBeenCalled() - expect(deps.loadThread).not.toHaveBeenCalled() - }) - - it('does not reset a blank chat that already follows the visible tab', async () => { - const workflow = unsavedWorkflow('workflows/Unsaved Workflow (2).json') - const { deps, follow } = setup(workflow, { - cloudIdFor: () => undefined, - currentThreadId: () => null, - followsVisibleWorkflow: () => true - }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).not.toHaveBeenCalled() - }) - - it('keeps the current chat while the user has a draft or a send in flight', async () => { - const workflow = unsavedWorkflow('workflows/Unsaved Workflow.json') - const { deps, follow } = setup(workflow, { - cloudIdFor: () => undefined, - userEngaged: () => true - }) - - await follow.followTab(workflow) - - expect(deps.startBlankChat).not.toHaveBeenCalled() - }) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts index e397a4d917e..4d88b58be55 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts @@ -56,10 +56,14 @@ type FollowDeps = { ) => Promise | boolean adoptWorkflowTarget: (workflow: ComfyWorkflow, workflowId: string) => void adoptRestoredWorkflowTab: (workflowId: string) => void - /** Shows the blank new-chat screen, targeting the visible tab. */ startBlankChat: () => void } +type WorkflowIdLookup = + | { status: 'found'; workflowId: string } + | { status: 'unlisted' } + | { status: 'failed' } + function requestedState( state: FollowState, event: Extract @@ -181,23 +185,17 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { deps.trackThreadStarted() } - /** - * The tab's cloud workflow id; `null` when a complete listing has no record - * of it (so it can have no chat); `undefined` when the listing failed. - */ - async function resolveWorkflowId( + async function lookUpWorkflowId( workflow: ComfyWorkflow - ): Promise { + ): Promise { const existing = deps.cloudIdFor(workflow) - if (existing !== undefined) return existing + if (existing !== undefined) return { status: 'found', workflowId: existing } const listed = await deps.refreshCloudWorkflowIds() - return deps.cloudIdFor(workflow) ?? (listed ? null : undefined) + const workflowId = deps.cloudIdFor(workflow) + if (workflowId !== undefined) return { status: 'found', workflowId } + return { status: listed ? 'unlisted' : 'failed' } } - /** - * A tab with no chat shows the blank new-chat screen for that tab, instead - * of leaving the previous tab's chat (and its workflow target) on screen. - */ function showBlankChat(operation: FollowOperation): void { if (!isCurrent(operation)) return if (deps.currentThreadId() === null && deps.followsVisibleWorkflow()) return @@ -233,17 +231,13 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { await openFollowedChat(latest, operation, workflowId) } - /** - * A temporary tab is never followed into a chat (one the agent created may - * be bound to one), but a fresh one with no binding has no chat. - */ function followTemporaryTab( workflow: ComfyWorkflow, operation: FollowOperation, blankWhenNoChat: boolean ): void { - if (blankWhenNoChat && deps.cloudIdFor(workflow) === undefined) - showBlankChat(operation) + const boundToCloudWorkflow = deps.cloudIdFor(workflow) !== undefined + if (blankWhenNoChat && !boundToCloudWorkflow) showBlankChat(operation) } async function followSavedTab( @@ -251,25 +245,20 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { operation: FollowOperation, blankWhenNoChat: boolean ): Promise { - const workflowId = await resolveWorkflowId(workflow) - if (workflowId === undefined || !isCurrent(operation)) return - if (workflowId === null) { + const lookup = await lookUpWorkflowId(workflow) + if (lookup.status === 'failed' || !isCurrent(operation)) return + if (lookup.status === 'unlisted') { if (blankWhenNoChat) showBlankChat(operation) return } if ( - !(await refreshForFollow(workflowId, operation)) || + !(await refreshForFollow(lookup.workflowId, operation)) || !isCurrent(operation) ) return - await openLatestChat(workflowId, operation, blankWhenNoChat) + await openLatestChat(lookup.workflowId, operation, blankWhenNoChat) } - /** - * `blankWhenNoChat`: a tab the user switched to that has no chat shows the - * blank chat. Off when the panel mounts, so a chat whose tab is not active - * keeps its restore path. - */ async function followTab( workflow: ComfyWorkflow, { blankWhenNoChat = true } = {}