optexity-browser-use 0.9.5__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.
Files changed (147) hide show
  1. browser_use/__init__.py +157 -0
  2. browser_use/actor/__init__.py +11 -0
  3. browser_use/actor/element.py +1175 -0
  4. browser_use/actor/mouse.py +134 -0
  5. browser_use/actor/page.py +561 -0
  6. browser_use/actor/playground/flights.py +41 -0
  7. browser_use/actor/playground/mixed_automation.py +54 -0
  8. browser_use/actor/playground/playground.py +236 -0
  9. browser_use/actor/utils.py +176 -0
  10. browser_use/agent/cloud_events.py +282 -0
  11. browser_use/agent/gif.py +424 -0
  12. browser_use/agent/judge.py +170 -0
  13. browser_use/agent/message_manager/service.py +473 -0
  14. browser_use/agent/message_manager/utils.py +52 -0
  15. browser_use/agent/message_manager/views.py +98 -0
  16. browser_use/agent/prompts.py +413 -0
  17. browser_use/agent/service.py +2316 -0
  18. browser_use/agent/system_prompt.md +185 -0
  19. browser_use/agent/system_prompt_flash.md +10 -0
  20. browser_use/agent/system_prompt_no_thinking.md +183 -0
  21. browser_use/agent/views.py +743 -0
  22. browser_use/browser/__init__.py +41 -0
  23. browser_use/browser/cloud/cloud.py +203 -0
  24. browser_use/browser/cloud/views.py +89 -0
  25. browser_use/browser/events.py +578 -0
  26. browser_use/browser/profile.py +1158 -0
  27. browser_use/browser/python_highlights.py +548 -0
  28. browser_use/browser/session.py +3225 -0
  29. browser_use/browser/session_manager.py +399 -0
  30. browser_use/browser/video_recorder.py +162 -0
  31. browser_use/browser/views.py +200 -0
  32. browser_use/browser/watchdog_base.py +260 -0
  33. browser_use/browser/watchdogs/__init__.py +0 -0
  34. browser_use/browser/watchdogs/aboutblank_watchdog.py +253 -0
  35. browser_use/browser/watchdogs/crash_watchdog.py +335 -0
  36. browser_use/browser/watchdogs/default_action_watchdog.py +2729 -0
  37. browser_use/browser/watchdogs/dom_watchdog.py +817 -0
  38. browser_use/browser/watchdogs/downloads_watchdog.py +1277 -0
  39. browser_use/browser/watchdogs/local_browser_watchdog.py +461 -0
  40. browser_use/browser/watchdogs/permissions_watchdog.py +43 -0
  41. browser_use/browser/watchdogs/popups_watchdog.py +143 -0
  42. browser_use/browser/watchdogs/recording_watchdog.py +126 -0
  43. browser_use/browser/watchdogs/screenshot_watchdog.py +62 -0
  44. browser_use/browser/watchdogs/security_watchdog.py +280 -0
  45. browser_use/browser/watchdogs/storage_state_watchdog.py +335 -0
  46. browser_use/cli.py +2359 -0
  47. browser_use/code_use/__init__.py +16 -0
  48. browser_use/code_use/formatting.py +192 -0
  49. browser_use/code_use/namespace.py +665 -0
  50. browser_use/code_use/notebook_export.py +276 -0
  51. browser_use/code_use/service.py +1340 -0
  52. browser_use/code_use/system_prompt.md +574 -0
  53. browser_use/code_use/utils.py +150 -0
  54. browser_use/code_use/views.py +171 -0
  55. browser_use/config.py +505 -0
  56. browser_use/controller/__init__.py +3 -0
  57. browser_use/dom/enhanced_snapshot.py +161 -0
  58. browser_use/dom/markdown_extractor.py +169 -0
  59. browser_use/dom/playground/extraction.py +312 -0
  60. browser_use/dom/playground/multi_act.py +32 -0
  61. browser_use/dom/serializer/clickable_elements.py +200 -0
  62. browser_use/dom/serializer/code_use_serializer.py +287 -0
  63. browser_use/dom/serializer/eval_serializer.py +478 -0
  64. browser_use/dom/serializer/html_serializer.py +212 -0
  65. browser_use/dom/serializer/paint_order.py +197 -0
  66. browser_use/dom/serializer/serializer.py +1170 -0
  67. browser_use/dom/service.py +825 -0
  68. browser_use/dom/utils.py +129 -0
  69. browser_use/dom/views.py +906 -0
  70. browser_use/exceptions.py +5 -0
  71. browser_use/filesystem/__init__.py +0 -0
  72. browser_use/filesystem/file_system.py +619 -0
  73. browser_use/init_cmd.py +376 -0
  74. browser_use/integrations/gmail/__init__.py +24 -0
  75. browser_use/integrations/gmail/actions.py +115 -0
  76. browser_use/integrations/gmail/service.py +225 -0
  77. browser_use/llm/__init__.py +155 -0
  78. browser_use/llm/anthropic/chat.py +242 -0
  79. browser_use/llm/anthropic/serializer.py +312 -0
  80. browser_use/llm/aws/__init__.py +36 -0
  81. browser_use/llm/aws/chat_anthropic.py +242 -0
  82. browser_use/llm/aws/chat_bedrock.py +289 -0
  83. browser_use/llm/aws/serializer.py +257 -0
  84. browser_use/llm/azure/chat.py +91 -0
  85. browser_use/llm/base.py +57 -0
  86. browser_use/llm/browser_use/__init__.py +3 -0
  87. browser_use/llm/browser_use/chat.py +201 -0
  88. browser_use/llm/cerebras/chat.py +193 -0
  89. browser_use/llm/cerebras/serializer.py +109 -0
  90. browser_use/llm/deepseek/chat.py +212 -0
  91. browser_use/llm/deepseek/serializer.py +109 -0
  92. browser_use/llm/exceptions.py +29 -0
  93. browser_use/llm/google/__init__.py +3 -0
  94. browser_use/llm/google/chat.py +542 -0
  95. browser_use/llm/google/serializer.py +120 -0
  96. browser_use/llm/groq/chat.py +229 -0
  97. browser_use/llm/groq/parser.py +158 -0
  98. browser_use/llm/groq/serializer.py +159 -0
  99. browser_use/llm/messages.py +238 -0
  100. browser_use/llm/models.py +271 -0
  101. browser_use/llm/oci_raw/__init__.py +10 -0
  102. browser_use/llm/oci_raw/chat.py +443 -0
  103. browser_use/llm/oci_raw/serializer.py +229 -0
  104. browser_use/llm/ollama/chat.py +97 -0
  105. browser_use/llm/ollama/serializer.py +143 -0
  106. browser_use/llm/openai/chat.py +264 -0
  107. browser_use/llm/openai/like.py +15 -0
  108. browser_use/llm/openai/serializer.py +165 -0
  109. browser_use/llm/openrouter/chat.py +211 -0
  110. browser_use/llm/openrouter/serializer.py +26 -0
  111. browser_use/llm/schema.py +176 -0
  112. browser_use/llm/views.py +48 -0
  113. browser_use/logging_config.py +330 -0
  114. browser_use/mcp/__init__.py +18 -0
  115. browser_use/mcp/__main__.py +12 -0
  116. browser_use/mcp/client.py +544 -0
  117. browser_use/mcp/controller.py +264 -0
  118. browser_use/mcp/server.py +1114 -0
  119. browser_use/observability.py +204 -0
  120. browser_use/py.typed +0 -0
  121. browser_use/sandbox/__init__.py +41 -0
  122. browser_use/sandbox/sandbox.py +637 -0
  123. browser_use/sandbox/views.py +132 -0
  124. browser_use/screenshots/__init__.py +1 -0
  125. browser_use/screenshots/service.py +52 -0
  126. browser_use/sync/__init__.py +6 -0
  127. browser_use/sync/auth.py +357 -0
  128. browser_use/sync/service.py +161 -0
  129. browser_use/telemetry/__init__.py +51 -0
  130. browser_use/telemetry/service.py +112 -0
  131. browser_use/telemetry/views.py +101 -0
  132. browser_use/tokens/__init__.py +0 -0
  133. browser_use/tokens/custom_pricing.py +24 -0
  134. browser_use/tokens/mappings.py +4 -0
  135. browser_use/tokens/service.py +580 -0
  136. browser_use/tokens/views.py +108 -0
  137. browser_use/tools/registry/service.py +572 -0
  138. browser_use/tools/registry/views.py +174 -0
  139. browser_use/tools/service.py +1675 -0
  140. browser_use/tools/utils.py +82 -0
  141. browser_use/tools/views.py +100 -0
  142. browser_use/utils.py +670 -0
  143. optexity_browser_use-0.9.5.dist-info/METADATA +344 -0
  144. optexity_browser_use-0.9.5.dist-info/RECORD +147 -0
  145. optexity_browser_use-0.9.5.dist-info/WHEEL +4 -0
  146. optexity_browser_use-0.9.5.dist-info/entry_points.txt +3 -0
  147. optexity_browser_use-0.9.5.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,276 @@
1
+ """Export code-use session to Jupyter notebook format."""
2
+
3
+ import json
4
+ import re
5
+ from pathlib import Path
6
+
7
+ from browser_use.code_use.service import CodeAgent
8
+
9
+ from .views import CellType, NotebookExport
10
+
11
+
12
+ def export_to_ipynb(agent: CodeAgent, output_path: str | Path) -> Path:
13
+ """
14
+ Export a NotebookSession to a Jupyter notebook (.ipynb) file.
15
+ Now includes JavaScript code blocks that were stored in the namespace.
16
+
17
+ Args:
18
+ session: The NotebookSession to export
19
+ output_path: Path where to save the notebook file
20
+ agent: Optional CodeAgent instance to access namespace for JavaScript blocks
21
+
22
+ Returns:
23
+ Path to the saved notebook file
24
+
25
+ Example:
26
+ ```python
27
+ session = await agent.run()
28
+ notebook_path = export_to_ipynb(agent, 'my_automation.ipynb')
29
+ print(f'Notebook saved to {notebook_path}')
30
+ ```
31
+ """
32
+ output_path = Path(output_path)
33
+
34
+ # Create notebook structure
35
+ notebook = NotebookExport(
36
+ metadata={
37
+ 'kernelspec': {'display_name': 'Python 3', 'language': 'python', 'name': 'python3'},
38
+ 'language_info': {
39
+ 'name': 'python',
40
+ 'version': '3.11.0',
41
+ 'mimetype': 'text/x-python',
42
+ 'codemirror_mode': {'name': 'ipython', 'version': 3},
43
+ 'pygments_lexer': 'ipython3',
44
+ 'nbconvert_exporter': 'python',
45
+ 'file_extension': '.py',
46
+ },
47
+ }
48
+ )
49
+
50
+ # Add setup cell at the beginning with proper type hints
51
+ setup_code = """import asyncio
52
+ import json
53
+ from typing import Any
54
+ from browser_use import BrowserSession
55
+ from browser_use.code_use import create_namespace
56
+
57
+ # Initialize browser and namespace
58
+ browser = BrowserSession()
59
+ await browser.start()
60
+
61
+ # Create namespace with all browser control functions
62
+ namespace: dict[str, Any] = create_namespace(browser)
63
+
64
+ # Import all functions into the current namespace
65
+ globals().update(namespace)
66
+
67
+ # Type hints for better IDE support (these are now available globally)
68
+ # navigate, click, input, evaluate, search, extract, scroll, done, etc.
69
+
70
+ print("Browser-use environment initialized!")
71
+ print("Available functions: navigate, click, input, evaluate, search, extract, done, etc.")"""
72
+
73
+ setup_cell = {
74
+ 'cell_type': 'code',
75
+ 'metadata': {},
76
+ 'source': setup_code.split('\n'),
77
+ 'execution_count': None,
78
+ 'outputs': [],
79
+ }
80
+ notebook.cells.append(setup_cell)
81
+
82
+ # Add JavaScript code blocks as variables FIRST
83
+ if hasattr(agent, 'namespace') and agent.namespace:
84
+ # Look for JavaScript variables in the namespace
85
+ code_block_vars = agent.namespace.get('_code_block_vars', set())
86
+
87
+ for var_name in sorted(code_block_vars):
88
+ var_value = agent.namespace.get(var_name)
89
+ if isinstance(var_value, str) and var_value.strip():
90
+ # Check if this looks like JavaScript code
91
+ # Look for common JS patterns
92
+ js_patterns = [
93
+ r'function\s+\w+\s*\(',
94
+ r'\(\s*function\s*\(\)',
95
+ r'=>\s*{',
96
+ r'document\.',
97
+ r'Array\.from\(',
98
+ r'\.querySelector',
99
+ r'\.textContent',
100
+ r'\.innerHTML',
101
+ r'return\s+',
102
+ r'console\.log',
103
+ r'window\.',
104
+ r'\.map\(',
105
+ r'\.filter\(',
106
+ r'\.forEach\(',
107
+ ]
108
+
109
+ is_js = any(re.search(pattern, var_value, re.IGNORECASE) for pattern in js_patterns)
110
+
111
+ if is_js:
112
+ # Create a code cell with the JavaScript variable
113
+ js_cell = {
114
+ 'cell_type': 'code',
115
+ 'metadata': {},
116
+ 'source': [f'# JavaScript Code Block: {var_name}\n', f'{var_name} = """{var_value}"""'],
117
+ 'execution_count': None,
118
+ 'outputs': [],
119
+ }
120
+ notebook.cells.append(js_cell)
121
+
122
+ # Convert cells
123
+ python_cell_count = 0
124
+ for cell in agent.session.cells:
125
+ notebook_cell: dict = {
126
+ 'cell_type': cell.cell_type.value,
127
+ 'metadata': {},
128
+ 'source': cell.source.splitlines(keepends=True),
129
+ }
130
+
131
+ if cell.cell_type == CellType.CODE:
132
+ python_cell_count += 1
133
+ notebook_cell['execution_count'] = cell.execution_count
134
+ notebook_cell['outputs'] = []
135
+
136
+ # Add output if available
137
+ if cell.output:
138
+ notebook_cell['outputs'].append(
139
+ {
140
+ 'output_type': 'stream',
141
+ 'name': 'stdout',
142
+ 'text': cell.output.split('\n'),
143
+ }
144
+ )
145
+
146
+ # Add error if available
147
+ if cell.error:
148
+ notebook_cell['outputs'].append(
149
+ {
150
+ 'output_type': 'error',
151
+ 'ename': 'Error',
152
+ 'evalue': cell.error.split('\n')[0] if cell.error else '',
153
+ 'traceback': cell.error.split('\n') if cell.error else [],
154
+ }
155
+ )
156
+
157
+ # Add browser state as a separate output
158
+ if cell.browser_state:
159
+ notebook_cell['outputs'].append(
160
+ {
161
+ 'output_type': 'stream',
162
+ 'name': 'stdout',
163
+ 'text': [f'Browser State:\n{cell.browser_state}'],
164
+ }
165
+ )
166
+
167
+ notebook.cells.append(notebook_cell)
168
+
169
+ # Write to file
170
+ output_path.parent.mkdir(parents=True, exist_ok=True)
171
+ with open(output_path, 'w', encoding='utf-8') as f:
172
+ json.dump(notebook.model_dump(), f, indent=2, ensure_ascii=False)
173
+
174
+ return output_path
175
+
176
+
177
+ def session_to_python_script(agent: CodeAgent) -> str:
178
+ """
179
+ Convert a CodeAgent session to a Python script.
180
+ Now includes JavaScript code blocks that were stored in the namespace.
181
+
182
+ Args:
183
+ agent: The CodeAgent instance to convert
184
+
185
+ Returns:
186
+ Python script as a string
187
+
188
+ Example:
189
+ ```python
190
+ await agent.run()
191
+ script = session_to_python_script(agent)
192
+ print(script)
193
+ ```
194
+ """
195
+ lines = []
196
+
197
+ lines.append('# Generated from browser-use code-use session\n')
198
+ lines.append('import asyncio\n')
199
+ lines.append('import json\n')
200
+ lines.append('from browser_use import BrowserSession\n')
201
+ lines.append('from browser_use.code_use import create_namespace\n\n')
202
+
203
+ lines.append('async def main():\n')
204
+ lines.append('\t# Initialize browser and namespace\n')
205
+ lines.append('\tbrowser = BrowserSession()\n')
206
+ lines.append('\tawait browser.start()\n\n')
207
+ lines.append('\t# Create namespace with all browser control functions\n')
208
+ lines.append('\tnamespace = create_namespace(browser)\n\n')
209
+ lines.append('\t# Extract functions from namespace for direct access\n')
210
+ lines.append('\tnavigate = namespace["navigate"]\n')
211
+ lines.append('\tclick = namespace["click"]\n')
212
+ lines.append('\tinput_text = namespace["input"]\n')
213
+ lines.append('\tevaluate = namespace["evaluate"]\n')
214
+ lines.append('\tsearch = namespace["search"]\n')
215
+ lines.append('\textract = namespace["extract"]\n')
216
+ lines.append('\tscroll = namespace["scroll"]\n')
217
+ lines.append('\tdone = namespace["done"]\n')
218
+ lines.append('\tgo_back = namespace["go_back"]\n')
219
+ lines.append('\twait = namespace["wait"]\n')
220
+ lines.append('\tscreenshot = namespace["screenshot"]\n')
221
+ lines.append('\tfind_text = namespace["find_text"]\n')
222
+ lines.append('\tswitch_tab = namespace["switch"]\n')
223
+ lines.append('\tclose_tab = namespace["close"]\n')
224
+ lines.append('\tdropdown_options = namespace["dropdown_options"]\n')
225
+ lines.append('\tselect_dropdown = namespace["select_dropdown"]\n')
226
+ lines.append('\tupload_file = namespace["upload_file"]\n')
227
+ lines.append('\tsend_keys = namespace["send_keys"]\n\n')
228
+
229
+ # Add JavaScript code blocks as variables FIRST
230
+ if hasattr(agent, 'namespace') and agent.namespace:
231
+ code_block_vars = agent.namespace.get('_code_block_vars', set())
232
+
233
+ for var_name in sorted(code_block_vars):
234
+ var_value = agent.namespace.get(var_name)
235
+ if isinstance(var_value, str) and var_value.strip():
236
+ # Check if this looks like JavaScript code
237
+ js_patterns = [
238
+ r'function\s+\w+\s*\(',
239
+ r'\(\s*function\s*\(\)',
240
+ r'=>\s*{',
241
+ r'document\.',
242
+ r'Array\.from\(',
243
+ r'\.querySelector',
244
+ r'\.textContent',
245
+ r'\.innerHTML',
246
+ r'return\s+',
247
+ r'console\.log',
248
+ r'window\.',
249
+ r'\.map\(',
250
+ r'\.filter\(',
251
+ r'\.forEach\(',
252
+ ]
253
+
254
+ is_js = any(re.search(pattern, var_value, re.IGNORECASE) for pattern in js_patterns)
255
+
256
+ if is_js:
257
+ lines.append(f'\t# JavaScript Code Block: {var_name}\n')
258
+ lines.append(f'\t{var_name} = """{var_value}"""\n\n')
259
+
260
+ for i, cell in enumerate(agent.session.cells):
261
+ if cell.cell_type == CellType.CODE:
262
+ lines.append(f'\t# Cell {i + 1}\n')
263
+
264
+ # Indent each line of source
265
+ source_lines = cell.source.split('\n')
266
+ for line in source_lines:
267
+ if line.strip(): # Only add non-empty lines
268
+ lines.append(f'\t{line}\n')
269
+
270
+ lines.append('\n')
271
+
272
+ lines.append('\tawait browser.stop()\n\n')
273
+ lines.append("if __name__ == '__main__':\n")
274
+ lines.append('\tasyncio.run(main())\n')
275
+
276
+ return ''.join(lines)