> Discover all available pages from the documentation index: https://mastra.zisheng.pro/zh-TW/llms.txt # VercelServerlessSandbox 將指令執行為 [Vercel](https://vercel.com) Serverless Function,提供全球分散式、零基礎架構且可自動擴展的執行環境。介面詳情請參閱 [WorkspaceSandbox 介面](https://mastra.zisheng.pro/zh-TW/reference/workspace/sandbox)。 > **警告:** VercelServerlessSandbox 是無狀態的,不提供持久檔案系統、互動式 shell 或長時間執行的背景處理程序。只有 `/tmp` 可寫入,而且其內容不會在呼叫之間保留。 > **備註:** 若要使用具備持久檔案系統、`sudo` 存取權、公開連接埠與背景處理程序的完整 Linux MicroVM,請使用以 [Vercel Sandbox](https://vercel.com/docs/vercel-sandbox) 產品為後端的 [`VercelSandbox`](https://mastra.zisheng.pro/zh-TW/reference/workspace/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 Token。若未提供,會改用 VERCEL\_TOKEN 環境變數。 **teamId** (`string`): 團隊範圍部署使用的 Vercel 團隊 ID。 **projectName** (`string`): 現有的 Vercel 專案名稱。省略時會自動產生。 **regions** (`string[]`): 部署區域。 (Default: `['iad1']`) **maxDuration** (`number`): 函式最長執行時間,單位為秒。 (Default: `60`) **memory** (`number`): 函式記憶體,單位為 MB。 (Default: `1024`) **env** (`Record`): 建置到已部署 Function 原始碼中的環境變數。這些值會在部署時嵌入,而非動態設定。 **commandTimeout** (`number`): 每次呼叫的指令逾時時間,單位為毫秒。 (Default: `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 秒)。 ## 相關內容 - [WorkspaceSandbox 介面](https://mastra.zisheng.pro/zh-TW/reference/workspace/sandbox) - [Workspace 類別](https://mastra.zisheng.pro/zh-TW/reference/workspace/workspace-class) - [E2BSandbox](https://mastra.zisheng.pro/zh-TW/reference/workspace/e2b-sandbox):具備持久檔案系統與處理程序管理功能的完整雲端 Sandbox