mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-20 03:12:15 +00:00
feat(core): Implement Dynamic Parameters within regular nodes used as AI Tools (#10862)
This commit is contained in:
@@ -359,7 +359,7 @@ const declarativeNodeOptionParameters: INodeProperties = {
|
||||
export function convertNodeToAiTool<
|
||||
T extends object & { description: INodeTypeDescription | INodeTypeBaseDescription },
|
||||
>(item: T): T {
|
||||
// quick helper function for typeguard down below
|
||||
// quick helper function for type-guard down below
|
||||
function isFullDescription(obj: unknown): obj is INodeTypeDescription {
|
||||
return typeof obj === 'object' && obj !== null && 'properties' in obj;
|
||||
}
|
||||
@@ -368,9 +368,33 @@ export function convertNodeToAiTool<
|
||||
item.description.name += 'Tool';
|
||||
item.description.inputs = [];
|
||||
item.description.outputs = [NodeConnectionType.AiTool];
|
||||
item.description.displayName += ' Tool (wrapped)';
|
||||
item.description.displayName += ' Tool';
|
||||
delete item.description.usableAsTool;
|
||||
|
||||
const hasResource = item.description.properties.some((prop) => prop.name === 'resource');
|
||||
const hasOperation = item.description.properties.some((prop) => prop.name === 'operation');
|
||||
|
||||
if (!item.description.properties.map((prop) => prop.name).includes('toolDescription')) {
|
||||
const descriptionType: INodeProperties = {
|
||||
displayName: 'Tool Description',
|
||||
name: 'descriptionType',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Set Automatically',
|
||||
value: 'auto',
|
||||
description: 'Automatically set based on resource and operation',
|
||||
},
|
||||
{
|
||||
name: 'Set Manually',
|
||||
value: 'manual',
|
||||
description: 'Manually set the description',
|
||||
},
|
||||
],
|
||||
default: 'auto',
|
||||
};
|
||||
|
||||
const descProp: INodeProperties = {
|
||||
displayName: 'Description',
|
||||
name: 'toolDescription',
|
||||
@@ -382,7 +406,29 @@ export function convertNodeToAiTool<
|
||||
'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}`,
|
||||
};
|
||||
|
||||
const noticeProp: INodeProperties = {
|
||||
displayName: 'Use the expression {{ $fromAI() }} for any data to be filled by the model',
|
||||
name: 'notice',
|
||||
type: 'notice',
|
||||
default: '',
|
||||
};
|
||||
|
||||
item.description.properties.unshift(descProp);
|
||||
|
||||
// If node has resource or operation we can determine pre-populate tool description based on it
|
||||
// so we add the descriptionType property as the first property
|
||||
if (hasResource || hasOperation) {
|
||||
item.description.properties.unshift(descriptionType);
|
||||
|
||||
descProp.displayOptions = {
|
||||
show: {
|
||||
descriptionType: ['manual'],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
item.description.properties.unshift(noticeProp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -961,6 +961,43 @@ export class WorkflowDataProxy {
|
||||
return taskData.data!.main[previousNodeOutput]![pairedItem.item];
|
||||
};
|
||||
|
||||
const handleFromAi = (
|
||||
name: string,
|
||||
_description?: string,
|
||||
_type: string = 'string',
|
||||
defaultValue?: unknown,
|
||||
) => {
|
||||
if (!name || name === '') {
|
||||
throw new ExpressionError('Please provide a key', {
|
||||
runIndex: that.runIndex,
|
||||
itemIndex: that.itemIndex,
|
||||
});
|
||||
}
|
||||
const nameValidationRegex = /^[a-zA-Z0-9_-]{0,64}$/;
|
||||
if (!nameValidationRegex.test(name)) {
|
||||
throw new ExpressionError(
|
||||
'Invalid parameter key, must be between 1 and 64 characters long and only contain lowercase letters, uppercase letters, numbers, underscores, and hyphens',
|
||||
{
|
||||
runIndex: that.runIndex,
|
||||
itemIndex: that.itemIndex,
|
||||
},
|
||||
);
|
||||
}
|
||||
const placeholdersDataInputData =
|
||||
that.runExecutionData?.resultData.runData[that.activeNodeName]?.[0].inputOverride?.[
|
||||
NodeConnectionType.AiTool
|
||||
]?.[0]?.[0].json;
|
||||
|
||||
if (Boolean(!placeholdersDataInputData)) {
|
||||
throw new ExpressionError('No execution data available', {
|
||||
runIndex: that.runIndex,
|
||||
itemIndex: that.itemIndex,
|
||||
type: 'no_execution_data',
|
||||
});
|
||||
}
|
||||
return placeholdersDataInputData?.[name] ?? defaultValue;
|
||||
};
|
||||
|
||||
const base = {
|
||||
$: (nodeName: string) => {
|
||||
if (!nodeName) {
|
||||
@@ -1303,6 +1340,10 @@ export class WorkflowDataProxy {
|
||||
);
|
||||
return dataProxy.getDataProxy();
|
||||
},
|
||||
$fromAI: handleFromAi,
|
||||
// Make sure mis-capitalized $fromAI is handled correctly even though we don't auto-complete it
|
||||
$fromai: handleFromAi,
|
||||
$fromAi: handleFromAi,
|
||||
$items: (nodeName?: string, outputIndex?: number, runIndex?: number) => {
|
||||
if (nodeName === undefined) {
|
||||
nodeName = (that.prevNodeGetter() as { name: string }).name;
|
||||
|
||||
Reference in New Issue
Block a user