Skip to content

Commit f371e4e

Browse files
author
katiue
committed
resolve with new webui
1 parent 7728637 commit f371e4e

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

webui.py

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from src.browser.custom_browser import CustomBrowser
2525
from src.agent.custom_prompts import CustomSystemPrompt
2626
from src.browser.config import BrowserPersistenceConfig
27-
from src.browser.custom_context import BrowserContextConfig
27+
from src.browser.custom_context import BrowserContextConfig, CustomBrowserContext
2828
from src.controller.custom_controller import CustomController
2929
from gradio.themes import Citrus, Default, Glass, Monochrome, Ocean, Origin, Soft, Base
3030
from src.utils.utils import update_model_dropdown, get_latest_files, capture_screenshot
@@ -35,6 +35,7 @@
3535
# Global variables for persistence
3636
_global_browser = None
3737
_global_browser_context = None
38+
_global_playwright = None
3839

3940
async def run_browser_agent(
4041
agent_type,
@@ -57,10 +58,7 @@ async def run_browser_agent(
5758
max_steps,
5859
use_vision,
5960
max_actions_per_step,
60-
tool_call_in_content,
61-
browser,
62-
browser_context,
63-
playwright
61+
tool_call_in_content
6462
):
6563
# Disable recording if the checkbox is unchecked
6664
if not enable_recording:
@@ -101,10 +99,7 @@ async def run_browser_agent(
10199
max_steps=max_steps,
102100
use_vision=use_vision,
103101
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
108103
)
109104
elif agent_type == "custom":
110105
final_result, errors, model_actions, model_thoughts, recorded_files, trace_file = await run_custom_agent(
@@ -122,10 +117,7 @@ async def run_browser_agent(
122117
max_steps=max_steps,
123118
use_vision=use_vision,
124119
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
129121
)
130122
else:
131123
raise ValueError(f"Invalid agent type: {agent_type}")
@@ -157,9 +149,6 @@ async def run_org_agent(
157149
use_vision,
158150
max_actions_per_step,
159151
tool_call_in_content,
160-
browser,
161-
browser_context,
162-
playwright
163152
):
164153
try:
165154
global _global_browser, _global_browser_context
@@ -244,10 +233,7 @@ async def run_custom_agent(
244233
max_steps,
245234
use_vision,
246235
max_actions_per_step,
247-
tool_call_in_content,
248-
browser,
249-
browser_context,
250-
playwright
236+
tool_call_in_content
251237
):
252238
controller = CustomController()
253239
persistence_config = BrowserPersistenceConfig.from_env()
@@ -265,7 +251,7 @@ async def run_custom_agent(
265251
controller = CustomController()
266252

267253
# Initialize global browser if needed
268-
if browser is None:
254+
if _global_browser is None:
269255
browser = CustomBrowser(
270256
config=BrowserConfig(
271257
headless=headless,
@@ -277,7 +263,7 @@ async def run_custom_agent(
277263

278264
# Handle browser context based on configuration
279265
if use_own_browser:
280-
if browser_context is None:
266+
if _global_browser_context is None:
281267
playwright = await async_playwright().start()
282268
chrome_exe = os.getenv("CHROME_PATH", "")
283269
chrome_use_data = os.getenv("CHROME_USER_DATA", "")
@@ -308,7 +294,7 @@ async def run_custom_agent(
308294
),
309295
)
310296
else:
311-
if browser_context is None:
297+
if _global_browser_context is None:
312298
browser_context = await browser.new_context(
313299
config=BrowserContextConfig(
314300
trace_path=save_trace_path if save_trace_path else None,
@@ -319,7 +305,6 @@ async def run_custom_agent(
319305
),
320306
),
321307
)
322-
)
323308

324309
# Create and run agent
325310
agent = CustomAgent(
@@ -369,6 +354,7 @@ async def run_custom_agent(
369354
async def run_with_stream(
370355
agent_type,
371356
llm_provider,
357+
keep_browser_open,
372358
llm_model_name,
373359
llm_temperature,
374360
llm_base_url,
@@ -389,7 +375,7 @@ async def run_with_stream(
389375
tool_call_in_content
390376
):
391377
"""Wrapper to run the agent and handle streaming."""
392-
global _global_browser, _global_browser_context
378+
global _global_browser, _global_browser_context, _global_playwright
393379

394380
try:
395381
# Initialize the global browser if it doesn't exist
@@ -421,6 +407,7 @@ async def run_with_stream(
421407
agent_type=agent_type,
422408
llm_provider=llm_provider,
423409
llm_model_name=llm_model_name,
410+
keep_browser_open=keep_browser_open,
424411
llm_temperature=llm_temperature,
425412
llm_base_url=llm_base_url,
426413
llm_api_key=llm_api_key,
@@ -437,10 +424,7 @@ async def run_with_stream(
437424
max_steps=max_steps,
438425
use_vision=use_vision,
439426
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
444428
)
445429
)
446430

@@ -452,7 +436,10 @@ async def run_with_stream(
452436
# Periodically update the stream while the agent task is running
453437
while not agent_task.done():
454438
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>"
456443
except Exception as e:
457444
html_content = f"<div class='error' style='width:80vw; height:90vh'>Screenshot error: {str(e)}</div>"
458445

0 commit comments

Comments
 (0)