Skip to content

feat: 支持tg平台的部分引用 - #9380

Closed
10knamesmore wants to merge 1 commit into
AstrBotDevs:masterfrom
10knamesmore:feat/support_tg_quote_message
Closed

feat: 支持tg平台的部分引用#9380
10knamesmore wants to merge 1 commit into
AstrBotDevs:masterfrom
10knamesmore:feat/support_tg_quote_message

Conversation

@10knamesmore

@10knamesmore 10knamesmore commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

close #7938
telegram平台 支持部分引用, 即引用整条消息的一部分, 接入这一点并让模型感知, 以便其可以精确定位到预期的消息中的某一段节选

Modifications / 改动点

AstrBot/astrbot/core/message/components.py:596 的 Replay 增加了一个 selected_excerpt 字段, 表示引用中的部分内容
AstrBot/astrbot/core/astr_main_agent.py:857 如果存在 selected_excerpt, 那么给模型的文本优先基于他渲染

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image image

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Support partial text quoting from Telegram and surface selected excerpts to the LLM when processing quoted messages.

New Features:

  • Add a selected_excerpt field to reply messages to represent user-selected fragments of the original message.
  • Use selected excerpts from quoted messages when constructing LLM prompts, falling back to full text when no valid excerpt is present.

Enhancements:

  • Normalize optional text values to consistently treat empty and whitespace-only strings as None.
  • Ensure quoted image captioning continues to work when partial excerpts are used in quoted messages.

Tests:

  • Add unit tests for normalize_optional_text behavior.
  • Add unit tests for processing quoted messages with and without selected excerpts.
  • Add Telegram adapter tests to cover manual text quotes, auto-generated quotes, blank quotes, and replies without quotes.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. labels Jul 25, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The prompt formatting for quoted messages (sender prefix plus either full text or <selected_excerpt> tag) is now spread between _process_quote_message and the Telegram adapter; consider extracting a small helper to build this quoted block so future changes to the tag format or sender display stay consistent across platforms.
  • In _process_quote_message, when a selected_excerpt is present you suppress the full quoted text entirely; if you expect users to sometimes want both focused and broader context, you might want to include the full text alongside the excerpt (or make this behavior configurable).
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The prompt formatting for quoted messages (sender prefix plus either full text or `<selected_excerpt>` tag) is now spread between `_process_quote_message` and the Telegram adapter; consider extracting a small helper to build this quoted block so future changes to the tag format or sender display stay consistent across platforms.
- In `_process_quote_message`, when a `selected_excerpt` is present you suppress the full quoted text entirely; if you expect users to sometimes want both focused and broader context, you might want to include the full text alongside the excerpt (or make this behavior configurable).

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@Soulter

Soulter commented Jul 25, 2026

Copy link
Copy Markdown
Member

Hi, thanks for your contribution. #9236 already implemented this feature.

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

Labels

area:core The bug / feature is about astrbot's core, backend area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Telegram 适配器:支持「部分引用」(TextQuote),让模型只看到用户实际选中的片段

2 participants