feat(gateway): config-driven per-chat channel_context injection - #43728
feat(gateway): config-driven per-chat channel_context injection#43728liuhao1024 wants to merge 1 commit into
Conversation
Add gateway.channel_context_map config option that maps chat_id strings to context text prepended to every inbound message from that chat. Supports two config shapes: - File path (string): JSON file with mtime-based caching (reloaded when file changes, zero hot-path cost when unchanged) - Inline dict: for static setups The config context is appended after any adapter-set channel_context (backfill), preserving the existing precedence model. Reuses the existing channel_context injection point in _prepare_inbound_message_text. Closes NousResearch#43650
|
Author of #43650 here — this implementation covers the exact use case that motivated the issue. Thanks for picking it up so quickly! I'm running an external orchestrator (tmux session bridge) where each monitored coding session has a dedicated chat, and today the chat→session hint requires patching
One non-blocking nit: the cache comment says it's keyed by Once this lands I'll switch my setup to |
teknium1
left a comment
There was a problem hiding this comment.
Thanks for targeting a real gateway configuration gap: current main still only prepends adapter-provided event.channel_context at gateway/run.py:10391-10395.
Problems
gateway/run.py:7535looks upsource.chat_id, but the PR examples usetelegram:<id>/discord:<id>.BasePlatformAdapter.build_source()stores raw chat ids atgateway/platforms/base.py:5478-5481, so those documented keys will not match ordinary events.- The new cache stores only mtime, although its comment says it is keyed by path and mtime. Repointing the setting to a distinct file with the same mtime can return stale context; the #43650 author noted the same issue.
- The tests patch the new loader, so they do not validate actual config resolution or the documented key form.
Suggested changes
- Establish one canonical key format and test normal Telegram/Discord
SessionSourcevalues against it. - Include the resolved path and a precise file signature in the cache key.
- Use the existing gateway runtime config loader and add a real temporary-
HERMES_HOMEintegration test plus user documentation.
This is an automated hermes-sweeper review.
| # happens after sender-prefix so the prefix only applies to the | ||
| # trigger message, not the backfill block. | ||
| # Also inject per-chat context from the channel_context_map config | ||
| # (adapter context takes precedence; config context is appended). |
There was a problem hiding this comment.
This lookup uses the raw source.chat_id, but the documented examples use telegram:<id> and discord:<id>. BasePlatformAdapter.build_source() stores the raw id separately from source.platform, so normal events will not match those examples. Please either form a platform-qualified lookup key here or revise the key contract and tests to use raw ids.
Problem
The inbound pipeline already supports a per-event
channel_contextfield —gateway/run.pyprepends it to the message text when present. However, only platform adapters can populate that field. There is no way for a user/operator to attach deterministic context to a specific chat via configuration.Use case: Running an external orchestrator where each monitored coding session has a dedicated chat. When a message arrives from one of those chats, the agent needs a deterministic hint ("this chat is bound to session X — forward commands via tool Y") so routing doesn't depend on the LLM guessing. Other shapes: per-chat workspace/project descriptions, per-chat language or persona notes, customer-specific context.
Today the only way to do this is patching the gateway source and re-applying the patch after every
hermes update.Solution
Add
gateway.channel_context_mapconfig option that mapschat_idstrings to context text prepended to every inbound message from that chat.Two config shapes supported:
Key design decisions:
channel_context(backfill) takes precedence; config context is appendedchannel_contextfield in_prepare_inbound_message_textChanges
hermes_cli/config.py: Addchannel_context_mapconfig key with default""undergatewaysectiongateway/run.py: Add_load_channel_context_map()helper with mtime caching + wire into_prepare_inbound_message_texttests/gateway/test_channel_context_map.py: 15 tests (10 unit + 5 integration)Closes #43650