fix(gateway): sync /model and /personality with CLI config.yaml pattern

This commit is contained in:
VencentSoliman 2026-02-27 11:14:14 -05:00
parent 4f3cb98e5e
commit 07fcb94bc0

View file

@ -971,25 +971,25 @@ class GatewayRunner:
async def _handle_model_command(self, event: MessageEvent) -> str: async def _handle_model_command(self, event: MessageEvent) -> str:
"""Handle /model command - show or change the current model.""" """Handle /model command - show or change the current model."""
args = event.get_command_args().strip() import yaml
# Resolve current model using the same chain as _run_agent args = event.get_command_args().strip()
current = os.getenv("HERMES_MODEL") or os.getenv("LLM_MODEL") config_path = _hermes_home / 'config.yaml'
if not current:
# Resolve current model the same way the agent init does:
# env vars first, then config.yaml always overrides.
current = os.getenv("HERMES_MODEL") or os.getenv("LLM_MODEL") or "anthropic/claude-opus-4.6"
try: try:
import yaml as _y if config_path.exists():
_cfg_path = _hermes_home / "config.yaml" with open(config_path) as f:
if _cfg_path.exists(): cfg = yaml.safe_load(f) or {}
with open(_cfg_path) as _f: model_cfg = cfg.get("model", {})
_cfg = _y.safe_load(_f) or {} if isinstance(model_cfg, str):
_model_cfg = _cfg.get("model", {}) current = model_cfg
if isinstance(_model_cfg, str): elif isinstance(model_cfg, dict):
current = _model_cfg current = model_cfg.get("default", current)
elif isinstance(_model_cfg, dict):
current = _model_cfg.get("default")
except Exception: except Exception:
pass pass
current = current or "anthropic/claude-opus-4.6"
if not args: if not args:
return f"🤖 **Current model:** `{current}`\n\nTo change: `/model provider/model-name`" return f"🤖 **Current model:** `{current}`\n\nTo change: `/model provider/model-name`"
@ -1003,23 +1003,42 @@ class GatewayRunner:
f"• `openai/gpt-4o`" f"• `openai/gpt-4o`"
) )
# Write to config.yaml (source of truth), same pattern as CLI save_config_value.
try:
user_config = {}
if config_path.exists():
with open(config_path) as f:
user_config = yaml.safe_load(f) or {}
if "model" not in user_config or not isinstance(user_config["model"], dict):
user_config["model"] = {}
user_config["model"]["default"] = args
with open(config_path, 'w') as f:
yaml.dump(user_config, f, default_flow_style=False, sort_keys=False)
except Exception as e:
return f"⚠️ Failed to save model change: {e}"
# Also set env var so code reading it before the next agent init sees the update.
os.environ["HERMES_MODEL"] = args os.environ["HERMES_MODEL"] = args
return f"🤖 Model changed to `{args}`\n_(takes effect on next message)_" return f"🤖 Model changed to `{args}`\n_(takes effect on next message)_"
async def _handle_personality_command(self, event: MessageEvent) -> str: async def _handle_personality_command(self, event: MessageEvent) -> str:
"""Handle /personality command - list or set a personality.""" """Handle /personality command - list or set a personality."""
import yaml
args = event.get_command_args().strip().lower() args = event.get_command_args().strip().lower()
config_path = _hermes_home / 'config.yaml'
try: try:
import yaml
config_path = _hermes_home / 'config.yaml'
if config_path.exists(): if config_path.exists():
with open(config_path, 'r') as f: with open(config_path, 'r') as f:
config = yaml.safe_load(f) or {} config = yaml.safe_load(f) or {}
personalities = config.get("agent", {}).get("personalities", {}) personalities = config.get("agent", {}).get("personalities", {})
else: else:
config = {}
personalities = {} personalities = {}
except Exception: except Exception:
config = {}
personalities = {} personalities = {}
if not personalities: if not personalities:
@ -1034,7 +1053,21 @@ class GatewayRunner:
return "\n".join(lines) return "\n".join(lines)
if args in personalities: if args in personalities:
os.environ["HERMES_PERSONALITY"] = personalities[args] new_prompt = personalities[args]
# Write to config.yaml, same pattern as CLI save_config_value.
try:
if "agent" not in config or not isinstance(config.get("agent"), dict):
config["agent"] = {}
config["agent"]["system_prompt"] = new_prompt
with open(config_path, 'w') as f:
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
except Exception as e:
return f"⚠️ Failed to save personality change: {e}"
# Update in-memory so it takes effect on the very next message.
self._ephemeral_system_prompt = new_prompt
return f"🎭 Personality set to **{args}**\n_(takes effect on next message)_" return f"🎭 Personality set to **{args}**\n_(takes effect on next message)_"
available = ", ".join(f"`{n}`" for n in personalities.keys()) available = ", ".join(f"`{n}`" for n in personalities.keys())