-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprod-server.cjs
More file actions
120 lines (103 loc) · 3.13 KB
/
Copy pathprod-server.cjs
File metadata and controls
120 lines (103 loc) · 3.13 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
const http = require('http');
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const PORT = 5173;
const DIST = path.join(__dirname, 'dist');
const MIME = {
'.html': 'text/html',
'.js': 'application/javascript',
'.css': 'text/css',
'.json': 'application/json',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.ico': 'image/x-icon',
};
let cachedDashboard = null;
let lastFetch = 0;
const CACHE_TTL = 4000;
function runCmd(cmd) {
try {
return execSync(cmd, { timeout: 10000, encoding: 'utf-8' });
} catch (e) {
return null;
}
}
function tryJson(raw) {
if (!raw) return null;
try { return JSON.parse(raw); } catch { return null; }
}
function getDashboardData() {
const now = Date.now();
if (cachedDashboard && (now - lastFetch) < CACHE_TTL) return cachedDashboard;
const statusRaw = runCmd('openclaw status --all --json 2>/dev/null');
const healthRaw = runCmd('openclaw health --json 2>/dev/null');
const status = tryJson(statusRaw);
const health = tryJson(healthRaw);
// Build sessions list from status data
let sessions = [];
if (status?.sessions?.recent) {
sessions = status.sessions.recent;
} else if (status?.sessions?.byAgent) {
sessions = status.sessions.byAgent.flatMap(a => a.recent || []);
}
// Build health info
let healthInfo = {};
if (health) {
healthInfo = {
gateway: health.ok ? 'OK' : 'Down',
gatewayPid: health.pid,
signal: health.channels?.signal?.probe?.ok ? 'OK' : 'Down',
uptime: health.uptime || null,
skills: 0,
};
} else {
healthInfo = { gateway: 'Unknown', signal: 'Unknown' };
}
const result = {
sessions,
health: healthInfo,
cronJobs: [],
activity: [],
gateway: status?.gateway || {},
agents: status?.agents || {},
heartbeat: status?.heartbeat || {},
defaults: status?.sessions?.defaults || {},
};
cachedDashboard = result;
lastFetch = now;
return result;
}
function serveStatic(req, res) {
let filePath = path.join(DIST, req.url === '/' ? 'index.html' : req.url);
// SPA fallback
if (!fs.existsSync(filePath)) {
filePath = path.join(DIST, 'index.html');
}
try {
const content = fs.readFileSync(filePath);
const ext = path.extname(filePath);
res.setHeader('Content-Type', MIME[ext] || 'application/octet-stream');
res.end(content);
} catch {
res.statusCode = 404;
res.end('Not found');
}
}
const server = http.createServer((req, res) => {
if (req.url === '/api/dashboard' || req.url === '/api/status') {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(JSON.stringify(getDashboardData()));
} else if (req.url === '/api/health') {
const raw = runCmd('openclaw health --json 2>/dev/null');
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.end(raw || JSON.stringify({ error: 'unavailable' }));
} else {
serveStatic(req, res);
}
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`Agent Dashboard production server on http://0.0.0.0:${PORT}`);
});