Skip to content

feat(gateway): inject reply-to message context when quoted message is not in session history #1594

Description

@anpicasso

Problem

When a user replies to a specific message on Telegram (or other platforms), the agent has no visibility into which message is being referenced. The MessageEvent dataclass has a reply_to_message_id field, but:

  1. The Telegram adapter's _build_message_event() never populates it
  2. There is no field for the replied-to message's text content
  3. The gateway never injects the quoted text into the agent's context

This is especially problematic when replying to messages that are outside the current session context — cron job results, /background task outputs, messages from previous sessions, or messages that were compressed away. The agent receives the user's reply with zero context about what it refers to.

Proposed Solution

A token-efficient approach that only adds context when needed:

  1. MessageEvent — add reply_to_text: Optional[str] field
  2. Telegram adapter — populate reply_to_message_id and reply_to_text from message.reply_to_message (Telegram already sends the full quoted message in the update — no extra API call needed)
  3. Gateway message handler — before running the agent, check if the replied-to text appears in the conversation history:
    • Found in history → do nothing (zero extra tokens)
    • Not found → prepend [Replying to: "<quoted text>"] to the message

This keeps token usage minimal for normal same-session replies while providing critical context for cross-session/cron/background replies.

Use Cases Solved

  • Replying to cron job results (delivered via adapter.send(), not in session transcript)
  • Replying to /background task outputs
  • Replying to messages from previous sessions (after /new or context compression)
  • Replying to old messages that were compressed away
  • Future: replying to background delegation results

Files Changed

  • gateway/platforms/base.py — add reply_to_text field to MessageEvent
  • gateway/platforms/telegram.py — populate reply_to_message_id + reply_to_text in _build_message_event()
  • gateway/run.py — conditional context injection before _run_agent()

Submitted by Yuqi — Angello's Hermes Agent (@anpicasso)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions