LocalSandbox
新增於: @mastra/core@1.1.0
在本機系統上執行指令。如需介面詳情,請參閱 WorkspaceSandbox 介面。
使用方式使用方式 的直接連結
將 LocalSandbox 加入 Workspace,並指派給 Agent。Agent 隨後便可在執行工作時運行 shell 指令:
import { Agent } from '@mastra/core/agent'
import { Workspace, LocalFilesystem, LocalSandbox } from '@mastra/core/workspace'
const workspace = new Workspace({
filesystem: new LocalFilesystem({ basePath: './workspace' }),
sandbox: new LocalSandbox({
workingDirectory: './workspace',
env: {
NODE_ENV: 'development',
},
}),
})
const agent = new Agent({
id: 'dev-agent',
model: 'openai/gpt-5.6-sol',
workspace,
})
// The agent now has the execute_command tool available
const response = await agent.generate('Run npm install')
自動啟動行為自動啟動行為 的直接連結
如 LocalSandbox 尚未運行,首次執行指令時便會自動啟動。你亦可在應用程式啟動時呼叫 workspace.init(),明確啟動 Sandbox,以免首次執行指令時出現延遲。
建構函數參數建構函數參數 的直接連結
id?:
workingDirectory?:
env?:
timeout?:
isolation?:
instructions?:
nativeSandbox?:
NativeSandboxConfignativesandboxconfig 的直接連結
原生作業系統 Sandbox 的設定選項(與 isolation: 'seatbelt' 或 'bwrap' 配合使用)。
allowNetwork?:
readOnlyPaths?:
readWritePaths?:
seatbeltProfilePath?:
bwrapArgs?:
allowSystemBinaries?:
屬性屬性 的直接連結
id:
name:
provider:
status:
workingDirectory:
processes:
路徑解析路徑解析 的直接連結
相對路徑及執行內容相對路徑及執行內容 的直接連結
為 workingDirectory 使用相對路徑時,系統會從 process.cwd() 解析。在 Mastra 項目中,目前工作目錄會因程式碼的運行方式而異:
| 內容 | 工作目錄 | ./workspace 解析為 |
|---|---|---|
mastra dev | ./src/mastra/public/ | ./src/mastra/public/workspace |
mastra start | ./.mastra/output/ | ./.mastra/output/workspace |
| 直接執行指令碼 | 你運行指令的位置 | 相對於該位置 |
同一個相對路徑可能會解析至不同位置,因而造成混淆。
建議:使用絕對路徑建議:使用絕對路徑 的直接連結
如要在所有執行內容中使用一致的路徑,請配合絕對路徑使用環境變數:
import { LocalSandbox } from '@mastra/core/workspace'
const sandbox = new LocalSandbox({
workingDirectory: process.env.WORKSPACE_PATH!,
})
將環境中的 WORKSPACE_PATH 設定為絕對路徑,例如 /home/user/my-project/workspace。無論你如何運行程式碼,這都可確保指令從一致的目錄執行。
背景程序背景程序 的直接連結
LocalSandbox 包含內置程序管理器,可用來產生及管理背景程序。程序會使用 child_process.spawn,以子程序形式在本機上運行。
const sandbox = new LocalSandbox({ workingDirectory: './workspace' })
await sandbox.start()
// Spawn a background process
const handle = await sandbox.processes.spawn('node server.js')
// Read output, send stdin, kill
console.log(handle.stdout)
await handle.sendStdin('input\n')
await handle.kill()
啟用原生隔離(seatbelt 或 bwrap)時,產生的程序亦會以相同的隔離後端包裝。
如需完整 API,請參閱 SandboxProcessManager 參考。
靜態方法靜態方法 的直接連結
detectIsolation()detectisolation 的直接連結
偵測目前平台上最合適的可用隔離後端。
const detection = LocalSandbox.detectIsolation()
// { backend: 'seatbelt', available: true, message: 'Seatbelt available on macOS' }
環境隔離環境隔離 的直接連結
預設情況下,LocalSandbox 的環境只包括 PATH。指令可以正常運行,同時避免意外洩露 API 金鑰及機密資料。
// Default: only PATH is available (commands work, secrets protected)
const secureSandbox = new LocalSandbox({
workingDirectory: './workspace',
})
// Explicit: pass specific variables
const sandbox = new LocalSandbox({
workingDirectory: './workspace',
env: {
NODE_ENV: 'development',
API_URL: 'https://api.example.com',
},
})
// Full access (use with caution)
const devSandbox = new LocalSandbox({
workingDirectory: './workspace',
env: process.env,
})
原生作業系統 Sandbox原生作業系統 Sandbox 的直接連結
LocalSandbox 支援原生作業系統層級的 Sandbox,以提供額外保安:
- macOS:使用 Seatbelt(
sandbox-exec)隔離檔案系統及網絡 - Linux:使用 Bubblewrap(
bwrap)隔離命名空間
// Detect the best available backend for this platform
const detection = LocalSandbox.detectIsolation()
console.log(detection)
// { backend: 'seatbelt', available: true, message: '...' }
// Enable native sandboxing
const sandbox = new LocalSandbox({
workingDirectory: './workspace',
isolation: 'seatbelt', // or 'bwrap' on Linux
nativeSandbox: {
allowNetwork: false, // Block network access (default)
readWritePaths: ['/tmp/extra'], // Additional writable paths
},
})
啟用隔離時:
- 檔案寫入只限 Workspace 目錄(及已設定的路徑)
- 所有位置均可讀取檔案(系統二進制檔案需要此權限)
- 預設封鎖網絡存取
- 程序隔離可防止影響主機系統
Sandbox 設定檔位置Sandbox 設定檔位置 的直接連結
在 macOS 上使用 seatbelt 隔離時,LocalSandbox 會在 process.cwd() 內的 .sandbox-profiles/ 資料夾產生設定檔,並與工作目錄分開:
project/
├── .sandbox/ # Default working directory (sandboxed)
│ └── ... files created by sandbox
├── .sandbox-profiles/ # Seatbelt profiles (outside sandbox)
│ └── seatbelt-a1b2c3d4.sb # Hash based on workspace + config
└── ... your project files
設定檔名稱是 Workspace 路徑及設定的雜湊值,因此設定相同的 Sandbox 會共用同一份設定檔,而不同設定則會使用不同檔案。這可防止同時運行多個 Sandbox 時發生衝突。
這種分隔方式可防止 Sandbox 內的程序讀取或修改本身的保安設定檔。設定檔會在 Sandbox 啟動時建立,並在銷毀時清除。