codex_runtime.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """``hermes codex-runtime`` — noninteractive counterpart of the ``/codex-runtime`` slash command.
  2. ``hermes codex-runtime migrate [--dry-run] [--json]`` runs the same ``~/.codex/config.toml``
  3. migration the slash command triggers when the codex app-server runtime is enabled, on the
  4. selected profile home (``hermes -p NAME codex-runtime migrate``), so automation no longer has to
  5. import ``hermes_cli.codex_runtime_plugin_migration`` directly (issue #79023).
  6. """
  7. from __future__ import annotations
  8. import argparse
  9. import dataclasses
  10. import json
  11. def cmd_codex_runtime_migrate(args: argparse.Namespace) -> int:
  12. """Project Hermes MCP servers (+ codex plugins) into ~/.codex/config.toml; 1 on any error."""
  13. from hermes_cli.codex_runtime_plugin_migration import migrate
  14. from hermes_cli.config import load_config
  15. report = migrate(load_config(), dry_run=args.dry_run)
  16. if args.json:
  17. payload = dataclasses.asdict(report)
  18. payload["target_path"] = str(report.target_path)
  19. print(json.dumps(payload, indent=2, sort_keys=True))
  20. else:
  21. print(report.summary())
  22. return 1 if report.errors else 0
  23. def build_codex_runtime_parser(subparsers) -> None:
  24. """Attach the ``codex-runtime`` subcommand (``migrate`` action) to ``subparsers``."""
  25. parser = subparsers.add_parser(
  26. "codex-runtime", help="Manage the optional codex app-server runtime (migrate MCP config)",
  27. description="Noninteractive counterpart of the /codex-runtime slash command. Toggling the "
  28. "runtime itself stays in the chat command (`/codex-runtime on|off`); `migrate` "
  29. "re-projects Hermes' mcp_servers + installed codex plugins into the managed block of "
  30. "~/.codex/config.toml for the selected profile.")
  31. actions = parser.add_subparsers(dest="codex_runtime_action")
  32. migrate_parser = actions.add_parser(
  33. "migrate", help="Regenerate the hermes-managed block in codex's config.toml",
  34. description="Idempotent: replaces the managed block, keeps user text verbatim, skips Hermes "
  35. "servers whose name the user already declares outside the block, validates the result "
  36. "as TOML before writing atomically.")
  37. migrate_parser.add_argument(
  38. "--dry-run", action="store_true", help="Report what would be written without touching config.toml")
  39. migrate_parser.add_argument(
  40. "--json", action="store_true", help="Print the migration report as JSON (for automation)")
  41. migrate_parser.set_defaults(func=cmd_codex_runtime_migrate)
  42. def _print_help(args):
  43. parser.print_help()
  44. return 0
  45. parser.set_defaults(func=_print_help)