diff --git a/browser_tests/tests/agent/agentPanelMocks.ts b/browser_tests/tests/agent/agentPanelMocks.ts index bb81a7200c8..b4a431926e3 100644 --- a/browser_tests/tests/agent/agentPanelMocks.ts +++ b/browser_tests/tests/agent/agentPanelMocks.ts @@ -40,9 +40,9 @@ export const THINKING_EVENT: AgentWsEvent = { export const TOOL_CALL_EVENT: AgentWsEvent = { type: 'agent_tool_call', data: { + tool_call_id: 'call-set-widget', tool_name: 'set_widget', - status: 'ok', - args: ['workflow', 'set-widget', 'workflow.json'], + status: 'success', duration_ms: 1300, message_id: TURN_ID, thread_id: THREAD_ID @@ -70,9 +70,9 @@ export const RESUMED_THINKING_EVENT: AgentWsEvent = { export const OPEN_TAB_TOOL_EVENT: AgentWsEvent = { type: 'agent_tool_call', data: { + tool_call_id: 'call-new-tab', tool_name: 'new_tab', - status: 'ok', - args: [], + status: 'success', duration_ms: 500, message_id: TURN_ID, thread_id: THREAD_ID @@ -82,9 +82,9 @@ export const OPEN_TAB_TOOL_EVENT: AgentWsEvent = { export const RESIZE_IMAGE_TOOL_EVENT: AgentWsEvent = { type: 'agent_tool_call', data: { + tool_call_id: 'call-resize-image-node', tool_name: 'resize_image_node', - status: 'ok', - args: [], + status: 'success', duration_ms: 200, message_id: TURN_ID, thread_id: THREAD_ID @@ -236,6 +236,7 @@ type AgentFixtures = { export const agentTest = comfyPageFixture.extend({ agentFlagEnabled: [true, { option: true }], + // oxlint-disable-next-line no-empty-pattern -- Playwright requires an object pattern. postedMessages: async ({}, use) => { await use([]) }, diff --git a/src/workbench/extensions/agent/AgentPanelRoot.test.ts b/src/workbench/extensions/agent/AgentPanelRoot.test.ts index 85879b04876..be115ebd5c0 100644 --- a/src/workbench/extensions/agent/AgentPanelRoot.test.ts +++ b/src/workbench/extensions/agent/AgentPanelRoot.test.ts @@ -1687,9 +1687,9 @@ describe('AgentPanelRoot transcript copy', () => { zAgentWsEventForTest({ type: 'agent_tool_call', data: { + tool_call_id: 'call-add-node', tool_name: 'add_node', - status: 'ok', - args: [], + status: 'success', message_id: 'turn-1', thread_id: 'th-1' } diff --git a/src/workbench/extensions/agent/components/agent/ConversationView.test.ts b/src/workbench/extensions/agent/components/agent/ConversationView.test.ts index a4cb1c6dad9..6587e1dfbcb 100644 --- a/src/workbench/extensions/agent/components/agent/ConversationView.test.ts +++ b/src/workbench/extensions/agent/components/agent/ConversationView.test.ts @@ -55,7 +55,13 @@ const delta = (id: string, text: string) => const toolCall = (id: string, name: string, status: string) => chat({ type: 'agent_tool_call', - data: { tool_name: name, status, args: [], message_id: id, thread_id: 'th' } + data: { + tool_call_id: `call-${name}`, + tool_name: name, + status, + message_id: id, + thread_id: 'th' + } }) const done = (id: string) => chat({ @@ -95,7 +101,7 @@ describe('ConversationView', () => { expect(await screen.findByText('pondering')).toBeInTheDocument() store.ingest(delta('msg-1', 'Here is a **cat**')) - store.ingest(toolCall('msg-1', 'add_node', 'ok')) + store.ingest(toolCall('msg-1', 'add_node', 'success')) store.ingest(done('msg-1')) expect(await screen.findByText('make a cat')).toBeInTheDocument() diff --git a/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts b/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts index 55321ddf252..fc28c0f2fc2 100644 --- a/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts +++ b/src/workbench/extensions/agent/components/agent/message/AgentMessage.test.ts @@ -75,9 +75,9 @@ describe('AgentMessage thinking narration', () => { transport.ingest({ type: 'agent_tool_call', data: { + tool_call_id: 'call-set-widget', tool_name: 'set_widget', - status: 'ok', - args: [], + status: 'success', message_id: 'msg-0', thread_id: 'thread-0' } diff --git a/src/workbench/extensions/agent/schemas/__fixtures__/agent/ws-turn-edit.jsonl b/src/workbench/extensions/agent/schemas/__fixtures__/agent/ws-turn-edit.jsonl index 6dc03130881..0e513936f15 100644 --- a/src/workbench/extensions/agent/schemas/__fixtures__/agent/ws-turn-edit.jsonl +++ b/src/workbench/extensions/agent/schemas/__fixtures__/agent/ws-turn-edit.jsonl @@ -1,9 +1,13 @@ {"capturedAt":1783366827411,"frame":{"type":"status","data":{"status":{"exec_info":{"queue_remaining":0}},"sid":"c3bf440f-8131-481d-b46d-fffbf799d950"}}} {"capturedAt":1783366879318,"frame":{"data":{"delta":"I'll find the exact widget addresses first, then edit those two values in place.","message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718"},"type":"agent_thinking"}} -{"capturedAt":1783366881383,"frame":{"data":{"args":["workflow","slots","workflow.json"],"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"ok","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_name":"list_slots"},"type":"agent_tool_call"}} +{"capturedAt":1783366881382,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"running","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-list-slots","tool_name":"list_slots"},"type":"agent_tool_call"}} +{"capturedAt":1783366881383,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"success","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-list-slots","tool_name":"list_slots"},"type":"agent_tool_call"}} {"capturedAt":1783366883967,"frame":{"data":{"delta":"Let me get the full slot list to find the exact node addresses.","message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718"},"type":"agent_thinking"}} -{"capturedAt":1783366884944,"frame":{"data":{"args":["workflow","ls-nodes","workflow.json"],"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"ok","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_name":"ls_nodes"},"type":"agent_tool_call"}} -{"capturedAt":1783366889506,"frame":{"data":{"args":["workflow","set-widget","workflow.json","--actor","MjVkuhzz6vSuA19LWfBa9lMQdKv2","--base-version","24","--where","cloud","--","1906165638342264.steps","12"],"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"ok","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_name":"set_widget"},"type":"agent_tool_call"}} -{"capturedAt":1783366891339,"frame":{"data":{"args":["workflow","set-widget","workflow.json","--actor","MjVkuhzz6vSuA19LWfBa9lMQdKv2","--base-version","25","--where","cloud","--","1906165638342264.cfg","6.5"],"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"ok","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_name":"set_widget"},"type":"agent_tool_call"}} +{"capturedAt":1783366884943,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"running","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-ls-nodes","tool_name":"ls_nodes"},"type":"agent_tool_call"}} +{"capturedAt":1783366884944,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"success","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-ls-nodes","tool_name":"ls_nodes"},"type":"agent_tool_call"}} +{"capturedAt":1783366889505,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"running","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-set-steps","tool_name":"set_widget"},"type":"agent_tool_call"}} +{"capturedAt":1783366889506,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"success","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-set-steps","tool_name":"set_widget"},"type":"agent_tool_call"}} +{"capturedAt":1783366891338,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"running","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-set-cfg","tool_name":"set_widget"},"type":"agent_tool_call"}} +{"capturedAt":1783366891339,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","status":"success","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","tool_call_id":"call-set-cfg","tool_name":"set_widget"},"type":"agent_tool_call"}} {"capturedAt":1783366894953,"frame":{"data":{"delta":"Done — both values updated in place, nothing else touched:\n\n- **Steps:** 20 → **12**\n- **Guidance (CFG):** 7 → **6.5**\n\nThe rest of the graph is unchanged, and I didn't run anything. Let me know when you'd like to generate.","message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718"},"type":"agent_message_delta"}} {"capturedAt":1783366894960,"frame":{"data":{"message_id":"172a6ede-7ab7-4b01-83b6-5b15f66dee4b","thread_id":"d4c016c4-3b8c-44cf-97de-1ae27e43e718","usage":{"cache_creation_input_tokens":0,"cache_read_input_tokens":35596,"input_tokens":4493,"output_tokens":425,"total_tokens":4918}},"type":"agent_message_done"}} diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts index 30fb1123bca..0c4bcd87ceb 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.test.ts @@ -41,6 +41,10 @@ interface RestLine { describe('agentApiSchema fixture gate', () => { describe('ws frames: every line is a valid agent event or a recognized-foreign frame', () => { + it('the ws fixture glob finds at least one capture', () => { + expect(wsPaths.length).toBeGreaterThan(0) + }) + it.for(wsPaths)('%s', (path) => { const lines = jsonlLines(path) as WsLine[] lines.forEach((line, index) => { @@ -139,7 +143,7 @@ describe('agentApiSchema contract subtleties', () => { } }) - it('keeps the retired draft frames foreign to the union', () => { + it('keeps draft frames foreign to this union', () => { expect(isAgentEvent('draft_patch')).toBe(false) expect(isAgentEvent('draft_version')).toBe(false) }) @@ -159,6 +163,36 @@ describe('agentApiSchema contract subtleties', () => { expect(parsed.success).toBe(true) }) + it('accepts the canonical tool lifecycle and rejects the legacy shape', () => { + const data = { + tool_call_id: 'call-1', + tool_name: 'add_node', + message_id: 'm1', + thread_id: 't1' + } + + for (const status of ['running', 'success', 'error']) { + expect( + zAgentWsEvent.safeParse({ + type: 'agent_tool_call', + data: { ...data, status } + }).success + ).toBe(true) + } + expect( + zAgentWsEvent.safeParse({ + type: 'agent_tool_call', + data: { ...data, tool_call_id: undefined, status: 'success' } + }).success + ).toBe(false) + expect( + zAgentWsEvent.safeParse({ + type: 'agent_tool_call', + data: { ...data, status: 'success', args: [] } + }).success + ).toBe(false) + }) + it('exposes exactly the five agent event types', () => { expect([...AGENT_WS_EVENT_TYPES].sort()).toEqual( [ diff --git a/src/workbench/extensions/agent/schemas/agentApiSchema.ts b/src/workbench/extensions/agent/schemas/agentApiSchema.ts index 3aad9f390b7..7f08e695b6e 100644 --- a/src/workbench/extensions/agent/schemas/agentApiSchema.ts +++ b/src/workbench/extensions/agent/schemas/agentApiSchema.ts @@ -3,6 +3,7 @@ import { z } from 'zod' const zTurnId = z.string().brand<'TurnId'>() export type TurnId = z.infer +export const toTurnId = (value: string): TurnId => zTurnId.parse(value) export const zAgentTurnAccepted = z .object({ @@ -81,9 +82,10 @@ const zAgentThinkingData = z const zAgentToolCallData = z .object({ + tool_call_id: z.string(), tool_name: z.string(), - status: z.string(), - args: z.array(z.string()), + status: z.enum(['running', 'success', 'error']), + args: z.never().optional(), duration_ms: z.number().optional(), message_id: z.string(), thread_id: z.string() diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts index 5ebf311206a..9c030148840 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it, vi } from 'vitest' -import type { AgentWsEvent, TurnId } from '../../schemas/agentApiSchema' -import { zAgentWsEvent } from '../../schemas/agentApiSchema' +import type { AgentWsEvent } from '../../schemas/agentApiSchema' +import { toTurnId, zAgentWsEvent } from '../../schemas/agentApiSchema' import type { AgentChatEvent } from './agentEventTransport' import { createAgentEventTransport } from './agentEventTransport' @@ -53,7 +53,7 @@ function isChatEvent(event: AgentWsEvent): event is AgentChatEvent { ) } -const T = 't1' as TurnId +const T = toTurnId('t1') function drive(events: AgentChatEvent[]): AssistantMessage { const message = createAssistantMessage(T) @@ -70,10 +70,20 @@ function thinking(delta: string): AgentChatEvent { } } -function toolCall(tool_name: string, status: string): AgentChatEvent { +function toolCall( + tool_name: string, + status: 'running' | 'success' | 'error', + tool_call_id = `call-${tool_name}` +): AgentChatEvent { return { type: 'agent_tool_call', - data: { tool_name, status, args: [], message_id: 'm', thread_id: 't' } + data: { + tool_call_id, + tool_name, + status, + message_id: 'm', + thread_id: 't' + } } } @@ -162,7 +172,7 @@ describe('agentEventTransport thinking chip', () => { it('thinking after prior text and tools reopens the status', () => { const message = drive([ delta('before'), - toolCall('run', 'ok'), + toolCall('run', 'success'), delta('after'), thinking('Planning the next step') ]) @@ -188,15 +198,11 @@ describe('agentEventTransport thinking narration', () => { }) it('a tool call clears the live narration but retains the completed step', () => { - const now = vi - .spyOn(Date, 'now') - .mockReturnValueOnce(1000) - .mockReturnValueOnce(2300) + vi.spyOn(Date, 'now').mockReturnValueOnce(1000).mockReturnValueOnce(2300) const message = drive([ thinking('Adding a node'), - toolCall('add_node', 'ok') + toolCall('add_node', 'success') ]) - now.mockRestore() expect(message.thinkingText).toBeUndefined() expect(thinkingParts(message)).toEqual([ { @@ -211,12 +217,13 @@ describe('agentEventTransport thinking narration', () => { it('a tool call after thinking clears the thinking status', () => { const message = drive([ thinking('Adding a node'), - toolCall('add_node', 'ok') + toolCall('add_node', 'success') ]) expect(message.thinking).toBe(false) }) it('the first text delta clears the live narration but retains the step', () => { + vi.spyOn(Date, 'now').mockReturnValue(1000) const message = drive([thinking('Writing a reply'), delta('Here')]) expect(message.thinkingText).toBeUndefined() expect(thinkingParts(message)).toEqual([ @@ -228,6 +235,7 @@ describe('agentEventTransport thinking narration', () => { const message = createAssistantMessage(T) const emit = vi.fn<(m: AssistantMessage) => void>() const transport = createAgentEventTransport(message, emit) + vi.spyOn(Date, 'now').mockReturnValue(1000) transport.ingest(thinking('Wrapping up')) transport.settle() const final = emit.mock.calls.at(-1)?.[0] ?? message @@ -237,14 +245,35 @@ describe('agentEventTransport thinking narration', () => { ]) }) + it('each emit is a distinct snapshot whose parts array ignores later events', () => { + const message = createAssistantMessage(T) + const emit = vi.fn<(m: AssistantMessage) => void>() + const transport = createAgentEventTransport(message, emit) + + transport.ingest(thinking('First')) + const first = emit.mock.calls.at(-1)![0] + const firstPartsLength = first.parts.length + transport.ingest(toolCall('add_node', 'success')) + const second = emit.mock.calls.at(-1)![0] + + expect(second).not.toBe(first) + expect(second.parts).not.toBe(first.parts) + expect(first.parts).toHaveLength(firstPartsLength) + expect(first.parts[0]).toEqual({ + type: 'thinking', + text: 'First', + state: 'streaming' + }) + }) + it('retains alternating reasoning and tool events in transcript order', () => { + vi.spyOn(Date, 'now').mockReturnValue(1000) const message = drive([ thinking('Inspecting the graph'), - toolCall('list_slots', 'ok'), + toolCall('list_slots', 'success'), thinking('Applying the edit'), - toolCall('set_widget', 'ok') + toolCall('set_widget', 'success') ]) - expect(message.parts).toEqual([ { type: 'thinking', @@ -275,7 +304,7 @@ describe('agentEventTransport text and tool parts', () => { it('delta -> tool -> delta yields text, tool, text as three parts', () => { const message = drive([ delta('before'), - toolCall('run', 'ok'), + toolCall('run', 'success'), delta('after') ]) expect(parts(message).map((p) => p.type)).toEqual(['text', 'tool', 'text']) @@ -289,6 +318,24 @@ describe('agentEventTransport text and tool parts', () => { ok: false }) }) + + it('folds running and success frames into one tool lifecycle', () => { + const message = drive([ + toolCall('run', 'running', 'call-1'), + toolCall('run', 'success', 'call-1') + ]) + + expect(toolParts(message)).toEqual([ + { + type: 'tool', + callId: 'call-1', + name: 'run', + state: 'done', + ok: true, + durationMs: undefined + } + ]) + }) }) describe('agentEventTransport settle lifecycle', () => { @@ -326,7 +373,7 @@ describe('agentEventTransport settle lifecycle', () => { const callsAfterSettle = emit.mock.calls.length transport.ingest(delta(' late')) - transport.ingest(toolCall('late_tool', 'ok')) + transport.ingest(toolCall('late_tool', 'success')) expect(emit.mock.calls.length).toBe(callsAfterSettle) expect(textParts(message)[0]).toMatchObject({ @@ -357,11 +404,38 @@ describe('agentEventTransport settle lifecycle', () => { }) }) + it('closes thinking before recording a tab switch', () => { + const message = drive([ + thinking('opening the workflow'), + activeTab('wf-1', 'Portrait upscale'), + thinking('checking its nodes') + ]) + + expect(parts(message).map((part) => part.type)).toEqual([ + 'thinking', + 'tabLink', + 'thinking' + ]) + expect(thinkingParts(message)).toEqual([ + { + type: 'thinking', + text: 'opening the workflow', + state: 'done' + }, + { + type: 'thinking', + text: 'checking its nodes', + state: 'streaming' + } + ]) + expect(message.thinkingText).toBe('checking its nodes') + }) + it('links a tab once even when the agent keeps working between announcements', () => { const message = drive([ activeTab('wf-1', 'First'), delta('adding the nodes'), - toolCall('add_node', 'ok'), + toolCall('add_node', 'success'), activeTab('wf-1', 'First') ]) diff --git a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts index 09586120370..a4d94b49e51 100644 --- a/src/workbench/extensions/agent/services/agent/agentEventTransport.ts +++ b/src/workbench/extensions/agent/services/agent/agentEventTransport.ts @@ -32,7 +32,7 @@ export function createAgentEventTransport( let openText: TextPart | null = null let openThinking: ThinkingPart | null = null let openThinkingStartedAt = 0 - let toolCount = 0 + const tools = new Map() let settled = false let lastTabWorkflowId: string | undefined @@ -85,15 +85,23 @@ export function createAgentEventTransport( closeOpenThinking() message.thinking = false message.thinkingText = undefined - const part: ToolPart = { - type: 'tool', - callId: `tool_${toolCount++}`, - name: event.data.tool_name, - state: 'done', - ok: event.data.status === 'ok', - durationMs: event.data.duration_ms + let part = tools.get(event.data.tool_call_id) + if (!part) { + part = { + type: 'tool', + callId: event.data.tool_call_id, + name: event.data.tool_name, + state: 'streaming' + } + tools.set(event.data.tool_call_id, part) + message.parts.push(part) + } + part.name = event.data.tool_name + if (event.data.status !== 'running') { + part.state = 'done' + part.ok = event.data.status === 'success' + part.durationMs = event.data.duration_ms } - message.parts.push(part) break } case 'agent_active_tab': { @@ -103,6 +111,9 @@ export function createAgentEventTransport( if (lastTabWorkflowId === event.data.workflow_id) return lastTabWorkflowId = event.data.workflow_id closeOpenText() + closeOpenThinking() + message.thinking = false + message.thinkingText = undefined message.parts.push({ type: 'tabLink', workflowId: event.data.workflow_id, diff --git a/src/workbench/extensions/agent/services/agent/agentMessageParts.ts b/src/workbench/extensions/agent/services/agent/agentMessageParts.ts index 3f5038d1d0c..21a46dafbfb 100644 --- a/src/workbench/extensions/agent/services/agent/agentMessageParts.ts +++ b/src/workbench/extensions/agent/services/agent/agentMessageParts.ts @@ -60,5 +60,5 @@ export function createAssistantMessage(id: TurnId): AssistantMessage { } export function snapshotMessage(message: AssistantMessage): AssistantMessage { - return { ...message, parts: [...message.parts] } + return { ...message, parts: message.parts.map((part) => ({ ...part })) } } diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts index 47bd4fdce21..01607bbc675 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.test.ts @@ -1,5 +1,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' +import type { CloudWorkflowEntry } from '../../schemas/agentApiSchema' + const fetchApi = vi.hoisted(() => vi.fn<(route: string, init?: RequestInit) => Promise>() ) @@ -69,25 +71,46 @@ describe('agentRestClient route + method', () => { }) it('listCloudWorkflows GETs the paginated workflows path until has_more is false', async () => { - const page = (data: unknown[], hasMore: boolean) => + const page = ( + offset: number, + data: CloudWorkflowEntry[], + hasMore: boolean, + nextCursor?: string + ) => jsonResponse(200, { data, pagination: { - offset: 0, + offset, limit: 100, total: data.length, - has_more: hasMore + has_more: hasMore, + next_cursor: nextCursor } }) - respond(page([{ id: 'wf-1', name: 'one' }], true)) - respond(page([{ id: 'wf-2', name: 'two' }], false)) + respond(page(0, [{ id: 'wf-1', name: 'one' }], true, 'next page')) + respond(page(1, [{ id: 'wf-2', name: 'two' }], false)) const workflows = await makeClient().listCloudWorkflows() - expect(fetchApi.mock.calls[0][0]).toBe('/workflows?limit=100&offset=0') - expect(fetchApi.mock.calls[1][0]).toBe('/workflows?limit=100&offset=100') + expect(fetchApi.mock.calls[0][0]).toBe('/workflows?limit=100') + expect(fetchApi.mock.calls[1][0]).toBe( + '/workflows?limit=100&after=next%20page' + ) expect(workflows.map((w) => w.id)).toEqual(['wf-1', 'wf-2']) }) + + it('stops pagination when the server does not provide a new cursor', async () => { + respond( + jsonResponse(200, { + data: [], + pagination: { offset: 0, limit: 100, total: 0, has_more: true } + }) + ) + + await makeClient().listCloudWorkflows() + + expect(fetchApi).toHaveBeenCalledTimes(1) + }) }) describe('postMessage wire body', () => { diff --git a/src/workbench/extensions/agent/services/agent/agentRestClient.ts b/src/workbench/extensions/agent/services/agent/agentRestClient.ts index 9ec76235b6e..179d0ebb83f 100644 --- a/src/workbench/extensions/agent/services/agent/agentRestClient.ts +++ b/src/workbench/extensions/agent/services/agent/agentRestClient.ts @@ -143,15 +143,20 @@ export function createAgentRestClient() { async function listCloudWorkflows(): Promise { const entries: CloudWorkflowEntry[] = [] let hasMore = false + let cursor: string | undefined for (let page = 0; page < CLOUD_WORKFLOW_MAX_PAGES; page++) { + const after = cursor ? `&after=${encodeURIComponent(cursor)}` : '' const result = await request( - `/workflows?limit=${CLOUD_WORKFLOW_PAGE_SIZE}&offset=${page * CLOUD_WORKFLOW_PAGE_SIZE}`, + `/workflows?limit=${CLOUD_WORKFLOW_PAGE_SIZE}${after}`, { method: 'GET' }, zCloudWorkflowIndex ) entries.push(...result.data) hasMore = result.pagination.has_more if (!hasMore) break + const nextCursor = result.pagination.next_cursor + if (!nextCursor || nextCursor === cursor) break + cursor = nextCursor } if (hasMore) console.warn( diff --git a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts index 14c54baeae0..c4b126dafe0 100644 --- a/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts +++ b/src/workbench/extensions/agent/stores/agent/agentConversationStore.test.ts @@ -23,7 +23,13 @@ const delta = (id: string, text: string): AgentChatEvent => const toolCall = (id: string, name: string, status: string): AgentChatEvent => chat({ type: 'agent_tool_call', - data: { tool_name: name, status, args: [], message_id: id, thread_id: 'th' } + data: { + tool_call_id: `call-${name}`, + tool_name: name, + status, + message_id: id, + thread_id: 'th' + } }) const done = (id: string): AgentChatEvent => chat({ @@ -201,7 +207,7 @@ describe('useAgentConversationStore', () => { it('folds a tool_call into the active turn', () => { const store = useAgentConversationStore() store.startTurn(T1) - store.ingest(toolCall('t1', 'add_node', 'ok')) + store.ingest(toolCall('t1', 'add_node', 'success')) expect(store.messages[0].parts[0]).toMatchObject({ type: 'tool', name: 'add_node',