feat: include reply/quote context in Claude prompt#194
Open
timurvafin wants to merge 1 commit intoRichardAtCT:mainfrom
Open
feat: include reply/quote context in Claude prompt#194timurvafin wants to merge 1 commit intoRichardAtCT:mainfrom
timurvafin wants to merge 1 commit intoRichardAtCT:mainfrom
Conversation
When a user replies to a message (especially with a highlighted
fragment — Telegram's partial-quote feature, Bot API 7.0+), the bot
previously read only `update.message.text` and dropped the quoted
context, so Claude had to guess what the user was referring to.
This PR extracts the quoted fragment (partial quote has priority
over the full replied-to message) and prepends it to the user's
new text as a markdown blockquote::
> quoted fragment line 1
> quoted fragment line 2
new user text
Works for both agentic_text and classic handle_text via a shared
helper `src/bot/utils/quote_prompt.build_user_prompt`. Falls back to
plain `message.text` when there's no reply/quote, preserving existing
behaviour.
Also handles replies to media (uses `caption` when `text` is absent)
and guards against MagicMock attributes in unit-test fixtures.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a user replies to a message (especially with a highlighted fragment — Telegram's partial-quote feature, Bot API 7.0+), the bot was reading only
update.message.textand dropping the quoted context. Claude then had to guess what the user was referring to.This PR extracts the quoted fragment and prepends it to the user's new text as a markdown blockquote, so Claude sees both the fragment and the response in a single prompt.
Before vs after
Before:
After:
Design
message.quote.text(partial quote, Bot API 7.0+) wins over the fullreply_to_message.text. If the user highlighted a fragment, that's what they mean.caption.> line\n> line\n\ntext), natural for Claude.src/bot/utils/quote_prompt.py— bothagentic_textand classichandle_textgo through it, so classic mode gets the feature for free.Changes
src/bot/utils/quote_prompt.py— newbuild_user_prompt(message) -> strhelper with priority logic and markdown-blockquote rendering.src/bot/orchestrator.py—agentic_textusesbuild_user_prompt(update.message)instead ofupdate.message.text.src/bot/handlers/message.py—handle_text_message(classic) same wiring;.pdfunaffected.tests/unit/test_bot/test_quote_prompt.py— 10 tests: plain/reply/partial-quote priority/multiline/media-caption/edge cases (no-text, no-reply, empty user text).Live verification
Reply via Telegram client with highlighted fragment of Claude's previous answer; bot's database prompt now includes the blockquote; Claude's reply is contextually correct.
Test plan
make test— 540 passed (10 new)black/isort/flake8clean🤖 Generated with Claude Code