Skip to content

Commit 430944a

Browse files
committed
feat(agentflow): add output structure options for iteration results
1 parent 2bbbec8 commit 430944a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

packages/components/nodes/agentflow/Iteration/Iteration.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ class Iteration_Agentflow implements INode {
3232
description: 'The input array to iterate over',
3333
acceptVariable: true,
3434
rows: 4
35+
},
36+
{
37+
label: 'Output Structure',
38+
name: 'iterationOutputStructure',
39+
type: 'options',
40+
description: 'How to structure the output from all iterations',
41+
options: [
42+
{
43+
label: 'Aggregated Text',
44+
name: 'aggregatedText',
45+
description: 'Join all iteration outputs into a single text separated by newlines'
46+
},
47+
{
48+
label: 'JSON Array',
49+
name: 'jsonArray',
50+
description: 'Preserve the input array structure — output[i] corresponds to the result of processing input[i]'
51+
}
52+
],
53+
default: 'aggregatedText'
3554
}
3655
]
3756
}
@@ -56,13 +75,15 @@ class Iteration_Agentflow implements INode {
5675
throw new Error('Invalid input array')
5776
}
5877

78+
const iterationOutputStructure = nodeData.inputs?.iterationOutputStructure ?? 'aggregatedText'
5979
const state = options.agentflowRuntime?.state as ICommonObject
6080

6181
const returnOutput = {
6282
id: nodeData.id,
6383
name: this.name,
6484
input: {
65-
iterationInput: iterationInputArray
85+
iterationInput: iterationInputArray,
86+
iterationOutputStructure
6687
},
6788
output: {},
6889
state

packages/server/src/utils/buildAgentflow.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ const executeNode = async ({
12431243
}
12441244

12451245
// Initialize array to collect results from iterations
1246+
const outputStructure = results.input?.iterationOutputStructure ?? 'aggregatedText'
12461247
const iterationResults: string[] = []
12471248

12481249
// Execute sub-flow for each item in the iteration array
@@ -1289,9 +1290,12 @@ const executeNode = async ({
12891290
productId
12901291
})
12911292

1292-
// Store the result
1293+
// Store the result.
1294+
// In jsonArray mode, always push to maintain 1:1 mapping with the input array.
12931295
if (subFlowResult?.text) {
12941296
iterationResults.push(subFlowResult.text)
1297+
} else if (outputStructure === 'jsonArray') {
1298+
iterationResults.push('')
12951299
}
12961300

12971301
// Add executed data from sub-flow to main execution data with appropriate iteration context
@@ -1351,7 +1355,7 @@ const executeNode = async ({
13511355
results.output = {
13521356
...(results.output || {}),
13531357
iterationResults,
1354-
content: iterationResults.join('\n')
1358+
content: outputStructure === 'jsonArray' ? JSON.stringify(iterationResults) : iterationResults.join('\n')
13551359
}
13561360

13571361
logger.debug(` 📊 Completed all iterations. Total results: ${iterationResults.length}`)

0 commit comments

Comments
 (0)