edited app.py to add middleware
Browse files- server/app.py +29 -0
server/app.py
CHANGED
|
@@ -8,10 +8,13 @@ baseline, actions) are mounted on the same FastAPI app.
|
|
| 8 |
|
| 9 |
from __future__ import annotations
|
| 10 |
|
|
|
|
| 11 |
import sys
|
| 12 |
from pathlib import Path
|
| 13 |
from typing import Any, Dict, List, Optional
|
| 14 |
|
|
|
|
|
|
|
| 15 |
from pydantic import Field
|
| 16 |
|
| 17 |
# Ensure project root is importable
|
|
@@ -183,6 +186,32 @@ app = create_app(
|
|
| 183 |
)
|
| 184 |
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
# ββ Custom endpoints (tasks, grader, baseline, actions) ββββββββββββββββββ
|
| 187 |
|
| 188 |
from pydantic import BaseModel
|
|
|
|
| 8 |
|
| 9 |
from __future__ import annotations
|
| 10 |
|
| 11 |
+
import json
|
| 12 |
import sys
|
| 13 |
from pathlib import Path
|
| 14 |
from typing import Any, Dict, List, Optional
|
| 15 |
|
| 16 |
+
from fastapi import Request
|
| 17 |
+
from fastapi.responses import JSONResponse
|
| 18 |
from pydantic import Field
|
| 19 |
|
| 20 |
# Ensure project root is importable
|
|
|
|
| 186 |
)
|
| 187 |
|
| 188 |
|
| 189 |
+
# ββ Compatibility middleware: accept flat {action_type, parameters} on /step β
|
| 190 |
+
|
| 191 |
+
@app.middleware("http")
|
| 192 |
+
async def _normalize_step(request: Request, call_next):
|
| 193 |
+
"""Allow clients to POST flat {action_type, parameters} to /step.
|
| 194 |
+
|
| 195 |
+
The OpenEnv framework requires {"action": {...}}. This middleware
|
| 196 |
+
transparently wraps the flat format so existing clients keep working.
|
| 197 |
+
"""
|
| 198 |
+
if request.method == "POST" and request.url.path in ("/step", "/step/"):
|
| 199 |
+
raw = await request.body()
|
| 200 |
+
if raw:
|
| 201 |
+
try:
|
| 202 |
+
data = json.loads(raw)
|
| 203 |
+
if isinstance(data, dict) and "action_type" in data and "action" not in data:
|
| 204 |
+
wrapped = json.dumps({"action": data}).encode()
|
| 205 |
+
|
| 206 |
+
async def _receive():
|
| 207 |
+
return {"type": "http.request", "body": wrapped, "more_body": False}
|
| 208 |
+
|
| 209 |
+
request = Request(request.scope, _receive)
|
| 210 |
+
except (json.JSONDecodeError, Exception):
|
| 211 |
+
pass
|
| 212 |
+
return await call_next(request)
|
| 213 |
+
|
| 214 |
+
|
| 215 |
# ββ Custom endpoints (tasks, grader, baseline, actions) ββββββββββββββββββ
|
| 216 |
|
| 217 |
from pydantic import BaseModel
|