gateway_cli_commands.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Classic CLI commands whose state lives in the gateway session, answered there.
  2. Rewired, the session's tools, checkpoints, environment and handoff watcher run in the gateway, so
  3. main's handlers (which read this process's own agent and environment) answer nothing or the wrong
  4. thing:
  5. - ``/diff session``: the checkpoint baseline is the gateway tools'. ``rollback.session_diff`` reads it
  6. under the session's frozen checkpoint policy (``--checkpoints`` included) and returns it as data
  7. for main's terminal renderer. Working-tree modes stay main's local git read of the same checkout.
  8. - ``/reload``: ``reload.env`` re-reads ``.env`` where turns and tools run (this process re-reads too).
  9. - ``/handoff``: no gateway verb carries a handoff yet; refused as Ink and Desktop refuse it, instead
  10. of the store's ``request_handoff`` refusal dying silently in the process loop.
  11. """
  12. from __future__ import annotations
  13. from agent.i18n import t
  14. from hermes_cli.gateway_client import GatewayClientError
  15. def _diff(cli, connection, cmd):
  16. args = cmd.partition(" ")[2].strip().lower().split()
  17. if "session" not in args:
  18. return False
  19. from hermes_cli.cli_commands_session_tools import _lines, _pr, _t, print_session_diff
  20. result = connection.call("rollback.session_diff", session_id=cli.session_id) or {}
  21. if not result.get("enabled"):
  22. _pr(*_lines(_t("diff.not_enabled")))
  23. else:
  24. print_session_diff(cli, result, stat_only=bool({"--stat", "stat"} & set(args)))
  25. return True
  26. def _reload(cli, connection, cmd):
  27. from hermes_cli.config import reload_env
  28. reload_env()
  29. updated = (connection.call("reload.env", session_id=cli.session_id) or {}).get("updated", 0)
  30. print(f" {t('cli.reload.env_reloaded', count=updated)}")
  31. return True
  32. def _handoff(cli, connection, cmd):
  33. from cli import _cprint
  34. from hermes_cli.gateway_chat_commands import refusal
  35. _cprint(f" {refusal('handoff')}")
  36. return True
  37. _ROUTES = {"diff": _diff, "reload": _reload, "handoff": _handoff}
  38. # Named refusals: nothing runs, so the gateway is not told (no ``pre_command``).
  39. REFUSED = frozenset({"handoff"})
  40. def run(cli, canonical, cmd):
  41. """True when the gateway answered the command (the local handler must not run)."""
  42. route = _ROUTES.get(canonical)
  43. if route is None:
  44. return False
  45. from hermes_cli.gateway_side import connection_for
  46. connection = connection_for(cli)
  47. if connection is None:
  48. return False
  49. try:
  50. return route(cli, connection, (cmd or "").strip())
  51. except GatewayClientError as exc:
  52. from cli import _cprint
  53. _cprint(f" /{canonical}: {exc}")
  54. return True