Skip to content

Commit 808d40c

Browse files
author
katiue
committed
optimize code
1 parent 4c1240c commit 808d40c

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

webui.py

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async def run_browser_agent(
8686
)
8787

8888
if agent_type == "org":
89-
final_result, errors, model_actions, model_thoughts = await run_org_agent(
89+
final_result, errors, model_actions, model_thoughts, recorded_files, trace_file = await run_org_agent(
9090
llm=llm,
9191
headless=headless,
9292
disable_security=disable_security,
@@ -99,7 +99,6 @@ async def run_browser_agent(
9999
use_vision=use_vision,
100100
max_actions_per_step=max_actions_per_step,
101101
tool_call_in_content=tool_call_in_content,
102-
browser_context=browser_context,
103102
)
104103
elif agent_type == "custom":
105104
final_result, errors, model_actions, model_thoughts = await run_custom_agent(
@@ -117,11 +116,10 @@ async def run_browser_agent(
117116
use_vision=use_vision,
118117
max_actions_per_step=max_actions_per_step,
119118
tool_call_in_content=tool_call_in_content,
120-
browser_context=browser_context,
121119
)
122120
else:
123121
raise ValueError(f"Invalid agent type: {agent_type}")
124-
122+
125123
# Get the list of videos after the agent runs (if recording is enabled)
126124
latest_video = None
127125
if save_recording_path:
@@ -147,61 +145,41 @@ async def run_org_agent(
147145
use_vision,
148146
max_actions_per_step,
149147
tool_call_in_content,
150-
browser_context=None, # receive context
151148
):
152-
browser = None
153-
if browser_context is None:
154-
browser = Browser(
155-
config=BrowserConfig(
156-
headless=headless, # Force non-headless for streaming
157-
disable_security=disable_security,
158-
extra_chromium_args=[f'--window-size={window_w},{window_h}'],
159-
)
149+
browser = Browser(
150+
config=BrowserConfig(
151+
headless=headless, # Force non-headless for streaming
152+
disable_security=disable_security,
153+
extra_chromium_args=[f'--window-size={window_w},{window_h}'],
160154
)
161-
async with await browser.new_context(
162-
config=BrowserContextConfig(
163-
trace_path=save_trace_path if save_trace_path else None,
164-
save_recording_path=save_recording_path if save_recording_path else None,
165-
no_viewport=False,
166-
browser_window_size=BrowserContextWindowSize(width=window_w, height=window_h),
167-
)
168-
) as browser_context_in:
169-
agent = Agent(
170-
task=task,
171-
llm=llm,
172-
use_vision=use_vision,
173-
max_actions_per_step=max_actions_per_step,
174-
tool_call_in_content=tool_call_in_content,
175-
browser_context=browser_context_in,
155+
)
156+
async with await browser.new_context(
157+
config=BrowserContextConfig(
158+
trace_path=save_trace_path if save_trace_path else None,
159+
save_recording_path=save_recording_path if save_recording_path else None,
160+
no_viewport=False,
161+
browser_window_size=BrowserContextWindowSize(width=window_w, height=window_h),
176162
)
177-
history = await agent.run(max_steps=max_steps)
178-
179-
final_result = history.final_result()
180-
errors = history.errors()
181-
model_actions = history.model_actions()
182-
model_thoughts = history.model_thoughts()
183-
184-
recorded_files = get_latest_files(save_recording_path)
185-
trace_file = get_latest_files(save_recording_path + "/../traces")
186-
187-
await browser.close()
188-
return final_result, errors, model_actions, model_thoughts, recorded_files.get('.webm'), trace_file.get('.zip')
189-
else:
190-
# Reuse existing context
163+
) as browser_context:
191164
agent = Agent(
192165
task=task,
193166
llm=llm,
194167
use_vision=use_vision,
195168
max_actions_per_step=max_actions_per_step,
169+
tool_call_in_content=tool_call_in_content,
196170
browser_context=browser_context,
197171
)
198172
history = await agent.run(max_steps=max_steps)
173+
199174
final_result = history.final_result()
200175
errors = history.errors()
201176
model_actions = history.model_actions()
202177
model_thoughts = history.model_thoughts()
178+
203179
recorded_files = get_latest_files(save_recording_path)
204180
trace_file = get_latest_files(save_trace_path)
181+
182+
await browser.close()
205183
return final_result, errors, model_actions, model_thoughts, recorded_files.get('.webm'), trace_file.get('.zip')
206184

207185
async def run_custom_agent(
@@ -219,7 +197,6 @@ async def run_custom_agent(
219197
use_vision,
220198
max_actions_per_step,
221199
tool_call_in_content,
222-
browser_context=None, # receive context
223200
):
224201
global _global_browser, _global_browser_context, _global_playwright
225202

@@ -431,7 +408,7 @@ async def run_with_stream(
431408
try:
432409
result = await agent_task
433410
if isinstance(result, tuple) and len(result) == 6:
434-
final_result, errors, model_actions, model_thoughts, recording, trace = result
411+
final_result, errors, model_actions, model_thoughts, recording, trace = agent_task
435412
else:
436413
errors = "Unexpected result format from agent"
437414
except Exception as e:

0 commit comments

Comments
 (0)