Skip to content

The agent that grows with you

refactor(hermes_cli): model_switch/moa layout compaction (AST-verified)

Teknium2845e3faf721
diff --git a/hermes_cli/moa_cmd.py b/hermes_cli/moa_cmd.py
index d3a34dbdc1f..dcf89d17266 100644
--- a/hermes_cli/moa_cmd.py
+++ b/hermes_cli/moa_cmd.py
@@ -12,7 +12,6 @@ from hermes_cli.moa_config import DEFAULT_MOA_PRESET_NAME, normalize_moa_config
def _prompt_choice(title: str, rows: list[str], default: int = 0) -> int:
try:
from hermes_cli.curses_ui import curses_radiolist
-
return curses_radiolist(title, rows, selected=default, cancel_returns=default)
except Exception:
for idx, row in enumerate(rows, start=1):
diff --git a/hermes_cli/moa_config.py b/hermes_cli/moa_config.py
index 6cc06289532..67e14099e55 100644
--- a/hermes_cli/moa_config.py
+++ b/hermes_cli/moa_config.py
@@ -100,7 +100,6 @@ def coerce_privacy_filter(value: Any) -> str:
def _clean_reasoning_effort(value: Any) -> str | None:
"""Canonical per-slot reasoning effort, or None when unset/invalid."""
from hermes_constants import parse_reasoning_effort
-
parsed = None if value is None or value is True else parse_reasoning_effort(value)
if parsed is None:
return None
@@ -213,7 +212,8 @@ def _normalize_preset(raw: Any) -> dict[str, Any]:
if not isinstance(raw, dict):
raw = {}
- refs = [item for item in (_clean_slot(item, include_enabled=True) for item in _reference_slots(raw.get("reference_models"))) if item is not None]
+ cleaned = (_clean_slot(item, include_enabled=True) for item in _reference_slots(raw.get("reference_models")))
+ refs = [item for item in cleaned if item is not None]
policy = str(raw.get("degraded_reference_policy") or "loud").strip().lower()
return {
@@ -285,7 +285,6 @@ def resolve_moa_preset(config: Any, name: str | None = None) -> dict[str, Any]:
preset = cfg["presets"].get(preset_name)
if preset is None:
from agent.errors import MoAPresetNotFoundError
-
available = ", ".join(cfg["presets"]) or "(none)"
raise MoAPresetNotFoundError(
f"MoA preset '{preset_name}' was not found. Available presets: "
diff --git a/hermes_cli/model_switch.py b/hermes_cli/model_switch.py
index dd8f7c653f5..e6b3900f472 100644
--- a/hermes_cli/model_switch.py
+++ b/hermes_cli/model_switch.py
@@ -17,13 +17,8 @@ from dataclasses import dataclass, field
from typing import Any, NamedTuple, Optional
from hermes_cli.providers import (
- ProviderDef,
- custom_provider_aliases,
- determine_api_mode,
- get_label,
- host_mandated_api_mode,
- is_aggregator,
- resolve_provider_full)
+ ProviderDef, custom_provider_aliases, determine_api_mode, get_label, host_mandated_api_mode,
+ is_aggregator, resolve_provider_full)
from hermes_cli.model_normalize import normalize_model_for_provider
from agent.models_dev import (
ModelCapabilities, ModelInfo, get_model_capabilities, get_model_info, list_provider_models)
@@ -113,19 +108,11 @@ def _bare_custom_provider_def(current_base_url: str) -> Optional[ProviderDef]:
if not base_url:
return None
return ProviderDef(
- id="custom",
- name="Custom endpoint",
- transport="openai_chat",
- api_key_env_vars=(),
- base_url=base_url,
- is_aggregator=False,
- auth_type="api_key",
- source="model-config")
+ id="custom", name="Custom endpoint", transport="openai_chat", api_key_env_vars=(),
+ base_url=base_url, is_aggregator=False, auth_type="api_key", source="model-config")
-# ---------------------------------------------------------------------------
-# Non-agentic model warning
-# ---------------------------------------------------------------------------
+# --- Non-agentic model warning
_HERMES_MODEL_WARNING = (
"Nous Research Hermes 3 & 4 models are NOT agentic and are not designed "
@@ -164,10 +151,8 @@ def _check_hermes_model_warning(model_name: str) -> str:
return _HERMES_MODEL_WARNING if is_nous_hermes_non_agentic(model_name) else ""
-# ---------------------------------------------------------------------------
-# Model aliases -- short names -> (vendor, family) with NO version numbers,
+# --- Model aliases -- short names -> (vendor, family) with NO version numbers,
# resolved dynamically against the live models.dev catalog.
-# ---------------------------------------------------------------------------
class ModelIdentity(NamedTuple):
"""Vendor slug and family prefix used for catalog resolution."""
@@ -199,11 +184,9 @@ MODEL_ALIASES: dict[str, ModelIdentity] = {
"trinity": ModelIdentity("arcee-ai", "trinity")}
-# ---------------------------------------------------------------------------
-# Direct aliases — exact model+provider+base_url for endpoints outside the
+# --- Direct aliases — exact model+provider+base_url for endpoints outside the
# models.dev catalog (Ollama Cloud, local servers). Checked BEFORE catalog
# resolution; loaded from config.yaml ``model_aliases:`` / ``model.aliases``.
-# ---------------------------------------------------------------------------
class DirectAlias(NamedTuple):
"""Exact model mapping that bypasses catalog resolution.
@@ -272,7 +255,8 @@ def _load_direct_aliases() -> dict[str, DirectAlias]:
elif isinstance(value, str) and value.strip():
val = value.strip()
provider, model = val.split("/", 1) if "/" in val else (current_provider, val)
- merged[key] = DirectAlias(model=model.strip(), provider=provider.strip() or current_provider, base_url="")
+ merged[key] = DirectAlias(
+ model=model.strip(), provider=provider.strip() or current_provider, base_url="")
except Exception:
pass
return merged
@@ -293,7 +277,6 @@ def _direct_alias_source_identity() -> Optional[tuple]:
"""Identity of the active profile's alias source; None means "do not reuse the cache"."""
try:
from hermes_constants import get_config_path
-
path = get_config_path()
except Exception:
return None
@@ -414,7 +397,8 @@ def resolve_startup_model_route(
return StartupModelRoute(model=direct.model, provider=explicit_provider, base_url=direct.base_url)
# Same owner as the interactive /model and oneshot paths: credential for the alias HOST.
alias_provider, alias_key = direct_alias_runtime_request(direct)
- return StartupModelRoute(model=direct.model, provider=alias_provider, base_url=direct.base_url, api_key=alias_key or "")
+ return StartupModelRoute(
+ model=direct.model, provider=alias_provider, base_url=direct.base_url, api_key=alias_key or "")
if explicit_provider or "/" not in raw:
return None
@@ -425,10 +409,8 @@ def resolve_startup_model_route(
if current_provider:
try:
from hermes_cli.providers import is_routing_aggregator, normalize_provider as _norm_prov
-
if is_routing_aggregator(_norm_prov(current_provider)):
from hermes_cli.models import _find_openrouter_slug
-
if _find_openrouter_slug(raw):
return None
except Exception:
@@ -441,7 +423,6 @@ def resolve_startup_model_route(
if isinstance(entry, dict) and str(entry.get("name") or "").strip())
try:
from hermes_cli.models import normalize_provider
-
canonical = normalize_provider(prefix)
except Exception:
canonical = prefix.lower()
@@ -455,9 +436,7 @@ def resolve_startup_model_route(
return None if is_aggregator(canonical) else StartupModelRoute(model=model, provider=provider)
-# ---------------------------------------------------------------------------
-# Result dataclasses
-# ---------------------------------------------------------------------------
+# --- Result dataclasses
@dataclass
class ModelSwitchResult:
@@ -493,9 +472,7 @@ class ModelFlagParseResult:
is_once: bool = False
-# ---------------------------------------------------------------------------
-# Flag parsing
-# ---------------------------------------------------------------------------
+# --- Flag parsing
_BOOL_FLAGS = {"--global": "is_global", "--session": "is_session", "--refresh": "force_refresh", "--once": "is_once"}
@@ -554,7 +531,6 @@ def resolve_persist_behavior(
return True
try:
from hermes_cli.config import load_config
-
model_cfg = load_config().get("model")
except Exception:
return False
@@ -567,11 +543,9 @@ def resolve_persist_behavior(
return not model_cfg
-# ---------------------------------------------------------------------------
-# Single-owner /model request parsing + effective-model resolution. Surfaces
+# --- Single-owner /model request parsing + effective-model resolution. Surfaces
# (cli.py, gateway/slash_commands.py, tui_gateway/server.py, api_server.py)
# map error codes to their own copy but never re-derive the semantics.
-# ---------------------------------------------------------------------------
# Error codes emitted by parse_model_switch_args().
MODEL_SWITCH_ERR_ONCE_WITH_GLOBAL = "once_with_global"
@@ -634,15 +608,9 @@ def parse_model_switch_args(raw: str) -> ModelSwitchRequest:
("global", parsed.is_global)) if on), "default")
return ModelSwitchRequest(
- raw=raw,
- target=parsed.model_input,
- explicit_provider=parsed.explicit_provider,
- is_global=parsed.is_global,
- is_session=parsed.is_session,
- is_once=parsed.is_once,
- force_refresh=parsed.force_refresh,
- scope=scope,
- errors=tuple(errors))
+ raw=raw, target=parsed.model_input, explicit_provider=parsed.explicit_provider,
+ is_global=parsed.is_global, is_session=parsed.is_session, is_once=parsed.is_once,
+ force_refresh=parsed.force_refresh, scope=scope, errors=tuple(errors))
def _effective_model_candidate(value: Any) -> str:
@@ -670,9 +638,7 @@ def resolve_effective_model(
return ""
-# ---------------------------------------------------------------------------
-# Alias resolution
-# ---------------------------------------------------------------------------
+# --- Alias resolution
def _model_sort_key(model_id: str, prefix: str) -> tuple:
"""Sort key preferring higher versions after the family prefix, then ranked suffix tokens.
@@ -838,7 +804,8 @@ def get_authenticated_provider_slugs(
return []
-def _resolve_alias_fallback(raw_input: str, authenticated_providers: list[str] = ()) -> Optional[tuple[str, str, str]]:
+def _resolve_alias_fallback(
+ raw_input: str, authenticated_providers: list[str] = ()) -> Optional[tuple[str, str, str]]:
"""Resolve an alias on the user's authenticated providers (``("openrouter", "nous")`` when none given).
AmbiguousAliasError propagates: the alias exists on this provider, the user just has to
@@ -869,7 +836,6 @@ def resolve_display_context_length(
if config_context_length is not None and (configured_model or configured_provider or configured_base_url):
try:
from hermes_cli.route_identity import should_clear_context_pin
-
if should_clear_context_pin(
configured_model, model, configured_base_url, base_url, configured_provider, provider,
):
@@ -905,7 +871,6 @@ async def resolve_display_context_length_async(
"""Thread-offloaded :func:`resolve_display_context_length` — the sync version runs blocking
provider probes that async gateway handlers must not run on the event loop."""
import asyncio
-
return await asyncio.to_thread(
resolve_display_context_length, model, provider, base_url=base_url, api_key=api_key,
model_info=model_info, custom_providers=custom_providers, config_context_length=config_context_length,
@@ -913,9 +878,7 @@ async def resolve_display_context_length_async(
configured_base_url=configured_base_url)
-# ---------------------------------------------------------------------------
-# Configured-provider detection for typed model names
-# ---------------------------------------------------------------------------
+# --- Configured-provider detection for typed model names
def _configured_provider_matches(
model_name: str, user_providers: Optional[dict], custom_providers: Optional[list]
@@ -970,15 +933,12 @@ def _resolve_named_custom_model_id(model_name: str, target_provider: str, custom
return model_name
-# ---------------------------------------------------------------------------
-# Core model-switching pipeline
-# ---------------------------------------------------------------------------
+# --- Core model-switching pipeline
def _runtime_creds(fallback_headers: dict, **kwargs) -> tuple[str, str, str, dict]:
"""``resolve_runtime_provider`` unpacked as ``(api_key, base_url, api_mode, extra_headers)``;
``extra_headers`` falls back to *fallback_headers*."""
from hermes_cli.runtime_provider import resolve_runtime_provider
-
rt = resolve_runtime_provider(**kwargs)
return rt.get("api_key", ""), rt.get("base_url", ""), rt.get("api_mode", ""), rt.get("extra_headers") or fallback_headers
@@ -997,7 +957,6 @@ def _entry_configured_key(cfg: dict, read_env) -> str:
def _ollama_configured_base() -> tuple[dict, str]:
from hermes_cli.models import _get_provider_config_dict
-
cfg = _get_provider_config_dict("ollama")
return cfg, str(cfg.get("base_url") or cfg.get("api") or cfg.get("url") or "").strip()
@@ -1024,7 +983,6 @@ def _aggregator_alias_error(
the user onto an unauthed endpoint (HTTP 401) and point at the real direct provider."""
from hermes_cli.models import _AGGREGATOR_PROVIDERS
from hermes_cli.providers import ALIASES
-
explicit_norm = explicit_provider.strip().lower()
alias_target = ALIASES.get(explicit_norm)
if not (
@@ -1096,7 +1054,6 @@ def _apply_direct_alias_endpoint(
"""
from hermes_cli.models import _same_ollama_native_root
from hermes_cli.runtime_provider import resolve_runtime_provider
-
alias_key = direct_alias_api_key(da)
if alias_key:
base_url, api_key = da.base_url, alias_key
@@ -1141,7 +1098,6 @@ def _moa_default_preset() -> str:
try:
from hermes_cli.config import load_config
from hermes_cli.moa_config import normalize_moa_config
-
return normalize_moa_config(load_config().get("moa") or {})["default_preset"]
except Exception:
return "default"
@@ -1243,7 +1199,8 @@ def _route_alias_fallback(st: _Switch, key: str) -> Optional[ModelSwitchResult]:
f"but no matching model was found in any provider catalog. "
f"Try specifying the full model name.")
st.target_provider, st.new_model, st.resolved_alias = fallback_result
- logger.debug("Alias '%s' resolved via fallback to %s on %s", st.resolved_alias, st.new_model, st.target_provider)
+ logger.debug(
+ "Alias '%s' resolved via fallback to %s on %s", st.resolved_alias, st.new_model, st.target_provider)
return None
@@ -1298,12 +1255,10 @@ def _route_from_model_input(st: _Switch) -> Optional[ModelSwitchResult]:
fallback (b) or ``vendor:model`` conversion (c) -> aggregator catalog search (d) ->
configured-provider match (d.5) -> detect_provider_for_model() as last resort (e)."""
from hermes_cli.models import detect_provider_for_model
-
raw_input, current_provider = st.raw_input, st.current_provider
try:
from hermes_cli.config import load_config
from hermes_cli.moa_config import exact_moa_preset_name, normalize_moa_config
-
moa_match = exact_moa_preset_name(normalize_moa_config(load_config().get("moa") or {}), raw_input)
except Exception:
moa_match = None # MoA config unreadable: fall through to plain alias resolution
@@ -1390,11 +1345,8 @@ def _creds_for_switched_provider(st: _Switch) -> Optional[ModelSwitchResult]:
st.validation_headers = _extra_headers_from_config(ucfg)
try:
api_key, base_url, st.api_mode, st.validation_headers = _runtime_creds(
- st.validation_headers,
- requested=st.target_provider,
- explicit_api_key=ukey or None,
- explicit_base_url=user_pdef.base_url,
- target_model=st.new_model)
+ st.validation_headers, requested=st.target_provider, explicit_api_key=ukey or None,
+ explicit_base_url=user_pdef.base_url, target_model=st.new_model)
st.api_key = api_key or ukey
st.base_url = base_url or user_pdef.base_url
except Exception:
@@ -1418,7 +1370,6 @@ def _creds_for_current_provider(st: _Switch) -> None:
Ollama-compatible endpoint keeps the endpoint in use; re-resolving bare ``custom`` from config
can fall through to an unrelated default provider."""
from hermes_cli.models import _get_ollama_request_headers, _same_ollama_native_root
-
keep_current_ollama_endpoint = False
ollama_headers: dict[str, str] = {}
if st.current_provider == "custom" and st.current_base_url:
@@ -1488,7 +1439,6 @@ def _validate_switch(st: _Switch) -> Optional[ModelSwitchResult]:
"""COMMON PATH part 2: normalize the model name for the target provider, validate it, and
accept config-declared models the remote catalog lacks."""
from hermes_cli.models import _get_ollama_request_headers, validate_requested_model
-
st.new_model = _resolve_named_custom_model_id(st.new_model, st.target_provider, st.custom_providers)
st.new_model = normalize_model_for_provider(st.new_model, st.target_provider)
@@ -1523,13 +1473,11 @@ def _validate_switch(st: _Switch) -> Optional[ModelSwitchResult]:
def _copilot_api_mode(provider: str, model: str, api_key: str) -> str:
from hermes_cli.models import copilot_model_api_mode
-
return copilot_model_api_mode(model, api_key=api_key)
def _opencode_api_mode(provider: str, model: str, api_key: str) -> str:
from hermes_cli.models import opencode_model_api_mode
-
return opencode_model_api_mode(provider, model)
@@ -1538,7 +1486,6 @@ def _nous_api_mode(provider: str, model: str, api_key: str) -> str:
# re-derive from the FINAL model so alias clears / empty fallbacks cannot leave Claude on the
# OpenAI wire.
from hermes_cli.providers import nous_api_mode
-
return nous_api_mode(model)
@@ -1568,9 +1515,7 @@ def _build_switch_result(st: _Switch) -> ModelSwitchResult:
capabilities = get_model_capabilities(st.target_provider, st.new_model, allow_network=True)
from agent.native_compaction import resolve_native_compaction_capabilities
runtime_capabilities = resolve_native_compaction_capabilities(
- model=st.new_model,
- base_url=st.base_url,
- provider=st.target_provider,
+ model=st.new_model, base_url=st.base_url, provider=st.target_provider,
is_codex_backend=st.target_provider.strip().lower() == "openai-codex")
model_info = get_model_info(st.target_provider, st.new_model, allow_network=True)
@@ -1606,15 +1551,9 @@ def _build_switch_result(st: _Switch) -> ModelSwitchResult:
def switch_model(
- raw_input: str,
- current_provider: str,
- current_model: str,
- current_base_url: str = "",
- current_api_key: str = "",
- is_global: bool = False,
- explicit_provider: str = "",
- user_providers: dict = None,
- custom_providers: list | None = None) -> ModelSwitchResult:
+ raw_input: str, current_provider: str, current_model: str, current_base_url: str = "",
+ current_api_key: str = "", is_global: bool = False, explicit_provider: str = "",
+ user_providers: dict = None, custom_providers: list | None = None) -> ModelSwitchResult:
"""Core model-switching pipeline shared between CLI and gateway.
Route (:func:`_route_explicit_provider` when ``--provider`` was given, else
@@ -1640,7 +1579,6 @@ def _extra_headers_from_config(entry: Any) -> dict[str, str]:
if not isinstance(entry, dict):
return {}
from hermes_cli.config import normalize_extra_headers
-
return normalize_extra_headers(entry.get("extra_headers"))
@@ -1655,7 +1593,6 @@ def _scoped_key_env(name: str) -> str:
"""
try:
from agent.secret_scope import get_secret
-
return (get_secret(name, "") or "").strip() if name else ""
except Exception:
return ""
diff --git a/hermes_cli/model_switch_providers.py b/hermes_cli/model_switch_providers.py
index 00e7ee80d9d..71f4b486c5e 100644
--- a/hermes_cli/model_switch_providers.py
+++ b/hermes_cli/model_switch_providers.py
@@ -25,8 +25,8 @@ _UNCAPPED_PICKER_PROVIDERS: frozenset[str] = frozenset({"opencode-zen", "opencod
def _save_discovered_models_to_config(
- api_url: str, model_ids: list[str], *, api_mode: Optional[str] = None, headers: Optional[dict[str, str]] = None,
-) -> None:
+ api_url: str, model_ids: list[str], *, api_mode: Optional[str] = None,
+ headers: Optional[dict[str, str]] = None) -> None:
"""Persist a successful ``/v1/models`` probe into the matching ``custom_providers`` entry.
Matches by base_url (slash-normalised), api_mode and headers. A failed config write is
@@ -37,7 +37,6 @@ def _save_discovered_models_to_config(
return
try:
from hermes_cli.config import load_config, save_config
-
cfg = load_config()
providers = cfg.get("custom_providers") or []
if not isinstance(providers, list):
@@ -67,7 +66,8 @@ def _save_discovered_models_to_config(
# migrates it to the clean entry-level flag.
if isinstance(existing, list) and existing == model_ids:
continue
- if isinstance(existing, dict) and entry_discovered and not legacy_discovered and list(existing) == model_ids:
+ if (isinstance(existing, dict) and entry_discovered and not legacy_discovered
+ and list(existing) == model_ids):
continue
entry["models"] = {model_id: {} for model_id in model_ids}
entry["models_discovered"] = True
@@ -99,11 +99,8 @@ def _fetch_picker_live_models(
api_mode: str | None = None) -> list[str] | None:
"""Fetch picker models with native Ollama and cached generic discovery."""
from hermes_cli.models import (
- _get_ollama_native_headers,
- _normalize_openai_base_url,
- cached_fetch_api_models,
- fetch_ollama_local_models,
- should_use_ollama_native_catalog)
+ _get_ollama_native_headers, _normalize_openai_base_url, cached_fetch_api_models,
+ fetch_ollama_local_models, should_use_ollama_native_catalog)
candidate_headers = _get_ollama_native_headers(api_url, api_key=api_key)
@@ -120,7 +117,8 @@ def _fetch_picker_live_models(
if api_key and not caller_has_authorization:
_drop(lambda k: k == "authorization")
candidate_headers["Authorization"] = f"Bearer {api_key}"
- use_native = should_use_ollama_native_catalog(native_catalog_provider, api_url, headers=candidate_headers or None)
+ use_native = should_use_ollama_native_catalog(
+ native_catalog_provider, api_url, headers=candidate_headers or None)
resolved_headers = candidate_headers or None if use_native else headers
if use_native:
@@ -131,7 +129,8 @@ def _fetch_picker_live_models(
return _NativePickerModelList(native_models)
# A failed native probe is not authoritative: retry the cached generic catalog.
api_url = _normalize_openai_base_url(api_url)
- generic_models = cached_fetch_api_models(api_key, api_url, timeout=timeout, headers=resolved_headers, api_mode=api_mode)
+ generic_models = cached_fetch_api_models(
+ api_key, api_url, timeout=timeout, headers=resolved_headers, api_mode=api_mode)
return generic_models if generic_models or use_native else None
@@ -149,7 +148,6 @@ def _credential_pool_is_usable(provider: str, *, raw_pool_present: bool = False)
"""
try:
from agent.credential_pool import load_pool
-
pool = load_pool(provider)
if pool.has_credentials():
return pool.has_available()
@@ -173,14 +171,11 @@ def prewarm_picker_cache_async() -> Optional["_threading.Thread"]:
def _warm() -> None:
try:
from hermes_cli.inventory import load_picker_context
-
ctx = load_picker_context()
# The result is discarded; the warm disk cache is the point.
list_authenticated_providers(
- current_provider=ctx.current_provider,
- current_base_url=ctx.current_base_url,
- current_model=ctx.current_model,
- user_providers=ctx.user_providers,
+ current_provider=ctx.current_provider, current_base_url=ctx.current_base_url,
+ current_model=ctx.current_model, user_providers=ctx.user_providers,
custom_providers=ctx.custom_providers,
excluded_providers=ctx.excluded_providers or [])
except Exception:
@@ -204,11 +199,8 @@ def _prefetch_provider_models_parallel(provider_slugs: list[str]) -> None:
silently skipped.
"""
from hermes_cli.models import (
- _PROVIDER_MODELS_CACHE_TTL,
- _credential_fingerprint,
- _load_provider_models_cache,
- cached_provider_model_ids,
- normalize_provider)
+ _PROVIDER_MODELS_CACHE_TTL, _credential_fingerprint, _load_provider_models_cache,
+ cached_provider_model_ids, normalize_provider)
# Read-only staleness check mirroring cached_provider_model_ids (which re-reads the cache
# itself, so a concurrent change between check and fetch is harmless).
@@ -233,7 +225,6 @@ def _prefetch_provider_models_parallel(provider_slugs: list[str]) -> None:
return
import concurrent.futures
-
def _fetch_one(slug: str) -> None:
try:
models = cached_provider_model_ids(slug, force_refresh=True)
@@ -274,7 +265,6 @@ def _iter_builtin_candidates(models_dev_data: dict, excluded: set, seen: set):
from hermes_cli.auth import PROVIDER_REGISTRY, is_runtime_provider_routable
from hermes_cli.models import _AGGREGATOR_PROVIDERS
from hermes_cli.providers import ALIASES
-
for hermes_id, mdev_id in PROVIDER_TO_MODELS_DEV.items():
alias_target = ALIASES.get(hermes_id)
if alias_target and alias_target != hermes_id and alias_target in _AGGREGATOR_PROVIDERS:
@@ -347,7 +337,6 @@ def _overlay_has_env_creds(pid: str, hermes_slug: str, overlay, read_env) -> boo
own probe; otherwise the provider is hidden from the picker even when fully configured.
"""
from hermes_cli.auth import PROVIDER_REGISTRY
-
has_creds = False
if overlay.auth_type == "vertex":
try:
@@ -406,7 +395,6 @@ def _live_or_curated_ids(slug: str, curated: dict, *fallback_keys: str, merge_mo
"""``cached_provider_model_ids`` (the SAME disk-cached list ``hermes model`` builds), falling
back to the curated list (merged with models.dev for preferred providers) when live is empty."""
from hermes_cli.models import _MODELS_DEV_PREFERRED, _merge_with_models_dev, cached_provider_model_ids
-
model_ids = cached_provider_model_ids(slug)
if not model_ids:
model_ids = _first_curated(curated, fallback_keys or (slug,))
@@ -429,7 +417,6 @@ def _aws_live_or_curated_ids(slug: str, curated: dict, *fallback_keys: str) -> l
"""Bedrock: live discovery reflects the active region (eu.*, ap.*) rather than the static
us.* list; any failure falls back to the curated list."""
from hermes_cli.models import cached_provider_model_ids
-
try:
ids = cached_provider_model_ids(slug)
if ids:
@@ -447,12 +434,9 @@ def _nous_picker_model_ids(curated: dict, force_fresh_nous_tier: bool) -> list:
model_ids = curated.get("nous", [])
try:
from hermes_cli.models import (
- get_pricing_for_provider,
- check_nous_free_tier,
- union_with_portal_free_recommendations,
+ get_pricing_for_provider, check_nous_free_tier, union_with_portal_free_recommendations,
union_with_portal_paid_recommendations)
from hermes_cli.auth import get_provider_auth_state
-
pricing = get_pricing_for_provider("nous") or {}
try:
portal = (get_provider_auth_state("nous") or {}).get("portal_base_url", "") or ""
@@ -466,7 +450,6 @@ def _nous_picker_model_ids(curated: dict, force_fresh_nous_tier: bool) -> list:
pass
try:
from hermes_cli.models import nous_policy_allowed_ids, restrict_to_nous_policy
-
model_ids = restrict_to_nous_policy(model_ids, nous_policy_allowed_ids(), rescue_empty=True)
except Exception:
pass
@@ -517,15 +500,8 @@ def _display_prefix(name: str) -> str:
def _discover_endpoint_models(
- api_key: str,
- api_url: str,
- native_catalog_provider: str,
- has_explicit_models: bool,
- *,
- headers: dict | None,
- api_mode: str | None,
- probe_live: bool,
- discovery_allowed: bool,
+ api_key: str, api_url: str, native_catalog_provider: str, has_explicit_models: bool, *,
+ headers: dict | None, api_mode: str | None, probe_live: bool, discovery_allowed: bool,
for_picker: bool) -> tuple[list | None, bool]:
"""Return ``(models, native_catalog_empty)`` for a custom endpoint row.
@@ -550,7 +526,6 @@ def _discover_endpoint_models(
elif discovery_allowed:
try:
from hermes_cli.models import cached_fetch_api_models
-
cached_models = cached_fetch_api_models(
api_key, api_url, cache_only=True, timeout=timeout, headers=headers, api_mode=api_mode,
)
@@ -561,7 +536,8 @@ def _discover_endpoint_models(
return None, False
-def _collect_authed_provider_slugs(models_dev_data: dict, curated: dict[str, list[str]], excluded: list[str]) -> list[str]:
+def _collect_authed_provider_slugs(
+ models_dev_data: dict, curated: dict[str, list[str]], excluded: list[str]) -> list[str]:
"""Quick-scan which providers have credentials, without fetching model lists.
Mirrors the credential checks of sections 1, 2 and 2b of :func:`list_authenticated_providers`
@@ -574,7 +550,6 @@ def _collect_authed_provider_slugs(models_dev_data: dict, curated: dict[str, lis
from hermes_cli.auth import PROVIDER_REGISTRY
from hermes_cli.providers import HERMES_OVERLAYS
from hermes_cli.models import CANONICAL_PROVIDERS
-
excluded_set = {str(p).strip().lower() for p in excluded if p}
slugs: list[str] = []
seen: set[str] = set()
@@ -602,7 +577,8 @@ def _collect_authed_provider_slugs(models_dev_data: dict, curated: dict[str, lis
if _skip(seen, excluded_set, cp.slug):
continue
cp_config = PROVIDER_REGISTRY.get(cp.slug)
- has_creds = bool(cp_config and cp_config.api_key_env_vars and _any_env(cp_config.api_key_env_vars, _scoped_key_env))
+ has_creds = bool(
+ cp_config and cp_config.api_key_env_vars and _any_env(cp_config.api_key_env_vars, _scoped_key_env))
if has_creds or _auth_store_has_provider(cp.slug) or _pool_usable(cp.slug):
_emit(cp.slug, cp.slug)
@@ -725,7 +701,6 @@ def _lap_builtin_rows(b: _PickerBuild, data: dict, user_providers: dict) -> None
"""Section 1: models.dev-mapped providers with api_key auth."""
from hermes_cli.model_switch import _declared_model_ids
from agent.models_dev import get_provider_info
-
for hermes_id, mdev_id, pconfig, env_vars in _iter_builtin_candidates(data, b.excluded, b.seen_slugs):
if not (_any_env(env_vars) or _raw_pool_usable(hermes_id)):
continue
@@ -737,14 +712,14 @@ def _lap_builtin_rows(b: _PickerBuild, data: dict, user_providers: dict) -> None
model_ids = list(dict.fromkeys([*configured_models, *model_ids]))
pinfo = get_provider_info(mdev_id)
display_name = pconfig.name if pconfig and pconfig.name else (pinfo.name if pinfo else mdev_id)
- b.add_builtin_row(hermes_id, display_name, b.current_provider in (hermes_id, mdev_id), model_ids, "built-in")
+ b.add_builtin_row(
+ hermes_id, display_name, b.current_provider in (hermes_id, mdev_id), model_ids, "built-in")
def _overlay_has_creds(b: _PickerBuild, pid: str, hermes_slug: str, overlay) -> bool:
"""Section-2 credential ladder: env/SDK, external-process executable, auth store, pool,
anthropic's external credential files."""
from hermes_cli.model_switch import _credential_pool_is_usable
-
if getattr(overlay, "keyless", False):
return True # served anonymously (opencode-free)
if overlay.auth_type == "aws_sdk":
@@ -804,7 +779,9 @@ def _lap_overlay_rows(b: _PickerBuild, data: dict) -> None:
mdev_to_hermes = {v: k for k, v in PROVIDER_TO_MODELS_DEV.items()}
for pid, overlay in HERMES_OVERLAYS.items():
hermes_slug = mdev_to_hermes.get(pid, pid)
- if _skip(b.seen_slugs, b.excluded, pid, hermes_slug) or not _overlay_has_creds(b, pid, hermes_slug, overlay):
+ if _skip(b.seen_slugs, b.excluded, pid, hermes_slug):
+ continue
+ if not _overlay_has_creds(b, pid, hermes_slug, overlay):
continue
if hermes_slug in {"openai-codex", "copilot", "copilot-acp"}:
# Live OAuth-backed discovery so Pro-only Codex slugs not in the static catalog
@@ -817,7 +794,8 @@ def _lap_overlay_rows(b: _PickerBuild, data: dict) -> None:
model_ids = _nous_picker_model_ids(b.curated, b.force_fresh_nous_tier)
else:
model_ids = _live_or_curated_ids(hermes_slug, b.curated, hermes_slug, pid)
- b.add_builtin_row(hermes_slug, get_label(hermes_slug), b.current_provider in (hermes_slug, pid), model_ids, "hermes")
+ b.add_builtin_row(
+ hermes_slug, get_label(hermes_slug), b.current_provider in (hermes_slug, pid), model_ids, "hermes")
b.seen_slugs.add(pid.lower())
@@ -854,7 +832,8 @@ def _lap_canonical_rows(b: _PickerBuild) -> None:
model_ids = _aws_live_or_curated_ids(cp.slug, b.curated)
else:
model_ids = _live_or_curated_ids(cp.slug, b.curated, merge_models_dev=False)
- b.add_builtin_row(cp.slug, cp.label, cp.slug == b.current_provider, model_ids, "canonical", uncapped_ok=False)
+ b.add_builtin_row(
+ cp.slug, cp.label, cp.slug == b.current_provider, model_ids, "canonical", uncapped_ok=False)
def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
@@ -864,7 +843,6 @@ def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
key_env/api_mode/headers keeps distinct rows since the wire protocol or tenant differs."""
from hermes_cli.model_switch import _declared_model_ids, _entry_models_discovered, _extra_headers_from_config, _models_config_is_allowlist, _scoped_key_env
from hermes_cli.config import coerce_provider_id, is_provider_enabled
-
ep_groups: dict[tuple, dict] = {}
for ep_name, ep_cfg in user_providers.items():
if not isinstance(ep_cfg, dict) or not is_provider_enabled(ep_cfg) or ep_name.lower() in b.seen_slugs:
@@ -874,7 +852,9 @@ def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
key_env = _entry_key_env(ep_cfg, "key_env", "api_key_env")
inline_api_key = str(ep_cfg.get("api_key", "") or "").strip()
headers = _extra_headers_from_config(ep_cfg)
- group_key = (_norm_url(api_url), _credential_identity(inline_api_key, key_env), _entry_api_mode(ep_cfg), tuple(sorted(headers.items())))
+ group_key = (
+ _norm_url(api_url), _credential_identity(inline_api_key, key_env), _entry_api_mode(ep_cfg),
+ tuple(sorted(headers.items())))
# ``default_model`` is the legacy key; ``model`` matches custom_providers.
default_model = ep_cfg.get("default_model", "") or ep_cfg.get("model", "")
@@ -931,8 +911,7 @@ def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
discovered, native_catalog_empty, _ = b.discover_endpoint(
grp["api_key"], api_url,
ep_name if str(ep_name).strip().lower() in {"ollama", "custom:ollama"} else "custom",
- grp["has_explicit_models"],
- headers=grp["headers"] or None, api_mode=grp["api_mode"],
+ grp["has_explicit_models"], headers=grp["headers"] or None, api_mode=grp["api_mode"],
discovery_allowed=grp["discovery_allowed"], is_current=is_current)
if discovered is not None:
models_list = discovered
@@ -967,7 +946,8 @@ def _lap_bare_custom_row(b: _PickerBuild, custom_providers: list | None) -> None
try:
discovered, native_catalog_empty = _discover_endpoint_models(
"", api_url, "custom", False, headers=None, api_mode=None,
- probe_live=bool(b.refresh or b.probe_current_custom_provider), discovery_allowed=True, for_picker=b.for_picker,
+ probe_live=bool(b.refresh or b.probe_current_custom_provider), discovery_allowed=True,
+ for_picker=b.for_picker,
)
if discovered is not None:
models = discovered
@@ -985,7 +965,6 @@ def _lap_custom_provider_rows(b: _PickerBuild, custom_providers: list) -> None:
their own rows."""
from hermes_cli.model_switch import _declared_model_ids, _entry_models_discovered, _extra_headers_from_config, _models_config_is_allowlist, _save_discovered_models_to_config, _scoped_key_env
from hermes_cli.config import coerce_provider_id
-
groups: dict[tuple, dict] = {}
for entry in custom_providers:
if not isinstance(entry, dict):
@@ -1039,7 +1018,8 @@ def _lap_custom_provider_rows(b: _PickerBuild, custom_providers: list) -> None:
section4_slugs: set = set()
current_url_group_count = sum(
- 1 for grp in groups.values() if b.current_base_url_norm and _norm_url(grp["api_url"]) == b.current_base_url_norm
+ 1 for grp in groups.values()
+ if b.current_base_url_norm and _norm_url(grp["api_url"]) == b.current_base_url_norm
)
for grp in groups.values():
api_url, api_key, slug = grp["api_url"], grp.get("api_key", ""), grp["slug"]
@@ -1062,7 +1042,8 @@ def _lap_custom_provider_rows(b: _PickerBuild, custom_providers: list) -> None:
if grp_url_norm and grp_url_norm in b.builtin_endpoints:
continue
is_current = b.endpoint_is_current(
- slug, {str(alias).lower() for alias in grp["aliases"]}, grp_url_norm, url_match_ok=current_url_group_count == 1,
+ slug, {str(alias).lower() for alias in grp["aliases"]}, grp_url_norm,
+ url_match_ok=current_url_group_count == 1,
)
discovered, native_catalog_empty, probe_live = b.discover_endpoint(
api_key, api_url,
@@ -1089,7 +1070,6 @@ def _build_curated_lists(current_provider: str, current_base_url: str, current_m
"""Curated model lists keyed by hermes provider id, plus the dynamic ones (nous manifest,
Ollama Cloud, LM Studio live probe)."""
from hermes_cli.models import OPENROUTER_MODELS, _PROVIDER_MODELS, get_curated_nous_model_ids
-
curated: dict[str, list[str]] = dict(_PROVIDER_MODELS)
curated["openrouter"] = [mid for mid, _ in OPENROUTER_MODELS]
# Remote manifest so new Portal models surface without a release; in-repo snapshot fallback.
@@ -1119,19 +1099,11 @@ def _build_curated_lists(current_provider: str, current_base_url: str, current_m
def list_authenticated_providers(
- current_provider: str = "",
- current_base_url: str = "",
- user_providers: dict = None,
- custom_providers: list | None = None,
- *,
- force_fresh_nous_tier: bool = False,
- max_models: int | None = None,
- current_model: str = "",
- refresh: bool = False,
- probe_custom_providers: bool = True,
- probe_current_custom_provider: bool = False,
- for_picker: bool = False,
- excluded_providers: list | None = None) -> List[dict]:
+ current_provider: str = "", current_base_url: str = "", user_providers: dict = None,
+ custom_providers: list | None = None, *, force_fresh_nous_tier: bool = False,
+ max_models: int | None = None, current_model: str = "", refresh: bool = False,
+ probe_custom_providers: bool = True, probe_current_custom_provider: bool = False,
+ for_picker: bool = False, excluded_providers: list | None = None) -> List[dict]:
"""Detect which providers have credentials and list their curated models.
Uses the hand-picked agentic lists from hermes_cli/models.py, NOT the full models.dev
@@ -1248,7 +1220,6 @@ def _prepend_moa_picker_provider(providers: List[dict], current_provider: str =
"""
try:
from hermes_cli.inventory import _moa_provider_row
-
moa_row = _moa_provider_row(current_provider)
if moa_row is None:
return providers
@@ -1258,14 +1229,9 @@ def _prepend_moa_picker_provider(providers: List[dict], current_provider: str =
def list_picker_providers(
- current_provider: str = "",
- current_base_url: str = "",
- user_providers: dict = None,
- custom_providers: list | None = None,
- max_models: int | None = None,
- current_model: str = "",
- include_moa: bool = False,
- excluded_providers: list | None = None) -> List[dict]:
+ current_provider: str = "", current_base_url: str = "", user_providers: dict = None,
+ custom_providers: list | None = None, max_models: int | None = None, current_model: str = "",
+ include_moa: bool = False, excluded_providers: list | None = None) -> List[dict]:
"""Interactive-picker variant of :func:`list_authenticated_providers`.
Narrows the payload to models actually callable in this install: OpenRouter's list is
@@ -1276,16 +1242,10 @@ def list_picker_providers(
"""
from hermes_cli.model_switch import list_authenticated_providers
from hermes_cli.models import fetch_openrouter_models
-
providers = list_authenticated_providers(
- current_provider=current_provider,
- current_base_url=current_base_url,
- user_providers=user_providers,
- custom_providers=custom_providers,
- max_models=max_models,
- current_model=current_model,
- for_picker=True,
- excluded_providers=excluded_providers)
+ current_provider=current_provider, current_base_url=current_base_url,
+ user_providers=user_providers, custom_providers=custom_providers, max_models=max_models,
+ current_model=current_model, for_picker=True, excluded_providers=excluded_providers)
if include_moa:
providers = _prepend_moa_picker_provider(providers, current_provider=current_provider)