refactor: extract resolve_model_provider helper, fix cross-provider routing

Replace duplicated inline provider resolution in routes.py and streaming.py
with a shared resolve_model_provider() helper in config.py.

Improvements over original:
- If model ID has a prefix matching any known direct-API provider
  (not just the config provider), strip it and route correctly.
  This handles edge cases like localStorage restoring a model from
  a different provider group.
- Single source of truth for the resolution logic.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Nathan Esquenazi
2026-04-01 22:56:34 -07:00
parent 2864c2b691
commit 241357595d
3 changed files with 37 additions and 21 deletions

View File

@@ -13,7 +13,7 @@ from pathlib import Path
from api.config import (
STREAMS, STREAMS_LOCK, CANCEL_FLAGS, CLI_TOOLSETS,
_get_session_agent_lock, _set_thread_env, _clear_thread_env,
cfg as _hermes_cfg,
resolve_model_provider,
)
# Lazy import to avoid circular deps -- hermes-agent is on sys.path via api/config.py
@@ -100,18 +100,10 @@ def _run_agent_streaming(session_id, msg_text, model, workspace, stream_id, atta
if AIAgent is None:
raise ImportError("AIAgent not available -- check that hermes-agent is on sys.path")
# Resolve provider from config so agent routes to the right API
_provider = None
model_cfg = _hermes_cfg.get('model', {})
if isinstance(model_cfg, dict):
_provider = model_cfg.get('provider')
# If model has provider/ prefix matching config provider, strip it
# so AIAgent doesn't misroute to OpenRouter
if _provider and '/' in model and model.startswith(_provider + '/'):
model = model.split('/', 1)[1]
resolved_model, resolved_provider = resolve_model_provider(model)
agent = AIAgent(
model=model,
provider=_provider,
model=resolved_model,
provider=resolved_provider,
platform='cli',
quiet_mode=True,
enabled_toolsets=CLI_TOOLSETS,