vibe-sketcherDocumentation
Source code for vibesketcher.server.reticular
"""REST routes for reticular assembly from drawn blocks on explicit nets (#202).
Building goes through ``POST /api/documents/{id}/periodic/build`` with a
``kind: reticular`` recipe (revision-checked, recorded as an edit and undone
like any build). These routes list the nets and preview an assembly without
changing the document.
"""
from __future__ import annotations
from typing import Any
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, ConfigDict, ValidationError
from vibesketcher.server.store import NotFound, Store
[docs]
class ReticularPreviewRequest(BaseModel):
"""A preview reads the current document; the answer names its revision."""
model_config = ConfigDict(extra="forbid")
recipe: dict[str, Any]
#: Include the built structure in the answer (it can be large).
structure: bool = False
[docs]
def router(store: Store) -> APIRouter:
routes = APIRouter(prefix="/api", tags=["reticular"])
@routes.get("/periodic/nets")
def periodic_nets() -> dict[str, Any]:
"""The nets a kind=reticular recipe can use, and the gated extensions."""
from vibesketcher.chem.reticular import net_catalog
return net_catalog()
@routes.post("/documents/{identifier}/periodic/reticular/preview")
def reticular_preview(identifier: str, request: ReticularPreviewRequest) -> dict[str, Any]:
"""Assembly record (counts, joins, transforms, diagnostics) or coded refusals."""
from vibesketcher.chem.reticular import ReticularRecipe, preview
try:
entry = store.get(identifier)
except NotFound:
raise HTTPException(404, f"no document {identifier!r}") from None
try:
recipe = ReticularRecipe.model_validate({"kind": "reticular", **request.recipe})
except ValidationError as error:
raise HTTPException(422, {"errors": [e["msg"] for e in error.errors()]}) from None
if recipe.identifier in {s.id for s in entry.document.periodic_structures}:
raise HTTPException(422, {"errors": [f"{recipe.identifier!r} already exists"]})
result = preview(recipe, entry.document)
if not request.structure:
result.pop("structure", None)
return {"revision": entry.document.revision, **result}
return routes