Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# server/app.py – OpenEnv HTTP server
|
| 2 |
+
import sys
|
| 3 |
+
import os
|
| 4 |
+
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
| 5 |
+
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from environment import CodeReviewEnv
|
| 8 |
+
from models import AnyAction, Observation, Reward, State, action_adapter
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="Code Review Environment", version="1.0.0")
|
| 11 |
+
env = CodeReviewEnv()
|
| 12 |
+
|
| 13 |
+
# ----------------------------------------------------------------------
|
| 14 |
+
# Health & metadata endpoints
|
| 15 |
+
# ----------------------------------------------------------------------
|
| 16 |
+
@app.get("/")
|
| 17 |
+
def root():
|
| 18 |
+
print("[ROOT] Health check hit")
|
| 19 |
+
return {"status": "crazy good"}
|
| 20 |
+
|
| 21 |
+
@app.get("/health")
|
| 22 |
+
def health():
|
| 23 |
+
print("[HEALTH] Service is healthy")
|
| 24 |
+
return {"status": "healthy"}
|
| 25 |
+
|
| 26 |
+
@app.get("/metadata")
|
| 27 |
+
def metadata():
|
| 28 |
+
print("[METADATA] Requested")
|
| 29 |
+
return {
|
| 30 |
+
"name": "Code Review Professional Workflow",
|
| 31 |
+
"description": (
|
| 32 |
+
"Multi‑turn code review environment for professional‑level bug fixing. "
|
| 33 |
+
"The agent must inspect, test, lint, query documentation, and negotiate with "
|
| 34 |
+
"a simulated (persona‑driven) author to get a fix accepted. "
|
| 35 |
+
"Includes 25 bugs across 5 difficulty levels, AST‑based injection, "
|
| 36 |
+
"a reward‑shaping system (full/core profiles), and curriculum learning. "
|
| 37 |
+
"Designed for RL training (PPO, DPO, or any policy‑gradient method)."
|
| 38 |
+
)
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
@app.get("/schema")
|
| 42 |
+
def schema():
|
| 43 |
+
print("[SCHEMA] Requested")
|
| 44 |
+
return {
|
| 45 |
+
"action": AnyAction.model_json_schema(),
|
| 46 |
+
"observation": Observation.model_json_schema(),
|
| 47 |
+
"state": State.model_json_schema()
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
@app.post("/mcp")
|
| 51 |
+
def mcp():
|
| 52 |
+
print("[MCP] Ping received")
|
| 53 |
+
return {"jsonrpc": "2.0", "result": None}
|
| 54 |
+
|
| 55 |
+
# ----------------------------------------------------------------------
|
| 56 |
+
# Environment endpoints
|
| 57 |
+
# ----------------------------------------------------------------------
|
| 58 |
+
@app.post("/reset")
|
| 59 |
+
def reset(task: str = "easy"):
|
| 60 |
+
try:
|
| 61 |
+
print(f"[RESET] Starting new episode | task={task}")
|
| 62 |
+
|
| 63 |
+
env.set_task(task)
|
| 64 |
+
obs = env.reset()
|
| 65 |
+
|
| 66 |
+
print(f"[RESET DONE] step={env._step_count}")
|
| 67 |
+
|
| 68 |
+
return obs.__dict__
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f"[RESET ERROR] {e}")
|
| 71 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 72 |
+
|
| 73 |
+
@app.post("/step")
|
| 74 |
+
def step(action: dict):
|
| 75 |
+
try:
|
| 76 |
+
print(f"[STEP INPUT] {action}")
|
| 77 |
+
|
| 78 |
+
parsed_action = action_adapter.validate_python(action)
|
| 79 |
+
obs, reward, done, info = env.step(parsed_action)
|
| 80 |
+
|
| 81 |
+
print(f"[STEP OUTPUT] reward={reward.value:.4f} | done={done}")
|
| 82 |
+
|
| 83 |
+
return {
|
| 84 |
+
"observation": obs.__dict__,
|
| 85 |
+
"reward": reward.value,
|
| 86 |
+
"done": done,
|
| 87 |
+
"info": info
|
| 88 |
+
}
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"[STEP ERROR] {e}")
|
| 91 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 92 |
+
|
| 93 |
+
@app.get("/state")
|
| 94 |
+
def state():
|
| 95 |
+
print("[STATE] Requested")
|
| 96 |
+
return env._get_observation().__dict__
|
| 97 |
+
|
| 98 |
+
# ----------------------------------------------------------------------
|
| 99 |
+
# Main entry point (for local testing)
|
| 100 |
+
# ----------------------------------------------------------------------
|
| 101 |
+
if __name__ == "__main__":
|
| 102 |
+
import uvicorn
|
| 103 |
+
print("[SERVER START] Running on http://0.0.0.0:7860")
|
| 104 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|