mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
* 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
197 lines
5.8 KiB
Python
197 lines
5.8 KiB
Python
from __future__ import annotations
|
|
|
|
from fastapi import APIRouter, Depends, Query, Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from astrbot.core import logger
|
|
from astrbot.dashboard.async_utils import run_maybe_async
|
|
from astrbot.dashboard.schemas import MigrationRequest, PipInstallRequest, UpdateRequest
|
|
from astrbot.dashboard.services.update_service import (
|
|
UpdateService,
|
|
UpdateServiceError,
|
|
UpdateServiceResult,
|
|
)
|
|
|
|
from .auth import AuthContext, require_dashboard_user, require_scope
|
|
|
|
router = APIRouter(tags=["Updates"])
|
|
legacy_router = APIRouter(
|
|
prefix="/api/update",
|
|
tags=["Dashboard Updates"],
|
|
include_in_schema=False,
|
|
)
|
|
|
|
|
|
def get_service(request: Request) -> UpdateService:
|
|
return request.app.state.services.updates
|
|
|
|
|
|
async def require_system_scope(request: Request) -> AuthContext:
|
|
return await require_scope(request, "system")
|
|
|
|
|
|
def _model_dict(payload) -> dict:
|
|
return payload.model_dump(exclude_none=True)
|
|
|
|
|
|
def _result_payload(result: UpdateServiceResult) -> dict:
|
|
if result.status == "success":
|
|
return {
|
|
"status": "success",
|
|
"message": result.message,
|
|
"data": result.data,
|
|
}
|
|
return {
|
|
"status": "ok",
|
|
"message": result.message,
|
|
"data": {} if result.data is None else result.data,
|
|
}
|
|
|
|
|
|
def _service_response(result: UpdateServiceResult) -> JSONResponse:
|
|
return JSONResponse(
|
|
_result_payload(result),
|
|
status_code=200,
|
|
headers=result.headers or None,
|
|
)
|
|
|
|
|
|
def _service_error(exc: UpdateServiceError) -> JSONResponse:
|
|
logger.error(f"Dashboard update operation failed: {exc}", exc_info=True)
|
|
return JSONResponse(
|
|
{"status": "error", "message": "An internal error has occurred.", "data": None},
|
|
status_code=200,
|
|
)
|
|
|
|
|
|
async def _run(operation) -> JSONResponse:
|
|
try:
|
|
result = await run_maybe_async(operation)
|
|
return _service_response(result)
|
|
except UpdateServiceError as exc:
|
|
return _service_error(exc)
|
|
|
|
|
|
@router.get("/updates/check")
|
|
async def check_updates(
|
|
update_type: str | None = Query(default=None, alias="type"),
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.check_update(update_type))
|
|
|
|
|
|
@legacy_router.get("/check")
|
|
async def check_dashboard_updates(
|
|
update_type: str | None = Query(default=None, alias="type"),
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.check_update(update_type))
|
|
|
|
|
|
@router.get("/updates/releases")
|
|
async def update_releases(
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(service.get_releases)
|
|
|
|
|
|
@legacy_router.get("/releases")
|
|
async def dashboard_update_releases(
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(service.get_releases)
|
|
|
|
|
|
@router.get("/updates/progress/{task_id}")
|
|
async def update_progress(
|
|
task_id: str,
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.get_update_progress(task_id))
|
|
|
|
|
|
@legacy_router.get("/progress")
|
|
async def dashboard_update_progress(
|
|
progress_id: str | None = Query(default=None, alias="id"),
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.get_update_progress(progress_id or ""))
|
|
|
|
|
|
@router.post("/updates/core")
|
|
async def update_core(
|
|
payload: UpdateRequest,
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.update_project(_model_dict(payload)))
|
|
|
|
|
|
@legacy_router.post("/do")
|
|
async def update_dashboard_core(
|
|
payload: UpdateRequest,
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.update_project(_model_dict(payload)))
|
|
|
|
|
|
@router.post("/updates/dashboard")
|
|
async def update_dashboard(
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(service.update_dashboard)
|
|
|
|
|
|
@legacy_router.post("/dashboard")
|
|
async def update_dashboard_assets(
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(service.update_dashboard)
|
|
|
|
|
|
@router.post("/pip/install")
|
|
async def install_pip_package(
|
|
payload: PipInstallRequest,
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.install_pip_package(_model_dict(payload)))
|
|
|
|
|
|
@legacy_router.post("/pip-install")
|
|
async def install_dashboard_pip_package(
|
|
payload: PipInstallRequest,
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
return await _run(lambda: service.install_pip_package(_model_dict(payload)))
|
|
|
|
|
|
@router.post("/migrations")
|
|
async def run_migration(
|
|
payload: MigrationRequest | None = None,
|
|
_auth: AuthContext = Depends(require_system_scope),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
body = _model_dict(payload) if payload is not None else {}
|
|
return await _run(lambda: service.do_migration_v4(body))
|
|
|
|
|
|
@legacy_router.post("/migration")
|
|
async def run_dashboard_migration(
|
|
payload: MigrationRequest | None = None,
|
|
_username: str = Depends(require_dashboard_user),
|
|
service: UpdateService = Depends(get_service),
|
|
):
|
|
body = _model_dict(payload) if payload is not None else {}
|
|
return await _run(lambda: service.do_migration_v4(body))
|