优化日志和回复逻辑

- 改进 VLM prompt,明确说明微信左右消息布局判断 is_self
- 添加详细调试日志,追踪回复判断过程
- 改进 should_reply 逻辑,添加 sender/content 日志
This commit is contained in:
2026-04-13 11:51:17 +08:00
parent 11671944e0
commit 9dd6dbff44
4 changed files with 41 additions and 13 deletions

Binary file not shown.

View File

@@ -51,16 +51,28 @@ class MessageProcessor:
def should_reply(self, chat_snapshot: ChatSnapshot) -> bool: def should_reply(self, chat_snapshot: ChatSnapshot) -> bool:
"""判断是否需要回复""" """判断是否需要回复"""
if not chat_snapshot.has_new: # 检查是否有消息
return False
# 检查是否是自己的消息
messages = chat_snapshot.messages messages = chat_snapshot.messages
if not messages: if not messages:
logger.debug("没有消息,不回复")
return False return False
# 检查最后一条消息
last_msg = messages[-1] last_msg = messages[-1]
if last_msg.get("is_self"): sender = last_msg.get("sender", "")
content = last_msg.get("content", "")
is_self = last_msg.get("is_self", False)
logger.debug(f"最后一条消息: sender={sender}, is_self={is_self}, content={content[:30]}")
# 如果是自己发的消息,不回复
if is_self:
logger.debug("自己发的消息,不回复")
return False
# 检查是否有新消息(未读标记)
if not chat_snapshot.has_new:
logger.debug("没有新消息,不回复")
return False return False
return True return True
@@ -267,10 +279,16 @@ class WeChatAgent:
# 判断是否需要回复 # 判断是否需要回复
if self.processor.should_reply(snapshot): if self.processor.should_reply(snapshot):
logger.info(f"需要回复,生成回复内容...")
reply = self.processor.generate_reply(snapshot) reply = self.processor.generate_reply(snapshot)
if reply: if reply:
logger.info(f"发送回复: {reply[:50]}...")
result = self.send_reply(reply) result = self.send_reply(reply)
self._emit("on_reply", result) self._emit("on_reply", result)
else:
logger.warning("生成回复为空")
else:
logger.debug("不需要回复(已读消息或自己发送)")
except Exception as e: except Exception as e:
logger.error(f"轮询处理异常: {e}") logger.error(f"轮询处理异常: {e}")

View File

@@ -283,7 +283,7 @@ class BailianVLMClient:
- current_chat: 当前聊天对象名称 - current_chat: 当前聊天对象名称
- has_new_message: 是否有新消息 - has_new_message: 是否有新消息
- messages: 消息列表,每条消息包含: - messages: 消息列表,每条消息包含:
- sender: 发送者 - sender: 发送者昵称
- content: 消息内容 - content: 消息内容
- time: 时间 - time: 时间
- is_self: 是否是自己发送的 - is_self: 是否是自己发送的
@@ -295,19 +295,29 @@ class BailianVLMClient:
print(f"{msg['sender']}: {msg['content']}") print(f"{msg['sender']}: {msg['content']}")
""" """
prompt = """请分析这个微信聊天截图,返回严格的 JSON 格式,不要包含其他内容: prompt = """请分析这个微信聊天截图,返回严格的 JSON 格式,不要包含其他内容:
关键识别规则:
1. 微信消息分为左右两侧:自己在右侧,对方在左侧
2. 右侧消息是""发送的is_self=true
3. 左侧消息是对方发送的is_self=false
4. 发送者(sender)应该是具体的个人昵称,不是群名或聊天标题
5. current_chat 是聊天窗口顶部的标题,如"Anna司梦 米果妈"
6. has_new_message 根据右上角是否有红色未读标记判断
返回 JSON
{ {
"current_chat": "当前聊天对象名称(如果是群聊,返回群名", "current_chat": "聊天窗口标题(顶部标题栏的文字",
"has_new_message": true或false根据是否有未读标记判断, "has_new_message": true或false右上角红色标记,
"messages": [ "messages": [
{ {
"sender": "发送者昵称", "sender": "发送者昵称(具体人名,不是聊天名)",
"content": "消息内容(图片用[图片]表示,语音用[语音]表示,视频用[视频]表示,文件用[文件]表示)", "content": "消息内容",
"time": "时间字符串,如10:30", "time": "时间如10:30",
"is_self": true或false是否是自己发送的消息 "is_self": true右侧消息我在右侧或false左侧消息对方
} }
] ]
} }
只返回 JSON不要其他文字。""" 只返回 JSON不要任何解释文字。"""
result = self.analyze_image(screenshot_path, prompt) result = self.analyze_image(screenshot_path, prompt)