Spaces:
Sleeping
Sleeping
File size: 3,781 Bytes
2f8e591 | 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | """
FastAPI server exposing the OpenENV SQL Debugging environment.
Endpoints:
POST /reset?task=<name> → 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."})
|