From 0e03c2e492d0d86a75bc117475fd59aa694bacee Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Sat, 26 Apr 2025 13:29:04 +0800 Subject: [PATCH] fix:ruff --- interest_monitor_gui.py | 4 +++- src/heart_flow/background_tasks.py | 8 +++---- src/heart_flow/heartflow.py | 1 - src/heart_flow/sub_heartflow.py | 16 +++++++------- src/heart_flow/subheartflow_manager.py | 30 +++++++++++++++----------- 5 files changed, 32 insertions(+), 27 deletions(-) diff --git a/interest_monitor_gui.py b/interest_monitor_gui.py index 3dbcb28fb..245a0ae99 100644 --- a/interest_monitor_gui.py +++ b/interest_monitor_gui.py @@ -246,7 +246,9 @@ class InterestMonitorApp: self.stream_sub_minds[stream_id] = subflow_entry.get("sub_mind", "N/A") self.stream_chat_states[stream_id] = subflow_entry.get("sub_chat_state", "N/A") self.stream_threshold_status[stream_id] = subflow_entry.get("is_above_threshold", False) - self.stream_last_active[stream_id] = subflow_entry.get("last_changed_state_time") # 存储原始时间戳 + self.stream_last_active[stream_id] = subflow_entry.get( + "last_changed_state_time" + ) # 存储原始时间戳 self.stream_last_interaction[stream_id] = subflow_entry.get( "last_interaction_time" ) # 存储原始时间戳 diff --git a/src/heart_flow/background_tasks.py b/src/heart_flow/background_tasks.py index c66a6128c..85b77579e 100644 --- a/src/heart_flow/background_tasks.py +++ b/src/heart_flow/background_tasks.py @@ -227,25 +227,23 @@ class BackgroundTaskManager: """ # 获取需要清理的子心流列表(包含ID和原因) flows_to_stop = self.subheartflow_manager.get_inactive_subheartflows() - + if not flows_to_stop: return # 没有需要清理的子心流直接返回 logger.info(f"准备删除 {len(flows_to_stop)} 个不活跃(1h)子心流") stopped_count = 0 - + # 逐个停止子心流 for flow_id in flows_to_stop: success = await self.subheartflow_manager.delete_subflow(flow_id) if success: stopped_count += 1 logger.debug(f"[清理任务] 已停止子心流 {flow_id}") - + # 记录最终清理结果 logger.info(f"[清理任务] 清理完成, 共停止 {stopped_count}/{len(flows_to_stop)} 个子心流") - - async def _perform_logging_work(self): """执行一轮状态日志记录。""" await self.interest_logger.log_all_states() diff --git a/src/heart_flow/heartflow.py b/src/heart_flow/heartflow.py index 3f7fa0f12..7d92ae528 100644 --- a/src/heart_flow/heartflow.py +++ b/src/heart_flow/heartflow.py @@ -80,7 +80,6 @@ class Heartflow: # 不再需要传入 self.current_state return await self.subheartflow_manager.get_or_create_subheartflow(subheartflow_id) - async def heartflow_start_working(self): """启动后台任务""" await self.background_task_manager.start_tasks() diff --git a/src/heart_flow/sub_heartflow.py b/src/heart_flow/sub_heartflow.py index b6cbdd228..9cbd7b3a4 100644 --- a/src/heart_flow/sub_heartflow.py +++ b/src/heart_flow/sub_heartflow.py @@ -2,7 +2,7 @@ from .observation import Observation, ChattingObservation import asyncio from src.config.config import global_config import time -from typing import Optional, List, Dict, Callable, Tuple +from typing import Optional, List, Dict, Tuple import traceback from src.common.logger import get_module_logger, LogConfig, SUB_HEARTFLOW_STYLE_CONFIG # noqa: E402 import random @@ -68,7 +68,7 @@ class InterestChatting: self.above_threshold = False self.start_hfc_probability = 0.0 - + async def initialize(self): async with self._task_lock: if self._is_running: @@ -84,7 +84,6 @@ class InterestChatting: self._is_running = True self.update_task = asyncio.create_task(self._run_update_loop(self.update_interval)) logger.debug("后台兴趣更新任务已创建并启动。") - def add_interest_dict(self, message: MessageRecv, interest_value: float, is_mentioned: bool): self.interest_dict[message.message_info.message_id] = (message, interest_value, is_mentioned) @@ -185,7 +184,6 @@ class InterestChatting: self._is_running = False logger.info("InterestChatting 更新循环已停止。") - async def stop_updates(self): """停止后台更新任务,使用锁确保并发安全""" async with self._task_lock: @@ -368,7 +366,7 @@ class SubHeartflow: async def change_chat_state(self, new_state: "ChatState"): """更新sub_heartflow的聊天状态,并管理 HeartFChatting 和 NormalChat 实例及任务""" current_state = self.chat_state.chat_status - + if current_state == new_state: return @@ -408,9 +406,11 @@ class SubHeartflow: if state_changed: self.update_last_chat_state_time() self.history_chat_state.append((current_state, self.chat_state_last_time)) - - logger.info(f"{log_prefix} 麦麦的聊天状态从 {current_state.value} (持续了 {self.chat_state_last_time} 秒) 变更为 {new_state.value}") - + + logger.info( + f"{log_prefix} 麦麦的聊天状态从 {current_state.value} (持续了 {self.chat_state_last_time} 秒) 变更为 {new_state.value}" + ) + self.chat_state.chat_status = new_state self.chat_state_last_time = 0 self.chat_state_changed_time = time.time() diff --git a/src/heart_flow/subheartflow_manager.py b/src/heart_flow/subheartflow_manager.py index d586fd43b..cd32136ae 100644 --- a/src/heart_flow/subheartflow_manager.py +++ b/src/heart_flow/subheartflow_manager.py @@ -11,7 +11,7 @@ from src.plugins.chat.chat_stream import chat_manager # 导入心流相关类 from src.heart_flow.sub_heartflow import SubHeartflow, ChatState -from src.heart_flow.mai_state_manager import MaiState, MaiStateInfo +from src.heart_flow.mai_state_manager import MaiStateInfo from .observation import ChattingObservation # 初始化日志记录器 @@ -32,15 +32,13 @@ class SubHeartflowManager: def __init__(self, mai_state_info: MaiStateInfo): self.subheartflows: Dict[Any, "SubHeartflow"] = {} self._lock = asyncio.Lock() # 用于保护 self.subheartflows 的访问 - self.mai_state_info: MaiStateInfo = mai_state_info # 存储传入的 MaiStateInfo 实例 + self.mai_state_info: MaiStateInfo = mai_state_info # 存储传入的 MaiStateInfo 实例 def get_all_subheartflows(self) -> List["SubHeartflow"]: """获取所有当前管理的 SubHeartflow 实例列表 (快照)。""" return list(self.subheartflows.values()) - - async def get_or_create_subheartflow( - self, subheartflow_id: Any - ) -> Optional["SubHeartflow"]: + + async def get_or_create_subheartflow(self, subheartflow_id: Any) -> Optional["SubHeartflow"]: """获取或创建指定ID的子心流实例 Args: @@ -136,7 +134,7 @@ class SubHeartflowManager: absent_last_time = subheartflow.chat_state_last_time if max_age_seconds and (current_time - absent_last_time) > max_age_seconds: flows_to_stop.append(subheartflow_id) - + return flows_to_stop async def enforce_subheartflow_limits(self): @@ -251,21 +249,30 @@ class SubHeartflowManager: # 再次检查子心流是否仍然存在于管理器中,以防万一在迭代过程中被移除 if subflow.chat_state.chat_status != ChatState.ABSENT: - logger.debug(f"正在将子心流 {stream_name} 的状态从 {subflow.chat_state.chat_status.value} 更改为 ABSENT") + logger.debug( + f"正在将子心流 {stream_name} 的状态从 {subflow.chat_state.chat_status.value} 更改为 ABSENT" + ) try: # 调用 change_chat_state 将状态设置为 ABSENT await subflow.change_chat_state(ChatState.ABSENT) # 验证状态是否真的改变了 - if flow_id in self.subheartflows and self.subheartflows[flow_id].chat_state.chat_status == ChatState.ABSENT: + if ( + flow_id in self.subheartflows + and self.subheartflows[flow_id].chat_state.chat_status == ChatState.ABSENT + ): changed_count += 1 else: - logger.warning(f"[停用] 尝试更改子心流 {stream_name} 状态后,状态仍未变为 ABSENT 或子心流已消失。") + logger.warning( + f"[停用] 尝试更改子心流 {stream_name} 状态后,状态仍未变为 ABSENT 或子心流已消失。" + ) except Exception as e: logger.error(f"[停用] 更改子心流 {stream_name} 状态为 ABSENT 时出错: {e}", exc_info=True) else: logger.debug(f"[停用] 子心流 {stream_name} 已处于 ABSENT 状态,无需更改。") - logger.info(f"下限完成,共处理 {len(flows_to_update)} 个子心流,成功将 {changed_count} 个子心流的状态更改为 ABSENT。") + logger.info( + f"下限完成,共处理 {len(flows_to_update)} 个子心流,成功将 {changed_count} 个子心流的状态更改为 ABSENT。" + ) async def evaluate_interest_and_promote(self): """评估子心流兴趣度,满足条件且未达上限则提升到FOCUSED状态(基于start_hfc_probability)""" @@ -440,4 +447,3 @@ class SubHeartflowManager: logger.error(f"删除 SubHeartflow {subheartflow_id} 时出错: {e}", exc_info=True) else: logger.warning(f"尝试删除不存在的 SubHeartflow: {subheartflow_id}") -