feat(tui): add unified command parser with model switching

This commit is contained in:
William Valentin
2026-02-05 10:52:49 -08:00
parent da2bb57488
commit 435146344e
2 changed files with 145 additions and 0 deletions
+85
View File
@@ -0,0 +1,85 @@
export type Command =
| { type: 'quit' }
| { type: 'reset' }
| { type: 'help' }
| { type: 'status' }
| { type: 'fullscreen' }
| { type: 'model'; name?: string }
| { type: 'transfer'; target: string }
| { type: 'message'; content: string };
export function parseCommand(input: string): Command | null {
const trimmed = input.trim();
if (!trimmed) return null;
// Quit
if (trimmed === '/quit' || trimmed === '/exit') {
return { type: 'quit' };
}
// Reset
if (trimmed === '/reset' || trimmed === '/clear') {
return { type: 'reset' };
}
// Help
if (trimmed === '/help' || trimmed === '/?') {
return { type: 'help' };
}
// Status
if (trimmed === '/status') {
return { type: 'status' };
}
// Fullscreen
if (trimmed === '/fullscreen' || trimmed === '/fs') {
return { type: 'fullscreen' };
}
// Model (with optional argument)
if (trimmed === '/model') {
return { type: 'model' };
}
if (trimmed.startsWith('/model ')) {
const name = trimmed.slice('/model '.length).trim();
return { type: 'model', name };
}
// Transfer
if (trimmed.startsWith('/transfer ')) {
const target = trimmed.slice('/transfer '.length).trim();
return { type: 'transfer', target };
}
// Regular message
return { type: 'message', content: trimmed };
}
export function getHelpText(): string {
return `
Commands:
/help, /? Show this help
/model [name] Show or switch model (local, default, fast, complex)
/reset, /clear Clear conversation history
/status Show session info and token usage
/fullscreen, /fs Switch to fullscreen mode
/transfer <dest> Transfer session to another frontend
/quit, /exit Exit TUI
`.trim();
}
export type ModelAlias = 'local' | 'default' | 'fast' | 'complex' | 'opus' | 'sonnet' | 'ollama';
export function resolveModelAlias(alias: string): 'local' | 'default' | 'fast' | 'complex' {
const map: Record<string, 'local' | 'default' | 'fast' | 'complex'> = {
local: 'local',
ollama: 'local',
default: 'default',
opus: 'default',
fast: 'fast',
sonnet: 'fast',
complex: 'complex',
};
return map[alias.toLowerCase()] ?? 'default';
}