mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-11-24 14:16:34 +09:00
🔖 Checkpoint: Edit /workspaces/claude-code-flow/src/cli/simple-commands/analysis.js
This commit is contained in:
1 parent
1cb0966e9f
commit
08e15e5269
3 files changed
+2
-164
No files matched your search
@@ -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"}
|
||||
@@ -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(-) "}
|
||||
@@ -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:`);
|
||||
|
||||
Reference in new issue
Block a user