BlaxelSandbox
在隔離的 Blaxel 雲端 Sandbox 中執行指令。提供安全且隔離的程式碼執行環境,並支援透過 FUSE 掛載雲端儲存空間(S3、GCS)。有關介面詳情,請參閱 WorkspaceSandbox 介面。
安裝安裝 的直接連結
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/blaxel
pnpm add @mastra/blaxel
yarn add @mastra/blaxel
bun add @mastra/blaxel
用法用法 的直接連結
將 BlaxelSandbox 加入 Workspace,並指派給 Agent:
import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { BlaxelSandbox } from '@mastra/blaxel'
const workspace = new Workspace({
sandbox: new BlaxelSandbox(),
})
const agent = new Agent({
id: 'code-agent',
name: 'Code Agent',
instructions: 'You are a coding assistant working in this workspace.',
model: 'anthropic/claude-sonnet-4-6',
workspace,
})
使用自訂映像檔及資源使用自訂映像檔及資源 的直接連結
使用具有額外記憶體的自訂 Docker 映像檔:
const workspace = new Workspace({
sandbox: new BlaxelSandbox({
image: 'node:20-slim',
memory: 8192,
region: 'auto',
timeout: '10m',
}),
})
建構函式參數建構函式參數 的直接連結
id?:
string
= 自動產生
此 Sandbox 實例的唯一識別碼。
image?:
string
= 'blaxel/ts-app:latest'
Sandbox 使用的 Docker 映像檔。以 Debian 為基礎的映像檔支援掛載 S3 及 GCS;以 Alpine 為基礎的映像檔只支援掛載 S3。
memory?:
number
= 4096
以 MB 為單位的記憶體配置。
timeout?:
string
以持續時間字串表示的執行逾時(例如 '5m'、'1h'),對應至 Blaxel Sandbox TTL。
region?:
string
= process.env.BL_REGION || 'auto'
要建立 Sandbox 的 Blaxel 區域。使用 'auto' 自動選擇區域,或設定 'us-pdx-1' 等具體區域。
env?:
Record<string, string>
要在 Sandbox 中設定的環境變數。
labels?:
Record<string, string>
Sandbox 的自訂標籤。
runtimes?:
SandboxRuntime[]
= ['node', 'python', 'bash']
支援的 runtime。有效值:'node'、'python'、'bash'、'ruby'、'go'、'rust'、'java'、'cpp'、'r'。
ports?:
Array<{ name?: string; target: number; protocol?: 'HTTP' | 'TCP' | 'UDP' }>
從 Sandbox 公開的連接埠。
屬性屬性 的直接連結
id:
string
Sandbox 實例識別碼。
name:
string
'BlaxelSandbox'
provider:
string
'blaxel'
status:
ProviderStatus
'pending' | 'initializing' | 'running' | 'stopped' | 'error'
instance:
SandboxInstance
底層 Blaxel SandboxInstance,可用於直接存取 Blaxel API。如果 Sandbox 尚未啟動,便會拋出 SandboxNotReadyError。
processes:
BlaxelProcessManager
背景程序管理器。請參閱 SandboxProcessManager 參考。
背景程序背景程序 的直接連結
BlaxelSandbox 包含內置程序管理器,可用於產生及管理背景程序。
const sandbox = new BlaxelSandbox({ id: 'dev-sandbox' })
await sandbox.start()
// Spawn a background process
const handle = await sandbox.processes.spawn('node server.js', {
env: { PORT: '3000' },
onStdout: data => console.log(data),
})
// Interact with the process
console.log(handle.stdout)
await handle.kill()
備註
Blaxel Sandbox 不支援 stdin。呼叫 handle.sendStdin() 會拋出錯誤。
如要查看完整 API,請參閱 SandboxProcessManager 參考。
掛載雲端儲存空間掛載雲端儲存空間 的直接連結
Blaxel Sandbox 可掛載 S3 或 GCS 檔案系統,讓你在 Sandbox 內以本機目錄方式存取雲端儲存空間。
S3S3 的直接連結
import { Workspace } from '@mastra/core/workspace'
import { S3Filesystem } from '@mastra/s3'
import { BlaxelSandbox } from '@mastra/blaxel'
const workspace = new Workspace({
mounts: {
'/data': new S3Filesystem({
bucket: 'my-bucket',
region: 'us-east-1',
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
}),
},
sandbox: new BlaxelSandbox(),
})
GCSGCS 的直接連結
import { Workspace } from '@mastra/core/workspace'
import { GCSFilesystem } from '@mastra/gcs'
import { BlaxelSandbox } from '@mastra/blaxel'
const workspace = new Workspace({
mounts: {
'/data': new GCSFilesystem({
bucket: 'my-bucket',
credentials: JSON.parse(process.env.GCS_SERVICE_ACCOUNT_KEY!),
}),
},
sandbox: new BlaxelSandbox(),
})