Skip to content

perf(OneBot): add a whitespace after At component#6238

Merged
Soulter merged 1 commit into
AstrBotDevs:masterfrom
MousseC:fix/at-space-loss
Mar 14, 2026
Merged

perf(OneBot): add a whitespace after At component#6238
Soulter merged 1 commit into
AstrBotDevs:masterfrom
MousseC:fix/at-space-loss

Conversation

@MousseC

@MousseC MousseC commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

修复 #6237:发送 At 消息时后面的空格丢失的问题。

Modifications / 改动点

修改了 astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py

  1. 添加 At 组件到 import
  2. _parse_onebot_json 方法中,仅在检测到 At 组件存在,才在其后插入一个空格,最小化干涉
  3. 由于Plain.toDict()中包含strip(),每个plain前后都不会有额外的空格,不会有双重空格问题
  • This is NOT a breaking change. / 这不是一个破坏性变更。

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

修复前:

  • AstrBot 日志:修好 [At:xxxxxx] 了吗(有空格)
  • NapCat 收到:修好 @xxx(xxxxx)了吗(空格丢失)

修复后:

  • NapCat 收到:修好 @xxx(xxxxx) 了吗(空格保留)

Checklist / 检查清单

  • 😊 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。/ If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
  • 👀 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。/ My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
  • 🤓 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到了 requirements.txtpyproject.toml 文件相应位置。/ 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.
  • 😮 我的更改没有引入恶意代码。/ My changes do not introduce malicious code.

Summary by Sourcery

Bug Fixes:

  • Fix loss of the space following an At mention when sending messages via the aiocqhttp OneBot adapter.

修复 At 组件后的空格在发送时被 strip 移除的问题。在消息解析时检测 At 组件并在其后额外插入空格。
@auto-assign auto-assign Bot requested review from LIghtJUNction and Soulter March 13, 2026 23:09
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 13, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

此拉取请求旨在解决一个影响消息格式的缺陷,即在将包含 At 提及的消息转换为 OneBot JSON 格式时,At 组件后的空格会丢失。通过在消息处理逻辑中明确地为 At 组件添加一个空格,此更改确保了消息内容的准确性和一致性,从而提升了用户在不同平台间发送消息的体验。

Highlights

  • 修复 At 消息后空格丢失问题: 解决了在将消息链转换为 OneBot JSON 格式时,At 消息组件后的空格会意外丢失的错误,确保了消息的正确显示。
  • 增强 At 组件处理逻辑: 在 _parse_onebot_json 方法中,为 At 组件添加了专门的处理逻辑,在其 OneBot JSON 表示后额外插入一个空格文本段,以避免与后续文本粘连。
  • 提升消息格式一致性: 此修复确保了包含 At 提及的消息在发送到 OneBot 平台时能保持预期的格式和可读性。
Changelog
  • astrbot/core/platform/sources/aiocqhttp/aiocqhttp_message_event.py
    • 导入了 At 消息组件。
    • 修改了 _parse_onebot_json 方法,以在检测到 At 组件时,在其 OneBot JSON 表示后插入一个空格文本段。
    • 重构了 _parse_onebot_json 方法的条件判断,增加了对其他消息段的通用处理逻辑。
Activity
  • 没有检测到任何活动。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@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 logic for handling At segments largely duplicates the common _from_segment_to_dict + ret.append path; consider restructuring the loop to call this once per segment and only conditionally append the extra space to reduce branching and repetition.
  • Unconditionally inserting a space after every At may introduce double spaces if the following Plain segment already begins with whitespace; consider peeking at the next segment or the next text’s leading character(s) so the behavior is more context-aware.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The logic for handling `At` segments largely duplicates the common `_from_segment_to_dict` + `ret.append` path; consider restructuring the loop to call this once per segment and only conditionally append the extra space to reduce branching and repetition.
- Unconditionally inserting a space after every `At` may introduce double spaces if the following `Plain` segment already begins with whitespace; consider peeking at the next segment or the next text’s leading character(s) so the behavior is more context-aware.

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.

@dosubot dosubot Bot added the area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. label Mar 13, 2026

@gemini-code-assist gemini-code-assist 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.

Code Review

The pull request introduces support for the At message component within the aiocqhttp_message_event.py file. It imports the At component and modifies the _parse_onebot_json function to correctly process At components by appending a space after them to prevent text concatenation issues. The parsing logic for message segments has also been generalized to ensure all component types are handled.

@MousseC

MousseC commented Mar 14, 2026

Copy link
Copy Markdown
Contributor Author

Hey - I've left some high level feedback:

  • The logic for handling At segments largely duplicates the common _from_segment_to_dict + ret.append path; consider restructuring the loop to call this once per segment and only conditionally append the extra space to reduce branching and repetition.
  • Unconditionally inserting a space after every At may introduce double spaces if the following Plain segment already begins with whitespace; consider peeking at the next segment or the next text’s leading character(s) so the behavior is more context-aware.

Prompt for AI Agents

Please address the comments from this code review:

## Overall Comments
- The logic for handling `At` segments largely duplicates the common `_from_segment_to_dict` + `ret.append` path; consider restructuring the loop to call this once per segment and only conditionally append the extra space to reduce branching and repetition.
- Unconditionally inserting a space after every `At` may introduce double spaces if the following `Plain` segment already begins with whitespace; consider peeking at the next segment or the next text’s leading character(s) so the behavior is more context-aware.

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.

在 _parse_onebot_json 方法中,仅在检测到 At 组件存在,才在其后插入一个空格,最小化干涉。
由于Plain.toDict()中包含strip(),每个plain前后都不会有额外的空格,不会有双重空格问题。

@Soulter Soulter changed the title fix: 修复 At 消息后空格丢失 perf(OneBot): add a whitespace after At component Mar 14, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Mar 14, 2026
@Soulter Soulter merged commit 89fbd75 into AstrBotDevs:master Mar 14, 2026
6 checks passed
@MousseC

MousseC commented Mar 14, 2026

Copy link
Copy Markdown
Contributor Author

@Soulter Hi!我想确认一下,目前我的PR被合并到master是不是预期内的?
我刚才检查时看到,还有一个已经合并PR #6244 选择从更上游的toDict方法直接删除了strip(),#6244 这个做法等于是将添加空格的选择权交给了各个plain或者插件。

那么我们目前的这个PR只是一个业务设计的选择:
我们还需要专门在_parse_onebot_json内部刻意往At插件后面插入一个空格,确保管线处理后输出的At至少有一个空格吗?
或者将添加空格的选择权交给Plain,不额外插入空格?

如果你最终决定不打算额外插入空格,我也很乐意revert此分支 ;)

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

Labels

area:platform The bug / feature is about IM platform adapter, such as QQ, Lark, Telegram, WebChat and so on. lgtm This PR has been approved by a maintainer size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants