refactor(web): unify main-slot model assignment base_url/context handling (#38593)
Both POST /api/model/set and the profile-model writer hand-rolled the same provider/default/base_url/context_length reconciliation. Extract it into _apply_main_model_assignment so the custom-vs-hosted base_url logic lives in one place — removing the future-drift risk where one site learns about custom base_url persistence and the other forgets. Behavior unchanged; pinned with a direct helper unit test.
This commit is contained in:
@@ -1047,6 +1047,39 @@ class TestWebServerEndpoints:
|
||||
assert data["ok"] is True
|
||||
assert data.get("gateway_tools", []) == []
|
||||
|
||||
def test_apply_main_model_assignment_base_url_and_context_reconcile(self):
|
||||
"""The shared main-slot assignment helper must persist base_url only for
|
||||
custom providers, clear stale base_url for hosted ones, and always drop
|
||||
a hardcoded context_length override. Both POST /api/model/set and
|
||||
profile-model writes route through this, so the contract is pinned here."""
|
||||
from hermes_cli.web_server import _apply_main_model_assignment
|
||||
|
||||
# Custom + base_url → persisted; stale context_length dropped.
|
||||
out = _apply_main_model_assignment(
|
||||
{"context_length": 8192}, "custom", "llama-3.1-8b", "http://127.0.0.1:8000/v1"
|
||||
)
|
||||
assert out["provider"] == "custom"
|
||||
assert out["default"] == "llama-3.1-8b"
|
||||
assert out["base_url"] == "http://127.0.0.1:8000/v1"
|
||||
assert "context_length" not in out
|
||||
|
||||
# Hosted provider → stale base_url cleared (no base_url supplied).
|
||||
out = _apply_main_model_assignment(
|
||||
{"base_url": "http://127.0.0.1:8000/v1"}, "openrouter", "anthropic/claude-opus-4.8"
|
||||
)
|
||||
assert out["provider"] == "openrouter"
|
||||
assert out["base_url"] == ""
|
||||
|
||||
# Custom WITHOUT a base_url → don't invent one, clear any stale value.
|
||||
out = _apply_main_model_assignment(
|
||||
{"base_url": "http://stale:1/v1"}, "custom", "m"
|
||||
)
|
||||
assert out["base_url"] == ""
|
||||
|
||||
# Non-dict input is coerced to a fresh dict (never raises).
|
||||
out = _apply_main_model_assignment("not-a-dict", "custom", "m", "http://x/v1")
|
||||
assert out == {"provider": "custom", "default": "m", "base_url": "http://x/v1"}
|
||||
|
||||
def test_parse_model_ids_handles_openai_and_bare_shapes(self):
|
||||
"""Model discovery must tolerate the common /v1/models shapes and
|
||||
never raise (so a slightly non-standard local endpoint still works)."""
|
||||
|
||||
Reference in New Issue
Block a user