本文介绍如何将无影 AgentBay 迁移到 FC Agent Sandbox e2b-desktop,包括能力映射、Desktop 模板构建与 noVNC 集成。
能力映射说明
方法名与签名以 e2b-desktop SDK 发布为准。部分能力通过沙箱内 shell 命令补齐,在下文表格中标注为shell 对齐。
无影 AgentBay |
| 说明 |
鼠标 | ||
|
| 直接传坐标; |
|
| 同上。 |
|
| 同名。 |
|
| 签名不同,元组参数。 |
|
| 同名。 |
|
| 无坐标参数,先移动再滚动。 |
|
| shell 对齐。 |
键盘 | ||
|
| 方法名不同。 |
|
| 单个键或组合键列表。 |
|
| shell 对齐。 |
截图 | ||
|
| 同名,PNG 字节。 |
|
| shell 对齐(scrot 支持 jpg/png;jpg 必须显式按 bytes 读)。 |
屏幕信息 | ||
|
| shell 对齐。 |
|
| 同名。 |
窗口管理 | ||
|
| shell 对齐。 |
|
| shell 对齐。 |
|
| shell 对齐。 |
|
| shell 对齐。 |
|
| shell 对齐(终端类应用按字符单元格取整)。 |
|
| shell 对齐。 |
|
| shell 对齐。 |
应用管理 | ||
|
| shell 对齐( |
|
| shell 对齐。 |
|
| shell 对齐。 |
|
| shell 对齐。 |
Agent(差异项) | ||
| 无 | C-1:暂不支持。 |
| 无 | C-2:暂不支持。 |
| 无 | C-3:暂不支持。 |
桌面观察 | ||
|
| 内置鉴权,请参见VNC 实时流。 |
C-1~C-3(自然语言 Agent)暂无提供计划,不在 FC Agent Sandbox 产品设计范畴内;其余能力均可通过 SDK 或 shell 命令对齐。
无需使用 Computer Use 的场景:仅操作网页时优先使用 Browser Use(CDP,Chrome DevTools Protocol);纯批处理场景使用 commands/code。
以上映射已使用
e2b-desktop==2.4.1+desktop:v0.0.44模板逐项实跑验证通过(29/29)。模板已预装
xdotool/scrot/xdpyinfo/xprop/xwininfo/xfconf-query,未预装wmctrl。窗口管理可使用上表中的 xdotool/xprop 等价命令;如需 wmctrl 语义,可执行apt-get install -y wmctrl安装,沙箱可访问 Debian 源。WebSDK 集成章节的前端示例已在真实 Chrome 中验证(RFB 连接、画面渲染、桌面变化实时刷新均通过)。
使用限制
Linux 桌面(Computer Use)迁移采用 e2b-desktop SDK:
鼠标 / 键盘 / 截图为已有的 SDK 方法(两步式);VNC 实时流内置且带鉴权。
必须使用其
Sandbox子类创建沙箱,沙箱生命周期语义与 e2b 一致(仍按create/connect/list/kill使用)。窗口 / 应用管理等超集能力不在本 SDK 范围,请参见能力映射说明。
内置模板映射
AgentBay | FC E2B | 说明 |
| desktop 模板(内测镜像 | FC 镜像已配置桌面环境,通过 |
前置准备:Desktop 模板
Desktop 模板不是开通即用的内置模板,需从 FC Desktop 镜像构建。镜像已内置 Xvfb + xfce4 + x11vnc + websockify + Chrome,无需自行编写 Dockerfile。
设置环境变量
export E2B_API_KEY=<YOUR-API-KEY>
export E2B_API_URL=https://api.cn-beijing.e2b.fc.aliyuncs.com
export E2B_DOMAIN=cn-beijing.e2b.fc.aliyuncs.com构建模板
安装依赖。
pip install "e2b>=2.31.0" e2b-desktop==2.4.1 python-dotenv将以下内容保存为
build.py。# build.py from dotenv import load_dotenv from e2b import Template, default_build_logger load_dotenv() # 这是当前的稳定版本,后续会更新 FROM_IMAGE = "fc-e2b-registry.cn-beijing.cr.aliyuncs.com/runtime/desktop:v0.0.44" build = Template.build( Template().from_image(FROM_IMAGE), name="my-desktop-template", # 这个可以任意设置 cpu_count=4, memory_mb=8192, on_build_logs=default_build_logger(), ) print(f"template_id: {build.template_id}")执行构建。
python build.py
镜像仓库地域需与
E2B_API_URL/E2B_DOMAIN一致。其他地域替换镜像地址中的cn-beijing。推荐规格:4 vCPU、8192 MB 内存。模板无需配置启动命令或就绪命令,Desktop SDK 会自动启动桌面环境和实时流。
主要迁移示例
常见交互循环(截图+action)
# === 迁移前:AgentBay ===
session = agent_bay.create(CreateSessionParams(image_id="linux_latest")).session
session.computer.move_mouse(640, 400)
session.computer.click_mouse(640, 400)
session.computer.input_text("echo hello")
session.computer.press_keys(["enter"])
png = session.computer.screenshot()
# === 迁移后:e2b-desktop SDK ===
from e2b_desktop import Sandbox
desktop = Sandbox.create(template="my-desktop-template", timeout=600)
try:
# 启动 Chrome 并等待渲染
desktop.launch("google-chrome", "https://example.com")
desktop.wait(10000)
# 鼠标:E2B SDK 为两步式(move + click);拖拽用 drag 元组
desktop.move_mouse(640, 400)
desktop.left_click()
desktop.double_click()
desktop.drag((100, 100), (300, 300))
desktop.scroll("down", 5)
# 键盘:write 输入文本,press 单键或组合键
desktop.launch("xfce4-terminal")
desktop.wait(3000)
desktop.write("echo hello")
desktop.press("enter")
desktop.press(["ctrl", "c"])
# 截图(PNG 字节,交给视觉模型)
image = desktop.screenshot()
with open("desktop.png", "wb") as f:
f.write(image)
finally:
desktop.kill()VNC 实时流(人工接管)
# 对应无影桌面场景的 link_url 观察方式;E2B SDK stream 内置鉴权
desktop.stream.start()
stream_url = desktop.stream.get_url()
# 将 stream_url 交给受控后端 / 浏览器 noVNC
desktop.stream.stop()WebSDK 集成
浏览器端通过 noVNC(CDN JS)直连沙箱 VNC 实时流,无需控制面 SDK 或 npm 构建。沙箱生命周期由后端(Python)管理,前端仅负责桌面渲染。
后端:获取 VNC 流地址
# 后端 Python:创建沙箱并获取 noVNC 流地址
from e2b_desktop import Sandbox
desktop = Sandbox.create(template="my-desktop-template", timeout=600)
desktop.stream.start()
stream_url = desktop.stream.get_url()
# 若 FC E2B 侧对数据面要求 traffic access token,通过 query 参数携带
# token = getattr(desktop, "traffic_access_token", None)
# if token:
# stream_url += f"&traffic_access_token={token}"
# 将 stream_url 返回给前端(如通过 API 响应或 WebSocket)前端:HTML + CDN noVNC
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>FC Desktop</title>
<style>
#vnc-screen { width: 100vw; height: 100vh; }
</style>
</head>
<body>
<div id="vnc-screen"></div>
<script type="module">
// ESM 引入 noVNC 官方构建(固定版本)
import RFB from 'https://cdn.jsdelivr.net/gh/novnc/noVNC@v1.6.0/core/rfb.js';
async function initVNC() {
// 从后端 API 获取 stream_url
const resp = await fetch('/api/sandbox/vnc-url');
const { streamUrl } = await resp.json();
const rfb = new RFB(
document.getElementById('vnc-screen'),
streamUrl, // https://{sandbox-host}/vnc.html?autoconnect=true&resize=scale
// 同一 URL 双重角色:浏览器直接打开是该 noVNC 页面文件;RFB 会把它升级为
// wss://{sandbox-host}/vnc.html 作为 WebSocket 端点(由 websockify 代理到 x11vnc)
{ credentials: { password: '' } } // stream.start() 未开鉴权时为空
);
rfb.scaleViewport = true;
rfb.resizeSession = false;
}
initVNC();
</script>
</body>
</html>浏览器直连
wss://{sandbox-host}/vnc.html无需自定义请求头(实测 GET 200、RFB 握手成功);VNC 鉴权由stream.start(require_auth=True)+get_url(auth_key=...)控制,开启后把auth_key一并返回前端并传入credentials.password。数据面如需 traffic access token,FC 后端支持以 query 参数方式传递(由后端拼进
stream_url返回,请参见后端:获取 VNC 流地址),浏览器 WebSocket 无需自定义头,也无需中继服务;noVNC 对额外的 query 参数透明(已随password=参数实测)。前端无需自研 Web SDK:noVNC(CDN ESM)即完整数据面客户端,连接参数全部在 URL 中。
Context 存储
FC Agent Sandbox 以组合式能力提供存储服务,可通过 AgentFS 或 OSS 实现数据持久化和共享:
附录:noVNC 集成 demo 示例
以下示例提供一个可本地运行的 noVNC 集成 demo,包含三个文件:后端 demo.py、前端 index.html 和凭证配置 .env。沙箱生命周期与凭证由后端管理,前端仅负责桌面渲染。
示例文件
.env 文件填入真实值后即含密钥,请勿提交代码库、分享或粘贴至聊天和工单。
数据流
浏览器 (index.html, noVNC CDN ESM)
│ GET /api/sandbox/vnc-url ──→ demo.py(仅返回 streamUrl)
│ RFB 直连 wss://{sandbox-host}/vnc.html ──→ websockify ──→ x11vnc ──→ Xvfb 桌面
└─ 渲染 + 键鼠输入(数据面不经后端)运行方法
安装依赖。
pip install "e2b>=2.31.0" e2b-desktop==2.4.1 python-dotenv在
.env中填入E2B_API_KEY等凭证的真实值。运行 demo。
python demo.py在浏览器打开终端输出的服务地址(形如
http://127.0.0.1:<RANDOM-PORT>/),即可查看并操作实时桌面。按下 Ctrl-C 退出,程序自动停止 VNC 流并销毁沙箱。
验证记录
后端 API:
/api/sandbox/vnc-url返回以https://开头且含/vnc.html的 streamUrl;/正常返回页面。退出:SIGTERM/Ctrl-C 后自动停止 VNC 流并销毁沙箱。
前端:已在真实 Chrome 中验证 RFB 连接、画面渲染和桌面变化实时刷新。