Files
AstrBot/astrbot/dashboard/api/extensions.py
Weilong Liao 0d8e8682db 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
2026-06-14 15:03:26 +08:00

181 lines
5.1 KiB
Python

from __future__ import annotations
from fastapi import APIRouter, Depends, Request
from astrbot.dashboard.responses import ApiError, ok
from astrbot.dashboard.schemas import (
CommandPermissionRequest,
CommandRenameRequest,
CommandToggleRequest,
CommandUpdateRequest,
)
from astrbot.dashboard.services.command_service import (
CommandService,
CommandServiceError,
)
from .auth import AuthContext, require_dashboard_user, require_scope
router = APIRouter(tags=["Extension Components"])
legacy_router = APIRouter(
prefix="/api",
tags=["Dashboard Extension Components"],
include_in_schema=False,
)
def get_command_service(request: Request) -> CommandService:
return request.app.state.services.commands
async def require_tool_scope(request: Request) -> AuthContext:
return await require_scope(request, "tool")
def _raise_command_error(exc: CommandServiceError) -> None:
raise ApiError(str(exc)) from exc
async def _list_commands(config_id: str | None, service: CommandService):
try:
return ok(await service.list_commands(config_id or ""))
except CommandServiceError as exc:
_raise_command_error(exc)
async def _list_command_conflicts(service: CommandService):
try:
return ok(await service.list_conflicts())
except CommandServiceError as exc:
_raise_command_error(exc)
async def _toggle_command(payload: CommandToggleRequest, service: CommandService):
try:
return ok(
await service.toggle_command(payload.handler_full_name, payload.enabled)
)
except CommandServiceError as exc:
_raise_command_error(exc)
async def _rename_command(payload: CommandRenameRequest, service: CommandService):
try:
return ok(
await service.rename_command(
payload.handler_full_name,
payload.new_name,
aliases=payload.aliases,
)
)
except CommandServiceError as exc:
_raise_command_error(exc)
async def _update_command_permission(
payload: CommandPermissionRequest,
service: CommandService,
):
try:
return ok(
await service.update_permission(
payload.handler_full_name, payload.permission
)
)
except CommandServiceError as exc:
_raise_command_error(exc)
@router.get("/commands")
async def list_commands(
config_id: str | None = None,
_auth: AuthContext = Depends(require_tool_scope),
service: CommandService = Depends(get_command_service),
):
return await _list_commands(config_id, service)
@router.get("/commands/conflicts")
async def list_command_conflicts(
_auth: AuthContext = Depends(require_tool_scope),
service: CommandService = Depends(get_command_service),
):
return await _list_command_conflicts(service)
@router.patch("/commands/{command_id:path}")
async def update_command(
command_id: str,
payload: CommandUpdateRequest,
_auth: AuthContext = Depends(require_tool_scope),
service: CommandService = Depends(get_command_service),
):
if payload.enabled is not None:
return await _toggle_command(
CommandToggleRequest(
handler_full_name=command_id,
enabled=payload.enabled,
),
service,
)
if payload.alias is not None:
return await _rename_command(
CommandRenameRequest(
handler_full_name=command_id,
new_name=payload.alias,
aliases=payload.aliases,
),
service,
)
return await _update_command_permission(
CommandPermissionRequest(
handler_full_name=command_id,
permission=payload.permission_group or "",
),
service,
)
@legacy_router.get("/commands")
async def list_dashboard_commands(
config_id: str | None = None,
_username: str = Depends(require_dashboard_user),
service: CommandService = Depends(get_command_service),
):
return await _list_commands(config_id, service)
@legacy_router.get("/commands/conflicts")
async def list_dashboard_command_conflicts(
_username: str = Depends(require_dashboard_user),
service: CommandService = Depends(get_command_service),
):
return await _list_command_conflicts(service)
@legacy_router.post("/commands/toggle")
async def toggle_dashboard_command(
payload: CommandToggleRequest,
_username: str = Depends(require_dashboard_user),
service: CommandService = Depends(get_command_service),
):
return await _toggle_command(payload, service)
@legacy_router.post("/commands/rename")
async def rename_dashboard_command(
payload: CommandRenameRequest,
_username: str = Depends(require_dashboard_user),
service: CommandService = Depends(get_command_service),
):
return await _rename_command(payload, service)
@legacy_router.post("/commands/permission")
async def update_dashboard_command_permission(
payload: CommandPermissionRequest,
_username: str = Depends(require_dashboard_user),
service: CommandService = Depends(get_command_service),
):
return await _update_command_permission(payload, service)