云端代码沙箱 SDK - 为 AI agents 提供安全的代码执行环境
@agentsphere/code-interpreter 更符合命名规范
code-interpreter-v1 模板,提供更好的性能
bash
npm install @agentsphere/code-interpreter
`
快速开始
$3
1. 注册 agentsphere 账号 这里
2. 获取 API 密钥 这里
3. 设置环境变量:
`bash
AGENTSPHERE_API_KEY=your_api_key_here
`
$3
`typescript
import { Sandbox } from '@agentsphere/code-interpreter'
const sandbox = await Sandbox.create()
console.log('沙箱已创建:', sandbox.sandboxId)
// 执行 Python 代码
const execution = await sandbox.runCode(
)
console.log('执行结果:', execution.text)
console.log('图表数据:', execution.results[0]?.data)
`
$3
`typescript
import { Sandbox } from '@agentsphere/code-interpreter'
// 创建沙箱并执行代码
const sbx = await Sandbox.create()
await sbx.runCode('x = 42')
// 暂停沙箱
const sandboxId = await sbx.betaPause()
console.log('沙箱已暂停:', sandboxId)
// 稍后恢复沙箱
const resumedSbx = await Sandbox.connect(sandboxId)
const result = await resumedSbx.runCode('print(f"x 的值是: {x}")')
console.log(result.text) // 输出: x 的值是: 42
await resumedSbx.kill()
`
API 文档
$3
#### Sandbox.create(template?: string)
创建新的沙箱实例
#### runCode(code: string, options?: RunCodeOpts)
执行代码,支持回调和自定义配置
#### createCodeContext(options?: CreateCodeContextOpts)
创建新的代码执行上下文
#### betaPause() 🆕
暂停沙箱状态,返回沙箱ID用于后续恢复
#### Sandbox.connect(sandboxId: string) 🆕
连接到已暂停的沙箱实例
$3
`typescript
interface RunCodeOpts {
onStdout?: (output: OutputMessage) => Promise | any
onStderr?: (output: OutputMessage) => Promise | any
onResult?: (data: Result) => Promise | any
onError?: (error: ExecutionError) => Promise | any
envs?: Record
timeoutMs?: number
requestTimeoutMs?: number
}
`
支持的语言
- Python (默认)
- JavaScript/TypeScript
- Java
- R
- Bash
- 更多语言持续添加中...
更新日志
$3
- ✅ 添加沙箱暂停/恢复功能
- ✅ 迁移到 agentsphere-sandbox-base 依赖
- ✅ 更新包名为 @agentsphere/code-interpreter`