main_provider_setup.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. """Provider setup wizard helpers: provider picker, custom-provider save/remove, auxiliary-model
  2. routing menu, API-key/reasoning prompts, Anthropic OAuth.
  3. Split out of ``hermes_cli/main.py``. Names that still live in main are imported lazily at call time.
  4. """
  5. import contextlib
  6. from typing import Optional
  7. from hermes_cli.model_setup_flows_common import _ask, _ensure_dict_section, _print_numbered, _radiolist, _say
  8. def _is_profile_api_key_provider(provider_id: str) -> bool:
  9. """True when *provider_id* maps to a profile with ``auth_type='api_key'`` — the catch-all
  10. in select_provider_and_model() so plugin providers dispatch to the generic key flow."""
  11. try:
  12. from providers import get_provider_profile
  13. _p = get_provider_profile(provider_id)
  14. return _p is not None and _p.auth_type == "api_key"
  15. except Exception:
  16. return False
  17. _GENERIC_API_KEY_PROVIDERS = frozenset({
  18. "openai-api", "gemini", "deepseek", "xai", "zai", "kimi-coding-cn",
  19. "minimax", "minimax-cn", "kilocode", "opencode-zen", "opencode-go",
  20. "alibaba", "huggingface", "xiaomi", "arcee", "gmi",
  21. "nvidia", "ollama-cloud", "tencent-tokenhub", "tencent-tokenplan", "lmstudio"})
  22. def _short_url(url: str) -> str:
  23. """``https://host/path/`` -> ``host/path`` for menu labels."""
  24. return url.replace("https://", "").replace("http://", "").rstrip("/")
  25. def _clear_stale_openai_base_url():
  26. """Remove OPENAI_BASE_URL from ~/.hermes/.env unless the active provider is 'custom' — a
  27. leftover value routes provider:auto auxiliary clients to the old custom endpoint."""
  28. from hermes_cli.config import get_env_value, save_env_value, load_config
  29. model_cfg = load_config().get("model", {})
  30. provider = (model_cfg.get("provider") or "").strip().lower() if isinstance(model_cfg, dict) else ""
  31. if provider == "custom" or not provider:
  32. return # custom provider legitimately uses OPENAI_BASE_URL
  33. stale_url = get_env_value("OPENAI_BASE_URL")
  34. if stale_url:
  35. save_env_value("OPENAI_BASE_URL", "")
  36. shown = f"{stale_url[:40]}..." if len(stale_url) > 40 else stale_url
  37. print(f"Cleared stale OPENAI_BASE_URL from .env (was: {shown})")
  38. # (task_key, display_name, short_description)
  39. _AUX_TASKS: list[tuple[str, str, str]] = [
  40. ("vision", "Vision", "image/screenshot analysis"),
  41. ("compression", "Compression", "context summarization"),
  42. ("approval", "Approval", "smart command approval"),
  43. ("mcp", "MCP", "MCP tool reasoning"),
  44. ("title_generation", "Title generation", "session titles"),
  45. ("review", "Review", "/review reviewer subagent"),
  46. ("memory_query_rewrite", "Memory query rewrite", "memory retrieval queries"),
  47. ("tts_audio_tags", "TTS audio tags", "Gemini TTS tag insertion"),
  48. ("voice_chat", "Voice chat", "spoken voice-mode replies"),
  49. ("triage_specifier", "Triage specifier", "kanban spec fleshing"),
  50. ("kanban_decomposer", "Kanban decomposer", "task decomposition"),
  51. ("profile_describer", "Profile describer", "auto profile descriptions"),
  52. ("curator", "Curator", "skill-usage review pass"),
  53. ("group_router", "Group router", "who answers in auto-routed Group Chats")]
  54. # Special non-auxiliary task surfaced in the same picker: subagent delegation. Routing lives
  55. # under top-level `delegation.*` (NOT `auxiliary.delegation`) because delegate_task spawns full
  56. # child agents via tools/delegate_tool.py::_resolve_delegation_credentials(), which reads that
  57. # section directly. "auto" means "inherit the parent agent" and is stored as empty strings —
  58. # never persist the literal "auto", or it would be resolved as a provider name.
  59. _DELEGATION_TASK_KEY = "delegation"
  60. _DELEGATION_TASK_NAME = "Delegation"
  61. _DELEGATION_TASK_DESC = "subagent model (delegate_task)"
  62. def _all_aux_tasks() -> list[tuple[str, str, str]]:
  63. """Built-in aux tasks (in order) followed by plugin-registered ones
  64. (:meth:`hermes_cli.plugins.PluginContext.register_auxiliary_task`)."""
  65. tasks = list(_AUX_TASKS)
  66. # Plugin discovery failure must not break the aux config UI.
  67. with contextlib.suppress(Exception):
  68. from hermes_cli.plugins import get_plugin_auxiliary_tasks
  69. for entry in get_plugin_auxiliary_tasks():
  70. tasks.append((entry["key"], entry["display_name"], entry["description"]))
  71. return tasks
  72. def _format_aux_current(task_cfg: dict) -> str:
  73. """Render the current aux config for display in the task menu."""
  74. if not isinstance(task_cfg, dict):
  75. return "auto"
  76. base_url = str(task_cfg.get("base_url") or "").strip()
  77. provider = str(task_cfg.get("provider") or "auto").strip() or "auto"
  78. model = str(task_cfg.get("model") or "").strip()
  79. effort = _aux_effort_word(task_cfg)
  80. suffix = (f" · {model}" if model else "") + (f" · {effort}" if effort else "")
  81. if base_url:
  82. return f"custom ({_short_url(base_url)})" + suffix
  83. if provider == "auto":
  84. return "auto" + suffix
  85. if model:
  86. return f"{provider}{suffix}"
  87. return provider + suffix
  88. def _aux_effort_word(task_cfg: dict) -> str:
  89. """The stored ``reasoning_effort`` as a display word ("" = provider default; YAML False = "none")."""
  90. raw = task_cfg.get("reasoning_effort") if isinstance(task_cfg, dict) else None
  91. if raw is False:
  92. return "none"
  93. return str(raw or "").strip().lower()
  94. def _delegation_cfg_as_task(cfg: dict) -> dict:
  95. """Project the top-level ``delegation`` section into aux-task shape (provider/model/
  96. base_url/api_key); an empty provider means "inherit parent" and renders as "auto"."""
  97. d = cfg.get("delegation")
  98. if not isinstance(d, dict):
  99. d = {}
  100. out = {k: str(d.get(k) or "").strip() for k in ("provider", "model", "base_url", "api_key")}
  101. out["reasoning_effort"] = d.get("reasoning_effort", "")
  102. return out
  103. def _aux_task_cfg(cfg: dict, task: str) -> dict:
  104. """The stored routing dict for *task* (delegation reads its top-level section)."""
  105. if task == _DELEGATION_TASK_KEY:
  106. return _delegation_cfg_as_task(cfg)
  107. aux = cfg.get("auxiliary", {}) if isinstance(cfg.get("auxiliary"), dict) else {}
  108. return aux.get(task, {}) if isinstance(aux.get(task), dict) else {}
  109. def _aux_task_cfg_for_display(cfg: dict, task: str) -> dict:
  110. """The routing dict to *display* for *task*. A plugin task registered with ``inherit_from``
  111. shows its resolved route (base included), so the menu never reports "auto" while the task
  112. actually runs on the base's provider; every other task shows exactly what is stored."""
  113. with contextlib.suppress(Exception):
  114. from hermes_cli.plugins import get_plugin_auxiliary_tasks
  115. if any(e.get("key") == task and e.get("inherit_from") for e in get_plugin_auxiliary_tasks()):
  116. from agent.auxiliary_client import _get_auxiliary_task_config
  117. return _get_auxiliary_task_config(task)
  118. return _aux_task_cfg(cfg, task)
  119. def _aux_task_display_name(task: str) -> str:
  120. """Display name for a task key, covering the special delegation entry."""
  121. if task == _DELEGATION_TASK_KEY:
  122. return _DELEGATION_TASK_NAME
  123. return next((name for key, name, _ in _all_aux_tasks() if key == task), task)
  124. def _save_aux_choice(task: str, *, provider: str, model: str = "", base_url: str = "",
  125. api_key: str = "", reasoning_effort: Optional[str] = None) -> None:
  126. """Persist an aux task's four routing fields (timeout etc. untouched; main model config never
  127. modified). ``delegation`` writes the top-level section, with "auto" stored as an empty provider.
  128. ``reasoning_effort``: a level word or "" (provider default) to write; None leaves the key alone."""
  129. from hermes_cli.config import load_config, save_config
  130. cfg = load_config()
  131. if task == _DELEGATION_TASK_KEY:
  132. entry = _ensure_dict_section(cfg, "delegation")
  133. provider = "" if provider == "auto" else provider
  134. else:
  135. entry = _ensure_dict_section(_ensure_dict_section(cfg, "auxiliary"), task)
  136. entry["provider"] = provider
  137. entry["model"] = model or ""
  138. entry["base_url"] = base_url or ""
  139. entry["api_key"] = api_key or ""
  140. if reasoning_effort is not None and _aux_task_takes_reasoning(task):
  141. entry["reasoning_effort"] = reasoning_effort
  142. save_config(cfg)
  143. def _aux_task_takes_reasoning(task: str) -> bool:
  144. """Whether the task's config block honours ``reasoning_effort``. MoA slots and
  145. ``memory_query_rewrite`` omit the key by design (``config_defaults._aux``); ``review``
  146. routes through delegation which reads ``delegation.reasoning_effort``, not its own block."""
  147. if task == _DELEGATION_TASK_KEY:
  148. return True
  149. from hermes_cli.config_defaults import DEFAULT_CONFIG
  150. block = (DEFAULT_CONFIG.get("auxiliary") or {}).get(task)
  151. if isinstance(block, dict):
  152. return "reasoning_effort" in block
  153. return True # plugin-registered task: the runtime folds the key in via _get_task_extra_body
  154. def _prompt_aux_reasoning_effort(task: str, current: str) -> Optional[str]:
  155. """Effort step for an aux task: a level, "none", "" (provider default / inherit parent), or None to
  156. keep current. The empty-value row is "Inherit parent" for delegation (a child inherits the parent's
  157. effort; wording from #105431 by @fangliquanflq) and "Provider default" for aux tasks."""
  158. from hermes_constants import VALID_REASONING_EFFORTS
  159. label = "Inherit parent" if task == _DELEGATION_TASK_KEY else "Provider default"
  160. return _prompt_reasoning_effort_selection(
  161. list(VALID_REASONING_EFFORTS), current_effort=current, default_label=label)
  162. def _reset_aux_to_auto() -> int:
  163. """Reset every known aux task (built-in + plugin) back to auto/empty. Returns number reset."""
  164. from hermes_cli.config import load_config, save_config
  165. from hermes_cli.config_defaults import DEFAULT_CONFIG
  166. defaults = DEFAULT_CONFIG.get("auxiliary") or {}
  167. def _clear(entry: dict, auto: str, task: str = "") -> bool:
  168. # Only the routing fields; timeout/download_timeout (aux) and max_concurrent_children
  169. # etc. (delegation) are user-tuned and preserved. *auto* is the reset provider value
  170. # ("auto" for aux tasks, "" for delegation); anything else counts as a change.
  171. changed = False
  172. if entry.get("provider") not in {None, "", auto}:
  173. entry["provider"] = auto
  174. changed = True
  175. for field in ("model", "base_url", "api_key", "reasoning_effort"):
  176. # Reset = the shipped default: "" everywhere except a slot that ships one (voice_chat: none).
  177. default = str((defaults.get(task) or {}).get(field) or "") if isinstance(defaults.get(task), dict) else ""
  178. if (entry.get(field) or entry.get(field) is False or default) and entry.get(field) != default:
  179. entry[field] = default
  180. changed = True
  181. return changed
  182. cfg = load_config()
  183. aux = _ensure_dict_section(cfg, "auxiliary")
  184. count = sum(_clear(_ensure_dict_section(aux, task), "auto", task) for task, _name, _desc in _all_aux_tasks())
  185. dele = cfg.get("delegation")
  186. if isinstance(dele, dict):
  187. count += _clear(dele, "")
  188. save_config(cfg)
  189. return count
  190. def _aux_config_menu() -> None:
  191. """Top-level auxiliary-model picker; loops until the user picks "Back"."""
  192. from hermes_cli.config import load_config
  193. while True:
  194. cfg = load_config()
  195. _say("", " Auxiliary models — side-task routing", "",
  196. " Side tasks (vision, compression, title generation, etc.) default",
  197. ' to your main chat model. "auto" means "use my main model" —',
  198. " Hermes only falls back to a lightweight backend (OpenRouter,",
  199. " Nous Portal) if the main model is unavailable. Override a",
  200. " task below if you want it pinned to a specific provider/model.", "")
  201. menu_tasks = _all_aux_tasks() + [(_DELEGATION_TASK_KEY, _DELEGATION_TASK_NAME, _DELEGATION_TASK_DESC)]
  202. name_col = max(len(name) for _, name, _ in menu_tasks) + 2
  203. desc_col = max(len(desc) for _, _, desc in menu_tasks) + 4
  204. entries = [
  205. (task_key, f"{name.ljust(name_col)}{('(' + desc + ')').ljust(desc_col)}"
  206. f"{_format_aux_current(_aux_task_cfg_for_display(cfg, task_key))}")
  207. for task_key, name, desc in menu_tasks]
  208. entries.append(("__reset__", "Reset all to auto"))
  209. entries.append(("__back__", "Back"))
  210. idx = _prompt_provider_choice([label for _, label in entries], default=0)
  211. if idx is None:
  212. return
  213. key = entries[idx][0]
  214. if key == "__back__":
  215. return
  216. if key == "__reset__":
  217. n = _reset_aux_to_auto()
  218. _say(f"Reset {n} auxiliary task(s) to auto." if n else "All auxiliary tasks were already set to auto.",
  219. "")
  220. continue
  221. _aux_select_for_task(key)
  222. def _aux_select_for_task(task: str) -> None:
  223. """Pick a provider + model for one aux task and persist it. Rows come from
  224. ``build_aux_picker_rows()`` (shared substrate): only already-configured providers appear."""
  225. from hermes_cli.config import load_config
  226. from hermes_cli.inventory import build_aux_picker_rows, format_aux_picker_entries
  227. cfg = load_config()
  228. task_cfg = _aux_task_cfg(cfg, task)
  229. current_provider = str(task_cfg.get("provider") or "auto").strip() or "auto"
  230. current_model = str(task_cfg.get("model") or "").strip()
  231. current_base_url = str(task_cfg.get("base_url") or "").strip()
  232. display_name = _aux_task_display_name(task)
  233. try:
  234. providers = build_aux_picker_rows(current_provider=current_provider, current_model=current_model,
  235. current_base_url=current_base_url)
  236. except Exception as exc:
  237. print(f"Could not detect authenticated providers: {exc}")
  238. providers = []
  239. # (slug, label, models); "auto" always first
  240. auto_marker = " ← current" if current_provider == "auto" and not current_base_url else ""
  241. auto_label = "auto (inherit main agent)" if task == _DELEGATION_TASK_KEY else "auto (recommended)"
  242. entries: list[tuple[str, str, list[str]]] = [("__auto__", f"{auto_label}{auto_marker}", [])]
  243. entries.extend(format_aux_picker_entries(providers, current_provider=current_provider,
  244. current_base_url=current_base_url))
  245. custom_marker = " ← current" if current_base_url else ""
  246. entries.append(("__custom__", f"Custom endpoint (direct URL){custom_marker}", []))
  247. entries.append(("__back__", "Back", []))
  248. _say("", f" Configure {display_name} — current: {_format_aux_current(_aux_task_cfg_for_display(cfg, task))}", "")
  249. idx = _prompt_provider_choice([label for _, label, _ in entries], default=0)
  250. if idx is None:
  251. return
  252. slug, _label, models = entries[idx]
  253. if slug == "__back__":
  254. return
  255. if slug == "__auto__":
  256. _save_aux_choice(task, provider="auto", model="", base_url="", api_key="", reasoning_effort="")
  257. print(f"{display_name}: reset to auto.")
  258. elif slug == "__custom__":
  259. _aux_flow_custom_endpoint(task, task_cfg)
  260. else:
  261. _aux_flow_provider_model(task, slug, models, current_model, current_effort=_aux_effort_word(task_cfg))
  262. def _aux_flow_provider_model(task: str, provider_slug: str, curated_models: list,
  263. current_model: str = "", current_effort: str = "") -> None:
  264. """Prompt for a model under an already-authenticated provider (then its reasoning effort),
  265. save to aux."""
  266. from hermes_cli.auth import _prompt_model_selection
  267. from hermes_cli.models_pricing import get_pricing_for_provider
  268. display_name = _aux_task_display_name(task)
  269. try:
  270. pricing = get_pricing_for_provider(provider_slug) or {}
  271. except Exception:
  272. pricing = {}
  273. model_list = list(curated_models)
  274. # _prompt_model_selection supports "Enter custom model name" and cancel; with no curated
  275. # list (rare) fall back to a raw input prompt.
  276. if not model_list:
  277. _say(f"No curated model list for {provider_slug}.", "Enter a model slug manually (blank = use provider default):")
  278. selected = _ask("Model: ", cancel_msg="")
  279. if selected is None:
  280. return
  281. else:
  282. selected = _prompt_model_selection(model_list, current_model=current_model, pricing=pricing,
  283. confirm_provider=provider_slug)
  284. if selected is None:
  285. print("No change.")
  286. return
  287. effort = _prompt_aux_reasoning_effort(task, current_effort) if _aux_task_takes_reasoning(task) else None
  288. _save_aux_choice(task, provider=provider_slug, model=selected or "", base_url="", api_key="",
  289. reasoning_effort=effort)
  290. effort_note = f" · reasoning {effort}" if effort else ""
  291. if selected:
  292. print(f"{display_name}: {provider_slug} · {selected}{effort_note}")
  293. else:
  294. print(f"{display_name}: {provider_slug} (provider default model){effort_note}")
  295. def _aux_flow_custom_endpoint(task: str, task_cfg: dict) -> None:
  296. """Prompt for a direct OpenAI-compatible base_url + optional api_key/model."""
  297. display_name = _aux_task_display_name(task)
  298. current_base_url = str(task_cfg.get("base_url") or "").strip()
  299. current_model = str(task_cfg.get("model") or "").strip()
  300. _say("", f" Custom endpoint for {display_name}",
  301. " Provide an OpenAI-compatible base URL (e.g. http://localhost:11434/v1)", "")
  302. url = _ask(f"Base URL [{current_base_url}]: " if current_base_url else "Base URL: ", cancel_msg="")
  303. if url is None:
  304. return
  305. url = url or current_base_url
  306. if not url:
  307. print("No URL provided. No change.")
  308. return
  309. model = _ask(f"Model slug (optional) [{current_model}]: " if current_model else "Model slug (optional): ",
  310. cancel_msg="")
  311. if model is None:
  312. return
  313. model = model or current_model
  314. api_key = _ask("API key (optional, blank = use OPENAI_API_KEY): ", secret=True, cancel_msg="")
  315. if api_key is None:
  316. return
  317. effort = (_prompt_aux_reasoning_effort(task, _aux_effort_word(task_cfg))
  318. if _aux_task_takes_reasoning(task) else None)
  319. _save_aux_choice(task, provider="custom", model=model, base_url=url, api_key=api_key, reasoning_effort=effort)
  320. print(f"{display_name}: custom ({_short_url(url)})" + (f" · {model}" if model else "")
  321. + (f" · reasoning {effort}" if effort else ""))
  322. _CANCELLED = object()
  323. def _ask_index(prompt: str, count: int, *, echo_cancel: bool):
  324. """Numbered-menu input loop: 0-based index in ``range(count)``, ``None`` on blank,
  325. ``_CANCELLED`` on Ctrl-C/EOF (*echo_cancel* prints a blank line first)."""
  326. while True:
  327. try:
  328. val = input(prompt).strip()
  329. if not val:
  330. return None
  331. idx = int(val) - 1
  332. if 0 <= idx < count:
  333. return idx
  334. print(f"Please enter 1-{count}")
  335. except ValueError:
  336. print("Please enter a number")
  337. except (KeyboardInterrupt, EOFError):
  338. if echo_cancel:
  339. print()
  340. return _CANCELLED
  341. def _prompt_provider_choice(choices, *, default=0, title="Select provider:"):
  342. """Provider menu with curses arrow keys; numbered-list fallback when curses is unavailable
  343. (piped stdin, non-TTY). Returns the selected index, or None if the user cancels."""
  344. with contextlib.suppress(Exception):
  345. from hermes_cli.setup import _curses_prompt_choice
  346. idx = _curses_prompt_choice(title, choices, default)
  347. if idx >= 0:
  348. print()
  349. return idx
  350. _print_numbered(title, choices, default)
  351. print()
  352. idx = _ask_index(f"Choice [1-{len(choices)}] ({default + 1}): ", len(choices), echo_cancel=True)
  353. if idx is None:
  354. return default
  355. return None if idx is _CANCELLED else idx
  356. _DEFAULT_QWEN_PORTAL_MODELS = [
  357. "qwen3-coder-plus", "qwen3-coder"]
  358. # (mode value, label, description, accepted answers); "" = auto-detect
  359. _CUSTOM_API_MODES = (
  360. ("", "Auto-detect", "Use Hermes URL heuristics; best for standard OpenAI-compatible endpoints.",
  361. ("1", "auto", "detect", "auto-detect")),
  362. ("chat_completions", "Chat Completions", "Use /chat/completions for standard OpenAI-compatible servers.",
  363. ("2", "chat", "chat_completions", "completions")),
  364. ("codex_responses", "Responses / Codex", "Use /responses for Codex-compatible tool-calling backends.",
  365. ("3", "responses", "codex", "codex_responses")),
  366. ("anthropic_messages", "Anthropic Messages", "Use /v1/messages for Anthropic-compatible endpoints.",
  367. ("4", "anthropic", "anthropic_messages", "messages")))
  368. _CUSTOM_API_MODE_ANSWERS = {answer: value for value, _, _, answers in _CUSTOM_API_MODES for answer in answers}
  369. def _prompt_custom_api_mode_selection(base_url: str, current_api_mode: str = "") -> Optional[str]:
  370. """Prompt for a custom provider API mode: an explicit mode string, or None for auto-detect."""
  371. from hermes_cli.runtime_provider import _detect_api_mode_for_url
  372. detected_mode = _detect_api_mode_for_url(base_url)
  373. default_mode = str(current_api_mode or "").strip().lower() or detected_mode or ""
  374. _say("", "Select API compatibility mode:")
  375. for idx, (value, label, description, _answers) in enumerate(_CUSTOM_API_MODES, 1):
  376. markers = [m for m, hit in (("detected", value == detected_mode), ("current", value == default_mode)) if hit]
  377. suffix = f" [{' / '.join(markers)}]" if markers else ""
  378. _say(f" {idx}. {label}{suffix}", f" {description}")
  379. try:
  380. raw = input("Choice [1-4, Enter to keep current/detected]: ").strip().lower()
  381. except (KeyboardInterrupt, EOFError):
  382. print("\nCancelled.")
  383. raise
  384. if not raw:
  385. return default_mode or None
  386. if raw in _CUSTOM_API_MODE_ANSWERS:
  387. return _CUSTOM_API_MODE_ANSWERS[raw] or None
  388. print(f"Invalid API mode choice: {raw}. Falling back to auto-detect.")
  389. return None
  390. def _auto_provider_name(base_url: str) -> str:
  391. """Display name from a custom endpoint URL, e.g. "Local (localhost:11434)" or
  392. "RunPod (xyz.runpod.io)" — the default offered during custom endpoint setup."""
  393. import re
  394. name = re.sub(r"/v1/?$", "", _short_url(base_url)).split("/")[0]
  395. if "localhost" in name or "127.0.0.1" in name:
  396. return f"Local ({name})"
  397. if "runpod" in name.lower():
  398. return f"RunPod ({name})"
  399. return name.capitalize()
  400. def _custom_provider_api_key_config_value(provider_info, resolved_api_key=""):
  401. """Return the value that should be persisted for a custom provider key."""
  402. api_key_ref = str(provider_info.get("api_key_ref", "") or "").strip()
  403. if api_key_ref:
  404. return api_key_ref
  405. key_env = str(provider_info.get("key_env", "") or "").strip()
  406. if key_env and not str(provider_info.get("api_key", "") or "").strip():
  407. return f"${{{key_env}}}"
  408. return str(resolved_api_key or "").strip()
  409. def _custom_provider_base_url_config_value(provider_info, resolved_base_url=""):
  410. """Return the value that should be persisted for a custom provider URL."""
  411. return str(provider_info.get("base_url_ref", "") or "").strip() or str(resolved_base_url or "").strip()
  412. def _save_custom_provider(base_url, api_key="", model="", context_length=None, name=None, api_mode=None,
  413. key_env=""):
  414. """Save a custom endpoint to ``custom_providers`` in config.yaml, deduplicated by base_url (an
  415. existing entry gets model / context_length / api_mode updated). *key_env* set means the caller
  416. already wrote the key to ``.env``; the entry references it instead of inlining the secret.
  417. See #69449.
  418. """
  419. from hermes_cli.config import load_config, save_config
  420. cfg = load_config()
  421. providers = cfg.get("custom_providers") or []
  422. if not isinstance(providers, list):
  423. providers = []
  424. for entry in providers:
  425. if not (isinstance(entry, dict) and entry.get("base_url", "").rstrip("/") == base_url.rstrip("/")):
  426. continue
  427. changed = False
  428. if model and entry.get("model") != model:
  429. entry["model"] = model
  430. changed = True
  431. if model and context_length:
  432. _ensure_dict_section(entry, "models")[model] = {"context_length": context_length}
  433. changed = True
  434. if api_mode:
  435. if entry.get("api_mode") != api_mode:
  436. entry["api_mode"] = api_mode
  437. changed = True
  438. elif "api_mode" in entry:
  439. entry.pop("api_mode", None)
  440. changed = True
  441. if key_env and (entry.get("key_env") != key_env or entry.get("api_key")):
  442. entry["key_env"] = key_env
  443. entry.pop("api_key", None)
  444. changed = True
  445. if changed:
  446. cfg["custom_providers"] = providers
  447. save_config(cfg)
  448. return # already saved, updated if needed
  449. name = name or _auto_provider_name(base_url)
  450. entry = {"name": name, "base_url": base_url}
  451. if key_env:
  452. entry["key_env"] = key_env
  453. elif api_key:
  454. entry["api_key"] = api_key
  455. if model:
  456. entry["model"] = model
  457. if api_mode:
  458. entry["api_mode"] = api_mode
  459. if model and context_length:
  460. entry["models"] = {model: {"context_length": context_length}}
  461. providers.append(entry)
  462. cfg["custom_providers"] = providers
  463. save_config(cfg)
  464. print(f' 💾 Saved to custom providers as "{name}" (edit in config.yaml)')
  465. def _remove_custom_provider(config):
  466. """Let the user remove a saved custom provider from config.yaml."""
  467. from hermes_cli.config import load_config, save_config
  468. cfg = load_config()
  469. providers = cfg.get("custom_providers") or []
  470. if not isinstance(providers, list) or not providers:
  471. print("No custom providers configured.")
  472. return
  473. print("Remove a custom provider:\n")
  474. choices = [
  475. f"{entry.get('name', 'unnamed')} ({_short_url(entry.get('base_url', ''))})" if isinstance(entry, dict) else str(entry)
  476. for entry in providers]
  477. choices.append("Cancel")
  478. idx = _radiolist("Select provider to remove:", list(choices))
  479. if idx is not None:
  480. print()
  481. if idx < 0:
  482. idx = None
  483. else:
  484. for i, c in enumerate(choices, 1):
  485. print(f" {i}. {c}")
  486. print()
  487. try:
  488. val = input(f"Choice [1-{len(choices)}]: ").strip()
  489. idx = int(val) - 1 if val else None
  490. except (ValueError, KeyboardInterrupt, EOFError):
  491. idx = None
  492. if idx is None or idx >= len(providers):
  493. print("No change.")
  494. return
  495. removed = providers.pop(idx)
  496. cfg["custom_providers"] = providers
  497. save_config(cfg)
  498. removed_name = removed.get("name", "unnamed") if isinstance(removed, dict) else str(removed)
  499. print(f'✅ Removed "{removed_name}" from custom providers.')
  500. def _prompt_reasoning_effort_selection(efforts, current_effort="", *, default_label=""):
  501. """Prompt for a reasoning effort. Returns a level, 'none', "" (only with *default_label*: the
  502. "use the provider default" row), or None to keep current."""
  503. deduped = list(dict.fromkeys(str(effort).strip().lower() for effort in efforts if str(effort).strip()))
  504. canonical_order = ("minimal", "low", "medium", "high", "xhigh", "max", "ultra")
  505. ordered = [effort for effort in canonical_order if effort in deduped]
  506. ordered.extend(effort for effort in deduped if effort not in canonical_order)
  507. if not ordered:
  508. return None
  509. def _label(effort):
  510. return f"{effort} ← currently in use" if effort == current_effort else effort
  511. disable_label = "Disable reasoning"
  512. skip_label = "Skip (keep current)"
  513. # (return value, label) for the rows after the ladder; "" = provider default (aux tasks only).
  514. tail: list[tuple[Optional[str], str]] = [("none", disable_label)]
  515. if default_label:
  516. tail.append(("", default_label + (" ← currently in use" if current_effort == "" else "")))
  517. tail.append((None, skip_label))
  518. tail_values = [v for v, _ in tail]
  519. if current_effort == "none":
  520. default_idx = len(ordered) + tail_values.index("none")
  521. elif current_effort in ordered:
  522. default_idx = ordered.index(current_effort)
  523. elif default_label and current_effort == "":
  524. default_idx = len(ordered) + tail_values.index("")
  525. elif "medium" in ordered:
  526. default_idx = ordered.index("medium")
  527. else:
  528. default_idx = 0
  529. n = len(ordered)
  530. rows = [_label(effort) for effort in ordered] + [label for _, label in tail]
  531. idx = _radiolist("Select reasoning effort:", rows, default_idx)
  532. if idx is not None:
  533. if idx < 0:
  534. return None
  535. print()
  536. else:
  537. print("Select reasoning effort:")
  538. for i, row in enumerate(rows, 1):
  539. print(f" {i}. {row}")
  540. print()
  541. idx = _ask_index(f"Choice [1-{len(rows)}] (default: keep current): ", len(rows), echo_cancel=False)
  542. if idx is None or idx is _CANCELLED:
  543. return None
  544. if idx < n:
  545. return ordered[idx]
  546. return tail_values[idx - n]
  547. def _model_choice_save_count() -> int:
  548. """Snapshot taken by ``select_provider_and_model`` before a flow runs (see below)."""
  549. from hermes_cli.auth_model_picker import model_choice_save_count
  550. return model_choice_save_count()
  551. def _offer_reasoning_after_pick(model_before: str, saves_before: int) -> None:
  552. """Post-flow effort step for ``select_provider_and_model``, like the chat ``/model`` picker:
  553. every flow persists through ``_save_model_choice``, so whenever one saved a pick (or
  554. ``model.default`` changed) offer the effort for the saved model + provider, including a
  555. provider switch that keeps the same model ID or a re-pick of the current model. Cancel /
  556. "No change." never prompts."""
  557. from hermes_cli.config import load_config
  558. model_cfg = load_config().get("model")
  559. if not isinstance(model_cfg, dict):
  560. return
  561. model = str(model_cfg.get("default") or "").strip()
  562. if not model or (model == model_before and _model_choice_save_count() == saves_before):
  563. return
  564. _prompt_main_reasoning_effort(model, str(model_cfg.get("provider") or ""))
  565. def _prompt_main_reasoning_effort(model: str, provider: str) -> None:
  566. """The effort step every ``hermes model`` flow shares: after a main-model pick, offer the
  567. model's supported levels (a profile may declare a per-model set via ``setup_reasoning_efforts``;
  568. everything else gets the full ladder) and persist ``agent.reasoning_effort``. Skipped when the catalog says the route has
  569. no reasoning control; "Skip" leaves the current value alone."""
  570. from hermes_cli.config import load_config, save_config
  571. from hermes_cli.setup import _current_reasoning_effort, _set_reasoning_effort
  572. efforts = _main_model_reasoning_efforts(model, provider)
  573. if efforts is None:
  574. return
  575. selected = _prompt_reasoning_effort_selection(efforts, current_effort=_current_reasoning_effort(load_config()))
  576. if selected is None:
  577. return
  578. cfg = load_config()
  579. _set_reasoning_effort(cfg, selected)
  580. save_config(cfg)
  581. print("Reasoning disabled for this model." if selected == "none" else f"Reasoning effort set to: {selected}")
  582. def _main_model_reasoning_efforts(model: str, provider: str) -> Optional[list[str]]:
  583. """Levels to offer for *model* on *provider*: None when the route has no reasoning control."""
  584. from hermes_constants import VALID_REASONING_EFFORTS
  585. slug = (provider or "").strip().lower()
  586. from providers import get_provider_profile
  587. profile = get_provider_profile(slug) if slug else None
  588. declared = profile.setup_reasoning_efforts(model) if profile and profile.setup_reasoning_efforts else None
  589. if declared is not None:
  590. return list(declared) or None
  591. try:
  592. from agent.models_dev import get_model_capabilities
  593. meta = get_model_capabilities(slug, model)
  594. except Exception:
  595. meta = None
  596. if meta is not None and meta.supports_reasoning is False:
  597. return None
  598. return list(VALID_REASONING_EFFORTS)
  599. def _prompt_api_key(pconfig, existing_key: str, provider_id: str = "", existing_source: str = "") -> tuple:
  600. """API-key entry for ``hermes setup`` / ``hermes model``: first-time entry, or [K]eep / [R]eplace /
  601. [C]lear when a key exists (a malformed paste is recoverable without editing ``.env``).
  602. Returns ``(resolved_key, abort)``; ``abort=True`` means the caller must ``return`` at once."""
  603. from hermes_cli.auth import LMSTUDIO_NOAUTH_PLACEHOLDER
  604. from hermes_cli.config import save_env_value
  605. key_env = pconfig.api_key_env_vars[0] if pconfig.api_key_env_vars else ""
  606. def _prompt_new_key(*, allow_lmstudio_default: bool) -> str:
  607. lmstudio_default = provider_id == "lmstudio" and allow_lmstudio_default
  608. if lmstudio_default:
  609. prompt = f"{key_env} (Enter for no-auth default {LMSTUDIO_NOAUTH_PLACEHOLDER!r}): "
  610. else:
  611. prompt = f"{key_env} (or Enter to cancel): "
  612. entered = _ask(prompt, secret=True, cancel_msg="")
  613. if entered is None:
  614. return ""
  615. if not entered and lmstudio_default:
  616. return LMSTUDIO_NOAUTH_PLACEHOLDER
  617. return entered
  618. if not existing_key:
  619. print(f"No {pconfig.name} API key configured.")
  620. if not key_env:
  621. return "", True
  622. new_key = _prompt_new_key(allow_lmstudio_default=True)
  623. if not new_key:
  624. print("Cancelled.")
  625. return "", True
  626. save_env_value(key_env, new_key)
  627. _say("API key saved.", "")
  628. return new_key, False
  629. # Already configured — offer K / R / C
  630. from hermes_cli.env_loader import format_secret_source_suffix
  631. source_suffix = format_secret_source_suffix(key_env) if key_env else ""
  632. print(f" {pconfig.name} API key: {existing_key[:8]}... ✓{source_suffix}")
  633. if not key_env:
  634. # Nothing we can rewrite; just acknowledge and move on.
  635. print()
  636. return existing_key, False
  637. pool_backed = existing_source.startswith("credential_pool:")
  638. menu = " [K]eep / [R]eplace (default K): " if pool_backed else " [K]eep / [R]eplace / [C]lear (default K): "
  639. choice = _ask(menu, raw=True, cancel_msg="", on_cancel="k").lower()
  640. if choice.startswith("r"):
  641. new_key = _prompt_new_key(allow_lmstudio_default=False)
  642. if not new_key:
  643. _say(" No change.", "")
  644. return existing_key, False
  645. save_env_value(key_env, new_key)
  646. _say(" API key updated.", "")
  647. return new_key, False
  648. if choice.startswith("c") and not pool_backed:
  649. save_env_value(key_env, "")
  650. print(f" API key cleared. Re-run `hermes setup` to configure {pconfig.name} again.")
  651. return "", True
  652. # Keep (default, or any other input)
  653. print()
  654. return existing_key, False
  655. def _infer_stepfun_region(base_url: str) -> str:
  656. """Infer the current StepFun region from the configured endpoint."""
  657. return "china" if "api.stepfun.com" in (base_url or "").strip().lower() else "international"
  658. def _stepfun_base_url_for_region(region: str) -> str:
  659. from hermes_cli.auth import STEPFUN_STEP_PLAN_CN_BASE_URL, STEPFUN_STEP_PLAN_INTL_BASE_URL
  660. return STEPFUN_STEP_PLAN_CN_BASE_URL if region == "china" else STEPFUN_STEP_PLAN_INTL_BASE_URL
  661. def _run_anthropic_oauth_flow(save_env_value):
  662. """Run the Claude OAuth setup-token flow. Returns True if credentials were saved."""
  663. from agent.anthropic_credentials import run_oauth_setup_token, read_claude_code_credentials, is_claude_code_token_valid
  664. from hermes_cli.config import save_anthropic_oauth_token, use_anthropic_claude_code_credentials
  665. def _activate_claude_code_credentials_if_available() -> bool:
  666. try:
  667. creds = read_claude_code_credentials()
  668. except Exception:
  669. creds = None
  670. if creds and (is_claude_code_token_valid(creds) or bool(creds.get("refreshToken"))):
  671. use_anthropic_claude_code_credentials(save_fn=save_env_value)
  672. print(" ✓ Claude Code credentials linked.")
  673. from hermes_constants import display_hermes_home as _dhh_fn
  674. print(f" Hermes will use Claude's credential store directly instead of copying a setup-token into {_dhh_fn()}/.env.")
  675. return True
  676. return False
  677. def _paste_token(prompt: str):
  678. """Manual setup-token entry: True saved, False empty, None cancelled."""
  679. token = _ask(prompt, secret=True, cancel_msg="")
  680. if not token:
  681. return token
  682. save_anthropic_oauth_token(token, save_fn=save_env_value)
  683. print(" ✓ Setup-token saved.")
  684. return True
  685. try:
  686. _say("", " Running 'claude setup-token' — follow the prompts below.",
  687. " A browser window will open for you to authorize access.", "")
  688. token = run_oauth_setup_token()
  689. if token:
  690. if _activate_claude_code_credentials_if_available():
  691. return True
  692. save_anthropic_oauth_token(token, save_fn=save_env_value)
  693. print(" ✓ OAuth credentials saved.")
  694. return True
  695. # Subprocess completed but no token auto-detected — ask user to paste
  696. _say("", " If the setup-token was displayed above, paste it here:", "")
  697. saved = _paste_token(" Paste setup-token (or Enter to cancel): ")
  698. if saved is None:
  699. return False
  700. if saved:
  701. return True
  702. print(" ⚠ Could not detect saved credentials.")
  703. return False
  704. except FileNotFoundError:
  705. # Claude CLI not installed — guide user through manual setup
  706. _say("", " The 'claude' CLI is required for OAuth login.", "", " To install and authenticate:", "",
  707. " 1. Install Claude Code: npm install -g @anthropic-ai/claude-code",
  708. " 2. Run: claude setup-token",
  709. " 3. Follow the browser prompts to authorize",
  710. " 4. Re-run: hermes model", "",
  711. " Or paste an existing setup-token now (sk-ant-oat-...):", "")
  712. saved = _paste_token(" Setup-token (or Enter to cancel): ")
  713. if saved is None:
  714. return False
  715. if saved:
  716. return True
  717. print(" Cancelled — install Claude Code and try again.")
  718. return False
  719. def _named_custom_provider_map(cfg) -> dict[str, dict[str, str]]:
  720. """Saved custom providers keyed by slug, with raw ``${ENV}`` refs preserved."""
  721. from hermes_cli.config import get_compatible_custom_providers, read_raw_config
  722. from hermes_cli.providers import custom_provider_slug
  723. # Raw (un-expanded) templates keyed by identity. ``get_compatible_custom_providers(
  724. # read_raw_config())`` is deliberately bypassed: its normalize step ``urlparse()``s
  725. # ``base_url`` and drops entries whose base_url is itself an env-ref template.
  726. raw_api_key_refs: dict[tuple, str] = {}
  727. raw_base_url_refs: dict[tuple, str] = {}
  728. raw_cfg = read_raw_config()
  729. raw_entries: list[tuple[str, str, dict]] = []
  730. raw_list = raw_cfg.get("custom_providers")
  731. if isinstance(raw_list, list):
  732. raw_entries.extend((e.get("name", ""), "", e) for e in raw_list if isinstance(e, dict))
  733. raw_providers = raw_cfg.get("providers")
  734. if isinstance(raw_providers, dict):
  735. raw_entries.extend((e.get("name", "") or k, k, e) for k, e in raw_providers.items() if isinstance(e, dict))
  736. for name, provider_key, raw_entry in raw_entries:
  737. template = str(raw_entry.get("api_key", "") or "").strip()
  738. base_template = str(raw_entry.get("base_url", "") or raw_entry.get("url", "") or raw_entry.get("api", "") or "").strip()
  739. name = str(name or "").strip()
  740. provider_key = str(provider_key or "").strip()
  741. model = str(raw_entry.get("model", "") or raw_entry.get("default_model", "") or "").strip()
  742. # Index by every identity the loaded (expanded) config might present: (name),
  743. # (name, model), (provider_key), (provider_key, model); case-insensitive names.
  744. keys = [k.lower() for k in (name, provider_key) if k]
  745. identities = [(k,) for k in keys] + [(k, model) for k in keys]
  746. for refs, tmpl in ((raw_api_key_refs, template), (raw_base_url_refs, base_template)):
  747. if "${" in tmpl:
  748. for identity in identities:
  749. refs.setdefault(identity, tmpl)
  750. def _lookup_ref(refs: dict[tuple, str], name: str, provider_key: str, model: str) -> str:
  751. name_lc = str(name or "").strip().lower()
  752. pkey_lc = str(provider_key or "").strip().lower()
  753. model = str(model or "").strip()
  754. return next((refs[i] for i in ((pkey_lc, model), (pkey_lc,), (name_lc, model), (name_lc,)) if i[0] and i in refs), "")
  755. custom_provider_map = {}
  756. for entry in get_compatible_custom_providers(cfg):
  757. if not isinstance(entry, dict):
  758. continue
  759. name = (entry.get("name") or "").strip()
  760. base_url = (entry.get("base_url") or "").strip()
  761. if not name or not base_url:
  762. continue
  763. provider_key = (entry.get("provider_key") or "").strip()
  764. model = entry.get("model", "")
  765. custom_provider_map[custom_provider_slug(name, provider_key)] = {
  766. "name": name,
  767. "base_url": base_url,
  768. "api_key": entry.get("api_key", ""),
  769. "key_env": entry.get("key_env") or entry.get("api_key_env", ""),
  770. "key_cmd": entry.get("key_cmd", ""),
  771. "model": model,
  772. "models": entry.get("models", {}),
  773. "models_discovered": entry.get("models_discovered", False),
  774. "extra_headers": entry.get("extra_headers", {}),
  775. "discover_models": entry.get("discover_models", True),
  776. "api_mode": entry.get("api_mode", ""),
  777. "provider_key": provider_key,
  778. "api_key_ref": _lookup_ref(raw_api_key_refs, name, provider_key, model),
  779. "base_url_ref": _lookup_ref(raw_base_url_refs, name, provider_key, model)}
  780. return custom_provider_map
  781. def _build_provider_picker_rows(config: dict, active: str, provider_labels: dict[str, str],
  782. custom_provider_map: dict[str, dict[str, str]]) -> tuple[list[tuple[str, str, list[str]]], int]:
  783. """Rows for the ``hermes model`` provider picker plus the pre-selected index. Canonical providers
  784. fold into display groups (PROVIDER_GROUPS): a group row's ``members`` drive a sub-picker, leaf
  785. rows have ``members == []``; saved custom providers and trailing actions stay flat. Honors
  786. ``model_catalog.excluded_providers`` (slug or alias, case-insensitive) like the gateway/TUI."""
  787. from hermes_cli.models import CANONICAL_PROVIDERS, _PROVIDER_ALIASES
  788. from hermes_cli.models_catalog_static import group_providers, listed_canonical_providers, provider_group_for_slug
  789. canonical_descs = {p.slug: p.tui_desc for p in CANONICAL_PROVIDERS}
  790. listed = listed_canonical_providers()
  791. _cli_excluded = {
  792. str(p).strip().lower()
  793. for p in (config.get("model_catalog", {}) or {}).get("excluded_providers") or []
  794. if p}
  795. if _cli_excluded:
  796. # A canonical provider is hidden if its slug OR any alias is excluded.
  797. _names_for: dict[str, set[str]] = {_p.slug: {_p.slug.lower()} for _p in CANONICAL_PROVIDERS}
  798. for _alias, _canon in _PROVIDER_ALIASES.items():
  799. _names_for.setdefault(_canon, {_canon.lower()}).add(_alias.lower())
  800. _visible_slugs = [p.slug for p in listed if not _names_for.get(p.slug, {p.slug.lower()}) & _cli_excluded]
  801. else:
  802. _visible_slugs = [p.slug for p in listed]
  803. # The active provider's group when grouped, otherwise the active slug itself.
  804. active_group = provider_group_for_slug(active) if active else ""
  805. # (key, label, members): members == [] → leaf row (provider slug / action);
  806. # members != [] → group row, key is "group:<gid>"
  807. ordered: list[tuple[str, str, list[str]]] = []
  808. default_idx = 0
  809. def _add(key, label, members, is_active):
  810. nonlocal default_idx
  811. if is_active:
  812. label = f"{label} ← currently active"
  813. default_idx = len(ordered)
  814. ordered.append((key, label, members))
  815. for row in group_providers(_visible_slugs):
  816. if row["kind"] == "group":
  817. gid = row["group_id"]
  818. group_desc = row.get("description", "")
  819. label = f"{row['label']} ▸ ({group_desc})" if group_desc else f"{row['label']} ▸"
  820. _add(f"group:{gid}", label, row["members"], bool(active_group) and gid == active_group)
  821. else:
  822. slug = row["slug"]
  823. label = canonical_descs.get(slug, provider_labels.get(slug, slug))
  824. if slug == "nous":
  825. # Same free-tier rule as the gateway/TUI pickers: relabel for a guest, hide
  826. # when nous.guest is off, untouched for a real account.
  827. from hermes_cli.model_switch_providers import _free_tier_nous_row
  828. tier_row = _free_tier_nous_row({"name": label, "models": []})
  829. if tier_row is None:
  830. continue
  831. label = tier_row["name"]
  832. _add(slug, label, [], bool(active) and slug == active)
  833. for key, provider_info in custom_provider_map.items():
  834. saved_model = provider_info.get("model", "")
  835. model_hint = f" — {saved_model}" if saved_model else ""
  836. _add(key, f"{provider_info['name']} ({_short_url(provider_info['base_url'])}){model_hint}", [],
  837. bool(active) and key == active)
  838. from hermes_cli.model_setup_flows_local import local_models_available
  839. from hermes_cli.providers import LLAMACPP_ALIASES
  840. if not _cli_excluded & set(LLAMACPP_ALIASES) and local_models_available():
  841. _add("llamacpp", "Local models (run open models on this machine — no account or API key)", [],
  842. active == "llamacpp")
  843. ordered.append(("custom", "Custom endpoint (enter URL manually)", []))
  844. if isinstance(config.get("custom_providers"), list) and config.get("custom_providers"):
  845. ordered.append(("remove-custom", "Remove a saved custom provider", []))
  846. ordered.append(("reasoning", "Reasoning effort for the current model...", []))
  847. ordered.append(("aux-config", "Configure auxiliary models...", []))
  848. ordered.append(("cancel", "Leave unchanged", []))
  849. return ordered, default_idx