跳到主要内容

VercelServerlessSandbox

Vercel 无服务器函数的形式执行命令。提供全球分布式、零基础设施且可自动扩缩容的执行环境。有关接口的详细信息,请参阅 WorkspaceSandbox 接口

注意

VercelServerlessSandbox 是无状态的。它不提供持久化文件系统、交互式 shell 或长时间运行的后台进程。只有 /tmp 可写,并且其中的内容在两次调用之间不会保留。

备注

如果需要具备持久化文件系统、sudo 访问权限、端口暴露和后台进程的完整 Linux MicroVM,请使用 VercelSandbox,它由 Vercel Sandbox 产品提供支持。

安装
安装的直接链接

npm install @mastra/vercel

使用方法
使用方法的直接链接

VercelServerlessSandbox 添加到 Workspace 并分配给 Agent:

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,
})

使用自定义资源进行团队范围的部署
使用自定义资源进行团队范围的部署的直接链接

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[]
= ['iad1']
部署区域。

maxDuration?:

number
= 60
函数的最长运行时间(秒)。

memory?:

number
= 1024
函数内存(MB)。

env?:

Record<string, string>
嵌入已部署函数源代码的环境变量。这些变量在部署时嵌入,而不是动态设置。

commandTimeout?:

number
= 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 秒)。