fix(weixin_offacc): 修复 active_send_mode 下 buffer 缺失导致消息丢弃#8731
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces an active send mode in the WeChat Official Account adapter's handle_msg method, enabling direct message delivery via the WeChat Customer Service API and bypassing the passive reply buffering mechanism. The reviewer suggested refactoring the code to unify the event creation and submission logic, which is currently duplicated between the active and passive sending modes.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| # === 主动发送模式 === | ||
| # 回复通过微信客服 API (client.message.send_text) 直接推送, | ||
| # 不依赖被动回复的缓冲机制。传入空字典作为 message_out 占位。 | ||
| if self.active_send_mode: | ||
| message_event = WeixinOfficialAccountPlatformEvent( | ||
| message_str=message.message_str, | ||
| message_obj=message, | ||
| platform_meta=self.meta(), | ||
| session_id=message.session_id, | ||
| client=self.client, | ||
| message_out={}, | ||
| ) | ||
| self.commit_event(message_event) | ||
| return | ||
|
|
||
| # === 被动回复模式 === | ||
| # 必须有缓冲状态来暂存 LLM 的异步回复,以应对微信 5 秒响应期限。 | ||
| buffer = self.user_buffer.get(message.sender.user_id, None) |
There was a problem hiding this comment.
在 handle_msg 方法中,主动发送模式和被动回复模式下创建 WeixinOfficialAccountPlatformEvent 并提交事件的逻辑存在重复。可以通过在 active_send_mode 下直接将 buffer 赋值为空字典 {},然后统一在方法末尾创建并提交事件,从而避免代码重复,提高代码的可维护性。
if self.active_send_mode:
buffer = {}
else:
# === 被动回复模式 ===
# 必须有缓冲状态来暂存 LLM 的异步回复,以应对微信 5 秒响应期限。
buffer = self.user_buffer.get(message.sender.user_id, None)References
- 避免在不同模式下重复编写相同的事件创建和提交逻辑,应尽量复用主体代码以减少冗余。
There was a problem hiding this comment.
好哩,已采纳。buffer = {} 是局部临时变量,不会残留到 user_buffer,避免了假 buffer 方案的内存泄漏和持久化问题。事件构造逻辑已统一为一处喵。
当 active_send_mode=True 时,handle_callback 在 L160 提前 return,
跳过了 user_buffer 的创建(L256),但 handle_msg 无条件要求 buffer
存在,导致所有用户消息在适配器层被静默丢弃。
修复:在 handle_msg 中按 active_send_mode 分叉,主动模式下直接
提交事件(message_out={} 在 send() 中不被访问,完全安全)。
Closes AstrBotDevs#6182
Closes AstrBotDevs#7566
05179c6 to
8b076a1
Compare
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- In the active_send_mode branch you currently set
buffer = {}; if downstream logic ever assumes a specific buffer structure this could break silently, so consider encapsulating this as a well-typed sentinel (e.g., a small dataclass or a named constant) or adding a type hint/assert to make the expected shape explicit. - Given that active_send_mode bypasses the buffer check entirely, it may be worth adding a short inline comment near where
bufferis later consumed (or an assertion in the active path) to guard against future refactors accidentally reintroducing buffer-dependent behavior for active mode.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the active_send_mode branch you currently set `buffer = {}`; if downstream logic ever assumes a specific buffer structure this could break silently, so consider encapsulating this as a well-typed sentinel (e.g., a small dataclass or a named constant) or adding a type hint/assert to make the expected shape explicit.
- Given that active_send_mode bypasses the buffer check entirely, it may be worth adding a short inline comment near where `buffer` is later consumed (or an assertion in the active path) to guard against future refactors accidentally reintroducing buffer-dependent behavior for active mode.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
当 active_send_mode=True 时,handle_callback 在 L160 提前 return,跳过了 user_buffer 的创建(L256),但 handle_msg 无条件要求 buffer 存在,导致所有用户消息在适配器层被静默丢弃。
修复:在 handle_msg 中按 active_send_mode 分叉,主动模式下直接提交事件(message_out={} 在 send() 中不被访问,完全安全)。被动模式行为零变化。
Closes #6182
Closes #7566
Modifications / 改动点
仅修改
astrbot/core/platform/sources/weixin_official_account/weixin_offacc_adapter.py的handle_msg方法(+17 行,零删除)。active_send_mode=True时直接提交事件,message_out={}(send()方法在主动模式下通过client.message.send_text()直接推送,不访问message_out)模式切换安全性:用户通过 Dashboard 切换
active_send_mode后,PlatformManager.reload()完整销毁旧 adapter 并创建新实例,不存在缓存属性过期问题。Screenshots or Test Results / 运行截图或测试结果
验证环境
active_send_mode: True验证步骤
docker restart astrbot修复前(历史日志)
用户
o9N_E0jYIBH4NPLCvjQIgr5z1c6I(此前 4 次 CRITICAL)发送消息时的典型日志:消息在适配器层被丢弃,无 event_bus、无 respond.stage。
修复后(同一用户
o9N_E0jYIBH4NPLCvjQIgr5z1c6I测试)验证结论
event_busrespond.stage并准备发送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.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: