Merge branches 'dev' and 'dev' of https://github.com/MoFox-Studio/MoFox_Bot into dev
This commit is contained in:
@@ -34,8 +34,14 @@ class BaseEventHandler(ABC):
|
||||
if EventType.UNKNOWN in self.init_subscribe:
|
||||
raise NotImplementedError("事件处理器必须指定 event_type")
|
||||
|
||||
from src.plugin_system.core.component_registry import component_registry
|
||||
self.plugin_config = component_registry.get_plugin_config(self.plugin_name)
|
||||
# 优先使用实例级别的 plugin_config,如果没有则使用类级别的配置
|
||||
# 事件管理器会在注册时通过 set_plugin_config 设置实例级别的配置
|
||||
instance_config = getattr(self, "plugin_config", None)
|
||||
if instance_config is not None:
|
||||
self.plugin_config = instance_config
|
||||
else:
|
||||
# 如果实例级别没有配置,则使用类级别的配置(向后兼容)
|
||||
self.plugin_config = getattr(self.__class__, "plugin_config", {})
|
||||
|
||||
@abstractmethod
|
||||
async def execute(self, kwargs: dict | None) -> Tuple[bool, bool, Optional[str]]:
|
||||
@@ -101,6 +107,9 @@ class BaseEventHandler(ABC):
|
||||
"""
|
||||
self.plugin_name = plugin_name
|
||||
|
||||
def set_plugin_config(self,plugin_config) -> None:
|
||||
self.plugin_config = plugin_config
|
||||
|
||||
def get_config(self, key: str, default=None):
|
||||
"""获取插件配置值,支持嵌套键访问
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
from typing import Dict, List, Optional, Any, Pattern, Tuple, Union, Type
|
||||
@@ -170,6 +171,8 @@ class ComponentRegistry:
|
||||
return False
|
||||
|
||||
action_class.plugin_name = action_info.plugin_name
|
||||
# 设置插件配置
|
||||
action_class.plugin_config = self.get_plugin_config(action_info.plugin_name) or {}
|
||||
self._action_registry[action_name] = action_class
|
||||
|
||||
# 如果启用,添加到默认动作集
|
||||
@@ -188,6 +191,8 @@ class ComponentRegistry:
|
||||
return False
|
||||
|
||||
command_class.plugin_name = command_info.plugin_name
|
||||
# 设置插件配置
|
||||
command_class.plugin_config = self.get_plugin_config(command_info.plugin_name) or {}
|
||||
self._command_registry[command_name] = command_class
|
||||
|
||||
# 如果启用了且有匹配模式
|
||||
@@ -220,6 +225,8 @@ class ComponentRegistry:
|
||||
self._plus_command_registry: Dict[str, Type[PlusCommand]] = {}
|
||||
|
||||
plus_command_class.plugin_name = plus_command_info.plugin_name
|
||||
# 设置插件配置
|
||||
plus_command_class.plugin_config = 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}")
|
||||
@@ -230,6 +237,8 @@ class ComponentRegistry:
|
||||
tool_name = tool_info.name
|
||||
|
||||
tool_class.plugin_name = tool_info.plugin_name
|
||||
# 设置插件配置
|
||||
tool_class.plugin_config = self.get_plugin_config(tool_info.plugin_name) or {}
|
||||
self._tool_registry[tool_name] = tool_class
|
||||
|
||||
# 如果是llm可用的且启用的工具,添加到 llm可用工具列表
|
||||
@@ -249,6 +258,8 @@ class ComponentRegistry:
|
||||
return False
|
||||
|
||||
handler_class.plugin_name = handler_info.plugin_name
|
||||
# 设置插件配置
|
||||
handler_class.plugin_config = self.get_plugin_config(handler_info.plugin_name) or {}
|
||||
self._event_handler_registry[handler_name] = handler_class
|
||||
|
||||
if not handler_info.enabled:
|
||||
@@ -259,7 +270,7 @@ class ComponentRegistry:
|
||||
# 使用EventManager进行事件处理器注册
|
||||
from src.plugin_system.core.event_manager import event_manager
|
||||
|
||||
return event_manager.register_event_handler(handler_class)
|
||||
return event_manager.register_event_handler(handler_class,self.get_plugin_config(handler_info.plugin_name) or {})
|
||||
|
||||
# === 组件移除相关 ===
|
||||
|
||||
@@ -656,20 +667,35 @@ class ComponentRegistry:
|
||||
plugin_info = self.get_plugin_info(plugin_name)
|
||||
return plugin_info.components if plugin_info else []
|
||||
|
||||
def get_plugin_config(self, plugin_name: str) -> Optional[dict]:
|
||||
def get_plugin_config(self, plugin_name: str) -> dict:
|
||||
"""获取插件配置
|
||||
|
||||
Args:
|
||||
plugin_name: 插件名称
|
||||
|
||||
Returns:
|
||||
Optional[dict]: 插件配置字典或None
|
||||
dict: 插件配置字典,如果插件实例不存在或配置为空,返回空字典
|
||||
"""
|
||||
# 从插件管理器获取插件实例的配置
|
||||
from src.plugin_system.core.plugin_manager import plugin_manager
|
||||
|
||||
plugin_instance = plugin_manager.get_plugin_instance(plugin_name)
|
||||
return plugin_instance.config if plugin_instance else None
|
||||
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, 'r', 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]:
|
||||
"""获取注册中心统计信息"""
|
||||
|
||||
@@ -166,6 +166,7 @@ class EventManager:
|
||||
|
||||
# 创建事件处理器实例,传递插件配置
|
||||
handler_instance = handler_class()
|
||||
handler_instance.plugin_config = plugin_config
|
||||
if plugin_config is not None and hasattr(handler_instance, 'set_plugin_config'):
|
||||
handler_instance.set_plugin_config(plugin_config)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user