mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-23 21:59:14 +09:00
- Reorganize v3/ into implementation/ subfolders: - planning/, architecture/, migration/, integration/ - security/, adrs/, research/, swarm-plans/ - Add 15-agent concurrent swarm implementation plans: - SWARM-OVERVIEW.md: Hierarchical mesh topology, 4 phases - AGENT-SPECIFICATIONS.md: 15 agent configs with capabilities - TDD-LONDON-SCHOOL-PLAN.md: Mock-first TDD methodology - BENCHMARK-OPTIMIZATION.md: Performance targets (2.49x-7.47x) - DEPLOYMENT-PLAN.md: Release strategy, CI/CD, rollback - GITHUB-ISSUE-TRACKING.md: Issue workflow with agent replies - Update .claude/settings.json with proper hooks format - Add bash aliases for claude --dangerously-skip-permissions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Claude Token Tracking - Extracts token usage from Claude sessions
|
|
* Works in background without interfering with Claude CLI
|
|
*/
|
|
|
|
import { spawn } from 'child_process';
|
|
import { trackTokens } from './token-tracker.js';
|
|
|
|
/**
|
|
* Parse token information from Claude telemetry output
|
|
*/
|
|
function parseTokensFromTelemetry(data) {
|
|
try {
|
|
// Look for lines containing token information
|
|
const lines = data.split('\n');
|
|
|
|
for (const line of lines) {
|
|
if (line.includes('api_request') && line.includes('attributes')) {
|
|
// Try to extract from structured output
|
|
const match = line.match(/input_tokens['":\s]+(\d+).*output_tokens['":\s]+(\d+)/);
|
|
if (match) {
|
|
return {
|
|
inputTokens: parseInt(match[1]),
|
|
outputTokens: parseInt(match[2])
|
|
};
|
|
}
|
|
}
|
|
|
|
// Also look for simpler patterns
|
|
if (line.includes('input_tokens:') || line.includes('output_tokens:')) {
|
|
const inputMatch = line.match(/input_tokens:\s*'?(\d+)'?/);
|
|
const outputMatch = line.match(/output_tokens:\s*'?(\d+)'?/);
|
|
|
|
if (inputMatch || outputMatch) {
|
|
return {
|
|
inputTokens: inputMatch ? parseInt(inputMatch[1]) : 0,
|
|
outputTokens: outputMatch ? parseInt(outputMatch[1]) : 0
|
|
};
|
|
}
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// Silently ignore parse errors
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Background token tracker for Claude sessions
|
|
*/
|
|
export async function trackClaudeSession() {
|
|
console.log('🔍 Claude token tracker started (running in background)');
|
|
console.log(' Token usage will be saved to .claude-flow/metrics/token-usage.json');
|
|
|
|
let totalInput = 0;
|
|
let totalOutput = 0;
|
|
let lastUpdate = Date.now();
|
|
|
|
// Monitor Claude's telemetry output
|
|
process.stdin.on('data', async (data) => {
|
|
const output = data.toString();
|
|
const tokens = parseTokensFromTelemetry(output);
|
|
|
|
if (tokens && (tokens.inputTokens > 0 || tokens.outputTokens > 0)) {
|
|
totalInput += tokens.inputTokens;
|
|
totalOutput += tokens.outputTokens;
|
|
|
|
// Track tokens
|
|
await trackTokens({
|
|
sessionId: `claude-session-${Date.now()}`,
|
|
agentType: 'claude-cli',
|
|
command: 'interactive',
|
|
inputTokens: tokens.inputTokens,
|
|
outputTokens: tokens.outputTokens,
|
|
metadata: {
|
|
source: 'telemetry_stream'
|
|
}
|
|
});
|
|
|
|
// Show update every 10 seconds max
|
|
if (Date.now() - lastUpdate > 10000) {
|
|
console.log(`📊 Token Update: Input: ${totalInput}, Output: ${totalOutput}`);
|
|
lastUpdate = Date.now();
|
|
}
|
|
}
|
|
});
|
|
|
|
// Handle shutdown
|
|
process.on('SIGINT', () => {
|
|
console.log(`\n📊 Session Total: Input: ${totalInput}, Output: ${totalOutput}`);
|
|
console.log('✅ Token tracking data saved');
|
|
process.exit(0);
|
|
});
|
|
}
|
|
|
|
// CLI interface
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
trackClaudeSession();
|
|
} |