cli_model_switch_mixin.py 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. """Model picker, /model switch application, runtime snapshot/restore, and codex runtime handling for the interactive CLI
  2. Mixin split out of ``cli.py``; bound onto ``HermesCLI`` via the MRO. cli.py-internal
  3. symbols are imported LAZILY inside each method (``from cli import ...``) — the mixin
  4. never imports ``cli`` at module load time (import cycle).
  5. Tests drive the switch paths with bare stubs lacking most mixin methods, so shared steps are
  6. module-level functions taking ``cli`` and siblings are called as ``HermesCLI.<name>(self, ...)``.
  7. """
  8. from __future__ import annotations
  9. import copy
  10. import logging
  11. import sys
  12. import threading
  13. from rich.markup import escape as _escape
  14. from agent.i18n import t
  15. from utils import base_url_host_matches
  16. from hermes_cli.cli_agent_setup_mixin import _retire_agent
  17. logger = logging.getLogger(__name__)
  18. # CLI-level fields describing the active model route; snapshotted before a switch / one-turn
  19. # override and restored wholesale on rollback. ``reasoning_config`` rides along because it is
  20. # resolved per model: a failed swap or `/model X --reasoning high --once` must not leave the
  21. # new model's effort behind with the old route.
  22. _RUNTIME_FIELDS = (
  23. "model", "provider", "requested_provider", "_explicit_api_key", "_explicit_base_url",
  24. "api_key", "base_url", "api_mode", "reasoning_config")
  25. def _runtime_fields(cli) -> dict:
  26. return {key: getattr(cli, key, None) for key in _RUNTIME_FIELDS}
  27. def _resolve_cli_reasoning(cli) -> None:
  28. """Re-resolve the CLI-level ``reasoning_config`` for ``cli.model`` through the shared chokepoint
  29. (per-model ``reasoning_overrides`` > global ``agent.reasoning_effort``). Startup resolves it once
  30. for the launch model; every path that moves ``cli.model`` must call this BEFORE the agent branch,
  31. because a lazily built agent inherits this field — an always-thinking model then goes out with
  32. the launch model's effort and 400s (#112921, #96012). ``agent.switch_model`` re-resolves its own
  33. copy for the live-agent path."""
  34. from cli import CLI_CONFIG
  35. from hermes_constants import resolve_reasoning_config
  36. # getattr: tests drive /new unbound on a SimpleNamespace without ``model`` (blank -> config default).
  37. cli.reasoning_config = resolve_reasoning_config(CLI_CONFIG, getattr(cli, "model", None) or "")
  38. cli._reasoning_user_set = False
  39. def stored_session_route(session_meta, *, current_model, current_provider):
  40. """The route a resumed session should run on, or ``None`` when the stored one is absent or
  41. already current. Returns ``(model, provider, base_url, api_mode, provider_changed)``; the
  42. canonical row reader is ``SessionDB.session_gateway_runtime`` (``model_config.gateway_runtime``,
  43. else the TUI's top-level keys). Bare ``custom`` is healed because the CLI resolve path
  44. hard-fails on it (the TUI gateway keeps it when a base_url exists)."""
  45. stored_model = str((session_meta or {}).get("model") or "").strip()
  46. if not stored_model:
  47. return None
  48. from hermes_state import SessionDB as _SessionDB
  49. runtime = _SessionDB.session_gateway_runtime(session_meta)
  50. base_url = runtime.get("base_url") or None
  51. api_mode = runtime.get("api_mode") or None
  52. stored_provider = runtime.get("provider") or None
  53. provider = _heal_bare_custom_provider(stored_provider, base_url=base_url, model=stored_model)
  54. if provider != stored_provider:
  55. # A healed entry resolves its own current endpoint. A route that didn't heal is gone with its
  56. # endpoint and wire, which must not ride on the ambient provider.
  57. base_url = None
  58. if provider is None:
  59. api_mode = None
  60. provider_changed = bool(provider) and provider != current_provider
  61. if stored_model == current_model and not provider_changed:
  62. return None
  63. from hermes_cli.runtime_provider import is_foreign_provider_endpoint
  64. if is_foreign_provider_endpoint(provider, base_url):
  65. # The endpoint and its wire belong to the provider this chat left; resolve the stored one's own.
  66. base_url = api_mode = None
  67. # A row's api_mode/base_url were written for whichever model the session last ran. Providers that
  68. # pick the wire per model (OpenCode Zen/Go, Nous, plugin profiles) re-derive both from the stored model, or a
  69. # resumed opencode-go session keeps a MiniMax-era anthropic_messages route for a chat_completions
  70. # model (#96066) — the CLI/oneshot twin of tui_gateway's _rederive_per_model_route.
  71. from hermes_cli.model_switch import model_derived_api_mode
  72. derived = model_derived_api_mode(provider or "", stored_model)
  73. if derived is not None:
  74. from hermes_cli.models import normalize_opencode_base_url
  75. api_mode = derived
  76. base_url = normalize_opencode_base_url(provider, api_mode, base_url) or None
  77. return stored_model, provider, base_url, api_mode, provider_changed
  78. def _heal_bare_custom_provider(provider, *, base_url, model):
  79. """Bare ``custom`` is a billing class, not a routable identity: persisting/restoring it makes a
  80. later resume hard-fail once the config default leaves the custom endpoint. Recover the durable
  81. ``custom:<name>`` menu key from the endpoint, else drop the provider (None)."""
  82. if str(provider or "").strip().lower() != "custom":
  83. return provider
  84. try:
  85. # Heal bare "custom" persisted by older builds / gateway turns: it's the resolved billing class, not
  86. # a routable identity. (Stricter than the TUI gateway's recovery, which keeps bare "custom" when a
  87. # base_url exists — the CLI's resolve path would hard-fail on it, #14676.)
  88. from hermes_cli.runtime_provider import canonical_custom_identity
  89. return canonical_custom_identity(base_url=base_url or None, model=model or None) or None
  90. except Exception:
  91. return None
  92. def _merge_preflight_warning(cli, result, custom_providers) -> None:
  93. """Fold the context-compression preflight warning into ``result`` (fail-soft)."""
  94. from cli import logger
  95. if cli.agent is None:
  96. return
  97. try:
  98. from hermes_cli.context_switch_guard import merge_preflight_compression_warning
  99. # Prefer the fresh inventory list (same source as switch_model / TUI); fall back
  100. # to the agent-init snapshot.
  101. merge_preflight_compression_warning(
  102. result,
  103. agent=cli.agent,
  104. messages=list(cli.conversation_history or []),
  105. custom_providers=custom_providers if custom_providers is not None
  106. else getattr(cli.agent, "_custom_providers", None),
  107. config_context_length=getattr(cli.agent, "_config_context_length", None))
  108. except Exception as exc:
  109. logger.debug("preflight-compression switch warning failed: %s", exc)
  110. def _print_switch_summary(cli, result, old_model, *, one_turn: bool, strict_context: bool) -> None:
  111. """Record the next-turn switch note and print the "Model switched" block.
  112. The note is prepended to the next user message (a mid-history system message would break
  113. providers and prompt caching). ``strict_context``: the typed /model path lets
  114. context-resolution errors propagate; the picker path swallows them.
  115. """
  116. from cli import _cprint
  117. from hermes_cli.model_switch import format_model_for_display, resolve_display_context_length
  118. _display_old = format_model_for_display(old_model)
  119. _display_new = format_model_for_display(result.new_model)
  120. cli._pending_model_switch_note = (
  121. f"[Note: model was just switched from {_display_old} to {_display_new} "
  122. f"via {result.provider_label or result.target_provider}. "
  123. f"{'This override applies to the next turn only. ' if one_turn else ''}"
  124. f"Adjust your self-identification accordingly.]")
  125. _cprint(f" {t('cli.model.switched', model=_display_new)}")
  126. _cprint(f" {t('gateway.model.provider_label', provider=result.provider_label or result.target_provider)}")
  127. if result.target_provider == "moa":
  128. # The preset name hides who pays: the aggregator runs every tool-loop step (#112359).
  129. from hermes_cli.moa_config import normalize_moa_config
  130. moa_cfg = cli.config.get("moa") if isinstance(cli.config, dict) else {}
  131. agg = normalize_moa_config(moa_cfg)["presets"].get(result.new_model, {}).get("aggregator") or {}
  132. if agg:
  133. _acting = f"{agg.get('provider')}:{agg.get('model')}"
  134. _cprint(f" {t('cli.model.acting_model', model=_acting)}")
  135. # Provider-aware context chain: Codex OAuth / Nous / profile live-catalog caps win over the raw
  136. # models.dev entry (gpt-5.5 is 1.05M on openai but 272K on Codex OAuth).
  137. mi = result.model_info
  138. agent = cli.agent
  139. try:
  140. ctx = resolve_display_context_length(
  141. result.new_model, result.target_provider,
  142. base_url=result.base_url or cli.base_url or "",
  143. api_key=result.api_key or cli.api_key or "", model_info=mi,
  144. config_context_length=getattr(agent, "_config_context_length", None) if agent else None,
  145. custom_providers=getattr(agent, "_custom_providers", None) if agent else None)
  146. except Exception:
  147. if strict_context:
  148. raise
  149. ctx = None
  150. if ctx:
  151. from agent.context_pin import context_window_suffix
  152. _cprint(f" {t('gateway.model.context_label', tokens=f'{ctx:,}')}" + context_window_suffix(
  153. ctx, getattr(agent, '_config_context_length', None) if agent else None, model=result.new_model,
  154. provider=result.target_provider, base_url=result.base_url or cli.base_url or ""))
  155. if mi:
  156. if mi.max_output:
  157. _cprint(f" {t('gateway.model.max_output_label', tokens=f'{mi.max_output:,}')}")
  158. _cprint(f" {t('gateway.model.capabilities_label', capabilities=mi.format_capabilities())}")
  159. cache_enabled = (
  160. (base_url_host_matches(result.base_url or "", "openrouter.ai") and "claude" in result.new_model.lower())
  161. or result.api_mode == "anthropic_messages")
  162. if cache_enabled:
  163. _cprint(f" {t('gateway.model.prompt_caching_enabled')}")
  164. if result.warning_message:
  165. _cprint(f" ⚠ {result.warning_message}")
  166. if getattr(result, "usage_notice", ""):
  167. _cprint(f" ⚠ {result.usage_notice}")
  168. def _switch_model_from(
  169. cli, raw_input, *, is_global, explicit_provider, user_providers, custom_providers):
  170. """``switch_model`` seeded with this CLI's live route."""
  171. from hermes_cli.model_switch import switch_model
  172. return switch_model(
  173. raw_input=raw_input, current_provider=cli.provider or "", current_model=cli.model or "",
  174. current_base_url=cli.base_url or "", current_api_key=cli.api_key or "", is_global=is_global,
  175. explicit_provider=explicit_provider, user_providers=user_providers,
  176. custom_providers=custom_providers)
  177. def _run_confirm_and_apply(cli, target, *args) -> None:
  178. """Run a confirm+apply sequence off the UI thread when the TUI is live.
  179. The expensive-model modal blocks its thread on a response queue (_prompt_text_input_modal);
  180. on the prompt_toolkit main thread that freezes rendering, so the modal never appears and the
  181. switch silently cancels after the 120s timeout.
  182. """
  183. if getattr(cli, "_app", None):
  184. threading.Thread(target=target, args=args, daemon=True).start()
  185. else:
  186. target(*args)
  187. def _one_turn_selection_context(cli):
  188. """Context the owner's large-context guard would see, for a ``/model --once`` that does not mutate.
  189. A live compressor measurement wins. Otherwise refresh ``session.stats``, then the session row's
  190. persisted usage anchor (a resume, before this process has run a turn).
  191. """
  192. from hermes_cli.model_selection_guards import SelectionContext, selection_context_for_agent
  193. agent = getattr(cli, "agent", None)
  194. if agent is not None and hasattr(agent, "_refresh_stats"):
  195. agent._refresh_stats()
  196. live = selection_context_for_agent(agent)
  197. if live is not None:
  198. return live
  199. from agent.usage_anchor import persisted_anchor_tokens
  200. db = getattr(cli, "_session_db", None)
  201. sid = getattr(cli, "session_id", None)
  202. if db is None or not sid or not callable(getattr(db, "get_messages_as_conversation", None)):
  203. return None
  204. try:
  205. messages = db.get_messages_as_conversation(sid)
  206. except Exception:
  207. logger.debug("one-turn selection context unavailable", exc_info=True)
  208. return None
  209. tokens = persisted_anchor_tokens(db, sid, messages)
  210. if not tokens:
  211. return None
  212. return SelectionContext(context_tokens=tokens, current_model=getattr(cli, "model", None) or None)
  213. def _ask_switch_anyway(cli, title: str, detail: str) -> bool:
  214. """The model-guard prompt (switch anyway once, or cancel): True only on an explicit yes."""
  215. choices = [
  216. ("once", t("cli.model.choice_switch_anyway"), t("cli.model.desc_switch_anyway")),
  217. ("cancel", t("cli.model.choice_cancel"), t("cli.model.desc_keep_current_model"))]
  218. raw = cli._prompt_text_input_modal(title=f"!!! {title} !!!", detail=detail, choices=choices, timeout=120)
  219. return cli._normalize_slash_confirm_choice(raw, choices) == "once"
  220. # `/model` usage block: (invocation form, description key). Forms are command syntax (never
  221. # translated); descriptions are padded to one column at render time.
  222. _MODEL_USAGE_ROWS = (
  223. ("/model <name>", "cli.model.usage_switch"),
  224. ("/model <name> --global", "cli.model.usage_switch_global"),
  225. ("/model <name> --once", "cli.model.usage_switch_once"),
  226. ("/model <name> --session", "cli.model.usage_switch_session"),
  227. ("/model <name> --provider <slug>", "cli.model.usage_switch_provider_model"),
  228. ("/model <name> --reasoning <level>", "cli.model.usage_switch_reasoning"),
  229. ("/model --provider <slug>", "cli.model.usage_switch_provider"),
  230. ("/model --refresh", "cli.model.usage_refresh"))
  231. _MODEL_USAGE_COL = 36
  232. def _configured_default_route() -> tuple[str, str]:
  233. """``(model, provider)`` config.yaml names NOW (``/model reset``'s target). A fresh read, not the
  234. startup snapshot: the dashboard or another surface may have changed the default since launch."""
  235. from hermes_cli.config import load_config_readonly
  236. model_cfg = (load_config_readonly() or {}).get("model")
  237. if not isinstance(model_cfg, dict):
  238. return (model_cfg.strip() if isinstance(model_cfg, str) else ""), ""
  239. provider = str(model_cfg.get("provider") or "").strip()
  240. return (str(model_cfg.get("default") or model_cfg.get("model") or "").strip(),
  241. "" if provider.lower() == "auto" else provider)
  242. def _picker_reasoning_rows() -> list[tuple[str, str]]:
  243. """``(value, label)`` rows for the picker's effort step: the canonical ladder, the off state,
  244. then a keep-current row (empty value = leave the effort alone)."""
  245. from hermes_constants import VALID_REASONING_EFFORTS
  246. rows = [(lvl, lvl) for lvl in VALID_REASONING_EFFORTS]
  247. rows.append(("none", t("cli.model.effort_none")))
  248. rows.append(("", t("cli.model.effort_keep_current")))
  249. return rows
  250. def _picker_offers_reasoning(provider_data: dict, model: str) -> bool:
  251. """False only when the inventory's capability map says the picked model has no reasoning
  252. control; unknown capabilities keep the step (a no-op dial beats hiding a real one)."""
  253. caps = (provider_data or {}).get("capabilities")
  254. entry = caps.get(model) if isinstance(caps, dict) else None
  255. return not (isinstance(entry, dict) and entry.get("reasoning") is False)
  256. def _apply_reasoning_after_switch(cli, effort: str, *, persist_global: bool, one_turn: bool = False) -> None:
  257. """Apply a ``--reasoning <level>`` that rode along with a model pick. Runs AFTER the swap: the
  258. agent's ``switch_model`` re-resolves ``reasoning_config`` from config.yaml, so an earlier write
  259. would be clobbered. Session-scoped unless the pick itself persists (``--global``).
  260. ``one_turn`` (``--once``): the level rides the one-turn admission (``model_override.reasoning``)
  261. and the restore snapshot puts the CLI's own level back. It is never a session pin."""
  262. from cli import CLI_CONFIG, _cprint, _parse_reasoning_config, save_config_value
  263. parsed = _parse_reasoning_config(effort)
  264. if parsed is None:
  265. return
  266. cli.reasoning_config = parsed
  267. if one_turn:
  268. from hermes_cli.gateway_agent import GatewayAgent
  269. pending = getattr(cli, "_pending_one_turn_model_restore", None)
  270. if isinstance(pending, dict):
  271. pending["one_turn_reasoning"] = effort # the agent chat rebuilds carries it too
  272. if isinstance(cli.agent, GatewayAgent):
  273. cli.agent.stage_turn_reasoning(parsed)
  274. elif cli.agent is not None:
  275. cli.agent.reasoning_config = parsed
  276. _cprint(f" {t('cli.model.reasoning_effort', effort=effort)}")
  277. return
  278. cli._reasoning_user_set = True
  279. if cli.agent is not None:
  280. # The gateway session's agent commits an explicit level even when it equals the inherited one.
  281. pin = getattr(cli.agent, "pin_reasoning", None)
  282. if pin is not None:
  283. pin(parsed)
  284. else:
  285. cli.agent.reasoning_config = parsed
  286. saved = persist_global and save_config_value("agent.reasoning_effort", effort)
  287. if saved:
  288. CLI_CONFIG.setdefault("agent", {})["reasoning_effort"] = effort
  289. _cprint(f" {t('cli.model.reasoning_effort_saved' if saved else 'cli.model.reasoning_effort', effort=effort)}")
  290. def _commit_model_switch(
  291. cli, result, *, persist_global: bool, one_turn: bool = False, picker: bool = False,
  292. reasoning_effort: str = "") -> None:
  293. """Stage + swap, print the summary, persist (session row unless --once; config on --global).
  294. ``picker``: tolerate context-resolution errors and label the config write "(--global)"; the
  295. typed path additionally records the one-turn restore snapshot. ``reasoning_effort`` (from
  296. ``--reasoning`` or the picker's effort step) is applied after the swap."""
  297. from cli import HermesCLI, _cprint
  298. old_model, old_provider = cli.model, getattr(cli, "provider", None)
  299. # A second ``--once`` before the turn restores to the route before the first one.
  300. snapshot = (getattr(cli, "_pending_one_turn_model_restore", None)
  301. or cli._snapshot_model_runtime()) if one_turn else None
  302. if snapshot is not None:
  303. # The level belongs to this --once pick only: a second pick without --reasoning drops it.
  304. # So does the alias it resolved through.
  305. snapshot = {key: value for key, value in snapshot.items()
  306. if key not in ("one_turn_reasoning", "one_turn_alias")}
  307. # ``--once`` must not session.mutate.
  308. cli._model_switch_one_turn = bool(one_turn)
  309. try:
  310. staged = cli._stage_and_swap_model(result, old_model)
  311. finally:
  312. cli._model_switch_one_turn = False
  313. if not staged:
  314. return
  315. # A TUI slash worker replays /model on its shadow CLI; the tui_gateway mirror counts the real switch.
  316. if not getattr(cli, "is_slash_worker", False):
  317. from hermes_cli.observability.shared_metrics_events import record_model_switch
  318. record_model_switch(
  319. from_provider=old_provider, to_provider=result.target_provider, surface="cli", from_model=old_model,
  320. session_id=getattr(getattr(cli, "agent", None), "session_id", None) or getattr(cli, "session_id", None))
  321. if not picker:
  322. cli._pending_one_turn_model_restore = snapshot
  323. if snapshot is not None and getattr(result, "resolved_via_alias", ""):
  324. snapshot["one_turn_alias"] = result.resolved_via_alias # the agent chat rebuilds carries it
  325. _print_switch_summary(cli, result, old_model, one_turn=one_turn, strict_context=not picker)
  326. if reasoning_effort:
  327. _apply_reasoning_after_switch(cli, reasoning_effort, persist_global=persist_global and not one_turn,
  328. one_turn=one_turn)
  329. if persist_global:
  330. from hermes_cli.model_switch import persist_model_selection
  331. persist_model_selection(result)
  332. _cprint(f" {t('cli.model.saved_global' if picker else 'cli.model.saved')}")
  333. elif one_turn:
  334. _cprint(f" {t('cli.model.next_turn_only')}")
  335. else:
  336. _cprint(f" {t('cli.model.session_only')}")
  337. # The row records what THIS session runs even on --global (else a later resume restores the
  338. # stale creation-time model); --once is restored after one turn and never touches the row.
  339. if not one_turn:
  340. HermesCLI._persist_model_switch_to_session(cli, result)
  341. def _show_model_picker(cli, ctx, force_refresh: bool) -> None:
  342. """``/model`` with no args: open the picker, or print usage when nothing is authed."""
  343. from cli import _cprint
  344. from hermes_cli.inventory import build_models_payload
  345. from hermes_cli.providers import get_label
  346. try:
  347. if ctx is None:
  348. raise RuntimeError("inventory context unavailable")
  349. providers = build_models_payload(
  350. ctx, probe_custom_providers=force_refresh,
  351. probe_current_custom_provider=not force_refresh,
  352. capabilities=True, # the effort step hides itself on reasoning-free routes
  353. # Keep providers whose pool is entirely in cooldown visible: limits are
  354. # per-model for many providers, so another model may work (#103829) —
  355. # same contract as the gateway picker (#66584) and aux pickers (#66624).
  356. for_picker=True,
  357. # Visibility only, not a faster probe: this call live-probes the current custom
  358. # endpoint, which historically enjoyed the full 5s discovery budget (#103843).
  359. fast_custom_probe=False,
  360. )["providers"]
  361. except Exception:
  362. providers = []
  363. if not providers:
  364. _cprint(f" {t('cli.model.no_authenticated_providers')}")
  365. _cprint("")
  366. for form, desc_key in _MODEL_USAGE_ROWS:
  367. _cprint(f" {form.ljust(_MODEL_USAGE_COL)} {t(desc_key)}")
  368. return
  369. cli._open_model_picker(
  370. providers, cli.model or "unknown", get_label(cli.provider) if cli.provider else "unknown",
  371. user_provs=ctx.user_providers if ctx is not None else None,
  372. custom_provs=ctx.custom_providers if ctx is not None else None)
  373. class CLIModelSwitchMixin:
  374. """Model picker, /model switch application, runtime snapshot/restore, and codex runtime handling for the interactive CLI"""
  375. def _normalize_model_for_provider(self, resolved_provider: str) -> bool:
  376. """Normalize provider-specific model IDs and routing."""
  377. from cli import _split_model_config_default
  378. current_model = str(self.model or "").strip()
  379. if isinstance(self.model, dict):
  380. current_model, _ = _split_model_config_default(self.model)
  381. changed = False
  382. def _adopt(canonical, notice) -> None:
  383. """Adopt ``canonical`` when it differs; ``notice(new)`` builds the warning text."""
  384. nonlocal current_model, changed
  385. if canonical and canonical != current_model:
  386. if not self._model_is_default:
  387. self._console_print(f"[yellow]⚠️ {notice(canonical)}[/]")
  388. self.model = canonical
  389. current_model = canonical
  390. changed = True
  391. def _adopt_with_mode(normalize, api_mode_of, notice) -> bool:
  392. """Provider families that also own the wire protocol: adopt id, then sync api_mode."""
  393. nonlocal changed
  394. try:
  395. _adopt(normalize(current_model), notice)
  396. resolved_mode = api_mode_of(current_model)
  397. if resolved_mode != self.api_mode:
  398. self.api_mode = resolved_mode
  399. changed = True
  400. except Exception:
  401. pass
  402. return changed
  403. try:
  404. from hermes_cli.model_normalize import (
  405. _AGGREGATOR_PROVIDERS, normalize_model_for_provider)
  406. if resolved_provider not in _AGGREGATOR_PROVIDERS:
  407. _adopt(
  408. normalize_model_for_provider(current_model, resolved_provider),
  409. lambda new: t("cli.model.normalized_model", old=current_model, new=new, provider=resolved_provider))
  410. except Exception:
  411. pass
  412. from providers import get_provider_profile
  413. profile = get_provider_profile(resolved_provider)
  414. try:
  415. native_id = profile.normalize_model_id(current_model, api_key=self.api_key or "") if profile else None
  416. except Exception: # a plugin hook failing must not block startup: generic repair continues
  417. logger.debug("normalize_model_id(%s) failed", resolved_provider, exc_info=True)
  418. native_id = None
  419. if native_id is not None: # a profile-owned id space also re-derives the wire for the new id
  420. from hermes_cli.model_switch import model_derived_api_mode
  421. return _adopt_with_mode(
  422. lambda m: native_id,
  423. lambda m: model_derived_api_mode(resolved_provider, m, self.api_key or "") or self.api_mode,
  424. lambda new: t("cli.model.normalized_model", old=current_model, new=new, provider=resolved_provider))
  425. from hermes_cli.models import opencode_provider_family
  426. if opencode_provider_family(resolved_provider) is not None:
  427. from hermes_cli.models import normalize_opencode_model_id, opencode_model_api_mode
  428. return _adopt_with_mode(
  429. lambda m: normalize_opencode_model_id(resolved_provider, m),
  430. lambda m: opencode_model_api_mode(resolved_provider, m),
  431. lambda new: t("cli.model.stripped_provider_prefix", old=current_model, new=new, provider=resolved_provider))
  432. if resolved_provider != "openai-codex":
  433. return changed
  434. # 1. Strip provider prefix ("openai/gpt-5.4" → "gpt-5.4")
  435. if "/" in current_model:
  436. slug = current_model.split("/", 1)[1]
  437. if not self._model_is_default:
  438. self._console_print(
  439. "[yellow]⚠️ " + _escape(t("cli.model.stripped_provider_prefix", old=current_model, new=slug, provider="OpenAI Codex")) + "[/]")
  440. self.model = slug
  441. current_model = slug
  442. changed = True
  443. # 2. Replace untouched default with a Codex model
  444. if self._model_is_default:
  445. from hermes_cli.codex_models import DEFAULT_CODEX_MODELS, get_codex_model_ids
  446. fallback_model = DEFAULT_CODEX_MODELS[0]
  447. try:
  448. # self.base_url is the route resolved with self.api_key (#121486).
  449. available = get_codex_model_ids(
  450. access_token=self.api_key if self.api_key else None, base_url=self.base_url or None)
  451. if available:
  452. fallback_model = available[0]
  453. except Exception:
  454. pass
  455. if current_model != fallback_model:
  456. self.model = fallback_model
  457. changed = True
  458. return changed
  459. def _persist_model_switch_to_session(self, result) -> None:
  460. """Persist a session-scoped /model switch to the session DB row.
  461. Writes the model column plus the route in both shapes readers use — nested
  462. ``gateway_runtime`` (CLI --resume) and top-level keys (TUI session.resume) — from one
  463. or-None dict so stale keys are DELETED (``_merge_model_config_json`` only deletes on
  464. explicit None) and the shapes never diverge.
  465. Writes the model column plus the runtime route so ``--resume`` (CLI, reads ``gateway_runtime``) and
  466. ``session.resume`` (TUI/desktop, reads top-level ``model_config`` keys via
  467. ``_stored_session_runtime_overrides``) both restore the switched provider instead of recombining the
  468. model with the ambient default (#79536). Mirrors the gateway's ``update_session_model()`` call.
  469. getattr: tests drive the switch paths with ``object.__new__`` stubs.
  470. """
  471. from cli import logger
  472. db = getattr(self, "_session_db", None)
  473. sid = getattr(self, "session_id", None)
  474. if not db or not sid:
  475. return
  476. route = {
  477. "provider": _heal_bare_custom_provider(
  478. result.target_provider, base_url=result.base_url, model=result.new_model,
  479. ) or None,
  480. # Both shapes use the same or-None discipline so stale keys from a previous switch are deleted
  481. # (not merely omitted) in BOTH the nested gateway_runtime dict (CLI reader) and the top-level
  482. # keys (TUI gateway reader). _merge_model_config_json only deletes on explicit None, so falsy
  483. # values must be converted, not filtered. Deriving the top-level from **route guarantees the two
  484. # shapes can never diverge — the asymmetry that caused the original stale-key bug (#85261
  485. # simplify-code review).
  486. "base_url": result.base_url or None,
  487. "api_mode": result.api_mode or None}
  488. try:
  489. db.update_session_model(sid, result.new_model)
  490. db.patch_session_model_config(sid, {"gateway_runtime": route, **route})
  491. except Exception:
  492. logger.debug("Failed to persist model switch to session DB", exc_info=True)
  493. def _restore_session_model(self, session_meta: dict, *, quiet: bool = False) -> None:
  494. """Restore model/provider from the session DB row on every resume path.
  495. Skips when no model is recorded or the CLI got an explicit ``-m`` (user intent wins).
  496. A different stored provider gets its credentials re-resolved — the ambient ``api_key``
  497. must not be sent to the session's endpoint; on failure the ambient credentials are kept
  498. so the session still opens (the first turn surfaces the auth error).
  499. """
  500. from cli import logger
  501. if not (session_meta or {}).get("model") or getattr(self, "_explicit_model_override", False):
  502. return
  503. route = stored_session_route(session_meta, current_model=self.model, current_provider=self.provider)
  504. if route is None:
  505. return
  506. stored_model, stored_provider, stored_base_url, stored_api_mode, provider_changed = route
  507. from hermes_cli.local_runtime.endpoint import LLAMACPP_ALIASES
  508. managed = str(stored_provider or "").strip().lower() in LLAMACPP_ALIASES
  509. self.model = stored_model
  510. if stored_provider:
  511. self.provider = stored_provider
  512. self.requested_provider = stored_provider
  513. if stored_base_url and not managed:
  514. self.base_url = stored_base_url
  515. if stored_api_mode:
  516. self.api_mode = stored_api_mode
  517. if managed and not (getattr(self, "_explicit_base_url", None) and not provider_changed):
  518. # The supervisor owns the live port: last boot's loopback URL (an ephemeral fallback when
  519. # 18434 was busy) must not pin the resume onto a dead endpoint. A launch-time --base-url
  520. # for this same provider is user intent and keeps winning.
  521. self._explicit_api_key = None
  522. self._explicit_base_url = None
  523. try:
  524. from hermes_cli.runtime_provider import resolve_runtime_provider
  525. resolved = resolve_runtime_provider(requested=stored_provider, target_model=self.model or None)
  526. if resolved.get("api_key"):
  527. self.api_key = resolved["api_key"]
  528. self._credential_pool = resolved.get("credential_pool")
  529. if resolved.get("base_url"):
  530. self.base_url = resolved["base_url"]
  531. if not stored_api_mode and resolved.get("api_mode"):
  532. self.api_mode = resolved["api_mode"]
  533. except Exception:
  534. if stored_base_url:
  535. self.base_url = stored_base_url
  536. logger.debug(
  537. "Credential re-resolution for resumed session provider "
  538. "%s failed; keeping ambient credentials",
  539. stored_provider, exc_info=True)
  540. elif provider_changed:
  541. # Launch-time explicit overrides belong to the AMBIENT provider and would poison
  542. # _ensure_runtime_credentials for the restored one. api_key is never persisted to
  543. # the session DB — runtime provider resolution owns credentials.
  544. self._explicit_api_key = None
  545. self._explicit_base_url = stored_base_url
  546. try:
  547. from hermes_cli.runtime_provider import resolve_runtime_provider
  548. resolved = resolve_runtime_provider(requested=stored_provider, target_model=self.model or None)
  549. if resolved.get("api_key"):
  550. self.api_key = resolved["api_key"]
  551. self._credential_pool = resolved.get("credential_pool")
  552. if not stored_base_url and resolved.get("base_url"):
  553. self.base_url = resolved["base_url"]
  554. if not stored_api_mode and resolved.get("api_mode"):
  555. self.api_mode = resolved["api_mode"]
  556. except Exception:
  557. logger.debug(
  558. "Credential re-resolution for resumed session provider "
  559. "%s failed; keeping ambient credentials",
  560. stored_provider, exc_info=True)
  561. _resolve_cli_reasoning(self)
  562. # Mid-chat /resume swaps the live agent; on startup --resume _init_agent picks up
  563. # self.model / self.provider / self.reasoning_config.
  564. if self.agent is not None:
  565. try:
  566. self.agent.switch_model(
  567. new_model=self.model, new_provider=self.provider, api_key=self.api_key or "",
  568. base_url=self.base_url or "", api_mode=self.api_mode or "")
  569. except Exception:
  570. logger.debug("In-place agent model swap on resume failed", exc_info=True)
  571. msg = t("cli.model.restored_from_session", model=stored_model)
  572. if stored_provider:
  573. msg += f" ({stored_provider})"
  574. if quiet:
  575. print(msg, file=sys.stderr)
  576. else:
  577. self._console_print(f"[dim]{_escape(msg)}[/dim]")
  578. def _open_model_picker(self, providers: list, current_model: str, current_provider: str, user_provs=None, custom_provs=None) -> None:
  579. """Open prompt_toolkit-native /model picker modal."""
  580. self._capture_modal_input_snapshot()
  581. self._model_picker_state = {
  582. "stage": "provider",
  583. "providers": providers,
  584. "selected": next((i for i, p in enumerate(providers) if p.get("is_current")), 0),
  585. "current_model": current_model,
  586. "current_provider": current_provider,
  587. "user_provs": user_provs,
  588. "custom_provs": custom_provs,
  589. "filter": ""}
  590. self._invalidate(min_interval=0.0)
  591. def _confirm_expensive_model_switch(self, result) -> bool:
  592. """Ask for explicit confirmation before applying costly model switches."""
  593. self._confirmed_switch_warning = None
  594. if not getattr(result, "success", False):
  595. return True
  596. try:
  597. from hermes_cli.model_selection_guards import (
  598. combined_selection_warning, selection_context_for_agent)
  599. primed = getattr(self, "_one_turn_selection_context", None)
  600. warning = combined_selection_warning(
  601. result.new_model, provider=result.target_provider,
  602. base_url=result.base_url or self.base_url or "",
  603. api_key=result.api_key or self.api_key or "", model_info=result.model_info,
  604. selection_context=primed if primed is not None else selection_context_for_agent(
  605. getattr(self, "agent", None)))
  606. except Exception:
  607. warning = None
  608. if warning is None:
  609. return True
  610. if not _ask_switch_anyway(self, warning.title, warning.message):
  611. return False
  612. self._confirmed_switch_warning = warning.message
  613. return True
  614. def _confirm_owner_model_switch(self, refusal) -> bool:
  615. """The gateway owner refused a guarded target (``confirmation_required``: cost, data policy,
  616. large context; Ink/Desktop/ACP show the same ``confirm_message``): ask in the same prompt as
  617. ``_confirm_expensive_model_switch``. Only a yes re-sends with the owner's token. The identical
  618. warning the user just confirmed for this switch is not asked twice."""
  619. from hermes_cli.gateway_mutations import confirmation_title
  620. message = refusal.get("confirm_message") or ""
  621. confirmed, self._confirmed_switch_warning = getattr(self, "_confirmed_switch_warning", None), None
  622. if message and message == confirmed:
  623. return True
  624. return _ask_switch_anyway(self, confirmation_title(refusal), message)
  625. def _confirm_and_apply_model_switch_result(
  626. self, result, persist_global: bool, custom_providers=None, reasoning_effort: str = "") -> None:
  627. from cli import _cprint
  628. try:
  629. if result.success and not self._confirm_expensive_model_switch(result):
  630. _cprint(f" {t('cli.model.switch_cancelled')}")
  631. return
  632. self._apply_model_switch_result(
  633. result, persist_global, custom_providers=custom_providers, reasoning_effort=reasoning_effort)
  634. except Exception as exc:
  635. _cprint(f" {t('cli.model.selection_failed', error=exc)}")
  636. def _close_model_picker(self) -> None:
  637. self._model_picker_state = None
  638. self._restore_modal_input_snapshot()
  639. self._invalidate(min_interval=0.0)
  640. def _snapshot_model_runtime(self) -> dict:
  641. """Capture current CLI and agent model runtime for one-turn restore."""
  642. agent = getattr(self, "agent", None)
  643. # ``reasoning_config`` is a mutable dict: deepcopy it so a later in-place edit cannot alias the snapshot.
  644. return {
  645. **_runtime_fields(self),
  646. "reasoning_config": copy.deepcopy(getattr(self, "reasoning_config", None)),
  647. "agent_primary_runtime": copy.deepcopy(
  648. getattr(agent, "_primary_runtime", None)
  649. ) if agent is not None else None}
  650. def _restore_model_runtime_snapshot(self, snapshot: dict | None) -> None:
  651. """Restore a model runtime captured before a one-turn override."""
  652. from cli import _cprint, logger
  653. if not snapshot:
  654. return
  655. one_turn = _runtime_fields(self)
  656. for key in _RUNTIME_FIELDS:
  657. if key in snapshot:
  658. setattr(self, key, snapshot.get(key))
  659. agent = getattr(self, "agent", None)
  660. if agent is None:
  661. return
  662. from hermes_cli.gateway_agent import GatewayAgent
  663. # A gateway agent's one-turn level only rode the admission: put it back locally, write nothing.
  664. restore_reasoning = (agent.restore_reasoning if isinstance(agent, GatewayAgent)
  665. else lambda config: setattr(agent, "reasoning_config", config))
  666. if "reasoning_config" in snapshot:
  667. restore_reasoning(snapshot["reasoning_config"])
  668. primary = snapshot.get("agent_primary_runtime")
  669. if primary and hasattr(agent, "_restore_primary_runtime"):
  670. try:
  671. agent._primary_runtime = copy.deepcopy(primary)
  672. agent._fallback_activated = True
  673. agent._rate_limited_until = 0
  674. if agent._restore_primary_runtime():
  675. return
  676. except Exception:
  677. logger.debug("CLI one-turn model restore via primary runtime failed", exc_info=True)
  678. if hasattr(agent, "switch_model"):
  679. try:
  680. # The classic CLI only builds a GatewayAgent (``_init_agent``): its ``once`` / ``restore`` are
  681. # named, so a rename fails loudly instead of turning the restore into a session mutate.
  682. restored = agent.switch_model(
  683. new_model=snapshot.get("model", ""), new_provider=snapshot.get("provider", ""),
  684. api_key=snapshot.get("api_key", ""), base_url=snapshot.get("base_url", ""),
  685. api_mode=snapshot.get("api_mode", ""),
  686. capabilities=snapshot.get("capabilities"), once=True, restore=True)
  687. except Exception as exc: # health: allow BLE001 -- runs in the turn's finally; reported below
  688. logger.warning("CLI one-turn model restore failed: %s", exc)
  689. restored = exc
  690. if restored is False or isinstance(restored, Exception):
  691. # The previous route did not come back. Stay on the one-turn model instead of
  692. # claiming the restore.
  693. for key, value in one_turn.items():
  694. setattr(self, key, value)
  695. notice = t("cli.model.switch_cancelled") if restored is False else t(
  696. "cli.model.switch_failed", model=snapshot.get("model", ""), error=restored, old_model=agent.model)
  697. _cprint(f" {notice}")
  698. return
  699. if "reasoning_config" in snapshot:
  700. restore_reasoning(snapshot["reasoning_config"])
  701. @staticmethod
  702. def _filter_model_picker_entries(entries: list, query: str) -> list:
  703. """Return (original_index, label) pairs matching ``query`` (case-insensitive subsequence;
  704. empty matches all). The ORIGINAL index keeps a filtered selection resolving to exactly one
  705. concrete model — filtering never introduces fuzzy *resolution*."""
  706. pairs = list(enumerate(entries))
  707. q = (query or "").strip().lower()
  708. if not q:
  709. return pairs
  710. def _subseq(needle: str, hay: str) -> bool:
  711. it = iter(hay)
  712. return all(ch in it for ch in needle)
  713. return [(i, e) for (i, e) in pairs if _subseq(q, str(e).lower())]
  714. @staticmethod
  715. def _compute_model_picker_viewport(
  716. selected: int, scroll_offset: int, n: int, term_rows: int, reserved_below: int = 6,
  717. panel_chrome: int = 6, min_visible: int = 3) -> tuple[int, int]:
  718. """Resolve (scroll_offset, visible) for the /model picker viewport. ``reserved_below``
  719. matches the approval/clarify panels (input, status bar, separators); ``panel_chrome`` is
  720. borders + blanks + hint row. The offset slides to keep ``selected`` on screen."""
  721. max_visible = max(min_visible, term_rows - reserved_below - panel_chrome)
  722. if n <= max_visible:
  723. return 0, n
  724. visible = max_visible
  725. if selected < scroll_offset:
  726. scroll_offset = selected
  727. elif selected >= scroll_offset + visible:
  728. scroll_offset = selected - visible + 1
  729. return max(0, min(scroll_offset, n - visible)), visible
  730. def _stage_and_swap_model(self, result, old_model) -> bool:
  731. """Stage ``result`` onto the CLI fields, then swap the live agent in place.
  732. CLI fields are snapshotted first so a failed agent swap rolls the whole CLI back —
  733. otherwise the staged broken credentials leak into the next turn even though the agent
  734. rolled back. Returns False after printing the failure (a failed switch is a no-op).
  735. """
  736. from cli import _cprint
  737. if self.agent is None and getattr(self, "_gateway_session_open", False) and not self._init_agent():
  738. # Before the first turn: the stand-in built for that turn reads the gateway session's model,
  739. # so the switch must commit on the session now; _init_agent printed why it could not.
  740. self._confirmed_switch_warning = None
  741. return False
  742. _cli_snapshot = _runtime_fields(self)
  743. self.model = result.new_model
  744. self.provider = result.target_provider
  745. self.requested_provider = result.target_provider
  746. # Always overwrite explicit overrides so stale credentials from the previous provider
  747. # (e.g. Ollama api_key/base_url) don't leak into the next resolution.
  748. self._explicit_api_key = result.api_key
  749. self._explicit_base_url = result.base_url
  750. if result.api_key:
  751. self.api_key = result.api_key
  752. if result.base_url:
  753. self.base_url = result.base_url
  754. if result.api_mode:
  755. self.api_mode = result.api_mode
  756. _resolve_cli_reasoning(self)
  757. if self.agent is not None:
  758. try:
  759. fields = dict(
  760. new_model=result.new_model, new_provider=result.target_provider,
  761. api_key=result.api_key, base_url=result.base_url, api_mode=result.api_mode,
  762. capabilities=getattr(result, "runtime_capabilities", None))
  763. if getattr(self, "_model_switch_one_turn", False):
  764. # The CLI and agent show the resolved model; the admission sends the alias.
  765. fields.update(once=True, alias=getattr(result, "resolved_via_alias", "") or "")
  766. if self.agent.switch_model(**fields) is False:
  767. # Declined at the gateway owner's guard prompt: nothing was committed.
  768. for _k, _v in _cli_snapshot.items():
  769. setattr(self, _k, _v)
  770. _cprint(f" {t('cli.model.switch_cancelled')}")
  771. return False
  772. except Exception as exc:
  773. # The agent rolled itself back to the old working model/client. Roll the CLI's own staged
  774. # fields back too and abort the rest of the commit (note + success print) so a failed switch
  775. # is a no-op rather than a dead session (#50163).
  776. # Agent rolled itself back; roll the CLI back too and abort so a failed switch is a no-op
  777. # rather than a dead session (#50163).
  778. for _k, _v in _cli_snapshot.items():
  779. setattr(self, _k, _v)
  780. if str(exc) == "launch_credentials_unavailable":
  781. # No model switch can fix a chat whose launch key changed; say what does.
  782. from hermes_cli.cli_chat_error_copy import refusal_text
  783. _cprint(f" {refusal_text(exc)}")
  784. return False
  785. _cprint(f" {t('cli.model.switch_failed', model=result.new_model, error=exc, old_model=old_model)}")
  786. return False
  787. finally:
  788. self._confirmed_switch_warning = None # this switch's confirmation only
  789. return True
  790. def _apply_model_switch_result(
  791. self, result, persist_global: bool, custom_providers=None, reasoning_effort: str = "") -> None:
  792. """Picker-path commit (see _commit_model_switch)."""
  793. from cli import _cprint
  794. if not result.success:
  795. _cprint(f" ✗ {result.error_message}")
  796. return
  797. _merge_preflight_warning(self, result, custom_providers)
  798. _commit_model_switch(self, result, persist_global=persist_global, picker=True,
  799. reasoning_effort=reasoning_effort)
  800. def _handle_model_picker_selection(self, persist_global: bool = False) -> None:
  801. state = self._model_picker_state
  802. if not state:
  803. return
  804. selected = state.get("selected", 0)
  805. stage = state.get("stage")
  806. if stage == "provider":
  807. providers = state.get("providers") or []
  808. if selected >= len(providers):
  809. self._close_model_picker()
  810. return
  811. provider_data = providers[selected]
  812. # Curated list (same as `hermes model` / gateway pickers); live catalog only when
  813. # it is empty (user-defined endpoints, per-resource providers such as azure-foundry).
  814. # Disk-cached like the gateway pickers: the live probe can walk several api-version
  815. # fallbacks with a 6 s timeout each, which must not block the REPL on every select.
  816. model_list = provider_data.get("models", [])
  817. if not model_list:
  818. try:
  819. from hermes_cli.models import cached_provider_model_ids
  820. model_list = cached_provider_model_ids(provider_data["slug"]) or model_list
  821. except Exception:
  822. pass
  823. from hermes_cli.models_validate import offered_model_ids
  824. model_list = offered_model_ids(
  825. model_list, provider_data.get("slug"), provider_data.get("api_url"))
  826. state.update(
  827. stage="model", provider_data=provider_data, model_list=model_list,
  828. selected=0, filter="", _filtered_pairs=None)
  829. self._invalidate(min_interval=0.0)
  830. return
  831. if stage == "model":
  832. provider_data = state.get("provider_data") or {}
  833. model_list = state.get("model_list") or []
  834. # Map the row through the active fuzzy filter; pairs carry the ORIGINAL index.
  835. filtered_pairs = state.get("_filtered_pairs")
  836. if filtered_pairs is None:
  837. filtered_pairs = list(enumerate(model_list))
  838. visible_labels = [e for (_i, e) in filtered_pairs]
  839. back_idx = len(visible_labels)
  840. if selected == back_idx:
  841. state.update(
  842. stage="provider", filter="", _filtered_pairs=None,
  843. selected=next((i for i, p in enumerate(state.get("providers") or [])
  844. if p.get("slug") == provider_data.get("slug")), 0))
  845. self._invalidate(min_interval=0.0)
  846. return
  847. if selected > back_idx: # cancel row (and anything past it)
  848. self._close_model_picker()
  849. return
  850. if 0 <= selected < back_idx:
  851. result = _switch_model_from(
  852. self, visible_labels[selected], is_global=persist_global,
  853. explicit_provider=provider_data.get("slug"),
  854. user_providers=state.get("user_provs"),
  855. custom_providers=state.get("custom_provs"))
  856. if result.success and _picker_offers_reasoning(provider_data, result.new_model):
  857. # Third step: effort for the picked model (skipped for routes the catalog
  858. # marks reasoning-free). Rows come from the canonical level set.
  859. state.update(stage="reasoning", switch_result=result, selected=0, _scroll_offset=0)
  860. self._invalidate(min_interval=0.0)
  861. return
  862. self._commit_picker_result(result, persist_global)
  863. return
  864. self._close_model_picker()
  865. if stage == "reasoning":
  866. rows = _picker_reasoning_rows()
  867. result = state.get("switch_result")
  868. if selected == len(rows): # ← Back to the model list
  869. state.update(stage="model", selected=0, _scroll_offset=0, switch_result=None)
  870. self._invalidate(min_interval=0.0)
  871. return
  872. if selected > len(rows) or result is None:
  873. self._close_model_picker()
  874. return
  875. self._commit_picker_result(result, persist_global, reasoning_effort=rows[selected][0])
  876. def _commit_picker_result(self, result, persist_global: bool, reasoning_effort: str = "") -> None:
  877. """Close the picker and run the confirm+apply sequence for ``result``."""
  878. state = self._model_picker_state or {}
  879. # Capture before close — picker state is cleared on close.
  880. _picker_custom_provs = state.get("custom_provs")
  881. self._close_model_picker()
  882. # The effort is appended only when picked: stubs/tests pin the historical arity.
  883. extra = (reasoning_effort,) if reasoning_effort else ()
  884. _run_confirm_and_apply(
  885. self, self._confirm_and_apply_model_switch_result,
  886. result, persist_global, _picker_custom_provs, *extra)
  887. def _handle_model_switch(self, cmd_original: str):
  888. """Handle /model command — switch model.
  889. Supports:
  890. /model — show current model + usage hints
  891. /model <name> — switch model (this session only)
  892. /model <name> --once — switch for the next turn only
  893. /model <name> --session — switch for this session only (explicit)
  894. /model <name> --global — switch and persist to config.yaml
  895. /model <name> --provider <provider> — switch provider + model
  896. /model <name> --reasoning <level> — switch and set reasoning effort in one step
  897. /model --provider <provider> — switch to provider, auto-detect model
  898. /model reset — back to config.yaml's model (keeps history)
  899. Switches are session-scoped unless ``model.persist_switch_by_default`` or ``--global``.
  900. """
  901. from cli import _cprint
  902. from hermes_cli.model_switch import parse_model_switch_args, resolve_persist_behavior
  903. parts = cmd_original.split(None, 1) # split off '/model'
  904. request = parse_model_switch_args(parts[1].strip() if len(parts) > 1 else "")
  905. if request.errors:
  906. # CLI decoration: " ✗ " prefix over the canonical error copy.
  907. _cprint(f" ✗ {request.error_messages()[0]}")
  908. return
  909. target, explicit_provider = request.target, request.explicit_provider
  910. if request.is_reset:
  911. # Session-scoped switch back to config.yaml's model; drops an armed --once restore,
  912. # which would otherwise re-plant the model being reset away from after the next turn.
  913. target, explicit_provider = _configured_default_route()
  914. if not target:
  915. _cprint(f" ✗ {t('cli.model.reset_no_default')}")
  916. return
  917. if target == self.model and explicit_provider in ("", self.provider):
  918. self._pending_one_turn_model_restore = None
  919. _cprint(f" {t('cli.model.reset_none', model=target)}")
  920. return
  921. one_turn = request.is_once
  922. persist_global = not request.is_reset and resolve_persist_behavior(
  923. request.is_global, request.is_session, is_once=one_turn,
  924. explicit_provider=request.explicit_provider)
  925. # --refresh: wipe the picker cache so every authed provider's /v1/models is re-fetched.
  926. if request.force_refresh:
  927. try:
  928. from hermes_cli.models import clear_provider_models_cache
  929. clear_provider_models_cache()
  930. _cprint(f" {t('cli.model.cleared_picker_cache')}")
  931. except Exception:
  932. pass
  933. # Live session state is overlaid truthy-only so empty self.* attrs don't clobber config.
  934. from hermes_cli.inventory import load_picker_context
  935. try:
  936. ctx = load_picker_context().with_overrides(
  937. current_provider=self.provider or "", current_model=self.model or "",
  938. current_base_url=self.base_url or "")
  939. except Exception:
  940. ctx = None
  941. # switch_model() + _open_model_picker still need the raw provider dicts.
  942. user_provs = ctx.user_providers if ctx is not None else None
  943. custom_provs = ctx.custom_providers if ctx is not None else None
  944. if not request.target and not request.explicit_provider:
  945. return _show_model_picker(self, ctx, request.force_refresh)
  946. result = _switch_model_from(
  947. self, target, is_global=persist_global, explicit_provider=explicit_provider,
  948. user_providers=user_provs, custom_providers=custom_provs)
  949. if not result.success:
  950. _cprint(f" ✗ {result.error_message}")
  951. return
  952. _merge_preflight_warning(self, result, custom_provs)
  953. extra = (request.reasoning_effort,) if request.reasoning_effort else ()
  954. _run_confirm_and_apply(
  955. self, self._confirm_and_apply_cli_model_switch,
  956. result, persist_global, one_turn, custom_provs, *extra)
  957. def _confirm_and_apply_cli_model_switch(
  958. self, result, persist_global: bool, one_turn: bool, custom_provs=None, reasoning_effort: str = "") -> None:
  959. """Confirm an expensive model switch and apply it (typed /model path). Runs on a worker
  960. thread when the TUI is active (see _run_confirm_and_apply) so the modal can render."""
  961. from cli import _cprint
  962. # ``--once`` does not reach the owner's mutate guard. A resumed session's compressor is
  963. # still cold; the persisted anchor is what that guard would have measured.
  964. self._one_turn_selection_context = _one_turn_selection_context(self) if one_turn else None
  965. try:
  966. if not self._confirm_expensive_model_switch(result):
  967. _cprint(f" {t('cli.model.switch_cancelled')}")
  968. return
  969. _commit_model_switch(self, result, persist_global=persist_global, one_turn=one_turn,
  970. reasoning_effort=reasoning_effort)
  971. finally:
  972. self._one_turn_selection_context = None
  973. def _handle_codex_runtime(self, cmd_original: str) -> None:
  974. """Handle /codex-runtime — toggle the codex app-server runtime opt-in.
  975. Usage:
  976. /codex-runtime — show current state
  977. /codex-runtime auto — Hermes default (chat_completions)
  978. /codex-runtime codex_app_server — hand turns to codex subprocess
  979. /codex-runtime on / off — synonyms for the above
  980. """
  981. from cli import _cprint
  982. from hermes_cli import codex_runtime_switch as crs
  983. parts = cmd_original.split(None, 1)
  984. new_value, errors = crs.parse_args(parts[1].strip() if len(parts) > 1 else "")
  985. if errors:
  986. for err in errors:
  987. _cprint(f"❌ {err}")
  988. return
  989. try:
  990. from hermes_cli.config import load_config, save_config
  991. except Exception as exc:
  992. _cprint(t("cli.model.could_not_load_config", error=exc))
  993. return
  994. result = crs.apply(
  995. load_config(), new_value,
  996. persist_callback=(save_config if new_value is not None else None))
  997. prefix = "✓" if result.success else "✗"
  998. for line in result.message.splitlines():
  999. _cprint(f" {prefix} {line}" if line.startswith("openai_runtime") else f" {line}")
  1000. if result.success and result.requires_new_session:
  1001. _cprint(f" {t('cli.model.tip_reset_new_session')}")
  1002. def _should_handle_model_command_inline(self, text: str, has_images: bool = False) -> bool:
  1003. """Return True when /model should be handled immediately on the UI thread."""
  1004. from cli import _looks_like_slash_command
  1005. if not text or has_images or not _looks_like_slash_command(text):
  1006. return False
  1007. try:
  1008. from hermes_cli.commands import resolve_command
  1009. cmd = resolve_command(text.split(None, 1)[0].lower().lstrip('/'))
  1010. return bool(cmd and cmd.name == "model")
  1011. except Exception:
  1012. return False
  1013. def _cmd_moa(self, cmd_original: str):
  1014. """/moa one-shot: run one prompt through the default MoA preset, then restore the prior
  1015. model (a session-long MoA switch goes through the picker's virtual MoA provider)."""
  1016. from cli import _cprint, _slash_args
  1017. from hermes_cli.moa_config import moa_usage, normalize_moa_config
  1018. payload = _slash_args(cmd_original)
  1019. if not payload:
  1020. _cprint(f" {moa_usage()}")
  1021. return True
  1022. moa_cfg = self.config.get("moa") if isinstance(self.config, dict) else {}
  1023. preset = normalize_moa_config(moa_cfg)["default_preset"]
  1024. self._pending_moa_restore_model = {
  1025. key: getattr(self, key, None)
  1026. for key in (
  1027. "requested_provider", "provider", "model", "api_key", "base_url", "api_mode")}
  1028. self.requested_provider = "moa"
  1029. self.provider = "moa"
  1030. self.model = preset
  1031. self.api_key = "moa-virtual-provider"
  1032. self.base_url = "moa://local"
  1033. self.api_mode = "chat_completions"
  1034. _retire_agent(self)
  1035. self._pending_moa_disable_after_turn = True
  1036. self._pending_agent_seed = payload
  1037. _cprint(f" {t('cli.model.moa_one_shot_queued', preset=preset)}")