Skip to content

feat: auto-detect provider from API key env vars - #53

Closed
allen-munsch wants to merge 1 commit into
dirge-code:mainfrom
allen-munsch:feat/auto-detect-provider-from-env
Closed

feat: auto-detect provider from API key env vars#53
allen-munsch wants to merge 1 commit into
dirge-code:mainfrom
allen-munsch:feat/auto-detect-provider-from-env

Conversation

@allen-munsch

@allen-munsch allen-munsch commented May 20, 2026

Copy link
Copy Markdown
Collaborator

Hey! Small quality-of-life improvement here.

The problem: Setting DEEPSEEK_API_KEY (or any provider's API key) still required explicitly passing --provider deepseek or 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_PROVIDER env var, or config file, dirge now checks for known API key environment variables and picks the matching provider automatically.

Resolution order is:

  1. --provider CLI flag / DIRGE_PROVIDER env var
  2. provider in config file
  3. Auto-detect from *_API_KEY env vars (new!)
  4. Default to "openrouter"

Supported vars: DEEPSEEK_API_KEY, OPENAI_API_KEY, ANTHROPIC_API_KEY, GEMINI_API_KEY, GLM_API_KEY, OLLAMA_API_KEY, OPENROUTER_API_KEY

Changes:

  • src/provider.rs — new auto_detect_provider() + 5 unit tests
  • src/cli.rsresolve_provider() calls auto-detect as a fallback

cargo 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

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
allen-munsch force-pushed the feat/auto-detect-provider-from-env branch from a139130 to 02d46ee Compare May 20, 2026 22:46
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>
@yogthos

yogthos commented May 21, 2026

Copy link
Copy Markdown
Collaborator

Closing in favor of #95, which rebases this onto current main and addresses test flakiness — the original tests mutated process-wide env vars and raced under parallel cargo test. Refactored to a pure helper with mock env lookup. You're credited as co-author on #95. Thanks for the contribution!

@yogthos yogthos closed this May 21, 2026
@allen-munsch

Copy link
Copy Markdown
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants