close_terminal_tool.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """Close a read-only agent terminal tab in the Hermes desktop GUI WITHOUT killing
  2. the mirrored ``terminal(background=true)`` process (output keeps buffering; the user
  3. can reopen it). Routes through the process registry's ``on_close`` sink, which the
  4. desktop gateway wires to a ``terminal.close`` event. ``desktop_ui`` toolset only."""
  5. import json
  6. from tools.process_registry import process_registry
  7. from tools.registry import registry, tool_error
  8. def close_terminal_tool(process_id: str) -> str:
  9. """Ask the desktop GUI to close a background process's read-only tab."""
  10. pid = (process_id or "").strip()
  11. if not pid:
  12. return tool_error("process_id is required (the background process whose tab to close).")
  13. return json.dumps(process_registry.request_close_terminal(pid), ensure_ascii=False)
  14. CLOSE_TERMINAL_SCHEMA = {
  15. "name": "close_terminal",
  16. "description": (
  17. "Hide a background process's desktop terminal tab; the process keeps running. "
  18. "Tabs mirror terminal(background=true) runs. "
  19. "This does NOT kill the process — it only drops the tab/view; the output "
  20. "keeps buffering and the user can reopen it from the status stack. Use it "
  21. "to tidy up when a background process's live terminal is no longer worth "
  22. "showing. To actually stop the process, use process(action='kill') instead."
  23. ),
  24. "parameters": {
  25. "type": "object",
  26. "properties": {
  27. "process_id": {
  28. "type": "string",
  29. "description": (
  30. "The background process's session id (from terminal(background=true) "
  31. "output or process(action='list')) whose tab should be closed."
  32. ),
  33. },
  34. },
  35. "required": ["process_id"],
  36. },
  37. }
  38. registry.register(
  39. name="close_terminal",
  40. toolset="desktop_ui",
  41. schema=CLOSE_TERMINAL_SCHEMA,
  42. handler=lambda args, **kw: close_terminal_tool(process_id=args.get("process_id", "")),
  43. emoji="🖥️",
  44. )