refactor(hermes_cli): model_switch/moa pass 2 (row/result packing, finalize helper)
This commit is contained in:
+32
-64
@@ -23,16 +23,11 @@ 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)
|
||||
from utils import base_url_hostname, base_url_origin
|
||||
from hermes_cli.model_switch_providers import ( # noqa: F401 (re-exported; callers/tests patch hermes_cli.model_switch.<name>)
|
||||
_NativePickerModelList,
|
||||
_collect_authed_provider_slugs,
|
||||
_credential_pool_is_usable,
|
||||
_fetch_picker_live_models,
|
||||
_picker_prewarm_done,
|
||||
_prefetch_provider_models_parallel,
|
||||
_save_discovered_models_to_config,
|
||||
list_authenticated_providers,
|
||||
list_picker_providers,
|
||||
# Re-exported: callers/tests patch hermes_cli.model_switch.<name>.
|
||||
from hermes_cli.model_switch_providers import ( # noqa: F401
|
||||
_NativePickerModelList, _collect_authed_provider_slugs, _credential_pool_is_usable,
|
||||
_fetch_picker_live_models, _picker_prewarm_done, _prefetch_provider_models_parallel,
|
||||
_save_discovered_models_to_config, list_authenticated_providers, list_picker_providers,
|
||||
prewarm_picker_cache_async)
|
||||
|
||||
|
||||
@@ -41,36 +36,33 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
def _declared_model_ids(value: Any) -> list[str]:
|
||||
"""Configured model IDs from ``{"id": {...}}``, ``["a", "b"]``, ``[{"id"|"name": ...}]`` or ``"a"``."""
|
||||
if isinstance(value, str):
|
||||
candidates: Any = [value]
|
||||
elif isinstance(value, dict):
|
||||
# Pre-fix Hermes wrote sentinel keys inside the user-facing ``models`` mapping.
|
||||
candidates = (k for k in value if k not in ("__explicit_model_allowlist__", "__discovered_model_catalog__"))
|
||||
elif isinstance(value, (list, tuple)):
|
||||
candidates = (_declared_item_id(item) if isinstance(item, dict) else item for item in value)
|
||||
else:
|
||||
return []
|
||||
ids: list[str] = []
|
||||
seen: set[str] = set()
|
||||
|
||||
def _add(candidate: Any) -> None:
|
||||
for candidate in candidates:
|
||||
if not isinstance(candidate, str):
|
||||
return
|
||||
continue # non-str items are dropped
|
||||
model_id = candidate.strip()
|
||||
if model_id and model_id.lower() not in seen:
|
||||
seen.add(model_id.lower())
|
||||
ids.append(model_id)
|
||||
|
||||
if isinstance(value, str):
|
||||
_add(value)
|
||||
elif isinstance(value, dict):
|
||||
# Pre-fix Hermes wrote sentinel keys inside the user-facing ``models`` mapping.
|
||||
for model_id in value:
|
||||
if model_id not in ("__explicit_model_allowlist__", "__discovered_model_catalog__"):
|
||||
_add(model_id)
|
||||
elif isinstance(value, (list, tuple)):
|
||||
for item in value:
|
||||
if isinstance(item, dict):
|
||||
model_id = item.get("id")
|
||||
if not isinstance(model_id, str) or not model_id.strip():
|
||||
model_id = item.get("name")
|
||||
_add(model_id)
|
||||
else:
|
||||
_add(item) # non-str items are dropped by _add
|
||||
return ids
|
||||
|
||||
|
||||
def _declared_item_id(item: dict) -> Any:
|
||||
"""``id`` of a ``[{"id": ...}]`` entry, falling back to ``name`` when blank/missing."""
|
||||
model_id = item.get("id")
|
||||
return model_id if isinstance(model_id, str) and model_id.strip() else item.get("name")
|
||||
|
||||
|
||||
def _entry_models_discovered(entry: Any) -> bool:
|
||||
"""True when the entry's ``models`` mapping was auto-discovered by Hermes.
|
||||
|
||||
@@ -857,25 +849,11 @@ def resolve_display_context_length(
|
||||
return None
|
||||
|
||||
|
||||
async def resolve_display_context_length_async(
|
||||
model: str,
|
||||
provider: str,
|
||||
base_url: str = "",
|
||||
api_key: str = "",
|
||||
model_info: Optional[ModelInfo] = None,
|
||||
custom_providers: list | None = None,
|
||||
config_context_length: int | None = None,
|
||||
configured_model: str | None = None,
|
||||
configured_provider: str | None = None,
|
||||
configured_base_url: str | None = None) -> Optional[int]:
|
||||
"""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."""
|
||||
async def resolve_display_context_length_async(model: str, provider: str, **kwargs) -> Optional[int]:
|
||||
"""Thread-offloaded :func:`resolve_display_context_length` (same keyword arguments) — the sync
|
||||
version runs blocking provider probes that async gateway handlers must not run on the 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,
|
||||
configured_model=configured_model, configured_provider=configured_provider,
|
||||
configured_base_url=configured_base_url)
|
||||
return await asyncio.to_thread(resolve_display_context_length, model, provider, **kwargs)
|
||||
|
||||
|
||||
# --- Configured-provider detection for typed model names
|
||||
@@ -1532,22 +1510,12 @@ def _build_switch_result(st: _Switch) -> ModelSwitchResult:
|
||||
request_overrides = None
|
||||
|
||||
return ModelSwitchResult(
|
||||
success=True,
|
||||
new_model=st.new_model,
|
||||
target_provider=st.target_provider,
|
||||
provider_changed=st.provider_changed,
|
||||
api_key=st.api_key,
|
||||
base_url=st.base_url,
|
||||
api_mode=st.api_mode,
|
||||
request_overrides=dict(request_overrides or {}),
|
||||
warning_message=" | ".join(warnings) if warnings else "",
|
||||
provider_label=st.provider_label,
|
||||
resolved_via_alias=st.resolved_alias,
|
||||
capabilities=capabilities,
|
||||
runtime_capabilities={k: v for k, v in runtime_capabilities.items()
|
||||
if isinstance(k, str) and isinstance(v, bool)},
|
||||
model_info=model_info,
|
||||
is_global=st.is_global)
|
||||
success=True, new_model=st.new_model, target_provider=st.target_provider,
|
||||
provider_changed=st.provider_changed, api_key=st.api_key, base_url=st.base_url, api_mode=st.api_mode,
|
||||
request_overrides=dict(request_overrides or {}), warning_message=" | ".join(warnings) if warnings else "",
|
||||
provider_label=st.provider_label, resolved_via_alias=st.resolved_alias, capabilities=capabilities,
|
||||
runtime_capabilities={k: v for k, v in runtime_capabilities.items() if isinstance(k, str) and isinstance(v, bool)},
|
||||
model_info=model_info, is_global=st.is_global)
|
||||
|
||||
|
||||
def switch_model(
|
||||
|
||||
@@ -360,18 +360,16 @@ def _has_fast_aws_sdk_signal() -> bool:
|
||||
Deliberately avoids botocore's full credential chain: picker discovery runs for non-Bedrock
|
||||
providers too, and botocore may probe EC2 IMDS (169.254.169.254) before giving up.
|
||||
"""
|
||||
env = os.environ
|
||||
if env.get("AWS_BEARER_TOKEN_BEDROCK", "").strip():
|
||||
return True
|
||||
if env.get("AWS_ACCESS_KEY_ID", "").strip() and env.get("AWS_SECRET_ACCESS_KEY", "").strip():
|
||||
return True
|
||||
return any(
|
||||
env.get(name, "").strip()
|
||||
for name in (
|
||||
"AWS_PROFILE",
|
||||
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",
|
||||
"AWS_CONTAINER_CREDENTIALS_FULL_URI",
|
||||
"AWS_WEB_IDENTITY_TOKEN_FILE"))
|
||||
def _set(name: str) -> bool:
|
||||
return bool(os.environ.get(name, "").strip())
|
||||
|
||||
return (
|
||||
_set("AWS_BEARER_TOKEN_BEDROCK")
|
||||
or (_set("AWS_ACCESS_KEY_ID") and _set("AWS_SECRET_ACCESS_KEY"))
|
||||
or any(_set(name) for name in (
|
||||
"AWS_PROFILE", "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI",
|
||||
"AWS_CONTAINER_CREDENTIALS_FULL_URI", "AWS_WEB_IDENTITY_TOKEN_FILE"))
|
||||
)
|
||||
|
||||
|
||||
def _has_aws_sdk_creds_for_listing(slug: str, current_provider: str) -> bool:
|
||||
@@ -638,15 +636,13 @@ class _PickerBuild:
|
||||
if normed:
|
||||
self.builtin_endpoints.add(normed)
|
||||
|
||||
def add_builtin_row(self, slug: str, name: str, is_current: bool, model_ids: list, source: str, *, uncapped_ok: bool = True) -> None:
|
||||
def add_builtin_row(
|
||||
self, slug: str, name: str, is_current: bool, model_ids: list, source: str, *, uncapped_ok: bool = True,
|
||||
) -> None:
|
||||
self.results.append({
|
||||
"slug": slug,
|
||||
"name": name,
|
||||
"is_current": is_current,
|
||||
"is_user_defined": False,
|
||||
"slug": slug, "name": name, "is_current": is_current, "is_user_defined": False,
|
||||
"models": _cap_models(model_ids, self.max_models, slug if uncapped_ok else ""),
|
||||
"total_models": len(model_ids),
|
||||
"source": source})
|
||||
"total_models": len(model_ids), "source": source})
|
||||
self.seen_slugs.add(slug.lower())
|
||||
self.record_builtin_endpoint(slug)
|
||||
|
||||
@@ -655,15 +651,9 @@ class _PickerBuild:
|
||||
*, source: str = "user-config", shown: list | None = None) -> None:
|
||||
"""Append a user-defined endpoint row (sections 3, 3b, 4)."""
|
||||
self.results.append({
|
||||
"slug": slug,
|
||||
"name": name,
|
||||
"is_current": is_current,
|
||||
"is_user_defined": True,
|
||||
"models": models if shown is None else shown,
|
||||
"total_models": len(models),
|
||||
"source": source,
|
||||
"api_url": api_url,
|
||||
"native_catalog_empty": native_catalog_empty})
|
||||
"slug": slug, "name": name, "is_current": is_current, "is_user_defined": True,
|
||||
"models": models if shown is None else shown, "total_models": len(models), "source": source,
|
||||
"api_url": api_url, "native_catalog_empty": native_catalog_empty})
|
||||
self.seen_slugs.add(slug.lower())
|
||||
|
||||
def endpoint_is_current(self, slug: str, aliases: set, url_norm: str, *, url_match_ok: bool = True) -> bool:
|
||||
@@ -872,20 +862,13 @@ def _lap_user_provider_rows(b: _PickerBuild, user_providers: dict) -> None:
|
||||
cut_at = next((i for i, t in enumerate(toks) if any(c.isdigit() for c in t.strip(".,()"))), None)
|
||||
if cut_at is not None and cut_at >= 2:
|
||||
grp_display = " ".join(toks[:cut_at]).strip()
|
||||
# slug = first ep_name encountered; probe key from the first member (inline api_key,
|
||||
# else key_env through the per-profile secret scope).
|
||||
ep_groups[group_key] = {
|
||||
"slug": ep_name, # first ep_name encountered
|
||||
"name": grp_display or display_name,
|
||||
"api_url": api_url,
|
||||
"models": [],
|
||||
"has_explicit_models": False,
|
||||
# Probe key from the first member: inline api_key, else key_env through the
|
||||
# per-profile secret scope.
|
||||
"api_key": inline_api_key or _scoped_key_env(key_env),
|
||||
"headers": headers,
|
||||
"api_mode": ep_cfg.get("api_mode"),
|
||||
"discovery_allowed": bool(api_url) and _discover_flag(ep_cfg),
|
||||
"raw_names": [],
|
||||
"aliases": set()}
|
||||
"slug": ep_name, "name": grp_display or display_name, "api_url": api_url, "models": [],
|
||||
"has_explicit_models": False, "api_key": inline_api_key or _scoped_key_env(key_env),
|
||||
"headers": headers, "api_mode": ep_cfg.get("api_mode"),
|
||||
"discovery_allowed": bool(api_url) and _discover_flag(ep_cfg), "raw_names": [], "aliases": set()}
|
||||
grp = ep_groups[group_key]
|
||||
for m in entry_models:
|
||||
if m and m not in grp["models"]:
|
||||
@@ -987,15 +970,9 @@ def _lap_custom_provider_rows(b: _PickerBuild, custom_providers: list) -> None:
|
||||
if group_key not in groups:
|
||||
display_name = prefix or raw_name
|
||||
groups[group_key] = {
|
||||
"slug": custom_provider_slug(display_name, provider_key),
|
||||
"name": display_name,
|
||||
"api_url": api_url,
|
||||
"api_key": api_key,
|
||||
"models": [],
|
||||
"has_explicit_models": False,
|
||||
"discover_models": discover,
|
||||
"api_mode": api_mode,
|
||||
"extra_headers": entry_extra_headers,
|
||||
"slug": custom_provider_slug(display_name, provider_key), "name": display_name,
|
||||
"api_url": api_url, "api_key": api_key, "models": [], "has_explicit_models": False,
|
||||
"discover_models": discover, "api_mode": api_mode, "extra_headers": entry_extra_headers,
|
||||
"aliases": set()}
|
||||
else:
|
||||
if api_key and not groups[group_key].get("api_key"):
|
||||
@@ -1141,19 +1118,13 @@ def list_authenticated_providers(
|
||||
user_providers = stringify_provider_map(user_providers)
|
||||
data = fetch_models_dev()
|
||||
|
||||
# A single excluded entry like ``copilot`` hides the provider under every key it surfaces
|
||||
# as (hermes_id / mdev_id / canonical slug).
|
||||
b = _PickerBuild(
|
||||
current_provider=current_provider,
|
||||
current_base_url=current_base_url,
|
||||
current_model=current_model,
|
||||
max_models=max_models,
|
||||
for_picker=for_picker,
|
||||
force_fresh_nous_tier=force_fresh_nous_tier,
|
||||
probe_custom_providers=probe_custom_providers,
|
||||
probe_current_custom_provider=probe_current_custom_provider,
|
||||
refresh=refresh,
|
||||
# A single entry like ``copilot`` hides the provider under every key it surfaces as
|
||||
# (hermes_id / mdev_id / canonical slug).
|
||||
excluded={str(p).strip().lower() for p in (excluded_providers or []) if p},
|
||||
current_provider=current_provider, current_base_url=current_base_url, current_model=current_model,
|
||||
max_models=max_models, for_picker=for_picker, force_fresh_nous_tier=force_fresh_nous_tier,
|
||||
probe_custom_providers=probe_custom_providers, probe_current_custom_provider=probe_current_custom_provider,
|
||||
refresh=refresh, excluded={str(p).strip().lower() for p in (excluded_providers or []) if p},
|
||||
curated=_build_curated_lists(current_provider, current_base_url, current_model))
|
||||
|
||||
# Warm the disk cache in parallel before the serial section loops (otherwise 15-30s of live
|
||||
@@ -1174,10 +1145,13 @@ def list_authenticated_providers(
|
||||
_lap_bare_custom_row(b, custom_providers)
|
||||
if custom_providers and isinstance(custom_providers, list):
|
||||
_lap_custom_provider_rows(b, custom_providers)
|
||||
results = b.results
|
||||
return _finalize_picker_rows(b.results, user_providers, current_model)
|
||||
|
||||
# ``providers.<name>.enabled: false`` post-filter covers built-in rows (sections 1-2) that
|
||||
# bypass the per-section gate; matched by slug and ``provider_id``.
|
||||
|
||||
def _finalize_picker_rows(results: list, user_providers, current_model: str) -> list:
|
||||
"""Post-passes: drop ``providers.<name>.enabled: false`` rows, inject the current model, sort."""
|
||||
# The enabled post-filter covers built-in rows (sections 1-2) that bypass the per-section
|
||||
# gate; matched by slug and ``provider_id``.
|
||||
try:
|
||||
from hermes_cli.config import is_provider_enabled
|
||||
if isinstance(user_providers, dict):
|
||||
@@ -1195,7 +1169,7 @@ def list_authenticated_providers(
|
||||
|
||||
# A custom/uncurated model set via `/model <provider>/<name>` would be invisible in every
|
||||
# picker (main and MoA slot pickers read these rows); inject it at the front of the current
|
||||
# provider's row as a uniform post-pass.
|
||||
# provider's row.
|
||||
if current_model:
|
||||
for row in results:
|
||||
if not row.get("is_current") or row.get("native_catalog_empty"):
|
||||
|
||||
Reference in New Issue
Block a user