Coverage for src / lilbee / providers / local_servers / registry.py: 100%
33 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-06-28 01:01 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-06-28 01:01 +0000
1"""Registry of known local servers and lookup helpers over it."""
3from __future__ import annotations
5from lilbee.providers.local_servers.lm_studio import LM_STUDIO
6from lilbee.providers.local_servers.ollama import OLLAMA
7from lilbee.providers.local_servers.spec import LocalServerSpec
9LOCAL_SERVERS: tuple[LocalServerSpec, ...] = (OLLAMA, LM_STUDIO)
11LOCAL_SERVER_KEYS: frozenset[str] = frozenset(spec.key for spec in LOCAL_SERVERS)
14def openai_models_url(base_url: str) -> str:
15 """Build the ``/v1/models`` URL, tolerating a base that already ends in ``/v1``."""
16 trimmed = base_url.rstrip("/")
17 if trimmed.endswith("/v1"):
18 trimmed = trimmed[: -len("/v1")]
19 return f"{trimmed}/v1/models"
22def detect_local_server(base_url: str) -> LocalServerSpec | None:
23 """Return the local server whose URL patterns match *base_url*, if any."""
24 url_lower = base_url.lower()
25 for spec in LOCAL_SERVERS:
26 if any(pattern in url_lower for pattern in spec.url_patterns):
27 return spec
28 return None
31def local_server_for_key(key: str) -> LocalServerSpec | None:
32 """Return the spec with routing *key*, or ``None``."""
33 for spec in LOCAL_SERVERS:
34 if spec.key == key:
35 return spec
36 return None
39def canonical_local_ref(name: str, source_key: str) -> str:
40 """Prefix a bare *name* with its local server's wire prefix.
42 *source_key* is a ModelSource value (``"ollama"``). No-op for non-local
43 sources and for names already carrying the prefix, so callers can dedup
44 the installed and catalog views.
45 """
46 spec = local_server_for_key(source_key)
47 if spec is None or name.startswith(spec.wire_prefix):
48 return name
49 return spec.qualify(name)
52def local_server_for_label(label: str) -> LocalServerSpec | None:
53 """Return the spec matching *label* by routing key or display name.
55 Discovery stamps the display value (``"LM Studio"``); a parsed ref carries
56 the routing key (``"lm_studio"``). Both resolve to the same spec.
57 """
58 lowered = label.lower()
59 for spec in LOCAL_SERVERS:
60 if spec.key == lowered or spec.display_name.lower() == lowered:
61 return spec
62 return None