File size: 3,534 Bytes
31e7814
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Coding Environment

A Python code execution environment that runs arbitrary Python code and returns results. Perfect for testing code execution infrastructure and demonstrating environment usage patterns.

## Quick Start

The simplest way to use the Coding environment is through the `CodingEnv` class:

```python
from envs.coding_env import CodeAction, CodingEnv

try:
    # Create environment from Docker image
    coding_env = CodingEnv.from_docker_image("coding-env:latest")

    # Reset
    result = coding_env.reset()
    print(f"Reset complete: exit_code={result.observation.exit_code}")

    # Execute Python code
    code_samples = [
        "print('Hello, World!')",
        "x = 5 + 3\nprint(f'Result: {x}')",
        "import math\nprint(math.pi)"
    ]

    for code in code_samples:
        result = coding_env.step(CodeAction(code=code))
        print(f"Code: {code}")
        print(f"  β†’ stdout: {result.observation.stdout.strip()}")
        print(f"  β†’ exit_code: {result.observation.exit_code}")

finally:
    # Always clean up
    coding_env.close()
```

That's it! The `CodingEnv.from_docker_image()` method handles:
- Starting the Docker container
- Waiting for the server to be ready
- Connecting to the environment
- Container cleanup when you call `close()`

## Building the Docker Image

Before using the environment, you need to build the Docker image:

```bash
# From project root
docker build -t coding-env:latest -f src/envs/coding_env/server/Dockerfile .
```

## Environment Details

### Action
**CodeAction**: Contains a single field
- `code` (str) - The Python code to execute

### Observation
**CodeObservation**: Contains the execution results
- `stdout` (str) - Standard output from code execution
- `stderr` (str) - Standard error from code execution
- `exit_code` (int) - Exit code (0 for success, non-zero for errors)

### State
**CodeState**: Tracks execution state
- `episode_id` (str) - Unique identifier for the episode
- `step_count` (int) - Number of steps taken
- `last_exit_code` (int) - Exit code from the last execution

## Advanced Usage

### Connecting to an Existing Server

If you already have a Coding environment server running, you can connect directly:

```python
from envs.coding_env import CodingEnv

# Connect to existing server
coding_env = CodingEnv(base_url="<ENV_HTTP_URL_HERE>")

# Use as normal
result = coding_env.reset()
result = coding_env.step(CodeAction(code="print('Hello!')"))
```

Note: When connecting to an existing server, `coding_env.close()` will NOT stop the server.

## Development & Testing

### Running the Full Example

Run the complete example that demonstrates the full workflow:

```bash
python3 src/envs/coding_env/client/example_usage.py
```

This example shows:
- Creating an environment from a Docker image
- Resetting and executing code through the environment
- Automatic cleanup with `close()`

## Project Structure

```
coding_env/
β”œβ”€β”€ README.md              # This file
β”œβ”€β”€ models.py              # Action, Observation, and State models
β”œβ”€β”€ client/
β”‚   β”œβ”€β”€ coding_env_client.py  # CodingEnv client implementation
β”‚   └── example_usage.py      # Usage examples
└── server/
    β”œβ”€β”€ python_codeact_env.py  # Core environment logic
    β”œβ”€β”€ app.py                 # FastAPI application
    β”œβ”€β”€ transforms.py          # Observation transforms
    β”œβ”€β”€ Dockerfile             # Container image definition
    └── README.md              # Server-specific documentation
```