'commit'
This commit is contained in:
@@ -63,7 +63,7 @@ class ChatMonitorBot:
|
||||
self.input_pos = None
|
||||
self.last_screen_hash = None
|
||||
self.last_processed_msg_hash = None
|
||||
self.check_interval = 5 # 检查频率 (秒)
|
||||
self.check_interval = 3 # 检查频率 (秒)
|
||||
|
||||
self.persona = (
|
||||
"你是一名1999年毕业、拥有27年一线教学经验的小学高级女教师,名叫‘大张老师’。你目前在‘长春市少惠林作文素养培养中心’工作。"
|
||||
@@ -222,15 +222,51 @@ class ChatMonitorBot:
|
||||
|
||||
# 初始化最后处理的消息哈希,避免重复回复第一条
|
||||
last_msg = self.dialogue_log[-1]
|
||||
# last_msg 是字典,需要转字符串再 encode
|
||||
|
||||
# --- 初始回复逻辑 (Added) ---
|
||||
# 如果最后一条是对方发的消息,说明可能需要回复
|
||||
sender = last_msg.get('sender', '')
|
||||
# 判断逻辑:只要不是"我",就认为是对方 (可能是 "对方", "糖豆爸爸" 等)
|
||||
if sender != "我":
|
||||
logger.info(f"💡 [首屏] 最后一条消息来自 '{sender}',尝试生成回复...")
|
||||
|
||||
# 构建上下文
|
||||
context_text = "\n".join([f"{m.get('sender')}: {m.get('content')}" for m in self.dialogue_log[:-1]])
|
||||
last_content = last_msg.get('content', '')
|
||||
|
||||
reply = await self.get_reply(last_content, context_text)
|
||||
if reply:
|
||||
logger.info(f"🤖 [首屏] LLM 建议回复: {reply}")
|
||||
|
||||
# 检查输入框位置
|
||||
if self.input_pos:
|
||||
logger.info(f"⚡ [首屏] 执行自动回复...")
|
||||
perform_input_action(self.device, self.input_pos, reply)
|
||||
|
||||
# 发送后更新 hash,避免进入循环后重复回复
|
||||
# 发送后,界面会变,但我们需要标记当前这条已经回过了
|
||||
msg_str = json.dumps(last_msg, sort_keys=True, ensure_ascii=False, default=numpy_serializer)
|
||||
self.last_processed_msg_hash = hashlib.md5(msg_str.encode('utf-8')).hexdigest()
|
||||
|
||||
logger.info("✅ [首屏] 回复已发送")
|
||||
else:
|
||||
logger.warning("❌ [首屏] 未找到输入框位置,无法发送")
|
||||
else:
|
||||
logger.info("⚪ [首屏] LLM 认为无需回复")
|
||||
else:
|
||||
logger.info("⚪ [首屏] 最后一条是自己发的,无需回复")
|
||||
|
||||
# 更新 Hash (如果刚才没发回复,也需要记录当前最后一条,防止循环里重复处理)
|
||||
if not self.last_processed_msg_hash:
|
||||
msg_str = json.dumps(last_msg, sort_keys=True, ensure_ascii=False, default=numpy_serializer)
|
||||
self.last_processed_msg_hash = hashlib.md5(msg_str.encode('utf-8')).hexdigest()
|
||||
|
||||
self.last_screen_hash = self.get_image_hash(self.screenshot_path)
|
||||
else:
|
||||
logger.warning("⚠️ 首次运行未识别到有效对话")
|
||||
|
||||
logger.info("🛑 测试结束:已完成所有语音的转换与读取。停止进入监控循环。")
|
||||
return # 测试模式:直接退出,不进入监控循环
|
||||
# logger.info("🛑 测试结束:已完成所有语音的转换与读取。停止进入监控循环。")
|
||||
# return # 测试模式:直接退出,不进入监控循环
|
||||
|
||||
# 3. 进入循环阶段
|
||||
logger.info("🔄 进入实时监控阶段...")
|
||||
@@ -275,40 +311,42 @@ class ChatMonitorBot:
|
||||
|
||||
# E. 判断是否需要回复 (对方发送且非重复消息)
|
||||
sender = last_msg.get('sender', '')
|
||||
if sender == "对方":
|
||||
if sender != "我":
|
||||
if current_msg_hash != self.last_processed_msg_hash:
|
||||
event_shot = WxUtil.get_next_debug_path("event_new_msg")
|
||||
self.device.screenshot(event_shot)
|
||||
logger.info(f"💡 发现新消息: {last_msg},保存现场截图: {event_shot}")
|
||||
logger.info(f"💡 [监控] 发现新消息: {last_msg},保存现场截图: {event_shot}")
|
||||
|
||||
# 获取上下文文本
|
||||
context_text = "\n".join(dialogue_log[:-1])
|
||||
# 获取上下文文本 (格式化为 Sender: Content)
|
||||
context_text = "\n".join([f"{m.get('sender')}: {m.get('content')}" for m in dialogue_log[:-1]])
|
||||
last_content = last_msg.get('content', '')
|
||||
|
||||
# 生成回复
|
||||
reply = await self.get_reply(last_msg, context_text)
|
||||
reply = await self.get_reply(last_content, context_text)
|
||||
|
||||
if reply:
|
||||
logger.info(f"🤖 LLM 回复: {reply}")
|
||||
logger.info(f"🤖 [监控] LLM 建议回复: {reply}")
|
||||
if self.input_pos:
|
||||
logger.info(f"⚡ [监控] 执行自动回复...")
|
||||
perform_input_action(self.device, self.input_pos, reply)
|
||||
|
||||
# 发送后截图留存
|
||||
reply_sent_shot = WxUtil.get_next_debug_path("event_reply_sent")
|
||||
self.device.screenshot(reply_sent_shot)
|
||||
logger.info(f"✅ 回复已发送,保存发送后截图: {reply_sent_shot}")
|
||||
logger.info(f"✅ [监控] 回复已发送,保存发送后截图: {reply_sent_shot}")
|
||||
|
||||
self.last_processed_msg_hash = current_msg_hash
|
||||
else:
|
||||
logger.warning("❌ 未找到输入框位置,无法发送")
|
||||
logger.warning("❌ [监控] 未找到输入框位置,无法发送")
|
||||
else:
|
||||
logger.warning("⚠️ LLM 未生成有效回复")
|
||||
logger.warning("⚠️ [监控] LLM 未生成有效回复")
|
||||
else:
|
||||
# 消息已处理过
|
||||
pass
|
||||
else:
|
||||
# 最后一条是我发送的
|
||||
if current_msg_hash != self.last_processed_msg_hash:
|
||||
logger.info(f"⚪ 最后一条消息非对方发送,跳过回复: {last_msg}")
|
||||
logger.info(f"⚪ [监控] 最后一条消息是自己发的,跳过回复: {last_msg}")
|
||||
self.last_processed_msg_hash = current_msg_hash
|
||||
|
||||
await asyncio.sleep(self.check_interval)
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user