wuying-agentbay-sdk 0.11.0 → 0.13.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-E7QC5S76.mjs → chunk-P2CXYF4T.mjs} +650 -154
- package/dist/chunk-P2CXYF4T.mjs.map +1 -0
- package/dist/{chunk-ZUB35HKV.cjs → chunk-WVWGLZDT.cjs} +593 -97
- package/dist/chunk-WVWGLZDT.cjs.map +1 -0
- package/dist/index.cjs +7702 -687
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.mts +2660 -1619
- package/dist/index.d.ts +2660 -1619
- package/dist/index.mjs +7514 -499
- package/dist/index.mjs.map +1 -1
- package/dist/{model-ZFTLKEMC.mjs → model-BRLR6F3P.mjs} +28 -2
- package/dist/model-KJHN3WYY.cjs +214 -0
- package/dist/{model-2G37RFQQ.cjs.map → model-KJHN3WYY.cjs.map} +1 -1
- package/docs/api/README.md +6 -0
- package/docs/api/browser-use/browser-agent.md +188 -0
- package/docs/api/browser-use/browser.md +1 -1
- package/docs/api/browser-use/fingerprint.md +154 -0
- package/docs/api/codespace/code.md +3 -0
- package/docs/api/common-features/advanced/agent.md +7 -63
- package/docs/api/common-features/advanced/browser-use-agent.md +118 -0
- package/docs/api/common-features/advanced/computer-use-agent.md +85 -0
- package/docs/api/common-features/basics/agentbay.md +99 -2
- package/docs/api/common-features/basics/command.md +35 -18
- package/docs/api/common-features/basics/context-manager.md +21 -2
- package/docs/api/common-features/basics/filesystem.md +36 -0
- package/docs/api/common-features/basics/session-params.md +382 -0
- package/docs/api/common-features/basics/session.md +130 -2
- package/docs/api/computer-use/computer.md +25 -25
- package/docs/api/mobile-use/mobile-simulate.md +135 -0
- package/docs/examples/browser-use/browser/basic-usage.ts +31 -24
- package/docs/examples/browser-use/browser/browser-type-example.ts +3 -4
- package/docs/examples/browser-use/browser/captcha_tongcheng.ts +60 -28
- package/docs/examples/browser-use/browser/run-2048.ts +47 -37
- package/docs/examples/browser-use/browser/run-sudoku.ts +55 -36
- package/docs/examples/browser-use/browser/screenshot-example.ts +6 -6
- package/docs/examples/codespace/enhanced_code/index.ts +86 -0
- package/docs/examples/common-features/advanced/agent-module-example.ts +1 -1
- package/docs/examples/common-features/advanced/archive-upload-mode-example/README.md +1 -1
- package/docs/examples/common-features/advanced/archive-upload-mode-example/archive-upload-mode-example.ts +5 -6
- package/docs/examples/common-features/basics/archive-upload-mode-example/README.md +1 -1
- package/docs/examples/common-features/basics/archive-upload-mode-example/main.ts +1 -1
- package/docs/examples/common-features/basics/filesystem-example/filesystem-example.ts +13 -0
- package/docs/examples/common-features/basics/filesystem-example/filesystem-filetransfer-example.ts +6 -7
- package/docs/examples/common-features/basics/filesystem-example/watch-directory-example.ts +1 -1
- 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/index.ts +1 -1
- 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 +5 -5
- package/dist/chunk-E7QC5S76.mjs.map +0 -1
- package/dist/chunk-ZUB35HKV.cjs.map +0 -1
- package/dist/model-2G37RFQQ.cjs +0 -188
- package/docs/examples/mobile-use/mobile-get-adb-url/package-lock.json +0 -279
- package/docs/examples/mobile-use/mobile-get-adb-url/package.json +0 -18
- /package/dist/{model-ZFTLKEMC.mjs.map → model-BRLR6F3P.mjs.map} +0 -0
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { AgentBay, Session, log, logError, newCreateSessionParams } from 'wuying-agentbay-sdk'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pause and resume a session
|
|
5
|
+
*
|
|
6
|
+
* This example demonstrates how to pause and resume a session using the AgentBay SDK.
|
|
7
|
+
* Pausing a session puts it into a dormant state, consuming fewer resources while
|
|
8
|
+
* maintaining its state. Resuming a session brings it back to an active state.
|
|
9
|
+
*/
|
|
10
|
+
async function pauseAndResumeSession() {
|
|
11
|
+
// Initialize the AgentBay client
|
|
12
|
+
const agentBay = new AgentBay({ apiKey: process.env.AGENTBAY_API_KEY });
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
// Create a session with labels for identification
|
|
16
|
+
const params = newCreateSessionParams().withLabels({
|
|
17
|
+
project: 'pause-resume-example',
|
|
18
|
+
environment: 'development',
|
|
19
|
+
purpose: 'demonstration'
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
log('Creating session...');
|
|
23
|
+
const createResult = await agentBay.create(params);
|
|
24
|
+
|
|
25
|
+
if (createResult.success && createResult.session) {
|
|
26
|
+
const session = createResult.session;
|
|
27
|
+
log(`✅ Session created successfully with ID: ${session.sessionId}`);
|
|
28
|
+
log(`📎 Request ID: ${createResult.requestId}`);
|
|
29
|
+
|
|
30
|
+
// Perform some work in the session before pausing
|
|
31
|
+
log('Performing some work in the session...');
|
|
32
|
+
const commandResult = await session.command.executeCommand('echo "Hello from paused session!"');
|
|
33
|
+
if (commandResult.success) {
|
|
34
|
+
log(`💻 Command output: ${commandResult.output}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Pause the session
|
|
38
|
+
log('\n⏸️ Pausing session...');
|
|
39
|
+
const pauseResult = await agentBay.pauseAsync(session);
|
|
40
|
+
|
|
41
|
+
if (pauseResult.success) {
|
|
42
|
+
log(`✅ Session pause initiated successfully`);
|
|
43
|
+
log(`📎 Pause Request ID: ${pauseResult.requestId}`);
|
|
44
|
+
|
|
45
|
+
// Check session status after pause
|
|
46
|
+
const sessionStatusAfterPause = await agentBay.getSession(session.sessionId);
|
|
47
|
+
if (sessionStatusAfterPause.success && sessionStatusAfterPause.data) {
|
|
48
|
+
log(`📊 Session status after pause: ${sessionStatusAfterPause.data.status || 'Unknown'}`);
|
|
49
|
+
}
|
|
50
|
+
} else {
|
|
51
|
+
log(`❌ Failed to pause session: ${pauseResult.errorMessage}`);
|
|
52
|
+
// Continue with the example even if pause fails
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Wait a bit for the pause to complete
|
|
56
|
+
log('\n⏳ Waiting for session to pause...');
|
|
57
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
58
|
+
|
|
59
|
+
// Resume the session
|
|
60
|
+
log('\n▶️ Resuming session...');
|
|
61
|
+
const resumeResult = await agentBay.resumeAsync(session);
|
|
62
|
+
|
|
63
|
+
if (resumeResult.success) {
|
|
64
|
+
log(`✅ Session resume initiated successfully`);
|
|
65
|
+
log(`📎 Resume Request ID: ${resumeResult.requestId}`);
|
|
66
|
+
|
|
67
|
+
// Check session status after resume
|
|
68
|
+
const sessionStatusAfterResume = await agentBay.getSession(session.sessionId);
|
|
69
|
+
if (sessionStatusAfterResume.success && sessionStatusAfterResume.data) {
|
|
70
|
+
log(`📊 Session status after resume: ${sessionStatusAfterResume.data.status || 'Unknown'}`);
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
log(`❌ Failed to resume session: ${resumeResult.errorMessage}`);
|
|
74
|
+
// Continue with the example even if resume fails
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Wait a bit for the resume to complete
|
|
78
|
+
log('\n⏳ Waiting for session to resume...');
|
|
79
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
80
|
+
|
|
81
|
+
// Perform some work in the session after resuming
|
|
82
|
+
log('\nPerforming some work in the resumed session...');
|
|
83
|
+
const commandResultAfterResume = await session.command.executeCommand('echo "Hello from resumed session!"');
|
|
84
|
+
if (commandResultAfterResume.success) {
|
|
85
|
+
log(`💻 Command output: ${commandResultAfterResume.output}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Clean up by deleting the session
|
|
89
|
+
log('\n🧹 Cleaning up - deleting session...');
|
|
90
|
+
const deleteResult = await agentBay.delete(session);
|
|
91
|
+
if (deleteResult.success) {
|
|
92
|
+
log('✅ Session deleted successfully');
|
|
93
|
+
} else {
|
|
94
|
+
log(`❌ Failed to delete session: ${deleteResult.errorMessage}`);
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
log(`❌ Failed to create session: ${createResult.errorMessage}`);
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
logError('Error in pause and resume example:', error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Handle non-existent session pause/resume
|
|
106
|
+
*
|
|
107
|
+
* This example demonstrates how the SDK handles attempts to pause or resume
|
|
108
|
+
* sessions that don't exist.
|
|
109
|
+
*/
|
|
110
|
+
async function handleNonExistentSession() {
|
|
111
|
+
// Initialize the AgentBay client
|
|
112
|
+
const agentBay = new AgentBay({ apiKey: process.env.AGENTBAY_API_KEY });
|
|
113
|
+
|
|
114
|
+
log('\n' + '='.repeat(50));
|
|
115
|
+
log('Handling Non-Existent Session Example');
|
|
116
|
+
log('='.repeat(50));
|
|
117
|
+
|
|
118
|
+
// Create a session object with invalid ID
|
|
119
|
+
const fakeSessionId = 'session-nonexistent-12345';
|
|
120
|
+
log(`\nCreating session object for non-existent session: ${fakeSessionId}`);
|
|
121
|
+
|
|
122
|
+
// Create a real session object with invalid ID
|
|
123
|
+
const fakeSession = new Session(agentBay, fakeSessionId);
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
// Try to pause the non-existent session
|
|
127
|
+
log('⏸️ Attempting to pause non-existent session...');
|
|
128
|
+
const pauseResult = await agentBay.pauseAsync(fakeSession);
|
|
129
|
+
log(`Pause result: Success=${pauseResult.success}, Error=${pauseResult.errorMessage || 'None'}`);
|
|
130
|
+
|
|
131
|
+
log('▶️ Attempting to resume non-existent session...');
|
|
132
|
+
const resumeResult = await agentBay.resumeAsync(fakeSession);
|
|
133
|
+
log(`Resume result: Success=${resumeResult.success}, Error=${resumeResult.errorMessage || 'None'}`);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
log(`Expected error when handling non-existent session: ${error}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Pause and resume with custom parameters
|
|
141
|
+
*
|
|
142
|
+
* This example demonstrates how to use custom timeout and polling interval
|
|
143
|
+
* parameters when pausing and resuming sessions.
|
|
144
|
+
*/
|
|
145
|
+
async function pauseAndResumeWithCustomParameters() {
|
|
146
|
+
// Initialize the AgentBay client
|
|
147
|
+
const agentBay = new AgentBay({ apiKey: process.env.AGENTBAY_API_KEY });
|
|
148
|
+
|
|
149
|
+
log('\n' + '='.repeat(50));
|
|
150
|
+
log('Custom Parameters Example');
|
|
151
|
+
log('='.repeat(50));
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
// Create a session
|
|
155
|
+
const params = newCreateSessionParams().withLabels({
|
|
156
|
+
project: 'pause-resume-custom-example',
|
|
157
|
+
environment: 'development',
|
|
158
|
+
purpose: 'custom-parameters'
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
log('Creating session with custom parameters example...');
|
|
162
|
+
const createResult = await agentBay.create(params);
|
|
163
|
+
|
|
164
|
+
if (createResult.success && createResult.session) {
|
|
165
|
+
const session = createResult.session;
|
|
166
|
+
log(`✅ Session created successfully with ID: ${session.sessionId}`);
|
|
167
|
+
|
|
168
|
+
// Pause the session with custom parameters
|
|
169
|
+
// Using shorter timeout and poll interval for demonstration
|
|
170
|
+
log('\n⏸️ Pausing session with custom parameters (timeout=30s, pollInterval=1s)...');
|
|
171
|
+
const pauseResult = await agentBay.pauseAsync(session, 30, 1);
|
|
172
|
+
|
|
173
|
+
if (pauseResult.success) {
|
|
174
|
+
log(`✅ Session pause with custom parameters initiated successfully`);
|
|
175
|
+
log(`📎 Request ID: ${pauseResult.requestId}`);
|
|
176
|
+
|
|
177
|
+
// Check session status after pause
|
|
178
|
+
const sessionStatusAfterPause = await agentBay.getSession(session.sessionId);
|
|
179
|
+
if (sessionStatusAfterPause.success && sessionStatusAfterPause.data) {
|
|
180
|
+
log(`📊 Session status after pause: ${sessionStatusAfterPause.data.status || 'Unknown'}`);
|
|
181
|
+
}
|
|
182
|
+
} else {
|
|
183
|
+
log(`❌ Failed to pause session with custom parameters: ${pauseResult.errorMessage}`);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Wait a bit
|
|
187
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
188
|
+
|
|
189
|
+
// Resume the session with custom parameters
|
|
190
|
+
log('\n▶️ Resuming session with custom parameters (timeout=30s, pollInterval=1s)...');
|
|
191
|
+
const resumeResult = await agentBay.resumeAsync(session, 30, 1);
|
|
192
|
+
|
|
193
|
+
if (resumeResult.success) {
|
|
194
|
+
log(`✅ Session resume with custom parameters initiated successfully`);
|
|
195
|
+
log(`📎 Request ID: ${resumeResult.requestId}`);
|
|
196
|
+
|
|
197
|
+
// Check session status after resume
|
|
198
|
+
const sessionStatusAfterResume = await agentBay.getSession(session.sessionId);
|
|
199
|
+
if (sessionStatusAfterResume.success && sessionStatusAfterResume.data) {
|
|
200
|
+
log(`📊 Session status after resume: ${sessionStatusAfterResume.data.status || 'Unknown'}`);
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
log(`❌ Failed to resume session with custom parameters: ${resumeResult.errorMessage}`);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Clean up
|
|
207
|
+
log('\n🧹 Cleaning up - deleting session...');
|
|
208
|
+
const deleteResult = await agentBay.delete(session);
|
|
209
|
+
if (deleteResult.success) {
|
|
210
|
+
log('✅ Session deleted successfully');
|
|
211
|
+
} else {
|
|
212
|
+
log(`❌ Failed to delete session: ${deleteResult.errorMessage}`);
|
|
213
|
+
}
|
|
214
|
+
} else {
|
|
215
|
+
log(`❌ Failed to create session: ${createResult.errorMessage}`);
|
|
216
|
+
}
|
|
217
|
+
} catch (error) {
|
|
218
|
+
logError('Error in custom parameters example:', error);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Run all examples
|
|
224
|
+
*/
|
|
225
|
+
async function main() {
|
|
226
|
+
log('Session Pause and Resume Examples');
|
|
227
|
+
log('=================================');
|
|
228
|
+
|
|
229
|
+
await pauseAndResumeSession();
|
|
230
|
+
await handleNonExistentSession();
|
|
231
|
+
await pauseAndResumeWithCustomParameters();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
main().catch(error => {
|
|
235
|
+
logError('Error in main:', error);
|
|
236
|
+
process.exit(1);
|
|
237
|
+
});
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mobile Simulate Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the mobile simulate feature to simulate
|
|
5
|
+
* different mobile devices across sessions.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// @ts-nocheck
|
|
9
|
+
import { AgentBay, Session, MobileSimulateService, MobileSimulateMode } from 'wuying-agentbay-sdk';
|
|
10
|
+
import { readFileSync } from 'fs';
|
|
11
|
+
import { join } from 'path';
|
|
12
|
+
|
|
13
|
+
let session1: Session | null = null;
|
|
14
|
+
let session2: Session | null = null;
|
|
15
|
+
let mobileSimContextId: string | null = null;
|
|
16
|
+
|
|
17
|
+
async function runOnFirstMobileSession(client: AgentBay): Promise<void> {
|
|
18
|
+
console.log('=== First Mobile Session ===');
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
// Upload mobile info file for first time
|
|
22
|
+
// How to get the mobile info file please contact the support team.
|
|
23
|
+
console.log('Uploading mobile info file for first time...');
|
|
24
|
+
|
|
25
|
+
// Get the path to mobile_info_model_a.json
|
|
26
|
+
// Navigate from typescript/docs/examples/mobile-use/mobile-simulate-example to resource/
|
|
27
|
+
const mobileInfoFilePath = join(
|
|
28
|
+
__dirname,
|
|
29
|
+
'..',
|
|
30
|
+
'..',
|
|
31
|
+
'..',
|
|
32
|
+
'..',
|
|
33
|
+
'resource',
|
|
34
|
+
'mobile_info_model_a.json'
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// Read the mobile info file
|
|
38
|
+
const mobileInfoContent = readFileSync(mobileInfoFilePath, 'utf-8');
|
|
39
|
+
|
|
40
|
+
// Upload mobile info
|
|
41
|
+
const simulateService = new MobileSimulateService(client);
|
|
42
|
+
simulateService.setSimulateEnable(true);
|
|
43
|
+
simulateService.setSimulateMode(MobileSimulateMode.PropertiesOnly);
|
|
44
|
+
const uploadResult = await simulateService.uploadMobileInfo(mobileInfoContent);
|
|
45
|
+
if (!uploadResult.success) {
|
|
46
|
+
throw new Error(`Failed to upload mobile info file: ${uploadResult.errorMessage}`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
mobileSimContextId = uploadResult.mobileSimulateContextId!;
|
|
50
|
+
console.log(`Mobile simulate context id uploaded successfully: ${mobileSimContextId}`);
|
|
51
|
+
|
|
52
|
+
// Create session with mobile simulate configuration
|
|
53
|
+
console.log('Creating first session...');
|
|
54
|
+
const sessionResult = await client.create({
|
|
55
|
+
imageId: 'mobile_latest',
|
|
56
|
+
extraConfigs: {
|
|
57
|
+
mobile: {
|
|
58
|
+
// Set mobile simulate config
|
|
59
|
+
simulateConfig: simulateService.getSimulateConfig(),
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (!sessionResult.success || !sessionResult.session) {
|
|
65
|
+
throw new Error(`Failed to create session: ${sessionResult.errorMessage}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
session1 = sessionResult.session;
|
|
69
|
+
console.log(`Session created with ID: ${session1!.sessionId}`);
|
|
70
|
+
|
|
71
|
+
// Wait for mobile simulate to complete
|
|
72
|
+
console.log('Waiting for mobile simulate to complete...');
|
|
73
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
74
|
+
|
|
75
|
+
// Get device model after mobile simulate
|
|
76
|
+
console.log('Getting device model after mobile simulate...');
|
|
77
|
+
const result = await session1!.command.executeCommand('getprop ro.product.model');
|
|
78
|
+
if (!result.success) {
|
|
79
|
+
throw new Error(`Failed to get device model: ${result.errorMessage}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const productModel = result.output.trim();
|
|
83
|
+
console.log(`First session device model: ${productModel}`);
|
|
84
|
+
|
|
85
|
+
} catch (error) {
|
|
86
|
+
throw new Error(`Error during run_on_first_mobile_session: ${error instanceof Error ? error.message : String(error)}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function runOnSecondMobileSession(client: AgentBay): Promise<void> {
|
|
91
|
+
console.log('\n=== Second Mobile Session ===');
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
// Use the same mobile simulate context id as the first session
|
|
95
|
+
console.log('Creating second session...');
|
|
96
|
+
const simulateService = new MobileSimulateService(client);
|
|
97
|
+
simulateService.setSimulateEnable(true);
|
|
98
|
+
simulateService.setSimulateMode(MobileSimulateMode.PropertiesOnly);
|
|
99
|
+
simulateService.setSimulateContextId(mobileSimContextId!);
|
|
100
|
+
const sessionResult = await client.create({
|
|
101
|
+
imageId: 'mobile_latest',
|
|
102
|
+
extraConfigs: {
|
|
103
|
+
mobile: {
|
|
104
|
+
lockResolution: false,
|
|
105
|
+
// Set mobile simulate config
|
|
106
|
+
simulateConfig: simulateService.getSimulateConfig(),
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (!sessionResult.success || !sessionResult.session) {
|
|
112
|
+
throw new Error(`Failed to create session: ${sessionResult.errorMessage}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
session2 = sessionResult.session;
|
|
116
|
+
console.log(`Session created with ID: ${session2!.sessionId}`);
|
|
117
|
+
|
|
118
|
+
// Wait for mobile simulate to complete
|
|
119
|
+
console.log('Waiting for mobile simulate to complete...');
|
|
120
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
121
|
+
|
|
122
|
+
// Get device model after mobile simulate
|
|
123
|
+
console.log('Getting device model after mobile simulate...');
|
|
124
|
+
const result = await session2!.command.executeCommand('getprop ro.product.model');
|
|
125
|
+
if (!result.success) {
|
|
126
|
+
throw new Error(`Failed to get device model: ${result.errorMessage}`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const productModel = result.output.trim();
|
|
130
|
+
console.log(`Second session device model: ${productModel}`);
|
|
131
|
+
|
|
132
|
+
} catch (error) {
|
|
133
|
+
throw new Error(`Error during run_on_second_mobile_session: ${error instanceof Error ? error.message : String(error)}`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async function main() {
|
|
138
|
+
console.log('=== Mobile Simulate Example ===\n');
|
|
139
|
+
|
|
140
|
+
// Get API key from environment variable
|
|
141
|
+
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
142
|
+
if (!apiKey) {
|
|
143
|
+
console.error('Error: AGENTBAY_API_KEY environment variable is not set');
|
|
144
|
+
console.error('Please set it with: export AGENTBAY_API_KEY=your_api_key');
|
|
145
|
+
process.exit(1);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Initialize AgentBay client
|
|
149
|
+
const client = new AgentBay({ apiKey });
|
|
150
|
+
console.log('AgentBay client initialized');
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
// Run on first mobile session
|
|
154
|
+
await runOnFirstMobileSession(client);
|
|
155
|
+
|
|
156
|
+
// Delete first session
|
|
157
|
+
console.log('\nDeleting first session...');
|
|
158
|
+
const deleteResult1 = await session1!.delete();
|
|
159
|
+
if (!deleteResult1.success) {
|
|
160
|
+
throw new Error(`Failed to delete first session: ${deleteResult1.errorMessage}`);
|
|
161
|
+
}
|
|
162
|
+
console.log(`First session deleted successfully (RequestID: ${deleteResult1.requestId})`);
|
|
163
|
+
|
|
164
|
+
// Run on second mobile session
|
|
165
|
+
await runOnSecondMobileSession(client);
|
|
166
|
+
|
|
167
|
+
// Delete second session
|
|
168
|
+
console.log('\nDeleting second session...');
|
|
169
|
+
const deleteResult2 = await session2!.delete();
|
|
170
|
+
if (!deleteResult2.success) {
|
|
171
|
+
throw new Error(`Failed to delete second session: ${deleteResult2.errorMessage}`);
|
|
172
|
+
}
|
|
173
|
+
console.log(`Second session deleted successfully (RequestID: ${deleteResult2.requestId})`);
|
|
174
|
+
|
|
175
|
+
console.log('\n=== Example completed successfully ===');
|
|
176
|
+
|
|
177
|
+
} catch (error) {
|
|
178
|
+
console.error('Error:', error instanceof Error ? error.message : String(error));
|
|
179
|
+
|
|
180
|
+
// Cleanup sessions on error
|
|
181
|
+
if (session1) {
|
|
182
|
+
try {
|
|
183
|
+
await session1.delete();
|
|
184
|
+
} catch (e) {
|
|
185
|
+
console.warn('Warning: Failed to cleanup first session');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (session2) {
|
|
189
|
+
try {
|
|
190
|
+
await session2.delete();
|
|
191
|
+
} catch (e) {
|
|
192
|
+
console.warn('Warning: Failed to cleanup second session');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Run the example
|
|
201
|
+
main();
|
|
202
|
+
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mobile Simulate with User Specific Context Example
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to use the mobile simulate feature with a user specific context
|
|
5
|
+
* to simulate mobile devices across sessions.
|
|
6
|
+
*
|
|
7
|
+
* Steps:
|
|
8
|
+
* 1. Get a user specific context
|
|
9
|
+
* 2. Check if the mobile dev info file exists in user's specific context, if not, get a mobile dev info file from DumpSDK or real device
|
|
10
|
+
* 3. Upload mobile dev info file to user's specific context
|
|
11
|
+
* 4. Create a session with mobile simulate configuration and user's specific context
|
|
12
|
+
* 5. Wait for mobile simulate to complete
|
|
13
|
+
* 6. Get device model after mobile simulate
|
|
14
|
+
* 7. Delete session
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// @ts-nocheck
|
|
18
|
+
import { AgentBay, Session, MobileSimulateService, MobileSimulateMode, ContextSync, SyncPolicy, BWList, WhiteList } from 'wuying-agentbay-sdk';
|
|
19
|
+
import { readFileSync } from 'fs';
|
|
20
|
+
import { join } from 'path';
|
|
21
|
+
|
|
22
|
+
let session: Session | null = null;
|
|
23
|
+
|
|
24
|
+
async function runOnMobileSession(client: AgentBay): Promise<void> {
|
|
25
|
+
try {
|
|
26
|
+
console.log('Getting a user specific context...');
|
|
27
|
+
const contextResult = await client.context.get('13000000001', true);
|
|
28
|
+
if (!contextResult.success || !contextResult.context) {
|
|
29
|
+
throw new Error(`Failed to get context: ${contextResult.errorMessage}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const context = contextResult.context;
|
|
33
|
+
console.log(`context.id = ${context.id}, context.name = ${context.name}`);
|
|
34
|
+
|
|
35
|
+
// Create sync policy with white list
|
|
36
|
+
const syncPolicy: SyncPolicy = {
|
|
37
|
+
bwList: {
|
|
38
|
+
whiteLists: [
|
|
39
|
+
{
|
|
40
|
+
path: '/com.wuying.devinfo',
|
|
41
|
+
excludePaths: []
|
|
42
|
+
}
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const contextSync: ContextSync = {
|
|
48
|
+
contextId: context.id,
|
|
49
|
+
path: '/data/data',
|
|
50
|
+
policy: syncPolicy
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
console.log('Checking or uploading mobile dev info file...');
|
|
54
|
+
|
|
55
|
+
const simulateService = new MobileSimulateService(client);
|
|
56
|
+
simulateService.setSimulateEnable(true);
|
|
57
|
+
simulateService.setSimulateMode(MobileSimulateMode.PropertiesOnly);
|
|
58
|
+
|
|
59
|
+
// Check if the mobile dev info file exists in user's specific context
|
|
60
|
+
const hasMobileInfo = await simulateService.hasMobileInfo(contextSync);
|
|
61
|
+
if (!hasMobileInfo) {
|
|
62
|
+
// If not, get a mobile dev info file from DumpSDK or real device
|
|
63
|
+
const mobileInfoFilePath = join(
|
|
64
|
+
__dirname,
|
|
65
|
+
'..',
|
|
66
|
+
'..',
|
|
67
|
+
'..',
|
|
68
|
+
'..',
|
|
69
|
+
'resource',
|
|
70
|
+
'mobile_info_model_a.json'
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
const mobileInfoContent = readFileSync(mobileInfoFilePath, 'utf-8');
|
|
74
|
+
|
|
75
|
+
const uploadResult = await simulateService.uploadMobileInfo(mobileInfoContent, contextSync);
|
|
76
|
+
if (!uploadResult.success) {
|
|
77
|
+
throw new Error(`Failed to upload mobile dev info: ${uploadResult.errorMessage}`);
|
|
78
|
+
}
|
|
79
|
+
console.log('Mobile dev info uploaded successfully');
|
|
80
|
+
} else {
|
|
81
|
+
console.log(`Mobile dev info already exists: ${hasMobileInfo}`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Create session with mobile simulate configuration and user's specific context
|
|
85
|
+
console.log('Creating session...');
|
|
86
|
+
const sessionResult = await client.create({
|
|
87
|
+
imageId: 'mobile_latest',
|
|
88
|
+
contextSync: [contextSync],
|
|
89
|
+
extraConfigs: {
|
|
90
|
+
mobile: {
|
|
91
|
+
// Set mobile simulate config
|
|
92
|
+
simulateConfig: simulateService.getSimulateConfig(),
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
if (!sessionResult.success || !sessionResult.session) {
|
|
98
|
+
throw new Error(`Failed to create session: ${sessionResult.errorMessage}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
session = sessionResult.session;
|
|
102
|
+
console.log(`Session created with ID: ${session.sessionId}`);
|
|
103
|
+
|
|
104
|
+
// Wait for mobile simulate to complete
|
|
105
|
+
console.log('Waiting for mobile simulate to complete...');
|
|
106
|
+
await new Promise(resolve => setTimeout(resolve, 5000));
|
|
107
|
+
|
|
108
|
+
// Get device model after mobile simulate
|
|
109
|
+
console.log('Getting device model after mobile simulate...');
|
|
110
|
+
const result = await session.command.executeCommand('getprop ro.product.model');
|
|
111
|
+
if (!result.success) {
|
|
112
|
+
throw new Error(`Failed to get device model: ${result.errorMessage}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const productModel = result.output.trim();
|
|
116
|
+
console.log(`Session device model: ${productModel}`);
|
|
117
|
+
|
|
118
|
+
} catch (error) {
|
|
119
|
+
throw new Error(`Error during runOnMobileSession: ${error instanceof Error ? error.message : String(error)}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function main() {
|
|
124
|
+
console.log('=== Mobile Simulate with User Specific Context Example ===\n');
|
|
125
|
+
|
|
126
|
+
// Get API key from environment variable
|
|
127
|
+
const apiKey = process.env.AGENTBAY_API_KEY;
|
|
128
|
+
if (!apiKey) {
|
|
129
|
+
console.error('Error: AGENTBAY_API_KEY environment variable is not set');
|
|
130
|
+
console.error('Please set it with: export AGENTBAY_API_KEY=your_api_key');
|
|
131
|
+
process.exit(1);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Initialize AgentBay client
|
|
135
|
+
const client = new AgentBay({ apiKey });
|
|
136
|
+
console.log('AgentBay client initialized');
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
// Run on mobile session
|
|
140
|
+
await runOnMobileSession(client);
|
|
141
|
+
|
|
142
|
+
// Delete session
|
|
143
|
+
console.log('\nDeleting session...');
|
|
144
|
+
const deleteResult = await session!.delete({ syncContext: true });
|
|
145
|
+
if (!deleteResult.success) {
|
|
146
|
+
throw new Error(`Failed to delete session: ${deleteResult.errorMessage}`);
|
|
147
|
+
}
|
|
148
|
+
console.log(`Session deleted successfully (RequestID: ${deleteResult.requestId})`);
|
|
149
|
+
|
|
150
|
+
console.log('\n=== Example completed successfully ===');
|
|
151
|
+
|
|
152
|
+
} catch (error) {
|
|
153
|
+
console.error('Error:', error instanceof Error ? error.message : String(error));
|
|
154
|
+
|
|
155
|
+
// Cleanup session on error
|
|
156
|
+
if (session) {
|
|
157
|
+
try {
|
|
158
|
+
await session.delete({ syncContext: true });
|
|
159
|
+
} catch (e) {
|
|
160
|
+
console.warn('Warning: Failed to cleanup session');
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
process.exit(1);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Run the example
|
|
169
|
+
main();
|
|
170
|
+
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wuying-agentbay-sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.13.0",
|
|
4
4
|
"description": "TypeScript SDK for interacting with the Wuying AgentBay cloud runtime environment",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"@types/jest": "^26.0.24",
|
|
51
51
|
"@types/js-yaml": "^4.0.9",
|
|
52
52
|
"@types/mocha": "^10.0.10",
|
|
53
|
-
"@types/node": "^18.
|
|
53
|
+
"@types/node": "^18.19.130",
|
|
54
54
|
"@types/sinon": "^10.0.0",
|
|
55
55
|
"@typescript-eslint/eslint-plugin": "^5.54.0",
|
|
56
56
|
"@typescript-eslint/parser": "^5.54.0",
|
|
@@ -64,11 +64,11 @@
|
|
|
64
64
|
"prettier": "^2.8.4",
|
|
65
65
|
"sinon": "^15.2.0",
|
|
66
66
|
"ts-jest": "^26.5.6",
|
|
67
|
-
"ts-node": "^10.9.2",
|
|
68
67
|
"tsup": "^8.5.0",
|
|
69
|
-
"typedoc": "^0.25.0",
|
|
70
68
|
"typedoc-plugin-markdown": "^3.16.0",
|
|
71
|
-
"typescript": "4.9.5"
|
|
69
|
+
"typescript": "^4.9.5",
|
|
70
|
+
"zod": "^3.25.76",
|
|
71
|
+
"zod-to-json-schema": "^3.22.4"
|
|
72
72
|
},
|
|
73
73
|
"dependencies": {
|
|
74
74
|
"@alicloud/openapi-core": "^1.0.4",
|