|
1 | 1 | import { mock } from 'jest-mock-extended'; |
2 | | -import type { IExecuteData, IRunData, EngineRequest } from 'n8n-workflow'; |
| 2 | +import type { IExecuteData, IRunData, EngineRequest, INodeExecutionData } from 'n8n-workflow'; |
3 | 3 |
|
4 | 4 | import { DirectedGraph } from '../partial-execution-utils'; |
5 | 5 | import { createNodeData } from '../partial-execution-utils/__tests__/helpers'; |
@@ -41,4 +41,250 @@ describe('handleRequests', () => { |
41 | 41 | }), |
42 | 42 | ).toThrowError('Workflow does not contain a node with the name of "does not exist".'); |
43 | 43 | }); |
| 44 | + |
| 45 | + test('merges agent input data with tool parameters for expression resolution', () => { |
| 46 | + // ARRANGE |
| 47 | + const toolNode = createNodeData({ name: 'Gmail Tool', type: types.passThrough }); |
| 48 | + const agentNode = createNodeData({ name: 'AI Agent', type: types.passThrough }); |
| 49 | + |
| 50 | + const workflow = new DirectedGraph() |
| 51 | + .addNodes(toolNode, agentNode) |
| 52 | + .toWorkflow({ name: '', active: false, nodeTypes }); |
| 53 | + |
| 54 | + // Agent received data from previous nodes with workflow context |
| 55 | + const agentInputData: INodeExecutionData[] = [ |
| 56 | + { |
| 57 | + json: { |
| 58 | + myNewField: 1, |
| 59 | + price_total: '344.00', |
| 60 | + existingData: 'from workflow', |
| 61 | + }, |
| 62 | + }, |
| 63 | + ]; |
| 64 | + |
| 65 | + const executionData: IExecuteData = { |
| 66 | + data: { |
| 67 | + main: [agentInputData], // Agent's main input at index 0 |
| 68 | + }, |
| 69 | + source: { |
| 70 | + main: [ |
| 71 | + { |
| 72 | + previousNode: 'Process Quotes', |
| 73 | + previousNodeOutput: 0, |
| 74 | + previousNodeRun: 0, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + node: agentNode, |
| 79 | + }; |
| 80 | + |
| 81 | + const request: EngineRequest = { |
| 82 | + actions: [ |
| 83 | + { |
| 84 | + actionType: 'ExecutionNodeAction', |
| 85 | + nodeName: 'Gmail Tool', |
| 86 | + input: { subject: 'Test Email', toolParam: 'from LLM' }, // LLM-provided parameters |
| 87 | + type: 'ai_tool', |
| 88 | + id: 'tool_call_123', |
| 89 | + metadata: { itemIndex: 0 }, |
| 90 | + }, |
| 91 | + ], |
| 92 | + metadata: {}, |
| 93 | + }; |
| 94 | + |
| 95 | + const runData: IRunData = {}; |
| 96 | + |
| 97 | + // ACT |
| 98 | + const result = handleRequest({ |
| 99 | + workflow, |
| 100 | + currentNode: agentNode, |
| 101 | + request, |
| 102 | + runIndex: 0, |
| 103 | + executionData, |
| 104 | + runData, |
| 105 | + }); |
| 106 | + |
| 107 | + // ASSERT |
| 108 | + const toolNodeToExecute = result.nodesToBeExecuted.find((n) => n.parentNode === 'AI Agent'); |
| 109 | + expect(toolNodeToExecute).toBeDefined(); |
| 110 | + |
| 111 | + // Verify merged data contains both workflow data and tool parameters |
| 112 | + const mergedJson = toolNodeToExecute!.parentOutputData[0][0].json; |
| 113 | + expect(mergedJson).toEqual({ |
| 114 | + myNewField: 1, |
| 115 | + price_total: '344.00', |
| 116 | + existingData: 'from workflow', |
| 117 | + subject: 'Test Email', |
| 118 | + toolParam: 'from LLM', |
| 119 | + toolCallId: 'tool_call_123', |
| 120 | + }); |
| 121 | + |
| 122 | + // Verify tool parameters take precedence (override workflow data) |
| 123 | + const requestWithOverride: EngineRequest = { |
| 124 | + actions: [ |
| 125 | + { |
| 126 | + actionType: 'ExecutionNodeAction', |
| 127 | + nodeName: 'Gmail Tool', |
| 128 | + input: { existingData: 'overridden by tool' }, // This should override workflow data |
| 129 | + type: 'ai_tool', |
| 130 | + id: 'tool_call_456', |
| 131 | + metadata: { itemIndex: 0 }, |
| 132 | + }, |
| 133 | + ], |
| 134 | + metadata: {}, |
| 135 | + }; |
| 136 | + |
| 137 | + const runData2: IRunData = {}; |
| 138 | + const result2 = handleRequest({ |
| 139 | + workflow, |
| 140 | + currentNode: agentNode, |
| 141 | + request: requestWithOverride, |
| 142 | + runIndex: 0, |
| 143 | + executionData, |
| 144 | + runData: runData2, |
| 145 | + }); |
| 146 | + |
| 147 | + const toolNodeToExecute2 = result2.nodesToBeExecuted.find((n) => n.parentNode === 'AI Agent'); |
| 148 | + const mergedJson2 = toolNodeToExecute2!.parentOutputData[0][0].json; |
| 149 | + expect(mergedJson2.existingData).toBe('overridden by tool'); |
| 150 | + }); |
| 151 | + |
| 152 | + test('uses output index 0 for tools regardless of agent input connection', () => { |
| 153 | + // ARRANGE |
| 154 | + const toolNode = createNodeData({ name: 'Gmail Tool', type: types.passThrough }); |
| 155 | + const agentNode = createNodeData({ name: 'AI Agent', type: types.passThrough }); |
| 156 | + const switchNode = createNodeData({ name: 'Switch', type: types.passThrough }); |
| 157 | + |
| 158 | + const workflow = new DirectedGraph() |
| 159 | + .addNodes(toolNode, agentNode, switchNode) |
| 160 | + .toWorkflow({ name: '', active: false, nodeTypes }); |
| 161 | + |
| 162 | + // Agent is connected to output 2 of the Switch node |
| 163 | + const agentInputData: INodeExecutionData[] = [ |
| 164 | + { |
| 165 | + json: { |
| 166 | + data: 'from switch output 2', |
| 167 | + }, |
| 168 | + }, |
| 169 | + ]; |
| 170 | + |
| 171 | + const executionData: IExecuteData = { |
| 172 | + data: { |
| 173 | + main: [agentInputData], |
| 174 | + }, |
| 175 | + source: { |
| 176 | + main: [ |
| 177 | + { |
| 178 | + previousNode: 'Switch', |
| 179 | + previousNodeOutput: 2, // Connected to Switch output 2 |
| 180 | + previousNodeRun: 0, |
| 181 | + }, |
| 182 | + ], |
| 183 | + }, |
| 184 | + node: agentNode, |
| 185 | + }; |
| 186 | + |
| 187 | + const request: EngineRequest = { |
| 188 | + actions: [ |
| 189 | + { |
| 190 | + actionType: 'ExecutionNodeAction', |
| 191 | + nodeName: 'Gmail Tool', |
| 192 | + input: { message: 'test' }, |
| 193 | + type: 'ai_tool', |
| 194 | + id: 'tool_call_789', |
| 195 | + metadata: { itemIndex: 0 }, |
| 196 | + }, |
| 197 | + ], |
| 198 | + metadata: {}, |
| 199 | + }; |
| 200 | + |
| 201 | + const runData: IRunData = {}; |
| 202 | + |
| 203 | + // ACT |
| 204 | + const result = handleRequest({ |
| 205 | + workflow, |
| 206 | + currentNode: agentNode, |
| 207 | + request, |
| 208 | + runIndex: 0, |
| 209 | + executionData, |
| 210 | + runData, |
| 211 | + }); |
| 212 | + |
| 213 | + // ASSERT |
| 214 | + const toolNodeToExecute = result.nodesToBeExecuted.find((n) => n.parentNode === 'AI Agent'); |
| 215 | + expect(toolNodeToExecute).toBeDefined(); |
| 216 | + |
| 217 | + // Verify parentOutputIndex is always 0 for tools (agents have only one main output) |
| 218 | + // This prevents "Cannot read properties of undefined (reading 'map')" error |
| 219 | + expect(toolNodeToExecute!.parentOutputIndex).toBe(0); |
| 220 | + }); |
| 221 | + |
| 222 | + test('handles multiple items correctly using itemIndex from metadata', () => { |
| 223 | + // ARRANGE |
| 224 | + const toolNode = createNodeData({ name: 'Gmail Tool', type: types.passThrough }); |
| 225 | + const agentNode = createNodeData({ name: 'AI Agent', type: types.passThrough }); |
| 226 | + |
| 227 | + const workflow = new DirectedGraph() |
| 228 | + .addNodes(toolNode, agentNode) |
| 229 | + .toWorkflow({ name: '', active: false, nodeTypes }); |
| 230 | + |
| 231 | + // Agent received multiple items from previous nodes |
| 232 | + const agentInputData: INodeExecutionData[] = [ |
| 233 | + { json: { id: 1, value: 'first item' } }, |
| 234 | + { json: { id: 2, value: 'second item' } }, |
| 235 | + { json: { id: 3, value: 'third item' } }, |
| 236 | + ]; |
| 237 | + |
| 238 | + const executionData: IExecuteData = { |
| 239 | + data: { |
| 240 | + main: [agentInputData], |
| 241 | + }, |
| 242 | + source: { |
| 243 | + main: [ |
| 244 | + { |
| 245 | + previousNode: 'Process Data', |
| 246 | + previousNodeOutput: 0, |
| 247 | + previousNodeRun: 0, |
| 248 | + }, |
| 249 | + ], |
| 250 | + }, |
| 251 | + node: agentNode, |
| 252 | + }; |
| 253 | + |
| 254 | + const request: EngineRequest = { |
| 255 | + actions: [ |
| 256 | + { |
| 257 | + actionType: 'ExecutionNodeAction', |
| 258 | + nodeName: 'Gmail Tool', |
| 259 | + input: { toolParam: 'for second item' }, |
| 260 | + type: 'ai_tool', |
| 261 | + id: 'tool_call_abc', |
| 262 | + metadata: { itemIndex: 1 }, // Processing second item |
| 263 | + }, |
| 264 | + ], |
| 265 | + metadata: {}, |
| 266 | + }; |
| 267 | + |
| 268 | + const runData: IRunData = {}; |
| 269 | + |
| 270 | + // ACT |
| 271 | + const result = handleRequest({ |
| 272 | + workflow, |
| 273 | + currentNode: agentNode, |
| 274 | + request, |
| 275 | + runIndex: 0, |
| 276 | + executionData, |
| 277 | + runData, |
| 278 | + }); |
| 279 | + |
| 280 | + // ASSERT |
| 281 | + const toolNodeToExecute = result.nodesToBeExecuted.find((n) => n.parentNode === 'AI Agent'); |
| 282 | + expect(toolNodeToExecute).toBeDefined(); |
| 283 | + |
| 284 | + // Verify correct item data is merged (second item with id: 2) |
| 285 | + const mergedJson = toolNodeToExecute!.parentOutputData[0][0].json; |
| 286 | + expect(mergedJson.id).toBe(2); |
| 287 | + expect(mergedJson.value).toBe('second item'); |
| 288 | + expect(mergedJson.toolParam).toBe('for second item'); |
| 289 | + }); |
44 | 290 | }); |
0 commit comments