feat(core): Implement Dynamic Parameters within regular nodes used as AI Tools (#10862)

This commit is contained in:
oleg
2024-10-02 13:31:22 +02:00
committed by GitHub
parent ae37035aad
commit ef5b7cf9b7
34 changed files with 1101 additions and 320 deletions

View File

@@ -1,296 +1,435 @@
/**
* @module NodeAsTool
* @description This module converts n8n nodes into LangChain tools by analyzing node parameters,
* identifying placeholders, and generating a Zod schema. It then creates a DynamicStructuredTool
* that can be used in LangChain workflows.
*
* General approach:
* 1. Recursively traverse node parameters to find placeholders, including in nested structures
* 2. Generate a Zod schema based on these placeholders, preserving the nested structure
* 3. Create a DynamicStructuredTool with the schema and a function that executes the n8n node
*
* Example:
* - Node parameters:
* {
* "inputText": "{{ '__PLACEHOLDER: Enter main text to process' }}",
* "options": {
* "language": "{{ '__PLACEHOLDER: Specify language' }}",
* "advanced": {
* "maxLength": "{{ '__PLACEHOLDER: Enter maximum length' }}"
* }
* }
* }
*
* - Generated Zod schema:
* z.object({
* "inputText": z.string().describe("Enter main text to process"),
* "options__language": z.string().describe("Specify language"),
* "options__advanced__maxLength": z.string().describe("Enter maximum length")
* }).required()
*
* - Resulting tool can be called with:
* {
* "inputText": "Hello, world!",
* "options__language": "en",
* "options__advanced__maxLength": "100"
* }
*
* Note: Nested properties are flattened with double underscores in the schema,
* but the tool reconstructs the original nested structure when executing the node.
*/
import { DynamicStructuredTool } from '@langchain/core/tools';
import {
NodeConnectionType,
type IExecuteFunctions,
type INodeParameters,
type INodeType,
} from 'n8n-workflow';
import type { IExecuteFunctions, INodeParameters, INodeType } from 'n8n-workflow';
import { jsonParse, NodeConnectionType, NodeOperationError } from 'n8n-workflow';
import { z } from 'zod';
/** Represents a nested object structure */
type NestedObject = { [key: string]: unknown };
/**
* Encodes a dot-notated key to a format safe for use as an object key.
* @param {string} key - The dot-notated key to encode.
* @returns {string} The encoded key.
*/
function encodeDotNotation(key: string): string {
// Replace dots with double underscores, then handle special case for '__value' for complicated params
return key.replace(/\./g, '__').replace('__value', '');
type AllowedTypes = 'string' | 'number' | 'boolean' | 'json';
interface FromAIArgument {
key: string;
description?: string;
type?: AllowedTypes;
defaultValue?: string | number | boolean | Record<string, unknown>;
}
/**
* Decodes an encoded key back to its original dot-notated form.
* @param {string} key - The encoded key to decode.
* @returns {string} The decoded, dot-notated key.
* AIParametersParser
*
* This class encapsulates the logic for parsing node parameters, extracting $fromAI calls,
* generating Zod schemas, and creating LangChain tools.
*/
function decodeDotNotation(key: string): string {
// Simply replace double underscores with dots
return key.replace(/__/g, '.');
}
class AIParametersParser {
private ctx: IExecuteFunctions;
/**
* Recursively traverses an object to find placeholder values.
* @param {NestedObject} obj - The object to traverse.
* @param {string[]} path - The current path in the object.
* @param {Map<string, string>} results - Map to store found placeholders.
* @returns {Map<string, string>} Updated map of placeholders.
*/
function traverseObject(
obj: NestedObject,
path: string[] = [],
results: Map<string, string> = new Map(),
): Map<string, string> {
for (const [key, value] of Object.entries(obj)) {
const currentPath = [...path, key];
const fullPath = currentPath.join('.');
/**
* Constructs an instance of AIParametersParser.
* @param ctx The execution context.
*/
constructor(ctx: IExecuteFunctions) {
this.ctx = ctx;
}
if (typeof value === 'string' && value.startsWith("{{ '__PLACEHOLDER")) {
// Store placeholder values with their full path
results.set(encodeDotNotation(fullPath), value);
} else if (Array.isArray(value)) {
// Recursively traverse arrays
// eslint-disable-next-line @typescript-eslint/no-use-before-define
traverseArray(value, currentPath, results);
} else if (typeof value === 'object' && value !== null) {
// Recursively traverse nested objects, but only if they're not empty
if (Object.keys(value).length > 0) {
traverseObject(value as NestedObject, currentPath, results);
}
/**
* Generates a Zod schema based on the provided FromAIArgument placeholder.
* @param placeholder The FromAIArgument object containing key, type, description, and defaultValue.
* @returns A Zod schema corresponding to the placeholder's type and constraints.
*/
private generateZodSchema(placeholder: FromAIArgument): z.ZodTypeAny {
let schema: z.ZodTypeAny;
switch (placeholder.type?.toLowerCase()) {
case 'string':
schema = z.string();
break;
case 'number':
schema = z.number();
break;
case 'boolean':
schema = z.boolean();
break;
case 'json':
schema = z.record(z.any());
break;
default:
schema = z.string();
}
if (placeholder.description) {
schema = schema.describe(`${schema.description ?? ''} ${placeholder.description}`.trim());
}
if (placeholder.defaultValue !== undefined) {
schema = schema.default(placeholder.defaultValue);
}
return schema;
}
/**
* Recursively traverses the nodeParameters object to find all $fromAI calls.
* @param payload The current object or value being traversed.
* @param collectedArgs The array collecting FromAIArgument objects.
*/
private traverseNodeParameters(payload: unknown, collectedArgs: FromAIArgument[]) {
if (typeof payload === 'string') {
const fromAICalls = this.extractFromAICalls(payload);
fromAICalls.forEach((call) => collectedArgs.push(call));
} else if (Array.isArray(payload)) {
payload.forEach((item: unknown) => this.traverseNodeParameters(item, collectedArgs));
} else if (typeof payload === 'object' && payload !== null) {
Object.values(payload).forEach((value) => this.traverseNodeParameters(value, collectedArgs));
}
}
return results;
}
/**
* Extracts all $fromAI calls from a given string
* @param str The string to search for $fromAI calls.
* @returns An array of FromAIArgument objects.
*
* This method uses a regular expression to find the start of each $fromAI function call
* in the input string. It then employs a character-by-character parsing approach to
* accurately extract the arguments of each call, handling nested parentheses and quoted strings.
*
* The parsing process:
* 1. Finds the starting position of a $fromAI call using regex.
* 2. Iterates through characters, keeping track of parentheses depth and quote status.
* 3. Handles escaped characters within quotes to avoid premature quote closing.
* 4. Builds the argument string until the matching closing parenthesis is found.
* 5. Parses the extracted argument string into a FromAIArgument object.
* 6. Repeats the process for all $fromAI calls in the input string.
*
*/
private extractFromAICalls(str: string): FromAIArgument[] {
const args: FromAIArgument[] = [];
// Regular expression to match the start of a $fromAI function call
const pattern = /\$fromAI\s*\(\s*/gi;
let match: RegExpExecArray | null;
/**
* Recursively traverses an array to find placeholder values.
* @param {unknown[]} arr - The array to traverse.
* @param {string[]} path - The current path in the array.
* @param {Map<string, string>} results - Map to store found placeholders.
*/
function traverseArray(arr: unknown[], path: string[], results: Map<string, string>): void {
arr.forEach((item, index) => {
const currentPath = [...path, index.toString()];
const fullPath = currentPath.join('.');
while ((match = pattern.exec(str)) !== null) {
const startIndex = match.index + match[0].length;
let current = startIndex;
let inQuotes = false;
let quoteChar = '';
let parenthesesCount = 1;
let argsString = '';
if (typeof item === 'string' && item.startsWith("{{ '__PLACEHOLDER")) {
// Store placeholder values with their full path
results.set(encodeDotNotation(fullPath), item);
} else if (Array.isArray(item)) {
// Recursively traverse nested arrays
traverseArray(item, currentPath, results);
} else if (typeof item === 'object' && item !== null) {
// Recursively traverse nested objects
traverseObject(item as NestedObject, currentPath, results);
}
});
}
// Parse the arguments string, handling nested parentheses and quotes
while (current < str.length && parenthesesCount > 0) {
const char = str[current];
/**
* Builds a nested object structure from matching keys and their values.
* @param {string} baseKey - The base key to start building from.
* @param {string[]} matchingKeys - Array of matching keys.
* @param {Record<string, string>} values - Object containing values for the keys.
* @returns {Record<string, unknown>} The built nested object structure.
*/
function buildStructureFromMatches(
baseKey: string,
matchingKeys: string[],
values: Record<string, string>,
): Record<string, unknown> {
const result = {};
if (inQuotes) {
// Handle characters inside quotes, including escaped characters
if (char === '\\' && current + 1 < str.length) {
argsString += char + str[current + 1];
current += 2;
continue;
}
for (const matchingKey of matchingKeys) {
const decodedKey = decodeDotNotation(matchingKey);
// Extract the part of the key after the base key
const remainingPath = decodedKey
.slice(baseKey.length)
.split('.')
.filter((k) => k !== '');
let current: Record<string, unknown> = result;
if (char === quoteChar) {
inQuotes = false;
quoteChar = '';
}
argsString += char;
} else {
// Handle characters outside quotes
if (['"', "'", '`'].includes(char)) {
inQuotes = true;
quoteChar = char;
} else if (char === '(') {
parenthesesCount++;
} else if (char === ')') {
parenthesesCount--;
}
// Build the nested structure
for (let i = 0; i < remainingPath.length - 1; i++) {
if (!(remainingPath[i] in current)) {
current[remainingPath[i]] = {};
// Only add characters if we're still inside the main parentheses
if (parenthesesCount > 0 || char !== ')') {
argsString += char;
}
}
current++;
}
// If parentheses are balanced, parse the arguments
if (parenthesesCount === 0) {
try {
const parsedArgs = this.parseArguments(argsString);
args.push(parsedArgs);
} catch (error) {
// If parsing fails, throw an ApplicationError with details
throw new NodeOperationError(
this.ctx.getNode(),
`Failed to parse $fromAI arguments: ${argsString}: ${error}`,
);
}
} else {
// Log an error if parentheses are unbalanced
throw new NodeOperationError(
this.ctx.getNode(),
`Unbalanced parentheses while parsing $fromAI call: ${str.slice(startIndex)}`,
);
}
current = current[remainingPath[i]] as Record<string, unknown>;
}
// Set the value at the deepest level
const lastKey = remainingPath[remainingPath.length - 1];
current[lastKey ?? matchingKey] = values[matchingKey];
return args;
}
// If no nested structure was created, return the direct value
return Object.keys(result).length === 0 ? values[encodeDotNotation(baseKey)] : result;
/**
* Parses the arguments of a single $fromAI function call.
* @param argsString The string containing the function arguments.
* @returns A FromAIArgument object.
*/
private parseArguments(argsString: string): FromAIArgument {
// Split arguments by commas not inside quotes
const args: string[] = [];
let currentArg = '';
let inQuotes = false;
let quoteChar = '';
let escapeNext = false;
for (let i = 0; i < argsString.length; i++) {
const char = argsString[i];
if (escapeNext) {
currentArg += char;
escapeNext = false;
continue;
}
if (char === '\\') {
escapeNext = true;
continue;
}
if (['"', "'", '`'].includes(char)) {
if (!inQuotes) {
inQuotes = true;
quoteChar = char;
currentArg += char;
} else if (char === quoteChar) {
inQuotes = false;
quoteChar = '';
currentArg += char;
} else {
currentArg += char;
}
continue;
}
if (char === ',' && !inQuotes) {
args.push(currentArg.trim());
currentArg = '';
continue;
}
currentArg += char;
}
if (currentArg) {
args.push(currentArg.trim());
}
// Remove surrounding quotes if present
const cleanArgs = args.map((arg) => {
const trimmed = arg.trim();
if (
(trimmed.startsWith("'") && trimmed.endsWith("'")) ||
(trimmed.startsWith('`') && trimmed.endsWith('`')) ||
(trimmed.startsWith('"') && trimmed.endsWith('"'))
) {
return trimmed
.slice(1, -1)
.replace(/\\'/g, "'")
.replace(/\\`/g, '`')
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
}
return trimmed;
});
const type = cleanArgs?.[2] || 'string';
if (!['string', 'number', 'boolean', 'json'].includes(type.toLowerCase())) {
throw new NodeOperationError(this.ctx.getNode(), `Invalid type: ${type}`);
}
return {
key: cleanArgs[0] || '',
description: cleanArgs[1],
type: (cleanArgs?.[2] ?? 'string') as AllowedTypes,
defaultValue: this.parseDefaultValue(cleanArgs[3]),
};
}
/**
* Parses the default value, preserving its original type.
* @param value The default value as a string.
* @returns The parsed default value in its appropriate type.
*/
private parseDefaultValue(
value: string | undefined,
): string | number | boolean | Record<string, unknown> | undefined {
if (value === undefined || value === '') return undefined;
const lowerValue = value.toLowerCase();
if (lowerValue === 'true') return true;
if (lowerValue === 'false') return false;
if (!isNaN(Number(value))) return Number(value);
try {
return jsonParse(value);
} catch {
return value;
}
}
/**
* Generates a description for a node based on the provided parameters.
* @param node The node type.
* @param nodeParameters The parameters of the node.
* @returns A string description for the node.
*/
private getDescription(node: INodeType, nodeParameters: INodeParameters): string {
const manualDescription = nodeParameters.toolDescription as string;
if (nodeParameters.descriptionType === 'auto') {
const resource = nodeParameters.resource as string;
const operation = nodeParameters.operation as string;
let description = node.description.description;
if (resource) {
description += `\n Resource: ${resource}`;
}
if (operation) {
description += `\n Operation: ${operation}`;
}
return description.trim();
}
if (nodeParameters.descriptionType === 'manual') {
return manualDescription ?? node.description.description;
}
return node.description.description;
}
/**
* Creates a DynamicStructuredTool from a node.
* @param node The node type.
* @param nodeParameters The parameters of the node.
* @returns A DynamicStructuredTool instance.
*/
public createTool(node: INodeType, nodeParameters: INodeParameters): DynamicStructuredTool {
const collectedArguments: FromAIArgument[] = [];
this.traverseNodeParameters(nodeParameters, collectedArguments);
// Validate each collected argument
const nameValidationRegex = /^[a-zA-Z0-9_-]{1,64}$/;
const keyMap = new Map<string, FromAIArgument>();
for (const argument of collectedArguments) {
if (argument.key.length === 0 || !nameValidationRegex.test(argument.key)) {
const isEmptyError = 'You must specify a key when using $fromAI()';
const isInvalidError = `Parameter key \`${argument.key}\` is invalid`;
const error = new Error(argument.key.length === 0 ? isEmptyError : isInvalidError);
throw new NodeOperationError(this.ctx.getNode(), error, {
description:
'Invalid parameter key, must be between 1 and 64 characters long and only contain letters, numbers, underscores, and hyphens',
});
}
if (keyMap.has(argument.key)) {
// If the key already exists in the Map
const existingArg = keyMap.get(argument.key)!;
// Check if the existing argument has the same description and type
if (
existingArg.description !== argument.description ||
existingArg.type !== argument.type
) {
// If not, throw an error for inconsistent duplicate keys
throw new NodeOperationError(
this.ctx.getNode(),
`Duplicate key '${argument.key}' found with different description or type`,
{
description:
'Ensure all $fromAI() calls with the same key have consistent descriptions and types',
},
);
}
// If the duplicate key has consistent description and type, it's allowed (no action needed)
} else {
// If the key doesn't exist in the Map, add it
keyMap.set(argument.key, argument);
}
}
// Remove duplicate keys, latest occurrence takes precedence
const uniqueArgsMap = collectedArguments.reduce((map, arg) => {
map.set(arg.key, arg);
return map;
}, new Map<string, FromAIArgument>());
const uniqueArguments = Array.from(uniqueArgsMap.values());
// Generate Zod schema from unique arguments
const schemaObj = uniqueArguments.reduce((acc: Record<string, z.ZodTypeAny>, placeholder) => {
acc[placeholder.key] = this.generateZodSchema(placeholder);
return acc;
}, {});
const schema = z.object(schemaObj).required();
const description = this.getDescription(node, nodeParameters);
const nodeName = this.ctx.getNode().name.replace(/ /g, '_');
const name = nodeName || node.description.name;
const tool = new DynamicStructuredTool({
name,
description,
schema,
func: async (functionArgs: z.infer<typeof schema>) => {
const { index } = this.ctx.addInputData(NodeConnectionType.AiTool, [
[{ json: functionArgs }],
]);
try {
// Execute the node with the proxied context
const result = await node.execute?.bind(this.ctx)();
// Process and map the results
const mappedResults = result?.[0]?.flatMap((item) => item.json);
// Add output data to the context
this.ctx.addOutputData(NodeConnectionType.AiTool, index, [
[{ json: { response: mappedResults } }],
]);
// Return the stringified results
return JSON.stringify(mappedResults);
} catch (error) {
const nodeError = new NodeOperationError(this.ctx.getNode(), error as Error);
this.ctx.addOutputData(NodeConnectionType.AiTool, index, nodeError);
return 'Error during node execution: ' + nodeError.description;
}
},
});
return tool;
}
}
/**
* Extracts the description from a placeholder string.
* @param {string} value - The placeholder string.
* @returns {string} The extracted description or a default message.
*/
function extractPlaceholderDescription(value: string): string {
const match = value.match(/{{ '__PLACEHOLDER:\s*(.+?)\s*' }}/);
return match ? match[1] : 'No description provided';
}
/**
* Creates a DynamicStructuredTool from an n8n node.
* @param {INodeType} node - The n8n node to convert.
* @param {IExecuteFunctions} ctx - The execution context.
* @param {INodeParameters} nodeParameters - The node parameters.
* @returns {DynamicStructuredTool} The created tool.
* Converts node into LangChain tool by analyzing node parameters,
* identifying placeholders using the $fromAI function, and generating a Zod schema. It then creates
* a DynamicStructuredTool that can be used in LangChain workflows.
*
* @param ctx The execution context.
* @param node The node type.
* @param nodeParameters The parameters of the node.
* @returns An object containing the DynamicStructuredTool instance.
*/
export function createNodeAsTool(
node: INodeType,
ctx: IExecuteFunctions,
nodeParameters: INodeParameters,
): DynamicStructuredTool {
// Find all placeholder values in the node parameters
const placeholderValues = traverseObject(nodeParameters);
// Generate Zod schema from placeholder values
const schemaObj: { [key: string]: z.ZodString } = {};
for (const [key, value] of placeholderValues.entries()) {
const description = extractPlaceholderDescription(value);
schemaObj[key] = z.string().describe(description);
}
const schema = z.object(schemaObj).required();
// Get the tool description from node parameters or use the default
const toolDescription = ctx.getNodeParameter(
'toolDescription',
0,
node.description.description,
) as string;
type GetNodeParameterMethod = IExecuteFunctions['getNodeParameter'];
const tool = new DynamicStructuredTool({
name: node.description.name,
description: toolDescription ? toolDescription : node.description.description,
schema,
func: async (functionArgs: z.infer<typeof schema>) => {
// Create a proxy for ctx to soft-override parameters with values from the LLM
const ctxProxy = new Proxy(ctx, {
get(target: IExecuteFunctions, prop: string | symbol, receiver: unknown) {
if (prop === 'getNodeParameter') {
// Override getNodeParameter method
// eslint-disable-next-line @typescript-eslint/unbound-method
return new Proxy(target.getNodeParameter, {
apply(
targetMethod: GetNodeParameterMethod,
thisArg: unknown,
argumentsList: Parameters<GetNodeParameterMethod>,
): ReturnType<GetNodeParameterMethod> {
const [key] = argumentsList;
if (typeof key !== 'string') {
// If key is not a string, use the original method
return Reflect.apply(targetMethod, thisArg, argumentsList);
}
const encodedKey = encodeDotNotation(key);
// Check if the full key or any more specific key is a placeholder
const matchingKeys = Array.from(placeholderValues.keys()).filter((k) =>
k.startsWith(encodedKey),
);
if (matchingKeys.length > 0) {
// If there are matching keys, build the structure using args
const res = buildStructureFromMatches(encodedKey, matchingKeys, functionArgs);
// Return either the specific value or the entire built structure
return res?.[decodeDotNotation(key)] ?? res;
}
// If no placeholder is found, use the original function
return Reflect.apply(targetMethod, thisArg, argumentsList);
},
});
}
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Reflect.get(target, prop, receiver);
},
});
// Add input data to the context
ctxProxy.addInputData(NodeConnectionType.AiTool, [[{ json: functionArgs }]]);
// Execute the node with the proxied context
const result = await node.execute?.bind(ctxProxy)();
// Process and map the results
const mappedResults = result?.[0]?.flatMap((item) => item.json);
// Add output data to the context
ctxProxy.addOutputData(NodeConnectionType.AiTool, 0, [
[{ json: { response: mappedResults } }],
]);
// Return the stringified results
return JSON.stringify(mappedResults);
},
});
return tool;
}
/**
* Asynchronously creates a DynamicStructuredTool from an n8n node.
* @param {IExecuteFunctions} ctx - The execution context.
* @param {INodeType} node - The n8n node to convert.
* @param {INodeParameters} nodeParameters - The node parameters.
* @returns {Promise<{response: DynamicStructuredTool}>} A promise that resolves to an object containing the created tool.
*/
export function getNodeAsTool(
ctx: IExecuteFunctions,
node: INodeType,
nodeParameters: INodeParameters,
) {
const parser = new AIParametersParser(ctx);
return {
response: createNodeAsTool(node, ctx, nodeParameters),
response: parser.createTool(node, nodeParameters),
};
}

View File

@@ -148,7 +148,7 @@ import {
UM_EMAIL_TEMPLATES_INVITE,
UM_EMAIL_TEMPLATES_PWRESET,
} from './Constants';
import { getNodeAsTool } from './CreateNodeAsTool';
import { createNodeAsTool } from './CreateNodeAsTool';
import {
getAllWorkflowExecutionMetadata,
getWorkflowExecutionMetadata,
@@ -2852,7 +2852,7 @@ async function getInputConnectionData(
if (!nodeType.supplyData) {
if (nodeType.description.outputs.includes(NodeConnectionType.AiTool)) {
nodeType.supplyData = async function (this: IExecuteFunctions) {
return getNodeAsTool(this, nodeType, this.getNode().parameters);
return createNodeAsTool(this, nodeType, this.getNode().parameters);
};
} else {
throw new ApplicationError('Node does not have a `supplyData` method defined', {