fix:优化代码逻辑

This commit is contained in:
SengokuCola
2025-07-26 17:43:18 +08:00
parent 7d2cef9a9c
commit 8c6dadc6f3
2 changed files with 43 additions and 18 deletions

View File

@@ -284,6 +284,8 @@ class HeartFChatting:
logger.error(f"{self.log_prefix} 动作修改失败: {e}") logger.error(f"{self.log_prefix} 动作修改失败: {e}")
# 如果normal开始一个回复生成进程先准备好回复其实是和planer同时进行的 # 如果normal开始一个回复生成进程先准备好回复其实是和planer同时进行的
gen_task = None
reply_to_str = ""
if self.loop_mode == ChatMode.NORMAL: if self.loop_mode == ChatMode.NORMAL:
reply_to_str = await self.build_reply_to_str(message_data) 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")) 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 action_data["loop_start_time"] = loop_start_time
if self.loop_mode == ChatMode.NORMAL:
if action_type == "reply": if action_type == "reply":
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复") logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复")
elif is_parallel: 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( logger.info(
f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作" f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复,但选择执行{action_type},不发表回复"
) )
else: 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 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 gather_timeout = global_config.chat.thinking_timeout
try: try:
response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout) 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已跳过") logger.warning(f"{self.log_prefix} 回复生成超时>{global_config.chat.thinking_timeout}s已跳过")
response_set = None response_set = None
if response_set:
content = " ".join([item[1] for item in response_set if item[0] == "text"])
# 模型炸了或超时,没有回复内容生成 # 模型炸了或超时,没有回复内容生成
if not response_set: if not response_set:
logger.warning(f"{self.log_prefix}模型未生成回复内容") logger.warning(f"{self.log_prefix}模型未生成回复内容")
return False 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: else:
logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复 (focus模式)") logger.info(f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复 (focus模式)")
@@ -391,6 +397,24 @@ class HeartFChatting:
return True return True
else: 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): with Timer("动作执行", cycle_timers):
success, reply_text, command = await self._handle_action( success, reply_text, command = await self._handle_action(

View File

@@ -225,8 +225,9 @@ class ActionPlanner:
reasoning = f"Planner 内部处理错误: {outer_e}" reasoning = f"Planner 内部处理错误: {outer_e}"
is_parallel = False is_parallel = False
if action in current_available_actions: if mode == ChatMode.NORMAL:
is_parallel = current_available_actions[action].parallel_action if action in current_available_actions:
is_parallel = current_available_actions[action].parallel_action
action_result = { action_result = {
"action_type": action, "action_type": action,