diff --git a/hermes_cli/cli_stream_mixin.py b/hermes_cli/cli_stream_mixin.py index 5714a1dcda..828378f1df 100644 --- a/hermes_cli/cli_stream_mixin.py +++ b/hermes_cli/cli_stream_mixin.py @@ -17,6 +17,34 @@ from contextlib import contextmanager from pathlib import Path from rich.markup import escape as _escape +# Model-generated reasoning tags: suppressed during streaming (they'd display as raw XML; +# the agent strips them from final_response too) unless show_reasoning routes them to the box. +_OPEN_TAGS = ("", "", "", "", "", "") +_CLOSE_TAGS = tuple(" int: + try: + return shutil.get_terminal_size().columns + except Exception: + return default + class CLIStreamMixin: """Streaming output, reasoning preview, tool progress callbacks, and busy-command spinner for the interactive CLI""" @@ -32,11 +60,10 @@ class CLIStreamMixin: def _on_notice(self, notice) -> None: """Queue an out-of-band AgentNotice for rendering at the next clean boundary. - Notices fire from inside the agent turn (cold-start seed during _init_agent, - per-turn _capture_credits after the API call) — printing immediately races the - streaming response and the line gets buried behind the prompt (see _cprint's - bg-thread caveat). So we QUEUE here and flush in _flush_credit_notices(), called - right after run_conversation returns. Fail-soft: never break the turn. + Notices fire from inside the agent turn (cold-start seed during _init_agent, per-turn + _capture_credits after the API call) — printing immediately races the streaming + response and the line gets buried behind the prompt. Flushed by + _flush_credit_notices() right after run_conversation returns. Fail-soft. """ try: text = getattr(notice, "text", "") or "" @@ -50,30 +77,23 @@ class CLIStreamMixin: pass def _flush_credit_notices(self) -> None: - """Print any queued credit notices as level-colored lines. Called at turn end - (after run_conversation) where _cprint paints cleanly above the prompt.""" + """Print queued credit notices as level-colored lines at turn end (after + run_conversation) where _cprint paints cleanly above the prompt.""" from cli import _DIM, _RST, _cprint try: pending = getattr(self, "_pending_credit_notices", None) if not pending: return self._pending_credit_notices = [] + colors = {"error": "\033[31m", "warn": "\033[33m", "success": "\033[32m", "info": _DIM} for level, text in pending: - color = { - "error": "\033[31m", - "warn": "\033[33m", - "success": "\033[32m", - "info": _DIM, - }.get(level, _DIM) - _cprint(f" {color}{text}{_RST}") + _cprint(f" {colors.get(level, _DIM)}{text}{_RST}") except Exception: pass def _on_notice_clear(self, key: str) -> None: - """Notice cleared. The REPL prints lines (no persistent slot to wipe), so - this drops any still-queued notice with that key is not tracked by key here; - it's a no-op for rendering — kept so the agent's clear callback is bound - symmetrically with the show callback (and so future REPL UIs can hook it).""" + """No-op for the REPL (lines are printed, no persistent slot to wipe); kept so the + agent's clear callback is bound symmetrically with the show callback.""" return def _current_reasoning_callback(self): @@ -90,32 +110,23 @@ class CLIStreamMixin: preview_text = reasoning_text.strip() if not preview_text: return - - try: - term_width = shutil.get_terminal_size().columns - except Exception: - term_width = 80 prefix = " [thinking] " - wrap_width = max(30, term_width - len(prefix) - 2) + wrap_width = max(30, _terminal_columns() - len(prefix) - 2) paragraphs = [] - raw_paragraphs = re.split(r"\n\s*\n+", preview_text.replace("\r\n", "\n")) - for paragraph in raw_paragraphs: + for paragraph in re.split(r"\n\s*\n+", preview_text.replace("\r\n", "\n")): compact = " ".join(line.strip() for line in paragraph.splitlines() if line.strip()) if compact: paragraphs.append(textwrap.fill(compact, width=wrap_width)) preview_text = "\n".join(paragraphs) if not preview_text: return - if self.verbose: _cprint(f" {_DIM}[thinking] {preview_text}{_RST}") return - lines = preview_text.splitlines() if len(lines) > 5: - preview = "\n".join(lines[:5]) - preview += f"\n ... ({len(lines) - 5} more lines)" + preview = "\n".join(lines[:5]) + f"\n ... ({len(lines) - 5} more lines)" else: preview = preview_text _cprint(f" {_DIM}[thinking] {preview}{_RST}") @@ -123,47 +134,32 @@ class CLIStreamMixin: def _flush_reasoning_preview(self, *, force: bool = False) -> None: """Flush buffered reasoning text at natural boundaries. - Some providers stream reasoning in tiny word or punctuation chunks. - Buffer them here so the preview path does not print one `[thinking]` - line per token. + Some providers stream reasoning in tiny word/punctuation chunks; buffering keeps the + preview path from printing one `[thinking]` line per token. """ buf = getattr(self, "_reasoning_preview_buf", "") if not buf: return - - try: - term_width = shutil.get_terminal_size().columns - except Exception: - term_width = 80 - target_width = max(40, term_width - len(" [thinking] ") - 4) - + target_width = max(40, _terminal_columns() - len(" [thinking] ") - 4) flush_text = "" - if force: - flush_text = buf - buf = "" + flush_text, buf = buf, "" else: line_break = buf.rfind("\n") min_newline_flush = max(16, target_width // 3) if line_break != -1 and ( line_break >= min_newline_flush - or buf.endswith("\n\n") - or buf.endswith(".\n") - or buf.endswith("!\n") - or buf.endswith("?\n") - or buf.endswith(":\n") + or buf.endswith(("\n\n", ".\n", "!\n", "?\n", ":\n")) ): - flush_text = buf[: line_break + 1] - buf = buf[line_break + 1 :] + flush_text, buf = buf[: line_break + 1], buf[line_break + 1 :] elif len(buf) >= target_width: search_start = max(20, target_width // 2) search_end = min(len(buf), max(target_width + (target_width // 3), target_width + 8)) - cut = -1 - for boundary in (" ", "\t", ".", "!", "?", ",", ";", ":"): - cut = max(cut, buf.rfind(boundary, search_start, search_end)) + cut = max( + buf.rfind(b, search_start, search_end) for b in (" ", "\t", ".", "!", "?", ",", ";", ":") + ) if cut != -1: - flush_text = buf[: cut + 1] - buf = buf[cut + 1 :] + flush_text, buf = buf[: cut + 1], buf[cut + 1 :] self._reasoning_preview_buf = buf.lstrip() if flush_text else buf if flush_text: @@ -180,29 +176,21 @@ class CLIStreamMixin: if len(lines) <= 1: return f"[bold {_accent_hex()}]●[/] [bold]{_escape(user_input)}[/]{ts_suffix}" - first_lines = int(getattr(self, "user_message_preview_first_lines", 2)) - last_lines = int(getattr(self, "user_message_preview_last_lines", 2)) - first_lines = max(1, first_lines) - last_lines = max(0, last_lines) + first_lines = max(1, int(getattr(self, "user_message_preview_first_lines", 2))) + last_lines = max(0, int(getattr(self, "user_message_preview_last_lines", 2))) head = lines[:first_lines] - remaining_after_head = max(0, len(lines) - len(head)) - tail_count = min(last_lines, remaining_after_head) + tail_count = min(last_lines, max(0, len(lines) - len(head))) tail = lines[-tail_count:] if tail_count else [] - hidden_middle_count = len(lines) - len(head) - len(tail) if hidden_middle_count < 0: hidden_middle_count = 0 tail = [] - preview_lines = [ - f"[bold {_accent_hex()}]●[/] [bold]{_escape(head[0])}[/]{ts_suffix}" - ] + preview_lines = [f"[bold {_accent_hex()}]●[/] [bold]{_escape(head[0])}[/]{ts_suffix}"] preview_lines.extend(f"[bold]{_escape(line)}[/]" for line in head[1:]) - if hidden_middle_count > 0: noun = "line" if hidden_middle_count == 1 else "lines" preview_lines.append(f"[dim]... (+{hidden_middle_count} more {noun})[/]") - preview_lines.extend(f"[bold]{_escape(line)}[/]" for line in tail) return "\n".join(preview_lines) @@ -215,9 +203,8 @@ class CLIStreamMixin: def _expand_ref(match): path = Path(match.group(1)) - # Use try/except instead of path.exists() to avoid TOCTOU race: - # the paste file may be deleted between check and read, causing - # the input to be silently dropped (#17666). + # try/except rather than path.exists(): the paste file may be deleted between + # check and read (TOCTOU), silently dropping the input. try: return path.read_text(encoding="utf-8") except (OSError, IOError): @@ -239,12 +226,9 @@ class CLIStreamMixin: def _stream_reasoning_delta(self, text: str) -> None: """Stream reasoning/thinking tokens into a dim box above the response. - Opens a dim reasoning box on first token, streams line-by-line. - The box is closed automatically when content tokens start arriving - (via _stream_delta → _emit_stream_text). - - Once the response box is open, suppress any further reasoning - rendering — a late thinking block (e.g. after an interrupt) would + Opens the box on the first token; closed automatically when content tokens arrive + (_stream_delta → _emit_stream_text). Once the response box is open, further + reasoning is suppressed — a late thinking block (e.g. after an interrupt) would otherwise draw a reasoning box inside the response box. """ from cli import _DIM, _RST, _cprint @@ -253,8 +237,6 @@ class CLIStreamMixin: self._reasoning_shown_this_turn = True if getattr(self, "_stream_box_opened", False): return - - # Open reasoning box on first reasoning token if not getattr(self, "_reasoning_box_opened", False): self._reasoning_box_opened = True w = self._scrollback_box_width() @@ -263,9 +245,8 @@ class CLIStreamMixin: _cprint(f"\n{_DIM}┌─{r_label}{'─' * max(r_fill - 1, 0)}┐{_RST}") self._reasoning_buf = getattr(self, "_reasoning_buf", "") + text - - # Emit complete lines, and force-flush long partial lines so - # reasoning is visible in real-time even without newlines. + # Emit complete lines; force-flush long partial lines so reasoning is visible in + # real-time even without newlines. while "\n" in self._reasoning_buf: line, self._reasoning_buf = self._reasoning_buf.split("\n", 1) _cprint(f"{_DIM}{line}{_RST}") @@ -274,38 +255,30 @@ class CLIStreamMixin: self._reasoning_buf = "" def _close_reasoning_box(self) -> None: - """Close the live reasoning box if it's open.""" + """Close the live reasoning box if it's open, then flush deferred content.""" from cli import _DIM, _RST, _cprint - if getattr(self, "_reasoning_box_opened", False): - # Flush remaining reasoning buffer - buf = getattr(self, "_reasoning_buf", "") - if buf: - _cprint(f"{_DIM}{buf}{_RST}") - self._reasoning_buf = "" - w = self._scrollback_box_width() - _cprint(f"{_DIM}└{'─' * (w - 2)}┘{_RST}") - self._reasoning_box_opened = False - - # Flush any content that was deferred while reasoning was rendering. - deferred = getattr(self, "_deferred_content", "") - if deferred: - self._deferred_content = "" - self._emit_stream_text(deferred) + if not getattr(self, "_reasoning_box_opened", False): + return + buf = getattr(self, "_reasoning_buf", "") + if buf: + _cprint(f"{_DIM}{buf}{_RST}") + self._reasoning_buf = "" + w = self._scrollback_box_width() + _cprint(f"{_DIM}└{'─' * (w - 2)}┘{_RST}") + self._reasoning_box_opened = False + deferred = getattr(self, "_deferred_content", "") + if deferred: + self._deferred_content = "" + self._emit_stream_text(deferred) def _stream_delta(self, text) -> None: """Line-buffered streaming callback for real-time token rendering. - Receives text deltas from the agent as tokens arrive. Buffers - partial lines and emits complete lines via _cprint to work - reliably with prompt_toolkit's patch_stdout. - - Reasoning/thinking blocks (, , etc.) - are suppressed during streaming since they'd display raw XML tags. - The agent strips them from the final response anyway. - - A ``None`` value signals an intermediate turn boundary (tools are - about to execute). Flushes any open boxes and resets state so - tool feed lines render cleanly between turns. + Buffers partial lines and emits complete lines via _cprint (reliable under + prompt_toolkit's patch_stdout). Reasoning tags are suppressed — or routed to the + reasoning box when show_reasoning is on. ``None`` signals an intermediate turn + boundary (tools about to execute): flush open boxes and reset state so tool feed + lines render cleanly between turns. """ if text is None: self._flush_stream() @@ -313,38 +286,19 @@ class CLIStreamMixin: return if not text: return - self._stream_started = True - - # ── Tag-based reasoning suppression ── - # Track whether we're inside a reasoning/thinking block. - # These tags are model-generated (system prompt tells the model - # to use them) and get stripped from final_response. We must - # suppress them during streaming too — unless show_reasoning is - # enabled, in which case we route the inner content to the - # reasoning display box instead of discarding it. - _OPEN_TAGS = ("", "", "", "", "", "") - _CLOSE_TAGS = ("", "", "", "", "", "") - - # Append to a pre-filter buffer first self._stream_prefilt = getattr(self, "_stream_prefilt", "") + text - # Check if we're entering a reasoning block. - # Only match tags that appear at a "block boundary": start of the - # stream, after a newline (with optional whitespace), or when nothing - # but whitespace has been emitted on the current line. - # This prevents false positives when models *mention* tags in prose - # like "(/think not producing tags)". - # - # _stream_last_was_newline tracks whether the last character emitted - # (or the start of the stream) is a line boundary. It's True at - # stream start and set True whenever emitted text ends with '\n'. + # Open tags only count at a "block boundary": stream start, after a newline (plus + # optional whitespace), or when only whitespace has been emitted on the current + # line. Prevents false positives when models *mention* tags in prose like + # "(/think not producing tags)". _stream_last_was_newline is True at + # stream start and whenever emitted text ends with '\n'. if not hasattr(self, "_stream_last_was_newline"): - self._stream_last_was_newline = True # start of stream = boundary + self._stream_last_was_newline = True if not getattr(self, "_in_reasoning_block", False): - # Case-insensitive matching against a lowercased view so - # mixed-case tag variants (, , …) are caught. + # Lowercased view so mixed-case variants (, , …) are caught. prefilt_lower = self._stream_prefilt.lower() for tag in _OPEN_TAGS: tag_lower = tag.lower() @@ -353,44 +307,30 @@ class CLIStreamMixin: idx = prefilt_lower.find(tag_lower, search_start) if idx == -1: break - # Check if this is a block boundary position preceding = self._stream_prefilt[:idx] - if idx == 0: - # At buffer start — only a boundary if we're at - # a line start (stream start or last emit ended - # with newline) - is_block_boundary = getattr(self, "_stream_last_was_newline", True) + last_nl = preceding.rfind("\n") + if last_nl == -1: + # No newline in buffer — boundary only if the last emit was a + # newline AND only whitespace precedes the tag (idx == 0 included). + is_block_boundary = ( + getattr(self, "_stream_last_was_newline", True) + and preceding.strip() == "" + ) else: - # Find last newline in the buffer before the tag - last_nl = preceding.rfind("\n") - if last_nl == -1: - # No newline in buffer — boundary only if - # last emit was a newline AND only whitespace - # has accumulated before the tag - is_block_boundary = ( - getattr(self, "_stream_last_was_newline", True) - and preceding.strip() == "" - ) - else: - # Text between last newline and tag must be - # whitespace-only - is_block_boundary = preceding[last_nl + 1:].strip() == "" + is_block_boundary = preceding[last_nl + 1:].strip() == "" if is_block_boundary: - # Emit everything before the tag if preceding: self._emit_stream_text(preceding) self._stream_last_was_newline = preceding.endswith("\n") self._in_reasoning_block = True self._stream_prefilt = self._stream_prefilt[idx + len(tag):] break - # Not a block boundary — keep searching after this occurrence search_start = idx + 1 if getattr(self, "_in_reasoning_block", False): break - # Could also be a partial open tag at the end — hold it back if not getattr(self, "_in_reasoning_block", False): - # Check for partial tag match at the end (case-insensitive) + # Hold back a possible partial open tag at the end (case-insensitive). safe = self._stream_prefilt for tag in _OPEN_TAGS: tag_lower = tag.lower() @@ -404,72 +344,88 @@ class CLIStreamMixin: self._stream_prefilt = self._stream_prefilt[len(safe):] return - # Inside a reasoning block — look for close tag. - # Keep accumulating _stream_prefilt because close tags can arrive - # split across multiple tokens (e.g. "..."). + # Inside a reasoning block — look for a close tag. Keep accumulating because close + # tags can arrive split across tokens ("..."). if getattr(self, "_in_reasoning_block", False): prefilt_lower = self._stream_prefilt.lower() for tag in _CLOSE_TAGS: idx = prefilt_lower.find(tag.lower()) if idx != -1: self._in_reasoning_block = False - # When show_reasoning is on, route inner content to - # the reasoning display box instead of discarding. if self.show_reasoning: inner = self._stream_prefilt[:idx] if inner: self._stream_reasoning_delta(inner) after = self._stream_prefilt[idx + len(tag):] self._stream_prefilt = "" - # Process remaining text after close tag through full - # filtering (it could contain another open tag) + # Text after the close tag goes through full filtering (it could + # contain another open tag). if after: self._stream_delta(after) return - # When show_reasoning is on, stream reasoning content live - # instead of silently accumulating. Keep only the tail that - # could be a partial close tag prefix. - max_tag_len = max(len(t) for t in _CLOSE_TAGS) - if len(self._stream_prefilt) > max_tag_len: + # Stream reasoning live when show_reasoning is on; keep only the tail that + # could be a partial close-tag prefix. + if len(self._stream_prefilt) > _MAX_CLOSE_TAG_LEN: if self.show_reasoning: - # Route the safe prefix to reasoning display - safe_reasoning = self._stream_prefilt[:-max_tag_len] - self._stream_reasoning_delta(safe_reasoning) - self._stream_prefilt = self._stream_prefilt[-max_tag_len:] + self._stream_reasoning_delta(self._stream_prefilt[:-_MAX_CLOSE_TAG_LEN]) + self._stream_prefilt = self._stream_prefilt[-_MAX_CLOSE_TAG_LEN:] return + def _emit_stream_line(self, printed_line: str) -> None: + """Print one response line with the skin's true-color text escape (if any).""" + from cli import _RST, _STREAM_PAD, _cprint + _tc = getattr(self, "_stream_text_ansi", "") + _cprint( + f"{_STREAM_PAD}{_tc}{printed_line}{_RST}" if _tc else f"{_STREAM_PAD}{printed_line}" + ) + + def _flush_stream_table_buf(self) -> None: + """Emit the held table block re-aligned as a whole. + + Cell-level markdown (`code`, **bold**, ~~strike~~) is stripped FIRST so the + realigner pads to the final visible cell width, not the marker-decorated width. + """ + from cli import ( + _strip_markdown_syntax, + _terminal_width_for_streaming, + realign_markdown_tables, + ) + buf = self._stream_table_buf + self._stream_table_buf = [] + self._in_stream_table = False + if not buf: + return + joined = "\n".join(buf) + if self.final_response_markdown == "strip": + joined = _strip_markdown_syntax(joined) + block = realign_markdown_tables(joined, _terminal_width_for_streaming()) + for ln in block.split("\n"): + self._emit_stream_line(ln) + def _emit_stream_text(self, text: str) -> None: """Emit filtered text to the streaming display.""" from cli import ( HermesCLI, _ACCENT, _RST, - _STREAM_PAD, _STREAM_PARTIAL_PREVIEW_LEN, _cprint, _strip_markdown_syntax, - _terminal_width_for_streaming, datetime, is_table_divider, looks_like_table_row, - realign_markdown_tables, ) if not text: return - - # When show_reasoning is on and reasoning is still rendering, - # defer content until the reasoning box closes. This ensures the - # reasoning block always appears BEFORE the response in the terminal. + # While the reasoning box is still rendering, defer content so the reasoning block + # always appears BEFORE the response. if self.show_reasoning and getattr(self, "_reasoning_box_opened", False): self._deferred_content = getattr(self, "_deferred_content", "") + text return - - # Close the live reasoning box before opening the response box self._close_reasoning_box() # Open the response box header on the very first visible text if not self._stream_box_opened: - # Strip leading whitespace/newlines before first visible content text = text.lstrip("\n") if not text: return @@ -482,12 +438,9 @@ class CLIStreamMixin: except Exception: label = "⚕ Hermes" _text_hex = "#FFF8DC" - # Build a true-color ANSI escape for the response text color - # so streamed content matches the Rich Panel appearance. + # True-color ANSI escape so streamed content matches the Rich Panel appearance. try: - _r = int(_text_hex[1:3], 16) - _g = int(_text_hex[3:5], 16) - _b = int(_text_hex[5:7], 16) + _r, _g, _b = (int(_text_hex[i:i + 2], 16) for i in (1, 3, 5)) self._stream_text_ansi = f"\033[38;2;{_r};{_g};{_b}m" except (ValueError, IndexError): self._stream_text_ansi = "" @@ -498,68 +451,29 @@ class CLIStreamMixin: _cprint(f"\n{_ACCENT}╭─{label}{'─' * max(fill - 1, 0)}╮{_RST}") self._stream_buf += text - - # Emit complete lines, keep partial remainder in buffer - _tc = getattr(self, "_stream_text_ansi", "") - - def _emit_one(printed_line: str) -> None: - _cprint(f"{_STREAM_PAD}{_tc}{printed_line}{_RST}" if _tc else f"{_STREAM_PAD}{printed_line}") - - def _flush_table_buf() -> None: - buf = self._stream_table_buf - self._stream_table_buf = [] - self._in_stream_table = False - if not buf: - return - # Strip cell-level markdown (`code`, **bold**, ~~strike~~) FIRST - # so the realigner pads to the final visible cell width, not - # the marker-decorated source width. Otherwise a body row - # like `` | Bold | `**bold**` | `` lands narrower than its - # header column once the markers are removed. - joined = "\n".join(buf) - if self.final_response_markdown == "strip": - joined = _strip_markdown_syntax(joined) - block = realign_markdown_tables(joined, _terminal_width_for_streaming()) - for ln in block.split("\n"): - _emit_one(ln) - while "\n" in self._stream_buf: line, self._stream_buf = self._stream_buf.split("\n", 1) - - # Hold table-shaped lines in a side-buffer so we can re-pad - # the whole block once it ends. Streaming line-by-line, we - # cannot re-align mid-table without reflowing already-printed - # rows; the cost is that the user sees the table appear in a - # single batch when the block closes instead of row-by-row. + # Table-shaped lines are held in a side-buffer and re-padded as a block once it + # ends (streaming line-by-line we cannot re-align already-printed rows); the + # table appears in one batch when the block closes. if self._in_stream_table: if looks_like_table_row(line) or is_table_divider(line): self._stream_table_buf.append(line) continue - # Block ended — flush the realigned table, then fall - # through to print the current (non-table) line. - _flush_table_buf() + self._flush_stream_table_buf() elif looks_like_table_row(line): self._stream_table_buf.append(line) self._in_stream_table = True continue - if self.final_response_markdown == "strip": line = _strip_markdown_syntax(line) - _emit_one(line) + self._emit_stream_line(line) - # Long partial lines are emitted ONLY at real newlines — we no - # longer hard-wrap paragraphs at terminal width ourselves. Each - # logical line lands in scrollback as one line; the TERMINAL - # soft-wraps it visually, and emulators (iTerm2/kitty/VTE/ - # xterm.js/Windows Terminal) rejoin soft-wrapped rows on copy, - # so highlight-copy yields the original unwrapped text — same - # outcome as the TUI's selection copy. (The pre-July-2026 chunk - # emitter baked real '\n's into every long paragraph, which is - # exactly what polluted copy/paste.) - # - # TTFT perception: while a long opening paragraph accumulates - # without a newline, mirror its tail into the status-bar spinner - # line so the user sees tokens arriving instead of a blank box. + # Long partial lines are emitted ONLY at real newlines — no hard-wrapping at + # terminal width: each logical line lands in scrollback as one line the terminal + # soft-wraps, so highlight-copy yields the original unwrapped text. For TTFT + # perception, mirror the tail of a long unfinished paragraph into the status-bar + # spinner so the user sees tokens arriving instead of a blank box. if ( self._stream_buf and not self._in_stream_table @@ -581,32 +495,23 @@ class CLIStreamMixin: from cli import ( _ACCENT, _RST, - _STREAM_PAD, _cprint, _strip_markdown_syntax, - _terminal_width_for_streaming, is_table_divider, looks_like_table_row, - realign_markdown_tables, ) - # If we're still inside a "reasoning block" at end-of-stream, it was - # a false positive — the model mentioned a tag like in prose - # but never closed it. Recover the buffered content as regular text. + # Still inside a "reasoning block" at end-of-stream means a false positive — the + # model mentioned a tag like in prose and never closed it. Recover the + # buffered content as regular text. if getattr(self, "_in_reasoning_block", False) and getattr(self, "_stream_prefilt", ""): self._in_reasoning_block = False self._emit_stream_text(self._stream_prefilt) self._stream_prefilt = "" - # Close reasoning box if still open (in case no content tokens arrived) self._close_reasoning_box() - _tc = getattr(self, "_stream_text_ansi", "") - - # If the stream buffer has a trailing partial line that looks like - # a table row, fold it into the table buffer so the whole block - # gets re-aligned together. Otherwise the final row prints raw - # (with the model's original under-padded spacing) while the rows - # above it are aligned. + # A trailing partial line that looks like a table row joins the table buffer so + # the whole block is re-aligned together (else the final row prints under-padded). if ( self._stream_buf and getattr(self, "_in_stream_table", False) @@ -614,25 +519,12 @@ class CLIStreamMixin: ): self._stream_table_buf.append(self._stream_buf) self._stream_buf = "" - - # Flush any buffered table rows first so their padding is - # finalised before the stream remainder lands. if getattr(self, "_stream_table_buf", None): - joined = "\n".join(self._stream_table_buf) - self._stream_table_buf = [] - self._in_stream_table = False - if self.final_response_markdown == "strip": - joined = _strip_markdown_syntax(joined) - block = realign_markdown_tables(joined, _terminal_width_for_streaming()) - for ln in block.split("\n"): - _cprint(f"{_STREAM_PAD}{_tc}{ln}{_RST}" if _tc else f"{_STREAM_PAD}{ln}") - + self._flush_stream_table_buf() if self._stream_buf: line = _strip_markdown_syntax(self._stream_buf) if self.final_response_markdown == "strip" else self._stream_buf - _cprint(f"{_STREAM_PAD}{_tc}{line}{_RST}" if _tc else f"{_STREAM_PAD}{line}") + self._emit_stream_line(line) self._stream_buf = "" - - # Close the response box if self._stream_box_opened: w = self._scrollback_box_width() _cprint(f"{_ACCENT}╰{'─' * (w - 2)}╯{_RST}") @@ -656,38 +548,26 @@ class CLIStreamMixin: def _slow_command_status(self, command: str) -> str: """Return a user-facing status message for slower slash commands.""" cmd_lower = command.lower().strip() - if cmd_lower.startswith("/skills search"): - return "Searching skills..." - if cmd_lower.startswith("/skills browse"): - return "Loading skills..." - if cmd_lower.startswith("/skills inspect"): - return "Inspecting skill..." - if cmd_lower.startswith("/skills install"): - return "Installing skill..." - if cmd_lower.startswith("/skills"): - return "Processing skills command..." - if cmd_lower == "/reload-mcp": - return "Reloading MCP servers..." - if cmd_lower == "/reload-skills" or cmd_lower == "/reload_skills": - return "Reloading skills..." - if cmd_lower.startswith("/browser"): - return "Configuring browser..." + exact = _SLOW_COMMAND_STATUS_EXACT.get(cmd_lower) + if exact: + return exact + for prefix, status in _SLOW_COMMAND_STATUS: + if cmd_lower.startswith(prefix): + return status return "Processing command..." def _command_spinner_frame(self) -> str: """Return the current spinner frame for slow slash commands.""" from cli import _COMMAND_SPINNER_FRAMES - frame_idx = int(time.monotonic() * 10) % len(_COMMAND_SPINNER_FRAMES) - return _COMMAND_SPINNER_FRAMES[frame_idx] + return _COMMAND_SPINNER_FRAMES[int(time.monotonic() * 10) % len(_COMMAND_SPINNER_FRAMES)] @contextmanager def _busy_command(self, status: str, *, blocks_input: bool = True): """Expose a temporary busy state in the TUI while a slash command runs. - Most synchronous slash commands must reserve the composer because their - completion changes the active session state. Manual compression is safe - to draft through: the queued input is processed against the compacted - history after the command completes. + Most synchronous slash commands must reserve the composer because their completion + changes the active session state. Manual compression is safe to draft through: the + queued input is processed against the compacted history afterwards. """ previous_blocks_input = getattr(self, "_command_blocks_input", False) self._command_running = True @@ -706,14 +586,10 @@ class CLIStreamMixin: def _preprocess_images_with_vision(self, text: str, images: list, *, announce: bool = True) -> str: """Analyze attached images via the vision tool and return enriched text. - Instead of embedding raw base64 ``image_url`` content parts in the - conversation (which only works with vision-capable models), this - pre-processes each image through the auxiliary vision model (Gemini - Flash) and prepends the descriptions to the user's message — the - same approach the messaging gateway uses. - - The local file path is included so the agent can re-examine the - image later with ``vision_analyze`` if needed. + Instead of embedding raw base64 ``image_url`` parts (vision-capable models only), + each image is described by the auxiliary vision model and the descriptions are + prepended to the user's message — the same approach the messaging gateway uses. + The local path is included so the agent can re-examine via ``vision_analyze``. """ from cli import _DIM, _RST, _cprint import asyncio as _asyncio @@ -724,7 +600,6 @@ class CLIStreamMixin: "Include any text, code, data, objects, people, layout, colors, " "and any other notable visual information." ) - enriched_parts = [] for img_path in images: if not img_path.exists(): @@ -763,62 +638,45 @@ class CLIStreamMixin: if announce: _cprint(f" {_DIM}⚠ vision analysis error — path included for retry{_RST}") - # Combine: vision descriptions first, then the user's original text + # Vision descriptions first, then the user's original text user_text = text if isinstance(text, str) and text else "" if enriched_parts: prefix = "\n\n".join(enriched_parts) return f"{prefix}\n\n{user_text}" if user_text else prefix return user_text or "What do you see in this image?" - def _output_console(self): - """Use prompt_toolkit-safe Rich rendering once the TUI is live.""" - from cli import ChatConsole - if getattr(self, "_app", None): - return ChatConsole() - return self.console - def _console_print(self, *args, **kwargs): - """Print through the active command-safe console.""" - self._output_console().print(*args, **kwargs) + """Print through the active command-safe console (prompt_toolkit-safe Rich once the + TUI is live, else the plain console).""" + from cli import ChatConsole + console = ChatConsole() if getattr(self, "_app", None) else self.console + console.print(*args, **kwargs) def _on_tool_gen_start(self, tool_name: str) -> None: """Called when the model begins generating tool-call arguments. - Closes any open streaming boxes (reasoning / response) exactly once, - then prints a short status line so the user sees activity instead of - a frozen screen while a large payload (e.g. 45 KB write_file) streams. + Closes any open streaming boxes exactly once, then prints a short status line so + the user sees activity while a large payload (e.g. 45 KB write_file) streams. """ from cli import _cprint if getattr(self, "_stream_box_opened", False): self._flush_stream() self._stream_box_opened = False self._close_reasoning_box() - from agent.display import get_tool_emoji - emoji = get_tool_emoji(tool_name, default="⚡") - _cprint(f" ┊ {emoji} preparing {tool_name}…") + _cprint(f" ┊ {get_tool_emoji(tool_name, default='⚡')} preparing {tool_name}…") def _on_tool_progress(self, event_type: str, function_name: str = None, preview: str = None, function_args: dict = None, **kwargs): - """Called on tool lifecycle events (tool.started, tool.completed, reasoning.available, etc.). + """Called on tool lifecycle events (tool.started, tool.completed, reasoning.available, …). - Updates the TUI spinner widget so the user can see what the agent - is doing during tool execution (fills the gap between thinking - spinner and next response). - - On tool.started, records a monotonic timestamp so get_spinner_text() - can show a live elapsed timer (the TUI poll loop already invalidates - every ~0.15s, so the counter updates automatically). - - When tool_progress_mode is "all" or "new", also prints a persistent - stacked line to scrollback on tool.completed so users can see the - full history of tool calls (not just the current one in the spinner). + Updates the TUI spinner so the user can see what the agent is doing during tool + execution. tool.started records a monotonic timestamp for the live elapsed timer. + When tool_progress_mode is "all"/"new"/"verbose", tool.completed also prints a + persistent stacked scrollback line (full tool-call history, not just the spinner). """ from cli import CLI_CONFIG, _DIM, _RST, _cprint, _hermes_home - # MoA reference-model outputs: render each reference's answer as a - # labelled thinking-style block BEFORE the aggregator acts, so the user - # sees the mixture-of-agents process instead of a silent pause. These - # are display-only events emitted by the MoA facade (agent_init relay); - # they never enter message history. + # MoA reference-model outputs: display-only events from the MoA facade; render each + # reference's answer as a labelled thinking-style block BEFORE the aggregator acts. if event_type == "moa.reference": label = function_name or "reference" text = preview or "" @@ -833,7 +691,6 @@ class CLIStreamMixin: try: self._emit_reasoning_preview(text) except Exception: - # Fallback: print the raw text dimmed if the preview helper fails. if text.strip(): _cprint(f" {_DIM}{text.strip()}{_RST}") self._invalidate() @@ -844,8 +701,8 @@ class CLIStreamMixin: self._invalidate() return - # Feed the pet: tools mean "running" (not reasoning); a failed tool - # latches the turn so it ends on a sulk. + # Feed the pet: tools mean "running" (not reasoning); a failed tool latches the + # turn so it ends on a sulk. if event_type == "tool.started": self._pet_reasoning = False elif event_type == "tool.completed" and kwargs.get("is_error"): @@ -855,29 +712,20 @@ class CLIStreamMixin: if event_type == "tool.completed": self._tool_start_time = 0.0 - # Per-turn accounting: this feed already sees every tool call with - # its result, so the summary line needs no agent-loop state. + # Per-turn accounting: this feed sees every tool call with its result. self._turn_summary_record( function_name, kwargs.get("result"), kwargs.get("is_error", False) ) - # Focus view: count the scrollback line we are NOT printing, so the - # post-turn recovery line can report how much was hidden. Counted - # against the pre-focus tool-progress mode, so a user who already - # had /verbose off is never told focus hid something it didn't. + # Focus view: count the scrollback line we are NOT printing so the post-turn + # recovery line can report how much was hidden. if getattr(self, "_focus_view_enabled", False): try: self._note_focus_hidden_line(function_name or "") except Exception: pass - # Print stacked scrollback line for "new" / "all" / "verbose" modes. - # "verbose" was previously omitted here, so non-streaming model - # calls (MoA aggregator, copilot-acp) rendered each tool only into - # the transient spinner line — which overwrites itself, so no - # scrollable tool history accumulated. Streaming models hid the bug - # because _on_tool_gen_start commits a "preparing" line per tool; - # non-streaming calls never emit that, leaving verbose mode with no - # committed line at all. "verbose" is strictly more than "all", so - # it must commit at least the same line. + # "verbose" must commit at least the same line as "all": non-streaming model + # calls (MoA aggregator, copilot-acp) never emit _on_tool_gen_start's + # "preparing" line, so without this no scrollable tool history accumulates. if function_name and self.tool_progress_mode in {"new", "all", "verbose"}: duration = kwargs.get("duration", 0.0) # Pop stored args from tool.started for this function @@ -896,12 +744,9 @@ class CLIStreamMixin: _cprint(f" {line}") except Exception: pass - # First-touch onboarding: on the first tool in this process - # that takes longer than the threshold while we're in the - # noisiest progress mode, print a one-time hint about - # /verbose. Latched on self so it fires at most once per - # process; persisted to config.yaml so it never fires again - # across processes either. + # First-touch onboarding: one-time /verbose hint on the first long tool in + # the noisiest mode. Latched on self (once per process) and persisted to + # config.yaml (never again across processes). try: if ( not getattr(self, "_long_tool_hint_fired", False) @@ -926,14 +771,12 @@ class CLIStreamMixin: if event_type != "tool.started": return if function_name and not function_name.startswith("_"): - from agent.display import get_tool_emoji - emoji = get_tool_emoji(function_name) + from agent.display import get_tool_emoji, get_tool_preview_max_len label = preview or function_name - from agent.display import get_tool_preview_max_len _pl = get_tool_preview_max_len() if _pl > 0 and len(label) > _pl: label = label[:_pl - 3] + "..." - self._spinner_text = f"{emoji} {label}" + self._spinner_text = f"{get_tool_emoji(function_name)} {label}" self._tool_start_time = time.monotonic() # Store args for stacked scrollback line on completion self._pending_tool_info.setdefault(function_name, []).append( @@ -946,7 +789,6 @@ class CLIStreamMixin: from cli import logger try: from agent.display import capture_local_edit_snapshot - snapshot = capture_local_edit_snapshot(function_name, function_args) if snapshot is not None: self._pending_edit_snapshots[tool_call_id] = snapshot @@ -956,9 +798,9 @@ class CLIStreamMixin: def _on_tool_complete(self, tool_call_id: str, function_name: str, function_args: dict, function_result: str): """Render file edits with inline diff after write-capable tools complete.""" from cli import _cprint, logger - # A top-level delegate_task dispatches in the background and re-enters as - # a fresh turn when done. Say so once — no spinner, nothing to poll — so - # the idle prompt doesn't read as "nothing happened" (⛓ tracks the work). + # A top-level delegate_task dispatches in the background and re-enters as a fresh + # turn when done. Say so once — no spinner, nothing to poll — so the idle prompt + # doesn't read as "nothing happened" (⛓ tracks the work). if function_name == "delegate_task": try: parsed = json.loads(function_result) if isinstance(function_result, str) else (function_result or {}) @@ -974,7 +816,6 @@ class CLIStreamMixin: snapshot = self._pending_edit_snapshots.pop(tool_call_id, None) try: from agent.display import render_edit_diff_with_delta - render_edit_diff_with_delta( function_name, function_result,