File size: 2,662 Bytes
0387a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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}