Dispatch AstrBot extension requests
这个提交存在于:
父节点
ff0c834357
当前提交
483034235c
共有 2 个文件被更改,包括 67 次插入 和 1 次删除
|
|
@ -7,12 +7,15 @@ use these pure helpers while registering plugin extension routes in-process.
|
|||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import inspect
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable
|
||||
|
||||
from backend.dto.common import ErrorCode, error_response
|
||||
from backend.route_registry import ROUTES, RouteSpec
|
||||
from backend.services.ui_service import API_BASE
|
||||
from core.identity import RequestContext
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -71,6 +74,25 @@ def runtime_route_table() -> list[dict[str, Any]]:
|
|||
]
|
||||
|
||||
|
||||
def dispatch_extension_request(
|
||||
ctx: RequestContext,
|
||||
method: str,
|
||||
path: str,
|
||||
*,
|
||||
query: dict[str, Any] | None = None,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
match = resolve_route(method, path)
|
||||
if match is None:
|
||||
return error_response(
|
||||
error_code=ErrorCode.RESOURCE_NOT_FOUND,
|
||||
message="route not found",
|
||||
details={"method": str(method or "").upper(), "path": normalize_extension_path(path)},
|
||||
request_id=ctx.request_id,
|
||||
)
|
||||
return match.handler(*_handler_args(match, ctx, query or {}, payload or {}))
|
||||
|
||||
|
||||
def _match_endpoint(pattern: str, endpoint: str) -> dict[str, str] | None:
|
||||
param_names = re.findall(r"\{([^{}]+)\}", pattern)
|
||||
regex = "^" + re.sub(r"\{([^{}]+)\}", r"([^/]+)", pattern) + "$"
|
||||
|
|
@ -80,6 +102,28 @@ def _match_endpoint(pattern: str, endpoint: str) -> dict[str, str] | None:
|
|||
return dict(zip(param_names, match.groups(), strict=False))
|
||||
|
||||
|
||||
def _handler_args(
|
||||
match: RouteMatch,
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any],
|
||||
payload: dict[str, Any],
|
||||
) -> list[Any]:
|
||||
args: list[Any] = []
|
||||
for index, parameter in enumerate(inspect.signature(match.handler).parameters.values()):
|
||||
if index == 0:
|
||||
args.append(ctx)
|
||||
continue
|
||||
if parameter.name == "query":
|
||||
args.append(query)
|
||||
elif parameter.name == "payload":
|
||||
args.append(payload)
|
||||
elif parameter.name in match.path_params:
|
||||
args.append(match.path_params[parameter.name])
|
||||
elif parameter.default is inspect.Parameter.empty:
|
||||
raise TypeError(f"cannot dispatch required parameter: {parameter.name}")
|
||||
return args
|
||||
|
||||
|
||||
def _load_handler(handler_path: str) -> Callable[..., Any]:
|
||||
module_name, function_name = handler_path.rsplit(".", 1)
|
||||
handler = getattr(importlib.import_module(module_name), function_name)
|
||||
|
|
|
|||
|
|
@ -3,9 +3,15 @@ from __future__ import annotations
|
|||
import importlib
|
||||
import unittest
|
||||
|
||||
from adapters.astrbot_adapter import normalize_extension_path, resolve_route, runtime_route_table
|
||||
from adapters.astrbot_adapter import (
|
||||
dispatch_extension_request,
|
||||
normalize_extension_path,
|
||||
resolve_route,
|
||||
runtime_route_table,
|
||||
)
|
||||
from adapters.webui_adapter import get_web_mount_manifest
|
||||
from backend.route_registry import ROUTES, assert_unique_routes, route_manifest
|
||||
from core.identity import RequestContext
|
||||
|
||||
|
||||
class RouteRegistryTests(unittest.TestCase):
|
||||
|
|
@ -78,6 +84,22 @@ class RouteRegistryTests(unittest.TestCase):
|
|||
table = runtime_route_table()
|
||||
self.assertTrue(any(route["path"].endswith("/dashboard/summary") for route in table))
|
||||
|
||||
def test_astrbot_adapter_dispatches_pure_route_handlers(self):
|
||||
ctx = RequestContext.from_astrbot(username="admin", user_id="user_001", permissions={"can_manage_config"})
|
||||
|
||||
response = dispatch_extension_request(
|
||||
ctx,
|
||||
"GET",
|
||||
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/ui/actions",
|
||||
)
|
||||
|
||||
self.assertTrue(response["ok"])
|
||||
self.assertTrue(any(item["action_id"] == "open_workbench" for item in response["data"]))
|
||||
|
||||
missing = dispatch_extension_request(ctx, "GET", "/api/v1/plugins/extensions/astrbot_plugin_imagestudio/nope")
|
||||
self.assertFalse(missing["ok"])
|
||||
self.assertEqual(missing["error_code"], "RESOURCE_NOT_FOUND")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用