跳至主要內容

VercelServerlessSandbox

將指令執行為 Vercel Serverless Function,提供全球分散式、零基礎架構且可自動擴展的執行環境。介面詳情請參閱 WorkspaceSandbox 介面

警告

VercelServerlessSandbox 是無狀態的,不提供持久檔案系統、互動式 shell 或長時間執行的背景處理程序。只有 /tmp 可寫入,而且其內容不會在呼叫之間保留。

備註

若要使用具備持久檔案系統、sudo 存取權、公開連接埠與背景處理程序的完整 Linux MicroVM,請使用以 Vercel Sandbox 產品為後端的 VercelSandbox

安裝
「安裝」的直接連結

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 Token。若未提供,會改用 VERCEL_TOKEN 環境變數。

teamId?:

string
團隊範圍部署使用的 Vercel 團隊 ID。

projectName?:

string
現有的 Vercel 專案名稱。省略時會自動產生。

regions?:

string[]
= ['iad1']
部署區域。

maxDuration?:

number
= 60
函式最長執行時間,單位為秒。

memory?:

number
= 1024
函式記憶體,單位為 MB。

env?:

Record<string, string>
建置到已部署 Function 原始碼中的環境變數。這些值會在部署時嵌入,而非動態設定。

commandTimeout?:

number
= 55000
每次呼叫的指令逾時時間,單位為毫秒。

instructions?:

string | ((opts) => string)
覆寫 getInstructions() 所傳回預設指示的自訂指示。傳入字串可完全取代預設值,或傳入函式加以擴充。

onStart?:

SandboxLifecycleHook
Sandbox 達到 running 狀態後呼叫的生命週期 hook。

onStop?:

SandboxLifecycleHook
Sandbox 停止前呼叫的生命週期 hook。

onDestroy?:

SandboxLifecycleHook
Sandbox 銷毀前呼叫的生命週期 hook。

屬性
「屬性」的直接連結

id:

string
此 Sandbox 執行個體的唯一識別碼。

name:

'VercelServerlessSandbox'
易讀名稱。

provider:

'vercel-serverless'
Provider 類型識別碼。

status:

ProviderStatus
目前的生命週期狀態:'pending''starting''running''stopping''stopped''destroying''destroyed''error'

限制
「限制」的直接連結

VercelServerlessSandbox 底層使用 Serverless Function,因此具有以下限制:

  • 沒有持久檔案系統:寫入 /tmp 的檔案是暫時性的,且會在呼叫之間清除。
  • 沒有互動式 shell:指令透過 /bin/sh -c 執行,不提供 stdin 串流。
  • 沒有背景處理程序:不提供處理程序管理器、PID 或長時間執行的工作。
  • 無法掛載:不支援雲端儲存空間掛載(S3、GCS)。
  • 逾時限制:最長執行時間受 maxDuration 限制(預設 60 秒)。