mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-19 19:11:13 +00:00
fix(Think Tool Node): Use dynamic tool name based on node name (#17364)
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { DynamicTool } from 'langchain/tools';
|
import { DynamicTool } from 'langchain/tools';
|
||||||
import {
|
import {
|
||||||
NodeConnectionTypes,
|
NodeConnectionTypes,
|
||||||
|
nodeNameToToolName,
|
||||||
type INodeType,
|
type INodeType,
|
||||||
type INodeTypeDescription,
|
type INodeTypeDescription,
|
||||||
type ISupplyDataFunctions,
|
type ISupplyDataFunctions,
|
||||||
@@ -22,7 +23,7 @@ export class ToolThink implements INodeType {
|
|||||||
icon: 'fa:brain',
|
icon: 'fa:brain',
|
||||||
iconColor: 'black',
|
iconColor: 'black',
|
||||||
group: ['transform'],
|
group: ['transform'],
|
||||||
version: 1,
|
version: [1, 1.1],
|
||||||
description: 'Invite the AI agent to do some thinking',
|
description: 'Invite the AI agent to do some thinking',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: 'Think',
|
name: 'Think',
|
||||||
@@ -62,9 +63,14 @@ export class ToolThink implements INodeType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
async supplyData(this: ISupplyDataFunctions, itemIndex: number): Promise<SupplyData> {
|
||||||
|
const node = this.getNode();
|
||||||
|
const { typeVersion } = node;
|
||||||
|
|
||||||
|
const name = typeVersion === 1 ? 'thinking_tool' : nodeNameToToolName(node);
|
||||||
const description = this.getNodeParameter('description', itemIndex) as string;
|
const description = this.getNodeParameter('description', itemIndex) as string;
|
||||||
|
|
||||||
const tool = new DynamicTool({
|
const tool = new DynamicTool({
|
||||||
name: 'thinking_tool',
|
name,
|
||||||
description,
|
description,
|
||||||
func: async (subject: string) => {
|
func: async (subject: string) => {
|
||||||
return subject;
|
return subject;
|
||||||
|
|||||||
@@ -1,27 +1,34 @@
|
|||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
import { DynamicTool } from 'langchain/tools';
|
import { DynamicTool } from 'langchain/tools';
|
||||||
import type { ISupplyDataFunctions } from 'n8n-workflow';
|
import type { ISupplyDataFunctions, INode } from 'n8n-workflow';
|
||||||
|
|
||||||
import { ToolThink } from '../ToolThink.node';
|
import { ToolThink } from '../ToolThink.node';
|
||||||
|
|
||||||
describe('ToolThink', () => {
|
describe('ToolThink', () => {
|
||||||
const thinkTool = new ToolThink();
|
const thinkTool = new ToolThink();
|
||||||
const helpers = mock<ISupplyDataFunctions['helpers']>();
|
const helpers = mock<ISupplyDataFunctions['helpers']>();
|
||||||
const executeFunctions = mock<ISupplyDataFunctions>({
|
|
||||||
helpers,
|
const createExecuteFunctions = (node: Partial<INode> = { typeVersion: 1 }) => {
|
||||||
});
|
const executeFunctions = mock<ISupplyDataFunctions>({
|
||||||
executeFunctions.addInputData.mockReturnValue({ index: 0 });
|
helpers,
|
||||||
executeFunctions.getNodeParameter.mockImplementation((paramName: string) => {
|
});
|
||||||
switch (paramName) {
|
executeFunctions.addInputData.mockReturnValue({ index: 0 });
|
||||||
case 'description':
|
executeFunctions.getNodeParameter.mockImplementation((paramName: string) => {
|
||||||
return 'Tool description';
|
switch (paramName) {
|
||||||
default:
|
case 'description':
|
||||||
return undefined;
|
return 'Tool description';
|
||||||
}
|
default:
|
||||||
});
|
return undefined;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
executeFunctions.getNode.mockReturnValue(mock<INode>(node));
|
||||||
|
return executeFunctions;
|
||||||
|
};
|
||||||
|
|
||||||
describe('Tool response', () => {
|
describe('Tool response', () => {
|
||||||
it('should return the same text as response when receiving a text input', async () => {
|
it('should return the same text as response when receiving a text input', async () => {
|
||||||
|
const executeFunctions = createExecuteFunctions();
|
||||||
|
|
||||||
const { response } = (await thinkTool.supplyData.call(executeFunctions, 0)) as {
|
const { response } = (await thinkTool.supplyData.call(executeFunctions, 0)) as {
|
||||||
response: DynamicTool;
|
response: DynamicTool;
|
||||||
};
|
};
|
||||||
@@ -30,5 +37,26 @@ describe('ToolThink', () => {
|
|||||||
const res = (await response.invoke('foo')) as string;
|
const res = (await response.invoke('foo')) as string;
|
||||||
expect(res).toEqual('foo');
|
expect(res).toEqual('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should use hardcoded name for version 1', async () => {
|
||||||
|
const executeFunctions = createExecuteFunctions({ typeVersion: 1 });
|
||||||
|
|
||||||
|
const { response } = (await thinkTool.supplyData.call(executeFunctions, 0)) as {
|
||||||
|
response: DynamicTool;
|
||||||
|
};
|
||||||
|
expect(response.name).toEqual('thinking_tool');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should use dynamic name from node for version 1.1', async () => {
|
||||||
|
const executeFunctions = createExecuteFunctions({
|
||||||
|
typeVersion: 1.1,
|
||||||
|
name: 'My Thinking Tool',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { response } = (await thinkTool.supplyData.call(executeFunctions, 0)) as {
|
||||||
|
response: DynamicTool;
|
||||||
|
};
|
||||||
|
expect(response.name).toEqual('My_Thinking_Tool');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user