fix:移除退出判断的重复检测

This commit is contained in:
SengokuCola
2025-07-07 20:33:29 +08:00
parent e564e9713a
commit f58d2adb0b
2 changed files with 71 additions and 167 deletions

View File

@@ -478,10 +478,6 @@ class HeartFChatting:
) )
# 设置系统命令,在下次循环检查时触发退出 # 设置系统命令,在下次循环检查时触发退出
command = "stop_focus_chat" command = "stop_focus_chat"
elif self._message_count >= current_threshold and global_config.chat.chat_mode != "auto":
logger.info(
f"{self.log_prefix} [非auto模式] 已发送 {self._message_count} 条消息,达到疲惫阈值 {current_threshold}但非auto模式不会自动退出"
)
else: else:
if reply_text == "timeout": if reply_text == "timeout":
self.reply_timeout_count += 1 self.reply_timeout_count += 1

View File

@@ -109,41 +109,11 @@ class NoReplyAction(BaseAction):
if global_config.chat.chat_mode == "auto" and self.is_group: if global_config.chat.chat_mode == "auto" and self.is_group:
# 检查是否超时 # 检查是否超时
if elapsed_time >= self._max_timeout: if elapsed_time >= self._max_timeout or self._check_no_activity_and_exit_focus(current_time):
logger.info(f"{self.log_prefix} 达到最大等待时间{self._max_timeout}秒,退出专注模式") logger.info(f"{self.log_prefix} 等待时间过久({self._max_timeout}或过去10分钟完全没有发言,退出专注模式")
# 标记退出专注模式 # 标记退出专注模式
self.action_data["_system_command"] = "stop_focus_chat" self.action_data["_system_command"] = "stop_focus_chat"
exit_reason = f"{global_config.bot.nickname}(你)等待了{self._max_timeout}秒,感觉群里没有新内容,决定退出专注模式,稍作休息" exit_reason = f"{global_config.bot.nickname}(你)等待了{self._max_timeout}秒,或完全没有说话,感觉群里没有新内容,决定退出专注模式,稍作休息"
await self.store_action_info(
action_build_into_prompt=True,
action_prompt_display=exit_reason,
action_done=True,
)
return True, exit_reason
# **新增**:检查回复频率,决定是否退出专注模式
should_exit_focus = await self._check_frequency_and_exit_focus(current_time)
if should_exit_focus:
logger.info(f"{self.log_prefix} 检测到回复频率过高,退出专注模式")
# 标记退出专注模式
self.action_data["_system_command"] = "stop_focus_chat"
exit_reason = (
f"{global_config.bot.nickname}(你)发现自己回复太频繁了,决定退出专注模式,稍作休息"
)
await self.store_action_info(
action_build_into_prompt=True,
action_prompt_display=exit_reason,
action_done=True,
)
return True, exit_reason
# **新增**检查过去10分钟是否完全没有发言如果是则退出专注模式
should_exit_no_activity = await self._check_no_activity_and_exit_focus(current_time)
if should_exit_no_activity:
logger.info(f"{self.log_prefix} 检测到过去10分钟完全没有发言退出专注模式")
# 标记退出专注模式
self.action_data["_system_command"] = "stop_focus_chat"
exit_reason = f"{global_config.bot.nickname}发现自己过去10分钟完全没有说话感觉可能不太活跃决定退出专注模式"
await self.store_action_info( await self.store_action_info(
action_build_into_prompt=True, action_build_into_prompt=True,
action_prompt_display=exit_reason, action_prompt_display=exit_reason,
@@ -220,14 +190,6 @@ class NoReplyAction(BaseAction):
frequency_block = "" frequency_block = ""
should_skip_llm_judge = False # 是否跳过LLM判断 should_skip_llm_judge = False # 是否跳过LLM判断
# 【新增】如果是私聊环境,跳过疲劳度检查
if not self.is_group:
frequency_block = "你正在和别人私聊,你不会疲惫,正常聊天即可。"
should_skip_llm_judge = False
logger.debug(f"{self.log_prefix} 私聊环境,跳过疲劳度检查")
else:
try: try:
# 获取过去10分钟的所有消息 # 获取过去10分钟的所有消息
past_10min_time = current_time - 600 # 10分钟前 past_10min_time = current_time - 600 # 10分钟前
@@ -297,6 +259,7 @@ class NoReplyAction(BaseAction):
frequency_block = "" frequency_block = ""
# 如果决定跳过LLM判断直接更新时间并继续等待 # 如果决定跳过LLM判断直接更新时间并继续等待
if should_skip_llm_judge: if should_skip_llm_judge:
last_judge_time = time.time() # 更新判断时间,避免立即重新判断 last_judge_time = time.time() # 更新判断时间,避免立即重新判断
continue # 跳过本次LLM判断继续循环等待 continue # 跳过本次LLM判断继续循环等待
@@ -389,7 +352,10 @@ class NoReplyAction(BaseAction):
logger.error(f"{self.log_prefix} 模型判断异常: {e},继续等待") logger.error(f"{self.log_prefix} 模型判断异常: {e},继续等待")
last_judge_time = time.time() # 异常时也更新时间,避免频繁重试 last_judge_time = time.time() # 异常时也更新时间,避免频繁重试
# 每10秒输出一次等待状态 # 每10秒输出一次等待状态
logger.info(f"{self.log_prefix} 开始等待新消息...")
if elapsed_time < 60: if elapsed_time < 60:
if int(elapsed_time) % 10 == 0 and int(elapsed_time) > 0: if int(elapsed_time) % 10 == 0 and int(elapsed_time) > 0:
logger.debug(f"{self.log_prefix} 已等待{elapsed_time:.0f}秒,等待新消息...") logger.debug(f"{self.log_prefix} 已等待{elapsed_time:.0f}秒,等待新消息...")
@@ -414,65 +380,7 @@ class NoReplyAction(BaseAction):
) )
return False, f"不回复动作执行失败: {e}" return False, f"不回复动作执行失败: {e}"
async def _check_frequency_and_exit_focus(self, current_time: float) -> bool: def _check_no_activity_and_exit_focus(self, current_time: float) -> bool:
"""检查回复频率,决定是否退出专注模式
Args:
current_time: 当前时间戳
Returns:
bool: 是否应该退出专注模式
"""
try:
# 只在auto模式下进行频率检查
if global_config.chat.chat_mode != "auto":
return False
# 获取检查窗口内的所有消息
window_start_time = current_time - self._frequency_check_window
all_messages = message_api.get_messages_by_time_in_chat(
chat_id=self.chat_id,
start_time=window_start_time,
end_time=current_time,
)
if not all_messages:
return False
# 统计bot自己的回复数量
bot_message_count = 0
user_id = global_config.bot.qq_account
for message in all_messages:
sender_id = message.get("user_id", "")
if sender_id == user_id:
bot_message_count += 1
# 计算当前回复频率(每分钟回复数)
window_minutes = self._frequency_check_window / 60
current_frequency = bot_message_count / window_minutes
# 计算阈值频率:使用 exit_focus_threshold * 1.5
threshold_multiplier = global_config.chat.exit_focus_threshold * 1.5
threshold_frequency = global_config.chat.get_current_talk_frequency(self.chat_id) * threshold_multiplier
# 判断是否超过阈值
if current_frequency > threshold_frequency:
logger.info(
f"{self.log_prefix} 回复频率检查:当前频率 {current_frequency:.2f}/分钟,超过阈值 {threshold_frequency:.2f}/分钟 (exit_threshold={global_config.chat.exit_focus_threshold} * 1.5),准备退出专注模式"
)
return True
else:
logger.debug(
f"{self.log_prefix} 回复频率检查:当前频率 {current_frequency:.2f}/分钟,未超过阈值 {threshold_frequency:.2f}/分钟 (exit_threshold={global_config.chat.exit_focus_threshold} * 1.5)"
)
return False
except Exception as e:
logger.error(f"{self.log_prefix} 检查回复频率时出错: {e}")
return False
async def _check_no_activity_and_exit_focus(self, current_time: float) -> bool:
"""检查过去10分钟是否完全没有发言决定是否退出专注模式 """检查过去10分钟是否完全没有发言决定是否退出专注模式
Args: Args: