清理MaiBot回复中的message_id数字前缀噪音

变更项:\n1. 新增 _sanitize_reply_text 方法,对回复文本做发送前净化。\n2. 增加规则:清理回复开头的长数字前缀(>=6位)及其后随标点/空白,避免 message_id 等内部标识外泄到群聊。\n3. 增加前导标点二次修整,去除数字剥离后残留的逗号/冒号等噪音字符。\n4. 在 _handle_incoming_package 中接入净化流程,保证实际发送前统一生效。
This commit is contained in:
liuwei
2026-04-29 11:49:57 +08:00
parent fe92b011ba
commit 028448fd4b

View File

@@ -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: