fix ruff
This commit is contained in:
SengokuCola
2025-07-26 21:34:29 +08:00
parent 034fc48b53
commit 5822ba3a89
3 changed files with 45 additions and 28 deletions

View File

@@ -1425,3 +1425,4 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@@ -261,6 +261,9 @@ class HeartFChatting:
message_data = {} message_data = {}
action_type = "no_action" action_type = "no_action"
reply_text = "" # 初始化reply_text变量避免UnboundLocalError reply_text = "" # 初始化reply_text变量避免UnboundLocalError
gen_task = None # 初始化gen_task变量避免UnboundLocalError
reply_to_str = "" # 初始化reply_to_str变量
# 创建新的循环信息 # 创建新的循环信息
cycle_timers, thinking_id = self.start_cycle() cycle_timers, thinking_id = self.start_cycle()
@@ -312,10 +315,9 @@ class HeartFChatting:
"action_prompt": "", "action_prompt": "",
} }
target_message = message_data target_message = message_data
# 如果normal开始一个回复生成进程先准备好回复其实是和planer同时进行的
gen_task = None # 如果normal模式且不跳过规划器开始一个回复生成进程先准备好回复其实是和planer同时进行的
reply_to_str = "" if not skip_planner:
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"))
@@ -341,35 +343,50 @@ class HeartFChatting:
f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作" f"{self.log_prefix}{global_config.bot.nickname} 决定进行回复, 同时执行{action_type}动作"
) )
else: else:
if not gen_task.done(): # 只有在gen_task存在时才进行相关操作
gen_task.cancel() if gen_task is not None:
logger.debug(f"{self.log_prefix} 已取消预生成的回复任务") if not gen_task.done():
logger.info( gen_task.cancel()
f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复,但选择执行{action_type},不发表回复" logger.debug(f"{self.log_prefix} 已取消预生成的回复任务")
) logger.info(
else: 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} 预生成的回复任务已完成") else:
logger.info( content = " ".join([item[1] for item in gen_task.result() if item[0] == "text"])
f"{self.log_prefix}{global_config.bot.nickname} 原本想要回复:{content},但选择执行{action_type},不发表回复" 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 == "reply" or is_parallel: if action_type == "reply" or is_parallel:
# 等待回复生成完毕 # 等待回复生成完毕
if self.loop_mode == ChatMode.NORMAL: if self.loop_mode == ChatMode.NORMAL:
gather_timeout = global_config.chat.thinking_timeout # 只有在gen_task存在时才等待
try: if gen_task is not None:
response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout) gather_timeout = global_config.chat.thinking_timeout
except asyncio.TimeoutError: try:
logger.warning(f"{self.log_prefix} 回复生成超时>{global_config.chat.thinking_timeout}s已跳过") response_set = await asyncio.wait_for(gen_task, timeout=gather_timeout)
response_set = None except asyncio.TimeoutError:
logger.warning(f"{self.log_prefix} 回复生成超时>{global_config.chat.thinking_timeout}s已跳过")
response_set = None
# 模型炸了或超时,没有回复内容生成 # 模型炸了或超时,没有回复内容生成
if not response_set: if not response_set:
logger.warning(f"{self.log_prefix}模型未生成回复内容") logger.warning(f"{self.log_prefix}模型未生成回复内容")
return False return False
else:
# 如果没有预生成任务,直接生成回复
if not reply_to_str:
reply_to_str = await self.build_reply_to_str(action_message)
with Timer("回复生成", cycle_timers):
response_set = await self._generate_response(action_message, available_actions, reply_to_str, "chat.replyer.normal")
if not response_set:
logger.warning(f"{self.log_prefix}模型未生成回复内容")
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模式)")

View File

@@ -8,9 +8,8 @@
from typing import List, Tuple, Type from typing import List, Tuple, Type
# 导入新插件系统 # 导入新插件系统
from src.plugin_system import BasePlugin, register_plugin, ComponentInfo, ActionActivationType from src.plugin_system import BasePlugin, register_plugin, ComponentInfo
from src.plugin_system.base.config_types import ConfigField from src.plugin_system.base.config_types import ConfigField
from src.config.config import global_config
# 导入依赖的系统组件 # 导入依赖的系统组件
from src.common.logger import get_logger from src.common.logger import get_logger