| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085 |
- """Dashboard config schema and model-assignment logic: CONFIG_SCHEMA construction, dynamic provider options, web<->config normalisation, main/aux model assignment.
- """
- import logging
- import os
- from dataclasses import replace
- from fastapi import HTTPException
- from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING
- from agent.model_metadata import is_local_endpoint
- from hermes_cli.config import (
- DEFAULT_CONFIG,
- cfg_get,
- clear_model_endpoint_credentials,
- find_provider_entry,
- read_raw_config,
- )
- from hermes_cli.config_backend import refusing_locked_omissions
- from hermes_cli.web_server_memory import _normalize_memory_provider_name
- from tools.transcription_common import STT_MODEL_CATALOG
- from tools.wake_word import _PROVIDER_PREFERENCE
- if TYPE_CHECKING:
- from hermes_cli.model_switch import ModelSwitchResult
- # Same logger the code used before extraction (record parity).
- _log = logging.getLogger("hermes_cli.web_server")
- # ---------------------------------------------------------------------------
- # Config schema — auto-generated from DEFAULT_CONFIG
- # ---------------------------------------------------------------------------
- def _memory_provider_options() -> list[str]:
- """Discovered memory providers for the ``memory.provider`` select.
- Directory-scan only (no provider imports), so safe at module import time. ``""``
- (built-in only) is always first; a discovery failure leaves only that.
- The literal ``builtin`` alias is deliberately NOT offered — built-in memory is not a
- provider plugin; ``_normalize_memory_provider_name`` maps legacy aliases back to ``""``.
- See #49513.
- """
- options = [""]
- try:
- from plugins.memory import list_memory_provider_names
- options.extend(list_memory_provider_names())
- except Exception:
- _log.debug("memory provider discovery failed", exc_info=True)
- return list(dict.fromkeys(options))
- def _timezone_options() -> list[str]:
- """Return sorted IANA timezone identifiers, cached at import time."""
- try:
- import zoneinfo
- return sorted(zoneinfo.available_timezones()) or ["UTC"]
- except Exception: # pragma: no cover
- return ["UTC"]
- def _select(description: str, *options: str, **extra: Any) -> dict[str, Any]:
- return {"type": "select", "description": description, "options": list(options), **extra}
- # Manual overrides for fields that need select options or custom types.
- _SCHEMA_OVERRIDES: dict[str, dict[str, Any]] = {
- "timezone": _select(
- "IANA timezone (e.g. America/New_York). Blank uses the system timezone.",
- *_timezone_options(), searchable=True, clearable=True,
- ),
- "memory.provider": _select("Memory provider plugin", *_memory_provider_options()),
- "model": {
- "type": "string",
- "description": "Default model (e.g. anthropic/claude-sonnet-4.6)",
- "category": "general",
- },
- "model_context_length": {
- "type": "number",
- "description": "Context window override (0 = auto-detect from model metadata)",
- "category": "general",
- },
- "terminal.backend": _select(
- "Terminal execution backend",
- "local", "docker", "ssh", "modal", "daytona", "vercel_sandbox",
- ),
- # sync with _SUPPORTED_VERCEL_RUNTIMES in terminal_tool.py
- "terminal.vercel_image": {
- "type": "string",
- "description": "Vercel Sandbox image: a Vercel managed image (vercel/sandbox/universal:latest) or a VCR repository[:tag]",
- },
- "terminal.vercel_runtime": _select(
- "Legacy Vercel Sandbox runtime (deprecated by Vercel; a pinned runtime overrides the image; clear to use the image)",
- "node24", "node22", "python3.13", clearable=True),
- "terminal.modal_mode": _select("Modal sandbox mode", "sandbox", "function"),
- "proxy.enabled": {
- "type": "boolean",
- "description": (
- "Docker-only egress credential firewall. Requires `hermes egress setup` "
- "and `hermes egress start`; Modal/SSH/Daytona are not wired yet."
- ),
- "category": "security",
- },
- "proxy.credential_source": _select(
- "Where iron-proxy loads real upstream secrets at start time", "env", "bitwarden", category="security"
- ),
- "proxy.enforce_on_docker": {
- "type": "boolean",
- "description": "Refuse Docker sandboxes when egress is enabled but not configured/running",
- "category": "security",
- },
- "auth.adopt_external_logins": {
- "type": "boolean",
- "description": (
- "Borrow and refresh the Codex CLI / Claude Code logins when Hermes has no usable login of its own. "
- "Off: Hermes uses only its own logins (`hermes auth add <provider>`)."
- ),
- "category": "security",
- },
- "wake_word.provider": _select(
- "Wake engine. Auto selects a platform-supported engine; Porcupine requires PORCUPINE_ACCESS_KEY.",
- "auto", *_PROVIDER_PREFERENCE,
- ),
- "tts.provider": _select(
- "Text-to-speech provider",
- "edge", "elevenlabs", "openai", "xai", "minimax", "mistral", "gemini", "neutts", "kittentts", "piper",
- ),
- "stt.provider": _select(
- "Speech-to-text provider", "local", "groq", "openai", "mistral", "xai", "elevenlabs", "deepinfra"),
- "stt.local.model": _select("Local faster-whisper model size", *STT_MODEL_CATALOG["local"]),
- "stt.groq.model": _select("Groq Whisper model", *STT_MODEL_CATALOG["groq"]),
- "stt.openai.model": _select("OpenAI transcription model", *STT_MODEL_CATALOG["openai"]),
- "stt.openai.streaming_model": _select("OpenAI live transcription model (stt.streaming)", "gpt-live-transcribe",
- "gpt-transcribe", "gpt-4o-transcribe", "gpt-4o-mini-transcribe"),
- "stt.mistral.model": _select("Mistral Voxtral transcription model", *STT_MODEL_CATALOG["mistral"]),
- "stt.xai.model": _select("xAI transcription model", *STT_MODEL_CATALOG["xai"]),
- "stt.elevenlabs.model_id": _select("ElevenLabs Scribe model", *STT_MODEL_CATALOG["elevenlabs"]),
- "display.skin": _select("CLI visual theme", "default", "ares", "mono", "slate"),
- "dashboard.theme": _select(
- "Web dashboard visual theme", "default", "midnight", "ember", "mono", "cyberpunk", "rose"
- ),
- "display.resume_display": _select("How resumed sessions display history", "minimal", "full", "off"),
- "display.busy_input_mode": _select("Input behavior while agent is running", "interrupt", "queue", "steer"),
- "approvals.mode": _select("Dangerous command approval mode", "manual", "smart", "off"),
- "context.engine": _select("Context management engine", "default", "custom"),
- "human_delay.mode": _select("Simulated typing delay mode", "off", "typing", "fixed"),
- "logging.level": _select("Log level for agent.log", "DEBUG", "INFO", "WARNING", "ERROR"),
- "agent.service_tier": _select(
- "Fast mode: fast = always, auto = first N seconds of each turn, cold = first turn only",
- "", "normal", "fast", "auto", "cold",
- ),
- "delegation.reasoning_effort": _select(
- "Reasoning effort for delegated subagents",
- "", "minimal", "low", "medium", "high", "xhigh", "max", "ultra",
- ),
- "updates.non_interactive_local_changes": _select(
- "When the chat app / gateway updates Hermes (no terminal prompt), "
- "what to do with uncommitted local source edits. 'stash' keeps them "
- "and re-applies them after the update; 'discard' throws them away. "
- "Terminal updates always ask, regardless of this setting.",
- "stash", "discard",
- ),
- "updates.refresh_cua_driver": {
- "type": "boolean",
- "description": (
- "Refresh an already-installed cua-driver during hermes update. "
- "Disable this on non-admin macOS accounts where /Applications is "
- "not writable."
- ),
- },
- "browser.headed": {
- "type": "boolean",
- "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.",
- },
- "plugins.hook_callback_timeout": {
- "type": "number",
- "description": (
- "Wall-clock cap (seconds) for timeout-bounded in-process Python "
- "plugin hook callbacks (hot-path observers + pre_tool_call). "
- "Timed-out pre_tool_call fails closed. 0 disables the cap; "
- "values above 600 are clamped. Caller-thread hooks such as "
- "subagent_stop are never moved onto a timeout worker."
- ),
- },
- "plugins.load_timeout_seconds": {
- "type": "number",
- "description": (
- "Deadline (seconds) for one plugin's import + register() at load. A plugin that "
- "overruns it is skipped with the reason 'load timed out' and the rest keep loading. "
- "0 disables the deadline; values above 600 are clamped."
- ),
- },
- }
- # Small categories fold into a bigger tab to avoid one-field orphan tabs. Several sources
- # (models_dev, onboarding, mcp, computer_use, telemetry, plugins, doctor, runtime, session,
- # nous, telegram) currently surface a single schema field each.
- _CATEGORY_MERGE: dict[str, str] = {
- "privacy": "security",
- "context": "agent",
- "skills": "agent",
- "cron": "agent",
- "network": "agent",
- "models_dev": "agent",
- "checkpoints": "agent",
- "approvals": "security",
- "human_delay": "display",
- "dashboard": "display",
- "code_execution": "agent",
- "prompt_caching": "agent",
- "bot_mode": "agent",
- "goals": "agent",
- "updates": "general",
- "onboarding": "agent",
- "telegram": "discord",
- "mcp": "agent",
- "computer_use": "agent",
- "telemetry": "security",
- "plugins": "agent",
- "doctor": "general",
- # `runtime.nofile_soft_limit` (#78873) is the only schema-surfaced runtime field — fold it into the
- # agent tab rather than spawning a one-field orphan category.
- "runtime": "agent",
- "session": "general",
- "nous": "agent",
- "connections": "agent",
- "auth": "security",
- # `fallback.min_switch_reset_seconds` is the only schema-surfaced fallback field.
- "fallback": "agent",
- }
- _UI_TYPES = ((bool, "boolean"), (int, "number"), (float, "number"), (list, "list"), (dict, "object"))
- def _infer_type(value: Any) -> str:
- """Infer a UI field type from a Python value."""
- return next((ui for py, ui in _UI_TYPES if isinstance(value, py)), "string")
- def _build_schema_from_config(config: dict[str, Any], prefix: str = "") -> dict[str, dict[str, Any]]:
- """Walk DEFAULT_CONFIG and produce a flat dot-path → field schema dict."""
- schema: dict[str, dict[str, Any]] = {}
- for key, value in config.items():
- full_key = f"{prefix}.{key}" if prefix else key
- if full_key == "_config_version":
- continue
- if isinstance(value, dict):
- schema.update(_build_schema_from_config(value, full_key))
- continue
- # Category: first path component for nested keys, "general" for top-level scalars.
- entry: dict[str, Any] = {
- "type": _infer_type(value),
- "description": full_key.replace(".", " → ").replace("_", " ").title(),
- "category": prefix.split(".")[0] if prefix else "general",
- }
- entry.update(_SCHEMA_OVERRIDES.get(full_key, {}))
- entry["category"] = _CATEGORY_MERGE.get(entry["category"], entry["category"])
- schema[full_key] = entry
- return schema
- def _config_schema_with_virtual_fields() -> dict[str, dict[str, Any]]:
- """DEFAULT_CONFIG schema plus the virtual ``model_context_length`` field, inserted right
- after ``model`` so it renders adjacent in the frontend."""
- ordered: dict[str, dict[str, Any]] = {}
- for key, entry in _build_schema_from_config(DEFAULT_CONFIG).items():
- ordered[key] = entry
- if key == "model":
- ordered["model_context_length"] = _SCHEMA_OVERRIDES["model_context_length"]
- return ordered
- CONFIG_SCHEMA = _config_schema_with_virtual_fields()
- def _is_command_provider_block(value: Any) -> bool:
- """True when *value* declares a command-type voice provider.
- Mirrors the runtime discriminators (``tools.tts_command_provider._is_command_provider_config`` /
- ``tools.transcription_command._is_command_stt_provider_config``) and the desktop's
- ``isCommandProvider``: ``type`` is OPTIONAL and case/space-insensitive (absent or
- normalizing to ``"command"``); ``command`` MUST be a non-empty string.
- """
- if not isinstance(value, dict):
- return False
- ptype = str(value.get("type") or "").strip().lower()
- if ptype and ptype != "command":
- return False
- command = value.get("command")
- return isinstance(command, str) and bool(command.strip())
- def _custom_provider_options(kind: str, builtin_names: list[str], cfg: dict[str, Any]) -> list[str]:
- """Merged ``tts``/``stt`` provider options without hard-coding vendor names.
- Built-in display names first (original order), then, deduped case-insensitively:
- 1. Command-type providers from canonical ``<kind>.providers.<name>`` and the legacy
- top-level ``<kind>.<name>`` — the runtime's dual resolution order. Names colliding
- with a RUNTIME built-in are excluded (the runtime rejects them before config lookup);
- the runtime sets are used rather than the display shortlist, which drifts.
- 2. Plugin-registered names from the tts/transcription registries — opportunistic: this
- process may never call ``discover_plugins()``, so the registry may be empty.
- 3. The current ``<kind>.provider`` value, so a custom active name stays selectable.
- Guard semantics mirror the desktop's ``commandProviderNames`` so both surfaces agree.
- """
- names = [str(n) for n in builtin_names]
- seen = {n.strip().lower() for n in names}
- if kind == "tts":
- from tools.tts_tool import BUILTIN_TTS_PROVIDERS as _runtime_builtins
- else:
- from tools.transcription_common import BUILTIN_STT_PROVIDERS as _runtime_builtins
- def _add(name: Any) -> None:
- stripped = name.strip() if isinstance(name, str) else ""
- if stripped and stripped.lower() not in seen:
- names.append(stripped)
- seen.add(stripped.lower())
- section = cfg.get(kind)
- if not isinstance(section, dict):
- section = {}
- providers_map = section.get("providers")
- candidate_blocks: list[Any] = [providers_map] if isinstance(providers_map, dict) else []
- candidate_blocks.append({k: v for k, v in section.items() if k != "providers"})
- for block in candidate_blocks:
- for name, value in block.items():
- if (
- isinstance(name, str)
- and name.strip().lower() not in _runtime_builtins
- and _is_command_provider_block(value)
- ):
- _add(name)
- try:
- if kind == "tts":
- from agent.tts_registry import list_providers as _list_voice_providers
- else:
- from agent.transcription_registry import list_providers as _list_voice_providers
- for _p in _list_voice_providers():
- _add(getattr(_p, "name", None))
- except Exception: # pragma: no cover - registry import should not break schema
- pass
- # ``cfg_get`` takes *keys*, not dotted paths.
- _add(cfg_get(cfg, kind, "provider"))
- return names
- def _memory_provider_schema_options(cfg: dict[str, Any]) -> list[str]:
- """Discovered memory providers plus the currently-configured one, so a value that is no
- longer discoverable (e.g. plugin removed from disk) never vanishes from the dropdown."""
- options = _memory_provider_options()
- memory = cfg.get("memory")
- current = _normalize_memory_provider_name(memory.get("provider") if isinstance(memory, dict) else None)
- if current and current not in options:
- options = [*options, current]
- return options
- def _schema_select_options(key: str) -> Optional[list[str]]:
- entry = CONFIG_SCHEMA.get(key)
- options = entry.get("options") if isinstance(entry, dict) else None
- return options if isinstance(options, list) else None
- def _schema_with_dynamic_provider_options() -> dict[str, dict[str, Any]]:
- """CONFIG_SCHEMA with per-request discovery-driven ``*.provider`` options merged.
- ``_SCHEMA_OVERRIDES`` freezes option lists at import time, so a provider installed after
- the server started never appears. Recomputing at request time reflects the CURRENT
- (possibly profile-scoped) config.yaml and mid-session plugin installs for every surface
- that reads the schema. ``CONFIG_SCHEMA`` is never mutated; changed entries are
- shallow-copied onto a copied mapping.
- """
- from hermes_cli.web_server_profiles import _plugin_terminal_backend_rows
- from hermes_cli.config import load_config
- try:
- cfg = load_config()
- except Exception: # pragma: no cover - schema must survive config errors
- return CONFIG_SCHEMA
- overlay: dict[str, dict[str, Any]] = {}
- def merge(key: str, options: list[str]) -> None:
- if _schema_select_options(key) is not None and options != CONFIG_SCHEMA[key]["options"]:
- overlay[key] = {**CONFIG_SCHEMA[key], "options": options}
- for kind in ("tts", "stt"):
- existing = _schema_select_options(f"{kind}.provider")
- if existing is not None:
- merge(f"{kind}.provider", _custom_provider_options(kind, list(existing), cfg))
- merge("memory.provider", _memory_provider_schema_options(cfg))
- tb_options = _schema_select_options("terminal.backend")
- if tb_options is not None:
- try:
- plugin_names = sorted({row["name"] for row in _plugin_terminal_backend_rows()} - set(tb_options))
- except Exception:
- plugin_names = []
- if plugin_names:
- merge("terminal.backend", [*tb_options, *plugin_names])
- return {**CONFIG_SCHEMA, **overlay} if overlay else CONFIG_SCHEMA
- def _normalize_main_model_assignment(provider: str, model: str) -> tuple[str, str]:
- """Normalize a main-slot (provider, model) pair before persisting.
- The per-card "Use as → Main model" menu can send the model's VENDOR prefix as the
- provider (analytics rows with no ``billing_provider``), producing e.g.
- ``provider: anthropic`` + ``default: anthropic/claude-opus-4.6`` — an aggregator slug on
- the native provider, which 400s. Two repairs at this single chokepoint:
- 1. Vendor-name → Hermes-provider: when the provider is not a known provider/alias but the
- model is a vendor-prefixed slug, keep the user's CURRENT aggregator if on one, else
- openrouter. User-declared ``providers:``/``custom_providers:`` entries resolve first,
- and durable named-custom slugs (``custom`` / ``custom:<name>``) are excluded —
- ``_KNOWN_PROVIDER_NAMES`` lists only the bare ``custom`` bucket, so without this a
- LiteLLM proxy serving ``ollama/glm-5.2`` would be silently reassigned to openrouter.
- Matching only that syntax (not ``startswith("custom")``) avoids swallowing
- unconfigured vendors like ``customproxy``.
- 2. Model-format normalization for the resolved provider via
- ``normalize_model_for_provider`` (custom/user providers keep the model verbatim).
- """
- from hermes_cli.config import load_config
- from hermes_cli.config import get_compatible_custom_providers
- from hermes_cli.models import _AGGREGATOR_PROVIDERS, _KNOWN_PROVIDER_NAMES, normalize_provider
- from hermes_cli.model_normalize import normalize_model_for_provider
- from hermes_cli.providers import resolve_custom_provider, resolve_user_provider
- prov_in = (provider or "").strip()
- model_in = (model or "").strip()
- canonical = normalize_provider(prov_in)
- try:
- cfg = load_config()
- except Exception:
- cfg = {}
- if not isinstance(cfg, dict):
- cfg = {}
- user_providers = cfg.get("providers")
- declared = resolve_user_provider(
- prov_in, user_providers if isinstance(user_providers, dict) else {}
- ) or resolve_custom_provider(prov_in, get_compatible_custom_providers(cfg))
- if declared is not None:
- return declared.id, model_in
- is_custom_provider_slug = canonical == "custom" or canonical.startswith("custom:")
- if canonical not in _KNOWN_PROVIDER_NAMES and not is_custom_provider_slug and "/" in model_in:
- try:
- cur_cfg = cfg.get("model", {})
- cur_provider = (
- str(cur_cfg.get("provider", "") or "").strip().lower() if isinstance(cur_cfg, dict) else ""
- )
- except Exception:
- cur_provider = ""
- if cur_provider and normalize_provider(cur_provider) in _AGGREGATOR_PROVIDERS:
- canonical = normalize_provider(cur_provider)
- prov_in = cur_provider
- else:
- from hermes_cli.models_detect import provider_has_credentials
- # Only guess OpenRouter when the user actually holds a key for it; otherwise keep the
- # pair as sent rather than persisting a provider they never selected.
- if provider_has_credentials("openrouter"):
- canonical = prov_in = "openrouter"
- if canonical in _KNOWN_PROVIDER_NAMES and not canonical.startswith("custom"):
- try:
- model_in = normalize_model_for_provider(model_in, canonical) or model_in
- except Exception:
- _log.debug("model normalization failed for %s/%s", prov_in, model_in, exc_info=True)
- return prov_in, model_in
- _BARE_CUSTOM_SELECTORS = frozenset({"custom", "local"})
- def _same_endpoint(a: Any, b: Any) -> bool:
- """Trailing-slash and case insensitive URL equality (``_save_custom_provider`` dedups the same way)."""
- return str(a or "").strip().rstrip("/").lower() == str(b or "").strip().rstrip("/").lower()
- def _entry_points_elsewhere(entry: Any, base_url: str) -> bool:
- """A configured provider block that names a DIFFERENT host than the submitted endpoint. Its
- credentials belong to that host; a block for the same host (e.g. its ``key_env``) still applies."""
- return isinstance(entry, dict) and not _same_endpoint(entry.get("base_url"), base_url)
- def _is_explicit_custom_endpoint(provider: str, base_url: str) -> bool:
- """A bare ``custom`` / ``local`` pick that carries its own endpoint URL defines a route; it is
- never a reference to an already-configured provider."""
- return provider.strip().lower() in _BARE_CUSTOM_SELECTORS and bool((base_url or "").strip())
- def _validated_main_model_selection(
- cfg: dict, provider: str, model: str, base_url: str = "", api_key: str = "",
- explicit_endpoint: Optional[bool] = None,
- ) -> "ModelSwitchResult":
- """Route a dashboard main-slot pick through ``switch_model`` (catalog/alias/credential
- validation) seeded with the configured route, exactly like a ``/model <model> --provider
- <provider> --global``. A bare ``custom`` target carries the submitted endpoint as the current
- one, which is how ``switch_model`` binds a custom base_url/key. Rejections become 400s."""
- from hermes_cli.config import get_compatible_custom_providers
- from hermes_cli.model_switch import switch_model
- model_cfg = cfg.get("model") if isinstance(cfg.get("model"), dict) else {}
- is_bare_custom = provider.strip().lower() in {"custom", "local"}
- # An explicit endpoint must not resolve through the configured providers: the legacy
- # bare-``custom`` recovery in ``resolve_custom_provider`` would bind the FIRST named entry, and
- # a ``providers.custom`` / ``providers.local`` block (same key as the bare selector) would win
- # the credential step and validate ITS host with ITS key. ``_prepare_main_assignment`` passes
- # whether the URL was SUBMITTED; a URL it filled from that block is a selection, not a route.
- if explicit_endpoint is None:
- explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
- user_providers = cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
- if explicit_endpoint:
- user_providers = {k: v for k, v in user_providers.items()
- if not (str(k).strip().lower() in _BARE_CUSTOM_SELECTORS
- and _entry_points_elsewhere(v, base_url))}
- # The submitted endpoint is seeded as a current bare-``custom`` route so the credential step
- # keeps it: arriving from another provider it re-resolves ``custom`` from config and would
- # validate the saved ``model.base_url`` (and its key) instead of the endpoint being saved.
- result = switch_model(
- raw_input=model, explicit_provider=provider, is_global=True,
- current_provider="custom" if explicit_endpoint else str(model_cfg.get("provider") or ""),
- current_model=str(model_cfg.get("default") or ""),
- current_base_url=base_url if is_bare_custom else str(model_cfg.get("base_url") or ""),
- current_api_key=api_key if is_bare_custom else "",
- user_providers=user_providers,
- custom_providers=[] if explicit_endpoint else get_compatible_custom_providers(cfg))
- if not result.success:
- raise HTTPException(status_code=400, detail=result.error_message or "model switch rejected")
- if is_bare_custom and base_url.strip():
- # The submitted endpoint IS the route this pick asked for; the credential step may have
- # re-resolved the bare target onto an env/config endpoint (CUSTOM_BASE_URL, a stale
- # model.base_url, the OPENROUTER_BASE_URL mirror). Restore the submitted endpoint AND the
- # wire protocol it mandates: ``model.base_url`` and ``model.api_mode`` are persisted
- # together, so a mode derived from the displaced host would route the submitted endpoint
- # over the wrong wire.
- from hermes_cli.providers import determine_api_mode
- url = base_url.strip()
- result = replace(result, base_url=url,
- api_mode=determine_api_mode(result.target_provider, url))
- return result
- def _apply_main_model_assignment(model_cfg: Any, result: "ModelSwitchResult", api_key: str = "") -> dict:
- """Apply a main-slot selection to a ``model`` config dict via the canonical /model shape
- (``hermes_cli.model_switch.apply_model_selection``). An explicit key for a custom endpoint is
- the one inline credential the runtime reads (``model.api_key``); the legacy ``api`` alias is
- dropped so a stale secret cannot shadow it.
- Returns a new dict."""
- from hermes_cli.config_backend import supports_file_tooling
- from hermes_cli.model_switch import apply_model_selection, remote_model_selection_guard
- before = model_cfg
- # Remote Config: the caller saves the effective block as a whole document, so a cleared
- # inherited key must be masked (null), and a locked route key refuses the whole assignment.
- model_cfg = apply_model_selection(model_cfg, result, mask_cleared=not supports_file_tooling())
- if api_key.strip():
- model_cfg["api_key"] = api_key.strip()
- drop_model_key(model_cfg, "api")
- remote_model_selection_guard(before, model_cfg)
- return model_cfg
- def drop_model_key(model_cfg: dict, key: str) -> None:
- """Remove *key* from a ``model`` block an assignment is building. Under Remote Config the block
- is the EFFECTIVE one, so a key it holds may be inherited from an upper level that a profile
- removal cannot reach (contract §5.4); store null ("no value") instead, the same mask
- :func:`hermes_cli.model_switch.apply_model_selection` applies. A key the block lacks stays
- absent. File backend: a plain removal."""
- from hermes_cli.config_backend import supports_file_tooling
- if key not in model_cfg:
- return
- if supports_file_tooling():
- model_cfg.pop(key, None)
- else:
- model_cfg[key] = None
- def _normalize_config_for_web(config: dict[str, Any]) -> dict[str, Any]:
- """Flatten a dict-form ``model`` to its string form (the schema is built from
- DEFAULT_CONFIG where ``model`` is a string) and surface ``model_context_length``
- as a top-level field (0 = auto-detect)."""
- config = dict(config)
- model_val = config.get("model")
- if isinstance(model_val, dict):
- ctx_len = model_val.get("context_length", 0)
- config["model"] = model_val.get("default", model_val.get("name", ""))
- config["model_context_length"] = ctx_len if isinstance(ctx_len, int) else 0
- else:
- config["model_context_length"] = 0
- return config
- # ---------------------------------------------------------------------------
- # Model assignment — main slot or auxiliary slots. Mirrors the model.options
- # JSON-RPC from tui_gateway but over REST so the Models page can drive it.
- # ---------------------------------------------------------------------------
- # Canonical auxiliary task slots. Keep in sync with DEFAULT_CONFIG["auxiliary"]
- # in hermes_cli/config.py — listed here for deterministic ordering in the UI.
- _AUX_TASK_SLOTS: tuple[str, ...] = (
- "vision", "compression", "approval", "mcp", "title_generation", "review", "voice_chat",
- "triage_specifier", "kanban_decomposer", "profile_describer", "curator",
- )
- def _plugin_aux_tasks() -> list[dict[str, Any]]:
- """Auxiliary tasks registered by plugins (``PluginContext.register_auxiliary_task``) for the
- Hermes home active in this context.
- Callers run inside ``_profile_scope`` / ``_config_profile_scope``, and ``get_plugin_manager()``
- keys its manager on the same context-local home, so a request for profile B enumerates
- B's plugins even though this process was started for profile A. Discovery failure is
- fail-soft: the built-in slots must keep working without plugins.
- """
- try:
- from hermes_cli.plugins import get_plugin_auxiliary_tasks
- return [dict(entry) for entry in get_plugin_auxiliary_tasks()
- if entry.get("key") and entry["key"] not in _AUX_TASK_SLOTS]
- except Exception: # health: allow BLE001 -- plugin discovery must never break the built-in slots
- _log.debug("plugin auxiliary task lookup failed", exc_info=True)
- return []
- def _aux_task_slots() -> tuple[str, ...]:
- """Every auxiliary slot the dashboard may read or assign: built-ins first (UI order), then
- plugin-registered keys. The picker in ``hermes model`` (``main_provider_setup._all_aux_tasks``)
- folds plugin tasks in the same way; the dashboard must not disagree with it."""
- return _AUX_TASK_SLOTS + tuple(entry["key"] for entry in _plugin_aux_tasks())
- def _dashboard_code_skew_guard() -> Optional[str]:
- """Return a "restart required" message when this process runs stale code, else None.
- Long-lived dashboard / Desktop-owned ``hermes serve`` processes freeze ``sys.modules``
- at boot; after ``hermes update`` replaces the checkout, a first-time lazy import can
- resolve a fresh consumer module against a stale cached dependency -> ImportError.
- Mirrors the gateway's ``_model_switch_skew_guard``: refuse the risky call with an
- actionable message. Never a false positive (non-git installs return None).
- ``/api/model/options`` 500 after the update added ``agent.model_metadata.is_grok_46_family`` while the
- running process kept serving the pre-update module (#86207).
- """
- from gateway.code_skew import detect_code_skew
- skew = detect_code_skew()
- if not skew:
- return None
- boot_rev, disk_rev = skew
- return (
- f"This process is running code from {boot_rev} but the checkout on "
- f"disk is now {disk_rev}. The model picker would risk a stale-module "
- f"crash — {_dashboard_skew_restart_hint()}"
- )
- def _dashboard_skew_restart_hint() -> str:
- """Restart advice matching how this process is owned — the same app backs the browser
- dashboard and Desktop-owned ``hermes serve``; naming a systemd unit would mislead
- macOS/launchd hosts and Desktop SSH backends.
- See #97046.
- """
- if os.environ.get("HERMES_SERVE_HEADLESS") == "1":
- return (
- "restart the Desktop-owned backend to load the new code "
- "(use Restart backend in Hermes Desktop, or quit and reopen the app)"
- )
- return (
- "restart this Hermes process to load the new code "
- "(hermes dashboard --port <port>, or the equivalent service restart for this install)"
- )
- def _resolve_assignment_credentials(model_cfg: dict, provider: str, provider_entry: Any) -> None:
- """Carry the provider's credential POINTER (``key_env`` / raw ``${VAR}``) onto ``model_cfg``.
- ``provider_entry`` comes from ``load_config()``, which expands ``${VAR}`` to plaintext;
- copying that into ``model.api_key`` would write the SECRET into config.yaml (and recreate
- it on every re-apply). Prefer the raw template; fall back to the expanded value only when
- the raw yaml itself stores the key as a literal (no new exposure).
- """
- try:
- _stored, raw_entry = find_provider_entry(read_raw_config().get("providers"), provider)
- except Exception:
- raw_entry = None
- if not isinstance(raw_entry, dict):
- raw_entry = {}
- key_env = str(raw_entry.get("key_env") or "").strip()
- if key_env:
- model_cfg["key_env"] = key_env
- # #88990: carry the credential POINTER, never a resolved secret. Masked, not dropped, under
- # Remote Config: an inherited inline key outranks key_env at runtime.
- drop_model_key(model_cfg, "api_key")
- elif isinstance(provider_entry, dict) and provider_entry.get("api_key"):
- raw_key = str(raw_entry.get("api_key") or "").strip()
- model_cfg["api_key"] = raw_key if raw_key.startswith("${") and raw_key.endswith("}") else provider_entry["api_key"]
- def _apply_nous_gateway_defaults(cfg: dict) -> list:
- """Mirror the CLI's post-model-selection behaviour when switching main to Nous: route
- *unconfigured* tools through the Nous Tool Gateway. Purely additive — tools with a direct
- key or explicit backend are skipped. Failures never block saving the assignment."""
- try:
- from hermes_cli.nous_subscription import apply_nous_managed_defaults
- from hermes_cli.tools_config import _get_platform_tools
- enabled = _get_platform_tools(cfg, "cli", include_default_mcp_servers=False)
- return sorted(apply_nous_managed_defaults(cfg, enabled_toolsets=enabled, force_fresh=True))
- except Exception:
- _log.debug("apply_nous_managed_defaults skipped", exc_info=True)
- return []
- def _endpoint_known_to_config(cfg: dict, base_url: str) -> bool:
- """True when *base_url* is already an endpoint fact in *cfg*: the inline ``model.base_url``,
- a ``providers.<slug>`` entry, or a ``custom_providers`` row. Comparison is trailing-slash
- and case insensitive (``_save_custom_provider`` dedups the same way)."""
- wanted = str(base_url or "").strip().rstrip("/").lower()
- if not wanted:
- return False
- def _matches(value: Any) -> bool:
- return str(value or "").strip().rstrip("/").lower() == wanted
- model_cfg = cfg.get("model")
- if isinstance(model_cfg, dict) and _matches(model_cfg.get("base_url")):
- return True
- for section_key in ("providers", "custom_providers"):
- section = cfg.get(section_key)
- entries: Any = section if isinstance(section, list) else (
- list(section.values()) if isinstance(section, dict) else [])
- for entry in entries:
- if isinstance(entry, dict) and _matches(entry.get("base_url") or entry.get("url") or entry.get("api")):
- return True
- return False
- def _register_custom_endpoint(base_url: str, api_key: str, model: str) -> None:
- """Register a named ``custom_providers`` entry for a custom/local endpoint (mirrors the
- ``hermes model`` custom flow) so the picker gets a proper ready row instead of a "needs
- setup" dead-end. Dedups by base_url; never blocks the already-persisted assignment.
- SKIPPED when the endpoint is already a fact in the config (#76324): the CLI gateway setup
- writes ``provider: custom`` with an inline ``base_url`` + ``api_mode``, and re-registering
- that same endpoint as a named ``custom:<slug>`` row on every dashboard re-save is what let
- the picker rewrite the CLI's shape into ``custom:<slug>`` + ``base_url: ''``. Only a
- GENUINELY new endpoint gets a named row.
- """
- try:
- from hermes_cli.main_provider_setup import _auto_provider_name, _save_custom_provider
- _save_custom_provider(base_url, api_key, model, name=_auto_provider_name(base_url))
- except Exception:
- _log.debug("custom_providers registration skipped", exc_info=True)
- def _stale_aux_pins(cfg: dict, new_provider: str) -> list:
- """Aux slots still pinned to a *different* provider than the new main one.
- Switching main never touches aux pins (independent, sticky per-task overrides) — a user
- leaving a now-unpaid provider keeps paying 402s on background calls until they reset
- them. We never auto-clear (pinning aux is legitimate) but report them so the UI can
- offer a "reset to main" nudge.
- """
- stale_aux: list[dict] = []
- aux_cfg = cfg.get("auxiliary", {})
- if not isinstance(aux_cfg, dict):
- return stale_aux
- for slot in _aux_task_slots():
- slot_cfg = aux_cfg.get(slot)
- if not isinstance(slot_cfg, dict):
- continue
- slot_provider = str(slot_cfg.get("provider", "") or "").strip()
- # "main" is an alias for the active main provider (auxiliary_client._normalize_aux_provider):
- # it follows the switch and is never a stale pin.
- if slot_provider and slot_provider.lower() not in {"auto", "", "main"} and slot_provider.lower() != new_provider:
- # A pin on a private/LAN endpoint (per-task base_url, e.g. a home Ollama box) never bills
- # a provider, so a main switch does not orphan it.
- if is_local_endpoint(str(slot_cfg.get("base_url", "") or "")):
- continue
- stale_aux.append({
- "task": slot, "provider": slot_provider, "model": str(slot_cfg.get("model", "") or ""),
- })
- return stale_aux
- def _provider_entry(cfg: dict, provider: str) -> Any:
- providers_cfg = cfg.get("providers")
- return providers_cfg.get(provider) if isinstance(providers_cfg, dict) else None
- def _prepare_main_assignment(cfg: dict, provider: str, model: str, base_url: str, api_key: str) -> "tuple[str, ModelSwitchResult]":
- """Validation half of a main-slot assignment: ``(effective base_url, switch result)``.
- ``switch_model`` fetches catalogs / probes endpoints, so callers run this BEFORE taking
- ``_CONFIG_MUTATION_LOCK``; it writes nothing."""
- if not provider or not model:
- raise HTTPException(status_code=400, detail="provider and model required for main")
- explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
- if explicit_endpoint:
- # The Local / custom endpoint wizard: the submitted URL IS the route. Normalizing would
- # recover a bare ``custom`` onto the first named custom provider and keep ITS URL (#145466).
- provider, model = provider.strip(), model.strip()
- else:
- provider, model = _normalize_main_model_assignment(provider, model)
- provider_entry = _provider_entry(cfg, provider)
- if not base_url and isinstance(provider_entry, dict) and provider_entry.get("base_url"):
- base_url = str(provider_entry.get("base_url") or "").strip()
- return base_url, _validated_main_model_selection(
- cfg, provider, model, base_url, api_key, explicit_endpoint=explicit_endpoint)
- def _apply_main_assignment_sync(cfg: dict, provider: str, model: str, base_url: str, api_key: str,
- prepared: "Optional[tuple[str, ModelSwitchResult]]" = None) -> dict:
- from hermes_cli.config import save_config
- from hermes_cli.free_tier_bootstrap import reconcile_record
- # Recorded from the SUBMITTED args, before preparation fills a URL from a named entry.
- explicit_endpoint = _is_explicit_custom_endpoint(provider, base_url)
- base_url, result = prepared or _prepare_main_assignment(cfg, provider, model, base_url, api_key)
- provider, model = result.target_provider, result.new_model
- provider_entry = _provider_entry(cfg, provider)
- # Snapshot BEFORE the new assignment overwrites cfg["model"]: this is the state the user
- # (CLI setup, a prior dashboard save) already had on disk, and it decides whether the
- # endpoint is genuinely NEW (register a named custom_providers row) or already known
- # (keep the existing shape — #76324).
- provider_lc = provider.strip().lower()
- endpoint_already_known = (
- provider_lc in {"custom", "local"} and bool(base_url) and _endpoint_known_to_config(cfg, base_url))
- model_cfg = _apply_main_model_assignment(cfg.get("model", {}), result, api_key)
- if not (explicit_endpoint and _entry_points_elsewhere(provider_entry, base_url)):
- # A submitted endpoint carries its own key (or none); a ``providers.custom`` block for
- # ANOTHER host shares the selector's name, and hydrating from it would pair the new URL with
- # that host's key. A block for the same host (its ``key_env``) still supplies the credential.
- _resolve_assignment_credentials(model_cfg, provider, provider_entry)
- cfg["model"] = model_cfg
- new_provider = provider.strip().lower()
- gateway_tools = _apply_nous_gateway_defaults(cfg) if new_provider == "nous" else []
- # One route: a model.* key locked by the time the save runs (even after the preflight guard
- # above, or on its CAS retry) refuses the whole save instead of being omitted from it.
- with refusing_locked_omissions("model"):
- save_config(cfg)
- if new_provider in {"custom", "local"} and base_url and not endpoint_already_known:
- _register_custom_endpoint(base_url, api_key, model)
- # The serve process's boot record may still say "nothing configured"; the chat gates on it.
- reconcile_record()
- return {
- "ok": True,
- "scope": "main",
- "provider": provider,
- "model": model,
- "base_url": model_cfg.get("base_url", ""),
- "gateway_tools": gateway_tools,
- "stale_aux": _stale_aux_pins(cfg, new_provider),
- # Window plans, Large model: the warning (never a confirm). Absent for everyone else.
- **({"notice": result.usage_notice} if getattr(result, "usage_notice", "") else {}),
- }
- # "Field omitted" sentinel for optional assignment fields whose None means "clear".
- _UNSET: Any = object()
- def _normalize_aux_reasoning_effort(value: Optional[str]) -> Optional[str]:
- """``auxiliary.<task>.reasoning_effort`` value for an assignment: None clears (inherit), else the
- canonical level (``none`` for a disable), 400 on an unknown level."""
- if value is None:
- return None
- from hermes_constants import parse_reasoning_effort
- parsed = parse_reasoning_effort(value)
- if parsed is None:
- from hermes_constants import VALID_REASONING_EFFORTS
- raise HTTPException(status_code=400,
- detail=f"reasoning_effort must be one of: none, {', '.join(VALID_REASONING_EFFORTS)}")
- return "none" if parsed.get("enabled") is False else parsed["effort"]
- def _aux_default_effort(slot: str) -> str:
- from hermes_cli.config_defaults import DEFAULT_CONFIG
- block = (DEFAULT_CONFIG.get("auxiliary") or {}).get(slot)
- return str(block.get("reasoning_effort") or "") if isinstance(block, dict) else ""
- def _apply_aux_assignment_sync(cfg: dict, provider: str, model: str, task: str, base_url: str, api_key: str,
- reasoning_effort: Optional[str] = _UNSET) -> dict:
- from hermes_cli.config import save_config
- aux = cfg.get("auxiliary")
- if not isinstance(aux, dict):
- aux = {}
- def _slot(slot: str) -> dict:
- slot_cfg = aux.get(slot)
- return slot_cfg if isinstance(slot_cfg, dict) else {}
- effort = _normalize_aux_reasoning_effort(reasoning_effort) if reasoning_effort is not _UNSET else _UNSET
- slots = _aux_task_slots()
- if task == "__reset__":
- # Reset every slot to provider="auto", model="", no effort override — keeps other fields intact.
- for slot in slots:
- slot_cfg = _slot(slot)
- slot_cfg["provider"] = "auto"
- slot_cfg["model"] = ""
- slot_cfg.pop("reasoning_effort", None)
- slot_cfg.pop("base_url", None)
- clear_model_endpoint_credentials(slot_cfg)
- aux[slot] = slot_cfg
- cfg["auxiliary"] = aux
- save_config(cfg)
- return {"ok": True, "scope": "auxiliary", "reset": True}
- if not provider:
- raise HTTPException(status_code=400, detail="provider required for auxiliary")
- targets = [task] if task else list(slots)
- new_provider = provider.strip().lower()
- for slot in targets:
- if slot not in slots:
- raise HTTPException(status_code=400, detail=f"unknown auxiliary task: {slot}")
- slot_cfg = _slot(slot)
- prev_provider = str(slot_cfg.get("provider") or "").strip().lower()
- slot_cfg["provider"] = provider
- slot_cfg["model"] = model
- if base_url:
- # Sibling of the main-slot endpoint handling: an aux assignment for a custom/local
- # endpoint must carry its own base_url/api_key (the auxiliary resolver reads
- # auxiliary.<task>.base_url/api_key), or it silently rebinds to model.base_url and
- # breaks once the main slot switches away.
- # The auxiliary resolver already reads auxiliary.<task>.base_url/api_key
- # (_resolve_task_provider_model), so persisting them here is what actually wires the endpoint
- # in. See #65254.
- slot_cfg["base_url"] = base_url
- if api_key:
- slot_cfg["api_key"] = api_key
- elif new_provider != prev_provider and new_provider != "custom":
- slot_cfg.pop("base_url", None)
- clear_model_endpoint_credentials(slot_cfg)
- if effort is None:
- # "Inherit" is an explicit "" where the slot ships a default (voice_chat: none); popping
- # the key would bring that default straight back.
- if _aux_default_effort(slot):
- slot_cfg["reasoning_effort"] = ""
- else:
- slot_cfg.pop("reasoning_effort", None)
- elif effort is not _UNSET:
- slot_cfg["reasoning_effort"] = effort
- aux[slot] = slot_cfg
- cfg["auxiliary"] = aux
- save_config(cfg)
- result = {"ok": True, "scope": "auxiliary", "tasks": targets, "provider": provider, "model": model}
- if effort is not _UNSET:
- result["reasoning_effort"] = effort
- return result
- def _apply_model_assignment_sync(
- scope: str, provider: str, model: str, task: str, base_url: str, api_key: str = "",
- reasoning_effort: Optional[str] = _UNSET, prepared: "Optional[tuple[str, ModelSwitchResult]]" = None,
- ):
- """Synchronous body of POST /api/model/set.
- Runs inside ``_profile_scope`` (worker thread) so every load_config/save_config lands in
- the requested profile. Raises HTTPException for validation errors. ``prepared`` is a
- ``_prepare_main_assignment`` result computed outside the config lock.
- """
- from hermes_cli.config import load_config
- cfg = load_config()
- if scope == "main":
- return _apply_main_assignment_sync(cfg, provider, model, base_url, api_key, prepared)
- return _apply_aux_assignment_sync(cfg, provider, model, task, base_url, api_key, reasoning_effort)
- def _infer_provider_on_model_change(model_val: str, prev_provider: str) -> tuple[str, str]:
- """Infer which provider serves ``model_val`` when the flat Config-page Model field changes.
- Returns ``(provider, model)``; ``provider`` is empty when no switch is warranted. Signals,
- in order: curated-catalog detection (``detect_provider_for_model``), then the vendor-slug
- heuristic — a ``vendor/model`` slug cannot belong to a non-aggregator provider (e.g.
- ``ollama-local``), so return the sentinel ``"openrouter"``; the caller's
- ``_normalize_main_model_assignment`` resolves the real aggregator (keeps the current one).
- """
- name = (model_val or "").strip()
- if not name:
- return "", name
- try:
- from hermes_cli.models import _AGGREGATOR_PROVIDERS, detect_provider_for_model, normalize_provider
- except Exception:
- return "", name
- try:
- detected = detect_provider_for_model(name, prev_provider)
- except Exception:
- detected = None
- if detected:
- return detected[0], detected[1]
- if "/" in name:
- try:
- from hermes_cli.models_detect import provider_has_credentials
- cur_is_aggregator = normalize_provider(prev_provider) in _AGGREGATOR_PROVIDERS
- # A vendor slug on a native provider is a guess at an aggregator; never guess one the
- # user has no key for — that silently writes a metered provider into config.yaml.
- if not cur_is_aggregator and provider_has_credentials("openrouter"):
- return "openrouter", name
- except Exception:
- pass
- return "", name
- def _denormalize_config_from_web(config: dict[str, Any]) -> dict[str, Any]:
- """Reverse ``_normalize_config_for_web`` before saving.
- Reconstructs ``model`` as a dict from the on-disk config to recover subkeys (provider,
- base_url, api_mode, ...) the GET response stripped. When the model name actually changed,
- re-detects the serving provider and routes through the assignment chokepoints (a user
- picking an OpenRouter model while on ``ollama-local`` would otherwise keep the stale
- provider and 404); saving unrelated fields never overwrites an explicit provider.
- ``model_context_length`` is written back as ``context_length`` (0 = auto-detect, key
- removed). A partial update (Settings autosave diff) that OMITS the key means "unchanged"
- and must leave the on-disk override alone — not be treated as an explicit 0.
- """
- from hermes_cli.config import load_config
- config = dict(config)
- config.pop("_model_meta", None)
- ctx_sent = "model_context_length" in config
- ctx_override = config.pop("model_context_length", 0)
- if not isinstance(ctx_override, int):
- try:
- ctx_override = int(ctx_override)
- except (TypeError, ValueError):
- ctx_override = 0
- model_val = config.get("model")
- has_model = isinstance(model_val, str) and bool(model_val)
- if not (has_model or ctx_sent):
- return config
- try:
- disk_cfg = load_config()
- except Exception:
- return config # can't read disk config — just use the string form
- # Only the disk READ has a fallback. A validation rejection below must propagate as its
- # HTTPException(400): swallowing it here left ``model`` a flat string, and the caller's
- # deep-merge then overwrote the whole on-disk ``model:`` dict (provider, base_url, slots).
- disk_model = disk_cfg.get("model")
- if isinstance(disk_model, dict):
- if has_model:
- prev_default = str(disk_model.get("default") or "").strip()
- prev_provider = str(disk_model.get("provider") or "").strip()
- if model_val != prev_default and prev_provider:
- new_provider, resolved_model = _infer_provider_on_model_change(model_val, prev_provider)
- if new_provider and new_provider.strip().lower() != prev_provider.lower():
- norm_provider, norm_model = _normalize_main_model_assignment(new_provider, resolved_model)
- result = _validated_main_model_selection(disk_cfg, norm_provider, norm_model)
- disk_model = _apply_main_model_assignment(disk_model, result)
- model_val = result.new_model
- disk_model["default"] = model_val
- if ctx_sent:
- if ctx_override > 0:
- disk_model["context_length"] = ctx_override
- else:
- disk_model.pop("context_length", None)
- config["model"] = disk_model
- elif ctx_sent and ctx_override > 0:
- # Model was a bare string (or absent) — upgrade to a dict for the override.
- if has_model:
- default = model_val
- elif isinstance(disk_model, str) and disk_model:
- default = disk_model
- else:
- default = ""
- config["model"] = {"default": default, "context_length": ctx_override}
- return config
|