From 6a88ced1d532bb93abde2d44a4d262db9e52a0f2 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Mon, 6 Jul 2026 10:11:30 +0800 Subject: [PATCH] =?UTF-8?q?S126=20AstrBot=E5=8E=9F=E7=94=9FWebAPI=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E5=85=BC=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 27 ++++++++++++++++++++++----- tests/test_plugin_entrypoint.py | 6 ++++-- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index 24eea67..a815454 100644 --- a/main.py +++ b/main.py @@ -178,17 +178,15 @@ def _bridge_registration_routes() -> list[dict[str, str]]: routes: list[dict[str, str]] = [] seen: set[tuple[str, str]] = set() catch_all_paths = ( - "/{endpoint:path}", - "/{path:path}", - "{endpoint:path}", - "{path:path}", + f"/{PLUGIN_NAME}/", + f"{PLUGIN_NAME}/", ) for method in sorted({route.method for route in ROUTES}): for path in catch_all_paths: seen.add((method, path)) routes.append({"method": method, "path": path, "endpoint": ""}) for route in ROUTES: - for path in (f"/{route.endpoint}", route.endpoint): + for path in (f"/{PLUGIN_NAME}/{route.endpoint}", f"{PLUGIN_NAME}/{route.endpoint}"): key = (route.method, path) if key in seen: continue @@ -199,11 +197,17 @@ def _bridge_registration_routes() -> list[dict[str, str]]: def _call_web_api_registrar(registrar: Callable[..., Any], path: str, handler: Callable[..., Any], method: str) -> bool: methods = [method] + desc = f"ImageStudio {method} {path}" attempts = ( + lambda: registrar(route=path, view_handler=handler, methods=methods, desc=desc), + lambda: registrar(route=path, view_handler=handler, methods=methods), lambda: registrar(path=path, handler=handler, methods=methods), + lambda: registrar(path=path, handler=handler, methods=methods, desc=desc), lambda: registrar(path=path, handler=handler, method=method), lambda: registrar(endpoint=path, handler=handler, methods=methods), + lambda: registrar(path, handler, methods, desc), lambda: registrar(path, handler, methods=methods), + lambda: registrar(path, handler, methods=methods, desc=desc), lambda: registrar(path, handler, method=method), lambda: registrar(path, methods, handler), lambda: registrar(path, method, handler), @@ -225,6 +229,7 @@ def _make_bridge_handler(endpoint: str, method: str) -> Callable[..., Any]: async def handler(request: Any | None = None, **kwargs: Any) -> dict[str, Any]: positional_endpoint = request if isinstance(request, str) else None actual_request = None if positional_endpoint is not None else request or kwargs.get("request") + actual_request = actual_request or _current_astrbot_web_request() actual_endpoint = endpoint or _first_text( positional_endpoint, kwargs.get("endpoint"), @@ -245,6 +250,18 @@ def _make_bridge_handler(endpoint: str, method: str) -> Callable[..., Any]: return handler +def _current_astrbot_web_request() -> Any | None: + try: + from astrbot.api.web import request as astrbot_web_request + except Exception: + return None + try: + getattr(astrbot_web_request, "method") + except Exception: + return None + return astrbot_web_request + + def _request_context_from_astrbot_request(request: Any | None) -> RequestContext: username = _first_text( _nested_attr(request, "user", "username"), diff --git a/tests/test_plugin_entrypoint.py b/tests/test_plugin_entrypoint.py index 5fdf718..d848e91 100644 --- a/tests/test_plugin_entrypoint.py +++ b/tests/test_plugin_entrypoint.py @@ -76,10 +76,12 @@ class PluginEntrypointTests(unittest.TestCase): self.assertTrue(result["registered"]) self.assertGreater(len(runtime.web_apis), len(ROUTES)) - self.assertTrue(any(api["path"] == "/ui/bootstrap" for api in runtime.web_apis)) + self.assertTrue(any(api["path"] == f"/{PLUGIN_NAME}/ui/bootstrap" for api in runtime.web_apis)) bootstrap_api = next( - api for api in runtime.web_apis if api["path"] == "/ui/bootstrap" and api["methods"] == ["GET"] + api + for api in runtime.web_apis + if api["path"] == f"/{PLUGIN_NAME}/ui/bootstrap" and api["methods"] == ["GET"] ) response = asyncio.run(bootstrap_api["handler"]())