File size: 10,347 Bytes
d5338b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5764a24
d5338b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5764a24
 
 
 
 
 
 
 
d5338b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a5c4936
 
 
 
d5338b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""

OptiChain-Env: OpenEnv-compliant Supply Chain Optimization Environment

=======================================================================

Entry point for the FastAPI server.



Required by:

  - openenv validate spec  (checks server/app.py exists)

  - openenv.yaml           (app: server.app:app)

  - Dockerfile             (CMD uvicorn server.app:app)



Usage:

    uvicorn server.app:app --host 0.0.0.0 --port 7860

"""

import logging
from fastapi import FastAPI, HTTPException, Body
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
from typing import Optional

from env.core import SupplyChainEnv
from env.schemas import (
    SupplyChainAction,
    SupplyChainObservation,
    SupplyChainState,
    SupplyChainReward,
)
from inference import get_agent_action

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Single shared environment instance (single-session, per OpenEnv spec)
# ---------------------------------------------------------------------------
env = SupplyChainEnv()

# ---------------------------------------------------------------------------
# FastAPI app β€” we build our own rather than using create_app() because:
#   1. create_app() instantiates a SECOND env internally (out of sync)
#   2. create_app() registers /reset, /step, /state first β€” our custom
#      versions with task_id support would be shadowed (FastAPI first-match)
#   3. We still inherit from the OpenEnv base classes (Environment, Action,
#      Observation, State) β€” the spec validator checks endpoint behaviour,
#      not whether create_app() was called.
# ---------------------------------------------------------------------------
app = FastAPI(
    title="OpenEnv: OptiChain Supply Chain Optimizer",
    description="A supply chain inventory management environment for agentic RL.",
    version="1.0.0",
)

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

# ---------------------------------------------------------------------------
# Static dashboard
# ---------------------------------------------------------------------------
app.mount("/static", StaticFiles(directory="static"), name="static")


@app.get("/")
def serve_dashboard():
    return FileResponse("static/index.html")


# ===================================================================
# OpenEnv-required endpoints
# POST /reset  β€” task_id selects Easy / Medium / Hard scenario
# POST /step   β€” advance simulation one day
# GET  /state  β€” current episode metadata (no side effects)
# GET  /grader β€” normalized score 0.0–1.0
# GET  /health β€” liveness probe for Hugging Face Spaces
# ===================================================================

class ResetRequest(BaseModel):
    task_id: str = "task_01_easy"
    seed: Optional[int] = None


@app.post("/reset", response_model=SupplyChainObservation)
def reset_env(req: Optional[ResetRequest] = Body(default=None)):
    """Wipe state and load a specific task (easy / medium / hard).



    The body is optional β€” the OpenEnv validator POSTs with no body, so we

    fall back to the default task_01_easy when req is None.

    """
    if req is None:
        req = ResetRequest()
    try:
        return env.reset(task_id=req.task_id, seed=req.seed)
    except ValueError as exc:
        raise HTTPException(status_code=400, detail=str(exc))


class StepResponse(BaseModel):
    observation: SupplyChainObservation
    reward: SupplyChainReward
    done: bool
    info: dict


@app.post("/step", response_model=StepResponse)
def step_env(action: SupplyChainAction):
    """Execute the agent's purchase orders and advance the simulation by one day."""
    if not env.catalog:
        raise HTTPException(status_code=400, detail="Environment not initialised. Call POST /reset first.")
    obs = env.step(action)
    return StepResponse(
        observation=obs,
        reward=SupplyChainReward(
            step_reward=obs.reward,
            total_reward=env.total_reward,
            grader_score=env.get_grader_score(),
        ),
        done=obs.done,
        info={
            "accepted": env.last_accepted_qty,
            "rejected": env.last_rejected_qty,
        },
    )


@app.get("/state", response_model=SupplyChainState)
def get_state():
    """Return current episode metadata (step count, reward, grader score) without advancing time."""
    return env.state


class GraderResponse(BaseModel):
    score: float


@app.get("/grader", response_model=GraderResponse)
def get_grader():
    """Return the final normalised score (0.0-1.0) for the current episode."""
    return GraderResponse(score=env.get_grader_score())


@app.get("/health")
def health_check():
    """Liveness probe β€” must return 200 for Hugging Face Spaces automated pings."""
    return {"status": "healthy"}


@app.get("/metadata")
def get_metadata():
    """OpenEnv-required: returns environment name and description."""
    return {
        "name": "optichain-inventory-v1",
        "description": (
            "AI-driven supply chain inventory optimization. "
            "The agent acts as a Supply Chain Manager, placing daily purchase orders "
            "to maximise profit across a 30-day simulation."
        ),
        "version": "1.0.0",
        "tasks": [t["id"] for t in [
            {"id": "task_01_easy"},
            {"id": "task_02_medium"},
            {"id": "task_03_hard"},
        ]],
    }


@app.post("/mcp")
def mcp_endpoint(request: dict):
    """OpenEnv-required: minimal JSON-RPC 2.0 handler for MCP compliance."""
    method = request.get("method", "")
    req_id = request.get("id", 1)

    if method == "initialize":
        return {
            "jsonrpc": "2.0",
            "id": req_id,
            "result": {
                "protocolVersion": "2024-11-05",
                "capabilities": {"tools": {}},
                "serverInfo": {"name": "optichain-inventory-v1", "version": "1.0.0"},
            },
        }

    if method == "tools/list":
        return {
            "jsonrpc": "2.0",
            "id": req_id,
            "result": {
                "tools": [
                    {
                        "name": "reset",
                        "description": "Reset the environment to a specific task.",
                        "inputSchema": {
                            "type": "object",
                            "properties": {"task_id": {"type": "string"}},
                            "required": ["task_id"],
                        },
                    },
                    {
                        "name": "step",
                        "description": "Advance the simulation one day with purchase orders.",
                        "inputSchema": SupplyChainAction.model_json_schema(),
                    },
                ]
            },
        }

    # Unknown method β€” return JSON-RPC error
    return {
        "jsonrpc": "2.0",
        "id": req_id,
        "error": {"code": -32601, "message": f"Method not found: {method}"},
    }


# ===================================================================
# Extra endpoints (task listing, schema, UI demo bridge)
# ===================================================================

@app.get("/tasks")
def get_tasks():
    """List available tasks and the action JSON schema."""
    return {
        "tasks": [
            {"id": "task_01_easy",   "name": "Stable Demand",         "difficulty": "easy"},
            {"id": "task_02_medium", "name": "Holiday Demand Spike",   "difficulty": "medium"},
            {"id": "task_03_hard",   "name": "Supply Chain Crisis",    "difficulty": "hard"},
        ],
        "action_schema": SupplyChainAction.model_json_schema(),
    }


@app.get("/schema")
def get_schema():
    """Return observation/action/state JSON schemas for agent introspection."""
    return {
        "observation": SupplyChainObservation.model_json_schema(),
        "action": SupplyChainAction.model_json_schema(),
        "reward": SupplyChainReward.model_json_schema(),
        "state": SupplyChainState.model_json_schema(),
    }


@app.post("/demo/step_sim")
def demo_step_sim():
    """UI bridge: ask the AI agent for a decision, step the env, return both to the dashboard."""
    if not env.catalog:
        raise HTTPException(status_code=400, detail="Environment not initialised. Call POST /reset first.")

    current_obs = env._build_observation(reward=0.0, done=False)

    try:
        action, reasoning = get_agent_action(current_obs)
    except Exception as exc:
        logger.error("Agent error in demo/step_sim: %s", exc, exc_info=True)
        action = SupplyChainAction(orders=[])
        reasoning = f"Agent error: {exc}. Defaulting to zero orders."

    obs = env.step(action)
    return {
        "observation": obs.model_dump(),
        # reward is a plain float so the dashboard JS can use it directly
        # (pnl chart, appendLog, etc.). Full breakdown is in reward_detail.
        "reward": obs.reward,
        "reward_detail": SupplyChainReward(
            step_reward=obs.reward,
            total_reward=env.total_reward,
            grader_score=env.get_grader_score(),
        ).model_dump(),
        "done": obs.done,
        "action_taken": action.model_dump(),
        "accepted": env.last_accepted_qty,
        "rejected": env.last_rejected_qty,
        "reasoning": reasoning,
    }


# ---------------------------------------------------------------------------
# Dev entry point
# ---------------------------------------------------------------------------
def main():
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)


if __name__ == "__main__":
    main()