Skip to content

Commit 2b16c12

Browse files
committed
Initial commit
LibreCodeInterpreter - A secure, open-source code interpreter API that provides sandboxed code execution in isolated Docker containers. Features: - Multi-language support (Python, JS, TS, Go, Java, C, C++, PHP, Rust, R, Fortran, D) - Sub-50ms Python execution with pre-warmed REPL containers - Container pooling for fast acquisition - Secure sandboxed execution with comprehensive resource limits - File management and session persistence - Admin dashboard for monitoring and API key management - Compatible with LibreChat's Code Interpreter API
0 parents  commit 2b16c12

178 files changed

Lines changed: 40445 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.dockerignore

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Git
2+
.git
3+
.gitignore
4+
5+
# Python
6+
__pycache__/
7+
*.py[cod]
8+
*$py.class
9+
*.so
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# Virtual environments
29+
venv/
30+
env/
31+
ENV/
32+
33+
# IDE
34+
.vscode/
35+
.idea/
36+
*.swp
37+
*.swo
38+
*~
39+
40+
# OS
41+
.DS_Store
42+
Thumbs.db
43+
44+
# Logs
45+
*.log
46+
logs/
47+
48+
# Test coverage
49+
.coverage
50+
htmlcov/
51+
.pytest_cache/
52+
53+
# Environment files
54+
.env
55+
.env.local
56+
.env.prod
57+
.env.staging
58+
59+
# Documentation
60+
docs/
61+
*.md
62+
!README.md
63+
64+
# Docker
65+
Dockerfile*
66+
docker-compose*.yml
67+
.dockerignore
68+
69+
# Deployment
70+
deploy.sh
71+
72+
# Data directories
73+
data/
74+
uploads/
75+
temp/
76+
77+
# Node modules (if any)
78+
node_modules/
79+
80+
# Temporary files
81+
*.tmp
82+
*.temp

.env.example

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
# Code Interpreter API Configuration
2+
3+
# API Configuration
4+
API_HOST=0.0.0.0
5+
API_PORT=8000
6+
API_DEBUG=false
7+
API_RELOAD=false
8+
9+
# SSL/HTTPS Configuration
10+
ENABLE_HTTPS=false
11+
HTTPS_PORT=443
12+
SSL_CERT_FILE=/app/ssl/cert.pem
13+
SSL_KEY_FILE=/app/ssl/key.pem
14+
SSL_REDIRECT=false
15+
# SSL_CA_CERTS=/app/ssl/ca.pem # Optional CA certificates
16+
17+
# Authentication Configuration
18+
API_KEY=your-secure-api-key-here-change-this-in-production
19+
# API_KEYS=key1,key2,key3 # Additional API keys (comma-separated)
20+
API_KEY_HEADER=x-api-key
21+
API_KEY_CACHE_TTL=300
22+
23+
# API Key Management Configuration
24+
# MASTER_API_KEY=your-secure-master-key # Required for CLI key management
25+
RATE_LIMIT_ENABLED=true
26+
27+
# Redis Configuration
28+
REDIS_HOST=localhost
29+
REDIS_PORT=6379
30+
REDIS_PASSWORD=
31+
REDIS_DB=0
32+
# Alternative: Use Redis URL instead of individual settings
33+
# REDIS_URL=redis://localhost:6379/0
34+
REDIS_MAX_CONNECTIONS=20
35+
REDIS_SOCKET_TIMEOUT=5
36+
REDIS_SOCKET_CONNECT_TIMEOUT=5
37+
38+
# MinIO/S3 Configuration
39+
MINIO_ENDPOINT=localhost:9000
40+
MINIO_ACCESS_KEY=minioadmin
41+
MINIO_SECRET_KEY=minioadmin
42+
MINIO_SECURE=false
43+
MINIO_BUCKET=code-interpreter-files
44+
MINIO_REGION=us-east-1
45+
46+
# Docker Configuration
47+
# DOCKER_BASE_URL=unix://var/run/docker.sock
48+
DOCKER_TIMEOUT=60
49+
DOCKER_NETWORK_MODE=none
50+
DOCKER_READ_ONLY=true
51+
52+
# Resource Limits - Execution
53+
MAX_EXECUTION_TIME=30
54+
MAX_MEMORY_MB=512
55+
MAX_CPU_QUOTA=50000
56+
MAX_PROCESSES=32
57+
MAX_OPEN_FILES=1024
58+
59+
# Resource Limits - Files
60+
MAX_FILE_SIZE_MB=10
61+
MAX_TOTAL_FILE_SIZE_MB=50
62+
MAX_FILES_PER_SESSION=50
63+
MAX_OUTPUT_FILES=10
64+
MAX_FILENAME_LENGTH=255
65+
66+
# Resource Limits - Sessions
67+
MAX_CONCURRENT_EXECUTIONS=10
68+
MAX_SESSIONS_PER_ENTITY=100
69+
70+
# Session Configuration
71+
# TTL applies only to MinIO-stored session data (files/metadata). Containers are ephemeral per execution.
72+
SESSION_TTL_HOURS=24
73+
SESSION_CLEANUP_INTERVAL_MINUTES=60
74+
SESSION_ID_LENGTH=32
75+
76+
# MinIO Orphan Cleanup (optional)
77+
# Enable periodic pruning of MinIO objects older than TTL with missing Redis sessions
78+
ENABLE_ORPHAN_MINIO_CLEANUP=false
79+
80+
# Container Pool Configuration
81+
CONTAINER_POOL_ENABLED=true
82+
CONTAINER_POOL_WARMUP_ON_STARTUP=true
83+
84+
# Per-language pool sizes (0 = on-demand only, no pre-warming)
85+
# Only set the languages you want to pre-warm
86+
CONTAINER_POOL_PY=5 # Python
87+
CONTAINER_POOL_JS=2 # JavaScript
88+
# CONTAINER_POOL_TS=0 # TypeScript (default: 0)
89+
# CONTAINER_POOL_GO=0 # Go (default: 0)
90+
# CONTAINER_POOL_JAVA=0 # Java (default: 0)
91+
# CONTAINER_POOL_C=0 # C (default: 0)
92+
# CONTAINER_POOL_CPP=0 # C++ (default: 0)
93+
# CONTAINER_POOL_PHP=0 # PHP (default: 0)
94+
# CONTAINER_POOL_RS=0 # Rust (default: 0)
95+
# CONTAINER_POOL_R=0 # R (default: 0)
96+
# CONTAINER_POOL_F90=0 # Fortran (default: 0)
97+
# CONTAINER_POOL_D=0 # D (default: 0)
98+
99+
# Pool optimization settings
100+
CONTAINER_POOL_PARALLEL_BATCH=5
101+
CONTAINER_POOL_REPLENISH_INTERVAL=2
102+
CONTAINER_POOL_EXHAUSTION_TRIGGER=true
103+
104+
# REPL Configuration (Python Fast Execution)
105+
# Pre-warmed Python interpreter for ~20-40ms execution latency
106+
REPL_ENABLED=true
107+
REPL_WARMUP_TIMEOUT_SECONDS=15
108+
REPL_HEALTH_CHECK_TIMEOUT_SECONDS=5
109+
110+
# State Persistence Configuration (Python)
111+
# Enables Python variable/function persistence across executions within same session
112+
STATE_PERSISTENCE_ENABLED=true
113+
# Redis hot storage TTL (default: 2 hours)
114+
STATE_TTL_SECONDS=7200
115+
# Maximum serialized state size
116+
STATE_MAX_SIZE_MB=50
117+
# Capture state even on execution failure
118+
STATE_CAPTURE_ON_ERROR=false
119+
120+
# State Archival Configuration (Python)
121+
# Archives inactive states from Redis to MinIO for long-term storage
122+
STATE_ARCHIVE_ENABLED=true
123+
# Archive to MinIO after this inactivity period (default: 1 hour)
124+
STATE_ARCHIVE_AFTER_SECONDS=3600
125+
# Keep archived states in MinIO for this many days
126+
STATE_ARCHIVE_TTL_DAYS=7
127+
# How often to check for states to archive
128+
STATE_ARCHIVE_CHECK_INTERVAL_SECONDS=300
129+
130+
# Detailed Metrics Configuration
131+
# Track per-API-key, per-language execution metrics
132+
DETAILED_METRICS_ENABLED=true
133+
# Maximum metrics to buffer in memory
134+
METRICS_BUFFER_SIZE=10000
135+
# Archive metrics to MinIO for long-term analysis
136+
METRICS_ARCHIVE_ENABLED=true
137+
# Keep archived metrics for this many days
138+
METRICS_ARCHIVE_RETENTION_DAYS=90
139+
140+
# Security Configuration
141+
ENABLE_NETWORK_ISOLATION=true
142+
ENABLE_FILESYSTEM_ISOLATION=true
143+
144+
# WAN Network Access Configuration
145+
# When enabled, execution containers can access the public internet
146+
# but are blocked from accessing host, other containers, and private networks
147+
# IMPORTANT: Requires NET_ADMIN capability for iptables management
148+
ENABLE_WAN_ACCESS=false
149+
WAN_NETWORK_NAME=code-interpreter-wan
150+
# WAN_DNS_SERVERS=8.8.8.8,1.1.1.1,8.8.4.4
151+
152+
# Logging Configuration
153+
LOG_LEVEL=INFO
154+
LOG_FORMAT=json
155+
# LOG_FILE=/var/log/code-interpreter-api.log
156+
LOG_MAX_SIZE_MB=100
157+
LOG_BACKUP_COUNT=5
158+
ENABLE_ACCESS_LOGS=true
159+
ENABLE_SECURITY_LOGS=true
160+
161+
# Health Check Configuration
162+
HEALTH_CHECK_INTERVAL=30
163+
HEALTH_CHECK_TIMEOUT=5
164+
165+
# Development Configuration
166+
ENABLE_CORS=false
167+
# CORS_ORIGINS=http://localhost:3000,http://localhost:8080
168+
ENABLE_DOCS=true
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ""
5+
labels: bug, needs-triage
6+
assignees: ""
7+
---
8+
9+
**Describe the bug**
10+
A clear and concise description of what the bug is.
11+
12+
**To Reproduce**
13+
Steps to reproduce the behavior:
14+
15+
1. Go to '...'
16+
2. Click on '...'
17+
3. Scroll down to '...'
18+
4. See error
19+
20+
**Expected behavior**
21+
A clear and concise description of what you expected to happen.
22+
23+
**Screenshots**
24+
If applicable, add screenshots to help explain your problem.
25+
26+
**Environment (please complete the following information):**
27+
28+
- OS: [e.g. Ubuntu 22.04]
29+
- Version [e.g. 1.0.0]
30+
31+
**Additional context**
32+
Add any other context about the problem here.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ""
5+
labels: enhancement, needs-triage
6+
assignees: ""
7+
---
8+
9+
**Is your feature request related to a problem? Please describe.**
10+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
11+
12+
**Describe the solution you'd like**
13+
A clear and concise description of what you want to happen.
14+
15+
**Describe alternatives you've considered**
16+
A clear and concise description of any alternative solutions or features you've considered.
17+
18+
**Additional context**
19+
Add any other context or screenshots about the feature request here.

.github/pull_request_template.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Description
2+
3+
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
4+
5+
Fixes # (issue)
6+
7+
## Type of change
8+
9+
Please delete options that are not relevant.
10+
11+
- [ ] Bug fix (non-breaking change which fixes an issue)
12+
- [ ] New feature (non-breaking change which adds functionality)
13+
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
14+
- [ ] Documentation update
15+
16+
# How Has This Been Tested?
17+
18+
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
19+
20+
- [ ] Test A
21+
- [ ] Test B
22+
23+
# Checklist:
24+
25+
- [ ] My code follows the style guidelines of this project
26+
- [ ] I have performed a self-review of my own code
27+
- [ ] I have commented my code, particularly in hard-to-understand areas
28+
- [ ] I have made corresponding changes to the documentation
29+
- [ ] My changes generate no new warnings
30+
- [ ] I have added tests that prove my fix is effective or that my feature works
31+
- [ ] New and existing unit tests pass locally with my changes
32+
- [ ] Any dependent changes have been merged and published in downstream modules
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Docker Build and Publish
2+
3+
on:
4+
push:
5+
branches: ["main", "dev"]
6+
tags: ["v*.*.*"]
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
IMAGE_NAME: ${{ github.repository }}
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
permissions:
16+
contents: read
17+
packages: write
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Set up QEMU
24+
uses: docker/setup-qemu-action@v3
25+
26+
- name: Set up Docker Buildx
27+
uses: docker/setup-buildx-action@v3
28+
29+
- name: Log into registry ${{ env.REGISTRY }}
30+
uses: docker/login-action@v3
31+
with:
32+
registry: ${{ env.REGISTRY }}
33+
username: ${{ github.actor }}
34+
password: ${{ secrets.GITHUB_TOKEN }}
35+
36+
- name: Extract Docker metadata
37+
id: meta
38+
uses: docker/metadata-action@v5
39+
with:
40+
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
41+
tags: |
42+
type=ref,event=branch
43+
type=semver,pattern={{version}}
44+
type=sha,format=long
45+
type=raw,value=latest,enable={{is_default_branch}}
46+
47+
- name: Build and push Docker image
48+
uses: docker/build-push-action@v5
49+
with:
50+
context: .
51+
push: true
52+
tags: ${{ steps.meta.outputs.tags }}
53+
labels: ${{ steps.meta.outputs.labels }}
54+
cache-from: type=gha
55+
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)