Communitygithub.com

vercel-labs/vercel-sandbox

在 Vercel Sandbox 微型虚拟机中运行 agent-browser 和 Chrome,实现来自任何 Vercel 部署应用的浏览器自动化。

vercel-sandbox 是什么?

此技能允许 AI 代理在 Vercel 托管的临时 Sandbox 微型虚拟机中执行浏览器自动化任务。它利用 `@agent-browser/sandbox` 和 `@vercel/sandbox` 库在 Next.js、SvelteKit、Nuxt 等框架中无缝集成。核心优势在于能够启动无头 Chrome 浏览器,执行截屏、无障碍快照以及多步骤工作流(如表单填写)等操作,且无需担心二进制大小限制。通过使用 Vercel Sandbox 快照(类似于预先配置好的 VM 磁盘映像),首次启动时间可从约 30 秒缩短至亚秒级。该技能还支持通过环境变量进行身份验证(在 Vercel 部署中自动通过 OIDC 完成),并可结合 Vercel Cron Jobs 执行定时任务,非常适合网页监控、数据抓取或定期截图等场景。

兼容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/vercel-labs/agent-browser/tree/main/skill-data/vercel-sandbox

在你喜欢的 AI 中提问

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

文档

使用 Vercel Sandbox 进行浏览器自动化

在临时的 Vercel Sandbox 微型虚拟机中运行 agent-browser + 无头 Chrome。按需启动 Linux 虚拟机,执行浏览器命令,然后关闭。适用于任何基于 Vercel 部署的框架(Next.js、SvelteKit、Nuxt、Remix、Astro 等)。

依赖

pnpm add @agent-browser/sandbox @vercel/sandbox

Sandbox 虚拟机需要 Chromium 的系统依赖以及 agent-browser 本身。@agent-browser/sandbox 辅助函数默认为全新的 Sandbox 安装这些依赖,并使用 Sandbox 快照(见下文)实现亚秒级启动。仅当 Sandbox 镜像已提供 Chromium 所需的库时,才传递 installSystemDependencies: false。

核心模式

import {
  createAgentBrowserSnapshot,
  runAgentBrowserCommand,
  withAgentBrowserSandbox,
  type VercelSandboxSession,
} from "@agent-browser/sandbox/vercel";

async function withBrowser<T>(
  fn: (sandbox: VercelSandboxSession) => Promise<T>,
): Promise<T> {
  return withAgentBrowserSandbox(fn);
}

截图

screenshot --json 命令将截图保存到文件并返回路径。以 base64 格式读取文件:

export async function screenshotUrl(url: string) {
  return withBrowser(async (sandbox) => {
    await runAgentBrowserCommand(sandbox, ["open", url]);

    const titleResult = await runAgentBrowserCommand<{ data?: { title?: string } }>(sandbox, [
      "get", "title",
    ]);
    const title = titleResult.json?.data?.title || url;

    const ssResult = await runAgentBrowserCommand<{ data?: { path?: string } }>(sandbox, [
      "screenshot",
    ]);
    const ssPath = ssResult.json?.data?.path;
    if (!ssPath) throw new Error("Screenshot did not return a file path.");
    const b64Result = await sandbox.runCommand("base64", ["-w", "0", ssPath]);
    const screenshot = (await b64Result.stdout()).trim();

    await runAgentBrowserCommand(sandbox, ["close"], { json: false });

    return { title, screenshot };
  });
}

无障碍快照

export async function snapshotUrl(url: string) {
  return withBrowser(async (sandbox) => {
    await runAgentBrowserCommand(sandbox, ["open", url]);

    const titleResult = await runAgentBrowserCommand<{ data?: { title?: string } }>(sandbox, [
      "get", "title",
    ]);
    const title = titleResult.json?.data?.title || url;

    const snapResult = await runAgentBrowserCommand(sandbox, ["snapshot", "-i", "-c"], {
      json: false,
    });

    await runAgentBrowserCommand(sandbox, ["close"], { json: false });

    return { title, snapshot: snapResult.stdout };
  });
}

多步骤工作流

Sandbox 在命令之间保持持久化,因此您可以运行完整的自动化序列:

export async function fillAndSubmitForm(
  url: string,
  data: Record<string, string>,
  postSubmitWaitArgs: string[],
) {
  return withBrowser(async (sandbox) => {
    await runAgentBrowserCommand(sandbox, ["open", url]);

    const snapResult = await runAgentBrowserCommand(sandbox, ["snapshot", "-i"], {
      json: false,
    });
    const snapshot = snapResult.stdout;
    // 解析快照以查找元素引用...

    for (const [ref, value] of Object.entries(data)) {
      await runAgentBrowserCommand(sandbox, ["fill", ref, value]);
    }

    await runAgentBrowserCommand(sandbox, ["click", "@e5"]);
    // 传递特定于应用程序的等待条件,例如 ["--url", "**/checkout/complete"],
    // ["--text", "Thanks"], 或 ["#confirmation"]。
    await runAgentBrowserCommand(sandbox, ["wait", ...postSubmitWaitArgs]);

    const ssResult = await runAgentBrowserCommand<{ data?: { path?: string } }>(sandbox, [
      "screenshot",
    ]);
    const ssPath = ssResult.json?.data?.path;
    if (!ssPath) throw new Error("Screenshot did not return a file path.");
    const b64Result = await sandbox.runCommand("base64", ["-w", "0", ssPath]);
    const screenshot = (await b64Result.stdout()).trim();

    await runAgentBrowserCommand(sandbox, ["close"], { json: false });

    return { screenshot };
  });
}

Sandbox 快照(快速启动)

Sandbox 快照 是一个已保存的 VM 映像,其中预装了系统依赖、agent-browser 和 Chromium。可以将其视为 Docker 镜像:无需每次都从头安装依赖,Sandbox 从预先构建的映像启动。

这与 agent-browser 的无障碍快照特性(agent-browser snapshot)无关,后者用于转储页面的无障碍树。Sandbox 快照是 Vercel 基础设施中的一个概念,用于实现 VM 的快速启动。

如果没有 Sandbox 快照,每次运行都需要安装系统依赖、agent-browser 和 Chromium(约 30 秒)。使用快照后,启动时间可缩短至亚秒级。

创建 Sandbox 快照

快照必须包含系统依赖(通过 dnf)、agent-browser 和 Chromium:

const snapshotId = await createAgentBrowserSnapshot();

运行一次,然后设置环境变量:

AGENT_BROWSER_SNAPSHOT_ID=snap_xxxxxxxxxxxx

演示应用程序中提供了一个辅助脚本:

npx tsx examples/environments/scripts/create-snapshot.ts

对于任何使用 Sandbox 模式的生产部署,建议采用此方式。

身份验证

在 Vercel 部署上,Sandbox SDK 通过 OIDC 自动进行身份验证。对于本地开发或显式控制,请设置:

VERCEL_TOKEN=<personal-access-token>
VERCEL_TEAM_ID=<team-id>
VERCEL_PROJECT_ID=<project-id>

这些变量会被传递到 Sandbox.create() 调用中。如果不存在,SDK 将回退到 VERCEL_OIDC_TOKEN(在 Vercel 上自动生效)。

定时工作流(Cron)

与 Vercel Cron Jobs 结合,用于重复的浏览器任务:

// app/api/cron/route.ts  (或您框架中的等效文件)
export async function GET() {
  const result = await withBrowser(async (sandbox) => {
    await sandbox.runCommand("agent-browser", ["open", "https://example.com/pricing"]);
    const snap = await sandbox.runCommand("agent-browser", ["snapshot", "-i", "-c"]);
    await sandbox.runCommand("agent-browser", ["close"]);
    return await snap.stdout();
  });

  // 处理结果,发送告警,存储数据...
  return Response.json({ ok: true, snapshot: result });
}
// vercel.json
{ "crons": [{ "path": "/api/cron", "schedule": "0 9 * * *" }] }

环境变量

变量是否必需描述
AGENT_BROWSER_SNAPSHOT_ID否(但推荐)预构建的 Sandbox 快照 ID,用于亚秒级启动(参见上文)
VERCEL_TOKEN否Vercel 个人访问令牌(用于本地开发;在 Vercel 上 OIDC 是自动的)
VERCEL_TEAM_ID否Vercel 团队 ID(用于本地开发)
VERCEL_PROJECT_ID否Vercel 项目 ID(用于本地开发)

框架示例

该模式在所有框架中的工作方式相同。唯一的区别在于放置服务器端代码的位置:

框架服务器端代码位置
Next.js服务器操作、API 路由、路由处理器
SvelteKit+page.server.ts, +server.ts
Nuxtserver/api/, server/routes/
Remixloader, action 函数
Astro.astro frontmatter, API 路由

示例

有关使用 Vercel Sandbox 模式的工作示例,请参阅 agent-browser 仓库中的 examples/environments/ 目录,其中包含 Sandbox 快照创建脚本、流式进度 UI 和速率限制。

Individual skills in this repo

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

agent-browser

Browser automation CLI for AI agents. Use when the user needs to interact with websites, including navigating pages, filling forms, clicking buttons, taking screenshots, extracting data, testing web apps, or automating any browser task. Triggers include requests to

vercel-labs/agentcore

Run agent-browser on AWS Bedrock AgentCore cloud browsers. Use when the user wants to use AgentCore, run browser automation on AWS, use a cloud browser with AWS credentials, or needs a managed browser session backed by AWS infrastructure. Triggers include "use agentcore", "run on AWS", "cloud browser with AWS", "bedrock browser", "agentcore session", or any task requiring AWS-hosted browser automation.

vercel-labs/derive-client

Reverse-engineer a website's internal API by recording browser traffic into a HAR file, then generate a standalone client or CLI that calls the endpoints directly, with no browser needed after the first recording. Use when asked to "derive a client", "build a CLI for <site>", "reverse engineer this site's API", "record network requests", "turn this site into an API", or when the same site will be automated repeatedly and direct HTTP calls would beat driving the browser every time.

vercel-labs/dogfood

Systematically explore and test a web application to find bugs, UX issues, and other problems. Use when asked to "dogfood", "QA", "exploratory test", "find issues", "bug hunt", "test this app/site/platform", or review the quality of a web application. Produces a structured report with full reproduction evidence -- step-by-step screenshots, repro videos, and detailed repro steps for every issue -- so findings can be handed directly to the responsible teams.

vercel-labs/electron

Automate Electron desktop apps (VS Code, Slack, Discord, Figma, Notion, Spotify, etc.) using agent-browser via Chrome DevTools Protocol. Use when the user needs to interact with an Electron app, automate a desktop app, connect to a running app, control a native app, or test an Electron application. Triggers include "automate Slack app", "control VS Code", "interact with Discord app", "test this Electron app", "connect to desktop app", or any task requiring automation of a native Electron application.

vercel-labs/protected-vercel-deployments

Access and test Vercel deployments protected by Vercel Authentication, SSO, or Deployment Protection with agent-browser. Use when a preview or production URL redirects to a Vercel login page, returns a protection 401 or 403, or needs short-lived Trusted Sources OIDC authentication instead of a static bypass secret or public exception.

vercel-labs/slack

Interact with Slack workspaces using browser automation. Use when the user needs to check unread channels, navigate Slack, send messages, extract data, find information, search conversations, or automate any Slack task. Triggers include "check my Slack", "what channels have unreads", "send a message to", "search Slack for", "extract from Slack", "find who said", or any task requiring programmatic Slack interaction.

vercel-labs/webmcp-gen

Build and validate experimental WebMCP tools for an existing web page. Use when an agent needs to expose a site's real workflows as page tools, create webmcp.init.js, define deterministic checks, or compare WebMCP with accessibility-tree automation.

相关技能