边缘开发者平台
  • Makers
    • 产品简介
    • 快速开始
      • Agent 开发
      • 导入 Git 仓库
      • 从模板开始
      • 直接上传
      • 从 AI 开始
    • 框架指南
      • Agent
      • 前端
        • Vite
        • React
        • Vue
        • Hugo
        • 其他框架
      • 后端
      • 全栈
        • Next.js
        • Nuxt
        • Astro
        • React Router
        • SvelteKit
        • TanStack Start
        • Vike
      • 自定义 404 页面
    • 项目指南
      • 项目管理
      • edgeone.json
      • 缓存配置
      • 构建输出配置
      • 错误码
    • 构建指南
    • 部署指南
      • 概览
      • 触发部署
      • 管理部署
      • 部署按钮
      • 使用 Github Action
      • 使用 Gitlab CI/CD
      • 使用 CNB 插件
      • 使用 IDE 插件
      • 使用 CodeBuddy IDE
    • 域名管理
      • 概览
      • 自定义域名
      • 配置 HTTPS 证书
        • 概览
        • 申请免费证书
        • 使用 SSL 托管证书
      • 配置 DNS 的 CNAME 记录
    • 可观测性
      • 概览
      • 指标分析
      • 日志分析
    • Functions
      • 概览
      • Edge Functions
      • Cloud Functions
        • 概览
        • Node.js
        • Python
        • Go
    • Agents
      • 概览
      • 快速开始
      • 对话存储
      • 可观测
      • 沙箱工具
        • 概览
        • Agent 框架使用
        • 沙箱原子 API
        • 网络搜索工具
      • Agent 鉴权
    • Models
      • 概览
      • 模型与厂商
        • 概览
        • 使用厂商密钥
          • OpenAI
          • Anthropic
          • Google AI Studio
          • DeepSeek
          • MiniMax
          • 混元
          • 智谱
          • 月之暗面
      • 常见问题
    • 存储
      • 概览
      • KV
      • Blob
    • 中间件
    • AI 原生开发
      • Skills
      • MCP
    • Copilot
      • 概览
      • 快速开始
    • API Token
    • EdgeOne CLI
    • 消息通知
    • 集成指南
      • AI
        • 对话型大模型集成
        • 图片大模型集成
      • 数据库
        • Supabase 集成
        • Pages KV 集成
      • 电商
        • Shopify 集成
        • WooCommerce 集成
      • 支付
        • Stripe 集成
        • Paddle 集成
      • CMS
        • WordPress 集成
        • Contentful 集成
        • Sanity 集成
        • Payload 集成
      • 身份验证
        • Supabase 集成
        • Clerk 集成
    • 最佳实践
      • 为网站添加 AI 对话助手
      • AI 对话式部署:使用 Skill 一句话部署项目
      • 使用通用大模型快速搭建 AI 应用
      • 使用边缘 AI 模型快速搭建对话型 AI 站点
      • 使用 Shopify 搭建电商平台
      • 使用 Supabase 和 Stripe 搭建 SaaS 站点
      • 如何快速搭建公司品牌站点
      • 如何快速搭建博客站点
    • 迁移指南
      • 从 Vercel 迁移至 EdgeOne Makers
      • 从 Cloudflare Pages 迁移至 EdgeOne Makers
      • 从 Netlify 迁移至 EdgeOne Makers
    • 排障指南
    • 常见问题
    • 限制与配额
    • 价格与套餐
    • 联系我们
    • 产品动态

沙箱原子 API

context.sandbox.* 是开发者直接调用的视角,可以基于沙箱原子 API 自己封装工具。

commands

在沙箱中执行一次性 shell 命令,每次调用都是独立新进程(不保留进程状态、不保留 cwd、不保留环境变量改动;如需变量跨调用保留,用 runCode)。
commands.run(cmd, opts?) — 执行 shell 命令。opts 支持 cwd / env / user / timeout(秒);返回 { stdout, stderr, exitCode }
TS 示例:
const { stdout, exitCode } = await context.sandbox.commands.run(
'pip install requests && python -c "import requests; print(requests.__version__)"',
{ timeout: 60 },
)
Python 示例:
result = await context.sandbox.commands.run(
'pip install requests && python -c "import requests; print(requests.__version__)"',
timeout=60,
)

files

读写沙箱实例内的文件系统。文本场景直接用,二进制资产(如 .png )建议先用 commands 在沙箱内下载或解码,再通过其他通道读出(比如上传到对象存储后给前端 URL)。
files.read(path) — 读取文本文件内容。
files.write(path, content) — 写入文本文件(不存在则创建,存在则覆盖)。
files.list(path) — 列出目录,返回 EntryInfo[]{ name, type, path })。
files.makeDir(path) / files.make_dir(path) — 创建目录。
files.exists(path) — 判断文件或目录是否存在。
files.remove(path) — 删除文件或目录。
TS 示例:
await context.sandbox.files.makeDir('/work/output')
await context.sandbox.files.write('/work/output/result.json', JSON.stringify(data))
const entries = await context.sandbox.files.list('/work/output')
Python 示例:
await context.sandbox.files.make_dir('/work/output')
await context.sandbox.files.write('/work/output/result.json', json.dumps(data))
entries = await context.sandbox.files.list('/work/output')

browser

通过 CDP 连接沙箱内置 Chromium(底层 Playwright),适合需要状态保持的多步浏览器流程,如登录后操作、表单交互、动态页面截图。
browser.cdpUrl — CDP 连接地址,可被外部 Playwright 直连。
browser.liveUrl — NoVNC 可视化地址,供人工实时查看页面。
browser.goto(url) — 导航到目标 URL,返回 { url, title, status }
browser.screenshot(fullPage?) — 截图返回 { base64Image }
browser.click(selector) — 点击元素。
browser.type(selector, text) — 在输入框键入文本。
browser.evaluate(script) — 在页面上下文执行 JavaScript。
browser.getContent() / browser.get_content() — 获取当前页面 HTML,返回 { content }
browser.close() — 关闭浏览器连接。
TS 示例:
// 登录后再截图(LLM 工具单独调 browser_screenshot 做不到)
await context.sandbox.browser.goto('https://example.com/login')
await context.sandbox.browser.type('input[name=user]', 'alice')
await context.sandbox.browser.type('input[name=pass]', 'secret')
await context.sandbox.browser.click('button[type=submit]')
await context.sandbox.browser.goto('https://example.com/dashboard')
const { base64Image } = await context.sandbox.browser.screenshot({fullPage: true})
Python 示例:
await context.sandbox.browser.goto('https://example.com/login')
await context.sandbox.browser.type('input[name=user]', 'alice')
await context.sandbox.browser.type('input[name=pass]', 'secret')
await context.sandbox.browser.click('button[type=submit]')
await context.sandbox.browser.goto('https://example.com/dashboard')
shot = await context.sandbox.browser.screenshot(full_page=True)

runCode

在沙箱内置 Jupyter kernel 中执行代码,变量跨调用保留
runCode(code, opts?) / run_code(...) — 执行代码。opts 支持 language(默认 python,也支持 javascript / r / bash)、timeout(秒);返回 Execution { results, logs, error }
TS 示例:
// 用 Python 处理 CSV 并生成图片
const exec = await context.sandbox.runCode(`
import pandas as pd
df = pd.read_csv('/work/data.csv')
df.plot().get_figure().savefig('/work/out.png')
print('done')
`, { language: 'python' })
python 示例:
exec = await context.sandbox.run_code(
"""
import pandas as pd
df = pd.read_csv('/work/data.csv')
df.plot().get_figure().savefig('/work/out.png')
print('done')
""",
language='python',
)