feat: 添加调试日志追踪KB匹配和OCR连续性问题

- gui.py: _search_and_log() 添加KB搜索输入/结果/相关性检查详细日志
- monitor.py: _has_user_colors() 添加像素检测初始化/统计/绿色检测日志
- monitor.py: QQ OCR路径添加截图信息日志
- 目的: 诊断牛头不对马嘴和连续消息OCR中断问题
This commit is contained in:
youpaishidifu
2026-08-09 21:11:52 +08:00
parent 417568f6fa
commit a3cc61bd7d
2 changed files with 37 additions and 5 deletions
+18
View File
@@ -333,17 +333,29 @@ class MsgBotApp(ctk.CTk):
def _search_and_log(self, platform: str, msg: str, msg_hash: str): def _search_and_log(self, platform: str, msg: str, msg_hash: str):
"""搜索知识库并输出结果,匹配成功时队列自动回复到主线程""" """搜索知识库并输出结果,匹配成功时队列自动回复到主线程"""
try: try:
# 详细调试日志:记录发送给KB搜索的完整消息
logger.debug(f"[{platform}] KB搜索输入: msg={msg!r} (长度={len(msg)})")
results = self.kb.search(msg, top_k=3) results = self.kb.search(msg, top_k=3)
lines = [f"[{platform.upper()}] {msg}"] lines = [f"[{platform.upper()}] {msg}"]
if results: if results:
answer_title = results[0][0]["title"] answer_title = results[0][0]["title"]
answer_score = results[0][1] answer_score = results[0][1]
lines.append(f" 🔍 找到 {len(results)} 条相关结果:") lines.append(f" 🔍 找到 {len(results)} 条相关结果:")
for i, (doc, score) in enumerate(results): for i, (doc, score) in enumerate(results):
title = doc["title"] title = doc["title"]
heading = doc.get("heading", "") heading = doc.get("heading", "")
body = doc.get("body", "") body = doc.get("body", "")
content = doc.get("content", "") content = doc.get("content", "")
# 详细调试日志:记录每个搜索结果的详细信息
logger.debug(f"[{platform}] KB结果[{i}]: 标题={title!r}, 匹配度={score:.2f}")
logger.debug(f"[{platform}] KB结果[{i}] heading={heading[:80]!r}")
logger.debug(f"[{platform}] KB结果[{i}] body={body[:120]!r}")
logger.debug(f"[{platform}] KB结果[{i}] content={content[:150]!r}")
# 显示更详细的信息 # 显示更详细的信息
lines.append(f" 📄 [{title}] (匹配度 {score:.2f})") lines.append(f" 📄 [{title}] (匹配度 {score:.2f})")
lines.append(f" 标题: {heading[:60]}") lines.append(f" 标题: {heading[:60]}")
@@ -359,6 +371,12 @@ class MsgBotApp(ctk.CTk):
else: else:
# 检查内容相关性 # 检查内容相关性
has_relevance = self.ar._has_content_relevance(msg, results[0][0], answer_score) has_relevance = self.ar._has_content_relevance(msg, results[0][0], answer_score)
# 详细调试日志:记录内容相关性检查结果
logger.debug(f"[{platform}] 内容相关性检查: msg={msg!r}, has_relevance={has_relevance}")
logger.debug(f"[{platform}] 内容相关性检查: doc标题={results[0][0].get('title', '')!r}")
logger.debug(f"[{platform}] 内容相关性检查: doc heading={results[0][0].get('heading', '')[:50]!r}")
if not has_relevance: if not has_relevance:
heading = results[0][0].get("heading", "")[:40] heading = results[0][0].get("heading", "")[:40]
lines.append(f" ⚠ 内容相关性检查失败 (heading={heading!r}),跳过") lines.append(f" ⚠ 内容相关性检查失败 (heading={heading!r}),跳过")
+19 -5
View File
@@ -2672,6 +2672,12 @@ class MessageMonitor:
logger.warning(f"[qq] 截图太小: {im.width}x{im.height}") logger.warning(f"[qq] 截图太小: {im.width}x{im.height}")
return [] return []
# 详细调试日志:记录截图信息和裁剪区域
logger.debug(f"[qq] OCR前截图信息: 尺寸={im.width}x{im.height}, "
f"窗口位置=({left},{top})-({right},{bottom}), "
f"裁剪区域=({cap_left},{cap_top})-({cap_right},{cap_bottom}), "
f"前台={is_foreground}")
# ⭐ 颜色像素检测:如果截图中只有 bot 蓝色气泡,无白色用户消息,跳过 # ⭐ 颜色像素检测:如果截图中只有 bot 蓝色气泡,无白色用户消息,跳过
if not self._has_user_colors(im, "qq"): if not self._has_user_colors(im, "qq"):
# ⚠️ 不设 _ocr_unchanged——像素检测失败≠截图没变化。 # ⚠️ 不设 _ocr_unchanged——像素检测失败≠截图没变化。
@@ -4085,6 +4091,10 @@ class MessageMonitor:
step_x = max(2, cw // 30) # 增加采样密度,提高白色气泡检测灵敏度 step_x = max(2, cw // 30) # 增加采样密度,提高白色气泡检测灵敏度
step_y = max(2, ch // 30) step_y = max(2, ch // 30)
# 详细调试日志:记录截图信息和采样参数
rlogger = logging.getLogger("msgbot.monitor")
rlogger.debug(f"[{platform}] 像素检测开始: 原始尺寸={w}x{h}, 检测区域={cw}x{ch}, 采样步长=({step_x},{step_y})")
if platform == "qq": if platform == "qq":
# QQ: 蓝色 = bot/对方气泡(右对齐) # QQ: 蓝色 = bot/对方气泡(右对齐)
def is_bot(r, g, b): def is_bot(r, g, b):
@@ -4122,11 +4132,11 @@ class MessageMonitor:
left_bot, left_other, left_white, left_total = analyze(left_half) left_bot, left_other, left_white, left_total = analyze(left_half)
right_bot, right_other, right_white, right_total = analyze(right_half) right_bot, right_other, right_white, right_total = analyze(right_half)
rlogger = logging.getLogger("msgbot.monitor") # 详细调试日志:记录像素统计结果
rlogger.debug(f"[{platform}] 像素: 左其它={left_other}/{left_total} " rlogger.debug(f"[{platform}] 像素统计: 左半区(其他={left_other}/{left_total}, "
f"{'自身' if platform=='wechat' else 'bot'}={left_bot} 白={left_white} | " f"{'自身' if platform=='wechat' else 'bot'}={left_bot}, 白色={left_white})")
f"右其它={right_other}/{right_total} " rlogger.debug(f"[{platform}] 像素统计: 右半区(其他={right_other}/{right_total}, "
f"{'自身' if platform=='wechat' else 'bot'}={right_bot} 白={right_white}") f"{'自身' if platform=='wechat' else 'bot'}={right_bot}, 白色={right_white})")
# ====== 微信专用逻辑 ====== # ====== 微信专用逻辑 ======
# 颜色语义: # 颜色语义:
@@ -4183,6 +4193,10 @@ class MessageMonitor:
except: except:
continue continue
total_green = left_green + right_green total_green = left_green + right_green
# 详细调试日志:记录绿色像素检测结果
rlogger.debug(f"[{platform}] 绿色像素检测: 左绿={left_green}, 右绿={right_green}, 总绿={total_green}")
if total_green >= 3: if total_green >= 3:
rlogger.debug(f"[qq] 像素检测: 检测到微信绿色像素(绿={total_green}), 判定为微信窗口, 跳过") rlogger.debug(f"[qq] 像素检测: 检测到微信绿色像素(绿={total_green}), 判定为微信窗口, 跳过")
return False return False