File size: 2,698 Bytes
dd480ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
 * 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}`);