UNPKG

@n8n-plus/n8n-plus

Version:

n8n Workflow Automation Tool (plus edition)

221 lines 9.64 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.buildFromJson = buildFromJson; const tool_1 = require("@n8n/agents/tool"); const credential_field_mapping_1 = require("./credential-field-mapping"); const provider_tool_aliases_1 = require("./provider-tool-aliases"); async function buildFromJson(config, toolDescriptors, options) { const { Agent } = await Promise.resolve().then(() => __importStar(require('@n8n/agents'))); const agent = new Agent(config.name); const resolvedModelConfig = await resolveModelConfig(config, options.credentialProvider); agent.model(resolvedModelConfig); const configuredSkills = getConfiguredSkills(config.skills ?? [], options.skills ?? {}); agent.instructions(config.instructions); if (config.tools) { for (const ref of config.tools) { const built = await resolveToolRef(ref, toolDescriptors, options); if (built) { agent.tool(built); } } } agent.skills(configuredSkills); if (config.providerTools) { for (const [name, args] of Object.entries(config.providerTools)) { const resolved = (0, provider_tool_aliases_1.resolveProviderToolName)(name); agent.providerTool({ name: resolved, args }); } } if (config.memory?.enabled) { await applyMemoryFromConfig(agent, config.memory, options.memoryFactory, options.credentialProvider); } if (config.config) { if (config.config.thinking) { const { provider, ...rest } = config.config.thinking; agent.thinking(provider, rest); } if (config.config.toolCallConcurrency) { agent.toolCallConcurrency(config.config.toolCallConcurrency); } if (config.config.maxIterations) { agent.configuration({ maxIterations: config.config.maxIterations }); } } return agent; } function getConfiguredSkills(refs, skills) { const seen = new Set(); const configured = []; for (const ref of refs) { if (seen.has(ref.id)) continue; seen.add(ref.id); const skill = skills[ref.id]; if (!skill) throw new Error(`Skill "${ref.id}" not found in stored skill bodies`); configured.push({ id: ref.id, name: skill.name, description: skill.description, instructions: skill.instructions, }); } return configured; } async function resolveToolRef(ref, descriptors, options) { switch (ref.type) { case 'custom': { const descriptor = descriptors[ref.id]; if (!descriptor) { throw new Error(`Custom tool "${ref.id}" not found in tool descriptors`); } const builtTool = { name: descriptor.name, description: descriptor.description, systemInstruction: descriptor.systemInstruction ?? undefined, inputSchema: descriptor.inputSchema ?? undefined, handler: async (input, ctx) => { return await options.toolExecutor.executeTool(descriptor.name, input, { resumeData: 'resumeData' in ctx ? ctx.resumeData : undefined, parentTelemetry: ctx.parentTelemetry, }); }, providerOptions: descriptor.providerOptions, }; if (ref.requireApproval) { return (0, tool_1.wrapToolForApproval)(builtTool, { requireApproval: true }); } return builtTool; } case 'workflow': { const marker = { name: ref.name ?? ref.workflow, description: ref.description ?? `Execute the "${ref.workflow}" workflow`, editable: false, metadata: { workflowTool: true, workflowName: ref.workflow, options: { name: ref.name, description: ref.description }, }, }; const tool = (await options.resolveTool?.(ref)) ?? marker; if (ref.requireApproval) { return (0, tool_1.wrapToolForApproval)(tool, { requireApproval: true }); } return tool; } case 'node': { const marker = { name: ref.name, description: ref.description ?? `Execute node ${ref.name}`, editable: false, metadata: { nodeTool: true, ...ref.node }, }; const tool = (await options.resolveTool?.(ref)) ?? marker; if (ref.requireApproval) { return (0, tool_1.wrapToolForApproval)(tool, { requireApproval: true }); } return tool; } } } async function applyMemoryFromConfig(agent, memoryConfig, memoryFactory, credentialProvider) { const { Memory } = await Promise.resolve().then(() => __importStar(require('@n8n/agents'))); const memory = new Memory(); const builtMemory = memoryFactory(memoryConfig); memory.storage(await Promise.resolve(builtMemory)); if (memoryConfig.lastMessages) { memory.lastMessages(memoryConfig.lastMessages); } if (memoryConfig.semanticRecall) { memory.semanticRecall(memoryConfig.semanticRecall); } if (memoryConfig.episodicMemory?.enabled === true) { memory.episodicMemory(await resolveEpisodicMemoryJsonConfig(memoryConfig.episodicMemory, credentialProvider)); } if (memoryConfig.observationalMemory?.enabled !== false) { const observationalMemory = memoryConfig.observationalMemory; memory.observationalMemory({ ...(observationalMemory?.observerThresholdTokens !== undefined && { observerThresholdTokens: observationalMemory.observerThresholdTokens, }), ...(observationalMemory?.reflectorThresholdTokens !== undefined && { reflectorThresholdTokens: observationalMemory.reflectorThresholdTokens, }), ...(observationalMemory?.renderTokenBudget !== undefined && { renderTokenBudget: observationalMemory.renderTokenBudget, }), ...(observationalMemory?.observationLogTailLimit !== undefined && { observationLogTailLimit: observationalMemory.observationLogTailLimit, }), ...(observationalMemory?.lockTtlMs !== undefined && { lockTtlMs: observationalMemory.lockTtlMs, }), }); } memory.titleGeneration({ sync: true }); agent.memory(memory); } async function resolveEpisodicMemoryJsonConfig(config, credentialProvider) { const { DEFAULT_EPISODIC_MEMORY_EMBEDDING_MODEL } = await Promise.resolve().then(() => __importStar(require('@n8n/agents'))); const embeddingModel = DEFAULT_EPISODIC_MEMORY_EMBEDDING_MODEL; const raw = await credentialProvider.resolve(config.credential); const mapped = (0, credential_field_mapping_1.mapCredentialForProvider)(getProviderPrefix(embeddingModel), raw); const embeddingProviderOptions = { ...(typeof mapped.apiKey === 'string' && { apiKey: mapped.apiKey }), ...(typeof mapped.baseURL === 'string' && { baseURL: mapped.baseURL }), }; return { enabled: true, ...(config.topK !== undefined && { topK: config.topK }), ...(config.maxEntriesPerRun !== undefined && { maxEntriesPerRun: config.maxEntriesPerRun }), embeddingProviderOptions, }; } async function resolveModelConfig(config, credentialProvider) { if (!config.credential) return config.model; const slashIdx = config.model.indexOf('/'); const providerPrefix = slashIdx !== -1 ? config.model.slice(0, slashIdx) : ''; const raw = await credentialProvider.resolve(config.credential); const mapped = (0, credential_field_mapping_1.mapCredentialForProvider)(providerPrefix, raw); return { id: config.model, ...mapped }; } function getProviderPrefix(modelId) { const slashIdx = modelId.indexOf('/'); return slashIdx !== -1 ? modelId.slice(0, slashIdx) : ''; } //# sourceMappingURL=from-json-config.js.map