Add AstrBot route resolver
这个提交存在于:
父节点
e1c5c4b645
当前提交
ff0c834357
共有 2 个文件被更改,包括 107 次插入 和 1 次删除
|
|
@ -1 +1,88 @@
|
|||
"""AstrBot runtime adapter placeholder."""
|
||||
"""AstrBot extension route resolution helpers.
|
||||
|
||||
This module intentionally avoids starting a standalone web server. AstrBot can
|
||||
use these pure helpers while registering plugin extension routes in-process.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import importlib
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable
|
||||
|
||||
from backend.route_registry import ROUTES, RouteSpec
|
||||
from backend.services.ui_service import API_BASE
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class RouteMatch:
|
||||
method: str
|
||||
endpoint: str
|
||||
permission: str
|
||||
handler_path: str
|
||||
handler: Callable[..., Any]
|
||||
path_params: dict[str, str]
|
||||
|
||||
|
||||
def normalize_extension_path(path: str) -> str:
|
||||
normalized = str(path or "").strip()
|
||||
if "?" in normalized:
|
||||
normalized = normalized.split("?", 1)[0]
|
||||
normalized = normalized.replace("\\", "/").strip("/")
|
||||
api_base = API_BASE.strip("/")
|
||||
if normalized == api_base:
|
||||
return ""
|
||||
if normalized.startswith(f"{api_base}/"):
|
||||
normalized = normalized[len(api_base) + 1 :]
|
||||
return normalized.strip("/")
|
||||
|
||||
|
||||
def resolve_route(method: str, path: str) -> RouteMatch | None:
|
||||
normalized_method = str(method or "").strip().upper()
|
||||
endpoint = normalize_extension_path(path)
|
||||
for route in ROUTES:
|
||||
if route.method != normalized_method:
|
||||
continue
|
||||
params = _match_endpoint(route.endpoint, endpoint)
|
||||
if params is None:
|
||||
continue
|
||||
return RouteMatch(
|
||||
method=route.method,
|
||||
endpoint=route.endpoint,
|
||||
permission=route.permission,
|
||||
handler_path=route.handler,
|
||||
handler=_load_handler(route.handler),
|
||||
path_params=params,
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def runtime_route_table() -> list[dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
"method": route.method,
|
||||
"endpoint": route.endpoint,
|
||||
"path": f"{API_BASE}/{route.endpoint}",
|
||||
"permission": route.permission,
|
||||
"handler": route.handler,
|
||||
}
|
||||
for route in ROUTES
|
||||
]
|
||||
|
||||
|
||||
def _match_endpoint(pattern: str, endpoint: str) -> dict[str, str] | None:
|
||||
param_names = re.findall(r"\{([^{}]+)\}", pattern)
|
||||
regex = "^" + re.sub(r"\{([^{}]+)\}", r"([^/]+)", pattern) + "$"
|
||||
match = re.match(regex, endpoint)
|
||||
if not match:
|
||||
return None
|
||||
return dict(zip(param_names, match.groups(), strict=False))
|
||||
|
||||
|
||||
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)
|
||||
if not callable(handler):
|
||||
raise TypeError(f"route handler is not callable: {handler_path}")
|
||||
return handler
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
import importlib
|
||||
import unittest
|
||||
|
||||
from adapters.astrbot_adapter import 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
|
||||
|
||||
|
|
@ -59,6 +60,24 @@ class RouteRegistryTests(unittest.TestCase):
|
|||
|
||||
self.assertTrue(callable(handler), route.handler)
|
||||
|
||||
def test_astrbot_adapter_resolves_extension_routes(self):
|
||||
self.assertEqual(
|
||||
normalize_extension_path(
|
||||
"/api/v1/plugins/extensions/astrbot_plugin_imagestudio/nodes/node_001/review?x=1"
|
||||
),
|
||||
"nodes/node_001/review",
|
||||
)
|
||||
|
||||
match = resolve_route("GET", "/api/v1/plugins/extensions/astrbot_plugin_imagestudio/nodes/node_001/review")
|
||||
|
||||
self.assertIsNotNone(match)
|
||||
self.assertEqual(match.endpoint, "nodes/{node_id}/review")
|
||||
self.assertEqual(match.path_params, {"node_id": "node_001"})
|
||||
self.assertTrue(callable(match.handler))
|
||||
|
||||
table = runtime_route_table()
|
||||
self.assertTrue(any(route["path"].endswith("/dashboard/summary") for route in table))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用