From bda4d3b68d0e62b9c81086e3dbc2a4c969478946 Mon Sep 17 00:00:00 2001 From: Kien Bui Date: Tue, 14 Apr 2026 11:44:22 +0000 Subject: [PATCH] fix: friendly onboarding when API key missing Before: cryptic 'Error: set ANTHROPIC_API_KEY [MC-E001]' After: welcome box with setup instructions for all providers Shows: - Anthropic, OpenAI, Gemini, Groq, OpenRouter examples - Local model option (ollama, no key needed) - Persist hint (~/.bashrc) - Docs link --- mc/crates/mc-cli/src/main.rs | 41 +++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/mc/crates/mc-cli/src/main.rs b/mc/crates/mc-cli/src/main.rs index 7482b44..f76324d 100644 --- a/mc/crates/mc-cli/src/main.rs +++ b/mc/crates/mc-cli/src/main.rs @@ -211,7 +211,46 @@ fn main() -> Result<()> { &config.provider_config, cli.base_url.as_deref(), cli.api_key.as_deref(), - )?; + ); + + let primary = match primary { + Ok(p) => p, + Err(e) => { + let err_str = format!("{e:?}"); // Debug format includes full chain + if err_str.contains("MC-E001") + || err_str.contains("missing API key") + || err_str.contains("MissingApiKey") + || err_str.contains("API_KEY") + { + eprintln!(); + eprintln!(" ╭─────────────────────────────────────────╮"); + eprintln!(" │ Welcome to magic-code! 🚀 │"); + eprintln!(" ╰─────────────────────────────────────────╯"); + eprintln!(); + eprintln!(" To get started, set an API key for your LLM provider:"); + eprintln!(); + eprintln!(" # Anthropic (default)"); + eprintln!(" export ANTHROPIC_API_KEY=\"sk-ant-...\""); + eprintln!(); + eprintln!(" # Or use another provider:"); + eprintln!(" export OPENAI_API_KEY=\"sk-...\" # then: magic-code --provider openai"); + eprintln!(" export GEMINI_API_KEY=\"...\" # then: magic-code --provider gemini"); + eprintln!( + " export GROQ_API_KEY=\"gsk_...\" # then: magic-code --provider groq" + ); + eprintln!(" export OPENROUTER_API_KEY=\"sk-or-...\" # then: magic-code --provider openrouter"); + eprintln!(); + eprintln!(" # Or use a local model (no API key needed):"); + eprintln!(" magic-code --provider ollama --model llama3"); + eprintln!(); + eprintln!(" Add to ~/.bashrc or ~/.zshrc to persist."); + eprintln!(" Docs: https://github.com/kienbui1995/mc-code#install"); + eprintln!(); + std::process::exit(1); + } + return Err(e.into()); + } + }; // Wrap with fallback provider if configured let provider: Box =