From 7169f33a84b093b6e23dd7465d0ed6f5f6af9a79 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 27 May 2025 00:38:40 -0700 Subject: [PATCH] fix(nodes): simplify response format for loops/parallels --- apps/sim/executor/index.ts | 1 - apps/sim/executor/loops.test.ts | 9 ++------- apps/sim/executor/loops.ts | 29 ++++++++++++++--------------- apps/sim/executor/parallels.test.ts | 6 ++---- apps/sim/executor/parallels.ts | 8 +------- 5 files changed, 19 insertions(+), 34 deletions(-) diff --git a/apps/sim/executor/index.ts b/apps/sim/executor/index.ts index 6dfe7568d3..112fe5422e 100644 --- a/apps/sim/executor/index.ts +++ b/apps/sim/executor/index.ts @@ -1332,7 +1332,6 @@ export class Executor { context, parallelInfo.parallelId, parallelInfo.iterationIndex, - actualBlockId, output ) } diff --git a/apps/sim/executor/loops.test.ts b/apps/sim/executor/loops.test.ts index 5e0a5ecb29..7dd43350d2 100644 --- a/apps/sim/executor/loops.test.ts +++ b/apps/sim/executor/loops.test.ts @@ -316,9 +316,7 @@ describe('LoopManager', () => { expect(loopState).toBeDefined() expect(loopState?.maxIterations).toBe(3) expect(loopState?.loopType).toBe('for') - expect(loopState?.executionResults.get('iteration_0')).toEqual({ - 'block-1': output, - }) + expect(loopState?.executionResults.get('iteration_0')).toEqual(output) }) test('should add to existing loop state', () => { @@ -341,10 +339,7 @@ describe('LoopManager', () => { const loopState = mockContext.loopExecutions.get('loop-1') const iterationResults = loopState?.executionResults.get('iteration_0') - expect(iterationResults).toEqual({ - 'block-1': output1, - 'block-2': output2, - }) + expect(iterationResults).toEqual(output2) }) test('should handle forEach loop state creation', () => { diff --git a/apps/sim/executor/loops.ts b/apps/sim/executor/loops.ts index ffb02df619..7f61aa8ed1 100644 --- a/apps/sim/executor/loops.ts +++ b/apps/sim/executor/loops.ts @@ -55,16 +55,17 @@ export class LoopManager { const currentIteration = context.loopIterations.get(loopId) || 0 // Store the results from this iteration before potentially resetting blocks - const iterationResults: Record = {} + const iterationResults: any[] = [] for (const nodeId of loop.nodes) { const blockState = context.blockStates.get(nodeId) if (blockState?.output) { - iterationResults[nodeId] = blockState.output + // Just push the output directly, not nested under block ID + iterationResults.push(blockState.output) } } // Store the iteration results - if (Object.keys(iterationResults).length > 0) { + if (iterationResults.length > 0) { this.storeIterationResult( context, loopId, @@ -117,8 +118,13 @@ export class LoopManager { if (loopState) { for (let i = 0; i < maxIterations; i++) { const result = loopState.executionResults.get(`iteration_${i}`) - if (result?.iteration) { - results.push(result.iteration) + if (result) { + // If result is an array (from multiple blocks in the loop), flatten it + if (Array.isArray(result)) { + results.push(...result) + } else { + results.push(result) + } } } } @@ -240,7 +246,7 @@ export class LoopManager { context: ExecutionContext, loopId: string, iterationIndex: number, - blockId: string, + _blockId: string, // Not used anymore since we're storing results directly output: any ): void { if (!context.loopExecutions) { @@ -266,16 +272,9 @@ export class LoopManager { context.loopExecutions.set(loopId, loopState) } - // Get or create the iteration results object + // Store the output directly for this iteration const iterationKey = `iteration_${iterationIndex}` - let iterationResults = loopState.executionResults.get(iterationKey) - if (!iterationResults) { - iterationResults = {} - loopState.executionResults.set(iterationKey, iterationResults) - } - - // Store the block's output for this iteration - iterationResults[blockId] = output + loopState.executionResults.set(iterationKey, output) } /** diff --git a/apps/sim/executor/parallels.test.ts b/apps/sim/executor/parallels.test.ts index 3ba2ea3c56..fdfdf2867e 100644 --- a/apps/sim/executor/parallels.test.ts +++ b/apps/sim/executor/parallels.test.ts @@ -275,11 +275,9 @@ describe('ParallelManager', () => { const output = { response: { result: 'test result' } } - manager.storeIterationResult(context, 'parallel-1', 1, 'func-1', output) + manager.storeIterationResult(context, 'parallel-1', 1, output) - expect(state.executionResults.get('iteration_1')).toEqual({ - 'func-1': output, - }) + expect(state.executionResults.get('iteration_1')).toEqual(output) }) }) diff --git a/apps/sim/executor/parallels.ts b/apps/sim/executor/parallels.ts index 64c93d4042..0530c694cc 100644 --- a/apps/sim/executor/parallels.ts +++ b/apps/sim/executor/parallels.ts @@ -210,17 +210,11 @@ export class ParallelManager { context: ExecutionContext, parallelId: string, iterationIndex: number, - blockId: string, output: NormalizedBlockOutput ): void { const parallelState = context.parallelExecutions?.get(parallelId) if (parallelState) { - const existingResults = - parallelState.executionResults.get(`iteration_${iterationIndex}`) || {} - parallelState.executionResults.set(`iteration_${iterationIndex}`, { - ...existingResults, - [blockId]: output, - }) + parallelState.executionResults.set(`iteration_${iterationIndex}`, output) } } }