"""FastAPI HTTP server exposing the OpenEnv interface for the ESC environment.
Endpoints
---------
GET / → health check + metadata
POST /reset → reset episode (optional task_id), returns initial Observation
POST /step → take one step with {"action": {"message": "..."}}
GET /state → return current EnvState
GET /tasks → list available tasks + difficulties
This server is stateless across replicas: the current environment snapshot is
stored in a signed, compressed cookie so any replica can serve the next
request as long as all nodes share the same session secret.
"""
from __future__ import annotations
import base64
import hashlib
import hmac
import json
import os
import zlib
import uvicorn
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import HTMLResponse
from src.env import ESCEnv
from src.models import ResetRequest, StepRequest
app = FastAPI(
title="Emotional Support Conversations (OpenEnv)",
version="0.1.0",
description=(
"An OpenEnv environment for open-ended emotional support "
"conversations. Reward shaping inspired by RLFF-ESC "
"(arXiv:2508.12935)."
),
)
SESSION_COOKIE = "esc_session_id"
SESSION_SECRET = os.getenv("ESC_SESSION_SECRET", "esc-openenv-dev-secret").encode("utf-8")
UI_HTML = """
Emotional Support Conversations
Emotional Support Conversations
Interactive browser playground for the deterministic OpenEnv benchmark.
The API stays unchanged; this page just calls /tasks,
/reset, /step, and /state.