feat(Call n8n Workflow Tool Node): Allow for the subworkflow tool to be used with any response shape

This commit is contained in:
Oleg Ivaniv 2024-11-05 16:46:05 +01:00
parent dfd785bc08
commit 9f6632e11a
No known key found for this signature in database

View file

@ -369,18 +369,18 @@ export class ToolWorkflow implements INodeType {
runManager?: CallbackManagerForToolRun, runManager?: CallbackManagerForToolRun,
): Promise<string> => { ): Promise<string> => {
const source = this.getNodeParameter('source', itemIndex) as string; const source = this.getNodeParameter('source', itemIndex) as string;
const responsePropertyName = this.getNodeParameter( // const responsePropertyName = this.getNodeParameter(
'responsePropertyName', // 'responsePropertyName',
itemIndex, // itemIndex,
) as string; // ) as string;
if (!responsePropertyName) { // if (!responsePropertyName) {
throw new NodeOperationError(this.getNode(), "Field to return can't be empty", { // throw new NodeOperationError(this.getNode(), "Field to return can't be empty", {
itemIndex, // itemIndex,
description: // description:
'Enter the name of a field in the last node of the workflow that contains the response to return', // 'Enter the name of a field in the last node of the workflow that contains the response to return',
}); // });
} // }
const workflowInfo: IExecuteWorkflowInfo = {}; const workflowInfo: IExecuteWorkflowInfo = {};
if (source === 'database') { if (source === 'database') {
@ -453,16 +453,11 @@ export class ToolWorkflow implements INodeType {
throw new NodeOperationError(this.getNode(), error as Error); throw new NodeOperationError(this.getNode(), error as Error);
} }
const response: string | undefined = get(receivedData, [ const response: string | undefined = get(receivedData, '[0][0].json') as string | undefined;
0,
0,
'json',
responsePropertyName,
]) as string | undefined;
if (response === undefined) { if (response === undefined) {
throw new NodeOperationError( throw new NodeOperationError(
this.getNode(), this.getNode(),
`There was an error: "The workflow did not return an item with the property '${responsePropertyName}'"`, 'There was an error: "The workflow did not return a response"',
); );
} }
@ -506,7 +501,12 @@ export class ToolWorkflow implements INodeType {
if (executionError) { if (executionError) {
void this.addOutputData(NodeConnectionType.AiTool, index, executionError); void this.addOutputData(NodeConnectionType.AiTool, index, executionError);
} else { } else {
void this.addOutputData(NodeConnectionType.AiTool, index, [[{ json: { response } }]]); const parsedResponse = jsonParse<IDataObject>(response);
// Output always needs to be an object
// so we try to parse the response as JSON and if it fails we just return the string wrapped in an object
void this.addOutputData(NodeConnectionType.AiTool, index, [
[{ json: typeof parsedResponse === 'object' ? parsedResponse : { response } }],
]);
} }
return response; return response;
}; };