Skip to content

Conversation

@Venus-Yim
Copy link
Contributor

@Venus-Yim Venus-Yim commented Dec 22, 2025

解决else 分支中对未知消息类型毫无防御,直接索引ComponentTypes[t],导致新类型markdown类信息报错并炸掉事件管道,且对应群聊单群永久不响应全部插件(除非重启);尝试支持markdown类型进行支持但未经过测试。

Modifications / 改动点

对astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py中的函数_convert_handle_message_event做了健壮修改和对新类型markdown支持的尝试。

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

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


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

以更稳健的方式处理不受支持的 aiocqhttp 消息段类型,避免破坏消息管道,并增加对 Markdown 消息段的初步支持。

Bug Fixes:

  • 当遇到未知的消息段类型时,通过记录日志并跳过这些段来防止崩溃,而不是盲目访问组件注册表索引。

Enhancements:

  • 为 Markdown 消息段增加初步处理逻辑,将其内容转换为文本组件。
  • 记录不受支持或解析失败的消息段,以便调试,同时不打断正常的处理流程。
Original summary in English

Summary by Sourcery

Handle unsupported aiocqhttp message segment types more defensively to avoid breaking the message pipeline and add initial support for markdown segments.

Bug Fixes:

  • Prevent crashes when encountering unknown message segment types by skipping them with logging instead of blindly indexing the component registry.

Enhancements:

  • Add preliminary handling for markdown message segments by converting their content into text components.
  • Log unsupported or failed-to-parse message segments to aid in debugging without interrupting normal processing.

解决else 分支中对未知消息类型毫无防御,直接索引ComponentTypes[t],导致新类型markdown类信息报错并炸掉事件管道,且对应群聊单群永久不响应插件;尝试支持markdown类型进行支持但未经过测试
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey - 我给出了一些总体反馈:

  • 在新的 elif t == "markdown" 分支中,m 在当前作用域中被引用但没有定义(不同于遍历 for m in m_groupat 分支),所以这里同样很可能需要对 m_group 进行循环,以避免出现 NameError,并正确处理多个 markdown 片段。
  • for m in m_group 循环内部的 if t not in ComponentTypes 检查会对每个片段重复执行;可以考虑把这个保护性检查移到循环外部,这样每个分组只检查一次类型,如果不支持就尽早返回。
给 AI Agent 的提示
Please address the comments from this code review:

## Overall Comments
- In the new `elif t == "markdown"` branch, `m` is referenced without being defined in that scope (unlike the `at` branch which iterates `for m in m_group`), so you likely need a loop over `m_group` there as well to avoid a `NameError` and to handle multiple markdown segments correctly.
- The `if t not in ComponentTypes` check inside the `for m in m_group` loop runs redundantly for every segment; consider moving this guard outside the loop so you only check the type once per group and return early if unsupported.

Sourcery 对开源项目是免费的——如果你觉得我们的评审有帮助,欢迎分享 ✨
帮我变得更有用!请在每条评论上点一下 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English

Hey - I've left some high level feedback:

  • In the new elif t == "markdown" branch, m is referenced without being defined in that scope (unlike the at branch which iterates for m in m_group), so you likely need a loop over m_group there as well to avoid a NameError and to handle multiple markdown segments correctly.
  • The if t not in ComponentTypes check inside the for m in m_group loop runs redundantly for every segment; consider moving this guard outside the loop so you only check the type once per group and return early if unsupported.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In the new `elif t == "markdown"` branch, `m` is referenced without being defined in that scope (unlike the `at` branch which iterates `for m in m_group`), so you likely need a loop over `m_group` there as well to avoid a `NameError` and to handle multiple markdown segments correctly.
- The `if t not in ComponentTypes` check inside the `for m in m_group` loop runs redundantly for every segment; consider moving this guard outside the loop so you only check the type once per group and return early if unsupported.

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.

@Venus-Yim
Copy link
Contributor Author

bug产生的报错信息如下:
Task exception was never retrieved
future: <Task finished name='Task-22451' coro=<CQHttp._handle_event_with_response() done, defined at /usr/local/lib/python3.11/site-packages/aiocqhttp/init.py:604> exception=KeyError('markdown')>
Traceback (most recent call last):
File "/usr/local/lib/python3.11/site-packages/aiocqhttp/init.py", line 606, in _handle_event_with_response
response = await self._handle_event(payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiocqhttp/init.py", line 599, in _handle_event
filter(lambda r: r is not None, await
^^^^^
File "/usr/local/lib/python3.11/site-packages/aiocqhttp/bus.py", line 60, in emit
results += await run_async_funcs(self._subscribers[event], *args,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/aiocqhttp/utils.py", line 40, in run_async_funcs
results += await asyncio.gather(*coros)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/AstrBot/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py", line 78, in group
abm = await self.convert_message(event)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/AstrBot/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py", line 115, in convert_message
abm = await self._convert_handle_message_event(event)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/AstrBot/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py", line 390, in _convert_handle_message_event
a = ComponentTypest
~~~~~~~~~~~~~~^^^
KeyError: 'markdown'

@Soulter Soulter changed the title Handle unsupported message types with logging perf: handle unsupported message types with logging in OneBot adapter Dec 23, 2025
@Soulter Soulter merged commit d853bfd into AstrBotDevs:master Dec 23, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants