Skip to content

Commit

Permalink
fix(core): Fix race condition in AI tool invocation with multiple ite…
Browse files Browse the repository at this point in the history
…ms from the parent (#12169)
  • Loading branch information
netroy authored Dec 11, 2024
1 parent 57c6a61 commit dce0c58
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/core/src/CreateNodeAsTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ class AIParametersParser {
description,
schema,
func: async (toolArgs: z.infer<typeof schema>) => {
const context = this.options.contextFactory(this.runIndex, {});
const currentRunIndex = this.runIndex++;
const context = this.options.contextFactory(currentRunIndex, {});
context.addInputData(NodeConnectionType.AiTool, [[{ json: toolArgs }]]);

try {
Expand All @@ -402,18 +403,16 @@ class AIParametersParser {
const mappedResults = result?.[0]?.flatMap((item) => item.json);

// Add output data to the context
context.addOutputData(NodeConnectionType.AiTool, this.runIndex, [
context.addOutputData(NodeConnectionType.AiTool, currentRunIndex, [
[{ json: { response: mappedResults } }],
]);

// Return the stringified results
return JSON.stringify(mappedResults);
} catch (error) {
const nodeError = new NodeOperationError(this.options.node, error as Error);
context.addOutputData(NodeConnectionType.AiTool, this.runIndex, nodeError);
context.addOutputData(NodeConnectionType.AiTool, currentRunIndex, nodeError);
return 'Error during node execution: ' + nodeError.description;
} finally {
this.runIndex++;
}
},
});
Expand Down

0 comments on commit dce0c58

Please sign in to comment.