dashboard.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """``hermes dashboard`` / ``hermes serve`` subcommand parsers.
  2. ``dashboard`` is the browser web UI; ``serve`` is the same gateway, headless —
  3. what the desktop app and remote backends run. ``serve`` also skips the web UI
  4. build (``headless_backend=True``): pure JSON-RPC/WS clients never load the SPA.
  5. Both share one handler (``cmd_dashboard`` → ``start_server``).
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. from typing import Callable
  10. def _add_server_runtime_args(parser) -> None:
  11. """Runtime flags shared by ``dashboard`` and ``serve`` (same ``web_server.start_server``)."""
  12. parser.add_argument(
  13. "--port", type=int, default=9119, help="Port (default 9119, 0 for auto-assign by OS)")
  14. parser.add_argument("--host", default="127.0.0.1", help="Host (default 127.0.0.1)")
  15. parser.add_argument(
  16. "--insecure", action="store_true",
  17. help="DEPRECATED / NO-OP. Formerly bypassed auth on a non-loopback "
  18. "bind. As of the June 2026 hardening it no longer disables "
  19. "authentication — a public bind always requires an auth provider "
  20. "(password or OAuth). Bind 127.0.0.1 + tunnel to keep it local.")
  21. parser.add_argument(
  22. "--skip-build", action="store_true",
  23. help="Skip the web UI build step and serve the existing dist directly. "
  24. "Useful for non-interactive contexts (Windows Scheduled Tasks, CI) "
  25. "where npm may not be available. Pre-build with: cd web && npm run build")
  26. parser.add_argument(
  27. "--isolated", action="store_true",
  28. help="When launched from a named profile, run a dedicated server scoped "
  29. "to that profile instead of routing to the machine-level server. "
  30. "Default behavior is unified: profile launches attach to (or start) "
  31. "ONE machine-level server and preselect the profile.")
  32. # Internal: set by the unified-launch re-exec to preselect the launching profile.
  33. parser.add_argument("--open-profile", dest="open_profile", default="", help=argparse.SUPPRESS)
  34. # Lifecycle flags win over the start-a-server flags (they exit first). No service
  35. # manager / PID file: they scan the process table for `hermes dashboard|serve`
  36. # cmdlines and SIGTERM them — the same path `hermes update` uses.
  37. parser.add_argument(
  38. "--stop", action="store_true", help="Stop all running Hermes web server processes and exit")
  39. parser.add_argument(
  40. "--status", action="store_true", help="List running Hermes web server processes and exit")
  41. def _configure_serve_parser(parser, *, cmd_dashboard: Callable) -> None:
  42. """Canonical ``serve`` arguments; shared by the full tree and Desktop's lean hot-path parser."""
  43. _add_server_runtime_args(parser)
  44. # Redundant (serve is always headless) but accepted so legacy callers don't error.
  45. parser.add_argument("--no-open", action="store_true", help=argparse.SUPPRESS)
  46. parser.add_argument(
  47. "--ssh-session-token-file", dest="ssh_session_token_file", metavar="PATH", default=None,
  48. help="Read a one-shot Desktop SSH session token from PATH")
  49. parser.add_argument(
  50. "--ssh-owner-nonce", dest="ssh_owner_nonce", metavar="NONCE", default=None,
  51. help="Identify a Desktop-owned SSH backend process")
  52. parser.set_defaults(func=cmd_dashboard, no_open=True, headless_backend=True, command="serve")
  53. def build_serve_parser(
  54. *, cmd_dashboard: Callable, add_help: bool = True, exit_on_error: bool = True,
  55. ) -> argparse.ArgumentParser:
  56. """Build the standalone parser used by the lean ``serve`` dispatch path."""
  57. parser = argparse.ArgumentParser(
  58. prog="hermes serve",
  59. description="Run the Hermes backend server - the JSON-RPC/WebSocket gateway the "
  60. "desktop app and remote clients connect to. Headless: it never opens "
  61. "a browser UI.",
  62. add_help=add_help, exit_on_error=exit_on_error)
  63. _configure_serve_parser(parser, cmd_dashboard=cmd_dashboard)
  64. return parser
  65. def build_dashboard_parser(
  66. subparsers, *, cmd_dashboard: Callable, cmd_dashboard_register: Callable) -> None:
  67. """Attach ``dashboard`` (browser UI) and ``serve`` (headless backend the desktop spawns)."""
  68. dashboard_parser = subparsers.add_parser(
  69. "dashboard", help="Start the web UI dashboard",
  70. description="Launch the Hermes Agent web dashboard for managing config, API keys, and sessions",
  71. )
  72. _add_server_runtime_args(dashboard_parser)
  73. dashboard_parser.add_argument(
  74. "--no-open", action="store_true", help="Don't open browser automatically")
  75. # Compat shim: desktop shells <= 0.15.x spawn `hermes dashboard --no-open --tui ...`;
  76. # `--tui` was removed (embedded chat always on). Accept + ignore so an old app with a
  77. # new CLI doesn't die on "unrecognized arguments". Drop once the app floor is > 0.16.0.
  78. dashboard_parser.add_argument("--tui", action="store_true", help=argparse.SUPPRESS)
  79. dashboard_parser.set_defaults(func=cmd_dashboard)
  80. # `serve`: same gateway as `dashboard`, never opens a browser. Exists so the desktop
  81. # app / remote backends launch a backend WITHOUT invoking `dashboard` — independent
  82. # surfaces that merely share this server.
  83. serve_parser = subparsers.add_parser(
  84. "serve",
  85. help="Start the Hermes backend server (headless; powers the desktop app and remote backends)",
  86. description="Run the Hermes backend server — the JSON-RPC/WebSocket gateway the "
  87. "desktop app and remote clients connect to. Headless: it never opens "
  88. "a browser UI.")
  89. _configure_serve_parser(serve_parser, cmd_dashboard=cmd_dashboard)
  90. # `register` is nested so bare `hermes dashboard` keeps launching the server.
  91. dashboard_subparsers = dashboard_parser.add_subparsers(dest="dashboard_subcommand")
  92. dashboard_register_parser = dashboard_subparsers.add_parser(
  93. "register",
  94. help="Register a self-hosted dashboard with Nous Portal (writes the OAuth client ID to .env)",
  95. description="Register this install as a self-hosted dashboard with your Nous "
  96. "Portal account. Creates an OAuth client, writes "
  97. "HERMES_DASHBOARD_OAUTH_CLIENT_ID into ~/.hermes/.env, and prints "
  98. "how to engage the login gate. Requires being logged in (hermes setup).")
  99. dashboard_register_parser.add_argument(
  100. "--name", default=None,
  101. help="Human-readable label for the dashboard (default: an auto-generated name)")
  102. dashboard_register_parser.add_argument(
  103. "--redirect-uri", dest="redirect_uri", default=None,
  104. help="Optional public HTTPS OAuth redirect URI for the dashboard, e.g. "
  105. "https://hermes.example.com/auth/callback. Omit for localhost-only use.")
  106. dashboard_register_parser.add_argument(
  107. "--portal-url", dest="portal_url", default=None,
  108. help="Override the Nous Portal base URL for registration (default: the "
  109. "portal you logged into). The access token must be valid at this "
  110. "portal. Also settable via HERMES_DASHBOARD_PORTAL_URL. Mainly for "
  111. "testing against a staging/preview portal.")
  112. dashboard_register_parser.set_defaults(func=cmd_dashboard_register)