Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(llm): root local LLM URLs at save time and stop rewriting keys
trohitgfdc3b59bc93c
diff --git a/server/config/llm_defaults.json b/server/config/llm_defaults.jsonindex 20d4380e..180274cb 100644--- a/server/config/llm_defaults.json+++ b/server/config/llm_defaults.json@@ -176,6 +176,7 @@"default_model": "anthropic/claude-sonnet-5","popular_models": ["anthropic/claude-sonnet-5", "anthropic/claude-opus-5", "openai/gpt-5.6-terra", "openai/gpt-5.6-sol", "openai/gpt-6-astra", "google/gemini-3.8-flash", "google/gemini-3.5-flash", "deepseek/deepseek-v4.1-flash", "moonshotai/kimi-k3", "x-ai/grok-4.20"],"detection_patterns": ["openrouter"],+ "open_world_models": true,"base_url": "https://openrouter.ai/api/v1","models_endpoint": "https://openrouter.ai/api/v1/models","api_key_param": "api_key",@@ -195,6 +196,8 @@"popular_models": [],"_popular_note": "Empty per the 1M-context policy — no Groq-served model reaches 1M (all top out at 131072). Models remain reachable via live fetch_models.","detection_patterns": ["groq"],+ "open_world_models": true,+ "_open_world_note": "Groq serves owner-qualified ids (openai/gpt-oss-120b) that contain no 'groq', so a pattern check would swap a valid selection for default_model.","base_url": "https://api.groq.com/openai/v1","models_endpoint": "https://api.groq.com/openai/v1/models","api_key_param": "api_key",@@ -400,6 +403,11 @@"display_name": "Ollama","_description": "Local Ollama server (OpenAI-compatible /v1). No `default_model` because the dropdown should reflect the user's actually-pulled models — fetch_models hits the user's saved {provider}_proxy URL and returns whatever Ollama reports; when the server is offline we want an empty list (a 'start your server' cue), not a placeholder name the user almost certainly doesn't have.","detection_patterns": ["ollama"],+ "open_world_models": true,+ "auth": {+ "placeholder_key": "ollama",+ "_note": "Ollama's OpenAI-compatibility docs (docs.ollama.com/openai): api_key='ollama', 'required but ignored'. Read by services/llm/config.py::resolve_credential."+ },"base_url": "http://localhost:11434/v1","models_endpoint": "http://localhost:11434/v1/models","api_key_param": "api_key",@@ -413,6 +421,11 @@"display_name": "LM Studio","_description": "Local LM Studio server (pure OpenAI-compatible). Same no-default-model rule as ollama — the dropdown reflects what the user has loaded in LM Studio, not a fake placeholder.","detection_patterns": ["lmstudio", "lm_studio", "lm-studio"],+ "open_world_models": true,+ "auth": {+ "placeholder_key": "lm-studio",+ "_note": "LM Studio's OpenAI-compatibility docs use api_key='lm-studio'; the server needs no key unless API tokens are enabled. Read by services/llm/config.py::resolve_credential."+ },"base_url": "http://localhost:1234/v1","models_endpoint": "http://localhost:1234/v1/models","api_key_param": "api_key",@@ -420,6 +433,21 @@"max_output_tokens": {"_default": 4096},"context_length": {"_default": 32768},"temperature_range": [0.0, 2.0]+ },++ "openai_compatible": {+ "display_name": "OpenAI-compatible",+ "_description": "User-named OpenAI-compatible endpoints: llama.cpp, vLLM, a LiteLLM proxy, a second Ollama host, and so on. No base_url and no default_model: each endpoint is its own provider reference, openai_compatible:<slug>, whose resolved base URL and model list are stored on its credential rows when it is saved (RFC-0003 D13). This block supplies the fallbacks every endpoint shares.",+ "detection_patterns": [],+ "open_world_models": true,+ "auth": {+ "placeholder_key": "sk-no-key-required",+ "_note": "Sent when an endpoint has no key: the value llama.cpp's server README uses in its OpenAI client example. Servers without auth ignore it, and the OpenAI SDK refuses an empty key."+ },+ "max_output_tokens": {"_default": 2048},+ "context_length": {"_default": 8192},+ "_limits_note": "Deliberately conservative. An overestimate overflows the server (or, on Ollama, silently truncates the prompt); an underestimate only compacts early. Saving an endpoint replaces these per model when the server reports its real context (llama.cpp /props, vLLM max_model_len) or the LiteLLM table knows the model id.",+ "temperature_range": [0.0, 2.0]}}}diff --git a/server/nodes/model/_credentials.py b/server/nodes/model/_credentials.pyindex 87242106..8e351b38 100644--- a/server/nodes/model/_credentials.py+++ b/server/nodes/model/_credentials.py@@ -8,13 +8,12 @@ The native SDK client pulls the key directly from:mod:`services.auth`; this class is the Credentials-modal + discoverymanifest, not the runtime client.-Local servers (Ollama, LM Studio) follow the same shape as the cloud-credentials but their api_key is optional — many users run them on-localhost with no auth. The existing ``{provider}_proxy`` mechanism-in :func:`services.ai.AIService.create_model` already handles the-"override base_url + use placeholder api_key" path; the credential-class only needs to return a placeholder when nothing is stored so-the central "API key is required" check in ``execute_chat`` passes.+Servers the user runs (Ollama, LM Studio) store their Base URL under+``{provider}_proxy`` and need no key unless the server enforces one;+without one, the placeholder the vendor documents is sent+(``auth.placeholder_key`` in llm_defaults.json, resolved by+:func:`services.llm.config.resolve_credential`). Both save through one+path, ``_local_validator.save_llm_server`` (RFC-0003 §6)."""from __future__ import annotations@@ -161,32 +160,26 @@ class _LocalLLM(_LLMApiKey):"""Base for local-server credentials (Ollama, LM Studio).Same shape as :class:`_LLMApiKey`, but ``resolve()`` returns the- documented Ollama placeholder when no key is stored instead of- raising. The user's custom server address rides on the existing- ``{id}_proxy`` credential — :func:`services.ai.AIService.create_model`- already reads it and OpenAIProvider already overrides ``base_url``- + forces ``api_key="ollama"``. Nothing else to wire.+ vendor's documented placeholder when no key is stored instead of+ raising. The server address rides on the ``{id}_proxy`` row, which+ the unifier reads before building the client."""@classmethodasync def resolve(cls, *, user_id: str = "owner") -> Dict[str, Any]:+ from services.llm.config import resolve_credentialfrom services.plugin.deps import get_auth_serviceapi_key = await get_auth_service().get_api_key(cls.id)- return {"api_key": api_key or "ollama"}+ return {"api_key": resolve_credential(cls.id, api_key)}@classmethodasync def validate(cls, data: Dict[str, Any]) -> Dict[str, Any]:- """Probe the user's local server via the official SDK.-- Overrides the base ``Credential.validate`` because local-LLM- side-effect ordering genuinely differs from the cloud case:- the user's URL is persisted under ``{cls.id}_proxy`` BEFORE- the probe runs, the placeholder ``api_key="ollama"`` is- stored under ``cls.id`` only on success, and per-model context- is registered in the model registry. Delegates to the- SDK-typed probe in ``_local_validator.py`` which already owns- that full flow.+ """Root the URL, list the loaded models, and save both rows.++ Overrides the base ``Credential.validate`` because the value is a+ Base URL, not a key, and two rows are written (``{id}_proxy`` and+ ``{id}``). See ``_local_validator.save_llm_server``."""from ._local_validator import validate_local_llmdiff --git a/server/nodes/model/_local_validator.py b/server/nodes/model/_local_validator.pyindex 7b4fbe4e..426f428c 100644--- a/server/nodes/model/_local_validator.py+++ b/server/nodes/model/_local_validator.py@@ -1,66 +1,64 @@-"""Local-LLM credential validator (Ollama, LM Studio).--Lives next to the chat-model plugins so all per-provider behaviour for-the local servers stays in `nodes/model/`. Registered into-`routers.websocket._SPECIAL_PROVIDER_VALIDATORS` from the same place-the cloud-provider mapping is declared — same shim shape as apify and-google_maps, the function body just lives here instead.--The frontend reuses the standard ``validate_api_key`` WebSocket message-for these providers; the ``api_key`` field carries the user's Base URL,-not a secret. We:--1. Save the URL under ``{provider}_proxy`` — the existing Ollama-style- auth-delegation key that the runtime path in ``services/ai.py``- already reads.-2. Probe the user's server via the *official* SDK (``ollama`` for- Ollama, ``lmstudio`` for LM Studio) — list installed models and- their actually-loaded ``context_length``. SDK-driven introspection- beats hand-rolled httpx against ``/api/show`` / ``/api/v0/models``- because the SDK ships the typed result struct (``ShowResponse``,- ``LlmInstanceInfo``) and stays compatible with version drift- upstream.-3. Store the placeholder api_key + discovered model list + per-model- ``context_length`` under the provider id. ``model_registry`` reads- the per-model context at runtime so a chat call never assumes a- bogus 32K when the user has a 4K-loaded model.-4. Return ``valid=True`` only when at least one model was found, so a- misconfigured URL surfaces as a clear "no models" message instead- of a silent success.--Connection failures (server down, wrong port, auth refused, timeout)-are caught off the SDK's own exceptions and mapped to specific-user-facing toasts — operators see "is the server running?" for-connect-refused, "wrong path" for 404, etc. The catalogue ``stored``-flag flips to False on every failure path via the broadcaster, so a-failed re-probe clears the previously-green palette dot.+"""Saving a server the user runs: Ollama, LM Studio, or a named endpoint.++One save path for every credential whose value is a base URL (RFC-0003 §6):++- ``ollama`` / ``lmstudio``: one server each; the kind is the provider id.+- ``openai_compatible:<slug>``: any number of named endpoints (llama.cpp,+ vLLM, a LiteLLM proxy, a second Ollama host, ...); the kind is detected+ once, here, and stored.++The frontend reuses the standard ``validate_api_key`` message; its+``api_key`` field carries the Base URL, not a secret. Steps, in order:++1. Resolve the credential: the user's key, else the placeholder the vendor+ documents (``auth.placeholder_key`` in llm_defaults.json).+2. Root the URL through the OpenAI surface the runtime uses+ (:func:`services.llm.endpoints.resolve_base_url`). This is what makes a+ green panel mean a working runtime: the old probe talked to each server's+ native API, so a URL missing ``/v1`` validated and then failed every run.+3. Kind: declared, or detected from native routes by body shape.+4. Models and per-model params, per kind. Ollama and LM Studio keep their+ official SDK probes, which report the context each loaded model actually+ runs with; llama.cpp reports ``n_ctx`` on ``/props``; anything else is+ sized from vLLM's ``max_model_len``, then the LiteLLM table.+5. Persist ``{ref}_proxy`` (the resolved URL) and ``{ref}`` (key, models,+ params), register the models, broadcast.++A failure at any step writes nothing and broadcasts nothing (D5): a typo in+a re-Fetch must not take down a configuration that works."""from __future__ import annotationsimport time-from typing import Any, Dict, List, Tuple+from datetime import datetime, timezone+from typing import Any, Dict, List, Optional, Tupleimport httpximport lmstudioimport ollamafrom core.logging import get_logger+from services.llm.config import resolve_credential+from services.llm.endpoints import SERVER_META_KEY, base_url_key, redact_url, resolve_base_urlfrom services.plugin.deps import get_auth_servicefrom services.status_broadcaster import get_status_broadcasterlogger = get_logger(__name__)+_DISPLAY_NAMES = {"ollama": "Ollama", "lmstudio": "LM Studio"}-def _classify_http_error(provider: str, base_url: str, exc: BaseException) -> Tuple[str, str]:- """Map an httpx / generic exception to (log_summary, user_message).+# Native routes are only asked once the host is known to answer, so a+# short timeout is enough and keeps a save well inside the WS budget.+_NATIVE_TIMEOUT_SECONDS = 3.0+++def _classify_http_error(display: str, base_url: str, exc: BaseException) -> Tuple[str, str]:+ """Map a native-probe exception to (log_summary, user_message).Both SDKs (ollama-python, lmstudio) raise httpx errors underneath.- Mapping these directly gives clean operator logs ("connect-refused"- vs "404 vs "timeout") + actionable user toasts without depending- on the openai SDK exception hierarchy.+ Rooting has already succeeded by the time a native probe runs, so a+ failure here is about the native API, not the base URL."""- display = "LM Studio" if provider == "lmstudio" else provider.capitalize()-if isinstance(exc, httpx.TimeoutException):return ("timeout", f"Request to {base_url} timed out — server may be overloaded or unreachable")@@ -72,12 +70,7 @@ def _classify_http_error(provider: str, base_url: str, exc: BaseException) -> Tuif status in (401, 403):return (f"HTTP {status}", f"{display} rejected the request — server requires auth.")if status == 404:- url = base_url.rstrip("/")- if url.endswith("/v1"):- hint = f"the {display} server is reachable but expected endpoints aren't exposed — check the server version."- else:- hint = f"the URL likely needs to end with `/v1` (e.g. {url}/v1)."- return (f"HTTP {status}", f"{display} returned 404 — {hint}")+ return (f"HTTP {status}", f"{display} returned 404 for its model list — check the server version.")if status == 429:return (f"HTTP {status}", f"{display} rate-limited the request — try again shortly.")return (f"HTTP {status}", f"{display} returned HTTP {status} — check the server logs.")@@ -90,29 +83,10 @@ def _classify_http_error(provider: str, base_url: str, exc: BaseException) -> Tureturn (type(exc).__name__, f"Could not reach {display}: {exc}")-async def _fail(- provider: str,- message: str,- *,- has_key: bool = False,-) -> Dict[str, Any]:- """Common rejection path: broadcast invalid status + return envelope.-- `has_key=True` is used after the URL has been persisted so the- palette doesn't go straight to the "unconfigured" gray dot — the- user's URL is on file, it just can't reach a working server right- now. `has_key=False` is used when we abort before persisting (only- the "Base URL required" early-exit today).- """- await get_status_broadcaster().update_api_key_status(- provider=provider,- valid=False,- message=message,- has_key=has_key,- models=[],- )+def _failure(provider_ref: str, message: str) -> Dict[str, Any]:+ """Rejection envelope. Deliberately no broadcast: nothing changed (D5)."""return {- "provider": provider,+ "provider": provider_ref,"success": True,"valid": False,"message": message,@@ -124,9 +98,9 @@ async def _fail(def _strip_v1_path(base_url: str) -> str:"""Return ``base_url`` with a trailing ``/v1`` segment stripped.- The user's stored URL is the OpenAI-compatible base- (``http://host:port/v1``). Both Ollama's REST API and LM Studio's- SDK want the host:port without the OpenAI-compat suffix.+ The stored URL is the OpenAI-compatible base (``http://host:port/v1``).+ Native APIs (Ollama's REST API, LM Studio's SDK, llama.cpp's ``/props``)+ live beside it, at the host without the OpenAI-compat suffix."""u = base_url.rstrip("/")if u.endswith("/v1"):@@ -151,8 +125,8 @@ async def _fetch_ollama_models(base_url: str) -> List[Dict[str, Any]]:If the user has models *pulled* but none currently loaded, ``ps()``returns an empty list — same semantics as LM Studio's- ``list_loaded()``. The validator surfaces this as the "load a model- and click Fetch again" message, which is already accurate.+ ``list_loaded()``. The save surfaces this as the "load a model and+ click Fetch again" message, which is accurate."""host = _strip_v1_path(base_url)client = ollama.AsyncClient(host=host, timeout=10.0)@@ -195,8 +169,8 @@ async def _fetch_lmstudio_models(base_url: str) -> List[Dict[str, Any]]:``params_string``); no string parsing.LM Studio's SDK takes ``api_host`` as ``host:port`` (no scheme, no- path), so the user's stored ``http://host:port/v1`` is stripped- before construction.+ path), so the stored ``http://host:port/v1`` is stripped before+ construction."""host = _strip_v1_path(base_url)api_host = host.split("://", 1)[-1]@@ -233,117 +207,230 @@ async def _fetch_lmstudio_models(base_url: str) -> List[Dict[str, Any]]:return out-async def _fetch_local_models(provider: str, base_url: str) -> List[Dict[str, Any]]:- """Dispatch to the per-provider SDK probe."""- if provider == "ollama":- return await _fetch_ollama_models(base_url)- if provider == "lmstudio":- return await _fetch_lmstudio_models(base_url)- raise ValueError(f"Unsupported local provider: {provider}")+async def _get_json(client: httpx.AsyncClient, url: str) -> Any:+ """GET ``url`` and return its JSON body, or ``None`` on any failure."""+ try:+ response = await client.get(url)+ if response.status_code // 100 != 2:+ return None+ return response.json()+ except Exception: # noqa: BLE001 — a missing route just means "not this kind"+ return None-async def validate_local_llm(data: Dict[str, Any]) -> Dict[str, Any]:- """Validator for ollama / lmstudio. Returns the standard response envelope.-- Called from :meth:`nodes.model._credentials._LocalLLM.validate` (the- ``Credential.validate`` hook ``handle_validate_api_key`` dispatches- to). All side effects (URL persistence, status broadcasts, model- registry registration) go through the ``StatusBroadcaster`` /- ``AuthService`` singletons — no per-request WebSocket reference is- needed.+async def _detect_kind(base_url: str, api_key: str) -> Tuple[str, Optional[Dict[str, Any]]]:+ """Which server answers at ``base_url``, from its native routes.++ Each route is accepted only by body shape, never by status: LM Studio+ answers HTTP 200 to routes it does not serve. llama.cpp is asked first+ because its server also mimics some Ollama routes. Returns the kind and,+ for llama.cpp, the ``/props`` body (it carries ``n_ctx``)."""- provider = data["provider"].lower()- base_url = data.get("api_key", "").strip()- session_id = data.get("session_id", "default")+ host = _strip_v1_path(base_url)+ headers = {"Authorization": f"Bearer {api_key}"}+ async with httpx.AsyncClient(timeout=_NATIVE_TIMEOUT_SECONDS, headers=headers) as client:+ props = await _get_json(client, f"{host}/props")+ if isinstance(props, dict) and isinstance(props.get("default_generation_settings"), dict):+ return "llamacpp", props+ body = await _get_json(client, f"{host}/api/v1/models")+ if isinstance(body, dict) and isinstance(body.get("models"), list):+ return "lmstudio", None+ body = await _get_json(client, f"{host}/api/version")+ if isinstance(body, dict) and isinstance(body.get("version"), str):+ return "ollama", None+ return "generic", None+++def _positive_int(value: Any) -> Optional[int]:+ if isinstance(value, bool) or not isinstance(value, (int, float)):+ return None+ number = int(value)+ return number if number > 0 else None+++def _generic_params(entry: Any) -> Dict[str, Any]:+ """Size and price one model on a server that does not describe it.++ The server's own figure wins: vLLM reports ``max_model_len`` on its+ ``/models`` entries, and that is the context it enforces. Otherwise the+ LiteLLM table, by model id. Otherwise nothing, and the declared+ ``openai_compatible`` defaults apply at run time.+ """+ from services.model_registry import get_model_registry- if not base_url:- return {"success": False, "valid": False, "error": "Base URL required"}+ params: Dict[str, Any] = {}+ extra = getattr(entry, "model_extra", None) or {}+ context = _positive_int(extra.get("max_model_len"))+ if context:+ params["context_length"] = context++ spec = get_model_registry().lookup_litellm(getattr(entry, "id", "") or "")+ if spec:+ params.setdefault("context_length", _positive_int(spec.get("max_input_tokens")))+ max_out = _positive_int(spec.get("max_output_tokens"))+ # An output budget as large as the window leaves no room for the+ # prompt; drop it and let the registry derive one from the context.+ if max_out and (not params.get("context_length") or max_out < params["context_length"]):+ params["max_output_tokens"] = max_out+ for field, key in (("input_cost_per_token", "input_price_per_mtok"), ("output_cost_per_token", "output_price_per_mtok")):+ cost = spec.get(field)+ if isinstance(cost, (int, float)) and not isinstance(cost, bool):+ params[key] = round(float(cost) * 1_000_000, 4)+ return {key: value for key, value in params.items() if value is not None}+++async def _describe_models(+ kind: str,+ base_url: str,+ entries: List[Any],+ props: Optional[Dict[str, Any]],+) -> Dict[str, Dict[str, Any]]:+ """``{model_id: params}`` for the server, per kind (RFC-0003 D15).++ Local kinds never take LiteLLM figures: the bound that matters is the+ context the server was started with, not the model's trained maximum,+ and a local run costs nothing.+ """+ if kind in ("ollama", "lmstudio"):+ fetch = _fetch_ollama_models if kind == "ollama" else _fetch_lmstudio_models+ return {e["id"]: {k: v for k, v in e.items() if k != "id"} for e in await fetch(base_url)}- auth_service = get_auth_service()+ ids = [entry for entry in entries if isinstance(getattr(entry, "id", None), str) and entry.id]+ if kind == "llamacpp":+ settings = (props or {}).get("default_generation_settings") or {}+ n_ctx = _positive_int(settings.get("n_ctx"))+ return {entry.id: ({"context_length": n_ctx} if n_ctx else {}) for entry in ids}- # Persist the URL first so the runtime path (services/ai.py) can- # read it via the existing {provider}_proxy lookup even before the- # probe succeeds. The URL stays persisted on probe failure so the- # user can re-click "Fetch" without re-entering it; the failure- # branch broadcasts has_key=True so the palette dot reflects "URL- # on file but currently unreachable" rather than "unconfigured".- await auth_service.store_api_key(- provider=f"{provider}_proxy",- api_key=base_url,- models=[],- session_id=session_id,- )+ from services.model_registry import get_model_registry++ await get_model_registry().ensure_litellm_table()+ return {entry.id: _generic_params(entry) for entry in ids}+++def _no_models_message(kind: str, display: str, base_url: str) -> str:+ if kind in ("ollama", "lmstudio"):+ return (+ f"Connected to {display} at {redact_url(base_url)}, but no models are loaded. "+ f"Load a model in {display} and click Fetch again."+ )+ return f"{display} at {redact_url(base_url)} lists no models."+++async def save_llm_server(+ provider_ref: str,+ candidate_url: str,+ user_key: Optional[str],+ *,+ display: str,+ kind: Optional[str] = None,+ label: str = "",+) -> Dict[str, Any]:+ """Root, describe and persist one server. Returns the WS envelope.++ ``kind`` is passed for providers that declare it (ollama, lmstudio) and+ left ``None`` for named endpoints, which are detected once here.+ """+ try:+ api_key = resolve_credential(provider_ref, user_key)+ except ValueError:+ return _failure(provider_ref, f"No API key is available for {display}.")++ resolved = await resolve_base_url(candidate_url, api_key=api_key)+ if not resolved.ok:+ logger.warning("LLM server save failed", provider=provider_ref, reason=resolved.reason)+ return _failure(provider_ref, resolved.reason)+ base_url = resolved.base_url++ props: Optional[Dict[str, Any]] = None+ if kind is None:+ kind, props = await _detect_kind(base_url, api_key)try:- entries = await _fetch_local_models(provider, base_url)- except (httpx.HTTPError, lmstudio.LMStudioError) as e:- log_summary, user_msg = _classify_http_error(provider, base_url, e)- logger.warning("[%s] model probe failed (%s) at %s", provider, log_summary, base_url)- return await _fail(provider, user_msg, has_key=True)- except Exception as e:- log_summary, user_msg = _classify_http_error(provider, base_url, e)- logger.warning("[%s] model probe unexpected error (%s) at %s: %s", provider, log_summary, base_url, e)- return await _fail(provider, user_msg, has_key=True)-- if not entries:- # Server reachable, responded with empty model list. Different- # failure mode from the connect-error branches above — keep the- # original "load a model" hint here since it's now accurate.- display = "LM Studio" if provider == "lmstudio" else provider.capitalize()- message = f"Connected to {display} at {base_url}, but no models are loaded. Load a model in {display} and click Fetch again."- logger.info("[%s] reachable at %s but returned 0 models", provider, base_url)- return await _fail(provider, message, has_key=True)-- # Pivot the SDK-probed entries into the storage shape: a parallel- # ``models`` list (for the legacy readers) plus a ``model_params``- # dict carrying every typed field the SDK exposed- # (``context_length``, ``vision``, ``supports_tools``,- # ``architecture``, ``param_size``, ``quantization``,- # ``max_context_length``). ``model_registry.register_local_model``- # consumes these directly to populate ``ModelInfo``.- models = [e["id"] for e in entries]- model_params: Dict[str, Dict[str, Any]] = {- e["id"]: {k: v for k, v in e.items() if k != "id"} for e in entries if any(k != "id" for k in e)+ model_params = await _describe_models(kind, base_url, resolved.models, props)+ except Exception as exc: # noqa: BLE001 — SDK and httpx failures, classified below+ log_summary, message = _classify_http_error(display, redact_url(base_url), exc)+ logger.warning("LLM server model probe failed", provider=provider_ref, error=log_summary, url=redact_url(base_url))+ return _failure(provider_ref, message)++ if not model_params:+ logger.info("LLM server reachable but lists no models", provider=provider_ref, url=redact_url(base_url))+ return _failure(provider_ref, _no_models_message(kind, display, base_url))++ models = list(model_params)+ # Only the redacted form is stored here: this JSON column is not+ # encrypted, and a URL can carry userinfo. The full URL lives in the+ # encrypted ``{ref}_proxy`` row.+ server_meta = {+ "label": label or display,+ "base_url": redact_url(base_url),+ "kind": kind,+ "probed_at": datetime.now(timezone.utc).isoformat(),}- # Store placeholder api_key + the real model list + per-model params.- # The placeholder ("ollama") is the documented value the OpenAI-style- # auth-delegation path expects when no real key is needed; it never- # leaves the process because the runtime SDK rewrites it when- # proxy_url is set.+ auth_service = get_auth_service()+ # The URL row first: a key row with no URL row is exactly what the+ # runtime reports as "not configured".+ await auth_service.store_api_key(provider=base_url_key(provider_ref), api_key=base_url, models=[])await auth_service.store_api_key(- provider=provider,- api_key="ollama",+ provider=provider_ref,+ api_key=api_key,models=models,- session_id=session_id,- model_params=model_params,+ model_params={**model_params, SERVER_META_KEY: server_meta},)- # Register each model in the in-memory model registry so the sync- # ``get_context_length`` / ``get_max_output_tokens`` lookups pick up- # the real loaded n_ctx without re-querying the DB on every chat- # call. Also keeps the runtime path branchless — local and cloud- # models share the same ``provider/model_id`` registry key.+ # Keep the sync ``get_context_length`` / ``get_max_output_tokens``+ # lookups on the real per-model values without a DB read per call.from services.model_registry import get_model_registryregistry = get_model_registry()+ registry.forget_local_models(provider_ref)for mid, params in model_params.items():- registry.register_local_model(provider, mid, params)+ if params:+ registry.register_local_model(provider_ref, mid, params)+ message = f"{len(models)} model(s) at {redact_url(base_url)}"await get_status_broadcaster().update_api_key_status(- provider=provider,+ provider=provider_ref,valid=True,- message=f"{len(models)} model(s) discovered at {base_url}",+ message=message,has_key=True,models=models,)- ctx_summary = ", ".join(f"{mid}={p['context_length']}" for mid, p in model_params.items()) or "no per-model context info"- logger.info("[%s] discovered %d model(s) at %s (%s)", provider, len(models), base_url, ctx_summary)+ ctx_summary = ", ".join(f"{mid}={p['context_length']}" for mid, p in model_params.items() if p.get("context_length"))+ logger.info(+ "LLM server saved",+ provider=provider_ref,+ kind=kind,+ url=redact_url(base_url),+ rewritten=resolved.rewritten,+ models=len(models),+ context=ctx_summary or "server did not report one",+ )return {- "provider": provider,+ "provider": provider_ref,"success": True,"valid": True,"models": models,- "message": f"Connected to {provider} at {base_url}",+ "message": message,+ "base_url": redact_url(base_url),+ "kind": kind,"timestamp": time.time(),}+++async def validate_local_llm(data: Dict[str, Any]) -> Dict[str, Any]:+ """``validate_api_key`` for ollama / lmstudio: one server, kind declared.++ Called from :meth:`nodes.model._credentials._LocalLLM.validate`.+ """+ provider = data["provider"].lower()+ base_url = (data.get("api_key") or "").strip()+ if not base_url:+ return {"success": False, "valid": False, "error": "Base URL required"}+ return await save_llm_server(+ provider,+ base_url,+ None,+ display=_DISPLAY_NAMES.get(provider, provider),+ kind=provider,+ )diff --git a/server/services/credentials/handlers.py b/server/services/credentials/handlers.pyindex a0300e30..40b4091a 100644--- a/server/services/credentials/handlers.py+++ b/server/services/credentials/handlers.py@@ -160,9 +160,18 @@ async def handle_delete_api_key(data: Dict[str, Any], websocket: WebSocket) -> Dprovider = data["provider"].lower()async def _do_delete() -> Dict[str, Any]:+ from services.llm.endpoints import base_url_key+ from services.model_registry import get_model_registry+auth_service = container.auth_service()broadcaster = get_status_broadcaster()- await auth_service.remove_api_key(provider, data.get("session_id", "default"))+ session_id = data.get("session_id", "default")+ await auth_service.remove_api_key(provider, session_id)+ # A provider's Base URL row and its registered models belong to it:+ # deleting a named endpoint (or a local server's key) must leave+ # neither behind. Both are no-ops for a provider that has none.+ await auth_service.remove_api_key(base_url_key(provider), session_id)+ get_model_registry().forget_local_models(provider)await broadcaster.update_api_key_status(provider,valid=False,diff --git a/server/services/llm/config.py b/server/services/llm/config.pyindex 45060f6e..e083dad8 100644--- a/server/services/llm/config.py+++ b/server/services/llm/config.py@@ -97,7 +97,59 @@ PROVIDER_CONFIGS: Dict[str, ProviderConfig] = _build_provider_configs()def get_provider_config(provider: str) -> Optional[ProviderConfig]:- return PROVIDER_CONFIGS.get(provider)+ return PROVIDER_CONFIGS.get(split_provider_ref(provider)[0])+++# ---------------------------------------------------------------------------+# Provider references (RFC-0003 D13)+# ---------------------------------------------------------------------------++#: The provider whose instances are user-named endpoints.+ENDPOINT_PROVIDER = "openai_compatible"+++def split_provider_ref(ref: str) -> Tuple[str, str]:+ """Split a provider reference into ``(provider, endpoint_slug)``.++ A reference is either a registered provider id (``"ollama"``) or a+ named OpenAI-compatible endpoint (``"openai_compatible:home-vllm"``);+ the slug is ``""`` for a plain provider. This is the only parser of+ the format. Every lookup keyed by ``llm_defaults.json`` resolves the+ reference through it, so an endpoint reads the ``openai_compatible``+ block while its credential rows stay keyed by the full reference.+ """+ name, _, slug = (ref or "").partition(":")+ return name, slug+++def endpoint_ref(slug: str) -> str:+ """The provider reference of the named endpoint ``slug``."""+ return f"{ENDPOINT_PROVIDER}:{slug}"+++def _provider_block(provider: str) -> Dict[str, Any]:+ """The ``llm_defaults.json`` block for a provider reference."""+ return LLM_DEFAULTS.get("providers", {}).get(split_provider_ref(provider)[0], {})+++def resolve_credential(provider: str, stored: Optional[str]) -> str:+ """Return the key to send for ``provider``.++ The stored key when there is one, else the placeholder the vendor+ documents for keyless servers, declared as+ ``providers.<name>.auth.placeholder_key`` (Ollama's docs: ``"ollama"``,+ "required but ignored"). Raises ``ValueError`` when neither exists.++ Never returns ``None``: the OpenAI SDK reads ``OPENAI_API_KEY`` when+ handed ``api_key=None`` and would send the operator's OpenAI key to+ whatever ``base_url`` is configured (RFC-0003 D7).+ """+ if stored:+ return stored+ placeholder = (_provider_block(provider).get("auth") or {}).get("placeholder_key")+ if placeholder:+ return placeholder+ raise ValueError(f"no API key configured for provider {provider!r}")# ---------------------------------------------------------------------------@@ -114,14 +166,15 @@ def detect_provider_from_model(model: str) -> str:def is_model_valid_for_provider(model: str, provider: str) -> bool:- # Open-world providers — OpenRouter is a multi-vendor proxy, Ollama and- # LM Studio serve user-installed models, and Groq's owner-qualified model- # IDs (for example ``openai/gpt-oss-120b``) do not contain "groq".- # Treat them as provider-selected; the upstream API will return a clear- # 404 for a genuinely missing model.- if provider in ("openrouter", "ollama", "lmstudio", "groq"):+ # Open-world providers serve ids no pattern can check: a proxy's+ # catalogue (OpenRouter), a local server's pulls (Ollama, LM Studio),+ # owner-qualified ids (Groq's ``openai/gpt-oss-120b``), or whatever a+ # named endpoint lists. They declare ``open_world_models`` and are+ # treated as provider-selected; the upstream returns a clear 404 for a+ # genuinely missing model.+ if _provider_block(provider).get("open_world_models"):return True- cfg = PROVIDER_CONFIGS.get(provider)+ cfg = get_provider_config(provider)if not cfg:return Truemodel_lower = model.lower()@@ -134,7 +187,7 @@ def is_model_valid_for_provider(model: str, provider: str) -> bool:def get_default_model(provider: str) -> str:- cfg = PROVIDER_CONFIGS.get(provider)+ cfg = get_provider_config(provider)return cfg.default_model if cfg else "gpt-5.2"@@ -168,7 +221,7 @@ def curated_models(provider: str) -> list:are real model names while gemini's ``popular_models`` carries``-latest`` aliases the Vertex backend rejects."""- provider_cfg = LLM_DEFAULTS.get("providers", {}).get(provider, {})+ provider_cfg = _provider_block(provider)explicit = provider_cfg.get("popular_models") or []if explicit:return list(explicit)@@ -185,8 +238,7 @@ def supports_model_listing(provider: str) -> bool:ships no model-list route at all (verified against its publishedOpenAPI spec), so calling ``client.models.list()`` there 404s."""- provider_cfg = LLM_DEFAULTS.get("providers", {}).get(provider, {})- return bool(provider_cfg.get("supports_model_listing", True))+ return bool(_provider_block(provider).get("supports_model_listing", True))# ---------------------------------------------------------------------------@@ -240,8 +292,7 @@ def resolve_temperature(params: dict, model: str, provider: str, thinking_enablereturn 1.0# Fixed temperature per model from llm_defaults.json (e.g. kimi-k2.5 = 0.6)- prov_json = LLM_DEFAULTS.get("providers", {}).get(provider, {})- fixed_temps = prov_json.get("fixed_temperature", {})+ fixed_temps = _provider_block(provider).get("fixed_temperature", {})for prefix, fixed_temp in fixed_temps.items():if model.startswith(prefix):return float(fixed_temp)diff --git a/server/services/llm/endpoints.py b/server/services/llm/endpoints.pynew file mode 100644index 00000000..d2ed0c1c--- /dev/null+++ b/server/services/llm/endpoints.py@@ -0,0 +1,178 @@+"""Where an OpenAI-compatible API is rooted, decided once, when it is saved.++RFC-0003 §6. A user-supplied base URL is ambiguous: llama.cpp and a LiteLLM+proxy serve the API at the root, Ollama, LM Studio and vLLM only under+``/v1``, and a path-prefixed deployment can be either. No string rule+resolves that, so :func:`resolve_base_url` asks the server, and the answer+is persisted. Call time only ever reads the stored value.++Two things make the probe trustworthy:++- It is the call execution makes. ``AsyncOpenAI.models.list()`` is exactly+ what ``OpenAIProvider.fetch_models`` sends, through the same client and+ the same URL concatenation, so a URL that resolves here is a URL the+ runtime can use.+- A status code proves nothing on its own. LM Studio answers HTTP 200 to+ routes it does not serve, so a candidate counts only when the body is an+ OpenAI list (a ``data`` array). A status-only probe would accept a+ ``/v1``-less LM Studio URL, which is the failure RFC-0003 §2.1 records.++This is the one module allowed to append ``/v1`` (RFC-0003 AG1/AG2). The+openai SDK is imported inside the probe, so importing this module for its+pure helpers costs nothing.+"""++from __future__ import annotations++from dataclasses import dataclass, field+from typing import Any, List, Optional+from urllib.parse import urlsplit, urlunsplit++from core.logging import get_logger++logger = get_logger(__name__)++#: Suffix of the credential row that holds a provider's base URL. The row+#: shape (``{provider}`` for the key, ``{provider}_proxy`` for the URL) is+#: shared by Ollama, LM Studio and every named endpoint.+BASE_URL_SUFFIX = "_proxy"++#: Reserved ``model_params`` entry on a saved server's key row, holding its+#: display metadata (label, redacted base URL, kind). Underscore-prefixed+#: like ``_default`` in llm_defaults.json: it is not a model.+SERVER_META_KEY = "_endpoint"++_OPENAI_PREFIX = "/v1"+++def base_url_key(provider_ref: str) -> str:+ """Credential-row key holding the resolved base URL of ``provider_ref``."""+ return f"{provider_ref}{BASE_URL_SUFFIX}"+++def redact_url(url: Any) -> str:+ """Render ``url`` for a log line or a user-facing message.++ Drops userinfo, the query string and the fragment, the parts of a URL+ that can carry a credential. Returns ``""`` for an empty value.+ """+ if not url:+ return ""+ parts = urlsplit(str(url))+ host = parts.hostname or ""+ if ":" in host: # IPv6 literal: hostname drops the brackets+ host = f"[{host}]"+ try:+ port = parts.port+ except ValueError: # malformed port: render the host alone+ port = None+ if port:+ host = f"{host}:{port}"+ return urlunsplit((parts.scheme, host, parts.path, "", ""))+++@dataclass(frozen=True)+class ResolvedBaseUrl:+ """Outcome of rooting a user-supplied URL.++ ``base_url`` is ``None`` when nothing was adopted, and ``reason`` then+ says why in words safe to show the user. ``models`` carries the entries+ of the adopted ``/models`` page (SDK ``Model`` objects, whose extra+ fields such as vLLM's ``max_model_len`` survive in ``model_extra``).+ """++ base_url: Optional[str]+ rewritten: bool = False+ models: List[Any] = field(default_factory=list)+ reason: str = ""++ @property+ def ok(self) -> bool:+ return self.base_url is not None+++def _normalize_candidate(candidate: str) -> Optional[str]:+ """Trim the input and require an absolute http(s) URL.++ A scheme-less value such as ``localhost:8080`` is rejected rather than+ guessed at: the user sees what to type instead of a URL we invented.+ """+ value = (candidate or "").strip().rstrip("/")+ parts = urlsplit(value)+ if parts.scheme not in ("http", "https") or not parts.netloc:+ return None+ return value+++async def resolve_base_url(candidate: str, *, api_key: str, timeout: float = 10.0) -> ResolvedBaseUrl:+ """Find the prefix at which ``candidate`` serves the OpenAI API.++ Tries ``candidate``, then ``candidate + "/v1"`` unless the input already+ ends there. Per candidate:++ - the ``/models`` page is an OpenAI list: adopt it;+ - 401/403: a server is there but refused the key; try the next one;+ - a connection-level failure: stop, the next candidate is the same host;+ - anything else (404, a 200 whose body is not a list): try the next one.++ ``api_key`` must already be resolved (``resolve_credential``): the SDK+ reads ``OPENAI_API_KEY`` when handed ``None``.+ """+ import openai++ base = _normalize_candidate(candidate)+ if base is None:+ return ResolvedBaseUrl(+ None,+ reason="Enter the full base URL, including http:// or https:// (for example http://localhost:8080/v1).",+ )++ candidates = [base] if base.endswith(_OPENAI_PREFIX) else [base, base + _OPENAI_PREFIX]+ key_rejected_at: Optional[str] = None++ for url in candidates:+ try:+ async with openai.AsyncOpenAI(base_url=url, api_key=api_key, max_retries=0, timeout=timeout) as client:+ page = await client.models.list()+ except (openai.AuthenticationError, openai.PermissionDeniedError):+ key_rejected_at = key_rejected_at or url+ continue+ except openai.APIConnectionError as exc:+ # APITimeoutError subclasses APIConnectionError. Either way the+ # host did not answer, and the other candidate is the same host.+ logger.info("endpoint unreachable", url=redact_url(url), error=type(exc).__name__)+ return ResolvedBaseUrl(None, reason=f"Could not reach {redact_url(base)}. Is the server running?")+ except Exception as exc: # noqa: BLE001 — any other answer means "not rooted here"+ logger.debug("no OpenAI models list at candidate", url=redact_url(url), error=type(exc).__name__)+ continue++ entries = getattr(page, "data", None)+ if not isinstance(entries, list):+ logger.debug("candidate answered without an OpenAI list", url=redact_url(url))+ continue++ rewritten = url != base+ if rewritten:+ logger.info("endpoint base URL rewritten", entered=redact_url(base), resolved=redact_url(url))+ return ResolvedBaseUrl(url, rewritten=rewritten, models=list(entries))++ if key_rejected_at is not None:+ return ResolvedBaseUrl(+ None,+ reason=f"Found an OpenAI-compatible server at {redact_url(key_rejected_at)}, but it rejected the API key.",+ )+ tried = " or ".join(redact_url(url) for url in candidates)+ return ResolvedBaseUrl(+ None,+ reason=f"No OpenAI-compatible API answered at {tried}. Check the base URL (for example http://localhost:8080/v1).",+ )+++__all__ = [+ "BASE_URL_SUFFIX",+ "SERVER_META_KEY",+ "ResolvedBaseUrl",+ "base_url_key",+ "redact_url",+ "resolve_base_url",+]diff --git a/server/services/llm/protocol.py b/server/services/llm/protocol.pyindex c03aad50..692dcc5c 100644--- a/server/services/llm/protocol.py+++ b/server/services/llm/protocol.py@@ -315,6 +315,9 @@ class LLMErrorCategory(str, Enum):TIMEOUT = "timeout"CONNECTION = "connection"SERVER = "server"+ # A 2xx whose body is not a completion: the request reached something+ # that does not speak this wire format at that path (RFC-0003 D9).+ PROTOCOL = "protocol"UNKNOWN = "unknown"@@ -331,6 +334,11 @@ class LLMError(Exception):request_id: Optional[str] = Noneretry_after: Optional[float] = Noneretry_after_raw: Optional[str] = None+ # Set by the raiser when the category text alone cannot say what the+ # user must change (e.g. which URL answered with a non-completion).+ # Must already be safe for public surfaces: no credential, no query+ # string, no userinfo.+ public_message: Optional[str] = Nonedef __post_init__(self) -> None:Exception.__init__(self, self.message)@@ -345,6 +353,9 @@ class LLMError(Exception):details, so execution boundaries must expose this property instead."""+ if self.public_message:+ return self.public_message+provider_names = {"anthropic": "Anthropic","gemini": "Gemini",@@ -360,15 +371,17 @@ class LLMError(Exception):"ollama": "Ollama","lmstudio": "LM Studio",}- provider_key = str(self.provider or "").strip().lower()- provider = provider_names.get(- provider_key, "The language model provider"- )- provider_object = (- provider- if provider_key in provider_names- else "the language model provider"- )+ # Common nouns take an article, so they read "The <noun>" as a+ # subject and "the <noun>" as an object.+ generic_nouns = {"openai_compatible": "OpenAI-compatible endpoint"}+ # A named endpoint's reference is "openai_compatible:<slug>".+ provider_key = str(self.provider or "").strip().lower().partition(":")[0]+ if provider_key in provider_names:+ provider = provider_object = provider_names[provider_key]+ else:+ noun = generic_nouns.get(provider_key, "language model provider")+ provider = f"The {noun}"+ provider_object = f"the {noun}"category = (self.category.valueif isinstance(self.category, LLMErrorCategory)@@ -407,6 +420,10 @@ class LLMError(Exception):LLMErrorCategory.SERVER.value: (f"{provider} is temporarily unavailable."),+ LLMErrorCategory.PROTOCOL.value: (+ f"{provider} answered without a completion. "+ "Check the configured base URL."+ ),LLMErrorCategory.UNKNOWN.value: (f"{provider} request failed."),diff --git a/server/services/llm/providers/anthropic.py b/server/services/llm/providers/anthropic.pyindex 3128386a..dcaea0c6 100644--- a/server/services/llm/providers/anthropic.py+++ b/server/services/llm/providers/anthropic.py@@ -38,7 +38,6 @@ class AnthropicProvider:}if proxy_url:kwargs["base_url"] = proxy_url- kwargs["api_key"] = "ollama"self._client = anthropic.AsyncAnthropic(**kwargs)# ------------------------------------------------------------------diff --git a/server/services/llm/providers/openai.py b/server/services/llm/providers/openai.pyindex 306ad8aa..f78d9d00 100644--- a/server/services/llm/providers/openai.py+++ b/server/services/llm/providers/openai.py@@ -42,13 +42,52 @@ class OpenAIProvider:"api_key": api_key,"max_retries": max(0, int(max_retries)),}+ # The user's stored URL (resolved when it was saved) wins over the+ # vendor-declared one. Neither is ever rewritten here (RFC-0003 D1).url = proxy_url or base_urlif url:kwargs["base_url"] = url- if proxy_url:- kwargs["api_key"] = "ollama"+ #: Which setting produced the URL, for failure logs (RFC-0003 D10).+ self.url_source = "proxy" if proxy_url else ("llm_defaults" if base_url else "sdk_default")self._client = openai.AsyncOpenAI(**kwargs)+ @property+ def endpoint_url(self) -> str:+ """The base URL requests actually go to."""+ return str(self._client.base_url)++ def _raise_if_error_body(self, resp: Any) -> None:+ """Turn a 2xx that carried an error body into a ``PROTOCOL`` failure.++ The SDK raises only on 4xx/5xx. A server asked for a route it does+ not serve can answer HTTP 200 with an ``{"error": ...}`` body (LM+ Studio does), which the SDK parses into a completion with no+ choices. Left alone that surfaces as "AI generated empty response",+ with nothing pointing at the base URL (RFC-0003 §2.1, D9).+ """+ extra = getattr(resp, "model_extra", None)+ error = extra.get("error") if isinstance(extra, dict) else None+ if error is None:+ candidate = getattr(resp, "error", None)+ if isinstance(candidate, (str, dict)):+ error = candidate+ if not error:+ return++ from services.llm.endpoints import redact_url+ from services.llm.protocol import LLMError, LLMErrorCategory++ url = redact_url(self.endpoint_url)+ raise LLMError(+ message=f"HTTP 2xx without choices from {url}: {str(error)[:300]}",+ provider=self.provider_name,+ category=LLMErrorCategory.PROTOCOL,+ public_message=(+ f"The server at {url} answered without a completion. "+ "Check the base URL configured for this provider."+ ),+ )+# ------------------------------------------------------------------# chat# ------------------------------------------------------------------@@ -351,8 +390,10 @@ class OpenAIProvider:return itemsdef _normalize(self, resp: Any, model: str) -> LLMResponse:- choice = resp.choices[0] if resp.choices else None+ choices = getattr(resp, "choices", None)+ choice = choices[0] if choices else Noneif not choice:+ self._raise_if_error_body(resp)return LLMResponse(model=model)msg = choice.messagediff --git a/server/services/llm/unifier.py b/server/services/llm/unifier.pyindex 870959fb..08830595 100644--- a/server/services/llm/unifier.py+++ b/server/services/llm/unifier.py@@ -22,6 +22,8 @@ from typing import Any, Dict, List, Optional, TYPE_CHECKINGfrom core.encryption import fingerprint_credentialfrom core.logging import get_logger+from services.llm.config import ENDPOINT_PROVIDER, resolve_credential, split_provider_ref+from services.llm.endpoints import base_url_key, redact_urlfrom services.llm.protocol import (LLMError,LLMErrorCategory,@@ -99,12 +101,16 @@ class ChatUnifier:exception flows through unchanged so genuine server bugs keeptheir full traceback via ``BaseNode.execute()`` 's generic``except Exception``.++ ``provider`` is a provider reference: a registered id, or a named+ endpoint ``openai_compatible:<slug>`` (RFC-0003 D13)."""- spec = get_provider(provider)+ spec = get_provider(split_provider_ref(provider)[0])+ api_key = self._resolve_credential(provider, api_key)entry: Optional[_ClientEntry] = Nonetry:entry = await self._acquire_client(- spec, api_key, sdk_max_retries=sdk_max_retries+ spec, api_key, provider_ref=provider, sdk_max_retries=sdk_max_retries)return await entry.client.chat(messages,@@ -115,19 +121,18 @@ class ChatUnifier:tools=tools,context_management=context_management,)+ except LLMError as error:+ # Raised by provider logic rather than the SDK, e.g. a 2xx that+ # carried an error body instead of a completion.+ if not translate_errors:+ raise+ self._log_failure("LLM provider request failed", error, entry)+ raise NodeUserError(error.user_message) from errorexcept spec.sdk_exception_types as e:error = LLMError.from_exception(provider, e)if not translate_errors:raise error from e- logger.warning(- "LLM provider request failed",- provider=error.provider,- category=error.category.value,- retryable=error.retryable,- status_code=error.status_code,- provider_code=error.provider_code,- request_id=error.request_id,- )+ self._log_failure("LLM provider request failed", error, entry)raise NodeUserError(error.user_message) from errorexcept (ValueError, TypeError, OSError) as e:# Only normalize generic configuration/transport failures raised@@ -168,24 +173,20 @@ class ChatUnifier:uniformly. An absent key is a no-op — every provider gets thefilter for free without per-provider Python."""- spec = get_provider(provider)+ spec = get_provider(split_provider_ref(provider)[0])+ api_key = self._resolve_credential(provider, api_key)entry: Optional[_ClientEntry] = Nonetry:entry = await self._acquire_client(- spec, api_key, sdk_max_retries=2+ spec, api_key, provider_ref=provider, sdk_max_retries=2)models = await entry.client.fetch_models(api_key)+ except LLMError as error:+ self._log_failure("LLM model-list request failed", error, entry)+ raise NodeUserError(error.user_message) from errorexcept spec.sdk_exception_types as e:error = LLMError.from_exception(provider, e)- logger.warning(- "LLM model-list request failed",- provider=error.provider,- category=error.category.value,- retryable=error.retryable,- status_code=error.status_code,- provider_code=error.provider_code,- request_id=error.request_id,- )+ self._log_failure("LLM model-list request failed", error, entry)raise NodeUserError(error.user_message) from errorexcept (ValueError, TypeError, OSError) as e:if entry is not None:@@ -224,7 +225,7 @@ class ChatUnifier:``services/llm/factory.py`` — the unifier IS the routing layer,so registry membership is the source of truth."""- return has_provider(provider)+ return has_provider(split_provider_ref(provider)[0])# ------------------------------------------------------------------# internals@@ -253,17 +254,19 @@ class ChatUnifier:api_key: str,*,sdk_max_retries: int,+ provider_ref: Optional[str] = None,) -> LLMProvider:"""Instantiate the provider implementation.Pulls the user-configured ``{provider}_proxy`` URL from the- encrypted credentials store (matches the legacy ai.py behavior)- and merges it with the provider's static ``client_kwargs`` (used- by OpenAI-compatible providers to pin their ``base_url``).+ encrypted credentials store and merges it with the provider's+ static ``client_kwargs`` (used by OpenAI-compatible providers to+ pin their ``base_url``)."""entry = await self._get_or_create_entry(spec,api_key,+ provider_ref=provider_ref,sdk_max_retries=sdk_max_retries,acquire=False,)@@ -275,12 +278,14 @@ class ChatUnifier:api_key: str,*,sdk_max_retries: int,+ provider_ref: Optional[str] = None,) -> _ClientEntry:"""Return an entry leased until ``_release_client`` is called."""return await self._get_or_create_entry(spec,api_key,+ provider_ref=provider_ref,sdk_max_retries=sdk_max_retries,acquire=True,)@@ -292,8 +297,20 @@ class ChatUnifier:*,sdk_max_retries: int,acquire: bool,+ provider_ref: Optional[str] = None,) -> _ClientEntry:- proxy_url = await self._auth.get_api_key(f"{spec.name}_proxy")+ # The base-URL row is keyed by the full reference, so each named+ # endpoint reads its own URL while sharing one registration.+ ref = provider_ref or spec.name+ proxy_url = await self._auth.get_api_key(base_url_key(ref))+ if spec.name == ENDPOINT_PROVIDER and not proxy_url:+ # A named endpoint exists only as its credential rows. Without+ # the URL row the SDK would default to api.openai.com.+ slug = split_provider_ref(ref)[1] or ref+ raise NodeUserError(+ f"The OpenAI-compatible endpoint '{slug}' is not configured. "+ "Add it under Credentials."+ )factory_kwargs = {"api_key": api_key,"proxy_url": proxy_url,@@ -392,7 +409,44 @@ class ChatUnifier:"""Read ``providers.<name>.incompatible_models`` from llm_defaults.json."""raw = (self._defaults.get("providers", {})- .get(provider, {})+ .get(split_provider_ref(provider)[0], {}).get("incompatible_models"))return set(raw or ())++ @staticmethod+ def _resolve_credential(provider: str, api_key: Optional[str]) -> str:+ """The key to send, or a user-facing error before any client exists.++ An empty key must never reach an SDK: the OpenAI SDK would read+ ``OPENAI_API_KEY`` and send it to the configured base URL+ (RFC-0003 D7). Keyless local servers get their declared placeholder.+ """+ try:+ return resolve_credential(provider, api_key)+ except ValueError:+ raise NodeUserError(+ f"No API key is configured for {provider!r}. Add one under Credentials."+ ) from None++ @staticmethod+ def _log_failure(event: str, error: LLMError, entry: Optional[_ClientEntry]) -> None:+ """One WARN line per provider failure, naming where the call went.++ ``url`` is redacted (no userinfo, query or fragment) and+ ``url_source`` says which setting produced it, so a routing mistake+ is distinguishable from a model failure (RFC-0003 D10). The key is+ never logged.+ """+ client = entry.client if entry is not None else None+ logger.warning(+ event,+ provider=error.provider,+ category=error.category.value,+ retryable=error.retryable,+ status_code=error.status_code,+ provider_code=error.provider_code,+ request_id=error.request_id,+ url=redact_url(getattr(client, "endpoint_url", None)),+ url_source=getattr(client, "url_source", None),+ )diff --git a/server/services/model_registry.py b/server/services/model_registry.pyindex d330e387..46fe00a9 100644--- a/server/services/model_registry.py+++ b/server/services/model_registry.py@@ -1,8 +1,19 @@"""Model Parameter Management Service.Centralized registry for model metadata (max_output_tokens, context_length,-temperature constraints, thinking capabilities). Fetches from OpenRouter's-public API and caches to JSON. Falls back to llm_defaults.json offline.+temperature constraints, thinking capabilities). Three sources, three files:++- OpenRouter's public model list, cached to ``config/model_registry.json``+ (a tracked, curated snapshot) and refreshed on a 24 h gate.+- Models registered from the user's own servers (Ollama, LM Studio, named+ OpenAI-compatible endpoints) when their credential is saved. These are+ per-install state, so they persist under DATA_DIR, never in the tracked+ snapshot, and an OpenRouter refresh never replaces them.+- LiteLLM's ``model_prices_and_context_window.json``, consulted only when an+ endpoint is saved, to size and price model ids its server does not+ describe (RFC-0003 D15). Cached under DATA_DIR on the same 24 h gate.++Falls back to llm_defaults.json offline."""import json@@ -26,6 +37,30 @@ OPENROUTER_MODELS_URL = "https://openrouter.ai/api/v1/models"CACHE_FILE = Path(__file__).parent.parent / "config" / "model_registry.json"CACHE_MAX_AGE = timedelta(hours=24)+LITELLM_MODELS_URL = "https://raw.githubusercontent.com/BerriAI/litellm/main/model_prices_and_context_window.json"+# The only LiteLLM fields anything reads. Everything else is dropped at+# fetch time, so the cache holds a few hundred KB, not the full table.+_LITELLM_FIELDS = (+ "max_input_tokens",+ "max_output_tokens",+ "input_cost_per_token",+ "output_cost_per_token",+)+++def _local_models_path() -> Path:+ """Where models registered from the user's own servers persist."""+ from core.paths import data_path++ return data_path("local_models.json")+++def _litellm_cache_path() -> Path:+ """Where the trimmed LiteLLM table is cached."""+ from core.paths import data_path++ return data_path("litellm_models.json")+# Provider normalization: OpenRouter provider -> OpenCompany providerPROVIDER_MAP = {"google": "gemini",@@ -115,7 +150,11 @@ class ModelRegistryService:"""def __init__(self):- self._models: Dict[str, ModelInfo] = {} # keyed by "provider/local_id"+ self._models: Dict[str, ModelInfo] = {} # OpenRouter, keyed by "provider/local_id"+ # Registered from the user's own servers, keyed "<provider ref>/<model id>".+ # Kept apart so an OpenRouter refresh cannot replace them.+ self._local_models: Dict[str, ModelInfo] = {}+ self._litellm: Dict[str, Dict[str, Any]] = {}self._cache_timestamp: Optional[datetime] = Noneself._llm_defaults: Dict[str, Any] = {}@@ -124,9 +163,11 @@ class ModelRegistryService:# -------------------------------------------------------------------------def startup(self) -> None:- """Synchronously load from cache file and llm_defaults.json."""+ """Synchronously load the caches and llm_defaults.json."""self._load_llm_defaults()self._load_cache()+ self._load_local_models()+ self._load_litellm_cache()count = len(self._models)if count > 0:logger.info(f"Model registry loaded {count} models from cache")@@ -141,6 +182,16 @@ class ModelRegistryService:return age > CACHE_MAX_AGEasync def refresh(self) -> int:+ """Refresh the LiteLLM table, then the OpenRouter snapshot.++ The LiteLLM refresh never raises: it only sizes endpoint models at+ save time, and a stale copy beats none. Returns the number of+ OpenRouter models loaded.+ """+ await self._refresh_litellm()+ return await self._refresh_openrouter()++ async def _refresh_openrouter(self) -> int:"""Fetch model data from OpenRouter public API and update cache.Returns the number of models loaded.@@ -206,19 +257,24 @@ class ModelRegistryService:# Strip [FREE] prefix if presentmodel = model.replace("[FREE] ", "")variants = self._model_variants(model)+ # Models registered from the user's own servers go first: they+ # carry the context the server actually loaded.+ sources = (self._local_models, self._models)# 1. Exact match (try all dot/hyphen variants)- for variant in variants:- key = f"{provider}/{variant}"- if key in self._models:- return self._models[key]+ for source in sources:+ for variant in variants:+ key = f"{provider}/{variant}"+ if key in source:+ return source[key]# 2. Prefix match (e.g., claude-3-5-sonnet-20241022 -> claude-3-5-sonnet)- for stored_key, info in self._models.items():- if info.provider == provider:- for variant in variants:- if variant.startswith(info.local_id) or info.local_id.startswith(variant):- return info+ for source in sources:+ for stored_key, info in source.items():+ if info.provider == provider:+ for variant in variants:+ if variant.startswith(info.local_id) or info.local_id.startswith(variant):+ return info# 3. Cross-provider lookup (OpenRouter only - same model on different# providers can have different context windows and limits)@@ -243,30 +299,27 @@ class ModelRegistryService:model_id: str,params: Dict[str, Any],) -> None:- """Register an Ollama / LM Studio model with its actual params.-- Called from ``validate_local_llm`` after the official SDK probe- (``ollama.AsyncClient.ps()`` / ``lmstudio.AsyncClient.llm.list_loaded()``)- returns each currently-loaded model with its typed params:- ``context_length`` (live n_ctx the server enforces),- ``vision`` / ``supports_tools`` capability flags, and metadata- (``architecture``, ``param_size``, ``quantization``). The sync- ``get_context_length`` / ``get_max_output_tokens`` lookups find- this entry first, so chat / agent execution honour the real- n_ctx the server is serving — no JSON guess, no string parsing.-- Stored under the same ``{provider}/{local_id}`` key shape as- cloud models so ``get_model_info``'s waterfall matches without- any per-provider branching. ``provider`` is canonical- (``ollama`` / ``lmstudio``).-- Idempotent: a re-validation overwrites the prior entry.-- Persisted to the same ``model_registry.json`` cache the- OpenRouter refresh writes — the entry survives a server- restart, so the user doesn't have to re-click "Fetch" every- time the process bounces just to keep the n_ctx context- registered for their local model.+ """Register a model served by the user's own server, with its params.++ Called when an Ollama, LM Studio or named OpenAI-compatible+ endpoint credential is saved (``nodes/model/_local_validator.py``).+ ``params`` carries what the server reported, or what save-time+ enrichment found: ``context_length`` (the n_ctx the server+ enforces), optional ``max_output_tokens``, ``vision`` /+ ``supports_tools`` flags, and optional ``input_price_per_mtok`` /+ ``output_price_per_mtok``. The sync ``get_context_length`` /+ ``get_max_output_tokens`` lookups find this entry first, so chat+ and agent execution honour the real n_ctx — no JSON guess.++ ``provider`` is the provider reference (``ollama``, ``lmstudio``,+ ``openai_compatible:<slug>``); the key shape is the same+ ``{provider}/{local_id}`` as cloud models, so ``get_model_info``+ matches without per-provider branching.++ Idempotent: a re-validation overwrites the prior entry. Persisted+ under DATA_DIR, not in the tracked OpenRouter snapshot, so it+ survives a restart without dirtying the repo, and an OpenRouter+ refresh cannot replace it."""ctx = int(params.get("context_length") or 0)max_out = int(params.get("max_output_tokens") or 0)@@ -297,10 +350,12 @@ class ModelRegistryService:local_id=model_id,context_length=ctx,max_output_tokens=max_out,+ input_price_per_mtok=float(params.get("input_price_per_mtok") or 0.0),+ output_price_per_mtok=float(params.get("output_price_per_mtok") or 0.0),temperature_range=(0.0, 2.0),supported_parameters=supported,)- self._models[f"{provider}/{model_id}"] = info+ self._local_models[f"{provider}/{model_id}"] = infologger.info("[%s] registered model %s (ctx=%s, max_out=%s, tools=%s, vision=%s)",provider,@@ -311,10 +366,21 @@ class ModelRegistryService:params.get("vision", False),)# Persist so the entry survives process restart.- try:- self._save_cache()- except Exception as e: # noqa: BLE001 — best-effort persistence- logger.warning("Failed to persist local model entry %s/%s: %s", provider, model_id, e)+ self._save_local_models()++ def forget_local_models(self, provider: str) -> None:+ """Drop every model registered for ``provider`` (a provider reference).++ Called when an endpoint is removed or re-saved, so a model the+ server no longer serves cannot keep answering lookups.+ """+ prefix = f"{provider}/"+ stale = [key for key in self._local_models if key.startswith(prefix)]+ if not stale:+ return+ for key in stale:+ del self._local_models[key]+ self._save_local_models()def get_max_output_tokens(self, model: str, provider: str) -> int:"""Get max output tokens: registry -> llm_defaults -> 4096."""@@ -339,9 +405,7 @@ class ModelRegistryService:return info.temperature_range# Fallback to llm_defaults or provider default- providers = self._llm_defaults.get("providers", {})- prov_cfg = providers.get(provider, {})- temp_range = prov_cfg.get("temperature_range")+ temp_range = self._provider_defaults(provider).get("temperature_range")if temp_range and isinstance(temp_range, list) and len(temp_range) == 2:return tuple(temp_range)@@ -354,8 +418,7 @@ class ModelRegistryService:return info.is_reasoning_model# Fallback: check llm_defaults reasoning_models list- providers = self._llm_defaults.get("providers", {})- reasoning_list = providers.get(provider, {}).get("reasoning_models", [])+ reasoning_list = self._provider_defaults(provider).get("reasoning_models", [])for pattern in reasoning_list:if model.startswith(pattern):return True@@ -377,8 +440,7 @@ class ModelRegistryService:return info.thinking_type# Fallback: check llm_defaults- providers = self._llm_defaults.get("providers", {})- prov_cfg = providers.get(provider, {})+ prov_cfg = self._provider_defaults(provider)# Check if model matches thinking_models patternsthinking_models = prov_cfg.get("thinking_models", [])@@ -559,6 +621,132 @@ class ModelRegistryService:except Exception as e:logger.warning(f"Failed to save model registry cache: {e}")+ def _load_local_models(self) -> None:+ """Load models registered from the user's own servers."""+ path = _local_models_path()+ if not path.exists():+ return+ try:+ with open(path, encoding="utf-8") as f:+ raw_models = json.load(f).get("models", {})+ for key, model_data in raw_models.items():+ try:+ self._local_models[key] = ModelInfo.from_dict(model_data)+ except (TypeError, KeyError) as e:+ logger.debug(f"Skipping invalid local model entry {key}: {e}")+ except Exception as e:+ logger.warning(f"Failed to load local model registry: {e}")++ def _save_local_models(self) -> None:+ """Persist models registered from the user's own servers. Best effort."""+ path = _local_models_path()+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ with open(path, "w", encoding="utf-8") as f:+ json.dump(+ {"models": {key: info.to_dict() for key, info in self._local_models.items()}},+ f,+ indent=2,+ default=str,+ )+ except Exception as e: # noqa: BLE001 — the in-memory entry still serves this process+ logger.warning(f"Failed to persist local model registry: {e}")++ # -------------------------------------------------------------------------+ # LiteLLM table (endpoint save-time sizing and pricing, RFC-0003 D15)+ # -------------------------------------------------------------------------++ def lookup_litellm(self, model_id: str) -> Optional[Dict[str, Any]]:+ """LiteLLM's entry for ``model_id``, or ``None``.++ An exact key wins. Otherwise a key whose part after the first ``/``+ (LiteLLM prefixes non-OpenAI ids with their host, e.g.+ ``together_ai/...``) equals the id, but only when exactly one key+ matches: hosts size and price the same model differently, so an+ ambiguous match is no match.+ """+ if not model_id:+ return None+ entry = self._litellm.get(model_id)+ if entry is not None:+ return entry+ matches = [spec for key, spec in self._litellm.items() if key.partition("/")[2] == model_id]+ return matches[0] if len(matches) == 1 else None++ async def ensure_litellm_table(self) -> None:+ """Fetch the LiteLLM table if this process has none yet.++ The 24 h refresh keeps it current; this covers a first endpoint+ save on a machine whose OpenRouter snapshot was still fresh, so+ the refresh has not run yet.+ """+ if not self._litellm:+ await self._refresh_litellm()++ async def _refresh_litellm(self) -> None:+ """Fetch and trim the LiteLLM table. Never raises."""+ try:+ async with httpx.AsyncClient(timeout=30.0) as client:+ response = await client.get(LITELLM_MODELS_URL)+ response.raise_for_status()+ raw = response.json()+ except Exception as e: # noqa: BLE001 — a stale copy beats none+ logger.warning(f"Model registry: LiteLLM table refresh failed, keeping the cached copy: {e}")+ return++ table = self._parse_litellm(raw)+ if not table:+ logger.warning("Model registry: LiteLLM table had no chat models, keeping the cached copy")+ return+ self._litellm = table+ self._save_litellm_cache()+ logger.info(f"Model registry: loaded {len(table)} chat models from LiteLLM")++ @staticmethod+ def _parse_litellm(raw: Any) -> Dict[str, Dict[str, Any]]:+ """Keep chat-mode entries and the four fields anything reads."""+ table: Dict[str, Dict[str, Any]] = {}+ if not isinstance(raw, dict):+ return table+ for model_id, spec in raw.items():+ if not isinstance(spec, dict) or spec.get("mode") != "chat":+ continue+ entry = {+ name: spec[name]+ for name in _LITELLM_FIELDS+ if isinstance(spec.get(name), (int, float)) and not isinstance(spec.get(name), bool)+ }+ if entry:+ table[model_id] = entry+ return table++ def _load_litellm_cache(self) -> None:+ path = _litellm_cache_path()+ if not path.exists():+ return+ try:+ with open(path, encoding="utf-8") as f:+ self._litellm = self._parse_litellm(json.load(f).get("models", {}))+ except Exception as e:+ logger.warning(f"Failed to load LiteLLM table cache: {e}")++ def _save_litellm_cache(self) -> None:+ path = _litellm_cache_path()+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ with open(path, "w", encoding="utf-8") as f:+ # Re-stamped with "mode" so the loader's parse applies unchanged.+ json.dump(+ {+ "_source": LITELLM_MODELS_URL,+ "_generated": datetime.now(timezone.utc).isoformat(),+ "models": {key: {**spec, "mode": "chat"} for key, spec in self._litellm.items()},+ },+ f,+ )+ except Exception as e: # noqa: BLE001 — the in-memory table still serves this process+ logger.warning(f"Failed to persist LiteLLM table cache: {e}")+# -------------------------------------------------------------------------# LLM defaults fallback# -------------------------------------------------------------------------@@ -601,10 +789,19 @@ class ModelRegistryService:passreturn block+ def _provider_defaults(self, provider: str) -> Dict[str, Any]:+ """The llm_defaults.json block for a provider reference.++ A named endpoint (``openai_compatible:<slug>``) reads the shared+ ``openai_compatible`` block.+ """+ from services.llm.config import split_provider_ref++ return self._llm_defaults.get("providers", {}).get(split_provider_ref(provider)[0], {})+def _get_default_max_output_tokens(self, provider: str, model: str) -> int:"""Fallback: get max output tokens from llm_defaults.json."""- providers = self._llm_defaults.get("providers", {})- token_map = providers.get(provider, {}).get("max_output_tokens", {})+ token_map = self._provider_defaults(provider).get("max_output_tokens", {})variants = self._model_variants(model)# Exact match (try dot/hyphen variants)@@ -623,8 +820,7 @@ class ModelRegistryService:def _get_default_context_length(self, provider: str, model: str) -> int:"""Fallback: get context length from llm_defaults.json."""- providers = self._llm_defaults.get("providers", {})- ctx_map = providers.get(provider, {}).get("context_length", {})+ ctx_map = self._provider_defaults(provider).get("context_length", {})variants = self._model_variants(model)# Exact match (try dot/hyphen variants)diff --git a/server/tests/llm/test_endpoint_resolution.py b/server/tests/llm/test_endpoint_resolution.pynew file mode 100644index 00000000..ffe0b210--- /dev/null+++ b/server/tests/llm/test_endpoint_resolution.py@@ -0,0 +1,135 @@+"""RFC-0003 §6 / AG3: rooting a user-supplied base URL by asking the server.++The probe goes through the real openai SDK (``models.list()``), mocked at the+HTTP layer, because "the probe is the call execution makes" is the property+under test.+"""++from __future__ import annotations++import httpx+import pytest+import respx++from services.llm.endpoints import redact_url, resolve_base_url++MODELS = {+ "object": "list",+ "data": [{"id": "qwen3", "object": "model", "created": 0, "owned_by": "local"}],+}+++async def test_a_list_at_the_root_is_adopted_as_entered():+ respx.get("http://host:8080/models").mock(return_value=httpx.Response(200, json=MODELS))+ v1 = respx.get("http://host:8080/v1/models")++ resolved = await resolve_base_url("http://host:8080/", api_key="k")++ assert resolved.ok+ assert resolved.base_url == "http://host:8080"+ assert resolved.rewritten is False+ assert [m.id for m in resolved.models] == ["qwen3"]+ assert not v1.called+++async def test_a_200_that_is_not_a_list_is_not_adopted():+ """LM Studio answers routes it does not serve with HTTP 200 (RFC-0003 §2.1)."""+ respx.get("http://host:1234/models").mock(+ return_value=httpx.Response(200, json={"error": "Unexpected endpoint or method. (GET /models)"})+ )+ respx.get("http://host:1234/v1/models").mock(return_value=httpx.Response(200, json=MODELS))++ resolved = await resolve_base_url("http://host:1234", api_key="lm-studio")++ assert resolved.base_url == "http://host:1234/v1"+ assert resolved.rewritten is True+++async def test_a_rejected_key_at_the_root_does_not_stop_a_list_under_v1():+ respx.get("http://gateway/models").mock(return_value=httpx.Response(401, json={"error": {"message": "no"}}))+ respx.get("http://gateway/v1/models").mock(return_value=httpx.Response(200, json=MODELS))++ resolved = await resolve_base_url("http://gateway", api_key="k")++ assert resolved.base_url == "http://gateway/v1"+++async def test_a_key_rejected_everywhere_is_reported_and_nothing_adopted():+ respx.get("http://vllm:8000/models").mock(return_value=httpx.Response(404))+ respx.get("http://vllm:8000/v1/models").mock(return_value=httpx.Response(401, json={"error": "Unauthorized"}))++ resolved = await resolve_base_url("http://vllm:8000", api_key="wrong")++ assert not resolved.ok+ assert "rejected the API key" in resolved.reason+ assert "http://vllm:8000/v1" in resolved.reason+++async def test_nothing_answering_names_both_candidates():+ respx.get("http://host/models").mock(return_value=httpx.Response(404))+ respx.get("http://host/v1/models").mock(return_value=httpx.Response(404))++ resolved = await resolve_base_url("http://host", api_key="k")++ assert not resolved.ok+ assert "http://host or http://host/v1" in resolved.reason+++async def test_a_connection_failure_stops_after_one_candidate():+ root = respx.get("http://down:9/models").mock(side_effect=httpx.ConnectError("refused"))+ v1 = respx.get("http://down:9/v1/models")++ resolved = await resolve_base_url("http://down:9", api_key="k")++ assert not resolved.ok+ assert "Could not reach http://down:9" in resolved.reason+ assert root.call_count == 1+ assert not v1.called+++async def test_input_ending_in_v1_is_the_only_candidate():+ v1 = respx.get("http://host/v1/models").mock(return_value=httpx.Response(404))+ doubled = respx.get("http://host/v1/v1/models")++ resolved = await resolve_base_url("http://host/v1", api_key="k")++ assert not resolved.ok+ assert v1.called+ assert not doubled.called+++async def test_a_scheme_less_url_is_refused_not_guessed():+ resolved = await resolve_base_url("localhost:8080", api_key="k")++ assert not resolved.ok+ assert "http://" in resolved.reason+++async def test_the_probe_sends_the_resolved_key_as_a_bearer_token():+ route = respx.get("http://host/models").mock(return_value=httpx.Response(200, json=MODELS))++ await resolve_base_url("http://host", api_key="sk-no-key-required")++ assert route.calls.last.request.headers["authorization"] == "Bearer sk-no-key-required"+++ ("url", "expected"),+ [+ ("http://user:pass@host:8080/v1?key=secret#frag", "http://host:8080/v1"),+ ("https://api.example.com/v1", "https://api.example.com/v1"),+ ("http://[::1]:1234/v1", "http://[::1]:1234/v1"),+ ("http://host:notaport/v1", "http://host/v1"),+ (None, ""),+ ],+)+def test_redact_url_drops_what_can_carry_a_credential(url, expected):+ assert redact_url(url) == expecteddiff --git a/server/tests/llm/test_protocol_guard.py b/server/tests/llm/test_protocol_guard.pynew file mode 100644index 00000000..cbca3464--- /dev/null+++ b/server/tests/llm/test_protocol_guard.py@@ -0,0 +1,113 @@+"""RFC-0003 D6 / D9 / AG10 / AG11 at the provider boundary.++A 2xx that carries an error body instead of a completion is a routing+failure, not an empty answer, and must say which URL answered. The key a+user stored is sent as stored: no provider rewrites it to a placeholder.+"""++from __future__ import annotations++from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++import services.llm # noqa: F401 — populate the registry+from services.llm.protocol import LLMError, LLMErrorCategory, LLMResponse, Message+from services.llm.providers.anthropic import AnthropicProvider+from services.llm.providers.openai import OpenAIProvider+from services.llm.registry import ProviderSpec+from services.llm.unifier import ChatUnifier+from services.plugin import NodeUserError++LEAKY_URL = "http://user:pw@host:1234/?token=t"+++def _provider(**kwargs) -> OpenAIProvider:+ with patch("openai.AsyncOpenAI") as client_cls:+ client_cls.return_value.base_url = kwargs.get("proxy_url") or kwargs.get("base_url") or "https://api.openai.com/v1"+ return OpenAIProvider("k", provider_name="lmstudio", **kwargs)+++class TestTwoHundredWithoutChoices:+ def test_an_error_body_raises_protocol_naming_the_redacted_url(self):+ provider = _provider(proxy_url=LEAKY_URL)+ response = SimpleNamespace(+ choices=None,+ usage=None,+ model_extra={"error": "Unexpected endpoint or method. (POST /chat/completions)"},+ )++ with pytest.raises(LLMError) as raised:+ provider._normalize(response, "m")++ error = raised.value+ assert error.category is LLMErrorCategory.PROTOCOL+ message = error.user_message+ assert "http://host:1234/" in message+ for secret in ("user", "pw", "token", "Unexpected endpoint", "Bearer"):+ assert secret not in message++ def test_no_choices_and_no_error_is_still_an_empty_response(self):+ response = SimpleNamespace(choices=[], usage=None, model_extra={})++ normalized = _provider()._normalize(response, "m")++ assert isinstance(normalized, LLMResponse)+ assert normalized.content == ""++ def test_a_mock_response_is_not_mistaken_for_an_error_body(self):+ response = MagicMock()+ response.choices = []++ assert _provider()._normalize(response, "m").content == ""++ async def test_the_unifier_turns_it_into_one_user_facing_error(self):+ error = LLMError(+ message="raw",+ provider="lmstudio",+ category=LLMErrorCategory.PROTOCOL,+ public_message="The server at http://host:1234 answered without a completion.",+ )+ client = MagicMock()+ client.chat = AsyncMock(side_effect=error)+ spec = ProviderSpec(name="lmstudio", factory=MagicMock(return_value=client), sdk_exception_refs=("openai:OpenAIError",))+ auth = MagicMock()+ auth.get_api_key = AsyncMock(return_value=None)+ unifier = ChatUnifier(defaults={"providers": {}}, auth_service=auth)++ with patch("services.llm.unifier.get_provider", return_value=spec):+ with pytest.raises(NodeUserError, match="answered without a completion"):+ await unifier.chat(+ provider="lmstudio",+ api_key="k",+ messages=[Message(role="user", content="hi")],+ model="m",+ )+++class TestTheStoredKeyIsSentAsStored:+ def test_openai_compatible_client_keeps_the_key_with_a_proxy(self):+ with patch("openai.AsyncOpenAI") as client_cls:+ provider = OpenAIProvider("real-key", proxy_url="http://host:1234/v1", provider_name="lmstudio")+ assert client_cls.call_args.kwargs["api_key"] == "real-key"+ assert client_cls.call_args.kwargs["base_url"] == "http://host:1234/v1"+ assert provider.url_source == "proxy"++ def test_anthropic_client_keeps_the_key_with_a_proxy(self):+ with patch("anthropic.AsyncAnthropic") as client_cls:+ AnthropicProvider("real-key", proxy_url="http://relay:8080")+ assert client_cls.call_args.kwargs["api_key"] == "real-key"++ @pytest.mark.parametrize(+ ("kwargs", "source"),+ [+ ({"proxy_url": "http://a/v1", "base_url": "http://b/v1"}, "proxy"),+ ({"base_url": "http://b/v1"}, "llm_defaults"),+ ({}, "sdk_default"),+ ],+ )+ def test_url_source_names_the_setting_that_produced_the_url(self, kwargs, source):+ with patch("openai.AsyncOpenAI"):+ provider = OpenAIProvider("k", **kwargs)+ assert provider.url_source == sourcediff --git a/server/tests/llm/test_rfc0003_source_contract.py b/server/tests/llm/test_rfc0003_source_contract.pynew file mode 100644index 00000000..9f0f5ec4--- /dev/null+++ b/server/tests/llm/test_rfc0003_source_contract.py@@ -0,0 +1,137 @@+"""RFC-0003 source gates: rules a code review would otherwise have to catch.++AST scans in the same style as ``test_plugin_shape.py``. Each test names the+decision it locks, so a failure says which rule a change broke.+"""++from __future__ import annotations++import ast+import dataclasses+import json+from pathlib import Path+from typing import Iterator, List, Optional, Tuple++SERVER_DIR = Path(__file__).resolve().parents[2]+LLM_DIR = SERVER_DIR / "services" / "llm"+MODEL_NODES_DIR = SERVER_DIR / "nodes" / "model"+ENDPOINTS_PY = LLM_DIR / "endpoints.py"+LOCAL_VALIDATOR_PY = MODEL_NODES_DIR / "_local_validator.py"++# The two places allowed to touch a "/v1" path segment (AG2): the save-time+# resolver, and the native-host derivation used only by native probes.+V1_ALLOWED_FILES = {ENDPOINTS_PY}+V1_ALLOWED_FUNCTIONS = {(LOCAL_VALIDATOR_PY, "_strip_v1_path")}++# Keys in llm_defaults.json that nothing reads. Listed so a NEW dead key+# fails AG9; tracked for removal in RFC-0003 §12.+KNOWN_DEAD_JSON_KEYS = {"api_key_param", "max_tokens_param"}+++def _python_files(*roots: Path) -> Iterator[Path]:+ for root in roots:+ for path in sorted(root.rglob("*.py")):+ if "__pycache__" not in path.parts:+ yield path+++def _nodes_with_function(tree: ast.AST) -> Iterator[Tuple[ast.AST, Optional[str]]]:+ """Every node, paired with the name of the innermost enclosing function."""++ def walk(node: ast.AST, function: Optional[str]) -> Iterator[Tuple[ast.AST, Optional[str]]]:+ for child in ast.iter_child_nodes(node):+ name = child.name if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)) else function+ yield child, name+ yield from walk(child, name)++ yield from walk(tree, None)+++def _allowed(path: Path, function: Optional[str]) -> bool:+ return path in V1_ALLOWED_FILES or (path, function) in V1_ALLOWED_FUNCTIONS+++def test_no_code_appends_or_strips_a_v1_segment_outside_the_allowlist():+ """AG1 / AG2 / D1: a configured base URL is used verbatim.++ Full vendor URLs (``https://openrouter.ai/api/v1``) are declarations and+ allowed; what is forbidden is a ``/v1`` *fragment*, which only code that+ appends or strips a segment needs, and ``rstrip("/")`` normalization.+ """+ violations: List[str] = []+ for path in _python_files(LLM_DIR, MODEL_NODES_DIR):+ tree = ast.parse(path.read_text(encoding="utf-8"))+ for node, function in _nodes_with_function(tree):+ if _allowed(path, function):+ continue+ if isinstance(node, ast.Constant) and isinstance(node.value, str) and node.value.startswith("/v1"):+ violations.append(f"{path.relative_to(SERVER_DIR)}:{node.lineno} '/v1' fragment")+ if (+ isinstance(node, ast.Call)+ and isinstance(node.func, ast.Attribute)+ and node.func.attr == "rstrip"+ and any(isinstance(arg, ast.Constant) and arg.value == "/" for arg in node.args)+ ):+ violations.append(f"{path.relative_to(SERVER_DIR)}:{node.lineno} rstrip('/')")+ assert not violations, "RFC-0003 D1: never rewrite a base URL outside the resolver:\n" + "\n".join(violations)+++def _provider_init_methods() -> Iterator[Tuple[Path, str, ast.FunctionDef]]:+ for path in _python_files(LLM_DIR / "providers"):+ tree = ast.parse(path.read_text(encoding="utf-8"))+ for cls in (node for node in ast.walk(tree) if isinstance(node, ast.ClassDef)):+ for item in cls.body:+ if isinstance(item, ast.FunctionDef) and item.name == "__init__":+ yield path, cls.name, item+++def test_no_provider_constructor_hardcodes_a_placeholder_key():+ """AG7 / D6: placeholders are declared in llm_defaults.json and resolved once."""+ placeholders = {"ollama", "lm-studio", "sk-no-key-required"}+ for path, class_name, init in _provider_init_methods():+ literals = {n.value for n in ast.walk(init) if isinstance(n, ast.Constant) and isinstance(n.value, str)}+ assert not literals & placeholders, f"{path.name}:{class_name}.__init__ hardcodes {literals & placeholders}"+++def test_client_construction_does_no_io():+ """AG4: rooting happens at save time; building a client only reads the result."""+ for path, class_name, init in _provider_init_methods():+ assert not any(isinstance(n, ast.Await) for n in ast.walk(init)), f"{path.name}:{class_name}.__init__ awaits"++ for path in _python_files(SERVER_DIR / "services", SERVER_DIR / "nodes"):+ if path in (ENDPOINTS_PY, LOCAL_VALIDATOR_PY):+ continue+ assert "resolve_base_url" not in path.read_text(encoding="utf-8"), (+ f"{path.relative_to(SERVER_DIR)} probes a base URL; only the save path may"+ )+++def test_provider_spec_gained_no_field():+ """AG12 / D11: per-provider facts are declared in JSON, not on the spec."""+ from services.provider_registry import ProviderSpec++ assert {f.name for f in dataclasses.fields(ProviderSpec)} == {+ "name",+ "factory",+ "sdk_exception_refs",+ "client_kwargs",+ }+++def test_every_llm_defaults_key_has_a_reader():+ """AG9 / D8: a flag ships only with a consumer.++ Every provider-block key must appear as a string literal somewhere in the+ server's code. A key nothing reads is how configuration goes quietly dead.+ """+ blocks = json.loads((SERVER_DIR / "config" / "llm_defaults.json").read_text(encoding="utf-8"))["providers"]+ keys = {key for block in blocks.values() for key in block if not key.startswith("_")}++ literals = set()+ for path in _python_files(SERVER_DIR / "services", SERVER_DIR / "nodes", SERVER_DIR / "routers"):+ tree = ast.parse(path.read_text(encoding="utf-8"))+ literals |= {n.value for n in ast.walk(tree) if isinstance(n, ast.Constant) and isinstance(n.value, str)}++ unread = keys - literals - KNOWN_DEAD_JSON_KEYS+ assert not unread, f"llm_defaults.json keys nothing reads: {sorted(unread)}"+ assert KNOWN_DEAD_JSON_KEYS <= keys, "a known-dead key was removed; drop it from KNOWN_DEAD_JSON_KEYS"diff --git a/server/tests/nodes/test_local_llm_save.py b/server/tests/nodes/test_local_llm_save.pynew file mode 100644index 00000000..0e56ab9a--- /dev/null+++ b/server/tests/nodes/test_local_llm_save.py@@ -0,0 +1,212 @@+"""RFC-0003 §6 save path: Ollama, LM Studio and named endpoints.++AG6 (a failed save changes nothing), AG13 (kind detection by body shape)+and AG14 (per-kind metadata; LiteLLM never feeds a local kind). HTTP is+mocked with respx; the Ollama / LM Studio SDK probes are replaced, since+what matters here is that they are still the ones called.+"""++from __future__ import annotations++from typing import Any, Dict, List, Optional+from unittest.mock import AsyncMock, MagicMock++import httpx+import pytest+import respx++import nodes.model._local_validator as lv++REF = "openai_compatible:home"+++def _models(*ids: str, **extra: Any) -> Dict[str, Any]:+ return {"object": "list", "data": [{"id": mid, "object": "model", **extra.get(mid, {})} for mid in ids]}+++class FakeAuth:+ """In-memory stand-in for AuthService's API-key surface."""++ def __init__(self) -> None:+ self.rows: Dict[str, Dict[str, Any]] = {}+ self.stored: List[str] = []++ async def store_api_key(self, provider, api_key, models, session_id="default", model_params=None):+ self.stored.append(provider)+ self.rows[provider] = {"key": api_key, "models": list(models), "model_params": model_params or {}}+ return True++ async def get_api_key(self, provider, session_id="default") -> Optional[str]:+ row = self.rows.get(provider)+ return row["key"] if row else None+++def auth(monkeypatch) -> FakeAuth:+ fake = FakeAuth()+ monkeypatch.setattr(lv, "get_auth_service", lambda: fake)+ return fake+++def broadcaster(monkeypatch) -> MagicMock:+ fake = MagicMock()+ fake.update_api_key_status = AsyncMock()+ monkeypatch.setattr(lv, "get_status_broadcaster", lambda: fake)+ return fake+++def registry(tmp_path, monkeypatch):+ import services.model_registry as mr++ monkeypatch.setattr(mr, "_local_models_path", lambda: tmp_path / "local_models.json")+ monkeypatch.setattr(mr, "_litellm_cache_path", lambda: tmp_path / "litellm_models.json")+ svc = mr.ModelRegistryService()+ svc._load_llm_defaults()+ svc.lookup_litellm = MagicMock(wraps=svc.lookup_litellm)+ svc.ensure_litellm_table = AsyncMock()+ monkeypatch.setattr(mr, "get_model_registry", lambda: svc)+ return svc+++class TestKindDetection:+ """Each native route counts only by body shape (LM Studio answers 200 to anything)."""++ LM_STUDIO_ERROR = {"error": "Unexpected endpoint or method. Returning 200 anyway"}++ @respx.mock+ async def test_llamacpp_is_recognized_by_props(self):+ props = {"default_generation_settings": {"n_ctx": 4096}}+ respx.get("http://host:8080/props").mock(return_value=httpx.Response(200, json=props))++ assert await lv._detect_kind("http://host:8080/v1", "k") == ("llamacpp", props)++ @respx.mock+ async def test_lm_studio_is_recognized_despite_answering_200_everywhere(self):+ respx.get("http://host:1234/props").mock(return_value=httpx.Response(200, json=self.LM_STUDIO_ERROR))+ respx.get("http://host:1234/api/v1/models").mock(return_value=httpx.Response(200, json={"models": []}))++ assert await lv._detect_kind("http://host:1234/v1", "k") == ("lmstudio", None)++ @respx.mock+ async def test_a_200_with_an_error_body_is_not_ollama(self):+ for path in ("/props", "/api/v1/models", "/api/version"):+ respx.get(f"http://host:1234{path}").mock(return_value=httpx.Response(200, json=self.LM_STUDIO_ERROR))++ assert await lv._detect_kind("http://host:1234/v1", "k") == ("generic", None)++ @respx.mock+ async def test_ollama_is_recognized_by_its_version_route(self):+ respx.get("http://host:11434/props").mock(return_value=httpx.Response(404))+ respx.get("http://host:11434/api/v1/models").mock(return_value=httpx.Response(404))+ respx.get("http://host:11434/api/version").mock(return_value=httpx.Response(200, json={"version": "0.12.0"}))++ assert await lv._detect_kind("http://host:11434/v1", "k") == ("ollama", None)++ @respx.mock+ async def test_anything_else_is_generic(self):+ respx.route(host="host").mock(return_value=httpx.Response(404))++ assert await lv._detect_kind("http://host:8000/v1", "k") == ("generic", None)+++class TestFailedSaveChangesNothing:+ @respx.mock+ async def test_an_unrooted_url_writes_and_broadcasts_nothing(self, auth, broadcaster, registry):+ respx.get("http://host/models").mock(return_value=httpx.Response(404))+ respx.get("http://host/v1/models").mock(return_value=httpx.Response(404))++ result = await lv.save_llm_server(REF, "http://host", None, display="home", label="home")++ assert result["valid"] is False+ assert "No OpenAI-compatible API" in result["message"]+ assert auth.stored == []+ broadcaster.update_api_key_status.assert_not_awaited()++ @respx.mock+ async def test_a_server_with_nothing_loaded_writes_nothing(self, auth, broadcaster, registry, monkeypatch):+ respx.get("http://host:11434/models").mock(return_value=httpx.Response(404))+ respx.get("http://host:11434/v1/models").mock(return_value=httpx.Response(200, json=_models("llama3:latest")))+ monkeypatch.setattr(lv, "_fetch_ollama_models", AsyncMock(return_value=[]))++ result = await lv.validate_local_llm({"provider": "ollama", "api_key": "http://host:11434"})++ assert result["valid"] is False+ assert "no models are loaded" in result["message"]+ assert auth.stored == []+ broadcaster.update_api_key_status.assert_not_awaited()+++class TestSaveByKind:+ @respx.mock+ async def test_ollama_keeps_its_sdk_probe_and_stores_both_rows(self, auth, broadcaster, registry, monkeypatch):+ respx.get("http://host:11434/models").mock(return_value=httpx.Response(404))+ respx.get("http://host:11434/v1/models").mock(return_value=httpx.Response(200, json=_models("llama3:latest")))+ probe = AsyncMock(return_value=[{"id": "llama3:latest", "context_length": 4096}])+ monkeypatch.setattr(lv, "_fetch_ollama_models", probe)++ result = await lv.validate_local_llm({"provider": "ollama", "api_key": "http://host:11434"})++ assert result["valid"] is True+ probe.assert_awaited_once_with("http://host:11434/v1")+ # The URL row first: a key row without one reads as "not configured".+ assert auth.stored == ["ollama_proxy", "ollama"]+ assert auth.rows["ollama_proxy"]["key"] == "http://host:11434/v1"+ row = auth.rows["ollama"]+ assert row["key"] == "ollama" # the documented placeholder, resolved from llm_defaults.json+ assert row["models"] == ["llama3:latest"]+ assert row["model_params"]["llama3:latest"] == {"context_length": 4096}+ assert row["model_params"]["_endpoint"]["kind"] == "ollama"+ assert registry.get_context_length("llama3:latest", "ollama") == 4096+ registry.lookup_litellm.assert_not_called()+ broadcaster.update_api_key_status.assert_awaited_once()++ @respx.mock+ async def test_llamacpp_is_sized_from_props_not_litellm(self, auth, broadcaster, registry):+ respx.get("http://host:8080/models").mock(return_value=httpx.Response(200, json=_models("model.gguf")))+ respx.get("http://host:8080/props").mock(+ return_value=httpx.Response(200, json={"default_generation_settings": {"n_ctx": 8192}})+ )++ result = await lv.save_llm_server(REF, "http://host:8080", None, display="llama", label="llama")++ assert result["valid"] is True and result["kind"] == "llamacpp"+ assert auth.rows[f"{REF}_proxy"]["key"] == "http://host:8080"+ assert auth.rows[REF]["key"] == "sk-no-key-required"+ assert auth.rows[REF]["model_params"]["model.gguf"] == {"context_length": 8192}+ registry.lookup_litellm.assert_not_called()++ @respx.mock+ async def test_generic_prefers_the_servers_context_then_litellm(self, auth, broadcaster, registry):+ respx.get("http://vllm:8000/models").mock(return_value=httpx.Response(404))+ respx.get("http://vllm:8000/v1/models").mock(+ return_value=httpx.Response(+ 200,+ json=_models("Qwen/Qwen3-8B", "gpt-4o", **{"Qwen/Qwen3-8B": {"max_model_len": 32768}}),+ )+ )+ for path in ("/props", "/api/v1/models", "/api/version"):+ respx.get(f"http://vllm:8000{path}").mock(return_value=httpx.Response(404))+ registry._litellm = {+ "gpt-4o": {+ "max_input_tokens": 128000,+ "max_output_tokens": 16384,+ "input_cost_per_token": 2.5e-06,+ "output_cost_per_token": 1e-05,+ }+ }++ result = await lv.save_llm_server(REF, "http://vllm:8000", "sk-user", display="vllm", label="vllm")++ assert result["valid"] is True and result["kind"] == "generic"+ params = auth.rows[REF]["model_params"]+ assert params["Qwen/Qwen3-8B"] == {"context_length": 32768}+ assert params["gpt-4o"] == {+ "context_length": 128000,+ "max_output_tokens": 16384,+ "input_price_per_mtok": 2.5,+ "output_price_per_mtok": 10.0,+ }+ assert auth.rows[REF]["key"] == "sk-user"+ assert registry.get_context_length("gpt-4o", REF) == 128000diff --git a/server/tests/services/test_model_registry_caches.py b/server/tests/services/test_model_registry_caches.pynew file mode 100644index 00000000..28aa8ed7--- /dev/null+++ b/server/tests/services/test_model_registry_caches.py@@ -0,0 +1,166 @@+"""RFC-0003 D15 / D16 / AG18: the model registry's three sources.++Models registered from the user's own servers live apart from the OpenRouter+snapshot (so a refresh cannot wipe them, and they never dirty the tracked+``config/model_registry.json``), and the LiteLLM table is trimmed, cached+under DATA_DIR, and matched conservatively.+"""++from __future__ import annotations++import json++import httpx+import pytest+import respx++import services.model_registry as mr++OPENROUTER = {+ "data": [+ {+ "id": "openai/gpt-test",+ "name": "OpenAI: GPT Test",+ "context_length": 400000,+ "top_provider": {"max_completion_tokens": 128000},+ "pricing": {"prompt": "0.000001", "completion": "0.000002"},+ }+ ]+}+LITELLM = {+ "sample_spec": {"mode": "chat", "max_input_tokens": "set to max input tokens"},+ "gpt-4o": {+ "mode": "chat",+ "max_input_tokens": 128000,+ "max_output_tokens": 16384,+ "input_cost_per_token": 2.5e-06,+ "output_cost_per_token": 1e-05,+ "litellm_provider": "openai",+ "supports_vision": True,+ },+ "together_ai/meta-llama/Llama-3.3-70B": {"mode": "chat", "max_input_tokens": 131072},+ "deepinfra/Qwen/Qwen3-8B": {"mode": "chat", "max_input_tokens": 32768},+ "fireworks_ai/Qwen/Qwen3-8B": {"mode": "chat", "max_input_tokens": 40960},+ "text-embedding-3-small": {"mode": "embedding", "max_input_tokens": 8191},+}+++def paths(tmp_path, monkeypatch):+ snapshot = tmp_path / "config" / "model_registry.json"+ monkeypatch.setattr(mr, "CACHE_FILE", snapshot)+ monkeypatch.setattr(mr, "_local_models_path", lambda: tmp_path / "data" / "local_models.json")+ monkeypatch.setattr(mr, "_litellm_cache_path", lambda: tmp_path / "data" / "litellm_models.json")+ return tmp_path+++def registry(paths):+ svc = mr.ModelRegistryService()+ svc._load_llm_defaults()+ return svc+++def _mock_feeds(litellm=LITELLM):+ respx.get(mr.OPENROUTER_MODELS_URL).mock(return_value=httpx.Response(200, json=OPENROUTER))+ return respx.get(mr.LITELLM_MODELS_URL).mock(return_value=httpx.Response(200, json=litellm))+++class TestLocalModels:+ @respx.mock+ async def test_a_refresh_does_not_wipe_models_registered_from_the_users_servers(self, registry):+ registry.register_local_model("ollama", "llama3:latest", {"context_length": 4096})+ _mock_feeds()++ await registry.refresh()++ assert registry.get_context_length("llama3:latest", "ollama") == 4096+ assert registry.get_context_length("gpt-test", "openai") == 400000++ @respx.mock+ async def test_they_persist_under_data_dir_never_in_the_tracked_snapshot(self, registry, paths):+ registry.register_local_model("openai_compatible:home", "qwen3", {"context_length": 32768})+ _mock_feeds()+ await registry.refresh()++ local = json.loads((paths / "data" / "local_models.json").read_text(encoding="utf-8"))+ snapshot = json.loads((paths / "config" / "model_registry.json").read_text(encoding="utf-8"))+ assert "openai_compatible:home/qwen3" in local["models"]+ assert not any(key.startswith(("openai_compatible:", "ollama/")) for key in snapshot["models"])++ reloaded = mr.ModelRegistryService()+ reloaded.startup()+ assert reloaded.get_context_length("qwen3", "openai_compatible:home") == 32768++ def test_forgetting_a_provider_drops_only_its_models(self, registry):+ registry.register_local_model("openai_compatible:a", "m", {"context_length": 1000})+ registry.register_local_model("openai_compatible:b", "m", {"context_length": 2000})++ registry.forget_local_models("openai_compatible:a")++ assert registry.get_model_info("m", "openai_compatible:a") is None+ assert registry.get_context_length("m", "openai_compatible:b") == 2000++ def test_a_registered_price_is_kept_for_pricing(self, registry):+ registry.register_local_model(+ "openai_compatible:gw", "gpt-4o", {"context_length": 128000, "input_price_per_mtok": 2.5, "output_price_per_mtok": 10.0}+ )++ info = registry.get_model_info("gpt-4o", "openai_compatible:gw")+ assert (info.input_price_per_mtok, info.output_price_per_mtok) == (2.5, 10.0)+++class TestLiteLLMTable:+ @respx.mock+ async def test_refresh_keeps_chat_models_and_the_four_fields_read(self, registry, paths):+ _mock_feeds()++ await registry.refresh()++ assert registry.lookup_litellm("gpt-4o") == {+ "max_input_tokens": 128000,+ "max_output_tokens": 16384,+ "input_cost_per_token": 2.5e-06,+ "output_cost_per_token": 1e-05,+ }+ assert registry.lookup_litellm("text-embedding-3-small") is None+ assert registry.lookup_litellm("sample_spec") is None+ assert (paths / "data" / "litellm_models.json").exists()++ @respx.mock+ async def test_a_host_prefixed_key_matches_only_when_unambiguous(self, registry):+ _mock_feeds()+ await registry.refresh()++ assert registry.lookup_litellm("meta-llama/Llama-3.3-70B") == {"max_input_tokens": 131072}+ # Two hosts serve it with different windows: no guess.+ assert registry.lookup_litellm("Qwen/Qwen3-8B") is None+ assert registry.lookup_litellm("never-heard-of-it") is None++ @respx.mock+ async def test_a_failed_fetch_keeps_the_cached_copy(self, registry):+ _mock_feeds()+ await registry.refresh()+ respx.get(mr.LITELLM_MODELS_URL).mock(side_effect=httpx.ConnectError("offline"))++ await registry._refresh_litellm()++ assert registry.lookup_litellm("gpt-4o") is not None++ @respx.mock+ async def test_offline_with_no_cache_simply_matches_nothing(self, registry):+ respx.get(mr.LITELLM_MODELS_URL).mock(side_effect=httpx.ConnectError("offline"))++ await registry.ensure_litellm_table()++ assert registry.lookup_litellm("gpt-4o") is None++ @respx.mock+ async def test_the_cache_is_reloaded_on_startup(self, registry):+ _mock_feeds()+ await registry.refresh()++ reloaded = mr.ModelRegistryService()+ reloaded.startup()++ assert reloaded.lookup_litellm("gpt-4o")["max_input_tokens"] == 128000