fix(providers): honor a custom base_url over models_url in fetch_models

Follow-up to the salvaged CommandCode signature fix: accepting base_url
but ignoring it left custom endpoints (user-configured model.base_url /
COMMANDCODE_BASE_URL proxies) fetching the public catalog instead of the
configured one. Reviewer dansigma flagged this on PR #88851.

Class-wide fix, not a CommandCode patch:

- providers/base.py: a caller base_url that DIFFERS from the profile's
  default now wins over models_url. Equality with the default means "not
  customised" (callers pass base_url unconditionally, defaulting to the
  profile's own URL) and keeps models_url as the endpoint, preserving the
  OpenRouter-style split-catalog behavior.
- commandcode: _fetch_commandcode_models() takes the endpoint override;
  both profile overrides forward base_url.
- Tests: base-class precedence (custom beats models_url, default does
  not), CommandCode redirect via live local HTTP server incl. claude-*
  filter, and default-echo hitting the canonical endpoint. All verified
  to fail against the pre-fix implementation (sabotage run).
This commit is contained in:
Teknium
2026-08-18 12:51:18 -07:00
parent 7072fc4f87
commit c7d0f6c35f
5 changed files with 166 additions and 11 deletions
@@ -47,14 +47,26 @@ _COMMANDCODE_ANTHROPIC_ENV = ("COMMANDCODE_API_KEY", "COMMANDCODE_ANTHROPIC_BASE
def _fetch_commandcode_models(
timeout: float = 10.0,
base_url: str | None = None,
) -> list[str] | None:
"""Fetch the live model list from the CommandCode /models endpoint.
Returns a flat list of model IDs or None on failure.
No auth required — the public models endpoint is open.
``base_url`` overrides the endpoint only when the caller passed a URL
that differs from the default ``_COMMANDCODE_BASE`` (a user-configured
``model.base_url`` / ``COMMANDCODE_BASE_URL`` pointing at a proxy or
custom deployment). The picker passes base_url unconditionally, falling
back to the profile default — equality means "not customised".
"""
caller_base = (base_url or "").strip()
if caller_base and caller_base.rstrip("/") != _COMMANDCODE_BASE.rstrip("/"):
models_url = caller_base.rstrip("/") + "/models"
else:
models_url = _COMMANDCODE_MODELS_URL
try:
req = urllib.request.Request(_COMMANDCODE_MODELS_URL)
req = urllib.request.Request(models_url)
req.add_header("Accept", "application/json")
req.add_header("User-Agent", _profile_user_agent())
with urllib.request.urlopen(req, timeout=timeout) as resp:
@@ -83,7 +95,7 @@ class CommandCodeProfile(ProviderProfile):
timeout: float = 8.0,
) -> list[str] | None:
"""Fetch from the public CommandCode /models endpoint."""
return _fetch_commandcode_models(timeout=timeout)
return _fetch_commandcode_models(timeout=timeout, base_url=base_url)
commandcode = CommandCodeProfile(
@@ -134,7 +146,7 @@ class CommandCodeAnthropicProfile(ProviderProfile):
Filter to Anthropic-family models only (claude-*).
"""
all_models = _fetch_commandcode_models(timeout=timeout)
all_models = _fetch_commandcode_models(timeout=timeout, base_url=base_url)
if all_models is None:
return None
return [m for m in all_models if m.startswith("claude-")]