diff --git a/astrbot/core/platform/sources/wecom_ai_bot/wecomai_event.py b/astrbot/core/platform/sources/wecom_ai_bot/wecomai_event.py index f27d4671e5..74d120f5f3 100644 --- a/astrbot/core/platform/sources/wecom_ai_bot/wecomai_event.py +++ b/astrbot/core/platform/sources/wecom_ai_bot/wecomai_event.py @@ -125,7 +125,17 @@ async def _send( return data @staticmethod - def _extract_plain_text_from_chain(message_chain: MessageChain | None) -> str: + def _extract_plain_text_from_chain( + message_chain: MessageChain | None, + strip_result: bool = True, + ) -> str: + """从消息链中提取纯文本 + + Args: + message_chain: 消息链 + strip_result: 是否去除首尾空白。流式输出时应设为 False, + 以保留换行等格式字符;非流式发送时可保留默认 True + """ if not message_chain: return "" plain_parts: list[str] = [] @@ -134,7 +144,8 @@ def _extract_plain_text_from_chain(message_chain: MessageChain | None) -> str: plain_parts.append(f"@{comp.name} ") elif isinstance(comp, Plain): plain_parts.append(comp.text) - return "".join(plain_parts).strip() + result = "".join(plain_parts) + return result.strip() if strip_result else result async def send(self, message: MessageChain | None) -> None: """发送消息""" @@ -254,7 +265,10 @@ async def send_streaming(self, generator, use_fallback=False) -> None: ) chain.squash_plain() - chunk_text = self._extract_plain_text_from_chain(chain) + # 流式输出不 strip,保留换行等格式字符 + chunk_text = self._extract_plain_text_from_chain( + chain, strip_result=False + ) if chunk_text: increment_plain += chunk_text now = asyncio.get_running_loop().time() @@ -334,7 +348,8 @@ async def enqueue_stream_plain(text: str) -> None: increment_plain = "" continue - chunk_text = self._extract_plain_text_from_chain(chain) + # 流式输出不 strip,保留换行等格式字符 + chunk_text = self._extract_plain_text_from_chain(chain, strip_result=False) if chunk_text: increment_plain += chunk_text final_data += chunk_text