Spaces:
Configuration error
Configuration error
File size: 8,787 Bytes
aa15bce |
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 |
"""Background watcher that surfaces important Gmail emails proactively."""
from __future__ import annotations
import asyncio
from datetime import datetime, timezone, timedelta
from pathlib import Path
from typing import List, Optional, TYPE_CHECKING
from .client import execute_gmail_tool, get_active_gmail_user_id
from .processing import EmailTextCleaner, ProcessedEmail, parse_gmail_fetch_response
from .seen_store import GmailSeenStore
from .importance_classifier import classify_email_importance
from ...logging_config import logger
from ...utils.timezones import convert_to_user_timezone
if TYPE_CHECKING: # pragma: no cover - typing only
from ...agents.interaction_agent.runtime import InteractionAgentRuntime
def _resolve_interaction_runtime() -> "InteractionAgentRuntime":
from ...agents.interaction_agent.runtime import InteractionAgentRuntime
return InteractionAgentRuntime()
DEFAULT_POLL_INTERVAL_SECONDS = 60.0
DEFAULT_LOOKBACK_MINUTES = 10
DEFAULT_MAX_RESULTS = 50
DEFAULT_SEEN_LIMIT = 300
_DATA_DIR = Path(__file__).resolve().parent.parent.parent / "data"
_DEFAULT_SEEN_PATH = _DATA_DIR / "gmail_seen.json"
class ImportantEmailWatcher:
"""Poll Gmail for recent messages and surface important ones."""
def __init__(
self,
poll_interval_seconds: float = DEFAULT_POLL_INTERVAL_SECONDS,
lookback_minutes: int = DEFAULT_LOOKBACK_MINUTES,
*,
seen_store: Optional[GmailSeenStore] = None,
) -> None:
self._poll_interval = poll_interval_seconds
self._lookback_minutes = lookback_minutes
self._lock = asyncio.Lock()
self._task: Optional[asyncio.Task[None]] = None
self._running = False
self._seen_store = seen_store or GmailSeenStore(_DEFAULT_SEEN_PATH, DEFAULT_SEEN_LIMIT)
self._cleaner = EmailTextCleaner(max_url_length=60)
self._has_seeded_initial_snapshot = False
self._last_poll_timestamp: Optional[datetime] = None
# Start the background email polling task
async def start(self) -> None:
async with self._lock:
if self._task and not self._task.done():
return
loop = asyncio.get_running_loop()
self._running = True
self._has_seeded_initial_snapshot = False
self._last_poll_timestamp = None
self._task = loop.create_task(self._run(), name="important-email-watcher")
logger.info(
"Important email watcher started",
extra={"interval_seconds": self._poll_interval, "lookback_minutes": self._lookback_minutes},
)
# Stop the background email polling task gracefully
async def stop(self) -> None:
async with self._lock:
self._running = False
if self._task:
self._task.cancel()
try:
await self._task
except asyncio.CancelledError:
pass
finally:
self._task = None
logger.info("Important email watcher stopped")
async def _run(self) -> None:
try:
while self._running:
try:
await self._poll_once()
except Exception as exc: # pragma: no cover - defensive
logger.exception("Important email watcher poll failed", extra={"error": str(exc)})
await asyncio.sleep(self._poll_interval)
except asyncio.CancelledError:
raise
# Poll Gmail once for new messages and classify them for importance
def _complete_poll(self, user_now: datetime) -> None:
self._last_poll_timestamp = user_now
self._has_seeded_initial_snapshot = True
async def _poll_once(self) -> None:
poll_started_at = datetime.now(timezone.utc)
user_now = convert_to_user_timezone(poll_started_at)
first_poll = not self._has_seeded_initial_snapshot
previous_poll_timestamp = self._last_poll_timestamp
interval_cutoff = user_now - timedelta(seconds=self._poll_interval)
cutoff_time = interval_cutoff
if previous_poll_timestamp is not None and previous_poll_timestamp > interval_cutoff:
cutoff_time = previous_poll_timestamp
composio_user_id = get_active_gmail_user_id()
if not composio_user_id:
logger.debug("Gmail not connected; skipping importance poll")
return
query = f"label:INBOX newer_than:{self._lookback_minutes}m"
arguments = {
"query": query,
"include_payload": True,
"max_results": DEFAULT_MAX_RESULTS,
}
try:
raw_result = execute_gmail_tool("GMAIL_FETCH_EMAILS", composio_user_id, arguments=arguments)
except Exception as exc:
logger.warning(
"Failed to fetch Gmail messages for watcher",
extra={"error": str(exc)},
)
return
processed_emails, _ = parse_gmail_fetch_response(
raw_result,
query=query,
cleaner=self._cleaner,
)
if not processed_emails:
logger.debug("No recent Gmail messages found for watcher")
self._complete_poll(user_now)
return
if first_poll:
self._seen_store.mark_seen(email.id for email in processed_emails)
logger.info(
"Important email watcher completed initial warmup",
extra={"skipped_ids": len(processed_emails)},
)
self._complete_poll(user_now)
return
unseen_emails: List[ProcessedEmail] = [
email for email in processed_emails if not self._seen_store.is_seen(email.id)
]
if not unseen_emails:
logger.info(
"Important email watcher check complete",
extra={"emails_reviewed": 0, "surfaced": 0},
)
self._complete_poll(user_now)
return
unseen_emails.sort(key=lambda email: email.timestamp or datetime.now(timezone.utc))
eligible_emails: List[ProcessedEmail] = []
aged_emails: List[ProcessedEmail] = []
for email in unseen_emails:
email_timestamp = email.timestamp
if email_timestamp.tzinfo is not None:
email_timestamp = email_timestamp.astimezone(user_now.tzinfo)
else:
email_timestamp = email_timestamp.replace(tzinfo=user_now.tzinfo)
if email_timestamp < cutoff_time:
aged_emails.append(email)
continue
eligible_emails.append(email)
if not eligible_emails and aged_emails:
self._seen_store.mark_seen(email.id for email in aged_emails)
logger.info(
"Important email watcher check complete",
extra={
"emails_reviewed": len(unseen_emails),
"surfaced": 0,
"suppressed_for_age": len(aged_emails),
},
)
self._complete_poll(user_now)
return
summaries_sent = 0
processed_ids: List[str] = [email.id for email in aged_emails]
for email in eligible_emails:
summary = await classify_email_importance(email)
processed_ids.append(email.id)
if not summary:
continue
summaries_sent += 1
await self._dispatch_summary(summary)
if processed_ids:
self._seen_store.mark_seen(processed_ids)
logger.info(
"Important email watcher check complete",
extra={
"emails_reviewed": len(unseen_emails),
"surfaced": summaries_sent,
"suppressed_for_age": len(aged_emails),
},
)
self._complete_poll(user_now)
async def _dispatch_summary(self, summary: str) -> None:
runtime = _resolve_interaction_runtime()
try:
contextualized = f"Important email watcher notification:\n{summary}"
await runtime.handle_agent_message(contextualized)
except Exception as exc: # pragma: no cover - defensive
logger.error(
"Failed to dispatch important email summary",
extra={"error": str(exc)},
)
_watcher_instance: Optional[ImportantEmailWatcher] = None
def get_important_email_watcher() -> ImportantEmailWatcher:
global _watcher_instance
if _watcher_instance is None:
_watcher_instance = ImportantEmailWatcher()
return _watcher_instance
__all__ = ["ImportantEmailWatcher", "get_important_email_watcher"]
|