feat(plugin_system): 引入插件HTTP端点系统
引入了全新的 `BaseRouterComponent` 组件类型,允许插件开发者通过继承并实现 `register_endpoints` 方法来创建 FastAPI 路由。 - 插件系统现在可以自动发现并注册这些路由组件,并将它们挂载到主 FastAPI 应用的 `/plugins/<plugin_name>` 前缀下。 - 新增了全局配置 `[plugin_http_system]`,提供了总开关、API 速率限制和 API 密钥认证 (`X-API-Key`) 等功能,以确保端点的安全性和稳定性。 - 更新了 `hello_world_plugin` 插件,增加了一个简单的 `/greet` 端点作为实现示例。
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
from .base_action import BaseAction
|
||||
from .base_command import BaseCommand
|
||||
from .base_events_handler import BaseEventHandler
|
||||
from .base_http_component import BaseRouterComponent
|
||||
from .base_plugin import BasePlugin
|
||||
from .base_prompt import BasePrompt
|
||||
from .base_tool import BaseTool
|
||||
@@ -55,7 +56,7 @@ __all__ = [
|
||||
"PluginMetadata",
|
||||
# 增强命令系统
|
||||
"PlusCommand",
|
||||
"PlusCommandAdapter",
|
||||
"BaseRouterComponent"
|
||||
"PlusCommandInfo",
|
||||
"PythonDependency",
|
||||
"ToolInfo",
|
||||
|
||||
37
src/plugin_system/base/base_http_component.py
Normal file
37
src/plugin_system/base/base_http_component.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from fastapi import APIRouter
|
||||
from .component_types import ComponentType, RouterInfo
|
||||
|
||||
class BaseRouterComponent(ABC):
|
||||
"""
|
||||
用于暴露HTTP端点的组件基类。
|
||||
插件开发者应继承此类,并实现 register_endpoints 方法来定义API路由。
|
||||
"""
|
||||
# 组件元数据,由插件管理器读取
|
||||
component_name: str
|
||||
component_description: str
|
||||
component_version: str = "1.0.0"
|
||||
|
||||
# 每个组件实例都会管理自己的APIRouter
|
||||
router: APIRouter
|
||||
|
||||
def __init__(self):
|
||||
self.router = APIRouter()
|
||||
self.register_endpoints()
|
||||
|
||||
@abstractmethod
|
||||
def register_endpoints(self) -> None:
|
||||
"""
|
||||
【开发者必须实现】
|
||||
在此方法中定义所有HTTP端点。
|
||||
"""
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def get_router_info(cls) -> "RouterInfo":
|
||||
"""从类属性生成RouterInfo"""
|
||||
return RouterInfo(
|
||||
name=cls.component_name,
|
||||
description=getattr(cls, "component_description", "路由组件"),
|
||||
component_type=ComponentType.ROUTER,
|
||||
)
|
||||
@@ -53,6 +53,7 @@ class ComponentType(Enum):
|
||||
CHATTER = "chatter" # 聊天处理器组件
|
||||
INTEREST_CALCULATOR = "interest_calculator" # 兴趣度计算组件
|
||||
PROMPT = "prompt" # Prompt组件
|
||||
ROUTER = "router" # 路由组件
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.value
|
||||
@@ -146,6 +147,7 @@ class PermissionNodeField:
|
||||
node_name: str # 节点名称 (例如 "manage" 或 "view")
|
||||
description: str # 权限描述
|
||||
|
||||
|
||||
@dataclass
|
||||
class ComponentInfo:
|
||||
"""组件信息"""
|
||||
@@ -442,3 +444,13 @@ class MaiMessages:
|
||||
def __post_init__(self):
|
||||
if self.message_segments is None:
|
||||
self.message_segments = []
|
||||
|
||||
@dataclass
|
||||
class RouterInfo(ComponentInfo):
|
||||
"""路由组件信息"""
|
||||
|
||||
auth_required: bool = False
|
||||
|
||||
def __post_init__(self):
|
||||
super().__post_init__()
|
||||
self.component_type = ComponentType.ROUTER
|
||||
|
||||
Reference in New Issue
Block a user