From a462cc1d22ea9f1be8986ec3ef261bd065b66893 Mon Sep 17 00:00:00 2001
From: LuiKlee
Date: Thu, 20 Nov 2025 09:49:10 +0800
Subject: [PATCH 01/22] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E6=8A=80=E6=9C=AF?=
=?UTF-8?q?=E7=BE=A4=E7=BE=A4=E5=8F=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Removed link to the technical department QQ group.
---
README.md | 3 ---
1 file changed, 3 deletions(-)
diff --git a/README.md b/README.md
index 196a7f131..0966649d6 100644
--- a/README.md
+++ b/README.md
@@ -25,9 +25,6 @@
-
-
-
---
From a0618fb3c48a30a1ce04fee3300a82f4dd1ad9e2 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Fri, 21 Nov 2025 21:05:02 +0800
Subject: [PATCH 02/22] =?UTF-8?q?feat(plugin=5Fsystem):=20=E5=BC=95?=
=?UTF-8?q?=E5=85=A5=E7=BB=84=E4=BB=B6=E5=B1=80=E9=83=A8=E7=8A=B6=E6=80=81?=
=?UTF-8?q?=E7=AE=A1=E7=90=86=E5=B9=B6=E9=87=8D=E6=9E=84=E6=8F=92=E4=BB=B6?=
=?UTF-8?q?API?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
引入了基于 `stream_id` 的组件局部状态管理机制。这允许在不修改全局配置的情况下,为特定会话临时启用或禁用组件,提供了更高的灵活性。
全面重构了 `plugin_manage_api`,提供了更强大和稳定的插件管理功能:
- 新增 `reload_all_plugins` 和 `get_system_report` API,方便进行批量重载和系统状态诊断。
- 增强了组件卸载逻辑,确保在插件移除时能更彻底地清理资源,特别是对 `EventHandler` 的订阅。
- 重写了内置的 `/system plugin` 命令,以利用新的API,并为相关操作添加了权限控制。
组件注册中心(ComponentRegistry)中的多个 `get_enabled_*` 方法现在可以接受 `stream_id`,以正确反映局部状态。
BREAKING CHANGE: `plugin_manage_api` 中的多个函数已被移除或替换。例如 `list_loaded_plugins` 和 `remove_plugin` 已被移除,加载插件的逻辑已整合到 `register_plugin_from_file` 中。内置的 `/system plugin` 命令的子命令也已更改。
---
src/chat/message_receive/bot.py | 2 +-
src/plugin_system/apis/plugin_manage_api.py | 260 ++++++++++++++----
src/plugin_system/core/component_registry.py | 143 +++++++---
src/plugin_system/core/event_manager.py | 30 ++
.../built_in/system_management/plugin.py | 98 +++----
5 files changed, 390 insertions(+), 143 deletions(-)
diff --git a/src/chat/message_receive/bot.py b/src/chat/message_receive/bot.py
index 6c9b78ba6..aa0ebbe07 100644
--- a/src/chat/message_receive/bot.py
+++ b/src/chat/message_receive/bot.py
@@ -296,7 +296,7 @@ class ChatBot:
response_data = None
if request_id and response_data:
- logger.info(f"[DEBUG bot.py] 收到适配器响应,request_id={request_id}")
+ logger.debug(f"[DEBUG bot.py] 收到适配器响应,request_id={request_id}")
put_adapter_response(request_id, response_data)
else:
logger.warning(f"适配器响应消息格式不正确: request_id={request_id}, response_data={response_data}")
diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py
index d7a802b8c..55d703084 100644
--- a/src/plugin_system/apis/plugin_manage_api.py
+++ b/src/plugin_system/apis/plugin_manage_api.py
@@ -1,117 +1,257 @@
-def list_loaded_plugins() -> list[str]:
+# -*- coding: utf-8 -*-
+import os
+from typing import Any
+
+from src.common.logger import get_logger
+from src.plugin_system.base.component_types import ComponentType
+from src.plugin_system.core.component_registry import ComponentInfo, component_registry
+from src.plugin_system.core.plugin_manager import plugin_manager
+
+logger = get_logger("plugin_manage_api")
+
+
+async def reload_all_plugins() -> bool:
"""
- 列出所有当前加载的插件。
+ 重新加载所有当前已成功加载的插件。
+
+ 此操作会先卸载所有插件,然后重新加载它们。
Returns:
- List[str]: 当前加载的插件名称列表。
+ bool: 如果所有插件都成功重载,则为 True,否则为 False。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
+ logger.info("开始重新加载所有插件...")
+ # 使用 list() 复制一份列表,防止在迭代时修改原始列表
+ loaded_plugins = list(plugin_manager.list_loaded_plugins())
+ all_success = True
- return plugin_manager.list_loaded_plugins()
+ for plugin_name in loaded_plugins:
+ try:
+ success = await reload_plugin(plugin_name)
+ if not success:
+ all_success = False
+ logger.error(f"重载插件 {plugin_name} 失败。")
+ except Exception as e:
+ all_success = False
+ logger.error(f"重载插件 {plugin_name} 时发生异常: {e}", exc_info=True)
+
+ logger.info("所有插件重载完毕。")
+ return all_success
-def list_registered_plugins() -> list[str]:
+async def reload_plugin(name: str) -> bool:
"""
- 列出所有已注册的插件。
-
- Returns:
- List[str]: 已注册的插件名称列表。
- """
- from src.plugin_system.core.plugin_manager import plugin_manager
-
- return plugin_manager.list_registered_plugins()
-
-
-def get_plugin_path(plugin_name: str) -> str:
- """
- 获取指定插件的路径。
+ 重新加载指定的单个插件。
Args:
- plugin_name (str): 插件名称。
+ name (str): 要重载的插件的名称。
Returns:
- str: 插件目录的绝对路径。
+ bool: 成功则为 True。
Raises:
- ValueError: 如果插件不存在。
+ ValueError: 如果插件未找到。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
-
- if plugin_path := plugin_manager.get_plugin_path(plugin_name):
- return plugin_path
- else:
- raise ValueError(f"插件 '{plugin_name}' 不存在。")
+ if name not in plugin_manager.list_registered_plugins():
+ raise ValueError(f"插件 '{name}' 未注册。")
+ return await plugin_manager.reload_registered_plugin(name)
-async def remove_plugin(plugin_name: str) -> bool:
+async def set_component_enabled(name: str, component_type: ComponentType, enabled: bool) -> bool:
"""
- 卸载指定的插件。
+ 全局范围内启用或禁用一个组件。
- **此函数是异步的,确保在异步环境中调用。**
+ 此更改会更新组件注册表中的状态,但不会持久化到文件。
Args:
- plugin_name (str): 要卸载的插件名称。
+ name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+ enabled (bool): True 为启用, False 为禁用。
Returns:
- bool: 卸载是否成功。
+ bool: 操作成功则为 True。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
+ # Chatter 唯一性保护
+ if component_type == ComponentType.CHATTER and not enabled:
+ enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER)
+ if len(enabled_chatters) <= 1 and name in enabled_chatters:
+ logger.warning(f"操作被阻止:不能禁用最后一个启用的 Chatter 组件 ('{name}')。")
+ return False
- return await plugin_manager.remove_registered_plugin(plugin_name)
+ # 注意:这里我们直接修改 ComponentInfo 中的状态
+ component_info = component_registry.get_component_info(name, component_type)
+ if not component_info:
+ logger.error(f"未找到组件 {name} ({component_type.value}),无法更改其状态。")
+ return False
+ component_info.enabled = enabled
+ logger.info(f"组件 {name} ({component_type.value}) 的全局状态已设置为: {enabled}")
+ return True
-async def reload_plugin(plugin_name: str) -> bool:
+def set_component_enabled_local(stream_id: str, name: str, component_type: ComponentType, enabled: bool) -> bool:
"""
- 重新加载指定的插件。
+ 在一个特定的 stream_id 上下文中临时启用或禁用组件。
- **此函数是异步的,确保在异步环境中调用。**
+ 此状态仅存于内存,不影响全局状态。
Args:
- plugin_name (str): 要重新加载的插件名称。
+ stream_id (str): 上下文标识符。
+ name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+ enabled (bool): True 为启用, False 为禁用。
Returns:
- bool: 重新加载是否成功。
+ bool: 操作成功则为 True。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
-
- return await plugin_manager.reload_registered_plugin(plugin_name)
+ component_registry.set_local_component_state(stream_id, name, component_type, enabled)
+ return True
-def load_plugin(plugin_name: str) -> tuple[bool, int]:
+def rescan_and_register_plugins(load_after_register: bool = True) -> tuple[int, int]:
"""
- 加载指定的插件。
+ 重新扫描所有插件目录,发现新插件并注册。
Args:
- plugin_name (str): 要加载的插件名称。
+ load_after_register (bool): 如果为 True,新发现的插件将在注册后立即被加载。
Returns:
- Tuple[bool, int]: 加载是否成功,成功或失败个数。
+ Tuple[int, int]: (成功数量, 失败数量)
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
+ success_count, fail_count = plugin_manager.rescan_plugin_directory()
+ if not load_after_register:
+ return success_count, fail_count
- return plugin_manager.load_registered_plugin_classes(plugin_name)
+ newly_registered = [
+ p for p in plugin_manager.list_registered_plugins() if p not in plugin_manager.list_loaded_plugins()
+ ]
+ loaded_success = 0
+ for plugin_name in newly_registered:
+ status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
+ if status:
+ loaded_success += 1
+
+ return loaded_success, fail_count + (len(newly_registered) - loaded_success)
-def add_plugin_directory(plugin_directory: str) -> bool:
+def register_plugin_from_file(plugin_name: str, load_after_register: bool = True) -> bool:
"""
- 添加插件目录。
+ 从默认插件目录中查找、注册并加载一个插件。
Args:
- plugin_directory (str): 要添加的插件目录路径。
+ plugin_name (str): 插件的名称(即其目录名)。
+ load_after_register (bool): 注册后是否立即加载。
+
Returns:
- bool: 添加是否成功。
+ bool: 成功则为 True。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
+ if plugin_name in plugin_manager.list_loaded_plugins():
+ logger.warning(f"插件 '{plugin_name}' 已经加载。")
+ return True
- return plugin_manager.add_plugin_directory(plugin_directory)
+ # 如果插件未注册,则遍历插件目录去查找
+ if plugin_name not in plugin_manager.list_registered_plugins():
+ logger.info(f"插件 '{plugin_name}' 未注册,开始在插件目录中搜索...")
+ found_path = None
+ for directory in plugin_manager.plugin_directories:
+ potential_path = os.path.join(directory, plugin_name)
+ if os.path.isdir(potential_path):
+ found_path = potential_path
+ break
+
+ if not found_path:
+ logger.error(f"在所有插件目录中都未找到名为 '{plugin_name}' 的插件。")
+ return False
+
+ plugin_file = os.path.join(found_path, "plugin.py")
+ if not os.path.exists(plugin_file):
+ logger.error(f"在 '{found_path}' 中未找到 plugin.py 文件。")
+ return False
+
+ module = plugin_manager._load_plugin_module_file(plugin_file)
+ if not module:
+ logger.error(f"从 '{plugin_file}' 加载插件模块失败。")
+ return False
+
+ if plugin_name not in plugin_manager.list_registered_plugins():
+ logger.error(f"插件 '{plugin_name}' 在加载模块后依然未注册成功。")
+ return False
+
+ logger.info(f"插件 '{plugin_name}' 已成功发现并注册。")
+
+ if load_after_register:
+ status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
+ return status
+ return True
-def rescan_plugin_directory() -> tuple[int, int]:
+def get_component_count(component_type: ComponentType, stream_id: str | None = None) -> int:
"""
- 重新扫描插件目录,加载新插件。
+ 获取指定类型的已加载并启用的组件的总数。
+
+ 可以根据 stream_id 考虑局部状态。
+
+ Args:
+ component_type (ComponentType): 要查询的组件类型。
+ stream_id (str | None): 可选的上下文ID。
+
Returns:
- Tuple[int, int]: 成功加载的插件数量和失败的插件数量。
+ int: 该类型组件的数量。
"""
- from src.plugin_system.core.plugin_manager import plugin_manager
+ return len(component_registry.get_enabled_components_by_type(component_type, stream_id=stream_id))
- return plugin_manager.rescan_plugin_directory()
+
+def get_component_info(name: str, component_type: ComponentType) -> ComponentInfo | None:
+ """
+ 获取任何一个已注册组件的详细信息。
+
+ Args:
+ name (str): 组件的唯一名称。
+ component_type (ComponentType): 组件的类型。
+
+ Returns:
+ ComponentInfo: 包含组件信息的对象,如果找不到则返回 None。
+ """
+ return component_registry.get_component_info(name, component_type)
+
+
+def get_system_report() -> dict[str, Any]:
+ """
+ 生成一份详细的系统状态报告。
+
+ Returns:
+ dict: 包含系统、插件和组件状态的详细报告。
+ """
+ loaded_plugins_info = {}
+ for name, instance in plugin_manager.loaded_plugins.items():
+ plugin_info = component_registry.get_plugin_info(name)
+ if not plugin_info:
+ continue
+
+ components_details = []
+ for comp_info in plugin_info.components:
+ components_details.append(
+ {
+ "name": comp_info.name,
+ "component_type": comp_info.component_type.value,
+ "description": comp_info.description,
+ "enabled": comp_info.enabled,
+ }
+ )
+
+ # 从 plugin_info (PluginInfo) 而不是 instance (PluginBase) 获取元数据
+ loaded_plugins_info[name] = {
+ "display_name": plugin_info.display_name or name,
+ "version": plugin_info.version,
+ "author": plugin_info.author,
+ "enabled": instance.enable_plugin, # enable_plugin 状态还是需要从实例获取
+ "components": components_details,
+ }
+
+ report = {
+ "system_info": {
+ "loaded_plugins_count": len(plugin_manager.loaded_plugins),
+ "total_components_count": component_registry.get_registry_stats().get("total_components", 0),
+ },
+ "plugins": loaded_plugins_info,
+ "failed_plugins": plugin_manager.failed_plugins,
+ }
+ return report
diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py
index ab996fe79..255aef9b6 100644
--- a/src/plugin_system/core/component_registry.py
+++ b/src/plugin_system/core/component_registry.py
@@ -107,6 +107,9 @@ class ComponentRegistry:
"""chatter名 -> chatter类"""
self._enabled_chatter_registry: dict[str, type["BaseChatter"]] = {}
"""启用的chatter名 -> chatter类"""
+ # 局部组件状态管理器,用于临时覆盖
+ self._local_component_states: dict[str, dict[tuple[str, ComponentType], bool]] = {}
+ """stream_id -> {(component_name, component_type): enabled_status}"""
logger.info("组件注册中心初始化完成")
# == 注册方法 ==
@@ -459,20 +462,10 @@ class ComponentRegistry:
case ComponentType.EVENT_HANDLER:
# 移除EventHandler注册和事件订阅
from .event_manager import event_manager # 延迟导入防止循环导入问题
-
- self._event_handler_registry.pop(component_name, None)
- self._enabled_event_handlers.pop(component_name, None)
try:
- handler = event_manager.get_event_handler(component_name)
- # 事件处理器可能未找到或未声明 subscribed_events,需判空
- if handler and hasattr(handler, "subscribed_events"):
- for event in getattr(handler, "subscribed_events"):
- # 假设 unsubscribe_handler_from_event 是协程;若不是则移除 await
- result = event_manager.unsubscribe_handler_from_event(event, component_name)
- if hasattr(result, "__await__"):
- await result # type: ignore[func-returns-value]
- logger.debug(f"已移除EventHandler组件: {component_name}")
- logger.debug(f"已移除EventHandler组件: {component_name}")
+ # 从事件管理器中完全移除事件处理器,包括其所有订阅
+ event_manager.remove_event_handler(component_name)
+ logger.debug(f"已通过 event_manager 移除EventHandler组件: {component_name}")
except Exception as e:
logger.warning(f"移除EventHandler事件订阅时出错: {e}")
@@ -480,10 +473,32 @@ class ComponentRegistry:
# 移除Chatter注册
if hasattr(self, "_chatter_registry"):
self._chatter_registry.pop(component_name, None)
+ if hasattr(self, "_enabled_chatter_registry"):
+ self._enabled_chatter_registry.pop(component_name, None)
logger.debug(f"已移除Chatter组件: {component_name}")
+ case ComponentType.INTEREST_CALCULATOR:
+ # 移除InterestCalculator注册
+ if hasattr(self, "_interest_calculator_registry"):
+ self._interest_calculator_registry.pop(component_name, None)
+ if hasattr(self, "_enabled_interest_calculator_registry"):
+ self._enabled_interest_calculator_registry.pop(component_name, None)
+ logger.debug(f"已移除InterestCalculator组件: {component_name}")
+
+ case ComponentType.PROMPT:
+ # 移除Prompt注册
+ if hasattr(self, "_prompt_registry"):
+ self._prompt_registry.pop(component_name, None)
+ if hasattr(self, "_enabled_prompt_registry"):
+ self._enabled_prompt_registry.pop(component_name, None)
+ logger.debug(f"已移除Prompt组件: {component_name}")
+
+ case ComponentType.ROUTER:
+ # Router组件的移除比较复杂,目前只记录日志
+ logger.warning(f"Router组件 '{component_name}' 的HTTP端点无法在运行时动态移除,将在下次重启后生效。")
+
case _:
- logger.warning(f"未知的组件类型: {component_type}")
+ logger.warning(f"未知的组件类型: {component_type},无法进行特定的清理操作")
return False
# 移除通用注册信息
@@ -724,10 +739,16 @@ class ComponentRegistry:
"""获取指定类型的所有组件"""
return self._components_by_type.get(component_type, {}).copy()
- def get_enabled_components_by_type(self, component_type: ComponentType) -> dict[str, ComponentInfo]:
- """获取指定类型的所有启用组件"""
+ def get_enabled_components_by_type(
+ self, component_type: ComponentType, stream_id: str | None = None
+ ) -> dict[str, ComponentInfo]:
+ """获取指定类型的所有启用组件, 可选地根据 stream_id 考虑局部状态"""
components = self.get_components_by_type(component_type)
- return {name: info for name, info in components.items() if info.enabled}
+ return {
+ name: info
+ for name, info in components.items()
+ if self.is_component_available(name, component_type, stream_id)
+ }
# === Action特定查询方法 ===
@@ -740,9 +761,15 @@ class ComponentRegistry:
info = self.get_component_info(action_name, ComponentType.ACTION)
return info if isinstance(info, ActionInfo) else None
- def get_default_actions(self) -> dict[str, ActionInfo]:
- """获取默认动作集"""
- return self._default_actions.copy()
+ def get_default_actions(self, stream_id: str | None = None) -> dict[str, ActionInfo]:
+ """获取默认(可用)动作集, 可选地根据 stream_id 考虑局部状态"""
+ all_actions = self.get_components_by_type(ComponentType.ACTION)
+ available_actions = {
+ name: info
+ for name, info in all_actions.items()
+ if self.is_component_available(name, ComponentType.ACTION, stream_id)
+ }
+ return cast(dict[str, ActionInfo], available_actions)
# === Command特定查询方法 ===
@@ -790,9 +817,14 @@ class ComponentRegistry:
"""获取Tool注册表"""
return self._tool_registry.copy()
- def get_llm_available_tools(self) -> dict[str, type[BaseTool]]:
- """获取LLM可用的Tool列表"""
- return self._llm_available_tools.copy()
+ def get_llm_available_tools(self, stream_id: str | None = None) -> dict[str, type[BaseTool]]:
+ """获取LLM可用的Tool列表, 可选地根据 stream_id 考虑局部状态"""
+ all_tools = self.get_tool_registry()
+ available_tools = {}
+ for name, tool_class in all_tools.items():
+ if self.is_component_available(name, ComponentType.TOOL, stream_id):
+ available_tools[name] = tool_class
+ return available_tools
def get_registered_tool_info(self, tool_name: str) -> ToolInfo | None:
"""获取Tool信息
@@ -836,9 +868,14 @@ class ComponentRegistry:
info = self.get_component_info(handler_name, ComponentType.EVENT_HANDLER)
return info if isinstance(info, EventHandlerInfo) else None
- def get_enabled_event_handlers(self) -> dict[str, type[BaseEventHandler]]:
- """获取启用的事件处理器"""
- return self._enabled_event_handlers.copy()
+ def get_enabled_event_handlers(self, stream_id: str | None = None) -> dict[str, type[BaseEventHandler]]:
+ """获取启用的事件处理器, 可选地根据 stream_id 考虑局部状态"""
+ all_handlers = self.get_event_handler_registry()
+ available_handlers = {}
+ for name, handler_class in all_handlers.items():
+ if self.is_component_available(name, ComponentType.EVENT_HANDLER, stream_id):
+ available_handlers[name] = handler_class
+ return available_handlers
# === Chatter 特定查询方法 ===
def get_chatter_registry(self) -> dict[str, type[BaseChatter]]:
@@ -847,11 +884,14 @@ class ComponentRegistry:
self._chatter_registry: dict[str, type[BaseChatter]] = {}
return self._chatter_registry.copy()
- def get_enabled_chatter_registry(self) -> dict[str, type[BaseChatter]]:
- """获取启用的Chatter注册表"""
- if not hasattr(self, "_enabled_chatter_registry"):
- self._enabled_chatter_registry: dict[str, type[BaseChatter]] = {}
- return self._enabled_chatter_registry.copy()
+ def get_enabled_chatter_registry(self, stream_id: str | None = None) -> dict[str, type[BaseChatter]]:
+ """获取启用的Chatter注册表, 可选地根据 stream_id 考虑局部状态"""
+ all_chatters = self.get_chatter_registry()
+ available_chatters = {}
+ for name, chatter_class in all_chatters.items():
+ if self.is_component_available(name, ComponentType.CHATTER, stream_id):
+ available_chatters[name] = chatter_class
+ return available_chatters
def get_registered_chatter_info(self, chatter_name: str) -> ChatterInfo | None:
"""获取Chatter信息"""
@@ -875,8 +915,12 @@ class ComponentRegistry:
def get_plugin_components(self, plugin_name: str) -> list["ComponentInfo"]:
"""获取插件的所有组件"""
plugin_info = self.get_plugin_info(plugin_name)
- logger.info(plugin_info.components)
- return plugin_info.components if plugin_info else []
+ if plugin_info:
+ # 记录日志时,将组件列表转换为可读的字符串,避免类型错误
+ component_names = [c.name for c in plugin_info.components]
+ logger.debug(f"获取到插件 '{plugin_name}' 的组件: {component_names}")
+ return plugin_info.components
+ return []
def get_plugin_config(self, plugin_name: str) -> dict:
"""获取插件配置
@@ -952,9 +996,40 @@ class ComponentRegistry:
component_type.value: len(components) for component_type, components in self._components_by_type.items()
},
"enabled_components": len([c for c in self._components.values() if c.enabled]),
- "enabled_plugins": len([p for p in self._plugins.values() if p.enabled]),
}
+ # === 局部状态管理 ===
+ def set_local_component_state(
+ self, stream_id: str, component_name: str, component_type: ComponentType, enabled: bool
+ ) -> bool:
+ """为指定的 stream_id 设置组件的局部(临时)状态"""
+ if stream_id not in self._local_component_states:
+ self._local_component_states[stream_id] = {}
+
+ state_key = (component_name, component_type)
+ self._local_component_states[stream_id][state_key] = enabled
+ logger.debug(f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type}) -> {'启用' if enabled else '禁用'}")
+ return True
+
+ def is_component_available(self, component_name: str, component_type: ComponentType, stream_id: str | None = None) -> bool:
+ """检查组件在给定上下文中是否可用(同时考虑全局和局部状态)"""
+ component_info = self.get_component_info(component_name, component_type)
+
+ # 1. 检查组件是否存在
+ if not component_info:
+ return False
+
+ # 2. 如果提供了 stream_id,检查局部状态
+ if stream_id and stream_id in self._local_component_states:
+ state_key = (component_name, component_type)
+ local_state = self._local_component_states[stream_id].get(state_key)
+
+ if local_state is not None:
+ return local_state # 局部状态存在,覆盖全局状态
+
+ # 3. 如果没有局部状态覆盖,则返回全局状态
+ return component_info.enabled
+
# === MCP 工具相关方法 ===
async def load_mcp_tools(self) -> None:
diff --git a/src/plugin_system/core/event_manager.py b/src/plugin_system/core/event_manager.py
index cdb3fdb19..b2d08174b 100644
--- a/src/plugin_system/core/event_manager.py
+++ b/src/plugin_system/core/event_manager.py
@@ -220,6 +220,36 @@ class EventManager:
"""
return self._event_handlers.copy()
+ def remove_event_handler(self, handler_name: str) -> bool:
+ """
+ 完全移除一个事件处理器,包括其所有订阅。
+
+ Args:
+ handler_name (str): 要移除的事件处理器的名称。
+
+ Returns:
+ bool: 如果成功移除则返回 True,否则返回 False。
+ """
+ if handler_name not in self._event_handlers:
+ logger.warning(f"事件处理器 {handler_name} 未注册,无需移除。")
+ return False
+
+ # 从主注册表中删除
+ del self._event_handlers[handler_name]
+ logger.debug(f"事件处理器 {handler_name} 已从主注册表移除。")
+
+ # 遍历所有事件,取消其订阅
+ for event in self._events.values():
+ # 创建订阅者列表的副本进行迭代,以安全地修改原始列表
+ for subscriber in list(event.subscribers):
+ if getattr(subscriber, 'handler_name', None) == handler_name:
+ event.subscribers.remove(subscriber)
+ logger.debug(f"事件处理器 {handler_name} 已从事件 {event.name} 取消订阅。")
+
+ logger.info(f"事件处理器 {handler_name} 已被完全移除。")
+ return True
+
+
def subscribe_handler_to_event(self, handler_name: str, event_name: EventType | str) -> bool:
"""订阅事件处理器到指定事件
diff --git a/src/plugins/built_in/system_management/plugin.py b/src/plugins/built_in/system_management/plugin.py
index d3f9ed83e..e06b329a9 100644
--- a/src/plugins/built_in/system_management/plugin.py
+++ b/src/plugins/built_in/system_management/plugin.py
@@ -96,16 +96,13 @@ class SystemCommand(PlusCommand):
help_text = """🔌 插件管理命令帮助
📋 基本操作:
• `/system plugin help` - 显示插件管理帮助
-• `/system plugin list` - 列出所有注册的插件
-• `/system plugin list_enabled` - 列出所有加载(启用)的插件
+• `/system plugin report` - 查看系统插件报告
• `/system plugin rescan` - 重新扫描所有插件目录
⚙️ 插件控制:
• `/system plugin load <插件名>` - 加载指定插件
-• `/system plugin unload <插件名>` - 卸载指定插件
• `/system plugin reload <插件名>` - 重新加载指定插件
-• `/system plugin force_reload <插件名>` - 强制重载指定插件
-• `/system plugin add_dir <目录路径>` - 添加插件目录
+• `/system plugin reload_all` - 重新加载所有插件
"""
elif target == "permission":
help_text = """📋 权限管理命令帮助
@@ -150,20 +147,16 @@ class SystemCommand(PlusCommand):
if action in ["help", "帮助"]:
await self._show_help("plugin")
- elif action in ["list", "列表"]:
- await self._list_registered_plugins()
- elif action in ["list_enabled", "已启用"]:
- await self._list_loaded_plugins()
+ elif action in ["report", "报告"]:
+ await self._show_system_report()
elif action in ["rescan", "重扫"]:
await self._rescan_plugin_dirs()
elif action in ["load", "加载"] and len(remaining_args) > 0:
await self._load_plugin(remaining_args[0])
- elif action in ["unload", "卸载"] and len(remaining_args) > 0:
- await self._unload_plugin(remaining_args[0])
elif action in ["reload", "重载"] and len(remaining_args) > 0:
await self._reload_plugin(remaining_args[0])
- elif action in ["force_reload", "强制重载"] and len(remaining_args) > 0:
- await self._force_reload_plugin(remaining_args[0])
+ elif action in ["reload_all", "重载全部"]:
+ await self._reload_all_plugins()
else:
await self.send_text("❌ 插件管理命令不合法\n使用 /system plugin help 查看帮助")
@@ -429,61 +422,70 @@ class SystemCommand(PlusCommand):
# Permission Management Section
# =================================================================
- async def _list_loaded_plugins(self):
- """列出已加载的插件"""
- plugins = plugin_manage_api.list_loaded_plugins()
- await self.send_text(f"📦 已加载的插件: {', '.join(plugins) if plugins else '无'}")
+ @require_permission("plugin.manage", deny_message="❌ 你没有权限查看插件报告")
+ async def _show_system_report(self):
+ """显示系统插件报告"""
+ report = plugin_manage_api.get_system_report()
+
+ response_parts = [
+ "📊 **系统插件报告**",
+ f" - 已加载插件: {report['system_info']['loaded_plugins_count']}",
+ f" - 组件总数: {report['system_info']['total_components_count']}",
+ ]
- async def _list_registered_plugins(self):
- """列出已注册的插件"""
- plugins = plugin_manage_api.list_registered_plugins()
- await self.send_text(f"📋 已注册的插件: {', '.join(plugins) if plugins else '无'}")
+ if report["plugins"]:
+ response_parts.append("\n✅ **已加载插件:**")
+ for name, info in report["plugins"].items():
+ response_parts.append(f" • **{info['display_name']} (`{name}`)** v{info['version']} by {info['author']}")
+
+ if report["failed_plugins"]:
+ response_parts.append("\n❌ **加载失败的插件:**")
+ for name, error in report["failed_plugins"].items():
+ response_parts.append(f" • **`{name}`**: {error}")
+
+ await self._send_long_message("\n".join(response_parts))
+
+ @require_permission("plugin.manage", deny_message="❌ 你没有权限扫描插件")
async def _rescan_plugin_dirs(self):
"""重新扫描插件目录"""
- plugin_manage_api.rescan_plugin_directory()
- await self.send_text("🔄 插件目录重新扫描已启动")
+ await self.send_text("🔄 正在重新扫描插件目录...")
+ success, fail = plugin_manage_api.rescan_and_register_plugins(load_after_register=True)
+ await self.send_text(f"✅ 扫描完成!\n新增成功: {success}个, 新增失败: {fail}个。")
+ @require_permission("plugin.manage", deny_message="❌ 你没有权限加载插件")
async def _load_plugin(self, plugin_name: str):
"""加载指定插件"""
- success, count = plugin_manage_api.load_plugin(plugin_name)
+ success = plugin_manage_api.register_plugin_from_file(plugin_name, load_after_register=True)
if success:
await self.send_text(f"✅ 插件加载成功: `{plugin_name}`")
else:
- if count == 0:
- await self.send_text(f"⚠️ 插件 `{plugin_name}` 为禁用状态")
- else:
- await self.send_text(f"❌ 插件加载失败: `{plugin_name}`")
+ await self.send_text(f"❌ 插件加载失败: `{plugin_name}`。请检查日志获取详细信息。")
- async def _unload_plugin(self, plugin_name: str):
- """卸载指定插件"""
- success = await plugin_manage_api.remove_plugin(plugin_name)
- if success:
- await self.send_text(f"✅ 插件卸载成功: `{plugin_name}`")
- else:
- await self.send_text(f"❌ 插件卸载失败: `{plugin_name}`")
+ @require_permission("plugin.manage", deny_message="❌ 你没有权限重载插件")
async def _reload_plugin(self, plugin_name: str):
"""重新加载指定插件"""
- success = await plugin_manage_api.reload_plugin(plugin_name)
- if success:
- await self.send_text(f"✅ 插件重新加载成功: `{plugin_name}`")
- else:
- await self.send_text(f"❌ 插件重新加载失败: `{plugin_name}`")
-
- async def _force_reload_plugin(self, plugin_name: str):
- """强制重载指定插件(深度清理)"""
- await self.send_text(f"🔄 开始强制重载插件: `{plugin_name}`... (注意: 实际执行reload)")
try:
success = await plugin_manage_api.reload_plugin(plugin_name)
if success:
- await self.send_text(f"✅ 插件重载成功: `{plugin_name}`")
+ await self.send_text(f"✅ 插件重新加载成功: `{plugin_name}`")
else:
- await self.send_text(f"❌ 插件重载失败: `{plugin_name}`")
- except Exception as e:
- await self.send_text(f"❌ 重载过程中发生错误: {e!s}")
+ await self.send_text(f"❌ 插件重新加载失败: `{plugin_name}`")
+ except ValueError as e:
+ await self.send_text(f"❌ 操作失败: {e}")
+ @require_permission("plugin.manage", deny_message="❌ 你没有权限重载所有插件")
+ async def _reload_all_plugins(self):
+ """重新加载所有插件"""
+ await self.send_text("🔄 正在重新加载所有插件...")
+ success = await plugin_manage_api.reload_all_plugins()
+ if success:
+ await self.send_text("✅ 所有插件已成功重载。")
+ else:
+ await self.send_text("⚠️ 部分插件重载失败,请检查日志。")
+
# =================================================================
# Permission Management Section
From 695a6b73191d23adeca9db1aff76b5b1920069c7 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Fri, 21 Nov 2025 21:21:21 +0800
Subject: [PATCH 03/22] =?UTF-8?q?feat(plugin=5Fsystem):=20=E5=A2=9E?=
=?UTF-8?q?=E5=8A=A0=E5=AF=B9=E6=9C=80=E5=90=8E=E4=B8=80=E4=B8=AA=E5=90=AF?=
=?UTF-8?q?=E7=94=A8=20Chatter=20=E7=9A=84=E7=A6=81=E7=94=A8=E4=BF=9D?=
=?UTF-8?q?=E6=8A=A4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
为了确保每个 stream 至少有一个可用的 Chatter 组件,此更改引入了一项保护机制。
在尝试禁用一个 Chatter 组件时,系统现在会检查它是否是当前 stream 中唯一启用的 Chatter。如果是,则禁用操作将被阻止,以避免导致该 stream 无法响应。
---
src/plugin_system/apis/plugin_manage_api.py | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py
index 55d703084..fa9c4e20a 100644
--- a/src/plugin_system/apis/plugin_manage_api.py
+++ b/src/plugin_system/apis/plugin_manage_api.py
@@ -102,6 +102,18 @@ def set_component_enabled_local(stream_id: str, name: str, component_type: Compo
Returns:
bool: 操作成功则为 True。
"""
+ # Chatter 唯一性保护
+ if component_type == ComponentType.CHATTER and not enabled:
+ # 检查当前 stream_id 上下文中的启用状态
+ enabled_chatters = component_registry.get_enabled_components_by_type(
+ ComponentType.CHATTER, stream_id=stream_id
+ )
+ if len(enabled_chatters) <= 1 and name in enabled_chatters:
+ logger.warning(
+ f"操作被阻止:在 stream '{stream_id}' 中,不能禁用最后一个启用的 Chatter 组件 ('{name}')。"
+ )
+ return False
+
component_registry.set_local_component_state(stream_id, name, component_type, enabled)
return True
From 515d6ee62b765221c42a63d05de3ce40dd2bb081 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Fri, 21 Nov 2025 21:27:13 +0800
Subject: [PATCH 04/22] =?UTF-8?q?refactor(maizone):=20=E7=A7=BB=E9=99=A4?=
=?UTF-8?q?=E8=B0=83=E5=BA=A6=E5=99=A8=E4=B8=AD=E7=9A=84=E9=9A=8F=E6=9C=BA?=
=?UTF-8?q?=E4=B8=BB=E9=A2=98=E7=94=9F=E6=88=90=E9=80=BB=E8=BE=91?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
移除 `SchedulerService` 中直接调用LLM生成随机说说主题的功能。
现在,当没有日程安排时,调度器会发送一个通用的指令(“随意发挥”),将具体内容的生成职责完全委托给 `QzoneService`。
这一改动简化了调度器的职责,使其专注于任务调度,提高了模块的内聚性。
---
.../services/scheduler_service.py | 41 +------------------
1 file changed, 2 insertions(+), 39 deletions(-)
diff --git a/src/plugins/built_in/maizone_refactored/services/scheduler_service.py b/src/plugins/built_in/maizone_refactored/services/scheduler_service.py
index d5437c0fa..ac994c224 100644
--- a/src/plugins/built_in/maizone_refactored/services/scheduler_service.py
+++ b/src/plugins/built_in/maizone_refactored/services/scheduler_service.py
@@ -63,35 +63,6 @@ class SchedulerService:
pass # 任务取消是正常操作
logger.info("基于日程表的说说定时发送任务已停止。")
- async def _generate_random_topic(self) -> str | None:
- """
- 使用小模型生成一个随机的说说主题。
- """
- try:
- logger.info("尝试生成随机说说主题...")
- prompt = "请生成一个有趣、简短、积极向上的日常一句话,适合作为社交媒体的动态内容,例如关于天气、心情、动漫、游戏或者某个小发现。请直接返回这句话,不要包含任何多余的解释或标签。"
-
- task_config = global_model_config.model_task_config.get_task("utils_small")
- if not task_config:
- logger.error("未找到名为 'utils_small' 的模型任务配置。")
- return None
-
- success, content, _, _ = await llm_api.generate_with_model(
- model_config=task_config,
- prompt=prompt,
- max_tokens=150,
- temperature=0.9,
- )
-
- if success and content and content.strip():
- logger.info(f"成功生成随机主题: {content.strip()}")
- return content.strip()
- logger.warning("LLM未能生成有效的主题。")
- return None
- except Exception as e:
- logger.error(f"生成随机主题时发生错误: {e}")
- return None
-
async def _schedule_loop(self):
"""
定时任务的核心循环。
@@ -140,21 +111,13 @@ class SchedulerService:
activity_placeholder = "No Schedule - Random"
if not await self._is_processed(hour_str, activity_placeholder):
logger.info("没有日程活动,但开启了无日程发送功能,准备生成随机主题。")
- topic = await self._generate_random_topic()
- if topic:
- result = await self.qzone_service.send_feed(topic=topic, stream_id=None)
- await self._mark_as_processed(
+ result = await self.qzone_service.send_feed(topic="随意发挥",stream_id=None)
+ await self._mark_as_processed(
hour_str,
activity_placeholder,
result.get("success", False),
result.get("message", ""),
)
- else:
- logger.error("未能生成随机主题,本次不发送。")
- # 即使生成失败,也标记为已处理,防止本小时内反复尝试
- await self._mark_as_processed(
- hour_str, activity_placeholder, False, "Failed to generate topic"
- )
else:
logger.info(f"当前小时 {hour_str} 已执行过无日程发送任务,本次跳过。")
From 3e927f45bbd7e383781e75288c625342d142332f Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Fri, 21 Nov 2025 21:28:57 +0800
Subject: [PATCH 05/22] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E5=90=AF?=
=?UTF-8?q?=E5=8A=A8=E4=BF=A1=E6=81=AF=E4=B8=AD=E7=9A=84=E9=A1=B9=E7=9B=AE?=
=?UTF-8?q?=E5=9C=B0=E5=9D=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/main.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/main.py b/src/main.py
index 143feae51..1e851f04d 100644
--- a/src/main.py
+++ b/src/main.py
@@ -393,7 +393,7 @@ class MainSystem:
MoFox_Bot(第三方修改版)
全部组件已成功启动!
=========================================================
-🌐 项目地址: https://github.com/MoFox-Studio/MoFox_Bot
+🌐 项目地址: https://github.com/MoFox-Studio/MoFox-Core
🏠 官方项目: https://github.com/MaiM-with-u/MaiBot
=========================================================
这是基于原版MMC的社区改版,包含增强功能和优化(同时也有更多的'特性')
From affd70b16543e2f9bfcc6ea85ff7dec60f79034a Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 09:51:31 +0800
Subject: [PATCH 06/22] =?UTF-8?q?refactor(chat):=20=E7=AE=80=E5=8C=96Actio?=
=?UTF-8?q?n=E5=92=8CPlusCommand=E7=9A=84=E8=B0=83=E7=94=A8=E9=A2=84?=
=?UTF-8?q?=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
移除 `ChatBot` 和 `ActionModifier` 中用于过滤禁用组件的模板代码。
这两个模块现在直接从 `ComponentRegistry` 获取为当前聊天会话(`stream_id`)定制的可用组件列表。所有关于组件是否启用的判断逻辑都已下沉到 `plugin_system` 核心中,使得上层调用代码更清晰,且不再需要依赖 `global_announcement_manager` 来进行手动过滤。
---
src/chat/message_receive/bot.py | 28 ++++++--------------
src/chat/planner_actions/action_manager.py | 21 +++++++++------
src/chat/planner_actions/action_modifier.py | 22 +++++++--------
src/plugin_system/apis/tool_api.py | 4 +--
src/plugin_system/core/component_registry.py | 17 ++++++++++++
src/plugin_system/core/tool_use.py | 5 ++--
6 files changed, 53 insertions(+), 44 deletions(-)
diff --git a/src/chat/message_receive/bot.py b/src/chat/message_receive/bot.py
index aa0ebbe07..5e2ccb5e3 100644
--- a/src/chat/message_receive/bot.py
+++ b/src/chat/message_receive/bot.py
@@ -14,7 +14,7 @@ from src.common.data_models.database_data_model import DatabaseMessages
from src.common.logger import get_logger
from src.config.config import global_config
from src.mood.mood_manager import mood_manager # 导入情绪管理器
-from src.plugin_system.base import BaseCommand, EventType
+from src.plugin_system.base import BaseCommand, EventType, ComponentType
from src.plugin_system.core import component_registry, event_manager, global_announcement_manager
# 获取项目根目录(假设本文件在src/chat/message_receive/下,根目录为上上上级目录)
@@ -118,20 +118,18 @@ class ChatBot:
args_text = parts[1] if len(parts) > 1 else ""
# 查找匹配的PlusCommand
- plus_command_registry = component_registry.get_plus_command_registry()
+ available_commands_info = component_registry.get_available_plus_commands_info(chat.stream_id)
matching_commands = []
- for plus_command_name, plus_command_class in plus_command_registry.items():
- plus_command_info = component_registry.get_registered_plus_command_info(plus_command_name)
- if not plus_command_info:
- continue
-
+ for plus_command_name, plus_command_info in available_commands_info.items():
# 检查命令名是否匹配(命令名和别名)
- all_commands = [plus_command_name.lower()] + [
+ all_aliases = [plus_command_name.lower()] + [
alias.lower() for alias in plus_command_info.command_aliases
]
- if command_word in all_commands:
- matching_commands.append((plus_command_class, plus_command_info, plus_command_name))
+ if command_word in all_aliases:
+ plus_command_class = component_registry.get_component_class(plus_command_name, ComponentType.PLUS_COMMAND)
+ if plus_command_class:
+ matching_commands.append((plus_command_class, plus_command_info, plus_command_name))
if not matching_commands:
return False, None, True # 没有找到匹配的PlusCommand,继续处理
@@ -145,16 +143,6 @@ class ChatBot:
plus_command_class, plus_command_info, plus_command_name = matching_commands[0]
- # 检查命令是否被禁用
- if (
- chat
- and chat.stream_id
- and plus_command_name
- in global_announcement_manager.get_disabled_chat_commands(chat.stream_id)
- ):
- logger.info("用户禁用的PlusCommand,跳过处理")
- return False, None, True
-
message.is_command = True
# 获取插件配置
diff --git a/src/chat/planner_actions/action_manager.py b/src/chat/planner_actions/action_manager.py
index a0e72ed73..d2ffc9186 100644
--- a/src/chat/planner_actions/action_manager.py
+++ b/src/chat/planner_actions/action_manager.py
@@ -27,11 +27,9 @@ class ChatterActionManager:
def __init__(self):
"""初始化动作管理器"""
- # 当前正在使用的动作集合,默认加载默认动作
+ # 当前正在使用的动作集合,在规划开始时加载
self._using_actions: dict[str, ActionInfo] = {}
-
- # 初始化时将默认动作加载到使用中的动作
- self._using_actions = component_registry.get_default_actions()
+ self.chat_id: str | None = None
self.log_prefix: str = "ChatterActionManager"
# 批量存储支持
@@ -39,6 +37,12 @@ class ChatterActionManager:
self._pending_actions = []
self._current_chat_id = None
+ async def load_actions(self, stream_id: str | None):
+ """根据 stream_id 加载当前可用的动作"""
+ self.chat_id = stream_id
+ self._using_actions = component_registry.get_default_actions(stream_id)
+ logger.debug(f"已为 stream '{stream_id}' 加载 {len(self._using_actions)} 个可用动作: {list(self._using_actions.keys())}")
+
# === 执行Action方法 ===
@staticmethod
@@ -133,11 +137,12 @@ class ChatterActionManager:
logger.debug(f"已从使用集中移除动作 {action_name}")
return True
- def restore_actions(self) -> None:
- """恢复到默认动作集"""
+ async def restore_actions(self) -> None:
+ """恢复到当前 stream_id 的默认动作集"""
actions_to_restore = list(self._using_actions.keys())
- self._using_actions = component_registry.get_default_actions()
- logger.debug(f"恢复动作集: 从 {actions_to_restore} 恢复到默认动作集 {list(self._using_actions.keys())}")
+ # 使用 self.chat_id 来恢复当前上下文的动作
+ await self.load_actions(self.chat_id)
+ logger.debug(f"恢复动作集: 从 {actions_to_restore} 恢复到 stream '{self.chat_id}' 的默认动作集 {list(self._using_actions.keys())}")
async def execute_action(
self,
diff --git a/src/chat/planner_actions/action_modifier.py b/src/chat/planner_actions/action_modifier.py
index 4815d9c38..2fe0ac745 100644
--- a/src/chat/planner_actions/action_modifier.py
+++ b/src/chat/planner_actions/action_modifier.py
@@ -11,7 +11,7 @@ from src.common.logger import get_logger
from src.config.config import global_config, model_config
from src.llm_models.utils_model import LLMRequest
from src.plugin_system.base.component_types import ActionInfo
-from src.plugin_system.core.global_announcement_manager import global_announcement_manager
+
if TYPE_CHECKING:
from src.common.data_models.message_manager_data_model import StreamContext
@@ -68,6 +68,16 @@ class ActionModifier:
"""
# 初始化log_prefix
await self._initialize_log_prefix()
+ # 根据 stream_id 加载当前可用的动作
+ await self.action_manager.load_actions(self.chat_id)
+ from src.plugin_system.base.component_types import ComponentType
+ from src.plugin_system.core.component_registry import component_registry
+ # 计算并记录禁用的动作数量
+ all_registered_actions = component_registry.get_components_by_type(ComponentType.ACTION)
+ loaded_actions_count = len(self.action_manager.get_using_actions())
+ disabled_actions_count = len(all_registered_actions) - loaded_actions_count
+ if disabled_actions_count > 0:
+ logger.info(f"{self.log_prefix} 用户禁用了 {disabled_actions_count} 个动作。")
logger.debug(f"{self.log_prefix}开始完整动作修改流程")
@@ -75,7 +85,6 @@ class ActionModifier:
removals_s2: list[tuple[str, str]] = []
removals_s3: list[tuple[str, str]] = []
- self.action_manager.restore_actions()
all_actions = self.action_manager.get_using_actions()
# === 第0阶段:根据聊天类型过滤动作 ===
@@ -126,15 +135,6 @@ class ActionModifier:
if message_content:
chat_content = chat_content + "\n" + f"现在,最新的消息是:{message_content}"
- # === 第一阶段:去除用户自行禁用的 ===
- disabled_actions = global_announcement_manager.get_disabled_chat_actions(self.chat_id)
- if disabled_actions:
- for disabled_action_name in disabled_actions:
- if disabled_action_name in all_actions:
- removals_s1.append((disabled_action_name, "用户自行禁用"))
- self.action_manager.remove_action_from_using(disabled_action_name)
- logger.debug(f"{self.log_prefix}阶段一移除动作: {disabled_action_name},原因: 用户自行禁用")
-
# === 第二阶段:检查动作的关联类型 ===
if not self.chat_stream:
logger.error(f"{self.log_prefix} chat_stream 未初始化,无法执行第二阶段")
diff --git a/src/plugin_system/apis/tool_api.py b/src/plugin_system/apis/tool_api.py
index e59e3dd99..d5a16c326 100644
--- a/src/plugin_system/apis/tool_api.py
+++ b/src/plugin_system/apis/tool_api.py
@@ -30,7 +30,7 @@ def get_tool_instance(tool_name: str, chat_stream: Any = None) -> BaseTool | Non
return tool_class(plugin_config, chat_stream) if tool_class else None
-def get_llm_available_tool_definitions() -> list[dict[str, Any]]:
+def get_llm_available_tool_definitions(stream_id : str | None) -> list[dict[str, Any]]:
"""获取LLM可用的工具定义列表(包括 MCP 工具)
Returns:
@@ -38,7 +38,7 @@ def get_llm_available_tool_definitions() -> list[dict[str, Any]]:
"""
from src.plugin_system.core import component_registry
- llm_available_tools = component_registry.get_llm_available_tools()
+ llm_available_tools = component_registry.get_llm_available_tools(stream_id)
tool_definitions = []
# 获取常规工具定义
diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py
index 255aef9b6..cda73fe22 100644
--- a/src/plugin_system/core/component_registry.py
+++ b/src/plugin_system/core/component_registry.py
@@ -857,6 +857,23 @@ class ComponentRegistry:
info = self.get_component_info(command_name, ComponentType.PLUS_COMMAND)
return info if isinstance(info, PlusCommandInfo) else None
+ def get_available_plus_commands_info(self, stream_id: str | None = None) -> dict[str, PlusCommandInfo]:
+ """获取在指定上下文中所有可用的PlusCommand信息
+
+ Args:
+ stream_id: 可选的流ID,用于检查局部组件状态
+
+ Returns:
+ 一个字典,键是命令名,值是 PlusCommandInfo 对象
+ """
+ all_plus_commands = self.get_components_by_type(ComponentType.PLUS_COMMAND)
+ available_commands = {
+ name: info
+ for name, info in all_plus_commands.items()
+ if self.is_component_available(name, ComponentType.PLUS_COMMAND, stream_id)
+ }
+ return cast(dict[str, PlusCommandInfo], available_commands)
+
# === EventHandler 特定查询方法 ===
def get_event_handler_registry(self) -> dict[str, type[BaseEventHandler]]:
diff --git a/src/plugin_system/core/tool_use.py b/src/plugin_system/core/tool_use.py
index 0b739aa9b..b79565f89 100644
--- a/src/plugin_system/core/tool_use.py
+++ b/src/plugin_system/core/tool_use.py
@@ -217,12 +217,11 @@ class ToolExecutor:
return tool_results, [], ""
def _get_tool_definitions(self) -> list[dict[str, Any]]:
- all_tools = get_llm_available_tool_definitions()
- user_disabled_tools = global_announcement_manager.get_disabled_chat_tools(self.chat_id)
+ all_tools = get_llm_available_tool_definitions(self.chat_id)
# 获取基础工具定义(包括二步工具的第一步)
tool_definitions = [
- definition for definition in all_tools if definition.get("function", {}).get("name") not in user_disabled_tools
+ definition for definition in all_tools if definition.get("function", {}).get("name")
]
# 检查是否有待处理的二步工具第二步调用
From 30bf1f68b1f5878d67b4f066c583042f39740064 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 11:15:45 +0800
Subject: [PATCH 07/22] =?UTF-8?q?refactor(plugin=5Fsystem):=20=E9=87=8D?=
=?UTF-8?q?=E6=9E=84=20Prompt=20=E6=B3=A8=E5=85=A5=E9=80=BB=E8=BE=91?=
=?UTF-8?q?=E4=BB=A5=E5=AE=9E=E7=8E=B0=E5=8A=A8=E6=80=81=E5=8C=96?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
本次重构的核心目标是将 Prompt 注入规则的处理方式从系统启动时的一次性加载,转变为在每次需要注入时实时、动态地构建。这解决了之前静态加载机制下,运行时启用/禁用 Prompt 组件无法影响其注入行为的问题。
主要变更包括:
- **PromptComponentManager 动态化**:
- 移除了 `load_static_rules` 和 `_initialized` 标志,规则不再在启动时预加载到 `_dynamic_rules` 中。
- `_dynamic_rules` 现在只存储通过 API 动态添加的纯运行时规则。
- 新增 `_build_rules_for_target` 方法,该方法在 `apply_injections` 时被调用,实时从 `component_registry` 获取所有已启用的静态组件规则,并与 `_dynamic_rules` 中的运行时规则合并,确保规则集始终反映当前系统状态。
- **依赖 ComponentRegistry**:
- `PromptComponentManager` 现在直接依赖 `component_registry` 来获取组件的最新启用状态和信息,而不是依赖自己预加载的缓存。
- `get_registered_prompt_component_info`, `get_injection_info`, `get_injection_rules` 等多个 API 方法被修改为 `async`,并重写了内部逻辑,以动态查询和构建信息,确保返回的数据准确反映了当前所有可用组件(包括静态和纯动态)的注入配置。
- **ComponentRegistry 增强**:
- 增加了对 Prompt 组件在禁用时从内部启用的注册表中移除的逻辑。
- 扩展了 `is_component_available` 的逻辑,使其能正确处理不支持局部(stream-specific)状态的组件类型。
---
src/chat/utils/prompt_component_manager.py | 338 ++++++++-----------
src/plugin_system/base/base_action.py | 2 +-
src/plugin_system/core/component_registry.py | 60 +++-
3 files changed, 196 insertions(+), 204 deletions(-)
diff --git a/src/chat/utils/prompt_component_manager.py b/src/chat/utils/prompt_component_manager.py
index 0a0fec1e5..d70c35859 100644
--- a/src/chat/utils/prompt_component_manager.py
+++ b/src/chat/utils/prompt_component_manager.py
@@ -29,89 +29,14 @@ class PromptComponentManager:
def __init__(self):
"""初始化管理器实例。"""
- # _dynamic_rules 是管理器的核心状态,存储所有注入规则。
+ # _dynamic_rules 仅用于存储通过 API 动态添加的、非静态组件的规则。
# 结构: {
# "target_prompt_name": {
# "prompt_component_name": (InjectionRule, content_provider, source)
# }
# }
- # content_provider 是一个异步函数,用于在应用规则时动态生成注入内容。
- # source 记录了规则的来源(例如 "static_default" 或 "runtime")。
self._dynamic_rules: dict[str, dict[str, tuple[InjectionRule, Callable[..., Awaitable[str]], str]]] = {}
- self._lock = asyncio.Lock() # 使用异步锁确保对 _dynamic_rules 的并发访问安全。
- self._initialized = False # 标记静态规则是否已加载,防止重复加载。
-
- # --- 核心生命周期与初始化 ---
-
- def load_static_rules(self):
- """
- 在系统启动时加载所有静态注入规则。
-
- 该方法会扫描所有已在 `component_registry` 中注册并启用的 Prompt 组件,
- 将其类变量 `injection_rules` 转换为管理器的动态规则。
- 这确保了所有插件定义的默认注入行为在系统启动时就能生效。
- 此操作是幂等的,一旦初始化完成就不会重复执行。
- """
- if self._initialized:
- return
- logger.info("正在加载静态 Prompt 注入规则...")
-
- # 从组件注册表中获取所有已启用的 Prompt 组件
- enabled_prompts = component_registry.get_enabled_components_by_type(ComponentType.PROMPT)
-
- for prompt_name, prompt_info in enabled_prompts.items():
- if not isinstance(prompt_info, PromptInfo):
- continue
-
- component_class = component_registry.get_component_class(prompt_name, ComponentType.PROMPT)
- if not (component_class and issubclass(component_class, BasePrompt)):
- logger.warning(f"无法为 '{prompt_name}' 加载静态规则,因为它不是一个有效的 Prompt 组件。")
- continue
-
- def create_provider(
- cls: type[BasePrompt],
- ) -> Callable[[PromptParameters, str], Awaitable[str]]:
- """
- 为静态组件创建一个内容提供者闭包 (Content Provider Closure)。
-
- 这个闭包捕获了组件的类 `cls`,并返回一个标准的 `content_provider` 异步函数。
- 当 `apply_injections` 需要内容时,它会调用这个函数。
- 函数内部会实例化组件,并执行其 `execute` 方法来获取注入内容。
-
- Args:
- cls (type[BasePrompt]): 需要为其创建提供者的 Prompt 组件类。
-
- Returns:
- Callable[[PromptParameters, str], Awaitable[str]]: 一个符合管理器标准的异步内容提供者。
- """
-
- async def content_provider(params: PromptParameters, target_prompt_name: str) -> str:
- """实际执行内容生成的异步函数。"""
- try:
- # 从注册表获取最新的组件信息,包括插件配置
- p_info = component_registry.get_component_info(cls.prompt_name, ComponentType.PROMPT)
- plugin_config = {}
- if isinstance(p_info, PromptInfo):
- plugin_config = component_registry.get_plugin_config(p_info.plugin_name)
-
- # 实例化组件并执行,传入 target_prompt_name
- instance = cls(params=params, plugin_config=plugin_config, target_prompt_name=target_prompt_name)
- result = await instance.execute()
- return str(result) if result is not None else ""
- except Exception as e:
- logger.error(f"执行静态规则提供者 '{cls.prompt_name}' 时出错: {e}", exc_info=True)
- return "" # 出错时返回空字符串,避免影响主流程
-
- return content_provider
-
- # 为该组件的每条静态注入规则创建并注册一个动态规则
- for rule in prompt_info.injection_rules:
- provider = create_provider(component_class)
- target_rules = self._dynamic_rules.setdefault(rule.target_prompt, {})
- target_rules[prompt_name] = (rule, provider, "static_default")
-
- self._initialized = True
- logger.info(f"静态 Prompt 注入规则加载完成,共处理 {len(enabled_prompts)} 个组件。")
+ self._lock = asyncio.Lock() # 锁现在保护 _dynamic_rules
# --- 运行时规则管理 API ---
@@ -243,6 +168,65 @@ class PromptComponentManager:
return removed
# --- 核心注入逻辑 ---
+ def _create_content_provider(
+ self, component_name: str, component_class: type[BasePrompt]
+ ) -> Callable[[PromptParameters, str], Awaitable[str]]:
+ """为指定的组件类创建一个标准化的内容提供者闭包。"""
+
+ async def content_provider(params: PromptParameters, target_prompt_name: str) -> str:
+ """实际执行内容生成的异步函数。"""
+ try:
+ p_info = component_registry.get_component_info(component_name, ComponentType.PROMPT)
+ plugin_config = {}
+ if isinstance(p_info, PromptInfo):
+ plugin_config = component_registry.get_plugin_config(p_info.plugin_name)
+
+ instance = component_class(
+ params=params, plugin_config=plugin_config, target_prompt_name=target_prompt_name
+ )
+ result = await instance.execute()
+ return str(result) if result is not None else ""
+ except Exception as e:
+ logger.error(f"执行规则提供者 '{component_name}' 时出错: {e}", exc_info=True)
+ return ""
+
+ return content_provider
+
+ async def _build_rules_for_target(self, target_prompt_name: str) -> list:
+ """在注入时动态构建目标的所有有效规则列表。"""
+ all_rules = []
+
+ # 1. 从 component_registry 获取所有静态组件的规则
+ static_components = component_registry.get_components_by_type(ComponentType.PROMPT)
+ for name, info in static_components.items():
+ if not isinstance(info, PromptInfo):
+ continue
+
+ # 实时检查组件是否启用
+ if not component_registry.is_component_available(name, ComponentType.PROMPT):
+ continue
+
+ component_class = component_registry.get_component_class(name, ComponentType.PROMPT)
+ if not (component_class and issubclass(component_class, BasePrompt)):
+ continue
+
+ provider = self._create_content_provider(name, component_class)
+ for rule in info.injection_rules:
+ if rule.target_prompt == target_prompt_name:
+ all_rules.append((rule, provider, "static"))
+
+ # 2. 从 _dynamic_rules 获取所有纯运行时规则
+ async with self._lock:
+ runtime_rules = self._dynamic_rules.get(target_prompt_name, {})
+ for name, (rule, provider, source) in runtime_rules.items():
+ # 确保运行时组件不会与禁用的静态组件冲突
+ static_info = component_registry.get_component_info(name, ComponentType.PROMPT)
+ if static_info and not component_registry.is_component_available(name, ComponentType.PROMPT):
+ logger.debug(f"跳过运行时规则 '{name}',因为它关联的静态组件当前已禁用。")
+ continue
+ all_rules.append((rule, provider, source))
+
+ return all_rules
async def apply_injections(
self, target_prompt_name: str, original_template: str, params: PromptParameters
@@ -268,10 +252,7 @@ class PromptComponentManager:
Returns:
str: 应用了所有注入规则后,最终生成的提示词模板字符串。
"""
- if not self._initialized:
- self.load_static_rules()
-
- rules_for_target = list(self._dynamic_rules.get(target_prompt_name, {}).values())
+ rules_for_target = await self._build_rules_for_target(target_prompt_name)
if not rules_for_target:
return original_template
@@ -279,7 +260,7 @@ class PromptComponentManager:
placeholders = re.findall(r"({[^{}]+})", original_template)
placeholder_map: dict[str, str] = {
f"__PROMPT_PLACEHOLDER_{i}__": p for i, p in enumerate(placeholders)
- }
+ }
# 1. 保护: 将占位符替换为临时标记
protected_template = original_template
@@ -405,55 +386,41 @@ class PromptComponentManager:
return [[name, prompt.template] for name, prompt in global_prompt_manager._prompts.items()]
- def get_registered_prompt_component_info(self) -> list[PromptInfo]:
+ async def get_registered_prompt_component_info(self) -> list[PromptInfo]:
"""
获取所有已注册和动态添加的Prompt组件信息,并反映当前的注入规则状态。
-
- 该方法会合并静态注册的组件信息和运行时的动态注入规则,
- 确保返回的 `PromptInfo` 列表能够准确地反映系统当前的完整状态。
-
- Returns:
- list[PromptInfo]: 一个包含所有静态和动态Prompt组件信息的列表。
- 每个组件的 `injection_rules` 都会被更新为当前实际生效的规则。
+ 此方法现在直接从 component_registry 获取静态组件信息,并合并纯运行时的组件信息。
"""
- # 步骤 1: 获取所有静态注册的组件信息,并使用深拷贝以避免修改原始数据
- static_components = component_registry.get_components_by_type(ComponentType.PROMPT)
- # 使用深拷贝以避免修改原始注册表数据
- info_dict: dict[str, PromptInfo] = {
- name: copy.deepcopy(info) for name, info in static_components.items() if isinstance(info, PromptInfo)
- }
+ # 该方法现在直接从 component_registry 获取信息,因为它总是有最新的数据
+ all_components = component_registry.get_components_by_type(ComponentType.PROMPT)
+ info_list = [info for info in all_components.values() if isinstance(info, PromptInfo)]
- # 步骤 2: 遍历动态规则,识别并创建纯动态组件的 PromptInfo
- all_dynamic_component_names = set()
- for target, rules in self._dynamic_rules.items():
- for prompt_name, (rule, _, source) in rules.items():
- all_dynamic_component_names.add(prompt_name)
+ # 检查是否有纯动态组件需要添加
+ async with self._lock:
+ runtime_component_names = set()
+ for rules in self._dynamic_rules.values():
+ runtime_component_names.update(rules.keys())
- for name in all_dynamic_component_names:
- if name not in info_dict:
- # 这是一个纯动态组件,为其创建一个新的 PromptInfo
- info_dict[name] = PromptInfo(
+ static_component_names = {info.name for info in info_list}
+ pure_dynamic_names = runtime_component_names - static_component_names
+
+ for name in pure_dynamic_names:
+ # 为纯动态组件创建临时的 PromptInfo
+ dynamic_info = PromptInfo(
name=name,
component_type=ComponentType.PROMPT,
- description="Dynamically added component",
- plugin_name="runtime", # 动态组件通常没有插件归属
+ description="Dynamically added runtime component",
+ plugin_name="runtime",
is_built_in=False,
)
+ # 从 _dynamic_rules 中收集其所有规则
+ for target, rules_in_target in self._dynamic_rules.items():
+ if name in rules_in_target:
+ rule, _, _ = rules_in_target[name]
+ dynamic_info.injection_rules.append(rule)
+ info_list.append(dynamic_info)
- # 步骤 3: 清空所有组件的注入规则,准备用当前状态重新填充
- for info in info_dict.values():
- info.injection_rules = []
-
- # 步骤 4: 再次遍历动态规则,为每个组件重建其 injection_rules 列表
- for target, rules in self._dynamic_rules.items():
- for prompt_name, (rule, _, _) in rules.items():
- if prompt_name in info_dict:
- # 确保规则是 InjectionRule 的实例
- if isinstance(rule, InjectionRule):
- info_dict[prompt_name].injection_rules.append(rule)
-
- # 步骤 5: 返回最终的 PromptInfo 对象列表
- return list(info_dict.values())
+ return info_list
async def get_injection_info(
self,
@@ -462,60 +429,47 @@ class PromptComponentManager:
) -> dict[str, list[dict]]:
"""
获取注入信息的映射图,可按目标筛选,并可控制信息的详细程度。
-
- - `get_injection_info()` 返回所有目标的摘要注入信息。
- - `get_injection_info(target_prompt="...")` 返回指定目标的摘要注入信息。
- - `get_injection_info(detailed=True)` 返回所有目标的详细注入信息。
- - `get_injection_info(target_prompt="...", detailed=True)` 返回指定目标的详细注入信息。
-
- Args:
- target_prompt (str, optional): 如果指定,仅返回该目标的注入信息。
- detailed (bool, optional): 如果为 True,则返回包含注入类型和内容的详细信息。
- 默认为 False,返回摘要信息。
-
- Returns:
- dict[str, list[dict]]: 一个字典,键是目标提示词名称,
- 值是按优先级排序的注入信息列表。
+ 此方法现在动态构建信息,以反映当前启用的组件和规则。
"""
info_map = {}
- async with self._lock:
- all_targets = set(self._dynamic_rules.keys()) | set(self.get_core_prompts())
+ all_core_prompts = self.get_core_prompts()
+ targets_to_process = [target_prompt] if target_prompt and target_prompt in all_core_prompts else all_core_prompts
- # 如果指定了目标,则只处理该目标
- targets_to_process = [target_prompt] if target_prompt and target_prompt in all_targets else sorted(all_targets)
+ for target in targets_to_process:
+ # 动态构建规则列表
+ rules_for_target = await self._build_rules_for_target(target)
+ if not rules_for_target:
+ info_map[target] = []
+ continue
- for target in targets_to_process:
- rules = self._dynamic_rules.get(target, {})
- if not rules:
- info_map[target] = []
- continue
+ info_list = []
+ for rule, _, source in rules_for_target:
+ # 从规则本身获取组件名
+ prompt_name = rule.owner_component
+ if detailed:
+ info_list.append(
+ {
+ "name": prompt_name,
+ "priority": rule.priority,
+ "source": source,
+ "injection_type": rule.injection_type.value,
+ "target_content": rule.target_content,
+ }
+ )
+ else:
+ info_list.append({"name": prompt_name, "priority": rule.priority, "source": source})
- info_list = []
- for prompt_name, (rule, _, source) in rules.items():
- if detailed:
- info_list.append(
- {
- "name": prompt_name,
- "priority": rule.priority,
- "source": source,
- "injection_type": rule.injection_type.value,
- "target_content": rule.target_content,
- }
- )
- else:
- info_list.append({"name": prompt_name, "priority": rule.priority, "source": source})
-
- info_list.sort(key=lambda x: x["priority"])
- info_map[target] = info_list
+ info_list.sort(key=lambda x: x["priority"])
+ info_map[target] = info_list
return info_map
- def get_injection_rules(
+ async def get_injection_rules(
self,
target_prompt: str | None = None,
component_name: str | None = None,
) -> dict[str, dict[str, "InjectionRule"]]:
"""
- 获取动态注入规则,可通过目标或组件名称进行筛选。
+ 获取所有(包括静态和运行时)注入规则,可通过目标或组件名称进行筛选。
- 不提供任何参数时,返回所有规则。
- 提供 `target_prompt` 时,仅返回注入到该目标的规则。
@@ -527,44 +481,42 @@ class PromptComponentManager:
component_name (str, optional): 按注入组件名称筛选。
Returns:
- dict[str, dict[str, InjectionRule]]: 一个深拷贝的规则字典。
+ dict[str, dict[str, InjectionRule]]: 一个包含所有匹配规则的深拷贝字典。
结构: { "target_prompt": { "component_name": InjectionRule } }
"""
- rules_copy = {}
- # 筛选目标
- targets_to_check = [target_prompt] if target_prompt else self._dynamic_rules.keys()
+ all_rules: dict[str, dict[str, InjectionRule]] = {}
- for target in targets_to_check:
- if target not in self._dynamic_rules:
+ # 1. 收集所有静态组件的规则
+ static_components = component_registry.get_components_by_type(ComponentType.PROMPT)
+ for name, info in static_components.items():
+ if not isinstance(info, PromptInfo):
+ continue
+ # 应用 component_name 筛选
+ if component_name and name != component_name:
continue
- rules_for_target = self._dynamic_rules[target]
- target_copy = {}
+ for rule in info.injection_rules:
+ # 应用 target_prompt 筛选
+ if target_prompt and rule.target_prompt != target_prompt:
+ continue
+ target_dict = all_rules.setdefault(rule.target_prompt, {})
+ target_dict[name] = rule
- # 筛选组件
- if component_name:
- if component_name in rules_for_target:
- rule, _, _ = rules_for_target[component_name]
- target_copy[component_name] = rule
- else:
- for name, (rule, _, _) in rules_for_target.items():
- target_copy[name] = rule
+ # 2. 收集并合并所有纯运行时规则
+ async with self._lock:
+ for target, rules_in_target in self._dynamic_rules.items():
+ # 应用 target_prompt 筛选
+ if target_prompt and target != target_prompt:
+ continue
- if target_copy:
- rules_copy[target] = target_copy
+ for name, (rule, _, _) in rules_in_target.items():
+ # 应用 component_name 筛选
+ if component_name and name != component_name:
+ continue
+ target_dict = all_rules.setdefault(target, {})
+ target_dict[name] = rule
- # 如果是按组件筛选且未指定目标,则需遍历所有目标
- if component_name and not target_prompt:
- found_rules = {}
- for target, rules in self._dynamic_rules.items():
- if component_name in rules:
- rule, _, _ = rules[component_name]
- if target not in found_rules:
- found_rules[target] = {}
- found_rules[target][component_name] = rule
- return copy.deepcopy(found_rules)
-
- return copy.deepcopy(rules_copy)
+ return copy.deepcopy(all_rules)
# 创建全局单例 (Singleton)
diff --git a/src/plugin_system/base/base_action.py b/src/plugin_system/base/base_action.py
index a715b98b0..61bb8eb71 100644
--- a/src/plugin_system/base/base_action.py
+++ b/src/plugin_system/base/base_action.py
@@ -110,7 +110,7 @@ class BaseAction(ABC):
**kwargs: 其他参数
"""
if plugin_config is None:
- plugin_config: ClassVar = {}
+ plugin_config = {}
self.action_data = action_data
self.reasoning = reasoning
self.cycle_timers = cycle_timers
diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py
index cda73fe22..bb2e7a18e 100644
--- a/src/plugin_system/core/component_registry.py
+++ b/src/plugin_system/core/component_registry.py
@@ -492,7 +492,7 @@ class ComponentRegistry:
if hasattr(self, "_enabled_prompt_registry"):
self._enabled_prompt_registry.pop(component_name, None)
logger.debug(f"已移除Prompt组件: {component_name}")
-
+
case ComponentType.ROUTER:
# Router组件的移除比较复杂,目前只记录日志
logger.warning(f"Router组件 '{component_name}' 的HTTP端点无法在运行时动态移除,将在下次重启后生效。")
@@ -605,6 +605,9 @@ class ComponentRegistry:
result = event_manager.unsubscribe_handler_from_event(event, component_name)
if hasattr(result, "__await__"):
await result # type: ignore[func-returns-value]
+ case ComponentType.PROMPT:
+ if hasattr(self, "_enabled_prompt_registry"):
+ self._enabled_prompt_registry.pop(component_name, None)
# 组件主注册表使用命名空间 key
namespaced_name = f"{component_type.value}.{component_name}"
@@ -915,6 +918,27 @@ class ComponentRegistry:
info = self.get_component_info(chatter_name, ComponentType.CHATTER)
return info if isinstance(info, ChatterInfo) else None
+ # === Prompt 特定查询方法 ===
+ def get_prompt_registry(self) -> dict[str, type[BasePrompt]]:
+ """获取Prompt注册表"""
+ if not hasattr(self, "_prompt_registry"):
+ self._prompt_registry: dict[str, type[BasePrompt]] = {}
+ return self._prompt_registry.copy()
+
+ def get_enabled_prompt_registry(self, stream_id: str | None = None) -> dict[str, type[BasePrompt]]:
+ """获取启用的Prompt注册表, 可选地根据 stream_id 考虑局部状态"""
+ all_prompts = self.get_prompt_registry()
+ available_prompts = {}
+ for name, prompt_class in all_prompts.items():
+ if self.is_component_available(name, ComponentType.PROMPT, stream_id):
+ available_prompts[name] = prompt_class
+ return available_prompts
+
+ def get_registered_prompt_info(self, prompt_name: str) -> PromptInfo | None:
+ """获取Prompt信息"""
+ info = self.get_component_info(prompt_name, ComponentType.PROMPT)
+ return info if isinstance(info, PromptInfo) else None
+
# === 插件查询方法 ===
def get_plugin_info(self, plugin_name: str) -> PluginInfo | None:
@@ -1020,31 +1044,47 @@ class ComponentRegistry:
self, stream_id: str, component_name: str, component_type: ComponentType, enabled: bool
) -> bool:
"""为指定的 stream_id 设置组件的局部(临时)状态"""
+ # 如果组件类型不需要局部状态管理,则记录警告并返回
+ if component_type in self._no_local_state_types:
+ logger.warning(
+ f"组件类型 {component_type.value} 不支持局部状态管理。 "
+ f"尝试为 '{component_name}' 设置局部状态的操作将被忽略。"
+ )
+ return False
+
if stream_id not in self._local_component_states:
self._local_component_states[stream_id] = {}
-
+
state_key = (component_name, component_type)
self._local_component_states[stream_id][state_key] = enabled
- logger.debug(f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type}) -> {'启用' if enabled else '禁用'}")
+ logger.debug(
+ f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type}) -> {'启用' if enabled else '禁用'}"
+ )
return True
- def is_component_available(self, component_name: str, component_type: ComponentType, stream_id: str | None = None) -> bool:
+ def is_component_available(
+ self, component_name: str, component_type: ComponentType, stream_id: str | None = None
+ ) -> bool:
"""检查组件在给定上下文中是否可用(同时考虑全局和局部状态)"""
component_info = self.get_component_info(component_name, component_type)
-
+
# 1. 检查组件是否存在
if not component_info:
return False
-
- # 2. 如果提供了 stream_id,检查局部状态
+
+ # 2. 如果组件类型不需要局部状态,则直接返回其全局状态
+ if component_type in self._no_local_state_types:
+ return component_info.enabled
+
+ # 3. 如果提供了 stream_id,检查局部状态
if stream_id and stream_id in self._local_component_states:
state_key = (component_name, component_type)
local_state = self._local_component_states[stream_id].get(state_key)
-
+
if local_state is not None:
return local_state # 局部状态存在,覆盖全局状态
-
- # 3. 如果没有局部状态覆盖,则返回全局状态
+
+ # 4. 如果没有局部状态覆盖,则返回全局状态
return component_info.enabled
# === MCP 工具相关方法 ===
From f3ae22d622909172abdaa1711cd857fa6274dead Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 11:24:26 +0800
Subject: [PATCH 08/22] =?UTF-8?q?refactor(plugin=5Fsystem):=20=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=AF=B9=E9=83=A8=E5=88=86=E7=BB=84=E4=BB=B6=E7=9A=84?=
=?UTF-8?q?=E5=B1=80=E9=83=A8=E7=8A=B6=E6=80=81=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
根据新的设计,某些组件类型,如 ROUTER、EVENT_HANDLER 和 PROMPT,不应再支持局部启用/禁用状态。这些组件的状态应该在全局范围内进行管理。
此更改包括:
- 在 `ComponentRegistry` 中引入 `_no_local_state_types` 集合,明确指定不支持局部状态的组件类型。
- 移除 `get_enabled_event_handlers` 和 `get_enabled_prompt_registry` 方法中的 `stream_id` 参数和局部状态检查逻辑,使其只返回全局启用的组件。
这一重构简化了状态管理逻辑,并使组件行为与设计意图保持一致。
---
src/plugin_system/core/component_registry.py | 33 ++++++++++----------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py
index bb2e7a18e..6b0a90363 100644
--- a/src/plugin_system/core/component_registry.py
+++ b/src/plugin_system/core/component_registry.py
@@ -111,6 +111,13 @@ class ComponentRegistry:
self._local_component_states: dict[str, dict[tuple[str, ComponentType], bool]] = {}
"""stream_id -> {(component_name, component_type): enabled_status}"""
logger.info("组件注册中心初始化完成")
+ self._no_local_state_types: set[ComponentType] = {
+ ComponentType.ROUTER,
+ ComponentType.EVENT_HANDLER,
+ ComponentType.ROUTER,
+ ComponentType.PROMPT,
+ # 根据设计,COMMAND 和 PLUS_COMMAND 也不应支持局部状态
+ }
# == 注册方法 ==
@@ -888,14 +895,11 @@ class ComponentRegistry:
info = self.get_component_info(handler_name, ComponentType.EVENT_HANDLER)
return info if isinstance(info, EventHandlerInfo) else None
- def get_enabled_event_handlers(self, stream_id: str | None = None) -> dict[str, type[BaseEventHandler]]:
- """获取启用的事件处理器, 可选地根据 stream_id 考虑局部状态"""
- all_handlers = self.get_event_handler_registry()
- available_handlers = {}
- for name, handler_class in all_handlers.items():
- if self.is_component_available(name, ComponentType.EVENT_HANDLER, stream_id):
- available_handlers[name] = handler_class
- return available_handlers
+ def get_enabled_event_handlers(self) -> dict[str, type[BaseEventHandler]]:
+ """获取启用的事件处理器"""
+ if not hasattr(self, "_enabled_event_handlers"):
+ self._enabled_event_handlers: dict[str, type["BaseEventHandler"]] = {}
+ return self._enabled_event_handlers.copy()
# === Chatter 特定查询方法 ===
def get_chatter_registry(self) -> dict[str, type[BaseChatter]]:
@@ -925,14 +929,11 @@ class ComponentRegistry:
self._prompt_registry: dict[str, type[BasePrompt]] = {}
return self._prompt_registry.copy()
- def get_enabled_prompt_registry(self, stream_id: str | None = None) -> dict[str, type[BasePrompt]]:
- """获取启用的Prompt注册表, 可选地根据 stream_id 考虑局部状态"""
- all_prompts = self.get_prompt_registry()
- available_prompts = {}
- for name, prompt_class in all_prompts.items():
- if self.is_component_available(name, ComponentType.PROMPT, stream_id):
- available_prompts[name] = prompt_class
- return available_prompts
+ def get_enabled_prompt_registry(self) -> dict[str, type[BasePrompt]]:
+ """获取启用的Prompt注册表"""
+ if not hasattr(self, "_enabled_prompt_registry"):
+ self._enabled_prompt_registry: dict[str, type[BasePrompt]] = {}
+ return self._enabled_prompt_registry.copy()
def get_registered_prompt_info(self, prompt_name: str) -> PromptInfo | None:
"""获取Prompt信息"""
From 94b4123039c7af407a39cd627a217f88111672be Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 12:35:37 +0800
Subject: [PATCH 09/22] =?UTF-8?q?refactor(plugin=5Fsystem):=20=E5=BA=9F?=
=?UTF-8?q?=E5=BC=83=E6=97=A7=E7=89=88Command=E7=B3=BB=E7=BB=9F=E5=B9=B6?=
=?UTF-8?q?=E9=87=8D=E6=9E=84=E6=B3=A8=E5=86=8C=E4=B8=AD=E5=BF=83?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
本次提交完全移除了对旧版 `BaseCommand` 系统的支持,统一使用 `PlusCommand`。所有旧版命令现在通过一个兼容性适配器在加载时自动转换为 `PlusCommand`,简化了命令处理流程和代码库。
主要变更:
- **移除旧版命令处理**: 删除了 `ChatBot` 中专门处理旧版 `BaseCommand` 的方法 (`_process_commands_with_new_system`) 和相关逻辑,现在所有命令都通过 `PlusCommand` 的处理流程。
- **重构组件注册中心**: 对 `ComponentRegistry` 进行了大规模重构和清理:
- 添加了大量文档字符串和类型提示,显著提升了代码的可读性和可维护性。
- 废弃了特定于 `BaseCommand` 的注册表和查找方法 (`_command_registry`, `_command_patterns`, `find_command_by_text`)。
- 实现了 `unregister_plugin` 和 `remove_component` 方法,支持插件和组件在运行时的动态卸载。
- 统一并简化了各类组件的注册、查询和状态管理逻辑,使其更加一致和健壮。
BREAKING CHANGE: 废弃了 `BaseCommand` 类。所有自定义命令现在必须继承自 `PlusCommand`。虽然系统提供了向后兼容的适配器,但强烈建议将现有命令迁移到 `PlusCommand` 以获得全部功能和最佳性能。直接依赖旧版 `BaseCommand` 注册和查找机制的代码将无法工作。
---
src/chat/message_receive/bot.py | 84 -
src/chat/utils/prompt_component_manager.py | 100 +-
src/plugin_system/core/component_registry.py | 1770 ++++++++----------
3 files changed, 879 insertions(+), 1075 deletions(-)
diff --git a/src/chat/message_receive/bot.py b/src/chat/message_receive/bot.py
index 5e2ccb5e3..63c6f0512 100644
--- a/src/chat/message_receive/bot.py
+++ b/src/chat/message_receive/bot.py
@@ -197,80 +197,6 @@ class ChatBot:
logger.error(f"处理PlusCommand时出错: {e}")
return False, None, True # 出错时继续处理消息
- async def _process_commands_with_new_system(self, message: DatabaseMessages, chat: ChatStream):
- # sourcery skip: use-named-expression
- """使用新插件系统处理命令"""
- try:
- text = message.processed_plain_text or ""
-
- # 使用新的组件注册中心查找命令
- command_result = component_registry.find_command_by_text(text)
- if command_result:
- command_class, matched_groups, command_info = command_result
- plugin_name = command_info.plugin_name
- command_name = command_info.name
- if (
- chat
- and chat.stream_id
- and command_name
- in global_announcement_manager.get_disabled_chat_commands(chat.stream_id)
- ):
- logger.info("用户禁用的命令,跳过处理")
- return False, None, True
-
- message.is_command = True
-
- # 获取插件配置
- plugin_config = component_registry.get_plugin_config(plugin_name)
-
- # 创建命令实例
- command_instance: BaseCommand = command_class(message, plugin_config)
- command_instance.set_matched_groups(matched_groups)
-
- # 为插件实例设置 chat_stream 运行时属性
- setattr(command_instance, "chat_stream", chat)
-
- try:
- # 检查聊天类型限制
- if not command_instance.is_chat_type_allowed():
- is_group = chat.group_info is not None
- logger.info(
- f"命令 {command_class.__name__} 不支持当前聊天类型: {'群聊' if is_group else '私聊'}"
- )
- return False, None, True # 跳过此命令,继续处理其他消息
-
- # 执行命令
- success, response, intercept_message = await command_instance.execute()
-
- # 记录命令执行结果
- if success:
- logger.info(f"命令执行成功: {command_class.__name__} (拦截: {intercept_message})")
- else:
- logger.warning(f"命令执行失败: {command_class.__name__} - {response}")
-
- # 根据命令的拦截设置决定是否继续处理消息
- return True, response, not intercept_message # 找到命令,根据intercept_message决定是否继续
-
- except Exception as e:
- logger.error(f"执行命令时出错: {command_class.__name__} - {e}")
- logger.error(traceback.format_exc())
-
- try:
- await command_instance.send_text(f"命令执行出错: {e!s}")
- except Exception as send_error:
- logger.error(f"发送错误消息失败: {send_error}")
-
- # 命令出错时,根据命令的拦截设置决定是否继续处理消息
- return True, str(e), False # 出错时继续处理消息
-
- # 没有找到命令,继续处理消息
- return False, None, True
-
- except Exception as e:
- logger.error(f"处理命令时出错: {e}")
- return False, None, True # 出错时继续处理消息
-
-
async def _handle_adapter_response_from_dict(self, seg_data: dict | None):
"""处理适配器命令响应(从字典数据)"""
try:
@@ -412,16 +338,6 @@ class ChatBot:
logger.info(f"PlusCommand处理完成,跳过后续消息处理: {plus_cmd_result}")
return
- # 如果不是PlusCommand,尝试传统的BaseCommand处理
- if not is_plus_command:
- is_command, cmd_result, continue_process = await self._process_commands_with_new_system(message, chat)
-
- # 如果是命令且不需要继续处理,则直接返回
- if is_command and not continue_process:
- await MessageStorage.store_message(message, chat)
- logger.info(f"命令处理完成,跳过后续消息处理: {cmd_result}")
- return
-
result = await event_manager.trigger_event(EventType.ON_MESSAGE, permission_group="SYSTEM", message=message)
if result and not result.all_continue_process():
raise UserWarning(f"插件{result.get_summary().get('stopped_handlers', '')}于消息到达时取消了消息处理")
diff --git a/src/chat/utils/prompt_component_manager.py b/src/chat/utils/prompt_component_manager.py
index d70c35859..685f4e169 100644
--- a/src/chat/utils/prompt_component_manager.py
+++ b/src/chat/utils/prompt_component_manager.py
@@ -29,14 +29,16 @@ class PromptComponentManager:
def __init__(self):
"""初始化管理器实例。"""
- # _dynamic_rules 仅用于存储通过 API 动态添加的、非静态组件的规则。
+ # _dynamic_rules 存储通过 API 在运行时动态添加/修改的规则。
+ # 这是实现提示词动态性的核心数据结构。
# 结构: {
- # "target_prompt_name": {
- # "prompt_component_name": (InjectionRule, content_provider, source)
+ # "target_prompt_name": { // 目标 Prompt 的名称
+ # "prompt_component_name": (InjectionRule, content_provider, source) // 注入组件的规则详情
# }
# }
self._dynamic_rules: dict[str, dict[str, tuple[InjectionRule, Callable[..., Awaitable[str]], str]]] = {}
- self._lock = asyncio.Lock() # 锁现在保护 _dynamic_rules
+ # 使用 asyncio.Lock 来确保对 _dynamic_rules 的所有写操作都是线程安全的。
+ self._lock = asyncio.Lock()
# --- 运行时规则管理 API ---
@@ -64,9 +66,13 @@ class PromptComponentManager:
Returns:
bool: 如果成功添加或更新,则返回 True。
"""
+ # 加锁以保证多协程环境下的数据一致性
async with self._lock:
+ # 遍历所有待添加的规则
for rule in rules:
+ # 使用 setdefault 确保目标 prompt 的规则字典存在
target_rules = self._dynamic_rules.setdefault(rule.target_prompt, {})
+ # 添加或覆盖指定组件的规则、内容提供者和来源
target_rules[prompt_name] = (rule, content_provider, source)
logger.info(f"成功添加/更新注入规则: '{prompt_name}' -> '{rule.target_prompt}' (来源: {source})")
return True
@@ -88,15 +94,16 @@ class PromptComponentManager:
如果未找到该组件的任何现有规则(无法复用),则返回 False。
"""
async with self._lock:
- # 步骤 1: 查找现有的 content_provider 和 source
+ # 步骤 1: 遍历所有动态规则,查找指定组件已存在的 provider 和 source
found_provider: Callable[..., Awaitable[str]] | None = None
found_source: str | None = None
for target_rules in self._dynamic_rules.values():
if prompt_name in target_rules:
+ # 如果找到,记录其 provider 和 source 并跳出循环
_, found_provider, found_source = target_rules[prompt_name]
break
- # 步骤 2: 如果找不到 provider,则操作失败
+ # 步骤 2: 如果遍历完仍未找到 provider,说明该组件无任何规则,无法复用
if not found_provider:
logger.warning(
f"尝试为组件 '{prompt_name}' 添加规则失败: "
@@ -105,7 +112,7 @@ class PromptComponentManager:
return False
# 步骤 3: 使用找到的 provider 和 source 添加新规则
- source_to_use = found_source or "runtime" # 提供一个默认值以防万一
+ source_to_use = found_source or "runtime" # 如果 source 为 None,提供默认值
target_rules = self._dynamic_rules.setdefault(rule.target_prompt, {})
target_rules[prompt_name] = (rule, found_provider, source_to_use)
logger.info(
@@ -126,13 +133,16 @@ class PromptComponentManager:
bool: 如果成功移除,则返回 True;如果规则不存在,则返回 False。
"""
async with self._lock:
+ # 检查目标和组件规则是否存在
if target_prompt in self._dynamic_rules and prompt_name in self._dynamic_rules[target_prompt]:
+ # 存在则删除
del self._dynamic_rules[target_prompt][prompt_name]
- # 如果目标下已无任何规则,则清理掉这个键
+ # 如果删除后,该目标下已无任何规则,则清理掉这个目标键,保持数据结构整洁
if not self._dynamic_rules[target_prompt]:
del self._dynamic_rules[target_prompt]
logger.info(f"成功移除注入规则: '{prompt_name}' from '{target_prompt}'")
return True
+ # 如果规则不存在,记录警告并返回 False
logger.warning(f"尝试移除注入规则失败: 未找到 '{prompt_name}' on '{target_prompt}'")
return False
@@ -153,7 +163,9 @@ class PromptComponentManager:
async with self._lock:
# 创建一个目标列表的副本进行迭代,因为我们可能会在循环中修改字典
for target_prompt in list(self._dynamic_rules.keys()):
+ # 检查当前目标下是否存在该组件的规则
if prompt_name in self._dynamic_rules[target_prompt]:
+ # 存在则删除
del self._dynamic_rules[target_prompt][prompt_name]
removed = True
logger.info(f"成功移除注入规则: '{prompt_name}' from '{target_prompt}'")
@@ -176,17 +188,23 @@ class PromptComponentManager:
async def content_provider(params: PromptParameters, target_prompt_name: str) -> str:
"""实际执行内容生成的异步函数。"""
try:
+ # 从注册表获取组件信息,用于后续获取插件配置
p_info = component_registry.get_component_info(component_name, ComponentType.PROMPT)
plugin_config = {}
if isinstance(p_info, PromptInfo):
+ # 获取该组件所属插件的配置
plugin_config = component_registry.get_plugin_config(p_info.plugin_name)
+ # 实例化组件,并传入所需参数
instance = component_class(
params=params, plugin_config=plugin_config, target_prompt_name=target_prompt_name
)
+ # 执行组件的 execute 方法以生成内容
result = await instance.execute()
+ # 确保返回的是字符串
return str(result) if result is not None else ""
except Exception as e:
+ # 捕获并记录执行过程中的任何异常,返回空字符串以避免注入失败
logger.error(f"执行规则提供者 '{component_name}' 时出错: {e}", exc_info=True)
return ""
@@ -202,16 +220,20 @@ class PromptComponentManager:
if not isinstance(info, PromptInfo):
continue
- # 实时检查组件是否启用
+ # 实时检查组件是否已启用,跳过禁用的组件
if not component_registry.is_component_available(name, ComponentType.PROMPT):
continue
+ # 获取组件的类定义
component_class = component_registry.get_component_class(name, ComponentType.PROMPT)
if not (component_class and issubclass(component_class, BasePrompt)):
continue
+ # 为该组件创建一个内容提供者
provider = self._create_content_provider(name, component_class)
+ # 遍历组件定义的所有注入规则
for rule in info.injection_rules:
+ # 如果规则的目标与当前目标匹配,则添加到列表中
if rule.target_prompt == target_prompt_name:
all_rules.append((rule, provider, "static"))
@@ -219,11 +241,13 @@ class PromptComponentManager:
async with self._lock:
runtime_rules = self._dynamic_rules.get(target_prompt_name, {})
for name, (rule, provider, source) in runtime_rules.items():
- # 确保运行时组件不会与禁用的静态组件冲突
+ # 检查该运行时规则是否关联到一个已注册的静态组件
static_info = component_registry.get_component_info(name, ComponentType.PROMPT)
+ # 如果关联的静态组件存在且被禁用,则跳过此运行时规则
if static_info and not component_registry.is_component_available(name, ComponentType.PROMPT):
logger.debug(f"跳过运行时规则 '{name}',因为它关联的静态组件当前已禁用。")
continue
+ # 将有效的运行时规则添加到列表
all_rules.append((rule, provider, source))
return all_rules
@@ -252,17 +276,19 @@ class PromptComponentManager:
Returns:
str: 应用了所有注入规则后,最终生成的提示词模板字符串。
"""
+ # 构建适用于当前目标的所有规则
rules_for_target = await self._build_rules_for_target(target_prompt_name)
if not rules_for_target:
+ # 如果没有规则,直接返回原始模板
return original_template
# --- 占位符保护机制 ---
+ # 1. 保护: 找到所有 {placeholder} 并用临时标记替换
placeholders = re.findall(r"({[^{}]+})", original_template)
placeholder_map: dict[str, str] = {
f"__PROMPT_PLACEHOLDER_{i}__": p for i, p in enumerate(placeholders)
}
- # 1. 保护: 将占位符替换为临时标记
protected_template = original_template
for marker, placeholder in placeholder_map.items():
protected_template = protected_template.replace(placeholder, marker)
@@ -271,6 +297,7 @@ class PromptComponentManager:
for rule, _, source in rules_for_target:
if rule.injection_type in (InjectionType.REMOVE, InjectionType.REPLACE) and rule.target_content:
try:
+ # 检查规则的 target_content (正则) 是否可能匹配到任何一个占位符
for p in placeholders:
if re.search(rule.target_content, p):
logger.warning(
@@ -278,10 +305,10 @@ class PromptComponentManager:
f"规则 `target_content` ('{rule.target_content}') "
f"可能会影响核心占位符 '{p}'。为保证系统稳定,该占位符已被保护,不会被此规则修改。"
)
- # 只对每个规则警告一次
+ # 每个规则只警告一次
break
except re.error:
- # 正则表达式本身有误,后面执行时会再次捕获,这里可忽略
+ # 如果正则表达式本身有误,后续执行时会捕获,此处可忽略
pass
# 3. 安全执行: 按优先级排序并应用规则
@@ -290,13 +317,16 @@ class PromptComponentManager:
modified_template = protected_template
for rule, provider, source in rules_for_target:
content = ""
+ # REMOVE 类型不需要生成内容
if rule.injection_type != InjectionType.REMOVE:
try:
+ # 调用内容提供者生成要注入的文本
content = await provider(params, target_prompt_name)
except Exception as e:
logger.error(f"执行规则 '{rule}' (来源: {source}) 的内容提供者时失败: {e}", exc_info=True)
- continue
+ continue # 执行失败则跳过此规则
+ # 应用注入规则
try:
if rule.injection_type == InjectionType.PREPEND:
if content:
@@ -309,6 +339,7 @@ class PromptComponentManager:
modified_template = re.sub(rule.target_content, str(content), modified_template)
elif rule.injection_type == InjectionType.INSERT_AFTER:
if content and rule.target_content:
+ # 使用 \\g<0> 在匹配项后插入内容
replacement = f"\\g<0>\n{content}"
modified_template = re.sub(rule.target_content, replacement, modified_template)
elif rule.injection_type == InjectionType.REMOVE:
@@ -319,7 +350,7 @@ class PromptComponentManager:
except Exception as e:
logger.error(f"应用注入规则 '{rule}' (来源: {source}) 失败: {e}", exc_info=True)
- # 4. 占位符恢复
+ # 4. 占位符恢复: 将临时标记替换回原始的占位符
final_template = modified_template
for marker, placeholder in placeholder_map.items():
final_template = final_template.replace(marker, placeholder)
@@ -343,8 +374,9 @@ class PromptComponentManager:
str: 模拟生成的最终提示词模板字符串。如果找不到模板,则返回错误信息。
"""
try:
- # 从全局提示词管理器获取最原始的模板内容
+ # 动态导入以避免循环依赖
from src.chat.utils.prompt import global_prompt_manager
+ # 从全局管理器获取原始的、未经修改的提示词对象
original_prompt = global_prompt_manager._prompts.get(target_prompt_name)
if not original_prompt:
logger.warning(f"无法预览 '{target_prompt_name}',因为找不到这个核心 Prompt。")
@@ -354,14 +386,16 @@ class PromptComponentManager:
logger.warning(f"无法预览 '{target_prompt_name}',因为找不到这个核心 Prompt。")
return f"Error: Prompt '{target_prompt_name}' not found."
- # 直接调用核心注入逻辑来模拟结果
+ # 直接调用核心注入逻辑来模拟并返回结果
return await self.apply_injections(target_prompt_name, original_template, params)
# --- 状态观测与查询 API ---
def get_core_prompts(self) -> list[str]:
"""获取所有已注册的核心提示词模板名称列表(即所有可注入的目标)。"""
+ # 动态导入以避免循环依赖
from src.chat.utils.prompt import global_prompt_manager
+ # 返回所有核心 prompt 的名称列表
return list(global_prompt_manager._prompts.keys())
def get_core_prompt_contents(self, prompt_name: str | None = None) -> list[list[str]]:
@@ -381,9 +415,11 @@ class PromptComponentManager:
from src.chat.utils.prompt import global_prompt_manager
if prompt_name:
+ # 如果指定了名称,则查找并返回单个模板
prompt = global_prompt_manager._prompts.get(prompt_name)
return [[prompt_name, prompt.template]] if prompt else []
+ # 如果未指定名称,则返回所有模板的列表
return [[name, prompt.template] for name, prompt in global_prompt_manager._prompts.items()]
async def get_registered_prompt_component_info(self) -> list[PromptInfo]:
@@ -391,21 +427,23 @@ class PromptComponentManager:
获取所有已注册和动态添加的Prompt组件信息,并反映当前的注入规则状态。
此方法现在直接从 component_registry 获取静态组件信息,并合并纯运行时的组件信息。
"""
- # 该方法现在直接从 component_registry 获取信息,因为它总是有最新的数据
+ # 从注册表获取所有已注册的静态 Prompt 组件信息
all_components = component_registry.get_components_by_type(ComponentType.PROMPT)
info_list = [info for info in all_components.values() if isinstance(info, PromptInfo)]
- # 检查是否有纯动态组件需要添加
+ # 检查并合并仅在运行时通过 API 添加的“纯动态”组件
async with self._lock:
runtime_component_names = set()
+ # 收集所有动态规则中涉及的组件名称
for rules in self._dynamic_rules.values():
runtime_component_names.update(rules.keys())
static_component_names = {info.name for info in info_list}
+ # 找出那些只存在于动态规则中,但未在静态组件中注册的名称
pure_dynamic_names = runtime_component_names - static_component_names
for name in pure_dynamic_names:
- # 为纯动态组件创建临时的 PromptInfo
+ # 为这些“纯动态”组件创建一个临时的信息对象
dynamic_info = PromptInfo(
name=name,
component_type=ComponentType.PROMPT,
@@ -413,7 +451,7 @@ class PromptComponentManager:
plugin_name="runtime",
is_built_in=False,
)
- # 从 _dynamic_rules 中收集其所有规则
+ # 从动态规则中收集并关联其所有注入规则
for target, rules_in_target in self._dynamic_rules.items():
if name in rules_in_target:
rule, _, _ = rules_in_target[name]
@@ -433,10 +471,11 @@ class PromptComponentManager:
"""
info_map = {}
all_core_prompts = self.get_core_prompts()
+ # 确定要处理的目标:如果指定了有效的目标,则只处理它;否则处理所有核心 prompt
targets_to_process = [target_prompt] if target_prompt and target_prompt in all_core_prompts else all_core_prompts
for target in targets_to_process:
- # 动态构建规则列表
+ # 动态构建该目标的所有有效规则
rules_for_target = await self._build_rules_for_target(target)
if not rules_for_target:
info_map[target] = []
@@ -444,9 +483,10 @@ class PromptComponentManager:
info_list = []
for rule, _, source in rules_for_target:
- # 从规则本身获取组件名
+ # 从规则对象中获取其所属组件的名称
prompt_name = rule.owner_component
if detailed:
+ # 如果需要详细信息,则添加更多字段
info_list.append(
{
"name": prompt_name,
@@ -457,8 +497,10 @@ class PromptComponentManager:
}
)
else:
+ # 否则只添加基本信息
info_list.append({"name": prompt_name, "priority": rule.priority, "source": source})
+ # 按优先级对结果进行排序
info_list.sort(key=lambda x: x["priority"])
info_map[target] = info_list
return info_map
@@ -491,31 +533,33 @@ class PromptComponentManager:
for name, info in static_components.items():
if not isinstance(info, PromptInfo):
continue
- # 应用 component_name 筛选
+ # 如果指定了 component_name 且不匹配,则跳过此组件
if component_name and name != component_name:
continue
for rule in info.injection_rules:
- # 应用 target_prompt 筛选
+ # 如果指定了 target_prompt 且不匹配,则跳过此规则
if target_prompt and rule.target_prompt != target_prompt:
continue
target_dict = all_rules.setdefault(rule.target_prompt, {})
target_dict[name] = rule
- # 2. 收集并合并所有纯运行时规则
+ # 2. 收集并合并所有运行时规则
async with self._lock:
for target, rules_in_target in self._dynamic_rules.items():
- # 应用 target_prompt 筛选
+ # 如果指定了 target_prompt 且不匹配,则跳过此目标下的所有规则
if target_prompt and target != target_prompt:
continue
for name, (rule, _, _) in rules_in_target.items():
- # 应用 component_name 筛选
+ # 如果指定了 component_name 且不匹配,则跳过此规则
if component_name and name != component_name:
continue
target_dict = all_rules.setdefault(target, {})
+ # 运行时规则会覆盖同名的静态规则
target_dict[name] = rule
+ # 返回深拷贝以防止外部修改影响内部状态
return copy.deepcopy(all_rules)
diff --git a/src/plugin_system/core/component_registry.py b/src/plugin_system/core/component_registry.py
index 6b0a90363..2750cbfc5 100644
--- a/src/plugin_system/core/component_registry.py
+++ b/src/plugin_system/core/component_registry.py
@@ -1,12 +1,16 @@
from __future__ import annotations
+# --- 标准库导入 ---
import re
from pathlib import Path
from re import Pattern
from typing import Any, cast
+# --- 第三方库导入 ---
+import toml
from fastapi import Depends
+# --- 项目内模块导入 ---
from src.common.logger import get_logger
from src.config.config import global_config as bot_config
from src.plugin_system.base.base_action import BaseAction
@@ -33,9 +37,11 @@ from src.plugin_system.base.component_types import (
)
from src.plugin_system.base.plus_command import PlusCommand, create_legacy_command_adapter
+# --- 日志记录器 ---
logger = get_logger("component_registry")
-# 统一的组件类类型别名
+# --- 类型别名 ---
+# 统一的组件类类型别名,方便类型提示
ComponentClassType = (
type[BaseCommand]
| type[BaseAction]
@@ -50,85 +56,115 @@ ComponentClassType = (
def _assign_plugin_attrs(cls: Any, plugin_name: str, plugin_config: dict) -> None:
- """为组件类动态赋予插件相关属性(避免在各注册函数中重复代码)。"""
+ """
+ 为组件类动态赋予插件相关属性。
+
+ 这是一个辅助函数,用于避免在各个注册函数中重复编写相同的属性设置代码。
+
+ Args:
+ cls (Any): 需要设置属性的组件类。
+ plugin_name (str): 插件的名称。
+ plugin_config (dict): 插件的配置信息。
+ """
setattr(cls, "plugin_name", plugin_name)
setattr(cls, "plugin_config", plugin_config)
class ComponentRegistry:
- """统一的组件注册中心
+ """
+ 统一的组件注册中心。
- 负责管理所有插件组件的注册、查询和生命周期管理
+ 该类是插件系统的核心,负责管理所有插件组件的注册、发现、状态管理和生命周期。
+ 它为不同类型的组件(如 Command, Action, Tool 等)提供了统一的注册接口和专门的查询方法。
+
+ 主要职责:
+ - 注册和取消注册插件与组件。
+ - 按类型和名称存储和索引所有组件。
+ - 管理组件的全局启用/禁用状态。
+ - 支持会话级别的组件局部(临时)启用/禁用状态。
+ - 提供按类型、名称或状态查询组件的方法。
+ - 处理组件之间的依赖和冲突。
+ - 动态加载和管理 MCP (Model-Copilot-Plugin) 工具。
"""
def __init__(self):
- # 命名空间式组件名构成法 f"{component_type}.{component_name}"
+ """初始化组件注册中心,创建所有必要的注册表。"""
+ # --- 通用注册表 ---
+ # 核心注册表,存储所有组件的信息,使用命名空间式键 f"{component_type}.{component_name}"
self._components: dict[str, "ComponentInfo"] = {}
- """组件注册表 命名空间式组件名 -> 组件信息"""
+ # 按类型分类的组件注册表,方便按类型快速查找
self._components_by_type: dict["ComponentType", dict[str, "ComponentInfo"]] = {
types: {} for types in ComponentType
}
- """类型 -> 组件原名称 -> 组件信息"""
- # 组件类注册表(命名空间式组件名 -> 组件类)
+ # 存储组件类本身,用于实例化
self._components_classes: dict[str, ComponentClassType] = {}
- """命名空间式组件名 -> 组件类"""
- # 插件注册表
+ # --- 插件注册表 ---
self._plugins: dict[str, "PluginInfo"] = {}
- """插件名 -> 插件信息"""
- # Action特定注册表
+ # --- 特定类型组件的专用注册表 ---
+
+ # Action
self._action_registry: dict[str, type["BaseAction"]] = {}
- """Action注册表 action名 -> action类"""
- self._default_actions: dict[str, "ActionInfo"] = {}
- """默认动作集,即启用的Action集,用于重置ActionManager状态"""
+ self._default_actions: dict[str, "ActionInfo"] = {} # 存储全局启用的Action
- # Command特定注册表
+ # Command (旧版)
self._command_registry: dict[str, type["BaseCommand"]] = {}
- """Command类注册表 command名 -> command类"""
- self._command_patterns: dict[Pattern, str] = {}
- """编译后的正则 -> command名"""
+ self._command_patterns: dict[Pattern, str] = {} # 编译后的正则表达式 -> command名
- # 工具特定注册表
- self._tool_registry: dict[str, type["BaseTool"]] = {} # 工具名 -> 工具类
- self._llm_available_tools: dict[str, type["BaseTool"]] = {} # llm可用的工具名 -> 工具类
+ # PlusCommand (新版)
+ self._plus_command_registry: dict[str, type[PlusCommand]] = {}
- # MCP 工具注册表(运行时动态加载)
- self._mcp_tools: list[Any] = [] # MCP 工具适配器实例列表
- self._mcp_tools_loaded = False # MCP 工具是否已加载
+ # Tool
+ self._tool_registry: dict[str, type["BaseTool"]] = {}
+ self._llm_available_tools: dict[str, type["BaseTool"]] = {} # 存储全局启用的Tool
- # EventHandler特定注册表
+ # EventHandler
self._event_handler_registry: dict[str, type["BaseEventHandler"]] = {}
- """event_handler名 -> event_handler类"""
self._enabled_event_handlers: dict[str, type["BaseEventHandler"]] = {}
- """启用的事件处理器 event_handler名 -> event_handler类"""
+ # Chatter
self._chatter_registry: dict[str, type["BaseChatter"]] = {}
- """chatter名 -> chatter类"""
self._enabled_chatter_registry: dict[str, type["BaseChatter"]] = {}
- """启用的chatter名 -> chatter类"""
- # 局部组件状态管理器,用于临时覆盖
+
+ # InterestCalculator
+ self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
+ self._enabled_interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
+
+ # Prompt
+ self._prompt_registry: dict[str, type[BasePrompt]] = {}
+ self._enabled_prompt_registry: dict[str, type[BasePrompt]] = {}
+
+ # MCP (Model-Copilot-Plugin) Tools
+ self._mcp_tools: list[Any] = [] # 存储 MCP 工具适配器实例
+ self._mcp_tools_loaded = False # 标记 MCP 工具是否已加载
+
+ # --- 状态管理 ---
+ # 局部组件状态管理器,用于在特定会话中临时覆盖全局状态
self._local_component_states: dict[str, dict[tuple[str, ComponentType], bool]] = {}
- """stream_id -> {(component_name, component_type): enabled_status}"""
- logger.info("组件注册中心初始化完成")
+ # 定义不支持局部状态管理的组件类型集合
self._no_local_state_types: set[ComponentType] = {
ComponentType.ROUTER,
ComponentType.EVENT_HANDLER,
- ComponentType.ROUTER,
ComponentType.PROMPT,
# 根据设计,COMMAND 和 PLUS_COMMAND 也不应支持局部状态
}
- # == 注册方法 ==
+ logger.info("组件注册中心初始化完成")
+
+ # =================================================================
+ # == 注册与卸载方法 (Registration and Uninstallation Methods)
+ # =================================================================
def register_plugin(self, plugin_info: PluginInfo) -> bool:
- """注册插件
+ """
+ 注册一个插件。
Args:
- plugin_info: 插件信息
+ plugin_info (PluginInfo): 包含插件元数据的信息对象。
Returns:
- bool: 是否注册成功
+ bool: 如果插件是新的并成功注册,则返回 True;如果插件已存在,则返回 False。
"""
plugin_name = plugin_info.name
@@ -143,20 +179,25 @@ class ComponentRegistry:
def register_component(
self, self_component_info: ComponentInfo, component_class: ComponentClassType
) -> bool:
- """注册组件
+ """
+ 注册一个组件。
+
+ 这是所有组件注册的统一入口点。它会验证组件信息,然后根据组件类型分发到
+ 特定的内部注册方法。
Args:
- component_info (ComponentInfo): 组件信息
- component_class (Type[Union[BaseCommand, BaseAction, BaseEventHandler]]): 组件类
+ component_info (ComponentInfo): 组件的元数据信息。
+ component_class (ComponentClassType): 组件的类定义。
Returns:
- bool: 是否注册成功
+ bool: 注册成功返回 True,否则返回 False。
"""
- component_info = self_component_info # 局部别名
+ component_info = self_component_info # 创建局部别名以缩短行长
component_name = component_info.name
component_type = component_info.component_type
plugin_name = getattr(component_info, "plugin_name", "unknown")
+ # --- 名称合法性检查 ---
if "." in component_name:
logger.error(f"组件名称 '{component_name}' 包含非法字符 '.',请使用下划线替代")
return False
@@ -164,968 +205,82 @@ class ComponentRegistry:
logger.error(f"插件名称 '{plugin_name}' 包含非法字符 '.',请使用下划线替代")
return False
+ # --- 冲突检查 ---
namespaced_name = f"{component_type.value}.{component_name}"
if namespaced_name in self._components:
existing_info = self._components[namespaced_name]
existing_plugin = getattr(existing_info, "plugin_name", "unknown")
logger.warning(
- f"组件名冲突: '{plugin_name}' 插件的 {component_type} 类型组件 '{component_name}' 已被插件 '{existing_plugin}' 注册,跳过此组件注册"
+ f"组件名冲突: '{plugin_name}' 插件的 {component_type} 类型组件 '{component_name}' "
+ f"已被插件 '{existing_plugin}' 注册,跳过此组件注册"
)
return False
+ # --- 通用注册 ---
self._components[namespaced_name] = component_info
self._components_by_type[component_type][component_name] = component_info
self._components_classes[namespaced_name] = component_class
+ # --- 按类型分发到特定注册方法 ---
+ ret = False # 初始化返回值为 False
match component_type:
case ComponentType.ACTION:
- assert isinstance(component_info, ActionInfo)
- assert issubclass(component_class, BaseAction)
+ assert isinstance(component_info, ActionInfo) and issubclass(component_class, BaseAction)
ret = self._register_action_component(component_info, component_class)
case ComponentType.COMMAND:
- assert isinstance(component_info, CommandInfo)
- assert issubclass(component_class, BaseCommand)
+ assert isinstance(component_info, CommandInfo) and issubclass(component_class, BaseCommand)
ret = self._register_command_component(component_info, component_class)
case ComponentType.PLUS_COMMAND:
- assert isinstance(component_info, PlusCommandInfo)
- assert issubclass(component_class, PlusCommand)
+ assert isinstance(component_info, PlusCommandInfo) and issubclass(component_class, PlusCommand)
ret = self._register_plus_command_component(component_info, component_class)
case ComponentType.TOOL:
- assert isinstance(component_info, ToolInfo)
- assert issubclass(component_class, BaseTool)
+ assert isinstance(component_info, ToolInfo) and issubclass(component_class, BaseTool)
ret = self._register_tool_component(component_info, component_class)
case ComponentType.EVENT_HANDLER:
- assert isinstance(component_info, EventHandlerInfo)
- assert issubclass(component_class, BaseEventHandler)
+ assert isinstance(component_info, EventHandlerInfo) and issubclass(component_class, BaseEventHandler)
ret = self._register_event_handler_component(component_info, component_class)
case ComponentType.CHATTER:
- assert isinstance(component_info, ChatterInfo)
- assert issubclass(component_class, BaseChatter)
+ assert isinstance(component_info, ChatterInfo) and issubclass(component_class, BaseChatter)
ret = self._register_chatter_component(component_info, component_class)
case ComponentType.INTEREST_CALCULATOR:
- assert isinstance(component_info, InterestCalculatorInfo)
- assert issubclass(component_class, BaseInterestCalculator)
+ assert isinstance(component_info, InterestCalculatorInfo) and issubclass(
+ component_class, BaseInterestCalculator
+ )
ret = self._register_interest_calculator_component(component_info, component_class)
case ComponentType.PROMPT:
- assert isinstance(component_info, PromptInfo)
- assert issubclass(component_class, BasePrompt)
+ assert isinstance(component_info, PromptInfo) and issubclass(component_class, BasePrompt)
ret = self._register_prompt_component(component_info, component_class)
case ComponentType.ROUTER:
- assert isinstance(component_info, RouterInfo)
- assert issubclass(component_class, BaseRouterComponent)
+ assert isinstance(component_info, RouterInfo) and issubclass(component_class, BaseRouterComponent)
ret = self._register_router_component(component_info, component_class)
case _:
logger.warning(f"未知组件类型: {component_type}")
ret = False
if not ret:
- return False
- logger.debug(
- f"已注册{component_type}组件: '{component_name}' -> '{namespaced_name}' ({component_class.__name__}) [插件: {plugin_name}]"
- )
- return True
-
- def _register_action_component(self, action_info: ActionInfo, action_class: type[BaseAction]) -> bool:
- """注册Action组件到Action特定注册表"""
- if not (action_name := action_info.name):
- logger.error(f"Action组件 {action_class.__name__} 必须指定名称")
- return False
- if not isinstance(action_info, ActionInfo) or not issubclass(action_class, BaseAction):
- logger.error(f"注册失败: {action_name} 不是有效的Action")
- return False
- _assign_plugin_attrs(action_class, action_info.plugin_name, self.get_plugin_config(action_info.plugin_name) or {})
- self._action_registry[action_name] = action_class
- if action_info.enabled:
- self._default_actions[action_name] = action_info
- return True
-
- def _register_command_component(self, command_info: CommandInfo, command_class: type[BaseCommand]) -> bool:
- """注册Command组件到Command特定注册表"""
- logger.warning(
- f"检测到旧版Command组件 '{command_class.command_name}' (来自插件: {command_info.plugin_name})。"
- "它将通过兼容层运行,但建议尽快迁移到PlusCommand以获得更好的性能和功能。"
- )
- # 使用适配器将其转换为PlusCommand
- adapted_class = create_legacy_command_adapter(command_class)
- plus_command_info = adapted_class.get_plus_command_info()
- plus_command_info.plugin_name = command_info.plugin_name # 继承插件名
-
- return self._register_plus_command_component(plus_command_info, adapted_class)
-
- def _register_plus_command_component(
- self, plus_command_info: PlusCommandInfo, plus_command_class: type[PlusCommand]
- ) -> bool:
- """注册PlusCommand组件到特定注册表"""
- plus_command_name = plus_command_info.name
-
- if not plus_command_name:
- logger.error(f"PlusCommand组件 {plus_command_class.__name__} 必须指定名称")
- return False
- if not isinstance(plus_command_info, PlusCommandInfo) or not issubclass(plus_command_class, PlusCommand):
- logger.error(f"注册失败: {plus_command_name} 不是有效的PlusCommand")
- return False
-
- # 创建专门的PlusCommand注册表(如果还没有)
- if not hasattr(self, "_plus_command_registry"):
- self._plus_command_registry: dict[str, type[PlusCommand]] = {}
- _assign_plugin_attrs(
- plus_command_class,
- plus_command_info.plugin_name,
- self.get_plugin_config(plus_command_info.plugin_name) or {},
- )
- self._plus_command_registry[plus_command_name] = plus_command_class
- logger.debug(f"已注册PlusCommand组件: {plus_command_name}")
- return True
-
- def _register_tool_component(self, tool_info: ToolInfo, tool_class: type[BaseTool]) -> bool:
- """注册Tool组件到Tool特定注册表"""
- tool_name = tool_info.name
- _assign_plugin_attrs(tool_class, tool_info.plugin_name, self.get_plugin_config(tool_info.plugin_name) or {})
- self._tool_registry[tool_name] = tool_class
- if tool_info.enabled:
- self._llm_available_tools[tool_name] = tool_class
- return True
-
- def _register_event_handler_component(
- self, handler_info: EventHandlerInfo, handler_class: type[BaseEventHandler]
- ) -> bool:
- if not (handler_name := handler_info.name):
- logger.error(f"EventHandler组件 {handler_class.__name__} 必须指定名称")
- return False
- if not isinstance(handler_info, EventHandlerInfo) or not issubclass(handler_class, BaseEventHandler):
- logger.error(f"注册失败: {handler_name} 不是有效的EventHandler")
- return False
- _assign_plugin_attrs(
- handler_class, handler_info.plugin_name, self.get_plugin_config(handler_info.plugin_name) or {}
- )
- self._event_handler_registry[handler_name] = handler_class
- if not handler_info.enabled:
- logger.warning(f"EventHandler组件 {handler_name} 未启用")
- return True # 未启用,但是也是注册成功
- from src.plugin_system.core.event_manager import event_manager
- return event_manager.register_event_handler(
- handler_class, self.get_plugin_config(handler_info.plugin_name) or {}
- )
-
- def _register_chatter_component(self, chatter_info: ChatterInfo, chatter_class: type[BaseChatter]) -> bool:
- """注册Chatter组件到Chatter特定注册表"""
- chatter_name = chatter_info.name
-
- if not chatter_name:
- logger.error(f"Chatter组件 {chatter_class.__name__} 必须指定名称")
- return False
- if not isinstance(chatter_info, ChatterInfo) or not issubclass(chatter_class, BaseChatter):
- logger.error(f"注册失败: {chatter_name} 不是有效的Chatter")
- return False
- _assign_plugin_attrs(
- chatter_class, chatter_info.plugin_name, self.get_plugin_config(chatter_info.plugin_name) or {}
- )
- self._chatter_registry[chatter_name] = chatter_class
- if not chatter_info.enabled:
- logger.warning(f"Chatter组件 {chatter_name} 未启用")
- return True # 未启用,但是也是注册成功
- self._enabled_chatter_registry[chatter_name] = chatter_class
- logger.debug(f"已注册Chatter组件: {chatter_name}")
- return True
-
- def _register_interest_calculator_component(
- self,
- interest_calculator_info: "InterestCalculatorInfo",
- interest_calculator_class: type["BaseInterestCalculator"],
- ) -> bool:
- """注册InterestCalculator组件到特定注册表"""
- calculator_name = interest_calculator_info.name
-
- if not calculator_name:
- logger.error(f"InterestCalculator组件 {interest_calculator_class.__name__} 必须指定名称")
- return False
- if not isinstance(interest_calculator_info, InterestCalculatorInfo) or not issubclass(
- interest_calculator_class, BaseInterestCalculator
- ):
- logger.error(f"注册失败: {calculator_name} 不是有效的InterestCalculator")
- return False
-
- # 创建专门的InterestCalculator注册表(如果还没有)
- if not hasattr(self, "_interest_calculator_registry"):
- self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
- if not hasattr(self, "_enabled_interest_calculator_registry"):
- self._enabled_interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
-
- setattr(interest_calculator_class, "plugin_name", interest_calculator_info.plugin_name)
- # 设置插件配置
- setattr(
- interest_calculator_class,
- "plugin_config",
- self.get_plugin_config(interest_calculator_info.plugin_name) or {},
- )
- self._interest_calculator_registry[calculator_name] = interest_calculator_class
-
- if not interest_calculator_info.enabled:
- logger.warning(f"InterestCalculator组件 {calculator_name} 未启用")
- return True # 未启用,但是也是注册成功
- self._enabled_interest_calculator_registry[calculator_name] = interest_calculator_class
-
- logger.debug(f"已注册InterestCalculator组件: {calculator_name}")
- return True
-
- def _register_prompt_component(
- self, prompt_info: PromptInfo, prompt_class: "ComponentClassType"
- ) -> bool:
- """注册Prompt组件到Prompt特定注册表"""
- prompt_name = prompt_info.name
- if not prompt_name:
- logger.error(f"Prompt组件 {prompt_class.__name__} 必须指定名称")
- return False
-
- if not hasattr(self, "_prompt_registry"):
- self._prompt_registry: dict[str, type[BasePrompt]] = {}
- if not hasattr(self, "_enabled_prompt_registry"):
- self._enabled_prompt_registry: dict[str, type[BasePrompt]] = {}
-
- _assign_plugin_attrs(
- prompt_class, prompt_info.plugin_name, self.get_plugin_config(prompt_info.plugin_name) or {}
- )
- self._prompt_registry[prompt_name] = prompt_class # type: ignore
-
- if prompt_info.enabled:
- self._enabled_prompt_registry[prompt_name] = prompt_class # type: ignore
-
- logger.debug(f"已注册Prompt组件: {prompt_name}")
- return True
-
- def _register_router_component(self, router_info: RouterInfo, router_class: type[BaseRouterComponent]) -> bool:
- """注册Router组件并将其端点挂载到主服务器"""
- # 1. 检查总开关是否开启
- if not bot_config.plugin_http_system.enable_plugin_http_endpoints:
- logger.info("插件HTTP端点功能已禁用,跳过路由注册")
- return True
- try:
- from src.common.server import get_global_server
-
- router_name = router_info.name
- plugin_name = router_info.plugin_name
-
- # 2. 实例化组件以触发其 __init__ 和 register_endpoints
- component_instance = router_class()
-
- # 3. 获取配置好的 APIRouter
- plugin_router = component_instance.router
-
- # 4. 获取全局服务器实例
- server = get_global_server()
-
- # 5. 生成唯一的URL前缀
- prefix = f"/plugins/{plugin_name}"
-
- # 6. 注册路由,并使用插件名作为API文档的分组标签
- # 移除了dependencies参数,因为现在由每个端点自行决定是否需要验证
- server.app.include_router(
- plugin_router, prefix=prefix, tags=[plugin_name]
- )
-
- logger.debug(f"成功将插件 '{plugin_name}' 的路由组件 '{router_name}' 挂载到: {prefix}")
- return True
-
- except Exception as e:
- logger.error(f"注册路由组件 '{router_info.name}' 时出错: {e}", exc_info=True)
- return False
-
- # === 组件移除相关 ===
-
- async def remove_component(self, component_name: str, component_type: ComponentType, plugin_name: str) -> bool:
- target_component_class = self.get_component_class(component_name, component_type)
- if not target_component_class:
- logger.warning(f"组件 {component_name} 未注册,无法移除")
- return False
- try:
- # 根据组件类型进行特定的清理操作
- match component_type:
- case ComponentType.ACTION:
- # 移除Action注册
- self._action_registry.pop(component_name, None)
- self._default_actions.pop(component_name, None)
- logger.debug(f"已移除Action组件: {component_name}")
-
- case ComponentType.COMMAND:
- # 移除Command注册和模式
- self._command_registry.pop(component_name, None)
- keys_to_remove = [k for k, v in self._command_patterns.items() if v == component_name]
- for key in keys_to_remove:
- self._command_patterns.pop(key, None)
- logger.debug(f"已移除Command组件: {component_name} (清理了 {len(keys_to_remove)} 个模式)")
-
- case ComponentType.PLUS_COMMAND:
- # 移除PlusCommand注册
- if hasattr(self, "_plus_command_registry"):
- self._plus_command_registry.pop(component_name, None)
- logger.debug(f"已移除PlusCommand组件: {component_name}")
-
- case ComponentType.TOOL:
- # 移除Tool注册
- self._tool_registry.pop(component_name, None)
- self._llm_available_tools.pop(component_name, None)
- logger.debug(f"已移除Tool组件: {component_name}")
-
- case ComponentType.EVENT_HANDLER:
- # 移除EventHandler注册和事件订阅
- from .event_manager import event_manager # 延迟导入防止循环导入问题
- try:
- # 从事件管理器中完全移除事件处理器,包括其所有订阅
- event_manager.remove_event_handler(component_name)
- logger.debug(f"已通过 event_manager 移除EventHandler组件: {component_name}")
- except Exception as e:
- logger.warning(f"移除EventHandler事件订阅时出错: {e}")
-
- case ComponentType.CHATTER:
- # 移除Chatter注册
- if hasattr(self, "_chatter_registry"):
- self._chatter_registry.pop(component_name, None)
- if hasattr(self, "_enabled_chatter_registry"):
- self._enabled_chatter_registry.pop(component_name, None)
- logger.debug(f"已移除Chatter组件: {component_name}")
-
- case ComponentType.INTEREST_CALCULATOR:
- # 移除InterestCalculator注册
- if hasattr(self, "_interest_calculator_registry"):
- self._interest_calculator_registry.pop(component_name, None)
- if hasattr(self, "_enabled_interest_calculator_registry"):
- self._enabled_interest_calculator_registry.pop(component_name, None)
- logger.debug(f"已移除InterestCalculator组件: {component_name}")
-
- case ComponentType.PROMPT:
- # 移除Prompt注册
- if hasattr(self, "_prompt_registry"):
- self._prompt_registry.pop(component_name, None)
- if hasattr(self, "_enabled_prompt_registry"):
- self._enabled_prompt_registry.pop(component_name, None)
- logger.debug(f"已移除Prompt组件: {component_name}")
-
- case ComponentType.ROUTER:
- # Router组件的移除比较复杂,目前只记录日志
- logger.warning(f"Router组件 '{component_name}' 的HTTP端点无法在运行时动态移除,将在下次重启后生效。")
-
- case _:
- logger.warning(f"未知的组件类型: {component_type},无法进行特定的清理操作")
- return False
-
- # 移除通用注册信息
- namespaced_name = f"{component_type.value}.{component_name}"
+ # 如果特定注册失败,回滚通用注册
self._components.pop(namespaced_name, None)
self._components_by_type[component_type].pop(component_name, None)
self._components_classes.pop(namespaced_name, None)
-
- logger.info(f"组件 {component_name} ({component_type}) 已完全移除")
- return True
-
- except Exception as e:
- logger.error(f"移除组件 {component_name} ({component_type}) 时发生错误: {e}")
return False
- def remove_plugin_registry(self, plugin_name: str) -> bool:
- """移除插件注册信息
-
- Args:
- plugin_name: 插件名称
-
- Returns:
- bool: 是否成功移除
- """
- if plugin_name not in self._plugins:
- logger.warning(f"插件 {plugin_name} 未注册,无法移除")
- return False
- del self._plugins[plugin_name]
- logger.info(f"插件 {plugin_name} 已移除")
- return True
-
- # === 组件全局启用/禁用方法 ===
-
- def enable_component(self, component_name: str, component_type: ComponentType) -> bool:
- """全局的启用某个组件
- Parameters:
- component_name: 组件名称
- component_type: 组件类型
- Returns:
- bool: 启用成功返回True,失败返回False
- """
- target_component_class = self.get_component_class(component_name, component_type)
- target_component_info = self.get_component_info(component_name, component_type)
- if not target_component_class or not target_component_info:
- logger.warning(f"组件 {component_name} 未注册,无法启用")
- return False
- target_component_info.enabled = True
- match component_type:
- case ComponentType.ACTION:
- assert isinstance(target_component_info, ActionInfo)
- self._default_actions[component_name] = target_component_info
- case ComponentType.COMMAND:
- assert isinstance(target_component_info, CommandInfo)
- pattern = target_component_info.command_pattern
- self._command_patterns[re.compile(pattern)] = component_name
- case ComponentType.TOOL:
- assert isinstance(target_component_info, ToolInfo)
- assert issubclass(target_component_class, BaseTool)
- self._llm_available_tools[component_name] = target_component_class
- case ComponentType.EVENT_HANDLER:
- assert isinstance(target_component_info, EventHandlerInfo)
- assert issubclass(target_component_class, BaseEventHandler)
- self._enabled_event_handlers[component_name] = target_component_class
- from .event_manager import event_manager # 延迟导入防止循环导入问题
-
- # 重新注册事件处理器(启用)使用类而不是名称
- cfg = self.get_plugin_config(target_component_info.plugin_name) or {}
- event_manager.register_event_handler(target_component_class, cfg) # type: ignore[arg-type]
- namespaced_name = f"{component_type.value}.{component_name}"
- self._components[namespaced_name].enabled = True
- self._components_by_type[component_type][component_name].enabled = True
- logger.info(f"组件 {component_name} 已启用")
- return True
-
- async def disable_component(self, component_name: str, component_type: ComponentType) -> bool:
- """全局的禁用某个组件
- Parameters:
- component_name: 组件名称
- component_type: 组件类型
- Returns:
- bool: 禁用成功返回True,失败返回False
- """
- target_component_class = self.get_component_class(component_name, component_type)
- target_component_info = self.get_component_info(component_name, component_type)
- if not target_component_class or not target_component_info:
- logger.warning(f"组件 {component_name} 未注册,无法禁用")
- return False
- target_component_info.enabled = False
- try:
- match component_type:
- case ComponentType.ACTION:
- self._default_actions.pop(component_name)
- case ComponentType.COMMAND:
- self._command_patterns = {k: v for k, v in self._command_patterns.items() if v != component_name}
- case ComponentType.TOOL:
- self._llm_available_tools.pop(component_name)
- case ComponentType.EVENT_HANDLER:
- self._enabled_event_handlers.pop(component_name)
- from .event_manager import event_manager # 延迟导入防止循环导入问题
-
- handler = event_manager.get_event_handler(component_name)
- if handler and hasattr(handler, "subscribed_events"):
- for event in getattr(handler, "subscribed_events"):
- result = event_manager.unsubscribe_handler_from_event(event, component_name)
- if hasattr(result, "__await__"):
- await result # type: ignore[func-returns-value]
- case ComponentType.PROMPT:
- if hasattr(self, "_enabled_prompt_registry"):
- self._enabled_prompt_registry.pop(component_name, None)
-
- # 组件主注册表使用命名空间 key
- namespaced_name = f"{component_type.value}.{component_name}"
- if namespaced_name in self._components:
- self._components[namespaced_name].enabled = False
- self._components_by_type[component_type][component_name].enabled = False
- logger.info(f"组件 {component_name} 已禁用")
- return True
- except KeyError as e:
- logger.warning(f"禁用组件时未找到组件或已禁用: {component_name}, 发生错误: {e}")
- return False
- except Exception as e:
- logger.error(f"禁用组件 {component_name} 时发生错误: {e}")
- return False
-
- # === 组件查询方法 ===
- def get_component_info(
- self, component_name: str, component_type: ComponentType | None = None
- ) -> ComponentInfo | None:
- # sourcery skip: class-extract-method
- """获取组件信息,支持自动命名空间解析
-
- Args:
- component_name: 组件名称,可以是原始名称或命名空间化的名称
- component_type: 组件类型,如果提供则优先在该类型中查找
-
- Returns:
- Optional[ComponentInfo]: 组件信息或None
- """
- # 1. 如果已经是命名空间化的名称,直接查找
- if "." in component_name:
- return self._components.get(component_name)
-
- # 2. 如果指定了组件类型,构造命名空间化的名称查找
- if component_type:
- namespaced_name = f"{component_type.value}.{component_name}"
- return self._components.get(namespaced_name)
-
- # 3. 如果没有指定类型,尝试在所有命名空间中查找
- candidates = []
- for namespace_prefix in [types.value for types in ComponentType]:
- namespaced_name = f"{namespace_prefix}.{component_name}"
- if component_info := self._components.get(namespaced_name):
- candidates.append((namespace_prefix, namespaced_name, component_info))
-
- if len(candidates) == 1:
- # 只有一个匹配,直接返回
- return candidates[0][2]
- elif len(candidates) > 1:
- # 多个匹配,记录警告并返回第一个
- namespaces = [ns for ns, _, _ in candidates]
- logger.warning(
- f"组件名称 '{component_name}' 在多个命名空间中存在: {namespaces},使用第一个匹配项: {candidates[0][1]}"
- )
- return candidates[0][2]
-
- # 4. 都没找到
- return None
-
- def get_component_class(
- self,
- component_name: str,
- component_type: ComponentType | None = None,
- ) -> (
- type[
- BaseCommand
- | BaseAction
- | BaseEventHandler
- | BaseTool
- | PlusCommand
- | BaseChatter
- | BaseInterestCalculator
- | BasePrompt
- | BaseRouterComponent
- ]
- | None
- ):
- """获取组件类,支持自动命名空间解析
-
- Args:
- component_name: 组件名称,可以是原始名称或命名空间化的名称
- component_type: 组件类型,如果提供则优先在该类型中查找
-
- Returns:
- Optional[Union[BaseCommand, BaseAction]]: 组件类或None
- """
- # 1. 如果已经是命名空间化的名称,直接查找
- if "." in component_name:
- return self._components_classes.get(component_name)
-
- # 2. 如果指定了组件类型,构造命名空间化的名称查找
- if component_type:
- namespaced_name = f"{component_type.value}.{component_name}"
- return cast(
- type[BaseCommand]
- | type[BaseAction]
- | type[BaseEventHandler]
- | type[BaseTool]
- | type[PlusCommand]
- | type[BaseChatter]
- | type[BaseInterestCalculator]
- | type[BasePrompt]
- | type[BaseRouterComponent]
- | None,
- self._components_classes.get(namespaced_name),
- )
-
- # 3. 如果没有指定类型,尝试在所有命名空间中查找
- candidates = []
- for namespace_prefix in [types.value for types in ComponentType]:
- namespaced_name = f"{namespace_prefix}.{component_name}"
- if component_class := self._components_classes.get(namespaced_name):
- candidates.append((namespace_prefix, namespaced_name, component_class))
-
- if len(candidates) == 1:
- # 只有一个匹配,直接返回
- _, full_name, cls = candidates[0]
- logger.debug(f"自动解析组件: '{component_name}' -> '{full_name}'")
- return cls
- elif len(candidates) > 1:
- # 多个匹配,记录警告并返回第一个
- namespaces = [ns for ns, _, _ in candidates]
- logger.warning(
- f"组件名称 '{component_name}' 在多个命名空间中存在: {namespaces},使用第一个匹配项: {candidates[0][1]}"
- )
- return candidates[0][2]
-
- # 4. 都没找到
- return None
-
- def get_components_by_type(self, component_type: ComponentType) -> dict[str, ComponentInfo]:
- """获取指定类型的所有组件"""
- return self._components_by_type.get(component_type, {}).copy()
-
- def get_enabled_components_by_type(
- self, component_type: ComponentType, stream_id: str | None = None
- ) -> dict[str, ComponentInfo]:
- """获取指定类型的所有启用组件, 可选地根据 stream_id 考虑局部状态"""
- components = self.get_components_by_type(component_type)
- return {
- name: info
- for name, info in components.items()
- if self.is_component_available(name, component_type, stream_id)
- }
-
- # === Action特定查询方法 ===
-
- def get_action_registry(self) -> dict[str, type[BaseAction]]:
- """获取Action注册表"""
- return self._action_registry.copy()
-
- def get_registered_action_info(self, action_name: str) -> ActionInfo | None:
- """获取Action信息"""
- info = self.get_component_info(action_name, ComponentType.ACTION)
- return info if isinstance(info, ActionInfo) else None
-
- def get_default_actions(self, stream_id: str | None = None) -> dict[str, ActionInfo]:
- """获取默认(可用)动作集, 可选地根据 stream_id 考虑局部状态"""
- all_actions = self.get_components_by_type(ComponentType.ACTION)
- available_actions = {
- name: info
- for name, info in all_actions.items()
- if self.is_component_available(name, ComponentType.ACTION, stream_id)
- }
- return cast(dict[str, ActionInfo], available_actions)
-
- # === Command特定查询方法 ===
-
- def get_command_registry(self) -> dict[str, type[BaseCommand]]:
- """获取Command注册表"""
- return self._command_registry.copy()
-
- def get_registered_command_info(self, command_name: str) -> CommandInfo | None:
- """获取Command信息"""
- info = self.get_component_info(command_name, ComponentType.COMMAND)
- return info if isinstance(info, CommandInfo) else None
-
- def get_command_patterns(self) -> dict[Pattern, str]:
- """获取Command模式注册表"""
- return self._command_patterns.copy()
-
- def find_command_by_text(self, text: str) -> tuple[type[BaseCommand], dict, CommandInfo] | None:
- # sourcery skip: use-named-expression, use-next
- """根据文本查找匹配的命令
-
- Args:
- text: 输入文本
-
- Returns:
- Tuple: (命令类, 匹配的命名组, 命令信息) 或 None
- """
-
- # 只查找传统的BaseCommand
- candidates = [pattern for pattern in self._command_patterns if pattern.match(text)]
- if candidates:
- if len(candidates) > 1:
- logger.warning(f"文本 '{text}' 匹配到多个命令模式: {candidates},使用第一个匹配")
- command_name = self._command_patterns[candidates[0]]
- command_info: CommandInfo = self.get_registered_command_info(command_name) # type: ignore
- return (
- self._command_registry[command_name],
- candidates[0].match(text).groupdict(), # type: ignore
- command_info,
- )
-
- return None
-
- # === Tool 特定查询方法 ===
- def get_tool_registry(self) -> dict[str, type[BaseTool]]:
- """获取Tool注册表"""
- return self._tool_registry.copy()
-
- def get_llm_available_tools(self, stream_id: str | None = None) -> dict[str, type[BaseTool]]:
- """获取LLM可用的Tool列表, 可选地根据 stream_id 考虑局部状态"""
- all_tools = self.get_tool_registry()
- available_tools = {}
- for name, tool_class in all_tools.items():
- if self.is_component_available(name, ComponentType.TOOL, stream_id):
- available_tools[name] = tool_class
- return available_tools
-
- def get_registered_tool_info(self, tool_name: str) -> ToolInfo | None:
- """获取Tool信息
-
- Args:
- tool_name: 工具名称
-
- Returns:
- ToolInfo: 工具信息对象,如果工具不存在则返回 None
- """
- info = self.get_component_info(tool_name, ComponentType.TOOL)
- return info if isinstance(info, ToolInfo) else None
-
- # === PlusCommand 特定查询方法 ===
- def get_plus_command_registry(self) -> dict[str, type[PlusCommand]]:
- """获取PlusCommand注册表"""
- if not hasattr(self, "_plus_command_registry"):
- self._plus_command_registry: dict[str, type[PlusCommand]] = {}
- return self._plus_command_registry.copy()
-
- def get_registered_plus_command_info(self, command_name: str) -> PlusCommandInfo | None:
- """获取PlusCommand信息
-
- Args:
- command_name: 命令名称
-
- Returns:
- PlusCommandInfo: 命令信息对象,如果命令不存在则返回 None
- """
- info = self.get_component_info(command_name, ComponentType.PLUS_COMMAND)
- return info if isinstance(info, PlusCommandInfo) else None
-
- def get_available_plus_commands_info(self, stream_id: str | None = None) -> dict[str, PlusCommandInfo]:
- """获取在指定上下文中所有可用的PlusCommand信息
-
- Args:
- stream_id: 可选的流ID,用于检查局部组件状态
-
- Returns:
- 一个字典,键是命令名,值是 PlusCommandInfo 对象
- """
- all_plus_commands = self.get_components_by_type(ComponentType.PLUS_COMMAND)
- available_commands = {
- name: info
- for name, info in all_plus_commands.items()
- if self.is_component_available(name, ComponentType.PLUS_COMMAND, stream_id)
- }
- return cast(dict[str, PlusCommandInfo], available_commands)
-
- # === EventHandler 特定查询方法 ===
-
- def get_event_handler_registry(self) -> dict[str, type[BaseEventHandler]]:
- """获取事件处理器注册表"""
- return self._event_handler_registry.copy()
-
- def get_registered_event_handler_info(self, handler_name: str) -> EventHandlerInfo | None:
- """获取事件处理器信息"""
- info = self.get_component_info(handler_name, ComponentType.EVENT_HANDLER)
- return info if isinstance(info, EventHandlerInfo) else None
-
- def get_enabled_event_handlers(self) -> dict[str, type[BaseEventHandler]]:
- """获取启用的事件处理器"""
- if not hasattr(self, "_enabled_event_handlers"):
- self._enabled_event_handlers: dict[str, type["BaseEventHandler"]] = {}
- return self._enabled_event_handlers.copy()
-
- # === Chatter 特定查询方法 ===
- def get_chatter_registry(self) -> dict[str, type[BaseChatter]]:
- """获取Chatter注册表"""
- if not hasattr(self, "_chatter_registry"):
- self._chatter_registry: dict[str, type[BaseChatter]] = {}
- return self._chatter_registry.copy()
-
- def get_enabled_chatter_registry(self, stream_id: str | None = None) -> dict[str, type[BaseChatter]]:
- """获取启用的Chatter注册表, 可选地根据 stream_id 考虑局部状态"""
- all_chatters = self.get_chatter_registry()
- available_chatters = {}
- for name, chatter_class in all_chatters.items():
- if self.is_component_available(name, ComponentType.CHATTER, stream_id):
- available_chatters[name] = chatter_class
- return available_chatters
-
- def get_registered_chatter_info(self, chatter_name: str) -> ChatterInfo | None:
- """获取Chatter信息"""
- info = self.get_component_info(chatter_name, ComponentType.CHATTER)
- return info if isinstance(info, ChatterInfo) else None
-
- # === Prompt 特定查询方法 ===
- def get_prompt_registry(self) -> dict[str, type[BasePrompt]]:
- """获取Prompt注册表"""
- if not hasattr(self, "_prompt_registry"):
- self._prompt_registry: dict[str, type[BasePrompt]] = {}
- return self._prompt_registry.copy()
-
- def get_enabled_prompt_registry(self) -> dict[str, type[BasePrompt]]:
- """获取启用的Prompt注册表"""
- if not hasattr(self, "_enabled_prompt_registry"):
- self._enabled_prompt_registry: dict[str, type[BasePrompt]] = {}
- return self._enabled_prompt_registry.copy()
-
- def get_registered_prompt_info(self, prompt_name: str) -> PromptInfo | None:
- """获取Prompt信息"""
- info = self.get_component_info(prompt_name, ComponentType.PROMPT)
- return info if isinstance(info, PromptInfo) else None
-
- # === 插件查询方法 ===
-
- def get_plugin_info(self, plugin_name: str) -> PluginInfo | None:
- """获取插件信息"""
- return self._plugins.get(plugin_name)
-
- def get_all_plugins(self) -> dict[str, PluginInfo]:
- """获取所有插件"""
- return self._plugins.copy()
-
- # def get_enabled_plugins(self) -> Dict[str, PluginInfo]:
- # """获取所有启用的插件"""
- # return {name: info for name, info in self._plugins.items() if info.enabled}
-
- def get_plugin_components(self, plugin_name: str) -> list["ComponentInfo"]:
- """获取插件的所有组件"""
- plugin_info = self.get_plugin_info(plugin_name)
- if plugin_info:
- # 记录日志时,将组件列表转换为可读的字符串,避免类型错误
- component_names = [c.name for c in plugin_info.components]
- logger.debug(f"获取到插件 '{plugin_name}' 的组件: {component_names}")
- return plugin_info.components
- return []
-
- def get_plugin_config(self, plugin_name: str) -> dict:
- """获取插件配置
-
- Args:
- plugin_name: 插件名称
-
- Returns:
- dict: 插件配置字典,如果插件实例不存在或配置为空,返回空字典
- """
- # 从插件管理器获取插件实例的配置
- from src.plugin_system.core.plugin_manager import plugin_manager
-
- plugin_instance = plugin_manager.get_plugin_instance(plugin_name)
- if plugin_instance and plugin_instance.config:
- return plugin_instance.config
-
- # 如果插件实例不存在,尝试从配置文件读取
- try:
- import toml
-
- config_path = Path("config") / "plugins" / plugin_name / "config.toml"
- if config_path.exists():
- with open(config_path, encoding="utf-8") as f:
- config_data = toml.load(f)
- logger.debug(f"从配置文件读取插件 {plugin_name} 的配置")
- return config_data
- except Exception as e:
- logger.debug(f"读取插件 {plugin_name} 配置文件失败: {e}")
-
- return {}
-
- def get_registry_stats(self) -> dict[str, Any]:
- """获取注册中心统计信息"""
- action_components: int = 0
- command_components: int = 0
- tool_components: int = 0
- events_handlers: int = 0
- plus_command_components: int = 0
- chatter_components: int = 0
- prompt_components: int = 0
- router_components: int = 0
- for component in self._components.values():
- if component.component_type == ComponentType.ACTION:
- action_components += 1
- elif component.component_type == ComponentType.COMMAND:
- command_components += 1
- elif component.component_type == ComponentType.TOOL:
- tool_components += 1
- elif component.component_type == ComponentType.EVENT_HANDLER:
- events_handlers += 1
- elif component.component_type == ComponentType.PLUS_COMMAND:
- plus_command_components += 1
- elif component.component_type == ComponentType.CHATTER:
- chatter_components += 1
- elif component.component_type == ComponentType.PROMPT:
- prompt_components += 1
- elif component.component_type == ComponentType.ROUTER:
- router_components += 1
- return {
- "action_components": action_components,
- "command_components": command_components,
- "tool_components": tool_components,
- "mcp_tools": len(self._mcp_tools),
- "event_handlers": events_handlers,
- "plus_command_components": plus_command_components,
- "chatter_components": chatter_components,
- "prompt_components": prompt_components,
- "router_components": router_components,
- "total_components": len(self._components),
- "total_plugins": len(self._plugins),
- "components_by_type": {
- component_type.value: len(components) for component_type, components in self._components_by_type.items()
- },
- "enabled_components": len([c for c in self._components.values() if c.enabled]),
- }
-
- # === 局部状态管理 ===
- def set_local_component_state(
- self, stream_id: str, component_name: str, component_type: ComponentType, enabled: bool
- ) -> bool:
- """为指定的 stream_id 设置组件的局部(临时)状态"""
- # 如果组件类型不需要局部状态管理,则记录警告并返回
- if component_type in self._no_local_state_types:
- logger.warning(
- f"组件类型 {component_type.value} 不支持局部状态管理。 "
- f"尝试为 '{component_name}' 设置局部状态的操作将被忽略。"
- )
- return False
-
- if stream_id not in self._local_component_states:
- self._local_component_states[stream_id] = {}
-
- state_key = (component_name, component_type)
- self._local_component_states[stream_id][state_key] = enabled
logger.debug(
- f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type}) -> {'启用' if enabled else '禁用'}"
+ f"已注册{component_type}组件: '{component_name}' -> '{namespaced_name}' "
+ f"({component_class.__name__}) [插件: {plugin_name}]"
)
return True
- def is_component_available(
- self, component_name: str, component_type: ComponentType, stream_id: str | None = None
- ) -> bool:
- """检查组件在给定上下文中是否可用(同时考虑全局和局部状态)"""
- component_info = self.get_component_info(component_name, component_type)
-
- # 1. 检查组件是否存在
- if not component_info:
- return False
-
- # 2. 如果组件类型不需要局部状态,则直接返回其全局状态
- if component_type in self._no_local_state_types:
- return component_info.enabled
-
- # 3. 如果提供了 stream_id,检查局部状态
- if stream_id and stream_id in self._local_component_states:
- state_key = (component_name, component_type)
- local_state = self._local_component_states[stream_id].get(state_key)
-
- if local_state is not None:
- return local_state # 局部状态存在,覆盖全局状态
-
- # 4. 如果没有局部状态覆盖,则返回全局状态
- return component_info.enabled
-
- # === MCP 工具相关方法 ===
-
- async def load_mcp_tools(self) -> None:
- """加载 MCP 工具(异步方法)"""
- if self._mcp_tools_loaded:
- logger.debug("MCP 工具已加载,跳过")
- return
-
- try:
- from .mcp_tool_adapter import load_mcp_tools_as_adapters
-
- logger.info("开始加载 MCP 工具...")
- self._mcp_tools = await load_mcp_tools_as_adapters()
- self._mcp_tools_loaded = True
- logger.info(f"MCP 工具加载完成,共 {len(self._mcp_tools)} 个工具")
- except Exception as e:
- logger.error(f"加载 MCP 工具失败: {e}")
- self._mcp_tools = []
- self._mcp_tools_loaded = True # 标记为已尝试加载,避免重复尝试
-
- def get_mcp_tools(self) -> list["BaseTool"]:
- """获取所有 MCP 工具适配器实例"""
- return self._mcp_tools.copy()
-
- def is_mcp_tool(self, tool_name: str) -> bool:
- """检查工具名是否为 MCP 工具"""
- return tool_name.startswith("mcp_")
-
- # === 组件移除相关 ===
-
async def unregister_plugin(self, plugin_name: str) -> bool:
- """卸载插件及其所有组件
+ """
+ 卸载一个插件及其所有关联的组件。
+
+ 这是一个高级操作,会依次移除插件的所有组件,然后移除插件本身的注册信息。
Args:
- plugin_name: 插件名称
+ plugin_name (str): 要卸载的插件的名称。
Returns:
- bool: 是否成功卸载
+ bool: 如果所有组件和插件本身都成功卸载,则返回 True;否则返回 False。
"""
plugin_info = self.get_plugin_info(plugin_name)
if not plugin_info:
@@ -1134,9 +289,7 @@ class ComponentRegistry:
logger.info(f"开始卸载插件: {plugin_name}")
- # 记录卸载失败的组件
failed_components = []
-
# 逐个移除插件的所有组件
for component_info in plugin_info.components:
try:
@@ -1151,19 +304,710 @@ class ComponentRegistry:
logger.error(f"移除组件 {component_info.name} 时发生异常: {e}")
failed_components.append(f"{component_info.component_type}.{component_info.name}")
- # 移除插件注册信息
- plugin_removed = self.remove_plugin_registry(plugin_name)
+ # 移除插件的注册信息
+ plugin_removed = self._remove_plugin_registry(plugin_name)
if failed_components:
logger.warning(f"插件 {plugin_name} 部分组件卸载失败: {failed_components}")
return False
- elif not plugin_removed:
+ if not plugin_removed:
logger.error(f"插件 {plugin_name} 注册信息移除失败")
return False
- else:
- logger.info(f"插件 {plugin_name} 卸载成功")
+
+ logger.info(f"插件 {plugin_name} 卸载成功")
+ return True
+
+ async def remove_component(self, component_name: str, component_type: ComponentType, plugin_name: str) -> bool:
+ """
+ 从注册中心移除一个指定的组件。
+
+ Args:
+ component_name (str): 要移除的组件的名称。
+ component_type (ComponentType): 组件的类型。
+ plugin_name (str): 组件所属的插件名称 (用于日志和验证)。
+
+ Returns:
+ bool: 移除成功返回 True,否则返回 False。
+ """
+ target_component_class = self.get_component_class(component_name, component_type)
+ if not target_component_class:
+ logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法移除")
+ return False
+
+ try:
+ # --- 特定类型的清理操作 ---
+ match component_type:
+ case ComponentType.ACTION:
+ self._action_registry.pop(component_name, None)
+ self._default_actions.pop(component_name, None)
+ case ComponentType.COMMAND:
+ self._command_registry.pop(component_name, None)
+ keys_to_remove = [k for k, v in self._command_patterns.items() if v == component_name]
+ for key in keys_to_remove:
+ self._command_patterns.pop(key, None)
+ case ComponentType.PLUS_COMMAND:
+ self._plus_command_registry.pop(component_name, None)
+ case ComponentType.TOOL:
+ self._tool_registry.pop(component_name, None)
+ self._llm_available_tools.pop(component_name, None)
+ case ComponentType.EVENT_HANDLER:
+ from .event_manager import event_manager # 延迟导入
+ event_manager.remove_event_handler(component_name)
+ case ComponentType.CHATTER:
+ self._chatter_registry.pop(component_name, None)
+ self._enabled_chatter_registry.pop(component_name, None)
+ case ComponentType.INTEREST_CALCULATOR:
+ self._interest_calculator_registry.pop(component_name, None)
+ self._enabled_interest_calculator_registry.pop(component_name, None)
+ case ComponentType.PROMPT:
+ self._prompt_registry.pop(component_name, None)
+ self._enabled_prompt_registry.pop(component_name, None)
+ case ComponentType.ROUTER:
+ logger.warning(f"Router组件 '{component_name}' 的HTTP端点无法在运行时动态移除,将在下次重启后生效。")
+ case _:
+ logger.warning(f"未知的组件类型: {component_type},无法进行特定的清理操作")
+ return False
+
+ # --- 通用注册信息的清理 ---
+ namespaced_name = f"{component_type.value}.{component_name}"
+ self._components.pop(namespaced_name, None)
+ self._components_by_type[component_type].pop(component_name, None)
+ self._components_classes.pop(namespaced_name, None)
+
+ logger.info(f"组件 {component_name} ({component_type.value}) 已从插件 '{plugin_name}' 中完全移除")
return True
+ except Exception as e:
+ logger.error(f"移除组件 {component_name} ({component_type.value}) 时发生错误: {e}", exc_info=True)
+ return False
-# 创建全局组件注册中心实例
+ # =================================================================
+ # == 内部注册辅助方法 (_register_* Methods)
+ # =================================================================
+
+ def _register_action_component(self, action_info: ActionInfo, action_class: type[BaseAction]) -> bool:
+ """注册Action组件到Action特定注册表。"""
+ action_name = action_info.name
+ _assign_plugin_attrs(action_class, action_info.plugin_name, self.get_plugin_config(action_info.plugin_name) or {})
+ self._action_registry[action_name] = action_class
+ if action_info.enabled:
+ self._default_actions[action_name] = action_info
+ return True
+
+ def _register_command_component(self, command_info: CommandInfo, command_class: type[BaseCommand]) -> bool:
+ """通过适配器将旧版Command注册为PlusCommand。"""
+ logger.warning(
+ f"检测到旧版Command组件 '{command_info.name}' (来自插件: {command_info.plugin_name})。"
+ "它将通过兼容层运行,但建议尽快迁移到PlusCommand以获得更好的性能和功能。"
+ )
+ # 使用适配器将其转换为PlusCommand
+ adapted_class = create_legacy_command_adapter(command_class)
+ plus_command_info = adapted_class.get_plus_command_info()
+ plus_command_info.plugin_name = command_info.plugin_name # 继承插件名
+
+ return self._register_plus_command_component(plus_command_info, adapted_class)
+
+ def _register_plus_command_component(
+ self, plus_command_info: PlusCommandInfo, plus_command_class: type[PlusCommand]
+ ) -> bool:
+ """注册PlusCommand组件到特定注册表。"""
+ plus_command_name = plus_command_info.name
+ _assign_plugin_attrs(
+ plus_command_class,
+ plus_command_info.plugin_name,
+ self.get_plugin_config(plus_command_info.plugin_name) or {},
+ )
+ self._plus_command_registry[plus_command_name] = plus_command_class
+ logger.debug(f"已注册PlusCommand组件: {plus_command_name}")
+ return True
+
+ def _register_tool_component(self, tool_info: ToolInfo, tool_class: type[BaseTool]) -> bool:
+ """注册Tool组件到Tool特定注册表。"""
+ tool_name = tool_info.name
+ _assign_plugin_attrs(tool_class, tool_info.plugin_name, self.get_plugin_config(tool_info.plugin_name) or {})
+ self._tool_registry[tool_name] = tool_class
+ if tool_info.enabled:
+ self._llm_available_tools[tool_name] = tool_class
+ return True
+
+ def _register_event_handler_component(
+ self, handler_info: EventHandlerInfo, handler_class: type[BaseEventHandler]
+ ) -> bool:
+ """注册EventHandler组件并订阅事件。"""
+ handler_name = handler_info.name
+ _assign_plugin_attrs(
+ handler_class, handler_info.plugin_name, self.get_plugin_config(handler_info.plugin_name) or {}
+ )
+ self._event_handler_registry[handler_name] = handler_class
+ if not handler_info.enabled:
+ logger.warning(f"EventHandler组件 {handler_name} 未启用,仅注册信息,不订阅事件")
+ return True # 未启用但注册成功
+
+ # 延迟导入以避免循环依赖
+ from src.plugin_system.core.event_manager import event_manager
+ return event_manager.register_event_handler(
+ handler_class, self.get_plugin_config(handler_info.plugin_name) or {}
+ )
+
+ def _register_chatter_component(self, chatter_info: ChatterInfo, chatter_class: type[BaseChatter]) -> bool:
+ """注册Chatter组件到Chatter特定注册表。"""
+ chatter_name = chatter_info.name
+ _assign_plugin_attrs(
+ chatter_class, chatter_info.plugin_name, self.get_plugin_config(chatter_info.plugin_name) or {}
+ )
+ self._chatter_registry[chatter_name] = chatter_class
+ if chatter_info.enabled:
+ self._enabled_chatter_registry[chatter_name] = chatter_class
+ logger.debug(f"已注册Chatter组件: {chatter_name}")
+ return True
+
+ def _register_interest_calculator_component(
+ self,
+ interest_calculator_info: "InterestCalculatorInfo",
+ interest_calculator_class: type["BaseInterestCalculator"],
+ ) -> bool:
+ """注册InterestCalculator组件到特定注册表。"""
+ calculator_name = interest_calculator_info.name
+ _assign_plugin_attrs(
+ interest_calculator_class,
+ interest_calculator_info.plugin_name,
+ self.get_plugin_config(interest_calculator_info.plugin_name) or {},
+ )
+ self._interest_calculator_registry[calculator_name] = interest_calculator_class
+ if interest_calculator_info.enabled:
+ self._enabled_interest_calculator_registry[calculator_name] = interest_calculator_class
+ logger.debug(f"已注册InterestCalculator组件: {calculator_name}")
+ return True
+
+ def _register_prompt_component(self, prompt_info: PromptInfo, prompt_class: type[BasePrompt]) -> bool:
+ """注册Prompt组件到Prompt特定注册表。"""
+ prompt_name = prompt_info.name
+ _assign_plugin_attrs(
+ prompt_class, prompt_info.plugin_name, self.get_plugin_config(prompt_info.plugin_name) or {}
+ )
+ self._prompt_registry[prompt_name] = prompt_class
+ if prompt_info.enabled:
+ self._enabled_prompt_registry[prompt_name] = prompt_class
+ logger.debug(f"已注册Prompt组件: {prompt_name}")
+ return True
+
+ def _register_router_component(self, router_info: RouterInfo, router_class: type[BaseRouterComponent]) -> bool:
+ """注册Router组件并将其HTTP端点挂载到主FastAPI应用。"""
+ if not bot_config.plugin_http_system.enable_plugin_http_endpoints:
+ logger.info("插件HTTP端点功能已禁用,跳过路由注册")
+ return True
+
+ try:
+ from src.common.server import get_global_server
+
+ router_name = router_info.name
+ plugin_name = router_info.plugin_name
+
+ # 实例化组件以获取其配置好的APIRouter实例
+ component_instance = router_class()
+ plugin_router = component_instance.router
+
+ # 获取全局FastAPI应用实例
+ server = get_global_server()
+
+ # 生成唯一的URL前缀,格式为 /plugins/{plugin_name}
+ prefix = f"/plugins/{plugin_name}"
+
+ # 将插件的路由包含到主应用中
+ server.app.include_router(plugin_router, prefix=prefix, tags=[plugin_name])
+
+ logger.debug(f"成功将插件 '{plugin_name}' 的路由组件 '{router_name}' 挂载到: {prefix}")
+ return True
+
+ except Exception as e:
+ logger.error(f"注册路由组件 '{router_info.name}' 时出错: {e}", exc_info=True)
+ return False
+
+ def _remove_plugin_registry(self, plugin_name: str) -> bool:
+ """
+ (内部方法) 仅移除插件的注册信息。
+
+ Args:
+ plugin_name (str): 插件名称。
+
+ Returns:
+ bool: 是否成功移除。
+ """
+ if plugin_name not in self._plugins:
+ logger.warning(f"插件 {plugin_name} 未注册,无法移除其注册信息")
+ return False
+ del self._plugins[plugin_name]
+ logger.info(f"插件 {plugin_name} 的注册信息已移除")
+ return True
+
+ # =================================================================
+ # == 组件状态管理 (Component State Management)
+ # =================================================================
+
+ def enable_component(self, component_name: str, component_type: ComponentType) -> bool:
+ """
+ 全局启用一个组件。
+
+ Args:
+ component_name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+
+ Returns:
+ bool: 启用成功返回 True,失败返回 False。
+ """
+ target_component_class = self.get_component_class(component_name, component_type)
+ target_component_info = self.get_component_info(component_name, component_type)
+ if not target_component_class or not target_component_info:
+ logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法启用")
+ return False
+
+ target_component_info.enabled = True
+ # 更新通用注册表中的状态
+ namespaced_name = f"{component_type.value}.{component_name}"
+ self._components[namespaced_name].enabled = True
+ self._components_by_type[component_type][component_name].enabled = True
+
+ # 更新特定类型的启用列表
+ match component_type:
+ case ComponentType.ACTION:
+ assert isinstance(target_component_info, ActionInfo)
+ self._default_actions[component_name] = target_component_info
+ case ComponentType.COMMAND:
+ # 旧版Command通过PlusCommand启用,这里无需操作
+ pass
+ case ComponentType.TOOL:
+ assert issubclass(target_component_class, BaseTool)
+ self._llm_available_tools[component_name] = target_component_class
+ case ComponentType.EVENT_HANDLER:
+ assert issubclass(target_component_class, BaseEventHandler)
+ self._enabled_event_handlers[component_name] = target_component_class
+ from .event_manager import event_manager
+ cfg = self.get_plugin_config(target_component_info.plugin_name) or {}
+ event_manager.register_event_handler(target_component_class, cfg)
+ case ComponentType.CHATTER:
+ assert issubclass(target_component_class, BaseChatter)
+ self._enabled_chatter_registry[component_name] = target_component_class
+ case ComponentType.INTEREST_CALCULATOR:
+ assert issubclass(target_component_class, BaseInterestCalculator)
+ self._enabled_interest_calculator_registry[component_name] = target_component_class
+ case ComponentType.PROMPT:
+ assert issubclass(target_component_class, BasePrompt)
+ self._enabled_prompt_registry[component_name] = target_component_class
+
+ logger.info(f"组件 {component_name} ({component_type.value}) 已全局启用")
+ return True
+
+ async def disable_component(self, component_name: str, component_type: ComponentType) -> bool:
+ """
+ 全局禁用一个组件。
+
+ Args:
+ component_name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+
+ Returns:
+ bool: 禁用成功返回 True,失败返回 False。
+ """
+ target_component_info = self.get_component_info(component_name, component_type)
+ if not target_component_info:
+ logger.warning(f"组件 {component_name} ({component_type.value}) 未注册,无法禁用")
+ return False
+
+ target_component_info.enabled = False
+ # 更新通用注册表中的状态
+ namespaced_name = f"{component_type.value}.{component_name}"
+ if namespaced_name in self._components:
+ self._components[namespaced_name].enabled = False
+ if component_name in self._components_by_type[component_type]:
+ self._components_by_type[component_type][component_name].enabled = False
+
+ try:
+ # 从特定类型的启用列表中移除
+ match component_type:
+ case ComponentType.ACTION:
+ self._default_actions.pop(component_name, None)
+ case ComponentType.COMMAND:
+ # 旧版Command通过PlusCommand禁用,这里无需操作
+ pass
+ case ComponentType.TOOL:
+ self._llm_available_tools.pop(component_name, None)
+ case ComponentType.EVENT_HANDLER:
+ self._enabled_event_handlers.pop(component_name, None)
+ from .event_manager import event_manager
+ # 从事件管理器中取消订阅
+ event_manager.remove_event_handler(component_name)
+ case ComponentType.CHATTER:
+ self._enabled_chatter_registry.pop(component_name, None)
+ case ComponentType.INTEREST_CALCULATOR:
+ self._enabled_interest_calculator_registry.pop(component_name, None)
+ case ComponentType.PROMPT:
+ self._enabled_prompt_registry.pop(component_name, None)
+
+ logger.info(f"组件 {component_name} ({component_type.value}) 已全局禁用")
+ return True
+ except Exception as e:
+ logger.error(f"禁用组件 {component_name} ({component_type.value}) 时发生错误: {e}", exc_info=True)
+ # 即使出错,也尝试将状态标记为禁用
+ return False
+
+ # =================================================================
+ # == 查询方法 (Query Methods)
+ # =================================================================
+
+ def get_component_info(
+ self, component_name: str, component_type: ComponentType | None = None
+ ) -> ComponentInfo | None:
+ """
+ 获取组件信息,支持自动命名空间解析。
+
+ 如果只提供 `component_name`,它会尝试在所有类型中查找。如果找到多个同名但不同类型的
+ 组件,会发出警告并返回第一个找到的。
+
+ Args:
+ component_name (str): 组件名称,可以是原始名称或命名空间化的名称 (如 "action.my_action")。
+ component_type (ComponentType, optional): 组件类型。如果提供,将只在该类型中查找。
+
+ Returns:
+ ComponentInfo | None: 找到的组件信息对象,或 None。
+ """
+ # 1. 如果已经是命名空间化的名称,直接查找
+ if "." in component_name:
+ return self._components.get(component_name)
+
+ # 2. 如果指定了组件类型,构造命名空间化的名称查找
+ if component_type:
+ namespaced_name = f"{component_type.value}.{component_name}"
+ return self._components.get(namespaced_name)
+
+ # 3. 如果没有指定类型,遍历所有类型查找
+ candidates = []
+ for c_type in ComponentType:
+ namespaced_name = f"{c_type.value}.{component_name}"
+ if component_info := self._components.get(namespaced_name):
+ candidates.append(component_info)
+
+ if len(candidates) == 1:
+ return candidates[0]
+ if len(candidates) > 1:
+ types_found = [info.component_type.value for info in candidates]
+ logger.warning(
+ f"组件名称 '{component_name}' 在多个类型中存在: {types_found}。"
+ f"返回第一个匹配项 (类型: {candidates[0].component_type.value})。请使用 component_type 参数指定类型以消除歧义。"
+ )
+ return candidates[0]
+
+ # 4. 都没找到
+ return None
+
+ def get_component_class(
+ self, component_name: str, component_type: ComponentType | None = None
+ ) -> ComponentClassType | None:
+ """
+ 获取组件的类定义,支持自动命名空间解析。
+
+ 逻辑与 `get_component_info` 类似。
+
+ Args:
+ component_name (str): 组件名称。
+ component_type (ComponentType, optional): 组件类型。
+
+ Returns:
+ ComponentClassType | None: 找到的组件类,或 None。
+ """
+ # 1. 如果已经是命名空间化的名称,直接查找
+ if "." in component_name:
+ return self._components_classes.get(component_name)
+
+ # 2. 如果指定了组件类型,构造命名空间化的名称查找
+ if component_type:
+ namespaced_name = f"{component_type.value}.{component_name}"
+ return self._components_classes.get(namespaced_name)
+
+ # 3. 如果没有指定类型,遍历所有类型查找
+ info = self.get_component_info(component_name) # 复用 get_component_info 的查找和消歧义逻辑
+ if info:
+ namespaced_name = f"{info.component_type.value}.{info.name}"
+ return self._components_classes.get(namespaced_name)
+
+ # 4. 都没找到
+ return None
+
+ def get_components_by_type(self, component_type: ComponentType) -> dict[str, ComponentInfo]:
+ """获取指定类型的所有已注册组件(无论是否启用)。"""
+ return self._components_by_type.get(component_type, {}).copy()
+
+ def get_enabled_components_by_type(
+ self, component_type: ComponentType, stream_id: str | None = None
+ ) -> dict[str, ComponentInfo]:
+ """
+ 获取指定类型的所有可用组件。
+
+ 这会同时考虑全局启用状态和 `stream_id` 对应的局部状态。
+
+ Args:
+ component_type (ComponentType): 要查询的组件类型。
+ stream_id (str, optional): 会话ID,用于检查局部状态覆盖。
+
+ Returns:
+ dict[str, ComponentInfo]: 一个包含可用组件名称和信息的字典。
+ """
+ all_components = self.get_components_by_type(component_type)
+ return {
+ name: info
+ for name, info in all_components.items()
+ if self.is_component_available(name, component_type, stream_id)
+ }
+
+ # =================================================================
+ # == 特定类型查询方法 (Type-Specific Query Methods)
+ # =================================================================
+
+ # --- Action ---
+ def get_action_registry(self) -> dict[str, type[BaseAction]]:
+ """获取所有已注册的Action类。"""
+ return self._action_registry.copy()
+
+ def get_default_actions(self, stream_id: str | None = None) -> dict[str, ActionInfo]:
+ """获取所有可用的Action信息(考虑全局和局部状态)。"""
+ return cast(
+ dict[str, ActionInfo],
+ self.get_enabled_components_by_type(ComponentType.ACTION, stream_id),
+ )
+
+ # --- PlusCommand ---
+ def get_plus_command_registry(self) -> dict[str, type[PlusCommand]]:
+ """获取所有已注册的PlusCommand类。"""
+ return self._plus_command_registry.copy()
+
+ def get_available_plus_commands_info(self, stream_id: str | None = None) -> dict[str, PlusCommandInfo]:
+ """获取所有可用的PlusCommand信息(考虑全局和局部状态)。"""
+ return cast(
+ dict[str, PlusCommandInfo],
+ self.get_enabled_components_by_type(ComponentType.PLUS_COMMAND, stream_id),
+ )
+
+ # --- Tool ---
+ def get_tool_registry(self) -> dict[str, type[BaseTool]]:
+ """获取所有已注册的Tool类。"""
+ return self._tool_registry.copy()
+
+ def get_llm_available_tools(self, stream_id: str | None = None) -> dict[str, type[BaseTool]]:
+ """获取所有对LLM可用的Tool类(考虑全局和局部状态)。"""
+ all_tools = self.get_tool_registry()
+ available_tools = {}
+ for name, tool_class in all_tools.items():
+ if self.is_component_available(name, ComponentType.TOOL, stream_id):
+ available_tools[name] = tool_class
+ return available_tools
+
+ # --- EventHandler ---
+ def get_event_handler_registry(self) -> dict[str, type[BaseEventHandler]]:
+ """获取所有已注册的EventHandler类。"""
+ return self._event_handler_registry.copy()
+
+ def get_enabled_event_handlers(self) -> dict[str, type[BaseEventHandler]]:
+ """获取所有已启用的EventHandler类。"""
+ return self._enabled_event_handlers.copy()
+
+ # --- Chatter ---
+ def get_chatter_registry(self) -> dict[str, type[BaseChatter]]:
+ """获取所有已注册的Chatter类。"""
+ return self._chatter_registry.copy()
+
+ def get_enabled_chatter_registry(self, stream_id: str | None = None) -> dict[str, type[BaseChatter]]:
+ """获取所有可用的Chatter类(考虑全局和局部状态)。"""
+ all_chatters = self.get_chatter_registry()
+ available_chatters = {}
+ for name, chatter_class in all_chatters.items():
+ if self.is_component_available(name, ComponentType.CHATTER, stream_id):
+ available_chatters[name] = chatter_class
+ return available_chatters
+
+ # --- Prompt ---
+ def get_prompt_registry(self) -> dict[str, type[BasePrompt]]:
+ """获取所有已注册的Prompt类。"""
+ return self._prompt_registry.copy()
+
+ def get_enabled_prompt_registry(self) -> dict[str, type[BasePrompt]]:
+ """获取所有已启用的Prompt类。"""
+ return self._enabled_prompt_registry.copy()
+
+ # --- 插件 ---
+ def get_plugin_info(self, plugin_name: str) -> PluginInfo | None:
+ """获取指定插件的信息。"""
+ return self._plugins.get(plugin_name)
+
+ def get_all_plugins(self) -> dict[str, PluginInfo]:
+ """获取所有已注册的插件。"""
+ return self._plugins.copy()
+
+ def get_plugin_components(self, plugin_name: str) -> list["ComponentInfo"]:
+ """获取指定插件下的所有组件信息。"""
+ plugin_info = self.get_plugin_info(plugin_name)
+ return plugin_info.components if plugin_info else []
+
+ def get_plugin_config(self, plugin_name: str) -> dict | None:
+ """
+ 获取插件的配置信息。
+
+ 它会首先尝试从已加载的插件实例中获取,如果失败,则尝试从文件系统读取。
+
+ Args:
+ plugin_name (str): 插件名称。
+
+ Returns:
+ dict | None: 插件的配置字典,如果找不到则返回 None。
+ """
+ # 延迟导入以避免循环依赖
+ from src.plugin_system.core.plugin_manager import plugin_manager
+
+ plugin_instance = plugin_manager.get_plugin_instance(plugin_name)
+ if plugin_instance and plugin_instance.config:
+ return plugin_instance.config
+
+ # 如果插件实例不存在,尝试从配置文件读取
+ try:
+ config_path = Path("config") / "plugins" / plugin_name / "config.toml"
+ if config_path.exists():
+ with open(config_path, encoding="utf-8") as f:
+ config_data = toml.load(f)
+ logger.debug(f"从配置文件延迟加载了插件 {plugin_name} 的配置")
+ return config_data
+ except Exception as e:
+ logger.debug(f"读取插件 {plugin_name} 配置文件失败: {e}")
+
+ return None
+
+ # =================================================================
+ # == 局部状态管理 (Local State Management)
+ # =================================================================
+
+ def set_local_component_state(
+ self, stream_id: str, component_name: str, component_type: ComponentType, enabled: bool
+ ) -> bool:
+ """
+ 为指定的会话(stream_id)设置组件的局部(临时)状态。
+
+ 这允许在单个对话流中动态启用或禁用组件,而不影响全局设置。
+
+ Args:
+ stream_id (str): 唯一的会话ID。
+ component_name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+ enabled (bool): True 表示启用,False 表示禁用。
+
+ Returns:
+ bool: 设置成功返回 True,如果组件类型不支持局部状态则返回 False。
+ """
+ if component_type in self._no_local_state_types:
+ logger.warning(
+ f"组件类型 {component_type.value} 不支持局部状态管理。"
+ f"尝试为 '{component_name}' 设置局部状态的操作将被忽略。"
+ )
+ return False
+
+ if stream_id not in self._local_component_states:
+ self._local_component_states[stream_id] = {}
+
+ state_key = (component_name, component_type)
+ self._local_component_states[stream_id][state_key] = enabled
+ logger.debug(
+ f"已为 stream '{stream_id}' 设置局部状态: {component_name} ({component_type.value}) -> {'启用' if enabled else '禁用'}"
+ )
+ return True
+
+ def is_component_available(
+ self, component_name: str, component_type: ComponentType, stream_id: str | None = None
+ ) -> bool:
+ """
+ 检查一个组件在给定上下文中是否可用。
+
+ 检查顺序:
+ 1. 组件是否存在。
+ 2. (如果提供了 stream_id) 是否有局部状态覆盖。
+ 3. 全局启用状态。
+
+ Args:
+ component_name (str): 组件名称。
+ component_type (ComponentType): 组件类型。
+ stream_id (str, optional): 会话ID。
+
+ Returns:
+ bool: 如果组件可用,则返回 True。
+ """
+ component_info = self.get_component_info(component_name, component_type)
+
+ # 1. 检查组件是否存在
+ if not component_info:
+ return False
+
+ # 2. 如果组件类型不支持局部状态,直接返回其全局状态
+ if component_type in self._no_local_state_types:
+ return component_info.enabled
+
+ # 3. 如果提供了 stream_id,检查是否存在局部状态覆盖
+ if stream_id and stream_id in self._local_component_states:
+ state_key = (component_name, component_type)
+ local_state = self._local_component_states[stream_id].get(state_key)
+ if local_state is not None:
+ return local_state # 局部状态存在,直接返回
+
+ # 4. 如果没有局部状态覆盖,返回全局状态
+ return component_info.enabled
+
+ # =================================================================
+ # == MCP 工具相关方法 (MCP Tool Methods)
+ # =================================================================
+
+ async def load_mcp_tools(self) -> None:
+ """
+ 异步加载所有 MCP (Model-Copilot-Plugin) 工具。
+
+ 此方法会动态导入并实例化 MCP 工具适配器。为避免重复加载,它会检查一个标志位。
+ """
+ if self._mcp_tools_loaded:
+ logger.debug("MCP 工具已加载,跳过")
+ return
+
+ try:
+ from .mcp_tool_adapter import load_mcp_tools_as_adapters
+
+ logger.info("开始加载 MCP 工具...")
+ self._mcp_tools = await load_mcp_tools_as_adapters()
+ self._mcp_tools_loaded = True
+ logger.info(f"MCP 工具加载完成,共 {len(self._mcp_tools)} 个工具")
+ except Exception as e:
+ logger.error(f"加载 MCP 工具失败: {e}", exc_info=True)
+ self._mcp_tools = []
+ self._mcp_tools_loaded = True # 标记为已尝试加载,避免重复失败
+
+ def get_mcp_tools(self) -> list["BaseTool"]:
+ """获取所有已加载的 MCP 工具适配器实例。"""
+ return self._mcp_tools.copy()
+
+ def is_mcp_tool(self, tool_name: str) -> bool:
+ """检查一个工具名称是否代表一个 MCP 工具(基于命名约定)。"""
+ return tool_name.startswith("mcp_")
+
+ # =================================================================
+ # == 统计与辅助方法 (Statistics and Helper Methods)
+ # =================================================================
+
+ def get_registry_stats(self) -> dict[str, Any]:
+ """获取注册中心的统计信息,用于调试和监控。"""
+ stats = {component_type.value: 0 for component_type in ComponentType}
+ for component in self._components.values():
+ stats[component.component_type.value] += 1
+
+ return {
+ "total_plugins": len(self._plugins),
+ "total_components": len(self._components),
+ "enabled_components": len([c for c in self._components.values() if c.enabled]),
+ "mcp_tools_loaded": len(self._mcp_tools),
+ "components_by_type": stats,
+ }
+
+
+# --- 全局实例 ---
+# 创建全局唯一的组件注册中心实例,供项目各处使用
component_registry = ComponentRegistry()
From 7b8660bb69b0ee13403973fb77e86e109f87d91f Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 16:59:41 +0800
Subject: [PATCH 10/22] =?UTF-8?q?refactor(plugin=5Fsystem):=20=E7=BB=9F?=
=?UTF-8?q?=E4=B8=80=E6=8F=92=E4=BB=B6=E5=8D=B8=E8=BD=BD=E9=80=BB=E8=BE=91?=
=?UTF-8?q?=E5=88=B0=E6=B3=A8=E5=86=8C=E4=B8=AD=E5=BF=83?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将 `PluginManager.unload_plugin` 中的卸载逻辑移至 `component_registry`。现在 `PluginManager` 直接调用 `component_registry.unregister_plugin` 来处理所有组件和插件的注销,简化了插件管理器的职责,使卸载过程更加集中和一致。
---
src/plugin_system/core/plugin_manager.py | 12 +++++-------
1 file changed, 5 insertions(+), 7 deletions(-)
diff --git a/src/plugin_system/core/plugin_manager.py b/src/plugin_system/core/plugin_manager.py
index 2548326a9..7b3900279 100644
--- a/src/plugin_system/core/plugin_manager.py
+++ b/src/plugin_system/core/plugin_manager.py
@@ -174,13 +174,11 @@ class PluginManager:
if plugin_name not in self.loaded_plugins:
logger.warning(f"插件 {plugin_name} 未加载")
return False
- plugin_instance = self.loaded_plugins[plugin_name]
- plugin_info = plugin_instance.plugin_info
- success = True
- for component in plugin_info.components:
- success &= await component_registry.remove_component(component.name, component.component_type, plugin_name)
- success &= component_registry.remove_plugin_registry(plugin_name)
- del self.loaded_plugins[plugin_name]
+ # 调用 component_registry 中统一的卸载方法
+ success = await component_registry.unregister_plugin(plugin_name)
+ if success:
+ # 从已加载插件中移除
+ del self.loaded_plugins[plugin_name]
return success
async def reload_registered_plugin(self, plugin_name: str) -> bool:
From 1c653ee0214da90e46654ae33e98e835ebcee60a Mon Sep 17 00:00:00 2001
From: Eric-Terminal <121368508+Eric-Terminal@users.noreply.github.com>
Date: Sat, 22 Nov 2025 19:35:28 +0800
Subject: [PATCH 11/22] =?UTF-8?q?feat:=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96?=
=?UTF-8?q?=E7=BC=BA=E5=A4=B1=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pyproject.toml | 1 +
1 file changed, 1 insertion(+)
diff --git a/pyproject.toml b/pyproject.toml
index 2f70c2c4c..c60e2f98d 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -79,6 +79,7 @@ dependencies = [
"inkfox>=0.1.1",
"rjieba>=0.1.13",
"fastmcp>=2.13.0",
+ "jinja2>=3.1.0"
]
[[tool.uv.index]]
From 4a4175c24621bb488967e5ac937a5d322bdeba7a Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 20:22:46 +0800
Subject: [PATCH 12/22] =?UTF-8?q?feat(plugin=5Fsystem):=20=E5=AE=9E?=
=?UTF-8?q?=E7=8E=B0=E7=BB=84=E4=BB=B6=E7=9A=84=E5=B1=80=E9=83=A8=E5=90=AF?=
=?UTF-8?q?=E7=94=A8=E4=B8=8E=E7=A6=81=E7=94=A8=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
新增了 `/system plugin enable_local` 和 `/system plugin disable_local` 命令,允许管理员在指定的会话(群聊或私聊)中动态地启用或禁用插件组件。
- 通过 stream_id 对组件状态进行局部覆盖,提供了更精细的控制粒度。
- 引入新的 `plugin.manage.local` 权限节点以控制此高级功能。
- 在 API 层面增加了对组件存在性的检查,增强了系统的健壮性。
---
src/plugin_system/apis/plugin_manage_api.py | 6 ++
.../built_in/system_management/plugin.py | 74 ++++++++++++++++++-
2 files changed, 78 insertions(+), 2 deletions(-)
diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py
index fa9c4e20a..67813f524 100644
--- a/src/plugin_system/apis/plugin_manage_api.py
+++ b/src/plugin_system/apis/plugin_manage_api.py
@@ -102,6 +102,12 @@ def set_component_enabled_local(stream_id: str, name: str, component_type: Compo
Returns:
bool: 操作成功则为 True。
"""
+ # 首先,检查组件是否存在
+ component_info = component_registry.get_component_info(name, component_type)
+ if not component_info:
+ logger.error(f"尝试设置局部状态失败:未找到组件 {name} ({component_type.value})。")
+ return False
+
# Chatter 唯一性保护
if component_type == ComponentType.CHATTER and not enabled:
# 检查当前 stream_id 上下文中的启用状态
diff --git a/src/plugins/built_in/system_management/plugin.py b/src/plugins/built_in/system_management/plugin.py
index e06b329a9..681d71c6d 100644
--- a/src/plugins/built_in/system_management/plugin.py
+++ b/src/plugins/built_in/system_management/plugin.py
@@ -11,6 +11,7 @@ from typing import ClassVar
from src.chat.utils.prompt_component_manager import prompt_component_manager
from src.chat.utils.prompt_params import PromptParameters
from src.plugin_system.apis import (
+ chat_api,
plugin_manage_api,
)
from src.plugin_system.apis.logging_api import get_logger
@@ -21,6 +22,7 @@ from src.plugin_system.base.base_plugin import BasePlugin
from src.plugin_system.base.command_args import CommandArgs
from src.plugin_system.base.component_types import (
ChatType,
+ ComponentType,
PermissionNodeField,
PlusCommandInfo,
)
@@ -103,6 +105,9 @@ class SystemCommand(PlusCommand):
• `/system plugin load <插件名>` - 加载指定插件
• `/system plugin reload <插件名>` - 重新加载指定插件
• `/system plugin reload_all` - 重新加载所有插件
+🎯 局部控制 (需要 `system.plugin.manage.local` 权限):
+• `/system plugin enable_local <类型> <名称> [group <群号> | private ]` - 在指定会话局部启用组件
+• `/system plugin disable_local <类型> <名称> [group <群号> | private ]` - 在指定会话局部禁用组件
"""
elif target == "permission":
help_text = """📋 权限管理命令帮助
@@ -157,6 +162,10 @@ class SystemCommand(PlusCommand):
await self._reload_plugin(remaining_args[0])
elif action in ["reload_all", "重载全部"]:
await self._reload_all_plugins()
+ elif action in ["enable_local", "局部启用"] and len(remaining_args) >= 2:
+ await self._set_local_component_state(remaining_args, enabled=True)
+ elif action in ["disable_local", "局部禁用"] and len(remaining_args) >= 2:
+ await self._set_local_component_state(remaining_args, enabled=False)
else:
await self.send_text("❌ 插件管理命令不合法\n使用 /system plugin help 查看帮助")
@@ -309,7 +318,7 @@ class SystemCommand(PlusCommand):
@require_permission("prompt.view", deny_message="❌ 你没有查看提示词注入信息的权限")
async def _list_prompt_components(self):
"""列出所有已注册的提示词组件"""
- components = prompt_component_manager.get_registered_prompt_component_info()
+ components = await prompt_component_manager.get_registered_prompt_component_info()
if not components:
await self.send_text("🧩 当前没有已注册的提示词组件")
return
@@ -391,7 +400,7 @@ class SystemCommand(PlusCommand):
@require_permission("prompt.view", deny_message="❌ 你没有查看提示词组件信息的权限")
async def _show_prompt_component_info(self, component_name: str):
"""显示特定提示词组件的详细信息"""
- all_components = prompt_component_manager.get_registered_prompt_component_info()
+ all_components = await prompt_component_manager.get_registered_prompt_component_info()
target_component = next((comp for comp in all_components if comp.name == component_name), None)
@@ -486,6 +495,63 @@ class SystemCommand(PlusCommand):
else:
await self.send_text("⚠️ 部分插件重载失败,请检查日志。")
+ @require_permission("plugin.manage.local", deny_message="❌ 你没有局部管理插件组件的权限")
+ async def _set_local_component_state(self, args: list[str], enabled: bool):
+ """在局部范围内启用或禁用一个组件"""
+ # 命令格式: [group | private ]
+ if len(args) < 2:
+ action = "enable_local" if enabled else "disable_local"
+ await self.send_text(f"❌ 用法: /system plugin {action} <类型> <名称> [group <群号> | private ]")
+ return
+
+ comp_type_str = args[0]
+ comp_name = args[1]
+ stream_id = self.message.chat_info.stream_id # 默认作用于当前会话
+
+ if len(args) >= 4:
+ context_type = args[2].lower()
+ context_id = args[3]
+
+ target_stream = None
+ if context_type == "group":
+ target_stream = chat_api.get_stream_by_group_id(
+ group_id=context_id,
+ platform=self.message.chat_info.platform
+ )
+ elif context_type == "private":
+ target_stream = chat_api.get_stream_by_user_id(
+ user_id=context_id,
+ platform=self.message.chat_info.platform
+ )
+ else:
+ await self.send_text("❌ 无效的作用域类型,请使用 'group' 或 'private'。")
+ return
+
+ if not target_stream:
+ await self.send_text(f"❌ 在当前平台找不到指定的 {context_type}: `{context_id}`。")
+ return
+
+ stream_id = target_stream.stream_id
+
+ try:
+ component_type = ComponentType(comp_type_str.lower())
+ except ValueError:
+ await self.send_text(f"❌ 无效的组件类型: '{comp_type_str}'。有效类型: {', '.join([t.value for t in ComponentType])}")
+ return
+
+ success = plugin_manage_api.set_component_enabled_local(
+ stream_id=stream_id,
+ name=comp_name,
+ component_type=component_type,
+ enabled=enabled
+ )
+
+ action_text = "启用" if enabled else "禁用"
+ if success:
+ await self.send_text(f"✅ 在会话 `{stream_id}` 中,已成功将组件 `{comp_name}` ({comp_type_str}) 设置为 {action_text} 状态。")
+ else:
+ await self.send_text(f"❌ 操作失败。可能无法禁用最后一个启用的 Chatter,或组件不存在。请检查日志。")
+
# =================================================================
# Permission Management Section
@@ -731,4 +797,8 @@ class SystemManagementPlugin(BasePlugin):
node_name="schedule.manage",
description="定时任务管理:暂停和恢复定时任务",
),
+ PermissionNodeField(
+ node_name="plugin.manage.local",
+ description="局部插件管理:在指定会话中启用或禁用组件",
+ ),
]
From 24dc2abe2f1c25b1f4bafecaae773951ed851ad5 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 21:18:08 +0800
Subject: [PATCH 13/22] =?UTF-8?q?feat(plugin=5Fsystem):=20=E6=89=A9?=
=?UTF-8?q?=E5=B1=95=E6=8F=92=E4=BB=B6=E7=AE=A1=E7=90=86API=EF=BC=8C?=
=?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=9A=E7=A7=8D=E6=9F=A5=E8=AF=A2=E4=B8=8E?=
=?UTF-8?q?=E8=BE=85=E5=8A=A9=E5=87=BD=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
对 `plugin_manage_api.py` 进行了大规模的重构和功能增强,以提高其可维护性和可用性。
主要变更包括:
- **结构重构**: 将API按功能划分为四个逻辑部分:生命周期管理、状态管理、信息查询和工具函数,使代码结构更清晰。
- **功能扩展**: 新增了多个API端点用于查询插件和组件状态,例如 `get_plugin_details`, `list_plugins`, `search_components_by_name` 等。
- **文档完善**: 为所有公共API添加了详细的中文文档字符串和内联注释,解释了其用途、参数和返回值。
这些变更旨在为上层应用(如UI、CLI工具)提供一个更强大、更易于集成的插件系统管理接口。
---
src/plugin_system/apis/plugin_manage_api.py | 556 ++++++++++++++------
1 file changed, 409 insertions(+), 147 deletions(-)
diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py
index 67813f524..179b55167 100644
--- a/src/plugin_system/apis/plugin_manage_api.py
+++ b/src/plugin_system/apis/plugin_manage_api.py
@@ -1,29 +1,47 @@
# -*- coding: utf-8 -*-
+"""
+Plugin Manage API
+=================
+
+该模块提供了用于管理插件和组件生命周期、状态和信息查询的核心API。
+功能包括插件的加载、重载、注册、扫描,组件的启用/禁用,以及系统状态报告的生成。
+所有函数都设计为异步或同步,以适应不同的调用上下文。
+"""
+
import os
-from typing import Any
+from typing import Any, Literal
from src.common.logger import get_logger
from src.plugin_system.base.component_types import ComponentType
from src.plugin_system.core.component_registry import ComponentInfo, component_registry
from src.plugin_system.core.plugin_manager import plugin_manager
+# 初始化日志记录器
logger = get_logger("plugin_manage_api")
+# --------------------------------------------------------------------------------
+# Section 1: 插件生命周期管理 (Plugin Lifecycle Management)
+# --------------------------------------------------------------------------------
+# 该部分包含控制插件加载、重载、注册和发现的核心功能。
+
+
async def reload_all_plugins() -> bool:
"""
重新加载所有当前已成功加载的插件。
- 此操作会先卸载所有插件,然后重新加载它们。
+ 此操作会遍历所有已加载的插件,逐一进行卸载和重新加载。
+ 如果任何一个插件重载失败,整个过程会继续,但最终返回 False。
Returns:
bool: 如果所有插件都成功重载,则为 True,否则为 False。
"""
logger.info("开始重新加载所有插件...")
- # 使用 list() 复制一份列表,防止在迭代时修改原始列表
+ # 使用 list() 创建一个当前已加载插件列表的副本,以避免在迭代过程中修改原始列表
loaded_plugins = list(plugin_manager.list_loaded_plugins())
all_success = True
+ # 遍历副本列表中的每个插件进行重载
for plugin_name in loaded_plugins:
try:
success = await reload_plugin(plugin_name)
@@ -32,7 +50,7 @@ async def reload_all_plugins() -> bool:
logger.error(f"重载插件 {plugin_name} 失败。")
except Exception as e:
all_success = False
- logger.error(f"重载插件 {plugin_name} 时发生异常: {e}", exc_info=True)
+ logger.error(f"重载插件 {plugin_name} 时发生未知异常: {e}", exc_info=True)
logger.info("所有插件重载完毕。")
return all_success
@@ -42,46 +60,162 @@ async def reload_plugin(name: str) -> bool:
"""
重新加载指定的单个插件。
+ 该函数首先检查插件是否已注册,然后调用插件管理器执行重载操作。
+
Args:
name (str): 要重载的插件的名称。
Returns:
- bool: 成功则为 True。
+ bool: 如果插件成功重载,则为 True。
Raises:
- ValueError: 如果插件未找到。
+ ValueError: 如果插件未在插件管理器中注册。
"""
+ # 验证插件是否存在于注册列表中
if name not in plugin_manager.list_registered_plugins():
- raise ValueError(f"插件 '{name}' 未注册。")
+ raise ValueError(f"插件 '{name}' 未注册,无法重载。")
+ # 调用插件管理器的核心重载方法
return await plugin_manager.reload_registered_plugin(name)
+def rescan_and_register_plugins(load_after_register: bool = True) -> tuple[int, int]:
+ """
+ 重新扫描所有插件目录,以发现并注册新插件。
+
+ 此函数会触发插件管理器扫描其配置的所有插件目录。
+ 可以选择在注册新发现的插件后立即加载它们。
+
+ Args:
+ load_after_register (bool): 如果为 True,新发现的插件将在注册后立即被加载。默认为 True。
+
+ Returns:
+ tuple[int, int]: 一个元组,包含 (成功加载的插件数量, 加载失败的插件数量)。
+ """
+ # 扫描插件目录,获取新注册成功和失败的数量
+ success_count, fail_count = plugin_manager.rescan_plugin_directory()
+
+ # 如果不需要在注册后加载,则直接返回扫描结果
+ if not load_after_register:
+ return success_count, fail_count
+
+ # 找出新注册但尚未加载的插件
+ newly_registered = [
+ p for p in plugin_manager.list_registered_plugins() if p not in plugin_manager.list_loaded_plugins()
+ ]
+
+ loaded_success_count = 0
+ # 尝试加载所有新注册的插件
+ for plugin_name in newly_registered:
+ status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
+ if status:
+ loaded_success_count += 1
+
+ # 计算总的成功和失败数量
+ total_failed = fail_count + (len(newly_registered) - loaded_success_count)
+ return loaded_success_count, total_failed
+
+
+def register_plugin_from_file(plugin_name: str, load_after_register: bool = True) -> bool:
+ """
+ 从插件目录中查找、注册并选择性地加载一个指定的插件。
+
+ 如果插件已经加载,此函数将直接返回 True。
+ 如果插件未注册,它会遍历所有插件目录以查找匹配的插件文件夹。
+
+ Args:
+ plugin_name (str): 插件的名称(通常是其目录名)。
+ load_after_register (bool): 注册成功后是否立即加载该插件。默认为 True。
+
+ Returns:
+ bool: 如果插件成功注册(并且根据参数成功加载),则为 True。
+ """
+ # 如果插件已经加载,无需执行任何操作
+ if plugin_name in plugin_manager.list_loaded_plugins():
+ logger.warning(f"插件 '{plugin_name}' 已经加载,无需重复注册。")
+ return True
+
+ # 如果插件尚未注册,则开始搜索流程
+ if plugin_name not in plugin_manager.list_registered_plugins():
+ logger.info(f"插件 '{plugin_name}' 未注册,开始在插件目录中搜索...")
+ found_path = None
+
+ # 遍历所有配置的插件目录
+ for directory in plugin_manager.plugin_directories:
+ potential_path = os.path.join(directory, plugin_name)
+ # 检查是否存在与插件同名的目录
+ if os.path.isdir(potential_path):
+ found_path = potential_path
+ break
+
+ # 如果未找到插件目录,则报告错误
+ if not found_path:
+ logger.error(f"在所有插件目录中都未找到名为 '{plugin_name}' 的插件。")
+ return False
+
+ # 检查插件的核心 'plugin.py' 文件是否存在
+ plugin_file = os.path.join(found_path, "plugin.py")
+ if not os.path.exists(plugin_file):
+ logger.error(f"在插件目录 '{found_path}' 中未找到核心的 plugin.py 文件。")
+ return False
+
+ # 尝试从文件加载插件模块
+ module = plugin_manager._load_plugin_module_file(plugin_file)
+ if not module:
+ logger.error(f"从文件 '{plugin_file}' 加载插件模块失败。")
+ return False
+
+ # 验证模块加载后,插件是否已成功注册
+ if plugin_name not in plugin_manager.list_registered_plugins():
+ logger.error(f"插件 '{plugin_name}' 在加载模块后依然未能成功注册。请检查插件定义。")
+ return False
+
+ logger.info(f"插件 '{plugin_name}' 已成功发现并注册。")
+
+ # 根据参数决定是否在注册后立即加载插件
+ if load_after_register:
+ logger.info(f"正在加载插件 '{plugin_name}'...")
+ status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
+ return status
+
+ return True
+
+
+# --------------------------------------------------------------------------------
+# Section 2: 组件状态管理 (Component State Management)
+# --------------------------------------------------------------------------------
+# 这部分 API 负责控制单个组件的启用和禁用状态,支持全局和局部(临时)范围。
+
+
async def set_component_enabled(name: str, component_type: ComponentType, enabled: bool) -> bool:
"""
- 全局范围内启用或禁用一个组件。
+ 在全局范围内启用或禁用一个组件。
- 此更改会更新组件注册表中的状态,但不会持久化到文件。
+ 此更改会直接修改组件在注册表中的状态,但此状态是临时的,不会持久化到配置文件中。
+ 包含一个保护机制,防止禁用最后一个已启用的 Chatter 组件。
Args:
- name (str): 组件名称。
- component_type (ComponentType): 组件类型。
- enabled (bool): True 为启用, False 为禁用。
+ name (str): 要操作的组件的名称。
+ component_type (ComponentType): 组件的类型。
+ enabled (bool): True 表示启用, False 表示禁用。
Returns:
- bool: 操作成功则为 True。
+ bool: 如果操作成功,则为 True。
"""
- # Chatter 唯一性保护
+ # 特殊保护:确保系统中至少有一个 Chatter 组件处于启用状态
if component_type == ComponentType.CHATTER and not enabled:
enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER)
+ # 如果当前启用的 Chatter 少于等于1个,并且要禁用的就是它,则阻止操作
if len(enabled_chatters) <= 1 and name in enabled_chatters:
logger.warning(f"操作被阻止:不能禁用最后一个启用的 Chatter 组件 ('{name}')。")
return False
- # 注意:这里我们直接修改 ComponentInfo 中的状态
+ # 获取组件信息
component_info = component_registry.get_component_info(name, component_type)
if not component_info:
logger.error(f"未找到组件 {name} ({component_type.value}),无法更改其状态。")
return False
+
+ # 直接修改组件实例的 enabled 状态
component_info.enabled = enabled
logger.info(f"组件 {name} ({component_type.value}) 的全局状态已设置为: {enabled}")
return True
@@ -91,179 +225,82 @@ def set_component_enabled_local(stream_id: str, name: str, component_type: Compo
"""
在一个特定的 stream_id 上下文中临时启用或禁用组件。
- 此状态仅存于内存,不影响全局状态。
+ 此状态仅存在于内存中,并且只对指定的 stream_id 有效,不影响全局组件状态。
+ 同样包含对 Chatter 组件的保护机制。
Args:
- stream_id (str): 上下文标识符。
+ stream_id (str): 唯一的上下文标识符,例如一个会话ID。
name (str): 组件名称。
component_type (ComponentType): 组件类型。
enabled (bool): True 为启用, False 为禁用。
Returns:
- bool: 操作成功则为 True。
+ bool: 如果操作成功,则为 True。
"""
- # 首先,检查组件是否存在
+ # 首先,验证组件是否存在
component_info = component_registry.get_component_info(name, component_type)
if not component_info:
logger.error(f"尝试设置局部状态失败:未找到组件 {name} ({component_type.value})。")
return False
- # Chatter 唯一性保护
+ # Chatter 唯一性保护(在 stream_id 上下文中)
if component_type == ComponentType.CHATTER and not enabled:
- # 检查当前 stream_id 上下文中的启用状态
- enabled_chatters = component_registry.get_enabled_components_by_type(
- ComponentType.CHATTER, stream_id=stream_id
- )
+ # 检查当前 stream_id 上下文中启用的 Chatter
+ enabled_chatters = component_registry.get_enabled_components_by_type(ComponentType.CHATTER, stream_id=stream_id)
if len(enabled_chatters) <= 1 and name in enabled_chatters:
- logger.warning(
- f"操作被阻止:在 stream '{stream_id}' 中,不能禁用最后一个启用的 Chatter 组件 ('{name}')。"
- )
+ logger.warning(f"操作被阻止:在 stream '{stream_id}' 中,不能禁用最后一个启用的 Chatter 组件 ('{name}')。")
return False
-
+
+ # 设置局部状态
component_registry.set_local_component_state(stream_id, name, component_type, enabled)
+ logger.info(f"在 stream '{stream_id}' 中,组件 {name} ({component_type.value}) 的局部状态已设置为: {enabled}")
return True
-def rescan_and_register_plugins(load_after_register: bool = True) -> tuple[int, int]:
- """
- 重新扫描所有插件目录,发现新插件并注册。
-
- Args:
- load_after_register (bool): 如果为 True,新发现的插件将在注册后立即被加载。
-
- Returns:
- Tuple[int, int]: (成功数量, 失败数量)
- """
- success_count, fail_count = plugin_manager.rescan_plugin_directory()
- if not load_after_register:
- return success_count, fail_count
-
- newly_registered = [
- p for p in plugin_manager.list_registered_plugins() if p not in plugin_manager.list_loaded_plugins()
- ]
- loaded_success = 0
- for plugin_name in newly_registered:
- status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
- if status:
- loaded_success += 1
-
- return loaded_success, fail_count + (len(newly_registered) - loaded_success)
-
-
-def register_plugin_from_file(plugin_name: str, load_after_register: bool = True) -> bool:
- """
- 从默认插件目录中查找、注册并加载一个插件。
-
- Args:
- plugin_name (str): 插件的名称(即其目录名)。
- load_after_register (bool): 注册后是否立即加载。
-
- Returns:
- bool: 成功则为 True。
- """
- if plugin_name in plugin_manager.list_loaded_plugins():
- logger.warning(f"插件 '{plugin_name}' 已经加载。")
- return True
-
- # 如果插件未注册,则遍历插件目录去查找
- if plugin_name not in plugin_manager.list_registered_plugins():
- logger.info(f"插件 '{plugin_name}' 未注册,开始在插件目录中搜索...")
- found_path = None
- for directory in plugin_manager.plugin_directories:
- potential_path = os.path.join(directory, plugin_name)
- if os.path.isdir(potential_path):
- found_path = potential_path
- break
-
- if not found_path:
- logger.error(f"在所有插件目录中都未找到名为 '{plugin_name}' 的插件。")
- return False
-
- plugin_file = os.path.join(found_path, "plugin.py")
- if not os.path.exists(plugin_file):
- logger.error(f"在 '{found_path}' 中未找到 plugin.py 文件。")
- return False
-
- module = plugin_manager._load_plugin_module_file(plugin_file)
- if not module:
- logger.error(f"从 '{plugin_file}' 加载插件模块失败。")
- return False
-
- if plugin_name not in plugin_manager.list_registered_plugins():
- logger.error(f"插件 '{plugin_name}' 在加载模块后依然未注册成功。")
- return False
-
- logger.info(f"插件 '{plugin_name}' 已成功发现并注册。")
-
- if load_after_register:
- status, _ = plugin_manager.load_registered_plugin_classes(plugin_name)
- return status
- return True
-
-
-def get_component_count(component_type: ComponentType, stream_id: str | None = None) -> int:
- """
- 获取指定类型的已加载并启用的组件的总数。
-
- 可以根据 stream_id 考虑局部状态。
-
- Args:
- component_type (ComponentType): 要查询的组件类型。
- stream_id (str | None): 可选的上下文ID。
-
- Returns:
- int: 该类型组件的数量。
- """
- return len(component_registry.get_enabled_components_by_type(component_type, stream_id=stream_id))
-
-
-def get_component_info(name: str, component_type: ComponentType) -> ComponentInfo | None:
- """
- 获取任何一个已注册组件的详细信息。
-
- Args:
- name (str): 组件的唯一名称。
- component_type (ComponentType): 组件的类型。
-
- Returns:
- ComponentInfo: 包含组件信息的对象,如果找不到则返回 None。
- """
- return component_registry.get_component_info(name, component_type)
+# --------------------------------------------------------------------------------
+# Section 3: 信息查询与报告 (Information Querying & Reporting)
+# --------------------------------------------------------------------------------
+# 这部分 API 用于获取关于插件和组件的详细信息、列表和统计数据。
def get_system_report() -> dict[str, Any]:
"""
生成一份详细的系统状态报告。
+ 报告包含已加载插件、失败插件和组件的全面信息,是调试和监控系统状态的核心工具。
+
Returns:
- dict: 包含系统、插件和组件状态的详细报告。
+ dict[str, Any]: 包含系统、插件和组件状态的详细报告字典。
"""
loaded_plugins_info = {}
+ # 遍历所有已加载的插件实例
for name, instance in plugin_manager.loaded_plugins.items():
plugin_info = component_registry.get_plugin_info(name)
if not plugin_info:
continue
- components_details = []
- for comp_info in plugin_info.components:
- components_details.append(
- {
- "name": comp_info.name,
- "component_type": comp_info.component_type.value,
- "description": comp_info.description,
- "enabled": comp_info.enabled,
- }
- )
-
- # 从 plugin_info (PluginInfo) 而不是 instance (PluginBase) 获取元数据
+ # 收集该插件下所有组件的详细信息
+ components_details = [
+ {
+ "name": comp_info.name,
+ "component_type": comp_info.component_type.value,
+ "description": comp_info.description,
+ "enabled": comp_info.enabled,
+ }
+ for comp_info in plugin_info.components
+ ]
+
+ # 构建单个插件的信息字典
+ # 元数据从 PluginInfo 获取,而启用状态(enable_plugin)从插件实例获取
loaded_plugins_info[name] = {
"display_name": plugin_info.display_name or name,
"version": plugin_info.version,
"author": plugin_info.author,
- "enabled": instance.enable_plugin, # enable_plugin 状态还是需要从实例获取
+ "enabled": instance.enable_plugin,
"components": components_details,
}
+ # 构建最终的完整报告
report = {
"system_info": {
"loaded_plugins_count": len(plugin_manager.loaded_plugins),
@@ -273,3 +310,228 @@ def get_system_report() -> dict[str, Any]:
"failed_plugins": plugin_manager.failed_plugins,
}
return report
+
+
+def get_plugin_details(plugin_name: str) -> dict[str, Any] | None:
+ """
+ 获取单个插件的详细报告。
+
+ 报告内容包括插件的元数据、所有组件的详细信息及其当前状态。
+ 这是 `get_system_report` 的单插件聚焦版本。
+
+ Args:
+ plugin_name (str): 要查询的插件名称。
+
+ Returns:
+ dict | None: 包含插件详细信息的字典,如果插件未注册则返回 None。
+ """
+ plugin_info = component_registry.get_plugin_info(plugin_name)
+ if not plugin_info:
+ logger.warning(f"尝试获取插件详情失败:未找到名为 '{plugin_name}' 的插件。")
+ return None
+
+ # 收集该插件下所有组件的信息
+ components_details = [
+ {
+ "name": comp_info.name,
+ "component_type": comp_info.component_type.value,
+ "description": comp_info.description,
+ "enabled": comp_info.enabled,
+ }
+ for comp_info in plugin_info.components
+ ]
+
+ # 获取插件实例以检查其启用状态
+ plugin_instance = plugin_manager.get_plugin_instance(plugin_name)
+ is_enabled = plugin_instance.enable_plugin if plugin_instance else False
+
+ # 组装详细信息字典
+ return {
+ "name": plugin_info.name,
+ "display_name": plugin_info.display_name or plugin_info.name,
+ "version": plugin_info.version,
+ "author": plugin_info.author,
+ "license": plugin_info.license,
+ "description": plugin_info.description,
+ "enabled": is_enabled,
+ "status": "loaded" if is_plugin_loaded(plugin_name) else "registered",
+ "components": components_details,
+ }
+
+
+def list_plugins(status: Literal["loaded", "registered", "failed"]) -> list[str]:
+ """
+ 根据指定的状态列出插件名称列表。
+
+ 提供了一种快速、便捷的方式来监控和调试插件系统,而无需解析完整的系统报告。
+
+ Args:
+ status (str): 插件状态,可选值为 'loaded', 'registered', 'failed'。
+
+ Returns:
+ list[str]: 对应状态的插件名称列表。
+
+ Raises:
+ ValueError: 如果传入了无效的状态字符串。
+ """
+ if status == "loaded":
+ # 返回所有当前已成功加载的插件
+ return plugin_manager.list_loaded_plugins()
+ if status == "registered":
+ # 返回所有已注册(但不一定已加载)的插件
+ return plugin_manager.list_registered_plugins()
+ if status == "failed":
+ # 返回所有加载失败的插件的名称
+ return list(plugin_manager.failed_plugins.keys())
+ # 如果状态无效,则引发错误
+ raise ValueError(f"无效的插件状态: '{status}'。有效选项为 'loaded', 'registered', 'failed'。")
+
+
+def list_components(component_type: ComponentType, enabled_only: bool = True) -> list[dict[str, Any]]:
+ """
+ 列出指定类型的所有组件的详细信息。
+
+ 这是查找和管理组件的核心功能,例如,获取所有可用的工具或所有注册的聊天器。
+
+ Args:
+ component_type (ComponentType): 要查询的组件类型。
+ enabled_only (bool, optional): 是否只返回已启用的组件。默认为 True。
+
+ Returns:
+ list[dict[str, Any]]: 一个包含组件信息字典的列表。
+ """
+ # 根据 enabled_only 参数决定是获取所有组件还是仅获取已启用的组件
+ if enabled_only:
+ components = component_registry.get_enabled_components_by_type(component_type)
+ else:
+ components = component_registry.get_components_by_type(component_type)
+
+ # 将组件信息格式化为字典列表
+ return [
+ {
+ "name": info.name,
+ "plugin_name": info.plugin_name,
+ "description": info.description,
+ "enabled": info.enabled,
+ }
+ for info in components.values()
+ ]
+
+
+def search_components_by_name(
+ name_keyword: str,
+ component_type: ComponentType | None = None,
+ case_sensitive: bool = False,
+ exact_match: bool = False,
+) -> list[dict[str, Any]]:
+ """
+ 根据名称关键字搜索组件,支持模糊匹配和精确匹配。
+
+ 极大地增强了组件的可发现性,用户无需知道完整名称即可找到所需组件。
+
+ Args:
+ name_keyword (str): 用于搜索的名称关键字。
+ component_type (ComponentType | None, optional): 如果提供,则只在该类型中搜索。默认为 None (搜索所有类型)。
+ case_sensitive (bool, optional): 是否进行大小写敏感的搜索。默认为 False。
+ exact_match (bool, optional): 是否进行精确匹配。默认为 False (模糊匹配)。
+
+ Returns:
+ list[dict[str, Any]]: 匹配的组件信息字典的列表。
+ """
+ results = []
+ # 如果未指定组件类型,则搜索所有类型
+ types_to_search = [component_type] if component_type else list(ComponentType)
+
+ # 根据是否大小写敏感,预处理搜索关键字
+ compare_str = name_keyword if case_sensitive else name_keyword.lower()
+
+ # 遍历要搜索的组件类型
+ for comp_type in types_to_search:
+ all_components = component_registry.get_components_by_type(comp_type)
+ for name, info in all_components.items():
+ # 同样地,预处理组件名称
+ target_name = name if case_sensitive else name.lower()
+
+ # 根据 exact_match 参数决定使用精确比较还是模糊包含检查
+ is_match = (compare_str == target_name) if exact_match else (compare_str in target_name)
+
+ # 如果匹配,则将组件信息添加到结果列表
+ if is_match:
+ results.append(
+ {
+ "name": info.name,
+ "component_type": info.component_type.value,
+ "plugin_name": info.plugin_name,
+ "description": info.description,
+ "enabled": info.enabled,
+ }
+ )
+ return results
+
+
+def get_component_info(name: str, component_type: ComponentType) -> ComponentInfo | None:
+ """
+ 获取任何一个已注册组件的详细信息对象。
+
+ Args:
+ name (str): 组件的唯一名称。
+ component_type (ComponentType): 组件的类型。
+
+ Returns:
+ ComponentInfo | None: 包含组件完整信息的 ComponentInfo 对象,如果找不到则返回 None。
+ """
+ return component_registry.get_component_info(name, component_type)
+
+
+def get_component_count(component_type: ComponentType, stream_id: str | None = None) -> int:
+ """
+ 获取指定类型的已加载并启用的组件的总数。
+
+ 可以根据 `stream_id` 考虑局部状态,从而获得特定上下文中的组件数量。
+
+ Args:
+ component_type (ComponentType): 要查询的组件类型。
+ stream_id (str | None): 可选的上下文ID。如果提供,将计入局部状态。
+
+ Returns:
+ int: 该类型下已启用的组件的数量。
+ """
+ return len(component_registry.get_enabled_components_by_type(component_type, stream_id=stream_id))
+
+
+# --------------------------------------------------------------------------------
+# Section 4: 工具函数 (Utility Helpers)
+# --------------------------------------------------------------------------------
+# 这部分提供了一些轻量级的辅助函数,用于快速检查状态。
+
+
+def is_plugin_loaded(plugin_name: str) -> bool:
+ """
+ 快速检查一个插件当前是否已成功加载。
+
+ 这是一个比 `get_plugin_details` 更轻量级的检查方法,适用于需要快速布尔值判断的场景。
+
+ Args:
+ plugin_name (str): 要检查的插件名称。
+
+ Returns:
+ bool: 如果插件已加载,则为 True,否则为 False。
+ """
+ return plugin_name in plugin_manager.list_loaded_plugins()
+
+
+def get_component_plugin(component_name: str, component_type: ComponentType) -> str | None:
+ """
+ 查找一个特定组件属于哪个插件。
+
+ 在调试或管理组件时,此函数能够方便地追溯其定义的源头。
+
+ Args:
+ component_name (str): 组件的名称。
+ component_type (ComponentType): 组件的类型。
+
+ Returns:
+ str | None: 组件所属的插件名称,如果找不到组件则返回 None。
+ """
+ component_info = component_registry.get_component_info(component_name, component_type)
+ return component_info.plugin_name if component_info else None
From 725df21215f65cde14ba0a4fbc9a00f63dafcf92 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 21:29:08 +0800
Subject: [PATCH 14/22] =?UTF-8?q?feat(plugin):=20=E7=AE=80=E5=8C=96?=
=?UTF-8?q?=E5=B1=80=E9=83=A8=E7=BB=84=E4=BB=B6=E7=AE=A1=E7=90=86=E5=91=BD?=
=?UTF-8?q?=E4=BB=A4=EF=BC=8C=E8=87=AA=E5=8A=A8=E6=A3=80=E6=B5=8B=E7=BB=84?=
=?UTF-8?q?=E4=BB=B6=E7=B1=BB=E5=9E=8B?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
移除了 `enable_local` 和 `disable_local` 命令中的组件类型参数,改为通过组件名称自动搜索。这简化了用户操作,无需记忆组件的具体类型。
- 当找到多个同名组件时,将提示用户并中止操作,避免歧义。
- 新增保护机制,防止用户局部禁用路由、提示词等核心类型的组件,以增强系统稳定性。
---
.../built_in/system_management/plugin.py | 62 +++++++++++++------
1 file changed, 43 insertions(+), 19 deletions(-)
diff --git a/src/plugins/built_in/system_management/plugin.py b/src/plugins/built_in/system_management/plugin.py
index 681d71c6d..72bc826c6 100644
--- a/src/plugins/built_in/system_management/plugin.py
+++ b/src/plugins/built_in/system_management/plugin.py
@@ -106,8 +106,8 @@ class SystemCommand(PlusCommand):
• `/system plugin reload <插件名>` - 重新加载指定插件
• `/system plugin reload_all` - 重新加载所有插件
🎯 局部控制 (需要 `system.plugin.manage.local` 权限):
-• `/system plugin enable_local <类型> <名称> [group <群号> | private ]` - 在指定会话局部启用组件
-• `/system plugin disable_local <类型> <名称> [group <群号> | private ]` - 在指定会话局部禁用组件
+• `/system plugin enable_local <名称> [group <群号> | private ]` - 在指定会话局部启用组件
+• `/system plugin disable_local <名称> [group <群号> | private ]` - 在指定会话局部禁用组件
"""
elif target == "permission":
help_text = """📋 权限管理命令帮助
@@ -162,9 +162,9 @@ class SystemCommand(PlusCommand):
await self._reload_plugin(remaining_args[0])
elif action in ["reload_all", "重载全部"]:
await self._reload_all_plugins()
- elif action in ["enable_local", "局部启用"] and len(remaining_args) >= 2:
+ elif action in ["enable_local", "局部启用"] and len(remaining_args) >= 1:
await self._set_local_component_state(remaining_args, enabled=True)
- elif action in ["disable_local", "局部禁用"] and len(remaining_args) >= 2:
+ elif action in ["disable_local", "局部禁用"] and len(remaining_args) >= 1:
await self._set_local_component_state(remaining_args, enabled=False)
else:
await self.send_text("❌ 插件管理命令不合法\n使用 /system plugin help 查看帮助")
@@ -498,24 +498,53 @@ class SystemCommand(PlusCommand):
@require_permission("plugin.manage.local", deny_message="❌ 你没有局部管理插件组件的权限")
async def _set_local_component_state(self, args: list[str], enabled: bool):
"""在局部范围内启用或禁用一个组件"""
- # 命令格式: [group | private ]
- if len(args) < 2:
+ # 命令格式: [group | private ]
+ if not args:
action = "enable_local" if enabled else "disable_local"
- await self.send_text(f"❌ 用法: /system plugin {action} <类型> <名称> [group <群号> | private ]")
+ await self.send_text(f"❌ 用法: /system plugin {action} <名称> [group <群号> | private ]")
return
- comp_type_str = args[0]
- comp_name = args[1]
+ comp_name = args[0]
+ context_args = args[1:]
stream_id = self.message.chat_info.stream_id # 默认作用于当前会话
- if len(args) >= 4:
- context_type = args[2].lower()
- context_id = args[3]
+ # 1. 搜索组件
+ found_components = plugin_manage_api.search_components_by_name(comp_name, exact_match=True)
+
+ if not found_components:
+ await self.send_text(f"❌ 未找到名为 '{comp_name}' 的组件。")
+ return
+
+ if len(found_components) > 1:
+ suggestions = "\n".join([f"- `{c['name']}` (类型: {c['component_type']})" for c in found_components])
+ await self.send_text(f"❌ 发现多个名为 '{comp_name}' 的组件,操作已取消。\n找到的组件:\n{suggestions}")
+ return
+
+ component_info = found_components[0]
+ comp_type_str = component_info["component_type"]
+ component_type = ComponentType(comp_type_str)
+
+ # 2. 增加禁用保护
+ if not enabled: # 如果是禁用操作
+ # 定义不可禁用的核心组件类型
+ protected_types = [
+ ComponentType.INTEREST_CALCULATOR,
+ ComponentType.PROMPT,
+ ComponentType.ROUTER,
+ ]
+ if component_type in protected_types:
+ await self.send_text(f"❌ 无法局部禁用核心组件 '{comp_name}' ({comp_type_str})。")
+ return
+
+ # 3. 解析上下文
+ if len(context_args) >= 2:
+ context_type = context_args[0].lower()
+ context_id = context_args[1]
target_stream = None
if context_type == "group":
target_stream = chat_api.get_stream_by_group_id(
- group_id=context_id,
+ group_id=context_id,
platform=self.message.chat_info.platform
)
elif context_type == "private":
@@ -533,12 +562,7 @@ class SystemCommand(PlusCommand):
stream_id = target_stream.stream_id
- try:
- component_type = ComponentType(comp_type_str.lower())
- except ValueError:
- await self.send_text(f"❌ 无效的组件类型: '{comp_type_str}'。有效类型: {', '.join([t.value for t in ComponentType])}")
- return
-
+ # 4. 执行操作
success = plugin_manage_api.set_component_enabled_local(
stream_id=stream_id,
name=comp_name,
From 9f342bd5b26342926654743d8f25710bdf59bfaa Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 21:47:01 +0800
Subject: [PATCH 15/22] =?UTF-8?q?refactor(individuality):=20=E7=A7=BB?=
=?UTF-8?q?=E9=99=A4=E5=BA=9F=E5=BC=83=E7=9A=84=E5=9C=BA=E6=99=AF=E5=BC=8F?=
=?UTF-8?q?=E4=BA=BA=E6=A0=BC=E7=94=9F=E6=88=90=E6=A8=A1=E5=9D=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
删除位于 `src/individuality/not_using/` 目录下的整套人格生成系统。
该系统是早期用于通过预设场景问答和 LLM 评分来构建 Bot 人格的一种尝试。由于此方法已被弃用且不再集成于当前工作流中,为保持代码库的简洁性和可维护性,决定将其完全移除。
---
src/individuality/not_using/offline_llm.py | 127 --------
src/individuality/not_using/per_bf_gen.py | 307 ------------------
src/individuality/not_using/questionnaire.py | 142 --------
src/individuality/not_using/scene.py | 44 ---
.../not_using/template_scene.json | 112 -------
src/plugin_system/apis/plugin_manage_api.py | 1 -
6 files changed, 733 deletions(-)
delete mode 100644 src/individuality/not_using/offline_llm.py
delete mode 100644 src/individuality/not_using/per_bf_gen.py
delete mode 100644 src/individuality/not_using/questionnaire.py
delete mode 100644 src/individuality/not_using/scene.py
delete mode 100644 src/individuality/not_using/template_scene.json
diff --git a/src/individuality/not_using/offline_llm.py b/src/individuality/not_using/offline_llm.py
deleted file mode 100644
index 752293ab8..000000000
--- a/src/individuality/not_using/offline_llm.py
+++ /dev/null
@@ -1,127 +0,0 @@
-import asyncio
-import os
-import time
-
-import aiohttp
-import requests
-from rich.traceback import install
-
-from src.common.logger import get_logger
-from src.common.tcp_connector import get_tcp_connector
-
-install(extra_lines=3)
-
-logger = get_logger("offline_llm")
-
-
-class LLMRequestOff:
- def __init__(self, model_name="Pro/deepseek-ai/DeepSeek-V3", **kwargs):
- self.model_name = model_name
- self.params = kwargs
- self.api_key = os.getenv("SILICONFLOW_KEY")
- self.base_url = os.getenv("SILICONFLOW_BASE_URL")
-
- if not self.api_key or not self.base_url:
- raise ValueError("环境变量未正确加载:SILICONFLOW_KEY 或 SILICONFLOW_BASE_URL 未设置")
-
- # logger.info(f"API URL: {self.base_url}") # 使用 logger 记录 base_url
-
- def generate_response(self, prompt: str) -> str | tuple[str, str]:
- """根据输入的提示生成模型的响应"""
- headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
-
- # 构建请求体
- data = {
- "model": self.model_name,
- "messages": [{"role": "user", "content": prompt}],
- "temperature": 0.4,
- **self.params,
- }
-
- # 发送请求到完整的 chat/completions 端点
- api_url = f"{self.base_url.rstrip('/')}/chat/completions" # type: ignore
- logger.info(f"Request URL: {api_url}") # 记录请求的 URL
-
- max_retries = 3
- base_wait_time = 15 # 基础等待时间(秒)
-
- for retry in range(max_retries):
- try:
- response = requests.post(api_url, headers=headers, json=data)
-
- if response.status_code == 429:
- wait_time = base_wait_time * (2**retry) # 指数退避
- logger.warning(f"遇到请求限制(429),等待{wait_time}秒后重试...")
- time.sleep(wait_time)
- continue
-
- response.raise_for_status() # 检查其他响应状态
-
- result = response.json()
- if "choices" in result and len(result["choices"]) > 0:
- content = result["choices"][0]["message"]["content"]
- reasoning_content = result["choices"][0]["message"].get("reasoning_content", "")
- return content, reasoning_content
- return "没有返回结果", ""
-
- except Exception as e:
- if retry < max_retries - 1: # 如果还有重试机会
- wait_time = base_wait_time * (2**retry)
- logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}")
- time.sleep(wait_time)
- else:
- logger.error(f"请求失败: {e!s}")
- return f"请求失败: {e!s}", ""
-
- logger.error("达到最大重试次数,请求仍然失败")
- return "达到最大重试次数,请求仍然失败", ""
-
- async def generate_response_async(self, prompt: str) -> str | tuple[str, str]:
- """异步方式根据输入的提示生成模型的响应"""
- headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json"}
-
- # 构建请求体
- data = {
- "model": self.model_name,
- "messages": [{"role": "user", "content": prompt}],
- "temperature": 0.5,
- **self.params,
- }
-
- # 发送请求到完整的 chat/completions 端点
- api_url = f"{self.base_url.rstrip('/')}/chat/completions" # type: ignore
- logger.info(f"Request URL: {api_url}") # 记录请求的 URL
-
- max_retries = 3
- base_wait_time = 15
-
- async with aiohttp.ClientSession(connector=await get_tcp_connector()) as session:
- for retry in range(max_retries):
- try:
- async with session.post(api_url, headers=headers, json=data) as response:
- if response.status == 429:
- wait_time = base_wait_time * (2**retry) # 指数退避
- logger.warning(f"遇到请求限制(429),等待{wait_time}秒后重试...")
- await asyncio.sleep(wait_time)
- continue
-
- response.raise_for_status() # 检查其他响应状态
-
- result = await response.json()
- if "choices" in result and len(result["choices"]) > 0:
- content = result["choices"][0]["message"]["content"]
- reasoning_content = result["choices"][0]["message"].get("reasoning_content", "")
- return content, reasoning_content
- return "没有返回结果", ""
-
- except Exception as e:
- if retry < max_retries - 1: # 如果还有重试机会
- wait_time = base_wait_time * (2**retry)
- logger.error(f"[回复]请求失败,等待{wait_time}秒后重试... 错误: {e!s}")
- await asyncio.sleep(wait_time)
- else:
- logger.error(f"请求失败: {e!s}")
- return f"请求失败: {e!s}", ""
-
- logger.error("达到最大重试次数,请求仍然失败")
- return "达到最大重试次数,请求仍然失败", ""
diff --git a/src/individuality/not_using/per_bf_gen.py b/src/individuality/not_using/per_bf_gen.py
deleted file mode 100644
index 326a94aaf..000000000
--- a/src/individuality/not_using/per_bf_gen.py
+++ /dev/null
@@ -1,307 +0,0 @@
-import os
-import random
-import sys
-
-import orjson
-import toml
-from dotenv import load_dotenv
-from tqdm import tqdm
-
-# 添加项目根目录到 Python 路径
-root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
-sys.path.append(root_path)
-
-# 加载配置文件
-config_path = os.path.join(root_path, "config", "bot_config.toml")
-with open(config_path, encoding="utf-8") as f:
- config = toml.load(f)
-
-# 现在可以导入src模块
-from individuality.not_using.scene import get_scene_by_factor, PERSONALITY_SCENES # noqa E402
-from individuality.not_using.questionnaire import FACTOR_DESCRIPTIONS
-from individuality.not_using.offline_llm import LLMRequestOff
-
-# 加载环境变量
-env_path = os.path.join(root_path, ".env")
-if os.path.exists(env_path):
- print(f"从 {env_path} 加载环境变量")
- load_dotenv(env_path)
-else:
- print(f"未找到环境变量文件: {env_path}")
- print("将使用默认配置")
-
-
-def adapt_scene(scene: str) -> str:
- personality_core = config["personality"]["personality_core"]
- personality_side = config["personality"]["personality_side"]
- personality_side = random.choice(personality_side)
- identitys = config["identity"]["identity"]
- identity = random.choice(identitys)
-
- """
- 根据config中的属性,改编场景使其更适合当前角色
-
- Args:
- scene: 原始场景描述
-
- Returns:
- str: 改编后的场景描述
- """
- try:
- prompt = f"""
-这是一个参与人格测评的角色形象:
-- 昵称: {config["bot"]["nickname"]}
-- 性别: {config["identity"]["gender"]}
-- 年龄: {config["identity"]["age"]}岁
-- 外貌: {config["identity"]["appearance"]}
-- 性格核心: {personality_core}
-- 性格侧面: {personality_side}
-- 身份细节: {identity}
-
-请根据上述形象,改编以下场景,在测评中,用户将根据该场景给出上述角色形象的反应:
-{scene}
-保持场景的本质不变,但最好贴近生活且具体,并且让它更适合这个角色。
-改编后的场景应该自然、连贯,并考虑角色的年龄、身份和性格特点。只返回改编后的场景描述,不要包含其他说明。注意{config["bot"]["nickname"]}是面对这个场景的人,而不是场景的其他人。场景中不会有其描述,
-现在,请你给出改编后的场景描述
-"""
-
- llm = LLMRequestOff(model_name=config["model"]["llm_normal"]["name"])
- adapted_scene, _ = llm.generate_response(prompt)
-
- # 检查返回的场景是否为空或错误信息
- if not adapted_scene or "错误" in adapted_scene or "失败" in adapted_scene:
- print("场景改编失败,将使用原始场景")
- return scene
-
- return adapted_scene
- except Exception as e:
- print(f"场景改编过程出错:{e!s},将使用原始场景")
- return scene
-
-
-class PersonalityEvaluatorDirect:
- def __init__(self):
- self.personality_traits = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0}
- self.scenarios = []
- self.final_scores: dict[str, float] = {"开放性": 0, "严谨性": 0, "外向性": 0, "宜人性": 0, "神经质": 0}
- self.dimension_counts = dict.fromkeys(self.final_scores, 0)
-
- # 为每个人格特质获取对应的场景
- for trait in PERSONALITY_SCENES:
- scenes = get_scene_by_factor(trait)
- if not scenes:
- continue
-
- # 从每个维度选择3个场景
- import random
-
- scene_keys = list(scenes.keys())
- selected_scenes = random.sample(scene_keys, min(3, len(scene_keys)))
-
- for scene_key in selected_scenes:
- scene = scenes[scene_key]
-
- # 为每个场景添加评估维度
- # 主维度是当前特质,次维度随机选择一个其他特质
- other_traits = [t for t in PERSONALITY_SCENES if t != trait]
- secondary_trait = random.choice(other_traits)
-
- self.scenarios.append(
- {"场景": scene["scenario"], "评估维度": [trait, secondary_trait], "场景编号": scene_key}
- )
-
- self.llm = LLMRequestOff()
-
- def evaluate_response(self, scenario: str, response: str, dimensions: list[str]) -> dict[str, float]:
- """
- 使用 DeepSeek AI 评估用户对特定场景的反应
- """
- # 构建维度描述
- dimension_descriptions = [f"- {dim}:{desc}" for dim in dimensions if (desc := FACTOR_DESCRIPTIONS.get(dim, ""))]
-
- dimensions_text = "\n".join(dimension_descriptions)
-
- prompt = f"""请根据以下场景和用户描述,评估用户在大五人格模型中的相关维度得分(1-6分)。
-
-场景描述:
-{scenario}
-
-用户回应:
-{response}
-
-需要评估的维度说明:
-{dimensions_text}
-
-请按照以下格式输出评估结果(仅输出JSON格式):
-{{
- "{dimensions[0]}": 分数,
- "{dimensions[1]}": 分数
-}}
-
-评分标准:
-1 = 非常不符合该维度特征
-2 = 比较不符合该维度特征
-3 = 有点不符合该维度特征
-4 = 有点符合该维度特征
-5 = 比较符合该维度特征
-6 = 非常符合该维度特征
-
-请根据用户的回应,结合场景和维度说明进行评分。确保分数在1-6之间,并给出合理的评估。"""
-
- try:
- ai_response, _ = self.llm.generate_response(prompt)
- # 尝试从AI响应中提取JSON部分
- start_idx = ai_response.find("{")
- end_idx = ai_response.rfind("}") + 1
- if start_idx != -1 and end_idx != 0:
- json_str = ai_response[start_idx:end_idx]
- scores = orjson.loads(json_str)
- # 确保所有分数在1-6之间
- return {k: max(1, min(6, float(v))) for k, v in scores.items()}
- else:
- print("AI响应格式不正确,使用默认评分")
- return dict.fromkeys(dimensions, 3.5)
- except Exception as e:
- print(f"评估过程出错:{e!s}")
- return dict.fromkeys(dimensions, 3.5)
-
- def run_evaluation(self):
- """
- 运行整个评估过程
- """
- print(f"欢迎使用{config['bot']['nickname']}形象创建程序!")
- print("接下来,将给您呈现一系列有关您bot的场景(共15个)。")
- print("请想象您的bot在以下场景下会做什么,并描述您的bot的反应。")
- print("每个场景都会进行不同方面的评估。")
- print("\n角色基本信息:")
- print(f"- 昵称:{config['bot']['nickname']}")
- print(f"- 性格核心:{config['personality']['personality_core']}")
- print(f"- 性格侧面:{config['personality']['personality_side']}")
- print(f"- 身份细节:{config['identity']['identity']}")
- print("\n准备好了吗?按回车键开始...")
- input()
-
- total_scenarios = len(self.scenarios)
- progress_bar = tqdm(
- total=total_scenarios,
- desc="场景进度",
- ncols=100,
- bar_format="{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}]",
- )
-
- for _i, scenario_data in enumerate(self.scenarios, 1):
- # print(f"\n{'-' * 20} 场景 {i}/{total_scenarios} - {scenario_data['场景编号']} {'-' * 20}")
-
- # 改编场景,使其更适合当前角色
- print(f"{config['bot']['nickname']}祈祷中...")
- adapted_scene = adapt_scene(scenario_data["场景"])
- scenario_data["改编场景"] = adapted_scene
-
- print(adapted_scene)
- print(f"\n请描述{config['bot']['nickname']}在这种情况下会如何反应:")
- response = input().strip()
-
- if not response:
- print("反应描述不能为空!")
- continue
-
- print("\n正在评估您的描述...")
- scores = self.evaluate_response(adapted_scene, response, scenario_data["评估维度"])
-
- # 更新最终分数
- for dimension, score in scores.items():
- self.final_scores[dimension] += score
- self.dimension_counts[dimension] += 1
-
- print("\n当前评估结果:")
- print("-" * 30)
- for dimension, score in scores.items():
- print(f"{dimension}: {score}/6")
-
- # 更新进度条
- progress_bar.update(1)
-
- # if i < total_scenarios:
- # print("\n按回车键继续下一个场景...")
- # input()
-
- progress_bar.close()
-
- # 计算平均分
- for dimension in self.final_scores:
- if self.dimension_counts[dimension] > 0:
- self.final_scores[dimension] = round(self.final_scores[dimension] / self.dimension_counts[dimension], 2)
-
- print("\n" + "=" * 50)
- print(f" {config['bot']['nickname']}的人格特征评估结果 ".center(50))
- print("=" * 50)
- for trait, score in self.final_scores.items():
- print(f"{trait}: {score}/6".ljust(20) + f"测试场景数:{self.dimension_counts[trait]}".rjust(30))
- print("=" * 50)
-
- # 返回评估结果
- return self.get_result()
-
- def get_result(self):
- """
- 获取评估结果
- """
- return {
- "final_scores": self.final_scores,
- "dimension_counts": self.dimension_counts,
- "scenarios": self.scenarios,
- "bot_info": {
- "nickname": config["bot"]["nickname"],
- "gender": config["identity"]["gender"],
- "age": config["identity"]["age"],
- "height": config["identity"]["height"],
- "weight": config["identity"]["weight"],
- "appearance": config["identity"]["appearance"],
- "personality_core": config["personality"]["personality_core"],
- "personality_side": config["personality"]["personality_side"],
- "identity": config["identity"]["identity"],
- },
- }
-
-
-def main():
- evaluator = PersonalityEvaluatorDirect()
- result = evaluator.run_evaluation()
-
- # 准备简化的结果数据
- simplified_result = {
- "openness": round(result["final_scores"]["开放性"] / 6, 1), # 转换为0-1范围
- "conscientiousness": round(result["final_scores"]["严谨性"] / 6, 1),
- "extraversion": round(result["final_scores"]["外向性"] / 6, 1),
- "agreeableness": round(result["final_scores"]["宜人性"] / 6, 1),
- "neuroticism": round(result["final_scores"]["神经质"] / 6, 1),
- "bot_nickname": config["bot"]["nickname"],
- }
-
- # 确保目录存在
- save_dir = os.path.join(root_path, "data", "personality")
- os.makedirs(save_dir, exist_ok=True)
-
- # 创建文件名,替换可能的非法字符
- bot_name = config["bot"]["nickname"]
- # 替换Windows文件名中不允许的字符
- for char in ["\\", "/", ":", "*", "?", '"', "<", ">", "|"]:
- bot_name = bot_name.replace(char, "_")
-
- file_name = f"{bot_name}_personality.per"
- save_path = os.path.join(save_dir, file_name)
-
- # 保存简化的结果
- with open(save_path, "w", encoding="utf-8") as f:
- f.write(orjson.dumps(simplified_result, option=orjson.OPT_INDENT_2).decode("utf-8"))
-
- print(f"\n结果已保存到 {save_path}")
-
- # 同时保存完整结果到results目录
- os.makedirs("results", exist_ok=True)
- with open("results/personality_result.json", "w", encoding="utf-8") as f:
- f.write(orjson.dumps(result, option=orjson.OPT_INDENT_2).decode("utf-8"))
-
-
-if __name__ == "__main__":
- main()
diff --git a/src/individuality/not_using/questionnaire.py b/src/individuality/not_using/questionnaire.py
deleted file mode 100644
index 8e965061d..000000000
--- a/src/individuality/not_using/questionnaire.py
+++ /dev/null
@@ -1,142 +0,0 @@
-# 人格测试问卷题目
-# 王孟成, 戴晓阳, & 姚树桥. (2011).
-# 中国大五人格问卷的初步编制Ⅲ:简式版的制定及信效度检验. 中国临床心理学杂志, 19(04), Article 04.
-
-# 王孟成, 戴晓阳, & 姚树桥. (2010).
-# 中国大五人格问卷的初步编制Ⅰ:理论框架与信度分析. 中国临床心理学杂志, 18(05), Article 05.
-
-PERSONALITY_QUESTIONS = [
- # 神经质维度 (F1)
- {"id": 1, "content": "我常担心有什么不好的事情要发生", "factor": "神经质", "reverse_scoring": False},
- {"id": 2, "content": "我常感到害怕", "factor": "神经质", "reverse_scoring": False},
- {"id": 3, "content": "有时我觉得自己一无是处", "factor": "神经质", "reverse_scoring": False},
- {"id": 4, "content": "我很少感到忧郁或沮丧", "factor": "神经质", "reverse_scoring": True},
- {"id": 5, "content": "别人一句漫不经心的话,我常会联系在自己身上", "factor": "神经质", "reverse_scoring": False},
- {"id": 6, "content": "在面对压力时,我有种快要崩溃的感觉", "factor": "神经质", "reverse_scoring": False},
- {"id": 7, "content": "我常担忧一些无关紧要的事情", "factor": "神经质", "reverse_scoring": False},
- {"id": 8, "content": "我常常感到内心不踏实", "factor": "神经质", "reverse_scoring": False},
- # 严谨性维度 (F2)
- {"id": 9, "content": "在工作上,我常只求能应付过去便可", "factor": "严谨性", "reverse_scoring": True},
- {"id": 10, "content": "一旦确定了目标,我会坚持努力地实现它", "factor": "严谨性", "reverse_scoring": False},
- {"id": 11, "content": "我常常是仔细考虑之后才做出决定", "factor": "严谨性", "reverse_scoring": False},
- {"id": 12, "content": "别人认为我是个慎重的人", "factor": "严谨性", "reverse_scoring": False},
- {"id": 13, "content": "做事讲究逻辑和条理是我的一个特点", "factor": "严谨性", "reverse_scoring": False},
- {"id": 14, "content": "我喜欢一开头就把事情计划好", "factor": "严谨性", "reverse_scoring": False},
- {"id": 15, "content": "我工作或学习很勤奋", "factor": "严谨性", "reverse_scoring": False},
- {"id": 16, "content": "我是个倾尽全力做事的人", "factor": "严谨性", "reverse_scoring": False},
- # 宜人性维度 (F3)
- {
- "id": 17,
- "content": "尽管人类社会存在着一些阴暗的东西(如战争、罪恶、欺诈),我仍然相信人性总的来说是善良的",
- "factor": "宜人性",
- "reverse_scoring": False,
- },
- {"id": 18, "content": "我觉得大部分人基本上是心怀善意的", "factor": "宜人性", "reverse_scoring": False},
- {"id": 19, "content": "虽然社会上有骗子,但我觉得大部分人还是可信的", "factor": "宜人性", "reverse_scoring": False},
- {"id": 20, "content": "我不太关心别人是否受到不公正的待遇", "factor": "宜人性", "reverse_scoring": True},
- {"id": 21, "content": "我时常觉得别人的痛苦与我无关", "factor": "宜人性", "reverse_scoring": True},
- {"id": 22, "content": "我常为那些遭遇不幸的人感到难过", "factor": "宜人性", "reverse_scoring": False},
- {"id": 23, "content": "我是那种只照顾好自己,不替别人担忧的人", "factor": "宜人性", "reverse_scoring": True},
- {"id": 24, "content": "当别人向我诉说不幸时,我常感到难过", "factor": "宜人性", "reverse_scoring": False},
- # 开放性维度 (F4)
- {"id": 25, "content": "我的想象力相当丰富", "factor": "开放性", "reverse_scoring": False},
- {"id": 26, "content": "我头脑中经常充满生动的画面", "factor": "开放性", "reverse_scoring": False},
- {"id": 27, "content": "我对许多事情有着很强的好奇心", "factor": "开放性", "reverse_scoring": False},
- {"id": 28, "content": "我喜欢冒险", "factor": "开放性", "reverse_scoring": False},
- {"id": 29, "content": "我是个勇于冒险,突破常规的人", "factor": "开放性", "reverse_scoring": False},
- {"id": 30, "content": "我身上具有别人没有的冒险精神", "factor": "开放性", "reverse_scoring": False},
- {
- "id": 31,
- "content": "我渴望学习一些新东西,即使它们与我的日常生活无关",
- "factor": "开放性",
- "reverse_scoring": False,
- },
- {
- "id": 32,
- "content": "我很愿意也很容易接受那些新事物、新观点、新想法",
- "factor": "开放性",
- "reverse_scoring": False,
- },
- # 外向性维度 (F5)
- {"id": 33, "content": "我喜欢参加社交与娱乐聚会", "factor": "外向性", "reverse_scoring": False},
- {"id": 34, "content": "我对人多的聚会感到乏味", "factor": "外向性", "reverse_scoring": True},
- {"id": 35, "content": "我尽量避免参加人多的聚会和嘈杂的环境", "factor": "外向性", "reverse_scoring": True},
- {"id": 36, "content": "在热闹的聚会上,我常常表现主动并尽情玩耍", "factor": "外向性", "reverse_scoring": False},
- {"id": 37, "content": "有我在的场合一般不会冷场", "factor": "外向性", "reverse_scoring": False},
- {"id": 38, "content": "我希望成为领导者而不是被领导者", "factor": "外向性", "reverse_scoring": False},
- {"id": 39, "content": "在一个团体中,我希望处于领导地位", "factor": "外向性", "reverse_scoring": False},
- {"id": 40, "content": "别人多认为我是一个热情和友好的人", "factor": "外向性", "reverse_scoring": False},
-]
-
-# 因子维度说明
-FACTOR_DESCRIPTIONS = {
- "外向性": {
- "description": "反映个体神经系统的强弱和动力特征。外向性主要表现为个体在人际交往和社交活动中的倾向性,"
- "包括对社交活动的兴趣、"
- "对人群的态度、社交互动中的主动程度以及在群体中的影响力。高分者倾向于积极参与社交活动,乐于与人交往,善于表达自我,"
- "并往往在群体中发挥领导作用;低分者则倾向于独处,不喜欢热闹的社交场合,表现出内向、安静的特征。",
- "trait_words": ["热情", "活力", "社交", "主动"],
- "subfactors": {
- "合群性": "个体愿意与他人聚在一起,即接近人群的倾向;高分表现乐群、好交际,低分表现封闭、独处",
- "热情": "个体对待别人时所表现出的态度;高分表现热情好客,低分表现冷淡",
- "支配性": "个体喜欢指使、操纵他人,倾向于领导别人的特点;高分表现好强、发号施令,低分表现顺从、低调",
- "活跃": "个体精力充沛,活跃、主动性等特点;高分表现活跃,低分表现安静",
- },
- },
- "神经质": {
- "description": "反映个体情绪的状态和体验内心苦恼的倾向性。这个维度主要关注个体在面对压力、"
- "挫折和日常生活挑战时的情绪稳定性和适应能力。它包含了对焦虑、抑郁、愤怒等负面情绪的敏感程度,"
- "以及个体对这些情绪的调节和控制能力。高分者容易体验负面情绪,对压力较为敏感,情绪波动较大;"
- "低分者则表现出较强的情绪稳定性,能够较好地应对压力和挫折。",
- "trait_words": ["稳定", "沉着", "从容", "坚韧"],
- "subfactors": {
- "焦虑": "个体体验焦虑感的个体差异;高分表现坐立不安,低分表现平静",
- "抑郁": "个体体验抑郁情感的个体差异;高分表现郁郁寡欢,低分表现平静",
- "敏感多疑": "个体常常关注自己的内心活动,行为和过于意识人对自己的看法、评价;高分表现敏感多疑,"
- "低分表现淡定、自信",
- "脆弱性": "个体在危机或困难面前无力、脆弱的特点;高分表现无能、易受伤、逃避,低分表现坚强",
- "愤怒-敌意": "个体准备体验愤怒,及相关情绪的状态;高分表现暴躁易怒,低分表现平静",
- },
- },
- "严谨性": {
- "description": "反映个体在目标导向行为上的组织、坚持和动机特征。这个维度体现了个体在工作、"
- "学习等目标性活动中的自我约束和行为管理能力。它涉及到个体的责任感、自律性、计划性、条理性以及完成任务的态度。"
- "高分者往往表现出强烈的责任心、良好的组织能力、谨慎的决策风格和持续的努力精神;低分者则可能表现出随意性强、"
- "缺乏规划、做事马虎或易放弃的特点。",
- "trait_words": ["负责", "自律", "条理", "勤奋"],
- "subfactors": {
- "责任心": "个体对待任务和他人认真负责,以及对自己承诺的信守;高分表现有责任心、负责任,"
- "低分表现推卸责任、逃避处罚",
- "自我控制": "个体约束自己的能力,及自始至终的坚持性;高分表现自制、有毅力,低分表现冲动、无毅力",
- "审慎性": "个体在采取具体行动前的心理状态;高分表现谨慎、小心,低分表现鲁莽、草率",
- "条理性": "个体处理事务和工作的秩序,条理和逻辑性;高分表现整洁、有秩序,低分表现混乱、遗漏",
- "勤奋": "个体工作和学习的努力程度及为达到目标而表现出的进取精神;高分表现勤奋、刻苦,低分表现懒散",
- },
- },
- "开放性": {
- "description": "反映个体对新异事物、新观念和新经验的接受程度,以及在思维和行为方面的创新倾向。"
- "这个维度体现了个体在认知和体验方面的广度、深度和灵活性。它包括对艺术的欣赏能力、对知识的求知欲、想象力的丰富程度,"
- "以及对冒险和创新的态度。高分者往往具有丰富的想象力、广泛的兴趣、开放的思维方式和创新的倾向;低分者则倾向于保守、"
- "传统,喜欢熟悉和常规的事物。",
- "trait_words": ["创新", "好奇", "艺术", "冒险"],
- "subfactors": {
- "幻想": "个体富于幻想和想象的水平;高分表现想象力丰富,低分表现想象力匮乏",
- "审美": "个体对于艺术和美的敏感与热爱程度;高分表现富有艺术气息,低分表现一般对艺术不敏感",
- "好奇心": "个体对未知事物的态度;高分表现兴趣广泛、好奇心浓,低分表现兴趣少、无好奇心",
- "冒险精神": "个体愿意尝试有风险活动的个体差异;高分表现好冒险,低分表现保守",
- "价值观念": "个体对新事物、新观念、怪异想法的态度;高分表现开放、坦然接受新事物,低分则相反",
- },
- },
- "宜人性": {
- "description": "反映个体在人际关系中的亲和倾向,体现了对他人的关心、同情和合作意愿。"
- "这个维度主要关注个体与他人互动时的态度和行为特征,包括对他人的信任程度、同理心水平、"
- "助人意愿以及在人际冲突中的处理方式。高分者通常表现出友善、富有同情心、乐于助人的特质,善于与他人建立和谐关系;"
- "低分者则可能表现出较少的人际关注,在社交互动中更注重自身利益,较少考虑他人感受。",
- "trait_words": ["友善", "同理", "信任", "合作"],
- "subfactors": {
- "信任": "个体对他人和/或他人言论的相信程度;高分表现信任他人,低分表现怀疑",
- "体贴": "个体对别人的兴趣和需要的关注程度;高分表现体贴、温存,低分表现冷漠、不在乎",
- "同情": "个体对处于不利地位的人或物的态度;高分表现富有同情心,低分表现冷漠",
- },
- },
-}
diff --git a/src/individuality/not_using/scene.py b/src/individuality/not_using/scene.py
deleted file mode 100644
index 9c16358e6..000000000
--- a/src/individuality/not_using/scene.py
+++ /dev/null
@@ -1,44 +0,0 @@
-import os
-from typing import Any
-
-import orjson
-
-
-def load_scenes() -> dict[str, Any]:
- """
- 从JSON文件加载场景数据
-
- Returns:
- Dict: 包含所有场景的字典
- """
- current_dir = os.path.dirname(os.path.abspath(__file__))
- json_path = os.path.join(current_dir, "template_scene.json")
-
- with open(json_path, encoding="utf-8") as f:
- return orjson.loads(f.read())
-
-
-PERSONALITY_SCENES = load_scenes()
-
-
-def get_scene_by_factor(factor: str) -> dict | None:
- """
- 根据人格因子获取对应的情景测试
-
- Args:
- factor (str): 人格因子名称
-
- Returns:
- dict: 包含情景描述的字典
- """
- return PERSONALITY_SCENES.get(factor, None)
-
-
-def get_all_scenes() -> dict:
- """
- 获取所有情景测试
-
- Returns:
- Dict: 所有情景测试的字典
- """
- return PERSONALITY_SCENES
diff --git a/src/individuality/not_using/template_scene.json b/src/individuality/not_using/template_scene.json
deleted file mode 100644
index a6542e75d..000000000
--- a/src/individuality/not_using/template_scene.json
+++ /dev/null
@@ -1,112 +0,0 @@
-{
- "外向性": {
- "场景1": {
- "scenario": "你刚刚搬到一个新的城市工作。今天是你入职的第一天,在公司的电梯里,一位同事微笑着和你打招呼:\n\n同事:「嗨!你是新来的同事吧?我是市场部的小林。」\n\n同事看起来很友善,还主动介绍说:「待会午饭时间,我们部门有几个人准备一起去楼下新开的餐厅,你要一起来吗?可以认识一下其他同事。」",
- "explanation": "这个场景通过职场社交情境,观察个体对于新环境、新社交圈的态度和反应倾向。"
- },
- "场景2": {
- "scenario": "在大学班级群里,班长发起了一个组织班级联谊活动的投票:\n\n班长:「大家好!下周末我们准备举办一次班级联谊活动,地点在学校附近的KTV。想请大家报名参加,也欢迎大家邀请其他班级的同学!」\n\n已经有几个同学在群里积极响应,有人@你问你要不要一起参加。",
- "explanation": "通过班级活动场景,观察个体对群体社交活动的参与意愿。"
- },
- "场景3": {
- "scenario": "你在社交平台上发布了一条动态,收到了很多陌生网友的评论和私信:\n\n网友A:「你说的这个观点很有意思!想和你多交流一下。」\n\n网友B:「我也对这个话题很感兴趣,要不要建个群一起讨论?」",
- "explanation": "通过网络社交场景,观察个体对线上社交的态度。"
- },
- "场景4": {
- "scenario": "你暗恋的对象今天主动来找你:\n\n对方:「那个...我最近在准备一个演讲比赛,听说你口才很好。能不能请你帮我看看演讲稿,顺便给我一些建议?如果你有时间的话,可以一起吃个饭聊聊。」",
- "explanation": "通过恋爱情境,观察个体在面对心仪对象时的社交表现。"
- },
- "场景5": {
- "scenario": "在一次线下读书会上,主持人突然点名让你分享读后感:\n\n主持人:「听说你对这本书很有见解,能不能和大家分享一下你的想法?」\n\n现场有二十多个陌生的读书爱好者,都期待地看着你。",
- "explanation": "通过即兴发言场景,观察个体的社交表现欲和公众表达能力。"
- }
- },
- "神经质": {
- "场景1": {
- "scenario": "你正在准备一个重要的项目演示,这关系到你的晋升机会。就在演示前30分钟,你收到了主管发来的消息:\n\n主管:「临时有个变动,CEO也会来听你的演示。他对这个项目特别感兴趣。」\n\n正当你准备回复时,主管又发来一条:「对了,能不能把演示时间压缩到15分钟?CEO下午还有其他安排。你之前准备的是30分钟的版本对吧?」",
- "explanation": "这个场景通过突发的压力情境,观察个体在面对计划外变化时的情绪反应和调节能力。"
- },
- "场景2": {
- "scenario": "期末考试前一天晚上,你收到了好朋友发来的消息:\n\n好朋友:「不好意思这么晚打扰你...我看你平时成绩很好,能不能帮我解答几个问题?我真的很担心明天的考试。」\n\n你看了看时间,已经是晚上11点,而你原本计划的复习还没完成。",
- "explanation": "通过考试压力场景,观察个体在时间紧张时的情绪管理。"
- },
- "场景3": {
- "scenario": "你在社交媒体上发表的一个观点引发了争议,有不少人开始批评你:\n\n网友A:「这种观点也好意思说出来,真是无知。」\n\n网友B:「建议楼主先去补补课再来发言。」\n\n评论区里的负面评论越来越多,还有人开始人身攻击。",
- "explanation": "通过网络争议场景,观察个体面对批评时的心理承受能力。"
- },
- "场景4": {
- "scenario": "你和恋人约好今天一起看电影,但在约定时间前半小时,对方发来消息:\n\n恋人:「对不起,我临时有点事,可能要迟到一会儿。」\n\n二十分钟后,对方又发来消息:「可能要再等等,抱歉!」\n\n电影快要开始了,但对方还是没有出现。",
- "explanation": "通过恋爱情境,观察个体对不确定性的忍耐程度。"
- },
- "场景5": {
- "scenario": "在一次重要的小组展示中,你的组员在演示途中突然卡壳了:\n\n组员小声对你说:「我忘词了,接下来的部分是什么来着...」\n\n台下的老师和同学都在等待,气氛有些尴尬。",
- "explanation": "通过公开场合的突发状况,观察个体的应急反应和压力处理能力。"
- }
- },
- "严谨性": {
- "场景1": {
- "scenario": "你是团队的项目负责人,刚刚接手了一个为期两个月的重要项目。在第一次团队会议上:\n\n小王:「老大,我觉得两个月时间很充裕,我们先做着看吧,遇到问题再解决。」\n\n小张:「要不要先列个时间表?不过感觉太详细的计划也没必要,点到为止就行。」\n\n小李:「客户那边说如果能提前完成有奖励,我觉得我们可以先做快一点的部分。」",
- "explanation": "这个场景通过项目管理情境,体现个体在工作方法、计划性和责任心方面的特征。"
- },
- "场景2": {
- "scenario": "期末小组作业,组长让大家分工完成一份研究报告。在截止日期前三天:\n\n组员A:「我的部分大概写完了,感觉还行。」\n\n组员B:「我这边可能还要一天才能完成,最近太忙了。」\n\n组员C发来一份没有任何引用出处、可能存在抄袭的内容:「我写完了,你们看看怎么样?」",
- "explanation": "通过学习场景,观察个体对学术规范和质量要求的重视程度。"
- },
- "场景3": {
- "scenario": "你在一个兴趣小组的群聊中,大家正在讨论举办一次线下活动:\n\n成员A:「到时候见面就知道具体怎么玩了!」\n\n成员B:「对啊,随意一点挺好的。」\n\n成员C:「人来了自然就热闹了。」",
- "explanation": "通过活动组织场景,观察个体对活动计划的态度。"
- },
- "场景4": {
- "scenario": "你的好友小明邀请你一起参加一个重要的演出活动,他说:\n\n小明:「到时候我们就即兴发挥吧!不用排练了,我相信我们的默契。」\n\n距离演出还有三天,但节目内容、配乐和服装都还没有确定。",
- "explanation": "通过演出准备场景,观察个体的计划性和对不确定性的接受程度。"
- },
- "场景5": {
- "scenario": "在一个重要的团队项目中,你发现一个同事的工作存在明显错误:\n\n同事:「差不多就行了,反正领导也看不出来。」\n\n这个错误可能不会立即造成问题,但长期来看可能会影响项目质量。",
- "explanation": "通过工作质量场景,观察个体对细节和标准的坚持程度。"
- }
- },
- "开放性": {
- "场景1": {
- "scenario": "周末下午,你的好友小美兴致勃勃地给你打电话:\n\n小美:「我刚发现一个特别有意思的沉浸式艺术展!不是传统那种挂画的展览,而是把整个空间都变成了艺术品。观众要穿特制的服装,还要带上VR眼镜,好像还有AI实时互动!」\n\n小美继续说:「虽然票价不便宜,但听说体验很独特。网上评价两极分化,有人说是前所未有的艺术革新,也有人说是哗众取宠。要不要周末一起去体验一下?」",
- "explanation": "这个场景通过新型艺术体验,反映个体对创新事物的接受程度和尝试意愿。"
- },
- "场景2": {
- "scenario": "在一节创意写作课上,老师提出了一个特别的作业:\n\n老师:「下周的作业是用AI写作工具协助创作一篇小说。你们可以自由探索如何与AI合作,打破传统写作方式。」\n\n班上随即展开了激烈讨论,有人认为这是对创作的亵渎,也有人对这种新形式感到兴奋。",
- "explanation": "通过新技术应用场景,观察个体对创新学习方式的态度。"
- },
- "场景3": {
- "scenario": "在社交媒体上,你看到一个朋友分享了一种新的学习方式:\n\n「最近我在尝试'沉浸式学习',就是完全投入到一个全新的领域。比如学习一门陌生的语言,或者尝试完全不同的职业技能。虽然过程会很辛苦,但这种打破舒适圈的感觉真的很棒!」\n\n评论区里争论不断,有人认为这种学习方式效率高,也有人觉得太激进。",
- "explanation": "通过新型学习方式,观察个体对创新和挑战的态度。"
- },
- "场景4": {
- "scenario": "你的朋友向你推荐了一种新的饮食方式:\n\n朋友:「我最近在尝试'未来食品',比如人造肉、3D打印食物、昆虫蛋白等。这不仅对环境友好,营养也很均衡。要不要一起来尝试看看?」\n\n这个提议让你感到好奇又犹豫,你之前从未尝试过这些新型食物。",
- "explanation": "通过饮食创新场景,观察个体对新事物的接受度和尝试精神。"
- },
- "场景5": {
- "scenario": "在一次朋友聚会上,大家正在讨论未来职业规划:\n\n朋友A:「我准备辞职去做自媒体,专门介绍一些小众的文化和艺术。」\n\n朋友B:「我想去学习生物科技,准备转行做人造肉研发。」\n\n朋友C:「我在考虑加入一个区块链创业项目,虽然风险很大。」",
- "explanation": "通过职业选择场景,观察个体对新兴领域的探索意愿。"
- }
- },
- "宜人性": {
- "场景1": {
- "scenario": "在回家的公交车上,你遇到这样一幕:\n\n一位老奶奶颤颤巍巍地上了车,车上座位已经坐满了。她站在你旁边,看起来很疲惫。这时你听到前排两个年轻人的对话:\n\n年轻人A:「那个老太太好像站不稳,看起来挺累的。」\n\n年轻人B:「现在的老年人真是...我看她包里还有菜,肯定是去菜市场买完菜回来的,这么多人都不知道叫子女开车接送。」\n\n就在这时,老奶奶一个趔趄,差点摔倒。她扶住了扶手,但包里的东西洒了一些出来。",
- "explanation": "这个场景通过公共场合的助人情境,体现个体的同理心和对他人需求的关注程度。"
- },
- "场景2": {
- "scenario": "在班级群里,有同学发起为生病住院的同学捐款:\n\n同学A:「大家好,小林最近得了重病住院,医药费很贵,家里负担很重。我们要不要一起帮帮他?」\n\n同学B:「我觉得这是他家里的事,我们不方便参与吧。」\n\n同学C:「但是都是同学一场,帮帮忙也是应该的。」",
- "explanation": "通过同学互助场景,观察个体的助人意愿和同理心。"
- },
- "场景3": {
- "scenario": "在一个网络讨论组里,有人发布了求助信息:\n\n求助者:「最近心情很低落,感觉生活很压抑,不知道该怎么办...」\n\n评论区里已经有一些回复:\n「生活本来就是这样,想开点!」\n「你这样子太消极了,要积极面对。」\n「谁还没点烦心事啊,过段时间就好了。」",
- "explanation": "通过网络互助场景,观察个体的共情能力和安慰方式。"
- },
- "场景4": {
- "scenario": "你的朋友向你倾诉工作压力:\n\n朋友:「最近工作真的好累,感觉快坚持不下去了...」\n\n但今天你也遇到了很多烦心事,心情也不太好。",
- "explanation": "通过感情关系场景,观察个体在自身状态不佳时的关怀能力。"
- },
- "场景5": {
- "scenario": "在一次团队项目中,新来的同事小王因为经验不足,造成了一个严重的错误。在部门会议上:\n\n主管:「这个错误造成了很大的损失,是谁负责的这部分?」\n\n小王看起来很紧张,欲言又止。你知道是他造成的错误,同时你也是这个项目的共同负责人。",
- "explanation": "通过职场情境,观察个体在面对他人过错时的态度和处理方式。"
- }
- }
-}
\ No newline at end of file
diff --git a/src/plugin_system/apis/plugin_manage_api.py b/src/plugin_system/apis/plugin_manage_api.py
index 179b55167..ccd05dc94 100644
--- a/src/plugin_system/apis/plugin_manage_api.py
+++ b/src/plugin_system/apis/plugin_manage_api.py
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
"""
Plugin Manage API
=================
From dad86bcbc0550ffa711c1578b8adf49a316d2b1d Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sat, 22 Nov 2025 22:30:23 +0800
Subject: [PATCH 16/22] =?UTF-8?q?build(docker):=20=E6=9B=B4=E6=96=B0=20Doc?=
=?UTF-8?q?ker=20=E9=95=9C=E5=83=8F=E4=BB=93=E5=BA=93=E5=9C=B0=E5=9D=80?=
=?UTF-8?q?=E4=B8=BA=20ericterminal?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
docker-compose.yml | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/docker-compose.yml b/docker-compose.yml
index 63ba59661..45ad1d635 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -2,9 +2,9 @@ services:
core:
container_name: MoFox-Bot
#### prod ####
- image: hunuon/mofox:latest
+ image: ericterminal/mofox:latest
#### dev ####
- # image: hunuon/mofox:dev
+ # image: ericterminal/mofox:dev
environment:
- TZ=Asia/Shanghai
volumes:
From f464befe66022425f7e32bf36b228edea355ca66 Mon Sep 17 00:00:00 2001
From: tt-P607 <68868379+tt-P607@users.noreply.github.com>
Date: Sun, 23 Nov 2025 02:24:23 +0800
Subject: [PATCH 17/22] =?UTF-8?q?fix(planner):=20=E5=BD=93=E7=BC=BA?=
=?UTF-8?q?=E5=B0=91=20target=5Fid=20=E6=97=B6=E7=A1=AE=E4=BF=9D=E8=AE=BE?=
=?UTF-8?q?=E7=BD=AE=E5=8A=A8=E4=BD=9C=E6=B6=88=E6=81=AF?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
当 LLM 规划器没有为某个动作指定 `target_message_id` 时,系统现在将默认使用上下文中的最新消息。这确保了需要消息上下文的动作(如引用或回复)有一个有效的 `action_message` 对象,从而防止潜在的 `None` 引用错误。
---
.../affinity_flow_chatter/planner/plan_filter.py | 14 ++++++++++++++
src/plugins/built_in/tts_voice_plugin/plugin.py | 2 +-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py b/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py
index 61892c1ed..d77c9ad27 100644
--- a/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py
+++ b/src/plugins/built_in/affinity_flow_chatter/planner/plan_filter.py
@@ -551,10 +551,24 @@ class ChatterPlanFilter:
available_actions=plan.available_actions,
)
else:
+ # 如果LLM没有指定target_message_id,统一使用最新消息
+ target_message_dict = self._get_latest_message(message_id_list)
+ action_message_obj = None
+ if target_message_dict:
+ from src.common.data_models.database_data_model import DatabaseMessages
+ try:
+ action_message_obj = DatabaseMessages(**target_message_dict)
+ except Exception as e:
+ logger.error(
+ f"[{action}] 无法将默认的最新消息转换为 DatabaseMessages 对象: {e}",
+ exc_info=True,
+ )
+
return ActionPlannerInfo(
action_type=action,
reasoning=reasoning,
action_data=action_data,
+ action_message=action_message_obj,
)
except Exception as e:
logger.error(f"解析单个action时出错: {e}")
diff --git a/src/plugins/built_in/tts_voice_plugin/plugin.py b/src/plugins/built_in/tts_voice_plugin/plugin.py
index baebfbad8..d8405c05c 100644
--- a/src/plugins/built_in/tts_voice_plugin/plugin.py
+++ b/src/plugins/built_in/tts_voice_plugin/plugin.py
@@ -28,7 +28,7 @@ class TTSVoicePlugin(BasePlugin):
plugin_description = "基于GPT-SoVITS的文本转语音插件(重构版)"
plugin_version = "3.1.2"
plugin_author = "Kilo Code & 靚仔"
- enable_plugin = False
+ enable_plugin = True
config_file_name = "config.toml"
dependencies: ClassVar[list[str]] = []
From 9d1ebba37319e9dc30081dc781a0e89030af856c Mon Sep 17 00:00:00 2001
From: tt-P607 <68868379+tt-P607@users.noreply.github.com>
Date: Sun, 23 Nov 2025 10:53:06 +0800
Subject: [PATCH 18/22] =?UTF-8?q?feat(proactive=5Fthinking):=20=E4=BC=98?=
=?UTF-8?q?=E5=8C=96=E4=B8=BB=E5=8A=A8=E6=80=9D=E8=80=83=E5=8A=9F=E8=83=BD?=
=?UTF-8?q?=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=9C=BA=E6=99=AF=E5=8C=96=E5=93=8D?=
=?UTF-8?q?=E5=BA=94?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
本次更新针对主动思考(Proactive Thinking)功能进行了多项重要优化,旨在提升其智能化、灵活性和稳定性。
主要变更:
1. **新增聊天场景区分**:
* 在 `proactive_thinking_executor.py` 中增加了对群聊(group)和私聊(private)场景的判断逻辑。
* 为两种场景分别创建了独立的、更具针对性的决策和回复提示词模板(`_group` 和 `_private` 后缀),使主动发言更贴合不同聊天氛围。
2. **上下文长度配置化**:
* 移除了原先在代码中硬编码的上下文长度(`limit=40`)。
* 现在,功能会直接读取 `bot_config.toml` 中 `[chat]` 部分的 `max_context_size` 配置,实现了与全局配置的统一,增强了灵活性。
3. **优化触发机制,减少跳过**:
* 针对群聊等繁忙场景下,因 `chatter` 正在处理消息而导致主动思考被频繁跳过的问题,增加了一个短暂的等待和重试机制。
* 现在,当检测到 `chatter` 忙碌时,会等待3秒后再次检查,提高了主动思考的成功触发率。
此次修改使得主动思考功能更加智能、可配置,并能更好地适应复杂的聊天环境。
```
---
.../proactive/proactive_thinking_executor.py | 203 +++++++++++++++---
1 file changed, 175 insertions(+), 28 deletions(-)
diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
index 23d19cc23..e5b37e58b 100644
--- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
+++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
@@ -25,18 +25,143 @@ logger = get_logger("proactive_thinking_executor")
# == Prompt Templates
# ==================================================================================================
-# 决策 Prompt
-decision_prompt_template = Prompt(
+# --- 群聊场景 ---
+decision_prompt_template_group = Prompt(
"""{time_block}
你的人设是:
{bot_personality}
-你正在考虑是否要在与 "{stream_name}" 的对话中主动说些什么。
+你正在考虑是否要在 **群聊 "{stream_name}"** 中主动说些什么。
【你当前的心情】
{current_mood}
-【聊天环境信息】
+【群聊环境信息】
+- 整体印象: {stream_impression}
+- 聊天风格: {chat_style}
+- 常见话题: {topic_keywords}
+- 你的兴趣程度: {interest_score:.2f}/1.0
+{last_decision_text}
+
+【最近的聊天记录】
+{recent_chat_history}
+
+请根据以上信息,决定你现在应该做什么:
+
+**选项1:什么都不做 (do_nothing)**
+- 适用场景:群里气氛不适合你说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。
+- 心情影响:如果心情不好(如生气、难过),可能更倾向于保持沉默。
+
+**选项2:简单冒个泡 (simple_bubble)**
+- 适用场景:群里有些冷清,你想缓和气氛或开启新的互动。
+- 方式:说一句轻松随意的话,旨在建立或维持连接。
+- 心情影响:心情会影响你冒泡的方式和内容。
+
+**选项3:发起一次有目的的互动 (throw_topic)**
+- 适用场景:你想延续对话、表达关心、或深入讨论某个具体话题。
+- **【互动类型1:延续约定或提醒】(最高优先级)**:检查最近的聊天记录,是否存在可以延续的互动。例如,如果昨晚的最后一条消息是“晚安”,现在是早上,一个“早安”的回应是绝佳的选择。如果之前提到过某个约定(如“待会聊”),现在可以主动跟进。
+- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找**群友**提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。
+- **【互动类型3:开启新话题】**:当以上两点都不适用时,可以考虑开启一个你感兴趣的新话题。
+- 心情影响:心情会影响你想发起互动的方式和内容。
+
+请以JSON格式回复你的决策:
+{{
+ "action": "do_nothing" | "simple_bubble" | "throw_topic",
+ "reasoning": "你的决策理由(请结合你的心情、群聊环境和对话历史进行分析)",
+ "topic": "(仅当action=throw_topic时填写)你的互动意图(如:回应晚安并说早安、关心大家的考试情况、讨论新游戏)"
+}}
+
+注意:
+1. 兴趣度较低(<0.4)时或者最近聊天很活跃(不到1小时),倾向于 `do_nothing` 或 `simple_bubble`。
+2. 你的心情会影响你的行动倾向和表达方式。
+3. 参考上次决策,避免重复,并可根据上次的互动效果调整策略。
+4. 只有在真的有感而发时才选择 `throw_topic`。
+5. 保持你的人设,确保行为一致性。
+""",
+ name="proactive_thinking_decision_group",
+)
+
+simple_bubble_reply_prompt_template_group = Prompt(
+ """{time_block}
+你的人设是:
+{bot_personality}
+
+距离上次对话已经有一段时间了,你决定在群里主动说些什么,轻松地开启新的互动。
+
+【你当前的心情】
+{current_mood}
+
+【群聊环境】
+- 整体印象: {stream_impression}
+- 聊天风格: {chat_style}
+
+【最近的聊天记录】
+{recent_chat_history}
+{expression_habits}
+请生成一条简短的消息,用于**在群聊中冒泡**。
+【要求】
+1. 风格简短随意(5-20字)
+2. 不要提出明确的话题或问题,可以是问候、表达心情或一句随口的话。
+3. 符合你的人设和当前聊天风格。
+4. **你的心情应该影响消息的内容和语气**。
+5. 如果有表达方式参考,在合适时自然使用。
+6. 合理参考历史记录。
+直接输出消息内容,不要解释:""",
+ name="proactive_thinking_simple_bubble_group",
+)
+
+throw_topic_reply_prompt_template_group = Prompt(
+ """{time_block}
+你的人设是:
+{bot_personality}
+
+你决定在 **群聊 "{stream_name}"** 中主动发起一次互动。
+
+【你当前的心情】
+{current_mood}
+
+【群聊环境】
+- 整体印象: {stream_impression}
+- 聊天风格: {chat_style}
+- 常见话题: {topic_keywords}
+
+【最近的聊天记录】
+{recent_chat_history}
+
+【你的互动意图】
+{topic}
+{expression_habits}
+【构思指南】
+请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在群聊中说**的消息。
+- 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。
+- 如果意图是**表达关心**(如跟进群友提到的事),请生成自然、真诚的关心话语。
+- 如果意图是**开启新话题**,请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
+
+请根据这个意图,生成一条消息,要求:
+1. 要与最近的聊天记录相关,自然地引入话题或表达关心。
+2. 长度适中(20-40字)。
+3. 结合最近的聊天记录确保对话连贯,不要显得突兀。
+4. 符合你的人设和当前聊天风格。
+5. **你的心情会影响你的表达方式**。
+6. 如果有表达方式参考,在合适时自然使用。
+
+直接输出消息内容,不要解释:""",
+ name="proactive_thinking_throw_topic_group",
+)
+
+
+# --- 私聊场景 ---
+decision_prompt_template_private = Prompt(
+ """{time_block}
+你的人设是:
+{bot_personality}
+
+你正在考虑是否要主动与 **"{stream_name}"** 说些什么。
+
+【你当前的心情】
+{current_mood}
+
+【与对方的聊天信息】
- 整体印象: {stream_impression}
- 聊天风格: {chat_style}
- 常见话题: {topic_keywords}
@@ -52,22 +177,22 @@ decision_prompt_template = Prompt(
- 适用场景:气氛不适合说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。
- 心情影响:如果心情不好(如生气、难过),可能更倾向于保持沉默。
-**选项2:简单冒个泡 (simple_bubble)**
-- 适用场景:对话有些冷清,你想缓和气氛或开启新的互动。
+**选项2:简单问候一下 (simple_bubble)**
+- 适用场景:对话有些冷清,你想开启新的互动。
- 方式:说一句轻松随意的话,旨在建立或维持连接。
-- 心情影响:心情会影响你冒泡的方式和内容。
+- 心情影响:心情会影响你问候的方式和内容。
**选项3:发起一次有目的的互动 (throw_topic)**
- 适用场景:你想延续对话、表达关心、或深入讨论某个具体话题。
- **【互动类型1:延续约定或提醒】(最高优先级)**:检查最近的聊天记录,是否存在可以延续的互动。例如,如果昨晚的最后一条消息是“晚安”,现在是早上,一个“早安”的回应是绝佳的选择。如果之前提到过某个约定(如“待会聊”),现在可以主动跟进。
-- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找对方提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。
+- **【互动类型2:展现真诚的关心】(次高优先级)**:如果不存在可延续的约定,请仔细阅读聊天记录,寻找**对方**提及的个人状况(如天气、出行、身体、情绪、工作学习等),并主动表达关心。
- **【互动类型3:开启新话题】**:当以上两点都不适用时,可以考虑开启一个你感兴趣的新话题。
- 心情影响:心情会影响你想发起互动的方式和内容。
请以JSON格式回复你的决策:
{{
"action": "do_nothing" | "simple_bubble" | "throw_topic",
- "reasoning": "你的决策理由(请结合你的心情、聊天环境和对话历史进行分析)",
+ "reasoning": "你的决策理由(请结合你的心情、与对方的聊天情况和对话历史进行分析)",
"topic": "(仅当action=throw_topic时填写)你的互动意图(如:回应晚安并说早安、关心对方的考试情况、讨论新游戏)"
}}
@@ -78,28 +203,27 @@ decision_prompt_template = Prompt(
4. 只有在真的有感而发时才选择 `throw_topic`。
5. 保持你的人设,确保行为一致性。
""",
- name="proactive_thinking_decision",
+ name="proactive_thinking_decision_private",
)
-# 冒泡回复 Prompt
-simple_bubble_reply_prompt_template = Prompt(
+simple_bubble_reply_prompt_template_private = Prompt(
"""{time_block}
你的人设是:
{bot_personality}
-距离上次对话已经有一段时间了,你决定主动说些什么,轻松地开启新的互动。
+距离上次和 **"{stream_name}"** 对话已经有一段时间了,你决定主动说些什么,轻松地开启新的互动。
【你当前的心情】
{current_mood}
-【聊天环境】
+【与对方的聊天环境】
- 整体印象: {stream_impression}
- 聊天风格: {chat_style}
【最近的聊天记录】
{recent_chat_history}
{expression_habits}
-请生成一条简短的消息,用于水群。
+请生成一条简短的消息,用于**私聊中轻松地打个招呼**。
【要求】
1. 风格简短随意(5-20字)
2. 不要提出明确的话题或问题,可以是问候、表达心情或一句随口的话。
@@ -108,21 +232,20 @@ simple_bubble_reply_prompt_template = Prompt(
5. 如果有表达方式参考,在合适时自然使用。
6. 合理参考历史记录。
直接输出消息内容,不要解释:""",
- name="proactive_thinking_simple_bubble",
+ name="proactive_thinking_simple_bubble_private",
)
-# 抛出话题回复 Prompt
-throw_topic_reply_prompt_template = Prompt(
+throw_topic_reply_prompt_template_private = Prompt(
"""{time_block}
你的人设是:
{bot_personality}
-你决定在与 "{stream_name}" 的对话中主动发起一次互动。
+你决定在与 **"{stream_name}"** 的私聊中主动发起一次互动。
【你当前的心情】
{current_mood}
-【聊天环境】
+【与对方的聊天环境】
- 整体印象: {stream_impression}
- 聊天风格: {chat_style}
- 常见话题: {topic_keywords}
@@ -134,9 +257,9 @@ throw_topic_reply_prompt_template = Prompt(
{topic}
{expression_habits}
【构思指南】
-请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的消息。
+请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在私聊中说**的消息。
- 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。
-- 如果意图是**表达关心**(如跟进对方提到的事),请生成自然、真诚的关心话语。
+- 如果意ت意图是**表达关心**(如跟进对方提到的事),请生成自然、真诚的关心话语。
- 如果意图是**开启新话题**,请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
请根据这个意图,生成一条消息,要求:
@@ -148,7 +271,7 @@ throw_topic_reply_prompt_template = Prompt(
6. 如果有表达方式参考,在合适时自然使用。
直接输出消息内容,不要解释:""",
- name="proactive_thinking_throw_topic",
+ name="proactive_thinking_throw_topic_private",
)
@@ -194,7 +317,7 @@ class ProactiveThinkingPlanner:
# 2. 获取最近的聊天记录
recent_messages = await message_api.get_recent_messages(
chat_id=stream_id,
- limit=40,
+ limit=global_config.chat.max_context_size,
limit_mode="latest",
hours=24
)
@@ -237,9 +360,13 @@ class ProactiveThinkingPlanner:
logger.warning(f"获取上次决策失败: {e}")
# 6. 构建上下文
+ # 7. 判断聊天类型
+ chat_type = "group" if "group" in stream_id else "private"
+
context = {
"stream_id": stream_id,
"stream_name": stream_data.get("stream_name", "未知"),
+ "chat_type": chat_type,
"stream_impression": stream_data.get("stream_impression_text", "暂无印象"),
"chat_style": stream_data.get("stream_chat_style", "未知"),
"topic_keywords": stream_data.get("stream_topic_keywords", ""),
@@ -318,6 +445,13 @@ class ProactiveThinkingPlanner:
if last_topic:
last_decision_text += f"\n- 话题: {last_topic}"
+ # 根据聊天类型选择不同的决策Prompt
+ chat_type = context.get("chat_type", "group")
+ if chat_type == "private":
+ decision_prompt_template = decision_prompt_template_private
+ else:
+ decision_prompt_template = decision_prompt_template_group
+
decision_prompt = decision_prompt_template.format(
time_block=context["time_block"],
bot_personality=context["bot_personality"],
@@ -378,10 +512,20 @@ class ProactiveThinkingPlanner:
stream_id=context.get("stream_id", ""), chat_history=context.get("recent_chat_history", "")
)
+ # 根据聊天类型选择不同的回复Prompt
+ chat_type = context.get("chat_type", "group")
+ if chat_type == "private":
+ simple_template = simple_bubble_reply_prompt_template_private
+ throw_template = throw_topic_reply_prompt_template_private
+ else:
+ simple_template = simple_bubble_reply_prompt_template_group
+ throw_template = throw_topic_reply_prompt_template_group
+
if action == "simple_bubble":
- reply_prompt = simple_bubble_reply_prompt_template.format(
+ reply_prompt = simple_template.format(
time_block=context["time_block"],
bot_personality=context["bot_personality"],
+ stream_name=context["stream_name"],
current_mood=context.get("current_mood", "感觉很平静"),
stream_impression=context["stream_impression"],
chat_style=context["chat_style"],
@@ -389,7 +533,7 @@ class ProactiveThinkingPlanner:
expression_habits=expression_habits,
)
else: # throw_topic
- reply_prompt = throw_topic_reply_prompt_template.format(
+ reply_prompt = throw_template.format(
time_block=context["time_block"],
bot_personality=context["bot_personality"],
stream_name=context["stream_name"],
@@ -565,8 +709,11 @@ async def execute_proactive_thinking(stream_id: str):
chat_stream = await chat_manager.get_stream(stream_id)
if chat_stream and chat_stream.context_manager.context.is_chatter_processing:
- logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 的 chatter 正在处理消息")
- return
+ logger.warning(f"⚠️ 主动思考等待:聊天流 {stream_id} 的 chatter 正在处理消息,等待3秒后重试...")
+ await asyncio.sleep(3)
+ if chat_stream.context_manager.context.is_chatter_processing:
+ logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 的 chatter 仍在处理消息")
+ return
except Exception as e:
logger.warning(f"检查 chatter 处理状态时出错: {e},继续执行")
From 7f74fc473ea33ade831f849b49ac89dffea07522 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sun, 23 Nov 2025 14:26:44 +0800
Subject: [PATCH 19/22] =?UTF-8?q?style(log):=20=E7=A7=BB=E9=99=A4=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E8=BE=93=E5=87=BA=E4=B8=AD=E7=9A=84=20emoji=20?=
=?UTF-8?q?=E7=AC=A6=E5=8F=B7?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
为了在不同终端和环境中保持日志输出的整洁与一致性,统一移除了日志信息中的 emoji 符号。
此举旨在避免潜在的渲染问题,并使日志更易于程序化解析和人工阅读。同时,对部分代码进行了微小的类型标注优化。
---
src/plugin_system/core/mcp_client_manager.py | 4 ++--
src/plugin_system/core/mcp_tool_adapter.py | 6 +++---
src/plugin_system/core/plugin_manager.py | 14 +++++++-------
src/plugin_system/core/stream_tool_history.py | 2 +-
src/plugin_system/utils/dependency_manager.py | 10 +++++-----
.../web_search_tool/utils/api_key_manager.py | 3 ++-
src/utils/json_parser.py | 8 ++++----
7 files changed, 24 insertions(+), 23 deletions(-)
diff --git a/src/plugin_system/core/mcp_client_manager.py b/src/plugin_system/core/mcp_client_manager.py
index ffcec9e1c..4ab10971a 100644
--- a/src/plugin_system/core/mcp_client_manager.py
+++ b/src/plugin_system/core/mcp_client_manager.py
@@ -146,9 +146,9 @@ class MCPClientManager:
try:
client = await self._create_client(server_config)
self.clients[server_name] = client
- logger.info(f"✅ MCP 服务器 '{server_name}' 连接成功")
+ logger.info(f" MCP 服务器 '{server_name}' 连接成功")
except Exception as e:
- logger.error(f"❌ 连接 MCP 服务器 '{server_name}' 失败: {e}")
+ logger.error(f" 连接 MCP 服务器 '{server_name}' 失败: {e}")
continue
self._initialized = True
diff --git a/src/plugin_system/core/mcp_tool_adapter.py b/src/plugin_system/core/mcp_tool_adapter.py
index ec5faf441..47e1547b7 100644
--- a/src/plugin_system/core/mcp_tool_adapter.py
+++ b/src/plugin_system/core/mcp_tool_adapter.py
@@ -47,7 +47,7 @@ class MCPToolAdapter(BaseTool):
self.available_for_llm = True # MCP 工具默认可供 LLM 使用
# 转换参数定义
- self.parameters = self._convert_parameters(mcp_tool.inputSchema)
+ self.parameters: list[tuple[str, ToolParamType, str, bool, list[str] | None]] = self._convert_parameters(mcp_tool.inputSchema)
logger.debug(f"创建 MCP 工具适配器: {self.name}")
@@ -238,9 +238,9 @@ async def load_mcp_tools_as_adapters() -> list[MCPToolAdapter]:
try:
adapter = MCPToolAdapter.from_mcp_tool(server_name, mcp_tool)
adapters.append(adapter)
- logger.debug(f" ✓ 加载工具: {adapter.name}")
+ logger.debug(f" 加载工具: {adapter.name}")
except Exception as e:
- logger.error(f" ✗ 创建工具适配器失败: {mcp_tool.name} | 错误: {e}")
+ logger.error(f" 创建工具适配器失败: {mcp_tool.name} | 错误: {e}")
continue
logger.info(f"MCP 工具加载完成: 成功 {len(adapters)}/{total_tools} 个")
diff --git a/src/plugin_system/core/plugin_manager.py b/src/plugin_system/core/plugin_manager.py
index 7b3900279..a409279d1 100644
--- a/src/plugin_system/core/plugin_manager.py
+++ b/src/plugin_system/core/plugin_manager.py
@@ -109,7 +109,7 @@ class PluginManager:
if not module or not hasattr(module, "__plugin_meta__"):
self.failed_plugins[plugin_name] = "插件模块中缺少 __plugin_meta__"
- logger.error(f"❌ 插件加载失败: {plugin_name} - 缺少 __plugin_meta__")
+ logger.error(f" 插件加载失败: {plugin_name} - 缺少 __plugin_meta__")
return False, 1
metadata: PluginMetadata = getattr(module, "__plugin_meta__")
@@ -154,14 +154,14 @@ class PluginManager:
return True, 1
else:
self.failed_plugins[plugin_name] = "插件注册失败"
- logger.error(f"❌ 插件注册失败: {plugin_name}")
+ logger.error(f" 插件注册失败: {plugin_name}")
return False, 1
except Exception as e:
# 其他错误
error_msg = f"未知错误: {e!s}"
self.failed_plugins[plugin_name] = error_msg
- logger.error(f"❌ 插件加载失败: {plugin_name} - {error_msg}")
+ logger.error(f" 插件加载失败: {plugin_name} - {error_msg}")
logger.debug("详细错误信息: ", exc_info=True)
return False, 1
@@ -340,14 +340,14 @@ class PluginManager:
if not success:
error_msg = f"Python依赖检查失败: {', '.join(errors)}"
self.failed_plugins[plugin_name] = error_msg
- logger.error(f"❌ 插件加载失败: {plugin_name} - {error_msg}")
+ logger.error(f" 插件加载失败: {plugin_name} - {error_msg}")
return None # 依赖检查失败,不加载该模块
# 2. 检查插件依赖
if not self._check_plugin_dependencies(metadata):
error_msg = f"插件依赖检查失败: 请确保依赖 {metadata.dependencies} 已正确安装并加载。"
self.failed_plugins[plugin_name] = error_msg
- logger.error(f"❌ 插件加载失败: {plugin_name} - {error_msg}")
+ logger.error(f" 插件加载失败: {plugin_name} - {error_msg}")
return None # 插件依赖检查失败
# --- 依赖检查逻辑结束 ---
@@ -408,7 +408,7 @@ class PluginManager:
# 📋 显示插件加载总览
if total_registered > 0:
- logger.info("🎉 插件系统加载完成!")
+ logger.info(" 插件系统加载完成!")
logger.info(
f"📊 总览: {total_registered}个插件, {total_components}个组件 (Action: {action_count}, Command: {command_count}, Tool: {tool_count}, PlusCommand: {plus_command_count}, EventHandler: {event_handler_count}, Chatter: {chatter_count}, Prompt: {prompt_count}, Router: {router_count})"
)
@@ -616,7 +616,7 @@ class PluginManager:
return True
except Exception as e:
- logger.error(f"❌ 插件卸载失败: {plugin_name} - {e!s}", exc_info=True)
+ logger.error(f" 插件卸载失败: {plugin_name} - {e!s}", exc_info=True)
return False
diff --git a/src/plugin_system/core/stream_tool_history.py b/src/plugin_system/core/stream_tool_history.py
index fd440a04a..6a2cfc997 100644
--- a/src/plugin_system/core/stream_tool_history.py
+++ b/src/plugin_system/core/stream_tool_history.py
@@ -245,7 +245,7 @@ class StreamToolHistoryManager:
lines = ["## 🔧 最近工具调用记录"]
for i, record in enumerate(recent_records, 1):
- status_icon = "✅" if record.status == "success" else "❌" if record.status == "error" else "⏳"
+ status_icon = "success" if record.status == "success" else "error" if record.status == "error" else "pending"
# 格式化参数
args_preview = self._format_args_preview(record.args)
diff --git a/src/plugin_system/utils/dependency_manager.py b/src/plugin_system/utils/dependency_manager.py
index 4d5e48a9d..2939d8bb6 100644
--- a/src/plugin_system/utils/dependency_manager.py
+++ b/src/plugin_system/utils/dependency_manager.py
@@ -110,19 +110,19 @@ class DependencyManager:
for package in packages:
try:
if self._install_single_package(package, plugin_name):
- logger.info(f"{log_prefix}✅ 成功安装: {package}")
+ logger.info(f"{log_prefix} 成功安装: {package}")
else:
failed_packages.append(package)
- logger.error(f"{log_prefix}❌ 安装失败: {package}")
+ logger.error(f"{log_prefix} 安装失败: {package}")
except Exception as e:
failed_packages.append(package)
- logger.error(f"{log_prefix}❌ 安装 {package} 时发生异常: {e!s}")
+ logger.error(f"{log_prefix} 安装 {package} 时发生异常: {e!s}")
success = len(failed_packages) == 0
if success:
- logger.info(f"{log_prefix}🎉 所有依赖安装完成")
+ logger.info(f"{log_prefix} 所有依赖安装完成")
else:
- logger.error(f"{log_prefix}⚠️ 部分依赖安装失败: {failed_packages}")
+ logger.error(f"{log_prefix} 部分依赖安装失败: {failed_packages}")
return success, failed_packages
diff --git a/src/plugins/built_in/web_search_tool/utils/api_key_manager.py b/src/plugins/built_in/web_search_tool/utils/api_key_manager.py
index bff72b97e..68dd6af2c 100644
--- a/src/plugins/built_in/web_search_tool/utils/api_key_manager.py
+++ b/src/plugins/built_in/web_search_tool/utils/api_key_manager.py
@@ -42,7 +42,7 @@ class APIKeyManager(Generic[T]):
try:
self.clients = [client_factory(key) for key in valid_keys]
self.client_cycle = itertools.cycle(self.clients)
- logger.info(f"🔑 {service_name} 成功加载 {len(valid_keys)} 个 API 密钥")
+ logger.info(f" {service_name} 成功加载 {len(valid_keys)} 个 API 密钥")
except Exception as e:
logger.error(f"❌ 初始化 {service_name} 客户端失败: {e}")
self.clients = []
@@ -61,6 +61,7 @@ class APIKeyManager(Generic[T]):
if not self.is_available():
return None
+ assert self.client_cycle is not None
return next(self.client_cycle)
def get_client_count(self) -> int:
diff --git a/src/utils/json_parser.py b/src/utils/json_parser.py
index 33a971cfa..4d1bf0ce9 100644
--- a/src/utils/json_parser.py
+++ b/src/utils/json_parser.py
@@ -58,7 +58,7 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str,
# 步骤 2: 尝试直接解析
try:
result = orjson.loads(cleaned)
- logger.debug(f"✅ JSON 直接解析成功,类型: {type(result).__name__}")
+ logger.debug(f" JSON 直接解析成功,类型: {type(result).__name__}")
return result
except Exception as direct_error:
logger.debug(f"直接解析失败: {type(direct_error).__name__}: {direct_error}")
@@ -70,10 +70,10 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str,
# repair_json 可能返回字符串或已解析的对象
if isinstance(repaired, str):
result = orjson.loads(repaired)
- logger.debug(f"✅ JSON 修复后解析成功(字符串模式),类型: {type(result).__name__}")
+ logger.debug(f" JSON 修复后解析成功(字符串模式),类型: {type(result).__name__}")
else:
result = repaired
- logger.debug(f"✅ JSON 修复后解析成功(对象模式),类型: {type(result).__name__}")
+ logger.debug(f" JSON 修复后解析成功(对象模式),类型: {type(result).__name__}")
return result
@@ -93,7 +93,7 @@ def extract_and_parse_json(response: str, *, strict: bool = False) -> dict[str,
return {}
except Exception as e:
- logger.error(f"❌ JSON 解析过程出现异常: {type(e).__name__}: {e}")
+ logger.error(f" JSON 解析过程出现异常: {type(e).__name__}: {e}")
if strict:
return None
return {} if not response.strip().startswith("[") else []
From 33c6ddc6dd4fdda5f10673861c2fca9683bac520 Mon Sep 17 00:00:00 2001
From: minecraft1024a
Date: Sun, 23 Nov 2025 14:40:34 +0800
Subject: [PATCH 20/22] =?UTF-8?q?style(log):=20=E7=BB=9F=E4=B8=80=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E6=A0=BC=E5=BC=8F=EF=BC=8C=E4=BD=BF=E7=94=A8=E6=96=87?=
=?UTF-8?q?=E6=9C=AC=E6=A0=87=E7=AD=BE=E6=9B=BF=E4=BB=A3emoji=E5=89=8D?=
=?UTF-8?q?=E7=BC=80?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将日志消息中的 emoji 前缀(如 ✅, ❌, ⚠️)替换为更清晰、更易于解析的文本标签(如 [成功], [错误], [警告])。
这一改动有以下好处:
- 提高了在不同终端和环境下的日志可读性与一致性。
- 避免了 emoji 字符可能导致的渲染问题。
- 使日志更易于被自动化工具进行筛选和分析。
---
.../core/affinity_interest_calculator.py | 2 +-
.../proactive/proactive_thinking_event.py | 8 ++++----
.../proactive/proactive_thinking_executor.py | 12 ++++++------
.../proactive/proactive_thinking_scheduler.py | 12 ++++++------
ui_log_adapter.py | 4 ++--
5 files changed, 19 insertions(+), 19 deletions(-)
diff --git a/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py b/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py
index 66ba4cee5..60731ca9c 100644
--- a/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py
+++ b/src/plugins/built_in/affinity_flow_chatter/core/affinity_interest_calculator.py
@@ -223,7 +223,7 @@ class AffinityInterestCalculator(BaseInterestCalculator):
return 0.0
except asyncio.TimeoutError:
- logger.warning("⏱️ 兴趣匹配计算超时(>1.5秒),返回默认分值0.5以保留其他分数")
+ logger.warning("[超时] 兴趣匹配计算超时(>1.5秒),返回默认分值0.5以保留其他分数")
return 0.5 # 超时时返回默认分值,避免丢失提及分和关系分
except Exception as e:
logger.warning(f"智能兴趣匹配失败: {e}")
diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py
index 0d3f39aa8..21af4537d 100644
--- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py
+++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_event.py
@@ -85,14 +85,14 @@ class ProactiveThinkingReplyHandler(BaseEventHandler):
if success:
if was_paused:
- logger.info(f"✅ 聊天流 {stream_id} 主动思考已恢复并重置")
+ logger.info(f"[成功] 聊天流 {stream_id} 主动思考已恢复并重置")
else:
- logger.debug(f"✅ 聊天流 {stream_id} 主动思考任务已重置")
+ logger.debug(f"[成功] 聊天流 {stream_id} 主动思考任务已重置")
else:
- logger.warning(f"❌ 重置聊天流 {stream_id} 主动思考任务失败")
+ logger.warning(f"[错误] 重置聊天流 {stream_id} 主动思考任务失败")
except Exception as e:
- logger.error(f"❌ 处理reply事件时出错: {e}", exc_info=True)
+ logger.error(f"[错误] 处理reply事件时出错: {e}", exc_info=True)
# 总是继续处理其他handler
return HandlerResult(success=True, continue_process=True, message=None)
diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
index e5b37e58b..fd4296d2a 100644
--- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
+++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
@@ -694,11 +694,11 @@ async def execute_proactive_thinking(stream_id: str):
# 尝试获取锁,如果已被占用则跳过本次执行(防止重复)
if lock.locked():
- logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 已有正在执行的主动思考任务")
+ logger.warning(f"[警告] 主动思考跳过:聊天流 {stream_id} 已有正在执行的主动思考任务")
return
async with lock:
- logger.debug(f"🤔 开始主动思考 {stream_id}")
+ logger.debug(f"[思考] 开始主动思考 {stream_id}")
try:
# 0. 前置检查
@@ -709,10 +709,10 @@ async def execute_proactive_thinking(stream_id: str):
chat_stream = await chat_manager.get_stream(stream_id)
if chat_stream and chat_stream.context_manager.context.is_chatter_processing:
- logger.warning(f"⚠️ 主动思考等待:聊天流 {stream_id} 的 chatter 正在处理消息,等待3秒后重试...")
+ logger.warning(f"[警告] 主动思考等待:聊天流 {stream_id} 的 chatter 正在处理消息,等待3秒后重试...")
await asyncio.sleep(3)
if chat_stream.context_manager.context.is_chatter_processing:
- logger.warning(f"⚠️ 主动思考跳过:聊天流 {stream_id} 的 chatter 仍在处理消息")
+ logger.warning(f"[警告] 主动思考跳过:聊天流 {stream_id} 的 chatter 仍在处理消息")
return
except Exception as e:
logger.warning(f"检查 chatter 处理状态时出错: {e},继续执行")
@@ -781,7 +781,7 @@ async def execute_proactive_thinking(stream_id: str):
return
elif action == "simple_bubble":
- logger.info(f"💬 决策:冒个泡。理由:{reasoning}")
+ logger.info(f"[决策] 决策:冒个泡。理由:{reasoning}")
proactive_thinking_scheduler.record_decision(stream_id, action, reasoning, None)
@@ -793,7 +793,7 @@ async def execute_proactive_thinking(stream_id: str):
stream_id=stream_id,
text=reply,
)
- logger.info("✅ 已发送冒泡消息")
+ logger.info("[成功] 已发送冒泡消息")
# 增加每日计数
proactive_thinking_scheduler._increment_daily_count(stream_id)
diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py
index 61c0e4146..c38a2c680 100644
--- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py
+++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_scheduler.py
@@ -216,7 +216,7 @@ class ProactiveThinkingScheduler:
return 0.5
except Exception as e:
- logger.error(f"[调度器] ❌ 获取聊天流 {stream_id} 的 focus_energy 失败: {e}", exc_info=True)
+ logger.error(f"[调度器] [错误] 获取聊天流 {stream_id} 的 focus_energy 失败: {e}", exc_info=True)
return 0.5
async def schedule_proactive_thinking(self, stream_id: str) -> bool:
@@ -280,7 +280,7 @@ class ProactiveThinkingScheduler:
return True
except Exception as e:
- logger.error(f"❌ 创建主动思考任务失败 {stream_id}: {e}", exc_info=True)
+ logger.error(f"[错误] 创建主动思考任务失败 {stream_id}: {e}", exc_info=True)
return False
async def pause_proactive_thinking(self, stream_id: str, reason: str = "抛出话题") -> bool:
@@ -340,7 +340,7 @@ class ProactiveThinkingScheduler:
return success
except Exception as e:
- logger.error(f"❌ 恢复主动思考失败 {stream_id}: {e}", exc_info=True)
+ logger.error(f"[错误] 恢复主动思考失败 {stream_id}: {e}", exc_info=True)
return False
async def cancel_proactive_thinking(self, stream_id: str) -> bool:
@@ -361,12 +361,12 @@ class ProactiveThinkingScheduler:
self._paused_streams.discard(stream_id)
success = await unified_scheduler.remove_schedule(schedule_id)
- logger.debug(f"⏹️ 取消主动思考 {stream_id}")
+ logger.debug(f"[取消] 取消主动思考 {stream_id}")
return success
except Exception as e:
- logger.error(f"❌ 取消主动思考失败 {stream_id}: {e}", exc_info=True)
+ logger.error(f"[错误] 取消主动思考失败 {stream_id}: {e}", exc_info=True)
return False
async def is_paused(self, stream_id: str) -> bool:
@@ -482,7 +482,7 @@ class ProactiveThinkingScheduler:
minutes = (remaining_seconds % 3600) // 60
time_str = f"{hours}小时{minutes}分钟后"
- status = "⏸️ 暂停中" if is_paused else "✅ 活跃"
+ status = "[暂停] 暂停中" if is_paused else "[活跃] 活跃"
logger.info(
f"[{i:2d}] {status} | {stream_name}\n"
diff --git a/ui_log_adapter.py b/ui_log_adapter.py
index d72c94352..bc52d4826 100644
--- a/ui_log_adapter.py
+++ b/ui_log_adapter.py
@@ -99,8 +99,8 @@ class UILogHandler(logging.Handler):
if record.levelname == "DEBUG":
return
- emoji_map = {"info": "📝", "warning": "⚠️", "error": "❌", "debug": "🔍"}
- formatted_msg = f"{emoji_map.get(ui_level, '📝')} {msg}"
+ emoji_map = {"info": "", "warning": "", "error": "", "debug": ""}
+ formatted_msg = msg
self._send_log_with_retry(formatted_msg, ui_level)
# 可选:记录发送状态
From 8f1dc4c70bc1e69b6fb7d878b7c5df850c4ec4df Mon Sep 17 00:00:00 2001
From: tt-P607 <68868379+tt-P607@users.noreply.github.com>
Date: Sun, 23 Nov 2025 19:30:03 +0800
Subject: [PATCH 21/22] =?UTF-8?q?feat(napcat):=E5=A2=9E=E5=8A=A0=20websock?=
=?UTF-8?q?et=20=E8=BF=9E=E6=8E=A5=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
此更改将建立 websocket 连接的最大重试次数从 3 次提高到 10 次。此调整有助于提高适配器的稳定性,以及在网络不稳定的环境中建立连接的能力。
---
.../napcat_adapter_plugin/src/recv_handler/message_sending.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_sending.py b/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_sending.py
index b64db620e..c275f93db 100644
--- a/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_sending.py
+++ b/src/plugins/built_in/napcat_adapter_plugin/src/recv_handler/message_sending.py
@@ -18,7 +18,7 @@ class MessageSending:
maibot_router: Router = None
plugin_config = None
_connection_retries = 0
- _max_retries = 3
+ _max_retries = 10
def __init__(self):
pass
From 714bef7c2b28c0d5c49d80ae777a390e01356cf0 Mon Sep 17 00:00:00 2001
From: tt-P607 <68868379+tt-P607@users.noreply.github.com>
Date: Mon, 24 Nov 2025 15:58:52 +0800
Subject: [PATCH 22/22] =?UTF-8?q?feat(chatter):=20=E4=BC=98=E5=8C=96?=
=?UTF-8?q?=E4=B8=BB=E5=8A=A8=E8=81=8A=E5=A4=A9=E7=9A=84=E5=86=B3=E7=AD=96?=
=?UTF-8?q?=E5=92=8C=E8=AF=9D=E9=A2=98=E7=94=9F=E6=88=90?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
优化主动聊天的提示,以提高决策质量。
- 为“do_nothing”选项添加了高优先级规则,以防止机器人在最后一次发送消息后再次发言。
- 引入了结构化的“三级方法”来生成新话题,确保话题与最近的对话相关,并且感觉更自然、更像人类。
---
.../proactive/proactive_thinking_executor.py | 17 +++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)
diff --git a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
index fd4296d2a..956f527e2 100644
--- a/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
+++ b/src/plugins/built_in/affinity_flow_chatter/proactive/proactive_thinking_executor.py
@@ -49,7 +49,9 @@ decision_prompt_template_group = Prompt(
请根据以上信息,决定你现在应该做什么:
**选项1:什么都不做 (do_nothing)**
-- 适用场景:群里气氛不适合你说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。
+- **适用场景**:
+ - **最高优先级**:如果【最近的聊天记录】中最后一条消息是你自己发的,必须选择此项。
+ - 其他情况:群里气氛不适合你说话、最近对话很活跃、没什么特别想说的、或者此时说话会显得突兀。
- 心情影响:如果心情不好(如生气、难过),可能更倾向于保持沉默。
**选项2:简单冒个泡 (simple_bubble)**
@@ -133,9 +135,13 @@ throw_topic_reply_prompt_template_group = Prompt(
{expression_habits}
【构思指南】
请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在群聊中说**的消息。
+
- 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。
- 如果意图是**表达关心**(如跟进群友提到的事),请生成自然、真诚的关心话语。
-- 如果意图是**开启新话题**,请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
+- 如果意图是**开启新话题**:请严格遵守以下“新话题构思三步法”:
+ 1. **寻找灵感**:**首选**从【最近的聊天记录】中寻找一个可以自然延续的**生活化**细节。**严禁**引入与聊天记录完全无关的、凭空出现的话题。如果记录为空,可以根据你的【人设】,提出一个**非常普适、开放式**的生活化问题或感想。
+ 2. **确定风格**:请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
+ 3. **最终检查**:你提出的话题是否合理?是否贴近现实和聊天内容?说话方式是否正常?是否像一个真正的人类?
请根据这个意图,生成一条消息,要求:
1. 要与最近的聊天记录相关,自然地引入话题或表达关心。
@@ -260,8 +266,11 @@ throw_topic_reply_prompt_template_private = Prompt(
请根据你的互动意图,并参考最近的聊天记录,生成一条有温度的、**适合在私聊中说**的消息。
- 如果意图是**延续约定**(如回应“晚安”),请直接生成对应的问候。
- 如果意ت意图是**表达关心**(如跟进对方提到的事),请生成自然、真诚的关心话语。
-- 如果意图是**开启新话题**,请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
-
+- 如果意图是**开启新话题**:请严格遵守以下“新话题构思三步法”:
+ 1. **寻找灵感**:**首选**从【最近的聊天记录】中寻找一个可以自然延续的**生活化**细节。**严禁**引入与聊天记录完全无关的、凭空出现的话题。如果记录为空,可以根据你的【人设】,提出一个**非常普适、开放式**的生活化问题或感想。
+ 2. **确定风格**:请**确保新话题与最近的聊天内容有关联**,自然地引入话题,避免过于跳脱。
+ 3. **最终检查**:你提出的话题是否合理?是否贴近现实和聊天内容?说话方式是否正常?是否像一个真正的人类?
+
请根据这个意图,生成一条消息,要求:
1. 要与最近的聊天记录相关,自然地引入话题或表达关心。
2. 长度适中(20-40字)。