UNPKG

cost-claude

Version:

Claude Code cost monitoring, analytics, and optimization toolkit

82 lines 3.75 kB
import chalk from 'chalk'; import { logger } from '../../utils/logger.js'; import { BudgetManager } from '../../services/budget-manager.js'; import { NotificationService } from '../../services/notification.js'; export async function budgetCommand(options) { try { const budgetManager = BudgetManager.getInstance(); const notificationService = new NotificationService(); budgetManager.on('budget-alert', (alert) => { console.log('\n' + alert.message); notificationService.notify({ title: '💰 Budget Alert', message: alert.message }); }); budgetManager.on('budget-exceeded', (data) => { console.log('\n' + chalk.red(data.message)); notificationService.notify({ title: '🚨 Budget Exceeded!', message: data.message, sound: true }); }); if (options.set && options.type) { const amount = parseFloat(options.set); if (isNaN(amount) || amount <= 0) { console.error(chalk.red('Invalid budget amount')); process.exit(1); } budgetManager.setBudget(options.type, amount); console.log(chalk.green(`✅ ${options.type} budget set to $${amount.toFixed(2)}`)); } if (options.alerts) { const thresholds = options.alerts.split(',').map(t => parseFloat(t.trim())); if (thresholds.some(t => isNaN(t) || t < 0 || t > 100)) { console.error(chalk.red('Invalid alert thresholds. Use comma-separated percentages (0-100)')); process.exit(1); } budgetManager.setAlertThresholds(thresholds); console.log(chalk.green(`✅ Alert thresholds set to: ${thresholds.join('%, ')}%`)); } if (options.reset) { if (options.type) { budgetManager.reset(options.type); console.log(chalk.green(`✅ ${options.type} budget usage reset`)); } else { budgetManager.reset(); console.log(chalk.green('✅ All period budgets reset')); } } if (options.recalculate) { const projectPath = options.path || '~/.claude/projects'; const expandedPath = projectPath.replace('~', process.env.HOME || ''); console.log(chalk.blue('Recalculating budget usage from project files...')); await budgetManager.calculateCurrentPeriodUsage(expandedPath); console.log(chalk.green('✅ Budget usage recalculated')); } if (options.status || (!options.set && !options.alerts && !options.reset && !options.recalculate)) { const status = budgetManager.getStatus(); const report = budgetManager.exportReport(); console.log('\n' + report); const recommendations = []; if (status.daily.limit === 0) { recommendations.push('💡 Set a daily budget with: cost-claude budget --set 10 --type daily'); } if (status.monthly.percentage > 80 && status.monthly.limit > 0) { recommendations.push('⚠️ You\'re approaching your monthly budget limit'); } if (recommendations.length > 0) { console.log('\n' + chalk.bold('Recommendations:')); recommendations.forEach(rec => console.log(' ' + rec)); } } } catch (error) { logger.error('Budget command failed:', error); console.error(chalk.red('Error:'), error.message); process.exit(1); } } //# sourceMappingURL=budget.js.map