AI SDK - E2B 沙盒
这个软件包是 实验 并瞄准AI SDK 7(现在稳定).
内部运行您的代理密码 E2B 软件 沙盒从 AI SDK 软件这是E2B对应的 @ai-sdk/ sandbox-vercel (英语)丢入一个 HarnessAgent,或者将一个会话直接交给您的 AI SDK 工具.
绳子本身如何运作 AI SDK 吊带文件。 。 。 .
设置
npm i @e2b/ai-sdk-sandbox
设定 E2B_API_KEY (或通过) apiKey )),在设置中. 通话 createE2BSandbox() 无法独自到达E2B. 沙盒在您调用时创建 createSession()。 。 。 .
使用量
import { createE2BSandbox } from '@e2b/ai-sdk-sandbox';
const e2bSandbox = createE2BSandbox({ template: 'base' });
const sandboxSession = await e2bSandbox.createSession();
// restricted() returns the same sandbox narrowed to the tool-safe surface
// (file I/O, run, spawn) with no lifecycle or network controls — this is what
// you hand an AI SDK tool's execute().
const restrictedSandboxSession = sandboxSession.restricted();
await restrictedSandboxSession.writeTextFile({ path: 'hello.txt', content: 'hi' });
const { stdout } = await restrictedSandboxSession.run({ command: 'cat hello.txt' });
console.log(stdout); // "hi"
await sandboxSession.stop();
restricted() 给你一个 Experimental_SandboxSession页:1 同样的暗沙盒,缩小到工具表面(文件 I/O, run, (中文(简体) ), spawn),仅与下行位相接相接的取景ports, (中文(简体) ), getPortEndpoint, (中文(简体) ), setNetworkPolicy, (中文(简体) ), stop已删除。 这就是安全界限: 密码你手持限制视图 无法阻止盒子或改变它的网络政策。 传到AI SDK工具的 execute() 通过 experimental_sandbox整个会话都用在绳子上 (见 调制文件 联 合 国 restricted() 合同。 )
设置
所有东西都用在你传递给的对象上 createE2BSandbox(...)数字 :
const e2bSandbox = createE2BSandbox({
template: 'base', // any E2B SandboxOpts
envs: { NODE_ENV: 'production' },
timeoutMs: 10 * 60 * 1000, // optional; defaults to 30 min
ports: [3000], // provider option
setupCommands: ['sudo npm install -g pnpm@9'], // provider option
});
任何E2B级的 [SandboxOpts](https://e2b.dev/docs) (单位:千美元)template, (中文(简体) ), envs, (中文(简体) ), metadata, (中文(简体) ), network,等等)直接通过. 提供商又增加了两个不属于E2B的SDK的选项:
| 选项 | 默认 | 它会做什么 |
|---|---|---|
ports | [] | 张贴广告的端口 session.ports吊带桥连接着第一个 E2B 可以通过 getHost,所以这真的是 只是它广告的列表. |
setupCommands | [] | 命令运行一次 在一个新鲜的沙盒, 在绳子靴子之前。 譬如说, ['sudo npm install -g pnpm@9'] 用于 claude-code/codex 适配器. |
有沙盒吗? 通过它作为 sandbox 用于重用(当您想要在会话中分享一次时会很困难)。 供应商不会碰它的生命周期,所以 stop() 财务报告和审定财务报表 destroy() 成为无私之道 清理工作仍属于你.
import { createE2BSandbox } from '@e2b/ai-sdk-sandbox';
import { Sandbox } from 'e2b';
const e2bSandbox = createE2BSandbox({
sandbox: await Sandbox.create({ template: 'base' }),
});
中期网络政策
你可以在已经运行的沙盒上收紧或放松出入口:
await sandboxSession.setNetworkPolicy?.({
mode: 'custom',
allowedHosts: ['api.example.com'],
deniedCIDRs: ['169.254.169.254/32'],
});
要求的转变和证书中介
setRequestTransformations() 替换管理的规则,
addRequestTransformations() 添加时不替换。 标题在
transform.headers 请求后由 E2B 的 Egress 代理注入
离开沙盒,所以真正的证书 从来没有进入它—— 操纵适配器
3⁄4 ̄ ̧漯B E2B 按主机匹配规则 :
(单位:千美元) path 匹配器在整个主机中应用,以及 method编号 :queryString编号 :headers
匹配者被拒绝,而不是沉默地扩大。 E2B 允许一次变换
每个主机规则,所以对同一个主机的多个转换被合并到
一条规则( 后发头优先于前发头) 。 规则匹配主机
准确性(规则 example.com 不涵盖 www.example.com)和
egress代理只转换了HTTPS请求. 网络政策依然存在
权威可以联系到哪些主机 。 见
examples/request-transformations.ts 现场检查.
运行代理( Claude Code, Codex)
const agent = new HarnessAgent({
harness: createClaudeCode(),
sandbox: createE2BSandbox({
template: 'pnpm-base', // base image + pnpm + 2GB, built via build-template.ts
ports: [4000],
}),
});
关于模板的一件事是: Claude-code 适配器安装了自己的被钉住的CLI并用pnpm(它没有使用系统)在沙盒内架起桥来 claude) (中文(简体) ). 安装 Claude-code 的 ~ 238 MB 本地二进制, 和 pnpm 搭载它 峰值在 1. 5 GB 左右—— 所以沙盒需要 pnpm 财务报告和审定财务报表 E2B RAM 的 ~ 2 GB 。 E2B RAM 固定在模板构建时间( 你不能每个沙盒设置它) , 所以用 pnpm 烤入的 pnpm 构建 2 GB 模板 examples/build-template.ts 并传其名. 一个完整的,可运行的版本生活在 examples/harness.ts。 。 。 .
很高兴知道
一些E2B特定的行为值得了解:
stop()暂停 沙盒,你待会再拿回来resumeSession。 。 。 .destroy()永远杀死它.- 继续使用沙盒元数据中的会话编号标记(E2B指定了编号,所以没有名字可以抬头),所以通过一个
sessionId改为createSession如果你打算继续. run财务报告和审定财务报表spawn关闭 E2B 的 60 秒/ 命令超时, 这样长的构建和背景服务器不会被切断 。 整个沙盒timeoutMs仍然适用.- 当绳子穿过时
identity+ 键onFirstCreate(例如: claude-code/codex靴子),设置运行 一次:它被俘虏为快照并被后会分叉从中取出,而不是重新运行. 该快照通过名称匹配listSnapshots,所以即使冷开始再用它。 对于提前控制的设置,预建的模板会更轻.
实例
examples/basic.ts:会话表面自行(写出、运行、产卵)examples/harness.ts: Claude 代码在 E2B 沙盒内工作examples/resume.ts暂停一个沙盒,把它捡起来examples/build-template.ts: 构建自定义模板( bakes pnpm in) 以通过template
运行其中任何一个 npm run example:basic (他们读 .env) (中文(简体) ).
许可证
麻省理工学院