File size: 2,723 Bytes
50472c6
696e428
50472c6
696e428
50472c6
 
3cdd5dd
50472c6
696e428
f5e767a
4ee4295
3cdd5dd
50472c6
 
 
 
 
 
3cdd5dd
50472c6
696e428
50472c6
696e428
50472c6
 
 
 
 
696e428
50472c6
696e428
 
50472c6
696e428
 
50472c6
696e428
 
50472c6
3cdd5dd
 
696e428
50472c6
3cdd5dd
50472c6
696e428
 
50472c6
696e428
 
3cdd5dd
696e428
50472c6
f5e767a
4e75650
434ff3a
3cdd5dd
434ff3a
f5e767a
cd83211
434ff3a
696e428
4e75650
f5e767a
50472c6
3cdd5dd
 
50472c6
3cdd5dd
3d4a913
 
f5e767a
2a1a309
 
 
 
 
3d4a913
2a1a309
3d4a913
 
 
 
 
 
 
 
f5e767a
 
50472c6
3cdd5dd
 
50472c6
696e428
 
50472c6
696e428
 
3cdd5dd
696e428
 
f5e767a
 
 
 
 
696e428
 
50472c6
9c1f053
 
 
 
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
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
import threading

from env import CodeDebugEnv
from tasks import Action

app = FastAPI()

# ---------------- GLOBAL STATE ----------------
env = CodeDebugEnv(difficulty="easy", task="easy_001")

results_store = {
    "status": "running",
    "stdout": "",
    "stderr": "",
    "exit_code": None
}

# ---------------- BACKGROUND INFERENCE ----------------
def run_inference():
    global results_store
    try:
        result = env.run_inference()

        results_store["status"] = "completed"
        results_store["stdout"] = result.stdout
        results_store["stderr"] = result.stderr
        results_store["exit_code"] = result.returncode

    except Exception as e:
        results_store["status"] = "error"
        results_store["error"] = str(e)


# ---------------- ROOT ----------------
@app.get("/")
def root():
    return {
        "env": "code-debug-env",
        "version": "1.0.0",
        "description": "AI Code Debugging OpenEnv",
        "status": results_store.get("status", "running"),
        "endpoints": ["/", "/health", "/results", "/reset", "/step", "/state"]
    }


# ---------------- HEALTH ----------------
@app.get("/health")
def health():
    return {"status": "ok"}


# ---------------- RESET ----------------
@app.post("/reset")
async def reset(request: Request):
    global env

    # Reinitialize environment
    env = CodeDebugEnv(difficulty="easy", task="easy_001")

    obs = env.reset()

    # Return RAW JSON (IMPORTANT)
    return JSONResponse(content=obs.model_dump())


# ---------------- STEP ----------------
@app.post("/step")
async def step(action: dict):
    try:
        # Convert dict → object with attribute (required by env)
        class ActionObj:
            def __init__(self, fixed_code):
                self.fixed_code = fixed_code

        action_obj = ActionObj(action.get("fixed_code"))

        result = env.step(action_obj)

        return JSONResponse(content=result.model_dump())

    except Exception as e:
        return JSONResponse(
            content={"error": str(e)},
            status_code=500
        )


# ---------------- STATE ----------------
@app.get("/state")
def state():
    return JSONResponse(content=env.state().model_dump())


# ---------------- RESULTS ----------------
@app.get("/results")
def results():
    return results_store


# ---------------- MAIN ENTRYPOINT (CRITICAL) ----------------
def main():
    import uvicorn

    # Start background inference thread
    thread = threading.Thread(target=run_inference, daemon=True)
    thread.start()

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

if __name__ == "__main__":
    main()