from __future__ import annotations from typing import Any, Dict, List, Optional from app.memory import ContextMessage class DeterministicAIEngine: """ Deterministic, offline-safe engine used for: - OpenEnv evaluation tasks (no network, reproducible) - local dev without credentials It implements the subset of the AIEngine interface used by the app. """ def __init__(self, *, model: str = "deterministic-v1") -> None: self.model = model def classify_intent(self, *, subject: str, from_email: str, body: str) -> Dict[str, Any]: text = f"{subject}\n{from_email}\n{body}".lower() spam_terms = ["unsubscribe", "win money", "lottery", "crypto", "airdrop", "free gift", "click here"] sales_terms = ["pricing", "quote", "demo", "trial", "purchase", "buy", "subscription", "invoice", "upgrade"] support_terms = ["can't", "cannot", "error", "issue", "bug", "reset", "login", "password", "help", "support"] if any(t in text for t in spam_terms): return {"intent": "Spam", "confidence": 0.95, "reasoning": "Contains common spam markers."} if any(t in text for t in support_terms): return {"intent": "Support", "confidence": 0.85, "reasoning": "Looks like a user issue/request for help."} if any(t in text for t in sales_terms): return {"intent": "Sales", "confidence": 0.8, "reasoning": "Mentions commercial intent / pricing."} return {"intent": "General", "confidence": 0.65, "reasoning": "No strong intent markers detected."} def generate_reply( self, *, subject: str, from_email: str, body: str, tone: str, context: List[ContextMessage], ) -> Dict[str, str]: greeting = "Hi" if tone in {"casual", "neutral"} else "Hello" closing = "Thanks" if tone in {"casual", "neutral"} else "Sincerely" context_hint = "" if context: # Keep deterministic and short. context_hint = " (noted prior context)" reply_subject = subject if subject.lower().startswith("re:") else f"Re: {subject}" reply_body = ( f"{greeting},\n\n" f"Thanks for reaching out{context_hint}. I read your message and will help you resolve this.\n\n" f"Next steps:\n" f"1) Confirm the account/email you used to sign in.\n" f"2) If this is a login issue, try a password reset and let me know the exact error message.\n\n" f"{closing},\n" f"AI Email Assistant" ) return {"reply_subject": reply_subject, "reply_body": reply_body}