重构:移除过时的napcat_adapter_plugin组件

- 从napcat_adapter_plugin中删除了stream_router.py、utils.py、video_handler.py、websocket_manager.py和todo.md文件。
- 在napcat_cache.json中为组和成员信息引入了一种新的缓存结构。
- 通过移除未使用的模块和整合功能,简化了插件的架构。
This commit is contained in:
Windpicker-owo
2025-11-26 16:40:31 +08:00
parent 8a6b141017
commit 8fc4cd4c3b
35 changed files with 162 additions and 9624 deletions

View File

@@ -41,6 +41,7 @@ from .base import (
EventHandlerInfo,
EventType,
PluginInfo,
AdapterInfo,
# 新增的增强命令系统
PlusCommand,
BaseRouterComponent,

View File

@@ -15,7 +15,7 @@ from typing import TYPE_CHECKING, Any, Dict, Optional
from mofox_wire import AdapterBase as MoFoxAdapterBase, CoreSink, MessageEnvelope, ProcessCoreSink
if TYPE_CHECKING:
from src.plugin_system.base.base_plugin import BasePlugin
from src.plugin_system import BasePlugin, AdapterInfo
from src.common.logger import get_logger

View File

@@ -3,6 +3,7 @@ from abc import abstractmethod
from src.common.logger import get_logger
from src.plugin_system.base.component_types import (
ActionInfo,
AdapterInfo,
CommandInfo,
ComponentType,
EventHandlerInfo,
@@ -13,6 +14,7 @@ from src.plugin_system.base.component_types import (
)
from .base_action import BaseAction
from .base_adapter import BaseAdapter
from .base_command import BaseCommand
from .base_events_handler import BaseEventHandler
from .base_interest_calculator import BaseInterestCalculator
@@ -35,27 +37,26 @@ class BasePlugin(PluginBase):
@classmethod
def _get_component_info_from_class(cls, component_class: type, component_type: ComponentType):
"""组件类自动生成组件信息
"""类获取组件信息
Args:
component_class: 组件类
component_type: 组件类型
Returns:
对应类型的ComponentInfo对象
对应的ComponentInfo对象
"""
if component_type == ComponentType.COMMAND:
if hasattr(component_class, "get_command_info"):
return component_class.get_command_info()
else:
logger.warning(f"Command {component_class.__name__} 缺少 get_command_info 方法")
logger.warning(f"Command组件 {component_class.__name__} 缺少 get_command_info 方法")
return None
elif component_type == ComponentType.ACTION:
if hasattr(component_class, "get_action_info"):
return component_class.get_action_info()
else:
logger.warning(f"Action {component_class.__name__} 缺少 get_action_info 方法")
logger.warning(f"Action组件 {component_class.__name__} 缺少 get_action_info 方法")
return None
elif component_type == ComponentType.INTEREST_CALCULATOR:
@@ -63,30 +64,37 @@ class BasePlugin(PluginBase):
return component_class.get_interest_calculator_info()
else:
logger.warning(
f"InterestCalculator {component_class.__name__} 缺少 get_interest_calculator_info 方法"
f"InterestCalculator组件 {component_class.__name__} 缺少 get_interest_calculator_info 方法"
)
return None
elif component_type == ComponentType.PLUS_COMMAND:
# PlusCommand的get_info逻辑可以在这里实现
logger.warning("PlusCommand的get_info逻辑尚未实现")
# PlusCommand组件的get_info方法尚未实现
logger.warning("PlusCommand组件的get_info方法尚未实现")
return None
elif component_type == ComponentType.TOOL:
# Tool的get_info逻辑可以在这里实现
logger.warning("Tool的get_info逻辑尚未实现")
# Tool组件的get_info方法尚未实现
logger.warning("Tool组件的get_info方法尚未实现")
return None
elif component_type == ComponentType.EVENT_HANDLER:
# EventHandler的get_info逻辑可以在这里实现
logger.warning("EventHandler的get_info逻辑尚未实现")
# EventHandler组件的get_info方法尚未实现
logger.warning("EventHandler组件的get_info方法尚未实现")
return None
elif component_type == ComponentType.PROMPT:
if hasattr(component_class, "get_prompt_info"):
return component_class.get_prompt_info()
else:
logger.warning(f"Prompt {component_class.__name__} 缺少 get_prompt_info 方法")
logger.warning(f"Prompt组件 {component_class.__name__} 缺少 get_prompt_info 方法")
return None
elif component_type == ComponentType.ADAPTER:
if hasattr(component_class, "get_adapter_info"):
return component_class.get_adapter_info()
else:
logger.warning(f"Adapter<EFBFBD><EFBFBD> {component_class.__name__} ȱ<><C8B1> get_adapter_info <20><><EFBFBD><EFBFBD>")
return None
else:
@@ -95,16 +103,13 @@ class BasePlugin(PluginBase):
@classmethod
def get_component_info(cls, component_class: type, component_type: ComponentType):
"""获取组件信息的通用方法
这是一个便捷方法内部调用_get_component_info_from_class
"""获取组件信息
Args:
component_class: 组件类
component_type: 组件类型
Returns:
对应类型的ComponentInfo对象
对应的ComponentInfo对象
"""
return cls._get_component_info_from_class(component_class, component_type)
@@ -113,6 +118,7 @@ class BasePlugin(PluginBase):
self,
) -> list[
tuple[ActionInfo, type[BaseAction]]
| tuple[AdapterInfo, type[BaseAdapter]]
| tuple[CommandInfo, type[BaseCommand]]
| tuple[PlusCommandInfo, type[PlusCommand]]
| tuple[EventHandlerInfo, type[BaseEventHandler]]

View File

@@ -152,6 +152,7 @@ class AdapterInfo:
name: str # 适配器名称
component_type: ComponentType = field(default=ComponentType.ADAPTER, init=False)
plugin_name: str = "" # <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
version: str = "1.0.0" # 适配器版本
platform: str = "unknown" # 平台名称
description: str = "" # 适配器描述

View File

@@ -10,6 +10,7 @@ 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
from src.plugin_system.base.base_adapter import BaseAdapter
from src.plugin_system.base.base_chatter import BaseChatter
from src.plugin_system.base.base_command import BaseCommand
from src.plugin_system.base.base_events_handler import BaseEventHandler
@@ -19,6 +20,7 @@ from src.plugin_system.base.base_prompt import BasePrompt
from src.plugin_system.base.base_tool import BaseTool
from src.plugin_system.base.component_types import (
ActionInfo,
AdapterInfo,
ChatterInfo,
CommandInfo,
ComponentInfo,
@@ -46,6 +48,7 @@ ComponentClassType = (
| type[BaseInterestCalculator]
| type[BasePrompt]
| type[BaseRouterComponent]
| type[BaseAdapter]
)
@@ -109,6 +112,21 @@ class ComponentRegistry:
"""启用的chatter名 -> chatter类"""
logger.info("组件注册中心初始化完成")
self._interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
"""兴趣计算器名 -> 兴趣计算器类"""
self._enabled_interest_calculator_registry: dict[str, type["BaseInterestCalculator"]] = {}
"""启用的兴趣计算器名 -> 兴趣计算器类"""
self._prompt_registry: dict[str, type["BasePrompt"]] = {}
"""提示词组件名 -> 提示词组件类"""
self._enabled_prompt_registry: dict[str, type["BasePrompt"]] = {}
"""启用的提示词组件名 -> 提示词组件类"""
self._adapter_registry: dict[str, type["BaseAdapter"]] = {}
"""适配器组件名 -> 适配器组件类"""
self._enabled_adapter_registry: dict[str, type["BaseAdapter"]] = {}
"""启用的适配器组件名 -> 适配器组件类"""
# == 注册方法 ==
def register_plugin(self, plugin_info: PluginInfo) -> bool:
@@ -204,6 +222,10 @@ class ComponentRegistry:
assert isinstance(component_info, RouterInfo)
assert issubclass(component_class, BaseRouterComponent)
ret = self._register_router_component(component_info, component_class)
case ComponentType.ADAPTER:
assert isinstance(component_info, AdapterInfo)
assert issubclass(component_class, BaseAdapter)
ret = self._register_adapter_component(component_info, component_class)
case _:
logger.warning(f"未知组件类型: {component_type}")
ret = False
@@ -335,12 +357,6 @@ class ComponentRegistry:
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"]] = {}
_assign_plugin_attrs(
interest_calculator_class,
interest_calculator_info.plugin_name,
@@ -365,11 +381,6 @@ class ComponentRegistry:
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 {}
)
@@ -420,6 +431,29 @@ class ComponentRegistry:
logger.error(f"注册路由组件 '{router_info.name}' 时出错: {e}", exc_info=True)
return False
def _register_adapter_component(self, adapter_info: AdapterInfo, adapter_class: type[BaseAdapter]) -> bool:
"""将Adapter组件注册到Adapter特定注册表"""
adapter_name = adapter_info.name
if not adapter_name:
logger.error(f"Adapter组件 {adapter_class.__name__} 必须指定名称")
return False
if not isinstance(adapter_info, AdapterInfo) or not issubclass(adapter_class, BaseAdapter):
logger.error(f"注册失败: {adapter_name} 不是有效的Adapter")
return False
_assign_plugin_attrs(
adapter_class, adapter_info.plugin_name, self.get_plugin_config(adapter_info.plugin_name) or {}
)
if not hasattr(self, "_adapter_registry"):
self._adapter_registry: dict[str, type[BaseAdapter]] = {}
self._adapter_registry[adapter_name] = adapter_class
if not adapter_info.enabled:
logger.warning(f"Adapter {adapter_name} 未启用")
return True
# === 组件移除相关 ===
async def remove_component(self, component_name: str, component_type: ComponentType, plugin_name: str) -> bool:
@@ -664,6 +698,7 @@ class ComponentRegistry:
| BaseInterestCalculator
| BasePrompt
| BaseRouterComponent
| BaseAdapter
]
| None
):
@@ -693,6 +728,7 @@ class ComponentRegistry:
| type[BaseInterestCalculator]
| type[BasePrompt]
| type[BaseRouterComponent]
| type[BaseAdapter]
| None,
self._components_classes.get(namespaced_name),
)
@@ -919,6 +955,7 @@ class ComponentRegistry:
chatter_components: int = 0
prompt_components: int = 0
router_components: int = 0
adapter_components: int = 0
for component in self._components.values():
if component.component_type == ComponentType.ACTION:
action_components += 1
@@ -936,6 +973,8 @@ class ComponentRegistry:
prompt_components += 1
elif component.component_type == ComponentType.ROUTER:
router_components += 1
elif component.component_type == ComponentType.ADAPTER:
adapter_components += 1
return {
"action_components": action_components,
"command_components": command_components,
@@ -946,6 +985,7 @@ class ComponentRegistry:
"chatter_components": chatter_components,
"prompt_components": prompt_components,
"router_components": router_components,
"adapter_components": adapter_components,
"total_components": len(self._components),
"total_plugins": len(self._plugins),
"components_by_type": {

View File

@@ -481,13 +481,14 @@ class PluginManager:
chatter_count = stats.get("chatter_components", 0)
prompt_count = stats.get("prompt_components", 0)
router_count = stats.get("router_components", 0)
adapter_count = stats.get("adapter_components", 0)
total_components = stats.get("total_components", 0)
# 📋 显示插件加载总览
if total_registered > 0:
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})"
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}, Adapter: {adapter_count})"
)
# 显示详细的插件列表
@@ -531,6 +532,9 @@ class PluginManager:
router_components = [
c for c in plugin_info.components if c.component_type == ComponentType.ROUTER
]
adapter_components = [
c for c in plugin_info.components if c.component_type == ComponentType.ADAPTER
]
if action_components:
action_details = [format_component(c) for c in action_components]
@@ -560,6 +564,9 @@ class PluginManager:
if router_components:
router_details = [format_component(c) for c in router_components]
logger.info(f" 🌐 Router组件: {', '.join(router_details)}")
if adapter_components:
adapter_details = [format_component(c) for c in adapter_components]
logger.info(f" 🔌 Adapter组件: {', '.join(adapter_details)}")
# 权限节点信息
if plugin_instance := self.loaded_plugins.get(plugin_name):
@@ -698,4 +705,4 @@ class PluginManager:
# 全局插件管理器实例
plugin_manager = PluginManager()
plugin_manager = PluginManager()