From 028448fd4b76b007245982382e79766338de9d72 Mon Sep 17 00:00:00 2001 From: liuwei Date: Wed, 29 Apr 2026 11:49:57 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E7=90=86MaiBot=E5=9B=9E=E5=A4=8D?= =?UTF-8?q?=E4=B8=AD=E7=9A=84message=5Fid=E6=95=B0=E5=AD=97=E5=89=8D?= =?UTF-8?q?=E7=BC=80=E5=99=AA=E9=9F=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 变更项:\n1. 新增 _sanitize_reply_text 方法,对回复文本做发送前净化。\n2. 增加规则:清理回复开头的长数字前缀(>=6位)及其后随标点/空白,避免 message_id 等内部标识外泄到群聊。\n3. 增加前导标点二次修整,去除数字剥离后残留的逗号/冒号等噪音字符。\n4. 在 _handle_incoming_package 中接入净化流程,保证实际发送前统一生效。 --- plugins/maibot_adapter/main.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugins/maibot_adapter/main.py b/plugins/maibot_adapter/main.py index adbc005..f14bfc0 100644 --- a/plugins/maibot_adapter/main.py +++ b/plugins/maibot_adapter/main.py @@ -1,5 +1,6 @@ import asyncio import json +import re import time import uuid from typing import Any, Dict, List, Optional, Tuple @@ -425,6 +426,7 @@ class MaiBotAdapterPlugin(MessagePluginInterface): message_segment = api_message.get("message_segment") or {} reply_text = self._extract_segment_text(message_segment).strip() + reply_text = self._sanitize_reply_text(reply_text) if not reply_text: self._log_runtime( f"[{self.name}] MaiBot 返回了空文本或非文本片段,忽略发送: package_id={package_id}, " @@ -838,6 +840,23 @@ class MaiBotAdapterPlugin(MessagePluginInterface): return str(data or "") + def _sanitize_reply_text(self, reply_text: str) -> str: + """清理 MaiBot 回复里的路由噪音,避免把 message_id 等内部标识发到群里。""" + text = str(reply_text or "").strip() + if not text: + return "" + + # 清理“纯数字前缀 + 标点/空白”的噪音: + # 1. 某些轮次里模型会把 message_id 或会话标识误拼到回复最前面; + # 2. 这些数字通常长度较长(>=6),且紧跟中文/英文标点或空白; + # 3. 只在“前缀位置”清理,避免误伤正文里的正常数字内容。 + text = re.sub(r"^\s*\d{6,}\s*[,,::;;\-—_~\s]*", "", text) + + # 若清理后前部还有一层“孤立标点”,再做一次轻量修整,避免出现“,在呢怎么了”这种残留。 + text = re.sub(r"^\s*[,,::;;\-—_~]+\s*", "", text) + + return text.strip() + def _parse_json_message(self, raw_text: str) -> Optional[Dict[str, Any]]: """统一处理 JSON 解析异常,避免接收循环被坏包打断。""" try: