-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_classifier_agent.py
More file actions
283 lines (236 loc) · 10.1 KB
/
Copy pathemail_classifier_agent.py
File metadata and controls
283 lines (236 loc) · 10.1 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import json
import time
import logging
import os
from datetime import datetime, timedelta, timezone
from typing import Dict
from gmail_client import GmailClient
from openrouter_classifier import OpenRouterClassifier
import config
logger = logging.getLogger(__name__)
class EmailClassifierAgent:
"""Main agent for classifying and labeling Gmail emails."""
def __init__(self):
"""Initialize the email classifier agent."""
# Initialize Gmail client
self.gmail_client = GmailClient(
credentials_path=config.GMAIL_CREDENTIALS_PATH,
token_path=config.GMAIL_TOKEN_PATH,
scopes=config.GMAIL_SCOPES,
headless=config.GMAIL_HEADLESS_MODE,
)
# Initialize OpenRouter classifier
self.classifier = OpenRouterClassifier(
api_key=config.OPENROUTER_API_KEY,
model=config.OPENROUTER_MODEL,
temperature=config.OPENROUTER_TEMPERATURE,
max_tokens=config.OPENROUTER_MAX_TOKENS,
)
# Create Gmail labels if they don't exist
self.label_id_map = self._initialize_labels()
# Load processed email state
self.state_file = config.STATE_FILE
self.retention_days = config.STATE_RETENTION_DAYS
self.processed_emails: Dict[str, str] = self._load_state()
logger.info(
f"Email Classifier Agent initialized with OpenRouter (model: {config.OPENROUTER_MODEL})"
)
logger.info(
f"Loaded {len(self.processed_emails)} processed emails from state "
f"(retention: {self.retention_days} days)"
)
def _initialize_labels(self) -> Dict[str, str]:
"""
Create Gmail labels for all configured labels.
Returns:
Dictionary mapping label names to Gmail label IDs
"""
label_map = {}
for label_name in config.LABELS:
label_id = self.gmail_client.create_label_if_not_exists(label_name)
if label_id:
label_map[label_name] = label_id
logger.info(f"Initialized {len(label_map)} Gmail labels")
return label_map
def _load_state(self) -> Dict[str, str]:
"""
Load processed email IDs with timestamps from state file.
Returns:
Dictionary mapping email IDs to ISO format timestamps
"""
if not os.path.exists(self.state_file):
logger.info(f"No state file found at {self.state_file}, starting fresh")
return {}
try:
with open(self.state_file, "r") as f:
state_data = json.load(f)
processed_emails_raw = state_data.get("processed_emails", {})
# Handle migration from old format (list) to new format (dict)
if isinstance(processed_emails_raw, list):
logger.info(
"Migrating state from old format (list) to new format (dict)"
)
# Convert list to dict with current timestamp for all entries
current_time = datetime.now(timezone.utc).isoformat()
processed_emails = {
email_id: current_time for email_id in processed_emails_raw
}
else:
processed_emails = processed_emails_raw
# Cleanup old entries
processed_emails = self._cleanup_old_state(processed_emails)
logger.info(
f"Loaded {len(processed_emails)} processed email IDs from {self.state_file}"
)
return processed_emails
except Exception as e:
logger.error(f"Error loading state file {self.state_file}: {e}")
return {}
def _cleanup_old_state(self, processed_emails: Dict[str, str]) -> Dict[str, str]:
"""
Remove entries older than retention period.
Args:
processed_emails: Dictionary of email_id -> timestamp
Returns:
Cleaned dictionary with only recent entries
"""
if self.retention_days <= 0:
# Retention disabled (keep all)
return processed_emails
cutoff_date = datetime.now(timezone.utc) - timedelta(days=self.retention_days)
original_count = len(processed_emails)
cleaned = {}
for email_id, timestamp_str in processed_emails.items():
try:
timestamp = datetime.fromisoformat(timestamp_str)
if timestamp >= cutoff_date:
cleaned[email_id] = timestamp_str
except (ValueError, TypeError) as e:
# Invalid timestamp, skip this entry
logger.warning(
f"Skipping entry with invalid timestamp: {email_id} - {e}"
)
continue
removed_count = original_count - len(cleaned)
if removed_count > 0:
logger.info(
f"Removed {removed_count} email(s) older than {self.retention_days} days from state"
)
return cleaned
def _save_state(self):
"""
Save processed email IDs with timestamps to state file.
"""
try:
# Ensure directory exists
state_dir = os.path.dirname(self.state_file)
if state_dir and not os.path.exists(state_dir):
os.makedirs(state_dir, exist_ok=True)
state_data = {"processed_emails": self.processed_emails}
with open(self.state_file, "w") as f:
json.dump(state_data, f, indent=2)
logger.debug(f"State saved to {self.state_file}")
except Exception as e:
logger.error(f"Error saving state file {self.state_file}: {e}")
def process_email(self, email: Dict) -> bool:
"""
Process a single email: classify it and apply labels.
Args:
email: Email dictionary from Gmail API
Returns:
True if successfully processed, False otherwise
"""
try:
email_id = email.get("id")
if not email_id:
logger.error("Email missing ID field")
return False
# Check if already processed
if email_id in self.processed_emails:
logger.info(
f"Skipping already processed email: {email['subject'][:50]}..."
)
return True # Return True since it was successfully handled before
logger.info(f"Processing email: {email['subject'][:50]}...")
# Classify the email
predicted_labels = self.classifier.classify_email(
email=email,
classification_prompt=config.CLASSIFICATION_PROMPT,
available_labels=config.LABELS,
)
if not predicted_labels:
logger.warning(f"No labels predicted for email: {email['subject']}")
# Still mark as processed to avoid re-attempting
self.processed_emails[email_id] = datetime.now(timezone.utc).isoformat()
self._save_state()
return False
# Get Gmail label IDs
label_ids = [
self.label_id_map[label]
for label in predicted_labels
if label in self.label_id_map
]
if label_ids:
# Apply labels to the email and optionally remove from inbox
self.gmail_client.add_labels_to_message(
email["id"], label_ids, remove_from_inbox=config.REMOVE_FROM_INBOX
)
action = (
"Applied labels and archived"
if config.REMOVE_FROM_INBOX
else "Applied labels"
)
logger.info(
f"{action} {predicted_labels} to email: {email['subject'][:50]}"
)
else:
logger.warning(
f"No valid label IDs found for predicted labels: {predicted_labels}"
)
# Mark as processed with timestamp and save state
self.processed_emails[email_id] = datetime.now(timezone.utc).isoformat()
self._save_state()
return True
except Exception as e:
logger.error(f"Error processing email {email.get('id', 'unknown')}: {e}")
return False
def run_continuous(self):
"""
Run the agent continuously, polling for new emails.
"""
logger.info(
f"Starting continuous email classifier agent (polling every {config.POLL_INTERVAL_SECONDS}s)"
)
while True:
try:
logger.info("=== Checking for new emails ===")
# Cleanup old state entries periodically
self.processed_emails = self._cleanup_old_state(self.processed_emails)
# Get unread emails
emails = self.gmail_client.get_unread_messages(
max_results=config.MAX_EMAILS_PER_POLL
)
if not emails:
logger.debug("No unread emails to process")
else:
# Process each email
processed_count = 0
for email in emails:
if self.process_email(email):
processed_count += 1
logger.info(
f"=== Processed {processed_count} out of {len(emails)} emails ==="
)
# Wait before next poll
logger.debug(f"Sleeping for {config.POLL_INTERVAL_SECONDS} seconds...")
time.sleep(config.POLL_INTERVAL_SECONDS)
except KeyboardInterrupt:
logger.info("Received interrupt signal, shutting down gracefully...")
break
except Exception as e:
logger.error(f"Error in continuous loop: {e}")
logger.info(
f"Waiting {config.POLL_INTERVAL_SECONDS} seconds before retry..."
)
time.sleep(config.POLL_INTERVAL_SECONDS)
logger.info("Email Classifier Agent stopped")