refactor: enhance configuration loading for GatewayRunner

- Implemented dynamic loading of environment variables and configuration from a YAML file to ensure fresh credentials for the GatewayRunner.
- Improved error handling during the loading process to accommodate different encoding scenarios and potential exceptions.
This commit is contained in:
teknium1
2026-02-25 16:40:52 -08:00
parent 9a858b8d67
commit 55a0178490

View File

@@ -1379,8 +1379,38 @@ class GatewayRunner:
if self._ephemeral_system_prompt:
combined_ephemeral = (combined_ephemeral + "\n\n" + self._ephemeral_system_prompt).strip()
# Re-read .env and config for fresh credentials (gateway is long-lived,
# keys may change without restart).
try:
load_dotenv(_env_path, override=True, encoding="utf-8")
except UnicodeDecodeError:
load_dotenv(_env_path, override=True, encoding="latin-1")
except Exception:
pass
api_key = os.getenv("OPENROUTER_API_KEY", "")
base_url = os.getenv("OPENROUTER_BASE_URL", "https://openrouter.ai/api/v1")
model = os.getenv("HERMES_MODEL", "anthropic/claude-opus-4.6")
try:
import yaml as _y
_cfg_path = Path.home() / ".hermes" / "config.yaml"
if _cfg_path.exists():
with open(_cfg_path) 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)
base_url = _model_cfg.get("base_url", base_url)
except Exception:
pass
agent = AIAgent(
model=os.getenv("HERMES_MODEL", "anthropic/claude-opus-4.6"),
model=model,
api_key=api_key,
base_url=base_url,
max_iterations=max_iterations,
quiet_mode=True,
enabled_toolsets=enabled_toolsets,