UNPKG

n8n

Version:

n8n Workflow Automation Tool

343 lines • 15.1 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; 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; }; })(); var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NodeCatalogService = void 0; const backend_common_1 = require("@n8n/backend-common"); const constants_1 = require("@n8n/constants"); const di_1 = require("@n8n/di"); const fs = __importStar(require("fs/promises")); const lru_cache_1 = require("lru-cache"); const path = __importStar(require("path")); const load_nodes_and_credentials_1 = require("../load-nodes-and-credentials"); const synthesize_type_def_1 = require("../modules/mcp-registry/synthesize-type-def"); const isBuiltinNodeId = (nodeId) => constants_1.BUILTIN_NODES_PACKAGES.some((pkg) => nodeId.startsWith(`${pkg}.`)); const nodeVersionNumbers = (description) => { if (Array.isArray(description.version)) return description.version; if (typeof description.version === 'number') return [description.version]; return []; }; const maxNodeVersion = (description) => Math.max(0, ...nodeVersionNumbers(description)); const parseRequestedVersion = (version) => { const normalized = version.replace(/^v/i, ''); if (/^\d+$/.test(normalized) && normalized.length === 2) { return Number(`${normalized[0]}.${normalized[1]}`); } return Number.parseFloat(normalized); }; const versionLabel = (description) => { const version = maxNodeVersion(description); return version > 0 ? String(version) : undefined; }; const UNFILTERED = Symbol('unfiltered'); const MAX_TYPE_DEFINITION_CACHE_BYTES = 16 * 1024 * 1024; const stringBytes = (value) => (value ? Buffer.byteLength(value, 'utf8') : 0); const definitionResultBytes = (item) => Math.max(1, stringBytes(item.content) + stringBytes(item.version) + stringBytes(item.error) + stringBytes(item.builderHint)); let NodeCatalogService = class NodeCatalogService { constructor(loadNodesAndCredentials, logger) { this.loadNodesAndCredentials = loadNodesAndCredentials; this.logger = logger; this.nodeDefinitionDirs = []; this.descriptionsById = new Map(); this.searchStates = new Map(); this.getCache = new Map(); this.getDefinitionCache = new lru_cache_1.LRUCache({ maxSize: MAX_TYPE_DEFINITION_CACHE_BYTES, sizeCalculation: definitionResultBytes, }); this.suggestCache = new Map(); this.loadNodesAndCredentials.addPostProcessor(async () => await this.refreshNodeTypes()); } async initialize() { this.initPromise ??= this.doInitialize(); await this.initPromise; } getNodeTypeParser() { if (!this.nodeTypeParser) { throw new Error('NodeCatalogService not initialized. Call initialize() first.'); } return this.nodeTypeParser; } getNodeDefinitionDirs() { return this.nodeDefinitionDirs; } async searchNodes(queries, options = {}) { const { nodeFilter } = options; const stateKey = nodeFilter ?? UNFILTERED; let state = this.searchStates.get(stateKey); if (!state) { state = { cache: new Map() }; this.searchStates.set(stateKey, state); } const cacheKey = JSON.stringify([...queries].sort()); const cached = state.cache.get(cacheKey); if (cached) return cached; if (!state.search) { const { searchCodeBuilderNodes } = await import('@n8n/ai-utilities/node-catalog'); const nodeTypeParser = this.getNodeTypeParser(); state.search = (searchQueries) => nodeFilter ? searchCodeBuilderNodes(nodeTypeParser, searchQueries, { nodeFilter }) : searchCodeBuilderNodes(nodeTypeParser, searchQueries); } const result = state.search(queries); state.cache.set(cacheKey, result); return result; } async getNodeTypes(nodeIds) { const cacheKey = JSON.stringify(nodeIds.map((id) => (typeof id === 'string' ? id : JSON.stringify(id))).sort()); const cached = this.getCache.get(cacheKey); if (cached) return cached; const onDiskIds = []; const synthesizeIds = []; for (const id of nodeIds) { const nodeId = typeof id === 'string' ? id : id.nodeId; if (isBuiltinNodeId(nodeId)) { onDiskIds.push(id); } else { synthesizeIds.push(id); } } const parts = []; const errors = []; for (const id of synthesizeIds) { const result = await this.getNodeTypeDefinition(this.toDefinitionRequest(id)); if (result.error) { errors.push(result.error); } else { parts.push(result.content); } } if (onDiskIds.length > 0) { const { getNodeTypes } = await import('@n8n/ai-utilities/node-catalog'); parts.push(getNodeTypes(onDiskIds, { nodeDefinitionDirs: this.nodeDefinitionDirs })); } if (errors.length > 0) { parts.push(`# Errors\n\n${errors.join('\n')}`); } const result = parts.join('\n\n'); this.getCache.set(cacheKey, result); return result; } async getNodeTypeDefinition(request) { const cacheKey = JSON.stringify(request); const cached = this.getDefinitionCache.get(cacheKey); if (cached) return cached; const result = isBuiltinNodeId(request.nodeId) ? await this.getBuiltinNodeTypeDefinition(request) : this.getSynthesizedNodeTypeDefinition(request); if (!result.error) { this.getDefinitionCache.set(cacheKey, result); } return result; } async getSuggestedNodes(categories) { const cacheKey = JSON.stringify([...categories].sort()); const cached = this.suggestCache.get(cacheKey); if (cached) return cached; const { getSuggestedNodes } = await import('@n8n/ai-utilities/node-catalog'); const result = getSuggestedNodes(this.getNodeTypeParser(), categories); this.suggestCache.set(cacheKey, result); return result; } async doInitialize() { const { NodeTypeParser: NodeTypeParserClass } = await import('@n8n/ai-utilities/node-catalog'); const { setSchemaBaseDirs } = await import('@n8n/workflow-sdk'); await this.loadNodesAndCredentials.postProcessLoaders(); const { nodes: nodeTypeDescriptions } = await this.loadNodesAndCredentials.collectTypes(); this.nodeTypeParser = new NodeTypeParserClass(nodeTypeDescriptions); this.indexDescriptions(nodeTypeDescriptions); this.nodeDefinitionDirs = await this.resolveBuiltinNodeDefinitionDirs(); setSchemaBaseDirs(this.nodeDefinitionDirs); this.logger.debug('NodeCatalogService initialized', { nodeTypeCount: nodeTypeDescriptions.length, nodeDefinitionDirs: this.nodeDefinitionDirs.length, }); } async refreshNodeTypes() { if (!this.nodeTypeParser) return; const { NodeTypeParser: NodeTypeParserClass } = await import('@n8n/ai-utilities/node-catalog'); const { nodes: nodeTypeDescriptions } = await this.loadNodesAndCredentials.collectTypes(); this.nodeTypeParser = new NodeTypeParserClass(nodeTypeDescriptions); this.indexDescriptions(nodeTypeDescriptions); this.searchStates.clear(); this.getCache.clear(); this.getDefinitionCache.clear(); this.suggestCache.clear(); this.logger.debug('NodeCatalogService refreshed node types', { nodeTypeCount: nodeTypeDescriptions.length, }); } indexDescriptions(descriptions) { this.descriptionsById.clear(); for (const description of descriptions) { const existing = this.descriptionsById.get(description.name); if (existing) { existing.push(description); } else { this.descriptionsById.set(description.name, [description]); } } } toDefinitionRequest(nodeRequest) { if (typeof nodeRequest === 'string') return { nodeId: nodeRequest }; return { nodeId: nodeRequest.nodeId, ...(nodeRequest.version ? { version: nodeRequest.version } : {}), ...(nodeRequest.resource ? { resource: nodeRequest.resource } : {}), ...(nodeRequest.operation ? { operation: nodeRequest.operation } : {}), ...(nodeRequest.mode ? { mode: nodeRequest.mode } : {}), }; } async getBuiltinNodeTypeDefinition(request) { const { getNodeTypeDefinition } = await import('@n8n/ai-utilities/node-catalog'); const result = getNodeTypeDefinition(request.nodeId, request.version, this.nodeDefinitionDirs, { resource: request.resource, operation: request.operation, mode: request.mode, }); const candidates = this.descriptionsById.get(request.nodeId); const description = candidates ? this.selectDescription(candidates, result.version ?? request.version) : undefined; const builderHint = description?.builderHint?.searchHint; if (result.error) { return { content: '', error: result.error, ...(builderHint ? { builderHint } : {}), }; } return { content: result.content, ...(result.version ? { version: result.version } : {}), ...(builderHint ? { builderHint } : {}), }; } getSynthesizedNodeTypeDefinition(request) { const candidates = this.descriptionsById.get(request.nodeId); if (!candidates?.length) { return { content: '', error: `Node type '${request.nodeId}' not found. Use search_nodes to find the correct node ID.`, }; } const description = this.selectDescription(candidates, request.version); if (!description) { return { content: '', error: this.versionNotFoundError(request.nodeId, request.version, candidates), }; } try { const version = versionLabel(description); return { content: (0, synthesize_type_def_1.synthesizeNodeTypeDef)(description), ...(version ? { version } : {}), ...(description.builderHint?.searchHint ? { builderHint: description.builderHint.searchHint } : {}), }; } catch (error) { this.logger.debug('Could not synthesize node type definition', { nodeId: request.nodeId, error, }); return { content: '', error: `Type definition for '${request.nodeId}' could not be generated from the node's description.`, }; } } selectDescription(candidates, requestedVersion) { if (requestedVersion !== undefined) { const wanted = parseRequestedVersion(requestedVersion); return candidates.find((d) => nodeVersionNumbers(d).includes(wanted)); } return candidates.reduce((latest, d) => maxNodeVersion(d) > maxNodeVersion(latest) ? d : latest); } versionNotFoundError(nodeId, requestedVersion, candidates) { const available = [...new Set(candidates.flatMap(nodeVersionNumbers))].sort((a, b) => a - b); return `Version '${requestedVersion}' not found for node '${nodeId}'. Available versions: ${available.join(', ')}.`; } async resolveBuiltinNodeDefinitionDirs() { const dirs = []; for (const packageId of constants_1.BUILTIN_NODES_PACKAGES) { try { const packageJsonPath = require.resolve(`${packageId}/package.json`); const distDir = path.dirname(packageJsonPath); let nodeDefsDir = path.join(distDir, 'dist', 'node-definitions'); const separator = process.platform === 'win32' ? '\\' : '/'; if (!nodeDefsDir.endsWith(separator)) { nodeDefsDir += separator; } await fs.access(nodeDefsDir); dirs.push(nodeDefsDir); } catch (error) { this.logger.debug(`Could not resolve node definitions for ${packageId}`, { error }); } } return dirs; } }; exports.NodeCatalogService = NodeCatalogService; exports.NodeCatalogService = NodeCatalogService = __decorate([ (0, di_1.Service)(), __metadata("design:paramtypes", [load_nodes_and_credentials_1.LoadNodesAndCredentials, backend_common_1.Logger]) ], NodeCatalogService); //# sourceMappingURL=node-catalog.service.js.map