mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-01 23:52:45 +08:00
The lazy router index and pip --report plan.json are written as UTF-8, but were read with the locale default; build_router_index.py now writes UTF-8 explicitly so the read and write sides share one contract.
47 lines
1.6 KiB
Python
47 lines
1.6 KiB
Python
"""Regenerate the lazy API routing index after changing router declarations.
|
|
|
|
Run with the project's Python environment: python scripts/build_router_index.py
|
|
The index is an optimization only: a stale fingerprint falls back to eager load.
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
|
os.environ["DEEPTUTOR_CHAT_ENGINE"] = "python"
|
|
|
|
from starlette.routing import WebSocketRoute # noqa: E402
|
|
|
|
from deeptutor.api.main import _router_loader # noqa: E402
|
|
from deeptutor.api.router_loader import INDEX_PATH, source_fingerprint # noqa: E402
|
|
|
|
routers = {}
|
|
for reference, router in _router_loader.records.items():
|
|
# Router-level lifecycle callbacks must be installed eagerly by FastAPI.
|
|
if (
|
|
router.on_startup
|
|
or router.on_shutdown
|
|
or type(router.lifespan_context).__name__ != "_DefaultLifespan"
|
|
):
|
|
continue
|
|
patterns = []
|
|
for route in router.routes:
|
|
if not hasattr(route, "path") or not hasattr(route, "name"):
|
|
raise ValueError(f"Unsupported nested route: {reference}")
|
|
patterns.append(
|
|
{
|
|
"path": route.path,
|
|
"name": route.name,
|
|
"kind": "websocket" if isinstance(route, WebSocketRoute) else "http",
|
|
"methods": sorted(getattr(route, "methods", None) or []),
|
|
}
|
|
)
|
|
routers[reference] = patterns
|
|
INDEX_PATH.write_text(
|
|
json.dumps({"fingerprint": source_fingerprint(), "routers": routers}, indent=2) + "\n",
|
|
encoding="utf-8",
|
|
)
|
|
print(f"Indexed {len(routers)} routers; {sum(map(len, routers.values()))} routes")
|