跳到主要内容

WorkspaceSandbox

添加于: @mastra/core@1.1.0

WorkspaceSandbox 接口定义 Workspace 如何执行命令和管理后台进程。

属性
属性的直接链接

processes?:

SandboxProcessManager
后台进程管理器。如果未实现,则进程管理 Tool 将不可用。请参阅 SandboxProcessManager 参考文档。

方法
方法的直接链接

start()
start的直接链接

启动 Sandbox。此方法由 workspace.init() 或首次调用 executeCommand() 时自动调用。

await sandbox.start()

此方法为执行命令准备 Sandbox。

stop()
stop的直接链接

停止 Sandbox。

await sandbox.stop?.()

destroy()
destroy的直接链接

清理 Sandbox 资源。由 workspace.destroy() 调用。

await sandbox.destroy()

executeCommand(command, args?, options?)
executecommandcommand-args-options的直接链接

执行 shell 命令。

const result = await sandbox.executeCommand('ls', ['-la', '/docs'])
const result = await sandbox.executeCommand('npm', ['install', 'lodash'])

参数:

command:

string
要执行的命令

args?:

string[]
命令参数

options?:

Options
配置选项。
Options

timeout?:

number
执行超时时间(毫秒)

cwd?:

string
命令的工作目录

env?:

Record<string, string>
额外的环境变量

onStdout?:

(data: string) => void
用于 stdout 流式输出的回调

onStderr?:

(data: string) => void
用于 stderr 流式输出的回调

getInfo()
getinfo的直接链接

获取 Sandbox 状态和资源信息。

const info = await sandbox.getInfo()
// { status: 'running', resources: { memoryMB: 512, cpuPercent: 5 } }

getInstructions(opts?)
getinstructionsopts的直接链接

返回此 Sandbox 工作方式的说明。将 Workspace 分配给 Agent 时,该说明会注入 Agent 的系统消息中。

const instructions = sandbox.getInstructions?.()
// 'Local command execution. Working directory: "/workspace".'

参数:

opts.requestContext?:

RequestContext
如果构造函数中提供了 instructions 函数,则转发给该函数。

返回值: string