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.
Files changed (108) hide show
  1. package/dist/chunk-BVWUCG4J.mjs +3402 -0
  2. package/dist/chunk-BVWUCG4J.mjs.map +1 -0
  3. package/dist/chunk-SL5GCAQE.cjs +3393 -0
  4. package/dist/chunk-SL5GCAQE.cjs.map +1 -0
  5. package/dist/index.cjs +4623 -3978
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.mts +3934 -1964
  8. package/dist/index.d.ts +3934 -1964
  9. package/dist/index.mjs +4601 -3956
  10. package/dist/index.mjs.map +1 -1
  11. package/dist/key-normalizer-AF7APGQZ.cjs +136 -0
  12. package/dist/key-normalizer-AF7APGQZ.cjs.map +1 -0
  13. package/dist/key-normalizer-QDBRLFHF.mjs +145 -0
  14. package/dist/key-normalizer-QDBRLFHF.mjs.map +1 -0
  15. package/dist/model-CNCGFWJH.cjs +200 -0
  16. package/dist/model-CNCGFWJH.cjs.map +1 -0
  17. package/dist/model-LGWQJWKQ.mjs +209 -0
  18. package/docs/api/README.md +26 -0
  19. package/docs/api/browser-use/browser.md +177 -0
  20. package/docs/api/browser-use/extension.md +284 -0
  21. package/docs/api/codespace/code.md +77 -0
  22. package/docs/api/common-features/advanced/agent.md +84 -0
  23. package/docs/api/common-features/advanced/oss.md +221 -0
  24. package/docs/api/common-features/basics/agentbay.md +277 -0
  25. package/docs/api/common-features/basics/command.md +83 -0
  26. package/docs/api/common-features/basics/context-manager.md +149 -0
  27. package/docs/api/common-features/basics/context-sync.md +51 -0
  28. package/docs/api/common-features/basics/context.md +348 -0
  29. package/docs/api/common-features/basics/filesystem.md +499 -0
  30. package/docs/api/common-features/basics/logging.md +77 -0
  31. package/docs/api/common-features/basics/session.md +542 -0
  32. package/docs/api/computer-use/computer.md +786 -0
  33. package/docs/api/mobile-use/mobile.md +395 -0
  34. package/docs/examples/README.md +332 -0
  35. package/docs/examples/basic-usage.ts +86 -0
  36. package/docs/examples/browser-use/browser/README.md +356 -0
  37. package/docs/examples/browser-use/browser/basic-usage.ts +136 -0
  38. package/docs/examples/browser-use/browser/browser-command-args.ts +117 -0
  39. package/docs/examples/browser-use/browser/browser-context-cookie-persistence.ts +348 -0
  40. package/docs/examples/browser-use/browser/browser-fingerprint-basic-usage.ts +121 -0
  41. package/docs/examples/browser-use/browser/browser-fingerprint-construct.ts +114 -0
  42. package/docs/examples/browser-use/browser/browser-fingerprint-local-sync.ts +98 -0
  43. package/docs/examples/browser-use/browser/browser-fingerprint-persistence.ts +242 -0
  44. package/docs/examples/browser-use/browser/browser-proxies.ts +149 -0
  45. package/docs/examples/browser-use/browser/browser-type-example.ts +266 -0
  46. package/docs/examples/browser-use/browser/browser-viewport.ts +129 -0
  47. package/docs/examples/browser-use/browser/call_for_user_jd.ts +184 -0
  48. package/docs/examples/browser-use/browser/captcha_tongcheng.ts +151 -0
  49. package/docs/examples/browser-use/browser/run-2048.ts +209 -0
  50. package/docs/examples/browser-use/browser/run-sudoku.ts +150 -0
  51. package/docs/examples/browser-use/browser/screenshot-example.ts +132 -0
  52. package/docs/examples/browser-use/extension-example/README.md +252 -0
  53. package/docs/examples/browser-use/extension-example/extension-example.ts +381 -0
  54. package/docs/examples/codespace/automation/automation-example.ts +322 -0
  55. package/docs/examples/common-features/advanced/agent-module-example/README.md +40 -0
  56. package/docs/examples/common-features/advanced/agent-module-example.ts +66 -0
  57. package/docs/examples/common-features/advanced/archive-upload-mode-example/README.md +212 -0
  58. package/docs/examples/common-features/advanced/archive-upload-mode-example/archive-upload-mode-example.ts +213 -0
  59. package/docs/examples/common-features/advanced/vpc-session-example/README.md +47 -0
  60. package/docs/examples/common-features/advanced/vpc-session-example.ts +106 -0
  61. package/docs/examples/common-features/basics/archive-upload-mode-example/README.md +236 -0
  62. package/docs/examples/common-features/basics/archive-upload-mode-example/main.ts +236 -0
  63. package/docs/examples/common-features/basics/command-example/README.md +47 -0
  64. package/docs/examples/common-features/basics/command-example/command-example.ts +153 -0
  65. package/docs/examples/common-features/basics/context-management/README.md +55 -0
  66. package/docs/examples/common-features/basics/context-management/context-management.ts +140 -0
  67. package/docs/examples/common-features/basics/data-persistence/README.md +129 -0
  68. package/docs/examples/common-features/basics/data-persistence/context-sync-demo.md +144 -0
  69. package/docs/examples/common-features/basics/data-persistence/context-sync-demo.ts +275 -0
  70. package/docs/examples/common-features/basics/data-persistence/data-persistence.ts +259 -0
  71. package/docs/examples/common-features/basics/data-persistence/recycle-policy-example.ts +294 -0
  72. package/docs/examples/common-features/basics/filesystem-example/README.md +57 -0
  73. package/docs/examples/common-features/basics/filesystem-example/filesystem-example.ts +164 -0
  74. package/docs/examples/common-features/basics/filesystem-example/filesystem-filetransfer-example.ts +153 -0
  75. package/docs/examples/common-features/basics/filesystem-example/watch-directory-example.ts +168 -0
  76. package/docs/examples/common-features/basics/get/README.md +136 -0
  77. package/docs/examples/common-features/basics/get/main.ts +79 -0
  78. package/docs/examples/common-features/basics/list_sessions/README.md +54 -0
  79. package/docs/examples/common-features/basics/list_sessions/main.ts +258 -0
  80. package/docs/examples/common-features/basics/mcp_tool_direct_call/README.md +142 -0
  81. package/docs/examples/common-features/basics/mcp_tool_direct_call/main.ts +135 -0
  82. package/docs/examples/common-features/basics/session-creation/README.md +28 -0
  83. package/docs/examples/common-features/basics/session-creation/session-creation.ts +295 -0
  84. package/docs/examples/common-features/basics/session-pause-resume/README.md +53 -0
  85. package/docs/examples/common-features/basics/session-pause-resume/session-pause-resume.ts +237 -0
  86. package/docs/examples/mobile-use/mobile-get-adb-url/README.md +92 -0
  87. package/docs/examples/mobile-use/mobile-get-adb-url/index.ts +80 -0
  88. package/docs/examples/mobile-use/mobile-get-adb-url/package-lock.json +279 -0
  89. package/docs/examples/mobile-use/mobile-get-adb-url/package.json +18 -0
  90. package/docs/examples/mobile-use/mobile-simulate-basic-usage.ts +202 -0
  91. package/docs/examples/mobile-use/mobile-simulate-with-ctx.ts +170 -0
  92. package/package.json +19 -12
  93. package/dist/application-KZWXH46T.mjs +0 -17
  94. package/dist/application-LMA7KSRH.cjs +0 -8
  95. package/dist/application-LMA7KSRH.cjs.map +0 -1
  96. package/dist/chunk-BQNGKBQF.mjs +0 -386
  97. package/dist/chunk-BQNGKBQF.mjs.map +0 -1
  98. package/dist/chunk-IOVGAAJL.cjs +0 -558
  99. package/dist/chunk-IOVGAAJL.cjs.map +0 -1
  100. package/dist/chunk-UF2TC2R4.mjs +0 -567
  101. package/dist/chunk-UF2TC2R4.mjs.map +0 -1
  102. package/dist/chunk-X6MS7Z5L.cjs +0 -377
  103. package/dist/chunk-X6MS7Z5L.cjs.map +0 -1
  104. package/dist/window-DH37ZDD5.mjs +0 -17
  105. package/dist/window-DH37ZDD5.mjs.map +0 -1
  106. package/dist/window-U7N3H735.cjs +0 -8
  107. package/dist/window-U7N3H735.cjs.map +0 -1
  108. /package/dist/{application-KZWXH46T.mjs.map → model-LGWQJWKQ.mjs.map} +0 -0
@@ -0,0 +1,86 @@
1
+ import { AgentBay, logError, log } from 'wuying-agentbay-sdk';
2
+
3
+ async function main() {
4
+ try {
5
+ // Use the test API key function from test-helpers
6
+ const apiKey = process.env.AGENTBAY_API_KEY || 'akm-xxx'; // Replace with your actual API key
7
+ if (!process.env.AGENTBAY_API_KEY) {
8
+ log('Warning: Using placeholder API key. Set AGENTBAY_API_KEY environment variable for production use.');
9
+ }
10
+
11
+ // Initialize the AgentBay client
12
+ const agentBay = new AgentBay({ apiKey });
13
+
14
+ // Create a new session with labels
15
+ log('Creating a new session with labels...');
16
+ const createResponse = await agentBay.create({imageId:'browser_latest'});
17
+ const session = createResponse.session;
18
+ log(`Session created with ID: ${session.sessionId}`);
19
+ log(`Create Session RequestId: ${createResponse.requestId}`);
20
+
21
+ // Execute a command
22
+ log('\nExecuting a command...');
23
+ const commandResponse = await session.command.executeCommand('ls -la');
24
+ log('Command result:', commandResponse.output);
25
+ log(`Execute Command RequestId: ${commandResponse.requestId}`);
26
+
27
+ // Read a file
28
+ log('\nReading a file...');
29
+ const fileResponse = await session.fileSystem.readFile('/etc/hosts');
30
+ log(`File content: ${fileResponse.content}`);
31
+ log(`Read File RequestId: ${fileResponse.requestId}`);
32
+
33
+ // Get the session link
34
+ log('\nGetting session link...');
35
+ try {
36
+ const linkResponse = await session.getLink();
37
+ log(`Session link: ${linkResponse.data}`);
38
+ log(`Get Link RequestId: ${linkResponse.requestId}`);
39
+ } catch (error) {
40
+ log(`Note: Failed to get session link: ${error}`);
41
+ }
42
+
43
+ // Note: Screenshot functionality is available via session.mobile.screenshot() or session.computer.screenshot()
44
+ // depending on the session type (mobile or computer). Browser sessions don't have a direct screenshot API
45
+ // at the session level - use browser automation tools like Playwright for browser screenshots.
46
+
47
+ // List all sessions by labels
48
+ log('\nListing sessions...');
49
+ try {
50
+ // List sessions with labels (page 1, limit 5)
51
+ const listResponse = await agentBay.list({ test: 'basic-usage' }, 1, 5);
52
+ log(`Available sessions count: ${listResponse.sessionIds.length}`);
53
+ if (listResponse.totalCount !== undefined) {
54
+ log(`Total count: ${listResponse.totalCount}`);
55
+ }
56
+ if (listResponse.maxResults !== undefined) {
57
+ log(`Max results: ${listResponse.maxResults}`);
58
+ }
59
+ log('Session IDs:', listResponse.sessionIds);
60
+ log(`List Sessions RequestId: ${listResponse.requestId}`);
61
+
62
+ // Demonstrate pagination if there's a next token
63
+ if (listResponse.nextToken) {
64
+ log('\nFetching next page...');
65
+ const nextPageResponse = await agentBay.list({ test: 'basic-usage' }, 2, 5);
66
+ log(`Next page sessions count: ${nextPageResponse.sessionIds.length}`);
67
+ log(`Next page RequestId: ${nextPageResponse.requestId}`);
68
+ }
69
+ } catch (error) {
70
+ log(`Note: Failed to list sessions: ${error}`);
71
+ }
72
+
73
+ // Delete the session
74
+ log('\nDeleting the session...');
75
+ const deleteResponse = await agentBay.delete(session);
76
+ log('Session deleted successfully');
77
+ log(`Delete Session RequestId: ${deleteResponse.requestId}`);
78
+
79
+ } catch (error) {
80
+ logError('Error:', error);
81
+ // In a Node.js environment, you would use process.exit(1) here
82
+ throw error;
83
+ }
84
+ }
85
+
86
+ main();
@@ -0,0 +1,356 @@
1
+ # TypeScript Browser Examples
2
+
3
+ This directory contains TypeScript examples demonstrating browser automation capabilities of the AgentBay SDK.
4
+
5
+ ## Prerequisites
6
+
7
+ 1. **Install TypeScript SDK**:
8
+ ```bash
9
+ npm install @wuying-org/agentbay-sdk
10
+ ```
11
+
12
+ 2. **Install Playwright**:
13
+ ```bash
14
+ npm install playwright
15
+ npx playwright install chromium
16
+ ```
17
+
18
+ 3. **Set API Key**:
19
+ ```bash
20
+ export AGENTBAY_API_KEY=your_api_key_here
21
+ ```
22
+
23
+ ## Examples
24
+
25
+ ### 1. basic-usage.ts
26
+
27
+ A basic example showing how to:
28
+ - Create a session with a browser-enabled image
29
+ - Initialize the browser with default options
30
+ - Connect to the browser using Playwright over CDP
31
+ - Navigate to a website and interact with it
32
+ - Proper cleanup of resources
33
+
34
+ **Run:**
35
+ ```bash
36
+ ts-node basic-usage.ts
37
+ ```
38
+
39
+ **Key Features:**
40
+ - Session creation and management
41
+ - Browser initialization
42
+ - CDP connection with Playwright
43
+ - Basic navigation
44
+ - Resource cleanup
45
+
46
+ ### 2. browser-viewport.ts
47
+
48
+ Demonstrates custom viewport and screen configuration:
49
+ - Setting custom user agent
50
+ - Configuring viewport dimensions
51
+ - Setting screen dimensions
52
+ - Verifying browser configuration
53
+
54
+ **Run:**
55
+ ```bash
56
+ ts-node browser-viewport.ts
57
+ ```
58
+
59
+ **Key Features:**
60
+ - Custom user agent
61
+ - Viewport configuration
62
+ - Screen dimensions
63
+ - Configuration verification
64
+
65
+ ### 3. browser-fingerprint-xxx.ts
66
+
67
+ Shows how to configure browser fingerprinting:
68
+ - Three construction methods: random generation, custom configuration, and local fingerprint sync
69
+ - Fingerprint persistence capabilities across sessions
70
+
71
+ **Run:**
72
+ ```bash
73
+ ts-node browser-fingerprint-xxx.ts
74
+ ```
75
+
76
+ ### 4. browser-proxies.ts
77
+
78
+ Demonstrates proxy configuration:
79
+ - Custom proxy setup
80
+ - WuYing proxy strategies (restricted/polling)
81
+ - Proxy authentication
82
+
83
+ **Run:**
84
+ ```bash
85
+ ts-node browser-proxies.ts
86
+ ```
87
+
88
+ **Key Features:**
89
+ - Custom proxy configuration
90
+ - WuYing proxy integration
91
+ - Proxy authentication
92
+ - IP rotation strategies
93
+
94
+ ### 5. browser-context-cookie-persistence.ts
95
+
96
+ Demonstrates cookie persistence across sessions:
97
+ - Creating sessions with Browser Context
98
+ - Setting cookies manually using Playwright
99
+ - Deleting sessions with context synchronization
100
+ - Verifying cookie persistence in new sessions
101
+
102
+ **Run:**
103
+ ```bash
104
+ ts-node browser-context-cookie-persistence.ts
105
+ ```
106
+
107
+ **Key Features:**
108
+ - Browser Context configuration
109
+ - Cookie persistence
110
+ - Cross-session data sharing
111
+ - Resource cleanup
112
+
113
+ ### 6. browser-type-example.ts
114
+
115
+ Comprehensive example demonstrating browser type selection:
116
+ - Chrome browser initialization
117
+ - Chromium browser initialization
118
+ - Default browser (undefined) usage
119
+ - Browser configuration verification
120
+ - Type-safe TypeScript patterns
121
+
122
+ **Run:**
123
+ ```bash
124
+ # Run full example (tests all browser types)
125
+ ts-node browser-type-example.ts
126
+
127
+ # Run quick example (Chrome only)
128
+ ts-node browser-type-example.ts --quick
129
+
130
+ # Run type-safe example (demonstrates TypeScript types)
131
+ ts-node browser-type-example.ts --type-safe
132
+ ```
133
+
134
+ **Key Features:**
135
+ - Browser type selection for Chrome, Chromium, and default
136
+ - TypeScript type safety demonstration
137
+ - Configuration validation
138
+ - Browser detection and verification
139
+ - Multiple usage patterns (BrowserOptionClass vs plain object)
140
+ - Command-line options for different test modes
141
+
142
+ ### 7. Game Automation Examples
143
+
144
+ **run-2048.ts** and **run-sudoku.ts** demonstrate:
145
+ - Complex interaction patterns
146
+ - Agent-based automation for games
147
+ - Advanced browser control
148
+ - AI-powered game solving
149
+
150
+ **Run:**
151
+ ```bash
152
+ ts-node run-2048.ts
153
+ ts-node run-sudoku.ts
154
+ ```
155
+
156
+ ### 8. screenshot-example.ts
157
+
158
+ An example demonstrating screenshot capabilities:
159
+ - Creating a browser session with AgentBay
160
+ - Using Playwright to connect to the browser instance
161
+ - Taking screenshots using direct Playwright integration (Uint8Array data)
162
+ - Saving screenshots to local files
163
+ - Customizing screenshot options (full page, image format, quality)
164
+
165
+ **Run:**
166
+ ```bash
167
+ ts-node screenshot-example.ts
168
+ ```
169
+
170
+ **Expected Output:**
171
+ ```
172
+ 📸 AgentBay Browser Screenshot Demo (TypeScript)
173
+ ==================================================
174
+ Initializing AgentBay client...
175
+ Creating a new session...
176
+ Session created with ID: sess-xxxxx
177
+ Browser initialized successfully
178
+ Endpoint URL: ws://...
179
+ 📸 Taking screenshot...
180
+ ℹ️ Note: Screenshot functionality requires Playwright TypeScript integration
181
+ ✅ Browser screenshot demo completed
182
+
183
+ 🧹 Cleaning up session sess-xxxxx...
184
+ ✅ Session deleted successfully
185
+ ```
186
+
187
+ ## Browser Type Selection
188
+
189
+ When using computer use images, you can choose between Chrome and Chromium:
190
+
191
+ ```typescript
192
+ import { BrowserOptionClass } from '@wuying-org/agentbay-sdk';
193
+
194
+ // Use Chrome
195
+ const option = new BrowserOptionClass(
196
+ false, // useStealth
197
+ undefined, // userAgent
198
+ undefined, // viewport
199
+ undefined, // screen
200
+ undefined, // fingerprint
201
+ false, // solveCaptchas
202
+ undefined, // proxies
203
+ 'chrome' as 'chrome' // browserType
204
+ );
205
+
206
+ // Or use plain object
207
+ const option = { browserType: 'chrome' as 'chrome' };
208
+
209
+ // Use Chromium
210
+ const option = { browserType: 'chromium' as 'chromium' };
211
+
212
+ // Use default (undefined - let image decide)
213
+ const option = new BrowserOptionClass();
214
+ ```
215
+
216
+ ## Common Patterns
217
+
218
+ ### Basic Browser Initialization
219
+
220
+ ```typescript
221
+ import { AgentBay, CreateSessionParams, BrowserOptionClass } from '@wuying-org/agentbay-sdk';
222
+
223
+ const agentBay = new AgentBay(process.env.AGENTBAY_API_KEY!);
224
+ const params = new CreateSessionParams({ imageId: 'browser_latest' });
225
+ const result = await agentBay.create(params);
226
+
227
+ if (!result.success || !result.session) {
228
+ throw new Error("Failed to create session");
229
+ }
230
+
231
+ const session = result.session;
232
+ const option = new BrowserOptionClass();
233
+ const success = await session.browser.initializeAsync(option);
234
+
235
+ if (!success) {
236
+ throw new Error("Browser initialization failed");
237
+ }
238
+ ```
239
+
240
+ ### Connecting Playwright
241
+
242
+ ```typescript
243
+ import { chromium } from 'playwright';
244
+
245
+ const endpointUrl = session.browser.getEndpointUrl();
246
+ const browser = await chromium.connectOverCDP(endpointUrl);
247
+ const context = browser.contexts()[0];
248
+ const page = await context.newPage();
249
+
250
+ // Use page...
251
+
252
+ await browser.close();
253
+ session.delete();
254
+ ```
255
+
256
+ ### Error Handling
257
+
258
+ ```typescript
259
+ try {
260
+ const success = await session.browser.initializeAsync(option);
261
+ if (!success) {
262
+ throw new Error("Initialization failed");
263
+ }
264
+
265
+ // Use browser...
266
+
267
+ } catch (error) {
268
+ console.error(`Error: ${error.message}`);
269
+ } finally {
270
+ session.delete();
271
+ }
272
+ ```
273
+
274
+ ### Type-Safe Configuration
275
+
276
+ ```typescript
277
+ import { BrowserOption } from '@wuying-org/agentbay-sdk';
278
+
279
+ const option: BrowserOption = {
280
+ browserType: 'chrome', // Type-checked
281
+ useStealth: true,
282
+ viewport: { width: 1920, height: 1080 },
283
+ fingerprint: {
284
+ devices: ["desktop"],
285
+ operatingSystems: ["windows", "macos"],
286
+ locales: ["en-US"]
287
+ }
288
+ };
289
+ ```
290
+
291
+ ## Troubleshooting
292
+
293
+ ### "AGENTBAY_API_KEY not set"
294
+
295
+ Set your API key:
296
+ ```bash
297
+ export AGENTBAY_API_KEY=your_api_key_here
298
+ ```
299
+
300
+ ### "Browser not initialized"
301
+
302
+ Ensure the browser is initialized before connecting:
303
+ ```typescript
304
+ if (!session.browser.isInitialized()) {
305
+ throw new Error("Browser must be initialized first");
306
+ }
307
+ ```
308
+
309
+ ### "Failed to connect over CDP"
310
+
311
+ Check that the browser initialization was successful:
312
+ ```typescript
313
+ const success = await session.browser.initializeAsync(option);
314
+ console.log(`Initialization success: ${success}`);
315
+ ```
316
+
317
+ ### TypeScript Compilation Errors
318
+
319
+ Ensure you have the correct types:
320
+ ```bash
321
+ npm install --save-dev @types/node
322
+ ```
323
+
324
+ ## Additional Resources
325
+
326
+ - [Browser API Reference](../../../api/browser-use/browser.md)
327
+ - [Browser Use Guide](../../../../../docs/guides/browser-use/README.md)
328
+ - [Core Features](../../../../../docs/guides/browser-use/core-features.md)
329
+ - [Advanced Features](../../../../../docs/guides/browser-use/advance-features.md)
330
+ - [Playwright Documentation](https://playwright.dev/docs/intro)
331
+
332
+ ## Example Output
333
+
334
+ ### Successful Execution
335
+
336
+ ```
337
+ Creating session...
338
+ Session created: sess-xxxxx
339
+ Initializing browser...
340
+ Browser initialized successfully
341
+ CDP endpoint: ws://...
342
+ Connecting to browser...
343
+ Page title: Example Domain
344
+ Browser automation completed
345
+ Session deleted
346
+ ```
347
+
348
+ ### With Custom Configuration
349
+
350
+ ```
351
+ Initializing browser with custom configuration...
352
+ User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)...
353
+ Viewport: 1920 x 1080
354
+ Screen: 1920 x 1080
355
+ Configuration verified successfully
356
+ ```
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Basic usage example for AgentBay Browser functionality in TypeScript.
3
+ * This example demonstrates the core browser operations without external dependencies.
4
+ */
5
+
6
+ import { AgentBay, CreateSessionParams, ActOptions, ObserveOptions, ExtractOptions } from 'wuying-agentbay-sdk';
7
+
8
+ // Simple schema for demonstration
9
+ class PageInfo {
10
+ title: string = "";
11
+ url: string = "";
12
+ }
13
+
14
+ async function main() {
15
+ // Get API key from environment variable
16
+ const apiKey = process.env.AGENTBAY_API_KEY;
17
+ if (!apiKey) {
18
+ console.log("Error: AGENTBAY_API_KEY environment variable not set");
19
+ return;
20
+ }
21
+
22
+ try {
23
+ // Initialize AgentBay client
24
+ console.log("Initializing AgentBay client...");
25
+ const agentBay = new AgentBay({ apiKey });
26
+
27
+ // Create a session with browser image
28
+ console.log("Creating a new session...");
29
+ const params: CreateSessionParams = {
30
+ imageId: "browser_latest",
31
+ };
32
+ const sessionResult = await agentBay.create(params);
33
+
34
+ if (!sessionResult.success) {
35
+ console.log("Failed to create session");
36
+ return;
37
+ }
38
+
39
+ const session = sessionResult.session;
40
+ console.log(`Session created with ID: ${session.sessionId}`);
41
+
42
+ // Initialize browser
43
+ console.log("Initializing browser...");
44
+ // Note: persistentPath is currently ignored by the SDK implementation
45
+ const initialized = await session.browser.initializeAsync({});
46
+ if (!initialized) {
47
+ console.log("Failed to initialize browser");
48
+ return;
49
+ }
50
+
51
+ console.log("Browser initialized successfully");
52
+
53
+ // Get browser endpoint for external connections (like Playwright)
54
+ const endpointUrl = await session.browser.getEndpointUrl();
55
+ console.log("Browser endpoint URL:", endpointUrl);
56
+
57
+ // Example: Mock page object (in real usage, you'd get this from Playwright)
58
+ const mockPage = {
59
+ url: () => "https://example.com",
60
+ title: () => "Example Domain"
61
+ };
62
+
63
+ // Example 1: Perform an action
64
+ console.log("\n--- Example 1: Performing an action ---");
65
+ try {
66
+ const actOptions: ActOptions = {
67
+ action: "Click the 'More information...' link",
68
+ timeoutMS: 5000
69
+ };
70
+
71
+ const actResult = await session.browser.agent.act(actOptions, mockPage);
72
+ console.log("Action result:", {
73
+ success: actResult.success,
74
+ message: actResult.message,
75
+ });
76
+ } catch (error) {
77
+ console.log("Action failed:", error);
78
+ }
79
+
80
+ // Example 2: Observe elements on the page
81
+ console.log("\n--- Example 2: Observing page elements ---");
82
+ try {
83
+ const observeOptions: ObserveOptions = {
84
+ instruction: "Find all links and buttons on the page"
85
+ };
86
+
87
+ const [observeSuccess, observations] = await session.browser.agent.observe(observeOptions, mockPage);
88
+ console.log("Observe success:", observeSuccess);
89
+ console.log("Number of observations:", observations.length);
90
+
91
+ observations.forEach((obs: any, index: number) => {
92
+ console.log(`Observation ${index + 1}:`, {
93
+ selector: obs.selector,
94
+ description: obs.description,
95
+ method: obs.method
96
+ });
97
+ });
98
+ } catch (error) {
99
+ console.log("Observation failed:", error);
100
+ }
101
+
102
+ // Example 3: Extract structured data from the page
103
+ console.log("\n--- Example 3: Extracting structured data ---");
104
+ try {
105
+ const extractOptions: ExtractOptions<PageInfo> = {
106
+ instruction: "Extract the page title and URL",
107
+ schema: PageInfo,
108
+ use_text_extract: false
109
+ };
110
+
111
+ const [extractSuccess, extractedData] = await session.browser.agent.extract(extractOptions, mockPage);
112
+ console.log("Extract success:", extractSuccess);
113
+ console.log("Extracted data count:", extractedData.length);
114
+
115
+ extractedData.forEach((data: PageInfo, index: number) => {
116
+ console.log(`Extracted item ${index + 1}:`, {
117
+ title: data.title,
118
+ url: data.url
119
+ });
120
+ });
121
+ } catch (error) {
122
+ console.log("Extraction failed:", error);
123
+ }
124
+
125
+ // Clean up
126
+ console.log("\n--- Cleanup ---");
127
+ console.log("Browser session completed successfully");
128
+
129
+ } catch (error) {
130
+ console.error("Error in main function:", error);
131
+ }
132
+ }
133
+
134
+ if (require.main === module) {
135
+ main().catch(console.error);
136
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Example demonstrating Browser Launch with Custom Command Arguments and
3
+ * go to Default Navigation URL with AgentBay SDK.
4
+ *
5
+ * This example shows how to configure browser with custom command arguments
6
+ * and go to default navigation URL:
7
+ * - Create AIBrowser session with custom command arguments and go to default navigation URL
8
+ * - Use playwright to connect to AIBrowser instance through CDP protocol
9
+ * - Verify the browser navigated to the default URL
10
+ * - Test custom command arguments effects
11
+ */
12
+
13
+ // @ts-nocheck
14
+ import { AgentBay, CreateSessionParams } from 'wuying-agentbay-sdk';
15
+ import { BrowserOption } from 'wuying-agentbay-sdk';
16
+ import { chromium } from 'playwright';
17
+
18
+ async function main(): Promise<void> {
19
+ // Get API key from environment variable
20
+ const apiKey = process.env.AGENTBAY_API_KEY;
21
+ if (!apiKey) {
22
+ console.log('Error: AGENTBAY_API_KEY environment variable not set');
23
+ return;
24
+ }
25
+
26
+ try {
27
+ // Initialize AgentBay client
28
+ console.log('Initializing AgentBay client...');
29
+ const agentBay = new AgentBay({ apiKey });
30
+
31
+ // Create a session
32
+ console.log('Creating a new session...');
33
+ const params: CreateSessionParams = {
34
+ imageId: 'linux_latest',
35
+ };
36
+ const sessionResult = await agentBay.create(params);
37
+
38
+ if (!sessionResult.success) {
39
+ console.log('Failed to create session');
40
+ return;
41
+ }
42
+
43
+ const session = sessionResult.session;
44
+ console.log(`Session created with ID: ${session.sessionId}`);
45
+
46
+ // Create browser option with user defined cmd args and default navigate url
47
+ const browserOption: BrowserOption = {
48
+ cmdArgs: ['--disable-features=PrivacySandboxSettings4'],
49
+ defaultNavigateUrl: 'chrome://version/',
50
+ };
51
+
52
+ console.log('Browser configuration:');
53
+ console.log('- Command arguments:', browserOption.cmdArgs);
54
+ console.log('- Default navigate URL:', browserOption.defaultNavigateUrl);
55
+
56
+ const initialized = await session.browser.initializeAsync(browserOption);
57
+ if (initialized) {
58
+ const endpointUrl = await session.browser.getEndpointUrl();
59
+ console.log('endpoint_url =', endpointUrl);
60
+
61
+ const browser = await chromium.connectOverCDP(endpointUrl);
62
+ const context = browser.contexts()[0];
63
+ console.log('page count =', context.pages().length);
64
+ const page = context.pages()[0];
65
+
66
+ try {
67
+ // Check if browser navigated to default URL
68
+ console.log('\n--- Check Default Navigation ---');
69
+ await page.waitForTimeout(2000); // Wait for navigation
70
+ const currentUrl = page.url();
71
+ console.log('Current URL:', currentUrl);
72
+
73
+ if (currentUrl.includes('chrome://version/')) {
74
+ console.log('✓ Browser successfully navigated to default URL');
75
+ } else {
76
+ console.log('✗ Browser did not navigate to default URL');
77
+ }
78
+
79
+ // Test command arguments effect by checking Chrome version page
80
+ if (currentUrl.includes('chrome://version/')) {
81
+ console.log('\n--- Check Chrome Version Info ---');
82
+ const versionInfo = await page.evaluate(() => {
83
+ const versionElement = document.querySelector('#version');
84
+ const commandLineElement = document.querySelector('#command_line');
85
+ return {
86
+ version: versionElement ? versionElement.textContent : 'Not found',
87
+ commandLine: commandLineElement ? commandLineElement.textContent : 'Not found'
88
+ };
89
+ });
90
+
91
+ console.log('Chrome Version:', versionInfo.version);
92
+ console.log('Command Line:', versionInfo.commandLine);
93
+
94
+ if (versionInfo.commandLine.includes('--disable-features=PrivacySandboxSettings4')) {
95
+ console.log('✓ Custom command argument found in browser');
96
+ } else {
97
+ console.log('✗ Custom command argument not found in browser');
98
+ }
99
+ }
100
+
101
+ await page.waitForTimeout(3000);
102
+ } finally {
103
+ await browser.close();
104
+ }
105
+ await session.browser.destroy();
106
+ }
107
+
108
+ // Clean up session
109
+ await agentBay.delete(session);
110
+ } catch (error) {
111
+ console.error('Error:', error);
112
+ }
113
+ }
114
+
115
+ if (require.main === module) {
116
+ main().catch(console.error);
117
+ }