openenv/coding_env
21
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 Coding Environment.9 10This module creates an HTTP server that exposes the PythonCodeActEnv11over HTTP and WebSocket endpoints, compatible with EnvClient.12 13Usage:14 # Development (with auto-reload):15 uvicorn envs.coding_env.server.app:app --reload --host 0.0.0.0 --port 800016 17 # Production:18 uvicorn envs.coding_env.server.app:app --host 0.0.0.0 --port 8000 --workers 419 20 # Or run directly:21 python -m envs.coding_env.server.app22"""23 24import os25from contextlib import suppress26 27from coding_env.models import CodeAction, CodeObservation28from coding_env.server.python_codeact_env import PythonCodeActEnv29from openenv.core.env_server import create_app30 31# Create the app with web interface and README integration32# Pass the class (factory) instead of an instance for WebSocket session support33app = create_app(PythonCodeActEnv, CodeAction, CodeObservation, env_name="coding_env")34 35 36def main():37 """Main entry point for running the server."""38 import uvicorn39 40 port = int(os.environ.get("SBX_SERVICE_PORT", "8000"))41 if proxy_dir := os.environ.get("SBX_PROXY_DIR"):42 socket_path = os.path.join(proxy_dir, f"{port}.sock")43 os.makedirs(proxy_dir, exist_ok=True)44 with suppress(FileNotFoundError):45 os.unlink(socket_path)46 uvicorn.run(app, uds=socket_path)47 else:48 uvicorn.run(app, host="0.0.0.0", port=port)49 50 51if __name__ == "__main__":52 main()53 