feat(providers): honor ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN#129
Open
kk7453603 wants to merge 1 commit into
Open
feat(providers): honor ANTHROPIC_BASE_URL + ANTHROPIC_AUTH_TOKEN#129kk7453603 wants to merge 1 commit into
kk7453603 wants to merge 1 commit into
Conversation
…nthropicProvider Make the Messages API endpoint and auth scheme env-overridable so watchmen works against any Anthropic-compatible proxy (z.ai, AWS Bedrock reverse-proxy, Azure Anthropic, internal gateways) without code changes. Changes to AnthropicProvider: - endpoint: class attr → property that reads ANTHROPIC_BASE_URL (default https://api.anthropic.com). Trailing slash tolerated. - headers(): when ANTHROPIC_AUTH_TOKEN is set, switch from x-api-key to Authorization: Bearer — matches Claude Code's env semantics so the same env that drives claude / pi.dev drives watchmen. - probe(): same env override for URL + auth, otherwise `watchmen settings api-key --check` would always hit api.anthropic.com and report proxy tokens as invalid. Env vars are read via config.read_env_var(), so they resolve from process env first, then ~/.config/watchmen/.env — same lookup path as every other watchmen env var. ClaudePro (subclass) keeps its hardcoded endpoint + OAuth headers: that flow always targets api.anthropic.com, env override would break it. Tests: - test_anthropic_headers_use_x_api_key: hardened — explicitly clears ANTHROPIC_AUTH_TOKEN so the test no longer flips auth schemes under env contamination (pre-existing latent bug). - test_anthropic_endpoint_defaults_to_api_anthropic_com: backward-compat assertion — when env unset, endpoint is unchanged. - test_anthropic_endpoint_honors_base_url_env: env override + trailing slash tolerance. - test_anthropic_headers_use_bearer_when_auth_token_set: Bearer path. - test_anthropic_probe_hits_env_base_url: probe honors env override for URL + auth. Patches httpx.get via monkeypatch (no module-swap leak). All 568 existing tests + 4 new ones pass. Verified end-to-end against z.ai's Anthropic-compatible endpoint: ANTHROPIC_BASE_URL=https://api.z.ai/api/anthropic \ watchmen settings api-key --provider anthropic --set <zai-token> → ✓ valid · 8 models accessible
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.
What
Adds env-var overrides to
AnthropicProviderso watchmen works against anyAnthropic-compatible proxy without code changes.
ANTHROPIC_BASE_URL— redirect the Messages API endpoint (z.ai,Bedrock reverse-proxy, Azure Anthropic, internal gateways).
ANTHROPIC_AUTH_TOKEN— switch auth fromx-api-keytoAuthorization: Bearer(matches Claude Code's env semantics).Env vars are read via
config.read_env_var()— process env first, then~/.config/watchmen/.env, same lookup path as every other watchmen env var.Mirrors the official anthropic SDK's contract, so the same env that drives
claude/pi.devdrives watchmen.ClaudePro(subclass) is intentionally untouched: its OAuth flow alwaystargets
api.anthropic.com, an env override would break it.Why
Users behind Anthropic-compatible proxies currently have to maintain a fork
just to change two hardcoded URLs (
providers.py:294,providers.py:441)and the auth header scheme. The env-var contract is the standard pattern —
the anthropic SDK, Claude Code, LangChain, and most third-party tools honor
these two variables.
Concrete use case: z.ai's Coding Plan gives Anthropic-API-compatible access
to GLM models at
https://api.z.ai/api/anthropic. Verified end-to-end:Changes
src/watchmen/providers.py:endpoint: class attr →@propertyreadingANTHROPIC_BASE_URL(default unchanged).
headers(): bearer-path whenANTHROPIC_AUTH_TOKENis set.probe(): same env override for URL + auth.tests/test_providers.py:test_anthropic_headers_use_x_api_key: hardened — explicitly clearsANTHROPIC_AUTH_TOKENso the test no longer leaks env contamination(pre-existing latent bug independent of this PR).
Backward compatibility
No behavior change when env vars are unset. All 568 existing tests pass.
Existing
ANTHROPIC_API_KEY-only installs keep working as before.Test plan