跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Blaxel

Executes commands in isolated Blaxel cloud sandboxes. Provides secure, isolated code execution environments with support for mounting cloud storage (S3, GCS) via FUSE. For interface details, see WorkspaceSandbox interface.

Installation
Installation的直接链接

npm install @mastra/blaxel

Usage
Usage的直接链接

Add a BlaxelSandbox to a workspace and assign it to an 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,
})

Custom image with resources
Custom image with resources的直接链接

Use a custom Docker image with additional memory:

const workspace = new Workspace({
sandbox: new BlaxelSandbox({
image: 'node:20-slim',
memory: 8192,
region: 'auto',
timeout: '10m',
}),
})

Constructor parameters
Constructor parameters的直接链接

id?:

string
= Auto-generated
Unique identifier for this sandbox instance.

image?:

string
= 'blaxel/ts-app:latest'
Docker image to use for the sandbox. Debian-based images support both S3 and GCS mounts. Alpine-based images support S3 mounts only.

memory?:

number
= 4096
Memory allocation in MB.

timeout?:

string
Execution timeout as a duration string (e.g. '5m', '1h'). Maps to the Blaxel sandbox TTL.

region?:

string
= process.env.BL_REGION || 'auto'
Blaxel region where the sandbox should be created. Use 'auto' to choose a region automatically, or set a concrete region like 'us-pdx-1'.

env?:

Record<string, string>
Environment variables to set in the sandbox.

workingDirectory?:

string
Default directory for command execution when no per-command cwd is given. A per-command cwd always wins. Use an absolute path.

labels?:

Record<string, string>
Custom labels for the sandbox.

runtimes?:

SandboxRuntime[]
= ['node', 'python', 'bash']
Supported runtimes. Valid values: 'node', 'python', 'bash', 'ruby', 'go', 'rust', 'java', 'cpp', 'r'.

ports?:

Array<{ name?: string; target: number; protocol?: 'HTTP' | 'TCP' | 'UDP' }>
Ports to expose from the sandbox.

Properties
Properties的直接链接

id:

string
Sandbox instance identifier.

name:

string
'BlaxelSandbox'

provider:

string
'blaxel'

status:

ProviderStatus
'pending' | 'initializing' | 'running' | 'stopped' | 'error'

instance:

SandboxInstance
The underlying Blaxel SandboxInstance for direct access to Blaxel APIs. Throws SandboxNotReadyError if the sandbox has not been started.

processes:

BlaxelProcessManager
Background process manager. See SandboxProcessManager reference.

Background processes
Background processes的直接链接

BlaxelSandbox includes a built-in process manager for spawning and managing background processes.

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
await handle.sendStdin('hello\n')
await handle.closeStdin()
console.log(handle.stdout)
await handle.kill()

Spawned processes have a writable stdin by default. Pass stdinMode: 'ignore' to start a process with stdin closed so commands that read stdin get EOF immediately.

See SandboxProcessManager reference for the full API.

Mounting cloud storage
Mounting cloud storage的直接链接

Blaxel sandboxes can mount S3 or GCS filesystems, making cloud storage accessible as a local directory inside the sandbox.

S3
S3的直接链接

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
GCS的直接链接

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(),
})