From 08e15e5269fe5c0ca2102e8785b3a1135b27ef17 Mon Sep 17 00:00:00 2001 From: rUv Date: Wed, 13 Aug 2025 20:15:21 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Checkpoint:=20Edit=20/workspaces?= =?UTF-8?q?/claude-code-flow/src/cli/simple-commands/analysis.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .claude/checkpoints/1755115939.json | 1 + .claude/checkpoints/1755115941.json | 1 + src/cli/simple-commands/analysis.js | 164 ---------------------------- 3 files changed, 2 insertions(+), 164 deletions(-) create mode 100644 .claude/checkpoints/1755115939.json create mode 100644 .claude/checkpoints/1755115941.json diff --git a/.claude/checkpoints/1755115939.json b/.claude/checkpoints/1755115939.json new file mode 100644 index 0000000000..1e9a059858 --- /dev/null +++ b/.claude/checkpoints/1755115939.json @@ -0,0 +1 @@ +{"branch": "checkpoint/pre-edit-20250813-201219", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/analysis.js", "timestamp": "2025-08-13T20:12:19Z", "type": "pre-edit", "original_branch": "alpha-89"} diff --git a/.claude/checkpoints/1755115941.json b/.claude/checkpoints/1755115941.json new file mode 100644 index 0000000000..a75d289894 --- /dev/null +++ b/.claude/checkpoints/1755115941.json @@ -0,0 +1 @@ +{"tag": "checkpoint-20250813-201221", "file": "/workspaces/claude-code-flow/src/cli/simple-commands/analysis.js", "timestamp": "2025-08-13T20:12:21Z", "type": "post-edit", "branch": "alpha-89", "diff_summary": " .claude-flow/metrics/performance.json | 2 +- .claude-flow/metrics/system-metrics.json | 120 +++++++++++++++++++++++++++++++ .claude-flow/metrics/task-metrics.json | 6 +- .claude/checkpoints/1755115870.json | 1 + .claude/checkpoints/1755115872.json | 1 + claude-flow-wiki | 0 src/cli/simple-commands/analysis.js | 15 +--- 7 files changed, 127 insertions(+), 18 deletions(-) "} diff --git a/src/cli/simple-commands/analysis.js b/src/cli/simple-commands/analysis.js index af525c2a9b..1d3962c325 100644 --- a/src/cli/simple-commands/analysis.js +++ b/src/cli/simple-commands/analysis.js @@ -312,170 +312,6 @@ EXAMPLES: // Helper functions for real token tracking are now imported from token-tracker.js -async function getLocalTokenMetrics(agent) { - // Look for Claude Code metrics in standard locations - const metricsPath = path.join(process.env.HOME || '', '.claude', 'metrics', 'usage.json'); - - try { - const data = await fs.readFile(metricsPath, 'utf8'); - const metrics = JSON.parse(data); - - // Extract token usage from metrics - const tokenData = { - total: 0, - input: 0, - output: 0, - byAgent: {} - }; - - // Process metrics based on Claude Code format - if (metrics.sessions) { - metrics.sessions.forEach(session => { - if (session.tokenUsage) { - tokenData.total += session.tokenUsage.total || 0; - tokenData.input += session.tokenUsage.input || 0; - tokenData.output += session.tokenUsage.output || 0; - - // Track by agent if available - if (session.agentType && (agent === 'all' || session.agentType === agent)) { - tokenData.byAgent[session.agentType] = - (tokenData.byAgent[session.agentType] || 0) + (session.tokenUsage.total || 0); - } - } - }); - } - - return tokenData; - } catch (err) { - // Fallback to environment variables or defaults - return getEnvironmentTokenMetrics(agent); - } -} - -async function getEnvironmentTokenMetrics(agent) { - // Check for token tracking in environment or config - const configPath = path.join(process.cwd(), '.claude-flow', 'token-usage.json'); - - try { - const data = await fs.readFile(configPath, 'utf8'); - return JSON.parse(data); - } catch (err) { - // Return empty data structure - return { - total: 0, - input: 0, - output: 0, - byAgent: {} - }; - } -} - -async function getOTelTokenMetrics(agent) { - // This would integrate with OpenTelemetry exporters - // For now, return placeholder implementation - return { - total: 0, - input: 0, - output: 0, - byAgent: {} - }; -} - -function calculateCost(tokenData) { - // Anthropic Claude pricing (as of knowledge cutoff) - // Claude 3 Opus: $15/$75 per million tokens (input/output) - // Claude 3 Sonnet: $3/$15 per million tokens - // Claude 3 Haiku: $0.25/$1.25 per million tokens - - // Default to Sonnet pricing for Claude Code - const inputPricePerMillion = 3.00; - const outputPricePerMillion = 15.00; - - return { - input: (tokenData.input / 1000000) * inputPricePerMillion, - output: (tokenData.output / 1000000) * outputPricePerMillion, - total: ((tokenData.input / 1000000) * inputPricePerMillion) + - ((tokenData.output / 1000000) * outputPricePerMillion) - }; -} - -function getAgentIcon(agentType) { - const icons = { - 'coordinator': '๐ŸŽฏ', - 'developer': '๐Ÿ‘จโ€๐Ÿ’ป', - 'researcher': '๐Ÿ”', - 'analyzer': '๐Ÿ“Š', - 'tester': '๐Ÿงช', - 'architect': '๐Ÿ—๏ธ', - 'reviewer': '๐Ÿ‘๏ธ' - }; - return icons[agentType.toLowerCase()] || '๐Ÿค–'; -} - -function generateOptimizationSuggestions(tokenData) { - const suggestions = []; - - // Analyze token usage patterns - if (tokenData.byAgent) { - Object.entries(tokenData.byAgent).forEach(([agentType, usage]) => { - const percentage = (usage / tokenData.total) * 100; - - if (percentage > 40) { - suggestions.push(`${agentType} agents: Consider prompt optimization (-15% potential)`); - } - if (percentage > 25) { - suggestions.push(`${agentType} agents: Implement response caching (-8% potential)`); - } - }); - } - - // General suggestions based on total usage - if (tokenData.total > 100000) { - suggestions.push('Consider using Claude Haiku for non-critical tasks (-90% cost)'); - } - - if (tokenData.output > tokenData.input * 2) { - suggestions.push('High output ratio: Consider more concise prompts'); - } - - return suggestions.length > 0 ? suggestions : ['Token usage is within optimal range']; -} - -async function generateTokenUsageReport(tokenData, agent) { - const reportsDir = path.join(process.cwd(), 'analysis-reports'); - - // Create reports directory if it doesn't exist - try { - await fs.mkdir(reportsDir, { recursive: true }); - } catch (err) { - // Directory might already exist - } - - const timestamp = Date.now(); - const reportPath = path.join(reportsDir, `token-usage-${timestamp}.csv`); - - // Generate CSV content - let csvContent = 'Timestamp,Agent,Input Tokens,Output Tokens,Total Tokens,Cost\\n'; - - if (tokenData.byAgent) { - Object.entries(tokenData.byAgent).forEach(([agentType, usage]) => { - const agentInput = Math.floor(usage * 0.6); // Estimate 60% input - const agentOutput = usage - agentInput; - const agentCost = calculateCost({ input: agentInput, output: agentOutput, total: usage }); - - csvContent += `${new Date().toISOString()},${agentType},${agentInput},${agentOutput},${usage},$${agentCost.total.toFixed(4)}\\n`; - }); - } else { - const cost = calculateCost(tokenData); - csvContent += `${new Date().toISOString()},${agent},${tokenData.input},${tokenData.output},${tokenData.total},$${cost.total.toFixed(4)}\\n`; - } - - // Write CSV file - await fs.writeFile(reportPath, csvContent); - - return reportPath; -} - async function showSimulatedTokenUsage(breakdown, costAnalysis) { // Show honest message about no data instead of fake numbers console.log(`\n๐Ÿ”ข TOKEN USAGE ANALYSIS:`);