computer_use_screen.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """``hermes computer-use screen`` — the Bot Desktop screen a profile's ``computer_use`` drives on a
  2. headless Linux gateway host, viewable from Hermes Desktop. ``status`` / ``start`` / ``stop`` /
  3. ``install`` mirror the Desktop pane's controls for ops shells and cloud images."""
  4. from __future__ import annotations
  5. import getpass
  6. import json
  7. import sys
  8. def _screen_status(args) -> int:
  9. from tools.bot_desktop import lease, runtime
  10. st = runtime.status()
  11. if bool(getattr(args, "json", False)):
  12. print(json.dumps({**st.as_dict(), "lease": lease.public_view(lease.get())}, indent=2, sort_keys=True))
  13. return 0 if st.running else 1
  14. if not st.supported:
  15. print("Bot Desktop screens run on Linux gateway hosts only (this host keeps its real display).")
  16. return 1
  17. if not st.installed:
  18. print("Bot Desktop: packages missing → " + ", ".join(st.missing))
  19. print(" Install: " + (st.install_command or "hermes computer-use screen install"))
  20. return 1
  21. if st.running:
  22. holder = lease.public_view(lease.get())
  23. who = f"human (viewer {holder['viewer_hash']})" if holder["holder"] == lease.HUMAN else "agent"
  24. print(f"Bot Desktop [{st.profile}]: running on DISPLAY {st.display} ({st.geometry}), pid {st.pid}")
  25. print(f" control: {who} rfb socket: {st.socket}")
  26. print(" View it: Hermes Desktop → Bots → this bot → Screen")
  27. return 0
  28. if st.blocker:
  29. print(f"Bot Desktop [{st.profile}]: installed, not running. {st.blocker}")
  30. return 1
  31. print(f"Bot Desktop [{st.profile}]: installed, not running. Start: hermes computer-use screen start")
  32. return 1
  33. def _screen_start(args) -> int:
  34. from tools.bot_desktop import runtime
  35. try:
  36. st = runtime.start()
  37. except RuntimeError as exc:
  38. print(f"Bot Desktop: {exc}")
  39. return 1
  40. print(f"Bot Desktop [{st.profile}]: running on DISPLAY {st.display} ({st.geometry})")
  41. return 0
  42. def _screen_stop(args) -> int:
  43. from tools.bot_desktop import runtime
  44. if runtime.is_supported_host():
  45. # Same door as display.stop: a human mid-takeover is never yanked by a runbook or a stray
  46. # `screen stop`; the decision is taken under the lease lock so a takeover cannot race it.
  47. from tools.bot_desktop import lease
  48. if lease.release(unless_human=not bool(getattr(args, "force", False))).holder == lease.HUMAN:
  49. print("Bot Desktop: a human holds this screen; re-run with --force to take it down anyway.")
  50. return 1
  51. print("Bot Desktop: stopped" if runtime.stop() else "Bot Desktop: was not running")
  52. return 0
  53. def _screen_install(args) -> int:
  54. from tools.bot_desktop import install, runtime
  55. if not runtime.is_supported_host():
  56. print("Bot Desktop screens run on Linux gateway hosts only.")
  57. return 1
  58. if not runtime.missing_binaries():
  59. print("Bot Desktop: packages already installed.")
  60. return 0
  61. cmd = runtime.install_command()
  62. if cmd is None:
  63. print("Bot Desktop: no supported package manager (apt/dnf/pacman) found. Install TigerVNC (Xvnc) and "
  64. "the Xfce core (xfwm4, xfce4-panel, xfdesktop, xfce4-settings) by hand.")
  65. return 1
  66. print(f"Bot Desktop: installing → {cmd}")
  67. if not bool(getattr(args, "yes", False)) and sys.stdin.isatty():
  68. answer = input("Proceed? [Y/n] ").strip().lower()
  69. if answer not in ("", "y", "yes"):
  70. return 1
  71. # Same runner as the Desktop pane's Install button: one per-profile slot, list-form spawn, sudo password
  72. # via stdin (-S) and never on the command line.
  73. try:
  74. rc = install.install_packages(ask_password=lambda: getpass.getpass("[sudo] password: "), on_line=print)
  75. except install.InstallBusy as exc:
  76. print(f"Bot Desktop: {exc}")
  77. return 1
  78. if rc == install.NO_SUDO:
  79. print("Bot Desktop: this host has no sudo. Run as root on the host:\n " + cmd.removeprefix("sudo "))
  80. return 1
  81. if rc != 0:
  82. print(f"Bot Desktop: installer exited {rc}")
  83. return rc or 1
  84. missing = runtime.missing_binaries()
  85. if missing:
  86. print("Bot Desktop: still missing " + ", ".join(missing))
  87. return 1
  88. print("Bot Desktop: ready. Start with `hermes computer-use screen start` or from Hermes Desktop.")
  89. return 0
  90. SCREEN_ACTIONS = {"status": _screen_status, "start": _screen_start, "stop": _screen_stop, "install": _screen_install}
  91. def build_screen_parser(computer_use_sub, add_json_flag) -> None:
  92. screen = computer_use_sub.add_parser(
  93. "screen", help="Bot Desktop: the headless screen this profile's computer_use drives (Linux)",
  94. description="On a headless Linux gateway host Hermes gives each profile its own Xfce screen\n"
  95. "(TigerVNC Xvnc on a private Unix socket). The agent's computer_use and headed\n"
  96. "browser act on it; Hermes Desktop shows it live and lets a human take over for\n"
  97. "logins, 2FA or CAPTCHAs, then hand control back.\n\n"
  98. "`install` adds the system packages (apt/dnf/pacman); `start`/`stop` manage this\n"
  99. "profile's screen; `status` shows display, control holder and socket.")
  100. sub = screen.add_subparsers(dest="computer_use_screen_action")
  101. st = sub.add_parser("status", help="Show whether this profile's screen is installed/running and who holds control")
  102. add_json_flag(st, "Emit the status payload as JSON.")
  103. sub.add_parser("start", help="Start this profile's screen")
  104. stop = sub.add_parser("stop", help="Stop this profile's screen (hands control back to the agent first)")
  105. stop.add_argument("--force", action="store_true", help="Stop even while a human holds control from Hermes Desktop")
  106. inst = sub.add_parser("install", help="Install TigerVNC + Xfce core via the host package manager")
  107. inst.add_argument("-y", "--yes", action="store_true", help="Do not ask before running the package manager")
  108. def _cmd(args):
  109. handler = SCREEN_ACTIONS.get(str(getattr(args, "computer_use_screen_action", None) or ""))
  110. if handler is not None:
  111. return handler(args)
  112. screen.print_help()
  113. screen.set_defaults(screen_func=_cmd)