🤖 自动格式化代码 [skip ci]

This commit is contained in:
github-actions[bot]
2025-04-28 10:30:11 +00:00
parent ef24f013de
commit 93f2ceb9de
7 changed files with 87 additions and 28 deletions

View File

@@ -138,9 +138,13 @@ class ActionPlanner:
) )
break break
else: else:
logger.debug(f"[私聊][{self.private_name}]Observation info chat history is empty or not available for bot time check.") logger.debug(
f"[私聊][{self.private_name}]Observation info chat history is empty or not available for bot time check."
)
except AttributeError: except AttributeError:
logger.warning(f"[私聊][{self.private_name}]ObservationInfo object might not have chat_history attribute yet for bot time check.") logger.warning(
f"[私聊][{self.private_name}]ObservationInfo object might not have chat_history attribute yet for bot time check."
)
except Exception as e: except Exception as e:
logger.warning(f"[私聊][{self.private_name}]获取 Bot 上次发言时间时出错: {e}") logger.warning(f"[私聊][{self.private_name}]获取 Bot 上次发言时间时出错: {e}")
@@ -159,14 +163,20 @@ class ActionPlanner:
except Exception: except Exception:
timeout_context = "重要提示:对方已经长时间没有回复你的消息了(这可能代表对方繁忙/不想回复/没注意到你的消息等情况,或在对方看来本次聊天已告一段落),请基于此情况规划下一步。\n" timeout_context = "重要提示:对方已经长时间没有回复你的消息了(这可能代表对方繁忙/不想回复/没注意到你的消息等情况,或在对方看来本次聊天已告一段落),请基于此情况规划下一步。\n"
else: else:
logger.debug(f"[私聊][{self.private_name}]Conversation info goal_list is empty or not available for timeout check.") logger.debug(
f"[私聊][{self.private_name}]Conversation info goal_list is empty or not available for timeout check."
)
except AttributeError: except AttributeError:
logger.warning(f"[私聊][{self.private_name}]ConversationInfo object might not have goal_list attribute yet for timeout check.") logger.warning(
f"[私聊][{self.private_name}]ConversationInfo object might not have goal_list attribute yet for timeout check."
)
except Exception as e: except Exception as e:
logger.warning(f"[私聊][{self.private_name}]检查超时目标时出错: {e}") logger.warning(f"[私聊][{self.private_name}]检查超时目标时出错: {e}")
# --- 构建通用 Prompt 参数 --- # --- 构建通用 Prompt 参数 ---
logger.debug(f"[私聊][{self.private_name}]开始规划行动:当前目标: {getattr(conversation_info, 'goal_list', '不可用')}") logger.debug(
f"[私聊][{self.private_name}]开始规划行动:当前目标: {getattr(conversation_info, 'goal_list', '不可用')}"
)
# 构建对话目标 (goals_str) # 构建对话目标 (goals_str)
goals_str = "" goals_str = ""
@@ -189,7 +199,9 @@ class ActionPlanner:
else: else:
goals_str = "- 目前没有明确对话目标,请考虑设定一个。\n" goals_str = "- 目前没有明确对话目标,请考虑设定一个。\n"
except AttributeError: except AttributeError:
logger.warning(f"[私聊][{self.private_name}]ConversationInfo object might not have goal_list attribute yet.") logger.warning(
f"[私聊][{self.private_name}]ConversationInfo object might not have goal_list attribute yet."
)
goals_str = "- 获取对话目标时出错。\n" goals_str = "- 获取对话目标时出错。\n"
except Exception as e: except Exception as e:
logger.error(f"[私聊][{self.private_name}]构建对话目标字符串时出错: {e}") logger.error(f"[私聊][{self.private_name}]构建对话目标字符串时出错: {e}")
@@ -223,7 +235,9 @@ class ActionPlanner:
f"[私聊][{self.private_name}]ObservationInfo has new_messages_count > 0 but unprocessed_messages is empty or missing." f"[私聊][{self.private_name}]ObservationInfo has new_messages_count > 0 but unprocessed_messages is empty or missing."
) )
except AttributeError: except AttributeError:
logger.warning(f"[私聊][{self.private_name}]ObservationInfo object might be missing expected attributes for chat history.") logger.warning(
f"[私聊][{self.private_name}]ObservationInfo object might be missing expected attributes for chat history."
)
chat_history_text = "获取聊天记录时出错。\n" chat_history_text = "获取聊天记录时出错。\n"
except Exception as e: except Exception as e:
logger.error(f"[私聊][{self.private_name}]处理聊天记录时发生未知错误: {e}") logger.error(f"[私聊][{self.private_name}]处理聊天记录时发生未知错误: {e}")
@@ -257,7 +271,9 @@ class ActionPlanner:
else: else:
logger.debug(f"[私聊][{self.private_name}]Conversation info done_action is empty or not available.") logger.debug(f"[私聊][{self.private_name}]Conversation info done_action is empty or not available.")
except AttributeError: except AttributeError:
logger.warning(f"[私聊][{self.private_name}]ConversationInfo object might not have done_action attribute yet.") logger.warning(
f"[私聊][{self.private_name}]ConversationInfo object might not have done_action attribute yet."
)
except Exception as e: except Exception as e:
logger.error(f"[私聊][{self.private_name}]访问行动历史时出错: {e}") logger.error(f"[私聊][{self.private_name}]访问行动历史时出错: {e}")

View File

@@ -145,7 +145,9 @@ class ChatObserver:
return False return False
has_new = self.last_message_time > time_point has_new = self.last_message_time > time_point
logger.debug(f"[私聊][{self.private_name}]判断是否在指定时间点后有新消息: {self.last_message_time} > {time_point} = {has_new}") logger.debug(
f"[私聊][{self.private_name}]判断是否在指定时间点后有新消息: {self.last_message_time} > {time_point} = {has_new}"
)
return has_new return has_new
def get_message_history( def get_message_history(

View File

@@ -149,7 +149,9 @@ class Conversation:
if hasattr(self.observation_info, "new_messages_count"): if hasattr(self.observation_info, "new_messages_count"):
initial_new_message_count = self.observation_info.new_messages_count + 1 # 算上麦麦自己发的那一条 initial_new_message_count = self.observation_info.new_messages_count + 1 # 算上麦麦自己发的那一条
else: else:
logger.warning(f"[私聊][{self.private_name}]ObservationInfo missing 'new_messages_count' before planning.") logger.warning(
f"[私聊][{self.private_name}]ObservationInfo missing 'new_messages_count' before planning."
)
# --- 调用 Action Planner --- # --- 调用 Action Planner ---
# 传递 self.conversation_info.last_successful_reply_action # 传递 self.conversation_info.last_successful_reply_action
@@ -162,7 +164,9 @@ class Conversation:
if hasattr(self.observation_info, "new_messages_count"): if hasattr(self.observation_info, "new_messages_count"):
current_new_message_count = self.observation_info.new_messages_count current_new_message_count = self.observation_info.new_messages_count
else: else:
logger.warning(f"[私聊][{self.private_name}]ObservationInfo missing 'new_messages_count' after planning.") logger.warning(
f"[私聊][{self.private_name}]ObservationInfo missing 'new_messages_count' after planning."
)
if current_new_message_count > initial_new_message_count + 2: if current_new_message_count > initial_new_message_count + 2:
logger.info( logger.info(
@@ -176,12 +180,16 @@ class Conversation:
# 包含 send_new_message # 包含 send_new_message
if initial_new_message_count > 0 and action in ["direct_reply", "send_new_message"]: if initial_new_message_count > 0 and action in ["direct_reply", "send_new_message"]:
if hasattr(self.observation_info, "clear_unprocessed_messages"): if hasattr(self.observation_info, "clear_unprocessed_messages"):
logger.debug(f"[私聊][{self.private_name}]准备执行 {action},清理 {initial_new_message_count} 条规划时已知的新消息。") logger.debug(
f"[私聊][{self.private_name}]准备执行 {action},清理 {initial_new_message_count} 条规划时已知的新消息。"
)
await self.observation_info.clear_unprocessed_messages() await self.observation_info.clear_unprocessed_messages()
if hasattr(self.observation_info, "new_messages_count"): if hasattr(self.observation_info, "new_messages_count"):
self.observation_info.new_messages_count = 0 self.observation_info.new_messages_count = 0
else: else:
logger.error(f"[私聊][{self.private_name}]无法清理未处理消息: ObservationInfo 缺少 clear_unprocessed_messages 方法!") logger.error(
f"[私聊][{self.private_name}]无法清理未处理消息: ObservationInfo 缺少 clear_unprocessed_messages 方法!"
)
await self._handle_action(action, reason, self.observation_info, self.conversation_info) await self._handle_action(action, reason, self.observation_info, self.conversation_info)
@@ -214,7 +222,9 @@ class Conversation:
"""检查在规划后是否有新消息""" """检查在规划后是否有新消息"""
# 检查 ObservationInfo 是否已初始化并且有 new_messages_count 属性 # 检查 ObservationInfo 是否已初始化并且有 new_messages_count 属性
if not hasattr(self, "observation_info") or not hasattr(self.observation_info, "new_messages_count"): if not hasattr(self, "observation_info") or not hasattr(self.observation_info, "new_messages_count"):
logger.warning(f"[私聊][{self.private_name}]ObservationInfo 未初始化或缺少 'new_messages_count' 属性,无法检查新消息。") logger.warning(
f"[私聊][{self.private_name}]ObservationInfo 未初始化或缺少 'new_messages_count' 属性,无法检查新消息。"
)
return False # 或者根据需要抛出错误 return False # 或者根据需要抛出错误
if self.observation_info.new_messages_count > 2: if self.observation_info.new_messages_count > 2:
@@ -225,7 +235,9 @@ class Conversation:
if hasattr(self, "conversation_info"): # 确保 conversation_info 已初始化 if hasattr(self, "conversation_info"): # 确保 conversation_info 已初始化
self.conversation_info.last_successful_reply_action = None self.conversation_info.last_successful_reply_action = None
else: else:
logger.warning(f"[私聊][{self.private_name}]ConversationInfo 未初始化,无法重置 last_successful_reply_action。") logger.warning(
f"[私聊][{self.private_name}]ConversationInfo 未初始化,无法重置 last_successful_reply_action。"
)
return True return True
return False return False
@@ -294,14 +306,18 @@ class Conversation:
while reply_attempt_count < max_reply_attempts and not is_suitable: while reply_attempt_count < max_reply_attempts and not is_suitable:
reply_attempt_count += 1 reply_attempt_count += 1
logger.info(f"[私聊][{self.private_name}]尝试生成追问回复 (第 {reply_attempt_count}/{max_reply_attempts} 次)...") logger.info(
f"[私聊][{self.private_name}]尝试生成追问回复 (第 {reply_attempt_count}/{max_reply_attempts} 次)..."
)
self.state = ConversationState.GENERATING self.state = ConversationState.GENERATING
# 1. 生成回复 (调用 generate 时传入 action_type) # 1. 生成回复 (调用 generate 时传入 action_type)
self.generated_reply = await self.reply_generator.generate( self.generated_reply = await self.reply_generator.generate(
observation_info, conversation_info, action_type="send_new_message" observation_info, conversation_info, action_type="send_new_message"
) )
logger.info(f"[私聊][{self.private_name}]第 {reply_attempt_count} 次生成的追问回复: {self.generated_reply}") logger.info(
f"[私聊][{self.private_name}]第 {reply_attempt_count} 次生成的追问回复: {self.generated_reply}"
)
# 2. 检查回复 (逻辑不变) # 2. 检查回复 (逻辑不变)
self.state = ConversationState.CHECKING self.state = ConversationState.CHECKING
@@ -326,7 +342,9 @@ class Conversation:
) )
break break
except Exception as check_err: except Exception as check_err:
logger.error(f"[私聊][{self.private_name}]第 {reply_attempt_count} 次调用 ReplyChecker (追问) 时出错: {check_err}") logger.error(
f"[私聊][{self.private_name}]第 {reply_attempt_count} 次调用 ReplyChecker (追问) 时出错: {check_err}"
)
check_reason = f"{reply_attempt_count} 次检查过程出错: {check_err}" check_reason = f"{reply_attempt_count} 次检查过程出错: {check_err}"
break break
@@ -351,7 +369,9 @@ class Conversation:
else: else:
# 追问失败 # 追问失败
logger.warning(f"[私聊][{self.private_name}]经过 {reply_attempt_count} 次尝试,未能生成合适的追问回复。最终原因: {check_reason}") logger.warning(
f"[私聊][{self.private_name}]经过 {reply_attempt_count} 次尝试,未能生成合适的追问回复。最终原因: {check_reason}"
)
conversation_info.done_action[action_index].update( conversation_info.done_action[action_index].update(
{"status": "recall", "final_reason": f"追问尝试{reply_attempt_count}次后失败: {check_reason}"} {"status": "recall", "final_reason": f"追问尝试{reply_attempt_count}次后失败: {check_reason}"}
) )
@@ -381,14 +401,18 @@ class Conversation:
while reply_attempt_count < max_reply_attempts and not is_suitable: while reply_attempt_count < max_reply_attempts and not is_suitable:
reply_attempt_count += 1 reply_attempt_count += 1
logger.info(f"[私聊][{self.private_name}]尝试生成首次回复 (第 {reply_attempt_count}/{max_reply_attempts} 次)...") logger.info(
f"[私聊][{self.private_name}]尝试生成首次回复 (第 {reply_attempt_count}/{max_reply_attempts} 次)..."
)
self.state = ConversationState.GENERATING self.state = ConversationState.GENERATING
# 1. 生成回复 # 1. 生成回复
self.generated_reply = await self.reply_generator.generate( self.generated_reply = await self.reply_generator.generate(
observation_info, conversation_info, action_type="direct_reply" observation_info, conversation_info, action_type="direct_reply"
) )
logger.info(f"[私聊][{self.private_name}]第 {reply_attempt_count} 次生成的首次回复: {self.generated_reply}") logger.info(
f"[私聊][{self.private_name}]第 {reply_attempt_count} 次生成的首次回复: {self.generated_reply}"
)
# 2. 检查回复 # 2. 检查回复
self.state = ConversationState.CHECKING self.state = ConversationState.CHECKING
@@ -413,7 +437,9 @@ class Conversation:
) )
break break
except Exception as check_err: except Exception as check_err:
logger.error(f"[私聊][{self.private_name}]第 {reply_attempt_count} 次调用 ReplyChecker (首次回复) 时出错: {check_err}") logger.error(
f"[私聊][{self.private_name}]第 {reply_attempt_count} 次调用 ReplyChecker (首次回复) 时出错: {check_err}"
)
check_reason = f"{reply_attempt_count} 次检查过程出错: {check_err}" check_reason = f"{reply_attempt_count} 次检查过程出错: {check_err}"
break break
@@ -438,7 +464,9 @@ class Conversation:
else: else:
# 首次回复失败 # 首次回复失败
logger.warning(f"[私聊][{self.private_name}]经过 {reply_attempt_count} 次尝试,未能生成合适的首次回复。最终原因: {check_reason}") logger.warning(
f"[私聊][{self.private_name}]经过 {reply_attempt_count} 次尝试,未能生成合适的首次回复。最终原因: {check_reason}"
)
conversation_info.done_action[action_index].update( conversation_info.done_action[action_index].update(
{"status": "recall", "final_reason": f"首次回复尝试{reply_attempt_count}次后失败: {check_reason}"} {"status": "recall", "final_reason": f"首次回复尝试{reply_attempt_count}次后失败: {check_reason}"}
) )
@@ -526,7 +554,9 @@ class Conversation:
logger.info(f"[私聊][{self.private_name}]不想再理你了...") logger.info(f"[私聊][{self.private_name}]不想再理你了...")
ignore_duration_seconds = 10 * 60 ignore_duration_seconds = 10 * 60
self.ignore_until_timestamp = time.time() + ignore_duration_seconds self.ignore_until_timestamp = time.time() + ignore_duration_seconds
logger.info(f"[私聊][{self.private_name}]将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}") logger.info(
f"[私聊][{self.private_name}]将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}"
)
self.state = ConversationState.IGNORED self.state = ConversationState.IGNORED
action_successful = True # 标记动作成功 action_successful = True # 标记动作成功

View File

@@ -155,7 +155,12 @@ class GoalAnalyzer:
# 使用改进后的get_items_from_json函数处理JSON数组 # 使用改进后的get_items_from_json函数处理JSON数组
success, result = get_items_from_json( success, result = get_items_from_json(
content, self.private_name, "goal", "reasoning", required_types={"goal": str, "reasoning": str}, allow_array=True content,
self.private_name,
"goal",
"reasoning",
required_types={"goal": str, "reasoning": str},
allow_array=True,
) )
if success: if success:

View File

@@ -50,7 +50,9 @@ class ReplyChecker:
# 可以用简单比较,或者更复杂的相似度库 (如 difflib) # 可以用简单比较,或者更复杂的相似度库 (如 difflib)
# 简单比较:是否完全相同 # 简单比较:是否完全相同
if reply == bot_messages[0]: # 和最近一条完全一样 if reply == bot_messages[0]: # 和最近一条完全一样
logger.warning(f"[私聊][{self.private_name}]ReplyChecker 检测到回复与上一条 Bot 消息完全相同: '{reply}'") logger.warning(
f"[私聊][{self.private_name}]ReplyChecker 检测到回复与上一条 Bot 消息完全相同: '{reply}'"
)
return ( return (
False, False,
"回复内容与你上一条发言完全相同,请修改,可以选择深入话题或寻找其它话题或等待", "回复内容与你上一条发言完全相同,请修改,可以选择深入话题或寻找其它话题或等待",

View File

@@ -90,7 +90,9 @@ class ReplyGenerator:
str: 生成的回复 str: 生成的回复
""" """
# 构建提示词 # 构建提示词
logger.debug(f"[私聊][{self.private_name}]开始生成回复 (动作类型: {action_type}):当前目标: {conversation_info.goal_list}") logger.debug(
f"[私聊][{self.private_name}]开始生成回复 (动作类型: {action_type}):当前目标: {conversation_info.goal_list}"
)
# --- 构建通用 Prompt 参数 --- # --- 构建通用 Prompt 参数 ---
# (这部分逻辑基本不变) # (这部分逻辑基本不变)

View File

@@ -47,7 +47,9 @@ class Waiter:
return True # 返回 True 表示超时 return True # 返回 True 表示超时
await asyncio.sleep(5) # 每 5 秒检查一次 await asyncio.sleep(5) # 每 5 秒检查一次
logger.debug(f"[私聊][{self.private_name}]等待中...") # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出 logger.debug(
f"[私聊][{self.private_name}]等待中..."
) # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出
async def wait_listening(self, conversation_info: ConversationInfo) -> bool: async def wait_listening(self, conversation_info: ConversationInfo) -> bool:
"""倾听用户发言或超时""" """倾听用户发言或超时"""