inventory.py 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  1. """Provider/model inventory — shared substrate for the dashboard ``/api/model/options``, the TUI
  2. ``model.options``/``model.save_key`` RPC handlers, and the interactive picker."""
  3. from __future__ import annotations
  4. import logging
  5. from contextvars import copy_context
  6. from dataclasses import dataclass, replace
  7. from threading import Lock, Thread, current_thread
  8. from typing import Any, Optional
  9. from datetime import UTC
  10. _pricing_prewarm_lock = Lock()
  11. _pricing_prewarm_threads: dict[tuple[str, tuple[tuple[str, str], ...]], Thread] = {}
  12. @dataclass(frozen=True)
  13. class ConfigContext:
  14. """Disk-config snapshot (``load_picker_context()``); the TUI overlays live agent state via
  15. ``with_overrides()``."""
  16. current_provider: str
  17. current_model: str
  18. current_base_url: str
  19. user_providers: dict
  20. custom_providers: list
  21. excluded_providers: list = None
  22. def with_overrides(
  23. self, *, current_provider: Optional[str] = None, current_model: Optional[str] = None,
  24. current_base_url: Optional[str] = None,
  25. ) -> ConfigContext:
  26. """Copy with TRUTHY overrides applied: the TUI reads agent attributes that may be empty strings
  27. before an agent is spawned — empties must not clobber the disk-config values."""
  28. overrides = (("current_provider", current_provider), ("current_model", current_model),
  29. ("current_base_url", current_base_url))
  30. kw = {k: v for k, v in overrides if v}
  31. return replace(self, **kw) if kw else self
  32. def load_picker_context() -> ConfigContext:
  33. """Load the disk-config snapshot every consumer needs."""
  34. from hermes_cli.config import (
  35. coerce_provider_id, get_compatible_custom_providers, load_config, stringify_provider_map,
  36. )
  37. cfg = load_config()
  38. model_cfg = cfg.get("model", {})
  39. if isinstance(model_cfg, dict):
  40. # YAML parses unquoted scalars as int (`provider: 2070`); keep strings so picker/options
  41. # paths never call `.strip()` on an int.
  42. current_model = str(model_cfg.get("default", model_cfg.get("name", "")) or "")
  43. current_provider = coerce_provider_id(model_cfg.get("provider", ""))
  44. current_base_url = str(model_cfg.get("base_url", "") or "")
  45. else: # config.model can be a bare string in older configs
  46. current_model, current_provider, current_base_url = (str(model_cfg) if model_cfg else ""), "", ""
  47. excluded = cfg.get("model_catalog", {}).get("excluded_providers") or []
  48. return ConfigContext(
  49. current_provider=current_provider, current_model=current_model, current_base_url=current_base_url,
  50. user_providers=stringify_provider_map(cfg.get("providers")),
  51. custom_providers=get_compatible_custom_providers(cfg),
  52. excluded_providers=excluded if isinstance(excluded, list) else [],
  53. )
  54. def _slug(row: dict) -> str:
  55. return str(row.get("slug") or "").strip().lower()
  56. def _without_slug(rows: list[dict], slug: str) -> list[dict]:
  57. return [r for r in rows if _slug(r) != slug]
  58. # ─── Public: payload builder ────────────────────────────────────────────
  59. def build_models_payload(
  60. ctx: ConfigContext, *, explicit_only: bool = False, include_unconfigured: bool = False,
  61. picker_hints: bool = False, canonical_order: bool = False, pricing: bool = False,
  62. pricing_cache_only: bool = False,
  63. capabilities: bool = False, featured: bool = False, force_fresh_nous_tier: bool = False,
  64. refresh: bool = False, probe_custom_providers: bool = True, probe_current_custom_provider: bool = False,
  65. for_picker: bool = False, max_models: int | None = None, non_blocking_catalogs: bool = False,
  66. fast_custom_probe: bool | None = None,
  67. ) -> dict:
  68. """Build the ``{providers, model, provider}`` shape every consumer needs. ``explicit_only`` keeps
  69. only providers the user explicitly configured — hides ambient/auto-seeded credentials from
  70. desktop chat pickers. ``pricing_cache_only``: with ``pricing``, use only values already resident
  71. in process caches (normal picker opens, while a background worker warms cold endpoints).
  72. ``non_blocking_catalogs``: provider catalogs come from the disk cache only — a degraded provider
  73. cannot stall the response (GUI picker opens). ``fast_custom_probe`` overrides the
  74. custom-endpoint discovery budget ``for_picker`` otherwise implies (1.5s vs 5s) — ``None`` keeps
  75. the coupling, ``False`` retains the full 5s budget (#103843)."""
  76. from hermes_cli.model_switch import list_authenticated_providers
  77. rows = list_authenticated_providers(
  78. current_provider=ctx.current_provider, current_base_url=ctx.current_base_url,
  79. current_model=ctx.current_model, user_providers=ctx.user_providers,
  80. custom_providers=ctx.custom_providers, force_fresh_nous_tier=force_fresh_nous_tier,
  81. max_models=max_models, refresh=refresh, probe_custom_providers=probe_custom_providers,
  82. probe_current_custom_provider=probe_current_custom_provider, for_picker=for_picker,
  83. excluded_providers=ctx.excluded_providers or [],
  84. non_blocking_catalogs=non_blocking_catalogs, fast_custom_probe=fast_custom_probe,
  85. )
  86. # Managed local runtime: staged GGUFs are selectable like any provider's models, but
  87. # list_authenticated_providers can't know about them (no credential — reachability is the
  88. # credential), so inject the row here where every picker surface inherits it.
  89. local_row = _local_runtime_row(ctx)
  90. if local_row is not None:
  91. rows = _without_slug(rows, "llamacpp") + [local_row]
  92. # A live session on the managed server reports provider "custom" (raw base_url label), which
  93. # would materialize a duplicate "Custom endpoint" row with the same staged models stealing the
  94. # checkmark. The Local row owns the managed server's identity — drop such custom rows.
  95. if local_row.get("is_current"):
  96. staged = set(local_row["models"])
  97. def _is_managed_custom(row: dict) -> bool:
  98. models = {str(m) for m in (row.get("models") or [])}
  99. return _slug(row) == "custom" and bool(models) and models <= staged
  100. rows = [r for r in rows if not _is_managed_custom(r)]
  101. moa_row = _moa_provider_row(ctx.current_provider)
  102. if moa_row is not None:
  103. rows = [moa_row] + _without_slug(rows, "moa")
  104. if explicit_only:
  105. rows = _filter_explicit_provider_rows(rows, ctx)
  106. # If the current provider lost its credential, list_authenticated_providers() omits it; keep
  107. # that one row so the UI shows the saved selection + a re-auth affordance instead of appearing
  108. # to jump providers. Exception: a "custom" current on the managed local server is already
  109. # represented by the Local row — the skeleton would resurrect the duplicate removed above.
  110. _local_owns_current = bool(local_row and local_row.get("is_current")
  111. and (ctx.current_provider or "").lower() == "custom")
  112. if not _local_owns_current:
  113. rows = list(rows) + _append_unconfigured_rows(rows, ctx, current_only=True)
  114. # A local proxy serving a model also in an aggregator's catalog would show under both, and picking
  115. # the aggregator row silently breaks the call — aggregators only list models no specific provider has.
  116. _strip_aggregator_overlaps(rows)
  117. if include_unconfigured:
  118. rows = list(rows) + _without_slug(_append_unconfigured_rows(rows, ctx), "moa")
  119. if picker_hints:
  120. _apply_picker_hints(rows)
  121. if canonical_order:
  122. rows = _reorder_canonical(rows)
  123. if pricing:
  124. _apply_pricing(rows, force_fresh_nous_tier=force_fresh_nous_tier, cached_only=pricing_cache_only)
  125. # Both metadata decorators consult ``model_overrides``. Snapshot the
  126. # read-only config once for this payload rather than letting each model
  127. # lookup reopen config.yaml through models_dev._cfg_get().
  128. metadata_config = None
  129. if capabilities or featured:
  130. try:
  131. from hermes_cli.config import load_config_readonly
  132. metadata_config = load_config_readonly()
  133. except Exception:
  134. metadata_config = None
  135. if capabilities:
  136. _apply_capabilities(rows, metadata_config=metadata_config)
  137. if featured:
  138. _apply_featured(rows, metadata_config=metadata_config)
  139. _apply_custom_aliases(rows)
  140. from hermes_cli.models_validate import drop_unofferable_model_ids
  141. drop_unofferable_model_ids(rows)
  142. return {"providers": rows, "model": ctx.current_model, "provider": ctx.current_provider}
  143. def _strip_aggregator_overlaps(rows: list[dict]) -> None:
  144. """Drop models from TRUE routing aggregators (OpenRouter, custom:* proxies) that a user-defined
  145. provider also serves. The is_user_defined guard matters: is_routing_aggregator() is True for every
  146. custom:* slug, so without it the dedup would empty a user's own custom row. Flat-namespace
  147. resellers (opencode-go/zen) serve every model first-party and keep shared names."""
  148. try:
  149. from hermes_cli.providers import is_routing_aggregator
  150. except Exception:
  151. return
  152. builtin_aggregators = {
  153. _slug(row) for row in rows
  154. if not row.get("is_user_defined") and is_routing_aggregator(_slug(row))
  155. }
  156. def _duplicates_builtin_aggregator(row: dict) -> bool:
  157. # A user row that IS the same upstream as a built-in aggregator (registered OpenRouter via
  158. # Settings → Providers, or a ``custom:openrouter`` slug) is that aggregator's twin, not a
  159. # rival: its catalog is a superset of the built-in row's, so counting it empties the
  160. # built-in row (openrouter → total=0 beside a live custom:openrouter row).
  161. row_slug = _slug(row)
  162. slug_suffix = (
  163. row_slug.split(":", 1)[1] if row_slug.startswith("custom:") else ""
  164. )
  165. if slug_suffix and slug_suffix in builtin_aggregators:
  166. return True
  167. from agent.model_metadata import _infer_provider_from_url
  168. inferred = _infer_provider_from_url(str(row.get("api_url") or ""))
  169. return inferred is not None and inferred in builtin_aggregators
  170. user_models: set[str] = set()
  171. for row in rows:
  172. if row.get("is_user_defined"):
  173. if builtin_aggregators and _duplicates_builtin_aggregator(row):
  174. continue # the twin IS that aggregator; it must not retro-strip it
  175. user_models.update(m.lower() for m in (row.get("models") or []))
  176. if not user_models:
  177. return
  178. for row in rows:
  179. if row.get("is_user_defined") or not is_routing_aggregator(row.get("slug", "")):
  180. continue
  181. # Only strip overlaps from TRUE routing aggregators (OpenRouter, custom:* proxies). Flat-namespace
  182. # resellers (opencode-go / opencode-zen) serve every listed model as a first-party model, so their
  183. # rows must keep models that a user's proxy happens to share a name with — otherwise a subscription
  184. # provider's own catalog (minimax-m3, glm-5, deepseek-v4-flash, ...) is silently gutted in the
  185. # picker. (#47077)
  186. original = row.get("models") or []
  187. filtered = [m for m in original if m.lower() not in user_models]
  188. if len(filtered) < len(original):
  189. row["models"] = filtered
  190. row["total_models"] = len(filtered)
  191. def build_model_options_payload(
  192. ctx: ConfigContext, *, explicit_only: bool = False, include_unconfigured: bool = False,
  193. refresh: bool = False,
  194. ) -> dict:
  195. """Shared API-server/dashboard/TUI payload. Normal open probes only the current custom provider so
  196. offline saved endpoints don't block the picker; explicit refresh probes all and busts the cache.
  197. A normal open (``refresh=False``) is a READ path: provider catalogs come from the disk cache
  198. only and stale/missing ones warm in the background, so a degraded provider (hanging endpoint,
  199. failed auth probe) delays neither the other providers' rows nor the response (#114215).
  200. ``for_picker=True`` keeps providers whose credential pool is entirely rate-limited visible:
  201. these are human-facing pickers, and hiding a temporarily exhausted pool makes providers vanish
  202. mid-session even though another model under the same provider may still work (same contract
  203. as ``/model`` and the aux pickers, #66584 / #66624). Visibility only: ``fast_custom_probe=False``
  204. keeps the live probe of the current custom endpoint on its full 5s discovery budget."""
  205. refresh = bool(refresh)
  206. payload = build_models_payload(
  207. ctx, explicit_only=bool(explicit_only), include_unconfigured=bool(include_unconfigured),
  208. picker_hints=True, canonical_order=True, pricing=True, pricing_cache_only=not refresh,
  209. capabilities=True, featured=True, for_picker=True, fast_custom_probe=False,
  210. refresh=refresh, probe_custom_providers=refresh, probe_current_custom_provider=not refresh,
  211. non_blocking_catalogs=not refresh,
  212. )
  213. _apply_limits(payload["providers"])
  214. _apply_usage(payload["providers"])
  215. if not refresh:
  216. _prewarm_pricing_async(payload["providers"], current_provider=ctx.current_provider,
  217. current_base_url=ctx.current_base_url)
  218. return payload
  219. def _apply_limits(rows: list[dict]) -> None:
  220. """Attach ``limit`` to rows whose credential pool is rate-limited, so a picker can say why and until
  221. when instead of the row just looking broken. Only providers with a persisted pool are read (no
  222. seeding), and a pool that fails to load says nothing rather than failing the whole catalog."""
  223. import logging
  224. from datetime import datetime, timezone
  225. from agent.credential_pool import load_pool
  226. from hermes_cli.auth import read_credential_pool
  227. def iso(epoch: float) -> str:
  228. return datetime.fromtimestamp(epoch, UTC).isoformat()
  229. pooled = {slug for slug, entries in read_credential_pool().items() if entries}
  230. for row in rows:
  231. slug = str(row.get("slug") or "")
  232. # Window plans say their limits in plan_usage (gauges + limit line); a credential cooldown chip
  233. # beside them would show a second, unrelated reset time.
  234. if slug not in pooled or row.get("is_user_defined") or row.get("plan_usage"):
  235. continue
  236. try:
  237. state = load_pool(slug).limit_state(row.get("models") or [])
  238. except Exception: # an unreadable pool must not fail the whole catalog
  239. logging.getLogger(__name__).debug("Pool limit read failed for %s", slug, exc_info=True)
  240. continue
  241. if state is None:
  242. continue
  243. if state["scope"] == "account":
  244. row["limit"] = {"scope": "account", "resets_at": iso(state["resets_at"])}
  245. else:
  246. row["limit"] = {"scope": "models", "models": {m: iso(at) for m, at in state["models"].items()}}
  247. def _apply_usage(rows: list[dict]) -> None:
  248. """Attach ``usage`` (subscription windows: % spent + reset) to signed-in rows that can report it,
  249. from the cache only, and ask for a background refresh, so the picker never waits on a usage API
  250. and a chip can warn before the wall instead of at it.
  251. Single-account providers keep the legacy gauge (``usage.windows``). A provider with a
  252. MULTI-ENTRY credential pool reports ``usage.accounts`` instead — one row per account, each with
  253. its own windows/state/resets_at — because one account's quota says nothing about its siblings;
  254. a provider-wide percentage there would be a fabricated average. A snapshot older than the
  255. staleness bound renders ``unknown``/``unavailable``, never a confident stale gauge. Nothing here
  256. mutates the pool or disables routing: telemetry never benches a credential."""
  257. from agent.account_usage_cache import (
  258. cached_account_usage, has_account_usage, refresh_account_usage_async,
  259. refresh_account_usage_entries_async, snapshot_is_stale,
  260. )
  261. from hermes_cli.auth import read_credential_pool
  262. def _wire_windows(snapshot) -> list[dict]:
  263. return [
  264. {"label": w.label, "used_percent": float(w.used_percent),
  265. "resets_at": w.reset_at.isoformat() if w.reset_at else None,
  266. "scope": w.scope}
  267. for w in (snapshot.windows if snapshot else ()) if w.used_percent is not None
  268. ]
  269. def _account_resets_at(snapshot, live_cooldown_until: float | None) -> str | None:
  270. """A quota-exhausted account recovers at the LATEST of its exhausted account-scoped
  271. windows (all of them must reopen before the account is whole again); a live
  272. credential-wide cooldown wins when later. Model-scoped windows never reset an account.
  273. Unknown stays None — the frontend renders its own advisory (e.g. the earliest sibling)."""
  274. resets: list[str] = []
  275. if live_cooldown_until:
  276. resets.append(_iso_from_epoch(live_cooldown_until))
  277. for window in (snapshot.windows if snapshot else ()):
  278. if window.scope != "account" or window.used_percent is None or window.reset_at is None:
  279. continue
  280. if float(window.used_percent) >= _EXHAUSTED_WINDOW_PERCENT:
  281. resets.append(window.reset_at.isoformat())
  282. return max(resets) if resets else None
  283. wanted: list[str] = []
  284. entry_requests: list[dict] = []
  285. pooled_sizes = {slug: len(entries) for slug, entries in read_credential_pool().items() if entries}
  286. for row in rows:
  287. slug = str(row.get("slug") or "")
  288. if not slug or row.get("is_user_defined") or row.get("authenticated") is False:
  289. continue
  290. supports_usage = has_account_usage(slug)
  291. # A single-entry pool (or no pool) is the single-account case: the legacy gauge stays
  292. # (chip contract unchanged). Only a genuinely multi-entry pool gets per-account rows —
  293. # there, a provider-wide percentage would be a fabricated average across logins.
  294. if pooled_sizes.get(slug, 0) < 2:
  295. if not supports_usage:
  296. continue
  297. wanted.append(slug)
  298. snapshot = cached_account_usage(slug)
  299. windows = _wire_windows(snapshot)
  300. if windows and not snapshot_is_stale(snapshot):
  301. row["usage"] = {"windows": windows}
  302. continue
  303. if supports_usage:
  304. wanted.append(slug)
  305. accounts = _pool_usage_accounts(slug, _wire_windows, _account_resets_at, entry_requests)
  306. if accounts is not None:
  307. row["usage"] = {"accounts": accounts}
  308. refresh_account_usage_async(wanted)
  309. refresh_account_usage_entries_async(entry_requests)
  310. # A window this spent counts as exhausted when computing an account's recovery time.
  311. _EXHAUSTED_WINDOW_PERCENT = 100.0
  312. def _iso_from_epoch(epoch: float) -> str:
  313. from datetime import datetime, timezone
  314. return datetime.fromtimestamp(float(epoch), UTC).isoformat()
  315. def _pool_usage_accounts(slug: str, wire_windows, account_resets_at,
  316. entry_requests: list[dict]) -> Optional[list[dict]]:
  317. """Per-account usage rows for one pooled provider, or None when the pool cannot be read.
  318. Read-only: entries are inspected via ``load_pool(slug)`` properties (no select/rotate/refresh).
  319. Dedupe is scoped to provider+host+trusted decoded account id where supported (several
  320. credentials of one Codex account are ONE account), else per-credential token identity — never
  321. a provider-wide singleton. Providers without a usage fetcher still get one metadata row per
  322. account with state unknown/limited/unavailable, so the chip can count honestly without bogus
  323. fetches. DEAD auth rows stay visible as ``unavailable`` (never a quota row). No secrets, raw
  324. payloads or base URLs ride the wire."""
  325. import logging
  326. import time
  327. from agent.account_usage_cache import _identity_id_for, cached_account_usage, has_account_usage, snapshot_is_stale
  328. from agent.credential_pool import STATUS_DEAD, _exhausted_until, load_pool
  329. try:
  330. pool = load_pool(slug)
  331. entries = list(pool.entries())
  332. except Exception: # an unreadable pool must not fail the whole catalog
  333. logging.getLogger(__name__).debug("Pool usage read failed for %s", slug, exc_info=True)
  334. return None
  335. if not entries:
  336. return None
  337. live = [e for e in entries if e.last_status != STATUS_DEAD]
  338. sole = len(live) <= 1
  339. supports_usage = has_account_usage(slug)
  340. now = time.time()
  341. accounts: list[dict] = []
  342. seen: dict[tuple[str, str, str], dict] = {}
  343. for entry in entries:
  344. identity_id = _identity_id_for(slug, entry)
  345. host = _pool_entry_host(slug, entry)
  346. dedupe_key = (slug, host, identity_id)
  347. is_dead = entry.last_status == STATUS_DEAD
  348. snapshot = (cached_account_usage(slug, identity_id=identity_id)
  349. if supports_usage and not is_dead else None)
  350. stale = snapshot_is_stale(snapshot)
  351. windows = wire_windows(snapshot) if not stale else []
  352. # 'limited' only when a live credential-wide cooldown benches this account or its
  353. # account-scoped quota windows are exhausted — telemetry alone never benches anything.
  354. cooldown_until = None if is_dead else _exhausted_until(entry, sole_credential=sole)
  355. live_cooldown = cooldown_until if cooldown_until and cooldown_until > now else None
  356. quota_exhausted = bool(windows) and any(
  357. w.get("scope") == "account" and w.get("used_percent") is not None
  358. and float(w["used_percent"]) >= _EXHAUSTED_WINDOW_PERCENT for w in windows)
  359. if is_dead:
  360. state = "unavailable"
  361. elif live_cooldown is not None or quota_exhausted:
  362. state = "limited"
  363. elif snapshot is None or stale or not windows:
  364. # No numeric live windows (failed/empty fetch, stale snapshot, non-supporting
  365. # provider): unknown — including a "ready-looking" account with nothing to show.
  366. state = "unknown"
  367. else:
  368. state = "ready"
  369. account_row = {
  370. "id": identity_id, "label": str(entry.label or ""), "windows": windows,
  371. "state": state, "resets_at": account_resets_at(snapshot, live_cooldown),
  372. }
  373. if dedupe_key in seen:
  374. # Same account under a second credential: keep the row that says more (state rank
  375. # limited > ready > unknown > unavailable), but never duplicate the account.
  376. existing = seen[dedupe_key]
  377. rank = {"limited": 3, "ready": 2, "unknown": 1, "unavailable": 0}
  378. if rank.get(account_row["state"], 0) > rank.get(existing["state"], 0):
  379. accounts[accounts.index(existing)] = account_row
  380. seen[dedupe_key] = account_row
  381. continue
  382. seen[dedupe_key] = account_row
  383. accounts.append(account_row)
  384. if supports_usage and not is_dead:
  385. entry_requests.append({
  386. "provider": slug, "identity_id": identity_id,
  387. "base_url": _pool_entry_route_base_url(slug, entry), "api_key": entry.runtime_api_key,
  388. })
  389. return accounts or None
  390. def _pool_entry_host(slug: str, entry) -> str:
  391. """Normalized route host of one entry, for account dedupe scoping. Never serialized."""
  392. from agent.credential_pool import _norm_url
  393. if slug == "openai-codex":
  394. from hermes_cli.auth_codex import _codex_pool_route_base_url
  395. return _norm_url(_codex_pool_route_base_url(entry.runtime_base_url))
  396. return _norm_url(getattr(entry, "runtime_base_url", None))
  397. def _pool_entry_route_base_url(slug: str, entry) -> Optional[str]:
  398. """The base URL a usage fetch for *entry* must target (its own route, never a sibling's)."""
  399. if slug == "openai-codex":
  400. from hermes_cli.auth_codex import _codex_pool_route_base_url
  401. return _codex_pool_route_base_url(entry.runtime_base_url)
  402. return getattr(entry, "runtime_base_url", None) or None
  403. # ─── Public: auxiliary-task pickers ─────────────────────────────────────
  404. def build_aux_picker_rows(
  405. *, current_provider: str = "", current_model: str = "", current_base_url: str = "",
  406. max_models: int | None = None,
  407. ) -> list[dict]:
  408. """Provider rows for any auxiliary-task picker (vision, compression, …). Honours
  409. ``excluded_providers``; exhausted-pool providers stay visible (``for_picker``); only the active
  410. custom endpoint is probed. ``moa`` is excluded: auxiliary_client unwraps it to its aggregator
  411. anyway, so offering it would be a choice silently rewritten.
  412. Aux pickers kept re-deriving their own kwargs and each one silently dropped a different slice of the
  413. user's configuration. Two independent contributor PRs landed against the same two call sites for exactly
  414. this: 52642 (user ``providers:`` / ``custom_providers:`` entries never appeared) and #66624 (providers
  415. with an exhausted credential pool were hidden). Both were per-site kwarg patches, so the next aux picker
  416. would have reintroduced the same gap. Routing through one function makes the correct behaviour the
  417. default that a new caller cannot forget:
  418. """
  419. ctx = load_picker_context().with_overrides(
  420. current_provider=current_provider, current_model=current_model, current_base_url=current_base_url,
  421. )
  422. rows = build_models_payload(
  423. ctx, for_picker=True, probe_custom_providers=False, probe_current_custom_provider=True,
  424. max_models=max_models,
  425. )["providers"]
  426. return _without_slug(rows, "moa")
  427. def format_aux_picker_entries(
  428. rows: list[dict], *, current_provider: str = "", current_base_url: str = "",
  429. ) -> list[tuple[str, str, list[str]]]:
  430. """Render aux-picker rows as ``(slug, label, models)``. A raw ``base_url`` custom endpoint is
  431. "current" only through that URL, never a slug — so with ``current_base_url`` set no row is marked."""
  432. entries: list[tuple[str, str, list[str]]] = []
  433. current_slug = str(current_provider or "").strip().lower()
  434. has_base_url = bool(str(current_base_url or "").strip())
  435. for row in rows:
  436. slug = str(row.get("slug") or "")
  437. name = row.get("name") or slug
  438. total = row.get("total_models") or len(row.get("models") or [])
  439. model_hint = f" — {total} models" if total else ""
  440. marker = " ← current" if slug.lower() == current_slug and current_slug and not has_base_url else ""
  441. entries.append((slug, f"{name}{model_hint}{marker}", list(row.get("models") or [])))
  442. return entries
  443. def _reasoning_catalog_reader(slug: str):
  444. """Per-model reasoning-capability reader for aggregators that publish one. Cache-only — the picker
  445. must never block on HTTP; a cold cache warms in the background and reports no restriction until then."""
  446. try:
  447. from hermes_cli.models_reasoning_caps import (
  448. nous_model_reasoning_capabilities,
  449. openrouter_model_reasoning_capabilities,
  450. warm_nous_reasoning_caps_async,
  451. warm_openrouter_reasoning_caps_async,
  452. )
  453. except Exception:
  454. return None
  455. readers = {
  456. "nous": (warm_nous_reasoning_caps_async, nous_model_reasoning_capabilities),
  457. "openrouter": (warm_openrouter_reasoning_caps_async, openrouter_model_reasoning_capabilities),
  458. }
  459. if slug not in readers:
  460. return None
  461. warm, read = readers[slug]
  462. warm()
  463. return read
  464. def _apply_capabilities(rows: list[dict], *, metadata_config: dict | None = None) -> None:
  465. """Attach ``{model: {fast, reasoning, ...}}`` per row. ``reasoning`` defaults True when the catalog is
  466. silent (the dial is a no-op on models that ignore it; hiding it from a capable model is worse). A
  467. serving aggregator's detail overrides models.dev (adds ``can_disable_reasoning``). ``supported_efforts``
  468. is deliberately NOT forwarded — it under-reports levels that work."""
  469. from agent.codex_service_tiers import daybreak_offered, ultrafast_offered
  470. from hermes_cli.models import model_supports_ultrafast, resolve_fast_mode_overrides
  471. try:
  472. from agent.models_dev import get_model_capabilities
  473. except Exception:
  474. get_model_capabilities = None # type: ignore[assignment]
  475. # Daybreak rides the Responses request; the opt-in codex app-server runtime never sends it.
  476. _model_cfg = (metadata_config or {}).get("model")
  477. _app_server = isinstance(_model_cfg, dict) and str(
  478. _model_cfg.get("openai_runtime") or "").strip().lower() == "codex_app_server"
  479. for row in rows:
  480. slug = row.get("slug") or ""
  481. caps: dict[str, dict[str, Any]] = {}
  482. read_reasoning_catalog = _reasoning_catalog_reader(slug.lower())
  483. for model in row.get("models") or []:
  484. reasoning = True
  485. if get_model_capabilities is not None and slug:
  486. try:
  487. meta = get_model_capabilities(slug, model, config=metadata_config)
  488. if meta is not None and meta.supports_reasoning is not None:
  489. reasoning = meta.supports_reasoning
  490. except Exception:
  491. reasoning = True
  492. fast = resolve_fast_mode_overrides(
  493. model, provider=slug, base_url=row.get("api_url")) is not None
  494. entry: dict[str, Any] = {"fast": fast, "reasoning": reasoning}
  495. if fast and model_supports_ultrafast(model) and ultrafast_offered(
  496. model, provider=slug, base_url=row.get("api_url")):
  497. entry["ultrafast"] = True
  498. if not _app_server and daybreak_offered(model, provider=slug, base_url=row.get("api_url")):
  499. entry["daybreak"] = True
  500. if reasoning and read_reasoning_catalog is not None:
  501. try:
  502. detail = read_reasoning_catalog(model)
  503. except Exception:
  504. detail = None
  505. if detail and not detail.get("supports_reasoning"):
  506. # Aggregator catalog beats models.dev for a route it serves: no reasoning param
  507. # means no reasoning controls, so no disable to describe either.
  508. entry["reasoning"] = False
  509. elif detail:
  510. entry["can_disable_reasoning"] = not detail.get("mandatory")
  511. caps[model] = entry
  512. row["capabilities"] = caps
  513. # Newest N models per lab an aggregator row features by default (older tail behind search/show-all);
  514. # 5 keeps a lab's headliners without letting a prolific vendor flood the view.
  515. _FEATURED_PER_LAB = 5
  516. def _apply_featured(rows: list[dict], *, metadata_config: dict | None = None) -> None:
  517. """Attach a ``featured_models`` shortlist to each routing-aggregator row: newest
  518. ``_FEATURED_PER_LAB`` per vendor by models.dev ``release_date`` (ranked within the row, never vs.
  519. today, so it is stable); ties keep curated order. Non-aggregators — including every user-defined
  520. row, whose ``models:`` list is an explicit allow-list — get an empty list and keep top-N
  521. behaviour (#120217)."""
  522. try:
  523. from agent.models_dev import get_model_info
  524. except Exception:
  525. get_model_info = None # type: ignore[assignment]
  526. # "Is this row an aggregator?" is answered canonically by is_routing_aggregator() — the same
  527. # predicate _strip_aggregator_overlaps() uses. Deriving it from model-id spelling (does any id
  528. # contain "/" and span >= 2 prefixes?) misread every Org/Model-shaped user provider as a
  529. # multi-lab aggregator and hid the models its owner configured by hand (#120217).
  530. try:
  531. from hermes_cli.providers import is_routing_aggregator
  532. except Exception:
  533. is_routing_aggregator = None # type: ignore[assignment]
  534. for row in rows:
  535. slug = str(row.get("slug") or "").strip().lower()
  536. models = row.get("models") or []
  537. if row.get("is_user_defined") or not (is_routing_aggregator and is_routing_aggregator(slug)):
  538. row["featured_models"] = []
  539. continue
  540. by_lab: dict[str, list[tuple[int, str, str]]] = {} # only multi-lab aggregators get a shortlist
  541. for pos, model in enumerate(models):
  542. lab = model.split("/", 1)[0] if "/" in model else ""
  543. if not lab: # no vendor prefix → single-namespace provider, not an aggregator
  544. by_lab = {}
  545. break
  546. date = ""
  547. if get_model_info is not None:
  548. info = (get_model_info(slug, model, config=metadata_config)
  549. or get_model_info("openrouter", model, config=metadata_config))
  550. date = getattr(info, "release_date", "") if info else ""
  551. by_lab.setdefault(lab, []).append((pos, date, model))
  552. if len(by_lab) < 2:
  553. row["featured_models"] = []
  554. continue
  555. featured: list[str] = []
  556. for entries in by_lab.values():
  557. # Newest release_date first; earlier list position breaks ties (sole key when undated).
  558. ranked = sorted(entries, key=lambda e: (e[1], -e[0]), reverse=True)
  559. featured.extend(model for _pos, _date, model in ranked[:_FEATURED_PER_LAB])
  560. order = {m: i for i, m in enumerate(models)} # keep the row's model order for stable rendering
  561. row["featured_models"] = sorted(featured, key=lambda m: order[m])
  562. def _apply_custom_aliases(rows: list[dict]) -> None:
  563. """Attach the accepted identity set to each user-defined row: ``model.options`` reports the canonical
  564. ``custom:<key>`` while rows carry the bare key as ``slug``, so GUI exact-match never finds the row.
  565. GUI pickers compare the two to decide which row is active; exact equality never matches for custom
  566. providers (#87035). Exposing ``aliases`` — every current and legacy spelling from
  567. :func:`hermes_cli.providers.custom_provider_aliases` — lets the frontend do a membership check instead.
  568. """
  569. from hermes_cli.providers import custom_provider_aliases
  570. for row in rows:
  571. if not row.get("is_user_defined"):
  572. continue
  573. try:
  574. row["aliases"] = sorted(
  575. custom_provider_aliases(str(row.get("name", "")), str(row.get("slug", ""))))
  576. except Exception:
  577. continue
  578. # ─── Internal: row post-processing ──────────────────────────────────────
  579. def _provider_auth_hint(slug: str) -> tuple[str, str]:
  580. """``(auth_type, key_env)`` for a canonical provider (``("api_key", "")`` when unregistered)."""
  581. from hermes_cli.auth import PROVIDER_REGISTRY
  582. cfg = PROVIDER_REGISTRY.get(slug)
  583. auth_type = cfg.auth_type if cfg else "api_key"
  584. key_env = cfg.api_key_env_vars[0] if (cfg and cfg.api_key_env_vars) else ""
  585. return auth_type, key_env
  586. def _row(slug: str, name: str, is_current: bool, **extra: Any) -> dict:
  587. return {"slug": slug, "name": name, "is_current": is_current, "is_user_defined": False, **extra}
  588. def _canonical_row(entry, cur: str, **extra: Any) -> dict:
  589. from hermes_cli.models import _PROVIDER_LABELS
  590. return _row(entry.slug, _PROVIDER_LABELS.get(entry.slug, entry.label), entry.slug.lower() == cur, **extra)
  591. def _append_unconfigured_rows(
  592. rows: list[dict], ctx: ConfigContext, *, current_only: bool = False,
  593. ) -> list[dict]:
  594. """Empty setup skeletons for canonical providers missing from ``rows`` — except the *current* one:
  595. if config.yaml still points at it but credentials are gone, keep a row carrying the saved model so
  596. GUI pickers don't silently snap to another provider."""
  597. from hermes_cli.models import _PROVIDER_ALIASES, _model_requires_account_discovery
  598. from hermes_cli.models_catalog_static import listed_canonical_providers
  599. seen = {r["slug"].lower() for r in rows}
  600. cur = (ctx.current_provider or "").lower()
  601. cur_model = str(ctx.current_model or "").strip()
  602. # Honor ``model_catalog.excluded_providers`` like ``list_authenticated_providers`` does — unconditionally,
  603. # even for the current provider — or ``include_unconfigured`` pickers (TUI ``/model``) resurrect every
  604. # excluded provider as a skeleton row (#68816). A slug is hidden when it or any alias is excluded, so an
  605. # alias exclusion (``google`` → ``gemini``) can't leak the canonical row back.
  606. excluded = {str(p).strip().lower() for p in (ctx.excluded_providers or []) if p}
  607. names_for: dict[str, set[str]] = {}
  608. for alias, canon in _PROVIDER_ALIASES.items():
  609. names_for.setdefault(canon.lower(), {canon.lower()}).add(alias.lower())
  610. extras: list[dict] = []
  611. for entry in listed_canonical_providers():
  612. slug = entry.slug.lower()
  613. if slug in seen:
  614. continue
  615. if names_for.get(slug, {slug}) & excluded:
  616. continue
  617. if current_only and entry.slug.lower() != cur:
  618. continue
  619. if entry.slug.lower() == cur:
  620. saved_model = "" if _model_requires_account_discovery(entry.slug, cur_model) else cur_model
  621. auth_type, key_env = _provider_auth_hint(entry.slug)
  622. tail = (
  623. "Astra requires successful account-scoped model discovery."
  624. if cur_model and not saved_model else "Showing the saved model only."
  625. )
  626. warning = (
  627. f"Configured provider missing usable credentials; paste {key_env} to reactivate. {tail}"
  628. if auth_type == "api_key" and key_env
  629. else f"Configured provider is not authenticated; run `hermes model` to reactivate. {tail}"
  630. )
  631. extras.append(_canonical_row(
  632. entry, cur, models=[saved_model] if saved_model else [], total_models=1 if saved_model else 0,
  633. source="configured-current", authenticated=False, auth_type=auth_type, key_env=key_env,
  634. warning=warning,
  635. ))
  636. continue
  637. extras.append(_canonical_row(entry, cur, models=[], total_models=0, source="canonical"))
  638. return extras
  639. def _anthropic_oauth_credentials_present() -> bool:
  640. """True when the user explicitly authenticated Anthropic via OAuth (Hermes device flow or Claude Code
  641. login) — those leave no trace in active_provider / model.provider / API-key env vars."""
  642. try:
  643. from agent.anthropic_credentials import read_claude_code_credentials, read_hermes_oauth_credentials
  644. readers = (read_hermes_oauth_credentials, read_claude_code_credentials)
  645. if any((read() or {}).get("accessToken") for read in readers):
  646. return True
  647. except Exception:
  648. return False
  649. # Pool-only OAuth entries (auth.json credential_pool.anthropic) are equally deliberate — discovery
  650. # accepts them via pool.has_credentials(), so the filter must too or those rows are built then
  651. # silently dropped. Read-only (no load_pool) so a picker open never mutates auth.json.
  652. try:
  653. from agent.credential_pool import AUTH_TYPE_OAUTH
  654. from hermes_cli.auth import read_credential_pool
  655. for entry in read_credential_pool("anthropic"):
  656. if (isinstance(entry, dict) and entry.get("auth_type") == AUTH_TYPE_OAUTH
  657. and str(entry.get("access_token") or "").strip()):
  658. return True
  659. except Exception:
  660. pass
  661. return False
  662. def _filter_explicit_provider_rows(rows: list[dict], ctx: ConfigContext) -> list[dict]:
  663. """Keep only rows backed by explicit user configuration — ``list_authenticated_providers`` also
  664. discovers ambient credentials (e.g. another CLI's login token) Desktop chat pickers must not show."""
  665. from hermes_cli.auth import is_provider_explicitly_configured
  666. current_slug = str(ctx.current_provider or "").strip().lower()
  667. def _is_explicit(row: dict, slug: str) -> bool:
  668. # Managed local models are explicit configuration by existence (gigabytes downloaded into the
  669. # machine-scoped dir); there is deliberately no config credential, so without the source clause
  670. # the row would only survive on the profile where Use was last clicked.
  671. if (row.get("is_user_defined") or (current_slug and slug == current_slug)
  672. or row.get("source") == "local-runtime"):
  673. return True
  674. if slug == "moa":
  675. # Virtual routing mode, not a configured provider: hide unless current (above) or the user
  676. # wrote an enabled preset into RAW config (the DEFAULT_CONFIG preset must not show MoA).
  677. return _raw_config_has_enabled_moa_preset()
  678. return (
  679. # Anthropic OAuth (device flow / Claude Code) and external-process CLIs (ACP agents) are
  680. # deliberate sign-ins that leave no trace in config/env; keep the rows discovery accepted.
  681. (slug == "anthropic" and _anthropic_oauth_credentials_present())
  682. or _external_process_signed_in(slug)
  683. or is_provider_explicitly_configured(slug)
  684. )
  685. return [row for row in rows
  686. if (slug := str(row.get("slug", "")).strip().lower()) and _is_explicit(row, slug)]
  687. def _external_process_signed_in(slug: str) -> bool:
  688. """True when an external-process provider has verified CLI credentials."""
  689. try:
  690. from hermes_cli.auth import PROVIDER_REGISTRY, get_external_process_provider_status
  691. pconfig = PROVIDER_REGISTRY.get(slug)
  692. return bool(pconfig and pconfig.auth_type == "external_process"
  693. and get_external_process_provider_status(slug).get("auth_verified"))
  694. except Exception:
  695. return False
  696. def _raw_config_has_enabled_moa_preset() -> bool:
  697. """True when the user's RAW config enables MoA: ``load_config()`` merges the DEFAULT_CONFIG preset for
  698. everyone, which is not a user choice; visible once one enabled preset (or legacy flat config) is saved."""
  699. try:
  700. from hermes_cli.config import read_raw_config
  701. raw = read_raw_config()
  702. except Exception:
  703. return False
  704. moa = raw.get("moa") if isinstance(raw, dict) else None
  705. if not isinstance(moa, dict):
  706. return False
  707. presets = moa.get("presets")
  708. if isinstance(presets, dict):
  709. return any(
  710. not isinstance(preset, dict) or preset.get("enabled", True)
  711. for name, preset in presets.items() if str(name or "").strip()
  712. )
  713. legacy_keys = {"reference_models", "aggregator", "reference_temperature", "aggregator_temperature",
  714. "max_tokens", "reference_max_tokens", "fanout"}
  715. return any(key in moa for key in legacy_keys) and bool(moa.get("enabled", True))
  716. def _apply_picker_hints(rows: list[dict]) -> None:
  717. """Add ``authenticated``/``auth_type``/``key_env``/``warning`` per row."""
  718. for row in rows:
  719. if "authenticated" in row:
  720. continue
  721. # Skeleton rows (_append_unconfigured_rows) have empty `models` AND source="canonical".
  722. is_skeleton = row.get("source") == "canonical" and not row.get("models")
  723. row["authenticated"] = not is_skeleton
  724. if not is_skeleton or row.get("is_user_defined"):
  725. continue
  726. auth_type, key_env = _provider_auth_hint(row["slug"])
  727. row["auth_type"] = auth_type
  728. row["key_env"] = key_env
  729. row["warning"] = (f"paste {key_env} to activate" if auth_type == "api_key" and key_env
  730. else f"run `hermes model` to configure ({auth_type})")
  731. def _reorder_canonical(rows: list[dict]) -> list[dict]:
  732. """Canonical slugs in ``CANONICAL_PROVIDERS`` order, truly-custom rows last. Keys on slug membership,
  733. NOT ``is_user_defined`` — ``providers:`` config rows carry that flag even for canonical slugs."""
  734. from hermes_cli.models import CANONICAL_PROVIDERS
  735. order = {e.slug: i for i, e in enumerate(CANONICAL_PROVIDERS)}
  736. canon = sorted((r for r in rows if r["slug"] in order), key=lambda r: order[r["slug"]])
  737. extras = [r for r in rows if r["slug"] not in order]
  738. return canon + extras
  739. def _attach_nous_plan_usage(row: dict, *, warm: bool):
  740. """Stamp ``plan_usage`` from the portal snapshot (window plans only) and return the PickerUsage (or
  741. None). A cache-only open reads the last snapshot at any age: the 60s account cache has usually lapsed
  742. by the time someone opens the composer menu. Accounts without a window plan get nothing."""
  743. try:
  744. from hermes_cli.model_picker_usage import nous_picker_usage
  745. usage = nous_picker_usage(warm=warm, any_age=not warm)
  746. except Exception:
  747. logging.getLogger(__name__).debug("Nous plan usage unavailable", exc_info=True)
  748. return None
  749. if usage is not None:
  750. row["plan_usage"] = usage.to_payload()
  751. # Main's subscription-usage chip (`usage.windows`): the same gauge for a window
  752. # plan, so it warns near a limit in the existing slot. Once an allowance is full the picker's limit
  753. # line says it (with what happens next), so the chip would only repeat it; paused plans say so in
  754. # the plan line.
  755. if not usage.paused_reason and not row.get("usage") and not any(g.left_fraction <= 0.0 for g in usage.gauges):
  756. windows = [_plan_usage_window(g) for g in usage.gauges if g.resets_at_ms]
  757. if windows:
  758. row["usage"] = {"windows": windows}
  759. return usage
  760. def _plan_usage_window(g) -> dict:
  761. """One window-plan allowance in main's ``ProviderUsageWindow`` shape. The label is Hermes's row title
  762. (``Today``), like the other providers' English window labels."""
  763. from datetime import datetime
  764. from agent.usage_gauges import WindowGauge, gauge_title
  765. return {
  766. "label": gauge_title(WindowGauge(g.label, g.scope, g.title or g.label, 0)),
  767. "used_percent": round(g.used_fraction * 100.0, 2),
  768. "resets_at": datetime.fromtimestamp(g.resets_at_ms / 1000, UTC).isoformat(),
  769. "scope": "account" if g.scope == "all" else "model",
  770. }
  771. # health: allow CC -- usage-window plans: one branch attaches plan usage to the Nous row (built in _attach_nous_plan_usage); over target on main
  772. def _apply_pricing(rows: list[dict], *, force_fresh_nous_tier: bool = False, cached_only: bool = False) -> None:
  773. """Set ``row["pricing"] = {model_id: {input, output, cache | None, free}}``; for Nous also
  774. ``free_tier`` (account is free-tier) and ``unavailable_models`` (paid models a free user can't pick).
  775. ``cached_only`` never hits the network: unknown Nous entitlement fails closed (``free_tier_pending``,
  776. all models locked) and missing pricing is marked ``pricing_pending``."""
  777. from hermes_cli.models_pricing import (
  778. _format_price_per_mtok,
  779. compute_sale_discount,
  780. get_pricing_for_provider,
  781. )
  782. from hermes_cli.models import (
  783. check_nous_free_tier,
  784. get_cached_nous_free_tier,
  785. partition_nous_models_by_tier,
  786. )
  787. nous_free_tier: Optional[bool] = None # resolved once (cached in models.py for the TTL window)
  788. for row in rows:
  789. slug = str(row.get("slug", "")).lower()
  790. models = row.get("models") or []
  791. if not models:
  792. continue
  793. if row.get("free_tier_row"):
  794. # The free tier's one model has no Portal pricing and no entitlement to read: pricing
  795. # it would lock the only row a free-tier install can select.
  796. continue
  797. try:
  798. pricing_kwargs = {"cached_only": True} if cached_only else {}
  799. if slug.startswith("custom:"):
  800. pricing_kwargs["base_url"] = str(row.get("api_url") or "")
  801. raw_pricing = get_pricing_for_provider(slug, **pricing_kwargs) or {}
  802. except Exception:
  803. raw_pricing = {}
  804. cached_nous_tier: Optional[bool] = None
  805. plan_usage = _attach_nous_plan_usage(row, warm=not cached_only) if slug == "nous" else None
  806. if slug == "nous" and cached_only:
  807. cached_nous_tier = get_cached_nous_free_tier()
  808. if cached_nous_tier is None:
  809. # Entitlement unknown: stay nonblocking but fail closed until the prewarm has populated
  810. # both caches, else a free account could briefly select paid models on first open.
  811. row["free_tier_pending"] = True
  812. row["unavailable_models"] = list(models)
  813. if not row.get("warning"): # say why every model renders locked
  814. row["warning"] = ("Checking Nous plan entitlement… models unlock on the "
  815. "next picker open or refresh.")
  816. continue
  817. if not raw_pricing:
  818. if slug == "nous":
  819. row["free_tier"] = bool(cached_nous_tier)
  820. row["pricing_pending"] = True
  821. row["unavailable_models"] = list(models) if cached_nous_tier else []
  822. continue
  823. formatted: dict[str, dict] = {}
  824. for mid in models:
  825. p = raw_pricing.get(mid)
  826. if not p:
  827. continue
  828. inp_raw, out_raw = p.get("prompt", ""), p.get("completion", "")
  829. cache_raw = p.get("input_cache_read", "")
  830. inp = _format_price_per_mtok(inp_raw) if inp_raw != "" else ""
  831. out = _format_price_per_mtok(out_raw) if out_raw != "" else ""
  832. entry: dict = {
  833. "input": inp, "output": out,
  834. "cache": _format_price_per_mtok(cache_raw) if cache_raw else None,
  835. "free": inp == "free" and out in ("free", ""), # both input and output cost nothing
  836. }
  837. # Sale chrome is Nous Portal-only (other catalogs' nested pricing.original is ignored); free
  838. # models get flat -100% chrome, was_* only when the gateway served an original.
  839. if slug == "nous":
  840. sale = compute_sale_discount(inp_raw, out_raw, p.get("original"))
  841. if sale is not None:
  842. discount_percent, was_prompt_raw, was_out_raw = sale
  843. entry["discount_percent"] = discount_percent
  844. for key, was_raw in (("was_input", was_prompt_raw), ("was_output", was_out_raw)):
  845. if was_raw != "":
  846. entry[key] = _format_price_per_mtok(was_raw)
  847. formatted[mid] = entry
  848. if formatted:
  849. row["pricing"] = formatted
  850. if slug == "nous":
  851. try:
  852. if nous_free_tier is None:
  853. nous_free_tier = (cached_nous_tier if cached_only
  854. else check_nous_free_tier(force_fresh=force_fresh_nous_tier))
  855. row["free_tier"] = bool(nous_free_tier)
  856. row["unavailable_models"] = (
  857. partition_nous_models_by_tier(list(models), raw_pricing, free_tier=True)[1]
  858. if nous_free_tier else [])
  859. except Exception: # tier detection failed — fail open (no gating)
  860. row["free_tier"] = False
  861. row["unavailable_models"] = []
  862. if plan_usage is not None:
  863. from hermes_cli.model_picker_usage import usage_levels
  864. # The gateway's per-plan word per model (free / low / medium / top_tier).
  865. row["usage_levels"] = usage_levels(list(models), raw_pricing)
  866. # Top-tier marks from the gateway (it decides, not the client).
  867. row["plan_usage"] = plan_usage.with_pricing({m: raw_pricing.get(m) for m in models}).to_payload()
  868. # Window plans: the picker shows the usage word, not per-token prices.
  869. row.pop("pricing", None)
  870. def _local_runtime_row(ctx: ConfigContext) -> dict | None:
  871. """The ``llamacpp`` row from staged GGUFs (``None`` when none) — downloaded models must be selectable
  872. before the server runs (selection starts it via the runtime_provider seam). The row's id comes from
  873. the provider registry's own definition, never a local literal: a row the resolver can't resolve is
  874. the bug this row's offline-first contract depends on not having."""
  875. try:
  876. from hermes_cli.local_runtime.bootstrap import staged_model_ids
  877. from hermes_cli.providers import LLAMACPP_ALIASES, LLAMACPP_PROVIDER_ID
  878. staged = staged_model_ids()
  879. if not staged:
  880. return None
  881. current = (ctx.current_provider or "").strip().lower() in LLAMACPP_ALIASES
  882. if not current:
  883. # A LIVE session on the managed server reports provider "custom" with the managed base_url;
  884. # match on the endpoint so the session being chatted in still shows a selection.
  885. try:
  886. from hermes_cli.local_runtime.endpoint import _state_endpoint
  887. managed = _state_endpoint()
  888. current = bool(managed and (ctx.current_base_url or "").strip().rstrip("/")
  889. == managed["base_url"].rstrip("/"))
  890. except Exception:
  891. current = False
  892. # Bare "Local" user-facing (engine name is an implementation detail); authenticated = reachability.
  893. return _row(LLAMACPP_PROVIDER_ID, "Local", current, models=staged, total_models=len(staged),
  894. source="local-runtime", authenticated=True, auth_type="local", warning=None)
  895. except Exception:
  896. return None
  897. def _prewarm_pricing_async(
  898. rows: list[dict], *, current_provider: str = "", current_base_url: str = "",
  899. ) -> Optional[Thread]:
  900. """Warm picker pricing caches without delaying the current payload (one worker per
  901. profile + endpoint scope; a live worker is reused)."""
  902. from hermes_constants import hermes_home_key
  903. from hermes_cli.models_pricing import pricing_cache_scope
  904. slugs = {
  905. (
  906. str(row.get("slug") or "").lower(),
  907. str(row.get("api_url") or "") if str(row.get("slug") or "").lower().startswith("custom:") else "",
  908. )
  909. for row in rows if row.get("slug")
  910. }
  911. endpoint_scope = tuple(sorted(
  912. (slug, pricing_cache_scope(
  913. slug, base_url=base_url, current_provider=current_provider, current_base_url=current_base_url,
  914. ))
  915. for slug, base_url in slugs))
  916. prewarm_key = (hermes_home_key(), endpoint_scope)
  917. with _pricing_prewarm_lock:
  918. current = _pricing_prewarm_threads.get(prewarm_key)
  919. if current is not None and current.is_alive():
  920. return current
  921. # The worker mutates only private copies; the pricing helpers populate shared process caches.
  922. worker_rows = [{**row, "models": list(row.get("models") or [])} for row in rows]
  923. def _worker() -> None:
  924. try:
  925. _apply_pricing(worker_rows)
  926. finally:
  927. with _pricing_prewarm_lock:
  928. if _pricing_prewarm_threads.get(prewarm_key) is current_thread():
  929. _pricing_prewarm_threads.pop(prewarm_key, None)
  930. thread = Thread(target=copy_context().run, args=(_worker,),
  931. name="hermes-picker-pricing-prewarm", daemon=True)
  932. _pricing_prewarm_threads[prewarm_key] = thread
  933. thread.start()
  934. return thread
  935. def _moa_provider_row(current_provider: str = "") -> dict | None:
  936. """The virtual ``moa`` row shared by the CLI inventory and gateway picker; ``None`` without presets.
  937. Strictly opt-in (#63353): the row only appears when the user's raw config.yaml explicitly
  938. enables at least one MoA preset. The synthesized ``default`` preset from
  939. ``normalize_moa_config({})`` — which every user gets via DEFAULT_CONFIG defaults — must not
  940. be treated as a user choice."""
  941. if not _raw_config_has_enabled_moa_preset():
  942. return None
  943. try:
  944. from hermes_cli.config import load_config
  945. from hermes_cli.moa_config import normalize_moa_config
  946. cfg = normalize_moa_config(load_config().get("moa") or {})
  947. models = list(cfg.get("presets", {}).keys())
  948. if not models:
  949. return None
  950. return _row(
  951. "moa", "Mixture of Agents", (current_provider or "").lower() == "moa", models=models,
  952. total_models=len(models), source="virtual", authenticated=True, auth_type="virtual",
  953. warning="Aggregator is the acting model billed for the run; references only advise once per user turn by default.")
  954. except Exception:
  955. return None