简化回复逻辑:只要对方有新消息就回复,不依赖has_new判断
1. 移除has_new_message依赖,只要有对方消息(is_self=False)就回复 2. 修复防重复逻辑:基于消息内容+聊天名的hash,10秒内不重复 3. 移除多余的chat_key赋值
This commit is contained in:
Binary file not shown.
@@ -256,16 +256,18 @@ class WeChatAgent:
|
|||||||
chat_name = chat_info.get("current_chat", "")
|
chat_name = chat_info.get("current_chat", "")
|
||||||
messages = chat_info.get("messages", [])
|
messages = chat_info.get("messages", [])
|
||||||
|
|
||||||
# 防重复处理(同一聊天 5 秒内不重复处理)
|
# 防重复处理:基于最新消息内容hash,10秒内不重复回复同一条
|
||||||
current_time = time.time()
|
current_time = time.time()
|
||||||
chat_key = f"{chat_name}_{hash(str(messages[-1:]))}"
|
if messages:
|
||||||
if chat_key in self._last_processed_time:
|
latest_content = messages[0].get("content", "")[:50] # 取最新消息的前50字符
|
||||||
if current_time - self._last_processed_time[chat_key] < 5:
|
dedup_key = f"{chat_name}_{latest_content}"
|
||||||
return
|
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:
|
if has_new or messages:
|
||||||
self._last_processed_time[chat_key] = current_time
|
|
||||||
|
|
||||||
# 创建快照
|
# 创建快照
|
||||||
snapshot = ChatSnapshot(
|
snapshot = ChatSnapshot(
|
||||||
timestamp=current_time,
|
timestamp=current_time,
|
||||||
@@ -290,8 +292,9 @@ class WeChatAgent:
|
|||||||
})
|
})
|
||||||
|
|
||||||
# 判断是否需要回复
|
# 判断是否需要回复
|
||||||
if self.processor.should_reply(snapshot):
|
# 策略:只要对方有新消息(is_self=False)就回复,不依赖 has_new_message
|
||||||
logger.info(f"需要回复,生成回复内容...")
|
if messages and not messages[0].get("is_self", True):
|
||||||
|
logger.info(f"检测到对方新消息,准备回复...")
|
||||||
reply = self.processor.generate_reply(snapshot)
|
reply = self.processor.generate_reply(snapshot)
|
||||||
if reply:
|
if reply:
|
||||||
logger.info(f"发送回复: {reply[:50]}...")
|
logger.info(f"发送回复: {reply[:50]}...")
|
||||||
@@ -300,7 +303,7 @@ class WeChatAgent:
|
|||||||
else:
|
else:
|
||||||
logger.warning("生成回复为空")
|
logger.warning("生成回复为空")
|
||||||
else:
|
else:
|
||||||
logger.debug("不需要回复")
|
logger.debug("不需要回复(无新消息或自己发送)")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"轮询处理异常: {e}")
|
logger.error(f"轮询处理异常: {e}")
|
||||||
|
|||||||
Reference in New Issue
Block a user