39
39
# Global variables for persistence
40
40
_global_browser = None
41
41
_global_browser_context = None
42
+ _global_agent = None
42
43
43
44
# Create the global agent state instance
44
45
_global_agent_state = AgentState ()
45
46
46
47
async def stop_agent ():
47
48
"""Request the agent to stop and update UI with enhanced feedback"""
48
- global _global_agent_state , _global_browser_context , _global_browser
49
+ global _global_agent_state , _global_browser_context , _global_browser , _global_agent
49
50
50
51
try :
51
52
# Request stop
52
- _global_agent_state . request_stop ()
53
+ _global_agent . stop ()
53
54
54
55
# Update UI immediately
55
56
message = "Stop requested - the agent will halt at the next safe point"
@@ -247,7 +248,7 @@ async def run_org_agent(
247
248
tool_calling_method
248
249
):
249
250
try :
250
- global _global_browser , _global_browser_context , _global_agent_state
251
+ global _global_browser , _global_browser_context , _global_agent_state , _global_agent
251
252
252
253
# Clear any previous stop request
253
254
_global_agent_state .clear_stop ()
@@ -284,20 +285,21 @@ async def run_org_agent(
284
285
),
285
286
)
286
287
)
287
-
288
- agent = Agent (
289
- task = task ,
290
- llm = llm ,
291
- use_vision = use_vision ,
292
- browser = _global_browser ,
293
- browser_context = _global_browser_context ,
294
- max_actions_per_step = max_actions_per_step ,
295
- tool_calling_method = tool_calling_method
296
- )
297
- history = await agent .run (max_steps = max_steps )
298
288
299
- history_file = os .path .join (save_agent_history_path , f"{ agent .agent_id } .json" )
300
- agent .save_history (history_file )
289
+ if _global_agent is None :
290
+ _global_agent = Agent (
291
+ task = task ,
292
+ llm = llm ,
293
+ use_vision = use_vision ,
294
+ browser = _global_browser ,
295
+ browser_context = _global_browser_context ,
296
+ max_actions_per_step = max_actions_per_step ,
297
+ tool_calling_method = tool_calling_method
298
+ )
299
+ history = await _global_agent .run (max_steps = max_steps )
300
+
301
+ history_file = os .path .join (save_agent_history_path , f"{ _global_agent .agent_id } .json" )
302
+ _global_agent .save_history (history_file )
301
303
302
304
final_result = history .final_result ()
303
305
errors = history .errors ()
@@ -313,6 +315,7 @@ async def run_org_agent(
313
315
errors = str (e ) + "\n " + traceback .format_exc ()
314
316
return '' , errors , '' , '' , None , None
315
317
finally :
318
+ _global_agent = None
316
319
# Handle cleanup based on persistence configuration
317
320
if not keep_browser_open :
318
321
if _global_browser_context :
@@ -342,7 +345,7 @@ async def run_custom_agent(
342
345
tool_calling_method
343
346
):
344
347
try :
345
- global _global_browser , _global_browser_context , _global_agent_state
348
+ global _global_browser , _global_browser_context , _global_agent_state , _global_agent
346
349
347
350
# Clear any previous stop request
348
351
_global_agent_state .clear_stop ()
@@ -384,24 +387,24 @@ async def run_custom_agent(
384
387
)
385
388
386
389
# Create and run agent
387
- agent = CustomAgent (
388
- task = task ,
389
- add_infos = add_infos ,
390
- use_vision = use_vision ,
391
- llm = llm ,
392
- browser = _global_browser ,
393
- browser_context = _global_browser_context ,
394
- controller = controller ,
395
- system_prompt_class = CustomSystemPrompt ,
396
- agent_prompt_class = CustomAgentMessagePrompt ,
397
- max_actions_per_step = max_actions_per_step ,
398
- agent_state = _global_agent_state ,
399
- tool_calling_method = tool_calling_method
400
- )
401
- history = await agent .run (max_steps = max_steps )
390
+ if _global_agent is None :
391
+ _global_agent = CustomAgent (
392
+ task = task ,
393
+ add_infos = add_infos ,
394
+ use_vision = use_vision ,
395
+ llm = llm ,
396
+ browser = _global_browser ,
397
+ browser_context = _global_browser_context ,
398
+ controller = controller ,
399
+ system_prompt_class = CustomSystemPrompt ,
400
+ agent_prompt_class = CustomAgentMessagePrompt ,
401
+ max_actions_per_step = max_actions_per_step ,
402
+ tool_calling_method = tool_calling_method
403
+ )
404
+ history = await _global_agent .run (max_steps = max_steps )
402
405
403
- history_file = os .path .join (save_agent_history_path , f"{ agent .agent_id } .json" )
404
- agent .save_history (history_file )
406
+ history_file = os .path .join (save_agent_history_path , f"{ _global_agent .agent_id } .json" )
407
+ _global_agent .save_history (history_file )
405
408
406
409
final_result = history .final_result ()
407
410
errors = history .errors ()
@@ -417,6 +420,7 @@ async def run_custom_agent(
417
420
errors = str (e ) + "\n " + traceback .format_exc ()
418
421
return '' , errors , '' , '' , None , None
419
422
finally :
423
+ _global_agent = None
420
424
# Handle cleanup based on persistence configuration
421
425
if not keep_browser_open :
422
426
if _global_browser_context :
0 commit comments