cli.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. """Profile-scoped Quick Local controls, loaded without the memory provider."""
  2. from pathlib import Path
  3. from hermes_constants import get_hermes_home
  4. def _run(args):
  5. from .local_server import LocalServer
  6. from .quick_local import QuickLocalSetupError, _server_accepts_config
  7. server = LocalServer(Path(get_hermes_home()))
  8. if not server.paths.server_config.is_file():
  9. print("Quick Local is not configured for this profile.")
  10. return
  11. try:
  12. if args.local_action == "stop":
  13. print("Quick Local stopped." if server.stop() else "Quick Local is already stopped.")
  14. return
  15. if args.local_action in {"start", "restart"}:
  16. from hermes_cli.config import load_config_readonly
  17. from .quick_local import DEPLOYMENT
  18. memory = load_config_readonly().get("memory", {})
  19. settings = memory.get("openviking", {}) if isinstance(memory, dict) else {}
  20. if (
  21. not isinstance(memory, dict)
  22. or memory.get("provider") != "openviking"
  23. or not isinstance(settings, dict)
  24. or settings.get("deployment") != DEPLOYMENT
  25. ):
  26. raise QuickLocalSetupError(
  27. "This profile is not using Quick Local. Run "
  28. "hermes memory setup openviking to select Quick Local."
  29. )
  30. started = server.start(restart=args.local_action == "restart")
  31. print(f"Starting Quick Local at {started.endpoint}...")
  32. server.wait_ready(
  33. started, lambda url: (_server_accepts_config(server._config(), url), "")
  34. )
  35. status = server.status()
  36. print(f"Quick Local: {status['state']} at {status['endpoint']}")
  37. if status["restart_required"]:
  38. print("Saved settings require a restart: hermes openviking local restart")
  39. except QuickLocalSetupError as exc:
  40. print(str(exc))
  41. raise SystemExit(1) from exc
  42. def register_cli(subparser):
  43. commands = subparser.add_subparsers(dest="openviking_command", required=True)
  44. local = commands.add_parser("local", help="Control this profile's Quick Local server")
  45. actions = local.add_subparsers(dest="local_action", required=True)
  46. for name in ("status", "start", "stop", "restart"):
  47. actions.add_parser(name).set_defaults(func=_run)