24
24
from src .browser .custom_browser import CustomBrowser
25
25
from src .agent .custom_prompts import CustomSystemPrompt
26
26
from src .browser .config import BrowserPersistenceConfig
27
- from src .browser .custom_context import BrowserContextConfig
27
+ from src .browser .custom_context import BrowserContextConfig , CustomBrowserContext
28
28
from src .controller .custom_controller import CustomController
29
29
from gradio .themes import Citrus , Default , Glass , Monochrome , Ocean , Origin , Soft , Base
30
30
from src .utils .utils import update_model_dropdown , get_latest_files , capture_screenshot
35
35
# Global variables for persistence
36
36
_global_browser = None
37
37
_global_browser_context = None
38
+ _global_playwright = None
38
39
39
40
async def run_browser_agent (
40
41
agent_type ,
@@ -57,10 +58,7 @@ async def run_browser_agent(
57
58
max_steps ,
58
59
use_vision ,
59
60
max_actions_per_step ,
60
- tool_call_in_content ,
61
- browser ,
62
- browser_context ,
63
- playwright
61
+ tool_call_in_content
64
62
):
65
63
# Disable recording if the checkbox is unchecked
66
64
if not enable_recording :
@@ -101,10 +99,7 @@ async def run_browser_agent(
101
99
max_steps = max_steps ,
102
100
use_vision = use_vision ,
103
101
max_actions_per_step = max_actions_per_step ,
104
- tool_call_in_content = tool_call_in_content ,
105
- browser = browser ,
106
- browser_context = browser_context ,
107
- playwright = playwright
102
+ tool_call_in_content = tool_call_in_content
108
103
)
109
104
elif agent_type == "custom" :
110
105
final_result , errors , model_actions , model_thoughts , recorded_files , trace_file = await run_custom_agent (
@@ -122,10 +117,7 @@ async def run_browser_agent(
122
117
max_steps = max_steps ,
123
118
use_vision = use_vision ,
124
119
max_actions_per_step = max_actions_per_step ,
125
- tool_call_in_content = tool_call_in_content ,
126
- browser = browser ,
127
- browser_context = browser_context ,
128
- playwright = playwright
120
+ tool_call_in_content = tool_call_in_content
129
121
)
130
122
else :
131
123
raise ValueError (f"Invalid agent type: { agent_type } " )
@@ -157,9 +149,6 @@ async def run_org_agent(
157
149
use_vision ,
158
150
max_actions_per_step ,
159
151
tool_call_in_content ,
160
- browser ,
161
- browser_context ,
162
- playwright
163
152
):
164
153
try :
165
154
global _global_browser , _global_browser_context
@@ -244,10 +233,7 @@ async def run_custom_agent(
244
233
max_steps ,
245
234
use_vision ,
246
235
max_actions_per_step ,
247
- tool_call_in_content ,
248
- browser ,
249
- browser_context ,
250
- playwright
236
+ tool_call_in_content
251
237
):
252
238
controller = CustomController ()
253
239
persistence_config = BrowserPersistenceConfig .from_env ()
@@ -265,7 +251,7 @@ async def run_custom_agent(
265
251
controller = CustomController ()
266
252
267
253
# Initialize global browser if needed
268
- if browser is None :
254
+ if _global_browser is None :
269
255
browser = CustomBrowser (
270
256
config = BrowserConfig (
271
257
headless = headless ,
@@ -277,7 +263,7 @@ async def run_custom_agent(
277
263
278
264
# Handle browser context based on configuration
279
265
if use_own_browser :
280
- if browser_context is None :
266
+ if _global_browser_context is None :
281
267
playwright = await async_playwright ().start ()
282
268
chrome_exe = os .getenv ("CHROME_PATH" , "" )
283
269
chrome_use_data = os .getenv ("CHROME_USER_DATA" , "" )
@@ -308,7 +294,7 @@ async def run_custom_agent(
308
294
),
309
295
)
310
296
else :
311
- if browser_context is None :
297
+ if _global_browser_context is None :
312
298
browser_context = await browser .new_context (
313
299
config = BrowserContextConfig (
314
300
trace_path = save_trace_path if save_trace_path else None ,
@@ -319,7 +305,6 @@ async def run_custom_agent(
319
305
),
320
306
),
321
307
)
322
- )
323
308
324
309
# Create and run agent
325
310
agent = CustomAgent (
@@ -369,6 +354,7 @@ async def run_custom_agent(
369
354
async def run_with_stream (
370
355
agent_type ,
371
356
llm_provider ,
357
+ keep_browser_open ,
372
358
llm_model_name ,
373
359
llm_temperature ,
374
360
llm_base_url ,
@@ -389,7 +375,7 @@ async def run_with_stream(
389
375
tool_call_in_content
390
376
):
391
377
"""Wrapper to run the agent and handle streaming."""
392
- global _global_browser , _global_browser_context
378
+ global _global_browser , _global_browser_context , _global_playwright
393
379
394
380
try :
395
381
# Initialize the global browser if it doesn't exist
@@ -421,6 +407,7 @@ async def run_with_stream(
421
407
agent_type = agent_type ,
422
408
llm_provider = llm_provider ,
423
409
llm_model_name = llm_model_name ,
410
+ keep_browser_open = keep_browser_open ,
424
411
llm_temperature = llm_temperature ,
425
412
llm_base_url = llm_base_url ,
426
413
llm_api_key = llm_api_key ,
@@ -437,10 +424,7 @@ async def run_with_stream(
437
424
max_steps = max_steps ,
438
425
use_vision = use_vision ,
439
426
max_actions_per_step = max_actions_per_step ,
440
- tool_call_in_content = tool_call_in_content ,
441
- browser = _global_browser ,
442
- browser_context = _global_browser_context ,
443
- playwright = _global_playwright if use_own_browser else None
427
+ tool_call_in_content = tool_call_in_content
444
428
)
445
429
)
446
430
@@ -452,7 +436,10 @@ async def run_with_stream(
452
436
# Periodically update the stream while the agent task is running
453
437
while not agent_task .done ():
454
438
try :
455
- html_content = await capture_screenshot (_global_browser_context )
439
+ if isinstance (_global_browser_context , CustomBrowserContext ):
440
+ html_content = await capture_screenshot (_global_browser_context )
441
+ else :
442
+ html_content = "<div class='error' style='width:80vw; height:90vh'>Invalid browser context type</div>"
456
443
except Exception as e :
457
444
html_content = f"<div class='error' style='width:80vw; height:90vh'>Screenshot error: { str (e )} </div>"
458
445
0 commit comments