Skip to main content

WorkspaceSandbox

WorkspaceSandbox 接口定义了工作区如何执行命令。

🌐 The WorkspaceSandbox interface defines how workspaces execute commands.

方法
Direct link to 方法

🌐 Methods

start()
Direct link to start

启动沙箱。由 workspace.init() 自动调用,或在第一次调用 executeCommand() 时调用。

🌐 Start the sandbox. Called automatically by workspace.init() or on first executeCommand() call.

await sandbox.start();

此方法为命令执行准备沙箱。

🌐 This method prepares the sandbox for command execution.

stop()
Direct link to stop

停止沙盒。

🌐 Stop the sandbox.

await sandbox.stop?.();

destroy()
Direct link to destroy

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

🌐 Clean up sandbox resources. Called by workspace.destroy().

await sandbox.destroy();

executeCommand(command, args?, options?)
Direct link to executecommandcommand-args-options

执行一个 shell 命令。

🌐 Execute a shell command.

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

参数:

command:

string
Command to execute

args?:

string[]
Command arguments

options.timeout?:

number
Execution timeout in milliseconds

options.cwd?:

string
Working directory for the command

options.env?:

Record<string, string>
Additional environment variables

options.onStdout?:

(data: string) => void
Callback for stdout streaming

options.onStderr?:

(data: string) => void
Callback for stderr streaming

getInfo()
Direct link to getinfo

获取沙盒状态和资源信息。

🌐 Get sandbox status and resource information.

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

getInstructions()
Direct link to getinstructions

获取关于此沙箱工作方式的描述。用于工具描述,以帮助代理理解执行环境。

🌐 Get a description of how this sandbox works. Used in tool descriptions to help agents understand the execution context.

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

🌐 Related