from utils.llm_utils import get_llm_response from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from pathlib import Path from datetime import datetime import json async def draft_email( recipient: str, subject: str, context: str, tone: str = 'professional' ) -> dict: """ Draft an email based on context Args: recipient: Email recipient subject: Email subject context: Context/purpose of email tone: Tone (professional, friendly, formal, casual) Returns: Dict with drafted email content """ try: prompt = f"""Draft a {tone} email with the following details: Recipient: {recipient} Subject: {subject} Context/Purpose: {context} Write a complete, well-formatted email including: - Appropriate greeting - Clear body paragraphs - Professional closing - Signature placeholder Tone should be: {tone} """ email_body = await get_llm_response(prompt, temperature=0.7) # Create formatted email HTML email_html = f"""
To: {recipient}
Subject: {subject}
{email_body.replace(chr(10), '
')}
""" # Save to file Path("data/outputs").mkdir(parents=True, exist_ok=True) output_path = f"data/outputs/email_draft_{datetime.now().strftime('%Y%m%d_%H%M%S')}.html" with open(output_path, 'w', encoding='utf-8') as f: f.write(email_html) # Also save plain text version txt_path = output_path.replace('.html', '.txt') with open(txt_path, 'w', encoding='utf-8') as f: f.write(f"To: {recipient}\nSubject: {subject}\n\n{email_body}") return { 'success': True, 'output_path_html': output_path, 'output_path_txt': txt_path, 'email_body': email_body, 'recipient': recipient, 'subject': subject } except Exception as e: return {'error': str(e), 'success': False} async def draft_reply_email(original_email: str, reply_context: str, tone: str = 'professional') -> dict: """Draft a reply to an email""" try: prompt = f"""Draft a reply to this email: ORIGINAL EMAIL: {original_email} REPLY CONTEXT/INSTRUCTIONS: {reply_context} Tone: {tone} Write a professional reply email.""" reply = await get_llm_response(prompt, temperature=0.7) Path("data/outputs").mkdir(parents=True, exist_ok=True) output_path = f"data/outputs/email_reply_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt" with open(output_path, 'w', encoding='utf-8') as f: f.write(reply) return { 'success': True, 'output_path': output_path, 'reply': reply } except Exception as e: return {'error': str(e), 'success': False}