StarTripper/ticket_ordering
0
1# Copyright (c) Meta Platforms, Inc. and affiliates.2# All rights reserved.3#4# This source code is licensed under the BSD-style license found in the5# LICENSE file in the root directory of this source tree.6 7"""8FastAPI application for the Ticket Ordering Environment.9 10This module creates an HTTP server that exposes the TicketOrderingEnvironment11over HTTP and WebSocket endpoints, compatible with EnvClient.12 13Endpoints:14 - POST /reset: Reset the environment15 - POST /step: Execute an action16 - GET /state: Get current environment state17 - GET /schema: Get action/observation schemas18 - WS /ws: WebSocket endpoint for persistent sessions19 20Usage:21 # Development (with auto-reload):22 uvicorn server.app:app --reload --host 0.0.0.0 --port 800023 24 # Production:25 uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 426 27 # Or run directly:28 python -m server.app29"""30 31try:32 from openenv.core.env_server.http_server import create_app33except Exception as e: # pragma: no cover34 raise ImportError(35 "openenv is required for the web interface. Install dependencies with '\n uv sync\n'"36 ) from e37 38try:39 from models import TicketOrderingAction, TicketOrderingObservation40 from server.ticket_ordering_environment import TicketOrderingEnvironment41except ModuleNotFoundError:42 from ..models import TicketOrderingAction, TicketOrderingObservation43 from .ticket_ordering_environment import TicketOrderingEnvironment44 45 46# Create the app with web interface and README integration47app = create_app(48 TicketOrderingEnvironment,49 TicketOrderingAction,50 TicketOrderingObservation,51 env_name="ticket_ordering",52 max_concurrent_envs=1, # increase this number to allow more concurrent WebSocket sessions53)54 55 56def main():57 """58 Entry point for openenv and direct execution.59 """60 import argparse61 import uvicorn62 63 # Move argument parsing inside main or handle defaults64 parser = argparse.ArgumentParser()65 parser.add_argument("--host", type=str, default="0.0.0.0")66 parser.add_argument("--port", type=int, default=8000)67 68 # parse_known_args prevents errors if openenv passes unexpected flags69 args, _ = parser.parse_known_args()70 71 # 'app' must be defined globally in this file (e.g., app = FastAPI())72 uvicorn.run("server.app:app", host=args.host, port=args.port, reload=False)73 74if __name__ == "__main__":75 main()76 