fix: respect OPENAI_BASE_URL when resolving API key priority

When base_url points to a non-OpenRouter endpoint (e.g. Z.ai),
OPENROUTER_API_KEY incorrectly takes priority over OPENAI_API_KEY,
sending the wrong credentials. This causes 401 errors on the main
inference path and forces users to comment out OPENROUTER_API_KEY,
which then breaks auxiliary clients (compression, vision).

Fix: check whether base_url contains "openrouter" and swap the key
priority accordingly. Also adds GLM-4.7 and GLM-5 context lengths
to DEFAULT_CONTEXT_LENGTHS.
This commit is contained in:
Dev User 2026-03-05 08:22:29 +00:00
parent 2af2f148ab
commit 3221818b6e
2 changed files with 20 additions and 6 deletions

View file

@ -72,12 +72,24 @@ def _resolve_openrouter_runtime(
or OPENROUTER_BASE_URL
).rstrip("/")
api_key = (
explicit_api_key
or os.getenv("OPENROUTER_API_KEY")
or os.getenv("OPENAI_API_KEY")
or ""
)
# When base_url points to a non-OpenRouter endpoint (e.g. Z.ai, local LLM),
# prefer OPENAI_API_KEY so the correct credentials reach the correct provider.
# This allows OPENROUTER_API_KEY to coexist for auxiliary tasks (compression
# summaries, vision, session search) without hijacking main inference.
if base_url and "openrouter" not in base_url.lower():
api_key = (
explicit_api_key
or os.getenv("OPENAI_API_KEY")
or os.getenv("OPENROUTER_API_KEY")
or ""
)
else:
api_key = (
explicit_api_key
or os.getenv("OPENROUTER_API_KEY")
or os.getenv("OPENAI_API_KEY")
or ""
)
source = "explicit" if (explicit_api_key or explicit_base_url) else "env/config"