Skip to content

Commit fcc67d3

Browse files
author
katiue
committed
headless streaming function
1 parent 8967be7 commit fcc67d3

File tree

2 files changed

+95
-99
lines changed

2 files changed

+95
-99
lines changed

src/utils/utils.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,15 @@ def get_latest_files(directory: str, file_types: list = ['.webm', '.zip']) -> Di
165165
print(f"Error getting latest {file_type} file: {e}")
166166

167167
return latest_files
168-
async def capture_screenshot(browser_context: CustomBrowserContext) -> str:
168+
async def capture_screenshot(browser_context) -> str:
169169
"""Capture and encode a screenshot"""
170-
latest_screenshot = ""
171170
try:
172171
# Extract the Playwright browser instance
173172
playwright_browser = browser_context.browser.playwright_browser # Ensure this is correct.
174173

175174
# Check if the browser instance is valid and if an existing context can be reused
176175
if playwright_browser and playwright_browser.contexts:
177176
playwright_context = playwright_browser.contexts[0]
178-
else:
179-
return latest_screenshot
180177

181178
# Access pages in the context
182179
if playwright_context:
@@ -199,7 +196,7 @@ async def capture_screenshot(browser_context: CustomBrowserContext) -> str:
199196
encoded = base64.b64encode(screenshot).decode('utf-8')
200197
return f'<img src="data:image/jpeg;base64,{encoded}" style="width:80vw; height:90vh ; border:1px solid #ccc;">'
201198
except Exception as e:
202-
return f"<div class='error' style='width:80vw; height:90vh'>Screenshot failed: {str(e)}</div>"
199+
return f"<div class='error' style='width:80vw; height:90vh'>Waiting for browser session...</div>"
203200

204201
except Exception as e:
205-
return f"<div class='error' style='width:80vw; height:90vh'>Screenshot error: {str(e)}</div>"
202+
return f"<div class='error' style='width:80vw; height:90vh'>Waiting for browser session...</div>"

webui.py

Lines changed: 92 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ async def run_org_agent(
216216
await _global_browser.close()
217217
_global_browser = None
218218

219-
220219
async def run_custom_agent(
221220
llm,
222221
use_own_browser,
@@ -332,72 +331,94 @@ async def run_with_stream(
332331
max_actions_per_step,
333332
tool_call_in_content
334333
):
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,
344374
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
346386
)
347387
)
348388

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
396413
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"
398419
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+
401422
yield [
402423
html_content,
403424
final_result,
@@ -407,39 +428,18 @@ async def run_with_stream(
407428
latest_videos,
408429
trace,
409430
]
410-
await asyncio.sleep(0.01)
411431

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"
419432
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+
]
443443

444444
# Define the theme map globally
445445
theme_map = {
@@ -735,7 +735,6 @@ def list_recordings(save_recording_path):
735735

736736
use_own_browser.change(fn=close_global_browser)
737737
keep_browser_open.change(fn=close_global_browser)
738-
739738
# Run button click handler
740739
run_button.click(
741740
fn=run_with_stream,

0 commit comments

Comments
 (0)