-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathworkflow-parser.js
More file actions
281 lines (242 loc) · 7.78 KB
/
workflow-parser.js
File metadata and controls
281 lines (242 loc) · 7.78 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Workflow Parser
*
* This script parses n8n workflows, extracts metadata, and generates descriptions.
* It will be used to vectorize workflows and store them in databases.
*/
const fs = require('fs');
const path = require('path');
const util = require('util');
const readdir = util.promisify(fs.readdir);
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
// Configuration
const WORKFLOWS_DIR = path.join(__dirname, 'workflows');
const OUTPUT_DIR = path.join(__dirname, 'processed-workflows');
// Ensure output directory exists
if (!fs.existsSync(OUTPUT_DIR)) {
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
}
/**
* Extract category from filename
* @param {string} filename - The workflow filename
* @returns {string} The category
*/
function extractCategory(filename) {
const parts = filename.split(':');
if (parts.length > 1) {
return parts[0];
}
return 'uncategorized';
}
/**
* Extract name from filename
* @param {string} filename - The workflow filename
* @returns {string} The name
*/
function extractName(filename) {
const parts = filename.split(':');
if (parts.length > 1) {
// Remove .json extension from the second part
return parts[1].replace('.json', '');
}
return filename.replace('.json', '');
}
/**
* Generate a description for a workflow based on its content
* @param {Object} workflow - The workflow object
* @returns {string} Generated description
*/
function generateDescription(workflow) {
let description = '';
// Add workflow name
if (workflow.name) {
description += `Workflow Name: ${workflow.name}\n\n`;
}
// Extract node types and count
const nodeTypes = {};
const nodeNames = new Set();
if (workflow.nodes && Array.isArray(workflow.nodes)) {
workflow.nodes.forEach(node => {
if (node.type) {
nodeTypes[node.type] = (nodeTypes[node.type] || 0) + 1;
}
if (node.name) {
nodeNames.add(node.name);
}
});
}
// Add node type summary
description += 'Node Types:\n';
Object.entries(nodeTypes).forEach(([type, count]) => {
description += `- ${type}: ${count}\n`;
});
// Add node names
description += '\nNode Names:\n';
Array.from(nodeNames).forEach(name => {
description += `- ${name}\n`;
});
// Add sticky notes content if available
const stickyNotes = workflow.nodes?.filter(node => node.type === 'n8n-nodes-base.stickyNote');
if (stickyNotes && stickyNotes.length > 0) {
description += '\nWorkflow Documentation:\n';
stickyNotes.forEach(note => {
if (note.parameters?.content) {
description += `${note.parameters.content}\n`;
}
});
}
return description;
}
/**
* Extract tags from a workflow
* @param {Object} workflow - The workflow object
* @returns {string[]} Array of tags
*/
function extractTags(workflow) {
const tags = new Set();
// Add existing tags if available
if (workflow.tags && Array.isArray(workflow.tags)) {
workflow.tags.forEach(tag => tags.add(tag));
}
// Extract node types as tags
if (workflow.nodes && Array.isArray(workflow.nodes)) {
workflow.nodes.forEach(node => {
if (node.type) {
// Extract the base node type without version or namespace
const baseType = node.type.split('.').pop();
if (baseType) tags.add(baseType);
// Add service-specific tags
if (node.type.includes('gmail')) tags.add('gmail');
if (node.type.includes('google')) tags.add('google');
if (node.type.includes('openai')) tags.add('openai');
if (node.type.includes('langchain')) tags.add('langchain');
if (node.type.includes('webhook')) tags.add('webhook');
if (node.type.includes('http')) tags.add('http');
if (node.type.includes('database')) tags.add('database');
if (node.type.includes('postgres')) tags.add('postgres');
if (node.type.includes('supabase')) tags.add('supabase');
}
});
}
return Array.from(tags);
}
/**
* Analyze workflow complexity
* @param {Object} workflow - The workflow object
* @returns {Object} Complexity metrics
*/
function analyzeComplexity(workflow) {
const metrics = {
nodeCount: 0,
connectionCount: 0,
uniqueNodeTypes: 0,
complexity: 'simple'
};
if (workflow.nodes && Array.isArray(workflow.nodes)) {
metrics.nodeCount = workflow.nodes.length;
// Count unique node types
const nodeTypes = new Set();
workflow.nodes.forEach(node => {
if (node.type) nodeTypes.add(node.type);
});
metrics.uniqueNodeTypes = nodeTypes.size;
}
// Count connections
if (workflow.connections) {
let connectionCount = 0;
Object.values(workflow.connections).forEach(conn => {
if (Array.isArray(conn.main)) {
conn.main.forEach(mainConn => {
if (Array.isArray(mainConn)) {
connectionCount += mainConn.length;
}
});
}
});
metrics.connectionCount = connectionCount;
}
// Determine complexity
if (metrics.nodeCount > 15 || metrics.connectionCount > 20) {
metrics.complexity = 'complex';
} else if (metrics.nodeCount > 7 || metrics.connectionCount > 10) {
metrics.complexity = 'moderate';
}
return metrics;
}
/**
* Process a single workflow file
* @param {string} filePath - Path to the workflow file
* @returns {Object} Processed workflow data
*/
async function processWorkflow(filePath) {
try {
const filename = path.basename(filePath);
const fileContent = await readFile(filePath, 'utf8');
const workflow = JSON.parse(fileContent);
const category = extractCategory(filename);
const name = extractName(filename);
const description = generateDescription(workflow);
const tags = extractTags(workflow);
const complexity = analyzeComplexity(workflow);
// Create enriched workflow object
const enrichedWorkflow = {
id: workflow.id || `generated-${Date.now()}`,
originalFilename: filename,
category,
name,
description,
tags,
complexity,
originalWorkflow: workflow
};
return enrichedWorkflow;
} catch (error) {
console.error(`Error processing workflow ${filePath}:`, error);
return null;
}
}
/**
* Main function to process all workflows
*/
async function processAllWorkflows() {
try {
// Get all workflow files
const files = await readdir(WORKFLOWS_DIR);
const workflowFiles = files.filter(file => file.endsWith('.json'));
console.log(`Found ${workflowFiles.length} workflow files to process`);
// Process each workflow
const processedWorkflows = [];
for (const file of workflowFiles) {
const filePath = path.join(WORKFLOWS_DIR, file);
const processedWorkflow = await processWorkflow(filePath);
if (processedWorkflow) {
processedWorkflows.push(processedWorkflow);
// Save individual processed workflow
const outputPath = path.join(OUTPUT_DIR, `${processedWorkflow.id}.json`);
await writeFile(outputPath, JSON.stringify(processedWorkflow, null, 2));
}
}
// Save summary of all workflows
const summaryPath = path.join(OUTPUT_DIR, 'workflows-summary.json');
await writeFile(summaryPath, JSON.stringify(processedWorkflows, null, 2));
console.log(`Successfully processed ${processedWorkflows.length} workflows`);
console.log(`Results saved to ${OUTPUT_DIR}`);
return processedWorkflows;
} catch (error) {
console.error('Error processing workflows:', error);
return [];
}
}
// Export functions for use in other scripts
module.exports = {
processAllWorkflows,
processWorkflow,
generateDescription,
extractTags,
analyzeComplexity
};
// Run the script if executed directly
if (require.main === module) {
processAllWorkflows().catch(console.error);
}