feat(discord): include reply/quote context in agent prompt (#339)#527
feat(discord): include reply/quote context in agent prompt (#339)#527ChunHao-dev wants to merge 2 commits intoopenabdev:mainfrom
Conversation
OpenAB PR ScreeningThis is auto-generated by the OpenAB project-screening flow for context collection and reviewer handoff.
Screening report## IntentThis PR fixes a Discord conversation-context gap: when a user replies to or quotes an earlier message in a thread, the agent currently only sees the new message text and loses the referenced message content. That makes prompts like “summarize this” or “answer that” ambiguous and degrades response quality for Discord users. FeatThis is a feature-sized fix to Discord prompt assembly. It adds reply/quote context to the agent prompt by resolving the referenced Discord message, formatting it into a quote block, and prepending it to the user’s new message before routing to the agent. Non-reply messages remain unchanged. Who It ServesThe primary beneficiary is Discord end users interacting with OpenAB agents in threads and reply chains. Secondarily, it helps maintainers and reviewers by making agent behavior more predictable and aligned with how users naturally converse in Discord. Rewritten PromptUpdate the Discord adapter so reply messages include the referenced message content in the prompt sent to the agent. Requirements:
Merge PitchThis is worth moving forward because it fixes a real prompt-quality issue in a core user interaction path without changing the broader routing model. The risk profile is low to moderate: behavior is isolated to Discord reply handling, but reviewers will likely want to confirm prompt formatting is stable, API fallback is safe, and quoted context does not introduce noisy or misleading prompt injection in edge cases. Best-Practice ComparisonOpenClaw principles:
Hermes Agent principles:
Overall:
Implementation Options
Comparison Table
RecommendationThe balanced option is the right path for merge discussion. It solves the actual user problem reliably, keeps the change scoped to the Discord adapter, and avoids overbuilding a general context system before the project has validated the need for broader prompt-enrichment rules. If this moves forward, the likely follow-up split is:
|
6324187 to
1310f68
Compare
Fix: resolve mentions in quoted message contentThe quoted message content from Change: Added Some(quoted) => {
let quoted_content = resolve_mentions("ed.content, bot_id);
format_quote_context("ed.author.name, "ed_content, &prompt)
}Minimal change — 4 lines added, 1 removed. |
Quoted message content was injected into the prompt without running resolve_mentions(), leaking raw Discord mention markup (<@BOT_ID>, <@&ROLE_ID>) into the LLM prompt. Apply the same resolve_mentions() pass used for the user's own message content.
a3f8f2c to
9dfd955
Compare
masami-agent
left a comment
There was a problem hiding this comment.
PR Review: #527
Summary
- Problem: When a user replies to (quotes) a message in a Discord thread, the quoted content is lost — the agent only sees the new message text with no context about what "this" refers to.
- Approach: Read
msg.referenced_message(gateway-provided, zero cost) with HTTP API fallback, prepend formatted quote block to the prompt before sending to the ACP agent. - Risk level: Low
Core Assessment
- Problem clearly stated: ✅ — well-documented in both issue #339 and PR description
- Approach appropriate: ✅ — two-tier resolution (gateway cache → HTTP fallback) is the correct pattern for serenity 0.12
- Alternatives considered: ✅ — the gateway-first + HTTP-fallback design is explicitly documented
- Best approach for now: ✅ — minimal, focused, non-breaking
Findings
Code correctness:
resolve_referenced_message()correctly handles serenity 0.12 types:message_reference.channel_idisChannelId(non-optional),message_idisOption<MessageId>— the?operator usage is correct.*referenced.clone()dereferences theBox<Message>— correct pattern forOption<Box<Message>>.resolve_mentions()is applied to the quoted content, which correctly strips bot mentions from the quoted text too. Good attention to detail.- Insertion point is correct: after
resolve_mentions()on the user's own message, before the empty-check gate. This means a reply with empty user text but non-empty quoted content will still be processed — which is the right behavior. - The
tracing::warn!on HTTP fetch failure with structured fields follows the project's existing logging pattern.
format_quote_context() design:
- Pure function, easy to test — good separation.
- Empty
quoted_contentreturns prompt unchanged — correct guard. - The format
[Quoted message from @{author_name}]:\n{content}\n\n{prompt}is clean and gives the agent clear context about who said what.
Review Summary
🔧 Suggested Changes
- Consider adding a length cap on quoted content. If someone quotes a very long message (e.g., a full code dump from the bot), the entire thing gets prepended to the prompt. A reasonable truncation (e.g., first 2000 chars with a
[truncated]marker) would prevent unexpectedly large prompts. Not blocking — this can be a follow-up.
ℹ️ Info
- This only handles single-level quoting (the direct
referenced_message). Nested quotes (quoting a message that itself was a reply) won't include the deeper context. This is fine for now — Discord's own UI only shows one level of reply context. - The HTTP fallback path (
channel_id.message(http, message_id)) counts against Discord's rate limit. In practice this should be rare since the gateway almost always includesreferenced_message, but worth noting for awareness.
⚪ Nits
- None — code is clean and well-structured.
Verdict
APPROVE — Clean, focused implementation. Single file changed, 4 unit tests, all 109 existing tests pass, CI green across all 7 smoke-test variants. The code correctly handles serenity 0.12 types, follows existing project patterns, and the insertion point is well-chosen. Ready for maintainer review.
obrutjack
left a comment
There was a problem hiding this comment.
Reviewed. Clean, focused implementation — gateway-first with HTTP fallback, resolve_mentions applied to quoted content, good test coverage. LGTM.
Summary
When a user replies to (quotes) a message in a Discord thread, the bot only sends the new message text to the agent. The quoted/referenced message content is lost — the agent has no idea what "this" refers to.
This PR reads
msg.referenced_messageand prepends the quoted content to the prompt:Implementation
resolve_referenced_message()— prefers gateway-providedreferenced_message(zero cost); falls back to HTTP API call viamessage_referenceif the gateway didn't include the full messageformat_quote_context()— pure function that formats the quote blockresolve_mentions(), before the prompt is sent to the routerresolve_referenced_messagereturnsNone)Testing
format_quote_context()(normal, empty content, empty prompt, multiline)Closes #339
Discord Discussion URL: https://discord.com/channels/1491295327620169908/1496538680142069800