概要
Session クラスは、AgentBay クラウド環境におけるセッションを表します。ファイルシステムを管理し、コマンドを実行するためのメソッドを提供します。
メソッド | 説明 | 環境 | ||||
ComputerUseLinux | ComputerUseWindows | BrowserUse | MobileUse | CodeSpace | ||
| AgentBay クラウド環境で新しいセッションを作成します。 | サポート | サポート | サポート | サポート | サポート |
| ID でセッションを削除します。 | サポート | サポート | サポート | サポート | サポート |
| このセッションのリンクを取得します。 | サポート | サポート | サポート | サポート | サポート |
| 利用可能なすべてのセッションをリスト表示します。 | サポート | サポート | サポート | サポート | サポート |
| このセッションに関する情報を取得します。 | サポート | サポート | サポート | サポート | サポート |
| このセッションのラベルを設定します。 | サポート | サポート | サポート | サポート | サポート |
| このセッションのラベルを取得します。 | サポート | サポート | サポート | サポート | サポート |
プロパティ
プロパティ名 | 説明 |
agent_bay | このセッションを作成した AgentBay インスタンス。 |
session_id | このセッションの ID。 |
resource_url | このセッションに関連付けられたリソースの URL。 |
file_system | このセッションの FileSystem インスタンス。 |
command | このセッションの Command インスタンス。 |
oss | このセッションの OSS インスタンス。 |
application | このセッションの ApplicationManager インスタンス。 |
window | このセッションの WindowManager インスタンス。 |
ui | このセッションの UI インスタンス。 |
context | このセッションの FileSystem インスタンス。 |
メソッド
delete — セッションの削除
Golang
Delete() (*DeleteResult, error)戻り値:
*DeleteResult: 成功ステータスとリクエスト ID を含む結果オブジェクト。error: セッションの削除に失敗した場合のエラーメッセージ。
例:
package main
import (
"fmt"
"os"
"github.com/aliyun/wuying-agentbay-sdk/golang/pkg/agentbay"
)
func main() {
// Initialize the SDK
client, err := agentbay.NewAgentBay("your_api_key", nil)
if err != nil {
fmt.Printf("Error initializing AgentBay client: %v\n", err)
os.Exit(1)
}
// Create a session
createResult, err := client.Create(nil)
if err != nil {
fmt.Printf("Error creating session: %v\n", err)
os.Exit(1)
}
session := createResult.Session
fmt.Printf("Session created with ID: %s\n", session.SessionID)
// Use the session...
// Delete the session
deleteResult, err := session.Delete()
if err != nil {
fmt.Printf("Error deleting session: %v\n", err)
os.Exit(1)
}
fmt.Println("Session deleted successfully")
fmt.Printf("Request ID: %s\n", deleteResult.RequestID)
}Python
delete() -> DeleteResult戻り値:
DeleteResult: 成功ステータス、リクエスト ID、およびエラーメッセージを含む結果オブジェクト。
例:
from agentbay import AgentBay
# Initialize the SDK
agent_bay = AgentBay(api_key="your_api_key")
# Create a session
result = agent_bay.create()
if result.success:
session = result.session
print(f"Session created with ID: {session.session_id}")
# Use the session...
# Delete the session
delete_result = session.delete()
if delete_result.success:
print("Session deleted successfully")
else:
print(f"Session deletion failed: {delete_result.error_message}")TypeScript
delete(): Promise<DeleteResult>戻り値:
Promise<DeleteResult>: 成功ステータス、リクエスト ID、およびエラーメッセージを含む結果オブジェクトに解決される Promise。
例:
import { AgentBay } from 'wuying-agentbay-sdk';
// Initialize the SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });
// Create and delete a session
async function createAndDeleteSession() {
try {
const result = await agentBay.create();
if (result.success) {
const session = result.session;
console.log(`Session created with ID: ${session.sessionId}`);
// Use the session...
// Delete the session
const deleteResult = await session.delete();
if (deleteResult.success) {
console.log('Session deleted successfully');
} else {
console.log(`Session deletion failed: ${deleteResult.errorMessage}`);
}
}
} catch (error) {
console.error('Error:', error);
}
}
createAndDeleteSession();set_labels — セッションラベルの設定
Golang
SetLabels(labels map[string]string) (*models.Response, error)パラメーター:
labels(map[string]string): 設定するラベルのキーと値のペア。
戻り値:
*models.Response: リクエスト ID とステータス情報を含む応答オブジェクト。error: ラベルの設定に失敗した場合のエラーメッセージ。
例:
// Set session labels
labels := map[string]string{
"project": "demo",
"environment": "testing",
"version": "1.0.0",
}
response, err := session.SetLabels(labels)
if err != nil {
fmt.Printf("Error setting labels: %v\n", err)
os.Exit(1)
}
fmt.Println("Labels set successfully")
fmt.Printf("Request ID: %s\n", response.RequestID)Python
set_labels(labels: Dict[str, str]) -> OperationResultパラメーター:
labels(Dict[str, str]): 設定するラベルのキーと値のペア。
戻り値:
OperationResult: 成功ステータス、リクエスト ID、およびエラーメッセージを含む結果オブジェクト。
例外:
AgentBayError: API エラーまたはその他の問題によりラベルの設定が失敗した場合にスローされます。
例:
# Set session labels
labels = {
"project": "demo",
"environment": "testing",
"version": "1.0.0"
}
result = session.set_labels(labels)
if result.success:
print("Labels set successfully")
else:
print(f"Labels setting failed: {result.error_message}")TypeScript
setLabels(labels: Record<string, string>): Promise<OperationResult>パラメーター:
labels(Record<string, string>): 設定するラベルのキーと値のペア。
戻り値:
Promise<OperationResult>: リクエスト ID とステータス情報を含む結果オブジェクトに解決される Promise。
例:
// Set session labels
async function setSessionLabels(session: Session) {
try {
const labels = {
project: 'demo',
environment: 'testing',
version: '1.0.0'
};
const result = await session.setLabels(labels);
console.log(`Labels set successfully. Request ID: ${result.requestId}`);
return result;
} catch (error) {
console.error(`Labels setting failed: ${error}`);
throw error;
}
}get_labels — セッションラベルの取得
Golang
GetLabels() (map[string]string, error)戻り値:
map[string]string: セッションラベルのキーと値のペア。error: ラベルの取得に失敗した場合のエラーメッセージ。
例:
// Get session labels
labels, err := session.GetLabels()
if err != nil {
fmt.Printf("Error getting labels: %v\n", err)
os.Exit(1)
}
fmt.Println("Session labels:")
for key, value := range labels {
fmt.Printf("%s: %s\n", key, value)
}Python
get_labels() -> Dict[str, str]戻り値:
Dict[str, str]: セッションラベルのキーと値のペア。
例外:
AgentBayError: API エラーまたはその他の問題によりラベルの取得が失敗した場合にスローされます。
例:
# Get session labels
try:
labels = session.get_labels()
print(f"Session labels: {labels}")
except AgentBayError as e:
print(f"Getting labels failed: {e}")TypeScript
getLabels(): Promise<LabelResult>戻り値:
Promise<LabelResult>: セッションラベル、リクエスト ID、および成功ステータスを含む結果オブジェクトに解決される Promise。
例:
// Get session labels
async function getSessionLabels(session: Session) {
try {
const result = await session.getLabels();
console.log(`Session labels: ${JSON.stringify(result.labels)}`);
console.log(`Request ID: ${result.requestId}`);
return result.labels;
} catch (error) {
console.error(`Getting labels failed: ${error}`);
throw error;
}
}info — セッション情報の取得
Golang
Info() (*SessionInfo, error)戻り値:
*SessionInfo: セッション情報を含むオブジェクトで、SessionID、ResourceURL、およびAppIDなどの情報が含まれます。error: セッション情報の取得に失敗した場合のエラーメッセージ。
例:
// Get session information
info, err := session.Info()
if err != nil {
fmt.Printf("Error getting session info: %v\n", err)
os.Exit(1)
}
fmt.Printf("Session ID: %s\n", info.SessionID)
fmt.Printf("Resource URL: %s\n", info.ResourceURL)
fmt.Printf("App ID: %s\n", info.AppID)Python
info() -> SessionInfo戻り値:
SessionInfo: セッション情報を含むオブジェクトで、session_id、resource_url、およびapp_idなどの情報を含みます。
例外:
AgentBayError: API エラーまたはその他の問題によりセッション情報の取得が失敗した場合にスローされます。
例:
# Get session information
try:
info = session.info()
print(f"Session ID: {info.session_id}")
print(f"Resource URL: {info.resource_url}")
print(f"App ID: {info.app_id}")
except AgentBayError as e:
print(f"Getting session information failed: {e}")TypeScript
info(): Promise<InfoResult>戻り値:
Promise<InfoResult>:sessionId、resourceUrl、リクエスト ID、成功ステータスなどのセッション情報を含む結果オブジェクトを返すプロミスです。
例:
// Get session information
async function getSessionInfo(session: Session) {
try {
const result = await session.info();
console.log(`Session ID: ${result.data.sessionId}`);
console.log(`Resource URL: ${result.data.resourceUrl}`);
console.log(`Request ID: ${result.requestId}`);
return result.data;
} catch (error) {
console.error(`Getting session information failed: ${error}`);
throw error;
}
}get_link — セッションリンクの取得
Golang
GetLink(protocolType string, port int) (string, error)パラメーター:
protocolType(文字列):リンクのプロトコル型で、httpやhttpsなどがあります。Empty の場合、デフォルトプロトコルを使用します。port(整数):リンクのポート番号です。値が0の場合、デフォルトポートを使用します。
戻り値:
文字列: 会話リンク、たとえばhttps://example.com/session/123:8443。error: リンクの取得に失敗した場合のエラーメッセージ。
例:
// Get the session link using the default protocol and port
link, err := session.GetLink("", 0)
if err != nil {
fmt.Printf("Error getting link: %v\n", err)
os.Exit(1)
}
fmt.Printf("Session link: %s\n", link)
// Get the link with a custom protocol and port
customLink, err := session.GetLink("https", 8443)
if err != nil {
fmt.Printf("Error getting custom link: %v\n", err)
os.Exit(1)
}
fmt.Printf("Custom link: %s\n", customLink)Python
get_link(protocol_type: Optional[str] = None, port: Optional[int] = None) -> strパラメーター:
protocol_type(str, オプション): リンクのプロトコルタイプ ("http"や"https"など) を指定します。None の場合は、デフォルトのプロトコルが使用されます。port(int、オプション): リンクのポート番号です。Noneの場合、デフォルトポートを使用します。
戻り値:
str: セッションリンク。例:https://example.com/session/123:8443。
例外:
AgentBayError: API エラーまたはその他の問題によりリンクの取得が失敗した場合にスローされます。
例:
# Get the session link
try:
link = session.get_link()
print(f"Session link: {link}")
# Get the link with a custom protocol and port
custom_link = session.get_link("https", 8443)
print(f"Custom link: {custom_link}")
except AgentBayError as e:
print(f"Getting link failed: {e}")TypeScript
getLink(protocolType?: string, port?: number): Promise<LinkResult>パラメーター:
protocolType(文字列、オプション): リンクのプロトコルタイプ。たとえば、"http"または"https"など。指定しない場合は、デフォルトプロトコルを使用します。port(number, optional): リンクのポート番号。指定しない場合、デフォルトのポートを使用します。
戻り値:
Promise<LinkResult>: セッションリンク、リクエスト ID、および成功ステータスを含む結果オブジェクトに解決される Promise。
例:
// Get the session link
async function getSessionLink(session: Session) {
try {
const result = await session.getLink();
console.log(`Session link: ${result.data}`);
console.log(`Request ID: ${result.requestId}`);
// Get the link with a custom protocol and port
const customResult = await session.getLink('https', 8443);
console.log(`Custom link: ${customResult.data}`);
return result.data;
} catch (error) {
console.error(`Getting link failed: ${error}`);
throw error;
}
}