From eae399fb95e61750257f61b537fb1cd5c24d9d6e Mon Sep 17 00:00:00 2001 From: UnCLASPrommer Date: Mon, 14 Jul 2025 23:40:09 +0800 Subject: [PATCH] typing change, use enum instead of string, fix typo --- src/chat/focus_chat/heartFC_chat.py | 33 ++++++++++----------- src/chat/message_receive/bot.py | 4 +-- src/chat/message_receive/message.py | 2 +- src/chat/planner_actions/action_modifier.py | 1 - src/chat/planner_actions/planner.py | 10 +++---- src/plugin_system/base/component_types.py | 1 + src/plugins/built_in/core_actions/plugin.py | 8 ++--- 7 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/chat/focus_chat/heartFC_chat.py b/src/chat/focus_chat/heartFC_chat.py index 577af5d00..0978b4b59 100644 --- a/src/chat/focus_chat/heartFC_chat.py +++ b/src/chat/focus_chat/heartFC_chat.py @@ -18,13 +18,12 @@ from src.chat.focus_chat.hfc_utils import CycleDetail from src.chat.focus_chat.hfc_utils import get_recent_message_stats from src.person_info.relationship_builder_manager import relationship_builder_manager from src.person_info.person_info import get_person_info_manager -from src.plugin_system.base.component_types import ActionInfo +from src.plugin_system.base.component_types import ActionInfo, ChatMode from src.plugin_system.apis import generator_api, send_api, message_api from src.chat.willing.willing_manager import get_willing_manager from ...mais4u.mais4u_chat.priority_manager import PriorityManager - ERROR_LOOP_INFO = { "loop_plan_info": { "action_result": { @@ -87,7 +86,7 @@ class HeartFChatting: self.relationship_builder = relationship_builder_manager.get_or_create_builder(self.stream_id) - self.loop_mode = "normal" + self.loop_mode = ChatMode.NORMAL # 初始循环模式为普通模式 # 新增:消息计数器和疲惫阈值 self._message_count = 0 # 发送的消息计数 @@ -120,13 +119,11 @@ class HeartFChatting: self.priority_manager = PriorityManager( normal_queue_max_size=5, ) - self.loop_mode = "priority" + self.loop_mode = ChatMode.PRIORITY else: self.priority_manager = None - logger.info( - f"{self.log_prefix} HeartFChatting 初始化完成" - ) + logger.info(f"{self.log_prefix} HeartFChatting 初始化完成") self.energy_value = 100 @@ -192,14 +189,14 @@ class HeartFChatting: ) async def _loopbody(self): - if self.loop_mode == "focus": + if self.loop_mode == ChatMode.FOCUS: self.energy_value -= 5 * global_config.chat.focus_value if self.energy_value <= 0: - self.loop_mode = "normal" + self.loop_mode = ChatMode.NORMAL return True return await self._observe() - elif self.loop_mode == "normal": + elif self.loop_mode == ChatMode.NORMAL: new_messages_data = get_raw_msg_by_timestamp_with_chat( chat_id=self.stream_id, timestamp_start=self.last_read_time, @@ -210,7 +207,7 @@ class HeartFChatting: ) if len(new_messages_data) > 4 * global_config.chat.focus_value: - self.loop_mode = "focus" + self.loop_mode = ChatMode.FOCUS self.energy_value = 100 return True @@ -255,14 +252,14 @@ class HeartFChatting: logger.error(f"{self.log_prefix} 动作修改失败: {e}") # 如果normal,开始一个回复生成进程,先准备好回复(其实是和planer同时进行的) - if self.loop_mode == "normal": + if self.loop_mode == ChatMode.NORMAL: 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)) with Timer("规划器", cycle_timers): plan_result = await self.action_planner.plan(mode=self.loop_mode) - action_result = plan_result.get("action_result", {}) + action_result: dict = plan_result.get("action_result", {}) # type: ignore action_type, action_data, reasoning, is_parallel = ( action_result.get("action_type", "error"), action_result.get("action_data", {}), @@ -272,7 +269,7 @@ class HeartFChatting: action_data["loop_start_time"] = loop_start_time - if self.loop_mode == "normal": + if self.loop_mode == ChatMode.NORMAL: if action_type == "no_action": logger.info(f"[{self.log_prefix}] {global_config.bot.nickname} 决定进行回复") elif is_parallel: @@ -337,7 +334,7 @@ class HeartFChatting: self.end_cycle(loop_info, cycle_timers) self.print_cycle_info(cycle_timers) - if self.loop_mode == "normal": + if self.loop_mode == ChatMode.NORMAL: await self.willing_manager.after_generate_reply_handle(message_data.get("message_id", "")) return True @@ -611,17 +608,17 @@ class HeartFChatting: ) reply_text = "" - first_replyed = False + first_replied = False for reply_seg in reply_set: data = reply_seg[1] - if not first_replyed: + if not first_replied: if need_reply: await send_api.text_to_stream( text=data, stream_id=self.chat_stream.stream_id, reply_to=reply_to, typing=False ) else: await send_api.text_to_stream(text=data, stream_id=self.chat_stream.stream_id, typing=False) - first_replyed = True + first_replied = True else: await send_api.text_to_stream(text=data, stream_id=self.chat_stream.stream_id, typing=True) reply_text += data diff --git a/src/chat/message_receive/bot.py b/src/chat/message_receive/bot.py index a029c2c4c..2084dcbf5 100644 --- a/src/chat/message_receive/bot.py +++ b/src/chat/message_receive/bot.py @@ -2,7 +2,7 @@ import traceback import os import re -from typing import Dict, Any +from typing import Dict, Any, Optional from maim_message import UserInfo from src.common.logger import get_logger @@ -210,7 +210,7 @@ class ChatBot: # 确认从接口发来的message是否有自定义的prompt模板信息 if message.message_info.template_info and not message.message_info.template_info.template_default: - template_group_name = message.message_info.template_info.template_name + template_group_name: Optional[str] = message.message_info.template_info.template_name # type: ignore template_items = message.message_info.template_info.template_items async with global_prompt_manager.async_message_scope(template_group_name): if isinstance(template_items, dict): diff --git a/src/chat/message_receive/message.py b/src/chat/message_receive/message.py index 511a4b704..a27afedb2 100644 --- a/src/chat/message_receive/message.py +++ b/src/chat/message_receive/message.py @@ -479,7 +479,7 @@ def message_from_db_dict(db_dict: dict) -> MessageRecv: msg = MessageRecv(recv_dict) # 从数据库字典中填充其他可选字段 - msg.interest_value = db_dict.get("interest_value") + msg.interest_value = db_dict.get("interest_value", 0.0) msg.is_mentioned = db_dict.get("is_mentioned") msg.priority_mode = db_dict.get("priority_mode", "interest") msg.priority_info = db_dict.get("priority_info") diff --git a/src/chat/planner_actions/action_modifier.py b/src/chat/planner_actions/action_modifier.py index ba9fcbdc3..c86f3f58e 100644 --- a/src/chat/planner_actions/action_modifier.py +++ b/src/chat/planner_actions/action_modifier.py @@ -7,7 +7,6 @@ from typing import List, Any, Dict, TYPE_CHECKING from src.common.logger import get_logger from src.config.config import global_config from src.llm_models.utils_model import LLMRequest -from src.chat.focus_chat.hfc_utils import CycleDetail from src.chat.message_receive.chat_stream import get_chat_manager, ChatMessageContext from src.chat.planner_actions.action_manager import ActionManager from src.chat.utils.chat_message_builder import get_raw_msg_before_timestamp_with_chat, build_readable_messages diff --git a/src/chat/planner_actions/planner.py b/src/chat/planner_actions/planner.py index f1eaf6ac1..cbd4c23ef 100644 --- a/src/chat/planner_actions/planner.py +++ b/src/chat/planner_actions/planner.py @@ -19,7 +19,7 @@ from src.chat.utils.chat_message_builder import ( from src.chat.utils.utils import get_chat_type_and_target_info from src.chat.planner_actions.action_manager import ActionManager from src.chat.message_receive.chat_stream import get_chat_manager -from src.plugin_system.base.component_types import ActionInfo +from src.plugin_system.base.component_types import ActionInfo, ChatMode logger = get_logger("planner") @@ -79,7 +79,7 @@ class ActionPlanner: self.last_obs_time_mark = 0.0 - async def plan(self, mode: str = "focus") -> Dict[str, Dict[str, Any]]: # sourcery skip: dict-comprehension + async def plan(self, mode: ChatMode = ChatMode.FOCUS) -> Dict[str, Dict[str, Any] | str]: # sourcery skip: dict-comprehension """ 规划器 (Planner): 使用LLM根据上下文决定做出什么动作。 """ @@ -108,7 +108,7 @@ class ActionPlanner: # 如果没有可用动作或只有no_reply动作,直接返回no_reply if not current_available_actions: - if mode == "focus": + if mode == ChatMode.FOCUS: action = "no_reply" else: action = "no_action" @@ -217,7 +217,7 @@ class ActionPlanner: is_group_chat: bool, # Now passed as argument chat_target_info: Optional[dict], # Now passed as argument current_available_actions: Dict[str, ActionInfo], - mode: str = "focus", + mode: ChatMode = ChatMode.FOCUS, ) -> str: # sourcery skip: use-join """构建 Planner LLM 的提示词 (获取模板并填充数据)""" try: @@ -247,7 +247,7 @@ class ActionPlanner: self.last_obs_time_mark = time.time() - if mode == "focus": + if mode == ChatMode.FOCUS: by_what = "聊天内容" no_action_block = """重要说明1: - 'no_reply' 表示只进行不进行回复,等待合适的回复时机 diff --git a/src/plugin_system/base/component_types.py b/src/plugin_system/base/component_types.py index c21f3ba33..9beac16ab 100644 --- a/src/plugin_system/base/component_types.py +++ b/src/plugin_system/base/component_types.py @@ -33,6 +33,7 @@ class ChatMode(Enum): FOCUS = "focus" # Focus聊天模式 NORMAL = "normal" # Normal聊天模式 + PRIORITY = "priority" # 优先级聊天模式 ALL = "all" # 所有聊天模式 def __str__(self): diff --git a/src/plugins/built_in/core_actions/plugin.py b/src/plugins/built_in/core_actions/plugin.py index a3c46f6cf..83b0abfda 100644 --- a/src/plugins/built_in/core_actions/plugin.py +++ b/src/plugins/built_in/core_actions/plugin.py @@ -110,16 +110,16 @@ class ReplyAction(BaseAction): # 构建回复文本 reply_text = "" - first_replyed = False + first_replied = False for reply_seg in reply_set: data = reply_seg[1] - if not first_replyed: + if not first_replied: if need_reply: await self.send_text(content=data, reply_to=self.action_data.get("reply_to", ""), typing=False) - first_replyed = True + first_replied = True else: await self.send_text(content=data, typing=False) - first_replyed = True + first_replied = True else: await self.send_text(content=data, typing=True) reply_text += data