feat: auto-detect provider from API key env vars - #53
Closed
allen-munsch wants to merge 1 commit into
Closed
Conversation
When a user sets an API key env var (e.g. DEEPSEEK_API_KEY) but doesn't explicitly configure a provider, dirge now auto-detects the matching provider instead of falling back to openrouter. Resolution order: CLI flag > config file > env var detection > openrouter Supports: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY Signed-off-by: allen-munsch <james.a.munsch@gmail.com>
allen-munsch
force-pushed
the
feat/auto-detect-provider-from-env
branch
from
May 20, 2026 22:46
a139130 to
02d46ee
Compare
yogthos
added a commit
that referenced
this pull request
May 21, 2026
…fix) (#95) * feat: auto-detect provider from API key env vars When a user sets an API key env var (e.g. DEEPSEEK_API_KEY) but doesn't explicitly configure a provider, dirge now auto-detects the matching provider instead of falling back to openrouter. Resolution order: CLI flag > config file > env var detection > openrouter Supports: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY Signed-off-by: allen-munsch <james.a.munsch@gmail.com> * test: replace env-var mutation with pure function + mock env lookup The original tests used `std::env::set_var` / `remove_var` which mutate process-wide state. Rust runs tests in parallel by default, so the tests raced under `cargo test` and 2 of 5 failed intermittently. Refactor: extract `auto_detect_provider_from(env_lookup)` as a pure helper. Production `auto_detect_provider()` passes `std::env::var`; tests pass a HashMap-backed closure via `mock_env(&[...])`. No process env mutation, no race. The candidate list moves to a module-level `PROVIDER_AUTODETECT_ORDER` so tests reference the same source of truth — adds an isolation test (`auto_detect_each_provider_in_isolation`) that exercises every entry, guarding against accidental drops or reorders. 6 tests now pass deterministically (was 5 flaky). --------- Signed-off-by: allen-munsch <james.a.munsch@gmail.com> Co-authored-by: allen-munsch <james.a.munsch@gmail.com> Co-authored-by: Yogthos <yogthos@gmail.com>
Collaborator
Collaborator
Author
|
🙏 Thank you |
allen-munsch
added a commit
to allen-munsch/dirge
that referenced
this pull request
Jun 3, 2026
…ed + test fix) (dirge-code#95) * feat: auto-detect provider from API key env vars When a user sets an API key env var (e.g. DEEPSEEK_API_KEY) but doesn't explicitly configure a provider, dirge now auto-detects the matching provider instead of falling back to openrouter. Resolution order: CLI flag > config file > env var detection > openrouter Supports: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY Signed-off-by: allen-munsch <james.a.munsch@gmail.com> * test: replace env-var mutation with pure function + mock env lookup The original tests used `std::env::set_var` / `remove_var` which mutate process-wide state. Rust runs tests in parallel by default, so the tests raced under `cargo test` and 2 of 5 failed intermittently. Refactor: extract `auto_detect_provider_from(env_lookup)` as a pure helper. Production `auto_detect_provider()` passes `std::env::var`; tests pass a HashMap-backed closure via `mock_env(&[...])`. No process env mutation, no race. The candidate list moves to a module-level `PROVIDER_AUTODETECT_ORDER` so tests reference the same source of truth — adds an isolation test (`auto_detect_each_provider_in_isolation`) that exercises every entry, guarding against accidental drops or reorders. 6 tests now pass deterministically (was 5 flaky). --------- Signed-off-by: allen-munsch <james.a.munsch@gmail.com> Co-authored-by: allen-munsch <james.a.munsch@gmail.com> Co-authored-by: Yogthos <yogthos@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hey! Small quality-of-life improvement here.
The problem: Setting
DEEPSEEK_API_KEY(or any provider's API key) still required explicitly passing--provider deepseekor configuring it — otherwise dirge would fall back to openrouter. Felt a bit fiddly.What this does: If no provider is set via CLI flag,
DIRGE_PROVIDERenv var, or config file, dirge now checks for known API key environment variables and picks the matching provider automatically.Resolution order is:
--providerCLI flag /DIRGE_PROVIDERenv varproviderin config file*_API_KEYenv vars (new!)"openrouter"Supported vars:
DEEPSEEK_API_KEY,OPENAI_API_KEY,ANTHROPIC_API_KEY,GEMINI_API_KEY,GLM_API_KEY,OLLAMA_API_KEY,OPENROUTER_API_KEYChanges:
src/provider.rs— newauto_detect_provider()+ 5 unit testssrc/cli.rs—resolve_provider()calls auto-detect as a fallbackcargo clippy is clean (no new warnings), cargo fmt passes, and all 5 new tests + all 497 existing non-plugin tests pass.
Thanks for the great project!
edit: written with dirge and deepseek v4 pro i looked at the code changes, looked okay