The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(hermes_cli): auth_model_picker — shared labels, dedupe custom-entry path, packed layout
Teknium323c0d88ebd6
diff --git a/hermes_cli/auth_model_picker.py b/hermes_cli/auth_model_picker.pyindex 669cb120cc3..2a9497b0462 100644--- a/hermes_cli/auth_model_picker.py+++ b/hermes_cli/auth_model_picker.py@@ -16,6 +16,10 @@ from hermes_cli.auth_constants import DEFAULT_NOUS_PORTAL_URL# Log-record parity with the origin module (caplog tests pin "hermes_cli.auth").logger = logging.getLogger("hermes_cli.auth")+_CUSTOM_LABEL = "Enter custom model name"+_SKIP_LABEL = "Skip (keep current)"+_CURRENT_SUFFIX = " ← currently in use"+def _confirm_selection_guards(model_id: str,@@ -25,23 +29,15 @@ def _confirm_selection_guards(api_key: str = "",include_kinds: Optional[List[str]] = None,) -> bool:- """Prompt before saving a model that trips any selection guard.+ """Prompt before saving a model that trips any selection guard (cost, data-policy, ...).- Runs the unified guard registry (cost, data-policy, future guards) and shows one [y/N] confirm- listing every warning that fired. Returns True to proceed, False to cancel.+ Shows one [y/N] confirm listing every warning that fired. Returns True to proceed."""try:- from hermes_cli.model_selection_guards import (- combined_message,- selection_warnings,- )+ from hermes_cli.model_selection_guards import combined_message, selection_warningswarnings = selection_warnings(- model_id,- provider=provider,- base_url=base_url,- api_key=api_key,- include_kinds=include_kinds,+ model_id, provider=provider, base_url=base_url, api_key=api_key, include_kinds=include_kinds,)except Exception:warnings = []@@ -81,11 +77,7 @@ class _ModelPickerRows:self.has_pricing = bool(pricing and any(pricing.get(m) for m in all_models))# Leave room for a leading "★ " on sale rows (Nous only).name_pad = 3 if sale_chrome else 2- self.name_col = (- max((len(m) for m in all_models), default=0) + name_pad- if self.has_pricing- else 0- )+ self.name_col = max((len(m) for m in all_models), default=0) + name_pad if self.has_pricing else 0# (inp, out, cache, pct|None, was_inp, was_out)self._price_cache: dict[str, tuple[str, str, str, int | None, str, str]] = {}self.price_col = 3 # minimum width@@ -94,10 +86,15 @@ class _ModelPickerRows:self.any_on_sale = Falseif not self.has_pricing:return++ def _was(raw: str) -> str:+ return _format_price_per_mtok(raw) if raw != "" else "?"+for mid in all_models:p = pricing.get(mid) # type: ignore[union-attr]pct: int | None = Nonewas_inp = was_out = ""+ inp, out, cache = "", "", ""if p:inp = _format_price_per_mtok(p.get("prompt", ""))out = _format_price_per_mtok(p.get("completion", ""))@@ -105,33 +102,14 @@ class _ModelPickerRows:cache = _format_price_per_mtok(cache_read) if cache_read else ""if cache:self.has_cache = True- if sale_chrome:- sale = compute_sale_discount(- p.get("prompt", ""),- p.get("completion", ""),- p.get("original"),- )- if sale is not None:- self.any_on_sale = True- pct, was_prompt_raw, was_out_raw = sale- # Natively-free models (no gateway original) carry- # empty was_* raws — leave them empty so the row- # shows bare "-100%" with no "was ?/?" suffix.- if was_prompt_raw == "" and was_out_raw == "":- was_inp = was_out = ""- else:- was_inp = (- _format_price_per_mtok(was_prompt_raw)- if was_prompt_raw != ""- else "?"- )- was_out = (- _format_price_per_mtok(was_out_raw)- if was_out_raw != ""- else "?"- )- else:- inp, out, cache = "", "", ""+ sale = compute_sale_discount(p.get("prompt", ""), p.get("completion", ""), p.get("original")) if sale_chrome else None+ if sale is not None:+ self.any_on_sale = True+ pct, was_prompt_raw, was_out_raw = sale+ # Natively-free models (no gateway original) carry empty was_* raws — leave+ # them empty so the row shows bare "-100%" with no "was ?/?" suffix.+ if was_prompt_raw != "" or was_out_raw != "":+ was_inp, was_out = _was(was_prompt_raw), _was(was_out_raw)self._price_cache[mid] = (inp, out, cache, pct, was_inp, was_out)self.price_col = max(self.price_col, len(inp), len(out))self.cache_col = max(self.cache_col, len(cache))@@ -140,37 +118,27 @@ class _ModelPickerRows:def segments(self, mid: str) -> list[tuple[str, str | None]]:"""Build a rich radiolist row: yellow ★/% , dim was, plain prices."""+ current = [(_CURRENT_SUFFIX, None)] if mid == self.current_model else []if not self.has_pricing:- segs: list[tuple[str, str | None]] = [(mid, None)]- if mid == self.current_model:- segs.append((" ← currently in use", None))- return segs+ return [(mid, None), *current]- inp, out, cache, pct, was_inp, was_out = self._price_cache.get(- mid, ("", "", "", None, "", "")- )+ inp, out, cache, pct, was_inp, was_out = self._price_cache.get(mid, ("", "", "", None, "", ""))on_sale = pct is not None# Reserve 2 columns for "★ " so sale and non-sale names share alignment.- star_w = 2if on_sale:- name_segs: list[tuple[str, str | None]] = [- ("★ ", "yellow"),- (f"{mid:<{self.name_col - star_w}}", None),- ]+ segs: list[tuple[str, str | None]] = [("★ ", "yellow"), (f"{mid:<{self.name_col - 2}}", None)]else:- name_segs = [(f"{mid:<{self.name_col}}", None)]+ segs = [(f"{mid:<{self.name_col}}", None)]price_part = f" {inp:>{self.price_col}} {out:>{self.price_col}}"if self.has_cache:price_part += f" {cache:>{self.cache_col}}"- segs = [*name_segs, (price_part, None)]+ segs.append((price_part, None))if on_sale:segs.append((f" -{pct}%", "yellow"))if was_inp or was_out:segs.append((f" was {was_inp}/{was_out}", "dim"))- if mid == self.current_model:- segs.append((" ← currently in use", None))- return segs+ return segs + currentdef label(self, mid: str) -> str:return "".join(text for text, _style in self.segments(mid))@@ -179,15 +147,13 @@ class _ModelPickerRows:"""``Select default model:`` plus an aligned pricing header hint when priced."""title = "Select default model:"if self.has_pricing:- # Align the header with the model column.- # Each choice is " {label}" (2 spaces) and we prepend- # a 3-char cursor region ("-> " or " "), so content starts at col 5.+ # Each choice is " {label}" (2 spaces) plus a 3-char cursor region ("-> " or " "),+ # so content starts at col 5.pad = " " * 5header = f"\n{pad}{'':>{self.name_col}} {'In':>{self.price_col}} {'Out':>{self.price_col}}"if self.has_cache:header += f" {'Cache':>{self.cache_col}}"- # Legend lives on the column-header line so it reads as a key- # (★ = on sale), not a fake menu row.+ # Legend lives on the column-header line so it reads as a key, not a fake menu row.title += header + " $/Mtok"if self.any_on_sale:title += " ★ = on sale"@@ -213,113 +179,91 @@ def _prompt_model_selection(from hermes_cli.cli_output import line_input_unavailable = unavailable_models or []- # Sale chrome (★ / -N% / was) is Nous Portal-only — never for OpenRouter- # or other providers even if pricing.original is somehow present.+ # Sale chrome (★ / -N% / was) is Nous Portal-only — never for OpenRouter or other providers+ # even if pricing.original is somehow present.sale_chrome = (confirm_provider or "").strip().lower() == "nous"def _confirmed_selection(mid: str) -> Optional[str]:if not mid:return None- # Unified guard registry (hermes_cli.model_selection_guards): the cost- # guard only runs when a provider is known (pricing lookups need one);- # id-keyed guards like the data-policy guard always run — they must- # fire even via a custom endpoint or gateway.- _kinds = None if confirm_provider else ["data_policy"]- if not _confirm_selection_guards(- mid,- provider=confirm_provider,- base_url=confirm_base_url,- api_key=confirm_api_key,- include_kinds=_kinds,- ):+ # The cost guard only runs when a provider is known (pricing lookups need one); id-keyed+ # guards like the data-policy guard always run — even via a custom endpoint or gateway.+ ok = _confirm_selection_guards(+ mid, provider=confirm_provider, base_url=confirm_base_url, api_key=confirm_api_key,+ include_kinds=None if confirm_provider else ["data_policy"],+ )+ return mid if ok else None++ def _custom_selection() -> Optional[str]:+ try:+ custom = line_input("Enter model name: ").strip()+ except (EOFError, KeyboardInterrupt):return None- return mid+ return _confirmed_selection(custom) if custom else None# Reorder: current model first, then the rest (deduplicated)- ordered = []- if current_model and current_model in model_ids:- ordered.append(current_model)- for mid in model_ids:- if mid not in ordered:- ordered.append(mid)+ ordered = list(dict.fromkeys(+ ([current_model] if current_model and current_model in model_ids else []) + list(model_ids)+ ))# All models for column-width computation (selectable + unavailable)- rows = _ModelPickerRows(- list(ordered) + list(_unavailable), pricing,- current_model=current_model, sale_chrome=sale_chrome,- )+ rows = _ModelPickerRows(ordered + list(_unavailable), pricing, current_model=current_model, sale_chrome=sale_chrome)_DIM = "\033[2m"_RESET = "\033[0m"- # Default cursor on the current model (index 0 if it was reordered to top)- default_idx = 0menu_title = rows.menu_title()_upgrade_url = (portal_url or DEFAULT_NOUS_PORTAL_URL).rstrip("/")+ n = len(ordered)# Try arrow-key menu first, fall back to number input.try:from hermes_cli.curses_ui import curses_radiolist- choices = [rows.segments(mid) for mid in ordered]- choices.append("Enter custom model name")- choices.append("Skip (keep current)")+ choices = [rows.segments(mid) for mid in ordered] + [_CUSTOM_LABEL, _SKIP_LABEL]unavailable_footer = unavailable_message.strip()if not unavailable_footer and _unavailable:unavailable_footer = f"Upgrade at {_upgrade_url} for paid models"- # The pricing column header (and any unavailable-models block) is shown- # as a multi-line description above the list so it survives the curses- # screen clear. menu_title already embeds the aligned price header.+ # The pricing column header (and any unavailable-models block) is shown as a multi-line+ # description above the list so it survives the curses screen clear. menu_title already+ # embeds the aligned price header; keep only the header/legend portion.desc_lines: list[str] = []if rows.has_pricing:- # menu_title is "Select default model:\n<pad><header> $/Mtok\n…"- # Keep only the header/legend portion for the description.header_part = menu_title.split("\n", 1)if len(header_part) > 1:desc_lines.extend(header_part[1].splitlines())if _unavailable:- for mid in _unavailable:- desc_lines.append(f" {rows.label(mid)}")+ desc_lines.extend(f" {rows.label(mid)}" for mid in _unavailable)desc_lines.append(f" ── {unavailable_footer} ──")- description = "\n".join(desc_lines) if desc_lines else None- # Search haystacks keep pricing labels visible while adding aliases- # for brand-less wire ids (e.g. Kimi Coding `k3` ↔ query "kimi").+ # Search haystacks keep pricing labels visible while adding aliases for brand-less wire+ # ids (e.g. Kimi Coding `k3` ↔ query "kimi"). model_search_text always starts with the+ # wire id; only append when aliases add tokens beyond the bare id already in the label.from hermes_cli.model_search import model_search_textmodel_search_labels = []for mid in ordered:- label = rows.label(mid)- haystack = model_search_text(mid)- # model_search_text always starts with the wire id; only append when- # aliases add tokens beyond the bare id already in the label.- model_search_labels.append(- label if haystack == mid else f"{label} {haystack}"- )- model_search_labels.append("Enter custom model name")- model_search_labels.append("Skip (keep current)")+ label, haystack = rows.label(mid), model_search_text(mid)+ model_search_labels.append(label if haystack == mid else f"{label} {haystack}")+ model_search_labels += [_CUSTOM_LABEL, _SKIP_LABEL]idx = curses_radiolist("Select default model:",choices,- selected=default_idx,+ selected=0, # cursor on the current model (index 0 if it was reordered to top)cancel_returns=-1,- description=description,+ description="\n".join(desc_lines) if desc_lines else None,searchable=True,search_labels=model_search_labels,)if idx < 0:return Noneprint()- if idx < len(ordered):+ if idx < n:return _confirmed_selection(ordered[idx])- elif idx == len(ordered):- try:- custom = line_input("Enter model name: ").strip()- except (EOFError, KeyboardInterrupt):- return None- return _confirmed_selection(custom) if custom else None+ if idx == n:+ return _custom_selection()return Noneexcept (ImportError, NotImplementedError, OSError, subprocess.SubprocessError):pass@@ -329,16 +273,12 @@ def _prompt_model_selection(from hermes_cli.colors import Colors, colorfor line in menu_title.splitlines():- if "★" in line:- print(line.replace("★", color("★", Colors.YELLOW), 1))- else:- print(line)- num_width = len(str(len(ordered) + 2))+ print(line.replace("★", color("★", Colors.YELLOW), 1) if "★" in line else line)+ num_width = len(str(n + 2))for i, mid in enumerate(ordered, 1):print(f" {i:>{num_width}}. {format_radio_item_ansi(rows.segments(mid))}")- n = len(ordered)- print(f" {n + 1:>{num_width}}. Enter custom model name")- print(f" {n + 2:>{num_width}}. Skip (keep current)")+ print(f" {n + 1:>{num_width}}. {_CUSTOM_LABEL}")+ print(f" {n + 2:>{num_width}}. {_SKIP_LABEL}")if _unavailable:unavailable_footer = unavailable_message.strip() or (@@ -358,10 +298,10 @@ def _prompt_model_selection(idx = int(choice)if 1 <= idx <= n:return _confirmed_selection(ordered[idx - 1])- elif idx == n + 1:+ if idx == n + 1:custom = line_input("Enter model name: ").strip()return _confirmed_selection(custom) if custom else None- elif idx == n + 2:+ if idx == n + 2:return Noneprint(f"Please enter 1-{n + 2}")except ValueError:@@ -371,11 +311,7 @@ def _prompt_model_selection(def _save_model_choice(model_id: str) -> None:- """Save the selected model to config.yaml (single source of truth).-- The model is stored in config.yaml only — NOT in .env. This avoids conflicts in multi-agent- setups where env vars would stomp each other.- """+ """Save the selected model to config.yaml only — NOT .env, which would stomp in multi-agent setups."""from hermes_cli.config import save_config, load_configconfig = load_config()