From 697d3b734f245a735e2faf871a7132dbfff56230 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Fri, 31 Oct 2025 21:23:06 +0800 Subject: [PATCH] =?UTF-8?q?refactor(replyer):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E4=B8=8E=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 `_generate_prompt` 方法中的 `reply_message` 参数统一为 `DatabaseMessages` 类型,并确保从 `anchor_message` 获取的 `sender_info` 被正确转换为 `UserInfo` 模型。同时,简化了情绪提示词的构建逻辑。 此外,在 `_get_master_prompt` 中增加了对 `user_info` 的空值检查,以避免潜在的属性访问错误。 --- src/chat/replyer/default_generator.py | 32 +++++++++++++-------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/chat/replyer/default_generator.py b/src/chat/replyer/default_generator.py index cb98f897e..ea05383d0 100644 --- a/src/chat/replyer/default_generator.py +++ b/src/chat/replyer/default_generator.py @@ -258,6 +258,8 @@ class DefaultReplyer: if not master_config or not master_config.enable: return "" + if not self.chat_stream.user_info: + return "" platform, user_id = self.chat_stream.platform, self.chat_stream.user_info.user_id try: if user_id: @@ -320,7 +322,7 @@ class DefaultReplyer: available_actions=available_actions, choosen_actions=choosen_actions, enable_tool=enable_tool, - reply_message=reply_message, + reply_message=DatabaseMessages(**reply_message) if isinstance(reply_message, dict) else reply_message, ) if not prompt: @@ -981,7 +983,6 @@ class DefaultReplyer: if unread_messages: unread_lines = [] for msg in unread_messages: - msg_id = msg.message_id msg_time = time.strftime("%H:%M:%S", time.localtime(msg.time)) msg_content = msg.processed_plain_text @@ -1082,7 +1083,7 @@ class DefaultReplyer: if unread_messages: unread_lines = [] for msg in unread_messages: - msg_id = msg.get("message_id", "") + msg.get("message_id", "") msg_time = time.strftime("%H:%M:%S", time.localtime(msg.get("time", time.time()))) msg_content = msg.get("processed_plain_text", "") @@ -1154,7 +1155,7 @@ class DefaultReplyer: extra_info: str = "", available_actions: dict[str, ActionInfo] | None = None, enable_tool: bool = True, - reply_message: dict[str, Any] | DatabaseMessages | None = None, + reply_message: DatabaseMessages | None = None, ) -> str: """ 构建回复器上下文 @@ -1643,17 +1644,11 @@ class DefaultReplyer: target = "(无消息内容)" # 添加情绪状态获取 + mood_prompt = "" if global_config.mood.enable_mood: chat_mood = mood_manager.get_mood_by_chat_id(chat_id) mood_prompt = chat_mood.mood_state - # 检查是否有愤怒状态的补充提示词 - angry_prompt_addition = mood_manager.get_angry_prompt_addition(chat_id) - if angry_prompt_addition: - mood_prompt = f"{mood_prompt}。{angry_prompt_addition}" - else: - mood_prompt = "" - # 从内存获取历史消息,避免重复查询数据库 from src.plugin_system.apis.chat_api import get_chat_manager @@ -1800,11 +1795,16 @@ class DefaultReplyer: platform=self.chat_stream.platform, ) - # 从 DatabaseMessages 获取 sender_info - if anchor_message: - sender_info = anchor_message.user_info - else: - sender_info = None + # 从 DatabaseMessages 获取 sender_info 并转换为 UserInfo + sender_info = None + if anchor_message and anchor_message.user_info: + db_user_info = anchor_message.user_info + sender_info = UserInfo( + platform=db_user_info.platform, + user_id=db_user_info.user_id, + user_nickname=db_user_info.user_nickname, + user_cardname=db_user_info.user_cardname, + ) return MessageSending( message_id=message_id, # 使用片段的唯一ID