From b8905c672a80034a2c37e5d708b3cec7b65b9d25 Mon Sep 17 00:00:00 2001 From: Navinatte Date: Mon, 11 Aug 2025 22:22:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=83=BD=E9=87=8F=E5=80=BC?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=20----?= =?UTF-8?q?=E4=B9=8B=E5=89=8D=E7=9A=84=E5=A4=AA=E5=88=B7=E5=B1=8F=E4=BA=86?= =?UTF-8?q?...=20-=20=E6=B7=BB=E5=8A=A0=E8=83=BD=E9=87=8F=E5=80=BC?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E6=97=B6=E9=97=B4=E9=97=B4=E9=9A=94=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=20(90=E7=A7=92)=20-=E6=9A=82=E6=97=B6=E5=85=88?= =?UTF-8?q?=E8=BF=99=E4=B9=88=E5=86=99=20-=20=E5=87=8F=E5=B0=91=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E5=88=B7=E5=B1=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改文件: - src/chat/chat_loop/heartFC_chat.py - template/model_config_template.toml: 别问为什么改了这个,问就是手残() --- src/chat/chat_loop/heartFC_chat.py | 96 +++++++++++++++++++++++++++-- template/model_config_template.toml | 2 +- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/src/chat/chat_loop/heartFC_chat.py b/src/chat/chat_loop/heartFC_chat.py index 044f43a11..207f4a586 100644 --- a/src/chat/chat_loop/heartFC_chat.py +++ b/src/chat/chat_loop/heartFC_chat.py @@ -113,8 +113,10 @@ class HeartFChatting: self.focus_energy = 1 self.no_reply_consecutive = 0 - # 最近三次no_reply的新消息兴趣度记录 - self.recent_interest_records: deque = deque(maxlen=3) + + # 能量值日志时间控制 + self.last_energy_log_time = 0 # 上次记录能量值日志的时间 + self.energy_log_interval = 90 # 能量值日志间隔(秒) async def start(self): """检查是否需要启动主循环,如果未激活则启动。""" @@ -163,6 +165,45 @@ class HeartFChatting: self._current_cycle_detail.timers = cycle_timers self._current_cycle_detail.end_time = time.time() + def _handle_energy_completion(self, task: asyncio.Task): + """处理能量循环任务的完成""" + if task.cancelled(): + logger.info(f"{self.log_prefix} 能量循环任务被取消") + elif task.exception(): + logger.error(f"{self.log_prefix} 能量循环任务发生异常: {task.exception()}") + + def _should_log_energy(self) -> bool: + """判断是否应该记录能量值日志(基于时间间隔控制)""" + current_time = time.time() + if current_time - self.last_energy_log_time >= self.energy_log_interval: + self.last_energy_log_time = current_time + return True + return False + + def _log_energy_change(self, action: str, reason: str = ""): + """记录能量值变化日志(受时间间隔控制)""" + if self._should_log_energy(): + if reason: + logger.info(f"{self.log_prefix} {action},{reason},当前能量值:{self.energy_value:.1f}") + else: + logger.info(f"{self.log_prefix} {action},当前能量值:{self.energy_value:.1f}") + else: + # 仍然以debug级别记录,便于调试 + if reason: + logger.debug(f"{self.log_prefix} {action},{reason},当前能量值:{self.energy_value:.1f}") + else: + logger.debug(f"{self.log_prefix} {action},当前能量值:{self.energy_value:.1f}") + + async def _energy_loop(self): + while self.running: + await asyncio.sleep(10) + if self.loop_mode == ChatMode.NORMAL: + self.energy_value -= 0.3 + self.energy_value = max(self.energy_value, 0.3) + if self.loop_mode == ChatMode.FOCUS: + self.energy_value -= 0.6 + self.energy_value = max(self.energy_value, 0.3) + def print_cycle_info(self, cycle_timers): # 记录循环信息和计时器结果 timer_strings = [] @@ -285,9 +326,56 @@ class HeartFChatting: # 统一的消息处理逻辑 should_process,interest_value = await self._should_process_messages(recent_messages_dict) - if should_process: + if self.loop_mode == ChatMode.FOCUS: + + if self.last_action == "no_reply": + if not await self._execute_no_reply(recent_messages_dict): + self.energy_value -= 0.3 / global_config.chat.focus_value + self._log_energy_change("能量值减少") + await asyncio.sleep(0.5) + return True + self.last_read_time = time.time() - await self._observe(interest_value = interest_value) + + if await self._observe(): + self.energy_value += 1 / global_config.chat.focus_value + self._log_energy_change("能量值增加") + + if self.energy_value <= 1: + self.energy_value = 1 + self.loop_mode = ChatMode.NORMAL + return True + + return True + elif self.loop_mode == ChatMode.NORMAL: + if global_config.chat.focus_value != 0: + if new_message_count > 3 / pow(global_config.chat.focus_value, 0.5): + self.loop_mode = ChatMode.FOCUS + self.energy_value = ( + 10 + (new_message_count / (3 / pow(global_config.chat.focus_value, 0.5))) * 10 + ) + return True + + if self.energy_value >= 30: + self.loop_mode = ChatMode.FOCUS + return True + + if new_message_count >= self.focus_energy: + earliest_messages_data = recent_messages_dict[0] + self.last_read_time = earliest_messages_data.get("time") + + if_think = await self.normal_response(earliest_messages_data) + if if_think: + factor = max(global_config.chat.focus_value, 0.1) + self.energy_value *= 1.1 * factor + self._log_energy_change("进行了思考,能量值按倍数增加") + else: + self.energy_value += 0.1 * global_config.chat.focus_value + self._log_energy_change("没有进行思考,能量值线性增加") + + # 这个可以保持debug级别,因为它是总结性信息 + logger.debug(f"{self.log_prefix} 当前能量值:{self.energy_value:.1f}") + return True else: # Normal模式:消息数量不足,等待 diff --git a/template/model_config_template.toml b/template/model_config_template.toml index 92ac8881f..3630b1523 100644 --- a/template/model_config_template.toml +++ b/template/model_config_template.toml @@ -15,7 +15,7 @@ retry_interval = 10 # 重试间隔时间(单位:秒) [[api_providers]] # SiliconFlow的API服务商配置 name = "SiliconFlow" base_url = "https://api.siliconflow.cn/v1" -api_key = "your-siliconflow-api-key" +api_key = "your-siliconflow-api-key-here" client_type = "openai" max_retry = 2 timeout = 30