mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
chore: Move AIParametersParser to n8n-workflow (no-changelog) (#12671)
This commit is contained in:
parent
d981b5659a
commit
13652c5ee2
|
@ -1,411 +1,133 @@
|
||||||
import { DynamicStructuredTool } from '@langchain/core/tools';
|
import { DynamicStructuredTool } from '@langchain/core/tools';
|
||||||
import type { IDataObject, INode, INodeType } from 'n8n-workflow';
|
import { generateZodSchema, NodeOperationError, traverseNodeParameters } from 'n8n-workflow';
|
||||||
import { jsonParse, NodeOperationError } from 'n8n-workflow';
|
import type { IDataObject, INode, INodeType, FromAIArgument } from 'n8n-workflow';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
type AllowedTypes = 'string' | 'number' | 'boolean' | 'json';
|
export type CreateNodeAsToolOptions = {
|
||||||
interface FromAIArgument {
|
|
||||||
key: string;
|
|
||||||
description?: string;
|
|
||||||
type?: AllowedTypes;
|
|
||||||
defaultValue?: string | number | boolean | Record<string, unknown>;
|
|
||||||
}
|
|
||||||
|
|
||||||
type ParserOptions = {
|
|
||||||
node: INode;
|
node: INode;
|
||||||
nodeType: INodeType;
|
nodeType: INodeType;
|
||||||
handleToolInvocation: (toolArgs: IDataObject) => Promise<unknown>;
|
handleToolInvocation: (toolArgs: IDataObject) => Promise<unknown>;
|
||||||
};
|
};
|
||||||
|
|
||||||
// This file is temporarily duplicated in `packages/@n8n/nodes-langchain/nodes/tools/ToolWorkflow/v2/utils/FromAIParser.ts`
|
|
||||||
// Please apply any changes in both files
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AIParametersParser
|
* Retrieves and validates the Zod schema for the tool.
|
||||||
*
|
*
|
||||||
* This class encapsulates the logic for parsing node parameters, extracting $fromAI calls,
|
* This method:
|
||||||
* generating Zod schemas, and creating LangChain tools.
|
* 1. Collects all $fromAI arguments from node parameters
|
||||||
|
* 2. Validates parameter keys against naming rules
|
||||||
|
* 3. Checks for duplicate keys and ensures consistency
|
||||||
|
* 4. Generates a Zod schema from the validated arguments
|
||||||
|
*
|
||||||
|
* @throws {NodeOperationError} When parameter keys are invalid or when duplicate keys have inconsistent definitions
|
||||||
|
* @returns {z.ZodObject} A Zod schema object representing the structure and validation rules for the node parameters
|
||||||
*/
|
*/
|
||||||
class AIParametersParser {
|
function getSchema(node: INode) {
|
||||||
/**
|
const collectedArguments: FromAIArgument[] = [];
|
||||||
* Constructs an instance of AIParametersParser.
|
try {
|
||||||
*/
|
traverseNodeParameters(node.parameters, collectedArguments);
|
||||||
constructor(private readonly options: ParserOptions) {}
|
} catch (error) {
|
||||||
|
throw new NodeOperationError(node, error as Error);
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Validate each collected argument
|
||||||
* Recursively traverses the nodeParameters object to find all $fromAI calls.
|
const nameValidationRegex = /^[a-zA-Z0-9_-]{1,64}$/;
|
||||||
* @param payload The current object or value being traversed.
|
const keyMap = new Map<string, FromAIArgument>();
|
||||||
* @param collectedArgs The array collecting FromAIArgument objects.
|
for (const argument of collectedArguments) {
|
||||||
*/
|
if (argument.key.length === 0 || !nameValidationRegex.test(argument.key)) {
|
||||||
private traverseNodeParameters(payload: unknown, collectedArgs: FromAIArgument[]) {
|
const isEmptyError = 'You must specify a key when using $fromAI()';
|
||||||
if (typeof payload === 'string') {
|
const isInvalidError = `Parameter key \`${argument.key}\` is invalid`;
|
||||||
const fromAICalls = this.extractFromAICalls(payload);
|
const error = new Error(argument.key.length === 0 ? isEmptyError : isInvalidError);
|
||||||
fromAICalls.forEach((call) => collectedArgs.push(call));
|
throw new NodeOperationError(node, error, {
|
||||||
} else if (Array.isArray(payload)) {
|
description:
|
||||||
payload.forEach((item: unknown) => this.traverseNodeParameters(item, collectedArgs));
|
'Invalid parameter key, must be between 1 and 64 characters long and only contain letters, numbers, underscores, and hyphens',
|
||||||
} else if (typeof payload === 'object' && payload !== null) {
|
});
|
||||||
Object.values(payload).forEach((value) => this.traverseNodeParameters(value, collectedArgs));
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (keyMap.has(argument.key)) {
|
||||||
* Extracts all $fromAI calls from a given string
|
// If the key already exists in the Map
|
||||||
* @param str The string to search for $fromAI calls.
|
const existingArg = keyMap.get(argument.key)!;
|
||||||
* @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;
|
|
||||||
|
|
||||||
while ((match = pattern.exec(str)) !== null) {
|
// Check if the existing argument has the same description and type
|
||||||
const startIndex = match.index + match[0].length;
|
if (existingArg.description !== argument.description || existingArg.type !== argument.type) {
|
||||||
let current = startIndex;
|
// If not, throw an error for inconsistent duplicate keys
|
||||||
let inQuotes = false;
|
|
||||||
let quoteChar = '';
|
|
||||||
let parenthesesCount = 1;
|
|
||||||
let argsString = '';
|
|
||||||
|
|
||||||
// Parse the arguments string, handling nested parentheses and quotes
|
|
||||||
while (current < str.length && parenthesesCount > 0) {
|
|
||||||
const char = str[current];
|
|
||||||
|
|
||||||
if (inQuotes) {
|
|
||||||
// Handle characters inside quotes, including escaped characters
|
|
||||||
if (char === '\\' && current + 1 < str.length) {
|
|
||||||
argsString += char + str[current + 1];
|
|
||||||
current += 2;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
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--;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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.options.node,
|
|
||||||
`Failed to parse $fromAI arguments: ${argsString}: ${error}`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Log an error if parentheses are unbalanced
|
|
||||||
throw new NodeOperationError(
|
throw new NodeOperationError(
|
||||||
this.options.node,
|
node,
|
||||||
`Unbalanced parentheses while parsing $fromAI call: ${str.slice(startIndex)}`,
|
`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 {
|
||||||
return args;
|
// If the key doesn't exist in the Map, add it
|
||||||
}
|
keyMap.set(argument.key, argument);
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.options.node, `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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
// Remove duplicate keys, latest occurrence takes precedence
|
||||||
* Retrieves and validates the Zod schema for the tool.
|
const uniqueArgsMap = collectedArguments.reduce((map, arg) => {
|
||||||
*
|
map.set(arg.key, arg);
|
||||||
* This method:
|
return map;
|
||||||
* 1. Collects all $fromAI arguments from node parameters
|
}, new Map<string, FromAIArgument>());
|
||||||
* 2. Validates parameter keys against naming rules
|
|
||||||
* 3. Checks for duplicate keys and ensures consistency
|
|
||||||
* 4. Generates a Zod schema from the validated arguments
|
|
||||||
*
|
|
||||||
* @throws {NodeOperationError} When parameter keys are invalid or when duplicate keys have inconsistent definitions
|
|
||||||
* @returns {z.ZodObject} A Zod schema object representing the structure and validation rules for the node parameters
|
|
||||||
*/
|
|
||||||
private getSchema() {
|
|
||||||
const { node } = this.options;
|
|
||||||
const collectedArguments: FromAIArgument[] = [];
|
|
||||||
this.traverseNodeParameters(node.parameters, collectedArguments);
|
|
||||||
|
|
||||||
// Validate each collected argument
|
const uniqueArguments = Array.from(uniqueArgsMap.values());
|
||||||
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(node, 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)) {
|
// Generate Zod schema from unique arguments
|
||||||
// If the key already exists in the Map
|
const schemaObj = uniqueArguments.reduce((acc: Record<string, z.ZodTypeAny>, placeholder) => {
|
||||||
const existingArg = keyMap.get(argument.key)!;
|
acc[placeholder.key] = generateZodSchema(placeholder);
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
|
||||||
// Check if the existing argument has the same description and type
|
return z.object(schemaObj).required();
|
||||||
if (
|
}
|
||||||
existingArg.description !== argument.description ||
|
|
||||||
existingArg.type !== argument.type
|
/**
|
||||||
) {
|
* Generates a description for a node based on the provided parameters.
|
||||||
// If not, throw an error for inconsistent duplicate keys
|
* @param node The node type.
|
||||||
throw new NodeOperationError(
|
* @param nodeParameters The parameters of the node.
|
||||||
node,
|
* @returns A string description for the node.
|
||||||
`Duplicate key '${argument.key}' found with different description or type`,
|
*/
|
||||||
{
|
function makeDescription(node: INode, nodeType: INodeType): string {
|
||||||
description:
|
const manualDescription = node.parameters.toolDescription as string;
|
||||||
'Ensure all $fromAI() calls with the same key have consistent descriptions and types',
|
|
||||||
},
|
if (node.parameters.descriptionType === 'auto') {
|
||||||
);
|
const resource = node.parameters.resource as string;
|
||||||
}
|
const operation = node.parameters.operation as string;
|
||||||
// If the duplicate key has consistent description and type, it's allowed (no action needed)
|
let description = nodeType.description.description;
|
||||||
} else {
|
if (resource) {
|
||||||
// If the key doesn't exist in the Map, add it
|
description += `\n Resource: ${resource}`;
|
||||||
keyMap.set(argument.key, argument);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if (operation) {
|
||||||
// Remove duplicate keys, latest occurrence takes precedence
|
description += `\n Operation: ${operation}`;
|
||||||
const uniqueArgsMap = collectedArguments.reduce((map, arg) => {
|
}
|
||||||
map.set(arg.key, arg);
|
return description.trim();
|
||||||
return map;
|
}
|
||||||
}, new Map<string, FromAIArgument>());
|
if (node.parameters.descriptionType === 'manual') {
|
||||||
|
return manualDescription ?? nodeType.description.description;
|
||||||
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;
|
|
||||||
}, {});
|
|
||||||
|
|
||||||
return z.object(schemaObj).required();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return nodeType.description.description;
|
||||||
* 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(): string {
|
|
||||||
const { node, nodeType } = this.options;
|
|
||||||
const manualDescription = node.parameters.toolDescription as string;
|
|
||||||
|
|
||||||
if (node.parameters.descriptionType === 'auto') {
|
/**
|
||||||
const resource = node.parameters.resource as string;
|
* Creates a DynamicStructuredTool from a node.
|
||||||
const operation = node.parameters.operation as string;
|
* @returns A DynamicStructuredTool instance.
|
||||||
let description = nodeType.description.description;
|
*/
|
||||||
if (resource) {
|
function createTool(options: CreateNodeAsToolOptions) {
|
||||||
description += `\n Resource: ${resource}`;
|
const { node, nodeType, handleToolInvocation } = options;
|
||||||
}
|
const schema = getSchema(node);
|
||||||
if (operation) {
|
const description = makeDescription(node, nodeType);
|
||||||
description += `\n Operation: ${operation}`;
|
const nodeName = node.name.replace(/ /g, '_');
|
||||||
}
|
const name = nodeName || nodeType.description.name;
|
||||||
return description.trim();
|
|
||||||
}
|
|
||||||
if (node.parameters.descriptionType === 'manual') {
|
|
||||||
return manualDescription ?? nodeType.description.description;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nodeType.description.description;
|
return new DynamicStructuredTool({
|
||||||
}
|
name,
|
||||||
|
description,
|
||||||
/**
|
schema,
|
||||||
* Creates a DynamicStructuredTool from a node.
|
func: async (toolArgs: z.infer<typeof schema>) => await handleToolInvocation(toolArgs),
|
||||||
* @returns A DynamicStructuredTool instance.
|
});
|
||||||
*/
|
|
||||||
createTool(): DynamicStructuredTool {
|
|
||||||
const { node, nodeType } = this.options;
|
|
||||||
const schema = this.getSchema();
|
|
||||||
const description = this.getDescription();
|
|
||||||
const nodeName = node.name.replace(/ /g, '_');
|
|
||||||
const name = nodeName || nodeType.description.name;
|
|
||||||
|
|
||||||
return new DynamicStructuredTool({
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
schema,
|
|
||||||
func: async (toolArgs: z.infer<typeof schema>) =>
|
|
||||||
await this.options.handleToolInvocation(toolArgs),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -413,7 +135,6 @@ class AIParametersParser {
|
||||||
* identifying placeholders using the $fromAI function, and generating a Zod schema. It then creates
|
* identifying placeholders using the $fromAI function, and generating a Zod schema. It then creates
|
||||||
* a DynamicStructuredTool that can be used in LangChain workflows.
|
* a DynamicStructuredTool that can be used in LangChain workflows.
|
||||||
*/
|
*/
|
||||||
export function createNodeAsTool(options: ParserOptions) {
|
export function createNodeAsTool(options: CreateNodeAsToolOptions) {
|
||||||
const parser = new AIParametersParser(options);
|
return { response: createTool(options) };
|
||||||
return { response: parser.createTool() };
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,6 +58,7 @@
|
||||||
"recast": "0.21.5",
|
"recast": "0.21.5",
|
||||||
"title-case": "3.0.3",
|
"title-case": "3.0.3",
|
||||||
"transliteration": "2.3.5",
|
"transliteration": "2.3.5",
|
||||||
"xml2js": "catalog:"
|
"xml2js": "catalog:",
|
||||||
|
"zod": "catalog:"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
265
packages/workflow/src/FromAIParseUtils.ts
Normal file
265
packages/workflow/src/FromAIParseUtils.ts
Normal file
|
@ -0,0 +1,265 @@
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
import { jsonParse } from './utils';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This file contains the logic for parsing node parameters and extracting $fromAI calls
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type FromAIArgumentType = 'string' | 'number' | 'boolean' | 'json';
|
||||||
|
export type FromAIArgument = {
|
||||||
|
key: string;
|
||||||
|
description?: string;
|
||||||
|
type?: FromAIArgumentType;
|
||||||
|
defaultValue?: string | number | boolean | Record<string, unknown>;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ParseError extends Error {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
export function 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
function 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the arguments of a single $fromAI function call.
|
||||||
|
* @param argsString The string containing the function arguments.
|
||||||
|
* @returns A FromAIArgument object.
|
||||||
|
*/
|
||||||
|
function 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 ParseError(`Invalid type: ${type}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
key: cleanArgs[0] || '',
|
||||||
|
description: cleanArgs[1],
|
||||||
|
type: (cleanArgs?.[2] ?? 'string') as FromAIArgumentType,
|
||||||
|
defaultValue: parseDefaultValue(cleanArgs[3]),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
export function 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;
|
||||||
|
|
||||||
|
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 = '';
|
||||||
|
|
||||||
|
// Parse the arguments string, handling nested parentheses and quotes
|
||||||
|
while (current < str.length && parenthesesCount > 0) {
|
||||||
|
const char = str[current];
|
||||||
|
|
||||||
|
if (inQuotes) {
|
||||||
|
// Handle characters inside quotes, including escaped characters
|
||||||
|
if (char === '\\' && current + 1 < str.length) {
|
||||||
|
argsString += char + str[current + 1];
|
||||||
|
current += 2;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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--;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 = parseArguments(argsString);
|
||||||
|
args.push(parsedArgs);
|
||||||
|
} catch (error) {
|
||||||
|
// If parsing fails, throw an ParseError with details
|
||||||
|
throw new ParseError(`Failed to parse $fromAI arguments: ${argsString}: ${String(error)}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Log an error if parentheses are unbalanced
|
||||||
|
throw new ParseError(
|
||||||
|
`Unbalanced parentheses while parsing $fromAI call: ${str.slice(startIndex)}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
export function traverseNodeParameters(payload: unknown, collectedArgs: FromAIArgument[]) {
|
||||||
|
if (typeof payload === 'string') {
|
||||||
|
const fromAICalls = extractFromAICalls(payload);
|
||||||
|
fromAICalls.forEach((call) => collectedArgs.push(call));
|
||||||
|
} else if (Array.isArray(payload)) {
|
||||||
|
payload.forEach((item: unknown) => traverseNodeParameters(item, collectedArgs));
|
||||||
|
} else if (typeof payload === 'object' && payload !== null) {
|
||||||
|
Object.values(payload).forEach((value) => traverseNodeParameters(value, collectedArgs));
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ export * from './Interfaces';
|
||||||
export * from './MessageEventBus';
|
export * from './MessageEventBus';
|
||||||
export * from './ExecutionStatus';
|
export * from './ExecutionStatus';
|
||||||
export * from './Expression';
|
export * from './Expression';
|
||||||
|
export * from './FromAIParseUtils';
|
||||||
export * from './NodeHelpers';
|
export * from './NodeHelpers';
|
||||||
export * from './Workflow';
|
export * from './Workflow';
|
||||||
export * from './WorkflowDataProxy';
|
export * from './WorkflowDataProxy';
|
||||||
|
|
84
packages/workflow/test/FromAIParseUtils.test.ts
Normal file
84
packages/workflow/test/FromAIParseUtils.test.ts
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
import {
|
||||||
|
extractFromAICalls,
|
||||||
|
type FromAIArgument,
|
||||||
|
traverseNodeParameters,
|
||||||
|
} from '@/FromAIParseUtils';
|
||||||
|
|
||||||
|
// Note that for historic reasons a lot of testing of this file happens indirectly in `packages/core/test/CreateNodeAsTool.test.ts`
|
||||||
|
|
||||||
|
describe('extractFromAICalls', () => {
|
||||||
|
test.each<[string, [unknown, unknown, unknown, unknown]]>([
|
||||||
|
['$fromAI("a", "b", "string")', ['a', 'b', 'string', undefined]],
|
||||||
|
['$fromAI("a", "b", "number", 5)', ['a', 'b', 'number', 5]],
|
||||||
|
['{{ $fromAI("a", "b", "boolean") }}', ['a', 'b', 'boolean', undefined]],
|
||||||
|
])('should parse args as expected for %s', (formula, [key, description, type, defaultValue]) => {
|
||||||
|
expect(extractFromAICalls(formula)).toEqual([
|
||||||
|
{
|
||||||
|
key,
|
||||||
|
description,
|
||||||
|
type,
|
||||||
|
defaultValue,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
['$fromAI("a", "b", "c")'],
|
||||||
|
['$fromAI("a", "b", "string"'],
|
||||||
|
['$fromAI("a", "b", "string, "d")'],
|
||||||
|
])('should throw as expected for %s', (formula) => {
|
||||||
|
expect(() => extractFromAICalls(formula)).toThrowError();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('supports multiple calls', () => {
|
||||||
|
const code = '$fromAI("a", "b", "number"); $fromAI("c", "d", "string")';
|
||||||
|
|
||||||
|
expect(extractFromAICalls(code)).toEqual([
|
||||||
|
{
|
||||||
|
key: 'a',
|
||||||
|
description: 'b',
|
||||||
|
type: 'number',
|
||||||
|
defaultValue: undefined,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'c',
|
||||||
|
description: 'd',
|
||||||
|
type: 'string',
|
||||||
|
defaultValue: undefined,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('supports no calls', () => {
|
||||||
|
const code = 'fromAI("a", "b", "number")';
|
||||||
|
|
||||||
|
expect(extractFromAICalls(code)).toEqual([]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('traverseNodeParameters', () => {
|
||||||
|
test.each<[string | string[] | Record<string, string>, [unknown, unknown, unknown, unknown]]>([
|
||||||
|
['$fromAI("a", "b", "string")', ['a', 'b', 'string', undefined]],
|
||||||
|
['$fromAI("a", "b", "number", 5)', ['a', 'b', 'number', 5]],
|
||||||
|
['{{ $fromAI("a", "b", "boolean") }}', ['a', 'b', 'boolean', undefined]],
|
||||||
|
[{ a: '{{ $fromAI("a", "b", "boolean") }}', b: 'five' }, ['a', 'b', 'boolean', undefined]],
|
||||||
|
[
|
||||||
|
['red', '{{ $fromAI("a", "b", "boolean") }}'],
|
||||||
|
['a', 'b', 'boolean', undefined],
|
||||||
|
],
|
||||||
|
])(
|
||||||
|
'should parse args as expected for %s',
|
||||||
|
(parameters, [key, description, type, defaultValue]) => {
|
||||||
|
const out: FromAIArgument[] = [];
|
||||||
|
traverseNodeParameters(parameters, out);
|
||||||
|
expect(out).toEqual([
|
||||||
|
{
|
||||||
|
key,
|
||||||
|
description,
|
||||||
|
type,
|
||||||
|
defaultValue,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
|
@ -2020,6 +2020,9 @@ importers:
|
||||||
xml2js:
|
xml2js:
|
||||||
specifier: 'catalog:'
|
specifier: 'catalog:'
|
||||||
version: 0.6.2
|
version: 0.6.2
|
||||||
|
zod:
|
||||||
|
specifier: 'catalog:'
|
||||||
|
version: 3.24.1
|
||||||
devDependencies:
|
devDependencies:
|
||||||
'@langchain/core':
|
'@langchain/core':
|
||||||
specifier: 'catalog:'
|
specifier: 'catalog:'
|
||||||
|
|
Loading…
Reference in a new issue