描述
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() {
// 初始化SDK
client, err := agentbay.NewAgentBay("your_api_key", nil)
if err != nil {
fmt.Printf("Error initializing AgentBay client: %v\n", err)
os.Exit(1)
}
// 创建会话
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)
// 使用会话...
// 删除会话
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
# 初始化SDK
agent_bay = AgentBay(api_key="your_api_key")
# 创建会话
result = agent_bay.create()
if result.success:
session = result.session
print(f"会话创建成功,ID: {session.session_id}")
# 使用会话...
# 删除会话
delete_result = session.delete()
if delete_result.success:
print("会话删除成功")
else:
print(f"会话删除失败: {delete_result.error_message}")
TypeScript
delete(): Promise<DeleteResult>=
返回值:
Promise<DeleteResult>
:返回包含成功状态、请求ID和错误信息的结果对象。
示例:
import { AgentBay } from 'wuying-agentbay-sdk';
// 初始化SDK
const agentBay = new AgentBay({ apiKey: 'your_api_key' });
// 创建并删除会话
async function createAndDeleteSession() {
try {
const result = await agentBay.create();
if (result.success) {
const session = result.session;
console.log(`会话创建成功,ID: ${session.sessionId}`);
// 使用会话...
// 删除会话
const deleteResult = await session.delete();
if (deleteResult.success) {
console.log('会话删除成功');
} else {
console.log(`会话删除失败: ${deleteResult.errorMessage}`);
}
}
} catch (error) {
console.error('错误:', error);
}
}
createAndDeleteSession();
set_labels
- 设置会话标签
Golang
SetLabels(labels map[string]string) (*models.Response, error)
参数:
labels
(map[string]string):表示要设置的标签的键值对。
返回值:
*models.Response
:包含请求ID和状态信息的响应对象。error
:若设置标签失败,返回错误信息。
示例:
// 设置会话标签
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错误或其他问题设置标签失败。
示例:
# 设置会话标签
labels = {
"project": "demo",
"environment": "testing",
"version": "1.0.0"
}
result = session.set_labels(labels)
if result.success:
print("标签设置成功")
else:
print(f"标签设置失败: {result.error_message}")
TypeScript
setLabels(labels: Record<string, string>): Promise<OperationResult>
参数:
labels
(Record<string, string>):表示要设置的标签的键值对。
返回值:
Promise<OperationResult>
:返回包含请求ID和状态信息的结果对象。
示例:
// 设置会话标签
async function setSessionLabels(session: Session) {
try {
const labels = {
project: 'demo',
environment: 'testing',
version: '1.0.0'
};
const result = await session.setLabels(labels);
console.log(`标签设置成功,请求 ID: ${result.requestId}`);
return result;
} catch (error) {
console.error(`标签设置失败: ${error}`);
throw error;
}
}
get_labels
- 获取会话标签
Golang
GetLabels() (map[string]string, error)
返回值:
map[string]string
:会话的标签键值对。error
:若获取标签失败,返回错误信息。
示例:
// 获取会话标签
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错误或其他问题获取标签失败。
示例:
# 获取会话标签
try:
labels = session.get_labels()
print(f"会话标签: {labels}")
except AgentBayError as e:
print(f"获取标签失败: {e}")
TypeScript
getLabels(): Promise<LabelResult>
返回值:
Promise<LabelResult>
:返回包含会话标签、请求ID和成功状态的结果对象。
示例:
// 获取会话标签
async function getSessionLabels(session: Session) {
try {
const result = await session.getLabels();
console.log(`会话标签: ${JSON.stringify(result.labels)}`);
console.log(`请求 ID: ${result.requestId}`);
return result.labels;
} catch (error) {
console.error(`获取标签失败: ${error}`);
throw error;
}
}
info - 获取会话信息
Golang
Info() (*SessionInfo, error)
返回值:
*SessionInfo
:包含会话信息的对象(如SessionID
、ResourceURL
、AppID
)。error
:若获取会话信息失败,返回错误信息。
示例:
// 获取会话信息
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错误或其他问题获取信息失败。
示例:
# 获取会话信息
try:
info = session.info()
print(f"会话 ID: {info.session_id}")
print(f"资源 URL: {info.resource_url}")
print(f"应用 ID: {info.app_id}")
except AgentBayError as e:
print(f"获取会话信息失败: {e}")
TypeScript
info(): Promise<InfoResult>
返回值:
Promise<InfoResult>
:返回包含会话信息(如sessionId
、resourceUrl
)、请求ID和成功状态的结果对象。
示例:
// 获取会话信息
async function getSessionInfo(session: Session) {
try {
const result = await session.info();
console.log(`会话 ID: ${result.data.sessionId}`);
console.log(`资源 URL: ${result.data.resourceUrl}`);
console.log(`请求 ID: ${result.requestId}`);
return result.data;
} catch (error) {
console.error(`获取会话信息失败: ${error}`);
throw error;
}
}
get_link
- 获取会话链接
Golang
GetLink(protocolType string, port int) (string, error)
参数:
protocolType
(string):链接的协议类型(如http
或http
)。若为空,使用默认协议。port
(int):链接的端口号。若为0
,使用默认端口。
返回值:
string
:会话链接(如https://example.com/session/123:8443
)。error
:若获取链接失败,返回错误信息。
示例:
// 获取使用默认协议和端口的会话链接
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)
// 获取自定义协议和端口的链接
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"
)。若为空,使用默认协议。port
(int, 可选):链接的端口号。若为None
,使用默认端口。
返回值:
str
:会话链接(如https://example.com/session/123:8443
)。
异常:
AgentBayError
:因API错误或其他问题获取链接失败。
示例:
# 获取会话链接
try:
link = session.get_link()
print(f"会话链接: {link}")
# 获取自定义协议和端口的链接
custom_link = session.get_link("https", 8443)
print(f"自定义链接: {custom_link}")
except AgentBayError as e:
print(f"获取链接失败: {e}")
TypeScript
getLink(protocolType?: string, port?: number): Promise<LinkResult>
参数:
protocolType
(string,可选):链接的协议类型(如"http"
或"https"
)。若未指定,使用默认协议。port
(number,可选):链接的端口号。若未指定,使用默认端口。
返回值:
Promise<LinkResult>
:返回包含会话链接、请求ID和成功状态的结果对象。
示例:
// 获取会话链接
async function getSessionLink(session: Session) {
try {
const result = await session.getLink();
console.log(`会话链接: ${result.data}`);
console.log(`请求 ID: ${result.requestId}`);
// 获取自定义协议和端口的链接
const customResult = await session.getLink('https', 8443);
console.log(`自定义链接: ${customResult.data}`);
return result.data;
} catch (error) {
console.error(`获取链接失败: ${error}`);
throw error;
}
}