cli_render.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. """Classic-CLI rendering helpers: reasoning-tag stripping, ANSI/skin colours, light-mode detection, markdown/final-content rendering, output-history replay, ``_cprint`` and the panel box/wrap helpers.
  2. Split out of ``cli.py``; ``cli`` re-exports every public name and moved bodies late-bind
  3. cli-level names through ``from cli import ...`` at call time so facade monkeypatch seams hold.
  4. """
  5. from __future__ import annotations
  6. import functools
  7. import itertools
  8. import os
  9. import re
  10. import shutil
  11. import sys
  12. import textwrap
  13. import threading
  14. import time
  15. from contextlib import contextmanager, suppress
  16. from agent.i18n import t
  17. from agent.think_scrubber import THINK_TAG_NAMES
  18. from hermes_cli.banner import format_banner_version_label
  19. from rich.console import Console
  20. from rich.text import Text as _RichText
  21. from typing import Any
  22. def _cli():
  23. """Late import of the ``cli`` facade: mutable CLI module state (and its test seams) lives there."""
  24. import cli
  25. return cli
  26. _REASONING_TAGS = THINK_TAG_NAMES
  27. _TOOL_CALL_TAGS = ("tool_call", "tool_calls", "tool_result", "function_call", "function_calls")
  28. def _strip_reasoning_tags(text: str) -> str:
  29. """Strip reasoning blocks (closed, unterminated, orphan-close) and leaked tool-call XML from display text.
  30. Keep in sync with ``agent.agent_runtime_helpers.strip_think_blocks`` and the stream consumer's think-tag sets.
  31. Also strips tool-call XML blocks some open models leak into visible content (``<tool_call>``,
  32. ``<function_calls>``, Gemma-style ``<function name="…">…</function>``). Ported from
  33. openclaw/openclaw#67318.
  34. """
  35. from cli import _REASONING_TAGS, _TOOL_CALL_TAGS
  36. cleaned = text
  37. for tag in _REASONING_TAGS:
  38. cleaned = re.sub(rf"<{tag}>.*?</{tag}>\s*", "", cleaned, flags=re.DOTALL | re.IGNORECASE)
  39. cleaned = re.sub(rf"<{tag}>.*$", "", cleaned, flags=re.DOTALL | re.IGNORECASE)
  40. cleaned = re.sub(rf"</{tag}>\s*", "", cleaned, flags=re.IGNORECASE)
  41. for tc_tag in _TOOL_CALL_TAGS:
  42. cleaned = re.sub(
  43. rf"<(?:[\w.-]+:)?{tc_tag}\b[^>]*>.*?</(?:[\w.-]+:)?{tc_tag}>\s*",
  44. "", cleaned, flags=re.DOTALL | re.IGNORECASE,
  45. )
  46. # <function name="..."> — boundary + attribute gated to avoid prose false positives.
  47. cleaned = re.sub(
  48. r'(?:(?<=^)|(?<=[\n\r.!?:]))[ \t]*<function\b[^>]*\bname\s*=[^>]*>(?:(?:(?!</function>).)*)</function>\s*',
  49. '', cleaned, flags=re.DOTALL | re.IGNORECASE,
  50. )
  51. # Stray closers and cut tool-call fragments share storage's compiled patterns (#101899, #102303).
  52. from agent.agent_runtime_helpers import _STRAY_TOOL_CALL_CLOSER_PATTERN, _UNTERMINATED_TOOL_CALL_PATTERN
  53. cleaned = _STRAY_TOOL_CALL_CLOSER_PATTERN.sub('', cleaned)
  54. cleaned = _UNTERMINATED_TOOL_CALL_PATTERN.sub('', cleaned)
  55. return cleaned.strip()
  56. def _assistant_content_as_text(content: Any) -> str:
  57. if content is None:
  58. return ""
  59. if isinstance(content, str):
  60. return content
  61. if isinstance(content, list):
  62. parts = [str(part.get("text", "")) for part in content if isinstance(part, dict) and part.get("type") == "text"]
  63. return "\n".join(p for p in parts if p)
  64. return str(content)
  65. def _assistant_copy_text(content: Any) -> str:
  66. from cli import _assistant_content_as_text, _strip_reasoning_tags
  67. return _strip_reasoning_tags(_assistant_content_as_text(content))
  68. _ACCENT_ANSI_DEFAULT = "\033[1;38;2;255;215;0m" # #FFD700 bold fallback
  69. _BOLD = "\033[1m"
  70. _RST = "\033[0m"
  71. _STREAM_PAD = "" # no indent: leading whitespace pollutes copy/paste
  72. _STREAM_PARTIAL_PREVIEW_LEN = 60 # tail of an unfinished line mirrored into the spinner
  73. def _hex_to_ansi(hex_color: str, *, bold: bool = False) -> str:
  74. """Convert '#RRGGBB' to a true-color ANSI escape, remapping dark-tuned colors in light mode."""
  75. from cli import _ACCENT_ANSI_DEFAULT, _maybe_remap_for_light_mode
  76. hex_color = _maybe_remap_for_light_mode(hex_color)
  77. try:
  78. r, g, b = (int(hex_color[i:i + 2], 16) for i in (1, 3, 5))
  79. return f"\033[{'1;' if bold else ''}38;2;{r};{g};{b}m"
  80. except (ValueError, IndexError):
  81. return _ACCENT_ANSI_DEFAULT if bold else "\033[38;2;184;134;11m"
  82. _TRUE_RE = re.compile(r"^(1|true|on|yes|y)$")
  83. _FALSE_RE = re.compile(r"^(0|false|off|no|n)$")
  84. _LIGHT_DEFAULT_TERM_PROGRAMS = frozenset() # Apple_Terminal isn't reliable; require explicit config
  85. def _luminance_from_hex(hex_str: str) -> float | None:
  86. """Rec.709 luma in [0, 1] for '#RGB'/'#RRGGBB', or None when malformed."""
  87. s = (hex_str or "").strip().lstrip("#")
  88. if len(s) == 3:
  89. s = "".join(c * 2 for c in s)
  90. if len(s) != 6 or not all(c in "0123456789abcdefABCDEF" for c in s):
  91. return None
  92. try:
  93. r, g, b = int(s[0:2], 16), int(s[2:4], 16), int(s[4:6], 16)
  94. except ValueError:
  95. return None
  96. return (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255.0
  97. _DA1_REPLY_RE = re.compile(rb"\x1b\[\?[0-9;]*c")
  98. def _query_osc11_background() -> str | None:
  99. """Terminal background via OSC 11 as "#RRGGBB", or None.
  100. Fenced with a DA1 sentinel (``ESC[c``): terminals answer in order and virtually all
  101. answer DA1, so its reply proves our OSC 11 was processed — otherwise a late reply
  102. leaks into prompt_toolkit's stdin as typed text. Skipped over SSH (round-trip too
  103. slow; a late BEL reads as Ctrl+G). A 50 ms drain after TCSAFLUSH catches stragglers.
  104. After the main read + TCSAFLUSH, a short drain window (50 ms) catches late-arriving bytes that slipped
  105. past the flush — a race observed on VPS and container terminals under load (#40250).
  106. """
  107. from cli import _DA1_REPLY_RE
  108. if not sys.stdin.isatty() or not sys.stdout.isatty():
  109. return None
  110. if any(os.environ.get(v) for v in ("SSH_CONNECTION", "SSH_CLIENT", "SSH_TTY")):
  111. return None
  112. try:
  113. import select
  114. import termios
  115. import tty
  116. fd = sys.stdin.fileno()
  117. old = termios.tcgetattr(fd)
  118. except Exception:
  119. return None
  120. try:
  121. try:
  122. tty.setcbreak(fd)
  123. except Exception:
  124. return None
  125. try:
  126. # One write so the OSC 11 query and DA1 fence cannot reorder.
  127. sys.stdout.write("\x1b]11;?\x1b\\\x1b[c")
  128. sys.stdout.flush()
  129. except Exception:
  130. return None
  131. # Read until the DA1 fence closes; the 1s deadline only covers terminals ignoring DA1.
  132. deadline = time.monotonic() + 1.0
  133. buf = b""
  134. while time.monotonic() < deadline:
  135. r, _, _ = select.select([fd], [], [], deadline - time.monotonic())
  136. if not r:
  137. continue
  138. try:
  139. chunk = os.read(fd, 64)
  140. except OSError:
  141. break
  142. if not chunk:
  143. break
  144. buf += chunk
  145. if _DA1_REPLY_RE.search(buf):
  146. break
  147. # Reply: \x1b]11;rgb:RRRR/GGGG/BBBB\x1b\\ — components are 1-4 hex digits.
  148. m = re.search(rb"rgb:([0-9a-fA-F]+)/([0-9a-fA-F]+)/([0-9a-fA-F]+)", buf)
  149. if not m:
  150. return None
  151. def norm(h: bytes) -> int:
  152. v = int(h, 16)
  153. bits = len(h) * 4
  154. return (v * 255) // ((1 << bits) - 1) if bits else 0
  155. r, g, b = norm(m.group(1)), norm(m.group(2)), norm(m.group(3))
  156. return f"#{r:02X}{g:02X}{b:02X}"
  157. finally:
  158. # TCSAFLUSH discards unread input, scrubbing a partial reply before prompt_toolkit reads it.
  159. with suppress(Exception):
  160. termios.tcsetattr(fd, termios.TCSAFLUSH, old)
  161. try:
  162. drain_deadline = time.monotonic() + 0.05
  163. while time.monotonic() < drain_deadline:
  164. r, _, _ = select.select([fd], [], [], drain_deadline - time.monotonic())
  165. if not r or not os.read(fd, 64):
  166. break
  167. except Exception:
  168. pass
  169. def _heal_cooked_mode_drift(fd: int) -> bool:
  170. """Re-apply raw mode on *fd* when termios drifted back to cooked (POSIX only).
  171. A lost ``run_in_terminal`` cooked_mode() restore makes the kernel line-buffer every
  172. keystroke and the CLI looks dead. Mirrors prompt_toolkit's raw_mode flag surgery in
  173. place. Returns True when healed; False when already raw or not inspectable.
  174. """
  175. try:
  176. import termios
  177. attrs = termios.tcgetattr(fd)
  178. except Exception:
  179. return False
  180. lflag = attrs[3]
  181. if not (lflag & (termios.ICANON | termios.ECHO)):
  182. return False # still raw — nothing to do
  183. attrs[3] = lflag & ~(termios.ECHO | termios.ICANON | termios.IEXTEN | termios.ISIG)
  184. attrs[0] = attrs[0] & ~(termios.IXON | termios.IXOFF | termios.ICRNL | termios.INLCR | termios.IGNCR)
  185. attrs[6][termios.VMIN] = 1
  186. try:
  187. termios.tcsetattr(fd, termios.TCSANOW, attrs)
  188. except Exception:
  189. return False
  190. return True
  191. def _detect_light_mode_uncached() -> bool:
  192. """The detection ladder documented above; may raise (caller maps errors to dark)."""
  193. from cli import _FALSE_RE, _LIGHT_DEFAULT_TERM_PROGRAMS, _TRUE_RE, _luminance_from_hex, _query_osc11_background
  194. for var in ("HERMES_LIGHT", "HERMES_TUI_LIGHT"):
  195. v = (os.environ.get(var) or "").strip().lower()
  196. if _TRUE_RE.match(v):
  197. return True
  198. if _FALSE_RE.match(v):
  199. return False
  200. theme = (os.environ.get("HERMES_TUI_THEME") or "").strip().lower()
  201. if theme == "light":
  202. return True
  203. if theme == "dark":
  204. return False
  205. bg_lum = _luminance_from_hex(os.environ.get("HERMES_TUI_BACKGROUND") or "")
  206. if bg_lum is not None:
  207. return bg_lum >= 0.5
  208. last = (os.environ.get("COLORFGBG") or "").strip().split(";")[-1]
  209. if last.isdigit() and 0 <= int(last) < 16:
  210. return int(last) in {7, 15}
  211. bg_color = _query_osc11_background()
  212. if bg_color:
  213. lum = _luminance_from_hex(bg_color)
  214. if lum is not None:
  215. return lum >= 0.5
  216. return (os.environ.get("TERM_PROGRAM") or "").strip() in _LIGHT_DEFAULT_TERM_PROGRAMS
  217. # Light-mode equivalents of skin colors unreadable on cream backgrounds. Only colors used
  218. # as STANDALONE foregrounds: ones paired with a dark bg (status bar text on #1a1a2e) would
  219. # become invisible the other direction, hence #C0C0C0/#888888/#555555/#8B8682 are skipped.
  220. _LIGHT_MODE_REMAP: dict[str, str] = {
  221. "#FFF8DC": "#1A1A1A", "#FFD700": "#9A6B00", "#FFBF00": "#8A5A00", "#B8860B": "#5C4500",
  222. "#DAA520": "#6B4F00", "#F1E6CF": "#1A1A1A", "#c9d1d9": "#24292F", "#EAF7FF": "#0F1B26",
  223. "#F5F5F5": "#1A1A1A", "#FFF0D4": "#1A1A1A", "#CD7F32": "#8A4F1A", "#FFEFB5": "#3A2A00",
  224. }
  225. _LIGHT_MODE_REMAP_UPPER = {k.upper(): v for k, v in _LIGHT_MODE_REMAP.items()}
  226. def _maybe_remap_for_light_mode(hex_color: str) -> str:
  227. """In light mode, remap a dark-tuned color to its higher-contrast equivalent."""
  228. from cli import _LIGHT_MODE_REMAP_UPPER, _detect_light_mode
  229. if not _detect_light_mode():
  230. return hex_color
  231. if not hex_color or not hex_color.startswith("#"):
  232. return hex_color
  233. return _LIGHT_MODE_REMAP_UPPER.get(hex_color.upper(), hex_color)
  234. def _install_skin_light_mode_hook() -> None:
  235. """Wrap SkinConfig.get_color so EVERY skin color read goes through the light-mode remap. Idempotent."""
  236. from cli import _maybe_remap_for_light_mode
  237. try:
  238. from hermes_cli.skin_engine import SkinConfig # type: ignore[import]
  239. except Exception:
  240. return
  241. if getattr(SkinConfig, "_hermes_light_mode_hook_installed", False):
  242. return
  243. _orig_get_color = SkinConfig.get_color
  244. def _wrapped_get_color(self, key, fallback=""):
  245. value = _orig_get_color(self, key, fallback)
  246. try:
  247. return _maybe_remap_for_light_mode(value)
  248. except Exception:
  249. return value
  250. SkinConfig.get_color = _wrapped_get_color # type: ignore[method-assign]
  251. SkinConfig._hermes_light_mode_hook_installed = True # type: ignore[attr-defined]
  252. class _SkinAwareAnsi:
  253. """Lazy ANSI escape resolved from the skin on first use; ``.reset()`` after a ``/skin`` switch."""
  254. def __init__(self, skin_key: str, fallback_hex: str = "#FFD700", *, bold: bool = False):
  255. self._skin_key = skin_key
  256. self._fallback_hex = fallback_hex
  257. self._bold = bold
  258. self._cached: str | None = None
  259. def __str__(self) -> str:
  260. from cli import _hex_to_ansi
  261. if self._cached is None:
  262. try:
  263. from hermes_cli.skin_engine import get_active_skin
  264. self._cached = _hex_to_ansi(
  265. get_active_skin().get_color(self._skin_key, self._fallback_hex),
  266. bold=self._bold,
  267. )
  268. except Exception:
  269. self._cached = _hex_to_ansi(self._fallback_hex, bold=self._bold)
  270. return self._cached
  271. def __add__(self, other: str) -> str:
  272. return str(self) + other
  273. def __radd__(self, other: str) -> str:
  274. return other + str(self)
  275. def reset(self) -> None:
  276. """Clear cache so the next access re-reads the skin."""
  277. self._cached = None
  278. _ACCENT = _SkinAwareAnsi("response_border", "#FFD700", bold=True)
  279. # dim+italic attributes (not a hex) so dim text inherits the terminal foreground in both modes.
  280. _DIM = "\x1b[2;3m"
  281. def _tty_wrap(s: str, sgr: str) -> str:
  282. """Wrap *s* in an SGR attribute when stdout is a real TTY; plain text otherwise."""
  283. try:
  284. return f"{sgr}{s}\x1b[0m" if sys.stdout.isatty() else str(s)
  285. except Exception:
  286. return str(s)
  287. _b = functools.partial(_tty_wrap, sgr="\x1b[1m") # bold when stdout is a real TTY
  288. _d = functools.partial(_tty_wrap, sgr="\x1b[2;3m") # dim-italic when stdout is a real TTY
  289. def _accent_hex() -> str:
  290. """Return the active skin accent color for legacy CLI output lines."""
  291. try:
  292. from hermes_cli.skin_engine import get_active_skin
  293. return get_active_skin().get_color("ui_accent", "#FFBF00")
  294. except Exception:
  295. return "#FFBF00"
  296. def _rich_text_from_ansi(text: str) -> _RichText:
  297. """Rich Text from ANSI output; literal ``[brackets]`` are not treated as markup."""
  298. return _RichText.from_ansi(text or "")
  299. def _strip_markdown_syntax(text: str) -> str:
  300. """Best-effort markdown marker removal for plain-text display."""
  301. from cli import _rich_text_from_ansi
  302. plain = _rich_text_from_ansi(text or "").plain
  303. # HR markers: "-"/"_" runs of 3+, but "*" only when exactly 3 (cron schedules "* * * * *").
  304. plain = re.sub(r"^\s{0,3}(?:[-_]\s*){3,}$", "", plain, flags=re.MULTILINE)
  305. plain = re.sub(r"^\s{0,3}(?:\*\s*){3}\s*$", "", plain, flags=re.MULTILINE)
  306. plain = re.sub(r"^\s{0,3}#{1,6}\s+", "", plain, flags=re.MULTILINE)
  307. # Blockquotes, lists, and checkboxes are preserved because they carry structure.
  308. plain = re.sub(r"(```+|~~~+)", "", plain)
  309. plain = re.sub(r"`([^`]*)`", r"\1", plain)
  310. plain = re.sub(r"!\[([^\]]*)\]\([^\)]*\)", r"\1", plain)
  311. plain = re.sub(r"\[([^\]]+)\]\([^\)]*\)", r"\1", plain)
  312. plain = re.sub(r"\*\*\*([^*]+)\*\*\*", r"\1", plain)
  313. plain = re.sub(r"(?<!\w)___([^_]+)___(?!\w)", r"\1", plain)
  314. plain = re.sub(r"\*\*([^*]+)\*\*", r"\1", plain)
  315. plain = re.sub(r"(?<!\w)__([^_]+)__(?!\w)", r"\1", plain)
  316. # `*emphasis*` only when the inner text is non-whitespace (cron expressions again).
  317. plain = re.sub(r"\*([^\s*][^*]*?[^\s*])\*", r"\1", plain)
  318. plain = re.sub(r"(?<!\w)_([^_]+)_(?!\w)", r"\1", plain)
  319. plain = re.sub(r"~~([^~]+)~~", r"\1", plain)
  320. plain = re.sub(r"\n{3,}", "\n\n", plain)
  321. return plain.strip("\n")
  322. _WINDOWS_PATH_WITH_DOT_SEGMENT_RE = re.compile(r"(?i)(?:\b[a-z]:\\|\\\\)[^\s`]*\\\.[^\s`]*")
  323. def _preserve_windows_dot_segments_for_markdown(text: str) -> str:
  324. r"""Double the ``\`` before hidden dirs in Windows paths: CommonMark reads ``\.`` as an escaped dot."""
  325. from cli import _WINDOWS_PATH_WITH_DOT_SEGMENT_RE
  326. if "\\." not in text:
  327. return text
  328. def _protect(match: re.Match[str]) -> str:
  329. return re.sub(r"(?<!\\)\\(?=\.)", r"\\\\", match.group(0))
  330. return _WINDOWS_PATH_WITH_DOT_SEGMENT_RE.sub(_protect, text)
  331. def _terminal_columns() -> int:
  332. try:
  333. return shutil.get_terminal_size((80, 24)).columns
  334. except Exception:
  335. return 80
  336. def _terminal_width_for_streaming() -> int:
  337. """Display cells inside the streamed response box (small margin for resize races)."""
  338. from cli import _STREAM_PAD, _terminal_columns
  339. return max(20, _terminal_columns() - len(_STREAM_PAD) - 2)
  340. def _render_final_assistant_content(text: str, mode: str = "render"):
  341. """Render final assistant content as markdown, stripped text, or raw text."""
  342. from cli import _preserve_windows_dot_segments_for_markdown, _rich_text_from_ansi, _strip_markdown_syntax, _terminal_columns, realign_markdown_tables
  343. from rich.markdown import Markdown
  344. # 1 border cell each side + margin so resize races don't push a borderline table into soft-wrap.
  345. panel_width = max(20, _terminal_columns() - 4)
  346. normalized_mode = str(mode or "render").strip().lower()
  347. if normalized_mode == "strip":
  348. # Strip first (inline markdown changes cell width), then re-align padding.
  349. return _RichText(realign_markdown_tables(_strip_markdown_syntax(text), panel_width))
  350. if normalized_mode == "raw":
  351. return _rich_text_from_ansi(text or "")
  352. # Normalising under-padded tables up front gives narrow-panel fallbacks consistent input.
  353. plain = _rich_text_from_ansi(text or "").plain
  354. plain = _preserve_windows_dot_segments_for_markdown(plain)
  355. plain = realign_markdown_tables(plain, panel_width)
  356. return Markdown(plain)
  357. def _post_stream_transform_output(response: str, result: dict | None) -> str:
  358. """Text still to display after a streamed response transform: the suffix, or the whole response when replaced."""
  359. if not result or not result.get("response_transformed"):
  360. return ""
  361. original = result.get("pre_transform_response") or ""
  362. if original and response.startswith(original):
  363. return response[len(original):]
  364. return f"\n{t('cli.render.response_transformed')}\n{response}"
  365. def _coerce_output_history_limit(value) -> int:
  366. try:
  367. return max(10, int(value))
  368. except (TypeError, ValueError):
  369. return 200
  370. def _clear_output_history() -> None:
  371. _cli()._OUTPUT_HISTORY.clear()
  372. _set_chrome_floor(None) # the screen is cleared with it
  373. def _output_history_recording() -> bool:
  374. return _cli()._OUTPUT_HISTORY_ENABLED and not _cli()._OUTPUT_HISTORY_REPLAYING and not _cli()._OUTPUT_HISTORY_SUPPRESSED
  375. def _record_output_history_entry(entry) -> None:
  376. from cli import _output_history_recording
  377. if _output_history_recording():
  378. _cli()._OUTPUT_HISTORY.append(entry)
  379. class _PaintedLine(str):
  380. """A recorded output line tagged with the terminal ``width`` it was painted at: a terminal
  381. that does not reflow keeps the rows it wrapped into then, whatever the width is now."""
  382. width = None
  383. def _painted_columns():
  384. """The width the terminal soft-wraps a print at right now, or ``None``."""
  385. from prompt_toolkit.application import get_app_or_none
  386. app = get_app_or_none()
  387. try:
  388. if app is not None:
  389. return app.output.get_size().columns
  390. return os.get_terminal_size(sys.__stdout__.fileno()).columns
  391. except (AttributeError, OSError, ValueError):
  392. return None
  393. def _record_output_history(text: str, *, force: bool = False) -> None:
  394. """Record ``text`` as painted now. ``force`` skips the recording check when the caller
  395. made it at print-request time (the print itself was deferred to the app loop)."""
  396. from cli import _output_history_recording
  397. if force or _output_history_recording():
  398. width = _painted_columns()
  399. lines = []
  400. # One entry per printed line: ``_pt_print`` ends every text with a newline, so "" and a
  401. # trailing "\n" are blank rows on screen and must count in the replay's row budget.
  402. for line in str(text).replace("\r", "").split("\n"):
  403. line = _PaintedLine(line)
  404. line.width = width
  405. lines.append(line)
  406. _cli()._OUTPUT_HISTORY.extend(lines)
  407. # prompt_toolkit's zero-width ``\001…\002`` brackets first: a crop that split one would leak its
  408. # escape to the ANSI parser (which quantizes an inline image's id color) or as text.
  409. _ANSI_SEQUENCE_RE = re.compile(r"\001[^\002]*\002|\x1b(?:\[[0-?]*[ -/]*[@-~]|\][^\x07\x1b]*(?:\x07|\x1b\\)|[@-Z\\-_])")
  410. def _ansi_drop_cells(line: str, cells: int) -> str:
  411. """``line`` without its first ``cells`` visible cells; escape sequences are kept for their styling."""
  412. from prompt_toolkit.utils import get_cwidth
  413. out, pos = [], 0
  414. for match in [*_ANSI_SEQUENCE_RE.finditer(line), None]:
  415. end = match.start() if match else len(line)
  416. for ch in line[pos:end]:
  417. if cells > 0:
  418. cells -= get_cwidth(ch)
  419. else:
  420. out.append(ch)
  421. if match:
  422. out.append(match.group())
  423. pos = match.end()
  424. return "".join(out)
  425. # TERM names the terminal the CLI talks to, a multiplexer included (tmux, GNU screen), and is set
  426. # by that terminal for its children — decisive whenever it names one. Everything else is inherited
  427. # from whatever started the shell (TMUX, KITTY_WINDOW_ID or TERM_PROGRAM=vscode leak into an st
  428. # or urxvt launched from there), so it only speaks for a generic TERM such as xterm-256color;
  429. # TMUX/STY first there: tmux with `default-terminal xterm-256color` (a common setup) hands its
  430. # panes a generic TERM plus TMUX, while XTERM_VERSION is only inherited from the outer xterm. Then
  431. # XTERM_VERSION: an xterm started from a VTE shell inherits VTE_VERSION but sets XTERM_VERSION.
  432. _REFLOW_TERM_PREFIXES = ("tmux", "screen", "xterm-kitty", "alacritty", "foot", "xterm-ghostty", "wezterm",
  433. "contour", "vte")
  434. # TERM is all that survives ssh; these prefixes name terminals that truncate rows in place.
  435. _NO_REFLOW_TERM_PREFIXES = ("linux", "st", "mosh", "vt", "cons", "rxvt")
  436. _MULTIPLEXER_ENV = ("TMUX", "STY")
  437. _NO_REFLOW_ENV = ("XTERM_VERSION",)
  438. _REFLOW_ENV = ("VTE_VERSION", "KITTY_WINDOW_ID", "WT_SESSION", "KONSOLE_VERSION",
  439. "ALACRITTY_WINDOW_ID", "WEZTERM_PANE", "GHOSTTY_RESOURCES_DIR")
  440. _REFLOW_TERM_PROGRAMS = ("iTerm.app", "Apple_Terminal", "WezTerm", "vscode", "ghostty", "Tabby", "Hyper")
  441. def _terminal_reflows() -> bool | None:
  442. """Whether the terminal re-wraps the rows it shows when its width changes: ``True``
  443. (tmux, GNU screen, VTE, kitty, iTerm2, Terminal.app, WezTerm, Alacritty, Windows Terminal —
  444. a shrink pushes the rows that grew into scrollback), ``False`` (xterm, st, urxvt, mosh, the
  445. Linux console keep every row in place, truncated) or ``None`` when nothing says (xterm or
  446. iTerm2 over ssh both look like ``TERM=xterm-256color``)."""
  447. env = os.environ
  448. term = env.get("TERM", "").lower()
  449. if term.startswith(_REFLOW_TERM_PREFIXES): # before "vt": TERM=vte-256color
  450. return True
  451. if term.startswith(_NO_REFLOW_TERM_PREFIXES):
  452. return False
  453. if any(env.get(name) for name in _MULTIPLEXER_ENV):
  454. return True
  455. if any(env.get(name) for name in _NO_REFLOW_ENV):
  456. return False
  457. if any(env.get(name) for name in _REFLOW_ENV) or env.get("TERM_PROGRAM") in _REFLOW_TERM_PROGRAMS:
  458. return True
  459. if env.get("LC_TERMINAL") == "iTerm2": # iTerm2 sets it so that ssh forwards it (LC_*)
  460. return True
  461. return None
  462. def _line_rows(line: str, columns: int) -> int:
  463. """Rows ``line`` fills when the terminal soft-wraps it at ``columns``."""
  464. from prompt_toolkit.formatted_text import ANSI, fragment_list_width, to_formatted_text
  465. width = fragment_list_width(to_formatted_text(ANSI(line)))
  466. return max(1, -(-width // columns)) if columns and columns > 0 else 1
  467. def _output_tail_fitting(lines: list[str], max_rows: int, columns: int, painted: bool = True) -> list[str]:
  468. """Newest ``lines`` filling ``max_rows`` rows, soft-wrapped at ``columns`` — or, with
  469. ``painted``, at the width each was painted at, as a terminal that does not reflow still
  470. shows it. The oldest one may only partly fit: its bottom rows are kept, the rows above
  471. them are already in scrollback."""
  472. kept, used = [], 0
  473. for line in reversed(lines):
  474. if used >= max_rows:
  475. break
  476. cols = (getattr(line, "width", None) if painted else None) or columns
  477. height = _line_rows(line, cols)
  478. if used + height > max_rows:
  479. kept.append(_ansi_drop_cells(line, (height - (max_rows - used)) * cols))
  480. break
  481. used += height
  482. kept.append(line)
  483. kept.reverse()
  484. return kept
  485. def _output_history_lines() -> list[str]:
  486. """The recorded output as the lines a replay paints (callable entries render now)."""
  487. rendered_lines = []
  488. for entry in tuple(_cli()._OUTPUT_HISTORY):
  489. lines = [entry]
  490. if callable(entry):
  491. try:
  492. lines = entry()
  493. except Exception:
  494. continue
  495. if isinstance(lines, str):
  496. lines = lines.splitlines()
  497. rendered_lines.extend(line if isinstance(line, str) else str(line) for line in lines)
  498. return rendered_lines
  499. def _output_history_rows(limit: int, columns: int, painted: bool):
  500. """Rows the whole recorded output fills (counted as ``_output_tail_fitting`` does), or
  501. ``None`` when that is ``limit`` rows or more."""
  502. if not _cli()._OUTPUT_HISTORY_ENABLED:
  503. return None
  504. total = 0
  505. for line in reversed(_output_history_lines()):
  506. total += _line_rows(line, (getattr(line, "width", None) if painted else None) or columns)
  507. if total >= limit:
  508. return None
  509. return total
  510. def _ansi_with_raw(text: str, raw: str = ""):
  511. """``ANSI(text)``, then *raw* as one zero-width fragment prompt_toolkit writes verbatim."""
  512. from cli import _PT_ANSI
  513. if not raw:
  514. return _PT_ANSI(text)
  515. from prompt_toolkit.formatted_text import to_formatted_text
  516. return [*to_formatted_text(_PT_ANSI(text)), ("[ZeroWidthEscape]", raw)]
  517. def _pt_print_ansi(text: str, raw: str = "") -> None:
  518. """``_pt_print(ANSI(text))``, falling back to ``print`` when stdout is not a real console."""
  519. from cli import _pt_print
  520. try:
  521. _pt_print(_ansi_with_raw(text, raw))
  522. except Exception:
  523. # NoConsoleScreenBufferError (Windows) / OSError when stdout is e.g. a worker log file.
  524. with suppress(Exception):
  525. print(text + raw)
  526. _HELD_PAINTS: list | None = None
  527. _HELD_PAINTS_LOCK = threading.Lock()
  528. _PAINT_SEQ = itertools.count()
  529. # ``(app, gate)`` while the CLI's app runs: ``gate()`` is True when output must wait for a resize
  530. # recovery (see ``CLITerminalMixin._output_waits_for_resize``).
  531. _PAINT_GATE = None
  532. def _set_paint_gate(app, gate) -> None:
  533. global _PAINT_GATE
  534. _PAINT_GATE = (app, gate) if gate is not None else None
  535. # Rows from the top of the prompt chrome down to the bottom of the screen, once a refill left
  536. # the chrome's top at a known row (``None``: unknown). prompt_toolkit learns that only through
  537. # CPR, which the CLI leaves off; drawn at least this tall, the chrome keeps reaching the bottom
  538. # row, where ``_transcript_room`` counts the transcript from, also when it shrinks (a narrower
  539. # status bar, a closed modal) — rows left blank below it would be counted as transcript (#95375).
  540. _CHROME_FLOOR = None
  541. def _set_chrome_floor(rows) -> None:
  542. global _CHROME_FLOOR
  543. _CHROME_FLOOR = rows
  544. def _chrome_floor():
  545. return _CHROME_FLOOR
  546. # Rows output scrolled into scrollback while the terminal's width changed under it: a terminal
  547. # that does not reflow may have truncated them first, if it resized before reading all of that
  548. # output. The next refill repaints them too — twice rather than truncated for good (#95375).
  549. _SUSPECT_ROWS = 0
  550. def _add_suspect_rows(rows: int) -> None:
  551. global _SUSPECT_ROWS
  552. _SUSPECT_ROWS += max(0, rows)
  553. def _take_suspect_rows() -> int:
  554. global _SUSPECT_ROWS
  555. rows, _SUSPECT_ROWS = _SUSPECT_ROWS, 0
  556. return rows
  557. def _hold_paints() -> None:
  558. """Hold ``_cprint`` paints until ``_release_paints``, while a resize awaits its recovery.
  559. A paint erases the prompt chrome from prompt_toolkit's cursor, which is stale once the
  560. terminal re-wrapped the chrome to its new width: rows of the old chrome would stay in the
  561. transcript, where the replay cannot account for them (#95375). On a terminal that does not
  562. reflow, a paint would scroll rows it truncated into scrollback before the refill restores them.
  563. """
  564. global _HELD_PAINTS
  565. with _HELD_PAINTS_LOCK:
  566. if _HELD_PAINTS is None:
  567. _HELD_PAINTS = []
  568. def _release_paints() -> None:
  569. """Paint, in the order they were requested, what ``_hold_paints`` held."""
  570. global _HELD_PAINTS
  571. with _HELD_PAINTS_LOCK:
  572. held, _HELD_PAINTS = _HELD_PAINTS or [], None
  573. for _seq, paint in sorted(held, key=lambda item: item[0]):
  574. with suppress(Exception):
  575. paint()
  576. def _paint_held(seq: int, paint, app=None) -> bool:
  577. """Queue ``paint`` (requested ``seq``-th) when paints are held — or must be, because the
  578. terminal's width changed under ``app`` and its recovery has not run yet."""
  579. gate = _PAINT_GATE
  580. if gate is not None and gate[0] is app and gate[1]():
  581. _hold_paints()
  582. with _HELD_PAINTS_LOCK:
  583. if _HELD_PAINTS is None:
  584. return False
  585. _HELD_PAINTS.append((seq, paint))
  586. return True
  587. def _cprint(text: str, *, raw: str = ""):
  588. """Print ANSI text through prompt_toolkit's renderer (patch_stdout swallows raw ANSI).
  589. From a background thread while an Application runs, a direct print races the input
  590. redraw and gets buried, so those are painted on the app's loop via ``call_soon_threadsafe``.
  591. *raw* is an escape sequence written verbatim right after *text* (an inline image's
  592. transmit): never parsed as ANSI, counted as zero cells, and kept out of the output
  593. history, so a resize replay repaints *text* alone and never crops or re-sends it.
  594. """
  595. from cli import _output_history_recording, _pt_print, _pt_print_ansi, _record_output_history
  596. recording = _output_history_recording()
  597. seq = next(_PAINT_SEQ)
  598. def _painted(paint):
  599. # Recorded when painted, not when requested: a redraw replaying the history must
  600. # neither print rows still queued for the loop nor size them at a stale width.
  601. def _paint():
  602. if recording:
  603. _record_output_history(text, force=True)
  604. paint()
  605. return _paint
  606. paint_pt = _painted(lambda: _pt_print(_ansi_with_raw(text, raw)))
  607. paint_fallback = _painted(lambda: _pt_print_ansi(text, raw))
  608. try:
  609. from prompt_toolkit.application import get_app_or_none, run_in_terminal
  610. except Exception:
  611. paint_pt()
  612. return
  613. try:
  614. app = get_app_or_none()
  615. except Exception:
  616. app = None
  617. if app is None or not getattr(app, "_is_running", False):
  618. _release_paints()
  619. paint_fallback()
  620. return
  621. import asyncio as _asyncio
  622. try:
  623. loop = app.loop # type: ignore[attr-defined]
  624. except Exception:
  625. loop = None
  626. try:
  627. # get_running_loop(): get_event_loop() warns from threads with no current loop.
  628. # Use get_running_loop() instead of get_event_loop() to avoid the DeprecationWarning /
  629. # RuntimeWarning emitted by Python 3.10+ when get_event_loop() is called from a thread that has no
  630. # current event loop set (e.g. the process_loop background thread). Fixes #19285.
  631. current_loop = _asyncio.get_running_loop()
  632. except Exception:
  633. current_loop = None
  634. if loop is None or (current_loop is loop and loop.is_running()):
  635. if not _paint_held(seq, paint_pt, app):
  636. paint_pt()
  637. return
  638. def _print_now():
  639. from prompt_toolkit.formatted_text import to_formatted_text
  640. from prompt_toolkit.renderer import print_formatted_text as _paint_formatted_text
  641. from prompt_toolkit.styles import Style
  642. _paint_formatted_text(app.output, to_formatted_text(_ansi_with_raw(text, raw)) + [("", "\n")], Style([]))
  643. paint_now = _painted(_print_now)
  644. def _schedule():
  645. if not getattr(app, "_is_running", False):
  646. paint_fallback()
  647. return
  648. if _paint_held(seq, _schedule, app):
  649. return
  650. with suppress(Exception):
  651. pending = getattr(app, "_running_in_terminal_f", None)
  652. if getattr(app, "_running_in_terminal", False) or (pending is not None and not pending.done()):
  653. # Another run_in_terminal body owns the terminal: paint after it. Never fall back
  654. # to a bare print on error: run_in_terminal may already have painted.
  655. import inspect as _inspect
  656. coro = run_in_terminal(lambda: _paint_held(seq, _schedule, app) or paint_now())
  657. if coro is not None and (_inspect.isawaitable(coro) or _inspect.iscoroutine(coro)):
  658. _asyncio.ensure_future(coro)
  659. return
  660. # What run_in_terminal does, but now: its erase and print would run a loop pass
  661. # later, when a resize may have landed after the check above (#95375).
  662. renderer = app.renderer
  663. floor = _CHROME_FLOOR
  664. if floor is not None: # the chrome, as tall as drawn, moves down by the rows printed
  665. screen = renderer._last_screen
  666. floor = max(floor, renderer._min_available_height, screen.height if screen else 0)
  667. columns = app.output.get_size().columns
  668. renderer.erase()
  669. paint_now()
  670. renderer.reset()
  671. printed = sum(_line_rows(line, columns) for line in text.split("\n"))
  672. if app.output.get_size().columns != columns:
  673. _add_suspect_rows(printed)
  674. if floor is not None:
  675. _set_chrome_floor(max(0, floor - printed))
  676. app._request_absolute_cursor_position()
  677. app._redraw()
  678. try:
  679. loop.call_soon_threadsafe(_schedule)
  680. except Exception:
  681. paint_fallback()
  682. def _prepend_note_to_message(message, note: str):
  683. """Prepend a one-shot note to a user message (str, or content-part list when an image is attached).
  684. For lists the note is folded into the first text part or inserted as a leading one.
  685. Unknown shapes are returned unchanged.
  686. """
  687. note = str(note or "").strip()
  688. if not note:
  689. return message
  690. if isinstance(message, str):
  691. return f"{note}\n\n{message}" if message else note
  692. if isinstance(message, list):
  693. parts = list(message)
  694. for i, part in enumerate(parts):
  695. if isinstance(part, dict) and part.get("type") == "text":
  696. text = part.get("text", "")
  697. parts[i] = {**part, "text": f"{note}\n\n{text}" if text else note}
  698. return parts
  699. return [{"type": "text", "text": note}, *parts]
  700. return message
  701. def _pt_app_is_running() -> bool:
  702. """Whether a prompt_toolkit Application currently owns the live terminal."""
  703. try:
  704. from prompt_toolkit.application import get_app_or_none
  705. app = get_app_or_none()
  706. except Exception:
  707. return False
  708. return app is not None and bool(getattr(app, "_is_running", False))
  709. def _cli_visible_print(text: str = "") -> None:
  710. """``print`` unless a prompt_toolkit Application owns the terminal (patch_stdout swallows bare prints)."""
  711. from cli import _cprint, _pt_app_is_running
  712. if _pt_app_is_running():
  713. _cprint(text)
  714. else:
  715. print(text)
  716. class ChatConsole:
  717. """Rich Console drop-in routing rendered ANSI through ``_cprint`` so colors survive patch_stdout."""
  718. def __init__(self):
  719. from io import StringIO
  720. self._buffer = StringIO()
  721. self._inner = Console(file=self._buffer, force_terminal=True, color_system="truecolor", highlight=False)
  722. def print(self, *args, **kwargs):
  723. from cli import _OSC_ESCAPE_RE, _cprint
  724. self._buffer.seek(0)
  725. self._buffer.truncate()
  726. self._inner.width = shutil.get_terminal_size((80, 24)).columns
  727. self._inner.print(*args, **kwargs)
  728. for line in _OSC_ESCAPE_RE.sub("", self._buffer.getvalue()).rstrip("\n").split("\n"):
  729. _cprint(line)
  730. @contextmanager
  731. def status(self, *_args, **_kwargs):
  732. """No-op ``console.status`` so slash helpers don't duplicate ``_busy_command()``'s indicator."""
  733. yield self
  734. def _build_compact_banner() -> str:
  735. """Build a compact banner that fits the current terminal width."""
  736. try:
  737. from hermes_cli.skin_engine import get_active_skin
  738. _skin = get_active_skin()
  739. except Exception:
  740. _skin = None
  741. def _color(key, default):
  742. return _skin.get_color(key, default) if _skin else default
  743. border_color = _color("banner_border", "#FFD700")
  744. title_color = _color("banner_title", "#FFBF00")
  745. dim_color = _color("banner_dim", "#B8860B")
  746. if (getattr(_skin, "name", "default") if _skin else "default") == "default":
  747. tiny_line = "☤ NOUS HERMES"
  748. else:
  749. tiny_line = _skin.get_branding("agent_name", "Hermes Agent") if _skin else "Hermes Agent"
  750. line1 = t("cli.render.banner_tagline", name=tiny_line)
  751. if os.environ.get("HERMES_FAST_STARTUP_BANNER") == "1":
  752. from hermes_cli import __release_date__ as _release_date
  753. from hermes_cli.version_info import get_version_info
  754. version_line = t("cli.render.banner_version", version=get_version_info().derived_version, date=_release_date)
  755. else:
  756. version_line = format_banner_version_label()
  757. w = min(shutil.get_terminal_size().columns - 2, 88)
  758. if w < 30:
  759. return f"\n[{title_color}]{tiny_line}[/] [dim {dim_color}]- Nous Research[/]\n"
  760. inner = w - 2 # inside the box border
  761. bar = "═" * w
  762. content_width = inner - 2
  763. line1 = line1[:content_width].ljust(content_width)
  764. line2 = version_line[:content_width].ljust(content_width)
  765. return (
  766. f"\n[bold {border_color}]╔{bar}╗[/]\n"
  767. f"[bold {border_color}]║[/] [{title_color}]{line1}[/] [bold {border_color}]║[/]\n"
  768. f"[bold {border_color}]║[/] [dim {dim_color}]{line2}[/] [bold {border_color}]║[/]\n"
  769. f"[bold {border_color}]╚{bar}╝[/]\n"
  770. )
  771. def _panel_box_width(title: str, content_lines: list[str], min_width: int = 46, max_width: int = 76) -> int:
  772. """Stable TUI panel width wide enough for the title and content (incl. borders)."""
  773. term_cols = shutil.get_terminal_size((100, 20)).columns
  774. longest = max([len(title)] + [len(line) for line in content_lines] + [min_width - 4])
  775. inner = min(max(longest + 4, min_width - 2), max_width - 2, max(24, term_cols - 6))
  776. return inner + 2 # leading/trailing space inside the borders
  777. def _wrap_panel_text(text: str, width: int, subsequent_indent: str = "", *, keep_ws: bool = False) -> list[str]:
  778. """Wrap panel text; ``keep_ws`` preserves whitespace (command/detail previews). Each source
  779. line wraps on its own: a newline inside a row would break the panel's right border."""
  780. kw = dict(replace_whitespace=False, drop_whitespace=False) if keep_ws else dict(break_long_words=False, break_on_hyphens=False)
  781. wrapped = [row for part in (text.expandtabs(4).splitlines() if keep_ws else [text])
  782. for row in (textwrap.wrap(part, width=max(8, width), subsequent_indent=subsequent_indent, **kw) or [""])]
  783. return wrapped or [""]
  784. _wrap_panel_text_keep_ws = functools.partial(_wrap_panel_text, keep_ws=True)
  785. def _append_panel_line(lines, border_style: str, content_style: str, text: str, box_width: int) -> None:
  786. lines.extend(((border_style, "│ "), (content_style, text.ljust(max(0, box_width - 2))), (border_style, " │\n")))
  787. def _append_blank_panel_line(lines, border_style: str, box_width: int) -> None:
  788. lines.append((border_style, "│" + (" " * box_width) + "│\n"))