fix: add CURRENT_PAGE definition, real usage tracking, swarm integration
This commit is contained in:
97
server.js
97
server.js
@@ -940,3 +940,100 @@ wss.on('connection', (socket) => {
|
||||
server.listen(PORT, '0.0.0.0', () => {
|
||||
console.log(`openclaw-taskboard listening on ${PORT}`);
|
||||
});
|
||||
|
||||
// ============ REAL USAGE TRACKING ============
|
||||
const REAL_SESSIONS_DIR = process.env.SESSIONS_DIR || '/app/agents';
|
||||
const SWARM_TASKS_FILE = process.env.SWARM_TASKS_FILE || '/app/swarm/active-tasks.json';
|
||||
|
||||
// GET /api/usage/real - Aggregate usage from session files
|
||||
app.get('/api/usage/real', async (req, res) => {
|
||||
try {
|
||||
const usageByAgent = {};
|
||||
const usageByModel = {};
|
||||
let totalInput = 0, totalOutput = 0, totalCost = 0;
|
||||
|
||||
if (!fs.existsSync(REAL_SESSIONS_DIR)) {
|
||||
return res.json({ error: 'sessions_dir_not_found', agents: {}, totals: {} });
|
||||
}
|
||||
|
||||
const agents = fs.readdirSync(REAL_SESSIONS_DIR).filter(d => {
|
||||
return fs.statSync(path.join(REAL_SESSIONS_DIR, d)).isDirectory();
|
||||
});
|
||||
|
||||
for (const agent of agents) {
|
||||
const sessionsDir = path.join(REAL_SESSIONS_DIR, agent, 'sessions');
|
||||
if (!fs.existsSync(sessionsDir)) continue;
|
||||
|
||||
let agentInput = 0, agentOutput = 0, agentCost = 0;
|
||||
|
||||
const sessions = fs.readdirSync(sessionsDir).filter(f => f.endsWith('.jsonl'));
|
||||
for (const sessionFile of sessions) {
|
||||
const filePath = path.join(sessionsDir, sessionFile);
|
||||
const lines = fs.readFileSync(filePath, 'utf8').split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (!line.trim()) continue;
|
||||
try {
|
||||
const msg = JSON.parse(line);
|
||||
if (msg.message?.usage) {
|
||||
const u = msg.message.usage;
|
||||
agentInput += u.input || 0;
|
||||
agentOutput += u.output || 0;
|
||||
agentCost += u.cost?.total || 0;
|
||||
|
||||
// Track by model
|
||||
const model = msg.message.model || 'unknown';
|
||||
if (!usageByModel[model]) {
|
||||
usageByModel[model] = { input: 0, output: 0, requests: 0 };
|
||||
}
|
||||
usageByModel[model].input += u.input || 0;
|
||||
usageByModel[model].output += u.output || 0;
|
||||
usageByModel[model].requests++;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
usageByAgent[agent] = {
|
||||
input: agentInput,
|
||||
output: agentOutput,
|
||||
total: agentInput + agentOutput,
|
||||
cost: agentCost
|
||||
};
|
||||
|
||||
totalInput += agentInput;
|
||||
totalOutput += agentOutput;
|
||||
totalCost += agentCost;
|
||||
}
|
||||
|
||||
res.json({
|
||||
agents: usageByAgent,
|
||||
models: usageByModel,
|
||||
totals: {
|
||||
input: totalInput,
|
||||
output: totalOutput,
|
||||
total: totalInput + totalOutput,
|
||||
cost: totalCost
|
||||
},
|
||||
lastUpdated: new Date().toISOString()
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Error calculating real usage:', err);
|
||||
res.status(500).json({ error: 'failed_to_calculate_usage' });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/swarm/tasks - Get swarm task registry
|
||||
app.get('/api/swarm/tasks', (req, res) => {
|
||||
try {
|
||||
if (fs.existsSync(SWARM_TASKS_FILE)) {
|
||||
const data = JSON.parse(fs.readFileSync(SWARM_TASKS_FILE, 'utf8'));
|
||||
res.json(data);
|
||||
} else {
|
||||
res.json({ tasks: [], message: 'swarm_registry_not_found' });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error reading swarm tasks:', err);
|
||||
res.status(500).json({ error: 'failed_to_read_swarm_tasks' });
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user