All Products
Search
Document Center

AgentBay:Command

Last Updated:Dec 18, 2025

Description

The Command class provides methods to execute commands in an AgentBay cloud environment session.

Method

Description

Environment

Linux Computer Use

Windows Computer Use

Browser Use

Mobile Use

Code Space

execute_command

Executes a command in the cloud environment.

Supported

Supported

Supported

Supported

Supported

shell

Executes a Shell command with timeout control.

Not supported

Not supported

Not supported

Not supported

Supported

Methods

ExecuteCommand - Execute a command

Golang

func (cmd *Command) ExecuteCommand(command string, timeoutMs ...int) (*CommandResult, error)

Parameters

  • command (string): The command to execute.

  • timeoutMs (int, optional): The command execution timeout in milliseconds. The default value is 1000 ms.

Return values

  • *CommandResult: A result object that contains the command output and the request ID.

  • error: An error message if the command execution fails.

CommandResult struct

type CommandResult struct {
    RequestID string // A unique request identifier for debugging
    Output    string // The output of the command
}

Python

def execute_command(command: str, timeout_ms: int = 1000) -> OperationResult

Parameters

  • command (str): The command to execute.

  • timeout_ms (int, optional): The command execution timeout in milliseconds. The default value is 1000 ms.

Return value

  • OperationResult: A result object that contains the command output (data), operation status, request ID, and error message.

TypeScript

executeCommand(command: string, timeoutMs: number = 1000): Promise<string>

Parameters

  • command (string): The command to execute.

  • timeoutMs (number, optional): The command execution timeout in milliseconds. The default value is 1000 ms.

Return value

  • Promise<string>: A Promise that resolves with the command output.

Exception

  • Error: Thrown if the command execution fails.

RunCode - Run code

Golang

func (cmd *Command) RunCode(code string, language string, timeoutS ...int) (*CommandResult, error)

Parameters

  • code (string): The code to execute.

  • language (string): The programming language of the code. Must be python or javascript.

  • timeoutS (int, optional): The code execution timeout in seconds. The default value is 300 s.

Return values

  • *CommandResult: A result object that contains the code execution output and the request ID.

  • error: An error message if the code execution fails or an unsupported language is specified.

Python

def run_code(code: str, language: str, timeout_s: int = 300) -> OperationResult

Parameters

  • code (str): The code to execute.

  • language (str): The programming language of the code. Must be python or javascript.

  • timeout_s (int, optional): The code execution timeout in seconds. The default value is 300 s.

Return value

  • OperationResult: A result object that contains the code execution output (data), operation status, request ID, and error message.

TypeScript

runCode(code: string, language: string, timeoutS: number = 300): Promise<string>

Parameters

  • code (string): The code to execute.

  • language (string): The programming language of the code. Must be python or javascript.

  • timeoutS (number, optional): The code execution timeout in seconds. The default value is 300 s.

Return value

  • Promise<string>: A Promise that resolves with the code execution output.

Exception

  • APIError: Thrown if the code execution fails or an unsupported language is specified.

Sample code

Golang

Execute a command

package main

import (
    "fmt"
    "log"
)

func main() {
    // Create a session
    agentBay := agentbay.NewAgentBay("your-api-key")
    sessionResult, err := agentBay.Create(nil)
    if err != nil {
        log.Fatal(err)
    }
    session := sessionResult.Session

    // Execute a command (default timeout: 1000 ms)
    result, err := session.Command.ExecuteCommand("ls -la")
    if err != nil {
        log.Printf("Error executing command: %v", err)
    } else {
        fmt.Printf("Command output: %s\n", result.Output)
    }

    // Execute a command (custom timeout: 2000 ms)
    resultWithTimeout, err := session.Command.ExecuteCommand("ls -la", 2000)
    if err != nil {
        log.Printf("Error executing command with timeout: %v", err)
    } else {
        fmt.Printf("Command output with timeout: %s\n", resultWithTimeout.Output)
    }
}

Run code

package main

import (
    "fmt"
    "log"
)

func main() {
    // Create a session
    agentBay := agentbay.NewAgentBay("your-api-key")
    sessionResult, err := agentBay.Create(nil)
    if err != nil {
        log.Fatal(err)
    }
    session := sessionResult.Session

    // Run Python code
    pythonCode := `
    print("Hello, world!")
    x = 1 + 1
    print(x)
    `
    result, err := session.Command.RunCode(pythonCode, "python")
    if err != nil {
        log.Printf("Error running Python code: %v", err)
    } else {
        fmt.Printf("Python code output: %s\n", result.Output)
    }

    // Run JavaScript code (custom timeout: 600 s)
    jsCode := `
    log("Hello, world!");
    const x = 1 + 1;
    log(x);
    `
    resultJS, err := session.Command.RunCode(jsCode, "javascript", 600)
    if err != nil {
        log.Printf("Error running JavaScript code: %v", err)
    } else {
        fmt.Printf("JavaScript code output: %s\n", resultJS.Output)
    }
}

Python

from agentbay import AgentBay

# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")

# Create a session
session_result = agent_bay.create()
if session_result.success:
    session = session_result.session

    # Execute a command (default timeout: 1000 ms)
    result = session.command.execute_command("ls -la")
    if result.success:
        print(f"Command output: {result.data}")
    else:
        print(f"Command execution failed: {result.error_message}")

    # Execute a command (custom timeout: 2000 ms)
    result_with_timeout = session.command.execute_command("ls -la", timeout_ms=2000)
    if result_with_timeout.success:
        print(f"Custom timeout command output: {result_with_timeout.data}")
    else:
        print(f"Command execution failed: {result_with_timeout.error_message}")

    # Run Python code
    python_code = """
    print("Hello, world!")
    x = 1 + 1
    print(x)
    """
    result_code = session.command.run_code(python_code, "python")
    if result_code.success:
        print(f"Python code execution result: {result_code.data}")
    else:
        print(f"Code execution failed: {result_code.error_message}")

    # Run JavaScript code (custom timeout: 600 s)
    js_code = """
    log("Hello, world!");
    const x = 1 + 1;
    log(x);
    """
    result_js = session.command.run_code(js_code, "javascript", timeout_s=600)
    if result_js.success:
        print(f"JavaScript code execution result: {result_js.data}")
    else:
        print(f"Code execution failed: {result_js.error_message}")

TypeScript

import { AgentBay } from 'wuying-agentbay-sdk';

// Initialize the SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });

// Create a session
async function createSession() {
  try {
    const sessionResult = await agentBay.create();
    const session = sessionResult.session;

    // Execute a command (default timeout: 1000 ms)
    const commandOutput = await session.command.executeCommand('ls -la');
    console.log(`Command output: ${commandOutput}`);

    // Execute a command (custom timeout: 2000 ms)
    const commandOutputWithTimeout = await session.command.executeCommand('ls -la', 2000);
    console.log(`Custom timeout command output: ${commandOutputWithTimeout}`);

    // Run Python code
    const pythonCode = `
    print("Hello, world!")
    x = 1 + 1
    print(x)
    `;
    const pythonResult = await session.command.runCode(pythonCode, 'python');
    console.log(`Python code execution result: ${pythonResult}`);

    // Run JavaScript code (custom timeout: 600 s)
    const jsCode = `
    log("Hello, world!");
    const x = 1 + 1;
    log(x);
    `;
    const jsResult = await session.command.runCode(jsCode, 'javascript', 600);
    console.log(`JavaScript code execution result: ${jsResult}`);
  } catch (error) {
    console.error('Operation failed:', error);
  }
}

createSession();