slash_commands_model.py 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. """Gateway slash commands that switch or tune the model route:
  2. /model, /codex-runtime, /reasoning, /fast, /personality.
  3. Split out of ``gateway/slash_commands.py``; bound onto ``GatewayRunner`` through
  4. ``GatewaySlashCommandsMixin``. Origin internals are imported lazily inside the bodies to avoid
  5. the import cycle.
  6. """
  7. from __future__ import annotations
  8. import asyncio
  9. import contextlib
  10. import dataclasses
  11. import logging
  12. from typing import Any, Optional
  13. from agent.i18n import t
  14. from gateway.platforms.event import MessageEvent
  15. from hermes_cli.config import atomic_config_write
  16. from utils import base_url_host_matches
  17. logger = logging.getLogger("gateway.run") # log-record parity with gateway/run.py
  18. # /fast argument -> (service tier, persisted value, i18n label key; None = value.upper()).
  19. _FAST_SELECTIONS = {
  20. "fast": ("priority", "fast", "gateway.fast.label_fast"),
  21. "on": ("priority", "fast", "gateway.fast.label_fast"),
  22. "normal": (None, "normal", "gateway.fast.label_normal"),
  23. "off": (None, "normal", "gateway.fast.label_normal"),
  24. "auto": ("auto", "auto", None),
  25. "cold": ("cold", "cold", None),
  26. "ultrafast": ("ultrafast", "ultrafast", None),
  27. }
  28. def _fast_route_supports(model: str, runtime: dict, tier: Optional[str] = None) -> bool:
  29. """The turn's own gate (``run_turn.py::_resolve_turn_agent_config``): a tier /fast accepts must be
  30. one the session's next request actually carries, so proxies and other providers are refused."""
  31. from hermes_cli.models import resolve_fast_mode_overrides
  32. return resolve_fast_mode_overrides(
  33. model, provider=runtime.get("provider"), base_url=runtime.get("base_url"), tier=tier) is not None
  34. # /reasoning display-toggle arguments -> show_reasoning value.
  35. _REASONING_DISPLAY_TOGGLES = {"show": True, "on": True, "hide": False, "off": False}
  36. def _model_switch_skew_guard() -> Optional[str]:
  37. """Refuse a model switch when the gateway is running stale code: a first-time lazy import on
  38. a new code path can crash on a stale cached dependency. Scoped to the highest-risk trigger."""
  39. from gateway.code_skew import detect_code_skew
  40. skew = detect_code_skew()
  41. if not skew:
  42. return None
  43. boot_rev, disk_rev = skew
  44. return t(
  45. "gateway.model.error_prefix",
  46. error=t("gateway.model.err_skew", boot_rev=boot_rev, disk_rev=disk_rev),
  47. )
  48. async def _persist_model_switch_to_config(result, config_path) -> None:
  49. """Write-through a resolved /model switch to the profile config at ``config_path``, off the
  50. event loop (the route comparison can do cold-start disk I/O)."""
  51. from hermes_cli.model_switch import persist_model_selection
  52. await asyncio.to_thread(persist_model_selection, result, config_path)
  53. @dataclasses.dataclass
  54. class _ModelSwitchContext:
  55. """Everything a /model switch needs beyond the target: current route + persistence policy."""
  56. session_key: str
  57. source: Any
  58. config_path: Any
  59. persist_global: bool
  60. one_turn: bool = False
  61. reasoning_effort: str = "" # `--reasoning <level>` riding with the pick (typed path only)
  62. restore_snapshot: Optional[dict] = None
  63. current_model: str = ""
  64. current_provider: str = "openrouter"
  65. # The provider actually configured/overridden (None = unset): ``current_provider`` defaults to
  66. # openrouter for switch_model, which must not label the configured model in metrics.
  67. route_provider: Optional[str] = None
  68. current_base_url: str = ""
  69. current_api_key: str = ""
  70. user_provs: Any = None
  71. custom_provs: Any = None
  72. excluded_provs: list = dataclasses.field(default_factory=list)
  73. def read_config(self) -> None:
  74. """Fill the current route from ``config_path``; fail-open to the defaults."""
  75. from gateway.run import _load_gateway_config
  76. try:
  77. cfg = _load_gateway_config(config_path=self.config_path)
  78. if not cfg:
  79. return
  80. model_cfg = cfg.get("model", {})
  81. if isinstance(model_cfg, dict):
  82. self.current_model = model_cfg.get("default", "")
  83. self.current_provider = model_cfg.get("provider", self.current_provider)
  84. self.route_provider = model_cfg.get("provider")
  85. self.current_base_url = model_cfg.get("base_url", "")
  86. self.user_provs = cfg.get("providers")
  87. try:
  88. from hermes_cli.config import get_compatible_custom_providers
  89. self.custom_provs = get_compatible_custom_providers(cfg)
  90. except Exception:
  91. self.custom_provs = cfg.get("custom_providers")
  92. excl = cfg.get("model_catalog", {}).get("excluded_providers")
  93. if isinstance(excl, list):
  94. self.excluded_provs = excl
  95. except Exception:
  96. pass
  97. def apply_override(self, override: dict) -> None:
  98. """A session /model override supersedes the configured route."""
  99. if override:
  100. self.current_model = override.get("model", self.current_model)
  101. self.current_provider = override.get("provider", self.current_provider)
  102. self.route_provider = override.get("provider", self.route_provider)
  103. self.current_base_url = override.get("base_url", self.current_base_url)
  104. self.current_api_key = override.get("api_key", self.current_api_key)
  105. _TEXT_LISTING_MODELS = 5
  106. def _model_provider_listing_lines(providers) -> list[str]:
  107. """Text-list body for ``/model`` with no args on platforms without a picker."""
  108. lines: list[str] = []
  109. for p in providers:
  110. tag = t("gateway.model.current_tag") if p["is_current"] else ""
  111. lines.append(f"**{p['name']}** `--provider {p['slug']}`{tag}:")
  112. if p["models"]:
  113. shown = p["models"][:_TEXT_LISTING_MODELS] # uncapped rows arrive full; this is a preview
  114. model_strs = ", ".join(f"`{m}`" for m in shown)
  115. hidden = p["total_models"] - len(shown)
  116. extra = t("gateway.model.more_models_suffix", count=hidden) if hidden > 0 else ""
  117. lines.append(f" {model_strs}{extra}")
  118. elif p.get("api_url"):
  119. lines.append(f" `{p['api_url']}`")
  120. lines.append("")
  121. return lines
  122. class GatewayModelCommandsMixin:
  123. """Model-route slash commands (/model, /codex-runtime, /reasoning, /fast, /personality)."""
  124. # ----------------------------------------------------------------- /model
  125. async def _perform_model_switch(
  126. self, ctx: _ModelSwitchContext, raw_input: str, explicit_provider, source
  127. ):
  128. """Resolve a /model switch off-loop. Returns ``(result, None)`` or ``(None, error_text)``."""
  129. from gateway.run import _load_gateway_config
  130. from hermes_cli.model_switch import switch_model
  131. skew_error = _model_switch_skew_guard()
  132. if skew_error:
  133. return None, skew_error
  134. # Off-loop: switch_model() can hit a synchronous models.dev fetch (15s) on a cold cache.
  135. result = await asyncio.to_thread(
  136. switch_model, raw_input=raw_input, current_provider=ctx.current_provider,
  137. current_model=ctx.current_model, current_base_url=ctx.current_base_url,
  138. current_api_key=ctx.current_api_key, is_global=ctx.persist_global,
  139. explicit_provider=explicit_provider, user_providers=ctx.user_provs,
  140. custom_providers=ctx.custom_provs,
  141. )
  142. if not result.success:
  143. return None, t("gateway.model.error_prefix", error=result.error_message)
  144. try:
  145. from hermes_cli.context_switch_guard import enrich_model_switch_warnings_for_gateway
  146. # Off-loop: merge_preflight_compression_warning() runs the sync provider probe ladder.
  147. await asyncio.to_thread(
  148. enrich_model_switch_warnings_for_gateway, result, self, session_key=ctx.session_key,
  149. source=source, custom_providers=ctx.custom_provs,
  150. load_gateway_config=_load_gateway_config,
  151. )
  152. except Exception as exc:
  153. logger.debug("preflight-compression switch warning failed: %s", exc)
  154. return result, None
  155. def _switch_cached_agent_model(self, result, ctx: _ModelSwitchContext, picker: bool) -> Optional[str]:
  156. """In-place swap on the cached agent; returns the error reply when it failed.
  157. The agent rolls back to the OLD model/client and re-raises; the commit (DB, override,
  158. eviction, config) is aborted so the next message doesn't rebuild a broken agent.
  159. """
  160. cached_agent = self._cached_agent_for(ctx.session_key)
  161. if cached_agent is None:
  162. return None
  163. try:
  164. cached_agent.switch_model(
  165. new_model=result.new_model, new_provider=result.target_provider,
  166. api_key=result.api_key, base_url=result.base_url, api_mode=result.api_mode,
  167. capabilities=getattr(result, "runtime_capabilities", None),
  168. )
  169. except Exception as exc:
  170. logger.warning(
  171. "%s model switch failed for cached agent: %s", "Picker" if picker else "In-place", exc
  172. )
  173. return t(
  174. "gateway.model.error_prefix",
  175. error=t("gateway.model.err_switch_failed", model=result.new_model, error=exc, current=ctx.current_model),
  176. )
  177. return None
  178. async def _record_model_switch(
  179. self, result, ctx: _ModelSwitchContext, *, source, one_turn: bool, picker: bool
  180. ) -> Optional[str]:
  181. """Persist a committed switch: session DB, next-turn note, config write-through, override map.
  182. Returns the warning for a ``--global`` switch whose ``config.yaml`` write or stale-override
  183. cleanup failed (the switch then stays a session override), else ``None``.
  184. """
  185. from hermes_cli.model_switch import format_model_for_display
  186. # Persist the new model to the session DB so the dashboard shows the updated model (#34850).
  187. _sess_db = getattr(self, "_session_db", None)
  188. if _sess_db is not None: # so the dashboard shows the updated model
  189. try:
  190. _sess_entry = await self.async_session_store.get_or_create_session(source)
  191. # Typed path: consume the auto-reset flag so the next message's cleanup does not
  192. # wipe the override stored below.
  193. if not picker and getattr(_sess_entry, "was_auto_reset", False):
  194. # See #48031.
  195. _sess_entry.was_auto_reset = False
  196. await _sess_db.update_session_model(
  197. _sess_entry.session_id, result.new_model, provider=result.target_provider,
  198. base_url=result.base_url, api_mode=result.api_mode,
  199. )
  200. except Exception as exc:
  201. logger.debug("Failed to persist model switch to DB: %s", exc)
  202. # Prepended to the next user message (no system messages mid-history). Display form strips
  203. # opaque Palantir RID prefixes; the override map keeps the full ID for the wire.
  204. if not hasattr(self, "_pending_model_notes"):
  205. self._pending_model_notes = {}
  206. self._pending_model_notes[ctx.session_key] = (
  207. f"[Note: model was just switched from {format_model_for_display(ctx.current_model)} to "
  208. f"{format_model_for_display(result.new_model)} "
  209. f"via {result.provider_label or result.target_provider}. "
  210. f"{'This override applies to the next turn only. ' if one_turn else ''}"
  211. f"Adjust your self-identification accordingly.]"
  212. )
  213. self._session_model_overrides[ctx.session_key] = {
  214. "model": result.new_model, "provider": result.target_provider, "api_key": result.api_key,
  215. "base_url": result.base_url, "api_mode": result.api_mode,
  216. "request_overrides": dict(result.request_overrides or {}),
  217. "capabilities": dict(result.runtime_capabilities or {}),
  218. }
  219. if one_turn:
  220. # A repeated --once before the turn runs must keep the EARLIEST snapshot: the later
  221. # command's snapshot is the first temporary model, not the user's standing override.
  222. self._claim_one_turn_restore(ctx.session_key, ctx.restore_snapshot)
  223. elif not picker and hasattr(self, "_pending_one_turn_model_restores"):
  224. self._pending_one_turn_model_restores.pop(ctx.session_key, None)
  225. # A --global switch has ONE durable authority: config.yaml. Write it first; on success drop
  226. # the session override (memory + store) — a redundant copy would shadow every later global
  227. # change after a restart (#100314: a stale override resumed `gpt-5.6-sol-900k` as the base
  228. # 272K model). On failure keep the override so the switch truthfully survives as session-only.
  229. global_error: Optional[str] = None
  230. if ctx.persist_global:
  231. try:
  232. await _persist_model_switch_to_config(result, ctx.config_path)
  233. except Exception as e:
  234. logger.warning("Failed to persist model switch: %s", e)
  235. global_error = t("gateway.model.err_config_not_updated", error=str(e) or type(e).__name__)
  236. # Precedence is session > channel_overrides > config.yaml: in a chat with a channel_overrides
  237. # model/provider the session override must stay, or the next turn runs the channel model.
  238. if ctx.persist_global and global_error is None and self._channel_override_for(source) is None:
  239. try:
  240. await self.async_session_store.set_model_override(ctx.session_key, None)
  241. except Exception as e:
  242. # Store still holds the stale copy: keep memory in agreement and report it (#100314).
  243. logger.warning("Failed to clear persisted session model override: %s", e)
  244. global_error = t("gateway.model.err_stale_override", error=e)
  245. else:
  246. self._session_model_overrides.pop(ctx.session_key, None)
  247. # Non-secret write-through so the override survives a restart (api_key/api_mode are
  248. # re-resolved on rehydration); a --once override must NOT outlive a restart.
  249. # Write-through the non-secret parts (model/provider/base_url) to the session store so the override
  250. # survives a gateway restart. api_key/api_mode are never persisted — they are re-resolved via
  251. # runtime provider resolution on rehydration. /model --once is intentionally EXCLUDED from the
  252. # write-through: a one-turn override must never survive a restart. The persisted value stays at the
  253. # pre-once state (the prior session override, or nothing), which is exactly what the finally-restore
  254. # reverts the in-memory dict to. (#29923 review defect: the original implementation wrote through,
  255. # so a crash before the restore rehydrated the once-model permanently.)
  256. elif not one_turn:
  257. try:
  258. await self.async_session_store.set_model_override(
  259. ctx.session_key, self._session_model_overrides[ctx.session_key]
  260. )
  261. except Exception:
  262. logger.debug("Failed to persist session model override", exc_info=True)
  263. self._evict_cached_agent(ctx.session_key) # next turn builds fresh from the override
  264. return global_error
  265. async def _model_switch_confirmation(
  266. self, result, ctx: _ModelSwitchContext, *, one_turn: bool, picker: bool,
  267. global_error: Optional[str] = None,
  268. ) -> str:
  269. """Confirmation text with full metadata (display form shortens opaque Palantir IDs)."""
  270. from gateway.run import _load_gateway_config
  271. from hermes_cli.model_switch import (
  272. effective_context_pin, format_model_for_display, resolve_display_context_length_async)
  273. lines = [
  274. t("gateway.model.switched", model=format_model_for_display(result.new_model)),
  275. t("gateway.model.provider_label", provider=result.provider_label or result.target_provider),
  276. ]
  277. # Provider-aware chain: Codex OAuth, Nous and profile live-catalog caps win over the raw models.dev entry.
  278. mi = result.model_info
  279. model_cfg: dict = {}
  280. config_ctx = None
  281. with contextlib.suppress(Exception): # fail-open on config read errors
  282. model_cfg = _load_gateway_config().get("model", {})
  283. if isinstance(model_cfg, dict) and model_cfg.get("context_length") is not None:
  284. config_ctx = int(model_cfg["context_length"])
  285. if not isinstance(model_cfg, dict):
  286. model_cfg = {}
  287. base_url = result.base_url or ctx.current_base_url or ""
  288. # The pin applies only while the route matches the config; the number and its label use the same answer.
  289. config_ctx = effective_context_pin(
  290. config_ctx, result.new_model, result.target_provider, base_url,
  291. model_cfg.get("default") or model_cfg.get("model"), model_cfg.get("provider"), model_cfg.get("base_url"))
  292. ctx_len = await resolve_display_context_length_async(
  293. result.new_model, result.target_provider, base_url=base_url,
  294. api_key=result.api_key or ctx.current_api_key or "", model_info=mi,
  295. custom_providers=ctx.custom_provs, config_context_length=config_ctx,
  296. )
  297. if ctx_len:
  298. from agent.context_pin import context_window_suffix
  299. lines.append(t("gateway.model.context_label", tokens=f"{ctx_len:,}") + context_window_suffix(
  300. ctx_len, config_ctx, model=result.new_model, provider=result.target_provider, base_url=base_url))
  301. if mi and mi.max_output:
  302. lines.append(t("gateway.model.max_output_label", tokens=f"{mi.max_output:,}"))
  303. if mi:
  304. lines.append(t("gateway.model.capabilities_label", capabilities=mi.format_capabilities()))
  305. openrouter_claude = (
  306. base_url_host_matches(result.base_url or "", "openrouter.ai") and "claude" in result.new_model.lower()
  307. )
  308. if not picker and (openrouter_claude or result.api_mode == "anthropic_messages"):
  309. lines.append(t("gateway.model.prompt_caching_enabled"))
  310. if result.warning_message:
  311. lines.append(t("gateway.model.warning_prefix", warning=result.warning_message))
  312. if getattr(result, "usage_notice", ""):
  313. lines.append(f"⚠️ {result.usage_notice}")
  314. if ctx.persist_global and global_error is not None:
  315. # Never claim a clean global commit the disk did not take (#100314).
  316. lines.append(t("gateway.model.warning_prefix", warning=global_error))
  317. lines.append(t("gateway.model.session_only_hint"))
  318. elif ctx.persist_global:
  319. lines.append(t("gateway.model.saved_global"))
  320. elif one_turn:
  321. lines.append(t("gateway.model.next_turn_only"))
  322. else:
  323. lines.append(t("gateway.model.session_only_hint"))
  324. return "\n".join(lines)
  325. async def _commit_model_switch(
  326. self, result, ctx: _ModelSwitchContext, *, source, picker: bool = False
  327. ) -> str:
  328. """Apply a resolved switch (cached agent, session, config) and build the confirmation; shared
  329. by the typed path and the picker callback (``picker=True`` never carries --once).
  330. Entry for the picker / cost-confirm callbacks, which fire outside ``_handle_model_command``
  331. and take the switch lock themselves; the typed path already holds it."""
  332. async with self._model_switch_lock():
  333. return await self._commit_model_switch_locked(result, ctx, source=source, picker=picker)
  334. def _channel_override_for(self, source):
  335. """This chat's ``channel_overrides`` entry (model/provider), or None."""
  336. from gateway.run import _get_channel_override
  337. cfg = getattr(self, "config", None)
  338. if not cfg or source is None:
  339. return None
  340. return _get_channel_override(
  341. cfg, source.platform, str(source.chat_id) if source.chat_id else "",
  342. thread_id=str(source.thread_id) if getattr(source, "thread_id", None) else None,
  343. parent_id=str(source.parent_chat_id) if getattr(source, "parent_chat_id", None) else None,
  344. )
  345. def _model_switch_lock(self) -> asyncio.Lock:
  346. """Runner-wide lock over a /model command's read-resolve-commit. Slash commands bypass the busy
  347. guard while no agent runs, so a second /model on the same (or another) session otherwise
  348. interleaves with the first's store/config awaits: two ``--global`` picks left config.yaml on
  349. whichever thread wrote last and a ``--global`` cleanup wiped a session pick issued after it
  350. (#100314). Lazy: the runner is built without __init__ in tests."""
  351. lock = self.__dict__.get("_model_switch_lock_obj")
  352. if lock is None:
  353. lock = self.__dict__["_model_switch_lock_obj"] = asyncio.Lock()
  354. return lock
  355. def _record_switch_metrics(self, result, ctx: _ModelSwitchContext, source) -> None:
  356. """Slash dispatch does not install the routed profile's scope, so a multiplexed runner binds
  357. the owning home for the switch row and its switch_away friction."""
  358. from hermes_cli.observability.shared_metrics_events import record_model_switch
  359. from hermes_constants import reset_hermes_home_override, set_hermes_home_override
  360. home = None
  361. if getattr(getattr(self, "config", None), "multiplex_profiles", False):
  362. with contextlib.suppress(Exception):
  363. home = self._resolve_profile_home_for_source(source)
  364. session_id = None
  365. with contextlib.suppress(Exception):
  366. session_id = getattr(self._cached_agent_for(ctx.session_key), "session_id", None)
  367. token = set_hermes_home_override(str(home)) if home else None
  368. try:
  369. record_model_switch(
  370. from_provider=ctx.route_provider, to_provider=result.target_provider, surface="gateway",
  371. from_model=ctx.current_model, session_id=session_id)
  372. finally:
  373. if token is not None:
  374. reset_hermes_home_override(token)
  375. async def _commit_model_switch_locked(self, result, ctx: _ModelSwitchContext, *, source, picker: bool) -> str:
  376. one_turn = False if picker else ctx.one_turn
  377. from gateway.session_setting_commands import defer_local_model_pick, local_authority_live
  378. authority, live = local_authority_live(self, source)
  379. level = ""
  380. if live is not None:
  381. # A local session runs its frozen policy: the owner's canonical model mutation is the
  382. # only commit, landing at the next turn boundary (this command is the running turn).
  383. if one_turn:
  384. return t("gateway.model.error_prefix", error=t("gateway.model.err_once_local"))
  385. from hermes_constants import parse_reasoning_effort
  386. effort = (ctx.reasoning_effort or "").strip().lower()
  387. level = effort if parse_reasoning_effort(effort) is not None else ""
  388. await asyncio.to_thread(defer_local_model_pick, authority, live, result, level)
  389. self._record_switch_metrics(result, ctx, source)
  390. global_error = None
  391. if ctx.persist_global:
  392. try:
  393. await _persist_model_switch_to_config(result, ctx.config_path)
  394. except Exception as e:
  395. logger.warning("Failed to persist model switch", exc_info=True)
  396. global_error = t("gateway.model.err_config_not_updated", error=str(e) or type(e).__name__)
  397. else:
  398. error = self._switch_cached_agent_model(result, ctx, picker)
  399. if error is not None:
  400. return error
  401. self._record_switch_metrics(result, ctx, source)
  402. global_error = await self._record_model_switch(
  403. result, ctx, source=source, one_turn=one_turn, picker=picker)
  404. reply = await self._model_switch_confirmation(
  405. result, ctx, one_turn=one_turn, picker=picker, global_error=global_error,
  406. )
  407. if level:
  408. # Committed with the local pick: only the config write-through and the reply remain.
  409. saved = (ctx.persist_global and global_error is None
  410. and self._save_gateway_config_key("agent.reasoning_effort", level))
  411. reply += "\n" + t("gateway.reasoning.set_global" if saved else "gateway.reasoning.set_session",
  412. effort=level)
  413. elif ctx.reasoning_effort and not one_turn:
  414. # `/model X --reasoning <level>`: same applier as /reasoning, same scope as the pick.
  415. # The record step already evicted the cached agent, so the pin lands on the rebuild.
  416. from gateway.run import _platform_config_key
  417. reply += "\n" + self._apply_reasoning_selection(
  418. ctx.session_key, _platform_config_key(source.platform), ctx.reasoning_effort,
  419. persist_global=ctx.persist_global and global_error is None)
  420. return reply + ("\n" + t("gateway.model.local_next_turn") if live is not None else "")
  421. async def _send_model_picker(self, event: MessageEvent, source, adapter, session_key: str, listing_kwargs: dict, on_model_selected) -> bool:
  422. """Send the interactive /model picker; False when nothing was sent (text fallback). *source*
  423. is session-key-normalized so the picker's thread metadata lands where the next turn reads."""
  424. from hermes_cli.model_switch_providers import list_picker_providers
  425. try: # off-loop: listing still reads config/disk cache synchronously (#41289)
  426. providers = await asyncio.to_thread(
  427. list_picker_providers, max_models=50, include_moa=True, **listing_kwargs
  428. )
  429. except Exception:
  430. providers = []
  431. if not providers:
  432. return False
  433. result = await adapter.send_model_picker(
  434. chat_id=source.chat_id, providers=providers,
  435. current_model=listing_kwargs["current_model"],
  436. current_provider=listing_kwargs["current_provider"], session_key=session_key,
  437. on_model_selected=on_model_selected,
  438. metadata=self._thread_metadata_for_source(source, self._reply_anchor_for_event(event)),
  439. )
  440. return bool(result.success)
  441. async def _model_listing_reply(
  442. self, event: MessageEvent, ctx: _ModelSwitchContext, profile_home
  443. ) -> Optional[str]:
  444. """``/model`` with no args: interactive picker where supported, else the text list."""
  445. from hermes_cli.model_switch import list_authenticated_providers
  446. from hermes_cli.providers import get_label
  447. listing_kwargs = dict(
  448. current_provider=ctx.current_provider, current_base_url=ctx.current_base_url,
  449. current_model=ctx.current_model, user_providers=ctx.user_provs,
  450. custom_providers=ctx.custom_provs, excluded_providers=ctx.excluded_provs,
  451. # Chat `/model` is a read path: catalogs come from the disk cache and stale ones warm
  452. # in the background, and only the selected custom endpoint is probed live, so one
  453. # degraded provider can't stall the reply (#74003). Mirrors the GUI read path.
  454. non_blocking_catalogs=True, probe_custom_providers=False, probe_current_custom_provider=True,
  455. )
  456. adapter = self._delivery_adapter_for(ctx.source)
  457. if adapter is not None and getattr(type(adapter), "send_model_picker", None) is not None:
  458. async def _picker_switch(model_id: str, provider_slug: str) -> str:
  459. # The picker callback binds the raw event source (pre-normalization).
  460. result, error = await self._perform_model_switch(ctx, model_id, provider_slug, event.source)
  461. if error is not None:
  462. return error
  463. return await self._commit_model_switch(result, ctx, source=event.source, picker=True)
  464. async def _on_model_selected(_chat_id: str, model_id: str, provider_slug: str) -> str:
  465. if profile_home is None:
  466. return await _picker_switch(model_id, provider_slug)
  467. from gateway.run import _profile_runtime_scope
  468. with _profile_runtime_scope(profile_home):
  469. return await _picker_switch(model_id, provider_slug)
  470. if await self._send_model_picker(event, ctx.source, adapter, ctx.session_key, listing_kwargs, _on_model_selected):
  471. return None # Picker sent — adapter handles the response
  472. lines = [t("gateway.model.current_label", model=ctx.current_model or t("gateway.shared.unknown_value"),
  473. provider=get_label(ctx.current_provider)), ""]
  474. try: # off-loop: listing still reads config/disk cache synchronously (#41289)
  475. providers = await asyncio.to_thread(list_authenticated_providers, max_models=_TEXT_LISTING_MODELS, **listing_kwargs)
  476. lines.extend(_model_provider_listing_lines(providers))
  477. except Exception:
  478. pass
  479. lines.append(t("gateway.model.usage_switch_model"))
  480. lines.append(t("gateway.model.usage_switch_provider"))
  481. lines.append(t("gateway.model.usage_persist"))
  482. lines.append(t("gateway.model.usage_reset"))
  483. return "\n".join(lines)
  484. async def _model_selection_guard_reply(
  485. self, event: MessageEvent, ctx: _ModelSwitchContext, result
  486. ) -> tuple[bool, Optional[str]]:
  487. """Selection-guard confirmation for the typed path (pickers confirm via their own UI).
  488. The unified registry (cost + data-policy guards) runs off-loop — pricing lookups may hit
  489. models.dev on a cache miss. Returns ``(fired, reply)``; the reply is None when the platform
  490. rendered confirm buttons itself.
  491. """
  492. try:
  493. from hermes_cli.model_selection_guards import (
  494. combined_selection_warning, selection_context_for_agent)
  495. warning = await asyncio.to_thread(
  496. combined_selection_warning, result.new_model, provider=result.target_provider,
  497. base_url=result.base_url or ctx.current_base_url or "",
  498. api_key=result.api_key or ctx.current_api_key or "", model_info=result.model_info,
  499. selection_context=selection_context_for_agent(self._cached_agent_for(ctx.session_key)),
  500. )
  501. except Exception:
  502. warning = None
  503. if warning is None:
  504. return False, None
  505. async def _on_cost_confirm(choice: str) -> str:
  506. if choice == "cancel":
  507. return t("gateway.model.switch_cancelled", model=ctx.current_model or t("gateway.shared.unknown_value"))
  508. # "once" and "always" both proceed — selection guards have no persistent opt-out.
  509. return await self._commit_model_switch(result, ctx, source=ctx.source)
  510. _p = self._typed_command_prefix_for(event.source.platform)
  511. message = t("gateway.model.guard_confirm", title=warning.title, message=warning.message, prefix=_p)
  512. return True, await self._request_slash_confirm(
  513. event=event, command="model", title=warning.title, message=message, handler=_on_cost_confirm,
  514. )
  515. async def _handle_model_command(self, event: MessageEvent) -> Optional[str]:
  516. """Handle /model command — switch model. Taken under the switch lock BEFORE the first await so
  517. concurrent commands commit in issue order (see ``_model_switch_lock``)."""
  518. async with self._model_switch_lock():
  519. return await self._handle_model_command_locked(event)
  520. async def _handle_model_command_locked(self, event: MessageEvent) -> Optional[str]:
  521. from gateway.run import _hermes_home
  522. from hermes_cli.model_switch import parse_model_switch_args, resolve_persist_behavior
  523. profile_home = None
  524. if getattr(getattr(self, "config", None), "multiplex_profiles", False):
  525. profile_home = self._resolve_profile_home_for_source(event.source)
  526. request = parse_model_switch_args(event.get_command_args().strip()) # single-owner parser
  527. if request.errors:
  528. return f"❌ {request.error_messages()[0]}" # gateway decoration over canonical copy
  529. if request.force_refresh: # bust the disk cache so the picker shows live data
  530. with contextlib.suppress(Exception):
  531. from hermes_cli.models import clear_provider_models_cache
  532. clear_provider_models_cache()
  533. # Normalize like a message turn (Telegram DM topic recovery) before deriving the override
  534. # key, so the override lands under the key the next turn reads.
  535. # Check for session override. See #30479.
  536. source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
  537. session_key = self._session_key_for_source(source)
  538. ctx = _ModelSwitchContext(
  539. # Gateway routing columns — forward ALL of them at CREATE time, same fix as the
  540. # compression-rotation bug in agent/conversation_compression.py. Without these, the branched
  541. # child row has NULL routing columns until switch_session() below calls
  542. # _record_gateway_session_peer() — a crash/kill anywhere between here and there (most plausibly
  543. # mid-history-copy, since each append_message call a few lines down is independently
  544. # best-effort) leaves the branch permanently unroutable: unreachable by chat/thread lookup, and
  545. # unreachable via /resume's IDOR guard too (which requires the row's chat_id/thread_id to match
  546. # the caller's). user_id is critical for the fallback lookup path (hermes_state.py:1994-2009)
  547. # that searches by the complete peer tuple when session_key doesn't match. origin_json and
  548. # display_name complete the identity (same shape as the reset path's db_create_kwargs in
  549. # gateway/session.py, #82633) so consumers that read routing/presentation data from state.db
  550. # (the mcp-serve plugin, mirror, channel directory) see the branch row fully formed with zero backfill gap.
  551. session_key=session_key,
  552. source=source,
  553. config_path=(profile_home or _hermes_home) / "config.yaml",
  554. persist_global=resolve_persist_behavior(
  555. request.is_global, request.is_session, is_once=request.is_once,
  556. explicit_provider=request.explicit_provider,
  557. ),
  558. one_turn=request.is_once,
  559. reasoning_effort=request.reasoning_effort,
  560. restore_snapshot=self._snapshot_session_model_override(session_key) if request.is_once else None,
  561. )
  562. ctx.read_config()
  563. if request.is_reset:
  564. return await self._handle_model_reset(event, ctx)
  565. ctx.apply_override(self._session_model_overrides.get(session_key, {}))
  566. if not request.target and not request.explicit_provider:
  567. return await self._model_listing_reply(event, ctx, profile_home)
  568. result, error = await self._perform_model_switch(ctx, request.target, request.explicit_provider, source)
  569. if error is not None:
  570. return error
  571. guard_fired, guard_reply = await self._model_selection_guard_reply(event, ctx, result)
  572. if guard_fired:
  573. return guard_reply
  574. return await self._commit_model_switch_locked(result, ctx, source=source, picker=False)
  575. def _channel_default_route(self, source, config_path) -> tuple[str, str]:
  576. """``(model, provider)`` this chat runs with no session override: channel_overrides, else the
  577. owning profile's config.yaml (read from *config_path*: slash dispatch runs outside the routed
  578. profile's scope on a multiplexed runner)."""
  579. from gateway.run import _load_gateway_config, _resolve_gateway_model
  580. cfg = _load_gateway_config(config_path=config_path) or {}
  581. model_cfg = cfg.get("model")
  582. provider = str(model_cfg.get("provider") or "") if isinstance(model_cfg, dict) else ""
  583. channel = self._channel_override_for(source)
  584. if channel is not None and channel.model:
  585. return channel.model, channel.provider or ""
  586. return _resolve_gateway_model(cfg), "" if provider.lower() == "auto" else provider
  587. async def _handle_model_reset(self, event: MessageEvent, ctx: _ModelSwitchContext) -> Optional[str]:
  588. """``/model reset``: drop this session's /model override (memory, store and any armed
  589. ``--once`` restore) so the next turn resolves channel_overrides > config.yaml again. The
  590. transcript is kept; before this the only way off a persisted override was ``/new``."""
  591. source, session_key = ctx.source, ctx.session_key
  592. default_model, default_provider = await asyncio.to_thread(
  593. self._channel_default_route, source, ctx.config_path)
  594. from gateway.session_setting_commands import local_authority_live
  595. if local_authority_live(self, source)[1] is not None:
  596. # A local session runs a frozen policy with no override tier: reset is a session-scoped
  597. # pick of the configured default, committed by the owner like any typed /model.
  598. ctx.persist_global = False
  599. result, error = await self._perform_model_switch(ctx, default_model, default_provider, source)
  600. if error is not None:
  601. return error
  602. return await self._commit_model_switch_locked(result, ctx, source=source, picker=False)
  603. # A restart leaves the persisted override unloaded until first use: load it, or the reply
  604. # says "none active" while the store resurrects it on the next message.
  605. await asyncio.to_thread(self._rehydrate_session_model_override, session_key)
  606. previous = self._session_model_overrides.pop(session_key, None)
  607. self._pending_one_turn_model_restores.pop(session_key, None)
  608. try:
  609. await self.async_session_store.set_model_override(session_key, None)
  610. except Exception:
  611. logger.warning("Failed to clear persisted session model override", exc_info=True)
  612. shown = default_model or t("gateway.shared.unknown_value")
  613. if previous is None:
  614. return t("gateway.model.reset_none", model=shown)
  615. from hermes_cli.model_switch import format_model_for_display
  616. _sess_db = getattr(self, "_session_db", None)
  617. if _sess_db is not None: # the dashboard's session-model column (#34850)
  618. try:
  619. entry = await self.async_session_store.get_or_create_session(source)
  620. await _sess_db.update_session_model(entry.session_id, default_model, provider=default_provider or None)
  621. except Exception:
  622. logger.debug("Failed to persist model reset to DB", exc_info=True)
  623. if not hasattr(self, "_pending_model_notes"):
  624. self._pending_model_notes = {}
  625. self._pending_model_notes[session_key] = (
  626. f"[Note: the session model override was cleared; model changed from "
  627. f"{format_model_for_display(previous.get('model') or '')} back to the default "
  628. f"{format_model_for_display(default_model)}. Adjust your self-identification accordingly.]")
  629. self._evict_cached_agent(session_key) # next turn builds from the default route
  630. return t("gateway.model.reset_done", model=format_model_for_display(shown))
  631. # -------------------------------------------------- usage-window limit: one-tap switch
  632. def _schedule_usage_limit_offer(self, event: MessageEvent, limit: dict, *, session_key: str = "",
  633. generation: Optional[int] = None) -> None:
  634. """After a usage-window refusal's reply: offer the plan's recommended model as a button where the
  635. platform already has quick-choice buttons, else one line of text.
  636. Runs as the adapter's post-delivery callback, so the offer always lands under the reply (the reply
  637. is sent after this turn returns). The tap switches this session only (never the default) and
  638. re-sends the refused message, like /retry. No credits button in a chat: anyone in the room could
  639. spend the owner's credits, so the Portal is named instead. Hermes never switches on its own."""
  640. try:
  641. adapter = self._delivery_adapter_for(event.source) if event.source is not None else None
  642. register = getattr(adapter, "register_post_delivery_callback", None)
  643. if session_key and callable(register):
  644. register(session_key, lambda: self._offer_usage_limit_switch(event, limit, generation),
  645. generation=generation)
  646. return
  647. self._track_background_task(self._offer_usage_limit_switch(event, limit, generation))
  648. except Exception:
  649. logger.debug("usage-limit offer scheduling failed", exc_info=True)
  650. async def _offer_usage_limit_switch(self, event: MessageEvent, limit: dict,
  651. generation: Optional[int] = None) -> None:
  652. from agent.plan_limits import limit_next_steps_from_payload
  653. try:
  654. suggestions = [s for s in (limit.get("suggestions") or []) if isinstance(s, dict) and s.get("model")]
  655. source = event.source
  656. if source is None:
  657. return
  658. prefix = self._typed_command_prefix_for(source.platform)
  659. adapter = self._delivery_adapter_for(source)
  660. session_key = self._session_key_for_source(source)
  661. if suggestions and adapter is not None and getattr(type(adapter), "send_choice_picker", None) is not None:
  662. async def _on_choice(_chat_id: str, value: str) -> str:
  663. if generation is not None and self._usage_limit_offer_is_stale(session_key, generation):
  664. # A newer turn ran since the refusal: /retry would re-run that one, not the refused one.
  665. return t("platform.discord.picker.choice_expired")
  666. return await self._accept_usage_limit_switch(
  667. event, value, offer=None if generation is None else (session_key, generation))
  668. choices = [{"value": str(s["model"]),
  669. "label": t("gateway.usage_limit.switch_choice", model=str(s.get("label") or s["model"]),
  670. note=str(s.get("note") or "")),
  671. "is_current": False} for s in suggestions]
  672. if await self._try_send_choice_picker(
  673. event, session_key, title=t("gateway.usage_limit.picker_title"), choices=choices,
  674. on_choice_selected=_on_choice):
  675. return
  676. text = limit_next_steps_from_payload(limit, model_command=f"{prefix}model", topup_command=None, portal=True)
  677. if adapter is not None and text:
  678. await adapter.send(source.chat_id, text, metadata=self._reply_metadata(event))
  679. except Exception:
  680. logger.debug("usage-limit offer failed", exc_info=True)
  681. def _usage_limit_offer_is_stale(self, session_key: str, generation: int) -> bool:
  682. """True once the session has started a newer run than the refused one. Fails open (not stale)."""
  683. try:
  684. return int(self._session_state(session_key).persistent.run_generation) != int(generation)
  685. except (AttributeError, KeyError, TypeError, ValueError):
  686. return False
  687. async def _accept_usage_limit_switch(self, event: MessageEvent, model_id: str, *,
  688. offer: Optional[tuple[str, int]] = None) -> str:
  689. """The tap: switch this session to *model_id* on Nous, then re-send the refused message.
  690. ``offer`` is the refused turn's ``(session_key, run_generation)``."""
  691. from gateway.run import _hermes_home
  692. source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
  693. session_key = self._session_key_for_source(source)
  694. profile_home = None
  695. if getattr(getattr(self, "config", None), "multiplex_profiles", False):
  696. profile_home = self._resolve_profile_home_for_source(event.source)
  697. ctx = _ModelSwitchContext(session_key=session_key, source=source,
  698. config_path=(profile_home or _hermes_home) / "config.yaml", persist_global=False)
  699. async def _switch() -> str:
  700. ctx.read_config()
  701. ctx.apply_override(self._session_model_overrides.get(session_key, {}))
  702. result, error = await self._perform_model_switch(ctx, model_id, "nous", event.source)
  703. if error is not None:
  704. return error
  705. return await self._commit_model_switch(result, ctx, source=event.source, picker=True)
  706. before = self._session_model_overrides.get(session_key)
  707. if profile_home is None:
  708. reply = await _switch()
  709. else:
  710. from gateway.run import _profile_runtime_scope
  711. with _profile_runtime_scope(profile_home):
  712. reply = await _switch()
  713. after = self._session_model_overrides.get(session_key)
  714. switched = after is not None and after is not before and after.get("provider") == "nous"
  715. if switched:
  716. # Only after the switch landed (an error reply leaves the override unchanged). Exactly a typed /retry from the same sender (rewinds the refused turn, runs the message
  717. # again, and the adapter delivers the reply). Called directly, the re-run's reply would be
  718. # returned to nobody.
  719. self._track_background_task(self._dispatch_usage_limit_retry(event, offer))
  720. return reply
  721. async def _dispatch_usage_limit_retry(self, event: MessageEvent, offer: Optional[tuple[str, int]] = None) -> None:
  722. from gateway.platforms.event import MessageType
  723. adapter = self._delivery_adapter_for(event.source)
  724. # Re-checked here, with no await before the /retry: the switch awaited model resolution, and a
  725. # turn that ran meanwhile is what /retry would rewind. The switch itself stands.
  726. if adapter is None or (offer is not None and self._usage_limit_offer_is_stale(*offer)):
  727. return
  728. try:
  729. await adapter.handle_message(MessageEvent(
  730. text="/retry", message_type=MessageType.TEXT, source=event.source,
  731. raw_message=event.raw_message, channel_prompt=event.channel_prompt))
  732. except Exception:
  733. logger.debug("usage-limit retry dispatch failed", exc_info=True)
  734. # -------------------------------------------------- /codex-runtime, /personality
  735. async def _handle_codex_runtime_command(self, event: MessageEvent) -> str:
  736. """Handle /codex-runtime; a real change evicts the cached agent so the new api_mode applies
  737. on the next message (avoids prompt-cache invalidation mid-session)."""
  738. from hermes_cli import codex_runtime_switch as crs
  739. new_value, errors = crs.parse_args(event.get_command_args().strip() if event else "")
  740. if errors:
  741. return "❌ " + "\n❌ ".join(errors)
  742. try:
  743. from hermes_cli.config import load_config, save_config
  744. except Exception as exc:
  745. return t("gateway.codex_runtime.config_load_failed", error=exc)
  746. result = crs.apply(
  747. load_config(), new_value, persist_callback=(save_config if new_value is not None else None),
  748. )
  749. if result.success and new_value is not None and result.requires_new_session:
  750. try:
  751. self._evict_cached_agent(self._session_key_for_source(event.source))
  752. except Exception:
  753. logger.debug("could not evict cached agent after codex-runtime change", exc_info=True)
  754. return f"{'✓' if result.success else '✗'} {result.message}"
  755. async def _handle_personality_command(self, event: MessageEvent) -> str:
  756. """Handle /personality — list or set a personality (hermes_cli.personality owns the state)."""
  757. from gateway.run import _load_gateway_config
  758. from hermes_cli.personality import (
  759. active_personality_name,
  760. available_personalities,
  761. describe_personality,
  762. persist_personality,
  763. resolve_personality,
  764. )
  765. args = event.get_command_args().strip()
  766. try:
  767. config = _load_gateway_config()
  768. except Exception:
  769. config = {}
  770. personalities = available_personalities(config)
  771. if not args:
  772. current = active_personality_name(config)
  773. lines = [t("gateway.personality.header"), t("gateway.personality.none_option")]
  774. for name, prompt in personalities.items():
  775. marker = " ✓" if name == current else ""
  776. lines.append(
  777. t("gateway.personality.item", name=f"{name}{marker}", preview=describe_personality(prompt))
  778. )
  779. lines.append(t("gateway.personality.usage"))
  780. return "\n".join(lines)
  781. try:
  782. name, _new_prompt = resolve_personality(args, config)
  783. except ValueError:
  784. available = "`none`, " + ", ".join(f"`{n}`" for n in personalities)
  785. return t("gateway.personality.unknown", name=args.lower(), available=available)
  786. # Persists the selection only (never agent.system_prompt, a user-owned overlay) into the
  787. # routed profile's config.yaml; the next turn re-resolves the prompt — no process-global state.
  788. if not persist_personality(name):
  789. return t("gateway.personality.save_failed", error=t("gateway.personality.err_config_write"))
  790. if not name:
  791. return t("gateway.personality.cleared")
  792. return t("gateway.personality.set_to", name=name)
  793. # ----------------------------------------------------------- /reasoning, /fast
  794. def _save_gateway_config_key(self, key_path: str, value) -> bool:
  795. """Save a dot-separated key to config.yaml (shared by /reasoning, /fast and their pickers)."""
  796. from gateway.slash_commands import _nested_dict
  797. from gateway.run import _gateway_config_home
  798. from hermes_cli.config import read_user_config_raw
  799. config_path = _gateway_config_home() / "config.yaml"
  800. try:
  801. user_config = read_user_config_raw(config_path) # raw: never persist merged defaults
  802. *parents, leaf = key_path.split(".")
  803. _nested_dict(user_config, *parents)[leaf] = value
  804. atomic_config_write(config_path, user_config)
  805. return True
  806. except Exception as e:
  807. logger.error("Failed to save config key %s: %s", key_path, e)
  808. return False
  809. def _set_reasoning_override(self, session_key: str, value) -> None:
  810. """Store (or clear with None) the session reasoning override and drop the cached agent."""
  811. self._set_session_reasoning_override(session_key, value)
  812. self._evict_cached_agent(session_key)
  813. def _apply_reasoning_selection(
  814. self, session_key: str, platform_key: str, value: str, persist_global: bool = False,
  815. ) -> str:
  816. """Apply a /reasoning argument (typed or picked) and return the reply."""
  817. from hermes_constants import parse_reasoning_effort
  818. value = (value or "").strip().lower()
  819. from gateway.session_setting_commands import local_live_for_route
  820. live = local_live_for_route(self, session_key)
  821. show = _REASONING_DISPLAY_TOGGLES.get(value)
  822. if show is not None: # per-platform display toggle
  823. self._show_reasoning = show
  824. if live is not None:
  825. live.show_reasoning = show # what this local session's viewers stream (config.get show_reasoning)
  826. self._save_gateway_config_key(f"display.platforms.{platform_key}.show_reasoning", show)
  827. key = "gateway.reasoning.display_set_on" if show else "gateway.reasoning.display_set_off"
  828. return t(key, platform=platform_key)
  829. if value == "reset":
  830. if persist_global:
  831. return t("gateway.reasoning.reset_global_unsupported")
  832. self._set_session_reasoning_override(session_key, None)
  833. self._reasoning_config = self._load_reasoning_config()
  834. self._evict_cached_agent(session_key)
  835. return t("gateway.reasoning.reset_done")
  836. parsed = parse_reasoning_effort(value)
  837. if parsed is None:
  838. return t("gateway.reasoning.unknown_arg", arg=value)
  839. self._reasoning_config = parsed
  840. if persist_global:
  841. if self._save_gateway_config_key("agent.reasoning_effort", value):
  842. # A local session runs its frozen level, not config.yaml's: it takes the level too.
  843. self._set_reasoning_override(session_key, parsed if live is not None else None)
  844. return t("gateway.reasoning.set_global", effort=value)
  845. self._set_reasoning_override(session_key, parsed)
  846. return t("gateway.reasoning.set_global_save_failed", effort=value)
  847. self._set_reasoning_override(session_key, parsed)
  848. return t("gateway.reasoning.set_session", effort=value)
  849. async def _try_send_choice_picker(
  850. self, event: MessageEvent, session_key: str, title: str, choices: list, on_choice_selected,
  851. ) -> bool:
  852. """Send an interactive choice picker when the adapter *type* supports it (the /model gate);
  853. a failed send returns False (text fallback) instead of erroring."""
  854. adapter = self._delivery_adapter_for(event.source)
  855. if adapter is None or getattr(type(adapter), "send_choice_picker", None) is None:
  856. return False
  857. try:
  858. result = await adapter.send_choice_picker(
  859. chat_id=event.source.chat_id, title=title, choices=choices, session_key=session_key,
  860. on_choice_selected=on_choice_selected, metadata=self._reply_metadata(event),
  861. )
  862. return bool(getattr(result, "success", False))
  863. except Exception as e:
  864. logger.warning("send_choice_picker failed, falling back to text: %s", e)
  865. return False
  866. async def _handle_reasoning_command(self, event: MessageEvent) -> Optional[str]:
  867. """Handle /reasoning command — manage reasoning effort and display toggle."""
  868. from gateway.run import _platform_config_key
  869. from hermes_constants import VALID_REASONING_EFFORTS
  870. raw_args = event.get_command_args().strip()
  871. args, persist_global = self._parse_reasoning_command_args(raw_args)
  872. # Normalize (Telegram DM topic recovery) so the override key matches the next turn's.
  873. # See #30479.
  874. _reasoning_source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
  875. session_key = self._session_key_for_source(_reasoning_source)
  876. self._show_reasoning = self._load_show_reasoning()
  877. from gateway.session_setting_commands import local_live
  878. if (_live := local_live(self, event.source)) is not None:
  879. self._show_reasoning = getattr(_live, "show_reasoning", None) is True
  880. # Effective model (session /model override wins) so per-model reasoning_overrides display.
  881. _session_model = str(
  882. ((getattr(self, "_session_model_overrides", {}) or {}).get(session_key) or {}).get("model") or ""
  883. )
  884. self._reasoning_config = self._resolve_session_reasoning_config(
  885. source=event.source, session_key=session_key, model=_session_model,
  886. )
  887. platform_key = _platform_config_key(event.source.platform)
  888. if raw_args: # typed path — same applier the picker uses
  889. return self._apply_reasoning_selection(session_key, platform_key, args, persist_global=persist_global)
  890. rc = self._reasoning_config
  891. # Labels tell the truth about the route: a Hermes-internal step (``ultra``) that the wire
  892. # clamps is shown as "ultra (sends max on this route)" instead of a distinct level (#61634).
  893. from agent.reasoning_effort import effort_display_label
  894. from gateway.run import _load_gateway_config
  895. from hermes_cli.codex_runtime_switch import get_current_runtime
  896. _session_route = ((getattr(self, "_session_model_overrides", {}) or {}).get(session_key) or {})
  897. _model_cfg = {}
  898. with contextlib.suppress(Exception): # fail-open on config read errors, like /model does
  899. _model_cfg = _load_gateway_config(config_path=self.config_path).get("model", {}) or {}
  900. _route = (
  901. _session_route.get("provider") or _model_cfg.get("provider"),
  902. _session_model or _model_cfg.get("default") or _model_cfg.get("model"),
  903. get_current_runtime({"model": _model_cfg}),
  904. )
  905. if rc is None:
  906. level, current_effort = t("gateway.reasoning.level_default"), "medium"
  907. elif rc.get("enabled") is False:
  908. level, current_effort = t("gateway.reasoning.level_disabled"), "none"
  909. else:
  910. current_effort = rc.get("effort", "medium")
  911. level = effort_display_label(current_effort, *_route)
  912. display_state = t("gateway.reasoning.display_on") if self._show_reasoning else t("gateway.reasoning.display_off")
  913. has_session_override = session_key in (getattr(self, "_session_reasoning_overrides", {}) or {})
  914. scope = t("gateway.reasoning.scope_session") if has_session_override else t("gateway.reasoning.scope_global")
  915. async def _on_reasoning_choice(_chat_id: str, value: str) -> str:
  916. return self._apply_reasoning_selection(session_key, platform_key, value)
  917. picker_sent = await self._try_send_choice_picker(
  918. event,
  919. session_key,
  920. title=t("gateway.reasoning.picker_title", level=level, scope=scope, display=display_state),
  921. choices=[
  922. {"value": "none", "label": t("gateway.reasoning.choice_none"), "is_current": current_effort == "none"},
  923. *({"value": lv, "label": effort_display_label(lv, *_route), "is_current": lv == current_effort}
  924. for lv in VALID_REASONING_EFFORTS),
  925. *({"value": v, "label": t(f"gateway.reasoning.choice_{v}"), "is_current": False}
  926. for v in ("reset", "show", "hide")),
  927. ],
  928. on_choice_selected=_on_reasoning_choice,
  929. )
  930. if picker_sent:
  931. return None # Picker sent — adapter handles the response
  932. return t("gateway.reasoning.status", level=level, scope=scope, display=display_state)
  933. def _apply_fast_selection(self, session_key: str, value: str, persist: bool = False) -> str:
  934. """Apply a /fast argument (typed or picked) and return the reply."""
  935. selection = _FAST_SELECTIONS.get(value)
  936. if selection is None:
  937. return t("gateway.fast.unknown_arg", arg=value)
  938. tier, saved_value, label_key = selection
  939. label = t(label_key) if label_key else value.upper()
  940. self._service_tier = tier
  941. from gateway.session_setting_commands import local_live_for_route
  942. if persist and self._save_gateway_config_key("agent.service_tier", saved_value):
  943. if local_live_for_route(self, session_key) is not None:
  944. # A local session runs its frozen tier, not config.yaml's: it takes the tier too.
  945. self._set_session_service_tier_override(session_key, tier)
  946. else:
  947. self._set_session_service_tier_override(session_key, None, clear=True) # global wins
  948. self._evict_cached_agent(session_key)
  949. return t("gateway.fast.saved", label=label)
  950. # Session override — also the fallback after a failed config write (as /reasoning --global).
  951. self._set_session_service_tier_override(session_key, tier)
  952. self._evict_cached_agent(session_key)
  953. return t("gateway.fast.session_only", label=label)
  954. async def _handle_fast_command(self, event: MessageEvent) -> Optional[str]:
  955. """Handle /fast — the CLI Priority Processing toggle; session-scoped unless ``--global``
  956. (persists agent.service_tier, parity with /model)."""
  957. from agent.fast_mode import FAST_ARG_ALIASES, service_tier_word
  958. from gateway.run import _load_gateway_config, _resolve_gateway_model
  959. # The /reasoning parser strips --global (any position) and normalizes unicode dashes.
  960. args, persist_global = self._parse_reasoning_command_args(event.get_command_args().strip().lower())
  961. # Normalized like /model and /reasoning (#30479): the tier override must land under the key
  962. # the next turn reads, and the eligibility check must see that turn's route.
  963. source = await asyncio.to_thread(self._normalize_source_for_session_key, event.source)
  964. session_key = self._session_key_for_source(source)
  965. self._service_tier = self._resolve_session_service_tier(session_key=session_key)
  966. user_config = _load_gateway_config()
  967. try:
  968. # Off the loop: credential resolution can refresh an OAuth token, as the turn does.
  969. model, runtime = await asyncio.to_thread(
  970. self._resolve_session_agent_runtime, source=source, session_key=session_key, user_config=user_config)
  971. except Exception:
  972. # No usable route at all (the turn fails too): judge the configured model alone.
  973. model, runtime = _resolve_gateway_model(user_config), {}
  974. if not _fast_route_supports(model, runtime):
  975. return t("gateway.fast.not_supported")
  976. args = FAST_ARG_ALIASES.get(args, args)
  977. from agent.codex_service_tiers import ultrafast_offered
  978. ultrafast = ultrafast_offered(model, provider=runtime.get("provider"), base_url=runtime.get("base_url"),
  979. access_token=runtime.get("api_key"))
  980. if args == "ultrafast" and not ultrafast:
  981. return t("gateway.fast.ultrafast_not_supported", model=model)
  982. if args and args != "status":
  983. return self._apply_fast_selection(session_key, args, persist=persist_global)
  984. mode = service_tier_word(self._service_tier)
  985. status = {"fast": t("gateway.fast.status_fast"), "normal": t("gateway.fast.status_normal")}.get(mode, mode)
  986. async def _on_fast_choice(_chat_id: str, value: str) -> str:
  987. return self._apply_fast_selection(session_key, value, persist=persist_global)
  988. picker_sent = await self._try_send_choice_picker(
  989. event,
  990. session_key,
  991. title=t("gateway.fast.picker_title", mode=status),
  992. choices=[
  993. {"value": v, "label": t(f"gateway.fast.choice_{v}"), "is_current": mode == v}
  994. for v in ("fast", "normal", "auto", "cold", *(("ultrafast",) if ultrafast else ()))
  995. ],
  996. on_choice_selected=_on_fast_choice,
  997. )
  998. if picker_sent:
  999. return None # Picker sent — adapter handles the response
  1000. return t("gateway.fast.status", mode=status)