gateway_setting_commands.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """Per-session setting commands of the classic CLI on the shared gateway (``slash.exec``).
  2. On main ``/personality`` ``/busy`` ``/approvals`` ``/footer`` ``/fast`` changed this process's own
  3. agent or config. Rewired, the session's agent lives in the gateway, so the gateway's handler
  4. decides and applies the change (gateway/session_setting_commands.py; RULING §1: one behaviour per
  5. command, every surface) and the CLI prints its reply. Client-side state the CLI itself enforces
  6. (``/busy``: what Enter does while a turn runs) is mirrored locally after the owner accepts it.
  7. ``/refine`` and ``/review`` take the same route: their background reviewer must run beside the
  8. session's agent in the gateway (as on messaging), where its result is delivered back to the session.
  9. ``/cron`` too: the profile's cron store belongs to the gateway that fires its jobs, so the gateway runs
  10. main's handler (one writer); ``/cron run`` lasts as long as the job, so a write waits with no deadline.
  11. ``/toolsets`` too: the gateway builds the report from its own registry (MCP and plugin toolsets
  12. included) with this session's toolsets marked (gateway/session_info_reads.py).
  13. """
  14. from __future__ import annotations
  15. import shlex
  16. # Canonical command -> mirror(cli, arg) run after the owner applied it (None: nothing local).
  17. _BUSY_MODES = frozenset({"queue", "steer", "interrupt"})
  18. def _mirror_busy(cli, arg):
  19. if arg.lower() in _BUSY_MODES:
  20. cli.busy_input_mode = arg.lower()
  21. def _watch_reviewer(cli, _arg):
  22. """/review spawns its reviewer off-turn: have the live-work dock poll for it right away."""
  23. from hermes_cli.gateway_tools import process_view
  24. view = process_view()
  25. if view is not None:
  26. view.watch_subagents()
  27. # Reports the owner already lays out (main's own line builders): printed as is.
  28. _VERBATIM = frozenset({"toolsets"})
  29. GATEWAY_SETTINGS = {"personality": None, "busy": _mirror_busy, "approvals": None, "footer": None, "fast": None,
  30. "refine": None, "review": _watch_reviewer, "cron": None,
  31. "toolsets": None}
  32. def _unbounded(canonical, arg):
  33. """True for a form answered only once its work is done, however long: a /cron write (``run`` runs the
  34. job). Its reads keep the deadline, and so does an unclosed quote, which the owner refuses at once."""
  35. if canonical != "cron":
  36. return False
  37. from gateway.session_cron_commands import cron_read
  38. try:
  39. return cron_read(shlex.split(arg)) is None
  40. except ValueError:
  41. return False
  42. def _call(cli, connection, canonical, arg):
  43. command = canonical + (" " + arg if arg else "")
  44. if not _unbounded(canonical, arg):
  45. return connection.call("slash.exec", session_id=cli.session_id, command=command)
  46. # No deadline, on a socket of its own so the session's prompts never queue behind it
  47. # (GatewayConnection.call_until); Ctrl+C stops waiting.
  48. from hermes_cli.gateway_plugin_commands import call_unbounded
  49. return call_unbounded(cli, connection, canonical, command)
  50. def run(cli, canonical, cmd):
  51. """True when the gateway answered the command (the local handler must not run)."""
  52. from hermes_cli.gateway_side import connection_for
  53. if canonical not in GATEWAY_SETTINGS:
  54. return False
  55. connection = connection_for(cli)
  56. if connection is None:
  57. return False
  58. from cli import _cprint
  59. from hermes_cli.gateway_client import GatewayClientError
  60. from hermes_cli.gateway_plugin_commands import STOPPED
  61. parts = (cmd or "").strip().split(None, 1)
  62. arg = parts[1].strip() if len(parts) > 1 else ""
  63. try:
  64. reply = _call(cli, connection, canonical, arg)
  65. except GatewayClientError as exc:
  66. _cprint(f" /{canonical}: {exc}")
  67. return True
  68. if reply is STOPPED:
  69. return True
  70. for line in str((reply or {}).get("output") or "").splitlines():
  71. _cprint(f" {line}" if line and canonical not in _VERBATIM else line)
  72. mirror = GATEWAY_SETTINGS[canonical]
  73. if mirror is not None:
  74. mirror(cli, arg)
  75. return True