Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,30 @@ def _resolve_runtime_agent_kwargs() -> dict:
}


def _resolve_gateway_model() -> str:
"""Read model from env/config — mirrors the resolution in _run_agent_sync.

Without this, temporary AIAgent instances (memory flush, /compress) fall
back to the hardcoded default ("anthropic/claude-opus-4.6") which fails
when the active provider is openai-codex.
"""
model = os.getenv("HERMES_MODEL") or os.getenv("LLM_MODEL") or "anthropic/claude-opus-4.6"
try:
import yaml as _y
_cfg_path = _hermes_home / "config.yaml"
if _cfg_path.exists():
with open(_cfg_path, encoding="utf-8") as _f:
_cfg = _y.safe_load(_f) or {}
_model_cfg = _cfg.get("model", {})
if isinstance(_model_cfg, str):
model = _model_cfg
elif isinstance(_model_cfg, dict):
model = _model_cfg.get("default", model)
except Exception:
pass
return model


class GatewayRunner:
"""
Main gateway controller.
Expand Down Expand Up @@ -258,8 +282,14 @@ def _flush_memories_for_session(self, old_session_id: str):
if not runtime_kwargs.get("api_key"):
return

# Resolve model from config — AIAgent's default is OpenRouter-
# formatted ("anthropic/claude-opus-4.6") which fails when the
# active provider is openai-codex.
model = _resolve_gateway_model()

tmp_agent = AIAgent(
**runtime_kwargs,
model=model,
max_iterations=8,
quiet_mode=True,
enabled_toolsets=["memory", "skills"],
Expand Down Expand Up @@ -1047,6 +1077,7 @@ async def _handle_message(self, event: MessageEvent) -> Optional[str]:
if len(_hyg_msgs) >= 4:
_hyg_agent = AIAgent(
**_hyg_runtime,
model=_hyg_model,
max_iterations=4,
quiet_mode=True,
enabled_toolsets=["memory"],
Expand Down Expand Up @@ -1867,6 +1898,9 @@ async def _handle_compress_command(self, event: MessageEvent) -> str:
if not runtime_kwargs.get("api_key"):
return "No provider configured -- cannot compress."

# Resolve model from config (same reason as memory flush above).
model = _resolve_gateway_model()

msgs = [
{"role": m.get("role"), "content": m.get("content")}
for m in history
Expand All @@ -1877,6 +1911,7 @@ async def _handle_compress_command(self, event: MessageEvent) -> str:

tmp_agent = AIAgent(
**runtime_kwargs,
model=model,
max_iterations=4,
quiet_mode=True,
enabled_toolsets=["memory"],
Expand Down
Loading