|
35 | 35 | from src.controller.custom_controller import CustomController
|
36 | 36 | from src.utils import utils
|
37 | 37 | from src.utils.utils import update_model_dropdown
|
| 38 | +from src.browser.config import BrowserPersistenceConfig |
| 39 | +from src.browser.custom_browser import CustomBrowser |
| 40 | +from src.browser.custom_context import CustomBrowserContext |
| 41 | +from browser_use.browser.browser import BrowserConfig |
| 42 | +from browser_use.browser.context import BrowserContextConfig, BrowserContextWindowSize |
| 43 | + |
| 44 | +# Global variables for persistence |
| 45 | +_global_browser = None |
| 46 | +_global_browser_context = None |
| 47 | +_global_playwright = None |
38 | 48 |
|
39 | 49 | async def run_browser_agent(
|
40 | 50 | agent_type,
|
@@ -196,96 +206,107 @@ async def run_custom_agent(
|
196 | 206 | max_actions_per_step,
|
197 | 207 | tool_call_in_content
|
198 | 208 | ):
|
| 209 | + global _global_browser, _global_browser_context, _global_playwright |
| 210 | + |
199 | 211 | controller = CustomController()
|
200 |
| - playwright = None |
201 |
| - browser_context_ = None |
| 212 | + persistence_config = BrowserPersistenceConfig.from_env() |
| 213 | + |
202 | 214 | try:
|
203 |
| - if use_own_browser: |
204 |
| - playwright = await async_playwright().start() |
205 |
| - chrome_exe = os.getenv("CHROME_PATH", "") |
206 |
| - chrome_use_data = os.getenv("CHROME_USER_DATA", "") |
207 |
| - |
208 |
| - if chrome_exe == "": |
209 |
| - chrome_exe = None |
210 |
| - elif not os.path.exists(chrome_exe): |
211 |
| - raise ValueError(f"Chrome executable not found at {chrome_exe}") |
212 |
| - |
213 |
| - if chrome_use_data == "": |
214 |
| - chrome_use_data = None |
215 |
| - |
216 |
| - browser_context_ = await playwright.chromium.launch_persistent_context( |
217 |
| - user_data_dir=chrome_use_data, |
218 |
| - executable_path=chrome_exe, |
219 |
| - no_viewport=False, |
220 |
| - headless=headless, # 保持浏览器窗口可见 |
221 |
| - user_agent=( |
222 |
| - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " |
223 |
| - "(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" |
224 |
| - ), |
225 |
| - java_script_enabled=True, |
226 |
| - bypass_csp=disable_security, |
227 |
| - ignore_https_errors=disable_security, |
228 |
| - record_video_dir=save_recording_path if save_recording_path else None, |
229 |
| - record_video_size={"width": window_w, "height": window_h}, |
| 215 | + # Initialize global browser if needed |
| 216 | + if _global_browser is None: |
| 217 | + _global_browser = CustomBrowser( |
| 218 | + config=BrowserConfig( |
| 219 | + headless=headless, |
| 220 | + disable_security=disable_security, |
| 221 | + extra_chromium_args=[f"--window-size={window_w},{window_h}"], |
| 222 | + ) |
230 | 223 | )
|
231 |
| - else: |
232 |
| - browser_context_ = None |
233 | 224 |
|
234 |
| - browser = CustomBrowser( |
235 |
| - config=BrowserConfig( |
236 |
| - headless=headless, |
237 |
| - disable_security=disable_security, |
238 |
| - extra_chromium_args=[f"--window-size={window_w},{window_h}"], |
239 |
| - ) |
240 |
| - ) |
241 |
| - async with await browser.new_context( |
242 |
| - config=BrowserContextConfig( |
243 |
| - trace_path=save_trace_path if save_trace_path else None, |
244 |
| - save_recording_path=save_recording_path |
245 |
| - if save_recording_path |
246 |
| - else None, |
| 225 | + # Handle browser context based on configuration |
| 226 | + if use_own_browser: |
| 227 | + if _global_browser_context is None: |
| 228 | + _global_playwright = await async_playwright().start() |
| 229 | + chrome_exe = os.getenv("CHROME_PATH", "") |
| 230 | + chrome_use_data = os.getenv("CHROME_USER_DATA", "") |
| 231 | + |
| 232 | + browser_context = await _global_playwright.chromium.launch_persistent_context( |
| 233 | + user_data_dir=chrome_use_data, |
| 234 | + executable_path=chrome_exe, |
247 | 235 | no_viewport=False,
|
248 |
| - browser_window_size=BrowserContextWindowSize( |
249 |
| - width=window_w, height=window_h |
| 236 | + headless=headless, |
| 237 | + user_agent=( |
| 238 | + "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " |
| 239 | + "(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36" |
250 | 240 | ),
|
251 |
| - ), |
252 |
| - context=browser_context_, |
253 |
| - ) as browser_context: |
254 |
| - agent = CustomAgent( |
255 |
| - task=task, |
256 |
| - add_infos=add_infos, |
257 |
| - use_vision=use_vision, |
258 |
| - llm=llm, |
259 |
| - browser_context=browser_context, |
260 |
| - controller=controller, |
261 |
| - system_prompt_class=CustomSystemPrompt, |
262 |
| - max_actions_per_step=max_actions_per_step, |
263 |
| - tool_call_in_content=tool_call_in_content |
264 |
| - ) |
265 |
| - history = await agent.run(max_steps=max_steps) |
| 241 | + java_script_enabled=True, |
| 242 | + bypass_csp=disable_security, |
| 243 | + ignore_https_errors=disable_security, |
| 244 | + record_video_dir=save_recording_path if save_recording_path else None, |
| 245 | + record_video_size={"width": window_w, "height": window_h}, |
| 246 | + ) |
| 247 | + _global_browser_context = await _global_browser.new_context( |
| 248 | + config=BrowserContextConfig( |
| 249 | + trace_path=save_trace_path if save_trace_path else None, |
| 250 | + save_recording_path=save_recording_path if save_recording_path else None, |
| 251 | + no_viewport=False, |
| 252 | + browser_window_size=BrowserContextWindowSize( |
| 253 | + width=window_w, height=window_h |
| 254 | + ), |
| 255 | + ), |
| 256 | + context=browser_context, |
| 257 | + ) |
| 258 | + else: |
| 259 | + if _global_browser_context is None: |
| 260 | + _global_browser_context = await _global_browser.new_context( |
| 261 | + config=BrowserContextConfig( |
| 262 | + trace_path=save_trace_path if save_trace_path else None, |
| 263 | + save_recording_path=save_recording_path if save_recording_path else None, |
| 264 | + no_viewport=False, |
| 265 | + browser_window_size=BrowserContextWindowSize( |
| 266 | + width=window_w, height=window_h |
| 267 | + ), |
| 268 | + ), |
| 269 | + ) |
266 | 270 |
|
267 |
| - final_result = history.final_result() |
268 |
| - errors = history.errors() |
269 |
| - model_actions = history.model_actions() |
270 |
| - model_thoughts = history.model_thoughts() |
| 271 | + # Create and run agent |
| 272 | + agent = CustomAgent( |
| 273 | + task=task, |
| 274 | + add_infos=add_infos, |
| 275 | + use_vision=use_vision, |
| 276 | + llm=llm, |
| 277 | + browser_context=_global_browser_context, |
| 278 | + controller=controller, |
| 279 | + system_prompt_class=CustomSystemPrompt, |
| 280 | + max_actions_per_step=max_actions_per_step, |
| 281 | + tool_call_in_content=tool_call_in_content |
| 282 | + ) |
| 283 | + history = await agent.run(max_steps=max_steps) |
| 284 | + |
| 285 | + final_result = history.final_result() |
| 286 | + errors = history.errors() |
| 287 | + model_actions = history.model_actions() |
| 288 | + model_thoughts = history.model_thoughts() |
271 | 289 |
|
272 | 290 | except Exception as e:
|
273 | 291 | import traceback
|
274 |
| - |
275 | 292 | traceback.print_exc()
|
276 |
| - final_result = "" |
277 | 293 | errors = str(e) + "\n" + traceback.format_exc()
|
278 |
| - model_actions = "" |
279 |
| - model_thoughts = "" |
| 294 | + |
280 | 295 | finally:
|
281 |
| - # 显式关闭持久化上下文 |
282 |
| - if browser_context_: |
283 |
| - await browser_context_.close() |
284 |
| - |
285 |
| - # 关闭 Playwright 对象 |
286 |
| - if playwright: |
287 |
| - await playwright.stop() |
288 |
| - await browser.close() |
| 296 | + # Handle cleanup based on persistence configuration |
| 297 | + if not persistence_config.persistent_session: |
| 298 | + if _global_browser_context: |
| 299 | + await _global_browser_context.close() |
| 300 | + _global_browser_context = None |
| 301 | + |
| 302 | + if _global_playwright: |
| 303 | + await _global_playwright.stop() |
| 304 | + _global_playwright = None |
| 305 | + |
| 306 | + if _global_browser: |
| 307 | + await _global_browser.close() |
| 308 | + _global_browser = None |
| 309 | + |
289 | 310 | return final_result, errors, model_actions, model_thoughts
|
290 | 311 |
|
291 | 312 | # Define the theme map globally
|
|
0 commit comments