AI News Hub Logo

AI News Hub

One Input Box, Two AI Modes — Detecting Whether the User Wants Error Help or Command Explanation

DEV Community
hiyoyo

If this is useful, a ❤️ helps others find it. All tests run on an 8-year-old MacBook Air. HiyokoHelper has one input box but handles two completely different use cases: Error diagnosis — "Why did this fail and how do I fix it?" Command explanation — "What does this command actually do?" The user doesn't select a mode. The app detects which one they need. pub enum InputMode { ErrorDiagnosis, CommandExplain, } pub fn detect_mode(input: &str) -> InputMode { let trimmed = input.trim(); let lines: Vec = trimmed.lines().collect(); if lines.len() == 1 { let line = lines[0]; let command_prefixes = [ "sudo ", "git ", "npm ", "yarn ", "cargo ", "brew ", "pip ", "docker ", "kubectl ", "ls ", "cd ", "mv ", "cp ", "chmod ", "systemctl ", "launchctl ", ]; let looks_like_command = command_prefixes.iter() .any(|p| line.starts_with(p)) || line.starts_with("$ ") || line.starts_with("% "); if looks_like_command { return InputMode::CommandExplain; } } InputMode::ErrorDiagnosis } pub fn build_prompt(input: &str, mode: &InputMode, lang: &Lang) -> String { match (mode, lang) { (InputMode::ErrorDiagnosis, Lang::Japanese) => format!( "あなたはターミナルエラーの専門家です。\ 以下のエラーの原因を1〜2文で説明し、\ 解決コマンドがあれば示してください。\n\n{}", input ), (InputMode::CommandExplain, Lang::Japanese) => format!( "あなたはターミナルコマンドの先生です。\ 以下のコマンドが何をするか初心者向けに説明し、\ 危険な副作用があれば警告してください。\n\n{}", input ), (InputMode::ErrorDiagnosis, Lang::English) => format!( "You are a terminal error specialist. \ Explain the cause in 1-2 sentences and provide a fix.\n\n{}", input ), (InputMode::CommandExplain, Lang::English) => format!( "You are a terminal command teacher. \ Explain what this command does for a beginner. \ Warn about dangerous side effects.\n\n{}", input ), } } const mode = detectMode(inputText); {mode === 'error' ? '🔍 エラー診断モード' : '📖 コマンド解説モード'} Updates live as they type. No confusion about what will happen on submit. HiyokoHelper (OSS) → github.com/hiyoyok/HiyokoHelper @hiyoyok