SuperAPI AI Directory

sandbox-service

Deployment & operations

View repository

Updated 10-16 09:58

About this skill

Features

A secure code execution sandbox service capable of:

  1. Executing code in isolated Docker containers
  2. Supporting multiple languages: Python, Shell, Bash
  3. Automatic timeout control and resource limits
  4. Automatically destroying containers after execution, with no state residue

Use Cases

  • Agents need to execute code to verify results
  • Running user-provided code snippets
  • Data processing and computation tasks
  • Script execution and automation

Security Restrictions

  • Network isolation:--network none
  • Memory limit: 256MB
  • CPU limit: 1 core
  • Execution time: up to 60 seconds
  • User privileges: non-root (nobody)
  • File system: read-only

Invocation Method

from services.sandbox_service.client import SandboxClient

client = SandboxClient()

# 健康检查
status = client.health_check()

# 执行 Python 代码
result = client.execute("print(1+1)", language="python")

# 执行 Shell 命令
result = client.execute("echo Hello && date", language="shell")

# 自定义超时(秒)
result = client.execute(
    code="import time; time.sleep(5)",
    language="python",
    timeout=10
)

# 传递环境变量
result = client.execute(
    code="import os; print(os.environ.get('MY_VAR'))",
    language="python",
    env_vars={"MY_VAR": "hello"}
)

# 检查执行结果
if result["success"]:
    print(f"输出: {result['stdout']}")
else:
    print(f"错误: {result['stderr']}")

Trust Mode (Code Fusion)

Using trusted_mode=True allows calling other services in code:

# 信任模式:允许访问 services 和网络
result = client.execute(
    code='''
from services.embedding_service.client import EmbeddingServiceClient
from services.rerank_service.client import RerankServiceClient

# 获取 embedding
embed_client = EmbeddingServiceClient()
vec1 = embed_client.embed_query("人工智能")
vec2 = embed_client.embed_query("机器学习")

# 计算相似度
import math
dot = sum(a*b for a,b in zip(vec1, vec2))
norm1 = math.sqrt(sum(a*a for a in vec1))
norm2 = math.sqrt(sum(b*b for b in vec2))
similarity = dot / (norm1 * norm2)

print(f"相似度: {similarity:.4f}")
''',
    language="python",
    trusted_mode=True  # 开启信任模式
)

Trust Mode Notes:

  • Allows access to the myagent_network network
  • Can call Python clients of other services
  • Suitable for Agent code fusion scenarios
  • Memory and CPU limits still apply

Return Format

{
  "success": true,
  "stdout": "2\n",
  "stderr": "",
  "exit_code": 0,
  "execution_time": 1.058,
  "error": null
}

Error Handling

{
  "success": false,
  "stdout": "",
  "stderr": "执行超时(3秒)",
  "exit_code": -1,
  "execution_time": 3.05,
  "error": "Execution timeout"
}

Supported Languages

Language Identifier Base Image
Python python, python3, py python:3.10-slim
Shell shell, sh alpine:latest
Bash bash bash:latest