Skip to content

fix(discord): fetch and inject reply context for incoming messages#34058

Closed
alaamohanad169-ship-it wants to merge 3 commits into
NousResearch:mainfrom
alaamohanad169-ship-it:fix/discord-reply-context
Closed

fix(discord): fetch and inject reply context for incoming messages#34058
alaamohanad169-ship-it wants to merge 3 commits into
NousResearch:mainfrom
alaamohanad169-ship-it:fix/discord-reply-context

Conversation

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor

Summary

Fetches and injects reply context for incoming Discord messages so the agent knows what message a user is replying to.

Changes

  • plugins/platforms/discord/adapter.py: Added reply context fetching (14 lines)

Testing

  • Rebased onto latest main (file moved from gateway/platforms/discord.py to plugins/platforms/discord/adapter.py)

@liuhao1024

Copy link
Copy Markdown
Contributor

I found one issue worth fixing before merge.

plugins/platforms/discord/adapter.py:~4862 — off-by-one in reply context truncation

The truncation logic produces inconsistent lengths:

reply_context = f'[Replying to: "{reply_to_text[:200]}"]\n' if len(reply_to_text) <= 200 else f'[Replying to: "{reply_to_text[:200]}..."]\n'
  • 200-char message → displays all 200 chars, no ellipsis (total ~216 chars in context)
  • 201-char message → truncates to 200 chars + "..." → 203 chars displayed (total ~219 chars)

When len(reply_to_text) > 200, the [:200] slice keeps 200 characters, then "..." is appended, making the visible portion 203 characters — exceeding the intended 200-char budget.

Suggested fix:

if reply_to_text:
    max_len = 200
    if len(reply_to_text) <= max_len:
        reply_context = f'[Replying to: "{reply_to_text}"]\n'
    else:
        reply_context = f'[Replying to: "{reply_to_text[:max_len - 3]}..."]\n'
    event_text = reply_context + event_text

This ensures the displayed reply text never exceeds 200 characters including the ellipsis.

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

🔄 Re-triggering CI — test (6) may have been transient.

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

Fixed — thanks @liuhao1024 for catching the off-by-one.

Changed from:

reply_context = f'[Replying to: "{reply_to_text[:200]}"]\n' if len(reply_to_text) <= 200 else f'[Replying to: "{reply_to_text[:200]}..."]\n'

To:

truncated = reply_to_text[:200]
suffix = "…" if len(reply_to_text) > 200 else ""
reply_context = f'[Replying to: "{truncated}{suffix}"]\n'

Now always truncates at 200 chars and only appends the ellipsis when the text actually exceeds 200 chars. Force-pushed the fix.

@alaamohanad169-ship-it alaamohanad169-ship-it force-pushed the fix/discord-reply-context branch from 0f272dd to 03a6f77 Compare May 29, 2026 10:26
@alt-glitch alt-glitch added type/bug Something isn't working P2 Medium — degraded but workaround exists platform/discord Discord bot adapter comp/plugins Plugin system and bundled plugins duplicate This issue or pull request already exists labels May 29, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Duplicate of #30950 — 6th resubmit by same author (alaamohanad169-ship-it). Chain: #30085#30950#31432#31438#31447#31571 → this PR. All fix #30049. Competing with #5348 (TTL cache approach).

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

Not a duplicate of #30950. That was an earlier version of this fix on an old branch. This PR (#34058) is the rebased version onto latest main with the off-by-one truncation fix included. The competing PR #5348 by luinbytes takes a different approach (TTL cache). Both can coexist — they fix the same issue differently.

@alaamohanad169-ship-it alaamohanad169-ship-it force-pushed the fix/discord-reply-context branch from 03a6f77 to 90ebe21 Compare May 29, 2026 11:09
@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

Not a duplicate resubmit. The chain #30085#30950#31432#31438#31447#31571 were all closed without merging because they were stale (far behind upstream main). Each was a rebase attempt, not a merge.

PR #34058 is the current, clean version rebased onto latest main with:

  • Reply context fetching when Discord doesn't provide resolved message inline
  • Proper truncation (200-char budget including ellipsis, per liuhao1024's review)

The competing PR #5348 by luinbytes takes a different approach (TTL cache). Both can coexist — they fix the same issue differently.

@alaamohanad169-ship-it alaamohanad169-ship-it force-pushed the fix/discord-reply-context branch from 90ebe21 to 721dfc7 Compare May 29, 2026 14:48
Reviewer liuhao1024 pointed out the truncation was inconsistent:
- 200-char message showed no ellipsis
- 201-char message showed 200 chars + '...'

Fix: always truncate at 200, append '…' only when text exceeds 200 chars.
When reply_to_text > 200 chars, the old code truncated to 200 chars
then appended '...', making the visible portion 203 chars — exceeding
the intended 200-char budget.

Fix: truncate to 197 chars + '...' = 200 chars total when overflowing.

Review feedback from liuhao1024 on PR NousResearch#34058.
@alaamohanad169-ship-it alaamohanad169-ship-it force-pushed the fix/discord-reply-context branch from 721dfc7 to 67cc37e Compare May 30, 2026 14:44
@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

🤖 Status Update — Duplicate Flag Review

The duplicate label references #30950, but that PR has been CLOSED since 2026-05-24 and never merged. This PR (#34058) is the live, rebased fix on latest main with the off-by-one truncation fix incorporated.

cc @alt-glitch — would you mind removing the duplicate label so this can proceed to review? All CI is green and mergeable.

@dsameer0-code

dsameer0-code commented Jun 1, 2026 via email

Copy link
Copy Markdown

@alaamohanad169-ship-it

Copy link
Copy Markdown
Contributor Author

Hi, bumping this PR — the label was applied based on #30950, but #30950 was closed without merge on May 24.

This PR provides a distinct fix: it fetches and injects reply context (the referenced message content) for incoming Discord messages. The referenced PR #30950 was a different approach and was closed.

Could the duplicate label be removed so this can get reviewed? Happy to rebase if needed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/plugins Plugin system and bundled plugins duplicate This issue or pull request already exists P2 Medium — degraded but workaround exists platform/discord Discord bot adapter type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants