feat: If an apiKey is provided, skip Oauth handling.
#5717
+40
−0
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.
Context
My situation is this:
provider.anthropic.options.apiKey.Using fake "profiles" (individually managed custom config files), I'd like to be able to use
opencodewith either type.Expected
When configured to use the
Anthropic Claudeprovider, if an (enterprise)apiKeyis provided, it should take precedence over the stored Oauth (Claude Code Pro) credentials. If not provided,opencodeshould use the Oauth credentials.Actual
Because of the way the configurations merging happens, the Oauth credentials (from
~/.local/share/opencode/auth.json) always "win", and the manually setapiKeyis ignored.This means all usage, even when I provide an enterprise API key, always ends up pointed at my personal Claude Pro subscription.
Solution
If an
apiKeyis configured & present, assume the user knows better & skip the loading of the Oauth credentials.Deeper Explanation of Setup/Situation
~/.config/opencode/my_profiles/<profile-name>.jsonc.provider.anthropic.options.apiKeypath, I supply{file:~/.secrets/<profile-name>.apikey}, with different key paths for each work config.opencodewith:OPENCODE_CONFIG=~/.config/opencode/my_profiles/<profile-name>.jsonc opencode.~/.local/share/opencode/auth.jsonaway) This works great for all the different clients/keys... but because that file is gone, I can no longer use my personal subscription (e.g.OPENCODE_CONFIG=~/.config/opencode/my_profiles/personal.jsonc opencode).~/.local/share/opencode/auth.jsonback) My personal Claude Pro subscription is back, but now takes over everything, & the client API keys are never used.This adds a small skip (non-specific to Anthropic/Claude) to the provider code, only if an
apiKeyis set/present. It includes a new passing test that demonstrates the behavior.