@fractal-solutions/qflow 1.0.29 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +504 -0
- package/README.md +170 -30
- package/dist/agent/tools.d.ts +3114 -181
- package/dist/agent/tools.js +492 -0
- package/dist/nodes/browser_control.d.ts +4 -0
- package/dist/nodes/browser_control.js +73 -0
- package/dist/nodes/data_validation.d.ts +9 -0
- package/dist/nodes/data_validation.js +48 -0
- package/dist/nodes/display_image.d.ts +4 -0
- package/dist/nodes/display_image.js +46 -0
- package/dist/nodes/gis.d.ts +37 -0
- package/dist/nodes/gis.js +106 -0
- package/dist/nodes/git.d.ts +4 -0
- package/dist/nodes/git.js +55 -0
- package/dist/nodes/hardware_interaction.d.ts +8 -0
- package/dist/nodes/hardware_interaction.js +97 -0
- package/dist/nodes/image_gallery.d.ts +8 -0
- package/dist/nodes/image_gallery.js +99 -0
- package/dist/nodes/index.d.ts +12 -0
- package/dist/nodes/index.js +12 -0
- package/dist/nodes/multimedia_processing.d.ts +10 -0
- package/dist/nodes/multimedia_processing.js +79 -0
- package/dist/nodes/pdf_processor.d.ts +4 -0
- package/dist/nodes/pdf_processor.js +75 -0
- package/dist/nodes/remote_execution.d.ts +4 -0
- package/dist/nodes/remote_execution.js +82 -0
- package/dist/nodes/scheduler.d.ts +20 -0
- package/dist/nodes/scheduler.js +97 -0
- package/dist/nodes/speech_synthesis.d.ts +45 -0
- package/dist/nodes/speech_synthesis.js +142 -0
- package/dist/nodes/spreadsheet.d.ts +10 -0
- package/dist/nodes/spreadsheet.js +241 -0
- package/dist/nodes/webhook.d.ts +15 -0
- package/dist/nodes/webhook.js +110 -0
- package/dist/nodes/websockets.d.ts +4 -0
- package/dist/nodes/websockets.js +73 -0
- package/package.json +11 -2
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import { promises as fs } from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import * as XLSX from 'xlsx'; // npm install xlsx
|
|
5
|
+
export class SpreadsheetNode extends AsyncNode {
|
|
6
|
+
constructor(maxRetries = 1, wait = 0) {
|
|
7
|
+
super(maxRetries, wait);
|
|
8
|
+
}
|
|
9
|
+
async execAsync() {
|
|
10
|
+
const { filePath, action, // 'read', 'write', 'list_sheets', 'read_range', 'write_range', 'append_rows', 'delete_rows', 'insert_rows', 'add_sheet', 'delete_sheet', 'rename_sheet', 'format_cells'
|
|
11
|
+
sheetName, // Required for .xlsx/.xls read/write/management
|
|
12
|
+
data, // Required for 'write', 'write_range', 'append_rows'
|
|
13
|
+
headerRow = true, // For read/write
|
|
14
|
+
range, // A1 notation for 'read_range', 'write_range', 'format_cells'
|
|
15
|
+
startRow, // For 'delete_rows', 'insert_rows'
|
|
16
|
+
numRows, // For 'delete_rows', 'insert_rows'
|
|
17
|
+
startCol, // For 'delete_cols', 'insert_cols' (conceptual, not implementing now)
|
|
18
|
+
numCols, // For 'delete_cols', 'insert_cols' (conceptual, not implementing now)
|
|
19
|
+
newSheetName, // For 'rename_sheet'
|
|
20
|
+
formats // For 'format_cells'
|
|
21
|
+
} = this.params;
|
|
22
|
+
if (!filePath) {
|
|
23
|
+
throw new Error('SpreadsheetNode requires a `filePath` parameter.');
|
|
24
|
+
}
|
|
25
|
+
if (!action || !['read', 'write', 'list_sheets', 'read_range', 'write_range', 'append_rows', 'delete_rows', 'insert_rows', 'add_sheet', 'delete_sheet', 'rename_sheet', 'format_cells'].includes(action)) {
|
|
26
|
+
throw new Error('SpreadsheetNode requires a valid `action`.');
|
|
27
|
+
}
|
|
28
|
+
const fileExtension = path.extname(filePath).toLowerCase();
|
|
29
|
+
let workbook;
|
|
30
|
+
let result;
|
|
31
|
+
// Helper to read workbook (for read/list_sheets/modify)
|
|
32
|
+
const readWorkbook = async () => {
|
|
33
|
+
try {
|
|
34
|
+
const fileBuffer = await fs.readFile(filePath);
|
|
35
|
+
return XLSX.read(fileBuffer, { type: 'buffer' });
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
throw new Error(`Failed to read spreadsheet file: ${e.message}. Ensure file exists and is accessible.`);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
// Helper to write workbook (for modify actions)
|
|
42
|
+
const writeWorkbook = async (wb) => {
|
|
43
|
+
try {
|
|
44
|
+
XLSX.writeFile(wb, filePath);
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
throw new Error(`Failed to write spreadsheet file: ${e.message}.`);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// Helper to get worksheet
|
|
51
|
+
const getWorksheet = (wb, sName) => {
|
|
52
|
+
if (!sName)
|
|
53
|
+
throw new Error('`sheetName` is required for this action.');
|
|
54
|
+
const ws = wb.Sheets[sName];
|
|
55
|
+
if (!ws)
|
|
56
|
+
throw new Error(`Sheet '${sName}' not found in workbook.`);
|
|
57
|
+
return ws;
|
|
58
|
+
};
|
|
59
|
+
switch (action) {
|
|
60
|
+
case 'list_sheets':
|
|
61
|
+
if (!['.xlsx', '.xls'].includes(fileExtension)) {
|
|
62
|
+
throw new Error(`List sheets action only supported for .xlsx or .xls files. Got: ${fileExtension}`);
|
|
63
|
+
}
|
|
64
|
+
workbook = await readWorkbook();
|
|
65
|
+
result = { sheets: workbook.SheetNames };
|
|
66
|
+
break;
|
|
67
|
+
case 'read':
|
|
68
|
+
if (!['.xlsx', '.xls', '.csv'].includes(fileExtension)) {
|
|
69
|
+
throw new Error(`Unsupported file format for reading: ${fileExtension}. Supported: .xlsx, .xls, .csv`);
|
|
70
|
+
}
|
|
71
|
+
workbook = await readWorkbook();
|
|
72
|
+
let worksheetRead;
|
|
73
|
+
if (fileExtension === '.csv') {
|
|
74
|
+
worksheetRead = workbook.Sheets[workbook.SheetNames[0]];
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
worksheetRead = getWorksheet(workbook, sheetName);
|
|
78
|
+
}
|
|
79
|
+
result = XLSX.utils.sheet_to_json(worksheetRead, { header: headerRow ? 1 : 'A' });
|
|
80
|
+
break;
|
|
81
|
+
case 'read_range':
|
|
82
|
+
if (!range)
|
|
83
|
+
throw new Error('`range` is required for read_range.');
|
|
84
|
+
if (!['.xlsx', '.xls', '.csv'].includes(fileExtension)) {
|
|
85
|
+
throw new Error(`Unsupported file format for reading range: ${fileExtension}. Supported: .xlsx, .xls, .csv`);
|
|
86
|
+
}
|
|
87
|
+
workbook = await readWorkbook();
|
|
88
|
+
let worksheetReadRange;
|
|
89
|
+
if (fileExtension === '.csv') {
|
|
90
|
+
worksheetReadRange = workbook.Sheets[workbook.SheetNames[0]];
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
worksheetReadRange = getWorksheet(workbook, sheetName);
|
|
94
|
+
}
|
|
95
|
+
result = XLSX.utils.sheet_to_json(worksheetReadRange, { header: headerRow ? 1 : 'A', range: range });
|
|
96
|
+
break;
|
|
97
|
+
case 'write':
|
|
98
|
+
if (!data || !Array.isArray(data) || data.length === 0)
|
|
99
|
+
throw new Error('`data` is required for write.');
|
|
100
|
+
if (!['.xlsx', '.csv'].includes(fileExtension)) {
|
|
101
|
+
throw new Error(`Unsupported file format for writing: ${fileExtension}. Supported: .xlsx, .csv`);
|
|
102
|
+
}
|
|
103
|
+
workbook = XLSX.utils.book_new();
|
|
104
|
+
const worksheetToWrite = XLSX.utils.json_to_sheet(data, { skipHeader: !headerRow });
|
|
105
|
+
if (fileExtension === '.csv') {
|
|
106
|
+
const csvContent = XLSX.utils.sheet_to_csv(worksheetToWrite);
|
|
107
|
+
await fs.writeFile(filePath, csvContent);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
if (!sheetName)
|
|
111
|
+
throw new Error('`sheetName` is required for .xlsx write.');
|
|
112
|
+
XLSX.utils.book_append_sheet(workbook, worksheetToWrite, sheetName);
|
|
113
|
+
await writeWorkbook(workbook);
|
|
114
|
+
}
|
|
115
|
+
result = { message: `Successfully wrote data to ${filePath}` };
|
|
116
|
+
break;
|
|
117
|
+
case 'write_range':
|
|
118
|
+
if (!range || !data || !Array.isArray(data))
|
|
119
|
+
throw new Error('`range` and `data` are required for write_range.');
|
|
120
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
121
|
+
throw new Error(`Unsupported file format for writing range: ${fileExtension}. Supported: .xlsx`);
|
|
122
|
+
workbook = await readWorkbook();
|
|
123
|
+
let worksheetWriteRange = getWorksheet(workbook, sheetName);
|
|
124
|
+
XLSX.utils.sheet_add_json(worksheetWriteRange, data, { origin: range, skipHeader: !headerRow });
|
|
125
|
+
await writeWorkbook(workbook);
|
|
126
|
+
result = { message: `Successfully wrote data to range ${range} in ${filePath}` };
|
|
127
|
+
break;
|
|
128
|
+
case 'append_rows':
|
|
129
|
+
if (!data || !Array.isArray(data))
|
|
130
|
+
throw new Error('`data` is required for append_rows.');
|
|
131
|
+
if (!['.xlsx', '.csv'].includes(fileExtension))
|
|
132
|
+
throw new Error(`Unsupported file format for appending: ${fileExtension}. Supported: .xlsx, .csv`);
|
|
133
|
+
workbook = await readWorkbook();
|
|
134
|
+
let worksheetAppend;
|
|
135
|
+
if (fileExtension === '.csv') {
|
|
136
|
+
worksheetAppend = workbook.Sheets[workbook.SheetNames[0]];
|
|
137
|
+
const csvContent = XLSX.utils.sheet_to_csv(worksheetAppend) + '\n' + XLSX.utils.sheet_to_csv(XLSX.utils.json_to_sheet(data, { skipHeader: true }));
|
|
138
|
+
await fs.writeFile(filePath, csvContent);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
worksheetAppend = getWorksheet(workbook, sheetName);
|
|
142
|
+
XLSX.utils.sheet_add_json(worksheetAppend, data, { skipHeader: true, origin: -1 }); // -1 appends to end
|
|
143
|
+
await writeWorkbook(workbook);
|
|
144
|
+
}
|
|
145
|
+
result = { message: `Successfully appended rows to ${filePath}` };
|
|
146
|
+
break;
|
|
147
|
+
case 'delete_rows':
|
|
148
|
+
if (startRow === undefined || numRows === undefined)
|
|
149
|
+
throw new Error('`startRow` and `numRows` are required for delete_rows.');
|
|
150
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
151
|
+
throw new Error(`Unsupported file format for deleting rows: ${fileExtension}. Supported: .xlsx`);
|
|
152
|
+
const deleteRowsHelper = (ws, start, num) => {
|
|
153
|
+
const range = XLSX.utils.decode_range(ws['!ref']);
|
|
154
|
+
for (let R = start; R < range.e.r; ++R) {
|
|
155
|
+
for (let C = range.s.c; C <= range.e.c; ++C) {
|
|
156
|
+
ws[XLSX.utils.encode_cell({ r: R, c: C })] = ws[XLSX.utils.encode_cell({ r: R + num, c: C })];
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
range.e.r -= num;
|
|
160
|
+
ws['!ref'] = XLSX.utils.encode_range(range.s, range.e);
|
|
161
|
+
};
|
|
162
|
+
workbook = await readWorkbook();
|
|
163
|
+
let worksheetDeleteRows = getWorksheet(workbook, sheetName);
|
|
164
|
+
deleteRowsHelper(worksheetDeleteRows, startRow - 1, numRows);
|
|
165
|
+
await writeWorkbook(workbook);
|
|
166
|
+
result = { message: `Successfully deleted ${numRows} rows starting from ${startRow} in ${filePath}` };
|
|
167
|
+
break;
|
|
168
|
+
case 'insert_rows':
|
|
169
|
+
if (startRow === undefined || numRows === undefined)
|
|
170
|
+
throw new Error('`startRow` and `numRows` are required for insert_rows.');
|
|
171
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
172
|
+
throw new Error(`Unsupported file format for inserting rows: ${fileExtension}. Supported: .xlsx`);
|
|
173
|
+
workbook = await readWorkbook();
|
|
174
|
+
let worksheetInsertRows = getWorksheet(workbook, sheetName);
|
|
175
|
+
XLSX.utils.sheet_add_aoa(worksheetInsertRows, Array(numRows).fill([]), { origin: startRow - 1 }); // Insert empty rows
|
|
176
|
+
await writeWorkbook(workbook);
|
|
177
|
+
result = { message: `Successfully inserted ${numRows} rows starting from ${startRow} in ${filePath}` };
|
|
178
|
+
break;
|
|
179
|
+
case 'add_sheet':
|
|
180
|
+
if (!newSheetName)
|
|
181
|
+
throw new Error('`newSheetName` is required for add_sheet.');
|
|
182
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
183
|
+
throw new Error(`Unsupported file format for adding sheet: ${fileExtension}. Supported: .xlsx`);
|
|
184
|
+
workbook = await readWorkbook();
|
|
185
|
+
if (workbook.SheetNames.includes(newSheetName))
|
|
186
|
+
throw new Error(`Sheet '${newSheetName}' already exists.`);
|
|
187
|
+
XLSX.utils.book_append_sheet(workbook, XLSX.utils.aoa_to_sheet([[]]), newSheetName); // Add empty sheet
|
|
188
|
+
await writeWorkbook(workbook);
|
|
189
|
+
result = { message: `Successfully added sheet '${newSheetName}' to ${filePath}` };
|
|
190
|
+
break;
|
|
191
|
+
case 'delete_sheet':
|
|
192
|
+
if (!sheetName)
|
|
193
|
+
throw new Error('`sheetName` is required for delete_sheet.');
|
|
194
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
195
|
+
throw new Error(`Unsupported file format for deleting sheet: ${fileExtension}. Supported: .xlsx`);
|
|
196
|
+
workbook = await readWorkbook();
|
|
197
|
+
if (!workbook.SheetNames.includes(sheetName))
|
|
198
|
+
throw new Error(`Sheet '${sheetName}' not found.`);
|
|
199
|
+
delete workbook.Sheets[sheetName];
|
|
200
|
+
workbook.SheetNames = workbook.SheetNames.filter(s => s !== sheetName);
|
|
201
|
+
await writeWorkbook(workbook);
|
|
202
|
+
result = { message: `Successfully deleted sheet '${sheetName}' from ${filePath}` };
|
|
203
|
+
break;
|
|
204
|
+
case 'rename_sheet':
|
|
205
|
+
if (!sheetName || !newSheetName)
|
|
206
|
+
throw new Error('`sheetName` and `newSheetName` are required for rename_sheet.');
|
|
207
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
208
|
+
throw new Error(`Unsupported file format for renaming sheet: ${fileExtension}. Supported: .xlsx`);
|
|
209
|
+
workbook = await readWorkbook();
|
|
210
|
+
if (!workbook.SheetNames.includes(sheetName))
|
|
211
|
+
throw new Error(`Sheet '${sheetName}' not found.`);
|
|
212
|
+
if (workbook.SheetNames.includes(newSheetName))
|
|
213
|
+
throw new Error(`Sheet '${newSheetName}' already exists.`);
|
|
214
|
+
workbook.SheetNames[workbook.SheetNames.indexOf(sheetName)] = newSheetName;
|
|
215
|
+
workbook.Sheets[newSheetName] = workbook.Sheets[sheetName];
|
|
216
|
+
delete workbook.Sheets[sheetName];
|
|
217
|
+
await writeWorkbook(workbook);
|
|
218
|
+
result = { message: `Successfully renamed sheet '${sheetName}' to '${newSheetName}' in ${filePath}` };
|
|
219
|
+
break;
|
|
220
|
+
case 'format_cells':
|
|
221
|
+
if (!range || !formats)
|
|
222
|
+
throw new Error('`range` and `formats` are required for format_cells.');
|
|
223
|
+
if (!['.xlsx'].includes(fileExtension))
|
|
224
|
+
throw new Error(`Unsupported file format for formatting cells: ${fileExtension}. Supported: .xlsx`);
|
|
225
|
+
workbook = await readWorkbook();
|
|
226
|
+
let worksheetFormat = getWorksheet(workbook, sheetName);
|
|
227
|
+
// This is a simplified example. Real formatting is complex with XLSX.
|
|
228
|
+
// XLSX.utils.format_cell is not for writing styles, but for formatting values.
|
|
229
|
+
// To apply styles, you typically need to manipulate the cell object directly
|
|
230
|
+
// or use a library that builds on top of XLSX for styling (e.g., exceljs, but that's another dependency).
|
|
231
|
+
// For now, we'll just acknowledge the request and return a message.
|
|
232
|
+
// A real implementation would involve iterating cells in the range and applying styles.
|
|
233
|
+
result = { message: `Formatting cells in range ${range} with provided formats (conceptual). Actual styling not fully implemented with basic XLSX.js.` };
|
|
234
|
+
await writeWorkbook(workbook); // Still write to save any other changes
|
|
235
|
+
break;
|
|
236
|
+
default:
|
|
237
|
+
throw new Error(`Unsupported action: ${action}`);
|
|
238
|
+
}
|
|
239
|
+
return result;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export class WebHookNode extends AsyncNode {
|
|
2
|
+
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse> | null;
|
|
3
|
+
flowToTrigger: AsyncFlow | null;
|
|
4
|
+
webhookPath: any;
|
|
5
|
+
sharedSecret: any;
|
|
6
|
+
execAsync(): Promise<{
|
|
7
|
+
status: string;
|
|
8
|
+
port: any;
|
|
9
|
+
path: any;
|
|
10
|
+
}>;
|
|
11
|
+
postAsync(shared: any, prepRes: any, execRes: any): Promise<any>;
|
|
12
|
+
}
|
|
13
|
+
import { AsyncNode } from '../qflow.js';
|
|
14
|
+
import http from 'http';
|
|
15
|
+
import { AsyncFlow } from '../qflow.js';
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AsyncNode, AsyncFlow } from '../qflow.js';
|
|
2
|
+
import http from 'http';
|
|
3
|
+
import { URL } from 'url'; // Node.js built-in
|
|
4
|
+
import crypto from 'crypto'; // Node.js built-in
|
|
5
|
+
export class WebHookNode extends AsyncNode {
|
|
6
|
+
constructor(maxRetries = 1, wait = 0) {
|
|
7
|
+
super(maxRetries, wait);
|
|
8
|
+
this.server = null;
|
|
9
|
+
this.flowToTrigger = null;
|
|
10
|
+
this.webhookPath = null;
|
|
11
|
+
this.sharedSecret = null;
|
|
12
|
+
}
|
|
13
|
+
async execAsync() {
|
|
14
|
+
const { port = 3000, // Port to listen on
|
|
15
|
+
path = '/webhook', // URL path for the webhook
|
|
16
|
+
flow, // The qflow AsyncFlow instance to trigger
|
|
17
|
+
sharedSecret, // Optional: for HMAC verification
|
|
18
|
+
responseStatus = 200, // HTTP status to send back
|
|
19
|
+
responseBody = { status: 'received' } // HTTP body to send back
|
|
20
|
+
} = this.params;
|
|
21
|
+
if (!flow || !(flow instanceof AsyncFlow)) {
|
|
22
|
+
throw new Error('WebHookNode requires a `flow` parameter of type AsyncFlow to trigger.');
|
|
23
|
+
}
|
|
24
|
+
this.flowToTrigger = flow;
|
|
25
|
+
this.webhookPath = path;
|
|
26
|
+
this.sharedSecret = sharedSecret;
|
|
27
|
+
// Ensure server is not already running
|
|
28
|
+
if (this.server && this.server.listening) {
|
|
29
|
+
console.warn(`[WebHookNode] Server already listening on port ${port}. Reusing existing server.`);
|
|
30
|
+
return { status: 'server_already_running', port, path };
|
|
31
|
+
}
|
|
32
|
+
this.server = http.createServer(async (req, res) => {
|
|
33
|
+
const requestUrl = new URL(req.url, `http://${req.headers.host}`);
|
|
34
|
+
if (req.method === 'POST' && requestUrl.pathname === this.webhookPath) {
|
|
35
|
+
let body = '';
|
|
36
|
+
req.on('data', chunk => {
|
|
37
|
+
body += chunk.toString();
|
|
38
|
+
});
|
|
39
|
+
req.on('end', async () => {
|
|
40
|
+
let payload;
|
|
41
|
+
try {
|
|
42
|
+
payload = JSON.parse(body);
|
|
43
|
+
}
|
|
44
|
+
catch (e) {
|
|
45
|
+
console.error('[WebHookNode] Failed to parse webhook body as JSON:', e);
|
|
46
|
+
res.writeHead(400, { 'Content-Type': 'application/json' });
|
|
47
|
+
res.end(JSON.stringify({ error: 'Invalid JSON payload' }));
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
// --- Security: HMAC Verification (if sharedSecret is provided) ---
|
|
51
|
+
if (this.sharedSecret) {
|
|
52
|
+
const signature = req.headers['x-hub-signature-256'] || req.headers['x-webhook-signature']; // Common headers
|
|
53
|
+
if (!signature) {
|
|
54
|
+
console.warn('[WebHookNode] Webhook received without signature, but sharedSecret is configured.');
|
|
55
|
+
res.writeHead(401, { 'Content-Type': 'application/json' });
|
|
56
|
+
res.end(JSON.stringify({ error: 'Missing signature' }));
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
const hmac = crypto.createHmac('sha256', this.sharedSecret);
|
|
60
|
+
hmac.update(body);
|
|
61
|
+
const digest = `sha256=${hmac.digest('hex')}`; // Format might vary (e.g., 'v1=...')
|
|
62
|
+
if (digest !== signature) { // Simple comparison, real-world might need more robust parsing
|
|
63
|
+
console.warn('[WebHookNode] Webhook signature mismatch. Request rejected.');
|
|
64
|
+
res.writeHead(403, { 'Content-Type': 'application/json' });
|
|
65
|
+
res.end(JSON.stringify({ error: 'Invalid signature' }));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
console.log('[WebHookNode] Webhook signature verified.');
|
|
69
|
+
}
|
|
70
|
+
console.log(`[WebHookNode] Webhook received on ${this.webhookPath}. Triggering flow...`);
|
|
71
|
+
// Trigger the flow in the background
|
|
72
|
+
// Create a new shared object for the triggered flow, including the webhook payload
|
|
73
|
+
const triggeredFlowShared = { webhook: { headers: req.headers, payload: payload, method: req.method, path: requestUrl.pathname } };
|
|
74
|
+
this.flowToTrigger.runAsync(triggeredFlowShared)
|
|
75
|
+
.then(flowResult => {
|
|
76
|
+
console.log('[WebHookNode] Triggered flow completed successfully:', flowResult);
|
|
77
|
+
})
|
|
78
|
+
.catch(flowError => {
|
|
79
|
+
console.error('[WebHookNode] Triggered flow failed:', flowError);
|
|
80
|
+
});
|
|
81
|
+
// Send response back to webhook sender
|
|
82
|
+
res.writeHead(responseStatus, { 'Content-Type': 'application/json' });
|
|
83
|
+
res.end(JSON.stringify(responseBody));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
res.writeHead(404, { 'Content-Type': 'text/plain' });
|
|
88
|
+
res.end('Not Found');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
await new Promise(resolve => {
|
|
92
|
+
this.server.listen(port, () => {
|
|
93
|
+
console.log(`[WebHookNode] Listening for webhooks on http://localhost:${port}${this.webhookPath}`);
|
|
94
|
+
resolve();
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
// This node returns immediately after starting the server.
|
|
98
|
+
// The flow continues, and the server runs in the background.
|
|
99
|
+
return { status: 'listening', port, path: this.webhookPath };
|
|
100
|
+
}
|
|
101
|
+
// Cleanup: Ensure the server is closed when the node's flow completes or is cancelled
|
|
102
|
+
async postAsync(shared, prepRes, execRes) {
|
|
103
|
+
// For a long-running webhook listener, the server should NOT be closed here.
|
|
104
|
+
// The server is intended to run in the background.
|
|
105
|
+
// Cleanup (server.close()) should be handled by the application's shutdown logic
|
|
106
|
+
// or if the WebHookNode is explicitly stopped by another part of the flow.
|
|
107
|
+
// We will remove the automatic server closing here.
|
|
108
|
+
return execRes;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { AsyncNode } from '../qflow.js';
|
|
2
|
+
import WebSocket from 'ws';
|
|
3
|
+
export class WebSocketsNode extends AsyncNode {
|
|
4
|
+
async execAsync(prepRes, shared) {
|
|
5
|
+
const { action, url, message, timeout = 5000, // 5-second timeout
|
|
6
|
+
} = this.params;
|
|
7
|
+
const connection = shared.webSocketConnection;
|
|
8
|
+
return new Promise(async (resolve, reject) => {
|
|
9
|
+
const timer = setTimeout(() => reject(new Error(`WebSocket action '${action}' timed out after ${timeout}ms`)), timeout);
|
|
10
|
+
try {
|
|
11
|
+
switch (action) {
|
|
12
|
+
case 'connect':
|
|
13
|
+
if (!url)
|
|
14
|
+
throw new Error('`url` parameter is required for `connect` action.');
|
|
15
|
+
const ws = new WebSocket(url);
|
|
16
|
+
ws.on('open', () => {
|
|
17
|
+
clearTimeout(timer);
|
|
18
|
+
shared.webSocketConnection = ws;
|
|
19
|
+
resolve({ message: `WebSocket connected to ${url}` });
|
|
20
|
+
});
|
|
21
|
+
ws.on('error', (err) => {
|
|
22
|
+
clearTimeout(timer);
|
|
23
|
+
reject(err);
|
|
24
|
+
});
|
|
25
|
+
break;
|
|
26
|
+
case 'send':
|
|
27
|
+
if (!connection)
|
|
28
|
+
throw new Error('No active WebSocket connection found.');
|
|
29
|
+
if (message === undefined)
|
|
30
|
+
throw new Error('`message` parameter is required for `send` action.');
|
|
31
|
+
const dataToSend = typeof message === 'object' ? JSON.stringify(message) : message;
|
|
32
|
+
connection.send(dataToSend);
|
|
33
|
+
clearTimeout(timer);
|
|
34
|
+
resolve({ message: 'Message sent' });
|
|
35
|
+
break;
|
|
36
|
+
case 'receive':
|
|
37
|
+
if (!connection)
|
|
38
|
+
throw new Error('No active WebSocket connection found.');
|
|
39
|
+
connection.once('message', (data) => {
|
|
40
|
+
clearTimeout(timer);
|
|
41
|
+
try {
|
|
42
|
+
// Try to parse as JSON, otherwise return as string
|
|
43
|
+
resolve(JSON.parse(data.toString()));
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
resolve(data.toString());
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
connection.once('error', (err) => {
|
|
50
|
+
clearTimeout(timer);
|
|
51
|
+
reject(err);
|
|
52
|
+
});
|
|
53
|
+
break;
|
|
54
|
+
case 'close':
|
|
55
|
+
if (connection) {
|
|
56
|
+
connection.close();
|
|
57
|
+
delete shared.webSocketConnection;
|
|
58
|
+
}
|
|
59
|
+
clearTimeout(timer);
|
|
60
|
+
resolve({ message: 'WebSocket connection closed' });
|
|
61
|
+
break;
|
|
62
|
+
default:
|
|
63
|
+
clearTimeout(timer);
|
|
64
|
+
throw new Error(`Unsupported WebSocket action: ${action}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
reject(error);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fractal-solutions/qflow",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.30",
|
|
4
4
|
"description": "A lightweight and flexible JavaScript library for creating and managing workflows and agents.",
|
|
5
5
|
"main": "dist/qflow.js",
|
|
6
6
|
"types": "dist/qflow.d.ts",
|
|
@@ -42,8 +42,17 @@
|
|
|
42
42
|
},
|
|
43
43
|
"homepage": "https://github.com/fractal-solutions/qflow#readme",
|
|
44
44
|
"dependencies": {
|
|
45
|
+
"ajv": "^8.17.1",
|
|
46
|
+
"ajv-formats": "^3.0.1",
|
|
45
47
|
"chalk": "^5.5.0",
|
|
46
|
-
"cheerio": "^1.0.0-rc.12"
|
|
48
|
+
"cheerio": "^1.0.0-rc.12",
|
|
49
|
+
"node-cron": "^4.2.1",
|
|
50
|
+
"playwright": "^1.45.0",
|
|
51
|
+
"serialport": "^13.0.0",
|
|
52
|
+
"simple-git": "^3.25.0",
|
|
53
|
+
"ssh2": "^1.16.0",
|
|
54
|
+
"ws": "^8.18.0",
|
|
55
|
+
"xlsx": "^0.18.5"
|
|
47
56
|
},
|
|
48
57
|
"devDependencies": {
|
|
49
58
|
"@types/bun": "latest"
|