@@ -216,7 +216,6 @@ async def run_org_agent(
216
216
await _global_browser .close ()
217
217
_global_browser = None
218
218
219
-
220
219
async def run_custom_agent (
221
220
llm ,
222
221
use_own_browser ,
@@ -332,72 +331,94 @@ async def run_with_stream(
332
331
max_actions_per_step ,
333
332
tool_call_in_content
334
333
):
335
- """Wrapper to run the agent and handle streaming."""
336
- global _global_browser , _global_browser_context
337
-
338
- try :
339
- # Initialize the global browser if it doesn't exist
340
- if _global_browser is None :
341
- _global_browser = CustomBrowser (
342
- config = BrowserConfig (
343
- headless = False ,
334
+ print (headless )
335
+ if not headless :
336
+ result = await run_browser_agent (
337
+ agent_type = agent_type ,
338
+ llm_provider = llm_provider ,
339
+ llm_model_name = llm_model_name ,
340
+ llm_temperature = llm_temperature ,
341
+ llm_base_url = llm_base_url ,
342
+ llm_api_key = llm_api_key ,
343
+ use_own_browser = use_own_browser ,
344
+ keep_browser_open = keep_browser_open ,
345
+ headless = headless ,
346
+ disable_security = disable_security ,
347
+ window_w = window_w ,
348
+ window_h = window_h ,
349
+ save_recording_path = save_recording_path ,
350
+ save_trace_path = save_trace_path ,
351
+ enable_recording = enable_recording ,
352
+ task = task ,
353
+ add_infos = add_infos ,
354
+ max_steps = max_steps ,
355
+ use_vision = use_vision ,
356
+ max_actions_per_step = max_actions_per_step ,
357
+ tool_call_in_content = tool_call_in_content
358
+ )
359
+ yield result
360
+ else :
361
+ try :
362
+ # Run the browser agent in the background
363
+ agent_task = asyncio .create_task (
364
+ run_browser_agent (
365
+ agent_type = agent_type ,
366
+ llm_provider = llm_provider ,
367
+ llm_model_name = llm_model_name ,
368
+ llm_temperature = llm_temperature ,
369
+ llm_base_url = llm_base_url ,
370
+ llm_api_key = llm_api_key ,
371
+ use_own_browser = use_own_browser ,
372
+ keep_browser_open = keep_browser_open ,
373
+ headless = headless ,
344
374
disable_security = disable_security ,
345
- extra_chromium_args = [f"--window-size={ window_w } ,{ window_h } " ],
375
+ window_w = window_w ,
376
+ window_h = window_h ,
377
+ save_recording_path = save_recording_path ,
378
+ save_trace_path = save_trace_path ,
379
+ enable_recording = enable_recording ,
380
+ task = task ,
381
+ add_infos = add_infos ,
382
+ max_steps = max_steps ,
383
+ use_vision = use_vision ,
384
+ max_actions_per_step = max_actions_per_step ,
385
+ tool_call_in_content = tool_call_in_content
346
386
)
347
387
)
348
388
349
- # Create or reuse browser context
350
- if _global_browser_context is None :
351
- _global_browser_context = await _global_browser .new_context (
352
- config = BrowserContextConfig (
353
- trace_path = save_trace_path if save_trace_path else None ,
354
- save_recording_path = save_recording_path if save_recording_path else None ,
355
- no_viewport = False ,
356
- browser_window_size = BrowserContextWindowSize (
357
- width = window_w , height = window_h
358
- ),
359
- )
360
- )
361
-
362
- # Run the browser agent in the background
363
- agent_task = asyncio .create_task (
364
- run_browser_agent (
365
- agent_type = agent_type ,
366
- llm_provider = llm_provider ,
367
- llm_model_name = llm_model_name ,
368
- llm_temperature = llm_temperature ,
369
- llm_base_url = llm_base_url ,
370
- llm_api_key = llm_api_key ,
371
- use_own_browser = use_own_browser ,
372
- keep_browser_open = keep_browser_open ,
373
- headless = headless ,
374
- disable_security = disable_security ,
375
- window_w = window_w ,
376
- window_h = window_h ,
377
- save_recording_path = save_recording_path ,
378
- save_trace_path = save_trace_path ,
379
- enable_recording = enable_recording ,
380
- task = task ,
381
- add_infos = add_infos ,
382
- max_steps = max_steps ,
383
- use_vision = use_vision ,
384
- max_actions_per_step = max_actions_per_step ,
385
- tool_call_in_content = tool_call_in_content
386
- )
387
- )
388
-
389
- # Initialize values for streaming
390
- html_content = "<div style='width:80vw; height:90vh'>Using browser...</div>"
391
- final_result = errors = model_actions = model_thoughts = ""
392
- latest_videos = trace = None
393
-
394
- # Periodically update the stream while the agent task is running
395
- while not agent_task .done ():
389
+ # Initialize values for streaming
390
+ html_content = "<div style='width:80vw; height:90vh'>Using browser...</div>"
391
+ final_result = errors = model_actions = model_thoughts = ""
392
+ latest_videos = trace = None
393
+
394
+ # Periodically update the stream while the agent task is running
395
+ while not agent_task .done ():
396
+ try :
397
+ html_content = await capture_screenshot (_global_browser_context )
398
+ except Exception as e :
399
+ html_content = f"<div style='width:80vw; height:90vh'>Waiting for browser session...</div>"
400
+
401
+ yield [
402
+ html_content ,
403
+ final_result ,
404
+ errors ,
405
+ model_actions ,
406
+ model_thoughts ,
407
+ latest_videos ,
408
+ trace ,
409
+ ]
410
+ await asyncio .sleep (0.01 )
411
+
412
+ # Once the agent task completes, get the results
396
413
try :
397
- html_content = await capture_screenshot (_global_browser_context )
414
+ result = await agent_task
415
+ if isinstance (result , tuple ) and len (result ) == 6 :
416
+ final_result , errors , model_actions , model_thoughts , latest_videos , trace = result
417
+ else :
418
+ errors = "Unexpected result format from agent"
398
419
except Exception as e :
399
- html_content = f"<div style='width:80vw; height:90vh'>Waiting for browser session...</div> "
400
-
420
+ errors = f"Agent error: { str ( e ) } "
421
+
401
422
yield [
402
423
html_content ,
403
424
final_result ,
@@ -407,39 +428,18 @@ async def run_with_stream(
407
428
latest_videos ,
408
429
trace ,
409
430
]
410
- await asyncio .sleep (0.01 )
411
431
412
- # Once the agent task completes, get the results
413
- try :
414
- result = await agent_task
415
- if isinstance (result , tuple ) and len (result ) == 6 :
416
- final_result , errors , model_actions , model_thoughts , latest_videos , trace = result
417
- else :
418
- errors = "Unexpected result format from agent"
419
432
except Exception as e :
420
- errors = f"Agent error: { str (e )} "
421
-
422
- yield [
423
- html_content ,
424
- final_result ,
425
- errors ,
426
- model_actions ,
427
- model_thoughts ,
428
- latest_videos ,
429
- trace ,
430
- ]
431
-
432
- except Exception as e :
433
- import traceback
434
- yield [
435
- f"<div style='width:80vw; height:90vh'>Waiting for browser session...</div>" ,
436
- "" ,
437
- f"Error: { str (e )} \n { traceback .format_exc ()} " ,
438
- "" ,
439
- "" ,
440
- None ,
441
- None ,
442
- ]
433
+ import traceback
434
+ yield [
435
+ f"<div style='width:80vw; height:90vh'>Waiting for browser session...</div>" ,
436
+ "" ,
437
+ f"Error: { str (e )} \n { traceback .format_exc ()} " ,
438
+ "" ,
439
+ "" ,
440
+ None ,
441
+ None ,
442
+ ]
443
443
444
444
# Define the theme map globally
445
445
theme_map = {
@@ -735,7 +735,6 @@ def list_recordings(save_recording_path):
735
735
736
736
use_own_browser .change (fn = close_global_browser )
737
737
keep_browser_open .change (fn = close_global_browser )
738
-
739
738
# Run button click handler
740
739
run_button .click (
741
740
fn = run_with_stream ,
0 commit comments