説明
Command クラスは、AgentBay クラウド環境セッションでコマンドを実行するメソッドを提供します。
メソッド | 説明 | 環境 | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| クラウド環境でコマンドを実行します。 | サポートされています | サポートされています | サポートされています | サポートされています | サポートされています |
| タイムアウト制御付きで Shell コマンドを実行します。 | サポートされていません | サポートされていません | サポートされていません | サポートされていません | サポートされています |
メソッド
ExecuteCommand - コマンドの実行
Golang
func (cmd *Command) ExecuteCommand(command string, timeoutMs ...int) (*CommandResult, error)パラメーター
command (string): 実行するコマンド。
timeoutMs (int, optional): コマンド実行のタイムアウト (ミリ秒単位) 。デフォルト値は
1000ms です。
戻り値
*CommandResult: コマンドの出力とリクエスト ID を含む結果オブジェクト。error: コマンドの実行が失敗した場合のエラーメッセージ。
CommandResult 構造体
type CommandResult struct {
RequestID string // デバッグ用の一意のリクエスト識別子
Output string // コマンドの出力
}Python
def execute_command(command: str, timeout_ms: int = 1000) -> OperationResultパラメーター
command (str): 実行するコマンド。
timeout_ms (int, optional): コマンド実行のタイムアウト (ミリ秒単位) 。デフォルト値は 1000 ms です。
戻り値
OperationResult: コマンドの出力 (データ) 、操作ステータス、リクエスト ID、およびエラーメッセージを含む結果オブジェクト。
TypeScript
executeCommand(command: string, timeoutMs: number = 1000): Promise<string>パラメーター
command (string): 実行するコマンド。
timeoutMs (number, optional): コマンド実行のタイムアウト (ミリ秒単位) 。デフォルト値は
1000ms です。
戻り値
Promise<string>: コマンドの出力で解決される Promise。
例外
Error: コマンドの実行が失敗した場合にスローされます。
RunCode - コードの実行
Golang
func (cmd *Command) RunCode(code string, language string, timeoutS ...int) (*CommandResult, error)パラメーター
code (string): 実行するコード。
language (string): コードのプログラミング言語。
pythonまたはjavascriptである必要があります。timeoutS (int, optional): コード実行のタイムアウト (秒単位) 。デフォルト値は
300s です。
戻り値
*CommandResult: コード実行の出力とリクエスト ID を含む結果オブジェクト。error: コードの実行が失敗した場合、またはサポートされていない言語が指定された場合のエラーメッセージ。
Python
def run_code(code: str, language: str, timeout_s: int = 300) -> OperationResultパラメーター
code (str): 実行するコード。
language (str): コードのプログラミング言語。
pythonまたはjavascriptである必要があります。timeout_s (int, optional): コード実行のタイムアウト (秒単位) 。デフォルト値は
300s です。
戻り値
OperationResult: コード実行の出力 (データ) 、操作ステータス、リクエスト ID、およびエラーメッセージを含む結果オブジェクト。
TypeScript
runCode(code: string, language: string, timeoutS: number = 300): Promise<string>パラメーター
code (string): 実行するコード。
language (string): コードのプログラミング言語。
pythonまたはjavascriptである必要があります。timeoutS (number, optional): コード実行のタイムアウト (秒単位) 。デフォルト値は
300s です。
戻り値
Promise<string>: コード実行の出力で解決される Promise。
例外
APIError: コードの実行が失敗した場合、またはサポートされていない言語が指定された場合にスローされます。
使用例
Golang
コマンドの実行
package main
import (
"fmt"
"log"
)
func main() {
// セッションの作成
agentBay := agentbay.NewAgentBay("your-api-key")
sessionResult, err := agentBay.Create(nil)
if err != nil {
log.Fatal(err)
}
session := sessionResult.Session
// コマンドの実行 (デフォルトのタイムアウト: 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)
}
// コマンドの実行 (カスタムタイムアウト: 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)
}
}コードの実行
package main
import (
"fmt"
"log"
)
func main() {
// セッションの作成
agentBay := agentbay.NewAgentBay("your-api-key")
sessionResult, err := agentBay.Create(nil)
if err != nil {
log.Fatal(err)
}
session := sessionResult.Session
// Python コードの実行
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)
}
// JavaScript コードの実行 (カスタムタイムアウト: 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
# SDK の初期化
agent_bay = AgentBay(api_key="your_api_key")
# セッションの作成
session_result = agent_bay.create()
if session_result.success:
session = session_result.session
# コマンドの実行 (デフォルトのタイムアウト: 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}")
# コマンドの実行 (カスタムタイムアウト: 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}")
# Python コードの実行
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}")
# JavaScript コードの実行 (カスタムタイムアウト: 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';
// SDK の初期化
const agentBay = new AgentBay({ apiKey: 'your_api_key' });
// セッションの作成
async function createSession() {
try {
const sessionResult = await agentBay.create();
const session = sessionResult.session;
// コマンドの実行 (デフォルトのタイムアウト: 1000 ms)
const commandOutput = await session.command.executeCommand('ls -la');
console.log(`Command output: ${commandOutput}`);
// コマンドの実行 (カスタムタイムアウト: 2000 ms)
const commandOutputWithTimeout = await session.command.executeCommand('ls -la', 2000);
console.log(`Custom timeout command output: ${commandOutputWithTimeout}`);
// Python コードの実行
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}`);
// JavaScript コードの実行 (カスタムタイムアウト: 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();