model_switch_providers.py 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  1. """Picker provider listing: credential discovery, curated/live model lists, row builders for
  2. list_authenticated_providers / list_picker_providers, and the parallel cache prefetch.
  3. Split out of ``hermes_cli/model_switch.py``; every moved name is re-imported there so
  4. ``hermes_cli.model_switch.<name>`` keeps resolving (and monkeypatching) as before."""
  5. from __future__ import annotations
  6. import logging
  7. import http.client
  8. import os
  9. import time
  10. import threading as _threading
  11. from dataclasses import dataclass, field
  12. from typing import Any, List, Optional
  13. from agent.command_token_source import build_command_token_provider, materialize_probe_api_key
  14. from hermes_cli.providers import custom_provider_aliases, custom_provider_slug, get_label
  15. from utils import base_url_host_matches
  16. # Log-record parity with the origin module.
  17. logger = logging.getLogger("hermes_cli.model_switch")
  18. # Rows never capped by max_models: aggregators whose full catalogs (70+ models) must stay visible, and
  19. # rows that are already a curated list (Nous = curated + Portal picks, OpenRouter = curated ∩ live),
  20. # where the cap only cut the bottom "Free tier" block and the Portal's appended recommendations.
  21. _UNCAPPED_PICKER_PROVIDERS: frozenset[str] = frozenset({"opencode-zen", "opencode-go", "nous", "openrouter"})
  22. def _save_discovered_models_to_config(
  23. api_url: str, model_ids: list[str], *, api_mode: Optional[str] = None,
  24. headers: Optional[dict[str, str]] = None, credential_identity: str | None = None) -> None:
  25. """Persist a successful ``/v1/models`` probe into the matching ``custom_providers`` entry.
  26. Matches by base_url (slash-normalised), api_mode and headers. A failed config write is
  27. swallowed — the picker still shows the live models for this session."""
  28. from hermes_cli.model_switch import _extra_headers_from_config
  29. if not api_url or not model_ids:
  30. return
  31. try:
  32. from hermes_cli.config import load_config, save_config
  33. cfg = load_config()
  34. providers = cfg.get("custom_providers") or []
  35. if not isinstance(providers, list):
  36. return
  37. norm_url = api_url.strip().rstrip("/").lower()
  38. changed = False
  39. for entry in providers:
  40. if not isinstance(entry, dict):
  41. continue
  42. entry_url = (entry.get("base_url", "") or entry.get("url", "")).strip()
  43. if entry_url.rstrip("/").lower() != norm_url or _entry_api_mode(entry) != api_mode:
  44. continue
  45. if headers is not None and _extra_headers_from_config(entry) != headers:
  46. continue
  47. if credential_identity is not None and _entry_credentials(entry, "key_env", "api_key_env")[2] != credential_identity:
  48. continue
  49. if not _discovered_catalog_stale(entry, model_ids):
  50. continue
  51. entry["models"] = {model_id: {} for model_id in model_ids}
  52. entry["models_discovered"] = True
  53. changed = True
  54. if changed:
  55. cfg["custom_providers"] = providers
  56. save_config(cfg)
  57. except Exception:
  58. pass
  59. def _discovered_catalog_stale(entry: dict, model_ids: list[str]) -> bool:
  60. """Whether a live probe may overwrite ``entry["models"]``.
  61. A ``models`` mapping or list of dicts is user-curated per-model metadata — never replaced.
  62. A mapping Hermes itself discovered (entry flag or legacy in-mapping sentinel) is ours to
  63. refresh, but only when stale; a legacy-shape entry is always rewritten so the save migrates
  64. it to the clean entry-level flag."""
  65. existing = entry.get("models")
  66. legacy_discovered = isinstance(existing, dict) and existing.get("__discovered_model_catalog__") is True
  67. entry_discovered = entry.get("models_discovered") is True or legacy_discovered
  68. if isinstance(existing, dict):
  69. return entry_discovered and (legacy_discovered or list(existing) != model_ids)
  70. if isinstance(existing, list):
  71. return not any(isinstance(m, dict) for m in existing) and existing != model_ids
  72. return True
  73. class _NativePickerModelList(list[str]):
  74. """A successful native catalog, including an authoritative empty one."""
  75. def _fetch_picker_live_models(
  76. api_key: Any, api_url: str, native_catalog_provider: str, preserve_native_models: bool,
  77. headers: dict[str, str] | None = None, timeout: float = 5.0,
  78. api_mode: str | None = None, *, cache: bool = True) -> list[str] | None:
  79. """Fetch picker models with native Ollama and cached generic discovery."""
  80. from hermes_cli.models import _get_ollama_native_headers, cached_fetch_api_models, fetch_api_models
  81. from hermes_cli.models_local import (
  82. _OLLAMA_LOCAL_MODELS_CACHE_TTL,
  83. _normalize_openai_base_url,
  84. fetch_ollama_local_models,
  85. should_use_ollama_native_catalog,
  86. )
  87. if callable(api_key):
  88. # Let the catalog cache decide whether any token or network I/O is needed.
  89. return cached_fetch_api_models(
  90. api_key, api_url, timeout=timeout, headers=headers, api_mode=api_mode,
  91. fetch_models=lambda: _fetch_picker_live_models(
  92. materialize_probe_api_key(api_key), api_url, native_catalog_provider,
  93. preserve_native_models, headers, timeout, api_mode, cache=False))
  94. candidate_headers = _get_ollama_native_headers(api_url, api_key=api_key)
  95. def _drop(pred) -> None:
  96. for key in tuple(candidate_headers):
  97. if pred(key.lower()):
  98. del candidate_headers[key]
  99. caller_has_authorization = any(key.lower() == "authorization" for key in (headers or {}))
  100. if headers:
  101. lowered = {existing.lower() for existing in headers}
  102. _drop(lambda k: k in lowered)
  103. candidate_headers.update(headers)
  104. if api_key and not caller_has_authorization:
  105. _drop(lambda k: k == "authorization")
  106. candidate_headers["Authorization"] = f"Bearer {api_key}"
  107. use_native = should_use_ollama_native_catalog(
  108. native_catalog_provider, api_url, headers=candidate_headers or None)
  109. resolved_headers = candidate_headers or None if use_native else headers
  110. if use_native:
  111. if preserve_native_models:
  112. return None
  113. def _probe_native_catalog() -> _NativePickerModelList | None:
  114. models = fetch_ollama_local_models(api_url, timeout=timeout, headers=resolved_headers)
  115. return None if models is None else _NativePickerModelList(models)
  116. # Admit the native catalog to the SHARED disk cache: a no-probe picker open (every endpoint
  117. # that is not the current one) reads ``provider_models_cache.json`` only, so a native probe
  118. # that answered here but was never stored came back empty on the next open — the provider's
  119. # whole group vanished from the picker until the user hit Refresh Models. Key the entry on
  120. # the caller's ``headers`` (what that cache_only read hashes), not ``resolved_headers``: the
  121. # native probe's synthesized Authorization would otherwise land under a fingerprint the
  122. # read side never computes, and a keyed endpoint kept flickering. Clamp the fresh window
  123. # to the native TTL (300s, as cached_provider_model_ids does for the built-in slug): a
  124. # locally pulled model must not stay invisible for the generic 1h TTL.
  125. native_models = (
  126. cached_fetch_api_models(
  127. api_key, api_url, timeout=timeout, headers=headers, api_mode=api_mode,
  128. fetch_models=_probe_native_catalog, ttl_seconds=_OLLAMA_LOCAL_MODELS_CACHE_TTL)
  129. if cache else _probe_native_catalog())
  130. if native_models is not None:
  131. return native_models
  132. # A failed native probe is not authoritative: retry the cached generic catalog.
  133. api_url = _normalize_openai_base_url(api_url)
  134. generic_models = (cached_fetch_api_models if cache else fetch_api_models)(
  135. api_key, api_url, timeout=timeout, headers=resolved_headers, api_mode=api_mode)
  136. return generic_models if generic_models or use_native else None
  137. # Process-level guard: the prewarm thread is spawned at most once per process, otherwise a
  138. # long-lived process (or repeated triggers) would leak one OS thread per call.
  139. _picker_prewarm_done = _threading.Event()
  140. def _credential_pool_is_usable(provider: str, *, raw_pool_present: bool = False, for_picker: bool = False) -> bool:
  141. """Whether *provider* has a credential that can be selected now.
  142. Legacy opaque ``auth.json`` pool values that do not deserialize into ``PooledCredential``
  143. stay visible (``raw_pool_present``); a real pool's availability is authoritative — an
  144. all-exhausted/dead pool is not authenticated. ``for_picker`` (human-facing pickers) also
  145. accepts a pool whose entries are all in cooldown: a rate-limited provider is not a signed-out
  146. one, and limits are per-model for many providers, so another model may still work."""
  147. try:
  148. from agent.credential_pool import load_pool
  149. pool = load_pool(provider)
  150. if pool.has_credentials():
  151. return for_picker or pool.has_available()
  152. except Exception:
  153. pass
  154. return raw_pool_present
  155. def prewarm_picker_cache_async() -> Optional[_threading.Thread]:
  156. """Warm ``provider_models_cache.json`` in a daemon thread by running the picker path once.
  157. The first ``/model`` open (or the first after the 1h TTL) otherwise blocks ~1-2s on serial
  158. live ``/v1/models`` fetches. Fire-and-forget, at most once per process, fully
  159. exception-isolated. Returns the thread (for tests) or None if already warmed."""
  160. from hermes_cli.model_switch import list_authenticated_providers
  161. if _picker_prewarm_done.is_set():
  162. return None
  163. _picker_prewarm_done.set()
  164. def _warm() -> None:
  165. try:
  166. from hermes_cli.inventory import load_picker_context
  167. ctx = load_picker_context()
  168. # The result is discarded; the warm disk cache is the point.
  169. list_authenticated_providers(
  170. current_provider=ctx.current_provider, current_base_url=ctx.current_base_url,
  171. current_model=ctx.current_model, user_providers=ctx.user_providers,
  172. custom_providers=ctx.custom_providers,
  173. excluded_providers=ctx.excluded_providers or [])
  174. except Exception:
  175. logger.debug("picker cache prewarm failed", exc_info=True)
  176. t = _threading.Thread(target=_warm, daemon=True, name="picker-cache-prewarm")
  177. t.start()
  178. return t
  179. def _prefetch_provider_models_parallel(provider_slugs: list[str]) -> None:
  180. """Fetch the provider catalogs the serial picker loop would block on, in parallel.
  181. Only providers the serial call cannot serve from disk are fetched — missing,
  182. fingerprint-mismatched, or hard-expired entries. TTL-expired non-empty rows are served
  183. inside the stale-serve window while revalidating off-thread, and an empty local-ollama
  184. catalog is authoritative inside its own short native TTL, so prefetching either trades a
  185. non-blocking serial read for a parallel fetch the picker waits on. Persistence is
  186. ``cached_provider_model_ids``'s own locked write; workers add none of their own."""
  187. from hermes_cli.models import (
  188. _credential_fingerprint, _disk_serve_tier, _load_provider_models_cache,
  189. _normalized_cache_slug, cached_provider_model_ids)
  190. now = time.time()
  191. # Gate on the row the serial call actually reads — _normalized_cache_slug keeps a bare
  192. # "ollama" as its own key instead of letting normalize_provider fold it into "custom".
  193. # cached_provider_model_ids re-reads the cache itself, so a concurrent change between
  194. # check and fetch is harmless.
  195. stale_slugs: list[str] = []
  196. cache = _load_provider_models_cache()
  197. for slug in provider_slugs:
  198. key = _normalized_cache_slug(slug)
  199. if key and _disk_serve_tier(cache.get(key), _credential_fingerprint(key), now,
  200. is_ollama=key == "ollama") is None:
  201. stale_slugs.append(key)
  202. if not stale_slugs:
  203. return
  204. import concurrent.futures
  205. def _fetch_one(slug: str) -> None:
  206. try:
  207. cached_provider_model_ids(slug, force_refresh=True)
  208. except Exception:
  209. pass # best-effort; picker falls back to curated list
  210. with concurrent.futures.ThreadPoolExecutor(
  211. max_workers=min(8, len(stale_slugs)), thread_name_prefix="model-cache-prefetch",
  212. ) as executor:
  213. list(executor.map(_fetch_one, stale_slugs))
  214. def _any_env(env_vars, read_env=os.environ.get) -> bool:
  215. return any(read_env(ev) for ev in env_vars)
  216. def _skip(seen: set, excluded: set, *keys: str) -> bool:
  217. """True when any of *keys* (lowercased) is already emitted or excluded."""
  218. lowered = [k.lower() for k in keys]
  219. return any(k in seen for k in lowered) or any(k in excluded for k in lowered)
  220. def _iter_builtin_candidates(models_dev_data: dict, excluded: set, seen: set):
  221. """Yield ``(hermes_id, mdev_id, pconfig, env_vars)`` for section-1 rows.
  222. Skips vendor names that alias through an aggregator (bare "openai" -> "openrouter" would
  223. silently switch a user onto an endpoint they may have no key for), aliases of another canonical
  224. profile ("kimi" -> "kimi-coding"), non-api_key auth types (section 2 handles them) and
  225. unroutable providers. PROVIDER_REGISTRY env var names win over models.dev's."""
  226. from agent.models_dev import models_dev_provider_map
  227. from hermes_cli.auth import PROVIDER_REGISTRY, is_runtime_provider_routable
  228. from hermes_cli.models import _AGGREGATOR_PROVIDERS
  229. from hermes_cli.providers import ALIASES
  230. for hermes_id, mdev_id in models_dev_provider_map().items():
  231. alias_target = ALIASES.get(hermes_id)
  232. if alias_target and alias_target != hermes_id and alias_target in _AGGREGATOR_PROVIDERS:
  233. continue
  234. try:
  235. from providers import get_provider_profile
  236. prof = get_provider_profile(hermes_id)
  237. if prof is not None and prof.name != hermes_id:
  238. continue
  239. except Exception:
  240. pass
  241. if hermes_id.lower() in seen:
  242. continue
  243. if hermes_id.lower() in excluded or mdev_id.lower() in excluded:
  244. continue
  245. pdata = models_dev_data.get(mdev_id)
  246. if not isinstance(pdata, dict):
  247. continue
  248. pconfig = PROVIDER_REGISTRY.get(hermes_id)
  249. if (pconfig and pconfig.auth_type != "api_key") or not is_runtime_provider_routable(hermes_id):
  250. continue
  251. env_vars = list(pconfig.api_key_env_vars) if pconfig and pconfig.api_key_env_vars else pdata.get("env", [])
  252. if isinstance(env_vars, list):
  253. yield hermes_id, mdev_id, pconfig, env_vars
  254. def _auth_store_has_provider(*keys: str) -> bool:
  255. """True when ``auth.json`` has a ``providers`` entry under any of *keys*."""
  256. try:
  257. from hermes_cli.auth import _load_auth_store
  258. store = _load_auth_store()
  259. providers_store = store.get("providers", {})
  260. return bool(store and any(k in providers_store for k in keys))
  261. except Exception as exc:
  262. logger.debug("Auth store check failed for %s: %s", keys[0] if keys else "", exc)
  263. return False
  264. def _raw_pool_usable(hermes_id: str, *, for_picker: bool = False) -> bool:
  265. """Section-1 pool check: only consult the pool when auth.json lists a raw entry."""
  266. try:
  267. from hermes_cli.auth import _load_auth_store
  268. store = _load_auth_store()
  269. if store and store.get("credential_pool", {}).get(hermes_id):
  270. return _credential_pool_is_usable(hermes_id, raw_pool_present=True, for_picker=for_picker)
  271. except Exception:
  272. pass
  273. return False
  274. def _pool_usable(slug: str, *, for_picker: bool = False) -> bool:
  275. try:
  276. return _credential_pool_is_usable(slug, for_picker=for_picker)
  277. except Exception as exc:
  278. logger.debug("Credential pool check failed for %s: %s", slug, exc)
  279. return False
  280. def _overlay_has_env_creds(pid: str, hermes_slug: str, overlay, read_env) -> bool:
  281. """Section-2 env/SDK credential check shared by the picker and the prefetch scan.
  282. Vertex authenticates via OAuth2 (service-account JSON / ADC), not an API key, so it gets its
  283. own probe; otherwise the provider is hidden from the picker even when fully configured."""
  284. from hermes_cli.auth import PROVIDER_REGISTRY
  285. has_creds = False
  286. if overlay.auth_type == "vertex":
  287. try:
  288. from agent.vertex_adapter import has_vertex_credentials
  289. has_creds = has_vertex_credentials()
  290. except Exception as exc:
  291. logger.debug("Vertex credential check failed: %s", exc)
  292. elif overlay.extra_env_vars:
  293. has_creds = _any_env(overlay.extra_env_vars, read_env)
  294. if not has_creds and overlay.auth_type == "api_key":
  295. for key in (pid, hermes_slug):
  296. pcfg = PROVIDER_REGISTRY.get(key)
  297. if pcfg and pcfg.api_key_env_vars and _any_env(pcfg.api_key_env_vars, read_env):
  298. return True
  299. if not has_creds and hermes_slug == "azure-foundry":
  300. has_creds = _azure_entra_configured(read_env)
  301. return has_creds
  302. def _azure_entra_configured(read_env=os.environ.get) -> bool:
  303. """Azure Foundry under ``model.auth_mode: entra_id`` mints a per-request bearer, so no
  304. ``AZURE_FOUNDRY_API_KEY`` ever exists; the row is configured once the runtime resolver's own
  305. inputs are (provider + auth_mode + an endpoint). No token is minted here (#27989)."""
  306. from hermes_cli.models import _get_model_config_dict
  307. model_cfg = _get_model_config_dict()
  308. if (str(model_cfg.get("provider") or "").strip().lower() != "azure-foundry"
  309. or str(model_cfg.get("auth_mode") or "").strip().lower() != "entra_id"):
  310. return False
  311. return bool(str(model_cfg.get("base_url") or "").strip() or read_env("AZURE_FOUNDRY_BASE_URL"))
  312. def _has_fast_aws_sdk_signal() -> bool:
  313. """True when explicit AWS auth config is present in the environment.
  314. Deliberately avoids botocore's full credential chain: picker discovery runs for non-Bedrock
  315. providers too, and botocore may probe EC2 IMDS (169.254.169.254) before giving up."""
  316. def _set(name: str) -> bool:
  317. return bool(os.environ.get(name, "").strip())
  318. return (
  319. _set("AWS_BEARER_TOKEN_BEDROCK")
  320. or (_set("AWS_ACCESS_KEY_ID") and _set("AWS_SECRET_ACCESS_KEY"))
  321. or any(_set(name) for name in (
  322. "AWS_PROFILE", "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",
  323. "AWS_CONTAINER_CREDENTIALS_FULL_URI", "AWS_WEB_IDENTITY_TOKEN_FILE")))
  324. def _has_aws_sdk_creds_for_listing(slug: str, current_provider: str) -> bool:
  325. """AWS SDK credential check; the full boto3 chain is only consulted for the *current* provider."""
  326. if _has_fast_aws_sdk_signal():
  327. return True
  328. if str(slug or "").strip().lower() != str(current_provider or "").strip().lower():
  329. return False
  330. try:
  331. from agent.bedrock_adapter import has_aws_credentials
  332. return bool(has_aws_credentials())
  333. except Exception:
  334. return False
  335. def _is_aws_sdk(pconfig) -> bool:
  336. return bool(pconfig) and getattr(pconfig, "auth_type", "") == "aws_sdk"
  337. def _live_or_curated_ids(slug: str, curated: dict, *fallback_keys: str, merge_models_dev: bool = True,
  338. non_blocking: bool = False) -> list:
  339. """``cached_provider_model_ids`` (the SAME disk-cached list ``hermes model`` builds), falling
  340. back to the curated list (merged with models.dev for preferred providers) when live is empty.
  341. ``non_blocking`` (GUI read path) reads the disk cache only — a provider that is slow or down
  342. contributes its curated list instead of stalling the whole picker (#114215)."""
  343. from hermes_cli.models import _MODELS_DEV_PREFERRED, _merge_with_models_dev, cached_provider_model_ids
  344. from hermes_cli.chat_catalog import without_generation_models
  345. model_ids = cached_provider_model_ids(slug, non_blocking=non_blocking)
  346. if not model_ids:
  347. model_ids = _first_curated(curated, fallback_keys or (slug,))
  348. if merge_models_dev and slug in _MODELS_DEV_PREFERRED:
  349. model_ids = _merge_with_models_dev(slug, model_ids)
  350. return without_generation_models(model_ids)
  351. def _first_curated(curated: dict, keys) -> list:
  352. """First non-empty curated list under *keys*; the last key's (possibly empty) value otherwise."""
  353. model_ids: list = []
  354. for key in keys:
  355. model_ids = curated.get(key, [])
  356. if model_ids:
  357. break
  358. return model_ids
  359. def _aws_live_or_curated_ids(slug: str, curated: dict, *fallback_keys: str,
  360. non_blocking: bool = False) -> list:
  361. """Bedrock: live discovery reflects the active region (eu.*, ap.*) rather than the static
  362. us.* list; any failure falls back to the curated list."""
  363. try:
  364. return _live_or_curated_ids(slug, curated, *fallback_keys, merge_models_dev=False,
  365. non_blocking=non_blocking) or []
  366. except Exception:
  367. return _first_curated(curated, fallback_keys or (slug,)) or []
  368. def _nous_picker_model_ids(curated: dict, force_fresh_nous_tier: bool) -> list:
  369. """Nous serves a huge live catalog; the picker shows ONLY the curated agentic list, augmented
  370. with the Portal's free/paid recommendations and paid-tier sale models, then narrowed by org
  371. policy. Mirrors ``_model_flow_nous`` so GUI pickers match the CLI. A failed
  372. recommendation fetch still yields a policy-filtered curated list."""
  373. model_ids = curated.get("nous", [])
  374. try:
  375. from hermes_cli.models_pricing import get_pricing_for_provider
  376. from hermes_cli.models import (
  377. check_nous_free_tier,
  378. union_with_nous_on_sale_models,
  379. union_with_portal_free_recommendations,
  380. union_with_portal_paid_recommendations,
  381. )
  382. from hermes_cli.auth import get_provider_auth_state
  383. # Cache-only: a cold cache must not hold the picker open. The Portal unions only append ids;
  384. # the on-sale union reads the rows the background prewarm filled under the same key the
  385. # fetcher registers (base + credential fingerprint), so a logged-in account's cached_only
  386. # read finds them.
  387. pricing = get_pricing_for_provider("nous", cached_only=True) or {}
  388. try:
  389. portal = (get_provider_auth_state("nous") or {}).get("portal_base_url", "") or ""
  390. except Exception:
  391. portal = ""
  392. if check_nous_free_tier(force_fresh=force_fresh_nous_tier):
  393. model_ids, _ = union_with_portal_free_recommendations(model_ids, pricing, portal)
  394. else:
  395. model_ids, _ = union_with_portal_paid_recommendations(model_ids, pricing, portal)
  396. model_ids = union_with_nous_on_sale_models(model_ids, pricing)
  397. except Exception:
  398. pass
  399. try:
  400. from hermes_cli.models_pricing import nous_policy_allowed_ids, restrict_to_nous_policy
  401. model_ids = restrict_to_nous_policy(model_ids, nous_policy_allowed_ids(), rescue_empty=True)
  402. except Exception:
  403. logger.debug("Nous model policy filter failed", exc_info=True)
  404. try:
  405. # Window plans: the plan's featured picks lead the list. Plan shape only (it rarely changes), so
  406. # the last snapshot at any age answers — a long-running gateway's 60s cache has usually lapsed
  407. # by the time someone opens /model.
  408. from hermes_cli.model_picker_usage import featured_first, nous_picker_usage
  409. usage = nous_picker_usage(any_age=True)
  410. if usage is not None:
  411. model_ids = featured_first(model_ids, usage.featured_model_ids)
  412. except Exception:
  413. pass
  414. return model_ids
  415. def _free_tier_nous_row(row: dict) -> dict | None:
  416. """The one free-tier rule for a Nous picker row, shared by every row builder.
  417. ``row`` carries at least ``name`` and ``models``. A guest identity carrying inference turns
  418. it into "Nous · free tier" with the single model ``nous/welcome`` (the welcome host serves
  419. nothing else). A guest that ``nous.guest: false`` has switched off yields ``None``: no Nous
  420. row at all, since there is nothing selectable. A real account (or no Nous state) passes the
  421. row through untouched. Builders that compute the full catalog lazily should pass
  422. ``models=[]`` and only compute when the returned row still has no models."""
  423. from hermes_cli import anon_auth
  424. if not anon_auth.has_guest():
  425. return row
  426. if not anon_auth.guest_allowed():
  427. return None
  428. if not anon_auth.free_inference_active():
  429. return row # a guest without the welcome trial here: the row is whatever real Nous login exists
  430. out = dict(row)
  431. out["name"] = anon_auth.FREE_TIER_LABEL
  432. out["models"] = [anon_auth.GUEST_MODEL]
  433. out["total_models"] = 1
  434. # The explicit flag every consumer keys on (pricing, badges): never the display name. It also
  435. # tells the picker this is the free tier's identity, distinct from ``free_tier`` (an account on
  436. # the free plan) which pricing sets from the Portal's entitlement read.
  437. out["free_tier_row"] = True
  438. return out
  439. def _cap_models(model_ids: list, max_models: int | None, slug: str = "") -> list:
  440. """Apply ``max_models``; rows in ``_UNCAPPED_PICKER_PROVIDERS`` show everything (``0`` still
  441. means slug-only: no models)."""
  442. if max_models is None or (max_models and slug in _UNCAPPED_PICKER_PROVIDERS):
  443. return model_ids
  444. return model_ids[:max_models]
  445. def _absorb_entry_models(grp: dict, entry: dict, active_model: Any) -> None:
  446. """Fold one config entry's models into its group: the active selection first, then the declared
  447. ``models:`` ids. The active selection alone never suppresses discovery, and a dict-shaped
  448. ``models:`` is metadata rather than an allowlist (see ``_models_config_is_allowlist``), so only
  449. list/string shapes pin the row."""
  450. from hermes_cli.model_switch import _declared_model_ids, _entry_models_discovered, _models_config_is_allowlist
  451. _extend_unique(grp["models"], [active_model])
  452. models_field = entry.get("models")
  453. if _models_config_is_allowlist(models_field, _entry_models_discovered(entry)):
  454. grp["has_explicit_models"] = True
  455. _extend_unique(grp["models"], _declared_model_ids(models_field))
  456. _split_chain_entries(grp["models"])
  457. def _split_chain_entries(models: list) -> None:
  458. """Split comma-separated fallback chains (``default_model: a,b,c`` — e.g. the volcengine
  459. agent plans) into individually selectable ids. The raw chain stays first so the server-side
  460. fallback behaviour itself remains the default pick; the split ids follow it. Fixes #50557."""
  461. for model in list(models):
  462. if isinstance(model, str) and "," in model:
  463. _extend_unique(models, [part.strip() for part in model.split(",")])
  464. def _extend_unique(target: list, items) -> None:
  465. """Append each truthy item of *items* not already in *target* (order preserved)."""
  466. for item in items:
  467. if item and item not in target:
  468. target.append(item)
  469. def _norm_url(url: Any) -> str:
  470. # Effective base URLs of every built-in row we emit (normalized lower+rstrip). Section 4 uses this to
  471. # hide ``custom_providers`` entries that point at the same endpoint as a built-in (e.g. a user-defined
  472. # "my-dashscope" on https://coding-intl.dashscope.aliyuncs.com/v1 collides with the built-in
  473. # alibaba-coding-plan row when DASHSCOPE_API_KEY is present). Fixes #16970.
  474. return str(url or "").strip().rstrip("/").lower()
  475. def _entry_base_url(entry: dict, keys: tuple = ("base_url", "url", "api")) -> str:
  476. return next((entry.get(key, "") for key in keys if entry.get(key, "")), "")
  477. def _entry_api_mode(entry: dict) -> str | None:
  478. return str(entry.get("api_mode") or entry.get("transport") or "").strip().lower() or None
  479. def _entry_credentials(entry: dict, *key_env_keys: str) -> tuple[Any, str, str]:
  480. """Unminted credential, env fallback, and stable grouping identity."""
  481. key_env = str(next((entry.get(k) for k in key_env_keys if entry.get(k)), "")).strip()
  482. source = build_command_token_provider(entry.get("key_cmd", ""), entry.get("name") or "custom")
  483. if source is not None:
  484. return source, key_env, source.cache_identity
  485. inline_api_key = str(entry.get("api_key", "") or "").strip()
  486. return inline_api_key, key_env, inline_api_key or (f"env:{key_env}" if key_env else "")
  487. def _discover_flag(entry: dict):
  488. """``discover_models`` (default True); ``"false"/"no"/"0"`` strings mean False."""
  489. discover = entry.get("discover_models", True)
  490. if isinstance(discover, str):
  491. discover = discover.lower() not in {"false", "no", "0"}
  492. return discover
  493. def _display_prefix(name: str) -> str:
  494. """Text before the per-model separator Hermes's own writer uses ("—" / " - ")."""
  495. return next((name.split(sep)[0].strip() for sep in ("—", " - ") if sep in name), name)
  496. def _group_display_name(display_name: str) -> str:
  497. """Section-3 row label: strip the per-model suffix and trailing version tokens ("Palantir
  498. Claude 4.7 Opus" -> "Palantir Claude") — cut at the first token containing a digit, only when
  499. >= 2 words remain (avoids over-trimming)."""
  500. grp_display = _display_prefix(display_name)
  501. toks = grp_display.split()
  502. cut_at = next((i for i, t in enumerate(toks) if any(c.isdigit() for c in t.strip(".,()"))), None)
  503. if cut_at is not None and cut_at >= 2:
  504. grp_display = " ".join(toks[:cut_at]).strip()
  505. return grp_display or display_name
  506. def _discover_endpoint_models(
  507. api_key: Any, api_url: str, native_catalog_provider: str, has_explicit_models: bool, *,
  508. headers: dict | None, api_mode: str | None, probe_live: bool, discovery_allowed: bool,
  509. fast_custom_probe: bool) -> tuple[list | None, bool]:
  510. """Return ``(models, native_catalog_empty)`` for a custom endpoint row.
  511. ``probe_live`` runs the native-aware picker fetch; otherwise, when discovery is allowed, a
  512. warm same-fingerprint cache entry still serves the full catalog with no round-trip.
  513. ``has_explicit_models`` gates the *probe* (a network-cost guard for keyless endpoints that
  514. declare a catalog), never the cache read — applying it to the read re-pins the endpoint to
  515. its declared subset. Returns ``(None, False)`` when nothing usable was found.
  516. ``fast_custom_probe`` picks the discovery budget (1.5s fast / 5s full) independently of the
  517. caller's exhausted-pool visibility flag: a picker that keeps cooldown providers visible
  518. still deserves the full budget when it is the one surface that live-probes the current
  519. custom endpoint."""
  520. timeout = 1.5 if fast_custom_probe else 5.0
  521. if probe_live:
  522. try:
  523. live_models = _fetch_picker_live_models(
  524. api_key, api_url, native_catalog_provider, has_explicit_models,
  525. headers=headers, timeout=timeout, api_mode=api_mode)
  526. is_native = isinstance(live_models, _NativePickerModelList)
  527. if is_native and has_explicit_models:
  528. return None, False
  529. if live_models is not None and (live_models or not has_explicit_models or is_native):
  530. return live_models, (is_native and not live_models)
  531. except Exception:
  532. pass
  533. elif discovery_allowed:
  534. try:
  535. from hermes_cli.models import cached_fetch_api_models
  536. cached_models = cached_fetch_api_models(
  537. api_key, api_url, cache_only=True, timeout=timeout, headers=headers, api_mode=api_mode,
  538. )
  539. if has_explicit_models and isinstance(cached_models, _NativePickerModelList):
  540. return None, False
  541. if cached_models or isinstance(cached_models, _NativePickerModelList):
  542. return cached_models, isinstance(cached_models, _NativePickerModelList) and not cached_models
  543. except (ImportError, OSError, RuntimeError, TimeoutError, TypeError, ValueError, http.client.HTTPException):
  544. pass
  545. return None, False
  546. def _collect_authed_provider_slugs(
  547. models_dev_data: dict, curated: dict[str, list[str]], excluded: list[str]) -> list[str]:
  548. """Quick-scan which providers have credentials, without fetching model lists.
  549. Mirrors the credential checks of sections 1, 2 and 2b of :func:`list_authenticated_providers`
  550. but never calls ``cached_provider_model_ids``; feeds :func:`_prefetch_provider_models_parallel`.
  551. Env vars are read through the per-profile secret scope. AWS SDK providers are skipped
  552. (heavier detection)."""
  553. from hermes_cli.model_switch import _scoped_key_env
  554. from agent.models_dev import models_dev_provider_map
  555. from hermes_cli.auth import PROVIDER_REGISTRY
  556. from hermes_cli.providers import hermes_overlays
  557. from hermes_cli.models import CANONICAL_PROVIDERS
  558. excluded_set = {str(p).strip().lower() for p in excluded if p}
  559. slugs: list[str] = []
  560. seen: set[str] = set()
  561. def _emit(slug: str, *keys: str) -> None:
  562. slugs.append(slug)
  563. seen.update(k.lower() for k in keys)
  564. for hermes_id, _mdev_id, _pconfig, env_vars in _iter_builtin_candidates(models_dev_data, excluded_set, seen):
  565. if _any_env(env_vars, _scoped_key_env) or _raw_pool_usable(hermes_id):
  566. _emit(hermes_id, hermes_id)
  567. mdev_to_hermes = {v: k for k, v in models_dev_provider_map().items()}
  568. for pid, overlay in hermes_overlays().items():
  569. hermes_slug = mdev_to_hermes.get(pid, pid)
  570. if _skip(seen, excluded_set, pid, hermes_slug) or overlay.auth_type == "aws_sdk":
  571. continue
  572. if (
  573. _overlay_has_env_creds(pid, hermes_slug, overlay, _scoped_key_env)
  574. or _auth_store_has_provider(pid, hermes_slug) or _pool_usable(hermes_slug)):
  575. _emit(hermes_slug, pid, hermes_slug)
  576. for cp in CANONICAL_PROVIDERS:
  577. if _skip(seen, excluded_set, cp.slug):
  578. continue
  579. cp_config = PROVIDER_REGISTRY.get(cp.slug)
  580. has_creds = bool(
  581. cp_config and cp_config.api_key_env_vars and _any_env(cp_config.api_key_env_vars, _scoped_key_env))
  582. if has_creds or _auth_store_has_provider(cp.slug) or _pool_usable(cp.slug):
  583. _emit(cp.slug, cp.slug)
  584. # Nous excluded: its picker branch builds from the curated list and never reads the
  585. # api_key-only cache entry a prefetch would write.
  586. return [s for s in slugs if s != "nous"]
  587. @dataclass
  588. class _PickerBuild:
  589. """State threaded through the ``list_authenticated_providers`` sections: 1 built-ins mapped to
  590. models.dev, 2 Hermes-only overlays, 2b canonical providers missed by 1/2, 3 ``providers:``
  591. entries + 3b the bare active custom endpoint, 4 ``custom_providers:`` entries. Row-builder
  592. imports of ``hermes_cli.auth/models`` stay lazy so tests can patch those modules."""
  593. current_provider: str
  594. current_base_url: str
  595. current_model: str
  596. max_models: int | None
  597. for_picker: bool
  598. force_fresh_nous_tier: bool
  599. probe_custom_providers: bool
  600. probe_current_custom_provider: bool
  601. refresh: bool
  602. excluded: set
  603. curated: dict
  604. # Discovery budget override for custom endpoints (1.5s fast / 5s full). None keeps the
  605. # historical coupling to for_picker; callers that only want exhausted-pool visibility set
  606. # for_picker=True, fast_custom_probe=False so their probe budget is unchanged (#103843).
  607. fast_custom_probe: bool | None = None
  608. # GUI read path: catalogs are read from cache only; stale/missing ones warm in the background.
  609. non_blocking_catalogs: bool = False
  610. results: list = field(default_factory=list)
  611. seen_slugs: set = field(default_factory=set) # lowercase-normalized to catch case variants
  612. # Effective base URLs of every built-in row: section 4 hides ``custom_providers`` duplicates.
  613. builtin_endpoints: set = field(default_factory=set)
  614. # (display_name, base_url) pairs from section 3 so section 4 skips overlapping rows.
  615. section3_pairs: set = field(default_factory=set)
  616. @property
  617. def current_provider_norm(self) -> str:
  618. return self.current_provider.lower()
  619. @property
  620. def current_base_url_norm(self) -> str:
  621. return self.current_base_url.rstrip("/").lower()
  622. @property
  623. def resolved_fast_custom_probe(self) -> bool:
  624. """None defers to for_picker so pre-existing callers keep their timeout."""
  625. return self.for_picker if self.fast_custom_probe is None else self.fast_custom_probe
  626. def can_probe_custom(self, *, row_is_current: bool) -> bool:
  627. return bool(self.probe_custom_providers or (self.probe_current_custom_provider and row_is_current))
  628. def record_builtin_endpoint(self, slug: str) -> None:
  629. """Prefer the live env override (e.g. DASHSCOPE_BASE_URL) over the static inference_base_url
  630. so dedup matches what a user typing that URL into custom_providers would actually hit."""
  631. from hermes_cli.auth import PROVIDER_REGISTRY
  632. pcfg = PROVIDER_REGISTRY.get(slug)
  633. if not pcfg:
  634. return
  635. url = os.environ.get(pcfg.base_url_env_var, "") if getattr(pcfg, "base_url_env_var", "") else ""
  636. normed = _norm_url(url or getattr(pcfg, "inference_base_url", "") or "")
  637. if normed:
  638. self.builtin_endpoints.add(normed)
  639. def add_builtin_row(
  640. self, slug: str, name: str, is_current: bool, model_ids: list, source: str, *, uncapped_ok: bool = True,
  641. ) -> None:
  642. row = {
  643. "slug": slug, "name": name, "is_current": is_current, "is_user_defined": False,
  644. "models": _cap_models(model_ids, self.max_models, slug if uncapped_ok else ""),
  645. "total_models": len(model_ids), "source": source}
  646. if slug == "nous":
  647. # Free-tier identity: one row "Nous · free tier" / nous/welcome, or no row when
  648. # nous.guest is off. Still marks the slug seen so a later lap cannot re-emit it.
  649. row = _free_tier_nous_row(row)
  650. if row is not None:
  651. self.results.append(row)
  652. self.seen_slugs.add(slug.lower())
  653. self.record_builtin_endpoint(slug)
  654. def add_endpoint_row(
  655. self, slug: str, name: str, api_url: str, models: list, is_current: bool, native_catalog_empty: bool,
  656. *, source: str = "user-config", shown: list | None = None) -> None:
  657. """Append a user-defined endpoint row (sections 3, 3b, 4)."""
  658. self.results.append({
  659. "slug": slug, "name": name, "is_current": is_current, "is_user_defined": True,
  660. "models": models if shown is None else shown, "total_models": len(models), "source": source,
  661. "api_url": api_url, "native_catalog_empty": native_catalog_empty})
  662. self.seen_slugs.add(slug.lower())
  663. def record_section3_pair(self, name: str, url_norm: str) -> bool:
  664. """Remember a (display_name, base_url) pair for section-4 dedup; False when either is blank."""
  665. pair = (str(name).strip().lower(), url_norm)
  666. if not (pair[0] and pair[1]):
  667. return False
  668. self.section3_pairs.add(pair)
  669. return True
  670. def endpoint_is_current(self, slug: str, aliases: set, url_norm: str, *, url_match_ok: bool = True) -> bool:
  671. """Row is current by slug/alias, or (bare ``custom`` provider) by matching base_url."""
  672. return (
  673. str(slug).strip().lower() == self.current_provider_norm
  674. or self.current_provider_norm in aliases
  675. or (
  676. self.current_provider_norm == "custom" and bool(self.current_base_url_norm)
  677. and url_norm == self.current_base_url_norm and url_match_ok))
  678. def discover_endpoint(
  679. self, api_key: Any, api_url: str, native_provider: str, has_explicit_models: bool, *,
  680. headers: dict | None, api_mode: str | None, discovery_allowed: bool, is_current: bool,
  681. ) -> tuple[list | None, bool, bool]:
  682. """Probe policy shared by sections 3 and 4 (returns ``(models, native_empty, probed)``):
  683. with an api_key live /models is the source of truth (replaces the partial ``models:``
  684. subset); without one, an allowlist-shaped ``models:`` narrows a public endpoint and skips
  685. the probe. A dict-shaped ``models:`` is metadata, so still probe; pin with
  686. ``discover_models: false``."""
  687. probe_live = (
  688. discovery_allowed and (bool(api_key) or not has_explicit_models)
  689. and self.can_probe_custom(row_is_current=is_current))
  690. discovered, native_catalog_empty = _discover_endpoint_models(
  691. api_key, api_url, native_provider, has_explicit_models,
  692. headers=headers, api_mode=api_mode, probe_live=probe_live,
  693. discovery_allowed=discovery_allowed, fast_custom_probe=self.resolved_fast_custom_probe)
  694. return discovered, native_catalog_empty, probe_live
  695. def _lap_lmstudio_row(b: _PickerBuild, user_providers: dict) -> None:
  696. """Section 0: the active / ``providers:``-configured LM Studio row from the live catalog.
  697. LM Studio has no models.dev mapping and its overlay row (section 2) needs a credential, so a
  698. hand-written ``model.provider: lmstudio`` left the slug unclaimed until section 3, where a bare
  699. ``providers.lmstudio: {request_timeout_seconds: ...}`` block (no ``base_url``/``models``) cannot
  700. discover anything and rendered a one-model ``user-config`` row — discarding the catalog
  701. ``_build_curated_lists`` had already live-probed into ``b.curated["lmstudio"]``. A block that
  702. points the slug at an endpoint of its own stays with section 3's custom-endpoint handling."""
  703. if "lmstudio" in b.excluded or "lmstudio" in b.seen_slugs:
  704. return
  705. configured = user_providers.get("lmstudio")
  706. if isinstance(configured, dict) and _entry_base_url(configured, ("base_url", "api", "url")):
  707. return
  708. is_current = b.current_provider_norm == "lmstudio"
  709. if not (is_current or isinstance(configured, dict)):
  710. return
  711. from hermes_cli.model_switch import _declared_model_ids
  712. configured_models = _declared_model_ids(configured.get("models")) if isinstance(configured, dict) else []
  713. model_ids = list(dict.fromkeys([*configured_models, *b.curated.get("lmstudio", [])]))
  714. b.add_builtin_row("lmstudio", get_label("lmstudio"), is_current, model_ids, "hermes")
  715. def _lap_builtin_rows(b: _PickerBuild, data: dict, user_providers: dict) -> None:
  716. """Section 1: models.dev-mapped providers with api_key auth."""
  717. from hermes_cli.model_switch import _declared_model_ids, _scoped_key_env
  718. from agent.models_dev import get_provider_info
  719. for hermes_id, mdev_id, pconfig, env_vars in _iter_builtin_candidates(data, b.excluded, b.seen_slugs):
  720. # Per-profile scope, never raw os.environ: a secondary profile's picker otherwise listed the
  721. # LAUNCH profile's env-keyed providers and hid its own .env-keyed ones.
  722. if not (_any_env(env_vars, _scoped_key_env) or _raw_pool_usable(hermes_id, for_picker=b.for_picker)):
  723. continue
  724. model_ids = _live_or_curated_ids(hermes_id, b.curated, non_blocking=b.non_blocking_catalogs)
  725. # A providers.<built-in>.models block extends the discovered catalog; section 3 cannot
  726. # emit it later because this row owns the slug.
  727. configured = user_providers.get(hermes_id) if isinstance(user_providers, dict) else None
  728. configured_models = _declared_model_ids(configured.get("models")) if isinstance(configured, dict) else []
  729. model_ids = list(dict.fromkeys([*configured_models, *model_ids]))
  730. pinfo = get_provider_info(mdev_id)
  731. display_name = pconfig.name if pconfig and pconfig.name else (pinfo.name if pinfo else mdev_id)
  732. b.add_builtin_row(
  733. hermes_id, display_name, b.current_provider in (hermes_id, mdev_id), model_ids, "built-in")
  734. def _overlay_has_creds(b: _PickerBuild, pid: str, hermes_slug: str, overlay) -> bool:
  735. """Section-2 credential ladder: env/SDK, external-process executable, auth store, pool,
  736. anthropic's external credential files."""
  737. if overlay.auth_type == "aws_sdk":
  738. has_creds = _has_aws_sdk_creds_for_listing(hermes_slug, b.current_provider)
  739. else:
  740. from hermes_cli.model_switch import _scoped_key_env
  741. has_creds = _overlay_has_env_creds(pid, hermes_slug, overlay, _scoped_key_env)
  742. # External-process providers (ACP CLIs) hold no key/token/pool entry by design — the
  743. # spawned subprocess brings its own auth. "Configured" means the executable resolves.
  744. # "Configured" means the executable resolves, which is exactly what get_auth_status() reports for them;
  745. # without this branch the has_creds filter below unconditionally hides the provider from every picker
  746. # (#63662).
  747. if not has_creds and overlay.auth_type == "external_process":
  748. try:
  749. from hermes_cli.auth import get_auth_status
  750. _ext_status = get_auth_status(hermes_slug) or {}
  751. has_creds = bool(_ext_status.get("logged_in") or _ext_status.get("configured"))
  752. except Exception as exc:
  753. logger.debug("External-process check failed for %s: %s", pid, exc)
  754. # Auth store / credential pool cover OAuth providers AND api_key providers that also support
  755. # OAuth (anthropic via Claude Code credential files).
  756. has_creds = has_creds or _auth_store_has_provider(pid, hermes_slug)
  757. if not has_creds:
  758. # Full auto-seeding pool check catches external stores (Codex CLI ~/.codex/auth.json)
  759. # not yet in auth.json.
  760. try:
  761. if _credential_pool_is_usable(hermes_slug, for_picker=b.for_picker):
  762. has_creds = True
  763. except Exception as exc:
  764. logger.debug("Credential pool check failed for %s: %s", hermes_slug, exc)
  765. if not has_creds and hermes_slug == "anthropic":
  766. # The pool gates anthropic behind is_provider_explicitly_configured() (aux tasks must not
  767. # consume Claude Code tokens); the picker is discovery-oriented, so read the files directly.
  768. try:
  769. from agent.anthropic_credentials import read_claude_code_credentials, read_hermes_oauth_credentials
  770. hermes_creds = read_hermes_oauth_credentials()
  771. cc_creds = read_claude_code_credentials()
  772. if (hermes_creds and hermes_creds.get("accessToken")) or (cc_creds and cc_creds.get("accessToken")):
  773. has_creds = True
  774. except Exception as exc:
  775. logger.debug("Anthropic external creds check failed: %s", exc)
  776. return has_creds
  777. def _lap_overlay_rows(b: _PickerBuild, data: dict, user_providers: dict) -> None:
  778. """Section 2: Hermes-only providers (nous, openai-codex, opencode-go, plugin overlays, ...)."""
  779. from agent.models_dev import models_dev_provider_map
  780. from hermes_cli.model_switch import _declared_model_ids
  781. from hermes_cli.providers import hermes_overlays
  782. # Overlay keys may be models.dev IDs while config.yaml uses Hermes IDs.
  783. mdev_to_hermes = {v: k for k, v in models_dev_provider_map().items()}
  784. for pid, overlay in hermes_overlays().items():
  785. hermes_slug = mdev_to_hermes.get(pid, pid)
  786. if _skip(b.seen_slugs, b.excluded, pid, hermes_slug):
  787. continue
  788. if not _overlay_has_creds(b, pid, hermes_slug, overlay):
  789. continue
  790. if hermes_slug == "openai-codex":
  791. # Live OAuth-backed discovery so Pro-only Codex slugs not in the static catalog
  792. # appear; falls back to curated when unreachable (or not yet cached on the read path).
  793. model_ids = _live_or_curated_ids(hermes_slug, b.curated, merge_models_dev=False,
  794. non_blocking=b.non_blocking_catalogs)
  795. elif overlay.auth_type == "aws_sdk":
  796. model_ids = _aws_live_or_curated_ids(hermes_slug, b.curated, hermes_slug, pid,
  797. non_blocking=b.non_blocking_catalogs)
  798. elif hermes_slug == "nous":
  799. # A guest identity never needs the Portal catalog: add_builtin_row pins nous/welcome
  800. # (or drops the row when nous.guest is off), so only a real account fetches.
  801. tier_row = _free_tier_nous_row({"name": get_label(hermes_slug), "models": []})
  802. real_account = tier_row is not None and not tier_row["models"]
  803. model_ids = _nous_picker_model_ids(b.curated, b.force_fresh_nous_tier) if real_account else []
  804. else:
  805. model_ids = _live_or_curated_ids(hermes_slug, b.curated, hermes_slug, pid,
  806. non_blocking=b.non_blocking_catalogs)
  807. # A providers.<overlay>.models block extends the row exactly as it does for built-in rows
  808. # (section 1); section 3 never emits it because this row owns the slug (#27989).
  809. configured = user_providers.get(hermes_slug) or user_providers.get(pid) if isinstance(user_providers, dict) else None
  810. if isinstance(configured, dict):
  811. model_ids = list(dict.fromkeys([*_declared_model_ids(configured.get("models")), *model_ids]))
  812. b.add_builtin_row(
  813. hermes_slug, get_label(hermes_slug), b.current_provider in (hermes_slug, pid), model_ids, "hermes")
  814. b.seen_slugs.add(pid.lower())
  815. def _lap_canonical_rows(b: _PickerBuild) -> None:
  816. """Section 2b: CANONICAL_PROVIDERS missed by sections 1/2."""
  817. from hermes_cli.auth import PROVIDER_REGISTRY
  818. from hermes_cli.models import CANONICAL_PROVIDERS
  819. for cp in CANONICAL_PROVIDERS:
  820. if _skip(b.seen_slugs, b.excluded, cp.slug):
  821. continue
  822. cp_config = PROVIDER_REGISTRY.get(cp.slug)
  823. has_creds = False
  824. if cp_config and cp_config.api_key_env_vars:
  825. lit = {ev for ev in cp_config.api_key_env_vars if os.environ.get(ev)}
  826. has_creds = bool(lit)
  827. # A regional "-cn" twin lit only by key vars shared with its non-CN sibling is a
  828. # phantom row: hide it unless it is the current provider, and only when it has a
  829. # dedicated var of its own the user could set.
  830. sib = PROVIDER_REGISTRY.get(cp.slug[:-3]) if cp.slug.endswith("-cn") else None
  831. sib_vars = set(sib.api_key_env_vars) if sib else set()
  832. if lit and lit <= sib_vars < set(cp_config.api_key_env_vars) and cp.slug != b.current_provider:
  833. continue
  834. has_creds = has_creds or _auth_store_has_provider(cp.slug) or _pool_usable(cp.slug, for_picker=b.for_picker) or (
  835. _is_aws_sdk(cp_config) and _has_aws_sdk_creds_for_listing(cp.slug, b.current_provider))
  836. if not has_creds and cp_config is not None and cp_config.auth_type == "external_process":
  837. # Subprocess-backed providers own their auth; the binary resolving is the credential
  838. # evidence for listing (same gate as the section-2 overlay rows and hermes auth status).
  839. try:
  840. from hermes_cli.auth import get_external_process_provider_status
  841. has_creds = bool(get_external_process_provider_status(cp.slug).get("configured"))
  842. except Exception as exc:
  843. logger.debug("External-process check failed for %s: %s", cp.slug, exc)
  844. if not has_creds:
  845. continue
  846. if _is_aws_sdk(cp_config):
  847. model_ids = _aws_live_or_curated_ids(cp.slug, b.curated, non_blocking=b.non_blocking_catalogs)
  848. else:
  849. model_ids = _live_or_curated_ids(cp.slug, b.curated, merge_models_dev=False,
  850. non_blocking=b.non_blocking_catalogs)
  851. b.add_builtin_row(
  852. cp.slug, cp.label, cp.slug == b.current_provider, model_ids, "canonical", uncapped_ok=False)
  853. def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
  854. """Section 3: ``providers:`` dict entries, grouped by (api_url, credential, api_mode,
  855. extra_headers) so keyed providers on one endpoint with the same wire protocol collapse into
  856. one row (two Palantir Claude entries -> one "Palantir Claude" row); a different
  857. key_env/api_mode/headers keeps distinct rows since the wire protocol or tenant differs."""
  858. from hermes_cli.model_switch import _extra_headers_from_config, _scoped_key_env
  859. from hermes_cli.config import coerce_provider_id, is_provider_enabled
  860. ep_groups: dict[tuple, dict] = {}
  861. for ep_name, ep_cfg in user_providers.items():
  862. if not isinstance(ep_cfg, dict) or not is_provider_enabled(ep_cfg) or ep_name.lower() in b.seen_slugs:
  863. continue
  864. display_name = coerce_provider_id(ep_cfg.get("name")) or ep_name
  865. api_url = _entry_base_url(ep_cfg, ("base_url", "api", "url"))
  866. inline_api_key, key_env, cred_identity = _entry_credentials(ep_cfg, "key_env", "api_key_env")
  867. headers = _extra_headers_from_config(ep_cfg)
  868. group_key = (_norm_url(api_url), cred_identity, _entry_api_mode(ep_cfg), tuple(sorted(headers.items())))
  869. if group_key not in ep_groups:
  870. # slug = first ep_name encountered; probe key from the first member (inline api_key,
  871. # else key_env through the per-profile secret scope).
  872. ep_groups[group_key] = {
  873. "slug": ep_name, "name": _group_display_name(display_name), "api_url": api_url, "models": [],
  874. "has_explicit_models": False,
  875. "api_key": inline_api_key or _scoped_key_env(key_env),
  876. "headers": headers, "api_mode": ep_cfg.get("api_mode"),
  877. "discovery_allowed": bool(api_url) and _discover_flag(ep_cfg), "raw_names": [], "aliases": set()}
  878. grp = ep_groups[group_key]
  879. # ``default_model`` is the legacy key; ``model`` matches custom_providers.
  880. _absorb_entry_models(grp, ep_cfg, ep_cfg.get("default_model", "") or ep_cfg.get("model", ""))
  881. grp["raw_names"].append(display_name)
  882. grp["aliases"].update(custom_provider_aliases(display_name, str(ep_name)))
  883. for grp in ep_groups.values():
  884. ep_name, display_name, api_url = grp["slug"], grp["name"], grp["api_url"]
  885. models_list = list(grp["models"])
  886. # Official OpenAI rows often have base_url but no models: dict — avoid a misleading zero count.
  887. if not models_list and base_url_host_matches(str(api_url).strip().lower(), "api.openai.com"):
  888. models_list = list(b.curated.get("openai") or [])
  889. ep_url_norm = _norm_url(api_url)
  890. ep_aliases = {str(alias).lower() for alias in grp["aliases"]}
  891. is_current = b.endpoint_is_current(ep_name, ep_aliases, ep_url_norm)
  892. discovered, native_catalog_empty, _ = b.discover_endpoint(
  893. grp["api_key"], api_url,
  894. ep_name if str(ep_name).strip().lower() in {"ollama", "custom:ollama"} else "custom",
  895. grp["has_explicit_models"], headers=grp["headers"] or None, api_mode=grp["api_mode"],
  896. discovery_allowed=grp["discovery_allowed"], is_current=is_current)
  897. if discovered is not None:
  898. models_list = discovered
  899. b.add_endpoint_row(ep_name, display_name, api_url, models_list, is_current, native_catalog_empty)
  900. b.seen_slugs.update(ep_aliases)
  901. # Record every raw member name so section 4 can match per-model custom_providers rows
  902. # even though the group label was collapsed.
  903. for raw_name in grp["raw_names"] or [display_name]:
  904. if b.record_section3_pair(raw_name, ep_url_norm):
  905. b.seen_slugs.add(custom_provider_slug(raw_name).lower())
  906. b.record_section3_pair(display_name, ep_url_norm)
  907. def _lap_bare_custom_row(b: _PickerBuild, custom_providers: list | None) -> None:
  908. """Section 3b: ``model.provider: custom`` + ``model.base_url`` with no named
  909. providers:/custom_providers row — surface it so /model does not look like it ignored
  910. config.yaml.
  911. The endpoint comes from the caller's current-provider slice when the session is on it,
  912. and otherwise from the static config.yaml ``model:`` block: the row used to appear only
  913. while it happened to be the active provider, so switching away made it vanish and the
  914. unconfigured-canonical fallback substituted a misleading 0-model placeholder (#59702).
  915. ``is_current`` is then true only when the caller's slice points back at this URL."""
  916. if "custom" in b.seen_slugs:
  917. return
  918. base_url = default_model = ""
  919. if b.current_provider_norm == "custom" and b.current_base_url:
  920. base_url = b.current_base_url
  921. default_model = b.current_model
  922. else:
  923. try:
  924. from hermes_cli.config import load_config
  925. cfg_model = load_config().get("model")
  926. except Exception: # health: allow BLE001 -- config.yaml may be user-edited or mid-migration; a read failure must degrade to "no row", never crash the picker
  927. cfg_model = None
  928. # config.model can be a bare string in older configs (see load_picker_context);
  929. # only a mapping carries a provider/base_url to recover here.
  930. if not isinstance(cfg_model, dict):
  931. return
  932. base_url = str(cfg_model.get("base_url") or "").strip()
  933. if str(cfg_model.get("provider") or "").strip().lower() != "custom" or not base_url:
  934. return
  935. default_model = str(cfg_model.get("default", cfg_model.get("name", "")) or "").strip()
  936. if any(
  937. isinstance(cp, dict) and _norm_url(_entry_base_url(cp)) == _norm_url(base_url)
  938. for cp in (custom_providers or [])):
  939. return
  940. api_url = base_url.strip().rstrip("/")
  941. is_current = b.current_provider_norm == "custom" and b.current_base_url_norm == _norm_url(api_url)
  942. models = [default_model] if default_model else []
  943. native_catalog_empty = False
  944. try:
  945. discovered, native_catalog_empty = _discover_endpoint_models(
  946. "", api_url, "custom", False, headers=None, api_mode=None,
  947. probe_live=bool(b.refresh or (b.probe_current_custom_provider and is_current)),
  948. discovery_allowed=True,
  949. fast_custom_probe=b.resolved_fast_custom_probe)
  950. if discovered is not None:
  951. models = discovered
  952. except Exception:
  953. pass
  954. b.add_endpoint_row(
  955. "custom", "Custom endpoint", api_url, models, is_current, native_catalog_empty,
  956. source="model-config", shown=_cap_models(models, b.max_models))
  957. def _lap_custom_provider_rows(b: _PickerBuild, custom_providers: list) -> None:
  958. """Section 4: ``custom_providers:`` entries (one model each) grouped into one row per
  959. (endpoint, credential identity, api_mode, extra_headers, display prefix). Four "Ollama — X"
  960. entries on one host become one "Ollama" row; distinct prefixes sharing a proxy URL keep
  961. their own rows."""
  962. from hermes_cli.model_switch import _extra_headers_from_config, _scoped_key_env
  963. from hermes_cli.config import coerce_provider_id
  964. groups: dict[tuple, dict] = {}
  965. for entry in custom_providers:
  966. if not isinstance(entry, dict):
  967. continue
  968. raw_name = coerce_provider_id(entry.get("name"))
  969. api_url = str(_entry_base_url(entry) or "").strip().rstrip("/")
  970. if not raw_name or not api_url:
  971. continue
  972. inline_api_key, key_env, cred_identity = _entry_credentials(entry, "key_env")
  973. api_key = inline_api_key or _scoped_key_env(key_env)
  974. api_mode = _entry_api_mode(entry)
  975. discover = _discover_flag(entry)
  976. entry_extra_headers = _extra_headers_from_config(entry)
  977. prefix = _display_prefix(raw_name)
  978. provider_key = str(entry.get("provider_key") or "").strip()
  979. group_key = (api_url, cred_identity, api_mode, tuple(sorted(entry_extra_headers.items())), prefix.lower())
  980. display_name = prefix or raw_name
  981. grp = groups.setdefault(group_key, {
  982. "slug": custom_provider_slug(display_name, provider_key), "name": display_name,
  983. "api_url": api_url, "api_key": "", "credential_identity": cred_identity, "models": [], "has_explicit_models": False,
  984. "discover_models": True, "api_mode": api_mode, "extra_headers": entry_extra_headers,
  985. "aliases": set()})
  986. grp["api_key"] = grp["api_key"] or api_key # first member with a key wins
  987. grp["discover_models"] = grp["discover_models"] and discover # one opt-out pins the whole row
  988. grp["aliases"].update(custom_provider_aliases(raw_name, provider_key))
  989. # ``model:`` is only the active selection; every configured model lives under ``models:``.
  990. _absorb_entry_models(grp, entry, (entry.get("model") or "").strip())
  991. section4_slugs: set = set()
  992. current_url_group_count = sum(
  993. 1 for grp in groups.values()
  994. if b.current_base_url_norm and _norm_url(grp["api_url"]) == b.current_base_url_norm)
  995. for grp in groups.values():
  996. api_url, api_key, slug = grp["api_url"], grp.get("api_key", ""), grp["slug"]
  997. # Slug claimed by a built-in/overlay/providers: row -> skip (don't shadow).
  998. if slug.lower() in b.seen_slugs and slug.lower() not in section4_slugs:
  999. continue
  1000. # Two custom endpoints with the same cleaned name: suffix a counter so both stay visible.
  1001. if slug.lower() in section4_slugs:
  1002. base_slug, n = slug, 2
  1003. while f"{base_slug}-{n}".lower() in b.seen_slugs:
  1004. n += 1
  1005. slug = f"{base_slug}-{n}"
  1006. grp["slug"] = slug
  1007. grp_url_norm = _norm_url(api_url)
  1008. pair_key = (str(grp["name"]).strip().lower(), grp_url_norm)
  1009. if pair_key[0] and pair_key[1] and pair_key in b.section3_pairs:
  1010. continue
  1011. # A built-in row already represents this endpoint (e.g. "my-dashscope" vs the
  1012. # alibaba-coding-plan row): keep the built-in, hide the shadow.
  1013. if grp_url_norm and grp_url_norm in b.builtin_endpoints:
  1014. continue
  1015. is_current = b.endpoint_is_current(
  1016. slug, {str(alias).lower() for alias in grp["aliases"]}, grp_url_norm,
  1017. url_match_ok=current_url_group_count == 1)
  1018. discovered, native_catalog_empty, probe_live = b.discover_endpoint(
  1019. api_key, api_url,
  1020. "ollama" if "ollama" in {str(slug).strip().lower(), str(grp.get("name") or "").strip().lower()} else "custom",
  1021. bool(grp.get("has_explicit_models")), headers=grp.get("extra_headers") or None,
  1022. api_mode=grp.get("api_mode"), discovery_allowed=bool(api_url) and grp.get("discover_models", True),
  1023. is_current=is_current)
  1024. if discovered is not None:
  1025. grp["models"] = discovered
  1026. if probe_live: # a successful live probe persists the catalog for no-probe surfaces
  1027. try:
  1028. _save_discovered_models_to_config(
  1029. api_url, discovered, api_mode=grp.get("api_mode"), headers=grp.get("extra_headers") or None,
  1030. credential_identity=grp["credential_identity"])
  1031. except Exception:
  1032. pass
  1033. b.add_endpoint_row(slug, grp["name"], grp["api_url"], grp["models"], is_current, native_catalog_empty)
  1034. section4_slugs.add(slug.lower())
  1035. def _build_curated_lists(current_provider: str, current_base_url: str, current_model: str,
  1036. non_blocking: bool = False) -> dict[str, list[str]]:
  1037. """Curated model lists keyed by hermes provider id, plus the dynamic ones (nous manifest,
  1038. Ollama Cloud, LM Studio live probe). ``non_blocking`` (GUI read path) takes cached Ollama Cloud
  1039. ids and warms them in the background rather than waiting on an 8s probe (#114215)."""
  1040. from hermes_cli.models import OPENROUTER_MODELS, _PROVIDER_MODELS, get_curated_nous_model_ids
  1041. curated: dict[str, list[str]] = dict(_PROVIDER_MODELS)
  1042. curated["openrouter"] = [mid for mid, _ in OPENROUTER_MODELS]
  1043. # Plugin profiles without a built-in row: this home's static_models, else fallback_models, are the
  1044. # curated floor, so the non-blocking GUI read (cold catalog cache) never lists an empty provider row.
  1045. from providers import list_providers
  1046. for _pp in list_providers():
  1047. if not curated.get(_pp.name) and (rows := list(_pp.static_models or _pp.fallback_models)):
  1048. curated[_pp.name] = rows
  1049. # Remote manifest so new Portal models surface without a release; in-repo snapshot fallback.
  1050. curated["nous"] = get_curated_nous_model_ids()
  1051. if "ollama-cloud" not in curated:
  1052. from hermes_cli.models import fetch_ollama_cloud_models
  1053. # Read path: cache only; the row's own SWR refresh (cached_provider_model_ids) warms it.
  1054. curated["ollama-cloud"] = fetch_ollama_cloud_models(cache_only=non_blocking)
  1055. # LM Studio has no static catalog: probe its native endpoint live. Base URL precedence:
  1056. # LM_BASE_URL > active config base_url (when current) > default. On auth rejection /
  1057. # unreachable, fall back to the current model so the picker still shows something offline.
  1058. # Left live even on the non-blocking path: it is a loopback endpoint with a 1.5s timeout, so it
  1059. # cannot be the degraded remote provider this path must never wait on.
  1060. is_current_lmstudio = current_provider.strip().lower() == "lmstudio"
  1061. if "lmstudio" not in curated and (os.environ.get("LM_API_KEY") or os.environ.get("LM_BASE_URL") or is_current_lmstudio):
  1062. from hermes_cli.models_local import fetch_lmstudio_models
  1063. from hermes_cli.auth import AuthError
  1064. lm_base = (
  1065. os.environ.get("LM_BASE_URL")
  1066. or (current_base_url if is_current_lmstudio and current_base_url else None)
  1067. or "http://127.0.0.1:1234/v1")
  1068. try:
  1069. live = fetch_lmstudio_models(api_key=os.environ.get("LM_API_KEY", ""), base_url=lm_base, timeout=1.5)
  1070. except AuthError:
  1071. live = []
  1072. if not live and is_current_lmstudio and current_model:
  1073. live = [current_model]
  1074. curated["lmstudio"] = live
  1075. return curated
  1076. def list_authenticated_providers(
  1077. current_provider: str = "", current_base_url: str = "", user_providers: dict | None = None,
  1078. custom_providers: list | None = None, *, force_fresh_nous_tier: bool = False,
  1079. max_models: int | None = None, current_model: str = "", refresh: bool = False,
  1080. probe_custom_providers: bool = True, probe_current_custom_provider: bool = False,
  1081. for_picker: bool = False, excluded_providers: list | None = None,
  1082. non_blocking_catalogs: bool = False, fast_custom_probe: bool | None = None) -> list[dict]:
  1083. """Detect which providers have credentials and list their curated (not full models.dev) models.
  1084. Returns dicts with ``slug`` (the --provider value), ``name``, ``is_current``,
  1085. ``is_user_defined``, ``models`` (up to max_models), ``total_models``, ``source``
  1086. ("built-in", "hermes", "canonical", "user-config", "model-config").
  1087. ``force_fresh_nous_tier`` bypasses the short Nous tier cache (account-sensitive flows only);
  1088. ``refresh`` busts the model-id disk cache up front (explicit user action only);
  1089. ``probe_custom_providers`` enables live ``/models`` discovery for saved custom endpoints (CLI
  1090. true, GUI false); ``probe_current_custom_provider`` probes only the selected custom endpoint.
  1091. ``non_blocking_catalogs`` is the GUI read path (``model.options``): provider catalogs come from
  1092. the disk cache only and stale/missing ones warm in the background, so a degraded provider
  1093. never stalls the picker (#114215). ``fast_custom_probe`` overrides the custom-endpoint
  1094. discovery budget ``for_picker`` otherwise implies (1.5s vs 5s) — ``None`` keeps the
  1095. historical coupling, ``False`` retains the full 5s budget for callers that only want
  1096. exhausted-pool visibility (#103843)."""
  1097. from agent.models_dev import fetch_models_dev
  1098. from hermes_cli.config import coerce_provider_id, stringify_provider_map
  1099. non_blocking_catalogs = bool(non_blocking_catalogs)
  1100. # Explicit refresh: drop every cached list so the calls below re-fetch live. A stale cache
  1101. # can fall back to the curated static list when its live fetch fails, silently dropping
  1102. # live-only models the user had seen.
  1103. if refresh:
  1104. non_blocking_catalogs = False # an explicit refresh is allowed to block on live probes
  1105. try:
  1106. from hermes_cli.models import clear_provider_models_cache
  1107. clear_provider_models_cache()
  1108. except Exception:
  1109. pass
  1110. # YAML parses unquoted numeric names (`provider: 2070`) as int.
  1111. # seen_slugs: set = set() # lowercase-normalized to catch case variants (#9545)
  1112. current_provider = coerce_provider_id(current_provider)
  1113. current_base_url = str(current_base_url or "").strip()
  1114. current_model = str(current_model or "").strip()
  1115. user_providers = stringify_provider_map(user_providers)
  1116. data = fetch_models_dev()
  1117. # A single excluded entry like ``kimi-coding`` hides the provider under every key it surfaces
  1118. # as (hermes_id / mdev_id / canonical slug).
  1119. b = _PickerBuild(
  1120. current_provider=current_provider, current_base_url=current_base_url, current_model=current_model,
  1121. max_models=max_models, for_picker=for_picker, force_fresh_nous_tier=force_fresh_nous_tier,
  1122. probe_custom_providers=probe_custom_providers, probe_current_custom_provider=probe_current_custom_provider,
  1123. refresh=refresh, excluded={str(p).strip().lower() for p in (excluded_providers or []) if p},
  1124. non_blocking_catalogs=non_blocking_catalogs, fast_custom_probe=fast_custom_probe,
  1125. curated=_build_curated_lists(current_provider, current_base_url, current_model,
  1126. non_blocking=non_blocking_catalogs))
  1127. # Warm the disk cache in parallel before the serial section loops (otherwise 15-30s of live
  1128. # round-trips on a cold cache). Skipped when refresh=True (serial path force-refreshes) and
  1129. # for <=3 providers (serial is fast enough; avoids thread-pool overhead). The GUI read path
  1130. # collects nothing either: every cache-only row read spawns its own deduped background refresh,
  1131. # so no thread pool is joined and no probe can hold up the response.
  1132. prefetch_slugs = ([] if (refresh or non_blocking_catalogs)
  1133. else _collect_authed_provider_slugs(data, b.curated, excluded_providers or []))
  1134. if len(prefetch_slugs) > 3:
  1135. try:
  1136. _prefetch_provider_models_parallel(prefetch_slugs)
  1137. except Exception:
  1138. pass # best-effort; serial path still works
  1139. _lap_lmstudio_row(b, user_providers if isinstance(user_providers, dict) else {})
  1140. _lap_builtin_rows(b, data, user_providers)
  1141. _lap_overlay_rows(b, data, user_providers)
  1142. _lap_canonical_rows(b)
  1143. if user_providers and isinstance(user_providers, dict):
  1144. _lap_user_provider_rows(b, user_providers)
  1145. _lap_bare_custom_row(b, custom_providers)
  1146. if custom_providers and isinstance(custom_providers, list):
  1147. _lap_custom_provider_rows(b, custom_providers)
  1148. return _finalize_picker_rows(b.results, user_providers, current_model)
  1149. def _finalize_picker_rows(results: list, user_providers, current_model: str) -> list:
  1150. """Post-passes: drop ``providers.<name>.enabled: false`` rows, inject the current model, sort."""
  1151. # The enabled post-filter covers built-in rows (sections 1-2) that bypass the per-section
  1152. # gate; matched by slug and ``provider_id``.
  1153. try:
  1154. from hermes_cli.config import is_provider_enabled
  1155. if isinstance(user_providers, dict):
  1156. disabled = {
  1157. str(name).strip().lower() for name, cfg in user_providers.items()
  1158. if isinstance(cfg, dict) and not is_provider_enabled(cfg)}
  1159. if disabled:
  1160. results = [
  1161. r for r in results
  1162. if str(r.get("provider_id", "")).strip().lower() not in disabled
  1163. and str(r.get("slug", "")).strip().lower() not in disabled]
  1164. except Exception:
  1165. pass
  1166. # A custom/uncurated model set via `/model <provider>/<name>` would be invisible in every
  1167. # picker (main and MoA slot pickers read these rows); inject it at the front of the current
  1168. # provider's row.
  1169. if current_model:
  1170. for row in results:
  1171. if not row.get("is_current") or row.get("native_catalog_empty"):
  1172. continue
  1173. models = row.get("models") or []
  1174. if current_model not in models:
  1175. from hermes_cli.models import _model_requires_account_discovery
  1176. if _model_requires_account_discovery(row.get("slug"), current_model):
  1177. break
  1178. row["models"] = [current_model, *models]
  1179. row["total_models"] = row.get("total_models", len(models)) + 1
  1180. break
  1181. # Current provider first, then by model count descending
  1182. results.sort(key=lambda r: (not r["is_current"], -r["total_models"]))
  1183. return results
  1184. def _prepend_moa_picker_provider(providers: list[dict], current_provider: str = "") -> list[dict]:
  1185. """Add the virtual MoA provider row used by interactive model pickers.
  1186. ``list_authenticated_providers()`` only returns real/auth-backed providers; the CLI inventory
  1187. adds MoA separately, so gateway pickers need the same virtual row here. Reuses the
  1188. inventory's single row builder so the row shape stays defined in one place."""
  1189. try:
  1190. from hermes_cli.inventory import _moa_provider_row
  1191. moa_row = _moa_provider_row(current_provider)
  1192. if moa_row is None:
  1193. return providers
  1194. return [moa_row] + [p for p in providers if str(p.get("slug", "")).lower() != "moa"]
  1195. except Exception:
  1196. return providers
  1197. def list_picker_providers(
  1198. current_provider: str = "", current_base_url: str = "", user_providers: dict | None = None,
  1199. custom_providers: list | None = None, max_models: int | None = None, current_model: str = "",
  1200. include_moa: bool = False, excluded_providers: list | None = None,
  1201. non_blocking_catalogs: bool = False, probe_custom_providers: bool = True,
  1202. probe_current_custom_provider: bool = False) -> list[dict]:
  1203. """Interactive-picker variant of :func:`list_authenticated_providers`.
  1204. OpenRouter's list is replaced with :func:`hermes_cli.models.fetch_openrouter_models` (curated
  1205. snapshot filtered against the live catalog) and rows left with no models are dropped — except
  1206. custom endpoints, where the user may supply their own model set through config.
  1207. ``non_blocking_catalogs`` makes every catalog read cache-only: provider catalogs warm in the
  1208. background, OpenRouter's stale disk copy is served as-is; the ``probe_*`` flags are forwarded."""
  1209. from hermes_cli.model_switch import list_authenticated_providers
  1210. from hermes_cli.models import fetch_openrouter_models
  1211. providers = list_authenticated_providers(
  1212. current_provider=current_provider, current_base_url=current_base_url,
  1213. user_providers=user_providers, custom_providers=custom_providers, max_models=max_models,
  1214. current_model=current_model, for_picker=True, excluded_providers=excluded_providers,
  1215. non_blocking_catalogs=non_blocking_catalogs, probe_custom_providers=probe_custom_providers,
  1216. probe_current_custom_provider=probe_current_custom_provider)
  1217. if include_moa:
  1218. providers = _prepend_moa_picker_provider(providers, current_provider=current_provider)
  1219. filtered: list[dict] = []
  1220. for p in providers:
  1221. if str(p.get("slug", "")).lower() == "openrouter":
  1222. try:
  1223. live_ids = [mid for mid, _ in fetch_openrouter_models(cache_only=non_blocking_catalogs)]
  1224. except Exception:
  1225. live_ids = list(p.get("models", []))
  1226. p = dict(p)
  1227. p["models"] = _cap_models(live_ids, max_models, "openrouter")
  1228. p["total_models"] = len(live_ids)
  1229. is_custom_endpoint = bool(p.get("is_user_defined")) and bool(p.get("api_url"))
  1230. if p.get("models") or is_custom_endpoint:
  1231. filtered.append(p)
  1232. from hermes_cli.models_validate import drop_unofferable_model_ids
  1233. drop_unofferable_model_ids(filtered)
  1234. _attach_window_model_notes(filtered)
  1235. return filtered
  1236. def _attach_window_model_notes(providers: list[dict]) -> None:
  1237. """Nous on a usage-window plan, for the messaging pickers (same words as the CLI/TUI/desktop pickers):
  1238. ``model_labels`` {id: "Large" | "Free"} (plan shape, any-age snapshot; other levels carry no tag), and
  1239. from a fresh snapshot only (a stale percent would mislabel it) ``plan_line`` (the limit line: "Weekly
  1240. limit reached · resets in 21h · paid models now use your credits (pay as you go)").
  1241. Every field is omitted when empty and for any account not on a window plan. Never raises."""
  1242. try:
  1243. nous = next((p for p in providers if str(p.get("slug", "")).lower() == "nous"), None)
  1244. if nous is None:
  1245. return
  1246. from hermes_cli.model_picker_usage import nous_picker_usage, usage_level_label, usage_tag_levels
  1247. fresh = nous_picker_usage()
  1248. usage = fresh or nous_picker_usage(any_age=True)
  1249. if usage is None:
  1250. return
  1251. from agent.plan_limits import cached_pricing
  1252. pricing = cached_pricing() # this profile's catalog only (cache-only, no credential refresh)
  1253. models = [str(m) for m in nous.get("models", [])]
  1254. # Subscription-billed rows carry no tag (usage_tag_levels).
  1255. labels = {m: label for m, lv in usage_tag_levels(models, pricing).items() if (label := usage_level_label(lv))}
  1256. if labels:
  1257. nous["model_labels"] = labels
  1258. if fresh is None:
  1259. return
  1260. line = usage.limit_line()
  1261. if line:
  1262. nous["plan_line"] = line
  1263. except Exception:
  1264. logger.debug("window model notes failed", exc_info=True)