> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # VercelServerlessSandbox 以 [Vercel](https://vercel.com) 无服务器函数的形式执行命令。提供全球分布式、零基础设施且可自动扩缩容的执行环境。有关接口的详细信息,请参阅 [WorkspaceSandbox 接口](https://mastra.zisheng.pro/reference/workspace/sandbox)。 > **注意:** VercelServerlessSandbox 是无状态的。它不提供持久化文件系统、交互式 shell 或长时间运行的后台进程。只有 `/tmp` 可写,并且其中的内容在两次调用之间不会保留。 > **备注:** 如果需要具备持久化文件系统、`sudo` 访问权限、端口暴露和后台进程的完整 Linux MicroVM,请使用 [`VercelSandbox`](https://mastra.zisheng.pro/reference/workspace/vercel-sandbox),它由 [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox) 产品提供支持。 ## 安装 **npm**: ```bash npm install @mastra/vercel ``` **pnpm**: ```bash pnpm add @mastra/vercel ``` **Yarn**: ```bash yarn add @mastra/vercel ``` **Bun**: ```bash bun add @mastra/vercel ``` ## 使用方法 将 `VercelServerlessSandbox` 添加到 Workspace 并分配给 Agent: ```typescript import { Agent } from '@mastra/core/agent' import { Workspace } from '@mastra/core/workspace' import { VercelServerlessSandbox } from '@mastra/vercel' const workspace = new Workspace({ sandbox: new VercelServerlessSandbox({ token: process.env.VERCEL_TOKEN, }), }) const agent = new Agent({ id: 'dev-agent', name: 'dev-agent', instructions: 'You are a coding assistant working in this workspace.', model: 'anthropic/claude-sonnet-4-6', workspace, }) ``` ### 使用自定义资源进行团队范围的部署 ```typescript const workspace = new Workspace({ sandbox: new VercelServerlessSandbox({ token: process.env.VERCEL_TOKEN, teamId: 'team_abc123', regions: ['iad1', 'sfo1'], maxDuration: 120, memory: 3008, env: { NODE_ENV: 'production', }, }), }) ``` ## 构造函数参数 **token** (`string`): Vercel API 令牌。未提供时使用 VERCEL\_TOKEN 环境变量。 **teamId** (`string`): 用于团队范围部署的 Vercel 团队 ID。 **projectName** (`string`): 现有 Vercel 项目名称。省略时自动生成。 **regions** (`string[]`): 部署区域。 (Default: `['iad1']`) **maxDuration** (`number`): 函数的最长运行时间(秒)。 (Default: `60`) **memory** (`number`): 函数内存(MB)。 (Default: `1024`) **env** (`Record`): 嵌入已部署函数源代码的环境变量。这些变量在部署时嵌入,而不是动态设置。 **commandTimeout** (`number`): 每次调用的命令超时时间(毫秒)。 (Default: `55000`) **instructions** (`string | ((opts) => string)`): 自定义说明,用于覆盖 getInstructions() 返回的默认说明。传入字符串可完全替换默认说明,传入函数可扩展默认说明。 **onStart** (`SandboxLifecycleHook`): Sandbox 达到 running 状态后调用的生命周期钩子。 **onStop** (`SandboxLifecycleHook`): Sandbox 停止前调用的生命周期钩子。 **onDestroy** (`SandboxLifecycleHook`): Sandbox 销毁前调用的生命周期钩子。 ## 属性 **id** (`string`): 此 Sandbox 实例的唯一标识符。 **name** (`'VercelServerlessSandbox'`): 便于阅读的名称。 **provider** (`'vercel-serverless'`): Provider 类型标识符。 **status** (`ProviderStatus`): 当前生命周期状态:'pending'、'starting'、'running'、'stopping'、'stopped'、'destroying'、'destroyed' 或 'error'。 ## 限制 VercelServerlessSandbox 底层使用无服务器函数,这意味着: - **无持久化文件系统**:写入 `/tmp` 的文件是临时文件,会在两次调用之间被清除。 - **无交互式 shell**:命令通过 `/bin/sh -c` 运行,不支持 stdin 流式传输。 - **无后台进程**:不提供进程管理器、PID 或长时间运行的任务。 - **不支持挂载**:不支持挂载云存储(S3、GCS)。 - **超时限制**:最长执行时间受 `maxDuration` 限制(默认 60 秒)。 ## 相关内容 - [WorkspaceSandbox 接口](https://mastra.zisheng.pro/reference/workspace/sandbox) - [Workspace 类](https://mastra.zisheng.pro/reference/workspace/workspace-class) - [E2BSandbox](https://mastra.zisheng.pro/reference/workspace/e2b-sandbox):提供持久化文件系统和进程管理的全功能云 Sandbox