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
23 changes: 18 additions & 5 deletions apps/sim/app/api/workflows/[id]/execute/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,8 @@ async function handleExecutePost(
blockType: string,
executionOrder: number,
iterationContext?: IterationContext,
childWorkflowContext?: ChildWorkflowContext
childWorkflowContext?: ChildWorkflowContext,
blockExecutionId?: string
) => {
reqLogger.info('onBlockStart called', { blockId, blockName, blockType })
await sendEvent({
Expand All @@ -1892,6 +1893,7 @@ async function handleExecutePost(
childWorkflowBlockId: childWorkflowContext.parentBlockId,
childWorkflowName: childWorkflowContext.workflowName,
}),
...(blockExecutionId && { blockExecutionId }),
},
})
}
Expand All @@ -1902,7 +1904,8 @@ async function handleExecutePost(
blockType: string,
callbackData: BlockCompletionCallbackData,
iterationContext?: IterationContext,
childWorkflowContext?: ChildWorkflowContext
childWorkflowContext?: ChildWorkflowContext,
blockExecutionId?: string
) => {
const compactCallbackData = {
...callbackData,
Expand Down Expand Up @@ -1982,6 +1985,9 @@ async function handleExecutePost(
}),
...childWorkflowData,
...instanceData,
...((blockExecutionId || callbackData.blockExecutionId) && {
blockExecutionId: blockExecutionId ?? callbackData.blockExecutionId,
}),
},
})
} else {
Expand Down Expand Up @@ -2021,13 +2027,17 @@ async function handleExecutePost(
}),
...childWorkflowData,
...instanceData,
...((blockExecutionId || callbackData.blockExecutionId) && {
blockExecutionId: blockExecutionId ?? callbackData.blockExecutionId,
}),
},
})
}
}

const onStream = async (streamingExec: StreamingExecution) => {
const blockId = (streamingExec.execution as any).blockId
const { blockExecutionId } = streamingExec

// Live answer text rides the sink when available (pending deltas
// stream as the model generates; chunk_reset clears intermediate
Expand All @@ -2038,6 +2048,7 @@ async function handleExecutePost(
// Sync window: attach sink before first await so pump delivers thinking/tools.
const unsubscribe = forwardAgentStreamToExecutionEvents(streamingExec, {
blockId,
blockExecutionId,
executionId,
workflowId,
sendEvent,
Expand Down Expand Up @@ -2079,7 +2090,7 @@ async function handleExecutePost(
timestamp: new Date().toISOString(),
executionId,
workflowId,
data: { blockId, chunk, display },
data: { blockId, ...(blockExecutionId && { blockExecutionId }), chunk, display },
})
}

Expand All @@ -2089,7 +2100,7 @@ async function handleExecutePost(
timestamp: new Date().toISOString(),
executionId,
workflowId,
data: { blockId },
data: { blockId, ...(blockExecutionId && { blockExecutionId }) },
})
}
} catch (error) {
Expand Down Expand Up @@ -2150,7 +2161,8 @@ async function handleExecutePost(
childWorkflowInstanceId: string,
iterationContext?: IterationContext,
executionOrder?: number,
childWorkflowContext?: ChildWorkflowContext
childWorkflowContext?: ChildWorkflowContext,
blockExecutionId?: string
) => {
await sendEvent({
type: 'block:childWorkflowStarted',
Expand All @@ -2174,6 +2186,7 @@ async function handleExecutePost(
childWorkflowName: childWorkflowContext.workflowName,
}),
...(executionOrder !== undefined && { executionOrder }),
...(blockExecutionId && { blockExecutionId }),
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* @vitest-environment jsdom
*/
import { act, type ReactNode } from 'react'
import { sleep } from '@sim/utils/helpers'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

const {
chatStoreState,
executionStoreState,
mockCancel,
mockExecute,
Expand All @@ -22,6 +24,9 @@ const {
workflowBlocks,
workflowStoreState,
} = vi.hoisted(() => {
const chatStoreState = {
selectedWorkflowOutputs: [] as string[],
}
const workflowBlocks = {
start: {
id: 'start',
Expand Down Expand Up @@ -82,6 +87,7 @@ const {
}

return {
chatStoreState,
executionStoreState,
mockCancel: vi.fn(),
mockExecute: vi.fn(),
Expand Down Expand Up @@ -167,9 +173,18 @@ vi.mock('@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-current-workflow

vi.mock('@/app/workspace/[workspaceId]/w/[workflowId]/utils/workflow-execution-utils', () => ({
addHttpErrorConsoleEntry: vi.fn(),
createBlockEventHandlers: () => ({
createBlockEventHandlers: (config: {
onBlockCompleteCallback?: (
blockId: string,
output: unknown,
blockExecutionId?: string
) => Promise<void>
}) => ({
onBlockStarted: vi.fn(),
onBlockCompleted: vi.fn(),
onBlockCompleted: vi.fn(
(data: { blockId: string; output: unknown; blockExecutionId?: string }) =>
config.onBlockCompleteCallback?.(data.blockId, data.output, data.blockExecutionId)
),
onBlockError: vi.fn(),
onBlockChildWorkflowStarted: vi.fn(),
}),
Expand Down Expand Up @@ -221,7 +236,7 @@ vi.mock('@/serializer', () => ({
vi.mock('@/stores/chat/store', () => ({
useChatStore: {
getState: () => ({
getSelectedWorkflowOutput: () => [],
getSelectedWorkflowOutput: () => chatStoreState.selectedWorkflowOutputs,
}),
},
}))
Expand Down Expand Up @@ -343,6 +358,7 @@ async function drainStream(value: unknown): Promise<void> {
describe('useWorkflowExecution cancellation', () => {
beforeEach(() => {
vi.clearAllMocks()
chatStoreState.selectedWorkflowOutputs = []
executionStoreState.getCurrentExecutionId.mockReturnValue('execution-1')
mockRequestJson.mockResolvedValue({ success: true })
})
Expand Down Expand Up @@ -402,6 +418,7 @@ describe('useWorkflowExecution cancellation', () => {
describe('useWorkflowExecution attachment uploads', () => {
beforeEach(() => {
vi.clearAllMocks()
chatStoreState.selectedWorkflowOutputs = []
executionStoreState.getCurrentExecutionId.mockReturnValue(null)
mockResolveStartCandidates.mockReturnValue([])
mockSelectBestTrigger.mockReturnValue([])
Expand Down Expand Up @@ -584,6 +601,52 @@ describe('useWorkflowExecution attachment uploads', () => {
unmount()
})

it('does not append a later sibling output after the block has streamed', async () => {
chatStoreState.selectedWorkflowOutputs = ['agent-1_content']
mockExecute.mockImplementationOnce(async (options) => {
options.onExecutionId?.('execution-1')
await options.callbacks?.onStreamChunk?.({
blockId: 'agent-1',
blockExecutionId: 'invoke-streamed',
chunk: 'streamed answer',
})
await sleep(0)
await options.callbacks?.onBlockCompleted?.({
blockId: 'agent-1',
blockExecutionId: 'invoke-streamed',
output: { content: 'streamed answer' },
})
await options.callbacks?.onBlockCompleted?.({
blockId: 'agent-1',
blockExecutionId: 'invoke-later',
output: { content: 'later answer' },
})
})

const { result, unmount } = renderWorkflowExecutionHook()
const decoder = new TextDecoder()
let streamedText = ''

await act(async () => {
const runResult = await result().handleRunWorkflow({ input: 'chat input' })
if (!isChatWorkflowRunResult(runResult)) {
throw new Error('Expected a chat workflow run result')
}
const reader = runResult.stream.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) break
streamedText += decoder.decode(value, { stream: true })
}
streamedText += decoder.decode()
})

expect(streamedText).toContain('streamed answer')
expect(streamedText).not.toContain('later answer')

unmount()
})

it('preserves legacy live thinking when no display projection field is sent', async () => {
mockExecute.mockImplementationOnce(async (options) => {
options.onExecutionId?.('execution-1')
Expand Down
Loading
Loading