-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
140 lines (124 loc) · 4.45 KB
/
Copy pathserver.js
File metadata and controls
140 lines (124 loc) · 4.45 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
const express = require('express');
const path = require('path');
const {
getSessions, getSession, getToolCalls,
getDecisions, getAuditEntries,
getDashboardStats, getLatestHealthChecks, getHealthHistory,
recordHealthCheck,
} = require('./database');
const app = express();
const PORT = process.env.PORT || 9400;
app.use(express.json());
app.use(express.static(path.join(__dirname, 'public')));
// ─── Health ────────────────────────────────────────
app.get('/api/health', (_req, res) => {
const stats = getDashboardStats();
res.json({ status: 'ok', ...stats, timestamp: new Date().toISOString() });
});
// ─── Sessions ──────────────────────────────────────
app.get('/api/sessions', (req, res) => {
const limit = parseInt(req.query.limit) || 50;
const sessions = getSessions({ limit, agentType: req.query.agentType, status: req.query.status });
res.json(sessions.map(s => ({
id: s.id,
agentType: s.agent_type,
model: s.model,
status: s.status,
grade: s.grade,
totalTokens: s.total_tokens,
estimatedCost: s.estimated_cost_usd,
taskDescription: s.task_description,
errorMessage: s.error_message,
startedAt: s.started_at,
endedAt: s.ended_at,
})));
});
app.get('/api/sessions/:id', (req, res) => {
const session = getSession(req.params.id);
if (!session) return res.status(404).json({ error: 'Session not found' });
const toolCalls = getToolCalls(session.id).map(c => ({
id: c.id,
toolName: c.tool_name,
toolServer: c.tool_server,
stepNumber: c.step_number,
input: safeParse(c.input_json),
output: safeParse(c.output_json),
outputSummary: c.output_summary,
durationMs: c.duration_ms,
status: c.status,
errorMessage: c.error_message,
timestamp: c.timestamp,
}));
const decisions = getDecisions(session.id);
const auditEntries = getAuditEntries(session.id);
res.json({
...session,
agentType: session.agent_type,
taskDescription: session.task_description,
errorMessage: session.error_message,
estimatedCost: session.estimated_cost_usd,
totalTokens: session.total_tokens,
startedAt: session.started_at,
endedAt: session.ended_at,
inputTokens: session.input_tokens,
outputTokens: session.output_tokens,
toolCalls,
decisions,
auditEntries,
toolCallCount: toolCalls.length,
errorCount: toolCalls.filter(c => c.status === 'error').length,
});
});
// ─── Tool Health ───────────────────────────────────
app.get('/api/tool-health', (_req, res) => {
const checks = getLatestHealthChecks();
res.json(checks.map(c => ({
id: c.id,
toolServer: c.tool_server,
healthScore: c.health_score,
grade: c.grade,
toolCount: c.tool_count,
gate: c.gate,
checkedAt: c.checked_at,
})));
});
app.get('/api/tool-health/:server/history', (req, res) => {
const history = getHealthHistory(req.params.server, 30);
res.json(history.map(c => ({
healthScore: c.health_score,
grade: c.grade,
gate: c.gate,
toolCount: c.tool_count,
checkedAt: c.checked_at,
})));
});
app.post('/api/tool-health', (req, res) => {
const { toolServer, healthScore, grade, toolCount, gate, checks } = req.body;
if (!toolServer) return res.status(400).json({ error: 'toolServer required' });
const result = recordHealthCheck({ toolServer, healthScore, grade, toolCount, gate, checks });
res.status(201).json(result);
});
// ─── Stats ─────────────────────────────────────────
app.get('/api/stats', (_req, res) => {
const stats = getDashboardStats();
res.json(stats);
});
// ─── Fallback ──────────────────────────────────────
app.get('*', (_req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
function safeParse(str) {
if (!str) return null;
try { return JSON.parse(str); } catch (_) { return str; }
}
function startServer(port) {
const p = port || PORT;
return new Promise((resolve) => {
app.listen(p, () => {
console.log('\n Agent Observability Dashboard');
console.log(` http://localhost:${p}\n`);
resolve(app);
});
});
}
module.exports = { app, startServer };