Spaces:
Sleeping
Sleeping
| from agents import Agent, Runner, trace, gen_trace_id | |
| from search_agent import search_agent | |
| from planner_agent_JD import planner_agent, WebSearchItem, WebSearchPlan | |
| from writer_agent import writer_agent, ReportData | |
| from email_agent import email_agent | |
| import asyncio | |
| # Wrap each agent as a Tool | |
| planner_tool = planner_agent.as_tool(tool_name="planner", tool_description="Plan the best search queries to address the user's question") | |
| search_tool = search_agent.as_tool(tool_name="search", tool_description="Search the web for information") | |
| writer_tool = writer_agent.as_tool(tool_name="write", tool_description="Write a report from the search results") | |
| email_tool = email_agent.as_tool(tool_name="email", tool_description="Send the report to the intended recipient") | |
| # Pass only Tools, not Agents, to execution_manager | |
| tools = [planner_tool, search_tool, writer_tool, email_tool] | |
| handoffs = [email_tool] | |
| execution_manager_instructions = ( | |
| "You are an execution manager responsible for conducting deep research projects using the tools provided. " | |
| "You never perform research or write content yourself; you always use the available tools to accomplish each step. " | |
| "You begin by planning the best search queries to address the user's question. " | |
| "Then you use those queries to gather information with the search tool. " | |
| "Next, you use the writer tool to synthesize the search results into a clear, useful report. " | |
| "Finally, you use the email tool to format and send the final report to the intended recipient. " | |
| "You may use each tool more than once if the results are unsatisfactory. " | |
| "Only proceed to the next step when you're confident the current output is high-quality and complete. " | |
| "Your role is to manage this multi-stage workflow carefully, ensuring accuracy, clarity, and usefulness at each stage." | |
| ) | |
| execution_manager = Agent( | |
| name="Execution Manager", | |
| instructions=execution_manager_instructions, | |
| tools=tools, | |
| handoffs=handoffs, | |
| model="gpt-4o-mini" | |
| ) | |
| class ResearchManager: | |
| async def run(self, refined_query: str): | |
| trace_id = gen_trace_id() | |
| with trace("Research trace", trace_id=trace_id): | |
| trace_url = f"https://platform.openai.com/traces/trace_id={trace_id}" | |
| print(f"View trace: {trace_url}") | |
| yield trace_url | |
| print("Running full research on:", refined_query) | |
| result = await Runner.run(execution_manager, refined_query) | |
| yield "Research complete." | |
| if hasattr(result.final_output, "markdown_report"): | |
| report = result.final_output.markdown_report | |
| yield report | |
| # If email html content is accessible, yield it too | |
| if hasattr(result.final_output, "email_html"): | |
| yield f"📧 Email Content:\n{result.final_output.email_html}" | |
| else: | |
| # fallback: yield the report again or an email sent message | |
| yield "📧 Email sent successfully with the report content above." | |
| else: | |
| yield str(result.final_output) | |