feat(discord): fetch thread history when bot is first mentioned mid-thread - #6712
feat(discord): fetch thread history when bot is first mentioned mid-thread#6712AgentWrapper wants to merge 1 commit into
Conversation
…hread Ports the Slack adapter's _fetch_thread_context() pattern to Discord. When the bot is @mentioned in an existing thread for the first time (no prior session), fetches up to 30 prior messages via channel.history() and injects them as context before the user's message. Follow-up messages use the normal session transcript. - Add _has_active_session_for_thread() using build_session_key() - Add _fetch_thread_context() using discord.Thread.history() - Inject context in _handle_message() only for existing threads with no prior session (skips auto-created threads) Closes NousResearch#6708
|
Heads up — I've opened #7466 which takes a similar approach but generalizes it into a base class hook ( The motivation was to avoid each platform adapter reimplementing the same session-check + context-fetch + prepend pattern independently. The base class also handles command safety (skipping Happy to coordinate if you'd prefer a different approach or want to merge aspects of both. |
Add fetch_thread_context() and has_active_session_for_event() to BasePlatformAdapter, providing a single extension point for platform adapters to fetch thread/conversation history when the bot first enters an existing thread. - Refactors Slack's existing thread context fetching into the new hook - Adds Discord implementation using channel.history(), giving Discord feature parity with Slack for thread context - Session liveness check evaluates reset policy (idle/daily expiry) so expired sessions still get context seeded and don't auto-trigger without @mention - Commands (/reset, /status, etc.) are not prefixed with context - Base default returns None so unimplemented platforms are unaffected Fixes NousResearch#6708 Related to NousResearch#6712 (absorbs Discord thread context into unified pattern) Related to NousResearch#1953, NousResearch#2950, NousResearch#5816 (Slack fixes refactored into base hook)
|
Thanks for the focused Discord implementation. This is an automated hermes-sweeper review; current
The related discussion of #7466 was considered; this close is based on the independently verified behavior already present on current |
Summary
When the Hermes bot is @mentioned in an existing Discord thread for the first time, it currently starts a blank session with no awareness of the thread's prior conversation. This PR ports the Slack adapter's
_fetch_thread_context()pattern to Discord, so the agent receives the thread history as context on first mention.Closes #6708
Changes
Three additions to
gateway/platforms/discord.py:_has_active_session_for_thread()— checks the session store viabuild_session_key()to determine if a session already exists for this thread. Respectsthread_sessions_per_userandgroup_sessions_per_userconfig settings. Mirrors the Slack adapter's implementation from feat(gateway): approval buttons for Slack & Telegram + Slack thread context #5890._fetch_thread_context()— fetches up to 30 prior messages from the Discord thread viachannel.history(oldest_first=True), formats them as:Skips the bot's own messages and the current trigger message. Strips bot @mentions from context messages.
Injection in
_handle_message()— when the message is in an existing thread (not auto-created), and there's no prior session for it, calls_fetch_thread_context()and prepends the result toevent_text. Follow-up messages use the normal session transcript.Design decisions
auto_threadcreates a brand-new thread, there's no history to fetch, so we skip the API call.build_session_key()as single source of truth — avoids the manual key construction bug that was fixed in the Slack adapter (fix/slack-thread-session-key-mismatch #5833), wherethread_sessions_per_userwas ignored.[Thread context] ... [End of thread context]envelope so the agent prompt is consistent across platforms.Testing