From b6d3ed110c214bc655d2e52096b5db49729bb31f Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Wed, 1 Oct 2025 15:44:35 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feat(napcat):=20=E5=A2=9E=E5=BC=BA=E5=8F=91?= =?UTF-8?q?=E9=80=81=E5=A4=84=E7=90=86=E7=A8=8B=E5=BA=8F=E7=9A=84=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E8=AE=B0=E5=BD=95=E5=92=8C=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为 Napcat 适配器中的 `send_handler` 增加了更详细的日志记录,以便更好地跟踪命令的发送和响应过程。同时,改进了表情回应命令的错误处理,以捕获并记录参数缺失或类型错误,提高了调试效率和系统的健壮性。 --- .../built_in/napcat_adapter_plugin/src/send_handler.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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 c8bc267af..53d12578e 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 @@ -156,7 +156,10 @@ class SendHandler: logger.error("命令或参数缺失") return None + logger.info(f"准备向 Napcat 发送命令: command='{command}', args_dict='{args_dict}'") response = await self.send_message_to_napcat(command, args_dict) + logger.info(f"收到 Napcat 的命令响应: {response}") + if response.get("status") == "ok": logger.info(f"命令 {command_name} 执行成功") else: @@ -530,12 +533,14 @@ class SendHandler: Returns: Tuple[CommandType, Dict[str, Any]] """ + logger.info(f"开始处理表情回应命令, 接收到参数: {args}") try: message_id = int(args["message_id"]) emoji_id = int(args["emoji_id"]) set_like = str(args["set"]) - except: - raise ValueError("缺少必需参数: message_id 或 emoji_id") + except (KeyError, ValueError) as e: + logger.error(f"处理表情回应命令时发生错误: {e}, 原始参数: {args}") + raise ValueError(f"缺少必需参数或参数类型错误: {e}") return ( CommandType.SET_EMOJI_LIKE.value, From a14ac31ca09a9d475495b14a292dbb769e02df56 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Wed, 1 Oct 2025 16:20:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?feat(social):=20=E9=99=90=E5=88=B6=E6=88=B3?= =?UTF-8?q?=E4=B8=80=E6=88=B3=E5=8A=A8=E4=BD=9C=E7=9A=84=E6=AC=A1=E6=95=B0?= =?UTF-8?q?=E5=B9=B6=E4=BC=98=E5=8C=96=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为防止滥用和骚扰,将单次"戳一戳"动作的最大次数限制为3次。 同时,更新了动作的使用指南,强调其为强打扰、高消耗行为,并禁止在模糊情境下使用,以引导模型更审慎地调用该功能。 --- src/plugins/built_in/social_toolkit_plugin/plugin.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/plugins/built_in/social_toolkit_plugin/plugin.py b/src/plugins/built_in/social_toolkit_plugin/plugin.py index 7bca6ab08..cb65f4a84 100644 --- a/src/plugins/built_in/social_toolkit_plugin/plugin.py +++ b/src/plugins/built_in/social_toolkit_plugin/plugin.py @@ -157,8 +157,10 @@ class PokeAction(BaseAction): 3. **互动提醒**: 你想以一种有趣的方式提醒或与某人互动,但请确保这是对话的自然延伸,而不是无故打扰。 4. **上下文需求**: 上下文明确需要你戳一个或多个人。 5. **频率限制**: 如果最近已经戳过,或者用户情绪不高,请绝对不要使用。 - - 请回答"是"或"否"。 + 6. **核心原则**: + * 这是一个**强打扰**且**高消耗**的动作。 + * **禁止**在模糊情境下使用。 + 请严格根据上述规则,回答“是”或“否”。 """ associated_types = ["text"] @@ -169,6 +171,8 @@ class PokeAction(BaseAction): try: times = int(self.action_data.get("times", 1)) + if times > 3: + times = 3 except (ValueError, TypeError): times = 1 From b0bd060c20edcf76e0bb993ec9ec9a1836b107c0 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Wed, 1 Oct 2025 16:21:13 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86=E6=BF=80?= =?UTF-8?q?=E6=B4=BB=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/built_in/social_toolkit_plugin/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/plugins/built_in/social_toolkit_plugin/plugin.py b/src/plugins/built_in/social_toolkit_plugin/plugin.py index cb65f4a84..a6d2714d7 100644 --- a/src/plugins/built_in/social_toolkit_plugin/plugin.py +++ b/src/plugins/built_in/social_toolkit_plugin/plugin.py @@ -140,7 +140,7 @@ class PokeAction(BaseAction): # === 基本信息(必须填写)=== action_name = "poke_user" action_description = "向用户发送戳一戳" - activation_type = ActionActivationType.ALWAYS + activation_type = ActionActivationType.LLM_JUDGE parallel_action = True # === 功能描述(必须填写)=== From dad95f08d634d0bebd955b3d0949fc5013537f08 Mon Sep 17 00:00:00 2001 From: minecraft1024a Date: Wed, 1 Oct 2025 17:02:57 +0800 Subject: [PATCH 4/4] =?UTF-8?q?refactor(context):=20=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=85=B4=E8=B6=A3=E5=BA=A6=E8=AE=A1=E7=AE=97=E7=9A=84=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E4=B8=8E=E5=BC=82=E6=AD=A5=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构了 `_calculate_message_interest` 方法,使其能够同时兼容同步和异步调用场景。 通过内部嵌套一个异步函数 `_get_score` 来封装核心的兴趣度计算逻辑,并根据是否存在正在运行的 asyncio 事件循环来决定是直接 `await` 还是使用 `asyncio.run()` 执行。这消除了对独立同步和异步方法的需要,简化了代码结构,并提高了在不同执行上下文中的健壮性。 同时,优化了异常处理和日志记录,对插件加载失败和计算失败提供了更清晰的调试信息。 --- src/chat/message_manager/context_manager.py | 70 +++++++++------------ 1 file changed, 28 insertions(+), 42 deletions(-) diff --git a/src/chat/message_manager/context_manager.py b/src/chat/message_manager/context_manager.py index 707830fef..dd4daa2c3 100644 --- a/src/chat/message_manager/context_manager.py +++ b/src/chat/message_manager/context_manager.py @@ -226,56 +226,42 @@ class SingleStreamContextManager: self.access_count += 1 async def _calculate_message_interest(self, message: DatabaseMessages) -> float: - """异步实现:使用插件的异步评分器正确 await 计算兴趣度并返回分数。""" - try: + """ + 异步计算消息的兴趣度。 + 此方法通过检查当前是否存在正在运行的 asyncio 事件循环来兼容同步和异步调用。 + """ + # 内部异步函数,封装实际的计算逻辑 + async def _get_score(): try: from src.plugins.built_in.affinity_flow_chatter.interest_scoring import ( chatter_interest_scoring_system, ) - try: - interest_score = await chatter_interest_scoring_system._calculate_single_message_score( - message=message, bot_nickname=global_config.bot.nickname - ) - interest_value = interest_score.total_score - logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}") - return interest_value - except Exception as e: - logger.warning(f"插件内部兴趣度计算失败: {e}") - return 0.5 - except Exception as e: - logger.warning(f"插件内部兴趣度计算加载失败,使用默认值: {e}") - return 0.5 - except Exception as e: - logger.error(f"计算消息兴趣度失败: {e}") - return 0.5 - - async def _calculate_message_interest_async(self, message: DatabaseMessages) -> float: - """异步实现:使用插件的异步评分器正确 await 计算兴趣度并返回分数。""" - try: - try: - from src.plugins.built_in.affinity_flow_chatter.interest_scoring import ( - chatter_interest_scoring_system, + interest_score = await chatter_interest_scoring_system._calculate_single_message_score( + message=message, bot_nickname=global_config.bot.nickname ) - - # 直接 await 插件的异步方法 - try: - interest_score = await chatter_interest_scoring_system._calculate_single_message_score( - message=message, bot_nickname=global_config.bot.nickname - ) - interest_value = interest_score.total_score - logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}") - return interest_value - except Exception as e: - logger.warning(f"插件内部兴趣度计算失败: {e}") - return 0.5 - + interest_value = interest_score.total_score + logger.debug(f"使用插件内部系统计算兴趣度: {interest_value:.3f}") + return interest_value + except ImportError as e: + logger.debug(f"兴趣度计算插件加载失败,可能未启用: {e}") + return 0.5 except Exception as e: - logger.warning(f"插件内部兴趣度计算加载失败,使用默认值: {e}") + # 在某些情况下(例如机器人自己的消息),没有兴趣度是正常的 + logger.info(f"插件内部兴趣度计算失败,使用默认值: {e}") return 0.5 - except Exception as e: - logger.error(f"计算消息兴趣度失败: {e}") - return 0.5 + # 检查并获取当前事件循环 + try: + loop = asyncio.get_running_loop() + except RuntimeError: # 'RuntimeError: There is no current event loop...' + loop = None + + if loop and loop.is_running(): + # 如果事件循环正在运行,直接 await + return await _get_score() + else: + # 否则,使用 asyncio.run() 来安全执行 + return asyncio.run(_get_score()) async def add_message_async(self, message: DatabaseMessages, skip_energy_update: bool = False) -> bool: """异步实现的 add_message:将消息添加到 context,并 await 能量更新与分发。"""