|
| 1 | +# CodeRunner REST API Implementation |
| 2 | + |
| 3 | +This document describes the new REST API implementation that provides InstaVM-compatible code execution endpoints alongside the existing MCP server. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +This PR implements a comprehensive REST API that enables: |
| 8 | + |
| 9 | +1. **Zero-config local code execution** - No API keys or setup required |
| 10 | +2. **InstaVM-compatible interface** - Seamless migration path to cloud |
| 11 | +3. **Session management** - Persistent execution contexts |
| 12 | +4. **Multi-language support** - Python, bash, JavaScript execution |
| 13 | +5. **Auto-container management** - Automatic Docker container lifecycle |
| 14 | + |
| 15 | +## New Components |
| 16 | + |
| 17 | +### 1. REST API Server (`api/rest_server.py`) |
| 18 | + |
| 19 | +FastAPI server running on port 8223 alongside the existing MCP server (port 8222). |
| 20 | + |
| 21 | +**Key Endpoints:** |
| 22 | +- `POST /execute` - Synchronous code execution (InstaVM compatible) |
| 23 | +- `POST /execute_async` - Asynchronous code execution |
| 24 | +- `POST /sessions` - Create execution session |
| 25 | +- `GET /sessions/{id}` - Get session info |
| 26 | +- `DELETE /sessions/{id}` - Close session |
| 27 | +- `GET /health` - Health check |
| 28 | +- `GET /languages` - List supported languages |
| 29 | + |
| 30 | +### 2. Session Management (`core/session_manager.py`) |
| 31 | + |
| 32 | +Manages persistent execution contexts with: |
| 33 | +- Dedicated kernel allocation per session |
| 34 | +- Automatic session cleanup (24-hour timeout) |
| 35 | +- Session state tracking and health monitoring |
| 36 | +- Integration with existing kernel pool |
| 37 | + |
| 38 | +### 3. Language Processing (`core/language_processor.py`) |
| 39 | + |
| 40 | +Multi-language execution support: |
| 41 | +- **Python**: Default execution environment |
| 42 | +- **Bash/Shell**: Commands prefixed with `%%bash` |
| 43 | +- **JavaScript**: Node.js execution via `%%javascript` |
| 44 | +- Automatic language detection and command preprocessing |
| 45 | + |
| 46 | +### 4. Container Management (`container_manager.py`) |
| 47 | + |
| 48 | +Auto-management of Docker containers: |
| 49 | +- Automatic container startup and health checking |
| 50 | +- Docker availability validation |
| 51 | +- Platform-specific installation guidance |
| 52 | +- Container lifecycle management (start/stop/remove) |
| 53 | + |
| 54 | +### 5. CodeRunner Client (`__init__.py`) |
| 55 | + |
| 56 | +Zero-configuration Python client: |
| 57 | +- Auto-starts Docker container if needed |
| 58 | +- InstaVM-compatible method signatures |
| 59 | +- Comprehensive error handling |
| 60 | +- Session management and health monitoring |
| 61 | + |
| 62 | +### 6. Cloud Migration Support (`cloud/__init__.py`) |
| 63 | + |
| 64 | +Seamless migration to InstaVM cloud: |
| 65 | +- Import alias pattern: `from coderunner.cloud import InstaVM as CodeRunner` |
| 66 | +- Identical interface between local and cloud execution |
| 67 | +- Graceful fallback when InstaVM package not installed |
| 68 | + |
| 69 | +## Usage Examples |
| 70 | + |
| 71 | +### Local Execution (Zero Config) |
| 72 | +```python |
| 73 | +from coderunner import CodeRunner |
| 74 | + |
| 75 | +# Auto-starts container, no setup required |
| 76 | +runner = CodeRunner() |
| 77 | + |
| 78 | +# Execute Python code |
| 79 | +result = runner.execute("print('Hello World!')") |
| 80 | +print(result['stdout']) # "Hello World!" |
| 81 | + |
| 82 | +# Execute bash commands |
| 83 | +result = runner.execute("echo 'Hello Bash!'", language="bash") |
| 84 | +print(result['stdout']) # "Hello Bash!" |
| 85 | +``` |
| 86 | + |
| 87 | +### Cloud Migration (1-2 Line Change) |
| 88 | +```python |
| 89 | +# Change import and add API key - everything else stays the same! |
| 90 | +from coderunner.cloud import InstaVM as CodeRunner |
| 91 | + |
| 92 | +runner = CodeRunner(api_key="your-key") |
| 93 | +result = runner.execute("print('Hello Cloud!')") |
| 94 | +``` |
| 95 | + |
| 96 | +### Session Management |
| 97 | +```python |
| 98 | +with CodeRunner() as runner: |
| 99 | + # Persistent session across executions |
| 100 | + runner.execute("x = 5") |
| 101 | + result = runner.execute("print(x)") |
| 102 | + print(result['stdout']) # "5" |
| 103 | +``` |
| 104 | + |
| 105 | +## API Compatibility |
| 106 | + |
| 107 | +The REST API implements the same interface as InstaVM for seamless migration: |
| 108 | + |
| 109 | +| Endpoint | Method | InstaVM Compatible | Description | |
| 110 | +|----------|--------|-------------------|-------------| |
| 111 | +| `/execute` | POST | ✅ | Synchronous execution | |
| 112 | +| `/execute_async` | POST | ✅ | Asynchronous execution | |
| 113 | +| `/sessions` | POST | ✅ | Create session | |
| 114 | +| `/sessions/{id}` | GET/DELETE | ✅ | Session management | |
| 115 | +| `/health` | GET | ✅ | Health check | |
| 116 | + |
| 117 | +## Architecture Changes |
| 118 | + |
| 119 | +### Container Configuration |
| 120 | +- **Port 8223**: New REST API server |
| 121 | +- **Port 8222**: Existing MCP server (unchanged) |
| 122 | +- **Port 8888**: Jupyter server (unchanged) |
| 123 | +- **Port 3000**: Playwright server (unchanged) |
| 124 | + |
| 125 | +### Process Flow |
| 126 | +``` |
| 127 | +entrypoint.sh |
| 128 | +├── Start Jupyter Server (port 8888) |
| 129 | +├── Start Playwright Server (port 3000) |
| 130 | +├── Start REST API Server (port 8223) [NEW] |
| 131 | +└── Start MCP Server (port 8222) [EXISTING] |
| 132 | +``` |
| 133 | + |
| 134 | +### Integration Points |
| 135 | +- REST API uses existing `kernel_pool` for execution |
| 136 | +- Session manager allocates dedicated kernels |
| 137 | +- Language processor handles multi-language support |
| 138 | +- All existing MCP functionality remains unchanged |
| 139 | + |
| 140 | +## Testing |
| 141 | + |
| 142 | +Comprehensive test suite covering: |
| 143 | +- **Language processor**: Normalization, detection, preprocessing |
| 144 | +- **Container manager**: Docker integration, health checks, lifecycle |
| 145 | +- **CodeRunner client**: Execution, sessions, error handling |
| 146 | +- **API endpoints**: Request/response validation, error cases |
| 147 | + |
| 148 | +Run tests: |
| 149 | +```bash |
| 150 | +pytest tests/ -v |
| 151 | +``` |
| 152 | + |
| 153 | +## Deployment |
| 154 | + |
| 155 | +### Docker Updates |
| 156 | +- `Dockerfile`: Expose port 8223 |
| 157 | +- `entrypoint.sh`: Start REST API server alongside MCP |
| 158 | +- `requirements.txt`: Add new dependencies |
| 159 | + |
| 160 | +### Local Development |
| 161 | +```bash |
| 162 | +# Start container with new API |
| 163 | +./install.sh |
| 164 | + |
| 165 | +# Test REST API |
| 166 | +curl http://localhost:8223/health |
| 167 | + |
| 168 | +# Test code execution |
| 169 | +curl -X POST http://localhost:8223/execute \ |
| 170 | + -H "Content-Type: application/json" \ |
| 171 | + -d '{"command": "print(\"Hello API!\")", "language": "python"}' |
| 172 | +``` |
| 173 | + |
| 174 | +## Migration Benefits |
| 175 | + |
| 176 | +### For New Users |
| 177 | +- **Zero friction**: No signups, API keys, or setup |
| 178 | +- **Instant start**: `pip install coderunner` and go |
| 179 | +- **Full functionality**: Python, bash, session management |
| 180 | + |
| 181 | +### For Existing InstaVM Users |
| 182 | +- **Easy testing**: Test workflows locally before cloud deployment |
| 183 | +- **Cost optimization**: Local development reduces cloud usage |
| 184 | +- **Simple migration**: 1-2 line import change |
| 185 | + |
| 186 | +### For Development Workflows |
| 187 | +- **Hybrid execution**: Local dev, cloud production |
| 188 | +- **Consistent interface**: Same code works everywhere |
| 189 | +- **Gradual adoption**: Migrate at your own pace |
| 190 | + |
| 191 | +## Backward Compatibility |
| 192 | + |
| 193 | +- ✅ **Existing MCP server unchanged** - All current functionality preserved |
| 194 | +- ✅ **Container ports unchanged** - MCP still on 8222, Jupyter on 8888 |
| 195 | +- ✅ **Dockerfile compatible** - New port 8223 added alongside existing |
| 196 | +- ✅ **Dependencies minimal** - Only adds client-side packages |
| 197 | + |
| 198 | +## Next Steps |
| 199 | + |
| 200 | +This PR provides the foundation for: |
| 201 | + |
| 202 | +1. **Browser automation integration** (Phase 2) |
| 203 | + - Enhanced Playwright API matching InstaVM |
| 204 | + - Browser session management |
| 205 | + - Screenshot and interaction capabilities |
| 206 | + |
| 207 | +2. **Advanced features** |
| 208 | + - File upload/download endpoints |
| 209 | + - Real-time execution streaming |
| 210 | + - Enhanced async task management |
| 211 | + |
| 212 | +3. **Integration improvements** |
| 213 | + - OpenAI function calling support |
| 214 | + - LangChain/LlamaIndex integrations |
| 215 | + - Enhanced error reporting |
| 216 | + |
| 217 | +## Review Focus Areas |
| 218 | + |
| 219 | +1. **REST API design** - InstaVM compatibility and error handling |
| 220 | +2. **Session management** - Kernel allocation and cleanup logic |
| 221 | +3. **Container management** - Docker integration and platform support |
| 222 | +4. **Client interface** - Method signatures and error handling |
| 223 | +5. **Test coverage** - Core functionality and edge cases |
0 commit comments