astrbot_plugin_imagestudio/adapters/webui_adapter.py

56 行
1.8 KiB
Python

"""AstrBot WebUI adapter helpers.
The actual AstrBot runtime performs route registration. This adapter keeps the
ImageStudio mount contract in one place so the runtime layer can wire it
without starting a standalone server.
"""
from __future__ import annotations
from typing import Any
from backend.dto.common import ErrorCode, error_response
from backend.services.web_hosting_service import WebHostingService
from backend.route_registry import route_manifest
def get_web_mount_manifest() -> dict:
manifest = WebHostingService().manifest()
manifest["api_routes"] = route_manifest()
return manifest
def resolve_web_asset(path: str, service: WebHostingService | None = None) -> dict[str, Any]:
web_service = service or WebHostingService()
relative_path = _standalone_relative_path(path)
if relative_path is None:
return error_response(
error_code=ErrorCode.RESOURCE_NOT_FOUND,
message="web asset route not found",
details={"path": path},
)
try:
metadata = web_service.resolve_standalone_asset(relative_path)
except Exception as exc:
from backend.dto.common import response_from_exception
return response_from_exception(exc)
return {
"ok": True,
"data": {
**metadata,
"relative_path": relative_path,
},
"warnings": [],
"request_id": None,
}
def _standalone_relative_path(path: str) -> str | None:
normalized = str(path or "").split("?", 1)[0].replace("\\", "/").strip("/")
mount = "api/v1/plugins/extensions/astrbot_plugin_imagestudio/web"
if normalized == mount:
return "index.html"
if normalized.startswith(f"{mount}/"):
return normalized[len(mount) + 1 :] or "index.html"
return None