config_providers.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. """Custom-provider entry normalization and per-route lookups (TLS, headers, context length, capabilities).
  2. Split out of ``hermes_cli/config.py``; every name is re-imported there, so
  3. ``hermes_cli.config.<name>`` keeps resolving (and monkeypatching) as before. Functions that call
  4. ``get_compatible_custom_providers`` / ``load_config*`` import them lazily from ``hermes_cli.config``
  5. so tests patching that module still intercept the call.
  6. """
  7. import logging
  8. import re
  9. from typing import Any, Dict, Iterator, List, Optional, Tuple
  10. from urllib.parse import urlparse
  11. from hermes_cli.route_identity import normalize_route_base_url
  12. # Log-record parity with the origin module.
  13. logger = logging.getLogger("hermes_cli.config")
  14. # ``_normalize_custom_provider_entry`` runs on every ``load_picker_context()`` call, so a warning
  15. # it emits would fire repeatedly for the same static config; on Windows that storm contends on
  16. # ``concurrent-log-handler``'s cross-process rotation lock and can stall the gateway/serve event
  17. # loop. Deduplicate per (provider, signature) for the process lifetime.
  18. _PROVIDER_NORMALIZE_WARNED: set = set()
  19. def _warn_once_per_provider(provider_key: str, signature: str, msg: str, *args: Any) -> None:
  20. """Emit ``logger.warning(msg, *args)`` at most once per (provider, signature)."""
  21. dedup_key = (provider_key or "?", signature)
  22. if dedup_key in _PROVIDER_NORMALIZE_WARNED:
  23. return
  24. _PROVIDER_NORMALIZE_WARNED.add(dedup_key)
  25. logger.warning(msg, *args)
  26. # Values accepted by earlier releases (and natural spellings) → canonical transport names consumed
  27. # by agent_init. Without this map an unrecognized api_mode was silently ignored and the transport
  28. # fell through to hostname-based guessing, so ``api_mode: openai`` could flip to
  29. # ``codex_responses`` after an update and break the provider.
  30. _API_MODE_ALIASES = {
  31. # See #66543.
  32. "openai": "chat_completions",
  33. "openai_chat": "chat_completions",
  34. "openai-chat": "chat_completions",
  35. "chat-completions": "chat_completions",
  36. "chatcompletions": "chat_completions",
  37. "responses": "codex_responses",
  38. "openai_responses": "codex_responses",
  39. "openai-responses": "codex_responses",
  40. "anthropic": "anthropic_messages",
  41. "anthropic-messages": "anthropic_messages",
  42. "messages": "anthropic_messages",
  43. "bedrock": "bedrock_converse",
  44. "bedrock-converse": "bedrock_converse"}
  45. _FALSE_WORDS = frozenset({"false", "0", "no", "off"})
  46. _TRUE_WORDS = frozenset({"true", "1", "yes", "on"})
  47. def _canonical_api_mode(api_mode: str) -> str:
  48. """Map alias ``api_mode`` spellings to canonical transport names (unknown pass through)."""
  49. cleaned = api_mode.strip()
  50. return _API_MODE_ALIASES.get(cleaned.lower(), cleaned)
  51. def coerce_provider_id(value: Any) -> str:
  52. """Provider identity fields are strings."""
  53. if value is None:
  54. return ""
  55. return str(value).strip()
  56. def stringify_provider_map(providers: Any) -> dict:
  57. """Copy a ``providers:`` mapping so keys are strings (unquoted YAML ``2070:`` loads as int)."""
  58. if not isinstance(providers, dict):
  59. return {}
  60. out: dict[str, Any] = {}
  61. for stored, value in providers.items():
  62. key = coerce_provider_id(stored)
  63. if key:
  64. out[key] = value
  65. return out
  66. def find_provider_entry(providers: Any, key: Any) -> tuple[Any, Optional[dict[str, Any]]]:
  67. """Return ``(stored_key, entry)`` matching *key* by string identity (exact hit, then scan)."""
  68. if not isinstance(providers, dict):
  69. return None, None
  70. want = coerce_provider_id(key)
  71. if not want:
  72. return None, None
  73. exact = providers.get(want)
  74. if isinstance(exact, dict):
  75. return want, exact
  76. for stored, entry in providers.items():
  77. if coerce_provider_id(stored) == want and isinstance(entry, dict):
  78. return stored, entry
  79. return None, None
  80. # camelCase aliases commonly used in hand-written provider configs.
  81. _CAMEL_ALIASES: dict[str, str] = {
  82. "apiKey": "api_key",
  83. "baseUrl": "base_url",
  84. "apiMode": "api_mode",
  85. "keyEnv": "key_env",
  86. "apiKeyEnv": "key_env", # OpenClaw-compatible + docs variant
  87. "defaultModel": "default_model",
  88. "contextLength": "context_length",
  89. "rateLimitDelay": "rate_limit_delay",
  90. "sessionAffinityHeader": "session_affinity_header"}
  91. _KNOWN_PROVIDER_KEYS = {
  92. # ``provider`` duplicates the ``providers.<name>`` mapping key and is unused here, but Hermes'
  93. # own config writer has historically emitted it. Accept it so self-written configs don't warn.
  94. "provider",
  95. "name", "api", "url", "base_url", "api_key", "key_env", "api_key_env", "key_cmd",
  96. "api_mode", "transport", "model", "default_model", "models", "models_discovered",
  97. "context_length", "rate_limit_delay", "request_timeout_seconds", "stale_timeout_seconds",
  98. "discover_models", "extra_body", "extra_headers", "capabilities", "ssl_ca_cert", "ssl_verify",
  99. "catalog_provider", "session_affinity_header"}
  100. def _pick_provider_base_url(entry: dict[str, Any], provider_key: str) -> str:
  101. """First usable URL among ``base_url``/``url``/``api``, or "".
  102. URLs with unresolved ``${ENV_VAR}`` / ``{region}`` placeholders are accepted unvalidated: they
  103. expand at runtime, and rejecting them here would silently drop the provider.
  104. """
  105. for url_key in ("base_url", "url", "api"):
  106. raw_url = entry.get(url_key)
  107. if not (isinstance(raw_url, str) and raw_url.strip()):
  108. continue
  109. candidate = raw_url.strip()
  110. # Accept URLs containing unresolved placeholder tokens — both ``${ENV_VAR}`` env-refs and bare
  111. # ``{region}``-style templates — without URL validation. They are expanded at runtime, so a caller
  112. # reaching this normalizer with raw (un-expanded) config would otherwise see the provider silently
  113. # dropped (#14457).
  114. if re.search(r"\{[^}]+\}", candidate):
  115. return candidate
  116. try:
  117. parsed = urlparse(candidate)
  118. except ValueError: # e.g. "http://[::1/v1" — one bad entry must not break every caller
  119. parsed = None
  120. if parsed and parsed.scheme and parsed.netloc:
  121. return candidate
  122. logger.warning(
  123. "providers.%s: '%s' value '%s' is not a valid URL "
  124. "(no scheme or host) — skipped",
  125. provider_key or "?", url_key, candidate)
  126. return ""
  127. def _normalize_provider_models(models: Any) -> tuple[dict[str, Any], bool]:
  128. """Normalize an entry's ``models`` to ``(models_dict, discovered_flag)``.
  129. The legacy in-mapping ``__discovered_model_catalog__`` sentinel is accepted and stripped; a
  130. plain list of ids or ``[{id: ...}]`` rows is converted so /model doesn't show (0) models.
  131. """
  132. discovered = False
  133. if isinstance(models, dict) and models:
  134. # Shallow-copy: `models` may alias a cached config sub-dict, and the normalized entry
  135. # escapes into long-lived runtime state.
  136. models_copy = dict(models)
  137. if models_copy.pop("__discovered_model_catalog__", None) is True:
  138. discovered = True
  139. models_copy.pop("__explicit_model_allowlist__", None)
  140. return models_copy, discovered
  141. if isinstance(models, list) and models:
  142. normalized_models: dict[str, Any] = {}
  143. for item in models:
  144. if isinstance(item, str) and item.strip():
  145. normalized_models[item.strip()] = {}
  146. continue
  147. if not isinstance(item, dict):
  148. continue
  149. model_id = item.get("id")
  150. if not isinstance(model_id, str) or not model_id.strip():
  151. model_id = item.get("name")
  152. if not isinstance(model_id, str) or not model_id.strip():
  153. continue
  154. normalized_models[model_id.strip()] = {
  155. k: v for k, v in item.items() if k not in {"id", "name"}}
  156. return normalized_models, discovered
  157. return {}, discovered
  158. def _normalize_custom_provider_entry(
  159. entry: Any, *, provider_key: str = "") -> Optional[dict[str, Any]]:
  160. """Return a runtime-compatible custom provider entry or ``None``."""
  161. if not isinstance(entry, dict):
  162. return None
  163. # Shallow-copy before alias normalization writes into the entry: callers pass live sub-dicts
  164. # from load_config_readonly()'s shared cache; mutating those violates its no-mutation contract
  165. # and leaks alias keys back into config.yaml on a later save_config(load_config()).
  166. entry = dict(entry)
  167. provider_key = coerce_provider_id(provider_key)
  168. # api_key_env is a documented snake_case alias for key_env (azure-foundry guide).
  169. if "api_key_env" in entry and "key_env" not in entry:
  170. entry["key_env"] = entry["api_key_env"]
  171. for camel, snake in _CAMEL_ALIASES.items():
  172. if camel in entry and snake not in entry:
  173. _warn_once_per_provider(
  174. provider_key, f"camel:{camel}",
  175. "providers.%s: camelCase key '%s' auto-mapped to '%s' "
  176. "(use snake_case to avoid this warning)",
  177. provider_key or "?", camel, snake)
  178. entry[snake] = entry[camel]
  179. unknown = set(entry.keys()) - _KNOWN_PROVIDER_KEYS - set(_CAMEL_ALIASES.keys())
  180. if unknown:
  181. _warn_once_per_provider(
  182. provider_key, "unknown:" + ",".join(sorted(unknown)),
  183. "providers.%s: unknown config keys ignored: %s",
  184. provider_key or "?", ", ".join(sorted(unknown)))
  185. base_url = _pick_provider_base_url(entry, provider_key)
  186. name = coerce_provider_id(entry.get("name")) or provider_key
  187. if not base_url or not name:
  188. return None
  189. normalized: dict[str, Any] = {"name": name, "base_url": base_url}
  190. if provider_key:
  191. normalized["provider_key"] = provider_key
  192. def _stripped(*keys: str) -> str:
  193. val = None
  194. for k in keys:
  195. val = entry.get(k)
  196. if val:
  197. break
  198. return val.strip() if isinstance(val, str) else ""
  199. def _put(field: str, value: Any) -> None:
  200. if value:
  201. normalized[field] = value
  202. _put("api_key", _stripped("api_key"))
  203. _put("key_cmd", _stripped("key_cmd"))
  204. key_env = _stripped("key_env", "api_key_env")
  205. _put("key_env", key_env)
  206. if key_env and entry.get("api_key_env") and not entry.get("key_env"):
  207. normalized["api_key_env"] = key_env
  208. api_mode = _stripped("api_mode", "transport")
  209. _put("api_mode", _canonical_api_mode(api_mode) if api_mode else "")
  210. _put("model", _stripped("model", "default_model"))
  211. # Catalogued vendor whose models this endpoint resells (metadata lookups only, never routing).
  212. _put("catalog_provider", _stripped("catalog_provider"))
  213. # ``models_discovered`` marks a mapping auto-discovered by Hermes, not hand-curated.
  214. models_dict, discovered = _normalize_provider_models(entry.get("models"))
  215. _put("models", models_dict)
  216. if entry.get("models_discovered") is True or discovered:
  217. normalized["models_discovered"] = True
  218. capabilities = entry.get("capabilities")
  219. if isinstance(capabilities, dict):
  220. _put("capabilities", {
  221. key: value for key, value in capabilities.items()
  222. if isinstance(key, str) and isinstance(value, bool)})
  223. for field, ok in (
  224. ("context_length", lambda v: isinstance(v, int) and v > 0),
  225. ("rate_limit_delay", lambda v: isinstance(v, (int, float)) and v >= 0),
  226. ("discover_models", lambda v: isinstance(v, (bool, str))),
  227. ):
  228. if ok(entry.get(field)):
  229. normalized[field] = entry[field]
  230. if isinstance(entry.get("extra_body"), dict):
  231. normalized["extra_body"] = dict(entry["extra_body"])
  232. # Per-provider extra HTTP headers may carry credentials — never log them downstream.
  233. _put("extra_headers", normalize_extra_headers(entry.get("extra_headers")))
  234. _put("session_affinity_header", _stripped("session_affinity_header"))
  235. _put("ssl_ca_cert", _stripped("ssl_ca_cert"))
  236. ssl_verify = entry.get("ssl_verify")
  237. if isinstance(ssl_verify, bool):
  238. normalized["ssl_verify"] = ssl_verify
  239. elif isinstance(ssl_verify, str) and ssl_verify.strip():
  240. normalized["ssl_verify"] = ssl_verify.strip()
  241. return normalized
  242. def _custom_provider_entry_to_provider_config(
  243. entry: Any, *, provider_key: str = "") -> Optional[dict[str, Any]]:
  244. """Translate a legacy custom provider entry to the v12 providers shape."""
  245. normalized = _normalize_custom_provider_entry(entry, provider_key=provider_key)
  246. if normalized is None:
  247. return None
  248. provider_entry: dict[str, Any] = {"api": normalized["base_url"]}
  249. for field in (
  250. "name", "api_key", "key_env", "key_cmd", "models", "models_discovered", "context_length",
  251. "rate_limit_delay", "discover_models", "extra_body", "extra_headers",
  252. "session_affinity_header", "ssl_ca_cert", "ssl_verify", "catalog_provider"):
  253. if field in normalized:
  254. provider_entry[field] = normalized[field]
  255. if "model" in normalized:
  256. provider_entry["default_model"] = normalized["model"]
  257. if "api_mode" in normalized:
  258. provider_entry["transport"] = normalized["api_mode"]
  259. return provider_entry
  260. def providers_dict_to_custom_providers(providers_dict: Any) -> list[dict[str, Any]]:
  261. """Normalize enabled ``providers`` config entries into the legacy custom-provider shape."""
  262. if not isinstance(providers_dict, dict):
  263. return []
  264. custom_providers: list[dict[str, Any]] = []
  265. for key, entry in providers_dict.items():
  266. if isinstance(entry, dict) and not is_provider_enabled(entry):
  267. continue
  268. normalized = _normalize_custom_provider_entry(entry, provider_key=coerce_provider_id(key))
  269. if normalized is not None:
  270. custom_providers.append(normalized)
  271. return custom_providers
  272. def get_compatible_custom_providers(
  273. config: Optional[dict[str, Any]] = None) -> list[dict[str, Any]]:
  274. """Deduplicated list view over legacy ``custom_providers`` and v12+ ``providers``.
  275. Never materialised back into config.yaml (it would duplicate entries in UIs).
  276. """
  277. from hermes_cli.config import load_config
  278. if config is None:
  279. config = load_config()
  280. custom_providers = config.get("custom_providers")
  281. if custom_providers is not None and not isinstance(custom_providers, list):
  282. # A malformed legacy value (a string written by an old `config set`) used to empty the
  283. # whole view silently — Desktop showed "Custom Endpoints 0" while valid v12+ `providers:`
  284. # entries still existed. Skip only the legacy list, and say so.
  285. logger.warning(
  286. "custom_providers is a %s, expected a list — skipping legacy entries; "
  287. "'providers:' entries are still used. Move provider configs to the 'providers:' section.",
  288. type(custom_providers).__name__)
  289. custom_providers = []
  290. candidates = [_normalize_custom_provider_entry(e) for e in (custom_providers or [])]
  291. candidates += providers_dict_to_custom_providers(config.get("providers"))
  292. def _norm(entry: dict[str, Any], field: str) -> str:
  293. return str(entry.get(field, "") or "").strip().lower()
  294. compatible: list[dict[str, Any]] = []
  295. seen_provider_keys: set = set()
  296. seen_name_url_pairs: set = set()
  297. for entry in candidates:
  298. if entry is None:
  299. continue
  300. provider_key = _norm(entry, "provider_key")
  301. name = _norm(entry, "name")
  302. base_url = str(entry.get("base_url", "") or "").strip().rstrip("/").lower()
  303. pair = (name, base_url, _norm(entry, "model"))
  304. if provider_key and provider_key in seen_provider_keys:
  305. continue
  306. if name and base_url and pair in seen_name_url_pairs:
  307. continue
  308. compatible.append(entry)
  309. if provider_key:
  310. seen_provider_keys.add(provider_key)
  311. if name and base_url:
  312. seen_name_url_pairs.add(pair)
  313. return compatible
  314. def _entries_for_route(
  315. base_url: str,
  316. custom_providers: Optional[list[dict[str, Any]]],
  317. config: Optional[dict[str, Any]]):
  318. """Yield entries whose normalized route identity equals *base_url*.
  319. None *custom_providers* → ``get_compatible_custom_providers(config)`` (failure → none).
  320. """
  321. from hermes_cli.config import get_compatible_custom_providers
  322. if custom_providers is None:
  323. try:
  324. custom_providers = get_compatible_custom_providers(config)
  325. except Exception:
  326. custom_providers = []
  327. if not base_url or not isinstance(custom_providers, list):
  328. return
  329. target_url = normalize_route_base_url(base_url)
  330. if not target_url:
  331. return
  332. for entry in custom_providers:
  333. if not isinstance(entry, dict):
  334. continue
  335. entry_url = normalize_route_base_url(entry.get("base_url"))
  336. if entry_url and entry_url == target_url:
  337. yield entry
  338. def get_custom_provider_api_mode(
  339. base_url: str,
  340. custom_providers: Optional[list[dict[str, Any]]] = None,
  341. config: Optional[dict[str, Any]] = None,
  342. ) -> str:
  343. """Canonical ``api_mode`` of the first custom entry serving *base_url*, or ``""``.
  344. Route identity is the URL, not the host: a Codex proxy on ``127.0.0.1`` declares its wire
  345. protocol here and nowhere else, so metadata lookups keyed on the transport read it from the
  346. entry instead of guessing from the hostname (#116191).
  347. """
  348. for entry in _entries_for_route(base_url, custom_providers, config):
  349. for field in ("api_mode", "transport"):
  350. value = entry.get(field)
  351. if isinstance(value, str) and value.strip():
  352. return _canonical_api_mode(value)
  353. return ""
  354. def _route_model_cfg(entry: dict[str, Any], model: str) -> Optional[dict[str, Any]]:
  355. """Return ``entry.models[model]`` when both are mappings, else None."""
  356. models = entry.get("models")
  357. if not isinstance(models, dict):
  358. return None
  359. model_cfg = models.get(model)
  360. return model_cfg if isinstance(model_cfg, dict) else None
  361. def _route_model_cfgs(
  362. model: str,
  363. base_url: str,
  364. custom_providers: Optional[list[dict[str, Any]]],
  365. config: Optional[dict[str, Any]]) -> Iterator[dict[str, Any]]:
  366. """Yield the ``models.<model>`` mapping of every entry matching *base_url*."""
  367. for entry in _entries_for_route(base_url, custom_providers, config):
  368. model_cfg = _route_model_cfg(entry, model)
  369. if model_cfg is not None:
  370. yield model_cfg
  371. def _coerce_ssl_verify(value: Any) -> Optional[bool]:
  372. if value is None:
  373. return None
  374. if isinstance(value, bool):
  375. return value
  376. if isinstance(value, str):
  377. lowered = value.strip().lower()
  378. return False if lowered in _FALSE_WORDS else True if lowered in _TRUE_WORDS else None
  379. return None
  380. def get_custom_provider_tls_settings(
  381. base_url: str,
  382. custom_providers: Optional[list[dict[str, Any]]] = None,
  383. config: Optional[dict[str, Any]] = None) -> dict[str, Any]:
  384. """Return TLS settings from a matching ``custom_providers`` / ``providers`` entry."""
  385. for entry in _entries_for_route(base_url, custom_providers, config):
  386. out: dict[str, Any] = {}
  387. ca = entry.get("ssl_ca_cert")
  388. if isinstance(ca, str) and ca.strip():
  389. out["ssl_ca_cert"] = ca.strip()
  390. verify = _coerce_ssl_verify(entry.get("ssl_verify"))
  391. if verify is not None:
  392. out["ssl_verify"] = verify
  393. return out
  394. return {}
  395. def apply_custom_provider_tls_to_client_kwargs(
  396. client_kwargs: dict[str, Any],
  397. base_url: str,
  398. custom_providers: Optional[list[dict[str, Any]]] = None,
  399. config: Optional[dict[str, Any]] = None) -> None:
  400. """Attach per-provider TLS knobs to OpenAI client kwargs when matched."""
  401. tls = get_custom_provider_tls_settings(base_url, custom_providers, config)
  402. if tls.get("ssl_ca_cert"):
  403. client_kwargs["ssl_ca_cert"] = tls["ssl_ca_cert"]
  404. if "ssl_verify" in tls:
  405. client_kwargs["ssl_verify"] = tls["ssl_verify"]
  406. def normalize_extra_headers(extra_headers: Any) -> dict[str, str]:
  407. """Normalize a raw ``extra_headers`` value into a ``dict[str, str]``.
  408. SECURITY: header values routinely carry credentials (Cloudflare Access service tokens, proxy
  409. auth, custom bearer schemes). Callers must never log the returned values.
  410. """
  411. if not isinstance(extra_headers, dict) or not extra_headers:
  412. return {}
  413. return {str(k): str(v) for k, v in extra_headers.items() if v is not None}
  414. def get_custom_provider_extra_headers(
  415. base_url: str,
  416. custom_providers: Optional[list[dict[str, Any]]] = None,
  417. config: Optional[dict[str, Any]] = None) -> dict[str, str]:
  418. """``extra_headers`` of the first route-matching entry declaring any, else ``{}``.
  419. SECURITY: values may carry credentials — callers must never log them."""
  420. for entry in _entries_for_route(base_url, custom_providers, config):
  421. headers = normalize_extra_headers(entry.get("extra_headers"))
  422. if headers:
  423. return headers
  424. return {}
  425. def apply_custom_provider_extra_headers_to_client_kwargs(
  426. client_kwargs: dict[str, Any],
  427. base_url: str,
  428. custom_providers: Optional[list[dict[str, Any]]] = None,
  429. config: Optional[dict[str, Any]] = None) -> None:
  430. """Merge per-provider ``extra_headers`` onto OpenAI client ``default_headers`` (provider wins
  431. over SDK defaults, most specific level). SECURITY: values may carry credentials; never log."""
  432. extra_headers = get_custom_provider_extra_headers(base_url, custom_providers, config)
  433. if not extra_headers:
  434. return
  435. merged = dict(client_kwargs.get("default_headers") or {})
  436. merged.update(extra_headers)
  437. client_kwargs["default_headers"] = merged
  438. def get_custom_provider_session_affinity_header(
  439. base_url: str,
  440. custom_providers: Optional[list[dict[str, Any]]] = None,
  441. config: Optional[dict[str, Any]] = None) -> str:
  442. """Header NAME declared as ``session_affinity_header`` on the route-matching entry, else "".
  443. Opt-in per provider (default off): Hermes never ships a session identifier to an endpoint
  444. that did not ask for one (#86241).
  445. """
  446. for entry in _entries_for_route(base_url, custom_providers, config):
  447. header = entry.get("session_affinity_header")
  448. if isinstance(header, str) and header.strip():
  449. return header.strip()
  450. return ""
  451. def get_custom_provider_context_length(
  452. model: str,
  453. base_url: str,
  454. custom_providers: Optional[list[dict[str, Any]]] = None,
  455. config: Optional[dict[str, Any]] = None) -> Optional[int]:
  456. """Per-model ``context_length`` override from a route-matching entry, or ``None``.
  457. Before this helper existed, the lookup was duplicated in ``run_agent.py``'s startup path only; every
  458. other path (notably ``/model`` switch) fell back to the 128K default. See #15779.
  459. """
  460. from hermes_cli.config import get_compatible_custom_providers, load_config_readonly
  461. if not model or not base_url:
  462. return None
  463. if custom_providers is None:
  464. try:
  465. # Step 0c now runs for every route with a base_url; the read-only loader skips the
  466. # per-call deepcopy load_config() pays (same pattern as get_custom_provider_model_capability).
  467. custom_providers = get_compatible_custom_providers(load_config_readonly() if config is None else config)
  468. except Exception:
  469. if config is None:
  470. return None
  471. raw = config.get("custom_providers")
  472. custom_providers = raw if isinstance(raw, list) else []
  473. def _positive_int(raw: Any) -> Optional[int]:
  474. try:
  475. ctx = int(raw)
  476. except (TypeError, ValueError):
  477. return None
  478. return ctx if ctx > 0 else None
  479. for model_cfg in _route_model_cfgs(model, base_url, custom_providers, config):
  480. ctx = _positive_int(model_cfg.get("context_length"))
  481. if ctx is not None:
  482. return ctx
  483. # Entry-level ``context_length`` (a documented key) backs every model the entry serves when no
  484. # per-model override exists; without it the /model switch re-derivation fell to the hardcoded
  485. # catalog while a cold start honoured the same setting via model.context_length (#98387).
  486. for entry in _entries_for_route(base_url, custom_providers, config):
  487. ctx = _positive_int(entry.get("context_length"))
  488. if ctx is not None:
  489. return ctx
  490. return None
  491. def get_custom_provider_model_capability(
  492. model: str,
  493. base_url: str,
  494. capability: str,
  495. custom_providers: Optional[list[dict[str, Any]]] = None,
  496. config: Optional[dict[str, Any]] = None) -> Optional[bool]:
  497. """Explicit boolean capability for one custom-provider model, or ``None``. Scoped to the
  498. normalized route + exact runtime model id so aliases can declare capabilities."""
  499. from hermes_cli.config import get_compatible_custom_providers, load_config_readonly
  500. if not model or not base_url or not capability:
  501. return None
  502. if custom_providers is None:
  503. try:
  504. if config is None:
  505. # Read-only path: entries are never mutated and get_compatible_custom_providers
  506. # shallow-copies each one, so the no-deepcopy cache is safe (~135us saved per call).
  507. config = load_config_readonly()
  508. custom_providers = get_compatible_custom_providers(config)
  509. except Exception:
  510. return None
  511. for model_cfg in _route_model_cfgs(model, base_url, custom_providers, config):
  512. value = model_cfg.get(capability)
  513. if isinstance(value, bool):
  514. return value
  515. return None
  516. def is_provider_enabled(provider_cfg: Optional[dict[str, Any]]) -> bool:
  517. """Whether a ``providers.<name>`` block is enabled: default True; only an explicit
  518. ``enabled: false`` hides it from the picker, ``/models``, runtime resolver and doctor."""
  519. if not isinstance(provider_cfg, dict):
  520. return True
  521. flag = provider_cfg.get("enabled", True)
  522. if isinstance(flag, bool):
  523. return flag
  524. # YAML can produce strings for "true"/"false" depending on quoting.
  525. if isinstance(flag, str):
  526. return flag.strip().lower() not in _FALSE_WORDS
  527. return bool(flag)