"""Controller API routes for Bridge V1.2.""" from __future__ import annotations from fastapi import APIRouter, HTTPException from controller.controller_loop import RUNS_DIR, run_controller from controller.controller_state import load as load_state from models.schemas import ControllerResponse, ControllerRunRequest router = APIRouter(prefix="/controller", tags=["controller"]) @router.post("/runs", response_model=ControllerResponse, status_code=201) def start_controller_run(body: ControllerRunRequest): """Execute a full controller loop: goal → decision → agent → result → stop. Blocks until the controller loop completes (max 3 iterations). Returns the final persisted controller state. """ try: state = run_controller(goal=body.goal, run_id=body.run_id) except ValueError as exc: raise HTTPException(status_code=409, detail=str(exc)) from exc return ControllerResponse(**state.to_dict()) @router.get("/runs/{ctrl_id}", response_model=ControllerResponse) def get_controller_run(ctrl_id: str): """Retrieve persisted controller state.""" state = load_state(RUNS_DIR, ctrl_id) if state is None: raise HTTPException(status_code=404, detail=f"controller run '{ctrl_id}' not found") return ControllerResponse(**state.to_dict())