fix(wecomai_event): 修复消息链提取纯文本时的空白处理#8563
Conversation
- 增加了strip_result参数以控制是否去除首尾空白 - 流式输出时保留换行等格式字符 - 更新相关调用以适应新参数
- 测试 _extract_plain_text_from_chain 方法在流式和非流式场景下的行为 - 确保流式输出时换行符等格式字符能够正确保留 - 覆盖了不同输入场景的测试用例
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- Consider making
strip_resulta keyword-only argument in_extract_plain_text_from_chain(e.g.,def _extract_plain_text_from_chain(message_chain: MessageChain | None, *, strip_result: bool = True)) to avoid accidental positional misuse and make its semantics clearer at call sites. - The new tests around
_extract_plain_text_from_chainare quite granular and repetitive; you could reduce duplication and improve readability by parametrizing similar cases (e.g., whitespace/newline/tab behaviors) withpytest.mark.parametrizeinstead of separate test functions.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider making `strip_result` a keyword-only argument in `_extract_plain_text_from_chain` (e.g., `def _extract_plain_text_from_chain(message_chain: MessageChain | None, *, strip_result: bool = True)`) to avoid accidental positional misuse and make its semantics clearer at call sites.
- The new tests around `_extract_plain_text_from_chain` are quite granular and repetitive; you could reduce duplication and improve readability by parametrizing similar cases (e.g., whitespace/newline/tab behaviors) with `pytest.mark.parametrize` instead of separate test functions.
## Individual Comments
### Comment 1
<location path="tests/unit/test_wecomai_event.py" line_range="15-24" />
<code_context>
+
+# ============================================================
+# strip_result 参数类型安全测试
+# ============================================================
+
+
+def test_strip_result_param_is_explicit():
+ """strip_result 是显式关键字参数,不影响位置参数调用"""
+ chain = MessageChain([Plain("测试")])
+ # 不传 strip_result,使用默认值 True
+ result1 = WecomAIBotMessageEvent._extract_plain_text_from_chain(chain)
+ # 显式传 strip_result=True
+ result2 = WecomAIBotMessageEvent._extract_plain_text_from_chain(chain, True)
+ assert result1 == result2 == "测试"
+
+
+def test_strip_result_false_prevents_whitespace_loss():
+ """验证 strip_result 参数机制:False 时保留空白,True 时去除"""
+ text = " 内容 "
+ chain = MessageChain([Plain(text)])
+
+ stripped = WecomAIBotMessageEvent._extract_plain_text_from_chain(chain, True)
+ notStripped = WecomAIBotMessageEvent._extract_plain_text_from_chain(chain, False)
+
+ assert stripped == "内容"
+ assert notStripped == " 内容 "
+ assert stripped != notStripped
</code_context>
<issue_to_address>
**issue (testing):** Add end-to-end tests around `send_streaming` / `enqueue_stream_plain` to ensure they actually use `strip_result=False` and preserve newlines in real flows.
Current tests validate `_extract_plain_text_from_chain` directly, but the original regression was in the streaming callers (`send_streaming` / `enqueue_stream_plain`) where `strip_result=False` is passed. If that argument is later removed or changed at the call sites, these tests could still pass.
Please add at least one async test for each of `send_streaming` and `enqueue_stream_plain` that:
- sends chunked content with `\n` (and optionally `\t`) through the real streaming path,
- inspects the final text or mocked downstream payload,
- asserts that newlines and formatting characters are preserved.
That will protect the actual streaming behavior, not just the helper function.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
There was a problem hiding this comment.
Code Review
This pull request introduces a strip_result parameter to _extract_plain_text_from_chain in wecomai_event.py to allow preserving formatting and newlines during streaming, and adds comprehensive unit tests to verify this behavior. The feedback suggests renaming a variable in the tests to comply with PEP 8 naming conventions.
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.
- 移除测试文件 test_wecomai_event.py,包含多个针对 _extract_plain_text_from_chain 方法的测试用例 - 测试用例涵盖了流式和非流式场景下的文本提取行为
Modifications / 改动点
strip_result参数以控制是否需要去除首尾空白close #8474
Screenshots or Test Results / 运行截图或测试结果
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.
/ 我的更改没有引入恶意代码。