Skip to content
Closed
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
170 changes: 168 additions & 2 deletions src/workbench/extensions/agent/AgentPanelRoot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const getServerFeature = vi.hoisted(() =>
vi.fn((_name: string, defaultValue?: unknown) => defaultValue)
)
const focusNodeInstance = vi.hoisted(() => vi.fn())
const socketSend = vi.hoisted(() => vi.fn())

vi.mock('@/composables/canvas/useFocusNode', () => ({
useFocusNode: () => ({ focusNodeInstance })
Expand Down Expand Up @@ -61,7 +62,7 @@ vi.mock('@/scripts/api', () => ({
fetchApi: (route: string, options?: RequestInit) =>
fetch(route.startsWith('/api') ? route : `/api${route}`, options),
getServerFeature,
socket: { readyState: 1, send: vi.fn() },
socket: { readyState: 1, send: socketSend },
addEventListener: ws.add,
removeEventListener: ws.remove,
addCustomEventListener: ws.add,
Expand Down Expand Up @@ -314,6 +315,7 @@ beforeEach(() => {
workflowService.saveWorkflowAs.mockClear()
workflowService.openWorkflow.mockClear()
focusNodeInstance.mockReset()
socketSend.mockReset()
})

const zAgentWsEventForTest = (raw: unknown): AgentChatEvent =>
Expand Down Expand Up @@ -1359,14 +1361,16 @@ describe('AgentPanelRoot canvas draft on send', () => {
],
links: []
})
hostStores.workflow.activeWorkflow = {
const activeWorkflow = {
path: 'workflows/video_minimax_h3_i2v.json',
directory: 'workflows',
filename: 'video_minimax_h3_i2v',
isTemporary: false,
isModified: false,
activeState
}
hostStores.workflow.tabs.set(activeWorkflow.path, activeWorkflow)
hostStores.workflow.activeWorkflow = activeWorkflow

render(AgentPanelRoot, { global: { plugins: [i18n] } })

Expand Down Expand Up @@ -2844,6 +2848,56 @@ describe('AgentPanelRoot workflow binding', () => {
expect(bodies[1]).not.toHaveProperty('current_tab')
})

it('keeps an existing thread workflow on its own tab when sending from an unsaved tab', async () => {
const origin = makeTab('wf-42')
const bodies = mockMessagesEndpoint('wf-42')

await renderAndSend('first message')
ws.emit('agent_message_done', { message_id: 'm-1', thread_id: 'th-1' })
await screen.findByRole('button', { name: 'Send' })

const scratch = addTab('workflows/Scratch.json', { isTemporary: true })
hostStores.workflow.activeWorkflow = scratch
await nextTick()
socketSend.mockClear()

await sendFromComposer('second message')

expect(bodies[1]).not.toHaveProperty('workflow_id')
expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-42')).toBe(
origin.path
)
expect(
useAgentWorkflowTabBindingStore().workflowIdFor(scratch.path)
).toBeUndefined()
expect(
socketSend.mock.calls.some(([frame]) =>
String(frame).includes('doc_subscribe')
)
).toBe(false)
})

it('does not bind an unsaved tab to a workflow that already has an open tab', async () => {
addTab('workflows/current.json')
const scratch = addTab('workflows/Scratch.json', { isTemporary: true })
hostStores.workflow.activeWorkflow = scratch
mockMessagesEndpoint('wf-42', [{ id: 'wf-42', name: 'current' }])

await renderAndSend('first message')

expect(
useAgentWorkflowTabBindingStore().workflowIdFor(scratch.path)
).toBeUndefined()
expect(
useAgentWorkflowTabBindingStore().tabPathFor('wf-42')
).toBeUndefined()
expect(
socketSend.mock.calls.some(([frame]) =>
String(frame).includes('doc_subscribe')
)
).toBe(false)
})

it('stages a mention pick once and reports the tag gesture', async () => {
makeTab('wf-42')
mockMessagesEndpoint('wf-42')
Expand Down Expand Up @@ -2961,6 +3015,118 @@ describe('AgentPanelRoot workflow binding', () => {
expect(app.loadGraphData).not.toHaveBeenCalled()
})

it('binds a minted workflow to its unsaved tab and subscribes once', async () => {
const tab = makeTab()
tab.isTemporary = true
mockMessagesEndpoint('wf-fresh')

await renderAndSend('build a graph')

expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-fresh')).toBe(
tab.path
)
const subscribes = socketSend.mock.calls
.map(
([frame]) =>
JSON.parse(String(frame)) as { type: string; data: unknown }
)
.filter(({ type }) => type === 'doc_subscribe')
expect(subscribes).toEqual([
expect.objectContaining({
data: expect.objectContaining({ workflow_id: 'wf-fresh' })
})
])
})

it('does not subscribe a minted workflow after its tab is backgrounded', async () => {
const origin = makeTab()
origin.isTemporary = true
const background = addTab('workflows/background.json')
vi.stubGlobal(
'fetch',
vi.fn(async (url: string, init?: RequestInit) => {
if (url.includes('/messages') && init?.method === 'POST') {
hostStores.workflow.activeWorkflow = background
return json(202, ack('wf-fresh', 'm-1'))
}
if (url.includes('/agent/threads'))
return json(200, { threads: [], pagination: { page: 1 } })
if (url.includes('/workflows'))
return json(200, {
data: [],
pagination: {
offset: 0,
limit: 100,
total: 0,
has_more: false
}
})
return new Response('{}', { status: 200 })
})
)

await renderAndSend('build a graph')

expect(useAgentWorkflowTabBindingStore().tabPathFor('wf-fresh')).toBe(
origin.path
)
expect(
socketSend.mock.calls.some(([frame]) =>
String(frame).includes('doc_subscribe')
)
).toBe(false)
})

it('does not reattribute a send when its originating tab closes during preparation', async () => {
const origin = makeTab('wf-origin')
origin.activeState = fromPartial<ComfyWorkflowJSON>({ id: 'origin-draft' })
const replacement = addTab('workflows/replacement.json', {
activeState: fromPartial<ComfyWorkflowJSON>({ id: 'replacement-draft' })
})
useAgentWorkflowTabBindingStore().bind('wf-replacement', replacement.path)
const bodies: Record<string, unknown>[] = []
let workflowRequests = 0
let releasePreparation: () => void = () => undefined
vi.stubGlobal(
'fetch',
vi.fn(async (url: string, init?: RequestInit) => {
if (url.includes('/messages') && init?.method === 'POST') {
bodies.push(JSON.parse(String(init.body)) as Record<string, unknown>)
return json(202, ack('wf-fresh', 'm-1'))
}
if (url.includes('/messages')) return json(200, [])
if (url.includes('/agent/threads'))
return json(200, { threads: [], pagination: { page: 1 } })
if (url.includes('/workflows')) {
workflowRequests++
if (workflowRequests > 1)
await new Promise<void>((resolve) => {
releasePreparation = resolve
})
return json(200, {
data: [],
pagination: { offset: 0, limit: 100, total: 0, has_more: false }
})
}
return new Response('{}', { status: 200 })
})
)

render(AgentPanelRoot, { global: { plugins: [i18n] } })
await vi.waitFor(() => expect(workflowRequests).toBe(1))
await userEvent.type(screen.getByRole('textbox'), 'build a graph')
await userEvent.click(screen.getByRole('button', { name: 'Send' }))
await vi.waitFor(() => expect(workflowRequests).toBe(2))
hostStores.workflow.tabs.delete(origin.path)
hostStores.workflow.activeWorkflow = replacement
releasePreparation()

await vi.waitFor(() => expect(bodies).toHaveLength(1))
expect(bodies[0]).not.toHaveProperty('workflow_id')
expect(bodies[0]).not.toHaveProperty('current_tab')
expect(bodies[0]).not.toHaveProperty('draft')
})

it('sends only the remaining chip after one is dismissed', async () => {
makeTab()
const bodies = mockMessagesEndpoint('wf-42')
Expand Down
49 changes: 39 additions & 10 deletions src/workbench/extensions/agent/AgentPanelRoot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,36 @@ function cloudIdFor(tab: ComfyWorkflow): string | undefined {

const workflowDetached = ref(false)

function activeWorkflowTurnContext(): WorkflowTurnContext | undefined {
function activeWorkflowTurnContext(
originTabPath?: string
): WorkflowTurnContext | undefined {
if (workflowDetached.value) return undefined
const active = workflowStore.activeWorkflow
const active =
originTabPath === undefined
? workflowStore.activeWorkflow
: workflowStore.getWorkflowByPath(originTabPath)
if (!active) return undefined
const bound = cloudIdFor(active)
return bound === undefined ? undefined : { id: bound, tabPath: active.path }
const id = cloudIdFor(active)
return id === undefined
? { tabPath: active.path }
Comment thread
christian-byrne marked this conversation as resolved.
Resolved
: { id, tabPath: active.path }
}

function activeWorkflowDraft(): DraftSnapshot | undefined {
function activeWorkflowDraft(
originTabPath?: string
): DraftSnapshot | undefined {
if (workflowDetached.value) return undefined
const active = workflowStore.activeWorkflow
const active =
originTabPath === undefined
? workflowStore.activeWorkflow
: workflowStore.getWorkflowByPath(originTabPath)
if (!active) return undefined
active.changeTracker?.captureCanvasState()
// captureCanvasState() folds the LIVE canvas into whichever workflow it is
// called on, so it is only correct while that workflow is still the active
// tab. If the user switched away mid-turn, the originating tab's own
// serialized activeState is the snapshot that belongs with this send.
if (active.path === workflowStore.activeWorkflow?.path)
active.changeTracker?.captureCanvasState()
const content = active.activeState
if (!content) return undefined
return { content }
Expand Down Expand Up @@ -341,15 +358,20 @@ function onClearWorkflow(): void {
workflowDetached.value = true
}

function openTabsSnapshot(): OpenTabsSnapshot | undefined {
function openTabsSnapshot(
originTabPath?: string
): OpenTabsSnapshot | undefined {
const openTabs = workflowStore.openWorkflows.flatMap((tab) => {
const workflowId = cloudIdFor(tab)
return workflowId === undefined
? []
: [{ workflow_id: workflowId, name: tab.filename }]
})
if (openTabs.length === 0) return undefined
const active = workflowStore.activeWorkflow
const active =
originTabPath === undefined
? workflowStore.activeWorkflow
: workflowStore.getWorkflowByPath(originTabPath)
return {
open_tabs: openTabs,
current_tab:
Expand All @@ -361,7 +383,14 @@ function onWorkflowAdopted(
workflowId: string,
sent: WorkflowTurnContext | undefined
): void {
if (sent !== undefined && sent.id === workflowId) {
if (sent === undefined) return
// An unbound tab adopts a workflow only when it was minted for this turn:
// an id that already resolves to an open tab belongs to that tab.
const adoptable =
sent.id === undefined
? boundTabFor(workflowId) === null
: sent.id === workflowId
if (adoptable) {
bindingStore.bind(workflowId, sent.tabPath)
tabActivity.setEditing(sent.tabPath)
}
Expand Down
Loading
Loading