> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # BlaxelSandbox 在隔离的 [Blaxel](https://blaxel.ai/) 云 Sandbox 中执行命令。它提供安全、隔离的代码执行环境,并支持通过 FUSE 挂载云存储(S3、GCS)。有关接口详情,请参阅 [WorkspaceSandbox 接口](https://mastra.zisheng.pro/reference/workspace/sandbox)。 ## 安装 **npm**: ```bash npm install @mastra/blaxel ``` **pnpm**: ```bash pnpm add @mastra/blaxel ``` **Yarn**: ```bash yarn add @mastra/blaxel ``` **Bun**: ```bash bun add @mastra/blaxel ``` ## 用法 将 `BlaxelSandbox` 添加到 Workspace,并将其分配给 Agent: ```typescript 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 镜像并分配更多内存: ```typescript const workspace = new Workspace({ sandbox: new BlaxelSandbox({ image: 'node:20-slim', memory: 8192, region: 'auto', timeout: '10m', }), }) ``` ## 构造函数参数 **id** (`string`): 此 Sandbox 实例的唯一标识符。 (Default: `自动生成`) **image** (`string`): Sandbox 使用的 Docker 镜像。基于 Debian 的镜像支持 S3 和 GCS 挂载;基于 Alpine 的镜像仅支持 S3 挂载。 (Default: `'blaxel/ts-app:latest'`) **memory** (`number`): 内存分配量(MB)。 (Default: `4096`) **timeout** (`string`): 以时长字符串表示的执行超时时间(例如 '5m'、'1h')。映射到 Blaxel Sandbox TTL。 **region** (`string`): 创建 Sandbox 的 Blaxel 区域。使用 'auto' 自动选择区域,也可以设置 'us-pdx-1' 等具体区域。 (Default: `process.env.BL_REGION || 'auto'`) **env** (`Record`): 要在 Sandbox 中设置的环境变量。 **labels** (`Record`): Sandbox 的自定义标签。 **runtimes** (`SandboxRuntime[]`): 支持的 runtime。有效值:'node'、'python'、'bash'、'ruby'、'go'、'rust'、'java'、'cpp'、'r'。 (Default: `['node', 'python', 'bash']`) **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` 包含用于生成和管理后台进程的内置进程管理器。 ```typescript 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` 参考](https://mastra.zisheng.pro/reference/workspace/process-manager)。 ## 挂载云存储 Blaxel Sandbox 可以挂载 S3 或 GCS 文件系统,使云存储能够作为 Sandbox 内的本地目录访问。 ### S3 ```typescript 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(), }) ``` ### GCS ```typescript 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(), }) ```