web_server_config.py 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  1. """Dashboard config schema and model-assignment logic: CONFIG_SCHEMA construction, dynamic provider options, web<->config normalisation, main/aux model assignment.
  2. """
  3. import logging
  4. import os
  5. from dataclasses import replace
  6. from fastapi import HTTPException
  7. from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
  8. from agent.model_metadata import is_local_endpoint
  9. from hermes_cli.config import (
  10. DEFAULT_CONFIG,
  11. cfg_get,
  12. clear_model_endpoint_credentials,
  13. find_provider_entry,
  14. read_raw_config,
  15. )
  16. from hermes_cli.config_backend import refusing_locked_omissions
  17. from hermes_cli.web_server_memory import _normalize_memory_provider_name
  18. from tools.transcription_common import STT_MODEL_CATALOG
  19. from tools.wake_word import _PROVIDER_PREFERENCE
  20. if TYPE_CHECKING:
  21. from hermes_cli.model_switch import ModelSwitchResult
  22. # Same logger the code used before extraction (record parity).
  23. _log = logging.getLogger("hermes_cli.web_server")
  24. # ---------------------------------------------------------------------------
  25. # Config schema — auto-generated from DEFAULT_CONFIG
  26. # ---------------------------------------------------------------------------
  27. def _memory_provider_options() -> list[str]:
  28. """Discovered memory providers for the ``memory.provider`` select.
  29. Directory-scan only (no provider imports), so safe at module import time. ``""``
  30. (built-in only) is always first; a discovery failure leaves only that.
  31. The literal ``builtin`` alias is deliberately NOT offered — built-in memory is not a
  32. provider plugin; ``_normalize_memory_provider_name`` maps legacy aliases back to ``""``.
  33. See #49513.
  34. """
  35. options = [""]
  36. try:
  37. from plugins.memory import list_memory_provider_names
  38. options.extend(list_memory_provider_names())
  39. except Exception:
  40. _log.debug("memory provider discovery failed", exc_info=True)
  41. return list(dict.fromkeys(options))
  42. def _timezone_options() -> list[str]:
  43. """Return sorted IANA timezone identifiers, cached at import time."""
  44. try:
  45. import zoneinfo
  46. return sorted(zoneinfo.available_timezones()) or ["UTC"]
  47. except Exception: # pragma: no cover
  48. return ["UTC"]
  49. def _select(description: str, *options: str, **extra: Any) -> dict[str, Any]:
  50. return {"type": "select", "description": description, "options": list(options), **extra}
  51. # Manual overrides for fields that need select options or custom types.
  52. _SCHEMA_OVERRIDES: dict[str, dict[str, Any]] = {
  53. "timezone": _select(
  54. "IANA timezone (e.g. America/New_York). Blank uses the system timezone.",
  55. *_timezone_options(), searchable=True, clearable=True,
  56. ),
  57. "memory.provider": _select("Memory provider plugin", *_memory_provider_options()),
  58. "model": {
  59. "type": "string",
  60. "description": "Default model (e.g. anthropic/claude-sonnet-4.6)",
  61. "category": "general",
  62. },
  63. "model_context_length": {
  64. "type": "number",
  65. "description": "Context window override (0 = auto-detect from model metadata)",
  66. "category": "general",
  67. },
  68. "terminal.backend": _select(
  69. "Terminal execution backend",
  70. "local", "docker", "ssh", "modal", "daytona", "vercel_sandbox",
  71. ),
  72. # sync with _SUPPORTED_VERCEL_RUNTIMES in terminal_tool.py
  73. "terminal.vercel_image": {
  74. "type": "string",
  75. "description": "Vercel Sandbox image: a Vercel managed image (vercel/sandbox/universal:latest) or a VCR repository[:tag]",
  76. },
  77. "terminal.vercel_runtime": _select(
  78. "Legacy Vercel Sandbox runtime (deprecated by Vercel; a pinned runtime overrides the image; clear to use the image)",
  79. "node24", "node22", "python3.13", clearable=True),
  80. "terminal.modal_mode": _select("Modal sandbox mode", "sandbox", "function"),
  81. "proxy.enabled": {
  82. "type": "boolean",
  83. "description": (
  84. "Docker-only egress credential firewall. Requires `hermes egress setup` "
  85. "and `hermes egress start`; Modal/SSH/Daytona are not wired yet."
  86. ),
  87. "category": "security",
  88. },
  89. "proxy.credential_source": _select(
  90. "Where iron-proxy loads real upstream secrets at start time", "env", "bitwarden", category="security"
  91. ),
  92. "proxy.enforce_on_docker": {
  93. "type": "boolean",
  94. "description": "Refuse Docker sandboxes when egress is enabled but not configured/running",
  95. "category": "security",
  96. },
  97. "auth.adopt_external_logins": {
  98. "type": "boolean",
  99. "description": (
  100. "Borrow and refresh the Codex CLI / Claude Code logins when Hermes has no usable login of its own. "
  101. "Off: Hermes uses only its own logins (`hermes auth add <provider>`)."
  102. ),
  103. "category": "security",
  104. },
  105. "wake_word.provider": _select(
  106. "Wake engine. Auto selects a platform-supported engine; Porcupine requires PORCUPINE_ACCESS_KEY.",
  107. "auto", *_PROVIDER_PREFERENCE,
  108. ),
  109. "tts.provider": _select(
  110. "Text-to-speech provider",
  111. "edge", "elevenlabs", "openai", "xai", "minimax", "mistral", "gemini", "neutts", "kittentts", "piper",
  112. ),
  113. "stt.provider": _select(
  114. "Speech-to-text provider", "local", "groq", "openai", "mistral", "xai", "elevenlabs", "deepinfra"),
  115. "stt.local.model": _select("Local faster-whisper model size", *STT_MODEL_CATALOG["local"]),
  116. "stt.groq.model": _select("Groq Whisper model", *STT_MODEL_CATALOG["groq"]),
  117. "stt.openai.model": _select("OpenAI transcription model", *STT_MODEL_CATALOG["openai"]),
  118. "stt.openai.streaming_model": _select("OpenAI live transcription model (stt.streaming)", "gpt-live-transcribe",
  119. "gpt-transcribe", "gpt-4o-transcribe", "gpt-4o-mini-transcribe"),
  120. "stt.mistral.model": _select("Mistral Voxtral transcription model", *STT_MODEL_CATALOG["mistral"]),
  121. "stt.xai.model": _select("xAI transcription model", *STT_MODEL_CATALOG["xai"]),
  122. "stt.elevenlabs.model_id": _select("ElevenLabs Scribe model", *STT_MODEL_CATALOG["elevenlabs"]),
  123. "display.skin": _select("CLI visual theme", "default", "ares", "mono", "slate"),
  124. "dashboard.theme": _select(
  125. "Web dashboard visual theme", "default", "midnight", "ember", "mono", "cyberpunk", "rose"
  126. ),
  127. "display.resume_display": _select("How resumed sessions display history", "minimal", "full", "off"),
  128. "display.busy_input_mode": _select("Input behavior while agent is running", "interrupt", "queue", "steer"),
  129. "approvals.mode": _select("Dangerous command approval mode", "manual", "smart", "off"),
  130. "context.engine": _select("Context management engine", "default", "custom"),
  131. "human_delay.mode": _select("Simulated typing delay mode", "off", "typing", "fixed"),
  132. "logging.level": _select("Log level for agent.log", "DEBUG", "INFO", "WARNING", "ERROR"),
  133. "agent.service_tier": _select(
  134. "Fast mode: fast = always, auto = first N seconds of each turn, cold = first turn only",
  135. "", "normal", "fast", "auto", "cold",
  136. ),
  137. "delegation.reasoning_effort": _select(
  138. "Reasoning effort for delegated subagents",
  139. "", "minimal", "low", "medium", "high", "xhigh", "max", "ultra",
  140. ),
  141. "updates.non_interactive_local_changes": _select(
  142. "When the chat app / gateway updates Hermes (no terminal prompt), "
  143. "what to do with uncommitted local source edits. 'stash' keeps them "
  144. "and re-applies them after the update; 'discard' throws them away. "
  145. "Terminal updates always ask, regardless of this setting.",
  146. "stash", "discard",
  147. ),
  148. "updates.refresh_cua_driver": {
  149. "type": "boolean",
  150. "description": (
  151. "Refresh an already-installed cua-driver during hermes update. "
  152. "Disable this on non-admin macOS accounts where /Applications is "
  153. "not writable."
  154. ),
  155. },
  156. "browser.headed": {
  157. "type": "boolean",
  158. "description": "Run the local browser in headed mode (visible window). Also keeps the window open between turns; idle sessions are still reaped after browser.inactivity_timeout.",
  159. },
  160. "plugins.hook_callback_timeout": {
  161. "type": "number",
  162. "description": (
  163. "Wall-clock cap (seconds) for timeout-bounded in-process Python "
  164. "plugin hook callbacks (hot-path observers + pre_tool_call). "
  165. "Timed-out pre_tool_call fails closed. 0 disables the cap; "
  166. "values above 600 are clamped. Caller-thread hooks such as "
  167. "subagent_stop are never moved onto a timeout worker."
  168. ),
  169. },
  170. "plugins.load_timeout_seconds": {
  171. "type": "number",
  172. "description": (
  173. "Deadline (seconds) for one plugin's import + register() at load. A plugin that "
  174. "overruns it is skipped with the reason 'load timed out' and the rest keep loading. "
  175. "0 disables the deadline; values above 600 are clamped."
  176. ),
  177. },
  178. }
  179. # Small categories fold into a bigger tab to avoid one-field orphan tabs. Several sources
  180. # (models_dev, onboarding, mcp, computer_use, telemetry, plugins, doctor, runtime, session,
  181. # nous, telegram) currently surface a single schema field each.
  182. _CATEGORY_MERGE: dict[str, str] = {
  183. "privacy": "security",
  184. "context": "agent",
  185. "skills": "agent",
  186. "cron": "agent",
  187. "network": "agent",
  188. "models_dev": "agent",
  189. "checkpoints": "agent",
  190. "approvals": "security",
  191. "human_delay": "display",
  192. "dashboard": "display",
  193. "code_execution": "agent",
  194. "prompt_caching": "agent",
  195. "bot_mode": "agent",
  196. "goals": "agent",
  197. "updates": "general",
  198. "onboarding": "agent",
  199. "telegram": "discord",
  200. "mcp": "agent",
  201. "computer_use": "agent",
  202. "telemetry": "security",
  203. "plugins": "agent",
  204. "doctor": "general",
  205. # `runtime.nofile_soft_limit` (#78873) is the only schema-surfaced runtime field — fold it into the
  206. # agent tab rather than spawning a one-field orphan category.
  207. "runtime": "agent",
  208. "session": "general",
  209. "nous": "agent",
  210. "connections": "agent",
  211. "auth": "security",
  212. # `fallback.min_switch_reset_seconds` is the only schema-surfaced fallback field.
  213. "fallback": "agent",
  214. }
  215. _UI_TYPES = ((bool, "boolean"), (int, "number"), (float, "number"), (list, "list"), (dict, "object"))
  216. def _infer_type(value: Any) -> str:
  217. """Infer a UI field type from a Python value."""
  218. return next((ui for py, ui in _UI_TYPES if isinstance(value, py)), "string")
  219. def _build_schema_from_config(config: dict[str, Any], prefix: str = "") -> dict[str, dict[str, Any]]:
  220. """Walk DEFAULT_CONFIG and produce a flat dot-path → field schema dict."""
  221. schema: dict[str, dict[str, Any]] = {}
  222. for key, value in config.items():
  223. full_key = f"{prefix}.{key}" if prefix else key
  224. if full_key == "_config_version":
  225. continue
  226. if isinstance(value, dict):
  227. schema.update(_build_schema_from_config(value, full_key))
  228. continue
  229. # Category: first path component for nested keys, "general" for top-level scalars.
  230. entry: dict[str, Any] = {
  231. "type": _infer_type(value),
  232. "description": full_key.replace(".", " → ").replace("_", " ").title(),
  233. "category": prefix.split(".")[0] if prefix else "general",
  234. }
  235. entry.update(_SCHEMA_OVERRIDES.get(full_key, {}))
  236. entry["category"] = _CATEGORY_MERGE.get(entry["category"], entry["category"])
  237. schema[full_key] = entry
  238. return schema
  239. def _config_schema_with_virtual_fields() -> dict[str, dict[str, Any]]:
  240. """DEFAULT_CONFIG schema plus the virtual ``model_context_length`` field, inserted right
  241. after ``model`` so it renders adjacent in the frontend."""
  242. ordered: dict[str, dict[str, Any]] = {}
  243. for key, entry in _build_schema_from_config(DEFAULT_CONFIG).items():
  244. ordered[key] = entry
  245. if key == "model":
  246. ordered["model_context_length"] = _SCHEMA_OVERRIDES["model_context_length"]
  247. return ordered
  248. CONFIG_SCHEMA = _config_schema_with_virtual_fields()
  249. def _is_command_provider_block(value: Any) -> bool:
  250. """True when *value* declares a command-type voice provider.
  251. Mirrors the runtime discriminators (``tools.tts_command_provider._is_command_provider_config`` /
  252. ``tools.transcription_command._is_command_stt_provider_config``) and the desktop's
  253. ``isCommandProvider``: ``type`` is OPTIONAL and case/space-insensitive (absent or
  254. normalizing to ``"command"``); ``command`` MUST be a non-empty string.
  255. """
  256. if not isinstance(value, dict):
  257. return False
  258. ptype = str(value.get("type") or "").strip().lower()
  259. if ptype and ptype != "command":
  260. return False
  261. command = value.get("command")
  262. return isinstance(command, str) and bool(command.strip())
  263. def _custom_provider_options(kind: str, builtin_names: list[str], cfg: dict[str, Any]) -> list[str]:
  264. """Merged ``tts``/``stt`` provider options without hard-coding vendor names.
  265. Built-in display names first (original order), then, deduped case-insensitively:
  266. 1. Command-type providers from canonical ``<kind>.providers.<name>`` and the legacy
  267. top-level ``<kind>.<name>`` — the runtime's dual resolution order. Names colliding
  268. with a RUNTIME built-in are excluded (the runtime rejects them before config lookup);
  269. the runtime sets are used rather than the display shortlist, which drifts.
  270. 2. Plugin-registered names from the tts/transcription registries — opportunistic: this
  271. process may never call ``discover_plugins()``, so the registry may be empty.
  272. 3. The current ``<kind>.provider`` value, so a custom active name stays selectable.
  273. Guard semantics mirror the desktop's ``commandProviderNames`` so both surfaces agree.
  274. """
  275. names = [str(n) for n in builtin_names]
  276. seen = {n.strip().lower() for n in names}
  277. if kind == "tts":
  278. from tools.tts_tool import BUILTIN_TTS_PROVIDERS as _runtime_builtins
  279. else:
  280. from tools.transcription_common import BUILTIN_STT_PROVIDERS as _runtime_builtins
  281. def _add(name: Any) -> None:
  282. stripped = name.strip() if isinstance(name, str) else ""
  283. if stripped and stripped.lower() not in seen:
  284. names.append(stripped)
  285. seen.add(stripped.lower())
  286. section = cfg.get(kind)
  287. if not isinstance(section, dict):
  288. section = {}
  289. providers_map = section.get("providers")
  290. candidate_blocks: list[Any] = [providers_map] if isinstance(providers_map, dict) else []
  291. candidate_blocks.append({k: v for k, v in section.items() if k != "providers"})
  292. for block in candidate_blocks:
  293. for name, value in block.items():
  294. if (
  295. isinstance(name, str)
  296. and name.strip().lower() not in _runtime_builtins
  297. and _is_command_provider_block(value)
  298. ):
  299. _add(name)
  300. try:
  301. if kind == "tts":
  302. from agent.tts_registry import list_providers as _list_voice_providers
  303. else:
  304. from agent.transcription_registry import list_providers as _list_voice_providers
  305. for _p in _list_voice_providers():
  306. _add(getattr(_p, "name", None))
  307. except Exception: # pragma: no cover - registry import should not break schema
  308. pass
  309. # ``cfg_get`` takes *keys*, not dotted paths.
  310. _add(cfg_get(cfg, kind, "provider"))
  311. return names
  312. def _memory_provider_schema_options(cfg: dict[str, Any]) -> list[str]:
  313. """Discovered memory providers plus the currently-configured one, so a value that is no
  314. longer discoverable (e.g. plugin removed from disk) never vanishes from the dropdown."""
  315. options = _memory_provider_options()
  316. memory = cfg.get("memory")
  317. current = _normalize_memory_provider_name(memory.get("provider") if isinstance(memory, dict) else None)
  318. if current and current not in options:
  319. options = [*options, current]
  320. return options
  321. def _schema_select_options(key: str) -> Optional[list[str]]:
  322. entry = CONFIG_SCHEMA.get(key)
  323. options = entry.get("options") if isinstance(entry, dict) else None
  324. return options if isinstance(options, list) else None
  325. def _schema_with_dynamic_provider_options() -> dict[str, dict[str, Any]]:
  326. """CONFIG_SCHEMA with per-request discovery-driven ``*.provider`` options merged.
  327. ``_SCHEMA_OVERRIDES`` freezes option lists at import time, so a provider installed after
  328. the server started never appears. Recomputing at request time reflects the CURRENT
  329. (possibly profile-scoped) config.yaml and mid-session plugin installs for every surface
  330. that reads the schema. ``CONFIG_SCHEMA`` is never mutated; changed entries are
  331. shallow-copied onto a copied mapping.
  332. """
  333. from hermes_cli.web_server_profiles import _plugin_terminal_backend_rows
  334. from hermes_cli.config import load_config
  335. try:
  336. cfg = load_config()
  337. except Exception: # pragma: no cover - schema must survive config errors
  338. return CONFIG_SCHEMA
  339. overlay: dict[str, dict[str, Any]] = {}
  340. def merge(key: str, options: list[str]) -> None:
  341. if _schema_select_options(key) is not None and options != CONFIG_SCHEMA[key]["options"]:
  342. overlay[key] = {**CONFIG_SCHEMA[key], "options": options}
  343. for kind in ("tts", "stt"):
  344. existing = _schema_select_options(f"{kind}.provider")
  345. if existing is not None:
  346. merge(f"{kind}.provider", _custom_provider_options(kind, list(existing), cfg))
  347. merge("memory.provider", _memory_provider_schema_options(cfg))
  348. tb_options = _schema_select_options("terminal.backend")
  349. if tb_options is not None:
  350. try:
  351. plugin_names = sorted({row["name"] for row in _plugin_terminal_backend_rows()} - set(tb_options))
  352. except Exception:
  353. plugin_names = []
  354. if plugin_names:
  355. merge("terminal.backend", [*tb_options, *plugin_names])
  356. return {**CONFIG_SCHEMA, **overlay} if overlay else CONFIG_SCHEMA
  357. def _normalize_main_model_assignment(provider: str, model: str) -> tuple[str, str]:
  358. """Normalize a main-slot (provider, model) pair before persisting.
  359. The per-card "Use as → Main model" menu can send the model's VENDOR prefix as the
  360. provider (analytics rows with no ``billing_provider``), producing e.g.
  361. ``provider: anthropic`` + ``default: anthropic/claude-opus-4.6`` — an aggregator slug on
  362. the native provider, which 400s. Two repairs at this single chokepoint:
  363. 1. Vendor-name → Hermes-provider: when the provider is not a known provider/alias but the
  364. model is a vendor-prefixed slug, keep the user's CURRENT aggregator if on one, else
  365. openrouter. User-declared ``providers:``/``custom_providers:`` entries resolve first,
  366. and durable named-custom slugs (``custom`` / ``custom:<name>``) are excluded —
  367. ``_KNOWN_PROVIDER_NAMES`` lists only the bare ``custom`` bucket, so without this a
  368. LiteLLM proxy serving ``ollama/glm-5.2`` would be silently reassigned to openrouter.
  369. Matching only that syntax (not ``startswith("custom")``) avoids swallowing
  370. unconfigured vendors like ``customproxy``.
  371. 2. Model-format normalization for the resolved provider via
  372. ``normalize_model_for_provider`` (custom/user providers keep the model verbatim).
  373. """
  374. from hermes_cli.config import load_config
  375. from hermes_cli.config import get_compatible_custom_providers
  376. from hermes_cli.models import _AGGREGATOR_PROVIDERS, _KNOWN_PROVIDER_NAMES, normalize_provider
  377. from hermes_cli.model_normalize import normalize_model_for_provider
  378. from hermes_cli.providers import resolve_custom_provider, resolve_user_provider
  379. prov_in = (provider or "").strip()
  380. model_in = (model or "").strip()
  381. canonical = normalize_provider(prov_in)
  382. try:
  383. cfg = load_config()
  384. except Exception:
  385. cfg = {}
  386. if not isinstance(cfg, dict):
  387. cfg = {}
  388. user_providers = cfg.get("providers")
  389. declared = resolve_user_provider(
  390. prov_in, user_providers if isinstance(user_providers, dict) else {}
  391. ) or resolve_custom_provider(prov_in, get_compatible_custom_providers(cfg))
  392. if declared is not None:
  393. return declared.id, model_in
  394. is_custom_provider_slug = canonical == "custom" or canonical.startswith("custom:")
  395. if canonical not in _KNOWN_PROVIDER_NAMES and not is_custom_provider_slug and "/" in model_in:
  396. try:
  397. cur_cfg = cfg.get("model", {})
  398. cur_provider = (
  399. str(cur_cfg.get("provider", "") or "").strip().lower() if isinstance(cur_cfg, dict) else ""
  400. )
  401. except Exception:
  402. cur_provider = ""
  403. if cur_provider and normalize_provider(cur_provider) in _AGGREGATOR_PROVIDERS:
  404. canonical = normalize_provider(cur_provider)
  405. prov_in = cur_provider
  406. else:
  407. from hermes_cli.models_detect import provider_has_credentials
  408. # Only guess OpenRouter when the user actually holds a key for it; otherwise keep the
  409. # pair as sent rather than persisting a provider they never selected.
  410. if provider_has_credentials("openrouter"):
  411. canonical = prov_in = "openrouter"
  412. if canonical in _KNOWN_PROVIDER_NAMES and not canonical.startswith("custom"):
  413. try:
  414. model_in = normalize_model_for_provider(model_in, canonical) or model_in
  415. except Exception:
  416. _log.debug("model normalization failed for %s/%s", prov_in, model_in, exc_info=True)
  417. return prov_in, model_in
  418. _BARE_CUSTOM_SELECTORS = frozenset({"custom", "local"})
  419. def _same_endpoint(a: Any, b: Any) -> bool:
  420. """Trailing-slash and case insensitive URL equality (``_save_custom_provider`` dedups the same way)."""
  421. return str(a or "").strip().rstrip("/").lower() == str(b or "").strip().rstrip("/").lower()
  422. def _entry_points_elsewhere(entry: Any, base_url: str) -> bool:
  423. """A configured provider block that names a DIFFERENT host than the submitted endpoint. Its
  424. credentials belong to that host; a block for the same host (e.g. its ``key_env``) still applies."""
  425. return isinstance(entry, dict) and not _same_endpoint(entry.get("base_url"), base_url)
  426. def _is_explicit_custom_endpoint(provider: str, base_url: str) -> bool:
  427. """A bare ``custom`` / ``local`` pick that carries its own endpoint URL defines a route; it is
  428. never a reference to an already-configured provider."""
  429. return provider.strip().lower() in _BARE_CUSTOM_SELECTORS and bool((base_url or "").strip())
  430. def _validated_main_model_selection(
  431. cfg: dict, provider: str, model: str, base_url: str = "", api_key: str = "",
  432. explicit_endpoint: Optional[bool] = None,
  433. ) -> "ModelSwitchResult":
  434. """Route a dashboard main-slot pick through ``switch_model`` (catalog/alias/credential
  435. validation) seeded with the configured route, exactly like a ``/model <model> --provider
  436. <provider> --global``. A bare ``custom`` target carries the submitted endpoint as the current
  437. one, which is how ``switch_model`` binds a custom base_url/key. Rejections become 400s."""
  438. from hermes_cli.config import get_compatible_custom_providers
  439. from hermes_cli.model_switch import switch_model
  440. model_cfg = cfg.get("model") if isinstance(cfg.get("model"), dict) else {}
  441. is_bare_custom = provider.strip().lower() in {"custom", "local"}
  442. # An explicit endpoint must not resolve through the configured providers: the legacy
  443. # bare-``custom`` recovery in ``resolve_custom_provider`` would bind the FIRST named entry, and
  444. # a ``providers.custom`` / ``providers.local`` block (same key as the bare selector) would win
  445. # the credential step and validate ITS host with ITS key. ``_prepare_main_assignment`` passes
  446. # whether the URL was SUBMITTED; a URL it filled from that block is a selection, not a route.
  447. if explicit_endpoint is None:
  448. explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
  449. user_providers = cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
  450. if explicit_endpoint:
  451. user_providers = {k: v for k, v in user_providers.items()
  452. if not (str(k).strip().lower() in _BARE_CUSTOM_SELECTORS
  453. and _entry_points_elsewhere(v, base_url))}
  454. # The submitted endpoint is seeded as a current bare-``custom`` route so the credential step
  455. # keeps it: arriving from another provider it re-resolves ``custom`` from config and would
  456. # validate the saved ``model.base_url`` (and its key) instead of the endpoint being saved.
  457. result = switch_model(
  458. raw_input=model, explicit_provider=provider, is_global=True,
  459. current_provider="custom" if explicit_endpoint else str(model_cfg.get("provider") or ""),
  460. current_model=str(model_cfg.get("default") or ""),
  461. current_base_url=base_url if is_bare_custom else str(model_cfg.get("base_url") or ""),
  462. current_api_key=api_key if is_bare_custom else "",
  463. user_providers=user_providers,
  464. custom_providers=[] if explicit_endpoint else get_compatible_custom_providers(cfg))
  465. if not result.success:
  466. raise HTTPException(status_code=400, detail=result.error_message or "model switch rejected")
  467. if is_bare_custom and base_url.strip():
  468. # The submitted endpoint IS the route this pick asked for; the credential step may have
  469. # re-resolved the bare target onto an env/config endpoint (CUSTOM_BASE_URL, a stale
  470. # model.base_url, the OPENROUTER_BASE_URL mirror). Restore the submitted endpoint AND the
  471. # wire protocol it mandates: ``model.base_url`` and ``model.api_mode`` are persisted
  472. # together, so a mode derived from the displaced host would route the submitted endpoint
  473. # over the wrong wire.
  474. from hermes_cli.providers import determine_api_mode
  475. url = base_url.strip()
  476. result = replace(result, base_url=url,
  477. api_mode=determine_api_mode(result.target_provider, url))
  478. return result
  479. def _apply_main_model_assignment(model_cfg: Any, result: "ModelSwitchResult", api_key: str = "") -> dict:
  480. """Apply a main-slot selection to a ``model`` config dict via the canonical /model shape
  481. (``hermes_cli.model_switch.apply_model_selection``). An explicit key for a custom endpoint is
  482. the one inline credential the runtime reads (``model.api_key``); the legacy ``api`` alias is
  483. dropped so a stale secret cannot shadow it.
  484. Returns a new dict."""
  485. from hermes_cli.config_backend import supports_file_tooling
  486. from hermes_cli.model_switch import apply_model_selection, remote_model_selection_guard
  487. before = model_cfg
  488. # Remote Config: the caller saves the effective block as a whole document, so a cleared
  489. # inherited key must be masked (null), and a locked route key refuses the whole assignment.
  490. model_cfg = apply_model_selection(model_cfg, result, mask_cleared=not supports_file_tooling())
  491. if api_key.strip():
  492. model_cfg["api_key"] = api_key.strip()
  493. drop_model_key(model_cfg, "api")
  494. remote_model_selection_guard(before, model_cfg)
  495. return model_cfg
  496. def drop_model_key(model_cfg: dict, key: str) -> None:
  497. """Remove *key* from a ``model`` block an assignment is building. Under Remote Config the block
  498. is the EFFECTIVE one, so a key it holds may be inherited from an upper level that a profile
  499. removal cannot reach (contract §5.4); store null ("no value") instead, the same mask
  500. :func:`hermes_cli.model_switch.apply_model_selection` applies. A key the block lacks stays
  501. absent. File backend: a plain removal."""
  502. from hermes_cli.config_backend import supports_file_tooling
  503. if key not in model_cfg:
  504. return
  505. if supports_file_tooling():
  506. model_cfg.pop(key, None)
  507. else:
  508. model_cfg[key] = None
  509. def _normalize_config_for_web(config: dict[str, Any]) -> dict[str, Any]:
  510. """Flatten a dict-form ``model`` to its string form (the schema is built from
  511. DEFAULT_CONFIG where ``model`` is a string) and surface ``model_context_length``
  512. as a top-level field (0 = auto-detect)."""
  513. config = dict(config)
  514. model_val = config.get("model")
  515. if isinstance(model_val, dict):
  516. ctx_len = model_val.get("context_length", 0)
  517. config["model"] = model_val.get("default", model_val.get("name", ""))
  518. config["model_context_length"] = ctx_len if isinstance(ctx_len, int) else 0
  519. else:
  520. config["model_context_length"] = 0
  521. return config
  522. # ---------------------------------------------------------------------------
  523. # Model assignment — main slot or auxiliary slots. Mirrors the model.options
  524. # JSON-RPC from tui_gateway but over REST so the Models page can drive it.
  525. # ---------------------------------------------------------------------------
  526. # Canonical auxiliary task slots. Keep in sync with DEFAULT_CONFIG["auxiliary"]
  527. # in hermes_cli/config.py — listed here for deterministic ordering in the UI.
  528. _AUX_TASK_SLOTS: tuple[str, ...] = (
  529. "vision", "compression", "approval", "mcp", "title_generation", "review", "voice_chat",
  530. "triage_specifier", "kanban_decomposer", "profile_describer", "curator",
  531. )
  532. def _plugin_aux_tasks() -> list[dict[str, Any]]:
  533. """Auxiliary tasks registered by plugins (``PluginContext.register_auxiliary_task``) for the
  534. Hermes home active in this context.
  535. Callers run inside ``_profile_scope`` / ``_config_profile_scope``, and ``get_plugin_manager()``
  536. keys its manager on the same context-local home, so a request for profile B enumerates
  537. B's plugins even though this process was started for profile A. Discovery failure is
  538. fail-soft: the built-in slots must keep working without plugins.
  539. """
  540. try:
  541. from hermes_cli.plugins import get_plugin_auxiliary_tasks
  542. return [dict(entry) for entry in get_plugin_auxiliary_tasks()
  543. if entry.get("key") and entry["key"] not in _AUX_TASK_SLOTS]
  544. except Exception: # health: allow BLE001 -- plugin discovery must never break the built-in slots
  545. _log.debug("plugin auxiliary task lookup failed", exc_info=True)
  546. return []
  547. def _aux_task_slots() -> tuple[str, ...]:
  548. """Every auxiliary slot the dashboard may read or assign: built-ins first (UI order), then
  549. plugin-registered keys. The picker in ``hermes model`` (``main_provider_setup._all_aux_tasks``)
  550. folds plugin tasks in the same way; the dashboard must not disagree with it."""
  551. return _AUX_TASK_SLOTS + tuple(entry["key"] for entry in _plugin_aux_tasks())
  552. def _dashboard_code_skew_guard() -> Optional[str]:
  553. """Return a "restart required" message when this process runs stale code, else None.
  554. Long-lived dashboard / Desktop-owned ``hermes serve`` processes freeze ``sys.modules``
  555. at boot; after ``hermes update`` replaces the checkout, a first-time lazy import can
  556. resolve a fresh consumer module against a stale cached dependency -> ImportError.
  557. Mirrors the gateway's ``_model_switch_skew_guard``: refuse the risky call with an
  558. actionable message. Never a false positive (non-git installs return None).
  559. ``/api/model/options`` 500 after the update added ``agent.model_metadata.is_grok_46_family`` while the
  560. running process kept serving the pre-update module (#86207).
  561. """
  562. from gateway.code_skew import detect_code_skew
  563. skew = detect_code_skew()
  564. if not skew:
  565. return None
  566. boot_rev, disk_rev = skew
  567. return (
  568. f"This process is running code from {boot_rev} but the checkout on "
  569. f"disk is now {disk_rev}. The model picker would risk a stale-module "
  570. f"crash — {_dashboard_skew_restart_hint()}"
  571. )
  572. def _dashboard_skew_restart_hint() -> str:
  573. """Restart advice matching how this process is owned — the same app backs the browser
  574. dashboard and Desktop-owned ``hermes serve``; naming a systemd unit would mislead
  575. macOS/launchd hosts and Desktop SSH backends.
  576. See #97046.
  577. """
  578. if os.environ.get("HERMES_SERVE_HEADLESS") == "1":
  579. return (
  580. "restart the Desktop-owned backend to load the new code "
  581. "(use Restart backend in Hermes Desktop, or quit and reopen the app)"
  582. )
  583. return (
  584. "restart this Hermes process to load the new code "
  585. "(hermes dashboard --port <port>, or the equivalent service restart for this install)"
  586. )
  587. def _resolve_assignment_credentials(model_cfg: dict, provider: str, provider_entry: Any) -> None:
  588. """Carry the provider's credential POINTER (``key_env`` / raw ``${VAR}``) onto ``model_cfg``.
  589. ``provider_entry`` comes from ``load_config()``, which expands ``${VAR}`` to plaintext;
  590. copying that into ``model.api_key`` would write the SECRET into config.yaml (and recreate
  591. it on every re-apply). Prefer the raw template; fall back to the expanded value only when
  592. the raw yaml itself stores the key as a literal (no new exposure).
  593. """
  594. try:
  595. _stored, raw_entry = find_provider_entry(read_raw_config().get("providers"), provider)
  596. except Exception:
  597. raw_entry = None
  598. if not isinstance(raw_entry, dict):
  599. raw_entry = {}
  600. key_env = str(raw_entry.get("key_env") or "").strip()
  601. if key_env:
  602. model_cfg["key_env"] = key_env
  603. # #88990: carry the credential POINTER, never a resolved secret. Masked, not dropped, under
  604. # Remote Config: an inherited inline key outranks key_env at runtime.
  605. drop_model_key(model_cfg, "api_key")
  606. elif isinstance(provider_entry, dict) and provider_entry.get("api_key"):
  607. raw_key = str(raw_entry.get("api_key") or "").strip()
  608. model_cfg["api_key"] = raw_key if raw_key.startswith("${") and raw_key.endswith("}") else provider_entry["api_key"]
  609. def _apply_nous_gateway_defaults(cfg: dict) -> list:
  610. """Mirror the CLI's post-model-selection behaviour when switching main to Nous: route
  611. *unconfigured* tools through the Nous Tool Gateway. Purely additive — tools with a direct
  612. key or explicit backend are skipped. Failures never block saving the assignment."""
  613. try:
  614. from hermes_cli.nous_subscription import apply_nous_managed_defaults
  615. from hermes_cli.tools_config import _get_platform_tools
  616. enabled = _get_platform_tools(cfg, "cli", include_default_mcp_servers=False)
  617. return sorted(apply_nous_managed_defaults(cfg, enabled_toolsets=enabled, force_fresh=True))
  618. except Exception:
  619. _log.debug("apply_nous_managed_defaults skipped", exc_info=True)
  620. return []
  621. def _endpoint_known_to_config(cfg: dict, base_url: str) -> bool:
  622. """True when *base_url* is already an endpoint fact in *cfg*: the inline ``model.base_url``,
  623. a ``providers.<slug>`` entry, or a ``custom_providers`` row. Comparison is trailing-slash
  624. and case insensitive (``_save_custom_provider`` dedups the same way)."""
  625. wanted = str(base_url or "").strip().rstrip("/").lower()
  626. if not wanted:
  627. return False
  628. def _matches(value: Any) -> bool:
  629. return str(value or "").strip().rstrip("/").lower() == wanted
  630. model_cfg = cfg.get("model")
  631. if isinstance(model_cfg, dict) and _matches(model_cfg.get("base_url")):
  632. return True
  633. for section_key in ("providers", "custom_providers"):
  634. section = cfg.get(section_key)
  635. entries: Any = section if isinstance(section, list) else (
  636. list(section.values()) if isinstance(section, dict) else [])
  637. for entry in entries:
  638. if isinstance(entry, dict) and _matches(entry.get("base_url") or entry.get("url") or entry.get("api")):
  639. return True
  640. return False
  641. def _register_custom_endpoint(base_url: str, api_key: str, model: str) -> None:
  642. """Register a named ``custom_providers`` entry for a custom/local endpoint (mirrors the
  643. ``hermes model`` custom flow) so the picker gets a proper ready row instead of a "needs
  644. setup" dead-end. Dedups by base_url; never blocks the already-persisted assignment.
  645. SKIPPED when the endpoint is already a fact in the config (#76324): the CLI gateway setup
  646. writes ``provider: custom`` with an inline ``base_url`` + ``api_mode``, and re-registering
  647. that same endpoint as a named ``custom:<slug>`` row on every dashboard re-save is what let
  648. the picker rewrite the CLI's shape into ``custom:<slug>`` + ``base_url: ''``. Only a
  649. GENUINELY new endpoint gets a named row.
  650. """
  651. try:
  652. from hermes_cli.main_provider_setup import _auto_provider_name, _save_custom_provider
  653. _save_custom_provider(base_url, api_key, model, name=_auto_provider_name(base_url))
  654. except Exception:
  655. _log.debug("custom_providers registration skipped", exc_info=True)
  656. def _stale_aux_pins(cfg: dict, new_provider: str) -> list:
  657. """Aux slots still pinned to a *different* provider than the new main one.
  658. Switching main never touches aux pins (independent, sticky per-task overrides) — a user
  659. leaving a now-unpaid provider keeps paying 402s on background calls until they reset
  660. them. We never auto-clear (pinning aux is legitimate) but report them so the UI can
  661. offer a "reset to main" nudge.
  662. """
  663. stale_aux: list[dict] = []
  664. aux_cfg = cfg.get("auxiliary", {})
  665. if not isinstance(aux_cfg, dict):
  666. return stale_aux
  667. for slot in _aux_task_slots():
  668. slot_cfg = aux_cfg.get(slot)
  669. if not isinstance(slot_cfg, dict):
  670. continue
  671. slot_provider = str(slot_cfg.get("provider", "") or "").strip()
  672. # "main" is an alias for the active main provider (auxiliary_client._normalize_aux_provider):
  673. # it follows the switch and is never a stale pin.
  674. if slot_provider and slot_provider.lower() not in {"auto", "", "main"} and slot_provider.lower() != new_provider:
  675. # A pin on a private/LAN endpoint (per-task base_url, e.g. a home Ollama box) never bills
  676. # a provider, so a main switch does not orphan it.
  677. if is_local_endpoint(str(slot_cfg.get("base_url", "") or "")):
  678. continue
  679. stale_aux.append({
  680. "task": slot, "provider": slot_provider, "model": str(slot_cfg.get("model", "") or ""),
  681. })
  682. return stale_aux
  683. def _provider_entry(cfg: dict, provider: str) -> Any:
  684. providers_cfg = cfg.get("providers")
  685. return providers_cfg.get(provider) if isinstance(providers_cfg, dict) else None
  686. def _prepare_main_assignment(cfg: dict, provider: str, model: str, base_url: str, api_key: str) -> "tuple[str, ModelSwitchResult]":
  687. """Validation half of a main-slot assignment: ``(effective base_url, switch result)``.
  688. ``switch_model`` fetches catalogs / probes endpoints, so callers run this BEFORE taking
  689. ``_CONFIG_MUTATION_LOCK``; it writes nothing."""
  690. if not provider or not model:
  691. raise HTTPException(status_code=400, detail="provider and model required for main")
  692. explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
  693. if explicit_endpoint:
  694. # The Local / custom endpoint wizard: the submitted URL IS the route. Normalizing would
  695. # recover a bare ``custom`` onto the first named custom provider and keep ITS URL (#145466).
  696. provider, model = provider.strip(), model.strip()
  697. else:
  698. provider, model = _normalize_main_model_assignment(provider, model)
  699. provider_entry = _provider_entry(cfg, provider)
  700. if not base_url and isinstance(provider_entry, dict) and provider_entry.get("base_url"):
  701. base_url = str(provider_entry.get("base_url") or "").strip()
  702. return base_url, _validated_main_model_selection(
  703. cfg, provider, model, base_url, api_key, explicit_endpoint=explicit_endpoint)
  704. def _apply_main_assignment_sync(cfg: dict, provider: str, model: str, base_url: str, api_key: str,
  705. prepared: "Optional[tuple[str, ModelSwitchResult]]" = None) -> dict:
  706. from hermes_cli.config import save_config
  707. from hermes_cli.free_tier_bootstrap import reconcile_record
  708. # Recorded from the SUBMITTED args, before preparation fills a URL from a named entry.
  709. explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
  710. base_url, result = prepared or _prepare_main_assignment(cfg, provider, model, base_url, api_key)
  711. provider, model = result.target_provider, result.new_model
  712. provider_entry = _provider_entry(cfg, provider)
  713. # Snapshot BEFORE the new assignment overwrites cfg["model"]: this is the state the user
  714. # (CLI setup, a prior dashboard save) already had on disk, and it decides whether the
  715. # endpoint is genuinely NEW (register a named custom_providers row) or already known
  716. # (keep the existing shape — #76324).
  717. provider_lc = provider.strip().lower()
  718. endpoint_already_known = (
  719. provider_lc in {"custom", "local"} and bool(base_url) and _endpoint_known_to_config(cfg, base_url))
  720. model_cfg = _apply_main_model_assignment(cfg.get("model", {}), result, api_key)
  721. if not (explicit_endpoint and _entry_points_elsewhere(provider_entry, base_url)):
  722. # A submitted endpoint carries its own key (or none); a ``providers.custom`` block for
  723. # ANOTHER host shares the selector's name, and hydrating from it would pair the new URL with
  724. # that host's key. A block for the same host (its ``key_env``) still supplies the credential.
  725. _resolve_assignment_credentials(model_cfg, provider, provider_entry)
  726. cfg["model"] = model_cfg
  727. new_provider = provider.strip().lower()
  728. gateway_tools = _apply_nous_gateway_defaults(cfg) if new_provider == "nous" else []
  729. # One route: a model.* key locked by the time the save runs (even after the preflight guard
  730. # above, or on its CAS retry) refuses the whole save instead of being omitted from it.
  731. with refusing_locked_omissions("model"):
  732. save_config(cfg)
  733. if new_provider in {"custom", "local"} and base_url and not endpoint_already_known:
  734. _register_custom_endpoint(base_url, api_key, model)
  735. # The serve process's boot record may still say "nothing configured"; the chat gates on it.
  736. reconcile_record()
  737. return {
  738. "ok": True,
  739. "scope": "main",
  740. "provider": provider,
  741. "model": model,
  742. "base_url": model_cfg.get("base_url", ""),
  743. "gateway_tools": gateway_tools,
  744. "stale_aux": _stale_aux_pins(cfg, new_provider),
  745. # Window plans, Large model: the warning (never a confirm). Absent for everyone else.
  746. **({"notice": result.usage_notice} if getattr(result, "usage_notice", "") else {}),
  747. }
  748. # "Field omitted" sentinel for optional assignment fields whose None means "clear".
  749. _UNSET: Any = object()
  750. def _normalize_aux_reasoning_effort(value: Optional[str]) -> Optional[str]:
  751. """``auxiliary.<task>.reasoning_effort`` value for an assignment: None clears (inherit), else the
  752. canonical level (``none`` for a disable), 400 on an unknown level."""
  753. if value is None:
  754. return None
  755. from hermes_constants import parse_reasoning_effort
  756. parsed = parse_reasoning_effort(value)
  757. if parsed is None:
  758. from hermes_constants import VALID_REASONING_EFFORTS
  759. raise HTTPException(status_code=400,
  760. detail=f"reasoning_effort must be one of: none, {', '.join(VALID_REASONING_EFFORTS)}")
  761. return "none" if parsed.get("enabled") is False else parsed["effort"]
  762. def _aux_default_effort(slot: str) -> str:
  763. from hermes_cli.config_defaults import DEFAULT_CONFIG
  764. block = (DEFAULT_CONFIG.get("auxiliary") or {}).get(slot)
  765. return str(block.get("reasoning_effort") or "") if isinstance(block, dict) else ""
  766. def _apply_aux_assignment_sync(cfg: dict, provider: str, model: str, task: str, base_url: str, api_key: str,
  767. reasoning_effort: Optional[str] = _UNSET) -> dict:
  768. from hermes_cli.config import save_config
  769. aux = cfg.get("auxiliary")
  770. if not isinstance(aux, dict):
  771. aux = {}
  772. def _slot(slot: str) -> dict:
  773. slot_cfg = aux.get(slot)
  774. return slot_cfg if isinstance(slot_cfg, dict) else {}
  775. effort = _normalize_aux_reasoning_effort(reasoning_effort) if reasoning_effort is not _UNSET else _UNSET
  776. slots = _aux_task_slots()
  777. if task == "__reset__":
  778. # Reset every slot to provider="auto", model="", no effort override — keeps other fields intact.
  779. for slot in slots:
  780. slot_cfg = _slot(slot)
  781. slot_cfg["provider"] = "auto"
  782. slot_cfg["model"] = ""
  783. slot_cfg.pop("reasoning_effort", None)
  784. slot_cfg.pop("base_url", None)
  785. clear_model_endpoint_credentials(slot_cfg)
  786. aux[slot] = slot_cfg
  787. cfg["auxiliary"] = aux
  788. save_config(cfg)
  789. return {"ok": True, "scope": "auxiliary", "reset": True}
  790. if not provider:
  791. raise HTTPException(status_code=400, detail="provider required for auxiliary")
  792. targets = [task] if task else list(slots)
  793. new_provider = provider.strip().lower()
  794. for slot in targets:
  795. if slot not in slots:
  796. raise HTTPException(status_code=400, detail=f"unknown auxiliary task: {slot}")
  797. slot_cfg = _slot(slot)
  798. prev_provider = str(slot_cfg.get("provider") or "").strip().lower()
  799. slot_cfg["provider"] = provider
  800. slot_cfg["model"] = model
  801. if base_url:
  802. # Sibling of the main-slot endpoint handling: an aux assignment for a custom/local
  803. # endpoint must carry its own base_url/api_key (the auxiliary resolver reads
  804. # auxiliary.<task>.base_url/api_key), or it silently rebinds to model.base_url and
  805. # breaks once the main slot switches away.
  806. # The auxiliary resolver already reads auxiliary.<task>.base_url/api_key
  807. # (_resolve_task_provider_model), so persisting them here is what actually wires the endpoint
  808. # in. See #65254.
  809. slot_cfg["base_url"] = base_url
  810. if api_key:
  811. slot_cfg["api_key"] = api_key
  812. elif new_provider != prev_provider and new_provider != "custom":
  813. slot_cfg.pop("base_url", None)
  814. clear_model_endpoint_credentials(slot_cfg)
  815. if effort is None:
  816. # "Inherit" is an explicit "" where the slot ships a default (voice_chat: none); popping
  817. # the key would bring that default straight back.
  818. if _aux_default_effort(slot):
  819. slot_cfg["reasoning_effort"] = ""
  820. else:
  821. slot_cfg.pop("reasoning_effort", None)
  822. elif effort is not _UNSET:
  823. slot_cfg["reasoning_effort"] = effort
  824. aux[slot] = slot_cfg
  825. cfg["auxiliary"] = aux
  826. save_config(cfg)
  827. result = {"ok": True, "scope": "auxiliary", "tasks": targets, "provider": provider, "model": model}
  828. if effort is not _UNSET:
  829. result["reasoning_effort"] = effort
  830. return result
  831. def _apply_model_assignment_sync(
  832. scope: str, provider: str, model: str, task: str, base_url: str, api_key: str = "",
  833. reasoning_effort: Optional[str] = _UNSET, prepared: "Optional[tuple[str, ModelSwitchResult]]" = None,
  834. ):
  835. """Synchronous body of POST /api/model/set.
  836. Runs inside ``_profile_scope`` (worker thread) so every load_config/save_config lands in
  837. the requested profile. Raises HTTPException for validation errors. ``prepared`` is a
  838. ``_prepare_main_assignment`` result computed outside the config lock.
  839. """
  840. from hermes_cli.config import load_config
  841. cfg = load_config()
  842. if scope == "main":
  843. return _apply_main_assignment_sync(cfg, provider, model, base_url, api_key, prepared)
  844. return _apply_aux_assignment_sync(cfg, provider, model, task, base_url, api_key, reasoning_effort)
  845. def _infer_provider_on_model_change(model_val: str, prev_provider: str) -> tuple[str, str]:
  846. """Infer which provider serves ``model_val`` when the flat Config-page Model field changes.
  847. Returns ``(provider, model)``; ``provider`` is empty when no switch is warranted. Signals,
  848. in order: curated-catalog detection (``detect_provider_for_model``), then the vendor-slug
  849. heuristic — a ``vendor/model`` slug cannot belong to a non-aggregator provider (e.g.
  850. ``ollama-local``), so return the sentinel ``"openrouter"``; the caller's
  851. ``_normalize_main_model_assignment`` resolves the real aggregator (keeps the current one).
  852. """
  853. name = (model_val or "").strip()
  854. if not name:
  855. return "", name
  856. try:
  857. from hermes_cli.models import _AGGREGATOR_PROVIDERS, detect_provider_for_model, normalize_provider
  858. except Exception:
  859. return "", name
  860. try:
  861. detected = detect_provider_for_model(name, prev_provider)
  862. except Exception:
  863. detected = None
  864. if detected:
  865. return detected[0], detected[1]
  866. if "/" in name:
  867. try:
  868. from hermes_cli.models_detect import provider_has_credentials
  869. cur_is_aggregator = normalize_provider(prev_provider) in _AGGREGATOR_PROVIDERS
  870. # A vendor slug on a native provider is a guess at an aggregator; never guess one the
  871. # user has no key for — that silently writes a metered provider into config.yaml.
  872. if not cur_is_aggregator and provider_has_credentials("openrouter"):
  873. return "openrouter", name
  874. except Exception:
  875. pass
  876. return "", name
  877. def _denormalize_config_from_web(config: dict[str, Any]) -> dict[str, Any]:
  878. """Reverse ``_normalize_config_for_web`` before saving.
  879. Reconstructs ``model`` as a dict from the on-disk config to recover subkeys (provider,
  880. base_url, api_mode, ...) the GET response stripped. When the model name actually changed,
  881. re-detects the serving provider and routes through the assignment chokepoints (a user
  882. picking an OpenRouter model while on ``ollama-local`` would otherwise keep the stale
  883. provider and 404); saving unrelated fields never overwrites an explicit provider.
  884. ``model_context_length`` is written back as ``context_length`` (0 = auto-detect, key
  885. removed). A partial update (Settings autosave diff) that OMITS the key means "unchanged"
  886. and must leave the on-disk override alone — not be treated as an explicit 0.
  887. """
  888. from hermes_cli.config import load_config
  889. config = dict(config)
  890. config.pop("_model_meta", None)
  891. ctx_sent = "model_context_length" in config
  892. ctx_override = config.pop("model_context_length", 0)
  893. if not isinstance(ctx_override, int):
  894. try:
  895. ctx_override = int(ctx_override)
  896. except (TypeError, ValueError):
  897. ctx_override = 0
  898. model_val = config.get("model")
  899. has_model = isinstance(model_val, str) and bool(model_val)
  900. if not (has_model or ctx_sent):
  901. return config
  902. try:
  903. disk_cfg = load_config()
  904. except Exception:
  905. return config # can't read disk config — just use the string form
  906. # Only the disk READ has a fallback. A validation rejection below must propagate as its
  907. # HTTPException(400): swallowing it here left ``model`` a flat string, and the caller's
  908. # deep-merge then overwrote the whole on-disk ``model:`` dict (provider, base_url, slots).
  909. disk_model = disk_cfg.get("model")
  910. if isinstance(disk_model, dict):
  911. if has_model:
  912. prev_default = str(disk_model.get("default") or "").strip()
  913. prev_provider = str(disk_model.get("provider") or "").strip()
  914. if model_val != prev_default and prev_provider:
  915. new_provider, resolved_model = _infer_provider_on_model_change(model_val, prev_provider)
  916. if new_provider and new_provider.strip().lower() != prev_provider.lower():
  917. norm_provider, norm_model = _normalize_main_model_assignment(new_provider, resolved_model)
  918. result = _validated_main_model_selection(disk_cfg, norm_provider, norm_model)
  919. disk_model = _apply_main_model_assignment(disk_model, result)
  920. model_val = result.new_model
  921. disk_model["default"] = model_val
  922. if ctx_sent:
  923. if ctx_override > 0:
  924. disk_model["context_length"] = ctx_override
  925. else:
  926. disk_model.pop("context_length", None)
  927. config["model"] = disk_model
  928. elif ctx_sent and ctx_override > 0:
  929. # Model was a bare string (or absent) — upgrade to a dict for the override.
  930. if has_model:
  931. default = model_val
  932. elif isinstance(disk_model, str) and disk_model:
  933. default = disk_model
  934. else:
  935. default = ""
  936. config["model"] = {"default": default, "context_length": ctx_override}
  937. return config