File size: 7,238 Bytes
64ccc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e864384
 
 
64ccc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc4a08
64ccc23
4cc4a08
 
 
 
64ccc23
4cc4a08
64ccc23
 
 
 
 
 
4cc4a08
 
64ccc23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85465ba
 
 
 
64ccc23
 
 
 
 
 
 
 
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
server/app.py
=============
FastAPI server for the Interactive SQL Query Repair environment.

Endpoints
---------
  GET  /health          β€” liveness probe
  POST /reset           β€” start a new episode
  POST /step            β€” take one action
  GET  /state           β€” current episode state
  GET  /tasks           β€” list all available tasks
  GET  /grader          β€” score the current (or last) episode
"""

from __future__ import annotations

import sys
import os
from fastapi import Body
from typing import Optional


# Allow imports from project root regardless of working directory
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from typing import Annotated, Union

from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel, Field

from app.data_loader import get_all_tasks, get_tasks_by_difficulty, task_count
from app.env import SQLRepairEnv
from app.models import (
    AskClarificationAction,
    Difficulty,
    EnvState,
    GraderResult,
    HealthResponse,
    Observation,
    ResetRequest,
    RunPartialAction,
    StepResult,
    SubmitQueryAction,
    TaskListResponse,
    TaskSummary,
)

# ---------------------------------------------------------------------------
# App setup
# ---------------------------------------------------------------------------

app = FastAPI(
    title="Interactive SQL Query Repair Environment",
    description=(
        "An OpenEnv-compatible RL environment where an agent fixes broken SQL "
        "queries through multi-turn interaction: running partial queries, asking "
        "schema clarifications, and submitting final repairs."
    ),
    version="1.0.0",
)

# One global environment instance per server process.
# For concurrent multi-agent evaluation, run multiple server processes.
_env = SQLRepairEnv()


# ---------------------------------------------------------------------------
# Action discriminator model
# ---------------------------------------------------------------------------

class StepRequest(BaseModel):
    """
    Wrapper that discriminates between the three action types.
    The 'type' field routes to the correct action model.
    """
    type:     str
    query:    str | None = None
    question: str | None = None

    def to_action(self) -> Union[SubmitQueryAction, RunPartialAction, AskClarificationAction]:
        if self.type == "submit_query":
            if not self.query:
                raise ValueError("submit_query action requires 'query' field")
            return SubmitQueryAction(query=self.query)
        elif self.type == "run_partial":
            if not self.query:
                raise ValueError("run_partial action requires 'query' field")
            return RunPartialAction(query=self.query)
        elif self.type == "ask_clarification":
            if not self.question:
                raise ValueError("ask_clarification action requires 'question' field")
            return AskClarificationAction(question=self.question)
        else:
            raise ValueError(
                f"Unknown action type '{self.type}'. "
                f"Must be one of: submit_query, run_partial, ask_clarification"
            )


# ---------------------------------------------------------------------------
# Endpoints
# ---------------------------------------------------------------------------

@app.get("/health", response_model=HealthResponse, tags=["meta"])
def health() -> HealthResponse:
    """Liveness probe β€” always returns 200 if the server is running."""
    return HealthResponse(status="ok")


@app.get("/tasks", response_model=TaskListResponse, tags=["meta"])
def list_tasks() -> TaskListResponse:
    """
    List all available tasks across all difficulty levels.
    Use the task 'id' field in POST /reset to start a specific episode.
    """
    summaries = [
        TaskSummary(
            id=t.id,
            difficulty=t.difficulty,
            intent=t.intent,
            error_type=t.error_type,
            min_turns_expected=t.min_turns_expected,
        )
        for t in get_all_tasks()
    ]
    return TaskListResponse(tasks=summaries)

@app.post("/reset", response_model=Observation, tags=["env"])
def reset(req: Optional[ResetRequest] = Body(default=None)) -> Observation:
    try:
        task_id = req.task_id if req and req.task_id else "easy_001"
        difficulty = req.difficulty if req and req.difficulty else "easy"

        obs = _env.reset(task_id=task_id, difficulty=difficulty)
        return obs

    except KeyError as e:
        raise HTTPException(status_code=404, detail=str(e))
    except ValueError as e:
        raise HTTPException(status_code=422, detail=str(e))
    except RuntimeError as e:
        raise HTTPException(status_code=500, detail=str(e))
    
    
@app.post("/step", response_model=StepResult, tags=["env"])
def step(req: StepRequest) -> StepResult:
    """
    Take one action in the current episode.

    Body (one of three shapes)
    --------------------------
      Submit query (ends episode):
        { "type": "submit_query", "query": "SELECT ..." }

      Run partial query (test without finalising):
        { "type": "run_partial", "query": "SELECT ..." }

      Ask clarification:
        { "type": "ask_clarification", "question": "what is the join key?" }

    Returns StepResult with observation, reward, done flag, and diagnostics.
    Call GET /grader after done=true to get the final score.
    """
    try:
        action = req.to_action()
    except ValueError as e:
        raise HTTPException(status_code=422, detail=str(e))

    try:
        result = _env.step(action)
        return result
    except RuntimeError as e:
        raise HTTPException(status_code=400, detail=str(e))


@app.get("/state", response_model=EnvState, tags=["env"])
def state() -> EnvState:
    """Return the current episode state (safe to call at any time)."""
    return _env.state()


@app.get("/grader", response_model=GraderResult, tags=["eval"])
def grader() -> GraderResult:
    """
    Compute and return the grader score for the current (or most recent) episode.

    Score formula
    -------------
      final = 0.60 Γ— correctness
            + 0.25 Γ— turn_efficiency
            + 0.15 Γ— clarification_quality

    All sub-scores are in [0.0, 1.0].
    Can be called mid-episode to see a partial score.
    """
    return _env.grade()

# ---------------------------------------------------------------------------
# Error handlers
# ---------------------------------------------------------------------------

@app.exception_handler(Exception)
async def generic_exception_handler(request, exc):
    return JSONResponse(
        status_code=500,
        content={"detail": f"Internal server error: {str(exc)}"},
    )

import uvicorn

def main():
    uvicorn.run("server.app:app", host="0.0.0.0", port=7860)

# ---------------------------------------------------------------------------
# Entry point (for direct python execution)
# ---------------------------------------------------------------------------

if __name__ == "__main__":
    import uvicorn
    uvicorn.run("server.app:app", host="0.0.0.0", port=7860, reload=False)