auth_model_picker.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. """Interactive model picker used after OAuth login.
  2. Re-exported from ``hermes_cli/auth.py`` (patch targets unchanged); origin helpers are imported
  3. lazily per function so ``hermes_cli.auth.<helper>`` patches still intercept and no cycle forms.
  4. """
  5. from __future__ import annotations
  6. import logging
  7. import subprocess
  8. from typing import Dict, List, Optional
  9. from hermes_cli.auth_constants import DEFAULT_NOUS_PORTAL_URL
  10. logger = logging.getLogger("hermes_cli.auth")
  11. _CUSTOM_LABEL = "Enter custom model name"
  12. _SKIP_LABEL = "Skip (keep current)"
  13. _CURRENT_SUFFIX = " ← currently in use"
  14. def _confirm_selection_guards(
  15. model_id: str, *, provider: str = "", base_url: str = "", api_key: str = "",
  16. include_kinds: Optional[list[str]] = None,
  17. ) -> bool:
  18. """Prompt before saving a model that trips any selection guard (cost, data-policy, ...).
  19. Shows one [y/N] confirm listing every warning that fired. Returns True to proceed.
  20. """
  21. try:
  22. from hermes_cli.model_selection_guards import combined_message, selection_warnings
  23. warnings = selection_warnings(
  24. model_id, provider=provider, base_url=base_url, api_key=api_key, include_kinds=include_kinds,
  25. )
  26. except Exception:
  27. warnings = []
  28. if not warnings:
  29. return True
  30. print()
  31. print("=" * 72)
  32. print(combined_message(warnings))
  33. print("=" * 72)
  34. try:
  35. response = input("Switch anyway? [y/N]: ").strip().lower()
  36. except (KeyboardInterrupt, EOFError):
  37. print()
  38. return False
  39. return response in {"y", "yes"}
  40. # Usage column on a window plan: "Large" / "Free" (other levels carry no tag).
  41. _COST_COL = len("Large")
  42. _GAUGE_GAP = " "
  43. # Style per gateway level: Free green, Large yellow (it draws on its own limit), the rest plain.
  44. _LEVEL_STYLE = {"free": "green", "top_tier": "yellow"}
  45. class _ModelPickerRows:
  46. """Column-aligned picker rows.
  47. Usage-window plan: ``Large`` / ``Free`` on the rows the gateway marks, exactly as
  48. the gateway sent it on this caller's catalog. Every other account keeps ``$/Mtok``.
  49. Chrome is emitted as styled segments, not ANSI baked into one string — curses addnstr would
  50. render escape bytes literally.
  51. """
  52. def __init__(
  53. self, all_models: list[str], pricing: Optional[dict[str, dict[str, str]]], *,
  54. current_model: str, sale_chrome: bool, usage=None,
  55. notes: Optional[dict[str, str]] = None,
  56. ) -> None:
  57. from hermes_cli.models_pricing import _format_price_per_mtok, compute_sale_discount
  58. self.current_model = current_model
  59. # Per-model dim annotation (e.g. "usage credits"); the row stays selectable.
  60. self.notes = notes or {}
  61. self.window_mode = usage is not None
  62. if self.window_mode:
  63. # The gateway's ``top_tier`` marks decide which rows a full large-model limit dims.
  64. usage = usage.with_pricing(pricing)
  65. self.usage = usage
  66. if self.window_mode:
  67. from hermes_cli.model_picker_usage import usage_tag_levels
  68. # Tags only: dimming reads ``usage.blocks`` (free_ids), so subscription-billed rows still never dim.
  69. self.levels = usage_tag_levels(all_models, pricing)
  70. self.has_pricing = False
  71. self.has_cache = False
  72. self.any_on_sale = False
  73. self.price_col = self.cache_col = 0
  74. self._price_cache = {}
  75. self.has_top_tier = "top_tier" in self.levels.values()
  76. self.name_col = max((len(m) for m in all_models), default=0) + 2
  77. return
  78. self.levels = {}
  79. self.has_top_tier = False
  80. self.has_pricing = bool(pricing and any(pricing.get(m) for m in all_models))
  81. # Leave room for a leading "★ " on sale rows (Nous only).
  82. name_pad = 3 if sale_chrome else 2
  83. self.name_col = max((len(m) for m in all_models), default=0) + name_pad if self.has_pricing else 0
  84. # (inp, out, cache, pct|None, was_inp, was_out)
  85. self._price_cache: dict[str, tuple[str, str, str, int | None, str, str]] = {}
  86. self.price_col = 3 # minimum width
  87. self.cache_col = 0 # only set if any model has cache pricing
  88. self.has_cache = False
  89. self.any_on_sale = False
  90. if not self.has_pricing:
  91. return
  92. def _was(raw: str) -> str:
  93. return _format_price_per_mtok(raw) if raw != "" else "?"
  94. for mid in all_models:
  95. p = pricing.get(mid) # type: ignore[union-attr]
  96. pct: int | None = None
  97. was_inp = was_out = ""
  98. inp, out, cache = "", "", ""
  99. if p:
  100. inp = _format_price_per_mtok(p.get("prompt", ""))
  101. out = _format_price_per_mtok(p.get("completion", ""))
  102. cache_read = p.get("input_cache_read", "")
  103. cache = _format_price_per_mtok(cache_read) if cache_read else ""
  104. if cache:
  105. self.has_cache = True
  106. sale = compute_sale_discount(p.get("prompt", ""), p.get("completion", ""), p.get("original")) if sale_chrome else None
  107. if sale is not None:
  108. self.any_on_sale = True
  109. pct, was_prompt_raw, was_out_raw = sale
  110. # Natively-free models (no gateway original) carry empty was_* raws — leave
  111. # them empty so the row shows bare "-100%" with no "was ?/?" suffix.
  112. if was_prompt_raw != "" or was_out_raw != "":
  113. was_inp, was_out = _was(was_prompt_raw), _was(was_out_raw)
  114. self._price_cache[mid] = (inp, out, cache, pct, was_inp, was_out)
  115. self.price_col = max(self.price_col, len(inp), len(out))
  116. self.cache_col = max(self.cache_col, len(cache))
  117. if self.has_cache:
  118. self.cache_col = max(self.cache_col, 5) # minimum: "Cache" header
  119. def _cost_segments(self, mid: str) -> list[tuple[str, str | None]]:
  120. """Window row: padded name (dim when a full allowance stops it) + Large / Free."""
  121. segs: list[tuple[str, str | None]] = [(f"{mid:<{self.name_col}}", "dim" if self.usage.blocks(mid) else None)]
  122. from hermes_cli.model_picker_usage import usage_level_label
  123. level = self.levels.get(mid)
  124. word = usage_level_label(level, "en")
  125. segs.extend(((_GAUGE_GAP, None), (f"{word:<{_COST_COL}}", _LEVEL_STYLE.get(level or ""))))
  126. return segs
  127. def segments(self, mid: str) -> list[tuple[str, str | None]]:
  128. """Build a rich radiolist row: yellow ★/% , dim was, plain prices — or usage gauges on the window."""
  129. current = [(_CURRENT_SUFFIX, None)] if mid == self.current_model else []
  130. note = [(f" · {self.notes[mid]}", "dim")] if self.notes.get(mid) else []
  131. if self.window_mode:
  132. return self._cost_segments(mid) + current
  133. if not self.has_pricing:
  134. return [(mid, None), *note, *current]
  135. inp, out, cache, pct, was_inp, was_out = self._price_cache.get(mid, ("", "", "", None, "", ""))
  136. on_sale = pct is not None
  137. # Reserve 2 columns for "★ " so sale and non-sale names share alignment.
  138. if on_sale:
  139. segs: list[tuple[str, str | None]] = [("★ ", "yellow"), (f"{mid:<{self.name_col - 2}}", None)]
  140. else:
  141. segs = [(f"{mid:<{self.name_col}}", None)]
  142. price_part = f" {inp:>{self.price_col}} {out:>{self.price_col}}"
  143. if self.has_cache:
  144. price_part += f" {cache:>{self.cache_col}}"
  145. segs.append((price_part, None))
  146. if on_sale:
  147. segs.append((f" -{pct}%", "yellow"))
  148. if was_inp or was_out:
  149. segs.append((f" was {was_inp}/{was_out}", "dim"))
  150. return segs + note + current
  151. def label(self, mid: str) -> str:
  152. return "".join(text for text, _style in self.segments(mid))
  153. def description_lines(self) -> list[str]:
  154. """Lines between the title and the list: plan gauges (window plan), column headers, legend.
  155. Each choice is " {label}" (2 spaces) plus a 3-char cursor region ("-> " or " "), so column
  156. content starts at col 5."""
  157. pad = " " * 5
  158. lines: list[str] = []
  159. if self.window_mode:
  160. from hermes_cli.model_picker_usage import plan_gauge_lines
  161. for i, line in enumerate(plan_gauge_lines(self.usage)):
  162. suffix = f" — {self.usage.included_summary}" if i == 0 else ""
  163. lines.append(f"{pad}{line}{suffix}")
  164. lines.append(f"{pad}{'':>{self.name_col}}{_GAUGE_GAP}Usage")
  165. return lines
  166. if self.has_pricing:
  167. header = f"{pad}{'':>{self.name_col}} {'In':>{self.price_col}} {'Out':>{self.price_col}}"
  168. if self.has_cache:
  169. header += f" {'Cache':>{self.cache_col}}"
  170. header += " $/Mtok"
  171. if self.any_on_sale:
  172. header += " ★ = on sale"
  173. lines.append(header)
  174. return lines
  175. def menu_title(self, title: str = "Select default model:") -> str:
  176. """*title* plus the description lines (headers / gauges), newline-joined."""
  177. lines = self.description_lines()
  178. return title + ("\n" + "\n".join(lines) if lines else "")
  179. def footnote(self) -> str:
  180. """Window plan with a Large row listed: the one line that says what Large means."""
  181. if not self.has_top_tier:
  182. return ""
  183. from hermes_cli.model_picker_usage import TOP_TIER_FOOTNOTE
  184. return TOP_TIER_FOOTNOTE
  185. def _picker_order(model_ids: list[str], current_model: str, window_usage) -> tuple[list[str], dict[int, str], int]:
  186. """``(ordered ids, {row index: section heading}, cursor start)`` for the picker.
  187. Window plan: "Recommended" (the plan's featured picks) then "Other models", with the current
  188. model left in its own section (the cursor starts on it). Everyone else: current first.
  189. """
  190. sections: dict[int, str] = {}
  191. if window_usage is not None:
  192. from hermes_cli.model_picker_usage import SECTION_OTHER, SECTION_RECOMMENDED, split_recommended
  193. head, rest = split_recommended(list(dict.fromkeys(model_ids)), window_usage.featured_model_ids)
  194. ordered = head + rest
  195. if head:
  196. sections[0] = SECTION_RECOMMENDED
  197. if rest:
  198. sections[len(head)] = SECTION_OTHER
  199. else:
  200. # Reorder: current model first, then the rest (deduplicated)
  201. ordered = list(dict.fromkeys(
  202. ([current_model] if current_model and current_model in model_ids else []) + list(model_ids)
  203. ))
  204. return ordered, sections, (ordered.index(current_model) if current_model in ordered else 0)
  205. def _print_numbered_rows(rows: _ModelPickerRows, menu_title: str, ordered: list[str], sections: dict[int, str]) -> int:
  206. """The no-curses fallback's title, numbered rows (+ custom / skip) and footnote; returns the number width."""
  207. from hermes_cli.curses_ui import colorize_gauge_bars_ansi, format_radio_item_ansi
  208. from hermes_cli.colors import Colors, color
  209. n, _DIM, _RESET = len(ordered), "\033[2m", "\033[0m"
  210. for line in menu_title.splitlines():
  211. if rows.window_mode:
  212. line = colorize_gauge_bars_ansi(line)
  213. print(line.replace("★", color("★", Colors.YELLOW), 1) if "★" in line else line)
  214. num_width = len(str(n + 2))
  215. for i, mid in enumerate(ordered, 1):
  216. if (i - 1) in sections:
  217. print(f" {_DIM}{sections[i - 1]}{_RESET}")
  218. print(f" {i:>{num_width}}. {format_radio_item_ansi(rows.segments(mid))}")
  219. print(f" {n + 1:>{num_width}}. {_CUSTOM_LABEL}")
  220. print(f" {n + 2:>{num_width}}. {_SKIP_LABEL}")
  221. if footnote := rows.footnote():
  222. print(f" {_DIM}{footnote}{_RESET}")
  223. return num_width
  224. def _prompt_model_selection(
  225. model_ids: list[str], current_model: str = "",
  226. pricing: Optional[dict[str, dict[str, str]]] = None,
  227. unavailable_models: Optional[list[str]] = None, portal_url: str = "",
  228. unavailable_message: str = "", confirm_provider: str = "", confirm_base_url: str = "",
  229. confirm_api_key: str = "", usage=None,
  230. notes: Optional[dict[str, str]] = None, title: str = "Select default model:",
  231. ) -> Optional[str]:
  232. """Interactive model picker; current_model listed first. Returns the chosen model ID or None. *title* heads
  233. the menu (a plugin picking a model for something other than the default says so).
  234. With *pricing* (``{model_id: {prompt, completion}}``) a compact price column is shown; models in
  235. *unavailable_models* render grayed out and unselectable with an upgrade link to *portal_url*.
  236. *usage* (``model_picker_usage.PickerUsage``, Nous only) swaps ``$/Mtok`` for plan gauges and the
  237. gateway's per-model usage word on a usage-window plan.
  238. """
  239. from hermes_cli.cli_output import line_input
  240. _unavailable = unavailable_models or []
  241. # Sale chrome is Nous Portal-only, even if pricing.original is present for another provider.
  242. sale_chrome = (confirm_provider or "").strip().lower() == "nous"
  243. def _confirmed_selection(mid: str) -> Optional[str]:
  244. if not mid:
  245. return None
  246. # Cost guard needs a known provider; id-keyed guards (data policy) always run.
  247. ok = _confirm_selection_guards(
  248. mid, provider=confirm_provider, base_url=confirm_base_url, api_key=confirm_api_key,
  249. include_kinds=None if confirm_provider else ["data_policy"],
  250. )
  251. return mid if ok else None
  252. def _custom_selection() -> Optional[str]:
  253. try:
  254. custom = line_input("Enter model name: ").strip()
  255. except (EOFError, KeyboardInterrupt):
  256. return None
  257. return _confirmed_selection(custom) if custom else None
  258. # All models for column-width computation (selectable + unavailable). Plan usage is Nous-only chrome.
  259. window_usage = usage if sale_chrome else None
  260. ordered, sections, start = _picker_order(model_ids, current_model, window_usage)
  261. rows = _ModelPickerRows(
  262. ordered + list(_unavailable), pricing, current_model=current_model, sale_chrome=sale_chrome,
  263. usage=window_usage, notes=notes)
  264. _DIM = "\033[2m"
  265. _RESET = "\033[0m"
  266. menu_title = rows.menu_title(title)
  267. _upgrade_url = (portal_url or DEFAULT_NOUS_PORTAL_URL).rstrip("/")
  268. n = len(ordered)
  269. # Try arrow-key menu first, fall back to number input.
  270. try:
  271. from hermes_cli.curses_ui import curses_radiolist
  272. choices = [rows.segments(mid) for mid in ordered] + [_CUSTOM_LABEL, _SKIP_LABEL]
  273. unavailable_footer = unavailable_message.strip()
  274. if not unavailable_footer and _unavailable:
  275. unavailable_footer = f"Upgrade at {_upgrade_url} for paid models"
  276. # Header/legend/gauges + unavailable block go in the description so they survive the curses clear.
  277. desc_lines: list[str] = rows.description_lines()
  278. if _unavailable:
  279. desc_lines.extend(f" {rows.label(mid)}" for mid in _unavailable)
  280. desc_lines.append(f" ── {unavailable_footer} ──")
  281. if footnote := rows.footnote():
  282. desc_lines.append(f" {footnote}")
  283. # Search haystack = label + aliases for brand-less wire ids (Kimi `k3` ↔ "kimi"); skip when
  284. # model_search_text adds nothing beyond the bare id.
  285. from hermes_cli.model_search import model_search_text
  286. model_search_labels = []
  287. for mid in ordered:
  288. label, haystack = rows.label(mid), model_search_text(mid)
  289. model_search_labels.append(label if haystack == mid else f"{label} {haystack}")
  290. model_search_labels += [_CUSTOM_LABEL, _SKIP_LABEL]
  291. idx = curses_radiolist(
  292. title,
  293. choices,
  294. selected=start, # cursor on the current model
  295. cancel_returns=-1,
  296. description="\n".join(desc_lines) if desc_lines else None,
  297. searchable=True,
  298. search_labels=model_search_labels,
  299. headings=sections or None,
  300. )
  301. if idx < 0:
  302. return None
  303. print()
  304. if idx < n:
  305. return _confirmed_selection(ordered[idx])
  306. if idx == n:
  307. return _custom_selection()
  308. return None
  309. except (ImportError, NotImplementedError, OSError, subprocess.SubprocessError):
  310. pass
  311. # Fallback: numbered list (ANSI colors for sale chrome / usage gauges)
  312. num_width = _print_numbered_rows(rows, menu_title, ordered, sections)
  313. if _unavailable:
  314. unavailable_footer = unavailable_message.strip() or (
  315. f"Unavailable models (requires paid tier — upgrade at {_upgrade_url})"
  316. )
  317. print()
  318. print(f" {_DIM}── {unavailable_footer} ──{_RESET}")
  319. for mid in _unavailable:
  320. print(f" {'':>{num_width}} {_DIM}{rows.label(mid)}{_RESET}")
  321. print()
  322. while True:
  323. try:
  324. choice = input(f"Choice [1-{n + 2}] (default: skip): ").strip()
  325. if not choice:
  326. return None
  327. idx = int(choice)
  328. if 1 <= idx <= n:
  329. return _confirmed_selection(ordered[idx - 1])
  330. if idx == n + 1:
  331. return _custom_selection()
  332. if idx == n + 2:
  333. return None
  334. print(f"Please enter 1-{n + 2}")
  335. except ValueError:
  336. print("Please enter a number")
  337. except (KeyboardInterrupt, EOFError):
  338. return None
  339. _model_choice_saves = 0 # bumped per saved pick; `hermes model` reads it to know a pick landed
  340. def model_choice_save_count() -> int:
  341. return _model_choice_saves
  342. def _save_model_choice(model_id: str) -> None:
  343. """Save the selected model to config.yaml only — NOT .env, which would stomp in multi-agent setups."""
  344. global _model_choice_saves
  345. from hermes_cli.config import save_config, load_config
  346. _model_choice_saves += 1
  347. config = load_config()
  348. # Always use dict format so provider/base_url can be stored alongside
  349. if isinstance(config.get("model"), dict):
  350. config["model"]["default"] = model_id
  351. else:
  352. config["model"] = {"default": model_id}
  353. save_config(config)
  354. from hermes_cli.observability.shared_metrics_setup import note_provider_setup_saved
  355. note_provider_setup_saved()
  356. # Public for plugins (catalog rule 9): the interactive model picker under a caller's title.
  357. prompt_model_selection = _prompt_model_selection