fix(telegram): honor message.quote for partial-quote reply context

When a Telegram user replies using the native quote feature to select
only part of a prior message, _build_message_event was injecting the
ENTIRE replied-to message into reply_to_text via
message.reply_to_message.text/caption. python-telegram-bot exposes
the user-selected substring as message.quote (TextQuote.text); we now
prefer that and fall back to the full replied-to text only when no
native quote is present.

The agent-visible "[Replying to: \"...\"]" prefix can otherwise expand
the user's narrow quote into the full prior message, causing the agent
to act on unrelated actionable-looking text the user did not select
(e.g. multi-item briefings where the user quotes one bullet but the
prefix injects every bullet). Falls back cleanly when message.quote
is absent (PTB <21 or replies that don't quote a substring).

Fixes #22619

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
briandevans 2026-05-09 08:14:23 -07:00 committed by Teknium
parent 78b8155ecb
commit 854c2ce309
2 changed files with 162 additions and 2 deletions

View file

@ -4026,12 +4026,28 @@ class TelegramAdapter(BasePlatformAdapter):
chat_topic=chat_topic,
)
# Extract reply context if this message is a reply
# Extract reply context if this message is a reply.
# Prefer Telegram's native partial quote (message.quote, TextQuote)
# so a user replying to a single selected substring of a prior
# multi-section message doesn't get the whole replied-to message
# injected into the agent's context — which can cause the agent
# to act on unrelated actionable-looking text the user didn't
# quote (#22619). Fall back to the full replied-to message text
# / caption when no native quote is present.
reply_to_id = None
reply_to_text = None
if message.reply_to_message:
reply_to_id = str(message.reply_to_message.message_id)
reply_to_text = message.reply_to_message.text or message.reply_to_message.caption or None
quote = getattr(message, "quote", None)
quote_text = getattr(quote, "text", None) if quote is not None else None
if quote_text:
reply_to_text = quote_text
else:
reply_to_text = (
message.reply_to_message.text
or message.reply_to_message.caption
or None
)
# Per-channel/topic ephemeral prompt
from gateway.platforms.base import resolve_channel_prompt