From e830923795a7fe6bc66a98c267a1ea3702c733d8 Mon Sep 17 00:00:00 2001 From: tt-P607 <68868379+tt-P607@users.noreply.github.com> Date: Sun, 7 Sep 2025 12:13:00 +0800 Subject: [PATCH] =?UTF-8?q?feat(at=5Fuser=5Fplugin):=20=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E5=9B=9E=E5=A4=8D=E5=99=A8=E7=94=9F=E6=88=90=E6=99=BA=E8=83=BD?= =?UTF-8?q?=E8=89=BE=E7=89=B9=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 艾特用户动作现在通过回复器生成智能回复内容,而不是直接发送固定消息。 这样可以使艾特消息更具上下文感知能力和智能化,提升用户体验。 增加了错误处理机制,确保在回复生成失败或聊天流不存在时能正确处理。 --- src/plugins/built_in/at_user_plugin/plugin.py | 77 ++++++++++++++++--- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/src/plugins/built_in/at_user_plugin/plugin.py b/src/plugins/built_in/at_user_plugin/plugin.py index 4b422a525..1611486be 100644 --- a/src/plugins/built_in/at_user_plugin/plugin.py +++ b/src/plugins/built_in/at_user_plugin/plugin.py @@ -60,19 +60,72 @@ class AtAction(BaseAction): if not user_info or not user_info.get("user_id"): logger.info(f"找不到名为 '{user_name}' 的用户。") return False, "用户不存在" - await self.send_command( - "SEND_AT_MESSAGE", - args={"qq_id": user_info.get("user_id"), "text": at_message}, - display_message=f"艾特用户 {user_name} 并发送消息: {at_message}", - ) - await self.store_action_info( - action_build_into_prompt=True, - action_prompt_display=f"执行了艾特用户动作:艾特用户 {user_name} 并发送消息: {at_message}", - action_done=True, - ) - logger.info("艾特用户的动作已触发,但具体实现待完成。") - return True, "艾特用户的动作已触发,但具体实现待完成。" + try: + # 使用回复器生成艾特回复,而不是直接发送命令 + from src.chat.replyer.default_generator import DefaultReplyer + from src.chat.message_receive.chat_stream import get_chat_manager + + # 获取当前聊天流 + chat_manager = get_chat_manager() + chat_stream = chat_manager.get_stream(self.chat_id) + + if not chat_stream: + logger.error(f"找不到聊天流: {self.stream_id}") + return False, "聊天流不存在" + + # 创建回复器实例 + replyer = DefaultReplyer(chat_stream) + + # 构建回复对象,将艾特消息作为回复目标 + reply_to = f"{user_name}:{at_message}" + extra_info = f"你需要艾特用户 {user_name} 并回复他们说: {at_message}" + + # 使用回复器生成回复 + success, llm_response, prompt = await replyer.generate_reply_with_context( + reply_to=reply_to, + extra_info=extra_info, + enable_tool=False, # 艾特回复通常不需要工具调用 + from_plugin=True # 标识来自插件 + ) + + if success and llm_response: + # 获取生成的回复内容 + reply_content = llm_response.get("content", "") + if reply_content: + # 获取用户QQ号,发送真正的艾特消息 + user_id = user_info.get("user_id") + + # 发送真正的艾特命令,使用回复器生成的智能内容 + await self.send_command( + "SEND_AT_MESSAGE", + args={"qq_id": user_id, "text": reply_content}, + display_message=f"艾特用户 {user_name} 并发送智能回复: {reply_content}", + ) + + await self.store_action_info( + action_build_into_prompt=True, + action_prompt_display=f"执行了艾特用户动作:艾特用户 {user_name} 并发送智能回复: {reply_content}", + action_done=True, + ) + + logger.info(f"成功通过回复器生成智能内容并发送真正的艾特消息给 {user_name}: {reply_content}") + return True, "智能艾特消息发送成功" + else: + logger.warning("回复器生成了空内容") + return False, "回复内容为空" + else: + logger.error("回复器生成回复失败") + return False, "回复生成失败" + + except Exception as e: + logger.error(f"执行艾特用户动作时发生异常: {e}", exc_info=True) + await self.store_action_info( + action_build_into_prompt=True, + action_prompt_display=f"执行艾特用户动作失败:{str(e)}", + action_done=False, + ) + return False, f"执行失败: {str(e)}" class AtCommand(BaseCommand):