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:
minecraft1024a
2025-11-16 12:41:35 +08:00
parent 4d088e6ce5
commit 42f0e0e023
15 changed files with 257 additions and 18 deletions

View File

@@ -10,10 +10,12 @@ from pathlib import Path
from typing import Any
import orjson
from fastapi import APIRouter, HTTPException, Query, Request
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from src.common.security import get_api_key
# 调整项目根目录的计算方式
project_root = Path(__file__).parent.parent.parent
data_dir = project_root / "data" / "memory_graph"
@@ -23,7 +25,7 @@ graph_data_cache = None
current_data_file = None
# FastAPI 路由
router = APIRouter()
router = APIRouter(dependencies=[Depends(get_api_key)])
# Jinja2 模板引擎
templates = Jinja2Templates(directory=str(Path(__file__).parent / "templates"))

View File

@@ -1,16 +1,17 @@
import time
from typing import Literal
from fastapi import APIRouter, HTTPException, Query
from fastapi import APIRouter, Depends, HTTPException, Query
from src.chat.message_receive.chat_stream import get_chat_manager
from src.common.logger import get_logger
from src.common.security import get_api_key
from src.config.config import global_config
from src.plugin_system.apis import message_api, person_api
logger = get_logger("HTTP消息API")
router = APIRouter()
router = APIRouter(dependencies=[Depends(get_api_key)])
@router.get("/messages/recent")
@@ -161,5 +162,3 @@ async def get_message_stats_by_chat(
# 统一异常处理
logger.error(f"获取消息统计时发生错误: {e}")
raise HTTPException(status_code=500, detail=str(e))

View File

@@ -1,16 +1,17 @@
from datetime import datetime, timedelta
from typing import Literal
from fastapi import APIRouter, HTTPException, Query
from fastapi import APIRouter, Depends, HTTPException, Query
from src.chat.utils.statistic import (
StatisticOutputTask,
)
from src.common.logger import get_logger
from src.common.security import get_api_key
logger = get_logger("LLM统计API")
router = APIRouter()
router = APIRouter(dependencies=[Depends(get_api_key)])
# 定义统计数据的键,以减少魔法字符串
TOTAL_REQ_CNT = "total_requests"