n8n
Version:
n8n Workflow Automation Tool
100 lines • 4.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveNodeTool = resolveNodeTool;
const agents_1 = require("@n8n/agents");
const ai_utilities_1 = require("@n8n/ai-utilities");
const n8n_workflow_1 = require("n8n-workflow");
const node_types_1 = require("../../../node-types");
const di_1 = require("@n8n/di");
function toExecutorCredentials(credentials) {
if (!credentials)
return undefined;
const out = {};
for (const [slot, ref] of Object.entries(credentials)) {
if (ref.id)
out[slot] = { id: ref.id, name: ref.name };
}
return Object.keys(out).length > 0 ? out : undefined;
}
function resolveToolNodeType(nodeType, nodeTypeVersion) {
if ((0, n8n_workflow_1.isToolType)(nodeType))
return nodeType;
const toolNodeType = `${nodeType}Tool`;
try {
di_1.Container.get(node_types_1.NodeTypes).getByNameAndVersion(toolNodeType, nodeTypeVersion);
return toolNodeType;
}
catch {
return nodeType;
}
}
async function resolveInputSchema(toolSchema, ctx) {
const collectedArguments = (0, ai_utilities_1.extractFromAIParameters)((toolSchema.node.nodeParameters ?? {}));
if (collectedArguments.length > 0)
return (0, ai_utilities_1.createZodSchemaFromArgs)(collectedArguments);
let nodeType;
const nodeTypeName = resolveToolNodeType(toolSchema.node.nodeType, toolSchema.node.nodeTypeVersion);
try {
nodeType = di_1.Container.get(node_types_1.NodeTypes).getByNameAndVersion(nodeTypeName, toolSchema.node.nodeTypeVersion);
}
catch {
return { type: 'object', properties: {} };
}
if (typeof nodeType.supplyData === 'function') {
const introspected = await ctx.executor.introspectSupplyDataToolSchema({
projectId: ctx.projectId,
nodeType: nodeTypeName,
nodeTypeVersion: toolSchema.node.nodeTypeVersion,
nodeParameters: toolSchema.node.nodeParameters,
credentials: toExecutorCredentials(toolSchema.node.credentials) ?? null,
});
if (introspected)
return introspected;
return {
type: 'object',
properties: {
input: {
type: 'string',
description: toolSchema.description ??
nodeType.description.description ??
`The query or input text to pass to ${toolSchema.node.nodeType}.`,
},
},
required: ['input'],
};
}
return { type: 'object', properties: {} };
}
async function resolveNodeTool(toolSchema, ctx) {
const sanitizedName = (0, n8n_workflow_1.nodeNameToToolName)(toolSchema.name);
const nodeType = resolveToolNodeType(toolSchema.node.nodeType, toolSchema.node.nodeTypeVersion);
const built = new agents_1.Tool(sanitizedName)
.description(toolSchema.description ?? `Execute the ${nodeType} node`)
.input(await resolveInputSchema(toolSchema, ctx))
.handler(async (input) => {
const result = await ctx.executor.executeInline({
nodeType,
nodeTypeVersion: toolSchema.node.nodeTypeVersion,
nodeParameters: toolSchema.node.nodeParameters,
credentialDetails: toExecutorCredentials(toolSchema.node.credentials),
inputData: [{ json: input }],
projectId: ctx.projectId,
});
if (result.status === 'error') {
throw new Error(result.error ?? `Node "${toolSchema.node.nodeType}" failed to execute`);
}
return result;
})
.build();
return {
...built,
metadata: {
kind: 'node',
nodeType: toolSchema.node.nodeType,
nodeTypeVersion: toolSchema.node.nodeTypeVersion,
displayName: toolSchema.name,
nodeParameters: toolSchema.node.nodeParameters,
},
};
}
//# sourceMappingURL=node-tool-factory.js.map