feat(editor): Align DynamicStructuredTool and DynamicTool name fields (#14604)

feat(Code Tool Node): Use node's name instead of separate name field as tool name

feat(Vector Store Tool Node): Use node's name instead of separate name field as tool name

feat(Custom n8n Workflow Tool Node): Use node's name instead of separate name field as tool name
This commit is contained in:
Jaakko Husso
2025-04-16 09:53:53 +03:00
committed by GitHub
parent d42e61bc35
commit 302258dda2
11 changed files with 298 additions and 13 deletions

View File

@@ -0,0 +1,81 @@
import { mock } from 'jest-mock-extended';
import { DynamicTool } from 'langchain/tools';
import { type INode, type ISupplyDataFunctions } from 'n8n-workflow';
import { ToolCode } from './ToolCode.node';
describe('ToolCode', () => {
describe('supplyData', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('should read name from node name on version >=1.2', async () => {
const node = new ToolCode();
const supplyDataResult = await node.supplyData.call(
mock<ISupplyDataFunctions>({
getNode: jest.fn(() => mock<INode>({ typeVersion: 1.2, name: 'test tool' })),
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
switch (paramName) {
case 'description':
return 'description text';
case 'name':
return 'wrong_field';
case 'specifyInputSchema':
return false;
case 'language':
return 'javaScript';
case 'jsCode':
return 'return 1;';
default:
return;
}
}),
}),
0,
);
expect(supplyDataResult.response).toBeInstanceOf(DynamicTool);
const tool = supplyDataResult.response as DynamicTool;
expect(tool.name).toBe('test_tool');
expect(tool.description).toBe('description text');
expect(tool.func).toBeInstanceOf(Function);
});
it('should read name from name parameter on version <1.2', async () => {
const node = new ToolCode();
const supplyDataResult = await node.supplyData.call(
mock<ISupplyDataFunctions>({
getNode: jest.fn(() => mock<INode>({ typeVersion: 1.1, name: 'wrong name' })),
getNodeParameter: jest.fn().mockImplementation((paramName, _itemIndex) => {
switch (paramName) {
case 'description':
return 'description text';
case 'name':
return 'test_tool';
case 'specifyInputSchema':
return false;
case 'language':
return 'javaScript';
case 'jsCode':
return 'return 1;';
default:
return;
}
}),
}),
0,
);
expect(supplyDataResult.response).toBeInstanceOf(DynamicTool);
const tool = supplyDataResult.response as DynamicTool;
expect(tool.name).toBe('test_tool');
expect(tool.description).toBe('description text');
expect(tool.func).toBeInstanceOf(Function);
});
});
});

View File

@@ -20,6 +20,7 @@ import {
buildJsonSchemaExampleField,
schemaTypeField,
} from '@utils/descriptions';
import { nodeNameToToolName } from '@utils/helpers';
import { convertJsonSchemaToZod, generateSchema } from '@utils/schemaParsing';
import { getConnectionHintNoticeField } from '@utils/sharedFields';
@@ -32,7 +33,7 @@ export class ToolCode implements INodeType {
icon: 'fa:code',
iconColor: 'black',
group: ['transform'],
version: [1, 1.1],
version: [1, 1.1, 1.2],
description: 'Write a tool in JS or Python',
defaults: {
name: 'Code Tool',
@@ -88,7 +89,7 @@ export class ToolCode implements INodeType {
'The name of the function to be called, could contain letters, numbers, and underscores only',
displayOptions: {
show: {
'@version': [{ _cnd: { gte: 1.1 } }],
'@version': [1.1],
},
},
},
@@ -181,7 +182,12 @@ export class ToolCode implements INodeType {
const node = this.getNode();
const workflowMode = this.getMode();
const name = this.getNodeParameter('name', itemIndex) as string;
const { typeVersion } = node;
const name =
typeVersion <= 1.1
? (this.getNodeParameter('name', itemIndex) as string)
: nodeNameToToolName(node);
const description = this.getNodeParameter('description', itemIndex) as string;
const useSchema = this.getNodeParameter('specifyInputSchema', itemIndex) as boolean;