From 8c6dadc6f3dae678ee46cab96181330c6b2ae4fe Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sat, 26 Jul 2025 17:43:18 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/chat_loop/heartFC_chat.py | 56 ++++++++++++++++++++--------- src/chat/planner_actions/planner.py | 5 +-- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/chat/chat_loop/heartFC_chat.py b/src/chat/chat_loop/heartFC_chat.py index ee190f86a..31e5b94d1 100644 --- a/src/chat/chat_loop/heartFC_chat.py +++ b/src/chat/chat_loop/heartFC_chat.py @@ -284,6 +284,8 @@ class HeartFChatting: logger.error(f"{self.log_prefix} 动作修改失败: {e}") # 如果normal,开始一个回复生成进程,先准备好回复(其实是和planer同时进行的) + gen_task = None + reply_to_str = "" if self.loop_mode == ChatMode.NORMAL: reply_to_str = await self.build_reply_to_str(message_data) gen_task = asyncio.create_task(self._generate_response(message_data, available_actions, reply_to_str, "chat.replyer.normal")) @@ -301,20 +303,32 @@ class HeartFChatting: action_data["loop_start_time"] = loop_start_time - if self.loop_mode == ChatMode.NORMAL: - if action_type == "reply": - logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复") - elif is_parallel: + + if action_type == "reply": + logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复") + elif is_parallel: + logger.info( + f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作" + ) + else: + if not gen_task.done(): + gen_task.cancel() + logger.debug(f"{self.log_prefix} 已取消预生成的回复任务") logger.info( - f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作" + f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复,但选择执行{action_type},不发表回复" ) else: - logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定执行{action_type}动作") + content = " ".join([item[1] for item in gen_task.result() if item[0] == "text"]) + logger.debug(f"{self.log_prefix} 预生成的回复任务已完成") + logger.info( + f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复:{content},但选择执行{action_type},不发表回复" + ) + action_message: Dict[str, Any] = message_data or target_message # type: ignore - if action_type == "no_action" or (self.loop_mode == ChatMode.FOCUS and action_type == "reply"): + if action_type == "reply" or is_parallel: # 等待回复生成完毕 - if action_type == "no_action": + if self.loop_mode == ChatMode.NORMAL: gather_timeout = global_config.chat.thinking_timeout try: response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout) @@ -322,18 +336,10 @@ class HeartFChatting: logger.warning(f"{self.log_prefix} 回复生成超时>{global_config.chat.thinking_timeout}s,已跳过") response_set = None - if response_set: - content = " ".join([item[1] for item in response_set if item[0] == "text"]) - # 模型炸了或超时,没有回复内容生成 if not response_set: logger.warning(f"{self.log_prefix}模型未生成回复内容") return False - elif action_type not in ["no_action"] and not is_parallel: - logger.info( - f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复:{content},但选择执行{action_type},不发表回复" - ) - return False else: logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复 (focus模式)") @@ -391,6 +397,24 @@ class HeartFChatting: return True else: + # 如果是并行执行且在normal模式下,需要等待预生成的回复任务完成 + # if self.loop_mode == ChatMode.NORMAL and is_parallel and gen_task: + # # 等待预生成的回复任务完成 + # gather_timeout = global_config.chat.thinking_timeout + # try: + # response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout) + # if response_set: + # # 发送回复 + # with Timer("回复发送", cycle_timers): + # reply_text_parallel = await self._send_response(response_set, reply_to_str, loop_start_time, action_message) + # logger.info(f"{self.log_prefix} 并行执行:已发送回复内容") + # else: + # logger.warning(f"{self.log_prefix} 并行执行:预生成回复内容为空") + # except asyncio.TimeoutError: + # logger.warning(f"{self.log_prefix} 并行执行:回复生成超时>{global_config.chat.thinking_timeout}s,已跳过") + # except asyncio.CancelledError: + # logger.debug(f"{self.log_prefix} 并行执行:回复生成任务已被取消") + # 动作执行计时 with Timer("动作执行", cycle_timers): success, reply_text, command = await self._handle_action( diff --git a/src/chat/planner_actions/planner.py b/src/chat/planner_actions/planner.py index f21cc8d32..4e33bafc8 100644 --- a/src/chat/planner_actions/planner.py +++ b/src/chat/planner_actions/planner.py @@ -225,8 +225,9 @@ class ActionPlanner: reasoning = f"Planner 内部处理错误: {outer_e}" is_parallel = False - if action in current_available_actions: - is_parallel = current_available_actions[action].parallel_action + if mode == ChatMode.NORMAL: + if action in current_available_actions: + is_parallel = current_available_actions[action].parallel_action action_result = { "action_type": action,