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..b29652780b9 100644 --- a/src/composables/useCoreCommands.ts +++ b/src/composables/useCoreCommands.ts @@ -176,6 +176,8 @@ export function useCoreCommands(): ComfyCommand[] { function: async () => { const previousWorkflowHadNodes = app.rootGraph._nodes.length > 0 await workflowService.loadBlankWorkflow() + 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..6226319b0c6 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,10 @@ describe('AgentPanelRoot history', () => { await vi.waitFor(() => expect(userPrompts()).toEqual(['Prompt in th-open']) ) + 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..e9ae3ed7c7f 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.vue +++ b/src/workbench/extensions/agent/AgentPanelRoot.vue @@ -1929,9 +1929,16 @@ const workflowChatFollow = useAgentWorkflowChatFollow({ useTelemetry()?.trackAgentThreadStarted({ source: 'history_select' }), restoreThreadWorkflow, adoptWorkflowTarget: adoptFollowedWorkflow, - adoptRestoredWorkflowTab + adoptRestoredWorkflowTab, + startBlankChat: startBlankChatForTab }) +function startBlankChatForTab(): void { + provisionalTitle.value = undefined + newChat() + agentPanelStore.startFollowingVisibleWorkflow() +} + const { failure: followFailure, openingWorkflowName, @@ -2010,7 +2017,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..d62168d0b61 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,93 @@ 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.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() }) }) diff --git a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts index c9c3fd435c1..4d88b58be55 100644 --- a/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts +++ b/src/workbench/extensions/agent/composables/agent/useAgentWorkflowChatFollow.ts @@ -56,8 +56,14 @@ type FollowDeps = { ) => Promise | boolean adoptWorkflowTarget: (workflow: ComfyWorkflow, workflowId: string) => void adoptRestoredWorkflowTab: (workflowId: string) => void + startBlankChat: () => void } +type WorkflowIdLookup = + | { status: 'found'; workflowId: string } + | { status: 'unlisted' } + | { status: 'failed' } + function requestedState( state: FollowState, event: Extract @@ -133,7 +139,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 +185,21 @@ export function useAgentWorkflowChatFollow(deps: FollowDeps) { deps.trackThreadStarted() } - async function resolveWorkflowId( + async function lookUpWorkflowId( workflow: ComfyWorkflow - ): Promise { + ): Promise { const existing = deps.cloudIdFor(workflow) - if (existing !== undefined) return existing - await deps.refreshCloudWorkflowIds() - return deps.cloudIdFor(workflow) + if (existing !== undefined) return { status: 'found', workflowId: existing } + const listed = await deps.refreshCloudWorkflowIds() + const workflowId = deps.cloudIdFor(workflow) + if (workflowId !== undefined) return { status: 'found', workflowId } + return { status: listed ? 'unlisted' : 'failed' } + } + + function showBlankChat(operation: FollowOperation): void { + if (!isCurrent(operation)) return + if (deps.currentThreadId() === null && deps.followsVisibleWorkflow()) return + deps.startBlankChat() } async function refreshForFollow( @@ -207,26 +221,55 @@ 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 { + function followTemporaryTab( + workflow: ComfyWorkflow, + operation: FollowOperation, + blankWhenNoChat: boolean + ): void { + const boundToCloudWorkflow = deps.cloudIdFor(workflow) !== undefined + if (blankWhenNoChat && !boundToCloudWorkflow) showBlankChat(operation) + } + + async function followSavedTab( + workflow: ComfyWorkflow, + operation: FollowOperation, + blankWhenNoChat: boolean + ): Promise { + const lookup = await lookUpWorkflowId(workflow) + if (lookup.status === 'failed' || !isCurrent(operation)) return + if (lookup.status === 'unlisted') { + if (blankWhenNoChat) showBlankChat(operation) + return + } + if ( + !(await refreshForFollow(lookup.workflowId, operation)) || + !isCurrent(operation) + ) + return + await openLatestChat(lookup.workflowId, operation, blankWhenNoChat) + } + + 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 { - const workflowId = await resolveWorkflowId(workflow) - if (workflowId === undefined || !isCurrent(operation)) return - if ( - !(await refreshForFollow(workflowId, operation)) || - !isCurrent(operation) - ) - return - await openLatestChat(workflowId, operation) + if (workflow.isTemporary) + followTemporaryTab(workflow, operation, blankWhenNoChat) + else await followSavedTab(workflow, operation, blankWhenNoChat) } finally { dispatch({ type: 'finished', generation: operation.generation }) }