diff --git a/src/core/__pycache__/engine.cpython-311.pyc b/src/core/__pycache__/engine.cpython-311.pyc index df4e051..bedaa9d 100644 Binary files a/src/core/__pycache__/engine.cpython-311.pyc and b/src/core/__pycache__/engine.cpython-311.pyc differ diff --git a/src/core/engine.py b/src/core/engine.py index 45aa280..b9bf09a 100644 --- a/src/core/engine.py +++ b/src/core/engine.py @@ -256,16 +256,18 @@ class WeChatAgent: chat_name = chat_info.get("current_chat", "") messages = chat_info.get("messages", []) - # 防重复处理(同一聊天 5 秒内不重复处理) + # 防重复处理:基于最新消息内容hash,10秒内不重复回复同一条 current_time = time.time() - chat_key = f"{chat_name}_{hash(str(messages[-1:]))}" - if chat_key in self._last_processed_time: - if current_time - self._last_processed_time[chat_key] < 5: - return + if messages: + latest_content = messages[0].get("content", "")[:50] # 取最新消息的前50字符 + dedup_key = f"{chat_name}_{latest_content}" + if dedup_key in self._last_processed_time: + if current_time - self._last_processed_time[dedup_key] < 10: + logger.debug(f"消息已处理过,跳过: {latest_content[:30]}") + return + self._last_processed_time[dedup_key] = current_time if has_new or messages: - self._last_processed_time[chat_key] = current_time - # 创建快照 snapshot = ChatSnapshot( timestamp=current_time, @@ -290,8 +292,9 @@ class WeChatAgent: }) # 判断是否需要回复 - if self.processor.should_reply(snapshot): - logger.info(f"需要回复,生成回复内容...") + # 策略:只要对方有新消息(is_self=False)就回复,不依赖 has_new_message + if messages and not messages[0].get("is_self", True): + logger.info(f"检测到对方新消息,准备回复...") reply = self.processor.generate_reply(snapshot) if reply: logger.info(f"发送回复: {reply[:50]}...") @@ -300,7 +303,7 @@ class WeChatAgent: else: logger.warning("生成回复为空") else: - logger.debug("不需要回复") + logger.debug("不需要回复(无新消息或自己发送)") except Exception as e: logger.error(f"轮询处理异常: {e}")