model_setup_flows_common.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. """Shared skeleton for the ``_model_flow_*`` wizards: banner, credentials, model list, picker,
  2. persist, confirmation — one copy of each step. Prompt strings and the config keys written (and
  3. their insertion order = config.yaml key order) are behavior; keep them byte-identical.
  4. main_provider_setup / auth / config imports are lazy on purpose: main.py imports the flows (import
  5. cycle) and tests patch ``hermes_cli.config.load_config`` etc. at call time.
  6. """
  7. from __future__ import annotations
  8. import contextlib
  9. import subprocess
  10. from urllib.parse import urlparse
  11. from hermes_cli.cli_output import line_input
  12. from hermes_cli.config import clear_model_endpoint_credentials
  13. _HTTP = ("http://", "https://")
  14. def _note_setup_failure(failure_class: str) -> None:
  15. """Why a flow returns without a pick, for the provider-setup metric (no-op outside a tracked flow)."""
  16. from hermes_cli.observability.shared_metrics_setup import note_provider_setup_failure
  17. note_provider_setup_failure(failure_class)
  18. def _say(*lines: str) -> None:
  19. """``print`` each line (``""`` = blank line); one call per banner block."""
  20. print("\n".join(lines))
  21. def _ask(prompt: str, *, secret: bool = False, raw: bool = False, cancel_msg: str | None = "\nCancelled.",
  22. on_cancel=None):
  23. """One stripped prompt answer. ``raw`` uses builtin ``input`` (numbered fallbacks),
  24. ``secret`` the masked prompt, else ``line_input``. Ctrl-C/EOF prints *cancel_msg*
  25. (``None`` = silent) and returns *on_cancel*."""
  26. if secret:
  27. from hermes_cli.secret_prompt import masked_secret_prompt as fn
  28. else:
  29. fn = input if raw else line_input
  30. try:
  31. return fn(prompt).strip()
  32. except (KeyboardInterrupt, EOFError):
  33. if cancel_msg is not None:
  34. print(cancel_msg)
  35. return on_cancel
  36. def _existing_api_key_for_model_flow(provider_id: str, pconfig) -> tuple[str, str]:
  37. """Resolve an existing wizard credential without changing its storage."""
  38. from hermes_cli.auth import _resolve_api_key_provider_secret
  39. return _resolve_api_key_provider_secret(provider_id, pconfig)
  40. def _ensure_flow_api_key(provider_id: str, pconfig, *, missing_hint=()) -> tuple[str, str, bool]:
  41. """Resolve the stored key, print *missing_hint* lines when none exists, then run
  42. ``_prompt_api_key`` (users can replace a stale key in-flow via K/R/C).
  43. Returns ``(existing_key, resolved_key, abort)``.
  44. """
  45. from hermes_cli.main_provider_setup import _prompt_api_key
  46. existing_key, existing_source = _existing_api_key_for_model_flow(provider_id, pconfig)
  47. if not existing_key:
  48. for line in missing_hint:
  49. print(line)
  50. resolved, abort = _prompt_api_key(pconfig, existing_key, provider_id=provider_id, existing_source=existing_source)
  51. return existing_key, resolved, abort
  52. def _load_config_model_section() -> tuple[dict, dict]:
  53. """Return ``(cfg, cfg["model"])`` with the model section coerced to a dict."""
  54. from hermes_cli.config import load_config
  55. cfg = load_config()
  56. model = cfg.get("model")
  57. if not isinstance(model, dict):
  58. model = {"default": model} if model else {}
  59. cfg["model"] = model
  60. return cfg, model
  61. def _begin_model_config(selected: str, provider: str) -> tuple[dict, dict]:
  62. """Record *selected* as the model choice and open the config model section
  63. with ``provider`` set; callers set endpoint fields then ``_commit_model_config``."""
  64. from hermes_cli.auth import _save_model_choice
  65. _save_model_choice(selected)
  66. cfg, model = _load_config_model_section()
  67. model["provider"] = provider
  68. return cfg, model
  69. def _commit_model_config(cfg: dict) -> None:
  70. """Persist *cfg* and deactivate any OAuth provider."""
  71. from hermes_cli.auth import deactivate_provider
  72. from hermes_cli.config import save_config
  73. save_config(cfg)
  74. deactivate_provider()
  75. def _persist_model(selected: str, provider: str, *, base_url: str | None = None, api_mode: str | None = None,
  76. drop_base_url: bool = False, drop_api_mode: bool = False, clear_creds: bool = True,
  77. finish=None) -> dict:
  78. """The standard persist step: ``_save_model_choice`` → model section with ``provider``,
  79. ``base_url`` then ``api_mode`` (that order is the config.yaml key order) → scrub inline
  80. endpoint credentials (``clear_creds``; ``drop_api_mode`` also pops ``api_mode``) →
  81. *finish(cfg, model)* for extra sections → save + deactivate OAuth provider."""
  82. cfg, model = _begin_model_config(selected, provider)
  83. if base_url is not None:
  84. model["base_url"] = base_url
  85. if api_mode is not None:
  86. model["api_mode"] = api_mode
  87. if drop_base_url:
  88. model.pop("base_url", None)
  89. if clear_creds:
  90. clear_model_endpoint_credentials(model, clear_api_mode=drop_api_mode)
  91. elif drop_api_mode:
  92. model.pop("api_mode", None)
  93. if finish is not None:
  94. finish(cfg, model)
  95. _commit_model_config(cfg)
  96. return model
  97. def _finish_model(selected, provider: str, done: str, *, no_change: str = "No change.", **persist_kw):
  98. """``_persist_model`` + confirmation line when *selected*, else the no-change line.
  99. Returns the saved model section (None when nothing was selected)."""
  100. if not selected:
  101. print(no_change)
  102. return None
  103. model = _persist_model(selected, provider, **persist_kw)
  104. print(done)
  105. return model
  106. def _activate_provider_model(selected, provider_id: str, base_url: str, done: str,
  107. no_change: str | None = "No change.") -> None:
  108. """OAuth-provider persist: model choice + ``_update_config_for_provider`` (which owns
  109. the auth-state bookkeeping), then *done*; *no_change* (``None`` = silent) otherwise."""
  110. from hermes_cli.auth import _save_model_choice, _update_config_for_provider
  111. if not selected:
  112. if no_change is not None:
  113. print(no_change)
  114. return
  115. _save_model_choice(selected)
  116. _update_config_for_provider(provider_id, base_url)
  117. print(done)
  118. def _ensure_dict_section(cfg: dict, key: str) -> dict:
  119. """Return ``cfg[key]`` as a dict, replacing a missing/non-dict value."""
  120. section = cfg.get(key)
  121. if not isinstance(section, dict):
  122. section = {}
  123. cfg[key] = section
  124. return section
  125. def _pick_model_or_prompt(model_list, prompt: str, **kwargs):
  126. """Radio picker when *model_list* is non-empty, else a free-text ``line_input``
  127. (None on Ctrl-C/EOF)."""
  128. from hermes_cli.auth import _prompt_model_selection
  129. if model_list:
  130. return _prompt_model_selection(model_list, **kwargs)
  131. return _ask(prompt, cancel_msg=None)
  132. def _login_retry_context(args) -> tuple[str, str]:
  133. """(retry_command, service_host) for a login helper's failure copy, from the ``ProviderConfig``
  134. among its positional args. Nous keeps ``hermes portal``; every other OAuth provider is retried
  135. with ``hermes auth add <provider>`` and named by its own portal host (``hermes login`` no longer
  136. exists). Falls back to ``hermes model`` when no provider config is in play."""
  137. pconfig = next((a for a in args if hasattr(a, "id") and hasattr(a, "portal_base_url")), None)
  138. if pconfig is None:
  139. return "hermes model", "the sign-in service"
  140. provider_id = str(getattr(pconfig, "id", "") or "")
  141. host = urlparse(str(getattr(pconfig, "portal_base_url", "") or "")).hostname or "the sign-in service"
  142. if provider_id == "nous":
  143. return "hermes portal", host
  144. return (f"hermes auth add {provider_id}" if provider_id else "hermes model"), host
  145. def _run_login(login_fn, *args, **kwargs) -> bool:
  146. """Run an OAuth login helper; print plain failure copy (what happened + retry command) and
  147. return False on SystemExit / any exception. The retry command and service host come from the
  148. provider config passed to the helper, so a MiniMax failure never says ``hermes portal`` /
  149. ``portal.nousresearch.com``. Helpers that print their own copy raise ``SystemExit(1)`` with no
  150. message, which stays silent; a SystemExit that carries a message (or a non-cancel code from a
  151. helper that printed nothing) gets a one-line explanation so the user is never left with no
  152. output."""
  153. from hermes_cli.auth_error_copy import sign_in_failure_lines
  154. retry_command, service_host = _login_retry_context(args)
  155. try:
  156. login_fn(*args, **kwargs)
  157. except SystemExit as exc:
  158. if exc.code in (130, None, 0):
  159. print("Sign-in was cancelled.")
  160. elif isinstance(exc.code, str) and exc.code.strip():
  161. print(f"Sign-in did not complete: {exc.code.strip()}")
  162. print(f"Run `{retry_command}` to try again.")
  163. else:
  164. print(f"Sign-in did not complete; run `{retry_command}` to try again.")
  165. return False
  166. except Exception as exc:
  167. for line in sign_in_failure_lines(exc, service_host=service_host, retry_command=retry_command):
  168. print(line)
  169. return False
  170. return True
  171. def _oauth_gate(logged_in: bool, name: str, login_fn, *login_args, fresh_name: str = "", recheck=None) -> bool:
  172. """Login-or-reuse step for OAuth providers. Not logged in → start a login. Logged in →
  173. use / reauthenticate (``force_new_login``; *recheck()* must then report logged in) /
  174. cancel. Returns False when the flow must stop."""
  175. if not logged_in:
  176. _say(f"Not logged into {name}. Starting login...", "")
  177. return _run_login(login_fn, *login_args)
  178. _say(f" {name} credentials: ✓", "")
  179. choice = _prompt_auth_credentials_choice(f"{name} credentials:")
  180. if choice == "cancel":
  181. return False
  182. if choice == "reauth":
  183. _say(f"Starting a fresh {fresh_name or name} login...", "")
  184. if not _run_login(login_fn, *login_args, force_new_login=True):
  185. return False
  186. if recheck is not None and not recheck():
  187. print("Login failed.")
  188. return False
  189. return True
  190. def _models_dev_merged(provider_id: str, curated) -> list:
  191. """Curated ids first (our agentic picks lead, and models.dev lag can't hide new ones), then
  192. models.dev agentic models not yet listed (case-insensitive; models.dev's spelling wins since
  193. it comes from the provider). Live-first providers keep
  194. models.dev order ahead of curated, matching ``_profile_live_catalog``. Empty list when
  195. models.dev has nothing / is unavailable."""
  196. from hermes_cli.models_catalog_static import _LIVE_FIRST_PICKER_PROVIDERS
  197. mdev_models: list = []
  198. with contextlib.suppress(Exception):
  199. from agent.models_dev import list_agentic_models
  200. mdev_models = list_agentic_models(provider_id)
  201. if not mdev_models:
  202. return []
  203. primary, secondary = (mdev_models, curated) if provider_id in _LIVE_FIRST_PICKER_PROVIDERS else (curated, mdev_models)
  204. canonical = {m.lower(): m for m in mdev_models}
  205. merged: dict = {}
  206. for m in [*primary, *secondary]:
  207. merged.setdefault(m.lower(), canonical.get(m.lower(), m))
  208. return list(merged.values())
  209. def _show_curated(model_list) -> None:
  210. if model_list:
  211. print(f' Showing {len(model_list)} curated models — use "Enter custom model name" for others.')
  212. def _prune_replaced_custom_model_config_credentials(base_url: str, *, provider_name: str = "") -> None:
  213. """Drop stale ``model_config`` ("the credential under ``model.api_key``") entries from inactive
  214. custom pools: after an explicit custom-endpoint switch an old pool still carrying that source
  215. points at the previous endpoint and could be selected before the fresh config."""
  216. try:
  217. from agent.credential_pool import CUSTOM_POOL_PREFIX, custom_provider_pool_key_candidates
  218. from hermes_cli.auth import read_credential_pool, write_credential_pool
  219. # A keyed ``providers.<key>`` endpoint stores under the durable slug while
  220. # legacy pools keep ``custom:<display-name>``; every identity the active
  221. # endpoint may occupy must be skipped or its own legacy pool gets pruned.
  222. # See #100413.
  223. active_pool_keys = {
  224. str(key).strip().lower()
  225. for key in custom_provider_pool_key_candidates(base_url, provider_name=provider_name or None)}
  226. if not active_pool_keys:
  227. return
  228. pools = read_credential_pool(None)
  229. if not isinstance(pools, dict):
  230. return
  231. for pool_key, entries in pools.items():
  232. if (
  233. not isinstance(pool_key, str)
  234. or not pool_key.startswith(CUSTOM_POOL_PREFIX)
  235. or pool_key in active_pool_keys
  236. or not isinstance(entries, list)):
  237. continue
  238. retained = [e for e in entries if not (isinstance(e, dict) and e.get("source") == "model_config")]
  239. if len(retained) != len(entries):
  240. removed_ids = [
  241. str(e["id"]) for e in entries
  242. if isinstance(e, dict) and e.get("source") == "model_config" and e.get("id")]
  243. write_credential_pool(pool_key, retained, removed_ids=removed_ids)
  244. except Exception:
  245. return
  246. def _curses_choice(title: str, rows: list, default_idx: int):
  247. """``_curses_prompt_choice`` index, or None when curses is unavailable (piped stdin,
  248. non-TTY) so the caller can fall back to a numbered prompt."""
  249. try:
  250. from hermes_cli.setup import _curses_prompt_choice
  251. return _curses_prompt_choice(title, rows, default_idx)
  252. except Exception:
  253. return None
  254. def _radiolist(title: str, items: list, default_idx: int = 0, **kw):
  255. """``curses_radiolist`` index (-1 = cancelled), or None when curses is unavailable so the
  256. caller can fall back to a numbered prompt."""
  257. try:
  258. from hermes_cli.curses_ui import curses_radiolist
  259. return curses_radiolist(title, items, selected=default_idx, cancel_returns=-1, **kw)
  260. except (ImportError, NotImplementedError, OSError, subprocess.SubprocessError):
  261. return None
  262. def _print_numbered(title: str, rows: list, default_idx: int) -> None:
  263. print(title)
  264. for i, row in enumerate(rows, 1):
  265. marker = "→" if (i - 1) == default_idx else " "
  266. print(f" {marker} {i}. {row}")
  267. def _prompt_auth_credentials_choice(title: str) -> str:
  268. """Prompt for reuse / reauthenticate / cancel with the standard radio UI.
  269. Returns one of ``"use"``, ``"reauth"``, ``"cancel"``. Falls back to a
  270. numbered prompt when curses is unavailable (piped stdin, non-TTY).
  271. """
  272. choices = ["Use existing credentials", "Reauthenticate (new OAuth login)", "Cancel"]
  273. idx = _curses_choice(title, choices, 0)
  274. if idx is not None and idx >= 0:
  275. print()
  276. return ("use", "reauth", "cancel")[idx]
  277. _print_numbered(title, choices, 0)
  278. print()
  279. choice = _ask(" Choice [1/2/3]: ", raw=True, cancel_msg=None, on_cancel="1")
  280. return {"2": "reauth", "3": "cancel"}.get(choice, "use")