Files
AstrBot/astrbot/dashboard/api/logs.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

132 lines
3.7 KiB
Python

from __future__ import annotations
from fastapi import APIRouter, Depends, Header, Request
from fastapi.responses import StreamingResponse
from astrbot.dashboard.responses import ApiError, ok
from astrbot.dashboard.schemas import TraceSettingsRequest
from astrbot.dashboard.services.log_service import LogService, LogServiceError
from .auth import AuthContext, require_dashboard_user, require_scope
router = APIRouter(tags=["Logs"])
legacy_router = APIRouter(
prefix="/api",
tags=["Dashboard Logs"],
include_in_schema=False,
)
async def require_system_scope(request: Request) -> AuthContext:
return await require_scope(request, "system")
def get_service(request: Request) -> LogService:
return request.app.state.services.logs
def _raise_log_error(exc: LogServiceError) -> None:
raise ApiError(str(exc)) from exc
def _log_stream_response(last_event_id: str | None, service: LogService):
return StreamingResponse(
service.stream_log_events(last_event_id),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"Transfer-Encoding": "chunked",
},
)
def _get_log_history(service: LogService):
try:
return ok(service.get_log_history())
except LogServiceError as exc:
_raise_log_error(exc)
def _get_trace_settings(service: LogService):
try:
return ok(service.get_trace_settings())
except LogServiceError as exc:
_raise_log_error(exc)
def _update_trace_settings(payload: TraceSettingsRequest, service: LogService):
try:
message = service.update_trace_settings(payload.model_dump(exclude_none=True))
return ok(message=message)
except LogServiceError as exc:
_raise_log_error(exc)
@router.get("/logs/history")
async def get_log_history(
_auth: AuthContext = Depends(require_system_scope),
service: LogService = Depends(get_service),
):
return _get_log_history(service)
@router.get("/logs/live")
async def live_logs(
last_event_id: str | None = Header(default=None, alias="Last-Event-ID"),
_auth: AuthContext = Depends(require_system_scope),
service: LogService = Depends(get_service),
):
return _log_stream_response(last_event_id, service)
@router.get("/trace/settings")
async def get_trace_settings(
_auth: AuthContext = Depends(require_system_scope),
service: LogService = Depends(get_service),
):
return _get_trace_settings(service)
@router.put("/trace/settings")
async def update_trace_settings(
payload: TraceSettingsRequest,
_auth: AuthContext = Depends(require_system_scope),
service: LogService = Depends(get_service),
):
return _update_trace_settings(payload, service)
@legacy_router.get("/log-history")
async def get_dashboard_log_history(
_username: str = Depends(require_dashboard_user),
service: LogService = Depends(get_service),
):
return _get_log_history(service)
@legacy_router.get("/live-log")
async def get_dashboard_live_logs(
last_event_id: str | None = Header(default=None, alias="Last-Event-ID"),
_username: str = Depends(require_dashboard_user),
service: LogService = Depends(get_service),
):
return _log_stream_response(last_event_id, service)
@legacy_router.get("/trace/settings")
async def get_dashboard_trace_settings(
_username: str = Depends(require_dashboard_user),
service: LogService = Depends(get_service),
):
return _get_trace_settings(service)
@legacy_router.post("/trace/settings")
async def update_dashboard_trace_settings(
payload: TraceSettingsRequest,
_username: str = Depends(require_dashboard_user),
service: LogService = Depends(get_service),
):
return _update_trace_settings(payload, service)