from __future__ import annotations from fastapi import FastAPI, HTTPException from fastapi.middleware.cors import CORSMiddleware from .data import TASK_REGISTRY from .env import ConfigDebuggerEnv from .models import ConfigAction, ResetRequest, StepResponse app = FastAPI(title="ConfigDebuggerEnv", version="1.0.0") app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) env = ConfigDebuggerEnv() @app.get("/") def root() -> dict[str, str]: return {"status": "ok", "env": "ConfigDebuggerEnv"} @app.get("/health") def health() -> dict[str, str]: return {"status": "healthy"} @app.get("/tasks") def tasks() -> dict[str, list[dict[str, str | int]]]: values: list[dict[str, str | int]] = [] for spec in TASK_REGISTRY.values(): values.append( { "id": spec.task_id, "name": spec.name, "description": spec.description, "difficulty": spec.difficulty, "max_steps": spec.max_steps, } ) return {"tasks": values} @app.post("/reset") def reset(payload: ResetRequest) -> dict[str, object]: task_id = payload.task_id or payload.task if task_id is None: raise HTTPException(status_code=400, detail="Provide task_id in request body") try: observation = env.reset(task_id) return { "observation": observation.model_dump(), "success": True, } except Exception as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc @app.post("/step", response_model=StepResponse) def step(action: ConfigAction) -> StepResponse: try: observation, reward, done, info = env.step(action) return StepResponse( observation=observation, reward=reward, done=done, info=info, ) except Exception as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc @app.get("/state") def state() -> dict[str, object]: try: current_state = env.state() return current_state.model_dump() except Exception as exc: raise HTTPException(status_code=400, detail=str(exc)) from exc