How It Works
Agent 循环:从指令到完成
Core Capabilities
六大核心能力
完整 Linux 桌面
Ubuntu 22.04 环境,XFCE 桌面,预装浏览器、编辑器等常用应用
鼠标 & 键盘 API
点击、双击、右键、拖拽、输入文字、按键组合,覆盖所有桌面交互
屏幕截图
实时捕获桌面状态,返回 Buffer 数据供 LLM 视觉模型分析
终端命令执行
直接运行 shell 命令,安装软件、配置环境、处理文件
VNC 实时流
浏览器中实时查看 Agent 操作过程,调试与监控一目了然
安全沙箱
完全隔离的云环境,Agent 操作不会影响主机系统
Implementation
核心代码实现
安装 SDK
npm i @e2b/desktop创建沙箱
typescript
import { Sandbox } from '@e2b/desktop'
// 创建桌面沙箱,5 分钟超时
const sandbox = await Sandbox.create({
resolution: [1024, 720],
dpi: 96,
timeoutMs: 300_000,
})
// 启动 VNC 流,浏览器可实时查看
await sandbox.stream.start()
const streamUrl = sandbox.stream.getUrl()
console.log('桌面地址:', streamUrl)执行桌面操作
typescript
const sandbox = await Sandbox.create({ timeoutMs: 300_000 })
// 鼠标操作
await sandbox.leftClick(500, 300)
await sandbox.rightClick(500, 300)
await sandbox.doubleClick(500, 300)
await sandbox.moveMouse(500, 300)
await sandbox.drag([100, 200], [400, 500])
// 键盘操作
await sandbox.write('Hello, world!')
await sandbox.press('Enter')
// 滚动
await sandbox.scroll('down', 3)
// 截图
const screenshot = await sandbox.screenshot()
// 终端命令
await sandbox.commands.run('ls -la /home')Agent 循环
typescript
const sandbox = await Sandbox.create({
resolution: [1024, 720],
timeoutMs: 300_000,
})
await sandbox.stream.start()
while (true) {
// 1. 捕获当前桌面状态
const screenshot = await sandbox.screenshot()
// 2. 发送截图给 LLM,获取下一步操作
const action = await getNextActionFromLLM(screenshot)
if (!action) break // LLM 表示任务完成
// 3. 在桌面上执行操作
switch (action.type) {
case 'click':
await sandbox.leftClick(action.x, action.y)
break
case 'type':
await sandbox.write(action.text)
break
case 'keypress':
await sandbox.press(action.keys)
break
case 'scroll':
await sandbox.scroll(
action.scrollY < 0 ? 'up' : 'down',
Math.abs(action.scrollY)
)
break
case 'drag':
await sandbox.drag(
[action.startX, action.startY],
[action.endX, action.endY]
)
break
}
}
await sandbox.kill()API Reference
支持的桌面操作
鼠标操作
leftClick(x, y)左键点击rightClick(x, y)右键点击doubleClick(x, y)双击middleClick(x, y)中键点击moveMouse(x, y)移动鼠标drag(start, end)拖拽键盘操作
write('text')输入文字press('Enter')按键 / 组合键scroll('down', n)向下滚动 n 格scroll('up', n)向上滚动 n 格screenshot()截图 (返回 Buffer)commands.run('cmd')执行终端命令