Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

πŸ”– Checkpoint: Edit /workspaces/claude-code-flow/src/cli/simple-commands/claude-track.js

rUvcebd3e6feaae
diff --git a/.claude/checkpoints/1755117497.json b/.claude/checkpoints/1755117497.json
new file mode 100644
index 0000000000..5a88b15026
--- /dev/null
+++ b/.claude/checkpoints/1755117497.json
@@ -0,0 +1 @@
+{"branch": "checkpoint/pre-edit-20250813-203817", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/swarm.js", "timestamp": "2025-08-13T20:38:17Z", "type": "pre-edit", "original_branch": "alpha-89"}
diff --git a/.claude/checkpoints/1755117499.json b/.claude/checkpoints/1755117499.json
new file mode 100644
index 0000000000..ebcd0da470
--- /dev/null
+++ b/.claude/checkpoints/1755117499.json
@@ -0,0 +1 @@
+{"tag": "checkpoint-20250813-203819", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/swarm.js", "timestamp": "2025-08-13T20:38:19Z", "type": "post-edit", "branch": "alpha-89", "diff_summary": " .claude-flow/metrics/performance.json | 2 +- .claude-flow/metrics/system-metrics.json | 12 ++++++++++++ .claude-flow/metrics/task-metrics.json | 6 +++--- .claude/checkpoints/task-1755117467.json | 1 + claude-flow-wiki | 0 src/cli/simple-commands/swarm.js | 33 ++++++++++++++------------------ 6 files changed, 31 insertions(+), 23 deletions(-) "}
diff --git a/src/cli/simple-commands/claude-track.js b/src/cli/simple-commands/claude-track.js
new file mode 100644
index 0000000000..345e88ed13
--- /dev/null
+++ b/src/cli/simple-commands/claude-track.js
@@ -0,0 +1,102 @@
+#!/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();
+}
\ No newline at end of file