moa_cmd.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. """CLI helpers for configuring Mixture of Agents."""
  2. from __future__ import annotations
  3. from typing import Any
  4. from hermes_cli.config import load_config, save_config
  5. from hermes_cli.inventory import build_models_payload, load_picker_context
  6. from hermes_cli.moa_config import DEFAULT_MOA_PRESET_NAME, normalize_moa_config
  7. def _prompt_choice(title: str, rows: list[str], default: int = 0) -> int:
  8. try:
  9. from hermes_cli.curses_ui import curses_radiolist
  10. return curses_radiolist(title, rows, selected=default, cancel_returns=default)
  11. except Exception:
  12. for idx, row in enumerate(rows, start=1):
  13. print(f"{idx}. {row}")
  14. raw = input(f"{title} [{default + 1}]: ").strip()
  15. if not raw:
  16. return default
  17. try:
  18. return max(0, min(len(rows) - 1, int(raw) - 1))
  19. except ValueError:
  20. return default
  21. def _model_options() -> list[dict[str, Any]]:
  22. payload = build_models_payload(
  23. # Keep the profile override inside the worker thread so the full sync picker build (config load,
  24. # pricing, refresh probes) runs off the event loop under the requested profile. Use
  25. # _config_profile_scope (contextvar only, no skill-module lock) — the payload build can block for
  26. # 15s on a models.dev cache miss, and _profile_scope's RLock held across that block starves
  27. # concurrent /api/config and freezes the server (#58576).
  28. load_picker_context(),
  29. # Slot pickers must only offer providers the user can actually call.
  30. # Including setup-only rows makes an unconfigured canonical provider
  31. # (usually OpenRouter, due to catalog ordering) become the default.
  32. include_unconfigured=False,
  33. picker_hints=True,
  34. canonical_order=True,
  35. pricing=True,
  36. capabilities=True,
  37. max_models=200)
  38. providers = payload.get("providers") or []
  39. return [p for p in providers if p.get("slug") and str(p.get("slug")).strip().lower() != "moa" and p.get("models")]
  40. def _pick_slot(current: dict[str, str] | None = None) -> dict[str, str]:
  41. providers = _model_options()
  42. if not providers:
  43. raise RuntimeError("No configured model providers found. Run `hermes model` first.")
  44. current_provider = (current or {}).get("provider", "")
  45. provider_default = next((idx for idx, p in enumerate(providers) if p.get("slug") == current_provider), 0)
  46. provider_rows = [f"{p.get('name') or p.get('slug')} ({p.get('slug')})" for p in providers]
  47. provider = providers[_prompt_choice("Select provider", provider_rows, provider_default)]
  48. models = list(provider.get("models") or [])
  49. if not models:
  50. raise RuntimeError(f"Provider {provider.get('slug')} has no selectable models")
  51. current_model = (current or {}).get("model", "")
  52. model_default = models.index(current_model) if current_model in models else 0
  53. model = models[_prompt_choice(f"Select model for {provider.get('slug')}", models, model_default)]
  54. return {"provider": str(provider.get("slug") or ""), "model": str(model)}
  55. def _format_slot(slot: dict[str, Any]) -> str:
  56. label = f"{slot['provider']}:{slot['model']}"
  57. effort = str(slot.get("reasoning_effort") or "").strip()
  58. return f"{label} [reasoning={effort}]" if effort else label
  59. def _provider_mismatch_notice(
  60. cfg: dict[str, Any], aggregator: dict[str, Any]
  61. ) -> str | None:
  62. main_provider = ""
  63. if isinstance(cfg, dict):
  64. model_section = cfg.get("model")
  65. if isinstance(model_section, dict):
  66. main_provider = str(model_section.get("provider") or "").strip().lower()
  67. agg_provider = str((aggregator or {}).get("provider") or "").strip().lower()
  68. if (
  69. not main_provider
  70. or not agg_provider
  71. or main_provider in ("moa", "auto") # "auto" is a routing pseudo-provider, not a billing seat
  72. or main_provider == agg_provider
  73. ):
  74. return None
  75. return (
  76. f"Aggregator is on {agg_provider}; the whole tool loop will be billed there, "
  77. f"not to {main_provider}."
  78. )
  79. def _print_config(config: dict[str, Any]) -> None:
  80. cfg = _moa_section(config)
  81. print("Mixture of Agents presets")
  82. print(f"Default: {cfg['default_preset']}")
  83. print(f"Active in config: {cfg.get('active_preset') or '(off)'}")
  84. for name, preset in cfg["presets"].items():
  85. print(f"\n{'*' if name == cfg['default_preset'] else ' '} {name}")
  86. print(" Reference models (advise once per user turn by default):")
  87. for idx, slot in enumerate(preset["reference_models"], start=1):
  88. print(f" {idx}. {_format_slot(slot)}")
  89. agg_slot = preset["aggregator"]
  90. print(
  91. f" Aggregator: {_format_slot(agg_slot)} (acting model — runs every step and carries almost all of the cost)"
  92. )
  93. notice = _provider_mismatch_notice(config, agg_slot)
  94. if notice:
  95. print(f" note: {notice}")
  96. def _moa_section(cfg: Any) -> dict[str, Any]:
  97. return normalize_moa_config(cfg.get("moa") if isinstance(cfg, dict) else {})
  98. def _save(cfg: dict, moa: dict[str, Any]) -> None:
  99. cfg["moa"] = normalize_moa_config(moa)
  100. save_config(cfg)
  101. def _cmd_list(cfg: dict, args) -> None:
  102. _print_config(cfg)
  103. def _cmd_configure(cfg: dict, args) -> None:
  104. moa = _moa_section(cfg)
  105. preset_name = (getattr(args, "name", None) or moa.get("default_preset") or DEFAULT_MOA_PRESET_NAME).strip()
  106. current = moa["presets"].get(preset_name, moa["presets"][moa["default_preset"]])
  107. print(f"Configure MoA preset: {preset_name}")
  108. print("Pick at least one reference model; choose Done when finished.")
  109. refs: list[dict[str, str]] = []
  110. existing = list(current.get("reference_models") or [])
  111. while True:
  112. base = existing[len(refs)] if len(refs) < len(existing) else None
  113. picked = _pick_slot(base)
  114. picked["enabled"] = bool((base or {}).get("enabled", True))
  115. refs.append(picked)
  116. if _prompt_choice("Add another reference model?", ["Add another", "Done"], 1) == 1:
  117. break
  118. print("Configure aggregator model.")
  119. print(
  120. "The aggregator is the acting model: it runs every tool-loop step, and almost all of the run's cost lands on its provider."
  121. )
  122. current = dict(current)
  123. current["reference_models"] = refs
  124. current["aggregator"] = _pick_slot(current.get("aggregator"))
  125. moa["presets"][preset_name] = current
  126. moa.setdefault("default_preset", preset_name)
  127. notice = _provider_mismatch_notice(cfg, current["aggregator"])
  128. if notice:
  129. print(notice)
  130. _save(cfg, moa)
  131. print(f"Saved MoA preset: {preset_name}")
  132. _print_config(cfg)
  133. def _cmd_delete(cfg: dict, args) -> None:
  134. moa = _moa_section(cfg)
  135. preset_name = (getattr(args, "name", None) or "").strip()
  136. if not preset_name:
  137. raise SystemExit("Usage: hermes moa delete <name>")
  138. if preset_name not in moa["presets"]:
  139. raise SystemExit(f"Unknown MoA preset: {preset_name}")
  140. if len(moa["presets"]) <= 1:
  141. raise SystemExit("Cannot delete the only MoA preset")
  142. del moa["presets"][preset_name]
  143. if moa["default_preset"] == preset_name:
  144. moa["default_preset"] = next(iter(moa["presets"]))
  145. if moa.get("active_preset") == preset_name:
  146. moa["active_preset"] = ""
  147. _save(cfg, moa)
  148. print(f"Deleted MoA preset: {preset_name}")
  149. _SUBCOMMANDS = {
  150. "list": _cmd_list,
  151. "ls": _cmd_list,
  152. "config": _cmd_configure,
  153. "configure": _cmd_configure,
  154. "delete": _cmd_delete}
  155. def cmd_moa(args) -> None:
  156. """Manage Mixture of Agents model presets."""
  157. cfg = load_config()
  158. sub = getattr(args, "moa_command", None) or "list"
  159. handler = _SUBCOMMANDS.get(sub)
  160. if handler is None:
  161. raise SystemExit(f"Unknown moa subcommand: {sub}")
  162. handler(cfg, args)