Skip to content

The agent that grows with you

refactor(cli,tui): move interrupt + session-tool handlers to topical modules

Austin Pickett4c3f816011c3
diff --git a/hermes_cli/cli_commands_mixin.py b/hermes_cli/cli_commands_mixin.py
index 51f2914dfa0..49782c48019 100644
--- a/hermes_cli/cli_commands_mixin.py
+++ b/hermes_cli/cli_commands_mixin.py
@@ -8,7 +8,6 @@ from __future__ import annotations
import argparse
import atexit
-import importlib
import io
import json
import os
@@ -33,86 +32,15 @@ from agent.i18n import t
from agent.message_metadata import message_identity
from agent.turn_context import extract_api_content_sidecar
from hermes_cli.cli_agent_setup_mixin import _retire_agent
+from hermes_cli.cli_commands_session_tools import ( # noqa: F401 moved there (ratchet); re-imported so existing `from cli_commands_mixin import _t` consumers keep working
+ CLICommandsSessionToolsMixin, _TTYBuf, _accent, _accent_line, _command_arg, _cp, _dim,
+ _dim_line, _gt, _lines, _pr, _probe, _save, _say_block, _shlex_args, _t, _tn)
from hermes_cli.browser_connect import (
DEFAULT_BROWSER_CDP_URL, discover_local_cdp_url, find_free_debug_port, is_browser_debug_ready,
launch_chrome_debug, local_port_in_use, manual_chrome_debug_command)
# Output helpers. Slash-command text is user-visible: every literal below is load-bearing.
-def _cp(*lines: str) -> None:
- """``_cprint`` each line (lazy import: cli.py imports this module)."""
- from cli import _cprint
- for line in lines:
- _cprint(line)
-
-
-def _pr(*lines: str) -> None:
- """print() each line."""
- for line in lines:
- print(line)
-
-
-def _save(key: str, value) -> bool:
- """cli.save_config_value, resolved lazily (cli.py imports this module)."""
- from cli import save_config_value
- return save_config_value(key, value)
-
-
-def _dim(text: str) -> str:
- """Wrap ``text`` in the dim ANSI escape."""
- from cli import _DIM, _RST
- return f"{_DIM}{text}{_RST}"
-
-
-def _dim_line(text: str) -> str:
- """Two-space indented dim line (the standard slash-command hint shape)."""
- return " " + _dim(text)
-
-
-def _accent(text: str) -> str:
- """Wrap ``text`` in the skin accent ANSI escape."""
- from cli import _ACCENT, _RST
- return f"{_ACCENT}{text}{_RST}"
-
-
-def _accent_line(text: str) -> str:
- """Two-space indented accent line (the standard slash-command headline shape)."""
- return f" {_accent(text)}"
-
-
-def _t(key: str, **kwargs) -> str:
- """Catalog text for this module: ``cli.commands.<key>`` in the active language."""
- return t(f"cli.commands.{key}", **kwargs)
-
-
-def _tn(key: str, count: int, **kwargs) -> str:
- """Plural-aware ``_t``: ``<key>_one`` when ``count == 1``, else ``<key>_other``."""
- return _t(f"{key}_{'one' if count == 1 else 'other'}", count=count, **kwargs)
-
-
-def _gt(key: str, **kwargs) -> str:
- """A ``gateway.<key>`` catalog entry the CLI shares verbatim with the messaging gateway."""
- return t(f"gateway.{key}", **kwargs)
-
-
-def _lines(text: str, pad: str = " ") -> list:
- """Each line of a (possibly multi-line) catalog value prefixed with ``pad`` (blank lines kept)."""
- return [f"{pad}{line}" if line else "" for line in text.splitlines()]
-
-
-def _probe(module: str, name: str, default, *args):
- """``<module>.<name>(*args)`` or ``default`` when the import or the call fails (optional
- subsystems: browser backends, async delegations, wake word, ...)."""
- try:
- return getattr(importlib.import_module(module), name)(*args)
- except Exception:
- return default
-
-
-class _TTYBuf(StringIO):
- """StringIO that claims to be a TTY so ``hermes_cli.colors.color()`` still emits ANSI escapes."""
- def isatty(self) -> bool:
- return True
_FAILED = object()
@@ -127,26 +55,6 @@ def _attempt(label: str, errors, fn, *args, **kwargs):
return _FAILED
-def _say_block(*lines: str) -> None:
- """print() the lines framed by a blank line above and below (the /browser output style)."""
- _pr("", *lines, "")
-
-
-def _command_arg(cmd: str, *, lower: bool = False) -> str:
- """Everything after the slash-command word, stripped (optionally lowercased)."""
- parts = (cmd or "").strip().split(None, 1)
- arg = parts[1].strip() if len(parts) > 1 else ""
- return arg.lower() if lower else arg
-
-
-def _shlex_args(cmd: str) -> list:
- """Tokens after the command word; falls back to whitespace split on unbalanced quotes."""
- try:
- return shlex.split(cmd)[1:] if cmd else []
- except ValueError:
- return (cmd or "").split()[1:]
-
-
def _take_flag(parts: list, flag: str):
"""Pop ``flag VALUE`` out of ``parts``: ``(rest, value, ok)``; ok=False when the value is
missing (caller prints usage)."""
@@ -244,7 +152,7 @@ def _split_scope_flags(raw: str):
"""``(arg, explicit_global)`` for /reasoning + /fast: session scope by default, ``--global``
persists to config.yaml, ``--session`` is an explicit no-op (parity with /model)."""
tokens = raw.strip().lower().split()
- return " ".join(t for t in tokens if t not in ("--global", "--session")), "--global" in tokens
+ return " ".join(tok for tok in tokens if tok not in ("--global", "--session")), "--global" in tokens
def _scope_outcome(explicit_global: bool, saved: bool) -> str:
@@ -614,7 +522,7 @@ _BROWSER_SUBCOMMANDS = {
}
-class CLICommandsMixin:
+class CLICommandsMixin(CLICommandsSessionToolsMixin):
"""Mixin holding the interactive-CLI slash-command handlers."""
# ---- /rollback ------------------------------------------------------------------------
@@ -908,211 +816,6 @@ class CLICommandsMixin:
print(f" {_t('import.wrapper_created', path=wrapper_path)}")
print(f" {_t('import.use_it', name=imported)}")
- # ---- /stop, /agents -------------------------------------------------------------------
- def _handle_stop_command(self):
- """Handle /stop — kill all running background processes and background (async) delegations.
- Separate from interrupt (stop the current turn), as in Codex.
-
- See #14602.
- """
- from tools.process_registry import process_registry
- running = [p for p in process_registry.list_sessions() if p.get("status") == "running"]
- # Background subagents live in their own registry, not the process registry.
- n_async = _probe("tools.async_delegation", "active_count", 0)
- if not running and not n_async:
- return print(f" {_t('stop.none_running')}")
- if running:
- print(f" {_t('stop.stopping', count=len(running))}")
- print(f" {_t('stop.stopped', count=process_registry.kill_all(source='cli.stop'))}")
- if n_async:
- from tools.async_delegation import interrupt_all
- print(f" {_t('stop.interrupted_delegations', count=interrupt_all(reason='/stop'))}")
-
- def _handle_agents_command(self):
- """Handle /agents — show background processes and agent status."""
- from tools.process_registry import format_uptime_short, process_registry
- processes = process_registry.list_sessions()
- running = [p for p in processes if p.get("status") == "running"]
- finished = [p for p in processes if p.get("status") != "running"]
- _cp(f" {_t('agents.running_processes', count=len(running))}")
- for p in running:
- up = format_uptime_short(p.get("uptime_seconds", 0))
- _cp(f" {p.get('session_id', '?')} · {up} · {p.get('command', '')[:80]}")
- if finished:
- _cp(f" {_t('agents.recently_finished', count=len(finished))}")
- # Background (async) delegations — delegate_task(background=true)
- delegations = _probe("tools.async_delegation", "list_async_delegations", [])
- if delegations:
- running_d = [d for d in delegations if d.get("status") in ("running", "stalling")]
- _cp(f" {_t('agents.background_delegations', count=len(running_d))}")
- for d in delegations:
- status = d.get("status", "?")
- line = f" {d.get('delegation_id', '?')} · {status} · {(d.get('goal') or '')[:60]}"
- # Live-status detail for in-flight delegations.
- # See #51690.
- if status == "stalling":
- quiet = d.get("stalled_after_quiet_seconds")
- if quiet is not None:
- line += _t("agents.no_progress", seconds=f"{quiet:.0f}")
- elif status == "running":
- quiet = d.get("seconds_since_progress")
- if quiet is not None and quiet >= 60:
- line += _t("agents.quiet", seconds=f"{quiet:.0f}")
- _cp(line)
- for i, child in enumerate(d.get("children_activity") or []):
- if not isinstance(child, dict):
- continue
- tool = child.get("current_tool")
- doing = _t("agents.in_tool", tool=tool) if tool else _t("agents.between_turns")
- part = " " + _t("agents.child_line", index=i + 1,
- api_calls=child.get("api_calls", "?"), doing=doing)
- idle = child.get("seconds_since_activity")
- if idle is not None:
- part += _t("agents.last_activity", seconds=f"{idle:.0f}")
- _cp(part)
- agent_running = getattr(self, "_agent_running", False)
- _cp(f" {_t('agents.agent_running') if agent_running else _t('agents.agent_idle')}")
-
- # ---- /journey, /paste, /copy, /image --------------------------------------------------
- def _handle_journey_command(self, cmd_original: str) -> None:
- """Handle /journey — the learning timeline (see `hermes journey`). Read-only views render
- Rich color that patch_stdout would swallow, so capture with forced ANSI and re-emit via
- ``_cprint``; ``delete``/``edit`` are interactive and keep real stdio."""
- from hermes_cli.journey import register_cli
- parser = argparse.ArgumentParser(prog="/journey", add_help=False)
- register_cli(parser)
- try:
- args = parser.parse_args(shlex.split(cmd_original)[1:])
- except SystemExit:
- return
- try:
- if getattr(args, "journey_action", None) in ("delete", "edit"):
- args.func(args)
- return
- args.force_color = True
- buf = io.StringIO()
- with redirect_stdout(buf):
- args.func(args)
- _cp(buf.getvalue().rstrip("\n"))
- except Exception as exc:
- _cp(f" {_t('journey.failed', error=exc)}")
-
- def _handle_paste_command(self):
- """Handle /paste — explicitly check clipboard for an image.
-
- This is the reliable fallback for terminals where BracketedPaste
- doesn't fire for image-only clipboard content (e.g., VSCode terminal,
- Windows Terminal with WSL2).
- """
- from hermes_cli.clipboard import has_clipboard_image
- if not has_clipboard_image():
- _cp(_dim_line(_t("paste.no_image")))
- elif self._try_attach_clipboard_image():
- _cp(f" {_t('paste.attached', index=len(self._attached_images))}")
- else:
- _cp(_dim_line(_t("paste.extract_failed")))
-
- def _handle_copy_command(self, cmd_original: str) -> None:
- """Handle /copy [number] — copy assistant output to clipboard."""
- from cli import _assistant_copy_text
- arg = _command_arg(cmd_original)
- assistant = [m for m in self.conversation_history if m.get("role") == "assistant"]
- if not assistant:
- return _cp(f" {_t('copy.nothing_yet')}")
- if arg:
- try:
- idx = int(arg) - 1
- except ValueError:
- return _cp(f" {_t('copy.usage')}")
- if idx < 0 or idx >= len(assistant):
- return _cp(f" {_t('copy.invalid_number', max=len(assistant))}")
- else: # latest response that has copyable text
- idx = next((i for i in range(len(assistant) - 1, -1, -1)
- if _assistant_copy_text(assistant[i].get("content"))), -1)
- if idx < 0:
- return _cp(f" {_t('copy.nothing_in_responses')}")
- text = _assistant_copy_text(assistant[idx].get("content"))
- if not text:
- return _cp(f" {_t('copy.nothing_in_response')}")
- try:
- from hermes_cli.clipboard import is_remote_shell_session, write_clipboard_text
- # Over SSH native tools write the REMOTE clipboard; OSC 52 reaches the user's terminal.
- # Locally, OSC 52 is the fallback when native tools are unavailable/fail (SSH/tmux).
- if is_remote_shell_session() or not write_clipboard_text(text):
- # Fixes #31528.
- self._write_osc52_clipboard(text)
- _cp(f" {_t('copy.copied_osc52', index=idx + 1)}")
- else:
- _cp(f" {_t('copy.copied', index=idx + 1)}")
- except Exception as e:
- _cp(f" {_t('copy.failed', error=e)}")
-
- def _handle_image_command(self, cmd_original: str):
- """Handle /image <path> — attach a local image file for the next prompt."""
- from cli import _DIM, _IMAGE_EXTENSIONS, _RST, _cprint, _resolve_attachment_path, _split_path_input
- raw_args = (cmd_original.split(None, 1)[1].strip() if " " in cmd_original else "")
- if not raw_args:
- hint = "/path/to/image.png"
- _cprint(f" {_DIM}{_t('image.usage', example=hint)}{_RST}")
- return
-
- path_token, _remainder = _split_path_input(raw_args)
- image_path = _resolve_attachment_path(path_token)
- if image_path is None:
- return _cp(_dim_line(_t("image.not_found", path=path_token)))
- if image_path.suffix.lower() not in _IMAGE_EXTENSIONS:
- return _cp(_dim_line(_t("image.unsupported", name=image_path.name)))
- self._attached_images.append(image_path)
- _cp(f" {_t('image.attached', name=image_path.name)}")
- if _remainder:
- _cprint(f" {_DIM}{_t('image.now_type_prompt', text=_remainder)}{_RST}")
-
- # ---- /tools, /profile -----------------------------------------------------------------
- def _handle_tools_command(self, cmd: str):
- """Handle /tools [list|disable|enable]. Bare shows the tool list; ``list`` shows per-toolset
- status; disable/enable save to config and reset the session so the new tool set takes
- effect cleanly (no prompt-cache breakage mid-conversation)."""
- parts = _shlex_args(cmd)
- subcommand = parts[0] if parts else ""
- if subcommand not in {"list", "disable", "enable"}:
- return self.show_tools()
- if subcommand == "list":
- return self._run_tools_config(tools_action="list", platform="cli")
- names = parts[1:]
- if not names:
- return _pr(_t("tools.usage", subcommand=subcommand),
- f" {_t('tools.example_toolset', subcommand=subcommand)}",
- f" {_t('tools.example_mcp', subcommand=subcommand)}")
- # Typing the command is consent. Do NOT use input() — it hangs in prompt_toolkit's loop.
- _cp(_accent(_t("tools.disabling" if subcommand == "disable" else "tools.enabling",
- names=", ".join(names))))
- self._run_tools_config(tools_action=subcommand, names=names, platform="cli")
- from hermes_cli.tools_config import _get_platform_tools
- from hermes_cli.config import load_config
- self.enabled_toolsets = _get_platform_tools(load_config(), "cli")
- self.new_session()
- _cp(_dim(_t("tools.session_reset")))
-
- def _run_tools_config(self, **ns) -> None:
- """Run ``tools_disable_enable_command``. Inside the interactive TUI its ANSI print() output
- is captured (isatty=True so colors still render) and re-emitted through _cprint so
- patch_stdout's StdoutProxy doesn't garble the escapes; standalone/tests call straight through."""
- from argparse import Namespace
- from hermes_cli.tools_config import tools_disable_enable_command
- if getattr(self, "_app", None) is None:
- return tools_disable_enable_command(Namespace(**ns))
- buf = _TTYBuf()
- with redirect_stdout(buf):
- tools_disable_enable_command(Namespace(**ns))
- _cp(*buf.getvalue().splitlines())
-
- def _handle_profile_command(self):
- """Display active profile name and home directory."""
- from hermes_cli.slash_exec import CommandContext, execute_command
- reply = execute_command("profile", CommandContext(surface="cli"))
- _say_block(f" {_t('profile.profile', profile=reply.data['profile'])}",
- f" {_t('profile.home', home=reply.data['home'])}")
-
# ---- /handoff -------------------------------------------------------------------------
_HANDOFF_PENDING_TIMEOUT = 60.0
@@ -1813,7 +1516,7 @@ class CLICommandsMixin:
"""Handle /blueprint — set up an automation from a blueprint template (shared handler).
Bare lists the catalog; ``<name>`` seeds the agent to ask for each value conversationally
(``agent_seed``, run as the next turn); ``<name> slot=val …`` creates the job directly."""
- args = " ".join(shlex.quote(t) for t in _shlex_args(cmd))
+ args = " ".join(shlex.quote(part) for part in _shlex_args(cmd))
try:
from hermes_cli.blueprint_cmd import handle_blueprint_command
result = handle_blueprint_command(args)
diff --git a/hermes_cli/cli_commands_session_tools.py b/hermes_cli/cli_commands_session_tools.py
new file mode 100644
index 00000000000..605786722a0
--- /dev/null
+++ b/hermes_cli/cli_commands_session_tools.py
@@ -0,0 +1,326 @@
+"""Shared print/translate helpers + session-info slash handlers (``cli_commands_mixin`` split).
+
+Moved verbatim from ``hermes_cli/cli_commands_mixin.py`` (file-line ratchet #68779): the
+lazy-binding contract carries over unchanged — ``cli.py``-internal symbols are imported at
+call time via ``from cli import ...``, so patch seams on the ``cli`` facade still intercept.
+``cli_commands_mixin`` re-imports every helper moved here, so existing ``from
+cli_commands_mixin import _t`` style imports keep working.
+"""
+
+from __future__ import annotations
+
+import argparse
+import importlib
+import io
+import shlex
+from contextlib import redirect_stdout
+from io import StringIO
+
+from agent.i18n import t # noqa: F401 module-global seam: _t/_tn/_gt close over it
+
+
+def _cp(*lines: str) -> None:
+ """``_cprint`` each line (lazy import: cli.py imports this module)."""
+ from cli import _cprint
+ for line in lines:
+ _cprint(line)
+
+
+def _pr(*lines: str) -> None:
+ """print() each line."""
+ for line in lines:
+ print(line)
+
+
+def _save(key: str, value) -> bool:
+ """cli.save_config_value, resolved lazily (cli.py imports this module)."""
+ from cli import save_config_value
+ return save_config_value(key, value)
+
+
+def _dim(text: str) -> str:
+ """Wrap ``text`` in the dim ANSI escape."""
+ from cli import _DIM, _RST
+ return f"{_DIM}{text}{_RST}"
+
+
+def _dim_line(text: str) -> str:
+ """Two-space indented dim line (the standard slash-command hint shape)."""
+ return " " + _dim(text)
+
+
+def _accent(text: str) -> str:
+ """Wrap ``text`` in the skin accent ANSI escape."""
+ from cli import _ACCENT, _RST
+ return f"{_ACCENT}{text}{_RST}"
+
+
+def _accent_line(text: str) -> str:
+ """Two-space indented accent line (the standard slash-command headline shape)."""
+ return f" {_accent(text)}"
+
+
+def _t(key: str, **kwargs) -> str:
+ """Catalog text for this module: ``cli.commands.<key>`` in the active language."""
+ return t(f"cli.commands.{key}", **kwargs)
+
+
+def _tn(key: str, count: int, **kwargs) -> str:
+ """Plural-aware ``_t``: ``<key>_one`` when ``count == 1``, else ``<key>_other``."""
+ return _t(f"{key}_{'one' if count == 1 else 'other'}", count=count, **kwargs)
+
+
+def _gt(key: str, **kwargs) -> str:
+ """A ``gateway.<key>`` catalog entry the CLI shares verbatim with the messaging gateway."""
+ return t(f"gateway.{key}", **kwargs)
+
+
+def _lines(text: str, pad: str = " ") -> list:
+ """Each line of a (possibly multi-line) catalog value prefixed with ``pad`` (blank lines kept)."""
+ return [f"{pad}{line}" if line else "" for line in text.splitlines()]
+
+
+def _probe(module: str, name: str, default, *args):
+ """``<module>.<name>(*args)`` or ``default`` when the import or the call fails (optional
+ subsystems: browser backends, async delegations, wake word, ...)."""
+ try:
+ return getattr(importlib.import_module(module), name)(*args)
+ except Exception:
+ return default
+
+
+class CLICommandsSessionToolsMixin:
+ """``/stop``, ``/agents``, ``/journey``, ``/paste``, ``/copy``, ``/image``, ``/tools``,
+ ``/profile`` handlers (moved from ``CLICommandsMixin``)."""
+
+
+ # ---- /stop, /agents -------------------------------------------------------------------
+ def _handle_stop_command(self):
+ """Handle /stop — kill all running background processes and background (async) delegations.
+ Separate from interrupt (stop the current turn), as in Codex.
+
+ See #14602.
+ """
+ from tools.process_registry import process_registry
+ running = [p for p in process_registry.list_sessions() if p.get("status") == "running"]
+ # Background subagents live in their own registry, not the process registry.
+ n_async = _probe("tools.async_delegation", "active_count", 0)
+ if not running and not n_async:
+ return print(f" {_t('stop.none_running')}")
+ if running:
+ print(f" {_t('stop.stopping', count=len(running))}")
+ print(f" {_t('stop.stopped', count=process_registry.kill_all(source='cli.stop'))}")
+ if n_async:
+ from tools.async_delegation import interrupt_all
+ print(f" {_t('stop.interrupted_delegations', count=interrupt_all(reason='/stop'))}")
+
+ def _handle_agents_command(self):
+ """Handle /agents — show background processes and agent status."""
+ from tools.process_registry import format_uptime_short, process_registry
+ processes = process_registry.list_sessions()
+ running = [p for p in processes if p.get("status") == "running"]
+ finished = [p for p in processes if p.get("status") != "running"]
+ _cp(f" {_t('agents.running_processes', count=len(running))}")
+ for p in running:
+ up = format_uptime_short(p.get("uptime_seconds", 0))
+ _cp(f" {p.get('session_id', '?')} · {up} · {p.get('command', '')[:80]}")
+ if finished:
+ _cp(f" {_t('agents.recently_finished', count=len(finished))}")
+ # Background (async) delegations — delegate_task(background=true)
+ delegations = _probe("tools.async_delegation", "list_async_delegations", [])
+ if delegations:
+ running_d = [d for d in delegations if d.get("status") in ("running", "stalling")]
+ _cp(f" {_t('agents.background_delegations', count=len(running_d))}")
+ for d in delegations:
+ status = d.get("status", "?")
+ line = f" {d.get('delegation_id', '?')} · {status} · {(d.get('goal') or '')[:60]}"
+ # Live-status detail for in-flight delegations.
+ # See #51690.
+ if status == "stalling":
+ quiet = d.get("stalled_after_quiet_seconds")
+ if quiet is not None:
+ line += _t("agents.no_progress", seconds=f"{quiet:.0f}")
+ elif status == "running":
+ quiet = d.get("seconds_since_progress")
+ if quiet is not None and quiet >= 60:
+ line += _t("agents.quiet", seconds=f"{quiet:.0f}")
+ _cp(line)
+ for i, child in enumerate(d.get("children_activity") or []):
+ if not isinstance(child, dict):
+ continue
+ tool = child.get("current_tool")
+ doing = _t("agents.in_tool", tool=tool) if tool else _t("agents.between_turns")
+ part = " " + _t("agents.child_line", index=i + 1,
+ api_calls=child.get("api_calls", "?"), doing=doing)
+ idle = child.get("seconds_since_activity")
+ if idle is not None:
+ part += _t("agents.last_activity", seconds=f"{idle:.0f}")
+ _cp(part)
+ agent_running = getattr(self, "_agent_running", False)
+ _cp(f" {_t('agents.agent_running') if agent_running else _t('agents.agent_idle')}")
+
+ # ---- /journey, /paste, /copy, /image --------------------------------------------------
+ def _handle_journey_command(self, cmd_original: str) -> None:
+ """Handle /journey — the learning timeline (see `hermes journey`). Read-only views render
+ Rich color that patch_stdout would swallow, so capture with forced ANSI and re-emit via
+ ``_cprint``; ``delete``/``edit`` are interactive and keep real stdio."""
+ from hermes_cli.journey import register_cli
+ parser = argparse.ArgumentParser(prog="/journey", add_help=False)
+ register_cli(parser)
+ try:
+ args = parser.parse_args(shlex.split(cmd_original)[1:])
+ except SystemExit:
+ return
+ try:
+ if getattr(args, "journey_action", None) in ("delete", "edit"):
+ args.func(args)
+ return
+ args.force_color = True
+ buf = io.StringIO()
+ with redirect_stdout(buf):
+ args.func(args)
+ _cp(buf.getvalue().rstrip("\n"))
+ except Exception as exc:
+ _cp(f" {_t('journey.failed', error=exc)}")
+
+ def _handle_paste_command(self):
+ """Handle /paste — explicitly check clipboard for an image.
+
+ This is the reliable fallback for terminals where BracketedPaste
+ doesn't fire for image-only clipboard content (e.g., VSCode terminal,
+ Windows Terminal with WSL2).
+ """
+ from hermes_cli.clipboard import has_clipboard_image
+ if not has_clipboard_image():
+ _cp(_dim_line(_t("paste.no_image")))
+ elif self._try_attach_clipboard_image():
+ _cp(f" {_t('paste.attached', index=len(self._attached_images))}")
+ else:
+ _cp(_dim_line(_t("paste.extract_failed")))
+
+ def _handle_copy_command(self, cmd_original: str) -> None:
+ """Handle /copy [number] — copy assistant output to clipboard."""
+ from cli import _assistant_copy_text
+ arg = _command_arg(cmd_original)
+ assistant = [m for m in self.conversation_history if m.get("role") == "assistant"]
+ if not assistant:
+ return _cp(f" {_t('copy.nothing_yet')}")
+ if arg:
+ try:
+ idx = int(arg) - 1
+ except ValueError:
+ return _cp(f" {_t('copy.usage')}")
+ if idx < 0 or idx >= len(assistant):
+ return _cp(f" {_t('copy.invalid_number', max=len(assistant))}")
+ else: # latest response that has copyable text
+ idx = next((i for i in range(len(assistant) - 1, -1, -1)
+ if _assistant_copy_text(assistant[i].get("content"))), -1)
+ if idx < 0:
+ return _cp(f" {_t('copy.nothing_in_responses')}")
+ text = _assistant_copy_text(assistant[idx].get("content"))
+ if not text:
+ return _cp(f" {_t('copy.nothing_in_response')}")
+ try:
+ from hermes_cli.clipboard import is_remote_shell_session, write_clipboard_text
+ # Over SSH native tools write the REMOTE clipboard; OSC 52 reaches the user's terminal.
+ # Locally, OSC 52 is the fallback when native tools are unavailable/fail (SSH/tmux).
+ if is_remote_shell_session() or not write_clipboard_text(text):
+ # Fixes #31528.
+ self._write_osc52_clipboard(text)
+ _cp(f" {_t('copy.copied_osc52', index=idx + 1)}")
+ else:
+ _cp(f" {_t('copy.copied', index=idx + 1)}")
+ except Exception as e:
+ _cp(f" {_t('copy.failed', error=e)}")
+
+ def _handle_image_command(self, cmd_original: str):
+ """Handle /image <path> — attach a local image file for the next prompt."""
+ from cli import _DIM, _IMAGE_EXTENSIONS, _RST, _cprint, _resolve_attachment_path, _split_path_input
+ raw_args = (cmd_original.split(None, 1)[1].strip() if " " in cmd_original else "")
+ if not raw_args:
+ hint = "/path/to/image.png"
+ _cprint(f" {_DIM}{_t('image.usage', example=hint)}{_RST}")
+ return
+
+ path_token, _remainder = _split_path_input(raw_args)
+ image_path = _resolve_attachment_path(path_token)
+ if image_path is None:
+ return _cp(_dim_line(_t("image.not_found", path=path_token)))
+ if image_path.suffix.lower() not in _IMAGE_EXTENSIONS:
+ return _cp(_dim_line(_t("image.unsupported", name=image_path.name)))
+ self._attached_images.append(image_path)
+ _cp(f" {_t('image.attached', name=image_path.name)}")
+ if _remainder:
+ _cprint(f" {_DIM}{_t('image.now_type_prompt', text=_remainder)}{_RST}")
+
+ # ---- /tools, /profile -----------------------------------------------------------------
+ def _handle_tools_command(self, cmd: str):
+ """Handle /tools [list|disable|enable]. Bare shows the tool list; ``list`` shows per-toolset
+ status; disable/enable save to config and reset the session so the new tool set takes
+ effect cleanly (no prompt-cache breakage mid-conversation)."""
+ parts = _shlex_args(cmd)
+ subcommand = parts[0] if parts else ""
+ if subcommand not in {"list", "disable", "enable"}:
+ return self.show_tools()
+ if subcommand == "list":
+ return self._run_tools_config(tools_action="list", platform="cli")
+ names = parts[1:]
+ if not names:
+ return _pr(_t("tools.usage", subcommand=subcommand),
+ f" {_t('tools.example_toolset', subcommand=subcommand)}",
+ f" {_t('tools.example_mcp', subcommand=subcommand)}")
+ # Typing the command is consent. Do NOT use input() — it hangs in prompt_toolkit's loop.
+ _cp(_accent(_t("tools.disabling" if subcommand == "disable" else "tools.enabling",
+ names=", ".join(names))))
+ self._run_tools_config(tools_action=subcommand, names=names, platform="cli")
+ from hermes_cli.tools_config import _get_platform_tools
+ from hermes_cli.config import load_config
+ self.enabled_toolsets = _get_platform_tools(load_config(), "cli")
+ self.new_session()
+ _cp(_dim(_t("tools.session_reset")))
+
+ def _run_tools_config(self, **ns) -> None:
+ """Run ``tools_disable_enable_command``. Inside the interactive TUI its ANSI print() output
+ is captured (isatty=True so colors still render) and re-emitted through _cprint so
+ patch_stdout's StdoutProxy doesn't garble the escapes; standalone/tests call straight through."""
+ from argparse import Namespace
+ from hermes_cli.tools_config import tools_disable_enable_command
+ if getattr(self, "_app", None) is None:
+ return tools_disable_enable_command(Namespace(**ns))
+ buf = _TTYBuf()
+ with redirect_stdout(buf):
+ tools_disable_enable_command(Namespace(**ns))
+ _cp(*buf.getvalue().splitlines())
+
+ def _handle_profile_command(self):
+ """Display active profile name and home directory."""
+ from hermes_cli.slash_exec import CommandContext, execute_command
+ reply = execute_command("profile", CommandContext(surface="cli"))
+ _say_block(f" {_t('profile.profile', profile=reply.data['profile'])}",
+ f" {_t('profile.home', home=reply.data['home'])}")
+
+
+class _TTYBuf(StringIO):
+ """StringIO that claims to be a TTY so ``hermes_cli.colors.color()`` still emits ANSI escapes."""
+ def isatty(self) -> bool:
+ return True
+
+
+def _say_block(*lines: str) -> None:
+ """print() the lines framed by a blank line above and below (the /browser output style)."""
+ _pr("", *lines, "")
+
+
+def _command_arg(cmd: str, *, lower: bool = False) -> str:
+ """Everything after the slash-command word, stripped (optionally lowercased)."""
+ parts = (cmd or "").strip().split(None, 1)
+ arg = parts[1].strip() if len(parts) > 1 else ""
+ return arg.lower() if lower else arg
+
+
+def _shlex_args(cmd: str) -> list:
+ """Tokens after the command word; falls back to whitespace split on unbalanced quotes."""
+ try:
+ return shlex.split(cmd)[1:] if cmd else []
+ except ValueError:
+ return (cmd or "").split()[1:]
diff --git a/tui_gateway/methods_session.py b/tui_gateway/methods_session.py
index cd54dd0792e..fb989d37903 100644
--- a/tui_gateway/methods_session.py
+++ b/tui_gateway/methods_session.py
@@ -2357,127 +2357,8 @@ def _(rid, params: dict, session: dict) -> dict:
return _branch_live(rid, params, session, omit_messages=True)
-def _resume_wake_after_interrupt() -> None:
- """Re-arm a wake lease held by the interrupt caller or a voice capture.
-
- ``_wake_resume_if_owner`` no-ops unless that object holds the lease, so an
- in-progress capture owned by someone else is not stolen. Interrupt already
- silenced TTS before it can return an error; this matches that cut. A
- ``not_interrupted`` hosted-task mismatch must not call it.
- """
- with _voice_sid_lock:
- voice_owner = _voice_wake_owner
- seen = []
- for owner in (_caller_transport(), voice_owner):
- if owner is None or any(owner is item for item in seen):
- continue
- seen.append(owner)
- _wake_resume_if_owner(owner)
-
-
-# ── interrupt / steer / redirect ─────────────────────────────────────
-@method("session.interrupt")
-def _(rid, params: dict) -> dict:
- _tts_stream_stop() # keypress barge-in also silences streaming TTS (voice is process-global)
- resume_wake = True
- try:
- session, err = _sess_nowait(params, rid)
- if err:
- return err
- if expected := _str_param(params, "expected_hosted_task_id"):
- with session["history_lock"]:
- task = session.get("_hosted_room_task")
- if not (session.get("running") and isinstance(task, dict) and task.get("task_id") == expected):
- resume_wake = False
- return _ok(rid, {"status": "not_interrupted", "interrupted": False})
- sid = str(params.get("session_id") or "")
- if _session_uses_compute_host(session):
- try:
- _interrupt_session_turn(sid, session, request_id=f"interrupt-{rid}")
- except Exception as exc:
- return _err(rid, 5019, f"compute-host interrupt failed: {exc}")
- return _ok(rid, {"status": "interrupted", "turn_isolation": True})
- session, err = _sess(params, rid)
- if err:
- return err
- _interrupt_session_turn(sid, session)
- # Retire the crash-recovery marker NOW: until the run thread's finally, a backend exit looks like a crash
- # and session.resume auto-continues the turn the user just stopped (the extra key covers compression
- # rotating session_key mid-turn).
- with session["history_lock"]:
- active_marker_key = str(session.pop("_active_turn_marker_key", "") or "")
- _retire_turn_marker(session, active_marker_key)
- return _ok(rid, {"status": "interrupted"})
- finally:
- if resume_wake:
- try:
- _resume_wake_after_interrupt()
- except Exception:
- logger.debug("session.interrupt wake resume failed", exc_info=True)
-def _apply_correction(rid, session: dict, verb: str, text: str, accepted_status: str) -> dict:
- """``agent.<verb>(text)``; on acceptance record it on the live turn (mid-turn resume rebuilds the bubble)
- and purge queued self-copies so post-turn drain cannot re-fire the old prompt."""
- try:
- accepted = getattr(session["agent"], verb)(text)
- except Exception as exc:
- return _err(rid, 5000, f"{verb} failed: {exc}")
- if accepted:
- with session["history_lock"]:
- _record_inflight_correction(session, text)
- # #84417: steer does not cancel the live original, but a server queue self-copy of that original
- # must still not re-fire after settle (same class as redirect).
- # #84417: purge server-queue self-duplicates of the live original so post-turn drain cannot
- # restart the pre-correction prompt.
- _drop_queued_duplicates_of_inflight_user(session)
- session["last_active"] = time.time()
- return _ok(rid, {"status": accepted_status if accepted else "rejected", "text": text})
-
-
-def _correction_method(name: str, verb: str, accepted_status: str, supported, unsupported: str):
- """steer/redirect RPC: ``params.text`` (4002, checked before the session) into a live session;
- ``supported(agent)`` gates 4010."""
- @method(name)
- def _(rid, params: dict) -> dict:
- if not (text := (params.get("text") or "").strip()):
- return _err(rid, 4002, "text is required")
- session, err = _sess_nowait(params, rid)
- if err:
- return err
- agent = session.get("agent")
- # Redirect during the turn-build window (running=True, agent None): queue for the next turn instead of
- # a misleading 4010 the client swallows into a lost follow-up.
- if verb == "redirect" and agent is None and session.get("running"):
- _enqueue_prompt(session, text, current_transport() or _stdio_transport)
- session["last_active"] = time.time()
- return _ok(rid, {"status": "queued", "text": text})
- # Compression in flight: queue instead of steering/redirecting. A correction that
- # reaches the provider mid-compression aborts the compression (explicit_interrupt)
- # — the follow-up kills the turn that would answer it (#61042). Queued here, it
- # drains when compression finishes (the Discord-gateway contract; mirrors the
- # interrupt→queue demotion in gateway/run_busy.py for the channel busy path).
- if _session_compression_in_flight(session):
- _enqueue_prompt(session, text, current_transport() or _stdio_transport)
- session["last_active"] = time.time()
- return _ok(rid, {"status": "queued", "text": text})
- if not supported(agent):
- return _err(rid, 4010, unsupported)
- # An idle agent accepts steer() but only the next turn drains it, spliced after an old tool
- # row (#64578). 'rejected' makes the client queue it as a normal next prompt.
- if verb == "steer" and not session.get("running"):
- return _ok(rid, {"status": "rejected", "text": text})
- return _apply_correction(rid, session, verb, text, accepted_status)
-
-
-# Inject text into the next tool result without interrupting (AIAgent.steer(): no new user turn, no role
-# alternation violation).
-_correction_method("session.steer", "steer", "queued", lambda agent: hasattr(agent, "steer"),
- "agent does not support steer")
-# Redirect the active model turn while preserving valid work/context.
-_correction_method("session.redirect", "redirect", "redirected",
- lambda agent: getattr(agent, "_supports_active_turn_redirect", False) is True
- and hasattr(agent, "redirect"), "agent does not support active-turn redirect")
# ── delegation / spawn trees ─────────────────────────────────────────
@@ -2621,3 +2502,5 @@ def _(rid, params: dict) -> dict:
def register(server) -> None:
"""Publish this module's helpers onto ``server`` (rebound to its globals) and install handlers."""
bind_module(globals(), server, skip=("_",))
+ from . import methods_session_interrupt
+ methods_session_interrupt.register(server)
diff --git a/tui_gateway/methods_session_interrupt.py b/tui_gateway/methods_session_interrupt.py
new file mode 100644
index 00000000000..737dbfd5e7a
--- /dev/null
+++ b/tui_gateway/methods_session_interrupt.py
@@ -0,0 +1,139 @@
+"""Interrupt / steer / redirect session handlers (``methods_session`` split).
+
+Moved verbatim from ``tui_gateway/methods_session.py`` (file-line ratchet #68779): the
+bodies close over server.py globals through ``method_ctx.bind_module`` exactly as before —
+``@method`` registration, ``_session_arg`` wrapping and module publication all still run
+from the parent's ``register()`` via ``HandlerRegistry``.
+"""
+
+from .method_ctx import HandlerRegistry, bind_module
+
+_registry = HandlerRegistry()
+method = _registry.method
+
+def _resume_wake_after_interrupt() -> None:
+ """Re-arm a wake lease held by the interrupt caller or a voice capture.
+
+ ``_wake_resume_if_owner`` no-ops unless that object holds the lease, so an
+ in-progress capture owned by someone else is not stolen. Interrupt already
+ silenced TTS before it can return an error; this matches that cut. A
+ ``not_interrupted`` hosted-task mismatch must not call it.
+ """
+ with _voice_sid_lock:
+ voice_owner = _voice_wake_owner
+ seen = []
+ for owner in (_caller_transport(), voice_owner):
+ if owner is None or any(owner is item for item in seen):
+ continue
+ seen.append(owner)
+ _wake_resume_if_owner(owner)
+
+
+# ── interrupt / steer / redirect ─────────────────────────────────────
+@method("session.interrupt")
+def _(rid, params: dict) -> dict:
+ _tts_stream_stop() # keypress barge-in also silences streaming TTS (voice is process-global)
+ resume_wake = True
+ try:
+ session, err = _sess_nowait(params, rid)
+ if err:
+ return err
+ if expected := _str_param(params, "expected_hosted_task_id"):
+ with session["history_lock"]:
+ task = session.get("_hosted_room_task")
+ if not (session.get("running") and isinstance(task, dict) and task.get("task_id") == expected):
+ resume_wake = False
+ return _ok(rid, {"status": "not_interrupted", "interrupted": False})
+ sid = str(params.get("session_id") or "")
+ if _session_uses_compute_host(session):
+ try:
+ _interrupt_session_turn(sid, session, request_id=f"interrupt-{rid}")
+ except Exception as exc: # health: allow BLE001 -- the bridge raises transport-specific errors unknown here; surfaced verbatim to the client, never swallowed
+ return _err(rid, 5019, f"compute-host interrupt failed: {exc}")
+ return _ok(rid, {"status": "interrupted", "turn_isolation": True})
+ session, err = _sess(params, rid)
+ if err:
+ return err
+ _interrupt_session_turn(sid, session)
+ # Retire the crash-recovery marker NOW: until the run thread's finally, a backend exit looks like a crash
+ # and session.resume auto-continues the turn the user just stopped (the extra key covers compression
+ # rotating session_key mid-turn).
+ with session["history_lock"]:
+ active_marker_key = str(session.pop("_active_turn_marker_key", "") or "")
+ _retire_turn_marker(session, active_marker_key)
+ return _ok(rid, {"status": "interrupted"})
+ finally:
+ if resume_wake:
+ try:
+ _resume_wake_after_interrupt()
+ except Exception:
+ logger.debug("session.interrupt wake resume failed", exc_info=True)
+
+
+def _apply_correction(rid, session: dict, verb: str, text: str, accepted_status: str) -> dict:
+ """``agent.<verb>(text)``; on acceptance record it on the live turn (mid-turn resume rebuilds the bubble)
+ and purge queued self-copies so post-turn drain cannot re-fire the old prompt."""
+ try:
+ accepted = getattr(session["agent"], verb)(text)
+ except Exception as exc:
+ return _err(rid, 5000, f"{verb} failed: {exc}")
+ if accepted:
+ with session["history_lock"]:
+ _record_inflight_correction(session, text)
+ # #84417: steer does not cancel the live original, but a server queue self-copy of that original
+ # must still not re-fire after settle (same class as redirect).
+ # #84417: purge server-queue self-duplicates of the live original so post-turn drain cannot
+ # restart the pre-correction prompt.
+ _drop_queued_duplicates_of_inflight_user(session)
+ session["last_active"] = time.time()
+ return _ok(rid, {"status": accepted_status if accepted else "rejected", "text": text})
+
+
+def _correction_method(name: str, verb: str, accepted_status: str, supported, unsupported: str):
+ """steer/redirect RPC: ``params.text`` (4002, checked before the session) into a live session;
+ ``supported(agent)`` gates 4010."""
+ @method(name)
+ def _(rid, params: dict) -> dict:
+ if not (text := (params.get("text") or "").strip()):
+ return _err(rid, 4002, "text is required")
+ session, err = _sess_nowait(params, rid)
+ if err:
+ return err
+ agent = session.get("agent")
+ # Redirect during the turn-build window (running=True, agent None): queue for the next turn instead of
+ # a misleading 4010 the client swallows into a lost follow-up.
+ if verb == "redirect" and agent is None and session.get("running"):
+ _enqueue_prompt(session, text, current_transport() or _stdio_transport)
+ session["last_active"] = time.time()
+ return _ok(rid, {"status": "queued", "text": text})
+ # Compression in flight: queue instead of steering/redirecting. A correction that
+ # reaches the provider mid-compression aborts the compression (explicit_interrupt)
+ # — the follow-up kills the turn that would answer it (#61042). Queued here, it
+ # drains when compression finishes (the Discord-gateway contract; mirrors the
+ # interrupt→queue demotion in gateway/run_busy.py for the channel busy path).
+ if _session_compression_in_flight(session):
+ _enqueue_prompt(session, text, current_transport() or _stdio_transport)
+ session["last_active"] = time.time()
+ return _ok(rid, {"status": "queued", "text": text})
+ if not supported(agent):
+ return _err(rid, 4010, unsupported)
+ # An idle agent accepts steer() but only the next turn drains it, spliced after an old tool
+ # row (#64578). 'rejected' makes the client queue it as a normal next prompt.
+ if verb == "steer" and not session.get("running"):
+ return _ok(rid, {"status": "rejected", "text": text})
+ return _apply_correction(rid, session, verb, text, accepted_status)
+
+
+# Inject text into the next tool result without interrupting (AIAgent.steer(): no new user turn, no role
+# alternation violation).
+_correction_method("session.steer", "steer", "queued", lambda agent: hasattr(agent, "steer"),
+ "agent does not support steer")
+# Redirect the active model turn while preserving valid work/context.
+_correction_method("session.redirect", "redirect", "redirected",
+ lambda agent: getattr(agent, "_supports_active_turn_redirect", False) is True
+ and hasattr(agent, "redirect"), "agent does not support active-turn redirect")
+
+
+def register(server) -> None:
+ """Publish this module's helpers onto ``server`` (rebound to its globals) and install handlers."""
+ bind_module(globals(), server, skip=("_",))