fix(api): resolve model provider from config to prevent misrouting
When the model dropdown sends a prefixed ID like "anthropic/claude-xxx", AIAgent interprets the provider/model format as an OpenRouter path and routes through OpenRouter instead of the direct Anthropic API. Fix: read the configured provider from config.yaml model section. If the model ID starts with the configured provider name followed by "/", strip that prefix and pass the provider explicitly to AIAgent. This ensures direct API providers (Anthropic, OpenAI, etc.) are used when configured, regardless of the model ID format from the dropdown.
This commit is contained in:
@@ -629,7 +629,15 @@ def _handle_chat_sync(handler, body):
|
||||
try:
|
||||
from run_agent import AIAgent
|
||||
with CHAT_LOCK:
|
||||
agent = AIAgent(model=s.model, platform='cli', quiet_mode=True,
|
||||
from api.config import cfg as _hcfg
|
||||
_model = s.model or ''
|
||||
_prov = None
|
||||
_mc = _hcfg.get('model', {})
|
||||
if isinstance(_mc, dict):
|
||||
_prov = _mc.get('provider')
|
||||
if _prov and '/' in _model and _model.startswith(_prov + '/'):
|
||||
_model = _model.split('/', 1)[1]
|
||||
agent = AIAgent(model=_model, provider=_prov, platform='cli', quiet_mode=True,
|
||||
enabled_toolsets=CLI_TOOLSETS, session_id=s.session_id)
|
||||
workspace_ctx = f"[Workspace: {s.workspace}]\n"
|
||||
workspace_system_msg = (
|
||||
|
||||
@@ -13,6 +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,
|
||||
)
|
||||
|
||||
# Lazy import to avoid circular deps -- hermes-agent is on sys.path via api/config.py
|
||||
@@ -99,8 +100,18 @@ 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]
|
||||
agent = AIAgent(
|
||||
model=model,
|
||||
provider=_provider,
|
||||
platform='cli',
|
||||
quiet_mode=True,
|
||||
enabled_toolsets=CLI_TOOLSETS,
|
||||
|
||||
Reference in New Issue
Block a user