sairaj2 commited on
Commit
5740942
·
1 Parent(s): c6baed0
env/README.md ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Env Environment Server
3
+ emoji: 📯
4
+ colorFrom: gray
5
+ colorTo: pink
6
+ sdk: docker
7
+ pinned: false
8
+ app_port: 8000
9
+ base_path: /web
10
+ tags:
11
+ - openenv
12
+ ---
13
+
14
+ # Env Environment
15
+
16
+ A simple test environment that echoes back messages. Perfect for testing the env APIs as well as demonstrating environment usage patterns.
17
+
18
+ ## Quick Start
19
+
20
+ The simplest way to use the Env environment is through the `EnvEnv` class:
21
+
22
+ ```python
23
+ from env import EnvAction, EnvEnv
24
+
25
+ try:
26
+ # Create environment from Docker image
27
+ envenv = EnvEnv.from_docker_image("env-env:latest")
28
+
29
+ # Reset
30
+ result = envenv.reset()
31
+ print(f"Reset: {result.observation.echoed_message}")
32
+
33
+ # Send multiple messages
34
+ messages = ["Hello, World!", "Testing echo", "Final message"]
35
+
36
+ for msg in messages:
37
+ result = envenv.step(EnvAction(message=msg))
38
+ print(f"Sent: '{msg}'")
39
+ print(f" → Echoed: '{result.observation.echoed_message}'")
40
+ print(f" → Length: {result.observation.message_length}")
41
+ print(f" → Reward: {result.reward}")
42
+
43
+ finally:
44
+ # Always clean up
45
+ envenv.close()
46
+ ```
47
+
48
+ That's it! The `EnvEnv.from_docker_image()` method handles:
49
+ - Starting the Docker container
50
+ - Waiting for the server to be ready
51
+ - Connecting to the environment
52
+ - Container cleanup when you call `close()`
53
+
54
+ ## Building the Docker Image
55
+
56
+ Before using the environment, you need to build the Docker image:
57
+
58
+ ```bash
59
+ # From project root
60
+ docker build -t env-env:latest -f server/Dockerfile .
61
+ ```
62
+
63
+ ## Deploying to Hugging Face Spaces
64
+
65
+ You can easily deploy your OpenEnv environment to Hugging Face Spaces using the `openenv push` command:
66
+
67
+ ```bash
68
+ # From the environment directory (where openenv.yaml is located)
69
+ openenv push
70
+
71
+ # Or specify options
72
+ openenv push --namespace my-org --private
73
+ ```
74
+
75
+ The `openenv push` command will:
76
+ 1. Validate that the directory is an OpenEnv environment (checks for `openenv.yaml`)
77
+ 2. Prepare a custom build for Hugging Face Docker space (enables web interface)
78
+ 3. Upload to Hugging Face (ensuring you're logged in)
79
+
80
+ ### Prerequisites
81
+
82
+ - Authenticate with Hugging Face: The command will prompt for login if not already authenticated
83
+
84
+ ### Options
85
+
86
+ - `--directory`, `-d`: Directory containing the OpenEnv environment (defaults to current directory)
87
+ - `--repo-id`, `-r`: Repository ID in format 'username/repo-name' (defaults to 'username/env-name' from openenv.yaml)
88
+ - `--base-image`, `-b`: Base Docker image to use (overrides Dockerfile FROM)
89
+ - `--private`: Deploy the space as private (default: public)
90
+
91
+ ### Examples
92
+
93
+ ```bash
94
+ # Push to your personal namespace (defaults to username/env-name from openenv.yaml)
95
+ openenv push
96
+
97
+ # Push to a specific repository
98
+ openenv push --repo-id my-org/my-env
99
+
100
+ # Push with a custom base image
101
+ openenv push --base-image ghcr.io/meta-pytorch/openenv-base:latest
102
+
103
+ # Push as a private space
104
+ openenv push --private
105
+
106
+ # Combine options
107
+ openenv push --repo-id my-org/my-env --base-image custom-base:latest --private
108
+ ```
109
+
110
+ After deployment, your space will be available at:
111
+ `https://huggingface.co/spaces/<repo-id>`
112
+
113
+ The deployed space includes:
114
+ - **Web Interface** at `/web` - Interactive UI for exploring the environment
115
+ - **API Documentation** at `/docs` - Full OpenAPI/Swagger interface
116
+ - **Health Check** at `/health` - Container health monitoring
117
+ - **WebSocket** at `/ws` - Persistent session endpoint for low-latency interactions
118
+
119
+ ## Environment Details
120
+
121
+ ### Action
122
+ **EnvAction**: Contains a single field
123
+ - `message` (str) - The message to echo back
124
+
125
+ ### Observation
126
+ **EnvObservation**: Contains the echo response and metadata
127
+ - `echoed_message` (str) - The message echoed back
128
+ - `message_length` (int) - Length of the message
129
+ - `reward` (float) - Reward based on message length (length × 0.1)
130
+ - `done` (bool) - Always False for echo environment
131
+ - `metadata` (dict) - Additional info like step count
132
+
133
+ ### Reward
134
+ The reward is calculated as: `message_length × 0.1`
135
+ - "Hi" → reward: 0.2
136
+ - "Hello, World!" → reward: 1.3
137
+ - Empty message → reward: 0.0
138
+
139
+ ## Advanced Usage
140
+
141
+ ### Connecting to an Existing Server
142
+
143
+ If you already have a Env environment server running, you can connect directly:
144
+
145
+ ```python
146
+ from env import EnvEnv
147
+
148
+ # Connect to existing server
149
+ envenv = EnvEnv(base_url="<ENV_HTTP_URL_HERE>")
150
+
151
+ # Use as normal
152
+ result = envenv.reset()
153
+ result = envenv.step(EnvAction(message="Hello!"))
154
+ ```
155
+
156
+ Note: When connecting to an existing server, `envenv.close()` will NOT stop the server.
157
+
158
+ ### Using the Context Manager
159
+
160
+ The client supports context manager usage for automatic connection management:
161
+
162
+ ```python
163
+ from env import EnvAction, EnvEnv
164
+
165
+ # Connect with context manager (auto-connects and closes)
166
+ with EnvEnv(base_url="http://localhost:8000") as env:
167
+ result = env.reset()
168
+ print(f"Reset: {result.observation.echoed_message}")
169
+ # Multiple steps with low latency
170
+ for msg in ["Hello", "World", "!"]:
171
+ result = env.step(EnvAction(message=msg))
172
+ print(f"Echoed: {result.observation.echoed_message}")
173
+ ```
174
+
175
+ The client uses WebSocket connections for:
176
+ - **Lower latency**: No HTTP connection overhead per request
177
+ - **Persistent session**: Server maintains your environment state
178
+ - **Efficient for episodes**: Better for many sequential steps
179
+
180
+ ### Concurrent WebSocket Sessions
181
+
182
+ The server supports multiple concurrent WebSocket connections. To enable this,
183
+ modify `server/app.py` to use factory mode:
184
+
185
+ ```python
186
+ # In server/app.py - use factory mode for concurrent sessions
187
+ app = create_app(
188
+ EnvEnvironment, # Pass class, not instance
189
+ EnvAction,
190
+ EnvObservation,
191
+ max_concurrent_envs=4, # Allow 4 concurrent sessions
192
+ )
193
+ ```
194
+
195
+ Then multiple clients can connect simultaneously:
196
+
197
+ ```python
198
+ from env import EnvAction, EnvEnv
199
+ from concurrent.futures import ThreadPoolExecutor
200
+
201
+ def run_episode(client_id: int):
202
+ with EnvEnv(base_url="http://localhost:8000") as env:
203
+ result = env.reset()
204
+ for i in range(10):
205
+ result = env.step(EnvAction(message=f"Client {client_id}, step {i}"))
206
+ return client_id, result.observation.message_length
207
+
208
+ # Run 4 episodes concurrently
209
+ with ThreadPoolExecutor(max_workers=4) as executor:
210
+ results = list(executor.map(run_episode, range(4)))
211
+ ```
212
+
213
+ ## Development & Testing
214
+
215
+ ### Direct Environment Testing
216
+
217
+ Test the environment logic directly without starting the HTTP server:
218
+
219
+ ```bash
220
+ # From the server directory
221
+ python3 server/env_environment.py
222
+ ```
223
+
224
+ This verifies that:
225
+ - Environment resets correctly
226
+ - Step executes actions properly
227
+ - State tracking works
228
+ - Rewards are calculated correctly
229
+
230
+ ### Running Locally
231
+
232
+ Run the server locally for development:
233
+
234
+ ```bash
235
+ uvicorn server.app:app --reload
236
+ ```
237
+
238
+ ## Project Structure
239
+
240
+ ```
241
+ env/
242
+ ├── .dockerignore # Docker build exclusions
243
+ ├── __init__.py # Module exports
244
+ ├── README.md # This file
245
+ ├── openenv.yaml # OpenEnv manifest
246
+ ├── pyproject.toml # Project metadata and dependencies
247
+ ├── uv.lock # Locked dependencies (generated)
248
+ ├── client.py # EnvEnv client
249
+ ├── models.py # Action and Observation models
250
+ └── server/
251
+ ├── __init__.py # Server module exports
252
+ ├── env_environment.py # Core environment logic
253
+ ├── app.py # FastAPI application (HTTP + WebSocket endpoints)
254
+ └── Dockerfile # Container image definition
255
+ ```
env/__init__.py CHANGED
@@ -1,8 +1,27 @@
1
- """
2
- OpenEnv Data Cleaning Environment Package
3
- """
 
 
4
 
5
- from env.datacleaner_env import DataCleaningEnv
6
- from env.models import Action, Observation, Reward, TaskConfig
7
 
8
- __all__ = ["DataCleaningEnv", "Action", "Observation", "Reward", "TaskConfig"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
 
7
+ """Env Environment."""
 
8
 
9
+ from .client import EnvEnv
10
+ from .models import EnvAction, EnvObservation
11
+
12
+ # Data Cleaning Environment exports (from testenv)
13
+ from .datacleaner_env import DataCleaningEnv
14
+ from .models import Action, Observation, Reward, TaskConfig
15
+
16
+ __all__ = [
17
+ # Original Env
18
+ "EnvAction",
19
+ "EnvObservation",
20
+ "EnvEnv",
21
+ # Data Cleaning Environment
22
+ "DataCleaningEnv",
23
+ "Action",
24
+ "Observation",
25
+ "Reward",
26
+ "TaskConfig",
27
+ ]
env/__pycache__/__init__.cpython-314.pyc CHANGED
Binary files a/env/__pycache__/__init__.cpython-314.pyc and b/env/__pycache__/__init__.cpython-314.pyc differ
 
env/__pycache__/action_engine.cpython-314.pyc DELETED
Binary file (25.8 kB)
 
env/__pycache__/client.cpython-314.pyc ADDED
Binary file (4.38 kB). View file
 
env/__pycache__/datacleaner_env.cpython-314.pyc DELETED
Binary file (21.1 kB)
 
env/__pycache__/grader.cpython-314.pyc DELETED
Binary file (14.5 kB)
 
env/__pycache__/models.cpython-314.pyc CHANGED
Binary files a/env/__pycache__/models.cpython-314.pyc and b/env/__pycache__/models.cpython-314.pyc differ
 
env/__pycache__/reward.cpython-314.pyc DELETED
Binary file (11.5 kB)
 
env/__pycache__/tasks.cpython-314.pyc DELETED
Binary file (3.2 kB)
 
env/client.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ """Env Environment Client."""
8
+
9
+ from typing import Dict
10
+
11
+ from openenv.core import EnvClient
12
+ from openenv.core.client_types import StepResult
13
+ from openenv.core.env_server.types import State
14
+
15
+ from .models import EnvAction, EnvObservation
16
+
17
+
18
+ class EnvEnv(
19
+ EnvClient[EnvAction, EnvObservation, State]
20
+ ):
21
+ """
22
+ Client for the Env Environment.
23
+
24
+ This client maintains a persistent WebSocket connection to the environment server,
25
+ enabling efficient multi-step interactions with lower latency.
26
+ Each client instance has its own dedicated environment session on the server.
27
+
28
+ Example:
29
+ >>> # Connect to a running server
30
+ >>> with EnvEnv(base_url="http://localhost:8000") as client:
31
+ ... result = client.reset()
32
+ ... print(result.observation.echoed_message)
33
+ ...
34
+ ... result = client.step(EnvAction(message="Hello!"))
35
+ ... print(result.observation.echoed_message)
36
+
37
+ Example with Docker:
38
+ >>> # Automatically start container and connect
39
+ >>> client = EnvEnv.from_docker_image("env-env:latest")
40
+ >>> try:
41
+ ... result = client.reset()
42
+ ... result = client.step(EnvAction(message="Test"))
43
+ ... finally:
44
+ ... client.close()
45
+ """
46
+
47
+ def _step_payload(self, action: EnvAction) -> Dict:
48
+ """
49
+ Convert EnvAction to JSON payload for step message.
50
+
51
+ Args:
52
+ action: EnvAction instance
53
+
54
+ Returns:
55
+ Dictionary representation suitable for JSON encoding
56
+ """
57
+ return {
58
+ "message": action.message,
59
+ }
60
+
61
+ def _parse_result(self, payload: Dict) -> StepResult[EnvObservation]:
62
+ """
63
+ Parse server response into StepResult[EnvObservation].
64
+
65
+ Args:
66
+ payload: JSON response data from server
67
+
68
+ Returns:
69
+ StepResult with EnvObservation
70
+ """
71
+ obs_data = payload.get("observation", {})
72
+ observation = EnvObservation(
73
+ echoed_message=obs_data.get("echoed_message", ""),
74
+ message_length=obs_data.get("message_length", 0),
75
+ done=payload.get("done", False),
76
+ reward=payload.get("reward"),
77
+ metadata=obs_data.get("metadata", {}),
78
+ )
79
+
80
+ return StepResult(
81
+ observation=observation,
82
+ reward=payload.get("reward"),
83
+ done=payload.get("done", False),
84
+ )
85
+
86
+ def _parse_state(self, payload: Dict) -> State:
87
+ """
88
+ Parse server response into State object.
89
+
90
+ Args:
91
+ payload: JSON response from state request
92
+
93
+ Returns:
94
+ State object with episode_id and step_count
95
+ """
96
+ return State(
97
+ episode_id=payload.get("episode_id"),
98
+ step_count=payload.get("step_count", 0),
99
+ )
env/models.py CHANGED
@@ -1,15 +1,43 @@
 
 
 
 
 
 
1
  """
2
- OpenEnv Data Cleaning Environment - Pydantic Data Models
3
- Strict typing for actions, observations, rewards, and task configurations.
 
 
4
  """
5
 
6
  from typing import Optional, Dict, Any, List
 
 
7
  from pydantic import BaseModel, Field
8
 
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  class Action(BaseModel):
11
  """
12
- OpenEnv-compliant action model.
13
  Represents a single action to be executed in the environment.
14
  """
15
  action_type: str = Field(
@@ -32,7 +60,7 @@ class Action(BaseModel):
32
 
33
  class Observation(BaseModel):
34
  """
35
- OpenEnv-compliant observation model.
36
  Represents the state observation returned after reset or step.
37
  """
38
  dataset_info: Dict[str, Any] = Field(
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
  """
8
+ Data models for the Env Environment.
9
+
10
+ The env environment is a simple test environment that echoes back messages.
11
+ Also includes data cleaning environment models from testenv.
12
  """
13
 
14
  from typing import Optional, Dict, Any, List
15
+
16
+ from openenv.core.env_server.types import Action as BaseAction, Observation as BaseObservation
17
  from pydantic import BaseModel, Field
18
 
19
 
20
+ class EnvAction(BaseAction):
21
+ """Action for the Env environment - just a message to echo."""
22
+
23
+ message: str = Field(..., description="Message to echo back")
24
+
25
+
26
+ class EnvObservation(BaseObservation):
27
+ """Observation from the Env environment - the echoed message."""
28
+
29
+ echoed_message: str = Field(default="", description="The echoed message")
30
+ message_length: int = Field(default=0, description="Length of the echoed message")
31
+
32
+
33
+ # ============================================================
34
+ # Data Cleaning Environment Models (from testenv)
35
+ # ============================================================
36
+
37
+
38
  class Action(BaseModel):
39
  """
40
+ OpenEnv-compliant action model for data cleaning.
41
  Represents a single action to be executed in the environment.
42
  """
43
  action_type: str = Field(
 
60
 
61
  class Observation(BaseModel):
62
  """
63
+ OpenEnv-compliant observation model for data cleaning.
64
  Represents the state observation returned after reset or step.
65
  """
66
  dataset_info: Dict[str, Any] = Field(
env/openenv.yaml ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ spec_version: 1
2
+ name: env
3
+ type: space
4
+ runtime: fastapi
5
+ app: server.app:app
6
+ port: 8000
7
+
env/pyproject.toml ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ [build-system]
8
+ requires = ["setuptools>=45", "wheel"]
9
+ build-backend = "setuptools.build_meta"
10
+
11
+ [project]
12
+ name = "openenv-env"
13
+ version = "0.1.0"
14
+ description = "Env environment for OpenEnv"
15
+ requires-python = ">=3.10"
16
+ dependencies = [
17
+ # Core OpenEnv runtime (provides FastAPI server + HTTP client types)
18
+ # install from github
19
+ # "openenv-core[core] @ git+https://github.com/meta-pytorch/OpenEnv.git",
20
+ "openenv-core[core]>=0.2.2",
21
+ # Environment-specific dependencies
22
+ # Add all dependencies needed for your environment here
23
+ # Examples:
24
+ # "numpy>=1.19.0",
25
+ # "torch>=2.0.0",
26
+ # "gymnasium>=0.29.0",
27
+ # "openspiel>=1.0.0",
28
+ # "smolagents>=1.22.0,<2",
29
+ ]
30
+
31
+ [project.optional-dependencies]
32
+ dev = [
33
+ "pytest>=8.0.0",
34
+ "pytest-cov>=4.0.0",
35
+ ]
36
+
37
+ [project.scripts]
38
+ # Server entry point - enables running via: uv run --project . server
39
+ # or: python -m env.server.app
40
+ server = "env.server.app:main"
41
+
42
+ [tool.setuptools]
43
+ include-package-data = true
44
+ packages = ["env", "env.server"]
45
+ package-dir = { "env" = ".", "env.server" = "server" }
env/reward.py CHANGED
@@ -16,7 +16,7 @@ logger = logging.getLogger("openenv-datacleaner.reward")
16
 
17
  class RewardCalculator:
18
  """
19
- Calculates structured rewards for data cleaning actions.
20
  Reward = quality + progress - penalty
21
  """
22
 
 
16
 
17
  class RewardCalculator:
18
  """
19
+ Calculates rewards for data cleaning actions.
20
  Reward = quality + progress - penalty
21
  """
22
 
env/server/Dockerfile ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ # Multi-stage build using openenv-base
8
+ # This Dockerfile is flexible and works for both:
9
+ # - In-repo environments (with local OpenEnv sources)
10
+ # - Standalone environments (with openenv from PyPI/Git)
11
+ # The build script (openenv build) handles context detection and sets appropriate build args.
12
+
13
+ ARG BASE_IMAGE=ghcr.io/meta-pytorch/openenv-base:latest
14
+ FROM ${BASE_IMAGE} AS builder
15
+
16
+ WORKDIR /app
17
+
18
+ # Ensure git is available (required for installing dependencies from VCS)
19
+ RUN apt-get update && \
20
+ apt-get install -y --no-install-recommends git && \
21
+ rm -rf /var/lib/apt/lists/*
22
+
23
+ # Build argument to control whether we're building standalone or in-repo
24
+ ARG BUILD_MODE=in-repo
25
+ ARG ENV_NAME=env
26
+
27
+ # Copy environment code (always at root of build context)
28
+ COPY . /app/env
29
+
30
+ # For in-repo builds, openenv is already vendored in the build context
31
+ # For standalone builds, openenv will be installed via pyproject.toml
32
+ WORKDIR /app/env
33
+
34
+ # Ensure uv is available (for local builds where base image lacks it)
35
+ RUN if ! command -v uv >/dev/null 2>&1; then \
36
+ curl -LsSf https://astral.sh/uv/install.sh | sh && \
37
+ mv /root/.local/bin/uv /usr/local/bin/uv && \
38
+ mv /root/.local/bin/uvx /usr/local/bin/uvx; \
39
+ fi
40
+
41
+ # Install dependencies using uv sync
42
+ # If uv.lock exists, use it; otherwise resolve on the fly
43
+ RUN --mount=type=cache,target=/root/.cache/uv \
44
+ if [ -f uv.lock ]; then \
45
+ uv sync --frozen --no-install-project --no-editable; \
46
+ else \
47
+ uv sync --no-install-project --no-editable; \
48
+ fi
49
+
50
+ RUN --mount=type=cache,target=/root/.cache/uv \
51
+ if [ -f uv.lock ]; then \
52
+ uv sync --frozen --no-editable; \
53
+ else \
54
+ uv sync --no-editable; \
55
+ fi
56
+
57
+ # Final runtime stage
58
+ FROM ${BASE_IMAGE}
59
+
60
+ WORKDIR /app
61
+
62
+ # Copy the virtual environment from builder
63
+ COPY --from=builder /app/env/.venv /app/.venv
64
+
65
+ # Copy the environment code
66
+ COPY --from=builder /app/env /app/env
67
+
68
+ # Set PATH to use the virtual environment
69
+ ENV PATH="/app/.venv/bin:$PATH"
70
+
71
+ # Set PYTHONPATH so imports work correctly
72
+ ENV PYTHONPATH="/app/env:$PYTHONPATH"
73
+
74
+ # Health check
75
+ HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
76
+ CMD curl -f http://localhost:8000/health || exit 1
77
+
78
+ # Run the FastAPI server
79
+ # The module path is constructed to work with the /app/env structure
80
+ CMD ["sh", "-c", "cd /app/env && uvicorn server.app:app --host 0.0.0.0 --port 8000"]
env/server/__init__.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ """Env environment server components."""
8
+
9
+ from .env_environment import EnvEnvironment
10
+
11
+ __all__ = ["EnvEnvironment"]
env/server/__pycache__/__init__.cpython-314.pyc ADDED
Binary file (404 Bytes). View file
 
env/server/__pycache__/app.cpython-314.pyc ADDED
Binary file (2.97 kB). View file
 
env/server/__pycache__/env_environment.cpython-314.pyc ADDED
Binary file (4.52 kB). View file
 
env/server/app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ """
8
+ FastAPI application for the Env Environment.
9
+
10
+ This module creates an HTTP server that exposes the EnvEnvironment
11
+ over HTTP and WebSocket endpoints, compatible with EnvClient.
12
+
13
+ Endpoints:
14
+ - POST /reset: Reset the environment
15
+ - POST /step: Execute an action
16
+ - GET /state: Get current environment state
17
+ - GET /schema: Get action/observation schemas
18
+ - WS /ws: WebSocket endpoint for persistent sessions
19
+
20
+ Usage:
21
+ # Development (with auto-reload):
22
+ uvicorn server.app:app --reload --host 0.0.0.0 --port 8000
23
+
24
+ # Production:
25
+ uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 4
26
+
27
+ # Or run directly:
28
+ python -m server.app
29
+ """
30
+
31
+ try:
32
+ from openenv.core.env_server.http_server import create_app
33
+ except Exception as e: # pragma: no cover
34
+ raise ImportError(
35
+ "openenv is required for the web interface. Install dependencies with '\n uv sync\n'"
36
+ ) from e
37
+
38
+ # Use absolute imports (PYTHONPATH is set to /app/env in Dockerfile)
39
+ from models import EnvAction, EnvObservation
40
+ from server.env_environment import EnvEnvironment
41
+
42
+
43
+ # Create the app with web interface and README integration
44
+ app = create_app(
45
+ EnvEnvironment,
46
+ EnvAction,
47
+ EnvObservation,
48
+ env_name="env",
49
+ max_concurrent_envs=1, # increase this number to allow more concurrent WebSocket sessions
50
+ )
51
+
52
+
53
+ def main(host: str = "0.0.0.0", port: int = 8000):
54
+ """
55
+ Entry point for direct execution via uv run or python -m.
56
+
57
+ This function enables running the server without Docker:
58
+ uv run --project . server
59
+ uv run --project . server --port 8001
60
+ python -m env.server.app
61
+
62
+ Args:
63
+ host: Host address to bind to (default: "0.0.0.0")
64
+ port: Port number to listen on (default: 8000)
65
+
66
+ For production deployments, consider using uvicorn directly with
67
+ multiple workers:
68
+ uvicorn env.server.app:app --workers 4
69
+ """
70
+ import uvicorn
71
+
72
+ uvicorn.run(app, host=host, port=port)
73
+
74
+
75
+ if __name__ == "__main__":
76
+ import argparse
77
+
78
+ parser = argparse.ArgumentParser()
79
+ parser.add_argument("--port", type=int, default=8000)
80
+ args = parser.parse_args()
81
+ main(port=args.port)
env/server/env_environment.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 the
5
+ # LICENSE file in the root directory of this source tree.
6
+
7
+ """
8
+ Env Environment Implementation.
9
+
10
+ A simple test environment that echoes back messages sent to it.
11
+ Perfect for testing HTTP server infrastructure.
12
+ """
13
+
14
+ from uuid import uuid4
15
+
16
+ from openenv.core.env_server.interfaces import Environment
17
+ from openenv.core.env_server.types import State
18
+
19
+ # Use absolute imports (PYTHONPATH is set to /app/env in Dockerfile)
20
+ from models import EnvAction, EnvObservation
21
+
22
+
23
+ class EnvEnvironment(Environment):
24
+ """
25
+ A simple echo environment that echoes back messages.
26
+
27
+ This environment is designed for testing the HTTP server infrastructure.
28
+ It maintains minimal state and simply echoes back whatever message it receives.
29
+
30
+ Example:
31
+ >>> env = EnvEnvironment()
32
+ >>> obs = env.reset()
33
+ >>> print(obs.echoed_message) # "Env environment ready!"
34
+ >>>
35
+ >>> obs = env.step(EnvAction(message="Hello"))
36
+ >>> print(obs.echoed_message) # "Hello"
37
+ >>> print(obs.message_length) # 5
38
+ """
39
+
40
+ # Enable concurrent WebSocket sessions.
41
+ # Set to True if your environment isolates state between instances.
42
+ # When True, multiple WebSocket clients can connect simultaneously, each
43
+ # getting their own environment instance (when using factory mode in app.py).
44
+ SUPPORTS_CONCURRENT_SESSIONS: bool = True
45
+
46
+ def __init__(self):
47
+ """Initialize the env environment."""
48
+ self._state = State(episode_id=str(uuid4()), step_count=0)
49
+ self._reset_count = 0
50
+
51
+ def reset(self) -> EnvObservation:
52
+ """
53
+ Reset the environment.
54
+
55
+ Returns:
56
+ EnvObservation with a ready message
57
+ """
58
+ self._state = State(episode_id=str(uuid4()), step_count=0)
59
+ self._reset_count += 1
60
+
61
+ return EnvObservation(
62
+ echoed_message="Env environment ready!",
63
+ message_length=0,
64
+ done=False,
65
+ reward=0.0,
66
+ )
67
+
68
+ def step(self, action: EnvAction) -> EnvObservation: # type: ignore[override]
69
+ """
70
+ Execute a step in the environment by echoing the message.
71
+
72
+ Args:
73
+ action: EnvAction containing the message to echo
74
+
75
+ Returns:
76
+ EnvObservation with the echoed message and its length
77
+ """
78
+ self._state.step_count += 1
79
+
80
+ message = action.message
81
+ length = len(message)
82
+
83
+ # Simple reward: longer messages get higher rewards
84
+ reward = length * 0.1
85
+
86
+ return EnvObservation(
87
+ echoed_message=message,
88
+ message_length=length,
89
+ done=False,
90
+ reward=reward,
91
+ metadata={"original_message": message, "step": self._state.step_count},
92
+ )
93
+
94
+ @property
95
+ def state(self) -> State:
96
+ """
97
+ Get the current environment state.
98
+
99
+ Returns:
100
+ Current State with episode_id and step_count
101
+ """
102
+ return self._state
env/server/requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ openenv[core]>=0.2.0
2
+ fastapi>=0.115.0
3
+ uvicorn>=0.24.0
4
+
5
+
6
+
static/index.html ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>OpenEnv Data Cleaner</title>
7
+ <style>
8
+ * {
9
+ margin: 0;
10
+ padding: 0;
11
+ box-sizing: border-box;
12
+ }
13
+
14
+ :root {
15
+ --primary: #6366f1;
16
+ --primary-dark: #4f46e5;
17
+ --success: #22c55e;
18
+ --warning: #f59e0b;
19
+ --danger: #ef4444;
20
+ --bg: #0f172a;
21
+ --bg-card: #1e293b;
22
+ --bg-input: #334155;
23
+ --text: #f1f5f9;
24
+ --text-muted: #94a3b8;
25
+ --border: #475569;
26
+ }
27
+
28
+ body {
29
+ font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
30
+ background: var(--bg);
31
+ color: var(--text);
32
+ min-height: 100vh;
33
+ }
34
+
35
+ .container {
36
+ max-width: 1400px;
37
+ margin: 0 auto;
38
+ padding: 20px;
39
+ }
40
+
41
+ header {
42
+ text-align: center;
43
+ padding: 30px 0;
44
+ border-bottom: 1px solid var(--border);
45
+ margin-bottom: 30px;
46
+ }
47
+
48
+ header h1 {
49
+ font-size: 2.5rem;
50
+ background: linear-gradient(135deg, var(--primary), #a855f7);
51
+ -webkit-background-clip: text;
52
+ -webkit-text-fill-color: transparent;
53
+ margin-bottom: 10px;
54
+ }
55
+
56
+ header p {
57
+ color: var(--text-muted);
58
+ font-size: 1.1rem;
59
+ }
60
+
61
+ .status-bar {
62
+ display: flex;
63
+ gap: 20px;
64
+ justify-content: center;
65
+ flex-wrap: wrap;
66
+ margin-bottom: 30px;
67
+ }
68
+
69
+ .status-item {
70
+ background: var(--bg-card);
71
+ padding: 15px 25px;
72
+ border-radius: 12px;
73
+ border: 1px solid var(--border);
74
+ min-width: 150px;
75
+ text-align: center;
76
+ }
77
+
78
+ .status-item .label {
79
+ font-size: 0.8rem;
80
+ color: var(--text-muted);
81
+ text-transform: uppercase;
82
+ letter-spacing: 1px;
83
+ }
84
+
85
+ .status-item .value {
86
+ font-size: 1.3rem;
87
+ font-weight: 600;
88
+ margin-top: 5px;
89
+ }
90
+
91
+ .status-item .value.healthy { color: var(--success); }
92
+ .status-item .value.warning { color: var(--warning); }
93
+ .status-item .value.error { color: var(--danger); }
94
+
95
+ .grid {
96
+ display: grid;
97
+ grid-template-columns: 300px 1fr;
98
+ gap: 30px;
99
+ }
100
+
101
+ @media (max-width: 900px) {
102
+ .grid {
103
+ grid-template-columns: 1fr;
104
+ }
105
+ }
106
+
107
+ .sidebar {
108
+ display: flex;
109
+ flex-direction: column;
110
+ gap: 20px;
111
+ }
112
+
113
+ .card {
114
+ background: var(--bg-card);
115
+ border-radius: 16px;
116
+ padding: 24px;
117
+ border: 1px solid var(--border);
118
+ }
119
+
120
+ .card h3 {
121
+ font-size: 1.1rem;
122
+ margin-bottom: 16px;
123
+ color: var(--text);
124
+ display: flex;
125
+ align-items: center;
126
+ gap: 10px;
127
+ }
128
+
129
+ .card h3 .icon {
130
+ font-size: 1.3rem;
131
+ }
132
+
133
+ select, button {
134
+ width: 100%;
135
+ padding: 12px 16px;
136
+ border-radius: 10px;
137
+ border: 1px solid var(--border);
138
+ font-size: 0.95rem;
139
+ cursor: pointer;
140
+ transition: all 0.2s;
141
+ }
142
+
143
+ select {
144
+ background: var(--bg-input);
145
+ color: var(--text);
146
+ margin-bottom: 12px;
147
+ }
148
+
149
+ select:focus {
150
+ outline: none;
151
+ border-color: var(--primary);
152
+ }
153
+
154
+ button {
155
+ font-weight: 600;
156
+ display: flex;
157
+ align-items: center;
158
+ justify-content: center;
159
+ gap: 8px;
160
+ }
161
+
162
+ .btn-primary {
163
+ background: var(--primary);
164
+ color: white;
165
+ border: none;
166
+ }
167
+
168
+ .btn-primary:hover {
169
+ background: var(--primary-dark);
170
+ }
171
+
172
+ .btn-success {
173
+ background: var(--success);
174
+ color: white;
175
+ border: none;
176
+ }
177
+