File size: 3,634 Bytes
73112c9
 
 
c5990f9
 
73112c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5990f9
 
73112c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5990f9
73112c9
 
c5990f9
73112c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5990f9
73112c9
c5990f9
73112c9
 
 
 
 
 
 
 
 
 
c5990f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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"""<!DOCTYPE html>
<html>
<head>
    <style>
        body {{ font-family: Arial, sans-serif; line-height: 1.6; }}
        .email-container {{ max-width: 600px; margin: 20px; }}
        .header {{ border-bottom: 2px solid #333; padding-bottom: 10px; }}
        .body {{ margin: 20px 0; }}
        .footer {{ border-top: 1px solid #ccc; padding-top: 10px; color: #666; }}
    </style>
</head>
<body>
    <div class="email-container">
        <div class="header">
            <strong>To:</strong> {recipient}<br>
            <strong>Subject:</strong> {subject}
        </div>
        <div class="body">
            {email_body.replace(chr(10), '<br>')}
        </div>
        <div class="footer">
            <em>Drafted by LifeAdmin AI</em>
        </div>
    </div>
</body>
</html>"""
        
        # 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}