feat(gateway): per-channel model and system prompt overrides (Fixes #1955)

- ChannelOverride + channel_overrides on PlatformConfig
- Resolve model/runtime: session /model, then channel_overrides, then global
- Thread/parent channel lookup; bridge discord.channel_overrides from YAML
- Drop unrelated test and delegate_tool changes from PR scope
This commit is contained in:
crazywriter1 2026-05-17 16:31:02 +03:00 committed by Teknium
parent ebef73f6b8
commit 0010c14e66
4 changed files with 336 additions and 40 deletions

View file

@ -1,5 +1,7 @@
"""Tests for per-channel model and system prompt overrides (Fixes #1955)."""
from unittest.mock import patch
import pytest
from gateway.config import (
@ -9,6 +11,7 @@ from gateway.config import (
PlatformConfig,
)
from gateway.run import _get_channel_override, GatewayRunner
from gateway.session import SessionSource
class TestGetChannelOverride:
@ -65,6 +68,60 @@ class TestGetChannelOverride:
)
assert _get_channel_override(config, Platform.DISCORD, "123").model == "gpt-4"
def test_thread_id_lookup_when_chat_id_misses(self):
config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"thread_99": ChannelOverride(model="topic-model"),
},
),
},
)
result = _get_channel_override(
config, Platform.DISCORD, "parent_chan", thread_id="thread_99"
)
assert result is not None
assert result.model == "topic-model"
def test_parent_id_fallback_when_thread_has_no_entry(self):
config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"parent_chan": ChannelOverride(model="parent-model"),
},
),
},
)
result = _get_channel_override(
config,
Platform.DISCORD,
"thread_only",
parent_id="parent_chan",
)
assert result is not None
assert result.model == "parent-model"
def test_exact_thread_overrides_parent(self):
config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"thread_1": ChannelOverride(model="thread-model"),
"parent_chan": ChannelOverride(model="parent-model"),
},
),
},
)
result = _get_channel_override(
config, Platform.DISCORD, "thread_1", parent_id="parent_chan"
)
assert result.model == "thread-model"
class TestResolveModelForChannel:
def test_uses_channel_override_when_present(self):
@ -86,7 +143,7 @@ class TestResolveModelForChannel:
def test_falls_back_to_global_when_no_override(self, monkeypatch):
monkeypatch.setattr(
"gateway.run._resolve_gateway_model",
lambda: "global-model/default",
lambda _cfg=None: "global-model/default",
)
config = GatewayConfig(
platforms={
@ -126,3 +183,120 @@ class TestGetSystemPromptForChannel:
runner._ephemeral_system_prompt = "Global prompt"
prompt = runner._get_system_prompt_for_channel(Platform.DISCORD, "other")
assert prompt == "Global prompt"
class TestResolveSessionAgentRuntimePriority:
"""Model/runtime priority: session /model → channel_overrides → global."""
def test_channel_override_beats_global(self):
runner = object.__new__(GatewayRunner)
runner._session_model_overrides = {}
runner.config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"chan_1": ChannelOverride(
model="channel/model",
provider="openrouter",
),
},
),
},
)
source = SessionSource(
platform=Platform.DISCORD,
chat_id="chan_1",
user_id="u1",
)
with patch("gateway.run._resolve_gateway_model", return_value="global/model"), \
patch("gateway.run._resolve_runtime_agent_kwargs", return_value={
"provider": "anthropic",
"api_key": "k",
"base_url": "https://api.anthropic.com",
"api_mode": "chat_completions",
}), \
patch(
"gateway.run._resolve_runtime_agent_kwargs_for_provider",
return_value={
"provider": "openrouter",
"api_key": "k2",
"base_url": "https://openrouter.ai/api/v1",
"api_mode": "chat_completions",
},
):
model, runtime = runner._resolve_session_agent_runtime(
source=source,
user_config={"model": {"default": "global/model"}},
)
assert model == "channel/model"
assert runtime["provider"] == "openrouter"
def test_session_model_beats_channel_override(self):
runner = object.__new__(GatewayRunner)
runner.config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"chan_1": ChannelOverride(model="channel/model"),
},
),
},
)
session_key = "agent:main:discord:channel:chan_1"
runner._session_model_overrides = {
session_key: {
"model": "session/model",
"provider": "anthropic",
},
}
source = SessionSource(
platform=Platform.DISCORD,
chat_id="chan_1",
chat_type="channel",
user_id="u1",
)
with patch("gateway.run._resolve_gateway_model", return_value="global/model"), \
patch("gateway.run._resolve_runtime_agent_kwargs", return_value={
"provider": "openrouter",
"api_key": "k",
"base_url": "https://openrouter.ai/api/v1",
"api_mode": "chat_completions",
}):
model, runtime = runner._resolve_session_agent_runtime(
source=source,
session_key=session_key,
)
assert model == "session/model"
assert runtime["provider"] == "anthropic"
def test_parent_channel_model_inherited_in_thread(self):
runner = object.__new__(GatewayRunner)
runner._session_model_overrides = {}
runner.config = GatewayConfig(
platforms={
Platform.DISCORD: PlatformConfig(
enabled=True,
channel_overrides={
"parent_chan": ChannelOverride(model="parent/model"),
},
),
},
)
source = SessionSource(
platform=Platform.DISCORD,
chat_id="thread_1",
chat_type="thread",
parent_chat_id="parent_chan",
user_id="u1",
)
with patch("gateway.run._resolve_gateway_model", return_value="global/model"), \
patch("gateway.run._resolve_runtime_agent_kwargs", return_value={
"provider": "anthropic",
"api_key": "k",
"base_url": "https://api.anthropic.com",
"api_mode": "chat_completions",
}):
model, _runtime = runner._resolve_session_agent_runtime(source=source)
assert model == "parent/model"

View file

@ -831,6 +831,31 @@ class TestLoadGatewayConfig:
assert config.always_log_local is False
def test_bridges_discord_channel_overrides_from_top_level_yaml(self, tmp_path, monkeypatch):
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()
config_path = hermes_home / "config.yaml"
config_path.write_text(
"discord:\n"
" channel_overrides:\n"
' "1234567890":\n'
" model: openrouter/healer-alpha\n"
" provider: openrouter\n"
" system_prompt: Daily news summarizer\n",
encoding="utf-8",
)
monkeypatch.setenv("HERMES_HOME", str(hermes_home))
config = load_gateway_config()
discord = config.platforms[Platform.DISCORD]
assert "1234567890" in discord.channel_overrides
ov = discord.channel_overrides["1234567890"]
assert ov.model == "openrouter/healer-alpha"
assert ov.provider == "openrouter"
assert ov.system_prompt == "Daily news summarizer"
def test_bridges_discord_channel_prompts_from_config_yaml(self, tmp_path, monkeypatch):
hermes_home = tmp_path / ".hermes"
hermes_home.mkdir()