mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-24 05:41:40 +00:00
refactor(inventory): extract shared ConfigContext + build_models_payload
Three call-sites in the codebase each duplicated the same config-slice
+ list_authenticated_providers + post-processing pattern:
- hermes_cli/web_server.py /api/model/options
- tui_gateway/server.py model.options JSON-RPC
- tui_gateway/server.py model.save_key JSON-RPC
This consolidates them onto hermes_cli/inventory.py:
load_picker_context() -> ConfigContext
Replaces the 17-LOC config-slice (model.{default,name,provider,
base_url}, providers:, custom_providers:) every consumer did
inline.
ConfigContext.with_overrides(*, current_provider=, current_model=,
current_base_url=) -> ConfigContext
Truthy-only overlay for TUI agent-session state on top of disk
config. Empty getattr(agent, ...) attrs MUST NOT clobber disk.
build_models_payload(ctx, *, include_unconfigured, picker_hints,
canonical_order, max_models) -> dict
Single payload builder. Delegates curation to
list_authenticated_providers (does not call provider_model_ids
per row \u2014 that pulls non-agentic models). picker_hints +
canonical_order produce the TUI ModelPickerDialog shape;
defaults match the dashboard's existing /api/model/options
contract.
Two latent bugs fixed by consolidation:
1. The dashboard read cfg.get('custom_providers') directly, missing
the v12+ keyed providers: form. Now both surfaces go through
get_compatible_custom_providers().
2. The TUI's canonical-merge keyed on is_user_defined to decide order.
Section 3 of list_authenticated_providers sets is_user_defined=True
on rows from the providers: config dict even when the slug is
canonical \u2014 that silently demoted them to the picker tail.
_reorder_canonical now keys on slug membership instead.
Stats: +666 / -145 (net +521). Module 240 LOC; 18 behavior tests.
This PR replaces the rejected #23369 (which bundled the consolidation
with new scriptable CLI surfaces \u2014 hermes models list/status, hermes
providers list \u2014 and a JSON contract that have no external user
demand). Just the refactor; the CLI surface is deferred to a separate
PR gated on actual demand.
Refs #23359.
This commit is contained in:
parent
4ceab16893
commit
efc32ab639
4 changed files with 666 additions and 145 deletions
|
|
@ -5155,94 +5155,37 @@ def _(rid, params: dict) -> dict:
|
|||
@method("model.options")
|
||||
def _(rid, params: dict) -> dict:
|
||||
try:
|
||||
from hermes_cli.model_switch import list_authenticated_providers
|
||||
from hermes_cli.models import CANONICAL_PROVIDERS, _PROVIDER_LABELS
|
||||
from hermes_cli.inventory import build_models_payload, load_picker_context
|
||||
|
||||
session = _sessions.get(params.get("session_id", ""))
|
||||
agent = session.get("agent") if session else None
|
||||
cfg = _load_cfg()
|
||||
current_provider = getattr(agent, "provider", "") or ""
|
||||
current_model = getattr(agent, "model", "") or _resolve_model()
|
||||
current_base_url = getattr(agent, "base_url", "") or ""
|
||||
# list_authenticated_providers already populates each provider's
|
||||
# "models" with the curated list (same source as `hermes model` and
|
||||
# classic CLI's /model picker). Do NOT overwrite with live
|
||||
# provider_model_ids() — that bypasses curation and pulls in
|
||||
# non-agentic models (e.g. Nous /models returns ~400 IDs including
|
||||
# TTS, embeddings, rerankers, image/video generators).
|
||||
user_provs = (
|
||||
cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
|
||||
# Layer agent-session state on top of disk config — once an agent
|
||||
# is spawned, IT owns the live provider/model/base_url. Empty
|
||||
# agent attributes must NOT clobber disk config (with_overrides
|
||||
# is truthy-only).
|
||||
ctx = load_picker_context().with_overrides(
|
||||
current_provider=getattr(agent, "provider", "") if agent else "",
|
||||
current_model=(
|
||||
(getattr(agent, "model", "") if agent else "") or _resolve_model()
|
||||
),
|
||||
current_base_url=getattr(agent, "base_url", "") if agent else "",
|
||||
)
|
||||
custom_provs = (
|
||||
cfg.get("custom_providers")
|
||||
if isinstance(cfg.get("custom_providers"), list)
|
||||
else []
|
||||
)
|
||||
authenticated = list_authenticated_providers(
|
||||
current_provider=current_provider,
|
||||
current_base_url=current_base_url,
|
||||
current_model=current_model,
|
||||
user_providers=user_provs,
|
||||
custom_providers=custom_provs,
|
||||
# picker_hints + canonical_order produce the TUI's required shape:
|
||||
# `authenticated`/`auth_type`/`key_env`/`warning` per row, in
|
||||
# CANONICAL_PROVIDERS declaration order. include_unconfigured=True
|
||||
# so the picker can show the full provider universe (with the
|
||||
# setup-hint warning attached) instead of only authed rows.
|
||||
# Curated model lists are preserved — list_authenticated_providers
|
||||
# populates `models` from the curated catalog, not provider_model_ids
|
||||
# (which would pull non-agentic models like TTS/embeddings/etc.).
|
||||
payload = build_models_payload(
|
||||
ctx,
|
||||
include_unconfigured=True,
|
||||
picker_hints=True,
|
||||
canonical_order=True,
|
||||
max_models=50,
|
||||
)
|
||||
|
||||
# Mark authenticated providers and build lookup by slug
|
||||
authed_map: dict = {}
|
||||
authed_extra: list = [] # user-defined/custom not in CANONICAL_PROVIDERS
|
||||
canonical_slugs = {e.slug for e in CANONICAL_PROVIDERS}
|
||||
for p in authenticated:
|
||||
p["authenticated"] = True
|
||||
authed_map[p["slug"]] = p
|
||||
if p["slug"] not in canonical_slugs:
|
||||
authed_extra.append(p)
|
||||
|
||||
# Build final list in CANONICAL_PROVIDERS order, merging auth data
|
||||
from hermes_cli.auth import PROVIDER_REGISTRY as _auth_reg
|
||||
|
||||
ordered: list = []
|
||||
for entry in CANONICAL_PROVIDERS:
|
||||
if entry.slug in authed_map:
|
||||
ordered.append(authed_map[entry.slug])
|
||||
else:
|
||||
pconfig = _auth_reg.get(entry.slug)
|
||||
auth_type = pconfig.auth_type if pconfig else "api_key"
|
||||
key_env = (
|
||||
pconfig.api_key_env_vars[0]
|
||||
if (pconfig and pconfig.api_key_env_vars)
|
||||
else ""
|
||||
)
|
||||
if auth_type == "api_key" and key_env:
|
||||
warning = f"paste {key_env} to activate"
|
||||
else:
|
||||
warning = f"run `hermes model` to configure ({auth_type})"
|
||||
ordered.append(
|
||||
{
|
||||
"slug": entry.slug,
|
||||
"name": _PROVIDER_LABELS.get(entry.slug, entry.label),
|
||||
"is_current": entry.slug == current_provider,
|
||||
"is_user_defined": False,
|
||||
"models": [],
|
||||
"total_models": 0,
|
||||
"source": "built-in",
|
||||
"authenticated": False,
|
||||
"auth_type": auth_type,
|
||||
"key_env": key_env,
|
||||
"warning": warning,
|
||||
}
|
||||
)
|
||||
|
||||
# Append user-defined/custom providers not in canonical list
|
||||
ordered.extend(authed_extra)
|
||||
|
||||
return _ok(
|
||||
rid,
|
||||
{
|
||||
"providers": ordered,
|
||||
"model": current_model,
|
||||
"provider": current_provider,
|
||||
},
|
||||
)
|
||||
return _ok(rid, payload)
|
||||
except Exception as e:
|
||||
return _err(rid, 5033, str(e))
|
||||
|
||||
|
|
@ -5261,7 +5204,7 @@ def _(rid, params: dict) -> dict:
|
|||
try:
|
||||
from hermes_cli.auth import PROVIDER_REGISTRY
|
||||
from hermes_cli.config import is_managed, save_env_value
|
||||
from hermes_cli.model_switch import list_authenticated_providers
|
||||
from hermes_cli.inventory import build_models_payload, load_picker_context
|
||||
|
||||
slug = (params.get("slug") or "").strip()
|
||||
api_key = (params.get("api_key") or "").strip()
|
||||
|
|
@ -5287,43 +5230,32 @@ def _(rid, params: dict) -> dict:
|
|||
# Save the key to ~/.hermes/.env
|
||||
env_var = pconfig.api_key_env_vars[0]
|
||||
save_env_value(env_var, api_key)
|
||||
# Also set in current process so list_authenticated_providers sees it
|
||||
# Also set in current process so the refreshed inventory sees it.
|
||||
import os
|
||||
|
||||
os.environ[env_var] = api_key
|
||||
|
||||
# Refresh provider data
|
||||
cfg = _load_cfg()
|
||||
# Refresh provider data via the shared inventory builder so this
|
||||
# surface stays in lock-step with model.options + dashboard
|
||||
# /api/model/options. picker_hints=True ensures the returned row
|
||||
# carries `authenticated` for the TUI frontend.
|
||||
session = _sessions.get(params.get("session_id", ""))
|
||||
agent = session.get("agent") if session else None
|
||||
current_provider = getattr(agent, "provider", "") or ""
|
||||
current_model = getattr(agent, "model", "") or _resolve_model()
|
||||
current_base_url = getattr(agent, "base_url", "") or ""
|
||||
|
||||
providers = list_authenticated_providers(
|
||||
current_provider=current_provider,
|
||||
current_base_url=current_base_url,
|
||||
current_model=current_model,
|
||||
user_providers=(
|
||||
cfg.get("providers") if isinstance(cfg.get("providers"), dict) else {}
|
||||
ctx = load_picker_context().with_overrides(
|
||||
current_provider=getattr(agent, "provider", "") if agent else "",
|
||||
current_model=(
|
||||
(getattr(agent, "model", "") if agent else "") or _resolve_model()
|
||||
),
|
||||
custom_providers=(
|
||||
cfg.get("custom_providers")
|
||||
if isinstance(cfg.get("custom_providers"), list)
|
||||
else []
|
||||
),
|
||||
max_models=50,
|
||||
current_base_url=getattr(agent, "base_url", "") if agent else "",
|
||||
)
|
||||
|
||||
# Find the newly-authenticated provider
|
||||
provider_data = None
|
||||
for p in providers:
|
||||
if p["slug"] == slug:
|
||||
provider_data = p
|
||||
break
|
||||
|
||||
if not provider_data:
|
||||
# Key was saved but provider didn't appear — still return success
|
||||
payload = build_models_payload(
|
||||
ctx, picker_hints=True, max_models=50,
|
||||
)
|
||||
provider_data = next(
|
||||
(p for p in payload["providers"] if p["slug"] == slug), None
|
||||
)
|
||||
if provider_data is None:
|
||||
# Key was saved but provider didn't appear — still return success.
|
||||
provider_data = {
|
||||
"slug": slug,
|
||||
"name": pconfig.name,
|
||||
|
|
@ -5332,7 +5264,8 @@ def _(rid, params: dict) -> dict:
|
|||
"total_models": 0,
|
||||
"authenticated": True,
|
||||
}
|
||||
|
||||
# picker_hints sets `authenticated` from the row state, but the
|
||||
# synthetic fallback above doesn't go through that path.
|
||||
provider_data["authenticated"] = True
|
||||
return _ok(rid, {"provider": provider_data})
|
||||
except Exception as e:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue