From 040ebf18d961b60db62446784850cd755f889979 Mon Sep 17 00:00:00 2001 From: SengokuCola <1026294844@qq.com> Date: Thu, 26 Jun 2025 00:18:21 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9Afocus=E7=8E=B0=E5=9C=A8=E6=9C=89?= =?UTF-8?q?=E6=8C=81=E7=BB=AD=E6=97=B6=E9=97=B4=E5=92=8C=E5=86=B7=E5=8D=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chat/heart_flow/sub_heartflow.py | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/chat/heart_flow/sub_heartflow.py b/src/chat/heart_flow/sub_heartflow.py index b78fba3ec..c815c589e 100644 --- a/src/chat/heart_flow/sub_heartflow.py +++ b/src/chat/heart_flow/sub_heartflow.py @@ -50,6 +50,9 @@ class SubHeartflow: self.should_stop = False # 停止标志 self.task: Optional[asyncio.Task] = None # 后台任务 + # focus模式退出冷却时间管理 + self.last_focus_exit_time: float = 0 # 上次退出focus模式的时间 + # 随便水群 normal_chat 和 认真水群 focus_chat 实例 # CHAT模式激活 随便水群 FOCUS模式激活 认真水群 self.heart_fc_instance: Optional[HeartFChatting] = None # 该sub_heartflow的HeartFChatting实例 @@ -140,6 +143,11 @@ class SubHeartflow: """ logger.info(f"{self.log_prefix} 收到NormalChat请求切换到focus模式") + # 检查是否在focus冷却期内 + if self.is_in_focus_cooldown(): + logger.info(f"{self.log_prefix} 正在focus冷却期内,忽略切换到focus模式的请求") + return + # 切换到focus模式 current_state = self.chat_state.chat_status if current_state == ChatState.NORMAL: @@ -280,6 +288,11 @@ class SubHeartflow: await self._stop_heart_fc_chat() state_changed = True + # --- 记录focus模式退出时间 --- + if state_changed and current_state == ChatState.FOCUSED and new_state != ChatState.FOCUSED: + self.last_focus_exit_time = time.time() + logger.debug(f"{log_prefix} 记录focus模式退出时间: {self.last_focus_exit_time}") + # --- 更新状态和最后活动时间 --- if state_changed: self.update_last_chat_state_time() @@ -396,3 +409,28 @@ class SubHeartflow: self.chat_state.chat_status = ChatState.ABSENT # 状态重置为不参与 logger.info(f"{self.log_prefix} 子心流关闭完成。") + + def is_in_focus_cooldown(self) -> bool: + """检查是否在focus模式的冷却期内 + + Returns: + bool: 如果在冷却期内返回True,否则返回False + """ + if self.last_focus_exit_time == 0: + return False + + # 基础冷却时间10分钟,受auto_focus_threshold调控 + base_cooldown = 10 * 60 # 10分钟转换为秒 + cooldown_duration = base_cooldown / global_config.chat.auto_focus_threshold + + current_time = time.time() + elapsed_since_exit = current_time - self.last_focus_exit_time + + is_cooling = elapsed_since_exit < cooldown_duration + + if is_cooling: + remaining_time = cooldown_duration - elapsed_since_exit + remaining_minutes = remaining_time / 60 + logger.debug(f"[{self.log_prefix}] focus冷却中,剩余时间: {remaining_minutes:.1f}分钟 (阈值: {global_config.chat.auto_focus_threshold})") + + return is_cooling