/** * Scripts: Build Knowledge Base * One-time or periodic script to extract n8n node schemas from the n8n repo * Run: node scripts/build-knowledge-base.js * This runs externally, NOT inside Cloudflare Worker */ const fs = require('fs'); const path = require('path'); console.log('[KnowledgeBase] Starting n8n node schema extraction...'); console.log('[KnowledgeBase] This script should be run against a local clone of https://github.com/n8n-io/n8n'); console.log(''); console.log('Usage:'); console.log(' 1. Clone n8n: git clone https://github.com/n8n-io/n8n.git /tmp/n8n'); console.log(' 2. Run: N8N_REPO=/tmp/n8n node scripts/build-knowledge-base.js'); console.log(''); const N8N_REPO = process.env.N8N_REPO; if (!N8N_REPO) { console.warn('[KnowledgeBase] N8N_REPO env var not set. Using embedded registry from knowledge/node-definitions/'); console.warn('[KnowledgeBase] For full schema extraction, set N8N_REPO=/path/to/n8n'); process.exit(0); } const nodesBasePath = path.join(N8N_REPO, 'packages/nodes-base/nodes'); if (!fs.existsSync(nodesBasePath)) { console.error(`[KnowledgeBase] n8n nodes-base not found at: ${nodesBasePath}`); process.exit(1); } const nodeRegistry = {}; let count = 0; function extractNodeDefs(dir) { const entries = fs.readdirSync(dir, { withFileTypes: true }); for (const entry of entries) { const fullPath = path.join(dir, entry.name); if (entry.isDirectory()) { extractNodeDefs(fullPath); } else if (entry.name.endsWith('.node.ts') || entry.name.endsWith('.node.js')) { try { // Read the file to extract description metadata const content = fs.readFileSync(fullPath, 'utf-8'); // Extract node type name from file const typeMatch = content.match(/name:\s*['"]([^'"]+)['"]/); const versionMatch = content.match(/version:\s*(\d+)/); const descMatch = content.match(/description:\s*['"]([^'"]+)['"]/); if (typeMatch) { const nodeType = typeMatch[1]; nodeRegistry[nodeType] = { type: nodeType, version: versionMatch ? parseInt(versionMatch[1]) : 1, description: descMatch ? descMatch[1] : '', filePath: fullPath.replace(N8N_REPO, ''), }; count++; } } catch { // skip files that can't be parsed } } } } extractNodeDefs(nodesBasePath); const outputPath = path.join(__dirname, '../knowledge/node-definitions/extracted-registry.json'); fs.writeFileSync(outputPath, JSON.stringify(nodeRegistry, null, 2)); console.log(`[KnowledgeBase] Extracted ${count} node definitions`); console.log(`[KnowledgeBase] Output written to: ${outputPath}`);