mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-16 17:46:45 +00:00
feat(core): Implement partial execution for all tool nodes (#15168)
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"node": "n8n-nodes-base.toolExecutor",
|
||||
"nodeVersion": "1.0",
|
||||
"codexVersion": "1.0",
|
||||
"details": "Can execute tools by simulating an agent function call with a given query.",
|
||||
"categories": ["Core Nodes"],
|
||||
"resources": {
|
||||
"primaryDocumentation": [
|
||||
{
|
||||
"url": "https://docs.n8n.io/integrations/builtin/core-nodes/n8n-nodes-base.editimage/"
|
||||
}
|
||||
]
|
||||
},
|
||||
"subcategories": {
|
||||
"Core Nodes": ["Helpers"]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { Tool, StructuredTool } from '@langchain/core/tools';
|
||||
import type { Toolkit } from 'langchain/agents';
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import { executeTool } from './utils/executeTool';
|
||||
|
||||
export class ToolExecutor implements INodeType {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Tool Executor',
|
||||
name: 'toolExecutor',
|
||||
version: 1,
|
||||
defaults: {
|
||||
name: 'Tool Executor',
|
||||
},
|
||||
hidden: true,
|
||||
inputs: [NodeConnectionTypes.Main, NodeConnectionTypes.AiTool],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Query',
|
||||
name: 'query',
|
||||
type: 'json',
|
||||
default: '{}',
|
||||
description: 'Parameters to pass to the tool as JSON or string',
|
||||
},
|
||||
{
|
||||
displayName: 'Tool Name',
|
||||
name: 'toolName',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'Name of the tool to execute if the connected tool is a toolkit',
|
||||
},
|
||||
],
|
||||
group: ['transform'],
|
||||
description: 'Node to execute tools without an AI Agent',
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const query = this.getNodeParameter('query', 0, {}) as string | object;
|
||||
const toolName = this.getNodeParameter('toolName', 0, '') as string;
|
||||
|
||||
let parsedQuery: string | object;
|
||||
|
||||
try {
|
||||
parsedQuery = typeof query === 'string' ? JSON.parse(query) : query;
|
||||
} catch (error) {
|
||||
parsedQuery = query;
|
||||
}
|
||||
|
||||
const resultData: INodeExecutionData[] = [];
|
||||
const toolInputs = await this.getInputConnectionData(NodeConnectionTypes.AiTool, 0);
|
||||
|
||||
if (!toolInputs || !Array.isArray(toolInputs)) {
|
||||
throw new NodeOperationError(this.getNode(), 'No tool inputs found');
|
||||
}
|
||||
|
||||
try {
|
||||
for (const tool of toolInputs) {
|
||||
// Handle toolkits
|
||||
if (tool && typeof (tool as Toolkit).getTools === 'function') {
|
||||
const toolsInToolkit = (tool as Toolkit).getTools();
|
||||
for (const toolkitTool of toolsInToolkit) {
|
||||
if (toolkitTool instanceof Tool || toolkitTool instanceof StructuredTool) {
|
||||
if (toolName === toolkitTool.name) {
|
||||
const result = await executeTool(toolkitTool, parsedQuery);
|
||||
resultData.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Handle single tool
|
||||
if (!toolName || toolName === (tool as Tool).name) {
|
||||
const result = await executeTool(tool as Tool, parsedQuery);
|
||||
resultData.push(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
`Error executing tool: ${(error as Error).message}`,
|
||||
);
|
||||
}
|
||||
return [resultData];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
import { DynamicTool, DynamicStructuredTool } from '@langchain/core/tools';
|
||||
import { mock } from 'jest-mock-extended';
|
||||
import type { IExecuteFunctions, INode } from 'n8n-workflow';
|
||||
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { ToolExecutor } from '../ToolExecutor.node';
|
||||
|
||||
describe('ToolExecutor Node', () => {
|
||||
let node: ToolExecutor;
|
||||
let mockExecuteFunction: jest.Mocked<IExecuteFunctions>;
|
||||
|
||||
beforeEach(() => {
|
||||
node = new ToolExecutor();
|
||||
mockExecuteFunction = mock<IExecuteFunctions>();
|
||||
|
||||
mockExecuteFunction.logger = {
|
||||
debug: jest.fn(),
|
||||
info: jest.fn(),
|
||||
warn: jest.fn(),
|
||||
error: jest.fn(),
|
||||
};
|
||||
|
||||
mockExecuteFunction.getNode.mockReturnValue({
|
||||
name: 'Tool Executor',
|
||||
typeVersion: 1,
|
||||
parameters: {},
|
||||
} as INode);
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('description', () => {
|
||||
it('should have the expected properties', () => {
|
||||
expect(node.description).toBeDefined();
|
||||
expect(node.description.name).toBe('toolExecutor');
|
||||
expect(node.description.displayName).toBe('Tool Executor');
|
||||
expect(node.description.version).toBe(1);
|
||||
expect(node.description.properties).toBeDefined();
|
||||
expect(node.description.inputs).toEqual([
|
||||
NodeConnectionTypes.Main,
|
||||
NodeConnectionTypes.AiTool,
|
||||
]);
|
||||
expect(node.description.outputs).toEqual([NodeConnectionTypes.Main]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('ToolExecutor', () => {
|
||||
it('should throw error if no tool inputs found', async () => {
|
||||
mockExecuteFunction.getInputConnectionData.mockResolvedValue(null);
|
||||
|
||||
await expect(node.execute.call(mockExecuteFunction)).rejects.toThrow(
|
||||
new NodeOperationError(mockExecuteFunction.getNode(), 'No tool inputs found'),
|
||||
);
|
||||
});
|
||||
|
||||
it('executes a basic tool with string input', async () => {
|
||||
const mockInvoke = jest.fn().mockResolvedValue('test result');
|
||||
|
||||
const mockTool = new DynamicTool({
|
||||
name: 'test_tool',
|
||||
description: 'A test tool',
|
||||
func: jest.fn(),
|
||||
});
|
||||
|
||||
mockTool.invoke = mockInvoke;
|
||||
|
||||
mockExecuteFunction.getInputConnectionData.mockResolvedValue([mockTool]);
|
||||
mockExecuteFunction.getNodeParameter.mockImplementation((param) => {
|
||||
if (param === 'query') return 'test input';
|
||||
return '';
|
||||
});
|
||||
|
||||
const result = await node.execute.call(mockExecuteFunction);
|
||||
|
||||
expect(mockInvoke).toHaveBeenCalledWith('test input');
|
||||
expect(result).toEqual([[{ json: 'test result' }]]);
|
||||
});
|
||||
|
||||
it('executes a structured tool with schema validation', async () => {
|
||||
const mockTool = new DynamicStructuredTool({
|
||||
name: 'test_structured_tool',
|
||||
description: 'A test structured tool',
|
||||
schema: z.object({
|
||||
number: z.number(),
|
||||
boolean: z.boolean(),
|
||||
}),
|
||||
func: jest.fn(),
|
||||
});
|
||||
|
||||
const mockInvoke = jest.fn().mockResolvedValue('test result');
|
||||
mockTool.invoke = mockInvoke;
|
||||
|
||||
mockExecuteFunction.getInputConnectionData.mockResolvedValue([mockTool]);
|
||||
mockExecuteFunction.getNodeParameter.mockImplementation((param) => {
|
||||
if (param === 'query') return { number: '42', boolean: 'true' };
|
||||
return '';
|
||||
});
|
||||
|
||||
const result = await node.execute.call(mockExecuteFunction);
|
||||
|
||||
expect(mockTool.invoke).toHaveBeenCalledWith({ number: 42, boolean: true });
|
||||
expect(result).toEqual([[{ json: 'test result' }]]);
|
||||
});
|
||||
|
||||
it('executes a specific tool from a toolkit with several tools', async () => {
|
||||
const mockTool = new DynamicTool({
|
||||
name: 'specific_tool',
|
||||
description: 'A specific tool',
|
||||
func: jest.fn().mockResolvedValue('specific result'),
|
||||
});
|
||||
|
||||
const irrelevantTool = new DynamicTool({
|
||||
name: 'other_tool',
|
||||
description: 'A specific irrelevant tool',
|
||||
func: jest.fn().mockResolvedValue('specific result'),
|
||||
});
|
||||
|
||||
mockTool.invoke = jest.fn().mockResolvedValue('specific result');
|
||||
|
||||
const toolkit = {
|
||||
getTools: () => [mockTool, irrelevantTool],
|
||||
};
|
||||
|
||||
mockExecuteFunction.getInputConnectionData.mockResolvedValue([toolkit]);
|
||||
mockExecuteFunction.getNodeParameter.mockImplementation((param) => {
|
||||
if (param === 'query') return 'test input';
|
||||
if (param === 'toolName') return 'specific_tool';
|
||||
return '';
|
||||
});
|
||||
|
||||
const result = await node.execute.call(mockExecuteFunction);
|
||||
|
||||
expect(mockTool.invoke).toHaveBeenCalledWith('test input');
|
||||
expect(result).toEqual([[{ json: 'specific result' }]]);
|
||||
});
|
||||
|
||||
it('handles JSON string query inputs', async () => {
|
||||
const mockTool = new DynamicTool({
|
||||
name: 'json_tool',
|
||||
description: 'A tool that handles JSON',
|
||||
func: jest.fn(),
|
||||
});
|
||||
mockTool.invoke = jest.fn().mockResolvedValue('json result');
|
||||
|
||||
mockExecuteFunction.getInputConnectionData.mockResolvedValue([mockTool]);
|
||||
mockExecuteFunction.getNodeParameter.mockImplementation((param) => {
|
||||
if (param === 'query') return '{"key": "value"}';
|
||||
return '';
|
||||
});
|
||||
|
||||
const result = await node.execute.call(mockExecuteFunction);
|
||||
|
||||
expect(mockTool.invoke).toHaveBeenCalledWith({ key: 'value' });
|
||||
expect(result).toEqual([[{ json: 'json result' }]]);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,119 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
import { convertValueBySchema, convertObjectBySchema } from '../utils/convertToSchema';
|
||||
|
||||
describe('convertToSchema', () => {
|
||||
describe('convertValueBySchema', () => {
|
||||
it('should convert string to number when schema is ZodNumber', () => {
|
||||
const result = convertValueBySchema('42', z.number());
|
||||
expect(result).toBe(42);
|
||||
});
|
||||
|
||||
it('should convert string to boolean when schema is ZodBoolean', () => {
|
||||
expect(convertValueBySchema('true', z.boolean())).toBe(true);
|
||||
expect(convertValueBySchema('false', z.boolean())).toBe(false);
|
||||
expect(convertValueBySchema('TRUE', z.boolean())).toBe(true);
|
||||
expect(convertValueBySchema('FALSE', z.boolean())).toBe(false);
|
||||
});
|
||||
|
||||
it('should parse JSON string when schema is ZodObject', () => {
|
||||
const result = convertValueBySchema(
|
||||
'{"key": "value", "other_key": 1, "booleanValue": false }',
|
||||
z.object({}),
|
||||
);
|
||||
expect(result).toEqual({ key: 'value', other_key: 1, booleanValue: false });
|
||||
});
|
||||
|
||||
it('should return original value if JSON parsing fails', () => {
|
||||
const result = convertValueBySchema('invalid json', z.object({}));
|
||||
expect(result).toEqual('invalid json');
|
||||
});
|
||||
|
||||
it('should return original value for non-string inputs', () => {
|
||||
const input = { key: 'value' };
|
||||
const result = convertValueBySchema(input, z.object({}));
|
||||
expect(result).toEqual(input);
|
||||
});
|
||||
});
|
||||
|
||||
describe('convertObjectBySchema', () => {
|
||||
it('should convert object values according to schema', () => {
|
||||
const schema = z.object({
|
||||
numberValue: z.number(),
|
||||
booleanValue: z.boolean(),
|
||||
object: z.object({}),
|
||||
unchanged: z.string(),
|
||||
});
|
||||
|
||||
const input = {
|
||||
numberValue: '42',
|
||||
booleanValue: 'true',
|
||||
object: '{"nested": "value"}',
|
||||
unchanged: 'string value',
|
||||
};
|
||||
|
||||
const result = convertObjectBySchema(input, schema);
|
||||
|
||||
expect(result).toEqual({
|
||||
numberValue: 42,
|
||||
booleanValue: true,
|
||||
object: { nested: 'value' },
|
||||
unchanged: 'string value',
|
||||
});
|
||||
});
|
||||
|
||||
it('should return original object if schema has no shape', () => {
|
||||
const input = { key: 'value' };
|
||||
const result = convertObjectBySchema(input, {});
|
||||
expect(result).toBe(input);
|
||||
});
|
||||
|
||||
it('should return original object if input is null', () => {
|
||||
const result = convertObjectBySchema(null, z.object({}));
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it('should handle nested objects', () => {
|
||||
const schema = z.object({
|
||||
nested: z.object({
|
||||
numberValue: z.number(),
|
||||
booleanValue: z.boolean(),
|
||||
}),
|
||||
});
|
||||
|
||||
const input = {
|
||||
nested: {
|
||||
numberValue: '42',
|
||||
booleanValue: 'true',
|
||||
},
|
||||
};
|
||||
|
||||
const result = convertObjectBySchema(input, schema);
|
||||
|
||||
expect(result).toEqual({
|
||||
nested: {
|
||||
numberValue: 42,
|
||||
booleanValue: true,
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should preserve fields not in schema', () => {
|
||||
const schema = z.object({
|
||||
number: z.number(),
|
||||
});
|
||||
|
||||
const input = {
|
||||
number: '42',
|
||||
extra: 'value',
|
||||
};
|
||||
|
||||
const result = convertObjectBySchema(input, schema);
|
||||
|
||||
expect(result).toEqual({
|
||||
number: 42,
|
||||
extra: 'value',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const convertValueBySchema = (value: unknown, schema: any): unknown => {
|
||||
if (!schema || !value) return value;
|
||||
|
||||
if (typeof value === 'string') {
|
||||
if (schema instanceof z.ZodNumber) {
|
||||
return Number(value);
|
||||
} else if (schema instanceof z.ZodBoolean) {
|
||||
return value.toLowerCase() === 'true';
|
||||
} else if (schema instanceof z.ZodObject) {
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
return convertValueBySchema(parsed, schema);
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (schema instanceof z.ZodObject && typeof value === 'object' && value !== null) {
|
||||
const result: any = {};
|
||||
for (const [key, val] of Object.entries(value)) {
|
||||
const fieldSchema = schema.shape[key];
|
||||
if (fieldSchema) {
|
||||
result[key] = convertValueBySchema(val, fieldSchema);
|
||||
} else {
|
||||
result[key] = val;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return value;
|
||||
};
|
||||
|
||||
export const convertObjectBySchema = (obj: any, schema: any): any => {
|
||||
return convertValueBySchema(obj, schema);
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
import type { StructuredTool } from 'langchain/tools';
|
||||
import { type IDataObject, type INodeExecutionData } from 'n8n-workflow';
|
||||
|
||||
import { convertObjectBySchema } from './convertToSchema';
|
||||
|
||||
export async function executeTool(
|
||||
tool: StructuredTool,
|
||||
query: string | object,
|
||||
): Promise<INodeExecutionData> {
|
||||
let convertedQuery: string | object = query;
|
||||
if ('schema' in tool && tool.schema) {
|
||||
convertedQuery = convertObjectBySchema(query, tool.schema);
|
||||
}
|
||||
|
||||
const result = await tool.invoke(convertedQuery);
|
||||
|
||||
return {
|
||||
json: result as IDataObject,
|
||||
};
|
||||
}
|
||||
@@ -38,6 +38,7 @@ describe('McpClientTool', () => {
|
||||
description: 'MyTool does something',
|
||||
name: 'MyTool',
|
||||
value: 'MyTool',
|
||||
inputSchema: { type: 'object', properties: { input: { type: 'string' } } },
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -28,5 +28,6 @@ export async function getTools(this: ILoadOptionsFunctions): Promise<INodeProper
|
||||
name: tool.name,
|
||||
value: tool.name,
|
||||
description: tool.description,
|
||||
inputSchema: tool.inputSchema,
|
||||
}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user