import os from typing import Dict import sendgrid from sendgrid.helpers.mail import Content, Email, Mail, To from agents import Agent, function_tool FROM_EMAIL = "stevenshi1126@gmail.com" def deliver_email(subject: str, html_body: str, recipient_email: str) -> None: """Send HTML via SendGrid: subject, HTML body, recipient (from address is fixed).""" sg = sendgrid.SendGridAPIClient(api_key=os.environ["SENDGRID_API_KEY"]) from_email = Email(FROM_EMAIL) to_email = To(recipient_email) content = Content("text/html", html_body) mail = Mail(from_email, to_email, subject, content).get() response = sg.client.mail.send.post(request_body=mail) print("Email response", response.status_code) def build_email_agent(recipient_email: str) -> Agent: """Agent turns markdown into HTML and calls send_email; To address is fixed in the tool.""" @function_tool def send_email(subject: str, html_body: str) -> Dict[str, str]: """Send one email with this subject and HTML body.""" deliver_email(subject, html_body, recipient_email) return {"status": "success"} instructions = ( "You receive a markdown research report. Convert it into clean, readable HTML: sensible " "headings, paragraphs, and lists; preserve structure and emphasis. Choose a concise subject line. " "Call send_email exactly once with subject and html_body. The recipient is already configured." ) return Agent( name="Email agent", instructions=instructions, tools=[send_email], model="gpt-4o-mini", )