Communitygithub.com

cloudflare/sandbox-next

构建或维护 Cloudflare Sandbox 应用,基于 @cloudflare/sandbox@next 版本(SDK 1.0 预览版)。如需移植稳定版应用,请使用 sandbox-migrate-to-next。

sandbox-next 是什么?

这个技能用于在 Cloudflare Containers 上使用 SDK 1.0 预览版(@cloudflare/sandbox@next)构建和运维沙箱应用。它提供了一套严格的合约和检索指南,确保开发者正确使用新一代 API,例如 sandbox.exec 返回进程句柄而非直接结果、无隐式 shell、进程间环境隔离等。该技能支持 Workers、Wrangler 以及自定义 Dockerfile,并涵盖进程执行、交互式终端、环境变量、文件存储、端口隧道、错误处理等核心功能。适用于新项目开发,而稳定版应用应使用 sandbox-stable 技能,迁移操作则通过 sandbox-migrate-to-next 完成。开发者必须遵循包线(@next tag)与容器镜像一致的原则,并在部署前进行类型检查、锁文件验证,以及确保不将敏感凭据存入沙箱环境。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/cloudflare/skills/tree/main/skills/sandbox-next

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

Sandbox SDK — @next (1.0 预览版)

在 Cloudflare Containers 上运行的隔离 Linux 环境,由 Workers 驱动。

优先使用预览文档和已安装的 @next 类型,而非记忆。 API 会变化;此技能是一个门控、一份合约以及一张检索地图——而不是完整手册。

我们建议 新项目 使用此版本。仍使用默认包的应用请用 sandbox-stable。仅在收到明确指令时,通过 sandbox-migrate-to-next 进行移植。

1. 门控 — 确认包线

在编写代码前,请检查应用:

检查项必须匹配
npm 依赖@cloudflare/sandbox@next(或其他预览标签)
容器镜像同一行(如 cloudflare/sandbox:next、next-python)
如果发现…操作
默认 @cloudflare/sandbox(无 @next)停止。 加载 sandbox-stable。不要应用此技能的 API。
用户希望从稳定版移植到 @next停止。 加载 sandbox-migrate-to-next。
仅自部署的 bridgeBridge 尚未 在 1.0 预览版线上。请将 bridge 保留在稳定版包和镜像上。Bridge(稳定版)

切勿将 @next 的 Worker 包与稳定版的容器镜像混合使用(反之亦然)。

技能安装:Agent 设置 · cloudflare/skills

2. 合约 — 不可妥协的规则

  • sandbox.exec(argv) 接受一个 argv 列表,并在进程 启动 时解析。它返回一个 句柄,而非命令的最终结果。
  • 通过句柄方法收集结果:output()、logs()、waitForExit()、waitForPort()、waitForLog()、kill(signal?)。
  • 无隐式 shell。Shell 语法需要显式指定 shell,例如 ["/bin/bash", "-lc", script]。
  • 每次启动相互独立。一次 exec 中的 cd / export 对下一次不可见。请每次启动时传递 cwd 和 env,或者使用单个 shell 脚本。
  • 进程句柄 没有标准输入。交互式使用 → 终端(createTerminal + connect)。
  • 本地等待的 timeout / AbortSignal 仅取消 等待操作。它们不会终止进程。请使用 kill 或 exec 的远程 timeout。
  • getProcess / listProcesses / getTerminal / listTerminals 不会 启动容器;当没有活动容器时返回 null / []。
  • 进程和终端 ID 属于 当前容器,而非永久与某个沙箱 ID 绑定。对于需要持久化的工作,请存储完整的任务(argv、cwd、env、应用状态)——而不仅仅是 ID。
  • 仅在 setEnvVars / 启动 env 中放置非机密配置。实时凭据保留在 Worker 中;当沙箱调用外部 API 时使用出站处理程序。
  • 不要 创造已移除的稳定版 API(核心上的 gitCheckout、字符串式 exec 完成、会话执行、sandbox.terminal(request))。
  • 不要 对每个错误都使用一个重试循环(参见错误文档)。

最小示例:

import { getSandbox, proxyToSandbox, Sandbox } from "@cloudflare/sandbox";

export { Sandbox };

const sandbox = getSandbox(env.Sandbox, "user-123");
const process = await sandbox.exec(["python3", "-c", "print(2 + 2)"]);
const result = await process.output({ encoding: "utf8" });
// result.stdout, result.exitCode

任务特定的 API 文档:references/api-quick-ref.md

示例索引(next 分支):references/examples.md

3. 检索 — 为任务打开文档

在实现之前获取页面。已安装的 @next 类型优先于猜测。

你需要…打开
定位/选择预览版1.0 预览版概述
第一个 Worker、wrangler、Dockerfile开始使用
exec、句柄、就绪性、持久性进程执行
进程 API 签名进程 API
沙箱 ID vs 容器 vs 休眠/销毁生命周期
cwd / env / setEnvVars环境
交互式 PTY / 浏览器终端终端 · 终端 API
Python/JS 代码解释器解释器 · 解释器 API
扩展模型扩展
错误类与恢复错误 · 错误 API
常见故障故障排除
API 中心API 参考
文件、挂载、备份、端口、隧道、proxyToSandbox共享区域的主文档(忽略仅稳定版的 session/transport/sandbox.terminal):文件 · 存储/挂载 · 端口 · 隧道 · 备份 · 出站流量 · 暴露服务 · 生产部署
示例应用next 上的示例
仍在使用稳定版包sandbox-stable · Sandbox 主文档
移植现有稳定版应用sandbox-migrate-to-next · 迁移指南

4. 发布前检查

  • 锁文件与 Dockerfile 在同一 @next 线上
  • 使用已安装的 @next 类型进行类型检查
  • 沙箱环境中不含实时机密信息
  • 使用自定义域名的生产预览主机名需要通配符 DNS(如果使用此类 URL 模式)

Individual skills in this repo

This repo contains 15 individual skills — each has its own dedicated page.

cloudflare/agents-sdk

Build AI agents on Cloudflare Workers using the Agents SDK. Load when creating stateful agents, durable workflows, real-time WebSocket apps, scheduled tasks, MCP servers, chat applications, voice agents, or browser automation. Covers Agent class, state management, callable RPC, Workflows, durable execution, queues, retries, observability, and React hooks. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/building-ai-agent-on-cloudflare

Builds AI agents on Cloudflare using the Agents SDK with state management, real-time WebSockets, scheduled tasks, tool integration, and chat capabilities. Generates production-ready agent code deployed to Workers. Use when: user wants to "build an agent", "AI agent", "chat agent", "stateful agent", mentions "Agents SDK", needs "real-time AI", "WebSocket AI", or asks about agent "state management", "scheduled tasks", or "tool calling". Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/building-mcp-server-on-cloudflare

Builds remote MCP (Model Context Protocol) servers on Cloudflare Workers with tools, OAuth authentication, and production deployment. Generates server code, configures auth providers, and deploys to Workers. Use when: user wants to "build MCP server", "create MCP tools", "remote MCP", "deploy MCP", add "OAuth to MCP", or mentions Model Context Protocol on Cloudflare. Also triggers on "MCP authentication" or "MCP deployment". Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/cloudflare

Comprehensive Cloudflare platform skill covering Workers, Pages, storage (KV, D1, R2), AI (Workers AI, Vectorize, Agents SDK), feature flags (Flagship), networking (Tunnel, Spectrum), security (WAF, DDoS), and infrastructure-as-code (Terraform, Pulumi). Use for any Cloudflare development task. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/cloudflare-email-service

Send and receive transactional emails with Cloudflare Email Service (Email Sending + Email Routing). Use when building email sending (Workers binding or REST API), email routing, Agents SDK email handling, or integrating email into any app — Workers, Node.js, Python, Go, etc. Also use for email deliverability, SPF/DKIM/DMARC, wrangler email setup, MCP email tools, or when a coding agent needs to send emails. Even for simple requests like "add email to my Worker" — this skill has critical config details.

cloudflare/cloudflare-one

Design, configure, troubleshoot, or review Cloudflare One Zero Trust and SASE deployments. Use cloudflare-one-migrations for migration planning from other vendors.

cloudflare/durable-objects

Create and review Cloudflare Durable Objects. Use when building stateful coordination (chat rooms, multiplayer games, booking systems), implementing RPC methods, SQLite storage, alarms, WebSockets, or reviewing DO code for best practices. Covers Workers integration, wrangler config, and testing with Vitest. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/sandbox-migrate-to-next

Migrate Cloudflare Sandbox apps from stable @cloudflare/sandbox to @cloudflare/sandbox@next (SDK 1.0 preview). Use sandbox-next for apps already on the preview.

cloudflare/sandbox-sdk

Build sandboxed applications for secure code execution. Load when building AI code execution, code interpreters, CI/CD systems, interactive dev environments, or executing untrusted code. Covers Sandbox SDK lifecycle, commands, files, code interpreter, and preview URLs. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/sandbox-stable

Build or maintain Cloudflare Sandbox apps on the stable @cloudflare/sandbox package. Use sandbox-next for preview apps and sandbox-migrate-to-next for stable-to-preview migrations.

cloudflare/skills

Build, migrate, and deploy Next.js apps on Cloudflare Workers with vinext. Use when starting a Next.js project on Cloudflare, moving an existing app to Workers, choosing between vinext and OpenNext, or setting up vinext for Workers. For setup, migration, or deployment, install vinext's upstream skills with `npx skills add cloudflare/vinext` if missing, then read and follow the applicable skill and docs.

cloudflare/turnstile-spin

Set up, repair, or migrate to Cloudflare Turnstile bot verification in an existing frontend and backend, including server-side Siteverify.

cloudflare/web-perf

Analyzes web performance using Chrome DevTools MCP. Measures Core Web Vitals (LCP, INP, CLS) and supplementary metrics (FCP, TBT, Speed Index), identifies render-blocking resources, network dependency chains, layout shifts, caching issues, and accessibility gaps. Use when asked to audit, profile, debug, or optimize page load performance, Lighthouse scores, or site speed. Biases towards retrieval from current documentation over pre-trained knowledge.

cloudflare/workers-best-practices

Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

cloudflare/wrangler

Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

相关技能