fix(nodes): simplify response format for loops/parallels

This commit is contained in:
Waleed Latif
2025-05-27 00:38:40 -07:00
parent 79bea155b3
commit 7169f33a84
5 changed files with 19 additions and 34 deletions

View File

@@ -1332,7 +1332,6 @@ export class Executor {
context,
parallelInfo.parallelId,
parallelInfo.iterationIndex,
actualBlockId,
output
)
}

View File

@@ -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', () => {

View File

@@ -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<string, any> = {}
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)
}
/**

View File

@@ -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)
})
})

View File

@@ -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)
}
}
}