加_is_markdown_document过滤: bot回复是md格式(%23标题、[链接](url)、-列表、反引号代码、|表格), 用户聊天框不用md语法, 借此区分bot旧回复vs用户新消息

This commit is contained in:
youpaishidifu
2026-07-26 19:19:37 +08:00
parent 3602ad6ef3
commit 59e3657684
+32
View File
@@ -1093,6 +1093,33 @@ class MessageMonitor:
return True
return False
@staticmethod
def _is_markdown_document(text: str) -> bool:
"""检测是否为 Markdown 格式的文档内容。
Bot 回复用 md 格式(# 标题、[链接](url)、列表等),用户日常对话不用。
借此区分 bot 旧回复和历史堆积 vs 用户新消息。
"""
if not text or len(text) < 10:
return False
# 行首是 # 标题(# ## ### #### 等)
if re.match(r'^#{1,6}\s', text):
return True
# 包含 markdown 链接 [text](url)
if re.search(r'\[.+?\]\(.+?\)', text):
return True
# 行首是 - 或 * 列表项(标准 md 列表)
if re.match(r'^[\-\*]\s', text):
return True
# 包含代码块标记 ``` 或 `code`
if re.search(r'`{1,3}', text):
return True
# 连续 3+ 行包含 markdown 特征:| 表格、--- 分隔线、> 引用
pipes = text.count('|')
if pipes >= 3:
return True
return False
def _paste_and_send(self, platform: str, text: str) -> bool:
"""粘贴文本到输入框并发送(剪贴板 + SendInput)。"""
hwnd = self._cached_hwnd.get(platform)
@@ -1612,6 +1639,11 @@ class MessageMonitor:
continue
if self._is_system_message(ts):
continue
# Markdown 文档过滤:bot 回复以 md 格式为主(#标题、[链接](url)、-列表项),
# 用户日常对话不用 md 语法。拦截 md 格式文本避免 bot 旧回复被重新处理。
if self._is_markdown_document(ts):
logger.debug(f"[{platform}] md文档跳过: {ts[:50]}")
continue
# 纯装饰符号(单字符非汉字非字母数字)跳过
if len(ts) == 1 and not re.match(r'[\u4e00-\u9fff\w]', ts):
continue