@fnet/cli 0.2.9 → 0.2.11
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/dist/fservice/index.js +1 -1
- package/package.json +1 -1
- package/template/fnet/node/src/cli/index.js.njk +5 -315
- package/template/fnet/node/src/cli/index.js.v1.njk +319 -0
- package/template/fnet/node/src/cli/v2/core/args-parser.njk +10 -0
- package/template/fnet/node/src/cli/v2/core/imports.njk +33 -0
- package/template/fnet/node/src/cli/v2/core/run-wrapper.njk +25 -0
- package/template/fnet/node/src/cli/v2/index.js.njk +184 -0
- package/template/fnet/node/src/cli/v2/modes/default/extend.njk +11 -0
- package/template/fnet/node/src/cli/v2/modes/default/standard.njk +20 -0
- package/template/fnet/node/src/cli/v2/modes/http/builtin.njk +66 -0
- package/template/fnet/node/src/cli/v2/modes/http/imports.njk +9 -0
- package/template/fnet/node/src/cli/v2/modes/http/index.njk +12 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/imports.njk +33 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/index.njk +30 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/server-setup.njk +15 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/tool-handlers.njk +46 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/transport-http.njk +222 -0
- package/template/fnet/node/src/cli/v2/modes/mcp/transport-stdio.njk +9 -0
- package/template/fnet/node/src/cli/v2/modes/pipeline/imports.njk +9 -0
- package/template/fnet/node/src/cli/v2/modes/pipeline/index.njk +113 -0
- package/template/fnet/node/src/cli/v2/modes/webhook/imports.njk +9 -0
- package/template/fnet/node/src/cli/v2/modes/webhook/index.njk +127 -0
- package/template/fnet/node/src/cli/v2/modes/websocket/imports.njk +15 -0
- package/template/fnet/node/src/cli/v2/modes/websocket/index.njk +104 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
{# ============================================================================
|
|
2
|
+
WEBSOCKET MODE - MAIN
|
|
3
|
+
============================================================================
|
|
4
|
+
WebSocket server for real-time bidirectional communication
|
|
5
|
+
Supports connection handling, message processing, and heartbeat
|
|
6
|
+
============================================================================ #}
|
|
7
|
+
|
|
8
|
+
if (cliMode === 'websocket') {
|
|
9
|
+
// WebSocket mode code
|
|
10
|
+
const port = args['cli-port'] || args.cli_port || 3000;
|
|
11
|
+
const heartbeat = args['ws-heartbeat'] || args.ws_heartbeat || 30000; // 30 seconds default
|
|
12
|
+
|
|
13
|
+
const wss = new WebSocketServer({ port });
|
|
14
|
+
|
|
15
|
+
// Store active connections
|
|
16
|
+
const clients = new Set();
|
|
17
|
+
|
|
18
|
+
wss.on('connection', (ws) => {
|
|
19
|
+
console.log('WebSocket client connected');
|
|
20
|
+
clients.add(ws);
|
|
21
|
+
|
|
22
|
+
// Set up heartbeat
|
|
23
|
+
ws.isAlive = true;
|
|
24
|
+
ws.on('pong', () => {
|
|
25
|
+
ws.isAlive = true;
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Handle incoming messages
|
|
29
|
+
ws.on('message', async (data) => {
|
|
30
|
+
try {
|
|
31
|
+
// Parse message
|
|
32
|
+
const message = data.toString();
|
|
33
|
+
let input;
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
input = JSON.parse(message);
|
|
37
|
+
} catch (e) {
|
|
38
|
+
// If not JSON, wrap in object
|
|
39
|
+
input = { data: message };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Validate input
|
|
43
|
+
// TODO: Add schema validation if needed
|
|
44
|
+
|
|
45
|
+
// Process with Engine
|
|
46
|
+
{% if atom.doc.features.cli.extend===true %}
|
|
47
|
+
const result = await runExtended(input, { Engine });
|
|
48
|
+
{% else %}
|
|
49
|
+
const engine = new Engine();
|
|
50
|
+
const result = await engine.run(input);
|
|
51
|
+
{% endif %}
|
|
52
|
+
|
|
53
|
+
// Send response back to client
|
|
54
|
+
ws.send(JSON.stringify(result));
|
|
55
|
+
|
|
56
|
+
} catch (error) {
|
|
57
|
+
// Send error to client
|
|
58
|
+
ws.send(JSON.stringify({
|
|
59
|
+
error: error.message,
|
|
60
|
+
type: 'processing_error'
|
|
61
|
+
}));
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Handle client disconnect
|
|
66
|
+
ws.on('close', () => {
|
|
67
|
+
console.log('WebSocket client disconnected');
|
|
68
|
+
clients.delete(ws);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Handle errors
|
|
72
|
+
ws.on('error', (error) => {
|
|
73
|
+
console.error('WebSocket error:', error.message);
|
|
74
|
+
clients.delete(ws);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Heartbeat interval
|
|
79
|
+
if (heartbeat > 0) {
|
|
80
|
+
const interval = setInterval(() => {
|
|
81
|
+
wss.clients.forEach((ws) => {
|
|
82
|
+
if (ws.isAlive === false) {
|
|
83
|
+
console.log('Terminating inactive client');
|
|
84
|
+
return ws.terminate();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
ws.isAlive = false;
|
|
88
|
+
ws.ping();
|
|
89
|
+
});
|
|
90
|
+
}, heartbeat);
|
|
91
|
+
|
|
92
|
+
wss.on('close', () => {
|
|
93
|
+
clearInterval(interval);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log(`WebSocket server started on port ${port}`);
|
|
98
|
+
if (heartbeat > 0) {
|
|
99
|
+
console.log(`Heartbeat interval: ${heartbeat}ms`);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
|