-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_mail_watcher.py
More file actions
219 lines (170 loc) · 6.74 KB
/
Copy pathsend_mail_watcher.py
File metadata and controls
219 lines (170 loc) · 6.74 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
SendMailWatcher - Monitors send_mails folder for outgoing email requests.
Watches send_mails/ directory for mail request files.
Copies them to Needs_Action/ so the orchestrator can process them.
Folder structure:
AI_Employee_Vault/
send_mails/ <- User writes email requests here
- to_john_doe.md <- "Send email to john@example.com about project update"
- to_client.md <- "Send email to client@company.com about invoice"
"""
import asyncio
import logging
import shutil
from datetime import datetime
from pathlib import Path
from base_watcher import BaseWatcher
import aiofiles
logger = logging.getLogger("SendMailWatcher")
class SendMailWatcher(BaseWatcher):
"""
Watches send_mails folder for email requests.
Reads mail request files in format:
- to: recipient@example.com
- subject: Email Subject Line
- body: Description/context for the email
Copies to Needs_Action/ for orchestrator processing.
"""
def __init__(
self,
send_mails_path: Path,
needs_action_path: Path,
logs_path: Path,
poll_interval: float = 10.0,
):
"""Initialize the watcher."""
super().__init__(
poll_interval=poll_interval,
max_retries=5,
initial_backoff=1.0,
max_backoff=30.0,
)
self.send_mails_path = Path(send_mails_path)
self.needs_action_path = Path(needs_action_path)
self.logs_path = Path(logs_path)
self._processed_files: set[str] = set()
@property
def name(self) -> str:
return "SendMailWatcher"
async def startup(self) -> None:
"""Initialize paths."""
self.send_mails_path.mkdir(parents=True, exist_ok=True)
self.needs_action_path.mkdir(parents=True, exist_ok=True)
self.logs_path.mkdir(parents=True, exist_ok=True)
logger.info(f"[SendMailWatcher] Started - watching: {self.send_mails_path}")
async def shutdown(self) -> None:
"""Cleanup."""
logger.info(f"[SendMailWatcher] Stopped")
async def poll(self) -> bool:
"""Check for new mail request files."""
mail_files = [
f for f in self.send_mails_path.glob("*.md")
if f.name != ".gitkeep" and f.name not in self._processed_files
]
if not mail_files:
return True
for mail_file in mail_files:
try:
await self._process_mail_request(mail_file)
self._processed_files.add(mail_file.name)
except Exception as e:
logger.error(f"[SendMailWatcher] Failed to process {mail_file.name}: {e}")
return False
return True
async def _process_mail_request(self, mail_path: Path) -> None:
"""Process a mail request file."""
logger.info(f"[SendMailWatcher] Processing: {mail_path.name}")
# Read the request
async with aiofiles.open(mail_path, "r", encoding="utf-8") as f:
content = await f.read()
# Parse recipient from content
recipient = self._extract_recipient(content)
if not recipient:
logger.warning(f"[SendMailWatcher] No recipient found in {mail_path.name}, using filename")
recipient = mail_path.stem.replace("_", " ").replace("-", " ")
# Create Email Draft format matching user's exact pattern
enriched_content = f"""# Email Draft
## To: {recipient}
## Subject: {self._extract_subject(content) or '(No Subject)'}
---
{content.strip()}
---
*Generated: {datetime.now().isoformat()}*"""
# Move to Needs_Action using email_<recipient>_<subject> naming pattern (same as orchestrator generates)
# Extract subject from content if available
subject = self._extract_subject(content) or "no-subject"
slug_subject = self._slugify(subject)[:50]
slug_recipient = self._slugify(recipient)[:30]
new_filename = f"email_{slug_recipient}_{slug_subject}.md"
new_path = self.needs_action_path / new_filename
# Write enriched content to Needs_Action
async with aiofiles.open(new_path, "w", encoding="utf-8") as f:
await f.write(enriched_content)
# Delete the original file from send_mails
mail_path.unlink()
logger.info(f"[SendMailWatcher] Moved to Needs_Action: {new_filename}")
# Log the action
await self._log_action("mail_request_moved", mail_path.name, {"recipient": recipient})
def _extract_recipient(self, content: str) -> str:
"""Extract recipient email from content."""
import re
# Look for "to: email@example.com" or "recipient: email@example.com"
patterns = [
r"^to:\s*(.+)$",
r"^recipient:\s*(.+)$",
r"email.*to:\s*(.+)$",
r"@(?:gmail|hotmail|outlook|yahoo)\.com",
]
content_lower = content.lower()
for pattern in patterns:
match = re.search(pattern, content_lower, re.MULTILINE)
if match:
return match.group(1).strip()
# Try to find any email address
email_match = re.search(r"[\w\.-]+@[\w\.-]+\.\w+", content)
if email_match:
return email_match.group(0)
return ""
def _extract_subject(self, content: str) -> str:
"""Extract subject from content."""
import re
patterns = [
r"^subject:\s*(.+)$",
r"^Subject:\s*(.+)$",
]
for pattern in patterns:
match = re.search(pattern, content, re.MULTILINE)
if match:
return match.group(1).strip()
return ""
def _slugify(self, text: str) -> str:
"""Convert text to a filesystem-safe slug."""
import re
slug = re.sub(r"[^a-zA-Z0-9\s-]", "", text)
slug = re.sub(r"[\s_]+", "-", slug)
slug = slug.strip("-")
return slug.lower() or "untitled"
async def _log_action(self, action: str, filename: str, details: dict) -> None:
"""Log to JSON file."""
import json
log_entry = {
"timestamp": datetime.now().isoformat(),
"action": action,
"filename": filename,
"details": details,
}
log_file = self.logs_path / f"send_mail_watcher_{datetime.now().strftime('%Y-%m-%d')}.jsonl"
async with aiofiles.open(log_file, "a", encoding="utf-8") as f:
await f.write(json.dumps(log_entry) + "\n")
def create_watcher_from_env(
send_mails_path: Path,
needs_action_path: Path,
logs_path: Path,
) -> SendMailWatcher:
"""Create SendMailWatcher from vault paths."""
return SendMailWatcher(
send_mails_path=send_mails_path,
needs_action_path=needs_action_path,
logs_path=logs_path,
poll_interval=10.0,
)