Skip to main content

沙盒

🌐 Sandbox

沙箱提供商使代理能够执行 shell 命令。当你在工作区配置沙箱时,代理可以在其任务中运行命令。

🌐 Sandbox providers give agents the ability to execute shell commands. When you configure a sandbox on a workspace, agents can run commands as part of their tasks.

沙箱提供商在受控环境中执行命令:

🌐 A sandbox provider executes commands in a controlled environment:

  • 命令执行 - 运行带参数的 shell 命令
  • 工作目录 - 从特定目录运行的命令
  • 环境变量 - 控制可用的变量
  • 超时 - 防止长时间运行的命令挂起
  • 隔离 - 可选的操作系统级别沙箱以增强安全性

当你将带有沙箱的工作区分配给代理时,Mastra 会自动将 execute_command 工具包含在代理的工具集中。

🌐 When you assign a workspace with a sandbox to an agent, Mastra automatically includes the execute_command tool in the agent's toolset.

支持的提供商
Direct link to 支持的提供商

🌐 Supported providers

可用提供商:

🌐 Available providers:

基本用法
Direct link to 基本用法

🌐 Basic usage

创建一个带有沙箱的工作区并将其分配给代理。然后,代理可以执行 shell 命令:

🌐 Create a workspace with a sandbox and assign it to an agent. The agent can then execute shell commands:

src/mastra/agents/dev-agent.ts
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',
}),
});

const agent = new Agent({
id: 'dev-agent',
model: 'openai/gpt-4o',
instructions: 'You are a helpful development assistant.',
workspace,
});

// The agent now has the execute_command tool available
const response = await agent.generate('Run `ls -la` in the workspace directory');

请参阅 LocalSandbox 参考 了解配置选项,包括环境隔离和本地操作系统沙箱。

🌐 See LocalSandbox reference for configuration options including environment isolation and native OS sandboxing.

代理工具
Direct link to 代理工具

🌐 Agent tools

当你在工作区配置沙箱时,代理会收到用于运行 shell 命令的 execute_command 工具。详情请参阅工作区类参考

🌐 When you configure a sandbox on a workspace, agents receive the execute_command tool for running shell commands. See workspace class reference for details.

🌐 Related