wuying-agentbay-sdk 0.10.2 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-BVWUCG4J.mjs +3402 -0
- package/dist/chunk-BVWUCG4J.mjs.map +1 -0
- package/dist/chunk-SL5GCAQE.cjs +3393 -0
- package/dist/chunk-SL5GCAQE.cjs.map +1 -0
- package/dist/index.cjs +4623 -3978
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +3934 -1964
- package/dist/index.d.ts +3934 -1964
- package/dist/index.mjs +4601 -3956
- package/dist/index.mjs.map +1 -1
- package/dist/key-normalizer-AF7APGQZ.cjs +136 -0
- package/dist/key-normalizer-AF7APGQZ.cjs.map +1 -0
- package/dist/key-normalizer-QDBRLFHF.mjs +145 -0
- package/dist/key-normalizer-QDBRLFHF.mjs.map +1 -0
- package/dist/model-CNCGFWJH.cjs +200 -0
- package/dist/model-CNCGFWJH.cjs.map +1 -0
- package/dist/model-LGWQJWKQ.mjs +209 -0
- package/docs/api/README.md +26 -0
- package/docs/api/browser-use/browser.md +177 -0
- package/docs/api/browser-use/extension.md +284 -0
- package/docs/api/codespace/code.md +77 -0
- package/docs/api/common-features/advanced/agent.md +84 -0
- package/docs/api/common-features/advanced/oss.md +221 -0
- package/docs/api/common-features/basics/agentbay.md +277 -0
- package/docs/api/common-features/basics/command.md +83 -0
- package/docs/api/common-features/basics/context-manager.md +149 -0
- package/docs/api/common-features/basics/context-sync.md +51 -0
- package/docs/api/common-features/basics/context.md +348 -0
- package/docs/api/common-features/basics/filesystem.md +499 -0
- package/docs/api/common-features/basics/logging.md +77 -0
- package/docs/api/common-features/basics/session.md +542 -0
- package/docs/api/computer-use/computer.md +786 -0
- package/docs/api/mobile-use/mobile.md +395 -0
- package/docs/examples/README.md +332 -0
- package/docs/examples/basic-usage.ts +86 -0
- package/docs/examples/browser-use/browser/README.md +356 -0
- package/docs/examples/browser-use/browser/basic-usage.ts +136 -0
- package/docs/examples/browser-use/browser/browser-command-args.ts +117 -0
- package/docs/examples/browser-use/browser/browser-context-cookie-persistence.ts +348 -0
- package/docs/examples/browser-use/browser/browser-fingerprint-basic-usage.ts +121 -0
- package/docs/examples/browser-use/browser/browser-fingerprint-construct.ts +114 -0
- package/docs/examples/browser-use/browser/browser-fingerprint-local-sync.ts +98 -0
- package/docs/examples/browser-use/browser/browser-fingerprint-persistence.ts +242 -0
- package/docs/examples/browser-use/browser/browser-proxies.ts +149 -0
- package/docs/examples/browser-use/browser/browser-type-example.ts +266 -0
- package/docs/examples/browser-use/browser/browser-viewport.ts +129 -0
- package/docs/examples/browser-use/browser/call_for_user_jd.ts +184 -0
- package/docs/examples/browser-use/browser/captcha_tongcheng.ts +151 -0
- package/docs/examples/browser-use/browser/run-2048.ts +209 -0
- package/docs/examples/browser-use/browser/run-sudoku.ts +150 -0
- package/docs/examples/browser-use/browser/screenshot-example.ts +132 -0
- package/docs/examples/browser-use/extension-example/README.md +252 -0
- package/docs/examples/browser-use/extension-example/extension-example.ts +381 -0
- package/docs/examples/codespace/automation/automation-example.ts +322 -0
- package/docs/examples/common-features/advanced/agent-module-example/README.md +40 -0
- package/docs/examples/common-features/advanced/agent-module-example.ts +66 -0
- package/docs/examples/common-features/advanced/archive-upload-mode-example/README.md +212 -0
- package/docs/examples/common-features/advanced/archive-upload-mode-example/archive-upload-mode-example.ts +213 -0
- package/docs/examples/common-features/advanced/vpc-session-example/README.md +47 -0
- package/docs/examples/common-features/advanced/vpc-session-example.ts +106 -0
- package/docs/examples/common-features/basics/archive-upload-mode-example/README.md +236 -0
- package/docs/examples/common-features/basics/archive-upload-mode-example/main.ts +236 -0
- package/docs/examples/common-features/basics/command-example/README.md +47 -0
- package/docs/examples/common-features/basics/command-example/command-example.ts +153 -0
- package/docs/examples/common-features/basics/context-management/README.md +55 -0
- package/docs/examples/common-features/basics/context-management/context-management.ts +140 -0
- package/docs/examples/common-features/basics/data-persistence/README.md +129 -0
- package/docs/examples/common-features/basics/data-persistence/context-sync-demo.md +144 -0
- package/docs/examples/common-features/basics/data-persistence/context-sync-demo.ts +275 -0
- package/docs/examples/common-features/basics/data-persistence/data-persistence.ts +259 -0
- package/docs/examples/common-features/basics/data-persistence/recycle-policy-example.ts +294 -0
- package/docs/examples/common-features/basics/filesystem-example/README.md +57 -0
- package/docs/examples/common-features/basics/filesystem-example/filesystem-example.ts +164 -0
- package/docs/examples/common-features/basics/filesystem-example/filesystem-filetransfer-example.ts +153 -0
- package/docs/examples/common-features/basics/filesystem-example/watch-directory-example.ts +168 -0
- package/docs/examples/common-features/basics/get/README.md +136 -0
- package/docs/examples/common-features/basics/get/main.ts +79 -0
- package/docs/examples/common-features/basics/list_sessions/README.md +54 -0
- package/docs/examples/common-features/basics/list_sessions/main.ts +258 -0
- package/docs/examples/common-features/basics/mcp_tool_direct_call/README.md +142 -0
- package/docs/examples/common-features/basics/mcp_tool_direct_call/main.ts +135 -0
- package/docs/examples/common-features/basics/session-creation/README.md +28 -0
- package/docs/examples/common-features/basics/session-creation/session-creation.ts +295 -0
- package/docs/examples/common-features/basics/session-pause-resume/README.md +53 -0
- package/docs/examples/common-features/basics/session-pause-resume/session-pause-resume.ts +237 -0
- package/docs/examples/mobile-use/mobile-get-adb-url/README.md +92 -0
- package/docs/examples/mobile-use/mobile-get-adb-url/index.ts +80 -0
- package/docs/examples/mobile-use/mobile-get-adb-url/package-lock.json +279 -0
- package/docs/examples/mobile-use/mobile-get-adb-url/package.json +18 -0
- package/docs/examples/mobile-use/mobile-simulate-basic-usage.ts +202 -0
- package/docs/examples/mobile-use/mobile-simulate-with-ctx.ts +170 -0
- package/package.json +19 -12
- package/dist/application-KZWXH46T.mjs +0 -17
- package/dist/application-LMA7KSRH.cjs +0 -8
- package/dist/application-LMA7KSRH.cjs.map +0 -1
- package/dist/chunk-BQNGKBQF.mjs +0 -386
- package/dist/chunk-BQNGKBQF.mjs.map +0 -1
- package/dist/chunk-IOVGAAJL.cjs +0 -558
- package/dist/chunk-IOVGAAJL.cjs.map +0 -1
- package/dist/chunk-UF2TC2R4.mjs +0 -567
- package/dist/chunk-UF2TC2R4.mjs.map +0 -1
- package/dist/chunk-X6MS7Z5L.cjs +0 -377
- package/dist/chunk-X6MS7Z5L.cjs.map +0 -1
- package/dist/window-DH37ZDD5.mjs +0 -17
- package/dist/window-DH37ZDD5.mjs.map +0 -1
- package/dist/window-U7N3H735.cjs +0 -8
- package/dist/window-U7N3H735.cjs.map +0 -1
- /package/dist/{application-KZWXH46T.mjs.map → model-LGWQJWKQ.mjs.map} +0 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Agent Module Example
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to use the Agent module to execute AI-powered tasks using natural language descriptions.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The Agent module allows you to execute complex tasks using human-readable instructions, monitor task status, and terminate running tasks. This capability extends the functionality of cloud sessions by adding AI-powered automation.
|
|
8
|
+
|
|
9
|
+
## Running the Example
|
|
10
|
+
|
|
11
|
+
1. Ensure you have the AgentBay SDK installed
|
|
12
|
+
2. Set the `AGENTBAY_API_KEY` environment variable with your valid API key
|
|
13
|
+
3. Run the example:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd docs/examples/typescript
|
|
17
|
+
npx ts-node agent-module-example.ts
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## What the Example Does
|
|
21
|
+
|
|
22
|
+
1. Initializes the AgentBay client with your API key
|
|
23
|
+
2. Creates a new session
|
|
24
|
+
3. Executes a simple task using the Agent module ("Calculate the square root of 144")
|
|
25
|
+
4. Displays the task results
|
|
26
|
+
5. Cleans up by deleting the session
|
|
27
|
+
|
|
28
|
+
## Key Concepts
|
|
29
|
+
|
|
30
|
+
- **Session Creation**: All Agent operations happen within a session context
|
|
31
|
+
- **Task Execution**: Tasks are executed using natural language descriptions
|
|
32
|
+
- **Result Handling**: Task results include success status, output, and error information
|
|
33
|
+
- **Resource Management**: Sessions should be properly deleted when no longer needed
|
|
34
|
+
|
|
35
|
+
## Next Steps
|
|
36
|
+
|
|
37
|
+
Try modifying the task description to perform different operations, such as:
|
|
38
|
+
- "Find the current weather in New York City"
|
|
39
|
+
- "Calculate the factorial of 10"
|
|
40
|
+
- "List the prime numbers between 1 and 100"
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic example of using the Agent module to execute tasks.
|
|
3
|
+
* This example demonstrates:
|
|
4
|
+
* - Creating a session with Agent capabilities
|
|
5
|
+
* - Executing a simple task using natural language
|
|
6
|
+
* - Handling task results
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { AgentBay } from 'wuying-agentbay-sdk';
|
|
10
|
+
|
|
11
|
+
async function main() {
|
|
12
|
+
// Get API key from environment variable
|
|
13
|
+
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
14
|
+
if (!apiKey) {
|
|
15
|
+
console.log("Error: AGENTBAY_API_KEY environment variable not set");
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Initialize AgentBay client
|
|
20
|
+
console.log("Initializing AgentBay client...");
|
|
21
|
+
const agentBay = new AgentBay({ apiKey });
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// Create a session with Windows latest image for Agent capabilities
|
|
25
|
+
console.log("Creating a new session with Windows latest image...");
|
|
26
|
+
const sessionResult = await agentBay.create({ imageId: "windows_latest" });
|
|
27
|
+
|
|
28
|
+
if (sessionResult.success) {
|
|
29
|
+
const session = sessionResult.session;
|
|
30
|
+
console.log(`Session created with ID: ${session.sessionId}`);
|
|
31
|
+
|
|
32
|
+
// Execute a task using the Agent
|
|
33
|
+
const taskDescription = "Calculate the square root of 144";
|
|
34
|
+
console.log(`Executing task: ${taskDescription}`);
|
|
35
|
+
|
|
36
|
+
const executionResult = await session.agent.executeTask(taskDescription, 5);
|
|
37
|
+
|
|
38
|
+
if (executionResult.success) {
|
|
39
|
+
console.log("Task completed successfully!");
|
|
40
|
+
console.log(`Task ID: ${executionResult.taskId}`);
|
|
41
|
+
console.log(`Task status: ${executionResult.taskStatus}`);
|
|
42
|
+
} else {
|
|
43
|
+
console.log(`Task failed: ${executionResult.errorMessage}`);
|
|
44
|
+
if (executionResult.taskId) {
|
|
45
|
+
console.log(`Task ID: ${executionResult.taskId}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Clean up - delete the session
|
|
50
|
+
const deleteResult = await agentBay.delete(session);
|
|
51
|
+
if (deleteResult.success) {
|
|
52
|
+
console.log("Session deleted successfully");
|
|
53
|
+
} else {
|
|
54
|
+
console.log(`Failed to delete session: ${deleteResult.errorMessage}`);
|
|
55
|
+
}
|
|
56
|
+
} else {
|
|
57
|
+
console.log(`Failed to create session: ${sessionResult.errorMessage}`);
|
|
58
|
+
}
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error("Error in main function:", error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (require.main === module) {
|
|
65
|
+
main().catch(console.error);
|
|
66
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
# Archive Upload Mode Context Sync Example
|
|
2
|
+
|
|
3
|
+
This directory contains examples demonstrating the Archive upload mode functionality for context synchronization in the AgentBay SDK.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The Archive upload mode is designed for efficient file transfer by compressing files before uploading them to the context storage. This is particularly useful when:
|
|
8
|
+
|
|
9
|
+
- Working with large files
|
|
10
|
+
- Dealing with many files
|
|
11
|
+
- Optimizing bandwidth usage
|
|
12
|
+
- Reducing upload time for compressible content
|
|
13
|
+
|
|
14
|
+
## Files
|
|
15
|
+
|
|
16
|
+
### `archive-upload-mode-example.ts`
|
|
17
|
+
|
|
18
|
+
A comprehensive example that demonstrates:
|
|
19
|
+
|
|
20
|
+
1. **Context Creation**: Creating a context for Archive upload mode
|
|
21
|
+
2. **Sync Policy Configuration**: Setting up sync policy with Archive uploadMode
|
|
22
|
+
3. **Session Management**: Creating and managing sessions with context sync
|
|
23
|
+
4. **File Operations**: Writing files to the context path
|
|
24
|
+
5. **Context Info**: Retrieving context status information
|
|
25
|
+
6. **File Verification**: Verifying file information and properties
|
|
26
|
+
7. **Cleanup**: Proper session cleanup and error handling
|
|
27
|
+
|
|
28
|
+
## Key Features Demonstrated
|
|
29
|
+
|
|
30
|
+
### Archive Upload Mode Configuration
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
// Configure sync policy with Archive upload mode
|
|
34
|
+
const syncPolicy = newSyncPolicy();
|
|
35
|
+
syncPolicy.uploadPolicy!.uploadMode = "Archive"; // Set to Archive mode
|
|
36
|
+
|
|
37
|
+
// Create context sync with Archive mode
|
|
38
|
+
const contextSync = newContextSync(
|
|
39
|
+
contextId,
|
|
40
|
+
"/tmp/archive-mode-test",
|
|
41
|
+
syncPolicy
|
|
42
|
+
);
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Session Creation with Context Sync
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
const sessionParams: CreateSessionParams = {
|
|
49
|
+
labels: {
|
|
50
|
+
example: `archive-mode-${uniqueId}`,
|
|
51
|
+
type: "archive-upload-demo",
|
|
52
|
+
uploadMode: "Archive"
|
|
53
|
+
},
|
|
54
|
+
contextSync: [contextSync]
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const sessionResult = await agentBay.create(sessionParams);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### File Operations
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
const fileSystem = new FileSystem(session);
|
|
64
|
+
|
|
65
|
+
// Write file to context path
|
|
66
|
+
const writeResult = await fileSystem.writeFile(filePath, fileContent, "overwrite");
|
|
67
|
+
|
|
68
|
+
// Get file information
|
|
69
|
+
const fileInfoResult = await fileSystem.getFileInfo(filePath);
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Context Information Retrieval
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
// Get context status information
|
|
76
|
+
const infoResult = await session.context.info();
|
|
77
|
+
|
|
78
|
+
// Display context status details
|
|
79
|
+
infoResult.contextStatusData.forEach((status, index) => {
|
|
80
|
+
console.log(`Context ID: ${status.contextId}`);
|
|
81
|
+
console.log(`Path: ${status.path}`);
|
|
82
|
+
console.log(`Status: ${status.status}`);
|
|
83
|
+
console.log(`Task Type: ${status.taskType}`);
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Running the Example
|
|
88
|
+
|
|
89
|
+
### Prerequisites
|
|
90
|
+
|
|
91
|
+
1. **Environment Setup**: Set your AgentBay API key
|
|
92
|
+
```bash
|
|
93
|
+
export AGENTBAY_API_KEY="your-api-key-here"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
2. **Dependencies**: Ensure you have the AgentBay SDK installed
|
|
97
|
+
```bash
|
|
98
|
+
npm install
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Execution
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# Navigate to the project root
|
|
105
|
+
cd /path/to/wuying-agentbay-sdk
|
|
106
|
+
|
|
107
|
+
# Run the example
|
|
108
|
+
npx ts-node docs/example/archive-upload-mode-example.ts
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Expected Output
|
|
112
|
+
|
|
113
|
+
The example will output detailed logs showing:
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
🚀 Archive Upload Mode Context Sync Example
|
|
117
|
+
============================================================
|
|
118
|
+
|
|
119
|
+
📦 Step 1: Creating context for Archive upload mode...
|
|
120
|
+
✅ Context created successfully!
|
|
121
|
+
Context ID: ctx_xxxxx
|
|
122
|
+
Request ID: req_xxxxx
|
|
123
|
+
|
|
124
|
+
⚙️ Step 2: Configuring sync policy with Archive upload mode...
|
|
125
|
+
✅ Sync policy configured with uploadMode: Archive
|
|
126
|
+
|
|
127
|
+
🔧 Step 3: Creating context sync configuration...
|
|
128
|
+
✅ Context sync created:
|
|
129
|
+
Context ID: ctx_xxxxx
|
|
130
|
+
Path: /tmp/archive-mode-test
|
|
131
|
+
Upload Mode: Archive
|
|
132
|
+
|
|
133
|
+
🏗️ Step 4: Creating session with Archive mode context sync...
|
|
134
|
+
✅ Session created successfully!
|
|
135
|
+
Session ID: sess_xxxxx
|
|
136
|
+
Request ID: req_xxxxx
|
|
137
|
+
App Instance ID: app_xxxxx
|
|
138
|
+
|
|
139
|
+
📝 Step 5: Creating test files in Archive mode context...
|
|
140
|
+
📄 Creating file: /tmp/archive-mode-test/test-file-5kb.txt
|
|
141
|
+
📊 File content size: 5120 bytes
|
|
142
|
+
✅ File write successful!
|
|
143
|
+
Request ID: req_xxxxx
|
|
144
|
+
|
|
145
|
+
📊 Step 6: Testing context info functionality...
|
|
146
|
+
✅ Context info retrieved successfully!
|
|
147
|
+
Request ID: req_xxxxx
|
|
148
|
+
Context status data count: X
|
|
149
|
+
|
|
150
|
+
📋 Context status details:
|
|
151
|
+
[0] Context ID: ctx_xxxxx
|
|
152
|
+
Path: /tmp/archive-mode-test
|
|
153
|
+
Status: Success
|
|
154
|
+
Task Type: upload
|
|
155
|
+
|
|
156
|
+
🔍 Step 7: Verifying file information...
|
|
157
|
+
✅ File info retrieved successfully!
|
|
158
|
+
Request ID: req_xxxxx
|
|
159
|
+
📄 File details:
|
|
160
|
+
Size: 5120 bytes
|
|
161
|
+
Is Directory: false
|
|
162
|
+
Modified Time: 2025-10-22T09:52:00Z
|
|
163
|
+
Mode: 644
|
|
164
|
+
|
|
165
|
+
🎉 Archive upload mode example completed successfully!
|
|
166
|
+
✅ All operations completed without errors.
|
|
167
|
+
|
|
168
|
+
🧹 Step 8: Cleaning up session...
|
|
169
|
+
✅ Session deleted successfully!
|
|
170
|
+
Success: true
|
|
171
|
+
Request ID: req_xxxxx
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Related Documentation
|
|
175
|
+
|
|
176
|
+
- [Context Sync Documentation](../../../../../../docs/guides/common-features/basics/data-persistence.md)
|
|
177
|
+
- [Session Management Guide](../../../../../../docs/guides/common-features/basics/session-management.md)
|
|
178
|
+
- [File Operations Guide](../../../../../../docs/guides/common-features/basics/file-operations.md)
|
|
179
|
+
|
|
180
|
+
## Troubleshooting
|
|
181
|
+
|
|
182
|
+
### Common Issues
|
|
183
|
+
|
|
184
|
+
1. **API Key Not Set**
|
|
185
|
+
```
|
|
186
|
+
Warning: AGENTBAY_API_KEY environment variable not set
|
|
187
|
+
```
|
|
188
|
+
**Solution**: Set the environment variable or update the API key in the code
|
|
189
|
+
|
|
190
|
+
2. **Context Creation Failed**
|
|
191
|
+
```
|
|
192
|
+
Context creation failed: [error message]
|
|
193
|
+
```
|
|
194
|
+
**Solution**: Check your API key and network connectivity
|
|
195
|
+
|
|
196
|
+
3. **Session Creation Failed**
|
|
197
|
+
```
|
|
198
|
+
Session creation failed: [error message]
|
|
199
|
+
```
|
|
200
|
+
**Solution**: Verify context sync configuration and try again
|
|
201
|
+
|
|
202
|
+
4. **File Operation Failed**
|
|
203
|
+
```
|
|
204
|
+
File write failed: [error message]
|
|
205
|
+
```
|
|
206
|
+
**Solution**: Check file path permissions and available disk space
|
|
207
|
+
|
|
208
|
+
## Support
|
|
209
|
+
|
|
210
|
+
For additional help:
|
|
211
|
+
- [GitHub Issues](https://github.com/aliyun/wuying-agentbay-sdk/issues)
|
|
212
|
+
- [Documentation Home](../../../../../../README.md)
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { AgentBay, CreateSessionParams, Session, newContextSync, newSyncPolicy, FileSystem } from "wuying-agentbay-sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Archive Upload Mode Context Sync Example
|
|
5
|
+
*
|
|
6
|
+
* This example demonstrates how to use Archive upload mode for context synchronization.
|
|
7
|
+
* Archive mode compresses files before uploading, which is more efficient for large files
|
|
8
|
+
* or when dealing with many files.
|
|
9
|
+
*
|
|
10
|
+
* Features demonstrated:
|
|
11
|
+
* - Creating context with Archive upload mode
|
|
12
|
+
* - Session creation with context sync
|
|
13
|
+
* - File operations in the context path
|
|
14
|
+
* - Context sync and info operations
|
|
15
|
+
* - Proper cleanup and error handling
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
function getAPIKey(): string {
|
|
19
|
+
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
20
|
+
if (!apiKey) {
|
|
21
|
+
console.warn("Warning: AGENTBAY_API_KEY environment variable not set. Using default key.");
|
|
22
|
+
return "your-api-key-here"; // Replace with your actual API key
|
|
23
|
+
}
|
|
24
|
+
return apiKey;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function generateUniqueId(): string {
|
|
28
|
+
const timestamp = Date.now() * 1000 + Math.floor(Math.random() * 1000);
|
|
29
|
+
const randomPart = Math.floor(Math.random() * 10000);
|
|
30
|
+
return `${timestamp}-${randomPart}`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function archiveUploadModeExample(): Promise<void> {
|
|
34
|
+
console.log("🚀 Archive Upload Mode Context Sync Example");
|
|
35
|
+
console.log("=" .repeat(60));
|
|
36
|
+
|
|
37
|
+
// Initialize AgentBay client
|
|
38
|
+
const agentBay = new AgentBay({ apiKey: getAPIKey() });
|
|
39
|
+
const uniqueId = generateUniqueId();
|
|
40
|
+
|
|
41
|
+
let session: Session | null = null;
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
// Step 1: Create context for Archive mode
|
|
45
|
+
console.log("\n📦 Step 1: Creating context for Archive upload mode...");
|
|
46
|
+
const contextName = `archive-mode-context-${uniqueId}`;
|
|
47
|
+
const contextResult = await agentBay.context.get(contextName, true);
|
|
48
|
+
|
|
49
|
+
if (!contextResult.success) {
|
|
50
|
+
throw new Error(`Context creation failed: ${contextResult.errorMessage}`);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
console.log(`✅ Context created successfully!`);
|
|
54
|
+
console.log(` Context ID: ${contextResult.contextId}`);
|
|
55
|
+
console.log(` Request ID: ${contextResult.requestId}`);
|
|
56
|
+
|
|
57
|
+
// Step 2: Configure sync policy with Archive upload mode
|
|
58
|
+
console.log("\n⚙️ Step 2: Configuring sync policy with Archive upload mode...");
|
|
59
|
+
const syncPolicy = newSyncPolicy();
|
|
60
|
+
syncPolicy.uploadPolicy!.uploadMode = "Archive"; // Set to Archive mode
|
|
61
|
+
|
|
62
|
+
console.log(`✅ Sync policy configured with uploadMode: ${syncPolicy.uploadPolicy!.uploadMode}`);
|
|
63
|
+
|
|
64
|
+
// Step 3: Create context sync configuration
|
|
65
|
+
console.log("\n🔧 Step 3: Creating context sync configuration...");
|
|
66
|
+
const contextSync = newContextSync(
|
|
67
|
+
contextResult.contextId,
|
|
68
|
+
"/tmp/archive-mode-test",
|
|
69
|
+
syncPolicy
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
console.log(`✅ Context sync created:`);
|
|
73
|
+
console.log(` Context ID: ${contextSync.contextId}`);
|
|
74
|
+
console.log(` Path: ${contextSync.path}`);
|
|
75
|
+
console.log(` Upload Mode: ${contextSync.policy?.uploadPolicy?.uploadMode}`);
|
|
76
|
+
|
|
77
|
+
// Step 4: Create session with Archive mode context sync
|
|
78
|
+
console.log("\n🏗️ Step 4: Creating session with Archive mode context sync...");
|
|
79
|
+
const sessionParams: CreateSessionParams = {
|
|
80
|
+
labels: {
|
|
81
|
+
example: `archive-mode-${uniqueId}`,
|
|
82
|
+
type: "archive-upload-demo",
|
|
83
|
+
uploadMode: "Archive"
|
|
84
|
+
},
|
|
85
|
+
contextSync: [contextSync]
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const sessionResult = await agentBay.create(sessionParams);
|
|
89
|
+
if (!sessionResult.success) {
|
|
90
|
+
throw new Error(`Session creation failed: ${sessionResult.errorMessage}`);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
session = sessionResult.session!;
|
|
94
|
+
console.log(`✅ Session created successfully!`);
|
|
95
|
+
console.log(` Session ID: ${session!.sessionId}`);
|
|
96
|
+
console.log(` Request ID: ${sessionResult.requestId}`);
|
|
97
|
+
|
|
98
|
+
// Get session info to verify setup
|
|
99
|
+
const sessionInfo = await agentBay.getSession(session!.sessionId);
|
|
100
|
+
if (sessionInfo.success && sessionInfo.data) {
|
|
101
|
+
console.log(` App Instance ID: ${sessionInfo.data.appInstanceId}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Step 5: Create and write test files
|
|
105
|
+
console.log("\n📝 Step 5: Creating test files in Archive mode context...");
|
|
106
|
+
const fileSystem = new FileSystem(session!);
|
|
107
|
+
|
|
108
|
+
// Generate 5KB test content
|
|
109
|
+
const contentSize = 5 * 1024; // 5KB
|
|
110
|
+
const baseContent = "Archive mode test successful! This is a test file created in the session path. ";
|
|
111
|
+
const repeatedContent = baseContent.repeat(Math.ceil(contentSize / baseContent.length));
|
|
112
|
+
const fileContent = repeatedContent.substring(0, contentSize);
|
|
113
|
+
|
|
114
|
+
const filePath = "/tmp/archive-mode-test/test-file-5kb.txt";
|
|
115
|
+
|
|
116
|
+
console.log(`📄 Creating file: ${filePath}`);
|
|
117
|
+
console.log(`📊 File content size: ${fileContent.length} bytes`);
|
|
118
|
+
|
|
119
|
+
const writeResult = await fileSystem.writeFile(filePath, fileContent, "overwrite");
|
|
120
|
+
|
|
121
|
+
if (!writeResult.success) {
|
|
122
|
+
throw new Error(`File write failed: ${writeResult.errorMessage}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
console.log(`✅ File write successful!`);
|
|
126
|
+
console.log(` Request ID: ${writeResult.requestId}`);
|
|
127
|
+
|
|
128
|
+
// Step 6: Test context info functionality
|
|
129
|
+
console.log("\n📊 Step 6: Testing context info functionality...");
|
|
130
|
+
const infoResult = await session!.context.info();
|
|
131
|
+
|
|
132
|
+
if (!infoResult.success) {
|
|
133
|
+
throw new Error(`Context info failed: ${infoResult.errorMessage}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
console.log(`✅ Context info retrieved successfully!`);
|
|
137
|
+
console.log(` Request ID: ${infoResult.requestId}`);
|
|
138
|
+
console.log(` Context status data count: ${infoResult.contextStatusData.length}`);
|
|
139
|
+
|
|
140
|
+
// Display context status details
|
|
141
|
+
if (infoResult.contextStatusData.length > 0) {
|
|
142
|
+
console.log("\n📋 Context status details:");
|
|
143
|
+
infoResult.contextStatusData.forEach((status, index) => {
|
|
144
|
+
console.log(` [${index}] Context ID: ${status.contextId}`);
|
|
145
|
+
console.log(` Path: ${status.path}`);
|
|
146
|
+
console.log(` Status: ${status.status}`);
|
|
147
|
+
console.log(` Task Type: ${status.taskType}`);
|
|
148
|
+
if (status.errorMessage) {
|
|
149
|
+
console.log(` Error: ${status.errorMessage}`);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Step 7: Verify file information
|
|
155
|
+
console.log("\n🔍 Step 7: Verifying file information...");
|
|
156
|
+
const fileInfoResult = await fileSystem.getFileInfo(filePath);
|
|
157
|
+
|
|
158
|
+
if (!fileInfoResult.success) {
|
|
159
|
+
throw new Error(`Get file info failed: ${fileInfoResult.errorMessage}`);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
console.log(`✅ File info retrieved successfully!`);
|
|
163
|
+
console.log(` Request ID: ${fileInfoResult.requestId}`);
|
|
164
|
+
|
|
165
|
+
if (fileInfoResult.fileInfo) {
|
|
166
|
+
console.log(`📄 File details:`);
|
|
167
|
+
console.log(` Size: ${fileInfoResult.fileInfo.size} bytes`);
|
|
168
|
+
console.log(` Is Directory: ${fileInfoResult.fileInfo.isDirectory}`);
|
|
169
|
+
console.log(` Modified Time: ${fileInfoResult.fileInfo.modTime}`);
|
|
170
|
+
console.log(` Mode: ${fileInfoResult.fileInfo.mode}`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
console.log("\n🎉 Archive upload mode example completed successfully!");
|
|
174
|
+
console.log("✅ All operations completed without errors.");
|
|
175
|
+
|
|
176
|
+
} catch (error) {
|
|
177
|
+
console.error("\n❌ Error occurred during example execution:");
|
|
178
|
+
console.error(error);
|
|
179
|
+
} finally {
|
|
180
|
+
// Step 8: Cleanup
|
|
181
|
+
if (session) {
|
|
182
|
+
console.log("\n🧹 Step 8: Cleaning up session...");
|
|
183
|
+
try {
|
|
184
|
+
const deleteResult = await agentBay.delete(session, true);
|
|
185
|
+
console.log(`✅ Session deleted successfully!`);
|
|
186
|
+
console.log(` Success: ${deleteResult.success}`);
|
|
187
|
+
console.log(` Request ID: ${deleteResult.requestId}`);
|
|
188
|
+
} catch (deleteError) {
|
|
189
|
+
console.error(`❌ Failed to delete session: ${deleteError}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Main execution
|
|
196
|
+
async function main(): Promise<void> {
|
|
197
|
+
try {
|
|
198
|
+
await archiveUploadModeExample();
|
|
199
|
+
} catch (error) {
|
|
200
|
+
console.error("❌ Example execution failed:", error);
|
|
201
|
+
process.exit(1);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Run the example if this file is executed directly
|
|
206
|
+
if (require.main === module) {
|
|
207
|
+
main().catch((error) => {
|
|
208
|
+
console.error("❌ Unhandled error:", error);
|
|
209
|
+
process.exit(1);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export { archiveUploadModeExample };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# VPC Session Example
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to create and use VPC (Virtual Private Cloud) sessions with the AgentBay SDK.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
VPC sessions provide enhanced security and networking capabilities by running your cloud sessions within a private network environment. This example shows how to create a VPC session and use its available modules.
|
|
8
|
+
|
|
9
|
+
## Running the Example
|
|
10
|
+
|
|
11
|
+
1. Ensure you have the AgentBay SDK installed
|
|
12
|
+
2. Set the `AGENTBAY_API_KEY` environment variable with your valid API key
|
|
13
|
+
3. Run the example:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
cd docs/examples/typescript
|
|
17
|
+
npx ts-node vpc-session-example.ts
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## What the Example Does
|
|
21
|
+
|
|
22
|
+
1. Initializes the AgentBay client with your API key
|
|
23
|
+
2. Creates a new VPC session with specific parameters
|
|
24
|
+
3. Tests FileSystem operations by writing and reading a file
|
|
25
|
+
4. Tests Command operations by executing system commands
|
|
26
|
+
5. Cleans up by deleting the session
|
|
27
|
+
|
|
28
|
+
## Key Concepts
|
|
29
|
+
|
|
30
|
+
- **VPC Session Creation**: VPC sessions are created by setting the `isVpc` parameter to `true`
|
|
31
|
+
- **Module Availability**: VPC sessions have limited module availability compared to standard sessions
|
|
32
|
+
- **Resource Management**: VPC sessions should be properly deleted when no longer needed
|
|
33
|
+
- **Security**: VPC sessions provide network isolation for sensitive operations
|
|
34
|
+
|
|
35
|
+
## Available Modules in VPC Sessions
|
|
36
|
+
|
|
37
|
+
1. **FileSystem**: File and directory operations
|
|
38
|
+
2. **Command**: Command execution
|
|
39
|
+
3. **System Tools**: Specialized system-level tools
|
|
40
|
+
4. **Browser Tools**: Browser automation capabilities
|
|
41
|
+
|
|
42
|
+
## Next Steps
|
|
43
|
+
|
|
44
|
+
Try modifying the example to:
|
|
45
|
+
- Use context synchronization with VPC sessions
|
|
46
|
+
- Test different command executions
|
|
47
|
+
- Implement error handling for unavailable modules
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Basic example of creating and using a VPC session.
|
|
3
|
+
* This example demonstrates:
|
|
4
|
+
* - Creating a VPC session with specific parameters
|
|
5
|
+
* - Using FileSystem operations in a VPC session
|
|
6
|
+
* - Using Command execution in a VPC session
|
|
7
|
+
* - Proper session cleanup
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { AgentBay, newCreateSessionParams } from 'wuying-agentbay-sdk';
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
// Get API key from environment variable
|
|
14
|
+
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
15
|
+
if (!apiKey) {
|
|
16
|
+
console.log("Error: AGENTBAY_API_KEY environment variable not set");
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Initialize AgentBay client
|
|
21
|
+
console.log("Initializing AgentBay client...");
|
|
22
|
+
const agentBay = new AgentBay({ apiKey });
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
// Create a VPC session
|
|
26
|
+
console.log("Creating a VPC session...");
|
|
27
|
+
const params = newCreateSessionParams()
|
|
28
|
+
.withImageId("linux_latest")
|
|
29
|
+
.withIsVpc(true)
|
|
30
|
+
.withLabels({
|
|
31
|
+
"test-type": "vpc-basic-example",
|
|
32
|
+
"purpose": "demonstration",
|
|
33
|
+
"timestamp": Math.floor(Date.now() / 1000).toString()
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const sessionResult = await agentBay.create(params);
|
|
37
|
+
|
|
38
|
+
if (!sessionResult.success) {
|
|
39
|
+
console.log(`Failed to create VPC session: ${sessionResult.errorMessage}`);
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const session = sessionResult.session;
|
|
44
|
+
console.log(`VPC session created successfully with ID: ${session.sessionId}`);
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// Test FileSystem operations
|
|
48
|
+
console.log("\n--- Testing FileSystem operations ---");
|
|
49
|
+
const testFilePath = "/tmp/vpc_example_test.txt";
|
|
50
|
+
const testContent = `Hello from VPC session! Created at ${new Date().toISOString()}`;
|
|
51
|
+
|
|
52
|
+
// Write file
|
|
53
|
+
const writeResult = await session.fileSystem.writeFile(testFilePath, testContent);
|
|
54
|
+
if (writeResult.success) {
|
|
55
|
+
console.log("✓ File written successfully");
|
|
56
|
+
} else {
|
|
57
|
+
console.log(`⚠ File write failed: ${writeResult.errorMessage}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Read file
|
|
61
|
+
const readResult = await session.fileSystem.readFile(testFilePath);
|
|
62
|
+
if (readResult.success) {
|
|
63
|
+
console.log(`✓ File read successfully. Content: ${readResult.content}`);
|
|
64
|
+
} else {
|
|
65
|
+
console.log(`⚠ File read failed: ${readResult.errorMessage}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Test Command operations
|
|
69
|
+
console.log("\n--- Testing Command operations ---");
|
|
70
|
+
|
|
71
|
+
// Get current user
|
|
72
|
+
const cmdResult = await session.command.executeCommand("whoami");
|
|
73
|
+
if (cmdResult.success) {
|
|
74
|
+
console.log(`✓ Current user: ${cmdResult.output.trim()}`);
|
|
75
|
+
} else {
|
|
76
|
+
console.log(`⚠ Command execution failed: ${cmdResult.errorMessage}`);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// List directory contents
|
|
80
|
+
const lsResult = await session.command.executeCommand("ls -la /tmp");
|
|
81
|
+
if (lsResult.success) {
|
|
82
|
+
console.log("✓ Directory listing successful");
|
|
83
|
+
console.log(` Output:\n${lsResult.output}`);
|
|
84
|
+
} else {
|
|
85
|
+
console.log(`⚠ Directory listing failed: ${lsResult.errorMessage}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
} finally {
|
|
89
|
+
// Clean up - delete the session
|
|
90
|
+
console.log("\n--- Cleaning up ---");
|
|
91
|
+
const deleteResult = await agentBay.delete(session);
|
|
92
|
+
if (deleteResult.success) {
|
|
93
|
+
console.log("✓ VPC session deleted successfully");
|
|
94
|
+
} else {
|
|
95
|
+
console.log(`⚠ Failed to delete VPC session: ${deleteResult.errorMessage}`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error("Error in main function:", error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (require.main === module) {
|
|
105
|
+
main().catch(console.error);
|
|
106
|
+
}
|