From 99697efe0ee7438255eec5e22be9b9017039be7e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Sun, 27 Apr 2025 03:08:02 +0000 Subject: [PATCH 01/11] =?UTF-8?q?=F0=9F=A4=96=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BB=A3=E7=A0=81=20[skip=20ci]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/action_planner.py | 10 +++++++- src/plugins/PFC/conversation.py | 42 ++++++++++++++++--------------- src/plugins/PFC/pfc_manager.py | 18 +++++++------ 3 files changed, 41 insertions(+), 29 deletions(-) diff --git a/src/plugins/PFC/action_planner.py b/src/plugins/PFC/action_planner.py index 082741263..05d5156ee 100644 --- a/src/plugins/PFC/action_planner.py +++ b/src/plugins/PFC/action_planner.py @@ -293,7 +293,15 @@ block_and_ignore: 更加极端的结束对话方式,直接结束对话并在 reason = result.get("reason", "LLM未提供原因,默认等待") # 验证action类型 - valid_actions = ["direct_reply", "fetch_knowledge", "wait", "listening", "rethink_goal", "end_conversation", "block_and_ignore"] + valid_actions = [ + "direct_reply", + "fetch_knowledge", + "wait", + "listening", + "rethink_goal", + "end_conversation", + "block_and_ignore", + ] if action not in valid_actions: logger.warning(f"LLM返回了未知的行动类型: '{action}',强制改为 wait") reason = f"(原始行动'{action}'无效,已强制改为wait) {reason}" diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index f68098f13..c8e2f2370 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -1,8 +1,10 @@ import time import asyncio import datetime + # from .message_storage import MongoDBMessageStorage from src.plugins.utils.chat_message_builder import build_readable_messages, get_raw_msg_before_timestamp_with_chat + # from ...config.config import global_config from typing import Dict, Any, Optional from ..chat.message import Message @@ -135,17 +137,17 @@ class Conversation: """思考步,PFC核心循环模块""" while self.should_continue: if self.ignore_until_timestamp and time.time() < self.ignore_until_timestamp: - # 仍在忽略期间,等待下次检查 - await asyncio.sleep(30) # 每 30 秒检查一次 - continue # 跳过本轮循环的剩余部分 + # 仍在忽略期间,等待下次检查 + await asyncio.sleep(30) # 每 30 秒检查一次 + continue # 跳过本轮循环的剩余部分 elif self.ignore_until_timestamp and time.time() >= self.ignore_until_timestamp: - # 忽略期结束,现在正常地结束对话 - logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。") - self.ignore_until_timestamp = None # 清除时间戳 - self.should_continue = False # 现在停止循环 - # (可选)在这里记录一个 'end_conversation' 动作 - # 或者确保管理器会基于 should_continue 为 False 来清理它 - continue # 跳过本轮循环的剩余部分,让它终止 + # 忽略期结束,现在正常地结束对话 + logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。") + self.ignore_until_timestamp = None # 清除时间戳 + self.should_continue = False # 现在停止循环 + # (可选)在这里记录一个 'end_conversation' 动作 + # 或者确保管理器会基于 should_continue 为 False 来清理它 + continue # 跳过本轮循环的剩余部分,让它终止 try: # --- 在规划前记录当前新消息数量 --- initial_new_message_count = 0 @@ -424,19 +426,19 @@ class Conversation: # 这里不需要 return,主循环会在下一轮检查 should_continue elif action == "block_and_ignore": - logger.info("不想再理你了...") + logger.info("不想再理你了...") # 1. 标记对话为暂时忽略 - ignore_duration_seconds = 10 * 60 # 10 分钟 - self.ignore_until_timestamp = time.time() + ignore_duration_seconds - logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}") - conversation_info.done_action[action_index].update( + ignore_duration_seconds = 10 * 60 # 10 分钟 + self.ignore_until_timestamp = time.time() + ignore_duration_seconds + logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}") + conversation_info.done_action[action_index].update( { - "status": "done", # 或者一个自定义状态,比如 "ignored" - "final_reason": "Detected potential harassment, ignoring temporarily.", # 检测到潜在骚扰,暂时忽略 - "time": datetime.datetime.now().strftime("%H:%M:%S"), + "status": "done", # 或者一个自定义状态,比如 "ignored" + "final_reason": "Detected potential harassment, ignoring temporarily.", # 检测到潜在骚扰,暂时忽略 + "time": datetime.datetime.now().strftime("%H:%M:%S"), } - ) - self.state = ConversationState.IGNORED + ) + self.state = ConversationState.IGNORED else: # 对应 'wait' 动作 self.state = ConversationState.WAITING diff --git a/src/plugins/PFC/pfc_manager.py b/src/plugins/PFC/pfc_manager.py index 2b91c0a9c..9aae33f24 100644 --- a/src/plugins/PFC/pfc_manager.py +++ b/src/plugins/PFC/pfc_manager.py @@ -47,18 +47,20 @@ class PFCManager: return self._instances[stream_id] if stream_id in self._instances: instance = self._instances[stream_id] - if hasattr(instance, 'ignore_until_timestamp') and \ - instance.ignore_until_timestamp and \ - time.time() < instance.ignore_until_timestamp: + if ( + hasattr(instance, "ignore_until_timestamp") + and instance.ignore_until_timestamp + and time.time() < instance.ignore_until_timestamp + ): logger.debug(f"会话实例当前处于忽略状态: {stream_id}") - # 返回 None 阻止交互。或者可以返回实例但标记它被忽略了喵? - # 还是返回 None 吧喵。 + # 返回 None 阻止交互。或者可以返回实例但标记它被忽略了喵? + # 还是返回 None 吧喵。 return None - # 检查 should_continue 状态 + # 检查 should_continue 状态 if instance.should_continue: - logger.debug(f"使用现有会话实例: {stream_id}") - return instance + logger.debug(f"使用现有会话实例: {stream_id}") + return instance # else: 实例存在但不应继续 try: # 创建新实例 From 0baec45c589149045fd5c413402c7ff95b8aacc2 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 11:41:11 +0800 Subject: [PATCH 02/11] fix 0 --- src/plugins/PFC/conversation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index c8e2f2370..5dd153cc6 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -288,7 +288,7 @@ class Conversation: # 2. 检查回复 self.state = ConversationState.CHECKING try: - current_goal_str = conversation_info.goal_list[0][0] if conversation_info.goal_list else "" + current_goal_str = conversation_info.goal_list[0]["goal"] if conversation_info.goal_list else "" # 注意:这里传递的是 reply_attempt_count - 1 作为 retry_count 给 checker is_suitable, check_reason, need_replan = await self.reply_generator.check_reply( reply=self.generated_reply, From 02b7ea79db09dd69469c525ad7610262c611ef7c Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 17:48:25 +0800 Subject: [PATCH 03/11] =?UTF-8?q?=E5=85=88=E4=BF=AE=E4=B8=AA=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/action_planner.py | 2 +- src/plugins/PFC/conversation.py | 114 ++++++++++++++--------------- src/plugins/PFC/reply_generator.py | 18 ++--- 3 files changed, 67 insertions(+), 67 deletions(-) diff --git a/src/plugins/PFC/action_planner.py b/src/plugins/PFC/action_planner.py index 701770181..ff13b5fad 100644 --- a/src/plugins/PFC/action_planner.py +++ b/src/plugins/PFC/action_planner.py @@ -279,7 +279,7 @@ class ActionPlanner: final_reason = action_data.get("final_reason", "") action_time = action_data.get("time", "") elif isinstance(action_data, tuple): - # 假设旧格式兼容 + # 假设旧格式兼容 if len(action_data) > 0: action_type = action_data[0] if len(action_data) > 1: plan_reason = action_data[1] # 可能是规划原因或最终原因 if len(action_data) > 2: status = action_data[2] diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index 4a309b1d9..04c2cca39 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -135,13 +135,13 @@ class Conversation: while self.should_continue: # 忽略逻辑 if self.ignore_until_timestamp and time.time() < self.ignore_until_timestamp: - await asyncio.sleep(30) - continue + await asyncio.sleep(30) + continue elif self.ignore_until_timestamp and time.time() >= self.ignore_until_timestamp: - logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。") - self.ignore_until_timestamp = None - self.should_continue = False - continue + logger.info(f"忽略时间已到 {self.stream_id},准备结束对话。") + self.ignore_until_timestamp = None + self.should_continue = False + continue try: # --- 在规划前记录当前新消息数量 --- initial_new_message_count = 0 @@ -239,13 +239,13 @@ class Conversation: # 尝试从 msg_dict 直接获取 chat_stream,如果失败则从全局 chat_manager 获取 chat_info = msg_dict.get("chat_info") if chat_info and isinstance(chat_info, dict): - chat_stream = ChatStream.from_dict(chat_info) + chat_stream = ChatStream.from_dict(chat_info) elif self.chat_stream: # 使用实例变量中的 chat_stream - chat_stream = self.chat_stream + chat_stream = self.chat_stream else: # Fallback: 尝试从 manager 获取 (可能需要 stream_id) - chat_stream = chat_manager.get_stream(self.stream_id) - if not chat_stream: - raise ValueError(f"无法确定 ChatStream for stream_id {self.stream_id}") + chat_stream = chat_manager.get_stream(self.stream_id) + if not chat_stream: + raise ValueError(f"无法确定 ChatStream for stream_id {self.stream_id}") user_info = UserInfo.from_dict(msg_dict.get("user_info", {})) @@ -416,7 +416,7 @@ class Conversation: # 循环结束,处理最终结果 if is_suitable: - # 检查是否有新消息 + # 检查是否有新消息 if self._check_new_messages_after_planning(): logger.info("生成首次回复期间收到新消息,取消发送,重新规划行动") conversation_info.done_action[action_index].update( @@ -461,58 +461,58 @@ class Conversation: try: # 检查 knowledge_fetcher 是否存在 if not hasattr(self, 'knowledge_fetcher'): - logger.error("KnowledgeFetcher 未初始化,无法获取知识。") - raise AttributeError("KnowledgeFetcher not initialized") + logger.error("KnowledgeFetcher 未初始化,无法获取知识。") + raise AttributeError("KnowledgeFetcher not initialized") knowledge, source = await self.knowledge_fetcher.fetch(knowledge_query, observation_info.chat_history) logger.info(f"获取到知识: {knowledge[:100]}..., 来源: {source}") if knowledge: - # 确保 knowledge_list 存在 - if not hasattr(conversation_info, 'knowledge_list'): - conversation_info.knowledge_list = [] - conversation_info.knowledge_list.append({"query": knowledge_query, "knowledge": knowledge, "source": source}) + # 确保 knowledge_list 存在 + if not hasattr(conversation_info, 'knowledge_list'): + conversation_info.knowledge_list = [] + conversation_info.knowledge_list.append({"query": knowledge_query, "knowledge": knowledge, "source": source}) action_successful = True except Exception as fetch_err: - logger.error(f"获取知识时出错: {fetch_err}") - conversation_info.done_action[action_index].update( - {"status": "recall", "final_reason": f"获取知识失败: {fetch_err}"} - ) - self.conversation_info.last_successful_reply_action = None # 重置状态 + logger.error(f"获取知识时出错: {fetch_err}") + conversation_info.done_action[action_index].update( + {"status": "recall", "final_reason": f"获取知识失败: {fetch_err}"} + ) + self.conversation_info.last_successful_reply_action = None # 重置状态 elif action == "rethink_goal": self.state = ConversationState.RETHINKING try: - # 检查 goal_analyzer 是否存在 + # 检查 goal_analyzer 是否存在 if not hasattr(self, 'goal_analyzer'): - logger.error("GoalAnalyzer 未初始化,无法重新思考目标。") - raise AttributeError("GoalAnalyzer not initialized") + logger.error("GoalAnalyzer 未初始化,无法重新思考目标。") + raise AttributeError("GoalAnalyzer not initialized") await self.goal_analyzer.analyze_goal(conversation_info, observation_info) action_successful = True except Exception as rethink_err: - logger.error(f"重新思考目标时出错: {rethink_err}") - conversation_info.done_action[action_index].update( - {"status": "recall", "final_reason": f"重新思考目标失败: {rethink_err}"} - ) - self.conversation_info.last_successful_reply_action = None # 重置状态 + logger.error(f"重新思考目标时出错: {rethink_err}") + conversation_info.done_action[action_index].update( + {"status": "recall", "final_reason": f"重新思考目标失败: {rethink_err}"} + ) + self.conversation_info.last_successful_reply_action = None # 重置状态 elif action == "listening": self.state = ConversationState.LISTENING logger.info("倾听对方发言...") try: - # 检查 waiter 是否存在 + # 检查 waiter 是否存在 if not hasattr(self, 'waiter'): - logger.error("Waiter 未初始化,无法倾听。") - raise AttributeError("Waiter not initialized") + logger.error("Waiter 未初始化,无法倾听。") + raise AttributeError("Waiter not initialized") timeout_occurred = await self.waiter.wait_listening(conversation_info) action_successful = True # Listening 完成就算成功 except Exception as listen_err: - logger.error(f"倾听时出错: {listen_err}") - conversation_info.done_action[action_index].update( - {"status": "recall", "final_reason": f"倾听失败: {listen_err}"} - ) - self.conversation_info.last_successful_reply_action = None # 重置状态 + logger.error(f"倾听时出错: {listen_err}") + conversation_info.done_action[action_index].update( + {"status": "recall", "final_reason": f"倾听失败: {listen_err}"} + ) + self.conversation_info.last_successful_reply_action = None # 重置状态 elif action == "end_conversation": @@ -521,29 +521,29 @@ class Conversation: action_successful = True # 标记动作成功 elif action == "block_and_ignore": - logger.info("不想再理你了...") - ignore_duration_seconds = 10 * 60 - self.ignore_until_timestamp = time.time() + ignore_duration_seconds - logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}") - self.state = ConversationState.IGNORED - action_successful = True # 标记动作成功 + logger.info("不想再理你了...") + ignore_duration_seconds = 10 * 60 + self.ignore_until_timestamp = time.time() + ignore_duration_seconds + logger.info(f"将忽略此对话直到: {datetime.datetime.fromtimestamp(self.ignore_until_timestamp)}") + self.state = ConversationState.IGNORED + action_successful = True # 标记动作成功 else: # 对应 'wait' 动作 self.state = ConversationState.WAITING logger.info("等待更多信息...") try: - # 检查 waiter 是否存在 + # 检查 waiter 是否存在 if not hasattr(self, 'waiter'): - logger.error("Waiter 未初始化,无法等待。") - raise AttributeError("Waiter not initialized") + logger.error("Waiter 未初始化,无法等待。") + raise AttributeError("Waiter not initialized") timeout_occurred = await self.waiter.wait(self.conversation_info) action_successful = True # Wait 完成就算成功 except Exception as wait_err: - logger.error(f"等待时出错: {wait_err}") - conversation_info.done_action[action_index].update( - {"status": "recall", "final_reason": f"等待失败: {wait_err}"} - ) - self.conversation_info.last_successful_reply_action = None # 重置状态 + logger.error(f"等待时出错: {wait_err}") + conversation_info.done_action[action_index].update( + {"status": "recall", "final_reason": f"等待失败: {wait_err}"} + ) + self.conversation_info.last_successful_reply_action = None # 重置状态 # --- 更新 Action History 状态 --- # 只有当动作本身成功时,才更新状态为 done @@ -572,11 +572,11 @@ class Conversation: # 发送消息 (确保 direct_sender 和 chat_stream 有效) if not hasattr(self, 'direct_sender') or not self.direct_sender: - logger.error("DirectMessageSender 未初始化,无法发送回复。") - return + logger.error("DirectMessageSender 未初始化,无法发送回复。") + return if not self.chat_stream: - logger.error("ChatStream 未初始化,无法发送回复。") - return + logger.error("ChatStream 未初始化,无法发送回复。") + return await self.direct_sender.send_message(chat_stream=self.chat_stream, content=reply_content) diff --git a/src/plugins/PFC/reply_generator.py b/src/plugins/PFC/reply_generator.py index ebde0a67a..0035f845e 100644 --- a/src/plugins/PFC/reply_generator.py +++ b/src/plugins/PFC/reply_generator.py @@ -114,15 +114,15 @@ class ReplyGenerator: # 获取聊天历史记录 (chat_history_text) chat_history_text = observation_info.chat_history_str if observation_info.new_messages_count > 0 and observation_info.unprocessed_messages: - new_messages_list = observation_info.unprocessed_messages - new_messages_str = await build_readable_messages( - new_messages_list, - replace_bot_name=True, - merge_messages=False, - timestamp_mode="relative", - read_mark=0.0, - ) - chat_history_text += f"\n--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n{new_messages_str}" + new_messages_list = observation_info.unprocessed_messages + new_messages_str = await build_readable_messages( + new_messages_list, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + ) + chat_history_text += f"\n--- 以下是 {observation_info.new_messages_count} 条新消息 ---\n{new_messages_str}" elif not chat_history_text: chat_history_text = "还没有聊天记录。" From 2ee60ae9898e5ff058587d4c41773fe7b0c4bf5d Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 17:55:23 +0800 Subject: [PATCH 04/11] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=A5=9E=E5=A5=87?= =?UTF-8?q?=E7=9A=840=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/conversation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index 04c2cca39..1107527ae 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -308,7 +308,7 @@ class Conversation: # 2. 检查回复 (逻辑不变) self.state = ConversationState.CHECKING try: - current_goal_str = conversation_info.goal_list[0][0] if conversation_info.goal_list else "" + current_goal_str = conversation_info.goal_list[0]["goal"] if conversation_info.goal_list else "" is_suitable, check_reason, need_replan = await self.reply_generator.check_reply( reply=self.generated_reply, goal=current_goal_str, From 1a3e27b7808d74b6abbf2c8860b09409cbe01da4 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 18:01:52 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E8=AE=A9=E9=BA=A6=E9=BA=A6=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E8=BF=9B=E5=85=A5=E8=BF=BD=E9=97=AE=E5=86=B3=E7=AD=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/conversation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index 1107527ae..5e7dae4b3 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -146,7 +146,7 @@ class Conversation: # --- 在规划前记录当前新消息数量 --- initial_new_message_count = 0 if hasattr(self.observation_info, "new_messages_count"): - initial_new_message_count = self.observation_info.new_messages_count + initial_new_message_count = self.observation_info.new_messages_count + 1 # 算上麦麦自己发的那一条 else: logger.warning("ObservationInfo missing 'new_messages_count' before planning.") From 0b51aeb43eaa26003fb3de6e18508d83fee2c51c Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 18:11:01 +0800 Subject: [PATCH 06/11] =?UTF-8?q?=E5=85=88=E5=9C=A8=E8=BF=99=E9=87=8C?= =?UTF-8?q?=E5=8A=A0=E4=B8=AA=E8=B0=83=E8=AF=95=E8=AF=AD=E5=8F=A5=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E4=BF=AE=E5=8F=A6=E5=A4=96=E4=B8=80=E4=B8=AAbug?= =?UTF-8?q?=E5=85=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/conversation.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index 5e7dae4b3..ccec0d8c9 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -308,7 +308,9 @@ class Conversation: # 2. 检查回复 (逻辑不变) self.state = ConversationState.CHECKING try: + print(conversation_info.goal_list) current_goal_str = conversation_info.goal_list[0]["goal"] if conversation_info.goal_list else "" + print("跳过小虫虫\n") is_suitable, check_reason, need_replan = await self.reply_generator.check_reply( reply=self.generated_reply, goal=current_goal_str, From 71f35d8f73467232da2a0a24c587032d4171f021 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 18:28:13 +0800 Subject: [PATCH 07/11] =?UTF-8?q?fix=20ReplyChecker=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/conversation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index ccec0d8c9..b1fe0731c 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -308,9 +308,7 @@ class Conversation: # 2. 检查回复 (逻辑不变) self.state = ConversationState.CHECKING try: - print(conversation_info.goal_list) current_goal_str = conversation_info.goal_list[0]["goal"] if conversation_info.goal_list else "" - print("跳过小虫虫\n") is_suitable, check_reason, need_replan = await self.reply_generator.check_reply( reply=self.generated_reply, goal=current_goal_str, @@ -394,7 +392,7 @@ class Conversation: # 2. 检查回复 self.state = ConversationState.CHECKING try: - current_goal_str = conversation_info.goal_list[0][0] if conversation_info.goal_list else "" + current_goal_str = conversation_info.goal_list[0]["goal"] if conversation_info.goal_list else "" is_suitable, check_reason, need_replan = await self.reply_generator.check_reply( reply=self.generated_reply, goal=current_goal_str, From 60b3187227241000b3daf810d311fa4468dbc592 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 18:40:17 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=8A=8Alogger=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E5=A4=8D=E8=BE=93=E5=87=BA=E5=A1=9E=E8=BF=9Bdebug?= =?UTF-8?q?=EF=BC=8C=E5=88=A0=E6=8E=89=E8=B0=83=E8=AF=95=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E8=AF=AD=E5=8F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/chat_observer.py | 2 -- src/plugins/PFC/chat_states.py | 6 ------ src/plugins/PFC/conversation.py | 2 +- src/plugins/PFC/observation_info.py | 2 -- src/plugins/PFC/reply_generator.py | 2 +- 5 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/plugins/PFC/chat_observer.py b/src/plugins/PFC/chat_observer.py index 697833c84..0305289fa 100644 --- a/src/plugins/PFC/chat_observer.py +++ b/src/plugins/PFC/chat_observer.py @@ -94,11 +94,9 @@ class ChatObserver: """ try: # 发送新消息通知 - # logger.info(f"发送新ccchandleer消息通知: {message}") notification = create_new_message_notification( sender="chat_observer", target="observation_info", message=message ) - # logger.info(f"发送新消ddddd息通知: {notification}") # print(self.notification_manager) await self.notification_manager.send_notification(notification) except Exception as e: diff --git a/src/plugins/PFC/chat_states.py b/src/plugins/PFC/chat_states.py index 1f8ee15fb..4b839b7bd 100644 --- a/src/plugins/PFC/chat_states.py +++ b/src/plugins/PFC/chat_states.py @@ -98,15 +98,11 @@ class NotificationManager: notification_type: 要处理的通知类型 handler: 处理器实例 """ - # print(1145145511114445551111444) if target not in self._handlers: - # print("没11有target") self._handlers[target] = {} if notification_type not in self._handlers[target]: - # print("没11有notification_type") self._handlers[target][notification_type] = [] # print(self._handlers[target][notification_type]) - # print(f"注册1111111111111111111111处理器: {target} {notification_type} {handler}") self._handlers[target][notification_type].append(handler) # print(self._handlers[target][notification_type]) @@ -132,7 +128,6 @@ class NotificationManager: async def send_notification(self, notification: Notification): """发送通知""" self._notification_history.append(notification) - # print("kaishichul-----------------------------------i") # 如果是状态通知,更新活跃状态 if isinstance(notification, StateNotification): @@ -145,7 +140,6 @@ class NotificationManager: target = notification.target if target in self._handlers: handlers = self._handlers[target].get(notification.type, []) - # print(1111111) # print(handlers) for handler in handlers: # print(f"调用处理器: {handler}") diff --git a/src/plugins/PFC/conversation.py b/src/plugins/PFC/conversation.py index b1fe0731c..394fc1d74 100644 --- a/src/plugins/PFC/conversation.py +++ b/src/plugins/PFC/conversation.py @@ -267,7 +267,7 @@ class Conversation: ): """处理规划的行动""" - logger.info(f"执行行动: {action}, 原因: {reason}") + logger.debug(f"执行行动: {action}, 原因: {reason}") # 记录action历史 (逻辑不变) current_action_record = { diff --git a/src/plugins/PFC/observation_info.py b/src/plugins/PFC/observation_info.py index 072b1fb6f..050d839fb 100644 --- a/src/plugins/PFC/observation_info.py +++ b/src/plugins/PFC/observation_info.py @@ -140,7 +140,6 @@ class ObservationInfo: self.chat_observer.notification_manager.register_handler( target="observation_info", notification_type=NotificationType.COLD_CHAT, handler=self.handler ) - print("1919810------------------------绑定-----------------------------") def unbind_from_chat_observer(self): """解除与chat_observer的绑定""" @@ -159,7 +158,6 @@ class ObservationInfo: Args: message: 消息数据 """ - # print("1919810-----------------------------------------------------") # logger.debug(f"更新信息from_message: {message}") self.last_message_time = message["time"] self.last_message_id = message["message_id"] diff --git a/src/plugins/PFC/reply_generator.py b/src/plugins/PFC/reply_generator.py index 0035f845e..59e261ac5 100644 --- a/src/plugins/PFC/reply_generator.py +++ b/src/plugins/PFC/reply_generator.py @@ -162,7 +162,7 @@ class ReplyGenerator: logger.debug(f"发送到LLM的生成提示词:\n------\n{prompt}\n------") try: content, _ = await self.llm.generate_response_async(prompt) - logger.info(f"生成的回复: {content}") + logger.debug(f"生成的回复: {content}") # 移除旧的检查新消息逻辑,这应该由 conversation 控制流处理 return content From a7eebc8988a51ecba9ec5956a306fcce4a8cf8a9 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 18:59:21 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=B3=A8=E9=87=8A=E6=8E=89=E9=A2=91?= =?UTF-8?q?=E7=B9=81=E6=97=A5=E5=BF=97=EF=BC=8C=E8=AE=A9=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=9B=B4=E5=8A=A0=E5=A5=BD=E7=9C=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/waiter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/PFC/waiter.py b/src/plugins/PFC/waiter.py index eaf8a768a..15405bc54 100644 --- a/src/plugins/PFC/waiter.py +++ b/src/plugins/PFC/waiter.py @@ -46,7 +46,7 @@ class Waiter: return True # 返回 True 表示超时 await asyncio.sleep(5) # 每 5 秒检查一次 - logger.info("等待中...") # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出 + # logger.info("等待中...") # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出 async def wait_listening(self, conversation_info: ConversationInfo) -> bool: """倾听用户发言或超时""" From e4b79aba4c9f700f18dcaa0792db3af054755131 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 19:10:54 +0800 Subject: [PATCH 10/11] =?UTF-8?q?=E6=8A=8A=E5=88=B7=E5=B1=8F=E7=9A=84?= =?UTF-8?q?=E4=B8=8D=E6=B3=A8=E9=87=8A=E6=8E=89=EF=BC=8C=E5=A1=9E=E8=BF=9B?= =?UTF-8?q?debug=E9=87=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/action_planner.py | 4 ++-- src/plugins/PFC/waiter.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/PFC/action_planner.py b/src/plugins/PFC/action_planner.py index ff13b5fad..7cdb48e9c 100644 --- a/src/plugins/PFC/action_planner.py +++ b/src/plugins/PFC/action_planner.py @@ -311,10 +311,10 @@ class ActionPlanner: # --- 选择 Prompt --- if last_successful_reply_action in ['direct_reply', 'send_new_message']: prompt_template = PROMPT_FOLLOW_UP - logger.info("使用 PROMPT_FOLLOW_UP (追问决策)") + logger.debug(f"使用 PROMPT_FOLLOW_UP (追问决策)") else: prompt_template = PROMPT_INITIAL_REPLY - logger.info("使用 PROMPT_INITIAL_REPLY (首次/非连续回复决策)") + logger.debug("使用 PROMPT_INITIAL_REPLY (首次/非连续回复决策)") # --- 格式化最终的 Prompt --- prompt = prompt_template.format( diff --git a/src/plugins/PFC/waiter.py b/src/plugins/PFC/waiter.py index 15405bc54..2362e74ed 100644 --- a/src/plugins/PFC/waiter.py +++ b/src/plugins/PFC/waiter.py @@ -46,7 +46,7 @@ class Waiter: return True # 返回 True 表示超时 await asyncio.sleep(5) # 每 5 秒检查一次 - # logger.info("等待中...") # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出 + logger.debug("等待中...") # 可以考虑把这个频繁日志注释掉,只在超时或收到消息时输出 async def wait_listening(self, conversation_info: ConversationInfo) -> bool: """倾听用户发言或超时""" @@ -73,4 +73,4 @@ class Waiter: return True # 返回 True 表示超时 await asyncio.sleep(5) # 每 5 秒检查一次 - logger.info("倾听等待中...") # 同上,可以考虑注释掉 + logger.debug("倾听等待中...") # 同上,可以考虑注释掉 From dba967c953582a681ade0d92d4e7062714b5a856 Mon Sep 17 00:00:00 2001 From: Bakadax Date: Sun, 27 Apr 2025 19:31:01 +0800 Subject: [PATCH 11/11] =?UTF-8?q?=E6=8A=8A=E6=89=80=E6=9C=89=E7=9A=84?= =?UTF-8?q?=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95=E9=83=BD=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/PFC/chat_observer.py | 4 ++-- src/plugins/PFC/pfc.py | 16 +++++++--------- src/plugins/PFC/pfc_KnowledgeFetcher.py | 12 ++++++++---- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/plugins/PFC/chat_observer.py b/src/plugins/PFC/chat_observer.py index 0305289fa..915618474 100644 --- a/src/plugins/PFC/chat_observer.py +++ b/src/plugins/PFC/chat_observer.py @@ -287,7 +287,7 @@ class ChatObserver: self._running = True self._task = asyncio.create_task(self._update_loop()) - logger.info(f"ChatObserver for {self.stream_id} started") + logger.debug(f"ChatObserver for {self.stream_id} started") def stop(self): """停止观察器""" @@ -296,7 +296,7 @@ class ChatObserver: self._update_complete.set() # 设置完成事件以解除等待 if self._task: self._task.cancel() - logger.info(f"ChatObserver for {self.stream_id} stopped") + logger.debug(f"ChatObserver for {self.stream_id} stopped") async def process_chat_history(self, messages: list): """处理聊天历史 diff --git a/src/plugins/PFC/pfc.py b/src/plugins/PFC/pfc.py index 5a70d02f3..a3acee47b 100644 --- a/src/plugins/PFC/pfc.py +++ b/src/plugins/PFC/pfc.py @@ -250,15 +250,13 @@ class GoalAnalyzer: async def analyze_conversation(self, goal, reasoning): messages = self.chat_observer.get_cached_messages() - chat_history_text = "" - for msg in messages: - time_str = datetime.datetime.fromtimestamp(msg["time"]).strftime("%H:%M:%S") - user_info = UserInfo.from_dict(msg.get("user_info", {})) - sender = user_info.user_nickname or f"用户{user_info.user_id}" - if sender == self.name: - sender = "你说" - chat_history_text += f"{time_str},{sender}:{msg.get('processed_plain_text', '')}\n" - + chat_history_text = await build_readable_messages( + messages, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + ) identity_details_only = self.identity_detail_info identity_addon = "" if isinstance(identity_details_only, str): diff --git a/src/plugins/PFC/pfc_KnowledgeFetcher.py b/src/plugins/PFC/pfc_KnowledgeFetcher.py index 95e66c8cd..958b05bf8 100644 --- a/src/plugins/PFC/pfc_KnowledgeFetcher.py +++ b/src/plugins/PFC/pfc_KnowledgeFetcher.py @@ -5,6 +5,7 @@ from ..models.utils_model import LLMRequest from ...config.config import global_config from ..chat.message import Message from ..knowledge.knowledge_lib import qa_manager +from ..utils.chat_message_builder import build_readable_messages logger = get_module_logger("knowledge_fetcher") @@ -50,10 +51,13 @@ class KnowledgeFetcher: Tuple[str, str]: (获取的知识, 知识来源) """ # 构建查询上下文 - chat_history_text = "" - for msg in chat_history: - # sender = msg.message_info.user_info.user_nickname or f"用户{msg.message_info.user_info.user_id}" - chat_history_text += f"{msg.detailed_plain_text}\n" + chat_history_text = await build_readable_messages( + chat_history, + replace_bot_name=True, + merge_messages=False, + timestamp_mode="relative", + read_mark=0.0, + ) # 从记忆中获取相关知识 related_memory = await HippocampusManager.get_instance().get_memory_from_text(