""" FastAPI server exposing the OpenENV SQL Debugging environment. Endpoints: POST /reset?task= → SQLObservation (reset with optional task selection) POST /step → StepResult (submit SQLAction, get StepResult) GET /state → SQLObservation (current state, no side effects) GET /health → {"status":"ok"} (liveness probe) """ from fastapi import FastAPI, HTTPException, Query from fastapi.responses import JSONResponse from models import SQLAction, SQLObservation, StepResult from env import SQLEnvironment app = FastAPI( title="OpenENV — SQL Debugging Environment", description=( "An OpenEnv-compliant RL environment where an AI agent debugs and fixes " "broken SQL queries against an in-memory SQLite database." ), version="1.0.0", ) # Single global environment instance (one agent at a time) _env = SQLEnvironment() # --------------------------------------------------------------------------- # /health # --------------------------------------------------------------------------- @app.get("/health", summary="Liveness probe") def health(): """Returns 200 OK with status=ok if the server is running.""" return {"status": "ok"} # --------------------------------------------------------------------------- # /reset # --------------------------------------------------------------------------- VALID_TASKS = ["fix-syntax", "fix-logic", "fix-and-optimize"] @app.post("/reset", response_model=SQLObservation, summary="Reset the environment") def reset( task: str = Query( default="fix-syntax", description="Task name. One of: fix-syntax, fix-logic, fix-and-optimize", ) ): """ Reset the environment with the specified task and return the initial observation. If no task is provided, defaults to 'fix-syntax'. """ if task not in VALID_TASKS: raise HTTPException( status_code=400, detail=f"Invalid task '{task}'. Must be one of: {VALID_TASKS}", ) try: obs = _env.reset(task_name=task) return obs except ValueError as exc: raise HTTPException(status_code=400, detail=str(exc)) except RuntimeError as exc: raise HTTPException(status_code=500, detail=str(exc)) # --------------------------------------------------------------------------- # /step # --------------------------------------------------------------------------- @app.post("/step", response_model=StepResult, summary="Submit an action") def step(action: SQLAction): """ Submit a SQL query as the agent's action. Returns a StepResult with the new observation, reward, done flag, and info dict. """ try: result = _env.step(action) return result except RuntimeError as exc: raise HTTPException(status_code=400, detail=str(exc)) # --------------------------------------------------------------------------- # /state # --------------------------------------------------------------------------- @app.get("/state", response_model=SQLObservation, summary="Get current state") def state(): """ Return the current observation without advancing the episode. Useful for agents that need to re-read the environment state. """ try: obs = _env.state() return obs except RuntimeError as exc: raise HTTPException(status_code=400, detail=str(exc)) # --------------------------------------------------------------------------- # Root redirect → docs # --------------------------------------------------------------------------- @app.get("/", include_in_schema=False) def root(): return JSONResponse({"message": "OpenENV SQL Debugging Environment. See /docs for API reference."}) def main(): import uvicorn uvicorn.run("server.app:app", host="0.0.0.0", port=7860) if __name__ == "__main__": main()