migrate.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. """CLI handlers for ``hermes migrate ...``.
  2. ``hermes migrate`` (bare) and ``hermes migrate <agent>`` move a user over from another agent through
  3. the agent-importers plugin; ``hermes migrate xai`` diagnoses and (with --apply) rewrites references to
  4. xAI models retired on May 15, 2026.
  5. """
  6. from __future__ import annotations
  7. import sys
  8. from pathlib import Path
  9. from typing import Any
  10. from hermes_cli.colors import Colors, color
  11. from hermes_cli.config import load_config
  12. def cmd_migrate(args: Any) -> int:
  13. """Dispatcher for ``hermes migrate <subtype>``."""
  14. if getattr(args, "migrate_type", None) == "xai":
  15. return cmd_migrate_xai(args)
  16. if getattr(args, "migrate_type", None) is None:
  17. from hermes_cli.config import get_hermes_home
  18. from hermes_cli.setup_agent_import import migrate_from_agents
  19. return migrate_from_agents(get_hermes_home())
  20. print("usage: hermes migrate [<agent> | xai [--apply] [--no-backup]]", file=sys.stderr)
  21. return 2
  22. # ``hermes migrate <agent>`` flags forwarded to ``hermes import-agent <agent>``, as (dest, flag).
  23. _AGENT_IMPORT_FLAGS = (("dry_run", "--dry-run"), ("overwrite", "--overwrite"), ("yes", "--yes"),
  24. ("activate_routines", "--activate-routines"), ("migrate_secrets", "--migrate-secrets"))
  25. def cmd_migrate_agent(args: Any) -> int:
  26. """``hermes migrate <agent>``: install the agent-importers plugin when missing, then run its
  27. import for that agent with the flags given here."""
  28. from hermes_cli.config import get_hermes_home
  29. from hermes_cli.setup_agent_import import migrate_from_agents
  30. extra = [flag for dest, flag in _AGENT_IMPORT_FLAGS if getattr(args, dest, False)]
  31. if getattr(args, "source", None):
  32. extra += ["--source", str(Path(args.source).expanduser())]
  33. for name in getattr(args, "bot", None) or ():
  34. extra += ["--bot", name]
  35. return migrate_from_agents(get_hermes_home(), args.migrate_agent, tuple(extra))
  36. def _fail(message: str) -> int:
  37. print(f" {color('✗', Colors.RED)} {message}", file=sys.stderr)
  38. return 1
  39. def cmd_migrate_xai(args: Any) -> int:
  40. """Run xAI May-15 model migration in dry-run or apply mode."""
  41. from hermes_cli.config_backend import config_exists
  42. from hermes_cli.xai_retirement import (
  43. MIGRATION_GUIDE_URL, RETIREMENT_DATE, apply_migration, find_retired_xai_refs, format_issue)
  44. apply = bool(getattr(args, "apply", False))
  45. no_backup = bool(getattr(args, "no_backup", False))
  46. issues = find_retired_xai_refs(load_config())
  47. print()
  48. print(color(f"◆ xAI Model Retirement Migration ({RETIREMENT_DATE})", Colors.CYAN, Colors.BOLD))
  49. print()
  50. if not issues:
  51. print(f" {color('✓', Colors.GREEN)} No retired xAI models in config — nothing to migrate.")
  52. return 0
  53. print(f" Found {len(issues)} retired xAI model reference(s):")
  54. print()
  55. for issue in issues:
  56. print(f" {color('⚠', Colors.YELLOW)} {format_issue(issue)}")
  57. print()
  58. print(f" {color('→', Colors.CYAN)} Migration guide: {MIGRATION_GUIDE_URL}")
  59. print()
  60. config_path = _resolve_config_path()
  61. if not apply:
  62. print(color("Dry-run mode — no changes written.", Colors.DIM))
  63. print(color(
  64. "Re-run with `hermes migrate xai --apply` to rewrite "
  65. f"{config_path} in-place (backup created automatically).",
  66. Colors.DIM))
  67. return 0
  68. if not config_path or not config_exists(config_path):
  69. return _fail(f"Could not locate config.yaml (looked at: {config_path})")
  70. try:
  71. result = apply_migration(config_path=config_path, issues=issues, backup=not no_backup)
  72. except Exception as exc:
  73. return _fail(f"Migration failed: {exc}")
  74. if not result.config_changed:
  75. print(f" {color('⚠', Colors.YELLOW)} No changes written.")
  76. return 0
  77. if result.backup_path is not None:
  78. print(f" {color('✓', Colors.GREEN)} Backup: {result.backup_path}")
  79. print(
  80. f" {color('✓', Colors.GREEN)} Updated {len(result.issues_resolved)} "
  81. f"slot(s) in {result.file_path}")
  82. print()
  83. print(color("Run `hermes doctor` to confirm no retired xAI models remain.", Colors.DIM))
  84. return 0
  85. def _resolve_config_path() -> Path:
  86. """Best-effort: locate the active config.yaml on disk."""
  87. from hermes_cli.config import get_hermes_home
  88. return get_hermes_home() / "config.yaml"