astrbot_plugin_imagestudio/adapters/webui_adapter.py

57 行
1.8 KiB
Python

2026-07-05 09:24:00 +08:00
"""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
2026-07-05 22:11:17 +08:00
from typing import Any
from backend.dto.common import ErrorCode, error_response
2026-07-05 09:24:00 +08:00
from backend.services.web_hosting_service import WebHostingService
from backend.route_registry import route_manifest
2026-07-05 09:24:00 +08:00
def get_web_mount_manifest() -> dict:
manifest = WebHostingService().manifest()
manifest["api_routes"] = route_manifest()
return manifest
2026-07-05 22:11:17 +08:00
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:
2026-07-05 22:14:51 +08:00
payload = web_service.standalone_asset_payload(relative_path)
2026-07-05 22:11:17 +08:00
except Exception as exc:
from backend.dto.common import response_from_exception
return response_from_exception(exc)
return {
"ok": True,
"data": {
2026-07-05 22:14:51 +08:00
**payload,
2026-07-05 22:11:17 +08:00
"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