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

@ -379,7 +379,7 @@ class PlatformConfig:
# noise; keep True for back-channels where the operator wants them.
gateway_restart_notification: bool = True
# Whether the gateway shows a "typing…" / "is thinking…" status indicator
# Whether the gateway shows a "typing…" / "is thinking…" status indicator
# while the agent processes a message on this platform. Default True
# preserves prior behavior. Set False on platforms where the indicator is
# unwanted (e.g. Slack's assistant.threads.setStatus "is thinking…", which
@ -420,7 +420,7 @@ class PlatformConfig:
if "home_channel" in data:
home_channel = HomeChannel.from_dict(data["home_channel"])
# gateway_restart_notification may be bridged into extra via the
# gateway_restart_notification may be bridged into extra via the
# shared-key loop in load_gateway_config(); check both top-level
# and extra so YAML ``discord: gateway_restart_notification: false``
# works without needing a separate platforms: block.
@ -448,7 +448,7 @@ class PlatformConfig:
api_key=data.get("api_key"),
home_channel=home_channel,
reply_to_mode=data.get("reply_to_mode", "first"),
gateway_restart_notification=_coerce_bool(_grn, True),
gateway_restart_notification=_coerce_bool(_grn, True),
typing_indicator=_coerce_bool(_typing, True),
channel_overrides=channel_overrides,
extra=data.get("extra", {}),
@ -1103,8 +1103,20 @@ def load_gateway_config() -> GatewayConfig:
bridged["gateway_restart_notification"] = platform_cfg["gateway_restart_notification"]
if "typing_indicator" in platform_cfg:
bridged["typing_indicator"] = platform_cfg["typing_indicator"]
has_channel_overrides = "channel_overrides" in platform_cfg
if has_channel_overrides:
raw_overrides = platform_cfg.get("channel_overrides")
if isinstance(raw_overrides, dict):
plat_data, _extra = _ensure_platform_extra_dict(
platforms_data, plat.value
)
plat_data["channel_overrides"] = {
str(cid): ov_data
for cid, ov_data in raw_overrides.items()
if isinstance(ov_data, dict)
}
enabled_was_explicit = _cfg_toplevel and "enabled" in platform_cfg
if not bridged and not enabled_was_explicit:
if not bridged and not enabled_was_explicit and not has_channel_overrides:
continue
plat_data, extra = _ensure_platform_extra_dict(platforms_data, plat.value)
if enabled_was_explicit:

View file

@ -2306,18 +2306,54 @@ def _resolve_gateway_model(config: dict | None = None) -> str:
return ""
def _channel_override_lookup_keys(
chat_id: str,
*,
thread_id: Optional[str] = None,
parent_id: Optional[str] = None,
) -> list[str]:
"""Ordered, de-duplicated keys for ``channel_overrides`` lookup.
Matches ``resolve_channel_prompt`` semantics: exact thread/channel id first,
then parent channel/forum id (Discord threads inherit parent overrides).
"""
keys: list[str] = []
seen: set[str] = set()
for key in (chat_id, thread_id, parent_id):
if not key:
continue
sk = str(key)
if sk in seen:
continue
seen.add(sk)
keys.append(sk)
return keys
def _get_channel_override(
config: GatewayConfig,
platform: Platform,
chat_id: str,
*,
thread_id: Optional[str] = None,
parent_id: Optional[str] = None,
) -> Optional[ChannelOverride]:
"""Return per-channel override for this platform/chat_id, or None."""
if not chat_id:
return None
"""Return per-channel override for this platform/chat_id, or None.
Looks up ``channel_overrides`` by ``chat_id``, then ``thread_id``, then
``parent_id`` (forum threads / child channels inherit the parent entry).
"""
platform_config = config.platforms.get(platform)
if not platform_config or not platform_config.channel_overrides:
return None
return platform_config.channel_overrides.get(str(chat_id))
overrides = platform_config.channel_overrides
for key in _channel_override_lookup_keys(
chat_id, thread_id=thread_id, parent_id=parent_id
):
ov = overrides.get(key)
if ov is not None:
return ov
return None
def _resolve_hermes_bin() -> Optional[list[str]]:
@ -3579,11 +3615,11 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
session_key: Optional[str] = None,
user_config: Optional[dict] = None,
) -> tuple[str, dict]:
"""Resolve model/runtime for a session, honoring session-scoped /model overrides.
"""Resolve model/runtime for a session.
If the session override already contains a complete provider bundle
(provider/api_key/base_url/api_mode), prefer it directly instead of
resolving fresh global runtime state first.
Priority (highest first): session ``/model`` ``channel_overrides``
global config/env (``_resolve_gateway_model(user_config)`` and default
provider resolution).
"""
resolved_session_key = session_key
if not resolved_session_key and source is not None:
@ -3632,30 +3668,43 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
runtime_model,
)
model = runtime_model
cfg = getattr(self, "config", None)
if cfg and source is not None:
chat_id = str(source.chat_id) if source.chat_id else ""
thread_id = (
str(source.thread_id) if getattr(source, "thread_id", None) else None
)
parent_id = (
str(source.parent_chat_id)
if getattr(source, "parent_chat_id", None)
else None
)
ch = _get_channel_override(
cfg,
source.platform,
chat_id,
thread_id=thread_id,
parent_id=parent_id,
)
if ch:
if ch.model:
model = ch.model
if ch.provider:
runtime_kwargs = _resolve_runtime_agent_kwargs_for_provider(
ch.provider
)
ch_runtime_model = runtime_kwargs.pop("model", None)
# Only adopt the provider's bundled model when the override
# did not specify an explicit model.
if ch_runtime_model and not ch.model:
model = ch_runtime_model
if override and resolved_session_key:
model, runtime_kwargs = self._apply_session_model_override(
resolved_session_key, model, runtime_kwargs
)
cfg = getattr(self, "config", None)
if cfg and source is not None and source.chat_id:
ch = _get_channel_override(cfg, source.platform, str(source.chat_id))
if ch:
channel_touch = False
if ch.model and not (override and override.get("model")):
model = ch.model
channel_touch = True
if ch.provider and not (override and override.get("provider")):
runtime_kwargs = _resolve_runtime_agent_kwargs_for_provider(ch.provider)
runtime_model = runtime_kwargs.pop("model", None)
if runtime_model:
model = runtime_model or model
channel_touch = True
if channel_touch and override and resolved_session_key:
model, runtime_kwargs = self._apply_session_model_override(
resolved_session_key, model, runtime_kwargs
)
# When the config has no model.default but a provider was resolved
# (e.g. user ran `hermes auth add openai-codex` without `hermes model`),
# fall back to the provider's first catalog model so the API call
@ -4528,20 +4577,53 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
cfg = _load_gateway_runtime_config()
return str(cfg_get(cfg, "agent", "system_prompt", default="") or "").strip()
def _resolve_model_for_channel(self, platform: Platform, chat_id: str) -> str:
"""Resolve model for this channel: channel_overrides[channel_id] else global default."""
def _resolve_model_for_channel(
self,
platform: Platform,
chat_id: str,
*,
user_config: Optional[dict] = None,
thread_id: Optional[str] = None,
parent_id: Optional[str] = None,
) -> str:
"""Resolve model for this channel: channel_overrides else global default."""
config = getattr(self, "config", None)
if config:
override = _get_channel_override(config, platform, chat_id)
override = _get_channel_override(
config,
platform,
chat_id,
thread_id=thread_id,
parent_id=parent_id,
)
if override and override.model:
return override.model
return _resolve_gateway_model()
return _resolve_gateway_model(user_config)
def _get_system_prompt_for_channel(self, platform: Platform, chat_id: str) -> str:
"""System prompt for this channel: channel override else global ephemeral."""
def _get_system_prompt_for_channel(
self,
platform: Platform,
chat_id: str,
*,
thread_id: Optional[str] = None,
parent_id: Optional[str] = None,
) -> str:
"""Ephemeral system prompt for this channel/thread.
Uses ``channel_overrides`` when set, else the global gateway prompt.
Legacy ``channel_prompts`` are applied separately via ``event.channel_prompt``
in ``run_sync`` (adapter ``resolve_channel_prompt``), so they are not
duplicated here.
"""
config = getattr(self, "config", None)
if config:
override = _get_channel_override(config, platform, chat_id)
override = _get_channel_override(
config,
platform,
chat_id,
thread_id=thread_id,
parent_id=parent_id,
)
if override and override.system_prompt:
return (override.system_prompt or "").strip()
return getattr(self, "_ephemeral_system_prompt", None) or ""
@ -16872,7 +16954,10 @@ class GatewayRunner(GatewayAuthorizationMixin, GatewayKanbanWatchersMixin, Gatew
if event_channel_prompt:
combined_ephemeral = (combined_ephemeral + "\n\n" + event_channel_prompt).strip()
cfg_channel_prompt = self._get_system_prompt_for_channel(
source.platform, source.chat_id or ""
source.platform,
source.chat_id or "",
thread_id=getattr(source, "thread_id", None),
parent_id=getattr(source, "parent_chat_id", None),
)
if cfg_channel_prompt:
combined_ephemeral = (combined_ephemeral + "\n\n" + cfg_channel_prompt).strip()