File size: 2,293 Bytes
57fc36e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware

from .data import TASK_REGISTRY
from .env import ConfigDebuggerEnv
from .models import ConfigAction, ResetRequest, StepResponse


app = FastAPI(title="ConfigDebuggerEnv", version="1.0.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

env = ConfigDebuggerEnv()


@app.get("/")
def root() -> dict[str, str]:
    return {"status": "ok", "env": "ConfigDebuggerEnv"}


@app.get("/health")
def health() -> dict[str, str]:
    return {"status": "healthy"}


@app.get("/tasks")
def tasks() -> dict[str, list[dict[str, str | int]]]:
    values: list[dict[str, str | int]] = []
    for spec in TASK_REGISTRY.values():
        values.append(
            {
                "id": spec.task_id,
                "name": spec.name,
                "description": spec.description,
                "difficulty": spec.difficulty,
                "max_steps": spec.max_steps,
            }
        )
    return {"tasks": values}


@app.post("/reset")
def reset(payload: ResetRequest) -> dict[str, object]:
    task_id = payload.task_id or payload.task
    if task_id is None:
        raise HTTPException(status_code=400, detail="Provide task_id in request body")

    try:
        observation = env.reset(task_id)
        return {
            "observation": observation.model_dump(),
            "success": True,
        }
    except Exception as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@app.post("/step", response_model=StepResponse)
def step(action: ConfigAction) -> StepResponse:
    try:
        observation, reward, done, info = env.step(action)
        return StepResponse(
            observation=observation,
            reward=reward,
            done=done,
            info=info,
        )
    except Exception as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc


@app.get("/state")
def state() -> dict[str, object]:
    try:
        current_state = env.state()
        return current_state.model_dump()
    except Exception as exc:
        raise HTTPException(status_code=400, detail=str(exc)) from exc