@@ -189,7 +189,7 @@ async def run_browser_agent(
189
189
trace_file ,
190
190
history_file ,
191
191
gr .update (value = "Stop" , interactive = True ), # Re-enable stop button
192
- gr .update (value = "Run" , interactive = True ) # Re-enable run button
192
+ gr .update (interactive = True ) # Re-enable run button
193
193
)
194
194
195
195
except Exception as e :
@@ -205,7 +205,7 @@ async def run_browser_agent(
205
205
None , # history_file
206
206
None , # trace_file
207
207
gr .update (value = "Stop" , interactive = True ), # Re-enable stop button
208
- gr .update (value = "Run" , interactive = True ) # Re-enable run button
208
+ gr .update (interactive = True ) # Re-enable run button
209
209
)
210
210
211
211
@@ -422,6 +422,7 @@ async def run_with_stream(
422
422
max_actions_per_step ,
423
423
tool_call_in_content
424
424
):
425
+ global _global_agent_state
425
426
stream_vw = 80
426
427
stream_vh = int (80 * window_h // window_w )
427
428
if not headless :
@@ -454,6 +455,7 @@ async def run_with_stream(
454
455
yield [html_content ] + list (result )
455
456
else :
456
457
try :
458
+ _global_agent_state .clear_stop ()
457
459
# Run the browser agent in the background
458
460
agent_task = asyncio .create_task (
459
461
run_browser_agent (
@@ -485,7 +487,7 @@ async def run_with_stream(
485
487
# Initialize values for streaming
486
488
html_content = f"<h1 style='width:{ stream_vw } vw; height:{ stream_vh } vh'>Using browser...</h1>"
487
489
final_result = errors = model_actions = model_thoughts = ""
488
- latest_videos = trace = None
490
+ latest_videos = trace = history_file = None
489
491
490
492
491
493
# Periodically update the stream while the agent task is running
@@ -498,27 +500,40 @@ async def run_with_stream(
498
500
html_content = f"<h1 style='width:{ stream_vw } vw; height:{ stream_vh } vh'>Waiting for browser session...</h1>"
499
501
except Exception as e :
500
502
html_content = f"<h1 style='width:{ stream_vw } vw; height:{ stream_vh } vh'>Waiting for browser session...</h1>"
501
-
502
- yield [
503
- html_content ,
504
- final_result ,
505
- errors ,
506
- model_actions ,
507
- model_thoughts ,
508
- latest_videos ,
509
- trace ,
510
- gr .update (value = "Stop" , interactive = True ), # Re-enable stop button
511
- gr .update (value = "Run" , interactive = True ) # Re-enable run button
512
- ]
503
+
504
+ if _global_agent_state and _global_agent_state .is_stop_requested ():
505
+ yield [
506
+ html_content ,
507
+ final_result ,
508
+ errors ,
509
+ model_actions ,
510
+ model_thoughts ,
511
+ latest_videos ,
512
+ trace ,
513
+ history_file ,
514
+ gr .update (value = "Stopping..." , interactive = False ), # stop_button
515
+ gr .update (interactive = False ), # run_button
516
+ ]
517
+ break
518
+ else :
519
+ yield [
520
+ html_content ,
521
+ final_result ,
522
+ errors ,
523
+ model_actions ,
524
+ model_thoughts ,
525
+ latest_videos ,
526
+ trace ,
527
+ history_file ,
528
+ gr .update (value = "Stop" , interactive = True ), # Re-enable stop button
529
+ gr .update (interactive = True ) # Re-enable run button
530
+ ]
513
531
await asyncio .sleep (0.05 )
514
532
515
533
# Once the agent task completes, get the results
516
534
try :
517
535
result = await agent_task
518
- if isinstance (result , tuple ) and len (result ) == 8 :
519
- final_result , errors , model_actions , model_thoughts , latest_videos , trace , stop_button , run_button = result
520
- else :
521
- errors = "Unexpected result format from agent"
536
+ final_result , errors , model_actions , model_thoughts , latest_videos , trace , history_file , stop_button , run_button = result
522
537
except Exception as e :
523
538
errors = f"Agent error: { str (e )} "
524
539
@@ -530,6 +545,7 @@ async def run_with_stream(
530
545
model_thoughts ,
531
546
latest_videos ,
532
547
trace ,
548
+ history_file ,
533
549
stop_button ,
534
550
run_button
535
551
]
@@ -544,8 +560,9 @@ async def run_with_stream(
544
560
"" ,
545
561
None ,
546
562
None ,
563
+ None ,
547
564
gr .update (value = "Stop" , interactive = True ), # Re-enable stop button
548
- gr .update (value = "Run" , interactive = True ) # Re-enable run button
565
+ gr .update (interactive = True ) # Re-enable run button
549
566
]
550
567
551
568
# Define the theme map globally
0 commit comments