@@ -86,7 +86,7 @@ async def run_browser_agent(
86
86
)
87
87
88
88
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 (
90
90
llm = llm ,
91
91
headless = headless ,
92
92
disable_security = disable_security ,
@@ -99,7 +99,6 @@ async def run_browser_agent(
99
99
use_vision = use_vision ,
100
100
max_actions_per_step = max_actions_per_step ,
101
101
tool_call_in_content = tool_call_in_content ,
102
- browser_context = browser_context ,
103
102
)
104
103
elif agent_type == "custom" :
105
104
final_result , errors , model_actions , model_thoughts = await run_custom_agent (
@@ -117,11 +116,10 @@ async def run_browser_agent(
117
116
use_vision = use_vision ,
118
117
max_actions_per_step = max_actions_per_step ,
119
118
tool_call_in_content = tool_call_in_content ,
120
- browser_context = browser_context ,
121
119
)
122
120
else :
123
121
raise ValueError (f"Invalid agent type: { agent_type } " )
124
-
122
+
125
123
# Get the list of videos after the agent runs (if recording is enabled)
126
124
latest_video = None
127
125
if save_recording_path :
@@ -147,61 +145,41 @@ async def run_org_agent(
147
145
use_vision ,
148
146
max_actions_per_step ,
149
147
tool_call_in_content ,
150
- browser_context = None , # receive context
151
148
):
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 } ' ],
160
154
)
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 ),
176
162
)
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 :
191
164
agent = Agent (
192
165
task = task ,
193
166
llm = llm ,
194
167
use_vision = use_vision ,
195
168
max_actions_per_step = max_actions_per_step ,
169
+ tool_call_in_content = tool_call_in_content ,
196
170
browser_context = browser_context ,
197
171
)
198
172
history = await agent .run (max_steps = max_steps )
173
+
199
174
final_result = history .final_result ()
200
175
errors = history .errors ()
201
176
model_actions = history .model_actions ()
202
177
model_thoughts = history .model_thoughts ()
178
+
203
179
recorded_files = get_latest_files (save_recording_path )
204
180
trace_file = get_latest_files (save_trace_path )
181
+
182
+ await browser .close ()
205
183
return final_result , errors , model_actions , model_thoughts , recorded_files .get ('.webm' ), trace_file .get ('.zip' )
206
184
207
185
async def run_custom_agent (
@@ -219,7 +197,6 @@ async def run_custom_agent(
219
197
use_vision ,
220
198
max_actions_per_step ,
221
199
tool_call_in_content ,
222
- browser_context = None , # receive context
223
200
):
224
201
global _global_browser , _global_browser_context , _global_playwright
225
202
@@ -431,7 +408,7 @@ async def run_with_stream(
431
408
try :
432
409
result = await agent_task
433
410
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
435
412
else :
436
413
errors = "Unexpected result format from agent"
437
414
except Exception as e :
0 commit comments