feat(core): Implement wrapping of regular nodes as AI Tools (#10641)

Co-authored-by: JP van Oosten <jp@n8n.io>
This commit is contained in:
oleg
2024-09-04 12:06:17 +02:00
committed by GitHub
parent f114035a6b
commit da44fe4b89
13 changed files with 615 additions and 18 deletions

View File

@@ -36,6 +36,7 @@ import type {
NodeParameterValue,
ResourceMapperValue,
INodeTypeDescription,
INodeTypeBaseDescription,
INodeOutputConfiguration,
INodeInputConfiguration,
GenericValue,
@@ -351,6 +352,58 @@ const declarativeNodeOptionParameters: INodeProperties = {
],
};
/**
* Determines if the node is of INodeType
*/
export function isINodeType(obj: unknown): obj is INodeType {
return typeof obj === 'object' && obj !== null && 'execute' in obj;
}
/**
* Modifies the description of the passed in object, such that it can be used
* as an AI Agent Tool.
* Returns the modified item (not copied)
*/
export function convertNodeToAiTool<
T extends object & { description: INodeTypeDescription | INodeTypeBaseDescription },
>(item: T): T {
// quick helper function for typeguard down below
function isFullDescription(obj: unknown): obj is INodeTypeDescription {
return typeof obj === 'object' && obj !== null && 'properties' in obj;
}
if (isFullDescription(item.description)) {
item.description.name += 'Tool';
item.description.inputs = [];
item.description.outputs = [NodeConnectionType.AiTool];
item.description.displayName += ' Tool (wrapped)';
delete item.description.usableAsTool;
if (!item.description.properties.map((prop) => prop.name).includes('toolDescription')) {
const descProp: INodeProperties = {
displayName: 'Description',
name: 'toolDescription',
type: 'string',
default: item.description.description,
required: true,
typeOptions: { rows: 2 },
description:
'Explain to the LLM what this tool does, a good, specific description would allow LLMs to produce expected results much more often',
placeholder: `e.g. ${item.description.description}`,
};
item.description.properties.unshift(descProp);
}
}
item.description.codex = {
categories: ['AI'],
subcategories: {
AI: ['Tools'],
Tools: ['Other Tools'],
},
};
return item;
}
/**
* Determines if the provided node type has any output types other than the main connection type.
* @param typeDescription The node's type description to check.