Skip to content

Commit

Permalink
refactor(Call n8n Sub-Workflow Tool Node): Remove duplicate FromAIPar…
Browse files Browse the repository at this point in the history
…ser service
  • Loading branch information
OlegIvaniv committed Feb 6, 2025
1 parent 00e3ebc commit 39a8d6f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 297 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getCurrentWorkflowInputData } from 'n8n-nodes-base/dist/utils/workflowI
import type {
ExecuteWorkflowData,
ExecutionError,
FromAIArgument,
IDataObject,
IExecuteWorkflowInfo,
INodeExecutionData,
Expand All @@ -18,12 +19,15 @@ import type {
IWorkflowDataProxyData,
ResourceMapperValue,
} from 'n8n-workflow';
import { jsonParse, NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import {
generateZodSchema,
jsonParse,
NodeConnectionType,
NodeOperationError,
traverseNodeParameters,
} from 'n8n-workflow';
import { z } from 'zod';

import type { FromAIArgument } from './FromAIParser';
import { AIParametersParser } from './FromAIParser';

/**
Main class for creating the Workflow tool
Processes the node parameters and creates AI Agent tool capable of executing n8n workflows
Expand Down Expand Up @@ -278,24 +282,21 @@ export class WorkflowToolService {
description: string,
func: (query: string | IDataObject, runManager?: CallbackManagerForToolRun) => Promise<string>,
): Promise<DynamicStructuredTool | DynamicTool> {
const fromAIParser = new AIParametersParser(this.context);
const collectedArguments = await this.extractFromAIParameters(fromAIParser);
const collectedArguments = await this.extractFromAIParameters();

// If there are no `fromAI` arguments, fallback to creating a simple tool
if (collectedArguments.length === 0) {
return new DynamicTool({ name, description, func });
}

// Otherwise, prepare Zod schema and create a structured tool
const schema = this.createZodSchema(collectedArguments, fromAIParser);
const schema = this.createZodSchema(collectedArguments);
return new DynamicStructuredTool({ schema, name, description, func });
}

private async extractFromAIParameters(
fromAIParser: AIParametersParser,
): Promise<FromAIArgument[]> {
private async extractFromAIParameters(): Promise<FromAIArgument[]> {
const collectedArguments: FromAIArgument[] = [];
fromAIParser.traverseNodeParameters(this.context.getNode().parameters, collectedArguments);
traverseNodeParameters(this.context.getNode().parameters, collectedArguments);

const uniqueArgsMap = new Map<string, FromAIArgument>();
for (const arg of collectedArguments) {
Expand All @@ -305,9 +306,9 @@ export class WorkflowToolService {
return Array.from(uniqueArgsMap.values());
}

private createZodSchema(args: FromAIArgument[], parser: AIParametersParser): z.ZodObject<any> {
private createZodSchema(args: FromAIArgument[]): z.ZodObject<any> {
const schemaObj = args.reduce((acc: Record<string, z.ZodTypeAny>, placeholder) => {
acc[placeholder.key] = parser.generateZodSchema(placeholder);
acc[placeholder.key] = generateZodSchema(placeholder);
return acc;
}, {});

Expand Down

0 comments on commit 39a8d6f

Please sign in to comment.