-
Notifications
You must be signed in to change notification settings - Fork 0
fix: friendly onboarding when API key missing #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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!(); | ||
|
Comment on lines
+225
to
+248
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| std::process::exit(1); | ||
|
Comment on lines
+216
to
+249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve machine-readable failures when
⚙️ Minimal guard |
||
| } | ||
| return Err(e.into()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| }; | ||
|
|
||
| // Wrap with fallback provider if configured | ||
| let provider: Box<dyn mc_core::LlmProvider> = | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check
err_str.contains("API_KEY")is too broad. It will likely trigger the onboarding message for other errors, such as an invalid or expired key (e.g.,[MC-E003] api error 401: Invalid API_KEY), which should be shown as a standard error to the user rather than a welcome message. Relying on the specific error codeMC-E001or the variant nameMissingApiKeyis more robust.