Skip to content

Commit 64acdf8

Browse files
committed
feat: Enhance error handling and reporting in deep research module
- Refactored deep research process to separate report generation logic - Added error handling to generate partial reports when research is interrupted - Implemented error notification in generated markdown reports - Improved logging and error tracking during research process
1 parent 95f7bae commit 64acdf8

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/utils/deep_research.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,23 @@ async def deep_research(task, llm, agent_state=None, **kwargs):
269269
logger.info("\nFinish Searching, Start Generating Report...")
270270

271271
# 5. Report Generation in Markdown (or JSON if you prefer)
272+
return await generate_final_report(task, history_infos, save_dir, llm)
273+
274+
except Exception as e:
275+
logger.error(f"Deep research Error: {e}")
276+
return await generate_final_report(task, history_infos, save_dir, llm, str(e))
277+
finally:
278+
if browser:
279+
await browser.close()
280+
if browser_context:
281+
await browser_context.close()
282+
logger.info("Browser closed.")
283+
284+
async def generate_final_report(task, history_infos, save_dir, llm, error_msg=None):
285+
"""Generate report from collected information with error handling"""
286+
try:
287+
logger.info("\nAttempting to generate final report from collected data...")
288+
272289
writer_system_prompt = """
273290
You are a **Deep Researcher** and a professional report writer tasked with creating polished, high-quality reports that fully meet the user's needs, based on the user's instructions and the relevant information provided. You will write the report using Markdown format, ensuring it is both informative and visually appealing.
274291
@@ -314,21 +331,21 @@ async def deep_research(task, llm, agent_state=None, **kwargs):
314331
logger.info(ai_report_msg.reasoning_content)
315332
logger.info("🤯 End Report Deep Thinking")
316333
report_content = ai_report_msg.content
317-
# Remove ```markdown or ``` at the *very beginning* and ``` at the *very end*, with optional whitespace
318334
report_content = re.sub(r"^```\s*markdown\s*|^\s*```|```\s*$", "", report_content, flags=re.MULTILINE)
319335
report_content = report_content.strip()
336+
337+
# Add error notification to the report
338+
if error_msg:
339+
report_content = f"## ⚠️ Research Incomplete - Partial Results\n" \
340+
f"**The research process was interrupted by an error:** {error_msg}\n\n" \
341+
f"{report_content}"
342+
320343
report_file_path = os.path.join(save_dir, "final_report.md")
321344
with open(report_file_path, "w", encoding="utf-8") as f:
322345
f.write(report_content)
323346
logger.info(f"Save Report at: {report_file_path}")
324347
return report_content, report_file_path
325348

326-
except Exception as e:
327-
logger.error(f"Deep research Error: {e}")
328-
return "", None
329-
finally:
330-
if browser:
331-
await browser.close()
332-
if browser_context:
333-
await browser_context.close()
334-
logger.info("Browser closed.")
349+
except Exception as report_error:
350+
logger.error(f"Failed to generate partial report: {report_error}")
351+
return f"Error generating report: {str(report_error)}", None

0 commit comments

Comments
 (0)