Add vector infrastructure maps and persistent location experience
This commit is contained in:
@@ -68,6 +68,33 @@ def build_ai_polygon_router(
|
||||
|
||||
router.include_router(build_route_preparation_router(routes, worlds, editor, runs, invoke, authenticate))
|
||||
|
||||
from k1link.web.spatial_experience_api import build_spatial_experience_router
|
||||
router.include_router(build_spatial_experience_router(data_dir, worlds, invoke, authenticate))
|
||||
|
||||
from k1link.simulation.ai_polygon.urban_maps import UrbanMapStore
|
||||
urban_maps = UrbanMapStore(data_dir)
|
||||
|
||||
@router.get("/worlds/{world_id}/urban-map")
|
||||
def urban_map(world_id: str):
|
||||
return invoke(urban_maps.get, invoke(worlds.get, world_id))
|
||||
|
||||
@router.post("/worker/worlds/{world_id}/urban-map")
|
||||
async def publish_urban_map(world_id: str, request: Request, authorization: str | None = Header(default=None)):
|
||||
authenticate(authorization)
|
||||
import json
|
||||
body = bytearray()
|
||||
async for chunk in request.stream():
|
||||
body.extend(chunk)
|
||||
if len(body) > 12 * 1024**2:
|
||||
raise HTTPException(413, "Infrastructure map exceeds size limit")
|
||||
try:
|
||||
document = json.loads(body)
|
||||
if not isinstance(document, dict):
|
||||
raise ValueError("Infrastructure map must be an object")
|
||||
return invoke(urban_maps.save, invoke(worlds.get, world_id), document)
|
||||
except (ValueError, UnicodeDecodeError, TypeError, KeyError) as exc:
|
||||
raise HTTPException(400, "Invalid infrastructure map") from exc
|
||||
|
||||
@router.get("/catalog")
|
||||
def catalog():
|
||||
runtime = runs.status()
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
"""Authenticated worker summaries, separate from live operator telemetry."""
|
||||
|
||||
import json
|
||||
import re
|
||||
|
||||
from fastapi import APIRouter, Header, HTTPException, Request
|
||||
|
||||
from k1link.simulation.ai_polygon.spatial_experience import read, save, validate
|
||||
from k1link.simulation.ai_polygon.worlds import write_json
|
||||
|
||||
|
||||
def build_spatial_experience_router(data_dir, worlds, invoke, authenticate):
|
||||
router = APIRouter()
|
||||
root = data_dir / "ai-polygon" / "spatial-experience"
|
||||
|
||||
def bound(world_id, document):
|
||||
world = worlds.get(world_id)
|
||||
validate(document)
|
||||
source = document["source"]
|
||||
if source["world_sha256"] != world["sha256"] or source["collider_sha256"] != world.get(
|
||||
"collider_sha256"
|
||||
):
|
||||
raise ValueError("Experience belongs to another scene revision")
|
||||
return root / world["sha256"]
|
||||
|
||||
def get(world_id):
|
||||
world = worlds.get(world_id)
|
||||
directory = root / world["sha256"]
|
||||
key = json.loads((directory / "latest.json").read_text())["robot_sha256"]
|
||||
if not re.fullmatch("[a-f0-9]{64}", key):
|
||||
raise ValueError("Invalid robot revision")
|
||||
document = read(directory / (key + ".json"))
|
||||
bound(world_id, document)
|
||||
return document
|
||||
|
||||
def store(world_id, document):
|
||||
directory = bound(world_id, document)
|
||||
key = document["source"]["robot_sha256"]
|
||||
save(directory / (key + ".json"), document)
|
||||
write_json(directory / "latest.json", {"robot_sha256": key})
|
||||
return {"visits": len(document["visits"]), "attempts": len(document["attempts"])}
|
||||
|
||||
@router.get("/worlds/{world_id}/spatial-experience")
|
||||
def experience(world_id: str):
|
||||
return invoke(get, world_id)
|
||||
|
||||
@router.post("/worker/worlds/{world_id}/spatial-experience")
|
||||
async def publish(
|
||||
world_id: str, request: Request, authorization: str | None = Header(default=None)
|
||||
):
|
||||
authenticate(authorization)
|
||||
body = bytearray()
|
||||
async for chunk in request.stream():
|
||||
body.extend(chunk)
|
||||
if len(body) > 8 * 1024**2:
|
||||
raise HTTPException(413, "Experience exceeds size limit")
|
||||
try:
|
||||
return invoke(store, world_id, json.loads(body))
|
||||
except (UnicodeDecodeError, json.JSONDecodeError, TypeError, KeyError) as exc:
|
||||
raise HTTPException(400, "Invalid location experience") from exc
|
||||
|
||||
return router
|
||||
Reference in New Issue
Block a user