From c2f78082b71b7251dd49a8a93d29bedab412cd1c Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Sun, 21 Sep 2025 07:22:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(chat):=20=E4=BF=AE=E5=A4=8D=20plan=20ex?= =?UTF-8?q?ecutor=20=E5=AF=B9=E5=B5=8C=E5=A5=97=20user=5Finfo=20=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E7=BB=93=E6=9E=84=E7=9A=84=E8=A7=A3=E6=9E=90=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `action_message` 可能以对象或字典的形式出现,且用户信息统一嵌套在 `user_info` 字段下。 旧代码在处理字典格式时,未能正确处理此嵌套结构,导致无法正确解析用户信息。本次修改统一了逻辑,确保在两种情况下都能稳定地从 `user_info` 中提取用户ID和昵称,增强了代码的健壮性。 --- src/chat/planner_actions/plan_executor.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/chat/planner_actions/plan_executor.py b/src/chat/planner_actions/plan_executor.py index 009250ccd..e8bf9fdf8 100644 --- a/src/chat/planner_actions/plan_executor.py +++ b/src/chat/planner_actions/plan_executor.py @@ -126,7 +126,13 @@ class PlanExecutor: try: logger.info(f"执行回复动作: {action_info.action_type}, 原因: {action_info.reasoning}") - if action_info.action_message.user_info.user_id == str(global_config.bot.qq_account): + # 获取用户ID - 兼容对象和字典 + if hasattr(action_info.action_message, "user_info"): + user_id = action_info.action_message.user_info.user_id + else: + user_id = action_info.action_message.get("user_info", {}).get("user_id") + + if user_id == str(global_config.bot.qq_account): logger.warning("尝试回复自己,跳过此动作以防止死循环。") return { "action_type": action_info.action_type, @@ -246,15 +252,17 @@ class PlanExecutor: return # 获取用户信息 - 处理对象和字典两种情况 - if hasattr(action_info.action_message, "user_id"): + if hasattr(action_info.action_message, "user_info"): # 对象情况 - user_id = action_info.action_message.user_id - user_name = getattr(action_info.action_message, "user_nickname", user_id) or user_id - user_message = getattr(action_info.action_message, "content", "") + user_info = action_info.action_message.user_info + user_id = user_info.user_id + user_name = user_info.user_nickname or user_id + user_message = action_info.action_message.content else: # 字典情况 - user_id = action_info.action_message.get("user_id", "") - user_name = action_info.action_message.get("user_nickname", user_id) or user_id + user_info = action_info.action_message.get("user_info", {}) + user_id = user_info.get("user_id") + user_name = user_info.get("user_nickname") or user_id user_message = action_info.action_message.get("content", "") if not user_id: From ceb4d2d7bbe4f218c64c44d1bd1cc4ca8a5673fc Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Sun, 21 Sep 2025 08:46:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(chat):=20=E4=BF=AE=E5=A4=8D=E5=B9=B6?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=B6=88=E6=81=AF=E5=9B=9E=E5=A4=8D=E4=B8=8E?= =?UTF-8?q?ID=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 先前的消息回复机制存在多个问题:回复行为随机且不可靠,临时消息ID生成逻辑过于复杂,且在规划和执行过程中ID转换容易出错,导致回复失败。 本次提交通过以下几点进行了全面的修复与优化: - **简化ID生成**:将临时的上下文消息ID生成逻辑从“索引+随机数”简化为纯索引(如 `m1`, `m2`),使其更可预测且易于调试。 - **修正ID替换**:在 `plan_filter` 中增加了关键逻辑,确保在执行 `reply` 动作前,将计划中使用的临时 `target_message_id` 替换为真实的数据库消息ID。 - **稳定回复行为**:移除了 `action_manager` 中的随机回复判断,现在只要存在上下文消息,就会触发引用回复。同时将各 `send_api` 的 `set_reply` 参数默认值改为 `True`,使回复成为默认行为。 - **增强ID兼容性**:修复了 `napcat_adapter` 中将消息ID强制转换为整数的问题,并为 `send_api` 增加了ID回退查找,提高了对不同平台消息ID格式的兼容性。 --- src/chat/planner_actions/action_manager.py | 4 +--- src/chat/planner_actions/plan_filter.py | 5 +++++ src/chat/utils/utils.py | 20 ++----------------- src/plugin_system/apis/send_api.py | 16 +++++++-------- .../napcat_adapter_plugin/src/send_handler.py | 2 +- 5 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/chat/planner_actions/action_manager.py b/src/chat/planner_actions/action_manager.py index ba6196804..4cc2c2a1d 100644 --- a/src/chat/planner_actions/action_manager.py +++ b/src/chat/planner_actions/action_manager.py @@ -430,8 +430,6 @@ class ActionManager: ) # 根据新消息数量决定是否需要引用回复 - need_reply = new_message_count >= random.randint(2, 4) - reply_text = "" is_proactive_thinking = (message_data.get("message_type") == "proactive_thinking") if message_data else True @@ -462,7 +460,7 @@ class ActionManager: text=data, stream_id=chat_stream.stream_id, reply_to_message=message_data, - set_reply=need_reply, + set_reply=bool(message_data), typing=False, ) first_replied = True diff --git a/src/chat/planner_actions/plan_filter.py b/src/chat/planner_actions/plan_filter.py index 765f7292e..3e731f368 100644 --- a/src/chat/planner_actions/plan_filter.py +++ b/src/chat/planner_actions/plan_filter.py @@ -379,6 +379,11 @@ class PlanFilter: if target_message_dict: # 直接使用字典作为action_message,避免DatabaseMessages对象创建失败 target_message_obj = target_message_dict + # 替换action_data中的临时ID为真实ID + if "target_message_id" in action_data: + real_message_id = target_message_dict.get("message_id") or target_message_dict.get("id") + if real_message_id: + action_data["target_message_id"] = real_message_id else: # 如果找不到目标消息,对于reply动作来说这是必需的,应该记录警告 if action == "reply": diff --git a/src/chat/utils/utils.py b/src/chat/utils/utils.py index 38780ec3f..048f7a066 100644 --- a/src/chat/utils/utils.py +++ b/src/chat/utils/utils.py @@ -693,25 +693,9 @@ def assign_message_ids(messages: List[Any]) -> List[Dict[str, Any]]: """ result = [] used_ids = set() - len_i = len(messages) - if len_i > 100: - a = 10 - b = 99 - else: - a = 1 - b = 9 - for i, message in enumerate(messages): - # 生成唯一的简短ID - while True: - # 使用索引+随机数生成简短ID - random_suffix = random.randint(a, b) - message_id = f"m{i + 1}{random_suffix}" - - if message_id not in used_ids: - used_ids.add(message_id) - break - + # 使用简单的索引作为ID + message_id = f"m{i + 1}" result.append({"id": message_id, "message": message}) return result diff --git a/src/plugin_system/apis/send_api.py b/src/plugin_system/apis/send_api.py index 334308795..54e2ed239 100644 --- a/src/plugin_system/apis/send_api.py +++ b/src/plugin_system/apis/send_api.py @@ -80,7 +80,7 @@ def message_dict_to_message_recv(message_dict: Dict[str, Any]) -> Optional[Messa message_info = { "platform": message_dict.get("chat_info_platform", ""), - "message_id": message_dict.get("message_id"), + "message_id": message_dict.get("message_id") or message_dict.get("chat_info_message_id"), "time": message_dict.get("time"), "group_info": group_info, "user_info": user_info, @@ -89,13 +89,13 @@ def message_dict_to_message_recv(message_dict: Dict[str, Any]) -> Optional[Messa "template_info": template_info, } - message_dict = { + new_message_dict = { "message_info": message_info, "raw_message": message_dict.get("processed_plain_text"), "processed_plain_text": message_dict.get("processed_plain_text"), } - message_recv = MessageRecv(message_dict) + message_recv = MessageRecv(new_message_dict) logger.info(f"[SendAPI] 找到匹配的回复消息,发送者: {message_dict.get('user_nickname', '')}") return message_recv @@ -246,7 +246,7 @@ async def text_to_stream( typing: bool = False, reply_to: str = "", reply_to_message: Optional[Dict[str, Any]] = None, - set_reply: bool = False, + set_reply: bool = True, storage_message: bool = True, ) -> bool: """向指定流发送文本消息 @@ -275,7 +275,7 @@ async def text_to_stream( async def emoji_to_stream( - emoji_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False + emoji_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = True ) -> bool: """向指定流发送表情包 @@ -293,7 +293,7 @@ async def emoji_to_stream( async def image_to_stream( - image_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = False + image_base64: str, stream_id: str, storage_message: bool = True, set_reply: bool = True ) -> bool: """向指定流发送图片 @@ -315,7 +315,7 @@ async def command_to_stream( stream_id: str, storage_message: bool = True, display_message: str = "", - set_reply: bool = False, + set_reply: bool = True, ) -> bool: """向指定流发送命令 @@ -340,7 +340,7 @@ async def custom_to_stream( typing: bool = False, reply_to: str = "", reply_to_message: Optional[Dict[str, Any]] = None, - set_reply: bool = False, + set_reply: bool = True, storage_message: bool = True, show_log: bool = True, ) -> bool: diff --git a/src/plugins/built_in/napcat_adapter_plugin/src/send_handler.py b/src/plugins/built_in/napcat_adapter_plugin/src/send_handler.py index 40e144821..d9eff74d8 100644 --- a/src/plugins/built_in/napcat_adapter_plugin/src/send_handler.py +++ b/src/plugins/built_in/napcat_adapter_plugin/src/send_handler.py @@ -302,7 +302,7 @@ class SendHandler: original_id = id.split("-")[1] msg_info_response = await self.send_message_to_napcat("get_msg", {"message_id": int(original_id)}) else: - msg_info_response = await self.send_message_to_napcat("get_msg", {"message_id": int(id)}) + msg_info_response = await self.send_message_to_napcat("get_msg", {"message_id": id}) replied_user_id = None if msg_info_response and msg_info_response.get("status") == "ok":