Skip to content

The agent that grows with you

fix(model): match alias ownership on the resolved provider id

teknium179d012bd2594
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index f6086f03a9e..b4652f8fa6d 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -737,7 +737,16 @@ def _ambiguous_alias_message(err: "AmbiguousAliasError") -> str:
f"Pick one with /model <exact-model-name>.")
-def resolve_alias(raw_input: str, current_provider: str) -> Optional[tuple[str, str, str]]:
+def _provider_identity(name: str, user_providers: Optional[dict] = None,
+ custom_providers: Optional[list] = None) -> str:
+ """Id a provider name routes to, e.g. ``custom:<name>`` for a legacy ``custom_providers``
+ entry, so an alias naming it by its bare name compares equal to the resolved provider."""
+ pdef = resolve_provider_full(name, user_providers, custom_providers) if name else None
+ return pdef.id if pdef is not None else normalize_provider(name or "")
+
+
+def resolve_alias(raw_input: str, current_provider: str, user_providers: Optional[dict] = None,
+ custom_providers: Optional[list] = None) -> Optional[tuple[str, str, str]]:
"""Resolve a short alias against the current provider's catalog.
Direct aliases (and reverse lookup by exact model id) win; then :data:`MODEL_ALIASES` is
@@ -756,10 +765,11 @@ def resolve_alias(raw_input: str, current_provider: str) -> Optional[tuple[str,
# different providers: prefer the one served by current_provider, since insertion order is
# not a routing decision and the wrong alias hands back another provider's base_url.
reverse_fallback: Optional[tuple[str, str, str]] = None
+ current_id = _provider_identity(current_provider, user_providers, custom_providers)
for alias_name, da in DIRECT_ALIASES.items():
if da.model.lower() != key:
continue
- if normalize_provider(da.provider or "") == normalize_provider(current_provider or ""):
+ if _provider_identity(da.provider, user_providers, custom_providers) == current_id:
return (da.provider, da.model, alias_name)
if reverse_fallback is None:
reverse_fallback = (da.provider, da.model, alias_name)
@@ -1237,7 +1247,7 @@ def _route_explicit_provider(st: _Switch) -> Optional[ModelSwitchResult]:
f"Specify the model explicitly: /model <model-name> --provider {st.explicit_provider}")
try:
- alias_result = resolve_alias(st.new_model, st.target_provider)
+ alias_result = resolve_alias(st.new_model, st.target_provider, st.user_providers, st.custom_providers)
except AmbiguousAliasError as err:
return st.fail(_ambiguous_alias_message(err), target_provider=st.target_provider)
if alias_result is not None:
@@ -1245,7 +1255,8 @@ def _route_explicit_provider(st: _Switch) -> Optional[ModelSwitchResult]:
# Adopt the alias (and with it its base_url and key) only when it belongs to the provider
# the user named: a reverse model-id match may land on another provider's alias, and
# honouring it would send the turn to that provider's endpoint under this one's identity.
- if normalize_provider(alias_provider or "") == normalize_provider(st.target_provider or ""):
+ if (_provider_identity(alias_provider, st.user_providers, st.custom_providers)
+ == _provider_identity(st.target_provider, st.user_providers, st.custom_providers)):
st.resolved_alias = alias_name
return None
diff --git a/tests/hermes_cli/test_ollama_cloud_auth.py b/tests/hermes_cli/test_ollama_cloud_auth.py
index 858714cda43..fe1d3416cc8 100644
--- a/tests/hermes_cli/test_ollama_cloud_auth.py
+++ b/tests/hermes_cli/test_ollama_cloud_auth.py
@@ -286,7 +286,7 @@ class TestSwitchModelDirectAliasOverride:
assert result.base_url == "http://localhost:11434/v1"
@staticmethod
- def _explicit_switch_to_provider_b(monkeypatch, aliases):
+ def _explicit_switch_to_provider_b(monkeypatch, aliases, explicit="provider-b", extra_cfg=""):
"""``/model shared-model --provider provider-b`` against a real config.yaml, with the
given direct aliases loaded. Only model validation is stubbed (no network)."""
import os
@@ -300,14 +300,17 @@ class TestSwitchModelDirectAliasOverride:
"model:\n provider: provider-a\n default: old-model\n"
"providers:\n"
" provider-a:\n base_url: https://api-a.example.com/v1\n"
- " provider-b:\n base_url: https://api-b.example.com/v1\n key_env: PROVIDER_B_KEY\n")
+ " provider-b:\n base_url: https://api-b.example.com/v1\n key_env: PROVIDER_B_KEY\n"
+ + extra_cfg)
monkeypatch.setattr(ms, "DIRECT_ALIASES", aliases)
monkeypatch.setattr("hermes_cli.models_validate.validate_requested_model",
lambda *a, **kw: {"accepted": True, "persist": True, "recognized": True, "message": None})
+ cfg = load_config()
return ms.switch_model(
"shared-model", "provider-a", "old-model",
current_base_url="https://api-a.example.com/v1", current_api_key="sk-provider-a",
- explicit_provider="provider-b", user_providers=load_config()["providers"])
+ explicit_provider=explicit, user_providers=cfg["providers"],
+ custom_providers=cfg.get("custom_providers"))
def test_explicit_provider_never_adopts_alias_bound_to_another_provider(self, monkeypatch):
"""An alias on another provider's endpoint that targets the same model id must not
@@ -340,3 +343,23 @@ class TestSwitchModelDirectAliasOverride:
assert result.resolved_via_alias == "b-alias"
assert result.base_url == "https://api-b.example.com/v2"
assert result.api_key != "sk-alias-host"
+
+ def test_explicit_provider_keeps_alias_owned_by_legacy_custom_provider(self, monkeypatch):
+ """A legacy ``custom_providers`` entry resolves to ``custom:<name>``; an alias that names
+ it by its bare name is still that provider's alias and keeps its own endpoint and key."""
+ from hermes_cli.model_switch import DirectAlias
+
+ result = self._explicit_switch_to_provider_b(monkeypatch, {
+ "a-alias": DirectAlias("shared-model", "provider-a", "https://alias-host.example.com/v1",
+ api_key="sk-alias-host"),
+ "corp-alias": DirectAlias("shared-model", "corp-llm", "https://corp.example.com/v2",
+ api_key="sk-corp-alias"),
+ }, explicit="corp-llm", extra_cfg=(
+ "custom_providers:\n - name: corp-llm\n"
+ " base_url: https://corp.example.com/v1\n api_key: sk-corp\n"))
+
+ assert result.success, result.error_message
+ assert result.target_provider == "custom:corp-llm"
+ assert result.resolved_via_alias == "corp-alias"
+ assert result.base_url == "https://corp.example.com/v2"
+ assert result.api_key == "sk-corp-alias"