cli_chat_turn_mixin.py 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. """chat() and its per-turn phases: image routing, staging, agent thread, interrupt monitor, rendering.
  2. Mixin bound onto ``HermesCLI`` via the MRO. cli.py-internal symbols are imported LAZILY
  3. inside each method — importing ``cli`` at module load time would be a cycle.
  4. """
  5. from __future__ import annotations
  6. import logging
  7. import os
  8. import queue
  9. import sys
  10. import threading
  11. import time
  12. from pathlib import Path
  13. from rich import box as rich_box
  14. from rich.panel import Panel
  15. from typing import Optional
  16. from agent.i18n import t
  17. from hermes_cli.cli_agent_setup_mixin import _retire_agent
  18. class CLIChatTurnMixin:
  19. """chat() and its per-turn phase helpers."""
  20. # Last completed turn's raw agent result. chat() returns only the rendered
  21. # response string, so one-shot callers that must map an outcome onto a
  22. # process exit code (see cli._run_single_query_mode) read this instead.
  23. _last_turn_result = None
  24. def _sync_fallback_chain_with_config(self, agent) -> None:
  25. """Adopt ``fallback_providers`` edits made while this chat is open (#95066) — the same
  26. per-turn, fail-closed contract as the Desktop/TUI and messaging gateways: a torn config.yaml
  27. keeps the last known-good chain instead of reading as "chain removed"."""
  28. from cli import logger
  29. try:
  30. from gateway.run import GatewayRunner
  31. from hermes_cli.config_effective import load_user_config_effective
  32. from hermes_cli.fallback_config import get_fallback_chain
  33. self._fallback_model = get_fallback_chain(load_user_config_effective(fail_closed=True))
  34. except Exception as e:
  35. logger.debug("fallback chain sync skipped (keeping current chain): %s", e)
  36. return
  37. GatewayRunner._apply_fallback_chain_to_agent(agent, self._fallback_model)
  38. def chat(self, message, images: list | None = None, voice_input: bool = False) -> Optional[str]:
  39. """Run one user turn; returns the agent's response, or None on error.
  40. Input typed while the agent runs goes to ``_interrupt_queue`` (separate from
  41. ``_pending_input`` so process_loop and the interrupt monitor never compete); an
  42. interrupting message is re-queued as the next turn. ``voice_input`` gates the
  43. concise voice-response prefix.
  44. Args: message: The user's message (str or multimodal content list) images: Optional list of Path
  45. objects for attached images voice_input: True when the message came from voice transcription (gates
  46. the concise voice-response prefix, #65827)
  47. """
  48. from cli import ChatConsole, _ChatTurn, _DIM, _RST, _accent_hex, _cprint, set_secret_capture_callback
  49. from tools.process_registry_notifications import TimelineNotification
  50. # Single-query and direct chat callers do not go through run().
  51. set_secret_capture_callback(self._secret_capture_callback)
  52. # Reset per turn; only a real interrupt flips it, so early returns leave it False.
  53. self._last_turn_interrupted = False
  54. self._last_turn_failed = False
  55. if not self._ensure_runtime_credentials():
  56. return None
  57. turn_route = self._resolve_turn_agent_config(message)
  58. if turn_route["signature"] != self._active_agent_route_signature:
  59. _retire_agent(self)
  60. # A first-turn agent build shows in the live spinner (the spinner thread repaints it), never
  61. # as a scrollback line.
  62. initializing = t("cli.chat.initializing_agent") if self.agent is None else ""
  63. if initializing:
  64. self._spinner_text = initializing
  65. try:
  66. if not self._init_agent(model_override=turn_route["model"], runtime_override=turn_route["runtime"],
  67. request_overrides=turn_route.get("request_overrides")):
  68. return None
  69. finally:
  70. if initializing and getattr(self, "_spinner_text", "") == initializing:
  71. self._spinner_text = ""
  72. agent = self.agent
  73. if agent is None:
  74. return None
  75. self._sync_fallback_chain_with_config(agent) # chain added after this chat opened reaches this turn
  76. message = self._chat_route_images(message, images)
  77. if isinstance(message, str) and not isinstance(message, TimelineNotification):
  78. # ``@`` references are expanded once, by the gateway, against this session's cwd.
  79. # Lone surrogates (rich-text clipboard paste) crash the OpenAI SDK's JSON serialization.
  80. from agent.message_sanitization import _sanitize_surrogates
  81. message = _sanitize_surrogates(message)
  82. self._chat_stage_user_message(agent, message)
  83. if isinstance(message, TimelineNotification):
  84. message = str(message) # UI metadata is on the staged row, never in model content.
  85. if not getattr(self, "_modern_layout", False):
  86. ChatConsole().print(f"[{_accent_hex()}]{'─' * 40}[/]")
  87. _cprint("")
  88. from agent.notification_presentation import notification_config_snapshot, notification_policy_snapshot
  89. with notification_policy_snapshot(agent, "cli", notification_config_snapshot()):
  90. turn = _ChatTurn()
  91. from gateway.warning_notifications import diagnostic_turn_muted
  92. turn.mute_notification_reply = diagnostic_turn_muted(
  93. agent._pending_cli_user_message.get("display_metadata"), "cli", agent._notification_config)
  94. try:
  95. self._reset_stream_state()
  96. # Not part of _reset_stream_state: must persist across intermediate turn
  97. # boundaries (tool-calling loops), reset once per user turn.
  98. self._reasoning_shown_this_turn = False
  99. self._streamed_text_this_turn = ""
  100. self._chat_setup_turn_audio(turn, message, voice_input)
  101. # Per-prompt elapsed timer — frozen when the agent thread finishes.
  102. self._prompt_start_time = time.time()
  103. self._prompt_duration = 0.0
  104. # Daemon: closing the terminal tab (SIGHUP) must not be kept alive by it.
  105. agent_thread = threading.Thread(target=self._chat_run_agent, args=(turn, message), daemon=True)
  106. agent_thread.start()
  107. interrupt_msg = self._chat_monitor_agent_thread(turn, agent_thread)
  108. self._chat_settle_turn(turn)
  109. return self._chat_render_turn(turn, agent_thread, interrupt_msg)
  110. except Exception as e:
  111. _cprint(t("gateway.model.error_prefix", error=e))
  112. return None
  113. finally:
  114. self._chat_release_turn_audio(turn)
  115. def _chat_release_turn_audio(self, turn):
  116. """Every exit path: stop the thinking sound, send the TTS sentinel, cut TTS only if abnormal."""
  117. from cli import logger
  118. if turn.thinking_started:
  119. try:
  120. from tools.voice_mode import stop_thinking_sound
  121. stop_thinking_sound()
  122. except Exception:
  123. pass
  124. # Safety-net sentinel for exception paths that skipped _chat_settle_turn's; a
  125. # duplicate is harmless (stream_tts_to_speaker exits on the first None).
  126. # stop_event only on abnormal exit: after a normal drain it would race the
  127. # playback worker and cut the final sentence mid-audio.
  128. if turn.text_queue is not None:
  129. try:
  130. turn.text_queue.put_nowait(None)
  131. except Exception:
  132. pass
  133. if turn.stop_event is not None and not turn.tts_normal_exit:
  134. logger.info("TTS CUT: exception finally block setting stop_event")
  135. turn.stop_event.set()
  136. if turn.tts_thread is not None and turn.tts_thread.is_alive():
  137. turn.tts_thread.join(timeout=5)
  138. def _chat_route_images(self, message, images):
  139. """Attach images natively (vision model) or pre-describe them as text; returns the message to send.
  140. "native" → OpenAI-style content parts (adapters translate per provider); "text" →
  141. vision_analyze each image and prepend the description. Decision: agent/image_routing.py.
  142. """
  143. from cli import _DIM, _RST, _cprint, _split_model_config_default
  144. if not images:
  145. return message
  146. text = message if isinstance(message, str) else ""
  147. try:
  148. from agent.image_routing import build_native_content_parts, decide_image_input_mode
  149. from hermes_cli.config import load_config
  150. _img_model = (_split_model_config_default(self.model)[0]
  151. if isinstance(self.model, dict) else str(self.model or ""))
  152. _img_provider = (_split_model_config_default(self.provider)[1]
  153. if isinstance(self.provider, dict) else str(self.provider or ""))
  154. _img_mode = decide_image_input_mode(
  155. _img_provider.strip(), _img_model.strip(), load_config(),
  156. requested_provider=(self.requested_provider or "").strip(),
  157. )
  158. except Exception as _img_exc:
  159. logging.debug("image_routing decision failed, defaulting to text: %s", _img_exc)
  160. _img_mode = "text"
  161. if _img_mode == "native":
  162. try:
  163. _img_str_paths = [str(p) for p in images]
  164. _parts, _skipped = build_native_content_parts(text, _img_str_paths)
  165. if _skipped:
  166. _cprint(f" {_DIM}{t('cli.chat.skipped_unreadable_images', count=len(_skipped))}{_RST}")
  167. if any(p.get("type") == "image_url" for p in _parts):
  168. _img_names = ", ".join(Path(p).name for p in _img_str_paths)
  169. _cprint(f" {_DIM}{t('cli.chat.attaching_images_natively', count=len(images), names=_img_names)}{_RST}")
  170. return _parts
  171. # All images unreadable — fall back to text enrichment.
  172. except Exception as _img_exc:
  173. logging.warning("native image attach failed, falling back to text: %s", _img_exc)
  174. return self._preprocess_images_with_vision(text, images)
  175. def _chat_stage_user_message(self, agent, message):
  176. """Append the staged user dict to the transcript under the agent's persist lock."""
  177. # Copy before appending: mutating ``agent._session_messages`` in this UI-only step
  178. # would expose a duplicate-prone snapshot to terminal-close persistence.
  179. if self.conversation_history is getattr(agent, "_session_messages", None):
  180. self.conversation_history = list(self.conversation_history)
  181. # Clear the prior turn's override before exposing the new staged input: a shutdown
  182. # before the worker prologue would otherwise persist old API-local text as this message.
  183. import contextlib
  184. from agent.message_metadata import stamp_message_timestamp
  185. persist_lock = getattr(agent, "_session_persist_lock", None)
  186. with persist_lock if persist_lock is not None else contextlib.nullcontext():
  187. agent._persist_user_message_idx = None
  188. agent._persist_user_message_override = None
  189. agent._persist_user_message_timestamp = None
  190. staged_user_message = stamp_message_timestamp({"role": "user", "content": message})
  191. from tools.process_registry_notifications import TimelineNotification
  192. if isinstance(message, TimelineNotification):
  193. staged_user_message.update(content=str(message), display_kind=message.display_kind,
  194. display_metadata={"display_text": message.display_text,
  195. "notification_category": message.notification_category})
  196. agent._pending_cli_user_message = staged_user_message
  197. self.conversation_history.append(staged_user_message)
  198. def _chat_setup_turn_audio(self, turn, message, voice_input):
  199. """Arm the full-duplex listener and the streaming-TTS pipeline for this turn (voice mode only)."""
  200. from cli import _ACCENT, _RST, _STREAM_PAD, _cprint, datetime
  201. if getattr(turn, "mute_notification_reply", False):
  202. return
  203. # Continuous voice mode: arm the mic NOW (utterance-submit), not at TTS playback —
  204. # it spans generation (speech interrupts the turn) and playback (speech cuts TTS)
  205. # and disarms itself when the turn is done. See _voice_full_duplex_listener.
  206. if self._voice_mode and self._voice_continuous:
  207. self._voice_last_tts_text = ""
  208. threading.Thread(target=self._voice_full_duplex_listener, daemon=True).start()
  209. # Streaming TTS: any working provider speaks sentence-by-sentence as tokens arrive.
  210. # Availability is check_tts_requirements() ALONE: the speaker side picks its own
  211. # output route per platform (PortAudio stream where usable, tempfile -> afplay on
  212. # macOS — sounddevice is deliberately never imported for output on Darwin, see
  213. # tts_tool_speaker._device_usable), so probing it here disabled streaming TTS on
  214. # macOS via the bare except below (#84046).
  215. if self._voice_tts:
  216. try:
  217. from tools.tts_tool import check_tts_requirements
  218. from tools.tts_tool_speaker import stream_tts_to_speaker
  219. turn.use_streaming_tts = check_tts_requirements()
  220. except Exception:
  221. logging.debug("streaming TTS arm check failed", exc_info=True)
  222. if turn.use_streaming_tts:
  223. turn.text_queue = queue.Queue()
  224. turn.stop_event = threading.Event()
  225. # display_callback only when token streaming is off: with streaming on,
  226. # _stream_delta already renders the text and this would print it twice.
  227. def display_callback(sentence: str):
  228. if not turn.box_opened and getattr(self, "_modern_layout", False):
  229. from hermes_cli.cli_modern_layout import answer_label
  230. turn.box_opened = True
  231. _cprint(answer_label())
  232. if not turn.box_opened:
  233. turn.box_opened = True
  234. label = " ☤ Hermes "
  235. if self.show_timestamps:
  236. label = f"{label}{datetime.now().strftime(self.timestamp_format)} "
  237. w = self._scrollback_box_width(getattr(self.console, "width", 80))
  238. fill = w - 2 - self._status_bar_display_width(label)
  239. _cprint(f"\n{_ACCENT}╭─{label}{'─' * max(fill - 1, 0)}╮{_RST}")
  240. _cprint(f"{_STREAM_PAD}{sentence.rstrip()}")
  241. turn.tts_thread = threading.Thread(
  242. target=stream_tts_to_speaker, args=(turn.text_queue, turn.stop_event, self._voice_tts_done),
  243. kwargs={"display_callback": None if self.streaming_enabled else display_callback},
  244. daemon=True,
  245. )
  246. turn.tts_thread.start()
  247. # Barge-in paths (voice key, full-duplex listener) cut playback via this event.
  248. self._voice_tts_stop = turn.stop_event
  249. def stream_callback(delta: str):
  250. turn.text_queue.put(delta)
  251. # Track what is being spoken so a playback-phase barge capture can be
  252. # checked against it (echo guard).
  253. self._voice_last_tts_text = (self._voice_last_tts_text or "") + delta
  254. turn.stream_callback = stream_callback
  255. # API-call-local only — run_conversation persists the original clean user message.
  256. if voice_input and isinstance(message, str):
  257. turn.voice_prefix = ("[Voice input — respond concisely and conversationally, "
  258. "2-3 sentences max. No code blocks or markdown.] ")
  259. def _chat_run_agent(self, turn, message):
  260. """Agent-thread body: bind per-thread callbacks/approval key, prepend one-shot notes, run the turn."""
  261. from cli import (
  262. _prepend_note_to_message, set_approval_callback, set_secret_capture_callback,
  263. set_sudo_password_callback,
  264. )
  265. from agent.vault_backends.unlock import set_code_prompt_callback, set_save_login_prompt_callback, set_unlock_prompt_callback
  266. # terminal_tool callbacks are thread-local: run()'s registration is invisible here.
  267. set_sudo_password_callback(self._sudo_password_callback)
  268. set_approval_callback(self._approval_callback)
  269. set_secret_capture_callback(self._secret_capture_callback)
  270. set_unlock_prompt_callback(self._vault_unlock_callback)
  271. set_save_login_prompt_callback(self._vault_save_login_callback)
  272. set_code_prompt_callback(self._vault_code_callback)
  273. # Bind the approval session key so ``is_current_session_yolo_enabled()`` resolves
  274. # against the same key ``/yolo`` toggles under (``enable_session_yolo(self.session_id)``).
  275. try:
  276. from tools.approval_context import reset_current_session_key, set_current_session_key
  277. _approval_session_token = set_current_session_key(self.session_id or "default")
  278. except Exception:
  279. reset_current_session_key = None # type: ignore[assignment]
  280. _approval_session_token = None
  281. agent_message = turn.voice_prefix + message if turn.voice_prefix else message
  282. self.agent._voice_turn_pending = bool(turn.voice_prefix) # auxiliary.voice_chat route
  283. # One-shot /model and /reload-skills notes; _prepend_note_to_message also handles
  284. # multimodal content-part lists (string concat raised TypeError with an image).
  285. _taken_notes = {}
  286. for _note_attr in ("_pending_model_switch_note", "_pending_skills_reload_note"):
  287. _note = getattr(self, _note_attr, None)
  288. if _note:
  289. agent_message = _prepend_note_to_message(agent_message, _note)
  290. _taken_notes[_note_attr] = _note
  291. setattr(self, _note_attr, None)
  292. # Barged mid-speech (VAD or record key)? Tell the model it was cut off.
  293. from tools.tts_streaming import SPEECH_INTERRUPTED_NOTE, take_speech_interrupted
  294. if take_speech_interrupted():
  295. agent_message = _prepend_note_to_message(agent_message, SPEECH_INTERRUPTED_NOTE)
  296. _moa_cfg = getattr(self, "_pending_moa_config", None)
  297. self._pending_moa_config = None
  298. # Notes and voice prefix are API-local: the staged input stays the durable transcript
  299. # value so a close-path marker follows the same dict instead of a second user row.
  300. _persist_clean_user_message = message if (turn.voice_prefix or agent_message != message) else None
  301. _one_turn_model_restore = getattr(self, "_pending_one_turn_model_restore", None)
  302. self._pending_one_turn_model_restore = None
  303. # Set only by a dispatcher-spawned `-q` run; a human turn keeps today's call shape.
  304. from agent.interrupt_compat import _accepts_keyword
  305. _author = getattr(self, "_single_query_turn_author", None)
  306. _author_kwargs = ({"turn_author": _author}
  307. if _author is not None and _accepts_keyword(self.agent.run_conversation, "turn_author") else {})
  308. try:
  309. from agent.notification_presentation import notification_turn
  310. muted = getattr(turn, "mute_notification_reply", False)
  311. with notification_turn(self.agent, muted=muted, session_id=self.session_id):
  312. turn.result = self.agent.run_conversation(
  313. user_message=agent_message,
  314. conversation_history=self.conversation_history[:-1],
  315. stream_callback=None if muted else turn.stream_callback, task_id=self.session_id,
  316. persist_user_message=_persist_clean_user_message, moa_config=_moa_cfg,
  317. **_author_kwargs,
  318. )
  319. if getattr(self, "_pending_moa_disable_after_turn", False):
  320. _restore = getattr(self, "_pending_moa_restore_model", None) or {}
  321. for _key, _value in _restore.items():
  322. if _value is not None:
  323. setattr(self, _key, _value)
  324. _retire_agent(self)
  325. self._pending_moa_restore_model = None
  326. self._pending_moa_disable_after_turn = False
  327. except Exception as exc:
  328. logging.error("run_conversation raised: %s", exc, exc_info=True)
  329. _summary = getattr(self.agent, '_summarize_api_error', lambda e: str(e)[:300])(exc)
  330. from hermes_cli.cli_chat_error_copy import chat_error_response
  331. turn.result = {
  332. "final_response": chat_error_response(
  333. exc, provider=str(getattr(self.agent, "provider", "") or self.provider or ""),
  334. model=str(getattr(self.agent, "model", "") or self.model or "")),
  335. "messages": [], "api_calls": 0,
  336. "completed": False, "failed": True, "error": _summary,
  337. }
  338. finally:
  339. _unadmitted = (turn.result or {}).get("admitted") is False
  340. if _unadmitted: # refused before the submit: the resend still owes the notes
  341. for _note_attr, _note in _taken_notes.items():
  342. setattr(self, _note_attr, _note)
  343. if _one_turn_model_restore and (_unadmitted or (turn.result or {}).get("failure_reason") == "session_busy"):
  344. # The owner admitted nothing: the resend is still the one turn the pick was for.
  345. self._pending_one_turn_model_restore = _one_turn_model_restore
  346. elif _one_turn_model_restore:
  347. self._restore_model_runtime_snapshot(_one_turn_model_restore)
  348. # Credit notices paint cleanly above the prompt here, not behind streamed output.
  349. self._flush_credit_notices()
  350. # A reused thread must never hold stale references to a disposed CLI instance.
  351. try:
  352. set_sudo_password_callback(None)
  353. set_approval_callback(None)
  354. set_secret_capture_callback(None)
  355. set_unlock_prompt_callback(None)
  356. set_save_login_prompt_callback(None)
  357. set_code_prompt_callback(None)
  358. except Exception:
  359. pass
  360. # Unbind the per-turn key; ``_session_yolo`` state itself persists across turns.
  361. if _approval_session_token is not None and reset_current_session_key is not None:
  362. try:
  363. reset_current_session_key(_approval_session_token)
  364. except Exception:
  365. pass
  366. def _chat_monitor_agent_thread(self, turn, agent_thread):
  367. """Poll the interrupt queue while the agent thread runs; returns the interrupting message (or None)."""
  368. from cli import _cprint, _hermes_home, logger
  369. # Ambient "thinking" blips in voice mode; skipped per-blip while TTS speaks, the mic
  370. # records or a barge capture is live. voice.thinking_sound gates it (default on).
  371. if self._voice_mode:
  372. try:
  373. from tools.voice_mode import start_thinking_sound
  374. turn.thinking_started = start_thinking_sound(should_play=lambda: (
  375. self._voice_tts_done.is_set() and not self._voice_recording
  376. and not self._voice_barge_capture.is_set()))
  377. except Exception:
  378. turn.thinking_started = False
  379. interrupt_msg = None
  380. while agent_thread.is_alive():
  381. try:
  382. interrupt_msg = self._interrupt_queue.get(timeout=0.1)
  383. except queue.Empty:
  384. # Flush the StdoutProxy buffer: it otherwise only flushes on input-triggered
  385. # renderer passes, so on macOS the CLI looks frozen until the user types.
  386. # Force prompt_toolkit to flush any pending stdout output from the agent thread. (#1624)
  387. self._invalidate(min_interval=0.15)
  388. continue
  389. if not interrupt_msg:
  390. continue
  391. # With a clarify question active, Enter routes to the clarify queue; anything
  392. # landing here is a race — don't interrupt, park it as the next turn.
  393. if self._clarify_state or self._clarify_freetext:
  394. try:
  395. self._pending_input.put(interrupt_msg)
  396. except Exception:
  397. pass
  398. interrupt_msg = None
  399. continue
  400. _cprint(f"\n{t('cli.chat.new_message_interrupting')}")
  401. if turn.stop_event is not None:
  402. turn.stop_event.set()
  403. self.agent.interrupt(interrupt_msg)
  404. # Modal prompts gate input until reset — otherwise the CLI freezes after an
  405. # interrupt until the prompt's own timeout.
  406. self._clear_active_overlays_for_interrupt()
  407. # Debug log to file (stdout may be devnull under redirect_stdout).
  408. try:
  409. with open(_hermes_home / "interrupt_debug.log", "a", encoding="utf-8") as _f:
  410. _f.write(f"{time.strftime('%H:%M:%S')} interrupt fired: msg={str(interrupt_msg)[:60]!r}, "
  411. f"children={len(self.agent._active_children)}, "
  412. f"parent._interrupt={self.agent._interrupt_requested}\n")
  413. _f.writelines(f" child[{_ci}]._interrupt={_ch._interrupt_requested}\n" for _ci, _ch in enumerate(self.agent._active_children))
  414. except Exception:
  415. pass
  416. break
  417. if interrupt_msg is not None:
  418. # After an interrupt the agent may take seconds to clean up (kill
  419. # subprocess, persist). Poll instead of a blocking join so another
  420. # interrupt (Ctrl+C sets _should_exit) or a stuck agent can't freeze
  421. # us; the thread is daemon and dies on process exit regardless.
  422. for _ in range(50): # 50 * 0.2s = 10s max
  423. agent_thread.join(timeout=0.2)
  424. if not agent_thread.is_alive() or self._should_exit:
  425. break
  426. if agent_thread.is_alive():
  427. logger.warning(
  428. "Agent thread still alive after interrupt "
  429. "(thread %s). Daemon thread will be cleaned up "
  430. "on exit.",
  431. agent_thread.ident,
  432. )
  433. else:
  434. agent_thread.join(timeout=30) # should be done already; guard edge cases
  435. return interrupt_msg
  436. def _chat_settle_turn(self, turn):
  437. """After the agent thread ends: freeze timers, flush streams, drain TTS, sync history/session id."""
  438. if self._prompt_start_time is not None:
  439. self._prompt_duration = max(0.0, time.time() - self._prompt_start_time)
  440. self._prompt_start_time = None
  441. self._last_turn_finished_at = time.time() # status bar idle time
  442. self._last_turn_result = turn.result
  443. # AsyncOpenAI clients bound to the worker's now-closed loop would crash
  444. # prompt_toolkit's loop from __del__ on GC.
  445. try:
  446. from agent.auxiliary_client import cleanup_stale_async_clients
  447. cleanup_stale_async_clients()
  448. except Exception:
  449. pass
  450. self._flush_stream()
  451. if turn.use_streaming_tts and turn.text_queue is not None:
  452. turn.text_queue.put(None) # end-of-text sentinel
  453. if turn.tts_thread is not None:
  454. turn.tts_thread.join(timeout=120)
  455. # A timed-out join leaves tts_normal_exit False so the release path's
  456. # stop_event kills the runaway worker.
  457. turn.tts_normal_exit = not turn.tts_thread.is_alive()
  458. # Drain the StdoutProxy buffer so tool/status lines render ABOVE the response
  459. # box; the sleep lets the renderer paint before we draw.
  460. sys.stdout.flush()
  461. time.sleep(0.15)
  462. if turn.result:
  463. self.conversation_history = turn.result.get("messages", self.conversation_history)
  464. # Mid-turn auto-compression continues in a child session: sync so /status, /resume,
  465. # titling and the exit summary target the live child, not the ended parent.
  466. if (self.agent and getattr(self.agent, "session_id", None)
  467. and self.agent.session_id != self.session_id):
  468. from tools.approval_yolo import transfer_session_yolo
  469. transfer_session_yolo(self.session_id, self.agent.session_id)
  470. self.session_id = self.agent.session_id
  471. self._write_terminal_breadcrumb()
  472. self._pending_title = None
  473. def _handle_discard_command(self, cmd_original: str):
  474. """/discard <admission_id>: acknowledge a turn the gateway lost in a restart without replaying
  475. it (the owner's fenced ``prompt.resolve_unknown``), so prompts may run again."""
  476. from cli import _cprint
  477. from hermes_cli.gateway_chat_view import lost_admission
  478. from hermes_cli.gateway_client import GatewayClientError
  479. from hermes_cli.gateway_side import connection_for
  480. admission_id = cmd_original.partition(" ")[2].strip()
  481. if not admission_id:
  482. _cprint(" Usage: /discard <admission_id>")
  483. return
  484. connection = connection_for(self)
  485. if connection is None:
  486. _cprint(" /discard: no gateway session is open")
  487. return
  488. try:
  489. lost = lost_admission(connection.reattach(self.session_id), admission_id)
  490. connection.call("prompt.resolve_unknown", session_id=self.session_id,
  491. admission_id=lost["admission_id"], execution_generation=lost["execution_generation"])
  492. # The cached pending list the next prompt checks: any other lost turn still refuses it.
  493. snapshot = connection.reattach(self.session_id)
  494. except GatewayClientError as exc:
  495. _cprint(f" /discard: {exc}")
  496. return
  497. left = sum(row["status"] == "unknown" for row in snapshot.get("pending", []))
  498. _cprint(f" Discarded {lost['admission_id']}; it was not re-run. "
  499. + (f"{left} more paused turn{'s' if left > 1 else ''} to /discard first." if left
  500. else "You can send messages again."))
  501. def _chat_render_turn(self, turn, agent_thread, interrupt_msg):
  502. """Post-turn display: errors, interrupt marker, reasoning/response panels, bell, re-queues.
  503. Returns the response text.
  504. """
  505. from cli import _DIM, _RST, _cprint, _suspend_output_history
  506. response = turn.result.get("final_response", "") if turn.result else ""
  507. if getattr(turn, "mute_notification_reply", False):
  508. pending, _ = self._chat_resolve_interrupt(turn, agent_thread, interrupt_msg, response)
  509. if pending:
  510. self._pending_input.put(pending)
  511. return None
  512. # "failed"/"partial" with an empty final_response: no usable answer.
  513. if turn.result and (turn.result.get("failed") or turn.result.get("partial")) and not response:
  514. from hermes_cli.cli_chat_error_copy import chat_error_response
  515. response = chat_error_response(
  516. str(turn.result.get("error") or t("cli.chat.unknown_error")),
  517. provider=str(getattr(self.agent, "provider", "") or self.provider or ""),
  518. model=str(getattr(self.agent, "model", "") or self.model or ""),
  519. failure_reason=turn.result.get("failure_reason"))
  520. # Stop continuous voice on persistent errors (e.g. 429) — else error→record→error loops.
  521. if self._voice_continuous:
  522. self._voice_continuous = False
  523. _cprint(f"\n{_DIM}{t('cli.chat.continuous_voice_stopped')}{_RST}")
  524. pending_message, _show_interrupt_marker = self._chat_resolve_interrupt(
  525. turn, agent_thread, interrupt_msg, response)
  526. self._chat_print_reasoning_box(turn)
  527. self._chat_print_response_panel(turn, response)
  528. # History suppressed so the marker is never recorded in _OUTPUT_HISTORY
  529. # (appending it to `response` duplicated it on redraw).
  530. if _show_interrupt_marker:
  531. with _suspend_output_history():
  532. _cprint(f"\n{_DIM}{t('cli.chat.interrupted_marker')}{_RST}")
  533. # Focus view: "⋯ N tool lines hidden" after the answer; resets the counter.
  534. try:
  535. self._emit_focus_recovery_line()
  536. except Exception:
  537. pass
  538. self._ring_bell(context=t("cli.modal.bell_turn_complete")) # propagates over SSH
  539. if turn.result and not turn.result.get("completed") and not turn.result.get("interrupted"):
  540. _api_calls = turn.result.get("api_calls", 0)
  541. _max_iter = getattr(self.agent, "max_iterations", 500)
  542. if _api_calls >= _max_iter:
  543. from gateway.warning_notifications import render_notification
  544. render_notification(
  545. lambda: _cprint(
  546. f"\n{_DIM}{t('cli.chat.iteration_budget_reached', used=_api_calls, max=_max_iter)}{_RST}"
  547. ),
  548. platform="cli", user_config=getattr(self.agent, "_notification_config", None))
  549. # Batch TTS unless streaming TTS already spoke the response.
  550. if self._voice_tts and response and not turn.use_streaming_tts:
  551. self._voice_speak_response_async(response)
  552. # Re-queue the interrupt message (plus any that arrived meanwhile) as the next
  553. # prompt. Only reached in busy_input_mode == "interrupt"; "queue" mode routes
  554. # Enter straight to _pending_input.
  555. if pending_message:
  556. all_parts = [pending_message]
  557. while not self._interrupt_queue.empty():
  558. try:
  559. extra = self._interrupt_queue.get_nowait()
  560. if extra:
  561. all_parts.append(extra)
  562. except queue.Empty:
  563. break
  564. # Payloads may be (text, images) tuples when the message carried image
  565. # attachments (bundled at cli_tui_mixin._tui_on_enter); unpack them here —
  566. # "\n".join(all_parts) raises TypeError on a tuple and the outer
  567. # handler swallows it, silently dropping the interrupt (#110737).
  568. text_parts: list[str] = []
  569. image_parts: list = []
  570. for part in all_parts:
  571. if isinstance(part, tuple):
  572. part_text, part_images = part
  573. text_parts.append(part_text)
  574. image_parts.extend(part_images or [])
  575. else:
  576. text_parts.append(part)
  577. combined = "\n".join(text_parts)
  578. payload = (combined, image_parts) if image_parts else combined
  579. preview = combined[:50] + ("..." if len(combined) > 50 else "")
  580. if len(all_parts) > 1:
  581. _cprint(f"\n{t('cli.chat.sending_after_interrupt_multi', count=len(all_parts), preview=preview)}")
  582. else:
  583. _cprint(f"\n{t('cli.chat.sending_after_interrupt', preview=preview)}")
  584. self._pending_input.put(payload)
  585. # A /steer the agent finished before absorbing becomes the next user turn.
  586. _leftover_steer = turn.result.get("pending_steer") if turn.result else None
  587. if _leftover_steer:
  588. preview = _leftover_steer[:60] + ("..." if len(_leftover_steer) > 60 else "")
  589. _cprint(f"\n{t('cli.chat.delivering_leftover_steer', preview=preview)}")
  590. self._pending_input.put(_leftover_steer)
  591. return response
  592. def _chat_resolve_interrupt(self, turn, agent_thread, interrupt_msg, response):
  593. """Return ``(pending_message, show_marker)``; clears a stale agent interrupt flag.
  594. The marker is printed separately after the response Panel (history suppressed)
  595. so a terminal redraw never duplicates it.
  596. """
  597. pending_message = None
  598. _show_interrupt_marker = False
  599. _interrupted_this_turn = bool(turn.result and turn.result.get("interrupted"))
  600. # Post-turn hooks (e.g. goal continuation) skip themselves on a user-cancelled turn.
  601. self._last_turn_interrupted = _interrupted_this_turn
  602. # ... and on a failed one (a refused turn, a provider error): nothing to judge.
  603. self._last_turn_failed = bool(turn.result and turn.result.get("failed"))
  604. if _interrupted_this_turn:
  605. pending_message = turn.result.get("interrupt_message") or interrupt_msg
  606. _show_interrupt_marker = bool(response and pending_message)
  607. elif interrupt_msg:
  608. # agent.interrupt() fired but the result doesn't acknowledge it (racy): either
  609. # the thread had passed its last interrupt check so finalize_turn() never saw
  610. # the flag, or the 10s post-interrupt wait expired and `result` is None. The
  611. # user's message must NOT be dropped — re-queue it as the next turn.
  612. pending_message = interrupt_msg
  613. # An interrupt landing after finalize_turn()'s clear_interrupt() leaves a stale
  614. # flag that would abort the NEXT turn at its first check. Clear it — but ONLY if
  615. # the thread exited: on an abandoned thread the flag is what eventually unwinds
  616. # the wedged tool.
  617. try:
  618. if (not agent_thread.is_alive() and self.agent
  619. and getattr(self.agent, "_interrupt_requested", False)):
  620. self.agent.clear_interrupt()
  621. except Exception:
  622. pass
  623. return pending_message, _show_interrupt_marker
  624. def _chat_print_reasoning_box(self, turn):
  625. """Collapsed reasoning box when show_reasoning is on and streaming did not already show it."""
  626. from cli import _DIM, _RST, _cprint
  627. # _reasoning_shown_this_turn, not _reasoning_stream_started: the latter resets at
  628. # intermediate turn boundaries (tool loops) and re-rendered the box after the answer.
  629. if self.show_reasoning and turn.result and not self._reasoning_shown_this_turn:
  630. reasoning = turn.result.get("last_reasoning")
  631. if reasoning and getattr(self, "_modern_layout", False):
  632. from hermes_cli.cli_modern_layout import thought_rows
  633. for line in thought_rows(None, reasoning, self.reasoning_full):
  634. _cprint(line)
  635. elif reasoning:
  636. w = self._scrollback_box_width()
  637. r_label = f" {t('cli.chat.reasoning_label')} "
  638. r_top = f"{_DIM}┌─{r_label}{'─' * max(w - 3 - len(r_label), 0)}┐{_RST}"
  639. r_bot = f"{_DIM}└{'─' * (w - 2)}┘{_RST}"
  640. # First 10 lines unless the user opted into /reasoning full.
  641. lines = reasoning.strip().splitlines()
  642. if len(lines) > 10 and not self.reasoning_full:
  643. display_reasoning = "\n".join(lines[:10])
  644. display_reasoning += f"\n{_DIM} {t('cli.chat.reasoning_more_lines', count=len(lines) - 10)}{_RST}"
  645. else:
  646. display_reasoning = reasoning.strip()
  647. _cprint(f"\n{r_top}\n{_DIM}{display_reasoning}{_RST}\n{r_bot}")
  648. def _chat_print_response_panel(self, turn, response):
  649. """Response box (close TTS-drawn box / post-stream transform / Rich Panel), then billing CTA."""
  650. from cli import (
  651. ChatConsole, _ACCENT, _RST, _cprint, _maybe_remap_for_light_mode, _post_stream_transform_output,
  652. _render_final_assistant_content,
  653. )
  654. if response and not (turn.result and turn.result.get("response_previewed", False)):
  655. try:
  656. from hermes_cli.skin_engine import get_active_skin
  657. _skin = get_active_skin()
  658. label = _skin.get_branding("response_label", "☤ Hermes")
  659. _resp_color = _maybe_remap_for_light_mode(_skin.get_color("response_border", "#CD7F32"))
  660. _resp_text = _maybe_remap_for_light_mode(_skin.get_color("banner_text", "#FFF8DC"))
  661. except Exception:
  662. label = "☤ Hermes"
  663. _resp_color = _maybe_remap_for_light_mode("#CD7F32")
  664. _resp_text = _maybe_remap_for_light_mode("#FFF8DC")
  665. is_error_response = turn.result and (turn.result.get("failed") or turn.result.get("partial"))
  666. # An interrupted reply that streamed before a tool-call boundary reset the segment
  667. # state is already on screen (#65666). Only suppress when the response IS that text:
  668. # unstreamed interrupt/status messages and completed replies still get their Panel.
  669. _interrupted_streamed = bool(
  670. self._last_turn_interrupted and response.strip()
  671. and " ".join(response.split()) in " ".join(self._streamed_text_this_turn.split())
  672. )
  673. already_streamed = (
  674. (self._stream_started and self._stream_box_opened) or _interrupted_streamed
  675. ) and not is_error_response
  676. modern = getattr(self, "_modern_layout", False)
  677. # A gateway refusal is Hermes's own copy (quoted user text, a /discard line to copy):
  678. # markdown stripping or rendering would rewrite the quote and merge the lines.
  679. mode = ("raw" if turn.result and turn.result.get("failure_reason") == "gateway_connection"
  680. else self.final_response_markdown)
  681. if turn.use_streaming_tts and turn.box_opened and not is_error_response:
  682. # Text already printed sentence-by-sentence; just close the box.
  683. if modern:
  684. from hermes_cli.cli_modern_layout import closing_separator
  685. _cprint(closing_separator(True))
  686. else:
  687. _cprint(f"\n{_ACCENT}╰{'─' * (self._scrollback_box_width() - 2)}╯{_RST}")
  688. elif already_streamed:
  689. # _flush_stream() already closed the streamed box; a post-stream transform
  690. # hook shows a suffix for append-only changes, else the full replacement.
  691. _post_stream_text = _post_stream_transform_output(response, turn.result)
  692. if _post_stream_text.strip():
  693. _cprint(_post_stream_text)
  694. elif modern:
  695. from hermes_cli.cli_modern_layout import answer_label, closing_separator
  696. _cprint(answer_label())
  697. ChatConsole().print(_render_final_assistant_content(response, mode=mode),
  698. style=_resp_text, soft_wrap=True) # terminal wraps; resize re-flows
  699. _cprint(closing_separator(True))
  700. else:
  701. ChatConsole().print(Panel(
  702. _render_final_assistant_content(response, mode=mode),
  703. title=f"[{_resp_color} bold]{label}[/]", title_align="left", border_style=_resp_color,
  704. style=_resp_text, box=rich_box.HORIZONTALS, padding=(1, 0),
  705. width=self._scrollback_box_width(),
  706. ))
  707. # Billing CTA pins the single action (Nous → /topup, others → billing page) so it
  708. # stays visible instead of scrolling away inside the response prose.
  709. _limit = (turn.result or {}).get("usage_limit") if turn.result else None
  710. if isinstance(_limit, dict):
  711. # Usage-window plan: what to do next, in commands that always work here. Hermes never
  712. # switches on its own; the user picks.
  713. try:
  714. from agent.plan_limits import limit_next_steps_from_payload
  715. ChatConsole().print(Panel(
  716. limit_next_steps_from_payload(_limit, markup=True),
  717. title="[#CD7F32 bold]⚡ Limit reached[/]", title_align="left", border_style="#CD7F32",
  718. box=rich_box.HORIZONTALS, padding=(1, 4), width=self._scrollback_box_width(),
  719. ))
  720. except Exception:
  721. logging.getLogger(__name__).debug("Usage-limit panel rendering failed", exc_info=True)
  722. elif (turn.result and turn.result.get("failure_reason") == "billing"
  723. and not turn.result.get("usage_window_message")):
  724. # (A window plan's refusal already reads as its own sentence with the way on; an
  725. # "Out of credits" box under it would contradict it.)
  726. _bb = turn.result.get("billing_block") or {}
  727. if _bb.get("is_nous"):
  728. _cta_lines = [t("cli.chat.billing_cta_nous")]
  729. else:
  730. _url = _bb.get("billing_url")
  731. _cta_lines = [t("cli.chat.billing_cta_provider",
  732. provider=_bb.get("provider_label") or t("cli.chat.your_provider"))
  733. + (f": [bold]{_url}[/]" if _url else ".")]
  734. _cta_lines.append(t("cli.chat.billing_cta_switch"))
  735. try:
  736. ChatConsole().print(Panel(
  737. "\n".join(_cta_lines), title=f"[#CD7F32 bold]{t('cli.chat.out_of_credits_title')}[/]",
  738. title_align="left", border_style="#CD7F32", box=rich_box.HORIZONTALS,
  739. padding=(1, 4), width=self._scrollback_box_width(),
  740. ))
  741. except Exception:
  742. pass