-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.mjs
More file actions
150 lines (136 loc) · 5.24 KB
/
Copy pathserver.mjs
File metadata and controls
150 lines (136 loc) · 5.24 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
import { createServer } from 'http';
import { readFile, readdir, stat } from 'fs/promises';
import { join, extname } from 'path';
const PORT = 5173;
const OPENCLAW_DIR = join(process.env.HOME, '.openclaw');
const SESSIONS_FILE = join(OPENCLAW_DIR, 'agents/main/sessions/sessions.json');
const CRON_FILE = join(OPENCLAW_DIR, 'cron/jobs.json');
const CONFIG_FILE = join(OPENCLAW_DIR, 'openclaw.json');
const DIST_DIR = join(import.meta.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',
};
async function readJSON(path) {
try { return JSON.parse(await readFile(path, 'utf-8')); } catch { return null; }
}
async function getGatewayPid() {
try {
const { execSync } = await import('child_process');
return parseInt(execSync('pgrep -f "openclaw-gateway"', { encoding: 'utf-8', timeout: 2000 }).trim().split('\n')[0]) || null;
} catch { return null; }
}
async function getUptime() {
try {
const pid = await getGatewayPid();
if (!pid) return null;
const { execSync } = await import('child_process');
return execSync(`ps -p ${pid} -o etime=`, { encoding: 'utf-8', timeout: 2000 }).trim();
} catch { return null; }
}
async function getSessionActivity(sessionsDir) {
const activity = [];
try {
const files = await readdir(sessionsDir);
const jsonlFiles = files.filter(f => f.endsWith('.jsonl')).slice(-5);
for (const file of jsonlFiles) {
try {
const content = await readFile(join(sessionsDir, file), 'utf-8');
const lines = content.trim().split('\n').slice(-10);
for (const line of lines) {
try {
const entry = JSON.parse(line);
if (entry.role === 'user' || entry.role === 'assistant') {
const text = typeof entry.content === 'string'
? entry.content.slice(0, 120)
: entry.content?.[0]?.text?.slice(0, 120) || '';
if (text && !text.startsWith('<system')) {
activity.push({ type: entry.role === 'user' ? 'session' : 'system', message: `[${entry.role}] ${text}`, timestamp: entry.ts || Date.now(), sessionFile: file });
}
}
if (entry.role === 'assistant' && Array.isArray(entry.content)) {
for (const block of entry.content) {
if (block.type === 'tool_use') {
activity.push({ type: 'tool', message: `Tool: ${block.name}`, timestamp: entry.ts || Date.now() });
}
}
}
} catch {}
}
} catch {}
}
} catch {}
return activity.sort((a, b) => (b.timestamp || 0) - (a.timestamp || 0)).slice(0, 20);
}
async function getDashboardData() {
const [sessionsData, cronData, config] = await Promise.all([
readJSON(SESSIONS_FILE), readJSON(CRON_FILE), readJSON(CONFIG_FILE),
]);
let skillsCount = 0;
if (sessionsData) {
for (const val of Object.values(sessionsData)) {
if (val.skillsSnapshot?.skills?.length) { skillsCount = val.skillsSnapshot.skills.length; break; }
}
}
const sessions = sessionsData
? Object.entries(sessionsData).map(([key, val]) => {
const { skillsSnapshot, systemPromptReport, ...rest } = val;
return { key, ...rest };
}).sort((a, b) => (b.updatedAt || 0) - (a.updatedAt || 0))
: [];
const cronJobs = cronData?.jobs || [];
const gatewayPid = await getGatewayPid();
const uptime = await getUptime();
const signalStatus = config?.channels?.signal ? 'OK' : 'Not configured';
const sessionsDir = join(OPENCLAW_DIR, 'agents/main/sessions');
const activity = await getSessionActivity(sessionsDir);
return {
sessions, cronJobs,
health: { gateway: !!gatewayPid, gatewayPid, signal: signalStatus, uptime, sessions: sessions.length, skills: skillsCount },
activity, timestamp: Date.now(),
};
}
async function serveStatic(res, urlPath) {
let filePath = join(DIST_DIR, urlPath === '/' ? 'index.html' : urlPath);
try {
const s = await stat(filePath);
if (s.isDirectory()) filePath = join(filePath, 'index.html');
const content = await readFile(filePath);
const ext = extname(filePath);
res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream', 'Cache-Control': ext === '.html' ? 'no-cache' : 'public, max-age=31536000' });
res.end(content);
} catch {
// SPA fallback
try {
const html = await readFile(join(DIST_DIR, 'index.html'));
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
res.end(html);
} catch {
res.writeHead(404);
res.end('Not found');
}
}
}
const server = createServer(async (req, res) => {
if (req.url === '/api/dashboard') {
res.setHeader('Content-Type', 'application/json');
try {
res.writeHead(200);
res.end(JSON.stringify(await getDashboardData()));
} catch (err) {
res.writeHead(500);
res.end(JSON.stringify({ error: err.message }));
}
} else {
await serveStatic(res, req.url);
}
});
server.listen(PORT, '0.0.0.0', () => {
console.log(`✨ Centauri Dashboard running at http://localhost:${PORT}`);
console.log(` Network: http://192.168.1.5:${PORT}`);
});