model_catalog.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. """ACP model picker: deduplicated ``provider:model`` rows from the Hermes inventory + named endpoints."""
  2. from __future__ import annotations
  3. import logging
  4. import os
  5. from dataclasses import dataclass, field
  6. from typing import Callable
  7. from acp.schema import ModelInfo, SessionModelState
  8. logger = logging.getLogger("acp_adapter.server")
  9. # Per-provider row cap (clients render all `availableModels` in one dropdown; mirrors the
  10. # MoA picker cap). Not a total cap; the current model is always kept via the fallback insert.
  11. ACP_MAX_MODELS_PER_PROVIDER = 200
  12. def _named_custom_provider_catalogs() -> list[tuple[str, str, list[tuple[str, str]]]]:
  13. """``(slug, label, [(model_id, description), ...])`` for named endpoints (v12 ``providers:``
  14. and legacy ``custom_providers:``), which canonical provider enumeration never lists.
  15. Models = the entry's declared models, refreshed from the live ``/models`` listing when a
  16. credential exists and ``discover_models`` isn't disabled; declared models survive a failed
  17. discovery (some endpoints have no ``/models`` route). Slugs use the ``custom:<name>`` shape
  18. ``parse_model_input``/``resolve_runtime_provider`` resolve, so choice ids round-trip."""
  19. try:
  20. from hermes_cli.config import (get_compatible_custom_providers, is_provider_enabled, load_config)
  21. from hermes_cli.model_switch import _declared_model_ids, _entry_models_discovered, _models_config_is_allowlist
  22. from hermes_cli.model_switch_providers import _NativePickerModelList, _fetch_picker_live_models
  23. from hermes_cli.model_switch_providers import _discover_flag
  24. from hermes_cli.models_local import should_use_ollama_native_catalog
  25. from hermes_cli.providers import custom_provider_slug
  26. except ImportError:
  27. return []
  28. try:
  29. cfg = load_config()
  30. entries = get_compatible_custom_providers(cfg)
  31. except Exception:
  32. logger.debug("Could not load named custom providers", exc_info=True)
  33. return []
  34. # ``get_compatible_custom_providers`` drops ``enabled``; read disabled keys from raw config.
  35. raw_providers = cfg.get("providers") if isinstance(cfg, dict) else None
  36. disabled_keys = {
  37. str(key).strip().lower()
  38. for key, raw in (raw_providers.items() if isinstance(raw_providers, dict) else ())
  39. if isinstance(raw, dict) and not is_provider_enabled(raw)
  40. }
  41. def _entry_catalog(entry: dict) -> tuple[str, str, list[tuple[str, str]]] | None:
  42. field = lambda key: str(entry.get(key) or "").strip()
  43. provider_key, name, base_url = field("provider_key"), field("name"), field("base_url")
  44. if provider_key.lower() in disabled_keys or not name or not base_url:
  45. return None
  46. slug = custom_provider_slug(name, provider_key)
  47. api_key = field("api_key")
  48. if not api_key:
  49. key_env = str(entry.get("key_env") or entry.get("api_key_env") or "").strip()
  50. api_key = os.environ.get(key_env, "").strip() if key_env else ""
  51. models_cfg = entry.get("models")
  52. declared = [m for m in dict.fromkeys([field("model"), *_declared_model_ids(models_cfg)]) if m]
  53. native_headers = entry.get("extra_headers") or None
  54. is_ollama_key = provider_key.lower() in {"ollama", "custom:ollama"}
  55. is_native_ollama = should_use_ollama_native_catalog(
  56. provider_key if is_ollama_key else "custom", base_url, headers=native_headers
  57. )
  58. if not api_key and not declared and not is_native_ollama:
  59. return None # nothing to discover with and nothing declared: not addressable
  60. model_ids = list(declared)
  61. live = None
  62. if _discover_flag(entry) and (api_key or is_native_ollama):
  63. try:
  64. live = _fetch_picker_live_models(
  65. api_key, base_url, provider_key if is_native_ollama and is_ollama_key else "custom",
  66. _models_config_is_allowlist(models_cfg, _entry_models_discovered(entry)),
  67. headers=native_headers, timeout=1.5, api_mode=entry.get("api_mode"),
  68. )
  69. except Exception:
  70. live = None
  71. if isinstance(live, _NativePickerModelList):
  72. model_ids = list(live)
  73. elif live is not None:
  74. model_ids = declared + [m for m in live if m not in declared]
  75. if not model_ids and not isinstance(live, _NativePickerModelList):
  76. return None
  77. return slug, name, [(mid, "") for mid in model_ids]
  78. catalogs = [_entry_catalog(entry) for entry in entries if isinstance(entry, dict)]
  79. return [c for c in catalogs if c is not None]
  80. def _semantic_provider(provider_id: str, normalize_provider: Callable[[str], str]) -> str:
  81. raw = str(provider_id or "").strip().lower()
  82. if raw in {"ollama", "custom:ollama"}:
  83. return "ollama"
  84. if raw.startswith("custom:"):
  85. return raw
  86. return normalize_provider(raw)
  87. def _empty_catalog_applies(
  88. provider_id: str, empty_authoritative: set[str], normalize_provider: Callable[[str], str]
  89. ) -> bool:
  90. """True when a named endpoint with an authoritative-empty catalog owns ``provider_id``."""
  91. raw = str(provider_id or "").strip().lower()
  92. normalized = normalize_provider(raw)
  93. if normalized == "custom":
  94. return any(
  95. candidate == raw
  96. or f"custom:{candidate}" == raw
  97. or (raw == "custom" and candidate == "custom")
  98. for candidate in empty_authoritative
  99. )
  100. return any(
  101. candidate == raw
  102. or candidate == f"custom:{normalized}"
  103. or candidate == f"custom:{raw}"
  104. or normalize_provider(candidate) == normalized
  105. for candidate in empty_authoritative
  106. )
  107. def _choice_provider(model_id: str) -> str:
  108. """Provider prefix of an encoded choice id; longest configured ``custom:`` slug wins."""
  109. parts = model_id.split(":")
  110. if parts[:1] == ["custom"] and len(parts) > 1:
  111. from hermes_cli.models import _configured_custom_provider_ids
  112. lowered = model_id.lower()
  113. for candidate in sorted(
  114. (p for p in _configured_custom_provider_ids() if p.startswith("custom:")), key=len, reverse=True,
  115. ):
  116. if lowered.startswith(candidate + ":"):
  117. return candidate
  118. return "custom"
  119. return parts[0]
  120. def encode_model_choice(provider: str | None, model: str | None) -> str:
  121. """``provider:model`` so ACP clients keep provider context."""
  122. raw_model = str(model or "").strip()
  123. if not raw_model:
  124. return ""
  125. raw_provider = str(provider or "").strip().lower()
  126. return f"{raw_provider}:{raw_model}" if raw_provider else raw_model
  127. @dataclass
  128. class _ModelCatalog:
  129. """Deduplicated ACP model rows from the inventory + named endpoints.
  130. Dedupes on the encoded choice id AND a semantic ``provider:model`` id (``ollama`` ==
  131. ``custom:ollama``). A bare/``custom`` current provider whose base_url matches an ollama
  132. inventory row is resolved to ``custom:ollama``."""
  133. normalize_provider: Callable[[str], str]
  134. current_model: str
  135. current_choice_provider: str
  136. current_base_url: str
  137. models: list[ModelInfo] = field(default_factory=list)
  138. seen_ids: set[str] = field(default_factory=set)
  139. seen_semantic_ids: set[str] = field(default_factory=set)
  140. empty_authoritative: set[str] = field(default_factory=set)
  141. def __post_init__(self) -> None:
  142. if self.current_choice_provider == "ollama":
  143. self.current_choice_provider = "custom:ollama"
  144. self._identity_resolved = self.current_choice_provider not in {"", "custom"}
  145. def semantic(self, provider_id: str) -> str:
  146. return _semantic_provider(provider_id, self.normalize_provider)
  147. def add(self, provider_id: str, model_id: str, name: str, description: str) -> None:
  148. choice_id = encode_model_choice(provider_id, model_id)
  149. semantic_id = f"{self.semantic(provider_id)}:{model_id}"
  150. if not choice_id or choice_id in self.seen_ids or semantic_id in self.seen_semantic_ids:
  151. return
  152. self.models.append(ModelInfo(model_id=choice_id, name=name, description=description))
  153. self.seen_ids.add(choice_id)
  154. self.seen_semantic_ids.add(semantic_id)
  155. def add_inventory_rows(self, rows: list, provider_label: Callable[[str], str]) -> None:
  156. for row in rows:
  157. raw_row_provider = str(row.get("slug") or "").strip().lower()
  158. row_provider = self.normalize_provider(raw_row_provider)
  159. row_base_url = str(row.get("api_url") or "").strip().rstrip("/").lower()
  160. if row.get("native_catalog_empty"):
  161. self.empty_authoritative.add(raw_row_provider)
  162. if not self._identity_resolved and raw_row_provider in {"ollama", "custom:ollama"} and (
  163. self.current_base_url and row_base_url == self.current_base_url
  164. ):
  165. self.current_choice_provider = "custom:ollama"
  166. self._identity_resolved = True
  167. row_models = row.get("models")
  168. if not row_provider or not isinstance(row_models, (list, tuple)):
  169. continue
  170. provider_name = str(row.get("name") or "").strip() or provider_label(row_provider)
  171. encoded_provider = (
  172. "custom:ollama" if raw_row_provider == "ollama"
  173. else raw_row_provider if raw_row_provider.startswith("custom:")
  174. else row_provider
  175. )
  176. for model_entry in row_models:
  177. if isinstance(model_entry, dict):
  178. model_entry = model_entry.get("id") or model_entry.get("model") or model_entry.get("name")
  179. rendered_model = str(model_entry or "").strip()
  180. if not rendered_model:
  181. continue
  182. is_current = rendered_model == self.current_model and (
  183. self.semantic(encoded_provider) == self.semantic(self.current_choice_provider)
  184. )
  185. self.add(
  186. encoded_provider, rendered_model, f"{provider_name} · {rendered_model}",
  187. f"Provider: {provider_name}" + (" • current" if is_current else ""),
  188. )
  189. def add_named_catalogs(self, catalogs: list, current_choice_provider: str) -> None:
  190. """Named user-defined endpoints (providers: / custom_providers:) are invisible
  191. to canonical enumeration — append them like the TUI /model picker. An empty
  192. catalog marks that slug authoritative-empty."""
  193. for named_slug, named_label, named_catalog in catalogs:
  194. if not named_catalog:
  195. self.empty_authoritative.add(str(named_slug).strip().lower())
  196. continue
  197. for named_model, named_desc in named_catalog:
  198. is_current = named_slug.lower() == current_choice_provider and named_model == self.current_model
  199. parts = [f"Provider: {named_label}", str(named_desc or "").strip(), "current" if is_current else ""]
  200. self.add(named_slug, named_model, named_model, " • ".join(part for part in parts if part))
  201. def build_model_state(model: str, provider: str, base_url: str) -> SessionModelState | None:
  202. """Picker state from the shared inventory + named endpoints; ``None`` when nothing is listable
  203. (caller falls back to a single current-model row). Raises on inventory failure."""
  204. from hermes_cli.inventory import build_models_payload, load_picker_context
  205. from hermes_cli.models import normalize_provider, provider_label
  206. normalized_provider = normalize_provider(provider)
  207. context = load_picker_context().with_overrides(
  208. current_provider=normalized_provider, current_model=model, current_base_url=base_url,
  209. )
  210. payload = build_models_payload(
  211. context, explicit_only=True, include_unconfigured=False, picker_hints=False,
  212. canonical_order=True, pricing=False, capabilities=False, refresh=False,
  213. probe_custom_providers=False, probe_current_custom_provider=False, max_models=ACP_MAX_MODELS_PER_PROVIDER,
  214. )
  215. named_catalogs = _named_custom_provider_catalogs()
  216. named_slugs = {str(slug).strip().lower() for slug, _label, _models in named_catalogs}
  217. current_choice_provider = str(provider or "").strip().lower()
  218. current_base = base_url.strip().rstrip("/").lower()
  219. # ``build_models_payload`` represents configured ``providers:`` entries by their raw
  220. # config key. ACP ids must instead use the durable ``custom:<key>`` identity so the
  221. # picker value round-trips through ``parse_model_input``. Only user-defined rows are
  222. # replaced by the named catalogs: a ``providers:`` key that shadows a canonical name
  223. # (``providers.openrouter:`` → proxy) must leave the canonical row — and a session that
  224. # runs on the canonical endpoint — alone, or picking "current" re-routes to the proxy.
  225. all_rows = payload.get("providers") or []
  226. canonical_current = any(
  227. str(r.get("slug") or "").strip().lower() == current_choice_provider and not r.get("is_user_defined")
  228. for r in all_rows
  229. )
  230. inventory_rows: list = []
  231. for row in all_rows:
  232. slug = str(row.get("slug") or "").strip().lower()
  233. if not row.get("is_user_defined") or not {slug, f"custom:{slug}"} & named_slugs:
  234. inventory_rows.append(row)
  235. continue
  236. row_base = str(row.get("api_url") or "").strip().rstrip("/").lower()
  237. if slug.removeprefix("custom:") == current_choice_provider and (current_base == row_base or not canonical_current):
  238. current_choice_provider = f"custom:{current_choice_provider}"
  239. cat = _ModelCatalog(
  240. normalize_provider=normalize_provider, current_model=model,
  241. current_choice_provider=current_choice_provider,
  242. current_base_url=current_base,
  243. )
  244. cat.add_inventory_rows(inventory_rows, provider_label)
  245. cat.add_named_catalogs(named_catalogs, current_choice_provider)
  246. available_models = cat.models
  247. def empty_applies(provider_id: str) -> bool:
  248. return _empty_catalog_applies(provider_id, cat.empty_authoritative, normalize_provider)
  249. if cat.empty_authoritative:
  250. available_models = [m for m in available_models if not empty_applies(_choice_provider(m.model_id))]
  251. current_is_empty = empty_applies(cat.current_choice_provider)
  252. if current_is_empty:
  253. available_models = [m for m in available_models if " • current" not in str(m.description or "")]
  254. current_model_id = "" if current_is_empty else encode_model_choice(cat.current_choice_provider, model)
  255. if current_model_id and current_model_id not in {item.model_id for item in available_models}:
  256. provider_name = provider_label(normalized_provider)
  257. available_models.insert(0, ModelInfo(
  258. model_id=current_model_id, name=f"{provider_name} · {model}",
  259. description=f"Provider: {provider_name} • current",
  260. ))
  261. if not available_models and current_is_empty:
  262. return SessionModelState(available_models=[], current_model_id="")
  263. if available_models:
  264. return SessionModelState(
  265. available_models=available_models,
  266. current_model_id=current_model_id if current_model_id or current_is_empty else available_models[0].model_id,
  267. )
  268. return None