capsule-ai-cli
Version:
The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing
59 lines (58 loc) ⢠2.54 kB
JavaScript
import { contextManager } from '../../services/context.js';
import { stateService } from '../../services/state.js';
import chalk from 'chalk';
export const compactCommand = {
name: 'compact',
description: 'Compact conversation history',
async execute() {
const stats = contextManager.getContextStats();
const model = stateService.getModel();
const limit = contextManager.getTokenLimit(model);
const percentage = Math.round((stats.tokenCount / limit) * 100);
if (stats.messageCount <= 10) {
return {
success: false,
message: `${chalk.bold('đď¸ Compacting Conversation History')}
Current usage: ${stats.tokenCount.toLocaleString()}/${(limit / 1000).toFixed(0)}k tokens (${percentage}%)
â ď¸ Too few messages to compact (need more than 10 messages)`
};
}
try {
const compactResult = await contextManager.compactManuallyAI();
if (compactResult.success) {
const newStats = contextManager.getContextStats();
const newPercentage = Math.round((newStats.tokenCount / limit) * 100);
return {
success: true,
message: `â Compacted ${compactResult.messagesCompacted} messages
Saved ${compactResult.tokensSaved?.toLocaleString()} tokens
New usage: ${newStats.tokenCount.toLocaleString()}/${(limit / 1000).toFixed(0)}k tokens (${newPercentage}%)`
};
}
else {
return {
success: false,
message: `â ď¸ ${compactResult.message}`
};
}
}
catch (error) {
const compactResult = contextManager.compactManually();
if (compactResult.success) {
const newStats = contextManager.getContextStats();
const newPercentage = Math.round((newStats.tokenCount / limit) * 100);
return {
success: true,
message: `â Compacted ${compactResult.messagesCompacted} messages (simple mode)
Saved ${compactResult.tokensSaved?.toLocaleString()} tokens
New usage: ${newStats.tokenCount.toLocaleString()}/${(limit / 1000).toFixed(0)}k tokens (${newPercentage}%)`
};
}
return {
success: false,
message: `â ď¸ ${compactResult.message}`
};
}
}
};
//# sourceMappingURL=compact.js.map