model_setup_flows.py 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. """Per-provider model-selection wizard flows for ``hermes setup`` / ``hermes model``.
  2. main / config / auth / models helpers are imported lazily inside bodies: avoids the main.py import
  3. cycle and lets tests patch ``hermes_cli.config.load_config`` etc. at call time. The shared skeleton
  4. lives in :mod:`hermes_cli.model_setup_flows_common`; the custom / Azure / Bedrock flows live in their
  5. own ``model_setup_flows_*`` modules.
  6. """
  7. from __future__ import annotations
  8. import contextlib
  9. import argparse
  10. import os
  11. from hermes_cli.config import clear_model_endpoint_credentials
  12. from hermes_cli.model_setup_flows_common import (
  13. _HTTP, _activate_provider_model, _ask, _commit_model_config, _curses_choice,
  14. _ensure_dict_section, _ensure_flow_api_key, _finish_model,
  15. _load_config_model_section, _models_dev_merged, _oauth_gate, _persist_model, _pick_model_or_prompt,
  16. _print_numbered, _prompt_auth_credentials_choice,
  17. _note_setup_failure, _run_login, _say, _show_curated)
  18. from hermes_cli.model_setup_flows_custom import _model_flow_custom, _model_flow_named_custom
  19. from hermes_cli.model_setup_flows_azure import _model_flow_azure_foundry
  20. from hermes_cli.model_setup_flows_bedrock import _model_flow_bedrock
  21. def _env_base_url(base_url_env: str) -> str:
  22. """Base-URL override from ``.env`` then the process environment ('' when unset)."""
  23. from hermes_cli.config import get_env_value
  24. if not base_url_env:
  25. return ""
  26. return get_env_value(base_url_env) or os.getenv(base_url_env, "")
  27. def _prompt_base_url_override(effective_base: str, base_url_env: str, *, persist_env: bool = True) -> str:
  28. """Optional ``Base URL [...]`` prompt; a valid override is saved to *base_url_env*."""
  29. from hermes_cli.config import save_env_value
  30. override = _ask(f"Base URL [{effective_base}]: ", cancel_msg="", on_cancel="")
  31. if override and base_url_env:
  32. if not override.startswith(_HTTP):
  33. print(" Invalid URL — must start with http:// or https://. Keeping current value.")
  34. else:
  35. if persist_env:
  36. save_env_value(base_url_env, override)
  37. return override
  38. return effective_base
  39. def _report_live_models(model_list, source: str) -> None:
  40. if model_list:
  41. print(f" Found {len(model_list)} model(s) from {source}")
  42. def _model_flow_openrouter(config, current_model=""):
  43. """OpenRouter provider: ensure API key, then pick model."""
  44. from hermes_constants import OPENROUTER_BASE_URL
  45. from hermes_cli.auth import ProviderConfig, _prompt_model_selection
  46. # OpenRouter isn't in PROVIDER_REGISTRY so we synthesize a minimal pconfig.
  47. pconfig = ProviderConfig(id="openrouter", name="OpenRouter", auth_type="api_key", api_key_env_vars=("OPENROUTER_API_KEY",))
  48. existing_key, _resolved, abort = _ensure_flow_api_key(
  49. "openrouter", pconfig, missing_hint=("Get one at: https://openrouter.ai/keys", ""))
  50. if abort:
  51. return
  52. from hermes_cli.models import model_ids
  53. from hermes_cli.models_pricing import get_pricing_for_provider
  54. openrouter_models = model_ids(force_refresh=True)
  55. # Live pricing is non-blocking — empty dict on failure.
  56. pricing = get_pricing_for_provider("openrouter", force_refresh=True)
  57. selected = _prompt_model_selection(
  58. openrouter_models, current_model=current_model, pricing=pricing, confirm_provider="openrouter",
  59. confirm_base_url=OPENROUTER_BASE_URL, confirm_api_key=_resolved or existing_key)
  60. _finish_model(selected, "openrouter", f"Default model set to: {selected} (via OpenRouter)",
  61. base_url=OPENROUTER_BASE_URL, api_mode="chat_completions")
  62. def _model_flow_ai_gateway(config, current_model=""):
  63. """Vercel AI Gateway provider: ensure API key, then pick model with pricing."""
  64. from hermes_constants import AI_GATEWAY_BASE_URL
  65. from hermes_cli.main_provider_setup import _prompt_api_key
  66. from hermes_cli.auth import PROVIDER_REGISTRY, _prompt_model_selection
  67. from hermes_cli.config import get_env_value
  68. pconfig = PROVIDER_REGISTRY["ai-gateway"]
  69. existing_key = get_env_value("AI_GATEWAY_API_KEY") or ""
  70. if not existing_key:
  71. _say("Create API key here: https://vercel.com/d?to=%2F%5Bteam%5D%2F%7E%2Fai-gateway&title=AI+Gateway",
  72. "Add a payment method to get $5 in free credits.", "")
  73. _resolved, abort = _prompt_api_key(pconfig, existing_key, provider_id="ai-gateway")
  74. if abort:
  75. return
  76. from hermes_cli.models import ai_gateway_model_ids
  77. from hermes_cli.models_pricing import get_pricing_for_provider
  78. models_list = ai_gateway_model_ids(force_refresh=True)
  79. pricing = get_pricing_for_provider("ai-gateway", force_refresh=True)
  80. selected = _prompt_model_selection(models_list, current_model=current_model, pricing=pricing)
  81. # Inline credentials are deliberately left untouched here (historical behavior).
  82. _finish_model(selected, "ai-gateway", f"Default model set to: {selected} (via Vercel AI Gateway)",
  83. base_url=AI_GATEWAY_BASE_URL, api_mode="chat_completions", clear_creds=False)
  84. def _model_flow_moa(config, current_model=""):
  85. """Mixture of Agents virtual provider: pick a preset (list always shown, even with one entry),
  86. persist it, print the breakdown. No credential step — presets reference configured providers."""
  87. from hermes_cli.auth import _save_model_choice
  88. from hermes_cli.moa_config import normalize_moa_config
  89. moa = normalize_moa_config(config.get("moa") if isinstance(config, dict) else {})
  90. presets = moa.get("presets") or {}
  91. if not presets:
  92. print("No MoA presets configured. Run `hermes moa configure <name>` first.")
  93. return
  94. names = list(presets.keys())
  95. default_name = moa.get("default_preset") or names[0]
  96. # Rows show the aggregator as the acting/billed model so the picker is informative before drilling in.
  97. rows = []
  98. for n in names:
  99. agg = presets[n].get("aggregator") or {}
  100. agg_label = f"{agg.get('provider')}:{agg.get('model')}" if agg else ""
  101. ref_count = len(presets[n].get("reference_models") or [])
  102. suffix = " ← default" if n == default_name else ""
  103. rows.append(f"{n} (acting: {agg_label}, {ref_count} refs){suffix}")
  104. default_idx = names.index(default_name) if default_name in names else 0
  105. title = "Select a Mixture of Agents preset:"
  106. idx = _curses_choice(title, rows, default_idx)
  107. if idx is None:
  108. _print_numbered(title, rows, default_idx)
  109. raw = _ask(f" Choice [1-{len(rows)}]: ", raw=True, cancel_msg="No change.")
  110. if raw is None:
  111. return
  112. try:
  113. idx = default_idx if not raw else max(0, min(len(rows) - 1, int(raw) - 1))
  114. except ValueError:
  115. print("No change.")
  116. return
  117. if idx < 0:
  118. print("No change.")
  119. return
  120. selected_name = names[idx]
  121. cfg, model = _load_config_model_section()
  122. model["default"] = selected_name
  123. model["provider"] = "moa"
  124. # Virtual local provider: drop stale endpoint credentials AND base_url (which
  125. # clear_model_endpoint_credentials intentionally leaves alone).
  126. clear_model_endpoint_credentials(model, clear_api_mode=True)
  127. model.pop("base_url", None)
  128. _commit_model_config(cfg)
  129. _save_model_choice(selected_name)
  130. preset = presets[selected_name]
  131. _say(
  132. "",
  133. f"Default model set to: {selected_name} (via Mixture of Agents)",
  134. f" Preset: {selected_name}",
  135. " Reference models (advise once per user turn):",
  136. )
  137. for i, slot in enumerate(preset.get("reference_models") or [], start=1):
  138. print(f" {i}. {slot.get('provider')}:{slot.get('model')}")
  139. agg = preset.get("aggregator") or {}
  140. print(
  141. f" Aggregator: {agg.get('provider')}:{agg.get('model')} (acting model — runs every step and carries almost all of the cost)"
  142. )
  143. def _nous_login_args(args) -> argparse.Namespace:
  144. return argparse.Namespace(
  145. portal_url=getattr(args, "portal_url", None), inference_url=getattr(args, "inference_url", None),
  146. client_id=getattr(args, "client_id", None), scope=getattr(args, "scope", None),
  147. no_browser=bool(getattr(args, "no_browser", False)), timeout=getattr(args, "timeout", None) or 15.0,
  148. ca_bundle=getattr(args, "ca_bundle", None), insecure=bool(getattr(args, "insecure", False)))
  149. def _nous_model_catalog(free_tier: bool, portal_url: str, model_ids: list, pricing: dict):
  150. """Free/paid-tier catalog for the Nous picker: ``(model_ids, pricing, unavailable_models,
  151. unavailable_message, policy_narrowed)`` or None (message already printed) when nothing is selectable."""
  152. from hermes_cli.models_pricing import nous_policy_allowed_ids, restrict_to_nous_policy
  153. from hermes_cli.models import (
  154. partition_nous_models_by_tier,
  155. union_with_nous_on_sale_models,
  156. union_with_portal_free_recommendations,
  157. union_with_portal_paid_recommendations,
  158. )
  159. # Free users: union with the Portal's freeRecommendedModels (newly launched free models appear
  160. # before the curated list catches up), then partition selectable/unavailable by Portal pricing.
  161. # Paid users: paidRecommendedModels plus every model on sale right now, no partition. Org policy
  162. # narrows BEFORE the tier split so a rescued id still has to pass the free/paid predicate.
  163. unavailable_models: list[str] = []
  164. unavailable_message = ""
  165. _policy_allowed = nous_policy_allowed_ids()
  166. if free_tier:
  167. try:
  168. from hermes_cli.nous_account import format_nous_portal_entitlement_message, get_nous_portal_account_info
  169. _account_info = get_nous_portal_account_info(force_fresh=True)
  170. unavailable_message = format_nous_portal_entitlement_message(_account_info, capability="paid Nous models") or ""
  171. except Exception:
  172. unavailable_message = ""
  173. model_ids, pricing = union_with_portal_free_recommendations(model_ids, pricing, portal_url)
  174. else:
  175. model_ids, pricing = union_with_portal_paid_recommendations(model_ids, pricing, portal_url)
  176. model_ids = union_with_nous_on_sale_models(model_ids, pricing)
  177. _before_policy = model_ids
  178. model_ids = restrict_to_nous_policy(model_ids, _policy_allowed, rescue_empty=True)
  179. _policy_narrowed = model_ids != _before_policy
  180. if free_tier:
  181. model_ids, unavailable_models = partition_nous_models_by_tier(model_ids, pricing, free_tier=True)
  182. if not model_ids and not unavailable_models:
  183. print("No models available for Nous Portal after filtering.")
  184. _note_setup_failure("no_models")
  185. return None
  186. if free_tier and not model_ids:
  187. print("No free models currently available.")
  188. if unavailable_models:
  189. from hermes_cli.auth import DEFAULT_NOUS_PORTAL_URL
  190. _url = (portal_url or DEFAULT_NOUS_PORTAL_URL).rstrip("/")
  191. print(unavailable_message or f"Upgrade at {_url} to access paid models.")
  192. return None
  193. return model_ids, pricing, unavailable_models, unavailable_message, _policy_narrowed
  194. def _nous_verified_credentials(creds_or_none=None):
  195. """Resolve Nous runtime credentials; on failure print the diagnosis (re-login when the
  196. session expired) and return None."""
  197. from hermes_cli.auth import (
  198. AuthError, PROVIDER_REGISTRY, _login_nous, format_auth_error, resolve_nous_runtime_credentials)
  199. try:
  200. return resolve_nous_runtime_credentials()
  201. except Exception as exc:
  202. relogin = isinstance(exc, AuthError) and exc.relogin_required
  203. msg = format_auth_error(exc) if isinstance(exc, AuthError) else str(exc)
  204. if relogin:
  205. _say(f"Session expired: {msg}", "Re-authenticating with Nous Portal...\n")
  206. try:
  207. _login_nous(_nous_login_args(None), PROVIDER_REGISTRY["nous"])
  208. except Exception as login_exc:
  209. print(f"Re-login failed: {login_exc}")
  210. _note_setup_failure("auth")
  211. return None
  212. print(f"Could not verify credentials: {msg}")
  213. _note_setup_failure("auth")
  214. return None
  215. def _nous_persist_selection(selected: str, creds: dict) -> dict:
  216. """Nous persist step: model choice + provider state, then rewrite ``model`` on a fresh
  217. config (the caller's may carry stale custom-provider fields) and clear a conflicting
  218. OPENAI_BASE_URL / OPENAI_API_KEY. Returns the saved config."""
  219. from hermes_cli.auth import _save_model_choice, _update_config_for_provider
  220. from hermes_cli.config import get_env_value, load_config, save_config, save_env_value
  221. _save_model_choice(selected)
  222. inference_url = creds.get("base_url", "")
  223. _update_config_for_provider("nous", inference_url)
  224. config = load_config()
  225. current_model_cfg = config.get("model")
  226. if isinstance(current_model_cfg, dict):
  227. model_cfg = dict(current_model_cfg)
  228. elif isinstance(current_model_cfg, str) and current_model_cfg.strip():
  229. model_cfg = {"default": current_model_cfg.strip()}
  230. else:
  231. model_cfg = {}
  232. model_cfg["provider"] = "nous"
  233. model_cfg["default"] = selected
  234. if inference_url and inference_url.strip():
  235. model_cfg["base_url"] = inference_url.rstrip("/")
  236. else:
  237. model_cfg.pop("base_url", None)
  238. clear_model_endpoint_credentials(model_cfg)
  239. config["model"] = model_cfg
  240. if get_env_value("OPENAI_BASE_URL"):
  241. save_env_value("OPENAI_BASE_URL", "")
  242. save_env_value("OPENAI_API_KEY", "")
  243. save_config(config)
  244. return config
  245. def _model_flow_nous(config, current_model="", args=None):
  246. """Nous Portal provider: ensure logged in, then pick model."""
  247. from hermes_cli.auth import get_provider_auth_state, _prompt_model_selection, _login_nous, PROVIDER_REGISTRY
  248. from hermes_cli.config import load_config
  249. from hermes_cli.nous_subscription import prompt_enable_tool_gateway
  250. state = get_provider_auth_state("nous")
  251. from hermes_cli import anon_auth
  252. # A free tier with no inference here signs in like no account; its apps move to the account after.
  253. if not state or not state.get("access_token") or anon_auth.guest_without_free_tier(state):
  254. _say("Not logged into Nous Portal. Starting login...", "")
  255. def _login_then_offer_gateway(login_args, pconfig):
  256. _login_nous(login_args, pconfig)
  257. # Offer Tool Gateway enablement for paid subscribers
  258. with contextlib.suppress(Exception):
  259. prompt_enable_tool_gateway(load_config() or {})
  260. # login_nous already handles model selection + config update
  261. _run_login(_login_then_offer_gateway, _nous_login_args(args), PROVIDER_REGISTRY["nous"])
  262. return
  263. # Already logged in — the curated list (agentic models users know from OpenRouter)
  264. # instead of the hundreds returned by the live /models endpoint.
  265. from hermes_cli.models import check_nous_free_tier, get_curated_nous_model_ids
  266. from hermes_cli.models_pricing import get_pricing_for_provider
  267. model_ids = get_curated_nous_model_ids()
  268. if not model_ids:
  269. print("No curated models available for Nous Portal.")
  270. return
  271. # Verify credentials are still valid (catches expired sessions early)
  272. creds = _nous_verified_credentials()
  273. if creds is None:
  274. return
  275. pricing = get_pricing_for_provider("nous")
  276. # Force fresh account data so recent credit purchases are reflected immediately.
  277. free_tier = check_nous_free_tier(force_fresh=True)
  278. if not free_tier:
  279. from hermes_cli.auth import resolve_nous_runtime_credentials
  280. try:
  281. creds = resolve_nous_runtime_credentials(force_refresh=True) or creds
  282. except Exception:
  283. # Runtime inference has its own paid-entitlement recovery; don't block.
  284. pass
  285. # Portal URL is needed for upgrade links and the recommendations endpoints.
  286. _nous_portal_url = ""
  287. with contextlib.suppress(Exception):
  288. _nous_portal_url = (get_provider_auth_state("nous") or {}).get("portal_base_url", "")
  289. catalog = _nous_model_catalog(free_tier, _nous_portal_url, model_ids, pricing)
  290. if catalog is None:
  291. return
  292. model_ids, pricing, unavailable_models, unavailable_message, _policy_narrowed = catalog
  293. from hermes_cli.nous_account import nous_policy_notice
  294. _policy_notice = nous_policy_notice(removed=_policy_narrowed)
  295. if _policy_notice:
  296. print(_policy_notice)
  297. from hermes_cli.model_picker_usage import featured_first, nous_picker_usage
  298. _usage = nous_picker_usage(warm=True)
  299. # Window plans: every model is usable; the plan's featured picks lead the list.
  300. if _usage is not None:
  301. model_ids = featured_first(model_ids, _usage.featured_model_ids)
  302. print(f'Showing {len(model_ids)} curated models — use "Enter custom model name" for others.')
  303. selected = _prompt_model_selection(
  304. model_ids, current_model=current_model, pricing=pricing, unavailable_models=unavailable_models,
  305. portal_url=_nous_portal_url, unavailable_message=unavailable_message, confirm_provider="nous",
  306. confirm_base_url=creds.get("base_url", ""), confirm_api_key=creds.get("api_key", ""),
  307. usage=_usage)
  308. if not selected:
  309. print("No change.")
  310. return
  311. config = _nous_persist_selection(selected, creds)
  312. print(f"Default model set to: {selected} (via Nous Portal)")
  313. if _usage is not None:
  314. from hermes_cli.model_picker_usage import large_model_notice
  315. if _notice := large_model_notice(selected, "nous"):
  316. print(f"⚠ {_notice}")
  317. # Offer Tool Gateway enablement for paid subscribers
  318. prompt_enable_tool_gateway(config)
  319. def _model_flow_openai_codex(config, current_model=""):
  320. """OpenAI Codex provider: ensure logged in, then pick model."""
  321. from hermes_cli.auth import (
  322. get_codex_auth_status, _prompt_model_selection, _login_openai_codex, PROVIDER_REGISTRY, DEFAULT_CODEX_BASE_URL,
  323. )
  324. from hermes_cli.codex_models import get_codex_model_ids
  325. if not _oauth_gate(
  326. bool(get_codex_auth_status().get("logged_in")), "OpenAI Codex", _login_openai_codex, argparse.Namespace(),
  327. PROVIDER_REGISTRY["openai-codex"], recheck=lambda: get_codex_auth_status().get("logged_in")):
  328. return
  329. # Prefer the credential pool (where `hermes auth` stores device_code tokens),
  330. # fall back to legacy provider state.
  331. # Token and route base travel together (#121486): a pooled gateway key must never be sent to
  332. # the chatgpt.com default by the catalog probe.
  333. _codex_token = _codex_base = None
  334. with contextlib.suppress(Exception):
  335. _codex_status = get_codex_auth_status()
  336. if _codex_status.get("logged_in"):
  337. _codex_token, _codex_base = _codex_status.get("api_key"), _codex_status.get("base_url")
  338. if not _codex_token:
  339. with contextlib.suppress(Exception):
  340. from hermes_cli.auth import resolve_codex_runtime_credentials
  341. _creds = resolve_codex_runtime_credentials()
  342. _codex_token, _codex_base = _creds.get("api_key"), _creds.get("base_url")
  343. codex_models = get_codex_model_ids(access_token=_codex_token, base_url=_codex_base)
  344. selected = _prompt_model_selection(
  345. codex_models, current_model=current_model, confirm_provider="openai-codex",
  346. confirm_base_url=_codex_base or DEFAULT_CODEX_BASE_URL, confirm_api_key=_codex_token or "")
  347. _activate_provider_model(selected, "openai-codex", DEFAULT_CODEX_BASE_URL,
  348. f"Default model set to: {selected} (via OpenAI Codex)")
  349. def _model_flow_xai_oauth(_config, current_model="", *, args=None):
  350. """xAI Grok OAuth (SuperGrok / Premium+) provider: ensure logged in, then pick model."""
  351. from hermes_cli.auth import (
  352. get_xai_oauth_auth_status, _prompt_model_selection, resolve_xai_oauth_runtime_credentials, _login_xai_oauth,
  353. DEFAULT_XAI_OAUTH_BASE_URL, PROVIDER_REGISTRY)
  354. from hermes_cli.models import provider_model_ids
  355. login_args = argparse.Namespace(no_browser=bool(getattr(args, "no_browser", False)), timeout=getattr(args, "timeout", None))
  356. if not _oauth_gate(
  357. bool(get_xai_oauth_auth_status().get("logged_in")), "xAI Grok OAuth (SuperGrok / Premium+)", _login_xai_oauth,
  358. login_args, PROVIDER_REGISTRY["xai-oauth"], fresh_name="xAI OAuth"):
  359. return
  360. # ``resolve_xai_oauth_runtime_credentials`` only reads the auth.json singleton, but
  361. # credentials may live only in the pool (``hermes auth add xai-oauth``) — fall back to
  362. # the default base URL so the picker still completes.
  363. base_url = DEFAULT_XAI_OAUTH_BASE_URL
  364. with contextlib.suppress(Exception):
  365. creds = resolve_xai_oauth_runtime_credentials()
  366. base_url = (creds.get("base_url") or "").strip().rstrip("/") or base_url
  367. models = provider_model_ids("xai-oauth")
  368. selected = _prompt_model_selection(models, current_model=current_model or (models[0] if models else "grok-4.6"))
  369. _activate_provider_model(selected, "xai-oauth", base_url,
  370. f"Default model set to: {selected} (via xAI Grok OAuth — SuperGrok / Premium+)")
  371. def _model_flow_qwen_oauth(_config, current_model=""):
  372. """Qwen OAuth provider: reuse local Qwen CLI login, then pick model."""
  373. from hermes_cli.main_provider_setup import _DEFAULT_QWEN_PORTAL_MODELS
  374. from hermes_cli.auth import (
  375. get_qwen_auth_status, resolve_qwen_runtime_credentials, _prompt_model_selection, DEFAULT_QWEN_BASE_URL)
  376. from hermes_cli.models import fetch_api_models
  377. status = get_qwen_auth_status()
  378. if not status.get("logged_in"):
  379. _say("Not logged into Qwen CLI OAuth.", "Run: qwen auth qwen-oauth",
  380. *([f"Expected credentials file: {status.get('auth_file')}"] if status.get("auth_file") else []),
  381. *([f"Error: {status.get('error')}"] if status.get("error") else []))
  382. return
  383. # Try live model discovery, fall back to curated list.
  384. models = None
  385. with contextlib.suppress(Exception):
  386. creds = resolve_qwen_runtime_credentials(refresh_if_expiring=True)
  387. models = fetch_api_models(creds["api_key"], creds["base_url"])
  388. if not models:
  389. models = list(_DEFAULT_QWEN_PORTAL_MODELS)
  390. default = current_model or (models[0] if models else "qwen3-coder-plus")
  391. selected = _prompt_model_selection(models, current_model=default, confirm_provider="qwen-oauth", confirm_base_url=DEFAULT_QWEN_BASE_URL)
  392. _activate_provider_model(selected, "qwen-oauth", DEFAULT_QWEN_BASE_URL, f"Default model set to: {selected} (via Qwen OAuth)")
  393. def _model_flow_minimax_oauth(config, current_model="", args=None):
  394. """MiniMax OAuth provider: ensure logged in, then pick model."""
  395. from hermes_cli.auth import (
  396. get_provider_auth_state, _prompt_model_selection, resolve_minimax_oauth_runtime_credentials, AuthError,
  397. format_auth_error, _login_minimax_oauth, PROVIDER_REGISTRY)
  398. state = get_provider_auth_state("minimax-oauth")
  399. if not state or not state.get("access_token"):
  400. _say("Not logged into MiniMax. Starting OAuth login...", "")
  401. mock_args = argparse.Namespace(
  402. region=getattr(args, "region", None) or "global", no_browser=bool(getattr(args, "no_browser", False)),
  403. timeout=getattr(args, "timeout", None) or 15.0)
  404. if not _run_login(_login_minimax_oauth, mock_args, PROVIDER_REGISTRY["minimax-oauth"]):
  405. return
  406. try:
  407. creds = resolve_minimax_oauth_runtime_credentials()
  408. except AuthError as exc:
  409. print(format_auth_error(exc))
  410. _note_setup_failure("auth")
  411. return
  412. from hermes_cli.models import _PROVIDER_MODELS
  413. model_ids = _PROVIDER_MODELS.get("minimax-oauth", [])
  414. selected = _prompt_model_selection(model_ids, current_model, confirm_provider="minimax-oauth", confirm_base_url=creds["base_url"])
  415. _activate_provider_model(selected, "minimax-oauth", creds["base_url"], f"\u2713 Using MiniMax model: {selected}", no_change=None)
  416. def _model_flow_kimi(config, current_model=""):
  417. """Kimi / Moonshot model selection; the endpoint is chosen by key prefix (no URL prompt):
  418. ``sk-kimi-*`` → api.kimi.com/coding/v1 (Kimi Coding Plan), other keys → Moonshot."""
  419. from hermes_cli.auth import PROVIDER_REGISTRY, KIMI_CODE_BASE_URL
  420. from hermes_cli.config import get_env_value, save_env_value
  421. from hermes_cli.models import _PROVIDER_MODELS
  422. provider_id = "kimi-coding"
  423. pconfig = PROVIDER_REGISTRY[provider_id]
  424. base_url_env = pconfig.base_url_env_var or ""
  425. _, existing_key, abort = _ensure_flow_api_key(provider_id, pconfig)
  426. if abort:
  427. return
  428. is_coding_plan = existing_key.startswith("sk-kimi-")
  429. if is_coding_plan:
  430. effective_base = KIMI_CODE_BASE_URL
  431. print(f" Detected Kimi Coding Plan key → {effective_base}")
  432. else:
  433. effective_base = pconfig.inference_base_url
  434. print(f" Using Moonshot endpoint → {effective_base}")
  435. # Clear any manual base URL override so auto-detection works at runtime
  436. if base_url_env and get_env_value(base_url_env):
  437. save_env_value(base_url_env, "")
  438. print()
  439. model_list = _PROVIDER_MODELS.get("kimi-coding" if is_coding_plan else "moonshot", [])
  440. selected = _pick_model_or_prompt(
  441. model_list, "Enter model name: ", current_model=current_model, confirm_provider=provider_id,
  442. confirm_base_url=effective_base, confirm_api_key=existing_key)
  443. # api_mode is dropped so the runtime auto-detects it from the URL.
  444. _finish_model(selected, provider_id, f"Default model set to: {selected} (via {'Kimi Coding' if is_coding_plan else 'Moonshot'})",
  445. base_url=effective_base, drop_api_mode=True)
  446. def _model_flow_stepfun(config, current_model=""):
  447. """StepFun Step Plan flow with region-specific endpoints."""
  448. from hermes_cli.main_provider_setup import _infer_stepfun_region, _prompt_provider_choice, _stepfun_base_url_for_region
  449. from hermes_cli.auth import PROVIDER_REGISTRY
  450. from hermes_cli.config import save_env_value
  451. from hermes_cli.models import _PROVIDER_MODELS, fetch_api_models, provider_model_ids
  452. provider_id = "stepfun"
  453. pconfig = PROVIDER_REGISTRY[provider_id]
  454. base_url_env = pconfig.base_url_env_var or ""
  455. _, existing_key, abort = _ensure_flow_api_key(provider_id, pconfig)
  456. if abort:
  457. return
  458. current_base = _env_base_url(base_url_env)
  459. if not current_base:
  460. model_cfg = config.get("model")
  461. if isinstance(model_cfg, dict):
  462. current_base = str(model_cfg.get("base_url") or "").strip()
  463. current_region = _infer_stepfun_region(current_base or pconfig.inference_base_url)
  464. regions = [(key, f"{name} ({_stepfun_base_url_for_region(key)})") for key, name in
  465. (("international", "International"), ("china", "China"))]
  466. # Active region first, marked; then the other; then Cancel.
  467. ordered_regions = ([(k, f"{label} ← currently active") for k, label in regions if k == current_region]
  468. + [(k, label) for k, label in regions if k != current_region] + [("cancel", "Cancel")])
  469. region_idx = _prompt_provider_choice([label for _, label in ordered_regions])
  470. if region_idx is None or ordered_regions[region_idx][0] == "cancel":
  471. print("No change.")
  472. return
  473. effective_base = _stepfun_base_url_for_region(ordered_regions[region_idx][0])
  474. if base_url_env:
  475. save_env_value(base_url_env, effective_base)
  476. # Same live+curated merge as the picker (``_stepfun_catalog``): Step Plan /models omits
  477. # Standard-API-only models, so the wizard must offer the same list /model shows (#41147).
  478. model_list = provider_model_ids(provider_id) or fetch_api_models(existing_key, effective_base)
  479. if model_list:
  480. print(f" Found {len(model_list)} model(s) from {pconfig.name} API")
  481. else:
  482. model_list = _PROVIDER_MODELS.get(provider_id, [])
  483. if model_list:
  484. print(f" Could not auto-detect models from {pconfig.name} API — showing Step Plan fallback catalog.")
  485. selected = _pick_model_or_prompt(
  486. model_list, "Model name: ", current_model=current_model, confirm_provider=provider_id,
  487. confirm_base_url=effective_base, confirm_api_key=existing_key)
  488. model = _finish_model(selected, provider_id, f"Default model set to: {selected} (via {pconfig.name})",
  489. base_url=effective_base, drop_api_mode=True)
  490. if model is not None:
  491. # Sync the caller's config dict so the setup wizard's final save_config(config) preserves our model
  492. # settings. Without this, the wizard overwrites model.provider/base_url with the stale values from
  493. # its own config dict (#4172).
  494. config["model"] = dict(model)
  495. def _model_flow_vertex(config, current_model=""):
  496. """Google Vertex AI (Gemini via the OpenAI-compatible endpoint). Auth is OAuth2 (service-account
  497. JSON or ADC): the credential *path* lives in .env (VERTEX_CREDENTIALS_PATH /
  498. GOOGLE_APPLICATION_CREDENTIALS); project ID and region are non-secret, saved under ``vertex:``."""
  499. from hermes_cli.auth import _prompt_model_selection
  500. from hermes_cli.config import load_config, get_env_value
  501. from hermes_cli.models import _PROVIDER_MODELS
  502. # 1. Credential source detection (fast, no network / no google-auth import).
  503. sa_path = (get_env_value("VERTEX_CREDENTIALS_PATH") or get_env_value("GOOGLE_APPLICATION_CREDENTIALS") or "").strip()
  504. if sa_path:
  505. print(f" Vertex credentials: service account JSON ({sa_path}) ✓")
  506. else:
  507. _say(" Vertex credentials: Application Default Credentials (ADC)",
  508. " Vertex uses OAuth2, not a static API key. Either:",
  509. " • run 'gcloud auth application-default login', or",
  510. " • set VERTEX_CREDENTIALS_PATH in ~/.hermes/.env to a service account JSON")
  511. print()
  512. vertex_cfg = load_config().get("vertex")
  513. if not isinstance(vertex_cfg, dict):
  514. vertex_cfg = {}
  515. # 2. Project ID (optional — falls back to the project embedded in creds).
  516. current_project = str(vertex_cfg.get("project_id") or "").strip()
  517. project_input = _ask(f" GCP project ID [{current_project or 'from credentials'}]: ", cancel_msg="")
  518. if project_input is None:
  519. return
  520. project_id = project_input or current_project
  521. # 3. Region (default global — required for the Gemini 3.x previews).
  522. current_region = str(vertex_cfg.get("region") or "global").strip() or "global"
  523. region_input = _ask(f" Vertex region [{current_region}]: ", cancel_msg="")
  524. if region_input is None:
  525. return
  526. region = region_input or current_region
  527. # 4. Model selection (curated list — Vertex has no /models listing route).
  528. model_list = _PROVIDER_MODELS.get("vertex", []) or ["google/gemini-3-pro-preview", "google/gemini-3-flash-preview"]
  529. host = "aiplatform.googleapis.com" if region == "global" else f"{region}-aiplatform.googleapis.com"
  530. base_url_preview = f"https://{host}/v1beta1/projects/<project>/locations/{region}/endpoints/openapi"
  531. selected = _prompt_model_selection(model_list, current_model=current_model, confirm_provider="vertex", confirm_base_url=base_url_preview)
  532. def _finish(cfg, _model):
  533. vcfg = _ensure_dict_section(cfg, "vertex")
  534. vcfg["project_id"] = project_id
  535. vcfg["region"] = region
  536. # base_url is computed at runtime from project+region; do not pin it.
  537. # api_mode is dropped: chat_completions is the profile default.
  538. _finish_model(selected, "vertex", f" Default model set to: {selected} (via Google Vertex AI, {region})", no_change=" No change.",
  539. drop_base_url=True, drop_api_mode=True, finish=_finish)
  540. def _select_zai_endpoint(current_base: str) -> str:
  541. """Picker for the official Z.AI endpoints (``ZAI_ENDPOINTS`` in ``hermes_cli.auth``, kept in
  542. sync with the probe list) plus a custom-proxy option. Returns the selected base URL;
  543. *current_base* on cancel/error."""
  544. from hermes_cli.main_provider_setup import _prompt_provider_choice
  545. from hermes_cli.auth import ZAI_ENDPOINTS
  546. options = [(label, url) for _, url, _, label in ZAI_ENDPOINTS]
  547. normalized_current = (current_base or "").strip().rstrip("/")
  548. # Default to the active endpoint when known; a custom URL defaults to "Custom proxy".
  549. default_idx = next((idx for idx, (_, url) in enumerate(options) if normalized_current == url.rstrip("/")),
  550. len(options) if normalized_current else 0)
  551. choices = [f"{label} ({url})" for label, url in options] + ["Custom proxy URL"]
  552. selected = _prompt_provider_choice(choices, default=default_idx, title="Select Z.AI / GLM endpoint:")
  553. if selected is None:
  554. return current_base
  555. if selected != len(options):
  556. return options[selected][1].rstrip("/")
  557. override = _ask(f"Custom base URL [{current_base}]: ", cancel_msg="")
  558. if not override:
  559. return current_base
  560. if not override.startswith(_HTTP):
  561. print(" Invalid URL — must start with http:// or https://. Keeping current value.")
  562. return current_base
  563. return override.rstrip("/")
  564. _GEMINI_FREE_TIER_NOTICE = (
  565. "", "❌ This Google API key is on the free tier (<= 250 requests/day for gemini-2.5-flash).",
  566. " Hermes typically makes 3-10 API calls per user turn (tool iterations + auxiliary tasks),",
  567. " so the free tier is exhausted after a handful of messages and cannot sustain",
  568. " an agent session.", "",
  569. " To use Gemini with Hermes, enable billing on your Google Cloud project and regenerate",
  570. " the key in a billing-enabled project: https://aistudio.google.com/apikey", "",
  571. " Alternatives with workable free usage: DeepSeek, OpenRouter (free models), Groq, Nous.", "",
  572. "Not saving Gemini as the default provider.")
  573. def _gemini_tier_ok(existing_key: str, pconfig, base_url_env: str) -> bool:
  574. """Gemini free-tier gate: free-tier daily quotas (<= 250 RPD for Flash) are exhausted in a
  575. handful of agent turns, so refuse a free-tier key. The probe is best-effort; network or
  576. auth errors fall through without blocking."""
  577. try:
  578. from agent.gemini_native_adapter import probe_gemini_tier
  579. except Exception:
  580. return True
  581. print(" Checking Gemini API tier...")
  582. tier = probe_gemini_tier(existing_key, _env_base_url(base_url_env) or pconfig.inference_base_url)
  583. if tier == "free":
  584. _say(*_GEMINI_FREE_TIER_NOTICE)
  585. return False
  586. # "unknown" (network/auth/unexpected response): don't block; the runtime 429 handler
  587. # surfaces free-tier guidance if needed.
  588. _say(" Tier check: paid ✓" if tier == "paid" else " Tier check: could not verify (proceeding anyway).", "")
  589. return True
  590. def _lmstudio_models(pconfig, curated, api_key, base_url):
  591. """LM Studio: live /api/v1/models probe only."""
  592. from hermes_cli.auth import AuthError
  593. from hermes_cli.models_local import fetch_lmstudio_models
  594. try:
  595. model_list = fetch_lmstudio_models(api_key=api_key, base_url=base_url)
  596. except AuthError as exc:
  597. _say(f" LM Studio rejected the request: {exc}", " Set LM_API_KEY (or update it) to match the server's bearer token.")
  598. model_list = []
  599. _report_live_models(model_list, "LM Studio")
  600. return model_list
  601. def _ollama_cloud_models(pconfig, curated, api_key, base_url):
  602. """Ollama Cloud: forced live refresh so newly released models appear the moment the user
  603. enters their key, not when the disk cache TTL expires."""
  604. from hermes_cli.models import fetch_ollama_cloud_models
  605. model_list = fetch_ollama_cloud_models(api_key=api_key, base_url=base_url, force_refresh=True)
  606. _report_live_models(model_list, "Ollama Cloud")
  607. return model_list
  608. def _novita_models(pconfig, curated, api_key, base_url):
  609. """Novita: live first, then models.dev, then curated."""
  610. from hermes_cli.models import fetch_api_models
  611. live_models = fetch_api_models(api_key, base_url)
  612. if live_models:
  613. _report_live_models(live_models, f"{pconfig.name} API")
  614. return live_models
  615. model_list = _models_dev_merged("novita", curated)
  616. if model_list:
  617. _report_live_models(model_list, "models.dev registry")
  618. return model_list
  619. _show_curated(curated)
  620. return curated
  621. # provider id -> (pconfig, curated, api_key_for_probe, effective_base) -> model list
  622. _SPECIAL_MODEL_LISTS = {
  623. "lmstudio": _lmstudio_models,
  624. "ollama-cloud": _ollama_cloud_models,
  625. "novita": _novita_models}
  626. def _api_key_provider_model_list(provider_id: str, pconfig, existing_key: str, key_env: str, effective_base: str) -> list:
  627. """Model list for an API-key provider: models.dev registry (cached, agentic/tool-capable filter)
  628. → curated static list (offline insurance) → provider-owned catalog (``ProviderProfile.fetch_models``
  629. merged with ``fallback_models`` exactly like the ``/model`` picker's
  630. ``models._profile_live_catalog``; generic /models probe for unregistered providers).
  631. Providers in ``_SPECIAL_MODEL_LISTS`` have their own resolution."""
  632. from hermes_cli.config import get_env_value
  633. from hermes_cli.models import fetch_api_models, probe_profile_catalog, static_catalog
  634. curated = static_catalog(provider_id)
  635. api_key_for_probe = existing_key or (get_env_value(key_env) if key_env else "")
  636. special = _SPECIAL_MODEL_LISTS.get(provider_id)
  637. if special is not None:
  638. return special(pconfig, curated, api_key_for_probe, effective_base)
  639. # models.dev first (tool-capable, noise-filtered), merged with curated so newly added
  640. # models still appear.
  641. model_list = _models_dev_merged(provider_id, curated)
  642. if model_list:
  643. _report_live_models(model_list, "models.dev registry")
  644. return model_list
  645. if curated and len(curated) >= 8:
  646. # Substantial curated list — use it directly, skip live probe
  647. _show_curated(curated)
  648. return curated
  649. from providers import get_provider_profile
  650. profile = get_provider_profile(provider_id)
  651. if profile is not None:
  652. # The profile owns endpoint (models_url), headers and response shape. Same probe as the
  653. # ``/model`` picker; when neither live nor fallback_models yields rows, the curated
  654. # ``_PROVIDER_MODELS`` row still applies (built-in providers with a short curated list).
  655. model_list = probe_profile_catalog(provider_id, profile, api_key_for_probe, effective_base)
  656. if model_list:
  657. _report_live_models(model_list, f"{pconfig.name} catalog")
  658. return model_list
  659. _show_curated(curated)
  660. return list(curated)
  661. live_models = fetch_api_models(api_key_for_probe, effective_base)
  662. if live_models and len(live_models) >= len(curated):
  663. _report_live_models(live_models, f"{pconfig.name} API")
  664. return live_models
  665. _show_curated(curated) # may be empty: falls through to raw input
  666. return curated
  667. def _model_flow_api_key_provider(config, provider_id, current_model=""):
  668. """Generic flow for API-key providers (z.ai, MiniMax, OpenCode, etc.)."""
  669. from hermes_cli.auth import PROVIDER_REGISTRY
  670. from hermes_cli.config import save_env_value, load_config
  671. from hermes_cli.models import opencode_model_api_mode, normalize_opencode_model_id
  672. pconfig = PROVIDER_REGISTRY[provider_id]
  673. key_env = pconfig.api_key_env_vars[0] if pconfig.api_key_env_vars else ""
  674. base_url_env = pconfig.base_url_env_var or ""
  675. is_opencode = provider_id in {"opencode-zen", "opencode-go"}
  676. _, existing_key, abort = _ensure_flow_api_key(provider_id, pconfig)
  677. if abort:
  678. return
  679. if provider_id == "gemini" and existing_key and not _gemini_tier_ok(existing_key, pconfig, base_url_env):
  680. return
  681. # Optional base URL override. Precedence: env var → config.yaml model.base_url → registry
  682. # default; reading config.yaml keeps a saved remote URL from being overwritten with
  683. # localhost when the user just presses Enter.
  684. current_base = _env_base_url(base_url_env)
  685. if not current_base:
  686. with contextlib.suppress(Exception):
  687. _m = load_config().get("model") or {}
  688. if str(_m.get("provider") or "").strip().lower() == provider_id:
  689. current_base = str(_m.get("base_url") or "").strip()
  690. effective_base = current_base or pconfig.inference_base_url
  691. if provider_id == "actual":
  692. from hermes_cli.providers import normalize_provider
  693. model_cfg = config.get("model") or {}
  694. if isinstance(model_cfg, dict) and normalize_provider(str(model_cfg.get("provider") or "")) == provider_id:
  695. effective_base = str(model_cfg.get("base_url") or "").strip() or effective_base
  696. if provider_id == "zai":
  697. # Four official endpoints with separate billing paths — a picker lets users match
  698. # the endpoint to their key type.
  699. chosen_base = _select_zai_endpoint(effective_base)
  700. if chosen_base and chosen_base != effective_base and base_url_env:
  701. save_env_value(base_url_env, chosen_base)
  702. effective_base = chosen_base
  703. else:
  704. effective_base = _prompt_base_url_override(effective_base, base_url_env, persist_env=provider_id != "actual")
  705. model_list = _api_key_provider_model_list(provider_id, pconfig, existing_key, key_env, effective_base)
  706. if is_opencode:
  707. model_list = [normalize_opencode_model_id(provider_id, mid) for mid in model_list]
  708. current_model = normalize_opencode_model_id(provider_id, current_model)
  709. model_list = list(dict.fromkeys(mid for mid in model_list if mid))
  710. # Per-model pricing when the provider supports it; get_pricing_for_provider() is memoized
  711. # and returns {} otherwise — never a blocking fetch beyond the catalog lookup above.
  712. pricing: dict = {}
  713. if model_list:
  714. try:
  715. from hermes_cli.models_pricing import get_pricing_for_provider
  716. pricing = get_pricing_for_provider(provider_id) or {}
  717. except Exception:
  718. pricing = {}
  719. selected = _pick_model_or_prompt(
  720. model_list, "Model name: ", current_model=current_model, pricing=pricing, confirm_provider=provider_id,
  721. confirm_base_url=effective_base, confirm_api_key=existing_key)
  722. if selected and is_opencode:
  723. selected = normalize_opencode_model_id(provider_id, selected)
  724. # OpenCode pins its api_mode; everyone else drops it so the runtime auto-detects.
  725. _finish_model(
  726. selected, provider_id, f"Default model set to: {selected} (via {pconfig.name})", base_url=effective_base,
  727. api_mode=opencode_model_api_mode(provider_id, selected) if selected and is_opencode else None,
  728. drop_api_mode=not is_opencode)
  729. def _anthropic_authenticate() -> bool:
  730. """Interactive Anthropic auth (OAuth subscription or API key). False = flow must stop."""
  731. from hermes_cli.main_provider_setup import _run_anthropic_oauth_flow
  732. from hermes_cli.config import save_env_value, save_anthropic_api_key
  733. _say("", " Choose authentication method:", "", " 1. Claude Pro/Max subscription (OAuth login)",
  734. " 2. Anthropic API key (pay-per-token)", " 3. Cancel", "")
  735. choice = _ask(" Choice [1/2/3]: ", raw=True, cancel_msg="")
  736. if choice is None:
  737. return False
  738. if choice == "1":
  739. return _run_anthropic_oauth_flow(save_env_value)
  740. if choice == "2":
  741. _say("", " Get an API key at: https://platform.claude.com/settings/keys", "")
  742. api_key = _ask(" API key (sk-ant-...): ", secret=True, cancel_msg="")
  743. if api_key is None:
  744. return False
  745. if not api_key:
  746. print(" Cancelled.")
  747. return False
  748. save_anthropic_api_key(api_key, save_fn=save_env_value)
  749. print(" ✓ API key saved.")
  750. return True
  751. print(" No change.")
  752. return False
  753. def _model_flow_anthropic(config, current_model=""):
  754. """Flow for Anthropic provider — OAuth subscription, API key, or Claude Code creds."""
  755. from hermes_cli.auth import get_anthropic_key
  756. from hermes_cli.models import _PROVIDER_MODELS
  757. # Check ALL credential sources
  758. existing_key = get_anthropic_key()
  759. cc_available = False
  760. with contextlib.suppress(Exception):
  761. from agent.anthropic_credentials import read_claude_code_credentials, is_claude_code_token_valid, _is_oauth_token
  762. cc_creds = read_claude_code_credentials()
  763. if cc_creds and is_claude_code_token_valid(cc_creds):
  764. cc_available = True
  765. # Stale-OAuth guard: an expired OAuth token with no valid cc_creds fallback is treated
  766. # as missing so the re-auth path is offered.
  767. existing_is_stale_oauth = bool(existing_key and _is_oauth_token(existing_key) and not cc_available)
  768. has_creds = (bool(existing_key) and not existing_is_stale_oauth) or cc_available
  769. needs_auth = not has_creds
  770. if has_creds:
  771. if existing_key:
  772. from hermes_cli.env_loader import format_secret_source_suffix
  773. from hermes_cli.auth import PROVIDER_REGISTRY
  774. # Surface which env var supplied the key so Bitwarden users see "(from Bitwarden)".
  775. source_suffix = ""
  776. for var in PROVIDER_REGISTRY["anthropic"].api_key_env_vars:
  777. if os.getenv(var, "").strip() == existing_key:
  778. source_suffix = format_secret_source_suffix(var)
  779. if source_suffix:
  780. break
  781. print(f" Anthropic credentials: {existing_key[:12]}... ✓{source_suffix}")
  782. elif cc_available:
  783. print(" Claude Code credentials: ✓ (auto-detected)")
  784. print()
  785. choice = _prompt_auth_credentials_choice("Anthropic credentials:")
  786. if choice == "reauth":
  787. needs_auth = True
  788. elif choice == "cancel":
  789. return
  790. # "use" (default): proceed to model selection with existing creds
  791. if needs_auth and not _anthropic_authenticate():
  792. return
  793. print()
  794. selected = _pick_model_or_prompt(
  795. _PROVIDER_MODELS.get("anthropic", []), "Model name (e.g., claude-sonnet-4-20250514): ",
  796. current_model=current_model, confirm_provider="anthropic")
  797. # Clear base_url: resolve_runtime_provider() always hardcodes Anthropic's URL, and a
  798. # stale value can contaminate other providers on a later switch.
  799. _finish_model(selected, "anthropic", f"Default model set to: {selected} (via Anthropic)", drop_base_url=True, drop_api_mode=True)
  800. # ── Generic flow for plugin providers without a bespoke `_model_flow_*` ────────────────────────────
  801. # The credential step is keyed by the profile's auth_type: each returns ``(base_url, api_key)`` or
  802. # None when the picker must stop (the helper already printed why). ``api_key`` providers keep
  803. # ``_model_flow_api_key_provider``; ``hermes_cli.main`` routes every registered profile missing
  804. # from ``_PROVIDER_MODEL_FLOWS`` here, so an admitted plugin is never a silent no-op.
  805. def _external_process_login_gate(profile, status) -> bool:
  806. """Logged in → say so (with plan). Logged out → run the CLI's own login on a TTY (it is
  807. browser/device based, so it must own the terminal), else print the instruction and stop."""
  808. import shlex
  809. import subprocess
  810. import sys
  811. if status["logged_in"]:
  812. plan = f" ({status['plan']})" if status.get("plan") else ""
  813. _say(f" {profile.display_name} credentials: ✓{plan}", "")
  814. return True
  815. login = status.get("login_command")
  816. if not login or not sys.stdin.isatty():
  817. _say(f" ✗ {status['detail']}")
  818. return False
  819. _say(f" {status['detail'].split('.')[0]}.", f" Starting `{shlex.join(login[-2:])}` (press Ctrl-C to cancel)...", "")
  820. try:
  821. subprocess.run(login, check=False)
  822. except (KeyboardInterrupt, OSError):
  823. print("Login cancelled or failed.")
  824. return False
  825. if not profile.setup_status()["logged_in"]:
  826. print("Login failed.")
  827. _note_setup_failure("auth")
  828. return False
  829. _say("", f" {profile.display_name} credentials: ✓", "")
  830. return True
  831. def _plugin_flow_external_process(provider_id: str, profile) -> tuple[str, str] | None:
  832. from hermes_cli.auth import get_external_process_provider_status, resolve_external_process_provider_credentials
  833. status = get_external_process_provider_status(provider_id)
  834. _say(f" {profile.display_name or provider_id} delegates Hermes turns to a local `{status.get('command') or profile.process_command}` process.",
  835. f" Command: {status.get('resolved_command') or status.get('command') or '(not found)'}",
  836. f" Backend marker: {status.get('base_url') or profile.base_url}", "")
  837. try:
  838. creds = resolve_external_process_provider_credentials(provider_id)
  839. except Exception as exc:
  840. _say(f" ⚠ {exc}")
  841. return None
  842. # A profile that can ask its CLI (``setup_status``) gates on login before anything is saved.
  843. probe = profile.setup_status()
  844. if probe is not None:
  845. if not probe["available"]:
  846. _say(f" ✗ {probe['detail']}")
  847. return None
  848. if not _external_process_login_gate(profile, probe):
  849. return None
  850. return str(creds.get("base_url") or profile.base_url or ""), ""
  851. def _plugin_flow_oauth(provider_id: str, profile) -> tuple[str, str] | None:
  852. from hermes_cli.auth import get_auth_status
  853. from hermes_cli.auth_plugin_providers import plugin_missing_auth_handler_error
  854. status = get_auth_status(provider_id)
  855. if not status.get("logged_in"):
  856. missing = plugin_missing_auth_handler_error(provider_id, "add")
  857. _say(f" ⚠ Not signed in to {profile.display_name or provider_id}.",
  858. f" {missing.code if missing else status.get('hint') or f'Run `hermes auth add {provider_id}` to sign in.'}")
  859. return None
  860. from agent.credential_pool import load_pool
  861. entry = load_pool(provider_id).select()
  862. base_url = (entry.runtime_base_url if entry else "") or status.get("base_url") or profile.base_url or ""
  863. return str(base_url), (entry.runtime_api_key if entry else "") or ""
  864. _PLUGIN_FLOW_CREDENTIALS = {
  865. "external_process": _plugin_flow_external_process,
  866. "oauth_device_code": _plugin_flow_oauth,
  867. "oauth_external": _plugin_flow_oauth,
  868. }
  869. def _profile_model_setup_flow(provider_id: str):
  870. """``flow(config, current_model, args)`` for a registered profile that owns its ``hermes model``
  871. step (``ProviderProfile.model_setup_flow``), else None."""
  872. from providers import get_provider_profile
  873. profile = get_provider_profile(provider_id)
  874. return getattr(profile, "model_setup_flow", None) if profile is not None else None
  875. def _is_profile_plugin_flow_provider(provider_id: str) -> bool:
  876. """True when *provider_id* is a registered profile whose auth_type the generic plugin flow handles."""
  877. from hermes_cli.auth_plugin_providers import plugin_profile
  878. profile = plugin_profile(provider_id)
  879. return profile is not None and profile.auth_type in _PLUGIN_FLOW_CREDENTIALS
  880. def _plugin_flow_live_rows(profile, api_key: str, base_url: str) -> tuple[list[str] | None, dict[str, str]]:
  881. """``(live ids, {id: note})``. ``discover_models()`` (the account's own annotated picker, e.g.
  882. ``usage credits``) wins when the profile implements it; otherwise the plain ``fetch_models()``."""
  883. with contextlib.suppress(Exception):
  884. rows = profile.discover_models()
  885. if rows:
  886. _say(f" Models below come from your {profile.display_name or profile.name} account.", "")
  887. return [r["id"] for r in rows], {r["id"]: r["note"] for r in rows if r.get("note")}
  888. live = None
  889. with contextlib.suppress(Exception):
  890. live = profile.fetch_models(api_key=api_key or None, base_url=base_url or None)
  891. return live, {}
  892. def _model_flow_plugin_provider(config, provider_id, current_model=""):
  893. """Generic ``hermes model`` flow for a registered plugin profile: credential step by auth_type,
  894. catalog via ``merge_profile_catalog`` (live ``fetch_models()`` ⊕ ``fallback_models``), persist."""
  895. from hermes_cli.auth_plugin_providers import plugin_profile
  896. from hermes_cli.models import merge_profile_catalog
  897. del config
  898. profile = plugin_profile(provider_id)
  899. creds = _PLUGIN_FLOW_CREDENTIALS[profile.auth_type](provider_id, profile)
  900. if creds is None:
  901. return
  902. base_url, api_key = creds
  903. live, notes = _plugin_flow_live_rows(profile, api_key, base_url)
  904. model_list = merge_profile_catalog(provider_id, profile, live) or []
  905. selected = _pick_model_or_prompt(
  906. model_list, "Model name: ", current_model=current_model, confirm_provider=provider_id,
  907. confirm_base_url=base_url, confirm_api_key=api_key, notes=notes)
  908. _finish_model(selected, provider_id, f"Default model set to: {selected} (via {profile.display_name or provider_id})",
  909. base_url=base_url or None, api_mode=profile.api_mode or None)