@@ -84,7 +84,7 @@ async def run_deep_research(webui_manager: WebuiManager, components: Dict[Compon
84
84
return
85
85
86
86
# Store base save dir for stop handler
87
- webui_manager ._dr_save_dir = base_save_dir
87
+ webui_manager .dr_save_dir = base_save_dir
88
88
os .makedirs (base_save_dir , exist_ok = True )
89
89
90
90
# --- 2. Initial UI Update ---
@@ -141,29 +141,29 @@ def get_setting(tab: str, key: str, default: Any = None):
141
141
}
142
142
143
143
# --- 4. Initialize or Get Agent ---
144
- if not webui_manager ._dr_agent :
145
- webui_manager ._dr_agent = DeepResearchAgent (
144
+ if not webui_manager .dr_agent :
145
+ webui_manager .dr_agent = DeepResearchAgent (
146
146
llm = llm ,
147
147
browser_config = browser_config_dict ,
148
148
mcp_server_config = mcp_config
149
149
)
150
150
logger .info ("DeepResearchAgent initialized." )
151
151
152
152
# --- 5. Start Agent Run ---
153
- agent_run_coro = await webui_manager ._dr_agent .run (
153
+ agent_run_coro = webui_manager .dr_agent .run (
154
154
topic = task_topic ,
155
155
task_id = task_id_to_resume ,
156
156
save_dir = base_save_dir ,
157
157
max_parallel_browsers = max_parallel_agents
158
158
)
159
159
agent_task = asyncio .create_task (agent_run_coro )
160
- webui_manager ._dr_current_task = agent_task
160
+ webui_manager .dr_current_task = agent_task
161
161
162
162
# Wait briefly for the agent to start and potentially create the task ID/folder
163
163
await asyncio .sleep (1.0 )
164
164
165
165
# Determine the actual task ID being used (agent sets this)
166
- running_task_id = webui_manager ._dr_agent .current_task_id
166
+ running_task_id = webui_manager .dr_agent .current_task_id
167
167
if not running_task_id :
168
168
# Agent might not have set it yet, try to get from result later? Risky.
169
169
# Or derive from resume_task_id if provided?
@@ -176,7 +176,7 @@ def get_setting(tab: str, key: str, default: Any = None):
176
176
else :
177
177
logger .info (f"Agent started with Task ID: { running_task_id } " )
178
178
179
- webui_manager ._dr_task_id = running_task_id # Store for stop handler
179
+ webui_manager .dr_task_id = running_task_id # Store for stop handler
180
180
181
181
# --- 6. Monitor Progress via research_plan.md ---
182
182
if running_task_id :
@@ -187,12 +187,11 @@ def get_setting(tab: str, key: str, default: Any = None):
187
187
else :
188
188
logger .warning ("Cannot monitor plan file: Task ID unknown." )
189
189
plan_file_path = None
190
-
190
+ last_plan_content = None
191
191
while not agent_task .done ():
192
192
update_dict = {}
193
-
194
- # Check for stop signal (agent sets self.stopped)
195
- agent_stopped = getattr (webui_manager ._dr_agent , 'stopped' , False )
193
+ update_dict [resume_task_id_comp ] = gr .update (value = running_task_id )
194
+ agent_stopped = getattr (webui_manager .dr_agent , 'stopped' , False )
196
195
if agent_stopped :
197
196
logger .info ("Stop signal detected from agent state." )
198
197
break # Exit monitoring loop
@@ -204,7 +203,8 @@ def get_setting(tab: str, key: str, default: Any = None):
204
203
if current_mtime > last_plan_mtime :
205
204
logger .info (f"Detected change in { plan_file_path } " )
206
205
plan_content = _read_file_safe (plan_file_path )
207
- if plan_content is not None and plan_content != last_plan_content :
206
+ if last_plan_content is None or (
207
+ plan_content is not None and plan_content != last_plan_content ):
208
208
update_dict [markdown_display_comp ] = gr .update (value = plan_content )
209
209
last_plan_content = plan_content
210
210
last_plan_mtime = current_mtime
@@ -230,7 +230,7 @@ def get_setting(tab: str, key: str, default: Any = None):
230
230
# Try to get task ID from result if not known before
231
231
if not running_task_id and final_result_dict and 'task_id' in final_result_dict :
232
232
running_task_id = final_result_dict ['task_id' ]
233
- webui_manager ._dr_task_id = running_task_id
233
+ webui_manager .dr_task_id = running_task_id
234
234
task_specific_dir = os .path .join (base_save_dir , str (running_task_id ))
235
235
report_file_path = os .path .join (task_specific_dir , "report.md" )
236
236
logger .info (f"Task ID confirmed from result: { running_task_id } " )
@@ -268,22 +268,14 @@ def get_setting(tab: str, key: str, default: Any = None):
268
268
269
269
finally :
270
270
# --- 8. Final UI Reset ---
271
- webui_manager ._dr_current_task = None # Clear task reference
272
- webui_manager ._dr_task_id = None # Clear running task ID
273
- # Optionally close agent resources if needed, e.g., browser pool
274
- if webui_manager ._dr_agent and hasattr (webui_manager ._dr_agent , 'close' ):
275
- try :
276
- await webui_manager ._dr_agent .close () # Assuming an async close method
277
- logger .info ("Closed DeepResearchAgent resources." )
278
- webui_manager ._dr_agent = None
279
- except Exception as e_close :
280
- logger .error (f"Error closing DeepResearchAgent: { e_close } " )
271
+ webui_manager .dr_current_task = None # Clear task reference
272
+ webui_manager .dr_task_id = None # Clear running task ID
281
273
282
274
yield {
283
275
start_button_comp : gr .update (value = "▶️ Run" , interactive = True ),
284
276
stop_button_comp : gr .update (interactive = False ),
285
277
research_task_comp : gr .update (interactive = True ),
286
- resume_task_id_comp : gr .update (interactive = True ),
278
+ resume_task_id_comp : gr .update (value = "" , interactive = True ),
287
279
parallel_num_comp : gr .update (interactive = True ),
288
280
save_dir_comp : gr .update (interactive = True ),
289
281
# Keep download button enabled if file exists
@@ -295,10 +287,10 @@ def get_setting(tab: str, key: str, default: Any = None):
295
287
async def stop_deep_research (webui_manager : WebuiManager ) -> Dict [Component , Any ]:
296
288
"""Handles the Stop button click."""
297
289
logger .info ("Stop button clicked for Deep Research." )
298
- agent = webui_manager ._dr_agent
299
- task = webui_manager ._dr_current_task
300
- task_id = webui_manager ._dr_task_id
301
- base_save_dir = webui_manager ._dr_save_dir
290
+ agent = webui_manager .dr_agent
291
+ task = webui_manager .dr_current_task
292
+ task_id = webui_manager .dr_task_id
293
+ base_save_dir = webui_manager .dr_save_dir
302
294
303
295
stop_button_comp = webui_manager .get_component_by_id ("deep_research_agent.stop_button" )
304
296
start_button_comp = webui_manager .get_component_by_id ("deep_research_agent.start_button" )
@@ -311,15 +303,11 @@ async def stop_deep_research(webui_manager: WebuiManager) -> Dict[Component, Any
311
303
312
304
if agent and task and not task .done ():
313
305
logger .info ("Signalling DeepResearchAgent to stop." )
314
- if hasattr (agent , 'stop' ):
315
- try :
316
- # Assuming stop is synchronous or sets a flag quickly
317
- agent .stop ()
318
- except Exception as e :
319
- logger .error (f"Error calling agent.stop(): { e } " )
320
- else :
321
- logger .warning ("Agent has no 'stop' method. Task cancellation might not be graceful." )
322
- # Task cancellation is handled by the run_deep_research finally block if needed
306
+ try :
307
+ # Assuming stop is synchronous or sets a flag quickly
308
+ await agent .stop ()
309
+ except Exception as e :
310
+ logger .error (f"Error calling agent.stop(): { e } " )
323
311
324
312
# The run_deep_research loop should detect the stop and exit.
325
313
# We yield an intermediate "Stopping..." state. The final reset is done by run_deep_research.
@@ -393,7 +381,7 @@ def create_deep_research_agent_tab(webui_manager: WebuiManager):
393
381
394
382
with gr .Group ():
395
383
research_task = gr .Textbox (label = "Research Task" , lines = 5 ,
396
- value = "Give me a detailed plan for traveling to Switzerland on June 1st." ,
384
+ value = "Give me a detailed travel plan to Switzerland from June 1st to 10th ." ,
397
385
interactive = True )
398
386
with gr .Row ():
399
387
resume_task_id = gr .Textbox (label = "Resume Task ID" , value = "" ,
@@ -418,7 +406,9 @@ def create_deep_research_agent_tab(webui_manager: WebuiManager):
418
406
stop_button = stop_button ,
419
407
markdown_display = markdown_display ,
420
408
markdown_download = markdown_download ,
421
- resume_task_id = resume_task_id
409
+ resume_task_id = resume_task_id ,
410
+ mcp_json_file = mcp_json_file ,
411
+ mcp_server_config = mcp_server_config ,
422
412
)
423
413
)
424
414
webui_manager .add_components ("deep_research_agent" , tab_components )
@@ -430,7 +420,7 @@ def create_deep_research_agent_tab(webui_manager: WebuiManager):
430
420
)
431
421
432
422
dr_tab_outputs = list (tab_components .values ())
433
- all_managed_inputs = webui_manager .get_components ()
423
+ all_managed_inputs = set ( webui_manager .get_components () )
434
424
435
425
# --- Define Event Handler Wrappers ---
436
426
async def start_wrapper (comps : Dict [Component , Any ]) -> AsyncGenerator [Dict [Component , Any ], None ]:
@@ -439,17 +429,17 @@ async def start_wrapper(comps: Dict[Component, Any]) -> AsyncGenerator[Dict[Comp
439
429
440
430
async def stop_wrapper () -> AsyncGenerator [Dict [Component , Any ], None ]:
441
431
update_dict = await stop_deep_research (webui_manager )
442
- yield update_dict # Yield the single dict update
432
+ yield update_dict
443
433
444
434
# --- Connect Handlers ---
445
435
start_button .click (
446
436
fn = start_wrapper ,
447
437
inputs = all_managed_inputs ,
448
- outputs = dr_tab_outputs # Update only components in this tab
438
+ outputs = dr_tab_outputs
449
439
)
450
440
451
441
stop_button .click (
452
442
fn = stop_wrapper ,
453
443
inputs = None ,
454
- outputs = dr_tab_outputs # Update only components in this tab
444
+ outputs = dr_tab_outputs
455
445
)
0 commit comments