cli_stream_mixin.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. """Streaming output, reasoning preview, tool progress callbacks, and busy-command spinner for the interactive CLI
  2. Mixin split out of ``cli.py``; bound onto ``HermesCLI`` via the MRO. cli.py-internal
  3. symbols are imported LAZILY inside each method (``from cli import ...``) — the mixin
  4. never imports ``cli`` at module load time (import cycle).
  5. """
  6. from __future__ import annotations
  7. import json
  8. import re
  9. import shutil
  10. import textwrap
  11. import time
  12. from contextlib import contextmanager
  13. from pathlib import Path
  14. from rich.markup import escape as _escape
  15. from agent.i18n import t
  16. from agent.think_scrubber import THINK_CLOSE_TAGS, THINK_OPEN_TAGS
  17. # Model-generated reasoning tags: suppressed during streaming (they'd display as raw XML;
  18. # the agent strips them from final_response too) unless show_reasoning routes them to the box.
  19. _OPEN_TAGS = THINK_OPEN_TAGS
  20. _CLOSE_TAGS = THINK_CLOSE_TAGS
  21. _MAX_CLOSE_TAG_LEN = max(len(t) for t in _CLOSE_TAGS)
  22. # Ordered (prefix, catalog key) rows for _slow_command_status — first match wins; the key is
  23. # resolved through t() at call time so the active language applies.
  24. _SLOW_COMMAND_STATUS = (
  25. ("/skills search", "cli.stream.busy_skills_search"), ("/skills browse", "cli.stream.busy_skills_browse"),
  26. ("/skills inspect", "cli.stream.busy_skills_inspect"), ("/skills install", "cli.stream.busy_skills_install"),
  27. ("/skills", "cli.stream.busy_skills"), ("/browser", "cli.stream.busy_browser"))
  28. _SLOW_COMMAND_STATUS_EXACT = {
  29. "/reload-mcp": "cli.stream.busy_reload_mcp",
  30. "/reload-skills": "cli.stream.busy_reload_skills",
  31. "/reload_skills": "cli.stream.busy_reload_skills"}
  32. def _thinking_prefix() -> str:
  33. """`` [thinking] `` — the reasoning-preview label; also measured for wrap width."""
  34. return f" {t('cli.stream.thinking_label')} "
  35. def _terminal_columns(default: int = 80) -> int:
  36. try:
  37. return shutil.get_terminal_size().columns
  38. except Exception:
  39. return default
  40. class CLIStreamMixin:
  41. """Streaming output, reasoning preview, tool progress callbacks, and busy-command spinner for the interactive CLI"""
  42. def _on_thinking(self, text: str) -> None:
  43. """Called by agent when thinking starts/stops. Updates TUI spinner."""
  44. if getattr(getattr(self, "agent", None), "_mute_notification_reply", False):
  45. return
  46. from gateway.warning_notifications import DiagnosticText, render_notification
  47. def show():
  48. if not text:
  49. self._flush_reasoning_preview(force=True)
  50. self._spinner_text = text or ""
  51. self._tool_start_time = 0.0 # clear tool timer when switching to thinking
  52. self._invalidate()
  53. render_notification(show, platform="cli", diagnostic=isinstance(text, DiagnosticText),
  54. user_config=getattr(getattr(self, "agent", None), "_notification_config", None))
  55. def _on_notice(self, notice) -> None:
  56. """Queue an out-of-band AgentNotice for rendering at the next clean boundary.
  57. Notices fire mid-turn (cold-start seed, per-turn _capture_credits); printing immediately
  58. races the stream and buries the line behind the prompt. Flushed by _flush_credit_notices()
  59. after run_conversation returns. Fail-soft.
  60. """
  61. try:
  62. text = getattr(notice, "text", "") or ""
  63. if getattr(getattr(self, "agent", None), "_mute_notification_reply", False):
  64. return
  65. if not text:
  66. return
  67. level = getattr(notice, "level", "info") or "info"
  68. from gateway.warning_notifications import is_diagnostic_notice, render_notification
  69. def queue_notice():
  70. if not hasattr(self, "_pending_credit_notices"):
  71. self._pending_credit_notices = []
  72. self._pending_credit_notices.append((level, text))
  73. render_notification(queue_notice, platform="cli", diagnostic=is_diagnostic_notice(notice),
  74. user_config=getattr(getattr(self, "agent", None), "_notification_config", None))
  75. except Exception:
  76. pass
  77. def _flush_credit_notices(self) -> None:
  78. """Print queued credit notices as level-colored lines at turn end (after
  79. run_conversation) where _cprint paints cleanly above the prompt."""
  80. from cli import _DIM, _RST, _cprint
  81. try:
  82. pending = getattr(self, "_pending_credit_notices", None)
  83. if not pending:
  84. return
  85. self._pending_credit_notices = []
  86. colors = {"error": "\033[31m", "warn": "\033[33m", "success": "\033[32m", "info": _DIM}
  87. for level, text in pending:
  88. _cprint(f" {colors.get(level, _DIM)}{text}{_RST}")
  89. except Exception:
  90. pass
  91. def _on_notice_clear(self, key: str) -> None:
  92. """No-op for the REPL (lines are printed, no persistent slot to wipe); kept so the
  93. agent's clear callback is bound symmetrically with the show callback."""
  94. return
  95. def _current_reasoning_callback(self):
  96. """Return the active reasoning display callback for the current mode."""
  97. if self.show_reasoning and self.streaming_enabled:
  98. return self._stream_reasoning_delta
  99. if self.verbose and not self.show_reasoning:
  100. return self._on_reasoning
  101. return None
  102. def _emit_reasoning_preview(self, reasoning_text: str) -> None:
  103. """Render a buffered reasoning preview as a single [thinking] block."""
  104. from cli import _DIM, _RST, _cprint
  105. preview_text = reasoning_text.strip()
  106. if not preview_text:
  107. return
  108. wrap_width = max(30, _terminal_columns() - len(_thinking_prefix()) - 2)
  109. paragraphs = []
  110. for paragraph in re.split(r"\n\s*\n+", preview_text.replace("\r\n", "\n")):
  111. compact = " ".join(line.strip() for line in paragraph.splitlines() if line.strip())
  112. if compact:
  113. paragraphs.append(textwrap.fill(compact, width=wrap_width))
  114. preview_text = "\n".join(paragraphs)
  115. if not preview_text:
  116. return
  117. if self.verbose:
  118. _cprint(f"{_DIM}{_thinking_prefix()}{preview_text}{_RST}")
  119. return
  120. lines = preview_text.splitlines()
  121. if len(lines) > 5:
  122. preview = "\n".join(lines[:5]) + f"\n {t('cli.stream.thinking_more_lines', count=len(lines) - 5)}"
  123. else:
  124. preview = preview_text
  125. _cprint(f"{_DIM}{_thinking_prefix()}{preview}{_RST}")
  126. def _flush_reasoning_preview(self, *, force: bool = False) -> None:
  127. """Flush buffered reasoning text at natural boundaries.
  128. Some providers stream reasoning in tiny word/punctuation chunks; buffering keeps the
  129. preview path from printing one `[thinking]` line per token.
  130. """
  131. buf = getattr(self, "_reasoning_preview_buf", "")
  132. if not buf:
  133. return
  134. target_width = max(40, _terminal_columns() - len(_thinking_prefix()) - 4)
  135. flush_text = ""
  136. if force:
  137. flush_text, buf = buf, ""
  138. else:
  139. line_break = buf.rfind("\n")
  140. min_newline_flush = max(16, target_width // 3)
  141. if line_break != -1 and (
  142. line_break >= min_newline_flush
  143. or buf.endswith(("\n\n", ".\n", "!\n", "?\n", ":\n"))):
  144. flush_text, buf = buf[: line_break + 1], buf[line_break + 1 :]
  145. elif len(buf) >= target_width:
  146. search_start = max(20, target_width // 2)
  147. search_end = min(
  148. len(buf), max(target_width + (target_width // 3), target_width + 8))
  149. cut = max(
  150. buf.rfind(b, search_start, search_end)
  151. for b in (" ", "\t", ".", "!", "?", ",", ";", ":"))
  152. if cut != -1:
  153. flush_text, buf = buf[: cut + 1], buf[cut + 1 :]
  154. self._reasoning_preview_buf = buf.lstrip() if flush_text else buf
  155. if flush_text:
  156. self._emit_reasoning_preview(flush_text)
  157. def _format_submitted_user_message_preview(self, user_input: str) -> str:
  158. """Format the submitted user-message scrollback preview."""
  159. from cli import _accent_hex, datetime
  160. ts_suffix = (
  161. f" [dim]{datetime.now().strftime(getattr(self, 'timestamp_format', '%H:%M'))}[/]"
  162. if getattr(self, "show_timestamps", False) else "")
  163. lines = user_input.split("\n")
  164. from hermes_cli.cli_modern_layout import is_modern, prompt_glyph
  165. mark = _escape(prompt_glyph()) if is_modern(self) else "●"
  166. if len(lines) <= 1:
  167. return f"[bold {_accent_hex()}]{mark}[/] [bold]{_escape(user_input)}[/]{ts_suffix}"
  168. first_lines = max(1, int(getattr(self, "user_message_preview_first_lines", 2)))
  169. last_lines = max(0, int(getattr(self, "user_message_preview_last_lines", 2)))
  170. head = lines[:first_lines]
  171. tail_count = min(last_lines, max(0, len(lines) - len(head)))
  172. tail = lines[-tail_count:] if tail_count else []
  173. hidden_middle_count = len(lines) - len(head) - len(tail)
  174. if hidden_middle_count < 0:
  175. hidden_middle_count = 0
  176. tail = []
  177. preview_lines = [f"[bold {_accent_hex()}]{mark}[/] [bold]{_escape(head[0])}[/]{ts_suffix}"]
  178. preview_lines.extend(f"[bold]{_escape(line)}[/]" for line in head[1:])
  179. if hidden_middle_count > 0:
  180. key = "cli.stream.preview_more_lines_one" if hidden_middle_count == 1 else "cli.stream.preview_more_lines_other"
  181. preview_lines.append(f"[dim]{_escape(t(key, count=hidden_middle_count))}[/]")
  182. preview_lines.extend(f"[bold]{_escape(line)}[/]" for line in tail)
  183. return "\n".join(preview_lines)
  184. def _expand_paste_references(self, text: str | None) -> str:
  185. """Expand [Pasted text #N -> file] placeholders into file contents."""
  186. from cli import logger
  187. if not isinstance(text, str) or "[Pasted text #" not in text:
  188. return text or ""
  189. paste_ref_re = re.compile(r'\[Pasted text #\d+: \d+ lines \u2192 (.+?)\]')
  190. def _expand_ref(match):
  191. path = Path(match.group(1))
  192. # try/except rather than path.exists(): the paste file may be deleted between
  193. # check and read (TOCTOU), silently dropping the input.
  194. try:
  195. # See #17666.
  196. return path.read_text(encoding="utf-8-sig")
  197. except OSError:
  198. logger.warning("Paste file gone or unreadable, returning placeholder: %s", path)
  199. return match.group(0)
  200. return paste_ref_re.sub(_expand_ref, text)
  201. def _print_user_message_preview(self, user_input: str) -> None:
  202. """Render a user message using the normal chat scrollback style."""
  203. from cli import ChatConsole, _accent_hex, _cprint
  204. from tools.process_registry_notifications import TimelineNotification
  205. if isinstance(user_input, TimelineNotification):
  206. from gateway.warning_notifications import render_notification
  207. render_notification(lambda: ChatConsole().print(f"[dim]◈ {_escape(user_input.display_text)}[/dim]"),
  208. platform="cli", diagnostic=user_input.notification_category == "diagnostic")
  209. return
  210. modern = getattr(self, "_modern_layout", False)
  211. if modern:
  212. from hermes_cli.cli_modern_layout import separator
  213. _cprint("\n" + separator(True))
  214. else:
  215. ChatConsole().print(f"[{_accent_hex()}]{'─' * 40}[/]")
  216. text = str(user_input or "")
  217. if "\n" in text:
  218. # soft_wrap in the glyph layout: the terminal wraps a long prompt, so a resize re-flows it.
  219. ChatConsole().print(self._format_submitted_user_message_preview(text), soft_wrap=modern)
  220. elif modern:
  221. from hermes_cli.cli_modern_layout import user_echo_markup
  222. ChatConsole().print(user_echo_markup(text, _accent_hex()), soft_wrap=True)
  223. else:
  224. ChatConsole().print(f"[bold {_accent_hex()}]●[/] [bold]{_escape(text)}[/]")
  225. if modern:
  226. from hermes_cli.cli_modern_layout import closing_separator
  227. _cprint(closing_separator(True))
  228. def _stream_reasoning_delta(self, text: str) -> None:
  229. """Stream reasoning tokens into a dim box above the response.
  230. Opened on the first token, closed when content arrives (_emit_stream_text). Once the
  231. response box is open further reasoning is suppressed — a late thinking block (e.g. after
  232. an interrupt) would otherwise draw a reasoning box inside the response box.
  233. """
  234. from cli import _DIM, _RST, _cprint
  235. if not text:
  236. return
  237. self._reasoning_shown_this_turn = True
  238. if getattr(self, "_stream_box_opened", False):
  239. return
  240. if getattr(self, "_modern_layout", False):
  241. from hermes_cli.cli_modern_layout import reasoning_text_line, separator
  242. if not getattr(self, "_reasoning_box_opened", False):
  243. self._reasoning_box_opened, self._reasoning_started_at = True, time.monotonic()
  244. self._reasoning_buf = ""
  245. _cprint("\n" + separator(False))
  246. self._reasoning_buf = getattr(self, "_reasoning_buf", "") + text
  247. # Live, as classic: complete lines now, long partial lines force-flushed.
  248. while "\n" in self._reasoning_buf:
  249. line, self._reasoning_buf = self._reasoning_buf.split("\n", 1)
  250. _cprint(reasoning_text_line(line))
  251. if len(self._reasoning_buf) > 80:
  252. _cprint(reasoning_text_line(self._reasoning_buf))
  253. self._reasoning_buf = ""
  254. return
  255. if not getattr(self, "_reasoning_box_opened", False):
  256. self._reasoning_box_opened = True
  257. w = self._scrollback_box_width()
  258. r_label = f" {t('cli.chat.reasoning_label')} "
  259. r_fill = w - 2 - len(r_label)
  260. _cprint(f"\n{_DIM}┌─{r_label}{'─' * max(r_fill - 1, 0)}┐{_RST}")
  261. self._reasoning_buf = getattr(self, "_reasoning_buf", "") + text
  262. # Emit complete lines; force-flush long partial lines so reasoning is visible in
  263. # real-time even without newlines.
  264. while "\n" in self._reasoning_buf:
  265. line, self._reasoning_buf = self._reasoning_buf.split("\n", 1)
  266. _cprint(f"{_DIM}{line}{_RST}")
  267. if len(self._reasoning_buf) > 80:
  268. _cprint(f"{_DIM}{self._reasoning_buf}{_RST}")
  269. self._reasoning_buf = ""
  270. def _agent_status_print(self, *args, **kwargs) -> None:
  271. """``agent._print_fn`` for the interactive CLI: agent status lines (subagent completion ``✓ [set n · i/N]``,
  272. background-process notices, spinner ``print_above`` text) arrive from other threads at any moment. While
  273. a response or reasoning box is being streamed they are HELD and released at the box footer, so a line
  274. never lands between two paragraphs of the reply. Outside a box they print immediately."""
  275. from cli import _cprint
  276. text = kwargs.get("sep", " ").join(str(a) for a in args)
  277. if getattr(self, "_stream_box_live", False) or getattr(self, "_reasoning_box_opened", False):
  278. self._held_status_lines = getattr(self, "_held_status_lines", []) + [text]
  279. return
  280. _cprint(text)
  281. def _release_held_status_lines(self) -> None:
  282. """Print status lines held while a box was open (called right after a box footer)."""
  283. from cli import _cprint
  284. held, self._held_status_lines = getattr(self, "_held_status_lines", []), []
  285. for line in held:
  286. _cprint(line)
  287. def _close_reasoning_box(self) -> None:
  288. """Close the live reasoning box if it's open (renders the buffered reasoning tail)."""
  289. from cli import _DIM, _RST, _cprint
  290. if not getattr(self, "_reasoning_box_opened", False):
  291. return
  292. if getattr(self, "_modern_layout", False):
  293. from hermes_cli.cli_modern_layout import closing_separator, reasoning_text_line, thought_line
  294. if getattr(self, "_reasoning_buf", ""):
  295. _cprint(reasoning_text_line(self._reasoning_buf))
  296. started = getattr(self, "_reasoning_started_at", 0.0)
  297. _cprint(thought_line(time.monotonic() - started if started else None))
  298. _cprint(closing_separator(False))
  299. self._reasoning_buf, self._reasoning_box_opened = "", False
  300. if not getattr(self, "_stream_box_live", False):
  301. self._release_held_status_lines()
  302. return
  303. buf = getattr(self, "_reasoning_buf", "")
  304. if buf:
  305. _cprint(f"{_DIM}{buf}{_RST}")
  306. self._reasoning_buf = ""
  307. w = self._scrollback_box_width()
  308. _cprint(f"{_DIM}└{'─' * (w - 2)}┘{_RST}")
  309. self._reasoning_box_opened = False
  310. if not getattr(self, "_stream_box_live", False):
  311. self._release_held_status_lines()
  312. def _stream_delta(self, text) -> None:
  313. """Line-buffered streaming callback for real-time token rendering.
  314. Emits complete lines via _cprint (reliable under prompt_toolkit's patch_stdout);
  315. reasoning tags are suppressed, or routed to the reasoning box when show_reasoning is on.
  316. ``None`` = intermediate turn boundary (tools about to run): flush boxes and reset state.
  317. """
  318. if text is None:
  319. self._flush_stream()
  320. self._reset_stream_state()
  321. return
  322. if not text:
  323. return
  324. self._stream_started = True
  325. self._stream_prefilt = getattr(self, "_stream_prefilt", "") + text
  326. # Open tags only count at a "block boundary" (stream start / after a newline plus
  327. # optional whitespace) so prose that *mentions* a tag — "(/think not producing
  328. # <think> tags)" — is not swallowed. _stream_last_was_newline tracks the boundary.
  329. if not hasattr(self, "_stream_last_was_newline"):
  330. self._stream_last_was_newline = True
  331. if not getattr(self, "_in_reasoning_block", False):
  332. # Lowercased view catches mixed-case variants (<Think>, <THINKING>, …).
  333. prefilt_lower = self._stream_prefilt.lower()
  334. for tag in _OPEN_TAGS:
  335. tag_lower = tag.lower()
  336. search_start = 0
  337. while True:
  338. idx = prefilt_lower.find(tag_lower, search_start)
  339. if idx == -1:
  340. break
  341. preceding = self._stream_prefilt[:idx]
  342. # Boundary: only whitespace since the last newline — or, with no newline
  343. # buffered yet, since the last emit (which must have ended a line).
  344. is_block_boundary = preceding[preceding.rfind("\n") + 1:].strip() == "" and (
  345. "\n" in preceding or getattr(self, "_stream_last_was_newline", True))
  346. if is_block_boundary:
  347. if preceding:
  348. self._emit_stream_text(preceding)
  349. self._stream_last_was_newline = preceding.endswith("\n")
  350. self._in_reasoning_block = True
  351. self._stream_prefilt = self._stream_prefilt[idx + len(tag):]
  352. break
  353. search_start = idx + 1
  354. if getattr(self, "_in_reasoning_block", False):
  355. break
  356. if not getattr(self, "_in_reasoning_block", False):
  357. # Hold back a possible partial open tag at the end (case-insensitive).
  358. safe = self._stream_prefilt
  359. for tag in _OPEN_TAGS:
  360. tag_lower = tag.lower()
  361. for i in range(1, len(tag)):
  362. if prefilt_lower.endswith(tag_lower[:i]):
  363. safe = self._stream_prefilt[:-i]
  364. break
  365. if safe:
  366. self._emit_stream_text(safe)
  367. self._stream_last_was_newline = safe.endswith("\n")
  368. self._stream_prefilt = self._stream_prefilt[len(safe):]
  369. return
  370. # Inside a reasoning block — look for a close tag; keep accumulating because close tags
  371. # can arrive split across tokens ("</REASONING_SCRATCH" + "PAD>...").
  372. if getattr(self, "_in_reasoning_block", False):
  373. prefilt_lower = self._stream_prefilt.lower()
  374. for tag in _CLOSE_TAGS:
  375. idx = prefilt_lower.find(tag.lower())
  376. if idx != -1:
  377. self._in_reasoning_block = False
  378. if self.show_reasoning:
  379. inner = self._stream_prefilt[:idx]
  380. if inner:
  381. self._stream_reasoning_delta(inner)
  382. after = self._stream_prefilt[idx + len(tag):]
  383. self._stream_prefilt = ""
  384. if after: # re-filter: the remainder could contain another open tag
  385. self._stream_delta(after)
  386. return
  387. # Stream reasoning live when show_reasoning is on; keep only a possible partial
  388. # close-tag tail.
  389. if len(self._stream_prefilt) > _MAX_CLOSE_TAG_LEN:
  390. if self.show_reasoning:
  391. self._stream_reasoning_delta(self._stream_prefilt[:-_MAX_CLOSE_TAG_LEN])
  392. self._stream_prefilt = self._stream_prefilt[-_MAX_CLOSE_TAG_LEN:]
  393. return
  394. def _emit_stream_line(self, printed_line: str) -> None:
  395. """Print one response line with the skin's true-color text escape (if any)."""
  396. from cli import _RST, _STREAM_PAD, _cprint
  397. _tc = getattr(self, "_stream_text_ansi", "")
  398. _cprint(
  399. f"{_STREAM_PAD}{_tc}{printed_line}{_RST}" if _tc else f"{_STREAM_PAD}{printed_line}")
  400. def _flush_stream_table_buf(self) -> None:
  401. """Emit the held table block re-aligned as a whole. Cell-level markdown is stripped FIRST
  402. so the realigner pads to the final visible width, not the marker-decorated width."""
  403. from cli import (
  404. _strip_markdown_syntax, _terminal_width_for_streaming, realign_markdown_tables)
  405. buf = self._stream_table_buf
  406. self._stream_table_buf = []
  407. self._in_stream_table = False
  408. if not buf:
  409. return
  410. joined = "\n".join(buf)
  411. if self.final_response_markdown == "strip":
  412. joined = _strip_markdown_syntax(joined)
  413. block = realign_markdown_tables(joined, _terminal_width_for_streaming())
  414. for ln in block.split("\n"):
  415. self._emit_stream_line(ln)
  416. def _emit_stream_text(self, text: str) -> None:
  417. """Emit filtered text to the streaming display."""
  418. from agent.markdown_tables import is_table_divider, looks_like_table_row
  419. from cli import (
  420. HermesCLI, _ACCENT, _RST, _STREAM_PARTIAL_PREVIEW_LEN, _cprint, _strip_markdown_syntax, datetime)
  421. if not text:
  422. return
  423. # Close a still-open reasoning box on the first content token so the answer streams
  424. # token-by-token; _close_reasoning_box renders the reasoning tail first, so ordering holds.
  425. self._close_reasoning_box()
  426. # Open the response box header on the very first visible text
  427. if not self._stream_box_opened:
  428. text = text.lstrip("\n")
  429. if not text:
  430. return
  431. self._stream_box_opened = True
  432. self._stream_box_live = True # header drawn; cleared at the footer
  433. try:
  434. from hermes_cli.skin_engine import get_active_skin
  435. _skin = get_active_skin()
  436. label = _skin.get_branding("response_label", "☤ Hermes")
  437. _text_hex = _skin.get_color("banner_text", "#FFF8DC")
  438. except Exception:
  439. label = "☤ Hermes"
  440. _text_hex = "#FFF8DC"
  441. try: # true-color escape so streamed text matches the Rich Panel appearance
  442. _r, _g, _b = (int(_text_hex[i:i + 2], 16) for i in (1, 3, 5))
  443. self._stream_text_ansi = f"\033[38;2;{_r};{_g};{_b}m"
  444. except (ValueError, IndexError):
  445. self._stream_text_ansi = ""
  446. if self.show_timestamps:
  447. label = f"{label} {datetime.now().strftime(getattr(self, 'timestamp_format', '%H:%M'))}"
  448. if getattr(self, "_modern_layout", False):
  449. from hermes_cli.cli_modern_layout import answer_label
  450. _cprint(answer_label(datetime.now().strftime(getattr(self, 'timestamp_format', '%H:%M'))
  451. if self.show_timestamps else ""))
  452. else:
  453. w = self._scrollback_box_width()
  454. fill = w - 2 - HermesCLI._status_bar_display_width(label)
  455. _cprint(f"\n{_ACCENT}╭─{label}{'─' * max(fill - 1, 0)}╮{_RST}")
  456. # Turn-level record of what actually reached the screen; survives _reset_stream_state at
  457. # tool-call boundaries so an interrupted reply isn't re-rendered as a Panel (#65666).
  458. self._streamed_text_this_turn = getattr(self, "_streamed_text_this_turn", "") + text
  459. self._stream_buf += text
  460. while "\n" in self._stream_buf:
  461. line, self._stream_buf = self._stream_buf.split("\n", 1)
  462. # Table rows are held and re-padded as a block once it ends (already-printed rows
  463. # can't be re-aligned), so a table appears in one batch when the block closes.
  464. if self._in_stream_table:
  465. if looks_like_table_row(line) or is_table_divider(line):
  466. self._stream_table_buf.append(line)
  467. continue
  468. self._flush_stream_table_buf()
  469. elif looks_like_table_row(line):
  470. self._stream_table_buf.append(line)
  471. self._in_stream_table = True
  472. continue
  473. if self.final_response_markdown == "strip":
  474. line = _strip_markdown_syntax(line)
  475. self._emit_stream_line(line)
  476. # Partial lines are emitted ONLY at real newlines (no hard-wrapping — the terminal
  477. # soft-wraps, so highlight-copy yields the original text). For TTFT perception, mirror
  478. # the tail of a long unfinished paragraph into the status-bar spinner.
  479. if (
  480. self._stream_buf
  481. and not self._in_stream_table
  482. and not self._stream_buf.lstrip().startswith("|")
  483. and len(self._stream_buf) >= 80):
  484. preview = self._stream_buf[-int(_STREAM_PARTIAL_PREVIEW_LEN):]
  485. cut = preview.find(" ")
  486. if 0 < cut < len(preview) - 1:
  487. preview = preview[cut + 1:]
  488. try:
  489. self._spinner_text = f"… {preview}"
  490. self._invalidate()
  491. except Exception:
  492. pass
  493. def _flush_stream(self) -> None:
  494. """Emit any remaining partial line from the stream buffer and close the box."""
  495. from agent.markdown_tables import is_table_divider, looks_like_table_row
  496. from cli import _ACCENT, _RST, _cprint, _strip_markdown_syntax
  497. # Still inside a "reasoning block" at end-of-stream = false positive (the model
  498. # mentioned a tag in prose and never closed it): recover the buffer as regular text.
  499. if getattr(self, "_in_reasoning_block", False) and getattr(self, "_stream_prefilt", ""):
  500. self._in_reasoning_block = False
  501. self._emit_stream_text(self._stream_prefilt)
  502. self._stream_prefilt = ""
  503. self._close_reasoning_box() # in case no content tokens arrived
  504. # A trailing partial table row joins the table buffer so the whole block is re-aligned
  505. # together (else the final row prints under-padded).
  506. if (
  507. self._stream_buf
  508. and getattr(self, "_in_stream_table", False)
  509. and (looks_like_table_row(self._stream_buf) or is_table_divider(self._stream_buf))):
  510. self._stream_table_buf.append(self._stream_buf)
  511. self._stream_buf = ""
  512. if getattr(self, "_stream_table_buf", None):
  513. self._flush_stream_table_buf()
  514. if self._stream_buf:
  515. line = _strip_markdown_syntax(self._stream_buf) if self.final_response_markdown == "strip" else self._stream_buf
  516. self._emit_stream_line(line)
  517. self._stream_buf = ""
  518. if self._stream_box_opened and getattr(self, "_stream_box_live", False):
  519. if getattr(self, "_modern_layout", False):
  520. from hermes_cli.cli_modern_layout import closing_separator
  521. _cprint(closing_separator(True))
  522. else:
  523. w = self._scrollback_box_width()
  524. _cprint(f"{_ACCENT}╰{'─' * (w - 2)}╯{_RST}")
  525. self._stream_box_live = False
  526. self._release_held_status_lines()
  527. def _reset_stream_state(self) -> None:
  528. """Reset streaming state before each agent invocation."""
  529. self._stream_buf = ""
  530. self._stream_started = False
  531. self._stream_box_opened = False
  532. self._stream_text_ansi = ""
  533. self._stream_prefilt = ""
  534. self._in_reasoning_block = False
  535. self._stream_last_was_newline = True
  536. self._reasoning_box_opened = False
  537. self._reasoning_buf = ""
  538. self._reasoning_preview_buf = ""
  539. # A batch cancelled/errored before any tool.started would otherwise mute the next turn's line.
  540. self.__dict__.pop("_tool_gen_announced", None)
  541. self._stream_table_buf = []
  542. self._in_stream_table = False
  543. self._stream_box_live = False
  544. def _slow_command_status(self, command: str) -> str:
  545. """Return a user-facing status message for slower slash commands."""
  546. cmd_lower = command.lower().strip()
  547. exact = _SLOW_COMMAND_STATUS_EXACT.get(cmd_lower)
  548. if exact:
  549. return t(exact)
  550. for prefix, key in _SLOW_COMMAND_STATUS:
  551. if cmd_lower.startswith(prefix):
  552. return t(key)
  553. return t("cli.stream.processing_command")
  554. def _command_spinner_frame(self) -> str:
  555. """Return the current spinner frame for slow slash commands."""
  556. from cli import _COMMAND_SPINNER_FRAMES
  557. return _COMMAND_SPINNER_FRAMES[int(time.monotonic() * 10) % len(_COMMAND_SPINNER_FRAMES)]
  558. @contextmanager
  559. def _busy_command(self, status: str, *, blocks_input: bool = True):
  560. """Expose a temporary busy state in the TUI while a slash command runs.
  561. Most sync slash commands reserve the composer (their completion changes session state);
  562. manual compression is safe to draft through (queued input runs against compacted history).
  563. """
  564. from cli import _cprint
  565. previous_blocks_input = getattr(self, "_command_blocks_input", False)
  566. self._command_running = True
  567. self._command_blocks_input = blocks_input
  568. self._command_status = status
  569. self._invalidate(min_interval=0.0)
  570. try:
  571. _cprint(f"⏳ {status}")
  572. yield
  573. finally:
  574. self._command_running = False
  575. self._command_blocks_input = previous_blocks_input
  576. self._command_status = ""
  577. self._invalidate(min_interval=0.0)
  578. def _preprocess_images_with_vision(self, text: str, images: list, *, announce: bool = True) -> str:
  579. """Describe attached images via the auxiliary vision model and prepend the descriptions
  580. to the user's text (works with non-vision models; same approach as the gateway). The
  581. local path is included so the agent can re-examine via ``vision_analyze``."""
  582. from cli import _DIM, _RST, _cprint
  583. import asyncio as _asyncio
  584. from gateway.warning_notifications import render_notification
  585. from tools.vision_tools import vision_analyze_tool
  586. analysis_prompt = (
  587. "Describe everything visible in this image in thorough detail. "
  588. "Include any text, code, data, objects, people, layout, colors, "
  589. "and any other notable visual information.")
  590. enriched_parts = []
  591. for img_path in images:
  592. if not img_path.exists():
  593. continue
  594. size_kb = img_path.stat().st_size // 1024
  595. if announce:
  596. _cprint(f" {_DIM}{t('cli.stream.vision_analyzing', name=img_path.name, size_kb=size_kb)}{_RST}")
  597. try:
  598. result_json = _asyncio.run(
  599. vision_analyze_tool(image_url=str(img_path), user_prompt=analysis_prompt))
  600. result = json.loads(result_json)
  601. if result.get("success"):
  602. description = result.get("analysis", "")
  603. enriched_parts.append(
  604. f"[The user attached an image. Here's what it contains:\n{description}]\n"
  605. f"[If you need a closer look, use vision_analyze with "
  606. f"image_url: {img_path}]")
  607. if announce:
  608. _cprint(f" {_DIM}{t('cli.stream.vision_analyzed')}{_RST}")
  609. else:
  610. enriched_parts.append(
  611. f"[The user attached an image but it couldn't be analyzed. "
  612. f"You can try examining it with vision_analyze using "
  613. f"image_url: {img_path}]")
  614. if announce:
  615. render_notification(lambda: _cprint(f" {_DIM}{t('cli.stream.vision_failed')}{_RST}"),
  616. platform="cli", user_config=getattr(getattr(self, "agent", None), "_notification_config", None))
  617. except Exception as e:
  618. enriched_parts.append(
  619. f"[The user attached an image but analysis failed ({e}). "
  620. f"You can try examining it with vision_analyze using "
  621. f"image_url: {img_path}]")
  622. if announce:
  623. render_notification(lambda: _cprint(f" {_DIM}{t('cli.stream.vision_error')}{_RST}"),
  624. platform="cli", user_config=getattr(getattr(self, "agent", None), "_notification_config", None))
  625. # Vision descriptions first, then the user's original text
  626. user_text = text if isinstance(text, str) and text else ""
  627. if enriched_parts:
  628. prefix = "\n\n".join(enriched_parts)
  629. return f"{prefix}\n\n{user_text}" if user_text else prefix
  630. return user_text or "What do you see in this image?"
  631. def _console_print(self, *args, **kwargs):
  632. """Print through the active command-safe console (prompt_toolkit-safe Rich once the
  633. TUI is live, else the plain console)."""
  634. from cli import ChatConsole
  635. console = ChatConsole() if getattr(self, "_app", None) else self.console
  636. console.print(*args, **kwargs)
  637. def _on_tool_gen_start(self, tool_name: str) -> None:
  638. """Model began generating tool-call arguments: close open boxes once, then print a status
  639. line so a large payload (e.g. 45 KB write_file) doesn't look like a frozen screen.
  640. Fires once per tool CALL, so a batch of parallel calls to the same tool printed the same
  641. line N times (#10478); repeats within one generation batch are coalesced. The set is
  642. cleared when a tool actually starts (``tool.started``), i.e. on the next batch."""
  643. from cli import _cprint
  644. if getattr(self, '_stream_box_opened', False):
  645. self._flush_stream()
  646. self._stream_box_opened = False
  647. self._close_reasoning_box()
  648. announced = self.__dict__.setdefault("_tool_gen_announced", set())
  649. if tool_name in announced:
  650. return
  651. announced.add(tool_name)
  652. from agent.display import bridge_generating_phrase, get_tool_emoji
  653. what = bridge_generating_phrase(tool_name) or tool_name
  654. if getattr(self, "_modern_layout", False): # the composer rule shows it; scrollback stays quiet
  655. self._spinner_text = f"{get_tool_emoji(tool_name, default='⚡')} {t('cli.modern.preparing', what=what)}"
  656. self._invalidate()
  657. return
  658. _cprint(f" ┊ {t('cli.stream.tool_preparing', emoji=get_tool_emoji(tool_name, default='⚡'), what=what)}")
  659. def _on_tool_progress(self, event_type: str, function_name: str | None = None, preview: str | None = None, function_args: dict | None = None, **kwargs):
  660. """Tool lifecycle events (tool.started / tool.completed / reasoning.* / moa.*).
  661. Drives the TUI spinner (tool.started stamps the elapsed timer); in "all"/"new"/"verbose"
  662. progress modes tool.completed also commits a stacked scrollback line (tool history).
  663. """
  664. from cli import CLI_CONFIG, _DIM, _RST, _cprint, _hermes_home
  665. # MoA reference outputs (display-only events from the MoA facade): render each answer
  666. # as a labelled thinking-style block BEFORE the aggregator acts.
  667. if event_type == "moa.reference":
  668. label = function_name or t("cli.stream.reference_noun")
  669. text = preview or ""
  670. idx = kwargs.get("moa_index")
  671. count = kwargs.get("moa_count")
  672. header = (t("cli.stream.reference_header_indexed", index=idx, count=count, label=label) if idx and count
  673. else t("cli.stream.reference_header", label=label))
  674. try:
  675. self._flush_reasoning_preview(force=True)
  676. except Exception:
  677. pass
  678. _cprint(f" {_DIM}┊ ◇ {header}{_RST}")
  679. try:
  680. self._emit_reasoning_preview(text)
  681. except Exception:
  682. if text.strip():
  683. _cprint(f" {_DIM}{text.strip()}{_RST}")
  684. self._invalidate()
  685. return
  686. if event_type == "moa.aggregating":
  687. agg = function_name or ""
  688. self._spinner_text = t("cli.stream.aggregating_detail", detail=agg) if agg else t("cli.stream.aggregating")
  689. self._invalidate()
  690. return
  691. # Feed the pet: tools mean "running"; a failed tool latches the turn to end on a sulk.
  692. if event_type == "tool.started":
  693. self._pet_reasoning = False
  694. self.__dict__.pop("_tool_gen_announced", None)
  695. elif event_type == "tool.completed" and kwargs.get("is_error"):
  696. self._pet_turn_error = True
  697. elif event_type and event_type.startswith("reasoning"):
  698. self._pet_reasoning = True
  699. if event_type == "tool.completed":
  700. self._tool_start_time = 0.0
  701. self._forget_tool_label(function_name)
  702. self._turn_summary_record(
  703. function_name, kwargs.get("result"), kwargs.get("is_error", False))
  704. # Focus view: count the hidden scrollback line for the post-turn recovery report.
  705. if getattr(self, "_focus_view_enabled", False):
  706. try:
  707. self._note_focus_hidden_line(function_name or "")
  708. except Exception:
  709. pass
  710. # "verbose" must commit the same line as "all": non-streaming calls (MoA aggregator,
  711. # ACP subprocess providers) never emit the "preparing" line, so nothing else builds history.
  712. if function_name and self.tool_progress_mode in {"new", "all", "verbose"}:
  713. duration = kwargs.get("duration", 0.0)
  714. # Pop stored args from tool.started for this function
  715. stored = self._pending_tool_info.get(function_name)
  716. stored_args = stored.pop(0) if stored else {}
  717. if stored is not None and not stored:
  718. del self._pending_tool_info[function_name]
  719. # "new" mode: skip consecutive repeats of the same tool
  720. if self.tool_progress_mode == "new" and function_name == self._last_scrollback_tool:
  721. self._print_tool_image(function_name, stored_args, kwargs.get("result"), kwargs.get("is_error"))
  722. self._invalidate()
  723. return
  724. self._last_scrollback_tool = function_name
  725. try:
  726. from hermes_cli.cli_modern_layout import completed_tool_line
  727. _cprint(completed_tool_line(self, function_name, stored_args, duration, kwargs.get("result"),
  728. kwargs.get("is_error", False)))
  729. except Exception:
  730. pass
  731. self._print_tool_image(function_name, stored_args, kwargs.get("result"), kwargs.get("is_error"))
  732. # One-time /verbose hint on the first long tool in the noisiest mode; latched
  733. # on self and persisted to config.yaml.
  734. try:
  735. if (
  736. not getattr(self, "_long_tool_hint_fired", False)
  737. and self.tool_progress_mode == "all"
  738. and duration >= 30.0):
  739. from agent.onboarding import (
  740. TOOL_PROGRESS_FLAG, is_seen, mark_seen, tool_progress_hint_cli)
  741. if not is_seen(CLI_CONFIG, TOOL_PROGRESS_FLAG):
  742. self._long_tool_hint_fired = True
  743. _cprint(f" {_DIM}{tool_progress_hint_cli()}{_RST}")
  744. mark_seen(_hermes_home / "config.yaml", TOOL_PROGRESS_FLAG)
  745. CLI_CONFIG.setdefault("onboarding", {}).setdefault("seen", {})[TOOL_PROGRESS_FLAG] = True
  746. except Exception:
  747. pass
  748. self._invalidate()
  749. return
  750. if event_type != "tool.started":
  751. return self._show_tool_progress(event_type, function_name, preview, kwargs.get("percent"))
  752. if function_name and not function_name.startswith("_"):
  753. from agent.display import get_tool_preview_max_len, tool_row_emoji
  754. label = preview or function_name
  755. _pl = get_tool_preview_max_len()
  756. if _pl > 0 and len(label) > _pl:
  757. label = label[:_pl - 3] + "..."
  758. self._spinner_text = f"{tool_row_emoji(function_name, function_args)} {label}"
  759. self.__dict__.setdefault("_running_tool_labels", {}).setdefault(function_name, []).append(self._spinner_text)
  760. self._tool_start_time = time.monotonic()
  761. # Store args for stacked scrollback line on completion
  762. self._pending_tool_info.setdefault(function_name, []).append(
  763. function_args if function_args is not None else {})
  764. self._invalidate()
  765. def _forget_tool_label(self, function_name) -> None:
  766. labels = self.__dict__.get("_running_tool_labels", {})
  767. if labels.get(function_name):
  768. labels[function_name].pop(0)
  769. if not labels[function_name]:
  770. del labels[function_name]
  771. def _show_tool_progress(self, event_type, function_name, message, percent) -> None:
  772. """Live MCP progress rides the spinner line: ``<tool label> · 42% · message``. The CLI has one
  773. spinner, so progress shows only while exactly one call of that tool runs and nothing else does:
  774. with parallel calls the label could belong to another call (tool.started carries no call id)."""
  775. labels = self.__dict__.get("_running_tool_labels", {})
  776. running = labels.get(function_name) or []
  777. if event_type == "tool.progress" and len(running) == 1 and sum(map(len, labels.values())) == 1:
  778. parts = [running[0]] + ([f"{percent}%"] if percent is not None else []) + ([message] if message else [])
  779. self._spinner_text = " · ".join(parts)
  780. self._invalidate()
  781. def _on_tool_start(self, tool_call_id: str, function_name: str, function_args: dict):
  782. """Capture local before-state for write-capable tools."""
  783. from cli import logger
  784. try:
  785. from agent.display import capture_local_edit_snapshot
  786. snapshot = capture_local_edit_snapshot(function_name, function_args)
  787. if snapshot is not None:
  788. self._pending_edit_snapshots[tool_call_id] = snapshot
  789. except Exception:
  790. logger.debug("Edit snapshot capture failed for %s", function_name, exc_info=True)
  791. def _print_tool_image(self, function_name, args, result, is_error) -> None:
  792. """Show the local image a finished tool produced or looked at (``display.inline_images``)."""
  793. from agent.terminal_image import tool_image_path
  794. from hermes_cli.cli_inline_images import print_inline_images
  795. if path := tool_image_path(function_name, args, result, is_error=bool(is_error)):
  796. print_inline_images(self, [path])
  797. def _on_tool_complete(self, tool_call_id: str, function_name: str, function_args: dict, function_result: str):
  798. """Render file edits with inline diff after write-capable tools complete."""
  799. from cli import _cprint, logger
  800. # A background delegate_task re-enters as a fresh turn when done; say so once so the
  801. # idle prompt doesn't read as "nothing happened".
  802. if function_name == "delegate_task":
  803. try:
  804. parsed = json.loads(function_result) if isinstance(function_result, str) else (function_result or {})
  805. except Exception:
  806. parsed = {}
  807. if isinstance(parsed, dict) and parsed.get("status") == "dispatched" and parsed.get("mode") == "background":
  808. n = parsed.get("count") or 1
  809. key = "cli.stream.background_running_one" if n == 1 else "cli.stream.background_running_other"
  810. try:
  811. _cprint(f"\033[2m{t(key, count=n)}\033[0m")
  812. except Exception:
  813. pass
  814. snapshot = self._pending_edit_snapshots.pop(tool_call_id, None)
  815. try:
  816. from agent.display import render_edit_diff_with_delta
  817. from hermes_cli.cli_modern_layout import diff_printer
  818. render_edit_diff_with_delta(
  819. function_name, function_result, function_args=function_args, snapshot=snapshot,
  820. print_fn=diff_printer(_cprint))
  821. except Exception:
  822. logger.debug("Edit diff preview failed for %s", function_name, exc_info=True)