Spaces:
Sleeping
Sleeping
File size: 54,769 Bytes
09f6e69 |
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 |
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
"""
HTTP server wrapper for Environment instances.
This module provides utilities to wrap any Environment subclass and expose it
over HTTP and WebSocket endpoints that EnvClient can consume.
"""
from __future__ import annotations
import asyncio
import inspect
import json
import os
import time
import uuid
from concurrent.futures import ThreadPoolExecutor
from typing import Any, Callable, Dict, Optional, Type
from fastapi import (
Body,
FastAPI,
HTTPException,
Request,
WebSocket,
WebSocketDisconnect,
status,
)
from pydantic import ValidationError
from .interfaces import Environment
from .route_config import (
GetEndpointConfig,
register_get_endpoints,
)
from .serialization import deserialize_action, serialize_observation
from .types import (
Action,
Observation,
ResetRequest,
ResetResponse,
State,
StepRequest,
StepResponse,
EnvironmentMetadata,
SchemaResponse,
HealthResponse,
HealthStatus,
ServerMode,
WSErrorCode,
WSResetMessage,
WSStepMessage,
WSStateMessage,
WSCloseMessage,
WSObservationResponse,
WSStateResponse,
WSErrorResponse,
ConcurrencyConfig,
ServerCapacityStatus,
SessionInfo,
)
from .mcp_types import (
JsonRpcErrorCode,
JsonRpcRequest,
JsonRpcResponse,
McpMethod,
WSMCPMessage,
WSMCPResponse,
)
def _make_json_serializable(obj: Any) -> Any:
"""
Convert an object to a JSON-serializable form.
Handles Pydantic models, dataclasses, and other common types.
Args:
obj: The object to convert
Returns:
A JSON-serializable representation of the object
"""
if obj is None:
return None
if isinstance(obj, (str, int, float, bool)):
return obj
if isinstance(obj, (list, tuple)):
return [_make_json_serializable(item) for item in obj]
if isinstance(obj, dict):
return {k: _make_json_serializable(v) for k, v in obj.items()}
if hasattr(obj, "model_dump"):
# Pydantic model
return obj.model_dump()
if hasattr(obj, "__dict__"):
# Object with __dict__
return {k: _make_json_serializable(v) for k, v in obj.__dict__.items()}
# Fallback to string representation
return str(obj)
from .exceptions import (
ConcurrencyConfigurationError,
SessionCapacityError,
EnvironmentFactoryError,
)
class HTTPEnvServer:
"""
HTTP server wrapper for Environment instances.
This class wraps an Environment and exposes its reset(), step(), and state
methods as HTTP and WebSocket endpoints compatible with EnvClient.
The server expects:
- Action deserialization: Converts JSON dict to Action subclass
- Observation serialization: Converts Observation subclass to JSON dict
Example:
>>> from core.env_server import HTTPEnvServer
>>> from envs.coding_env.server import CodeExecutionEnvironment
>>> from envs.coding_env.models import CodeAction, CodeObservation
>>>
>>> # Pass environment class (factory pattern)
>>> server = HTTPEnvServer(
... env=CodeExecutionEnvironment,
... action_cls=CodeAction,
... observation_cls=CodeObservation,
... max_concurrent_envs=4,
... )
>>>
>>> # Register routes with FastAPI
>>> from fastapi import FastAPI
>>> app = FastAPI()
>>> server.register_routes(app)
"""
def __init__(
self,
env: Callable[[], Environment],
action_cls: Type[Action],
observation_cls: Type[Observation],
max_concurrent_envs: Optional[int] = None,
concurrency_config: Optional[ConcurrencyConfig] = None,
):
"""
Initialize HTTP server wrapper.
Args:
env: Environment factory (callable) that creates new instances.
Will be called to create a new environment for each WebSocket session.
action_cls: The Action subclass this environment expects
observation_cls: The Observation subclass this environment returns
max_concurrent_envs: Maximum number of concurrent WebSocket sessions.
Mutually exclusive with concurrency_config.
concurrency_config: Optional ConcurrencyConfig for advanced concurrency settings.
Mutually exclusive with max_concurrent_envs.
Raises:
ValueError: If both max_concurrent_envs and concurrency_config are provided.
ConcurrencyConfigurationError: If max_concurrent_envs > 1 for an
environment that is not marked as SUPPORTS_CONCURRENT_SESSIONS.
"""
# Validate that env is callable
if not callable(env):
raise TypeError(
f"env must be a callable (class or factory function), got {type(env)}. "
f"Pass the environment class (e.g., MyEnvironment) not an instance (e.g., MyEnvironment())."
)
self._env_factory: Callable[[], Environment] = env
# Handle concurrency configuration
if max_concurrent_envs is not None and concurrency_config is not None:
raise ValueError(
"Cannot specify both 'max_concurrent_envs' and 'concurrency_config'. "
"Please use only one method to configure concurrency."
)
if concurrency_config is not None:
self._concurrency_config = concurrency_config
elif max_concurrent_envs is not None:
self._concurrency_config = ConcurrencyConfig(
max_concurrent_envs=max_concurrent_envs,
session_timeout=None,
)
else:
# Default configuration
self._concurrency_config = ConcurrencyConfig(
max_concurrent_envs=1,
session_timeout=None,
)
self._max_concurrent_envs = self._concurrency_config.max_concurrent_envs
# Validate concurrency configuration
self._validate_concurrency_safety()
self.action_cls = action_cls
self.observation_cls = observation_cls
# Session management for WebSocket connections
self._sessions: Dict[str, Environment] = {}
self._session_executors: Dict[str, ThreadPoolExecutor] = {}
self._session_info: Dict[str, SessionInfo] = {}
self._session_lock = asyncio.Lock()
# Create thread pool for running sync code in async context
# This is needed for environments using sync libraries (e.g., Playwright)
self._executor = ThreadPoolExecutor(max_workers=32)
def _validate_concurrency_safety(self) -> None:
"""
Validate that the environment supports the configured concurrency level.
Raises:
ConcurrencyConfigurationError: If max_concurrent_envs > 1 for an
environment that is not marked as SUPPORTS_CONCURRENT_SESSIONS.
"""
if self._max_concurrent_envs <= 1:
return
if inspect.isclass(self._env_factory):
env_cls = self._env_factory
else:
_temp_env = self._env_factory()
env_cls = type(_temp_env)
_temp_env.close()
del _temp_env
if not getattr(env_cls, "SUPPORTS_CONCURRENT_SESSIONS", False):
raise ConcurrencyConfigurationError(
environment_name=env_cls.__name__,
max_concurrent_envs=self._max_concurrent_envs,
)
def get_capacity_status(self) -> ServerCapacityStatus:
"""
Get the current capacity status of the server.
Returns:
ServerCapacityStatus with current session counts and availability.
"""
return ServerCapacityStatus.from_counts(
active=len(self._sessions),
max_sessions=self._max_concurrent_envs,
)
async def _run_sync_in_thread_pool(
self, func: Callable[..., Observation], *args, **kwargs
) -> Observation:
"""Run a synchronous function in the thread pool executor."""
loop = asyncio.get_event_loop()
return await loop.run_in_executor(self._executor, lambda: func(*args, **kwargs))
def _get_valid_kwargs(
self,
sig: inspect.Signature,
kwargs: Dict[str, Any],
skip_params: Optional[set[str]] = None,
) -> Dict[str, Any]:
"""Filter kwargs to only include parameters accepted by the function signature."""
if skip_params is None:
skip_params = set()
valid_kwargs = {}
has_kwargs = any(
p.kind == inspect.Parameter.VAR_KEYWORD for p in sig.parameters.values()
)
for k, v in kwargs.items():
if k in sig.parameters or has_kwargs:
if k not in skip_params:
valid_kwargs[k] = v
return valid_kwargs
async def _create_session(self) -> tuple[str, Environment]:
"""
Create a new WebSocket session with its own environment instance.
Returns:
Tuple of (session_id, environment)
Raises:
SessionCapacityError: If max concurrent sessions reached
EnvironmentFactoryError: If the factory fails to create an environment
"""
async with self._session_lock:
if len(self._sessions) >= self._max_concurrent_envs:
raise SessionCapacityError(
active_sessions=len(self._sessions),
max_sessions=self._max_concurrent_envs,
)
session_id = str(uuid.uuid4())
current_time = time.time()
# Create executor and reserve slot so capacity is not exceeded while
# we create the env outside the lock (avoids blocking other sessions)
executor = ThreadPoolExecutor(max_workers=1)
self._session_executors[session_id] = executor
self._sessions[session_id] = None # placeholder until env is ready
try:
# Create environment in the executor thread (outside lock)
loop = asyncio.get_event_loop()
env = await loop.run_in_executor(executor, self._env_factory)
except Exception as e:
async with self._session_lock:
executor.shutdown(wait=False)
self._session_executors.pop(session_id, None)
self._sessions.pop(session_id, None)
factory_name = getattr(
self._env_factory, "__name__", str(self._env_factory)
)
raise EnvironmentFactoryError(factory_name) from e
async with self._session_lock:
self._sessions[session_id] = env
self._session_info[session_id] = SessionInfo(
session_id=session_id,
created_at=current_time,
last_activity_at=current_time,
step_count=0,
environment_type=type(env).__name__,
)
return session_id, env
async def _destroy_session(self, session_id: str) -> None:
"""
Destroy a WebSocket session and cleanup resources.
Args:
session_id: The session ID to destroy
"""
async with self._session_lock:
env = self._sessions.pop(session_id, None)
executor = self._session_executors.pop(session_id, None)
self._session_info.pop(session_id, None)
# Run close() in the same executor where the env was created
# This is required for thread-sensitive libraries like Playwright/greenlet
if env is not None:
if executor is not None:
try:
loop = asyncio.get_event_loop()
await loop.run_in_executor(executor, env.close)
except Exception:
# If executor close fails, try direct close as fallback
try:
env.close()
except Exception:
pass # Best effort cleanup
else:
try:
env.close()
except Exception:
pass # Best effort cleanup
# Shutdown executor after close is done
if executor is not None:
executor.shutdown(wait=False)
def _update_session_activity(
self, session_id: str, increment_step: bool = False
) -> None:
"""
Update session activity timestamp and optionally increment step count.
Args:
session_id: The session ID to update
increment_step: If True, increment the step count
"""
if session_id in self._session_info:
self._session_info[session_id].last_activity_at = time.time()
if increment_step:
self._session_info[session_id].step_count += 1
def get_session_info(self, session_id: str) -> Optional[SessionInfo]:
"""
Get information about a specific session.
Args:
session_id: The session ID to query
Returns:
SessionInfo if the session exists, None otherwise
"""
return self._session_info.get(session_id)
async def _run_in_session_executor(
self, session_id: str, func: Callable[..., Observation], *args, **kwargs
) -> Observation:
"""Run a synchronous function in the session's thread pool executor."""
executor = self._session_executors.get(session_id, self._executor)
loop = asyncio.get_event_loop()
return await loop.run_in_executor(executor, lambda: func(*args, **kwargs))
@property
def active_sessions(self) -> int:
"""Return the number of active WebSocket sessions."""
return len(self._sessions)
@property
def max_concurrent_envs(self) -> int:
"""Return the maximum number of concurrent environments."""
return self._max_concurrent_envs
@property
def is_concurrency_safe(self) -> bool:
"""Return whether the environment is marked as concurrency safe."""
import inspect
if inspect.isclass(self._env_factory):
return getattr(self._env_factory, "SUPPORTS_CONCURRENT_SESSIONS", False)
else:
_temp_env = self._env_factory()
result = getattr(_temp_env, "SUPPORTS_CONCURRENT_SESSIONS", False)
_temp_env.close()
del _temp_env
return result
@property
def concurrency_config(self) -> ConcurrencyConfig:
"""Return the concurrency configuration."""
return self._concurrency_config
def register_routes(
self, app: FastAPI, mode: ServerMode | str = ServerMode.SIMULATION
) -> None:
"""
Register HTTP routes on a FastAPI application.
Args:
app: FastAPI application instance
mode: Server mode - either SIMULATION or PRODUCTION (or string equivalents).
In production mode, simulation control endpoints (/reset, /step, /state)
are NOT registered. Only safe endpoints (/health, /schema, /metadata, /ws)
are available. Defaults to SIMULATION for backwards compatibility.
Raises:
ValueError: If mode is not a valid ServerMode or string equivalent.
"""
# Convert string to ServerMode enum for backwards compatibility
if isinstance(mode, str):
try:
mode = ServerMode(mode.lower())
except ValueError:
valid_modes = [m.value for m in ServerMode]
raise ValueError(
f"Invalid mode: '{mode}'. Must be one of: {valid_modes}"
)
# Helper function to handle reset endpoint
async def reset_handler(
request: ResetRequest = Body(default_factory=ResetRequest),
) -> ResetResponse:
"""Reset endpoint - returns initial observation."""
_env = self._env_factory()
try:
kwargs = request.model_dump(exclude_unset=True)
is_async = _env.reset_async.__func__ is not Environment.reset_async
if is_async:
sig = inspect.signature(_env.reset_async)
else:
sig = inspect.signature(_env.reset)
valid_kwargs = self._get_valid_kwargs(sig, kwargs)
if is_async:
observation = await _env.reset_async(**valid_kwargs)
else:
observation = await self._run_sync_in_thread_pool(
_env.reset, **valid_kwargs
)
return ResetResponse(**serialize_observation(observation))
finally:
_env.close()
# Helper function to handle step endpoint
async def step_handler(request: StepRequest) -> StepResponse:
"""Step endpoint - executes action and returns observation."""
action_data = request.action
try:
action = deserialize_action(action_data, self.action_cls)
except ValidationError as e:
raise HTTPException(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT, detail=e.errors()
)
_env = self._env_factory()
try:
kwargs = request.model_dump(exclude_unset=True, exclude={"action"})
is_async = _env.step_async.__func__ is not Environment.step_async
if is_async:
sig = inspect.signature(_env.step_async)
else:
sig = inspect.signature(_env.step)
valid_kwargs = self._get_valid_kwargs(
sig, kwargs, skip_params={"action"}
)
if is_async:
observation = await _env.step_async(action, **valid_kwargs)
else:
observation = await self._run_sync_in_thread_pool(
_env.step, action, **valid_kwargs
)
return StepResponse(**serialize_observation(observation))
finally:
_env.close()
# Helper function to handle MCP endpoint
async def mcp_handler(
request: JsonRpcRequest, session_env: Optional[Environment] = None
) -> JsonRpcResponse:
"""
Handle MCP JSON-RPC requests.
Supports tools/list and tools/call methods in JSON-RPC 2.0 format.
"""
method = request.method
request_id = request.id
# Use provided session environment or create temporary one
if session_env is not None:
_env = session_env
should_close = False
else:
_env = self._env_factory()
should_close = True
try:
if method == McpMethod.TOOLS_LIST:
# Check if environment is MCP-enabled
if not hasattr(_env, "mcp_client"):
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
"Environment does not support MCP",
request_id=request_id,
)
# Use async context manager for MCP client
async with _env.mcp_client:
tools = await _env.mcp_client.list_tools()
return JsonRpcResponse.success(
result={
"tools": [
t.model_dump() if hasattr(t, "model_dump") else dict(t)
for t in tools
]
},
request_id=request_id,
)
elif method == McpMethod.TOOLS_CALL:
params = request.params
tool_name = params.get("name")
arguments = params.get("arguments", {})
if not hasattr(_env, "mcp_client"):
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
"Environment does not support MCP",
request_id=request_id,
)
if not tool_name:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_REQUEST,
"Missing 'name' in params",
request_id=request_id,
)
# Use async context manager for MCP client
async with _env.mcp_client:
result = await _env.mcp_client.call_tool(
name=tool_name, arguments=arguments
)
# Ensure result is JSON serializable
serializable_result = _make_json_serializable(result)
return JsonRpcResponse.success(
result=serializable_result,
request_id=request_id,
)
else:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.METHOD_NOT_FOUND,
f"Method not found: {method}",
request_id=request_id,
)
except Exception as e:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
str(e),
request_id=request_id,
)
finally:
if should_close:
_env.close()
# Register MCP WebSocket endpoint (available in both production and simulation modes)
@app.websocket("/mcp")
async def mcp_websocket_endpoint(websocket: WebSocket):
"""
WebSocket endpoint for MCP JSON-RPC requests.
Each WebSocket connection gets its own environment instance for MCP operations.
Message Protocol:
- Client sends: JSON-RPC 2.0 request (tools/list, tools/call)
- Server responds: JSON-RPC 2.0 response (result or error)
"""
await websocket.accept()
session_id = None
session_env = None
try:
# Create session with dedicated environment
session_id, session_env = await self._create_session()
while True:
# Receive message from client
raw_message = await websocket.receive_text()
try:
jsonrpc_dict = json.loads(raw_message)
jsonrpc_request = JsonRpcRequest(**jsonrpc_dict)
except json.JSONDecodeError as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.PARSE_ERROR,
f"Parse error: {e}",
)
await websocket.send_text(error_resp.model_dump_json())
continue
except ValidationError as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_REQUEST,
f"Invalid request: {e}",
)
await websocket.send_text(error_resp.model_dump_json())
continue
try:
# Call mcp_handler with session environment
response = await mcp_handler(
jsonrpc_request, session_env=session_env
)
await websocket.send_text(response.model_dump_json())
except Exception as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
str(e),
request_id=jsonrpc_request.id,
)
await websocket.send_text(error_resp.model_dump_json())
except WebSocketDisconnect:
pass
except SessionCapacityError as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.SERVER_ERROR,
str(e),
data={
"active_sessions": e.active_sessions,
"max_sessions": e.max_sessions,
},
)
await websocket.send_text(error_resp.model_dump_json())
except EnvironmentFactoryError as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.SERVER_ERROR,
str(e),
data={"factory_name": e.factory_name},
)
await websocket.send_text(error_resp.model_dump_json())
except Exception as e:
error_resp = JsonRpcResponse.error_response(
JsonRpcErrorCode.SERVER_ERROR,
str(e),
)
await websocket.send_text(error_resp.model_dump_json())
finally:
if session_id:
await self._destroy_session(session_id)
try:
await websocket.close()
except RuntimeError:
pass
# Register simulation control routes only in simulation mode
if mode == ServerMode.SIMULATION:
@app.post(
"/reset",
response_model=ResetResponse,
tags=["Environment Control"],
summary="Reset the environment",
description="""
Reset the environment to its initial state and return the first observation.
You can optionally provide a seed for reproducibility and an episode_id for tracking.
""",
responses={
200: {
"description": "Environment reset successfully",
"content": {
"application/json": {
"example": {
"observation": {"status": "ready", "data": {}},
"reward": None,
"done": False,
}
}
},
}
},
)
async def reset(
request: ResetRequest = Body(default_factory=ResetRequest),
) -> ResetResponse:
return await reset_handler(request)
@app.post(
"/step",
response_model=StepResponse,
tags=["Environment Control"],
summary="Execute an action in the environment",
description="""
Execute an action in the environment and receive the resulting observation.
The action must conform to the environment's action schema, which can be
retrieved from the `/schema` endpoint. If the action is invalid,
the endpoint will return HTTP 422 with detailed validation errors.
The response includes:
- **observation**: The environment's response to the action
- **reward**: Optional reward signal (float or None)
- **done**: Boolean indicating if the episode has terminated
""",
responses={
200: {
"description": "Action executed successfully",
"content": {
"application/json": {
"example": {
"observation": {"status": "success", "data": {}},
"reward": 1.0,
"done": False,
}
}
},
},
422: {
"description": "Validation error - invalid action format or values",
"content": {
"application/json": {
"example": {
"detail": [
{
"type": "string_too_short",
"loc": ["body", "action", "message"],
"msg": "String should have at least 1 character",
"input": "",
}
]
}
}
},
},
500: {
"description": "Internal server error during action execution"
},
},
)
async def step(request: StepRequest) -> StepResponse:
return await step_handler(request)
def get_state_handler() -> State:
_env = self._env_factory()
try:
return _env.state
finally:
_env.close()
def get_metadata_handler() -> EnvironmentMetadata:
_env = self._env_factory()
try:
return _env.get_metadata()
finally:
_env.close()
# Build list of GET endpoints based on mode
get_endpoints = [
GetEndpointConfig(
path="/metadata",
handler=get_metadata_handler,
response_model=EnvironmentMetadata,
tag="Environment Info",
summary="Get environment metadata",
description="""
Get metadata about this environment.
Returns information about the environment including name, description,
version, author, and documentation links.
""",
),
GetEndpointConfig(
path="/health",
handler=lambda: HealthResponse(status=HealthStatus.HEALTHY),
response_model=HealthResponse,
tag="Health",
summary="Health check",
description="Check if the environment server is running and healthy.",
),
]
# Only register /state endpoint in simulation mode
if mode == ServerMode.SIMULATION:
get_endpoints.insert(
0,
GetEndpointConfig(
path="/state",
handler=get_state_handler,
response_model=State,
tag="State Management",
summary="Get current environment state",
description="""
Retrieve the current internal state of the environment.
The structure of the state object is defined by the environment's State model.
""",
),
)
register_get_endpoints(app, get_endpoints)
# Register combined schema endpoint
@app.get(
"/schema",
response_model=SchemaResponse,
tags=["Schema"],
summary="Get all JSON schemas",
description="""
Get JSON schemas for actions, observations, and state in a single response.
Returns a combined schema object containing:
- **action**: JSON schema for actions accepted by this environment
- **observation**: JSON schema for observations returned by this environment
- **state**: JSON schema for environment state objects
This is more efficient than calling individual schema endpoints and provides
all schema information needed to interact with the environment.
""",
responses={
200: {
"description": "Combined schemas retrieved successfully",
"content": {
"application/json": {
"example": {
"action": {
"type": "object",
"properties": {"message": {"type": "string"}},
},
"observation": {
"type": "object",
"properties": {"response": {"type": "string"}},
},
"state": {
"type": "object",
"properties": {"step_count": {"type": "integer"}},
},
}
}
},
}
},
)
async def get_schemas() -> SchemaResponse:
"""Return all schemas in one response."""
return SchemaResponse(
action=self.action_cls.model_json_schema(),
observation=self.observation_cls.model_json_schema(),
state=State.model_json_schema(),
)
# Register MCP endpoint for production mode (direct MCP access)
@app.post("/mcp")
async def mcp_endpoint(request_raw: Request) -> Dict[str, Any]:
"""
MCP JSON-RPC endpoint for production mode.
Bypasses step() overhead and provides direct access to MCP tools.
Supports tools/list and tools/call methods.
"""
# Parse JSON manually to handle parse errors gracefully
try:
body = await request_raw.body()
request_dict = json.loads(body)
request = JsonRpcRequest(**request_dict)
except json.JSONDecodeError:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.PARSE_ERROR
).model_dump()
except ValidationError as e:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_REQUEST,
f"Invalid request: {e}",
).model_dump()
except Exception:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.PARSE_ERROR
).model_dump()
method = request.method
params = request.params
request_id = request.id
# Create a temporary environment for MCP access
_env = self._env_factory()
try:
# Check if environment supports MCP
if not hasattr(_env, "mcp_client") and not hasattr(_env, "mcp_server"):
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
"Environment does not support MCP",
request_id=request_id,
).model_dump()
if method == McpMethod.TOOLS_LIST:
# List tools from MCP server
if hasattr(_env, "mcp_client") and _env.mcp_client:
async with _env.mcp_client:
tools = await _env.mcp_client.list_tools()
return JsonRpcResponse.success(
result={
"tools": [
t.model_dump()
if hasattr(t, "model_dump")
else dict(t)
for t in tools
]
},
request_id=request_id,
).model_dump()
elif hasattr(_env, "mcp_server") and _env.mcp_server:
# Use server directly
tools = []
if hasattr(_env.mcp_server, "_tool_manager"):
tool_manager = _env.mcp_server._tool_manager
if hasattr(tool_manager, "_tools"):
for tool_name, tool in tool_manager._tools.items():
tool_dict = {
"name": tool.name,
"description": tool.description or "",
"inputSchema": tool.parameters or {},
}
tools.append(tool_dict)
return JsonRpcResponse.success(
result={"tools": tools},
request_id=request_id,
).model_dump()
else:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
"MCP server not available",
request_id=request_id,
).model_dump()
elif method == McpMethod.TOOLS_CALL:
tool_name = params.get("name")
arguments = params.get("arguments", {})
if not tool_name:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_PARAMS,
"Invalid params - 'name' is required",
request_id=request_id,
).model_dump()
# Call tool via MCP
if hasattr(_env, "mcp_client") and _env.mcp_client:
async with _env.mcp_client:
result = await _env.mcp_client.call_tool(
name=tool_name, arguments=arguments
)
elif hasattr(_env, "mcp_server") and hasattr(
_env.mcp_server, "_tool_manager"
):
# Call tool directly on FastMCP server
tool_manager = _env.mcp_server._tool_manager
if tool_name in tool_manager._tools:
tool = tool_manager._tools[tool_name]
result = tool.fn(**arguments)
else:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_PARAMS,
f"Tool not found: {tool_name}",
request_id=request_id,
).model_dump()
else:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
"MCP server not available",
request_id=request_id,
).model_dump()
# Make result JSON serializable
serializable_result = _make_json_serializable(result)
return JsonRpcResponse.success(
result=serializable_result,
request_id=request_id,
).model_dump()
else:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.METHOD_NOT_FOUND,
f"Method not found: {method}",
request_id=request_id,
).model_dump()
except Exception as e:
return JsonRpcResponse.error_response(
JsonRpcErrorCode.INTERNAL_ERROR,
str(e),
request_id=request_id,
).model_dump()
finally:
_env.close()
# Register WebSocket endpoint for persistent sessions
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
"""
WebSocket endpoint for persistent environment sessions.
Each WebSocket connection gets its own environment instance.
Message Protocol:
- Client sends: WSResetMessage | WSStepMessage | WSStateMessage | WSCloseMessage
- Server responds: WSObservationResponse | WSStateResponse | WSErrorResponse
"""
await websocket.accept()
session_id = None
session_env = None
try:
# Create session with dedicated environment
session_id, session_env = await self._create_session()
while True:
# Receive message from client
raw_message = await websocket.receive_text()
try:
message_dict = json.loads(raw_message)
except json.JSONDecodeError as e:
error_resp = WSErrorResponse(
data={
"message": f"Invalid JSON: {e}",
"code": WSErrorCode.INVALID_JSON,
}
)
await websocket.send_text(error_resp.model_dump_json())
continue
msg_type = message_dict.get("type", "")
try:
match msg_type:
case "reset":
msg = WSResetMessage(**message_dict)
is_async = (
session_env.reset_async.__func__
is not Environment.reset_async
)
if is_async:
sig = inspect.signature(session_env.reset_async)
valid_kwargs = self._get_valid_kwargs(sig, msg.data)
observation = await session_env.reset_async(
**valid_kwargs
)
else:
sig = inspect.signature(session_env.reset)
valid_kwargs = self._get_valid_kwargs(sig, msg.data)
observation = await self._run_in_session_executor(
session_id, session_env.reset, **valid_kwargs
)
self._update_session_activity(session_id)
response = WSObservationResponse(
data=serialize_observation(observation),
)
case "step":
msg = WSStepMessage(**message_dict)
action = deserialize_action(msg.data, self.action_cls)
is_async = (
session_env.step_async.__func__
is not Environment.step_async
)
if is_async:
observation = await session_env.step_async(action)
else:
observation = await self._run_in_session_executor(
session_id, session_env.step, action
)
self._update_session_activity(
session_id, increment_step=True
)
response = WSObservationResponse(
data=serialize_observation(observation)
)
case "state":
msg = WSStateMessage(**message_dict)
state = session_env.state
if hasattr(state, "model_dump"):
state_data = state.model_dump()
else:
state_data = dict(state) if state else {}
response = WSStateResponse(data=state_data)
case "close":
msg = WSCloseMessage(**message_dict)
break
case "mcp":
msg = WSMCPMessage(**message_dict)
try:
rpc_request = JsonRpcRequest(**msg.data)
except (ValidationError, Exception) as e:
rpc_response = JsonRpcResponse.error_response(
JsonRpcErrorCode.INVALID_REQUEST,
f"Invalid request: {e}",
)
else:
rpc_response = await mcp_handler(
rpc_request,
session_env=session_env,
)
response = WSMCPResponse(data=rpc_response.model_dump())
case _:
response = WSErrorResponse(
data={
"message": f"Unknown message type: {msg_type}",
"code": WSErrorCode.UNKNOWN_TYPE,
}
)
await websocket.send_text(response.model_dump_json())
except ValidationError as e:
error_resp = WSErrorResponse(
data={
"message": "Invalid message",
"code": WSErrorCode.VALIDATION_ERROR,
"errors": e.errors(),
}
)
await websocket.send_text(error_resp.model_dump_json())
except Exception as e:
error_resp = WSErrorResponse(
data={
"message": str(e),
"code": WSErrorCode.EXECUTION_ERROR,
}
)
await websocket.send_text(error_resp.model_dump_json())
except WebSocketDisconnect:
pass
except SessionCapacityError as e:
error_resp = WSErrorResponse(
data={
"message": str(e),
"code": WSErrorCode.CAPACITY_REACHED,
"active_sessions": e.active_sessions,
"max_sessions": e.max_sessions,
}
)
await websocket.send_text(error_resp.model_dump_json())
except EnvironmentFactoryError as e:
error_resp = WSErrorResponse(
data={
"message": str(e),
"code": WSErrorCode.FACTORY_ERROR,
"factory_name": e.factory_name,
}
)
await websocket.send_text(error_resp.model_dump_json())
except Exception as e:
error_resp = WSErrorResponse(
data={"message": str(e), "code": WSErrorCode.SESSION_ERROR}
)
await websocket.send_text(error_resp.model_dump_json())
finally:
if session_id:
await self._destroy_session(session_id)
try:
await websocket.close()
except RuntimeError:
pass
def create_app(
env: Callable[[], Environment],
action_cls: Type[Action],
observation_cls: Type[Observation],
env_name: Optional[str] = None,
max_concurrent_envs: Optional[int] = None,
concurrency_config: Optional[ConcurrencyConfig] = None,
gradio_builder: Optional[Callable[..., Any]] = None,
) -> FastAPI:
"""
Create a FastAPI application with or without web interface.
This function creates a FastAPI app with the web interface enabled by default,
including README integration for better user experience.
Args:
env: Environment factory (callable) that creates new instances
action_cls: The Action subclass this environment expects
observation_cls: The Observation subclass this environment returns
env_name: Optional environment name for README loading
max_concurrent_envs: Maximum concurrent WebSocket sessions.
Mutually exclusive with concurrency_config.
concurrency_config: Optional ConcurrencyConfig for advanced concurrency settings.
Mutually exclusive with max_concurrent_envs.
gradio_builder: Optional callable to build a custom Gradio UI at /web.
Signature: (web_manager, action_fields, metadata, is_chat_env, title,
quick_start_md) -> gr.Blocks. When None, the default Gradio app is used.
See docs/customizing-web-ui.md.
Returns:
FastAPI application instance with or without web interface and README integration
"""
# Check if web interface should be enabled
# This can be controlled via environment variable or build argument
enable_web = os.getenv("ENABLE_WEB_INTERFACE", "false").lower() in (
"true",
"1",
"yes",
)
if enable_web:
# Gradio-based web UI (gradio is a core dependency)
from .web_interface import create_web_interface_app
return create_web_interface_app(
env,
action_cls,
observation_cls,
env_name,
max_concurrent_envs,
concurrency_config,
gradio_builder=gradio_builder,
)
else:
# Use standard FastAPI app without web interface
return create_fastapi_app(
env, action_cls, observation_cls, max_concurrent_envs, concurrency_config
)
def create_fastapi_app(
env: Callable[[], Environment],
action_cls: Type[Action],
observation_cls: Type[Observation],
max_concurrent_envs: Optional[int] = None,
concurrency_config: Optional[ConcurrencyConfig] = None,
) -> FastAPI:
"""
Create a FastAPI application with comprehensive documentation.
Args:
env: Environment factory (callable) that creates new instances
action_cls: The Action subclass this environment expects
observation_cls: The Observation subclass this environment returns
max_concurrent_envs: Maximum concurrent WebSocket sessions.
Mutually exclusive with concurrency_config.
concurrency_config: Optional ConcurrencyConfig for advanced concurrency settings.
Mutually exclusive with max_concurrent_envs.
Returns:
FastAPI application instance
"""
try:
from fastapi import FastAPI
except ImportError:
raise ImportError(
"FastAPI is required. Install with: pip install fastapi uvicorn"
)
app = FastAPI(
title="OpenEnv Environment HTTP API",
version="1.0.0",
description="""
# OpenEnv Environment HTTP API
HTTP API for interacting with OpenEnv environments through a standardized interface.
## Features
* **Environment Reset**: Initialize or restart episodes
* **Action Execution**: Send actions and receive observations
* **State Inspection**: Query current environment state
* **Schema Access**: Retrieve JSON schemas for actions and observations
## Workflow
1. Call `/reset` to start a new episode and get initial observation
2. Call `/step` repeatedly with actions to interact with environment
3. Episode ends when observation returns `done: true`
4. Call `/state` anytime to inspect current environment state
## Documentation
* **Swagger UI**: Available at `/docs`
* **ReDoc**: Available at `/redoc`
* **OpenAPI Schema**: Available at `/openapi.json`
""",
openapi_tags=[
{
"name": "Environment Control",
"description": "Core operations for environment interaction (reset, step)",
},
{
"name": "State Management",
"description": "Operations for inspecting environment state",
},
{
"name": "Environment Info",
"description": "Information about the environment",
},
{
"name": "Schema",
"description": "JSON Schema endpoints for actions, observations, and state",
},
{"name": "Health", "description": "Service health and status checks"},
],
docs_url="/docs",
redoc_url="/redoc",
openapi_url="/openapi.json",
contact={
"name": "OpenEnv Team",
"url": "https://github.com/meta-pytorch/OpenEnv",
},
license_info={
"name": "BSD-3-Clause",
"url": "https://github.com/meta-pytorch/OpenEnv/blob/main/LICENSE",
},
)
server = HTTPEnvServer(
env,
action_cls,
observation_cls,
max_concurrent_envs,
concurrency_config=concurrency_config,
)
server.register_routes(app)
return app
|