Skip to content

Commit b10a606

Browse files
author
katiue
committed
reduce changes
1 parent 9ed66a0 commit b10a606

File tree

1 file changed

+17
-51
lines changed

1 file changed

+17
-51
lines changed

webui.py

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ async def run_custom_agent(
234234
use_vision,
235235
max_actions_per_step,
236236
tool_call_in_content
237-
):
237+
):
238238
try:
239239
global _global_browser, _global_browser_context
240240

@@ -259,57 +259,27 @@ async def run_custom_agent(
259259
)
260260

261261
# Handle browser context based on configuration
262-
if use_own_browser:
263-
if _global_browser_context is None:
264-
playwright = await async_playwright().start()
265-
chrome_exe = os.getenv("CHROME_PATH", "")
266-
chrome_use_data = os.getenv("CHROME_USER_DATA", "")
267-
268-
browser_context = await playwright.chromium.launch_persistent_context(
269-
user_data_dir=chrome_use_data,
270-
executable_path=chrome_exe,
262+
263+
if _global_browser_context is None:
264+
_global_browser_context = await _global_browser.new_context(
265+
config=BrowserContextConfig(
266+
trace_path=save_trace_path if save_trace_path else None,
267+
save_recording_path=save_recording_path if save_recording_path else None,
271268
no_viewport=False,
272-
headless=headless,
273-
user_agent=(
274-
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
275-
"(KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
276-
),
277-
java_script_enabled=True,
278-
bypass_csp=disable_security,
279-
ignore_https_errors=disable_security,
280-
record_video_dir=save_recording_path if save_recording_path else None,
281-
record_video_size={"width": window_w, "height": window_h},
282-
)
283-
browser_context = await _global_browser.new_context(
284-
config=BrowserContextConfig(
285-
trace_path=save_trace_path if save_trace_path else None,
286-
save_recording_path=save_recording_path if save_recording_path else None,
287-
no_viewport=False,
288-
browser_window_size=BrowserContextWindowSize(
289-
width=window_w, height=window_h
290-
),
291-
),
292-
)
293-
else:
294-
if _global_browser_context is None:
295-
browser_context = await _global_browser.new_context(
296-
config=BrowserContextConfig(
297-
trace_path=save_trace_path if save_trace_path else None,
298-
save_recording_path=save_recording_path if save_recording_path else None,
299-
no_viewport=False,
300-
browser_window_size=BrowserContextWindowSize(
301-
width=window_w, height=window_h
302-
),
269+
browser_window_size=BrowserContextWindowSize(
270+
width=window_w, height=window_h
303271
),
304272
)
273+
)
305274

306275
# Create and run agent
307276
agent = CustomAgent(
308277
task=task,
309278
add_infos=add_infos,
310279
use_vision=use_vision,
311280
llm=llm,
312-
browser_context=browser_context,
281+
browser=_global_browser,
282+
browser_context=_global_browser_context,
313283
controller=controller,
314284
system_prompt_class=CustomSystemPrompt,
315285
max_actions_per_step=max_actions_per_step,
@@ -334,18 +304,14 @@ async def run_custom_agent(
334304
trace_file = {}
335305
finally:
336306
# Handle cleanup based on persistence configuration
337-
if not persistence_config.persistent_session:
338-
if browser_context:
339-
await browser_context.close()
340-
browser_context = None
341-
342-
if playwright:
343-
await playwright.stop()
344-
playwright = None
307+
if not keep_browser_open:
308+
if _global_browser_context:
309+
await _global_browser_context.close()
310+
_global_browser_context = None
345311

346312
if _global_browser:
347313
await _global_browser.close()
348-
browser = None
314+
_global_browser = None
349315
return final_result, errors, model_actions, model_thoughts, trace_file.get('.webm'), recorded_files.get('.zip')
350316

351317
async def run_with_stream(

0 commit comments

Comments
 (0)