mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
refactor(core): migrate backend backbone from Quart to FastAPI and introduce more OpenAPI (#8688)
* refactor: migrate to fastapi * structure refactor * fix: pyright fix * refactor: improve error handling and public messages in plugin services * feat(api): refactor API client integration and enhance request handling - Updated API client configuration to use a dedicated HTTP client. - Introduced utility functions for generating options, queries, and form data for API requests. - Refactored multiple API methods to utilize the new utility functions for improved consistency and readability. - Renamed types for clarity and updated import statements accordingly. feat(docs): add script to update OpenAPI JSON from YAML spec - Created a Python script to convert OpenAPI YAML specification to JSON format. - The script supports customizable input and output paths. - Ensured the script handles directory creation for output paths and validates the YAML structure. * fix * feat(auth): implement rate limiting for v1 login endpoint and enhance request handling * Refactor dashboard API routers to use legacy_router for backward compatibility - Changed all instances of dashboard_router to legacy_router across multiple API modules including platform, plugins, providers, sessions, skills, stats, subagents, t2i, tools, updates, and asgi_runtime. - Updated route definitions to ensure existing endpoints remain functional under the new router structure. - Introduced support for Quart request context in asgi_runtime to enhance compatibility with existing Quart-based plugins. - Added a test case to validate the functionality of the new Quart request context handling in plugin extensions. * chore: remove cli test * fix: update dashboard tests for fastapi migration * chore: satisfy ruff checks * fix: update openapi api key scopes * fix: sync config scope chip selection * fix: restore quart dependency * docs: clarify quart plugin api compatibility * docs: update openapi scope documentation * fix: use singular skill openapi scope * fix: hide update service exception details * fix: address fastapi review comments * fix: address dashboard review findings * docs: revert unrelated package deployment changes * docs: update agent api generation guidance * feat: add plugin page web api helpers * docs: add plugin page bridge demo * fix: type plugin upload files * fix: stabilize plugin page uploads * fix: type plugin web request proxy * docs: remove plugin page docs example * fix: authenticate plugin page SSE bridge
This commit is contained in:
@@ -1,41 +1,27 @@
|
||||
import asyncio
|
||||
import uuid
|
||||
from io import BytesIO
|
||||
from unittest.mock import AsyncMock
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from quart import Quart, g, request
|
||||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from astrbot.core import LogBroker
|
||||
from astrbot.core.core_lifecycle import AstrBotCoreLifecycle
|
||||
from astrbot.core.db.sqlite import SQLiteDatabase
|
||||
from astrbot.core.utils.auth_password import (
|
||||
hash_dashboard_password,
|
||||
hash_legacy_dashboard_password,
|
||||
hash_md5_dashboard_password,
|
||||
)
|
||||
from astrbot.dashboard.routes.route import Response
|
||||
from astrbot.dashboard.api import open_api as open_api_routes
|
||||
from astrbot.dashboard.asgi_runtime import FastAPIAppAdapter
|
||||
from astrbot.dashboard.responses import ok
|
||||
from astrbot.dashboard.server import AstrBotDashboard
|
||||
|
||||
_TEST_DASHBOARD_PASSWORD = "AstrbotTest123"
|
||||
|
||||
|
||||
def _get_open_api_route(app: Quart):
|
||||
rule = next(
|
||||
(
|
||||
item
|
||||
for item in app.url_map.iter_rules()
|
||||
if item.rule == "/api/v1/chat" and "POST" in item.methods
|
||||
),
|
||||
None,
|
||||
)
|
||||
assert rule is not None
|
||||
return app.view_functions[rule.endpoint].__self__
|
||||
|
||||
|
||||
async def _create_api_key(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
*,
|
||||
scopes: list[str],
|
||||
@@ -71,7 +57,7 @@ async def core_lifecycle_td(tmp_path_factory):
|
||||
hash_dashboard_password(dashboard_password)
|
||||
)
|
||||
core_lifecycle.astrbot_config["dashboard"]["password"] = (
|
||||
hash_legacy_dashboard_password(dashboard_password)
|
||||
hash_md5_dashboard_password(dashboard_password)
|
||||
)
|
||||
object.__setattr__(
|
||||
core_lifecycle,
|
||||
@@ -107,7 +93,9 @@ def _resolve_dashboard_password(core_lifecycle_td: AstrBotCoreLifecycle) -> str:
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="module")
|
||||
async def authenticated_header(app: Quart, core_lifecycle_td: AstrBotCoreLifecycle):
|
||||
async def authenticated_header(
|
||||
app: FastAPIAppAdapter, core_lifecycle_td: AstrBotCoreLifecycle
|
||||
):
|
||||
test_client = app.test_client()
|
||||
response = await test_client.post(
|
||||
"/api/auth/login",
|
||||
@@ -122,7 +110,9 @@ async def authenticated_header(app: Quart, core_lifecycle_td: AstrBotCoreLifecyc
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_api_key_scope_and_revoke(app: Quart, authenticated_header: dict):
|
||||
async def test_api_key_scope_and_revoke(
|
||||
app: FastAPIAppAdapter, authenticated_header: dict
|
||||
):
|
||||
test_client = app.test_client()
|
||||
|
||||
raw_key, key_id = await _create_api_key(
|
||||
@@ -177,7 +167,9 @@ async def test_api_key_scope_and_revoke(app: Quart, authenticated_header: dict):
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_send_message_with_api_key(app: Quart, authenticated_header: dict):
|
||||
async def test_open_send_message_with_api_key(
|
||||
app: FastAPIAppAdapter, authenticated_header: dict
|
||||
):
|
||||
test_client = app.test_client()
|
||||
|
||||
raw_key, _ = await _create_api_key(
|
||||
@@ -202,7 +194,7 @@ async def test_open_send_message_with_api_key(app: Quart, authenticated_header:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_send_auto_session_id_and_username(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
core_lifecycle_td: AstrBotCoreLifecycle,
|
||||
):
|
||||
@@ -214,24 +206,21 @@ async def test_open_chat_send_auto_session_id_and_username(
|
||||
scopes=["chat"],
|
||||
name_prefix="chat-send-key",
|
||||
)
|
||||
open_api_route = _get_open_api_route(app)
|
||||
|
||||
original_chat = open_api_route.chat_route.chat
|
||||
|
||||
async def fake_chat(post_data: dict | None = None):
|
||||
payload = post_data or await request.get_json()
|
||||
return (
|
||||
Response()
|
||||
.ok(
|
||||
data={
|
||||
"session_id": payload.get("session_id"),
|
||||
"creator": g.get("username"),
|
||||
}
|
||||
)
|
||||
.__dict__
|
||||
async def fake_chat_response(_chat_service, username: str, post_data: dict):
|
||||
return ok(
|
||||
{
|
||||
"session_id": post_data.get("session_id"),
|
||||
"creator": username,
|
||||
}
|
||||
)
|
||||
|
||||
open_api_route.chat_route.chat = fake_chat
|
||||
monkeypatch = pytest.MonkeyPatch()
|
||||
monkeypatch.setattr(
|
||||
open_api_routes,
|
||||
"_build_streaming_chat_response",
|
||||
fake_chat_response,
|
||||
)
|
||||
try:
|
||||
send_res = await test_client.post(
|
||||
"/api/v1/chat",
|
||||
@@ -243,7 +232,7 @@ async def test_open_chat_send_auto_session_id_and_username(
|
||||
headers={"X-API-Key": raw_key},
|
||||
)
|
||||
finally:
|
||||
open_api_route.chat_route.chat = original_chat
|
||||
monkeypatch.undo()
|
||||
|
||||
assert send_res.status_code == 200
|
||||
send_data = await send_res.get_json()
|
||||
@@ -293,7 +282,7 @@ async def test_open_chat_send_auto_session_id_and_username(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_sessions_pagination(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
core_lifecycle_td: AstrBotCoreLifecycle,
|
||||
):
|
||||
@@ -358,7 +347,7 @@ async def test_open_chat_sessions_pagination(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_configs_list(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
):
|
||||
test_client = app.test_client()
|
||||
@@ -388,7 +377,7 @@ async def test_open_chat_configs_list(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_api_auth_validation_and_key_carriers(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
):
|
||||
test_client = app.test_client()
|
||||
@@ -432,7 +421,7 @@ async def test_open_api_auth_validation_and_key_carriers(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_send_conversation_alias_and_blank_username(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
core_lifecycle_td: AstrBotCoreLifecycle,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
@@ -444,16 +433,18 @@ async def test_open_chat_send_conversation_alias_and_blank_username(
|
||||
scopes=["chat"],
|
||||
name_prefix="chat-conversation-key",
|
||||
)
|
||||
open_api_route = _get_open_api_route(app)
|
||||
|
||||
async def fake_chat(post_data: dict | None = None):
|
||||
payload = post_data or await request.get_json()
|
||||
resolved_session_id = payload.get("session_id") or payload.get(
|
||||
async def fake_chat_response(_chat_service, _username: str, post_data: dict):
|
||||
resolved_session_id = post_data.get("session_id") or post_data.get(
|
||||
"conversation_id"
|
||||
)
|
||||
return Response().ok(data={"session_id": resolved_session_id}).__dict__
|
||||
return ok({"session_id": resolved_session_id})
|
||||
|
||||
monkeypatch.setattr(open_api_route.chat_route, "chat", fake_chat)
|
||||
monkeypatch.setattr(
|
||||
open_api_routes,
|
||||
"_build_streaming_chat_response",
|
||||
fake_chat_response,
|
||||
)
|
||||
|
||||
conversation_id = f"open_api_conversation_{uuid.uuid4().hex[:10]}"
|
||||
send_res = await test_client.post(
|
||||
@@ -494,7 +485,7 @@ async def test_open_chat_send_conversation_alias_and_blank_username(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_send_config_resolution(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
):
|
||||
@@ -505,8 +496,6 @@ async def test_open_chat_send_config_resolution(
|
||||
scopes=["chat"],
|
||||
name_prefix="chat-config-resolution-key",
|
||||
)
|
||||
open_api_route = _get_open_api_route(app)
|
||||
|
||||
conf_list = [
|
||||
{
|
||||
"id": "default",
|
||||
@@ -518,35 +507,38 @@ async def test_open_chat_send_config_resolution(
|
||||
{"id": "cfg-1", "name": "Duplicated", "path": "a.json", "is_default": False},
|
||||
{"id": "cfg-2", "name": "Duplicated", "path": "b.json", "is_default": False},
|
||||
]
|
||||
monkeypatch.setattr(open_api_route, "_get_chat_config_list", lambda: conf_list)
|
||||
monkeypatch.setattr(
|
||||
open_api_routes,
|
||||
"_get_chat_config_list",
|
||||
lambda _service: conf_list,
|
||||
)
|
||||
|
||||
update_route = AsyncMock()
|
||||
delete_route = AsyncMock()
|
||||
monkeypatch.setattr(
|
||||
open_api_route.core_lifecycle.umop_config_router,
|
||||
app._dashboard_server.core_lifecycle.umop_config_router,
|
||||
"update_route",
|
||||
update_route,
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
open_api_route.core_lifecycle.umop_config_router,
|
||||
app._dashboard_server.core_lifecycle.umop_config_router,
|
||||
"delete_route",
|
||||
delete_route,
|
||||
)
|
||||
|
||||
async def fake_chat(post_data: dict | None = None):
|
||||
payload = post_data or await request.get_json()
|
||||
return (
|
||||
Response()
|
||||
.ok(
|
||||
data={
|
||||
"session_id": payload.get("session_id"),
|
||||
"creator": g.get("username"),
|
||||
}
|
||||
)
|
||||
.__dict__
|
||||
async def fake_chat_response(_chat_service, username: str, post_data: dict):
|
||||
return ok(
|
||||
{
|
||||
"session_id": post_data.get("session_id"),
|
||||
"creator": username,
|
||||
}
|
||||
)
|
||||
|
||||
monkeypatch.setattr(open_api_route.chat_route, "chat", fake_chat)
|
||||
monkeypatch.setattr(
|
||||
open_api_routes,
|
||||
"_build_streaming_chat_response",
|
||||
fake_chat_response,
|
||||
)
|
||||
|
||||
invalid_config_id_res = await test_client.post(
|
||||
"/api/v1/chat",
|
||||
@@ -632,7 +624,7 @@ async def test_open_chat_send_config_resolution(
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_chat_sessions_input_validation_and_filtering(
|
||||
app: Quart,
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
core_lifecycle_td: AstrBotCoreLifecycle,
|
||||
):
|
||||
@@ -708,7 +700,9 @@ async def test_open_chat_sessions_input_validation_and_filtering(
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_send_message_error_paths(app: Quart, authenticated_header: dict):
|
||||
async def test_open_send_message_error_paths(
|
||||
app: FastAPIAppAdapter, authenticated_header: dict
|
||||
):
|
||||
test_client = app.test_client()
|
||||
raw_key, _ = await _create_api_key(
|
||||
app,
|
||||
@@ -763,41 +757,48 @@ async def test_open_send_message_error_paths(app: Quart, authenticated_header: d
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_open_file_upload_requires_file_and_can_upload(
|
||||
app: Quart,
|
||||
async def test_open_api_key_scope_normalization(
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
):
|
||||
test_client = app.test_client()
|
||||
raw_key, _ = await _create_api_key(
|
||||
app,
|
||||
authenticated_header,
|
||||
scopes=["file"],
|
||||
name_prefix="file-scope-key",
|
||||
)
|
||||
|
||||
missing_file_res = await test_client.post(
|
||||
"/api/v1/file",
|
||||
data={},
|
||||
headers={"X-API-Key": raw_key},
|
||||
config_res = await test_client.post(
|
||||
"/api/apikey/create",
|
||||
json={"name": "config-contained-scopes-key", "scopes": ["config"]},
|
||||
headers=authenticated_header,
|
||||
)
|
||||
missing_file_data = await missing_file_res.get_json()
|
||||
assert missing_file_data["status"] == "error"
|
||||
assert missing_file_data["message"] == "Missing key: file"
|
||||
config_data = await config_res.get_json()
|
||||
|
||||
upload_res = await test_client.post(
|
||||
"/api/v1/file",
|
||||
files={
|
||||
"file": FileStorage(
|
||||
stream=BytesIO(b"openapi-file-content"),
|
||||
filename="openapi_test.txt",
|
||||
content_type="text/plain",
|
||||
)
|
||||
},
|
||||
headers={"X-API-Key": raw_key},
|
||||
assert config_res.status_code == 200
|
||||
assert config_data["status"] == "ok"
|
||||
assert set(config_data["data"]["scopes"]) == {"config", "bot", "provider"}
|
||||
|
||||
extra_scope_res = await test_client.post(
|
||||
"/api/apikey/create",
|
||||
json={"name": "mcp-skill-scope-key", "scopes": ["mcp", "skill"]},
|
||||
headers=authenticated_header,
|
||||
)
|
||||
assert upload_res.status_code == 200
|
||||
upload_data = await upload_res.get_json()
|
||||
assert upload_data["status"] == "ok"
|
||||
assert isinstance(upload_data["data"]["attachment_id"], str)
|
||||
assert upload_data["data"]["filename"] == "openapi_test.txt"
|
||||
assert upload_data["data"]["type"] == "file"
|
||||
extra_scope_data = await extra_scope_res.get_json()
|
||||
|
||||
assert extra_scope_res.status_code == 200
|
||||
assert extra_scope_data["status"] == "ok"
|
||||
assert set(extra_scope_data["data"]["scopes"]) == {"mcp", "skill"}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_scope_is_not_available_for_developer_api_key(
|
||||
app: FastAPIAppAdapter,
|
||||
authenticated_header: dict,
|
||||
):
|
||||
test_client = app.test_client()
|
||||
create_res = await test_client.post(
|
||||
"/api/apikey/create",
|
||||
json={"name": "file-scope-key", "scopes": ["file"]},
|
||||
headers=authenticated_header,
|
||||
)
|
||||
create_data = await create_res.get_json()
|
||||
|
||||
assert create_res.status_code == 400
|
||||
assert create_data["status"] == "error"
|
||||
assert create_data["message"] == "Invalid scopes: file"
|
||||
|
||||
Reference in New Issue
Block a user