capsule-ai-cli
Version:
The AI Model Orchestrator - Intelligent multi-model workflows with device-locked licensing
139 lines • 5.42 kB
JavaScript
import { EventEmitter } from 'events';
export class CommandProcessor extends EventEmitter {
commands = new Map();
constructor() {
super();
this.initializeCommands();
}
initializeCommands() {
const defaultCommands = [
{ cmd: '/help', desc: 'Show available commands', handler: 'help' },
{ cmd: '/exit', desc: 'Exit Capsule CLI', handler: 'exit' },
{ cmd: '/clear', desc: 'Clear the screen', handler: 'clear' },
{ cmd: '/model', desc: 'Change AI model', handler: 'model' },
{ cmd: '/provider', desc: 'Change AI provider', handler: 'provider' },
{ cmd: '/keys', desc: 'Manage API keys', handler: 'keys' },
{ cmd: '/chats', desc: 'List and switch between previous chats', handler: 'chats' },
{ cmd: '/new', desc: 'Start a new chat', handler: 'new' },
{ cmd: '/context', desc: 'Show current context stats', handler: 'context' },
{ cmd: '/compact', desc: 'Compact conversation history', handler: 'compact' },
{ cmd: '/stats', desc: 'Show usage statistics', handler: 'stats' },
{ cmd: '/cost', desc: 'Show total cost across all chats', handler: 'cost' },
{ cmd: '/auth', desc: 'Authenticate with Capsule', handler: 'auth' },
{ cmd: '/status', desc: 'Show auth & usage status', handler: 'status' },
{ cmd: '/logout', desc: 'Log out from Capsule', handler: 'logout' }
];
defaultCommands.forEach(cmd => {
this.commands.set(cmd.cmd, cmd);
});
}
getCommands() {
return Array.from(this.commands.values()).map(({ cmd, desc }) => ({ cmd, desc }));
}
isCommand(input) {
return input.startsWith('/');
}
parseCommand(input) {
const parts = input.trim().split(/\s+/);
const command = parts[0].toLowerCase();
const args = parts.slice(1);
return { command, args };
}
validateCommand(command) {
return this.commands.has(command);
}
async processCommand(input) {
if (!this.isCommand(input)) {
return {
success: false,
message: 'Not a command'
};
}
const { command, args } = this.parseCommand(input);
if (command === '/quit') {
return this.processCommand('/exit');
}
if (!this.validateCommand(command)) {
return {
success: false,
message: `Unknown command: ${command}. Type /help for available commands.`
};
}
const cmdConfig = this.commands.get(command);
if (!cmdConfig) {
return {
success: false,
message: 'Command configuration not found'
};
}
this.emit('command', {
command,
args,
handler: cmdConfig.handler
});
return {
success: true,
action: cmdConfig.handler,
data: { command, args }
};
}
getHelpText() {
const lines = [
'📚 Available Commands:',
''
];
const maxCmdLength = Math.max(...Array.from(this.commands.values()).map(c => c.cmd.length));
this.commands.forEach(cmd => {
const paddedCmd = cmd.cmd.padEnd(maxCmdLength + 2);
lines.push(` ${paddedCmd} - ${cmd.desc}`);
});
lines.push('');
lines.push('Keyboard Shortcuts:');
lines.push(' ↑/↓ - Navigate command history');
lines.push(' Tab - Auto-complete commands');
lines.push(' Shift+Tab - Toggle between modes');
lines.push(' Ctrl+R - Toggle recent tool calls panel');
lines.push(' ESC - Clear current input');
lines.push(' ESC×2 - Go back to previous message');
lines.push(' Enter×2 - Send multi-line input');
lines.push(' Ctrl+C - Exit');
return lines.join('\n');
}
addCommand(command, description, handler) {
this.commands.set(command, {
cmd: command,
desc: description,
handler: handler || command.substring(1)
});
}
removeCommand(command) {
return this.commands.delete(command);
}
requiresInput(command) {
const inputRequiredCommands = ['/auth'];
return inputRequiredCommands.includes(command);
}
getCommandDescription(command) {
const cmd = this.commands.get(command);
return cmd ? cmd.desc : null;
}
suggestCommands(partial) {
if (!partial.startsWith('/')) {
return [];
}
const search = partial.toLowerCase();
return this.getCommands().filter(cmd => cmd.cmd.toLowerCase().startsWith(search));
}
getCommandUsage(command) {
const usageInfo = {
'/auth': '/auth [email] - Authenticate with optional email',
'/model': '/model - Select from available models',
'/provider': '/provider - Select from available providers',
'/chats': '/chats - List and switch between chats',
'/keys': '/keys - Configure API keys for providers'
};
return usageInfo[command] || null;
}
}
export const commandProcessor = new CommandProcessor();
//# sourceMappingURL=command-processor.js.map