-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
432 lines (380 loc) · 16.6 KB
/
Copy pathcli.js
File metadata and controls
432 lines (380 loc) · 16.6 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#!/usr/bin/env node
const path = require('path');
const { startServer } = require('./server');
const { startProxy, startRecording, recordToolCall, finishRecording } = require('./proxy');
const { startMcpServer } = require('./mcp-server');
const { getSessions, getToolCalls, getDashboardStats } = require('./database');
const pkg = require('./package.json');
const command = process.argv[2];
const args = process.argv.slice(3);
function usage() {
console.log(`
agent-obs — Open Source Agent Observability
Commands:
server Run as MCP server (recommended — agent self-reports all actions)
dashboard [--port <n>] [--quick] Start the web dashboard
setup One-command onboarding — detect agent, configure MCP
demo Show a pre-loaded demo session in the dashboard
check [--last <n>] Show latest session details
stats Show aggregate session stats
start <description> Start a recording session (manual mode)
stop <session-id> End a recording session
log <session-id> Log a tool call (pipe JSON on stdin)
proxy [--desc] -- ... Transparent MCP proxy (fallback — MCP-only capture)
inspect <session-id> Show session details in terminal
Examples:
agent-obs server
agent-obs demo
agent-obs dashboard
agent-obs check
agent-obs check --last 3
agent-obs stats
agent-obs proxy --desc "fix login bug" -- npx @modelcontextprotocol/server-filesystem /tmp
agent-obs inspect abc12345
Without a command, starts the dashboard on port 9400.
`);
}
function timeAgo(sqliteUtc) {
if (!sqliteUtc) return 'unknown';
const then = new Date(sqliteUtc.replace(' ', 'T') + 'Z').getTime();
if (isNaN(then)) return sqliteUtc;
const diffSec = Math.max(0, Math.floor((Date.now() - then) / 1000));
if (diffSec < 60) return `${diffSec}s ago`;
if (diffSec < 3600) return `${Math.floor(diffSec / 60)}m ago`;
if (diffSec < 86400) return `${Math.floor(diffSec / 3600)}h ago`;
return `${Math.floor(diffSec / 86400)}d ago`;
}
function printSummary() {
console.log(`agent-obs v${pkg.version}`);
try {
const stats = getDashboardStats();
const sessions = getSessions({ limit: 1 });
const lastGrade = sessions.length ? sessions[0].grade || '?' : '?';
if (stats.totalSessions === 0) {
console.log('No sessions yet');
} else {
console.log(`${stats.totalSessions} sessions · ${stats.totalToolCalls} tool calls · last grade ${lastGrade}`);
}
} catch (_) {
console.log('No sessions yet');
}
console.log('Dashboard: http://localhost:9400');
}
async function main() {
if (command === 'help' || command === '--help' || command === '-h') {
printSummary();
usage();
process.exit(0);
}
if (command === 'dashboard') {
const portIdx = args.indexOf('--port');
const quickIdx = args.indexOf('--quick');
const port = portIdx >= 0 ? parseInt(args[portIdx + 1]) : 9400;
if (quickIdx >= 0) {
const { fork } = require('child_process');
fork(path.join(__dirname, 'cli.js'), ['server'], { stdio: 'ignore', detached: true }).unref();
console.log('[agent-obs] MCP server started (agent can now connect)');
}
printSummary();
await startServer(port);
return;
}
if (command === 'server') {
startMcpServer();
return;
}
if (command === 'check') {
const lastN = args.indexOf('--last') >= 0 ? parseInt(args[args.indexOf('--last') + 1]) || 1 : 1;
const sessions = getSessions({ limit: lastN });
if (!sessions.length) {
console.log('No agent sessions recorded yet.');
console.log('Start the dashboard with: agent-obs dashboard');
process.exit(0);
}
if (lastN === 1) {
const s = sessions[0];
const calls = getToolCalls(s.id);
const errors = calls.filter(c => c.status === 'error').length;
const totalMs = calls.reduce((sum, c) => sum + (c.duration_ms || 0), 0);
console.log(`Session: ${s.id.slice(0, 8)}`);
console.log(`Agent: ${s.agent_type}`);
console.log(`Task: ${s.task_description || '(none)'}`);
console.log(`Status: ${s.status} | Grade: ${s.grade || 'N/A'}`);
console.log(`Tools: ${calls.length} calls | ${errors} ${errors === 1 ? 'error' : 'errors'} | ${(totalMs / 1000).toFixed(1)}s total`);
console.log(`Tokens: ${(s.total_tokens || 0).toLocaleString()} | Cost: $${s.estimated_cost_usd}`);
console.log(`Started: ${timeAgo(s.started_at)}`);
} else {
for (const s of sessions) {
const calls = getToolCalls(s.id);
const errors = calls.filter(c => c.status === 'error').length;
const statusIcon = s.status === 'complete' ? '✓' : s.status === 'error' ? '✗' : '⋯';
console.log(`${statusIcon} ${s.id.slice(0, 8)} | ${s.grade || '?'} | ${(s.task_description || '(none)').slice(0, 50)} | ${calls.length} calls | ${errors} errors | $${s.estimated_cost_usd}`);
}
}
process.exit(0);
}
if (command === 'stats') {
const stats = getDashboardStats();
console.log('── Agent Observability ──');
console.log(`Total sessions: ${stats.totalSessions}`);
console.log(`Completed: ${stats.completedSessions}`);
console.log(`Failed: ${stats.failedSessions}`);
console.log(`Total tool calls: ${stats.totalToolCalls}`);
console.log(`Total tokens: ${(stats.totalTokens || 0).toLocaleString()}`);
console.log(`Total cost: $${stats.totalCost.toFixed(2)}`);
console.log(`Avg duration: ${Math.round(stats.avgDurationSeconds)}s`);
const gradeValues = { A: 4, B: 3, C: 2, D: 1, F: 0 };
const distribution = { A: 0, B: 0, C: 0, D: 0, F: 0 };
const graded = getSessions({ limit: 10000 }).filter(s => s.grade in gradeValues);
for (const s of graded) distribution[s.grade]++;
if (graded.length) {
const avg = graded.reduce((sum, s) => sum + gradeValues[s.grade], 0) / graded.length;
const avgGrade = ['F', 'D', 'C', 'B', 'A'][Math.round(avg)];
console.log(`Avg grade: ${avgGrade}`);
}
console.log('');
console.log('Grade distribution:');
console.log(` A: ${distribution.A} B: ${distribution.B} C: ${distribution.C} D: ${distribution.D} F: ${distribution.F}`);
process.exit(0);
}
if (command === 'setup') {
const fs = require('fs');
const path = require('path');
const cwd = process.cwd();
let detected = [];
let actions = [];
const opencodeJson = path.join(cwd, 'opencode.json');
if (fs.existsSync(opencodeJson)) {
detected.push('opencode');
try {
const config = JSON.parse(fs.readFileSync(opencodeJson, 'utf8'));
if (!config.mcp) config.mcp = {};
if (!config.mcp['agent-obs']) {
config.mcp['agent-obs'] = {
type: 'local',
command: ['npx', '-y', 'agent-obs@latest', 'server'],
enabled: true,
};
fs.writeFileSync(opencodeJson, JSON.stringify(config, null, 2) + '\n');
actions.push('opencode.json updated (agent-obs MCP server added)');
} else {
actions.push('opencode.json already has agent-obs ✓');
}
} catch (e) {
actions.push('Could not update opencode.json: ' + e.message);
}
const agentsMd = path.join(cwd, 'AGENTS.md');
const instruction = '\n## Agent Observability (Self-Reporting)\n\nWhen the `agent-obs` MCP server is connected, self-report every tool call:\n\n- Call `agent_obs_start_session` at the start of every task\n- Call `agent_obs_log_tool_call` after EVERY tool call (Read, Write, Edit, Bash, Glob, Grep, etc.) with tool name, duration, status, and output summary\n- Call `agent_obs_end_session` at task completion with token counts\n- NEVER skip log_tool_call — every tool call must be logged. One call = one log.\n- Log errors with status: "error" and the error message\n- Do NOT log the agent-obs tools themselves (start_session, log_tool_call, end_session)\n';
if (fs.existsSync(agentsMd)) {
const content = fs.readFileSync(agentsMd, 'utf8');
if (!content.includes('Agent Observability (Self-Reporting)')) {
fs.appendFileSync(agentsMd, instruction);
actions.push('AGENTS.md updated (self-reporting instruction added)');
} else {
actions.push('AGENTS.md already has self-reporting instruction ✓');
}
} else {
fs.writeFileSync(agentsMd, instruction + '\n');
actions.push('AGENTS.md created (self-reporting instruction added)');
}
}
const claudeDir = path.join(cwd, '.claude');
const mcpJson = path.join(cwd, '.mcp.json');
if (fs.existsSync(claudeDir) || fs.existsSync(mcpJson)) {
detected.push('claude-code');
const target = mcpJson;
let mcpConfig = {};
if (fs.existsSync(target)) {
try { mcpConfig = JSON.parse(fs.readFileSync(target, 'utf8')); } catch (_) { /* use empty default */ }
}
if (!mcpConfig.mcpServers) mcpConfig.mcpServers = {};
if (!mcpConfig.mcpServers['agent-obs']) {
mcpConfig.mcpServers['agent-obs'] = {
command: 'npx',
args: ['-y', 'agent-obs@latest', 'server'],
};
fs.writeFileSync(target, JSON.stringify(mcpConfig, null, 2) + '\n');
actions.push('.mcp.json created (agent-obs MCP server configured)');
} else {
actions.push('.mcp.json already has agent-obs ✓');
}
}
const cursorDir = path.join(cwd, '.cursor');
if (fs.existsSync(cursorDir)) {
detected.push('cursor');
actions.push('Cursor detected — add this to Cursor Settings > MCP:');
actions.push(' { "agent-obs": { "command": "npx", "args": ["-y", "agent-obs@latest", "server"] } }');
}
if (detected.length === 0) {
detected.push('no-agent');
actions.push('No AI agent detected in current directory.');
// Create a starter .mcp.json so they have something to work with
const mcpJsonPath = path.join(cwd, '.mcp.json');
if (!fs.existsSync(mcpJsonPath)) {
const starter = { mcpServers: { 'agent-obs': { command: 'npx', args: ['-y', 'agent-obs@latest', 'server'] } } };
fs.writeFileSync(mcpJsonPath, JSON.stringify(starter, null, 2) + '\n');
actions.push('Created .mcp.json with agent-obs pre-configured');
} else {
actions.push('.mcp.json already exists — add agent-obs manually if not present');
}
// Also create a starter instruction file
const instrPath = path.join(cwd, '.agent-obs-instructions.md');
if (!fs.existsSync(instrPath)) {
fs.writeFileSync(instrPath, `After every tool call, report to agent-obs's log_tool_call.
Start each task with start_session. End with end_session.
Never skip log_tool_call — every tool call must be logged.\n`);
actions.push('Created .agent-obs-instructions.md (self-reporting prompt)');
}
actions.push('');
actions.push('Next: paste the config into your agent\'s MCP settings,');
actions.push('or run your agent from this directory to auto-detect.');
}
console.log('╔══════════════════════════════════════════╗');
console.log('║ agent-obs v' + pkg.version + ' — Setup ║');
console.log('╚══════════════════════════════════════════╝');
console.log('');
console.log('Detected: ' + detected.join(', '));
actions.forEach(a => console.log(' ✓ ' + a));
console.log('');
console.log('Next: restart ' + detected[0]);
console.log('Dashboard: http://localhost:9400');
console.log('');
console.log('To verify:');
console.log(' agent-obs dashboard');
console.log(' # Run any task in your agent');
console.log(' # Sessions should appear automatically');
// Auto-open dashboard
const { exec } = require('child_process');
exec('open http://localhost:9400 2>/dev/null || xdg-open http://localhost:9400 2>/dev/null');
process.exit(0);
}
if (command === 'proxy') {
const separatorIdx = args.indexOf('--');
if (separatorIdx < 0) {
console.error('Usage: agent-obs proxy [--desc <text>] -- <command...>');
process.exit(1);
}
let taskDesc = 'MCP proxy session';
let agentType = 'opencode';
const proxyArgs = args.slice(0, separatorIdx);
for (let i = 0; i < proxyArgs.length; i++) {
if (proxyArgs[i] === '--desc' && proxyArgs[i + 1]) {
taskDesc = proxyArgs[i + 1];
i++;
} else if (proxyArgs[i] === '--agent' && proxyArgs[i + 1]) {
agentType = proxyArgs[i + 1];
i++;
}
}
const cmdArgs = args.slice(separatorIdx + 1);
if (cmdArgs.length === 0) {
console.error('No command specified after --');
process.exit(1);
}
const targetCommand = cmdArgs.join(' ');
startProxy(targetCommand, [], { taskDescription: taskDesc, agentType });
return;
}
if (command === 'start') {
const desc = args.join(' ') || 'Manual session';
const session = startRecording({ taskDescription: desc });
console.log(JSON.stringify(session));
process.exit(0);
}
if (command === 'stop') {
const sessionId = args[0];
if (!sessionId) {
console.error('Usage: agent-obs stop <session-id>');
process.exit(1);
}
const result = finishRecording(sessionId, { status: 'complete' });
console.log(JSON.stringify(result));
process.exit(0);
}
if (command === 'log') {
const sessionId = args[0];
if (!sessionId) {
console.error('Usage: agent-obs log <session-id>');
process.exit(1);
}
const chunks = [];
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', chunk => chunks.push(chunk));
process.stdin.on('end', () => {
try {
const data = JSON.parse(chunks.join(''));
const result = recordToolCall(sessionId, data);
console.log(JSON.stringify(result));
process.exit(0);
} catch (e) {
console.error('Invalid JSON input:', e.message);
process.exit(1);
}
});
// If stdin already ended (no pipe), exit
if (process.stdin.isTTY) {
console.error('Usage: echo \'{"toolName":"..."}\' | agent-obs log <session-id>');
process.exit(1);
}
return;
}
if (command === 'demo') {
const portIdx = args.indexOf('--port');
const port = portIdx >= 0 ? parseInt(args[portIdx + 1]) : 9400;
const { seedDemoSession } = require('./database');
seedDemoSession();
printSummary();
console.log('Demo session loaded — showing a real agent trace');
const { exec } = require('child_process');
exec(`open http://localhost:${port} 2>/dev/null || xdg-open http://localhost:${port} 2>/dev/null`);
await startServer(port);
return;
}
if (command === 'inspect') {
const sessionId = args[0];
if (!sessionId) {
console.error('Usage: agent-obs inspect <session-id>');
process.exit(1);
}
const { getSession, getToolCalls } = require('./database');
const session = getSession(sessionId);
if (!session) {
console.error('Session not found');
process.exit(1);
}
const calls = getToolCalls(session.id);
console.log(`\n Session: ${session.id}`);
console.log(` Agent: ${session.agent_type}`);
console.log(` Task: ${session.task_description || '(none)'}`);
console.log(` Status: ${session.status} | Grade: ${session.grade || 'N/A'}`);
console.log(` Tokens: ${session.total_tokens} | Cost: $${session.estimated_cost_usd}`);
console.log(` Started: ${session.started_at}`);
if (session.ended_at) console.log(` Ended: ${session.ended_at}`);
if (session.error_message) console.log(` Error: ${session.error_message}`);
console.log(`\n Tool Calls (${calls.length}):`);
for (const call of calls) {
const status = call.status === 'error' ? '✗' : '✓';
console.log(` ${status} Step ${call.step_number}: ${call.tool_name} (${call.duration_ms}ms)`);
if (call.output_summary) {
console.log(` → ${call.output_summary.slice(0, 120)}`);
}
if (call.error_message) {
console.log(` ✗ ${call.error_message}`);
}
}
console.log('');
process.exit(0);
}
// Default: start dashboard
printSummary();
const { exec } = require('child_process');
const dashboardUrl = 'http://localhost:9400';
console.log('Opening ' + dashboardUrl + ' ...');
exec('open ' + dashboardUrl + ' 2>/dev/null || xdg-open ' + dashboardUrl + ' 2>/dev/null');
await startServer(9400);
}
main().catch(err => {
console.error('agent-obs error:', err.message);
process.exit(1);
});