-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
86 lines (63 loc) · 2.53 KB
/
api_server.py
File metadata and controls
86 lines (63 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
from __future__ import annotations
import sys
from pathlib import Path
import uvicorn
from fastapi import FastAPI, HTTPException
REPO_ROOT = Path(__file__).resolve().parent.parent
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
from lib.service_api.execution import default_workspace_root, get_run, submit_run
from lib.service_api.models import RunCreateRequest
from lib.service_api.registry import build_default_registry
from lib.service_api.storage import FileRunStore
def create_app(workspace_root: Path | None = None) -> FastAPI:
workspace_root = Path(workspace_root or default_workspace_root()).resolve()
registry = build_default_registry()
store = FileRunStore(workspace_root)
app = FastAPI(
title="Bio Studio Online Control Plane",
version="0.1.0",
)
@app.get("/healthz")
def healthz() -> dict[str, str]:
return {
"status": "ok",
"workspace_root": str(workspace_root),
}
@app.get("/v1/capabilities")
def list_capabilities():
return registry.list_capabilities()
@app.post("/v1/plans/preview")
def preview_plan(request: RunCreateRequest):
preview_request = request.model_copy(update={"execution_mode": "plan"})
from lib.service_api.planner import plan_request
return plan_request(preview_request, registry)
@app.post("/v1/runs")
def create_run(request: RunCreateRequest):
record, preview = submit_run(request, workspace_root=workspace_root, registry=registry)
return {"run": record, "plan_preview": preview}
@app.get("/v1/runs/{run_id}")
def read_run(run_id: str):
try:
return get_run(run_id, workspace_root=workspace_root)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
@app.get("/v1/runs/{run_id}/events")
def read_run_events(run_id: str):
try:
return store.read_events(run_id)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
@app.get("/v1/runs/{run_id}/artifacts")
def read_run_artifacts(run_id: str):
try:
return store.list_artifacts(run_id)
except FileNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
return app
app = create_app()
def main() -> None:
uvicorn.run("scripts.api_server:app", host="127.0.0.1", port=8000, reload=False)
if __name__ == "__main__":
main()