hermes-agent/tools/close_terminal_tool.py
Brooklyn Nicholson e117cfdff0 feat(desktop): live agent terminals + agent-driven tab close
Make the read-only agent terminal mirrors stream in real time and give
the agent a desktop-only way to dismiss its own tabs.

- Stream background output live: the local reader used a blocking
  read(4096) that buffered small periodic output until EOF, so agent
  tabs only "filled in" at process exit. Switch to buffer.read1(4096)
  (decoded) for incremental chunks.
- Route agent.terminal.output / terminal.close to the window that owns
  the process (its gateway session) instead of an empty session id, so
  events actually reach the desktop renderer.
- Add close_terminal: a HERMES_DESKTOP-gated tool (sibling of
  read_terminal) that drops a process's read-only tab WITHOUT killing it
  via process_registry.on_close; output keeps buffering and the user can
  reopen from the status stack.
- ⌘W now closes a focused agent tab: mark the agent instance
  data-terminal and focus it on activation so isFocusWithin routes there.
- ensureTerminal() no longer spawns an extra user shell when a tab
  already exists (e.g. opening a background task from the status stack).
2026-06-28 21:15:14 -05:00

69 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""Close a read-only agent terminal tab in the Hermes desktop GUI.
Each ``terminal(background=true)`` process is mirrored as a read-only tab in the
desktop's terminal pane. This tool lets the agent drop a tab it no longer needs
to show — WITHOUT killing the process (use ``process(action='kill')`` for that).
The output keeps buffering and the user can reopen the tab from the status stack.
It routes through the process registry's ``on_close`` sink, which the desktop
gateway wires to emit a ``terminal.close`` event the renderer handles. Like
``read_terminal`` it is gated on ``HERMES_DESKTOP`` so it never appears outside
the GUI.
"""
import json
import os
from tools.process_registry import process_registry
from tools.registry import registry, tool_error
def close_terminal_tool(process_id: str) -> str:
"""Ask the desktop GUI to close a background process's read-only tab."""
pid = (process_id or "").strip()
if not pid:
return tool_error("process_id is required (the background process whose tab to close).")
return json.dumps(process_registry.request_close_terminal(pid), ensure_ascii=False)
def check_close_terminal_requirements() -> bool:
"""Desktop GUI only — HERMES_DESKTOP is set on the gateway the app spawns."""
return (os.getenv("HERMES_DESKTOP") or "").strip().lower() in ("1", "true", "yes")
CLOSE_TERMINAL_SCHEMA = {
"name": "close_terminal",
"description": (
"Close the read-only terminal tab for one of your background processes in "
"the Hermes desktop GUI (the tabs mirroring terminal(background=true) runs). "
"This does NOT kill the process — it only drops the tab/view; the output "
"keeps buffering and the user can reopen it from the status stack. Use it "
"to tidy up when a background process's live terminal is no longer worth "
"showing. To actually stop the process, use process(action='kill') instead."
),
"parameters": {
"type": "object",
"properties": {
"process_id": {
"type": "string",
"description": (
"The background process's session id (from terminal(background=true) "
"output or process(action='list')) whose tab should be closed."
),
},
},
"required": ["process_id"],
},
}
registry.register(
name="close_terminal",
toolset="terminal",
schema=CLOSE_TERMINAL_SCHEMA,
handler=lambda args, **kw: close_terminal_tool(process_id=args.get("process_id", "")),
check_fn=check_close_terminal_requirements,
emoji="🖥️",
)