Skip to content

Populate reply_to_text for Discord message replies#5348

Open
luinbytes wants to merge 1 commit into
NousResearch:mainfrom
luinbytes:fix/discord-reply-context
Open

Populate reply_to_text for Discord message replies#5348
luinbytes wants to merge 1 commit into
NousResearch:mainfrom
luinbytes:fix/discord-reply-context

Conversation

@luinbytes

@luinbytes luinbytes commented Apr 5, 2026

Copy link
Copy Markdown

Summary

Discord replies pass reply_to_message_id but never populate reply_to_text. Other platforms (Telegram, Feishu, WeCom) already set this field, and run.py (line 2750) injects it as [Replying to: "..."] context when the quoted message isn't in session history. Discord just skips this entirely.

What it does

On incoming Discord messages with message.reference:

  1. Check reference.resolved (cached by discord.py, no API call)
  2. Check in-memory reply cache (avoids redundant API calls for repeated references)
  3. Fall back to channel.fetch_message() for older messages
  4. Include the author's display name in the reply text
  5. Fall back to message caption for media-only messages

In-memory reply cache

Repeated replies to the same message no longer trigger separate API calls. The cache stores (author_name, text, timestamp) per message ID, with a 5-minute TTL and 500-entry cap. Pruning follows the same pattern as the existing _seen_messages dedup cache.

Author attribution

reply_to_text includes the author's display name (e.g. "Lu: can you see that i replied?") so the agent knows who wrote the original message. This is baked into the text field rather than requiring changes to MessageEvent or run.py.

Edge cases

  • message_id is None on partial references (deleted messages) — guarded, reply_to_text stays None
  • resolved_reference is None for older messages — fetched from API and cached
  • Fetch fails (deleted message, permissions) — logged at debug, reply_to_text stays None
  • Bot messages, system messages, messages with only embeds — content is empty, filtered to None
  • Media messages with captions — caption used as fallback text
  • Cache overflow — pruned to TTL, capped at 500 entries

@Helmi

Helmi commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Nice approach — the three-tier lookup (resolved → cache → API fetch) is exactly right, and the author attribution makes it much more useful than a raw quote.

@teknium1 this is a genuinely missing feature for Discord interactions — replies to cron notifications and similar messages lose all context. Happy to help rebase if there are conflicts, or test anything further. Would be great to get this moving.

  messages to avoid redundant API calls
- Include author display_name in reply_to_text so the agent knows
  who wrote the original message
- Fall back to message caption for replies to media-only messages
- Guard against message_id being None on partial references
@luinbytes luinbytes force-pushed the fix/discord-reply-context branch from 51520ea to cfa1f91 Compare April 13, 2026 16:08
@luinbytes

Copy link
Copy Markdown
Author

bump — rebased onto latest upstream, all conflicts resolved. @teknium1

@Gerkinfeltser Gerkinfeltser mentioned this pull request Apr 15, 2026
7 tasks
Gerkinfeltser pushed a commit to Gerkinfeltser/hermes-agent that referenced this pull request Apr 16, 2026
…e is absent

When a thread is created via 'Create Thread' (right-click), messages inside
the thread have no message.reference field. Fall back to
message.channel.message_id (the thread's origin message ID) to fetch the
OP content and populate reply_to_text.

Fixes: NousResearch#5348
@alt-glitch alt-glitch added type/feature New feature or request P2 Medium — degraded but workaround exists platform/discord Discord bot adapter comp/gateway Gateway runner, session dispatch, delivery labels May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists platform/discord Discord bot adapter type/feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants