The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): save a new endpoint from the Local / custom endpoint wizard when a named custom provider exists
Teknium6140f2dec94a
diff --git a/hermes_cli/web_server_config.py b/hermes_cli/web_server_config.pyindex bcc48d7f8fe..4c4b453f32f 100644--- a/hermes_cli/web_server_config.py+++ b/hermes_cli/web_server_config.py@@ -476,6 +476,12 @@ def _normalize_main_model_assignment(provider: str, model: str) -> tuple[str, streturn prov_in, model_in+def _is_explicit_custom_endpoint(provider: str, base_url: str) -> bool:+ """A bare ``custom`` / ``local`` pick that carries its own endpoint URL defines a route; it is+ never a reference to an already-configured provider."""+ return provider.strip().lower() in {"custom", "local"} and bool((base_url or "").strip())++def _validated_main_model_selection(cfg: dict, provider: str, model: str, base_url: str = "", api_key: str = "") -> "ModelSwitchResult":@@ -488,13 +494,16 @@ def _validated_main_model_selection(model_cfg = cfg.get("model") if isinstance(cfg.get("model"), dict) else {}is_bare_custom = provider.strip().lower() in {"custom", "local"}+ # An explicit endpoint must not resolve through the named custom providers: the legacy+ # bare-``custom`` recovery in ``resolve_custom_provider`` would bind the FIRST named entry.+ explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)result = switch_model(raw_input=model, explicit_provider=provider, is_global=True,current_provider=str(model_cfg.get("provider") or ""), current_model=str(model_cfg.get("default") or ""),current_base_url=base_url if is_bare_custom else str(model_cfg.get("base_url") or ""),current_api_key=api_key if is_bare_custom else "",user_providers=cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {},- custom_providers=get_compatible_custom_providers(cfg))+ custom_providers=[] if explicit_endpoint else get_compatible_custom_providers(cfg))if not result.success:raise HTTPException(status_code=400, detail=result.error_message or "model switch rejected")if is_bare_custom and base_url.strip():@@ -767,7 +776,12 @@ def _prepare_main_assignment(cfg: dict, provider: str, model: str, base_url: str``_CONFIG_MUTATION_LOCK``; it writes nothing."""if not provider or not model:raise HTTPException(status_code=400, detail="provider and model required for main")- provider, model = _normalize_main_model_assignment(provider, model)+ if _is_explicit_custom_endpoint(provider, base_url):+ # The Local / custom endpoint wizard: the submitted URL IS the route. Normalizing would+ # recover a bare ``custom`` onto the first named custom provider and keep ITS URL (#145466).+ provider, model = provider.strip(), model.strip()+ else:+ provider, model = _normalize_main_model_assignment(provider, model)provider_entry = _provider_entry(cfg, provider)if not base_url and isinstance(provider_entry, dict) and provider_entry.get("base_url"):base_url = str(provider_entry.get("base_url") or "").strip()diff --git a/tests/hermes_cli/test_model_set_explicit_custom_endpoint.py b/tests/hermes_cli/test_model_set_explicit_custom_endpoint.pynew file mode 100644index 00000000000..86d8ba73147--- /dev/null+++ b/tests/hermes_cli/test_model_set_explicit_custom_endpoint.py@@ -0,0 +1,81 @@+"""#145466: the Local / custom endpoint wizard posts ``provider: custom`` + a NEW ``base_url`` to+POST /api/model/set. With a named custom provider already configured, the legacy bare-``custom``+recovery bound the request to that provider: the response said ok, but its old URL stayed the+route and the new endpoint was never saved."""++import importlib+import json++import pytest++OLD_URL = "http://127.0.0.1:9/old/v1"+NEW_URL = "http://127.0.0.1:9/new/v1"++LAYOUTS = {+ "custom_providers_list": {+ "custom_providers": [{"name": "provider-a", "base_url": OLD_URL, "api_key": "key-a", "model": "model-a"}],+ },+ "providers_dict": {+ "providers": {"custom:provider-a": {"name": "provider-a", "base_url": OLD_URL, "api_key": "key-a",+ "default_model": "model-a"}},+ },+}+++def isolated_home(tmp_path, monkeypatch):+ home = tmp_path / ".hermes"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ import hermes_cli.config as config_mod++ importlib.reload(config_mod)+ yield home+ importlib.reload(config_mod)+++def _write(home, layout):+ cfg = {"model": {"provider": "custom:provider-a", "default": "model-a"}, **LAYOUTS[layout]}+ (home / "config.yaml").write_text(json.dumps(cfg), encoding="utf-8")+++def _read(home):+ import hermes_yaml as yaml++ return yaml.safe_load((home / "config.yaml").read_text(encoding="utf-8")) or {}+++def _endpoint_urls(cfg):+ rows = list(cfg.get("custom_providers") or []) + list((cfg.get("providers") or {}).values())+ return {str(r.get("base_url") or "") for r in rows if isinstance(r, dict)}++[email protected]("layout", sorted(LAYOUTS))[email protected]("api_key", ["key-b", ""])+def test_wizard_save_with_new_url_routes_to_and_saves_the_new_endpoint(isolated_home, layout, api_key):+ from hermes_cli.web_server_config import _apply_model_assignment_sync++ _write(isolated_home, layout)+ result = _apply_model_assignment_sync("main", "custom", "model-b", "", NEW_URL, api_key)++ assert (result["provider"], result["base_url"]) == ("custom", NEW_URL)+ cfg = _read(isolated_home)+ model = cfg["model"]+ assert (model["provider"], model["default"], model["base_url"]) == ("custom", "model-b", NEW_URL)+ # The existing provider's credential never follows the request onto the new host.+ assert model.get("api_key", "") == api_key+ # Both endpoints are listed afterwards: the new one saved, the old one kept.+ assert {OLD_URL, NEW_URL} <= _endpoint_urls(cfg)+++def test_bare_custom_without_url_still_recovers_the_named_provider(isolated_home):+ """Control: legacy configs whose stored provider is a bare ``custom`` keep self-healing onto+ the first named custom provider when no endpoint is submitted."""+ from hermes_cli.web_server_config import _normalize_main_model_assignment, _prepare_main_assignment++ _write(isolated_home, "custom_providers_list")+ assert _normalize_main_model_assignment("custom", "model-a")[0] == "custom:provider-a"+ from hermes_cli.config import load_config++ _base_url, result = _prepare_main_assignment(load_config(), "custom", "model-a", "", "")+ assert (result.target_provider, result.base_url) == ("custom:provider-a", OLD_URL)