The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cli): probe model.base_url in the model picker catalog (#121387)
ahisblessed9d7536d803b7
diff --git a/hermes_cli/models.py b/hermes_cli/models.pyindex 4fa57de4f40..7c40d055be9 100644--- a/hermes_cli/models.py+++ b/hermes_cli/models.py@@ -1600,6 +1600,48 @@ def _drop_delisted_opencode_models(normalized: str, rows: Optional[list[str]]) -return rows+def _configured_relay_base_url(provider: str) -> str:+ """``model.base_url`` when it points the *configured* provider at a relay/proxy, else "".++ Discovery must probe the same endpoint inference uses (#121387): with ``model.base_url``+ set for the configured provider, the vendor's canonical host is NOT the catalog to list.+ Mirrors the ``$OPENAI_BASE_URL`` -> ``model.base_url`` -> canonical precedence of+ ``_openai_discovery_base_url`` for every built-in provider, not just OpenAI.+ """+ try:+ model_cfg = _get_model_config_dict()+ except Exception:+ return ""+ cfg_provider = str(model_cfg.get("provider") or "").strip().lower()+ if not cfg_provider or not provider:+ return ""+ try:+ if normalize_provider(provider) != normalize_provider(cfg_provider):+ return ""+ except Exception:+ return ""+ return str(model_cfg.get("base_url") or "").strip().rstrip("/")+++def _relay_model_catalog(normalized: str, relay: str) -> Optional[list[str]]:+ """Live catalog probed at a configured ``model.base_url`` relay, or None to fall through.++ Returns only the relay's live ids (no curated merge): a relay user must see the relay's+ catalog, and a failed/empty probe degrades to the canonical fetchers untouched.+ """+ try:+ from providers import get_provider_profile++ profile = get_provider_profile(normalized)+ if profile is None or getattr(profile, "auth_type", "") != "api_key":+ return None+ api_key, _ = _api_key_credentials(normalized)+ live = profile.fetch_models(api_key=api_key, base_url=relay)+ return [str(m) for m in (live or []) if m] or None+ except Exception:+ return None++def provider_model_ids(provider: Optional[str], *, force_refresh: bool = False) -> list[str]:"""Best known model catalog for a provider: per-provider live fetchers, then the generic profilefetch, then the static list (merged with models.dev for ``_MODELS_DEV_PREFERRED`` providers)."""@@ -1608,6 +1650,14 @@ def provider_model_ids(provider: Optional[str], *, force_refresh: bool = False)return _ollama_local_catalog(force_refresh)normalized = normalize_provider(provider)+ # A configured `model.base_url` relay wins over the canonical per-provider fetchers: the+ # picker must list what the configured endpoint serves and must not touch the vendor+ # host (#121387). Any probe failure falls through to the existing canonical path.+ relay = _configured_relay_base_url(provider)+ if relay:+ relayed = _relay_model_catalog(normalized, relay)+ if relayed:+ return relayedfetcher = _PROVIDER_CATALOG_FETCHERS.get(normalized)if fetcher is not None:models = fetcher(normalized, force_refresh)diff --git a/tests/hermes_cli/test_models_relay_base_url.py b/tests/hermes_cli/test_models_relay_base_url.pynew file mode 100644index 00000000000..134a3f6e1fe--- /dev/null+++ b/tests/hermes_cli/test_models_relay_base_url.py@@ -0,0 +1,91 @@+"""`model.base_url` relays must be probed instead of the vendor's canonical host (#121387)."""++from types import SimpleNamespace++import hermes_cli.models as models+++class _RecordingProfile:+ auth_type = "api_key"+ fallback_models = []++ def __init__(self):+ self.calls = []++ def fetch_models(self, api_key=None, base_url=None):+ self.calls.append((api_key, base_url))+ return ["relay-only-model"]+++def test_relay_base_url_is_probed_for_configured_provider(monkeypatch):+ monkeypatch.setattr(+ models,+ "_get_model_config_dict",+ lambda: {+ "provider": "deepseek",+ "base_url": "http://127.0.0.1:9001/deepseek/v1",+ },+ )+ profile = _RecordingProfile()+ import providers++ monkeypatch.setattr(providers, "get_provider_profile", lambda name: profile)+ monkeypatch.setattr(models, "_api_key_credentials", lambda name: (None, None))+ monkeypatch.setattr(+ models,+ "_PROVIDER_CATALOG_FETCHERS",+ {+ "deepseek": lambda n, f: (_ for _ in ()).throw(+ AssertionError("canonical host touched")+ )+ },+ )++ assert models.provider_model_ids("deepseek", force_refresh=True) == [+ "relay-only-model"+ ]+ assert profile.calls == [(None, "http://127.0.0.1:9001/deepseek/v1")]+++def test_base_url_for_a_different_provider_is_ignored(monkeypatch):+ monkeypatch.setattr(+ models,+ "_get_model_config_dict",+ lambda: {+ "provider": "deepseek",+ "base_url": "http://127.0.0.1:9001/deepseek/v1",+ },+ )+ assert models._configured_relay_base_url("openai") == ""+++def test_failed_relay_probe_falls_through_to_canonical_fetchers(monkeypatch):+ monkeypatch.setattr(+ models,+ "_get_model_config_dict",+ lambda: {+ "provider": "deepseek",+ "base_url": "http://127.0.0.1:9001/deepseek/v1",+ },+ )++ def _boom(**kwargs):+ raise RuntimeError("relay down")++ import providers++ monkeypatch.setattr(+ providers,+ "get_provider_profile",+ lambda name: SimpleNamespace(+ auth_type="api_key", fetch_models=_boom, fallback_models=[]+ ),+ )+ monkeypatch.setattr(models, "_api_key_credentials", lambda name: (None, None))+ monkeypatch.setattr(+ models,+ "_PROVIDER_CATALOG_FETCHERS",+ {"deepseek": lambda n, f: ["canonical-model"]},+ )++ assert models.provider_model_ids("deepseek") == ["canonical-model"]