import asyncio from planner_agent import SearchOutput, SearchPlanItems, planner_agent from search_agent import search_agent from writer_agent import writer_agent, ReportData from email_agent import email_agent from agents import Runner, gen_trace_id, trace, tool, model_settings, Agent class ResearchManager: async def run(self, query: str): """Run the deep research process, yielding the status updates and the final report""" trace_id = gen_trace_id() with trace("Research trace", trace_id=trace_id): print( f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}" ) yield f"View trace: https://platform.openai.com/traces/trace?trace_id={trace_id}" print("Starting research...") search_plan = await self.plan_searches(query) yield "Searches planned, starting to search..." search_results = await self.perform_search(search_plan) yield "Searches complete, writing report..." report = await self.write_report(query, search_results) yield "Report written, sending email..." await self.send_email_with_report(report) yield "Email sent, research complete" yield report.report async def plan_searches(self, query: str) -> SearchPlanItems: """Given a research query, plan the searches to perform for the query. The output should be a SearchPlanItems object that contains a list of SearchOutput objects, where each SearchOutput object contains a search query and a reason for why that search is being performed.""" print("Planning searches...") result = await Runner.run(planner_agent, f"Query: {query}") print(f"Will perform {len(result.final_output.searches)} searches") return result.final_output_as(SearchPlanItems) async def perform_search(self, search_plan: SearchPlanItems): # creating a list of task coroutines for each search query in the search plan. tasks = [ asyncio.create_task(self.search(search_item)) for search_item in search_plan.searches ] # running all the search tasks concurrently and waiting for them to complete. The results will be stored in the 'results' variable as a list of summaries corresponding to each search query. results = await asyncio.gather(*tasks) print("All searches completed.") return results ##we would have all the search results and now we share this to the writer to go thorugh it and write a report async def search(self, search_item: SearchOutput): """Given a SearchOutput object, perform the search and return the summary of the search results.""" response = await Runner.run(search_agent, search_item.query) return str(response.final_output) if response.final_output else "" async def write_report(self, query: str, search_results: list[str]): """Given the original research query and a list of search result summaries, write a detailed report that answers the research question. The report should be in markdown format and should be at least 1000 words long.""" print("Thinking about report...") input = f"Original query: {query}\nSummarized search results: {search_results}" result = await Runner.run(writer_agent, input) print("Finished writing report") # The line `return result.final_output_as(ReportData)` is converting the final output of the # report generation process into a specific data type or class called `ReportData`. This # conversion is likely necessary to ensure that the output conforms to a predefined structure # or format expected by the rest of the program or downstream processes. return result.final_output_as(ReportData) async def send_email_with_report(self, report: ReportData): """Given a detailed report in markdown format, send an email with the report converted into clean, well presented HTML. The subject line of the email should be 'Research Report: {report_title}', where {report_title} is a concise title that captures the main topic of the report.""" result = await Runner.run(email_agent, report.report) print(f"Email sent successfully to {result.final_output}") return report