Contents

Agent基础设施之:沙盒 阿里的通用沙盒OpenSandbox

OpenSandbox 是一个面向 AI 应用场景设计的「通用沙箱平台」,为大模型相关的能力(命令执行、文件操作、代码执行、浏览器操作、Agent 运行等)提供 多语言 SDK、沙箱接口协议和沙箱运行时

核心特性

  • 多语言 SDK:提供 Python、Java/Kotlin、JavaScript/TypeScript 等语言的客户端 SDK,Go SDK 仍在规划中。
  • 沙箱协议:定义了沙箱生命周期管理 API 和沙箱执行 API。你可以通过这些沙箱协议扩展自己的沙箱运行时。
  • 沙箱运行时:默认实现沙箱生命周期管理,支持 Docker 和 Kubernetes 运行时,实现大规模分布式沙箱调度。
  • 沙箱环境:内置 Command、Filesystem、Code Interpreter 实现。并提供 Coding Agent(Claude Code 等)、浏览器自动化(Chrome、Playwright)和桌面环境(VNC、VS Code)等示例。

使用示例

沙箱基础操作

环境要求:

  • Docker(本地运行必需)
  • Python 3.10+(本地 runtime 和快速开始)

1. 克隆仓库

git clone https://github.com/alibaba/OpenSandbox.git
cd OpenSandbox

2. 启动沙箱 Server

cd server
uv sync
cp example.config.zh.toml ~/.sandbox.toml # 复制配置文件
uv run python -m src.main # 启动服务

3. 创建代码解释器,并在沙箱中执行命令

安装 Code Interpreter SDK

uv pip install opensandbox-code-interpreter

创建沙箱并执行命令

import asyncio
from datetime import timedelta

from code_interpreter import CodeInterpreter, SupportedLanguage
from opensandbox import Sandbox
from opensandbox.models import WriteEntry

async def main() -> None:
    # 1. Create a sandbox
    sandbox = await Sandbox.create(
        "sandbox-registry.cn-zhangjiakou.cr.aliyuncs.com/opensandbox/code-interpreter:latest",
        entrypoint= ["/opt/opensandbox/code-interpreter.sh"],
        env={"PYTHON_VERSION": "3.11"},
        timeout=timedelta(minutes=10),
    )

    async with sandbox:

        # 2. Execute a shell command
        execution = await sandbox.commands.run("echo 'Hello OpenSandbox!'")
        print(execution.logs.stdout[0].text)

        # 3. Write a file
        await sandbox.files.write_files([
            WriteEntry(path="/tmp/hello.txt", data="Hello World", mode=644)
        ])

        # 4. Read a file
        content = await sandbox.files.read_file("/tmp/hello.txt")
        print(f"Content: {content}") # Content: Hello World

        # 5. Create a code interpreter
        interpreter = await CodeInterpreter.create(sandbox)

        # 6. 执行 Python 代码(单次执行:直接传 language)
        result = await interpreter.codes.run(
              """
                  import sys
                  print(sys.version)
                  result = 2 + 2
                  result
              """,
              language=SupportedLanguage.PYTHON,
        )

        print(result.result[0].text) # 4
        print(result.logs.stdout[0].text) # 3.11.14

    # 7. Cleanup the sandbox
    await sandbox.kill()

if __name__ == "__main__":
    asyncio.run(main())

更多示例

OpenSandbox 提供了丰富的示例来演示不同场景下的沙箱使用方式。所有示例代码位于 examples/ 目录下。

🎯 基础示例

  • code-interpreter - Code Interpreter SDK 完整示例

    • 展示如何使用 Code Interpreter SDK 在沙箱中运行命令,执行 Python/Java/Go/TS 代码
    • 包含上下文创建、代码执行、结果输出等完整流程
    • 支持自定义语言版本
  • aio-sandbox - All-in-One 沙箱示例

    • 使用 OpenSandbox SDK 创建 agent-sandbox 沙箱
    • 演示如何连接并使用 AIO 沙箱提供的完整功能

🤖 Coding Agent 集成

在 OpenSandbox 中,集成各类 Coding Agent,包括 Claude Code、Google Gemini、OpenAI Codex 等。

  • claude-code - Claude Code 集成
  • gemini-cli - Google Gemini CLI 集成
  • codex-cli - OpenAI Codex CLI 集成
  • iflow-cli - iFLow CLI 集成
  • langgraph - LangGraph 集成
  • google-adk - Google ADK 集成

🌐 浏览器与桌面环境

  • chrome - Chrome 无头浏览器

    • 启动带有远程调试功能的 Chromium 浏览器
    • 提供 VNC (端口 5901) 和 DevTools (端口 9222) 访问
    • 适合需要浏览器自动化或调试的场景
  • playwright - Playwright 浏览器自动化

    • 使用 Playwright + Chromium 在无头模式下抓取网页内容
    • 可提取网页标题、正文等信息
    • 适合网页爬虫和自动化测试
  • desktop - VNC 桌面环境

    • 启动完整的桌面环境(Xvfb + x11vnc + fluxbox)
    • 通过 VNC 客户端远程访问沙箱桌面
    • 支持自定义 VNC 密码
  • vscode - VS Code Web 环境

    • 在沙箱中运行 code-server (VS Code Web 版本)
    • 通过浏览器访问完整的 VS Code 开发环境
    • 适合远程开发和代码编辑场景