camel-ai 0.2.71a7__py3-none-any.whl → 0.2.71a9__py3-none-any.whl

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.

Potentially problematic release.


This version of camel-ai might be problematic. Click here for more details.

@@ -0,0 +1,447 @@
1
+ #!/usr/bin/env python3
2
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # ========= Copyright 2023-2024 @ CAMEL-AI.org. All Rights Reserved. =========
15
+
16
+ """
17
+ Configuration for browser automation including stealth mode and timeouts.
18
+
19
+ This module contains all the configuration needed to make the browser
20
+ appear as a regular user browser and configure action timeouts.
21
+ """
22
+
23
+ import os
24
+ from typing import Any, Dict, List, Optional
25
+
26
+
27
+ class BrowserConfig:
28
+ r"""Configuration class for browser settings including stealth mode and
29
+ timeouts."""
30
+
31
+ # Default timeout values (in milliseconds)
32
+ DEFAULT_ACTION_TIMEOUT = 3000
33
+ DEFAULT_SHORT_TIMEOUT = 1000
34
+ DEFAULT_NAVIGATION_TIMEOUT = 10000
35
+ DEFAULT_NETWORK_IDLE_TIMEOUT = 5000
36
+ DEFAULT_SCREENSHOT_TIMEOUT = 15000
37
+ DEFAULT_PAGE_STABILITY_TIMEOUT = 1500
38
+ DEFAULT_DOM_CONTENT_LOADED_TIMEOUT = 5000
39
+
40
+ # Default action limits
41
+ DEFAULT_MAX_SCROLL_AMOUNT = 5000 # Maximum scroll distance in pixels
42
+
43
+ @staticmethod
44
+ def get_timeout_config() -> Dict[str, int]:
45
+ r"""Get timeout configuration with environment variable support.
46
+
47
+ Returns:
48
+ Dict[str, int]: Timeout configuration in milliseconds.
49
+ """
50
+ return {
51
+ 'default_timeout': int(
52
+ os.getenv(
53
+ 'HYBRID_BROWSER_DEFAULT_TIMEOUT',
54
+ BrowserConfig.DEFAULT_ACTION_TIMEOUT,
55
+ )
56
+ ),
57
+ 'short_timeout': int(
58
+ os.getenv(
59
+ 'HYBRID_BROWSER_SHORT_TIMEOUT',
60
+ BrowserConfig.DEFAULT_SHORT_TIMEOUT,
61
+ )
62
+ ),
63
+ 'navigation_timeout': int(
64
+ os.getenv(
65
+ 'HYBRID_BROWSER_NAVIGATION_TIMEOUT',
66
+ BrowserConfig.DEFAULT_NAVIGATION_TIMEOUT,
67
+ )
68
+ ),
69
+ 'network_idle_timeout': int(
70
+ os.getenv(
71
+ 'HYBRID_BROWSER_NETWORK_IDLE_TIMEOUT',
72
+ BrowserConfig.DEFAULT_NETWORK_IDLE_TIMEOUT,
73
+ )
74
+ ),
75
+ 'screenshot_timeout': int(
76
+ os.getenv(
77
+ 'HYBRID_BROWSER_SCREENSHOT_TIMEOUT',
78
+ BrowserConfig.DEFAULT_SCREENSHOT_TIMEOUT,
79
+ )
80
+ ),
81
+ 'page_stability_timeout': int(
82
+ os.getenv(
83
+ 'HYBRID_BROWSER_PAGE_STABILITY_TIMEOUT',
84
+ BrowserConfig.DEFAULT_PAGE_STABILITY_TIMEOUT,
85
+ )
86
+ ),
87
+ 'dom_content_loaded_timeout': int(
88
+ os.getenv(
89
+ 'HYBRID_BROWSER_DOM_CONTENT_LOADED_TIMEOUT',
90
+ BrowserConfig.DEFAULT_DOM_CONTENT_LOADED_TIMEOUT,
91
+ )
92
+ ),
93
+ }
94
+
95
+ @staticmethod
96
+ def get_action_limits() -> Dict[str, int]:
97
+ r"""Get action limits configuration with environment variable support.
98
+
99
+ Returns:
100
+ Dict[str, int]: Action limits configuration.
101
+ """
102
+ return {
103
+ 'max_scroll_amount': int(
104
+ os.getenv(
105
+ 'HYBRID_BROWSER_MAX_SCROLL_AMOUNT',
106
+ BrowserConfig.DEFAULT_MAX_SCROLL_AMOUNT,
107
+ )
108
+ ),
109
+ }
110
+
111
+ @staticmethod
112
+ def get_action_timeout(override: Optional[int] = None) -> int:
113
+ r"""Get action timeout with optional override.
114
+
115
+ Args:
116
+ override: Optional timeout override value in milliseconds.
117
+
118
+ Returns:
119
+ int: Timeout value in milliseconds.
120
+ """
121
+ if override is not None:
122
+ return override
123
+ return BrowserConfig.get_timeout_config()['default_timeout']
124
+
125
+ @staticmethod
126
+ def get_short_timeout(override: Optional[int] = None) -> int:
127
+ r"""Get short timeout with optional override.
128
+
129
+ Args:
130
+ override: Optional timeout override value in milliseconds.
131
+
132
+ Returns:
133
+ int: Timeout value in milliseconds.
134
+ """
135
+ if override is not None:
136
+ return override
137
+ return BrowserConfig.get_timeout_config()['short_timeout']
138
+
139
+ @staticmethod
140
+ def get_navigation_timeout(override: Optional[int] = None) -> int:
141
+ r"""Get navigation timeout with optional override.
142
+
143
+ Args:
144
+ override: Optional timeout override value in milliseconds.
145
+
146
+ Returns:
147
+ int: Timeout value in milliseconds.
148
+ """
149
+ if override is not None:
150
+ return override
151
+ return BrowserConfig.get_timeout_config()['navigation_timeout']
152
+
153
+ @staticmethod
154
+ def get_network_idle_timeout(override: Optional[int] = None) -> int:
155
+ r"""Get network idle timeout with optional override.
156
+
157
+ Args:
158
+ override: Optional timeout override value in milliseconds.
159
+
160
+ Returns:
161
+ int: Timeout value in milliseconds.
162
+ """
163
+ if override is not None:
164
+ return override
165
+ return BrowserConfig.get_timeout_config()['network_idle_timeout']
166
+
167
+ @staticmethod
168
+ def get_max_scroll_amount(override: Optional[int] = None) -> int:
169
+ r"""Get maximum scroll amount with optional override.
170
+
171
+ Args:
172
+ override: Optional scroll amount override value in pixels.
173
+
174
+ Returns:
175
+ int: Maximum scroll amount in pixels.
176
+ """
177
+ if override is not None:
178
+ return override
179
+ return BrowserConfig.get_action_limits()['max_scroll_amount']
180
+
181
+ @staticmethod
182
+ def get_screenshot_timeout(override: Optional[int] = None) -> int:
183
+ r"""Get screenshot timeout with optional override.
184
+
185
+ Args:
186
+ override: Optional timeout override value in milliseconds.
187
+
188
+ Returns:
189
+ int: Timeout value in milliseconds.
190
+ """
191
+ if override is not None:
192
+ return override
193
+ return BrowserConfig.get_timeout_config()['screenshot_timeout']
194
+
195
+ @staticmethod
196
+ def get_page_stability_timeout(override: Optional[int] = None) -> int:
197
+ r"""Get page stability timeout with optional override.
198
+
199
+ Args:
200
+ override: Optional timeout override value in milliseconds.
201
+
202
+ Returns:
203
+ int: Timeout value in milliseconds.
204
+ """
205
+ if override is not None:
206
+ return override
207
+ return BrowserConfig.get_timeout_config()['page_stability_timeout']
208
+
209
+ @staticmethod
210
+ def get_dom_content_loaded_timeout(override: Optional[int] = None) -> int:
211
+ r"""Get DOM content loaded timeout with optional override.
212
+
213
+ Args:
214
+ override: Optional timeout override value in milliseconds.
215
+
216
+ Returns:
217
+ int: Timeout value in milliseconds.
218
+ """
219
+ if override is not None:
220
+ return override
221
+ return BrowserConfig.get_timeout_config()['dom_content_loaded_timeout']
222
+
223
+ @staticmethod
224
+ def get_launch_args() -> List[str]:
225
+ r"""Get Chrome launch arguments for stealth mode.
226
+
227
+ Returns:
228
+ List[str]: Chrome command line arguments to avoid detection.
229
+ """
230
+ return [
231
+ '--disable-blink-features=AutomationControlled',
232
+ '--disable-features=VizDisplayCompositor',
233
+ '--disable-ipc-flooding-protection',
234
+ '--disable-renderer-backgrounding',
235
+ '--disable-backgrounding-occluded-windows',
236
+ '--disable-dev-shm-usage',
237
+ '--disable-extensions',
238
+ '--disable-plugins',
239
+ '--disable-default-apps',
240
+ '--disable-sync',
241
+ '--no-default-browser-check',
242
+ '--no-first-run',
243
+ '--no-sandbox',
244
+ '--disable-setuid-sandbox',
245
+ '--disable-web-security',
246
+ '--disable-features=TranslateUI',
247
+ '--disable-features=BlinkGenPropertyTrees',
248
+ '--disable-component-extensions-with-background-pages',
249
+ ]
250
+
251
+ @staticmethod
252
+ def get_context_options() -> Dict[str, Any]:
253
+ r"""Get browser context options for stealth mode.
254
+
255
+ Returns:
256
+ Dict[str, Any]: Browser context configuration options.
257
+ """
258
+ return {
259
+ 'user_agent': (
260
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
261
+ 'AppleWebKit/537.36 (KHTML, like Gecko) '
262
+ 'Chrome/131.0.0.0 Safari/537.36'
263
+ ),
264
+ 'viewport': {'width': 1920, 'height': 1080},
265
+ 'locale': 'en-US',
266
+ 'timezone_id': 'America/New_York',
267
+ 'geolocation': {'latitude': 40.7128, 'longitude': -74.0060},
268
+ 'permissions': ['geolocation'],
269
+ }
270
+
271
+ @staticmethod
272
+ def get_http_headers() -> Dict[str, str]:
273
+ r"""Get HTTP headers for stealth mode.
274
+
275
+ Returns:
276
+ Dict[str, str]: HTTP headers to appear more like a real browser.
277
+ """
278
+ return {
279
+ 'Accept': (
280
+ 'text/html,application/xhtml+xml,application/xml;q=0.9,'
281
+ 'image/avif,image/webp,image/apng,*/*;q=0.8,'
282
+ 'application/signed-exchange;v=b3;q=0.7'
283
+ ),
284
+ 'Accept-Language': 'en-US,en;q=0.9',
285
+ 'Accept-Encoding': 'gzip, deflate, br, zstd',
286
+ 'Cache-Control': 'max-age=0',
287
+ 'Sec-Ch-Ua': (
288
+ '"Google Chrome";v="131", "Chromium";v="131", '
289
+ '"Not=A?Brand";v="24"'
290
+ ),
291
+ 'Sec-Ch-Ua-Mobile': '?0',
292
+ 'Sec-Ch-Ua-Platform': '"Windows"',
293
+ 'Sec-Fetch-Dest': 'document',
294
+ 'Sec-Fetch-Mode': 'navigate',
295
+ 'Sec-Fetch-Site': 'none',
296
+ 'Sec-Fetch-User': '?1',
297
+ 'Upgrade-Insecure-Requests': '1',
298
+ }
299
+
300
+ @staticmethod
301
+ def get_stealth_config() -> Dict[str, Any]:
302
+ r"""Get stealth configuration.
303
+
304
+ Returns:
305
+ Dict[str, Any]: Complete stealth configuration.
306
+ """
307
+ return {
308
+ 'launch_args': BrowserConfig.get_launch_args(),
309
+ 'context_options': BrowserConfig.get_context_options(),
310
+ 'http_headers': BrowserConfig.get_http_headers(),
311
+ }
312
+
313
+ @staticmethod
314
+ def get_all_config() -> Dict[str, Any]:
315
+ r"""Get all browser configuration including stealth, timeouts,
316
+ and action limits.
317
+
318
+ Returns:
319
+ Dict[str, Any]: Complete browser configuration.
320
+ """
321
+ return {
322
+ 'timeouts': BrowserConfig.get_timeout_config(),
323
+ 'action_limits': BrowserConfig.get_action_limits(),
324
+ 'stealth': BrowserConfig.get_stealth_config(),
325
+ }
326
+
327
+
328
+ # Legacy ConfigLoader class for compatibility (now just wraps BrowserConfig)
329
+ class ConfigLoader:
330
+ r"""Legacy wrapper for BrowserConfig - maintained for backward
331
+ compatibility."""
332
+
333
+ @classmethod
334
+ def get_browser_config(cls):
335
+ r"""Get the BrowserConfig class."""
336
+ return BrowserConfig
337
+
338
+ @classmethod
339
+ def get_stealth_config(cls):
340
+ r"""Get the StealthConfig class (alias)."""
341
+ return BrowserConfig # StealthConfig is an alias for BrowserConfig
342
+
343
+ @classmethod
344
+ def get_timeout_config(cls) -> Dict[str, int]:
345
+ r"""Get timeout configuration."""
346
+ return BrowserConfig.get_timeout_config()
347
+
348
+ @classmethod
349
+ def get_action_timeout(cls, override: Optional[int] = None) -> int:
350
+ r"""Get action timeout with optional override."""
351
+ return BrowserConfig.get_action_timeout(override)
352
+
353
+ @classmethod
354
+ def get_short_timeout(cls, override: Optional[int] = None) -> int:
355
+ r"""Get short timeout with optional override."""
356
+ return BrowserConfig.get_short_timeout(override)
357
+
358
+ @classmethod
359
+ def get_navigation_timeout(cls, override: Optional[int] = None) -> int:
360
+ r"""Get navigation timeout with optional override."""
361
+ return BrowserConfig.get_navigation_timeout(override)
362
+
363
+ @classmethod
364
+ def get_network_idle_timeout(cls, override: Optional[int] = None) -> int:
365
+ r"""Get network idle timeout with optional override."""
366
+ return BrowserConfig.get_network_idle_timeout(override)
367
+
368
+ @classmethod
369
+ def get_max_scroll_amount(cls, override: Optional[int] = None) -> int:
370
+ r"""Get maximum scroll amount with optional override."""
371
+ return BrowserConfig.get_max_scroll_amount(override)
372
+
373
+ @classmethod
374
+ def get_screenshot_timeout(cls, override: Optional[int] = None) -> int:
375
+ r"""Get screenshot timeout with optional override."""
376
+ return BrowserConfig.get_screenshot_timeout(override)
377
+
378
+ @classmethod
379
+ def get_page_stability_timeout(cls, override: Optional[int] = None) -> int:
380
+ r"""Get page stability timeout with optional override."""
381
+ return BrowserConfig.get_page_stability_timeout(override)
382
+
383
+ @classmethod
384
+ def get_dom_content_loaded_timeout(
385
+ cls, override: Optional[int] = None
386
+ ) -> int:
387
+ r"""Get DOM content loaded timeout with optional override."""
388
+ return BrowserConfig.get_dom_content_loaded_timeout(override)
389
+
390
+
391
+ # Backward compatibility aliases and convenience functions
392
+ StealthConfig = BrowserConfig
393
+
394
+
395
+ def get_browser_config():
396
+ r"""Get BrowserConfig class."""
397
+ return BrowserConfig
398
+
399
+
400
+ def get_stealth_config():
401
+ r"""Get StealthConfig class."""
402
+ return BrowserConfig
403
+
404
+
405
+ def get_timeout_config() -> Dict[str, int]:
406
+ r"""Get timeout configuration."""
407
+ return BrowserConfig.get_timeout_config()
408
+
409
+
410
+ def get_action_timeout(override: Optional[int] = None) -> int:
411
+ r"""Get action timeout with optional override."""
412
+ return BrowserConfig.get_action_timeout(override)
413
+
414
+
415
+ def get_short_timeout(override: Optional[int] = None) -> int:
416
+ r"""Get short timeout with optional override."""
417
+ return BrowserConfig.get_short_timeout(override)
418
+
419
+
420
+ def get_navigation_timeout(override: Optional[int] = None) -> int:
421
+ r"""Get navigation timeout with optional override."""
422
+ return BrowserConfig.get_navigation_timeout(override)
423
+
424
+
425
+ def get_network_idle_timeout(override: Optional[int] = None) -> int:
426
+ r"""Get network idle timeout with optional override."""
427
+ return BrowserConfig.get_network_idle_timeout(override)
428
+
429
+
430
+ def get_max_scroll_amount(override: Optional[int] = None) -> int:
431
+ r"""Get maximum scroll amount with optional override."""
432
+ return BrowserConfig.get_max_scroll_amount(override)
433
+
434
+
435
+ def get_screenshot_timeout(override: Optional[int] = None) -> int:
436
+ r"""Get screenshot timeout with optional override."""
437
+ return BrowserConfig.get_screenshot_timeout(override)
438
+
439
+
440
+ def get_page_stability_timeout(override: Optional[int] = None) -> int:
441
+ r"""Get page stability timeout with optional override."""
442
+ return BrowserConfig.get_page_stability_timeout(override)
443
+
444
+
445
+ def get_dom_content_loaded_timeout(override: Optional[int] = None) -> int:
446
+ r"""Get DOM content loaded timeout with optional override."""
447
+ return BrowserConfig.get_dom_content_loaded_timeout(override)