Spaces:
Runtime error

File size: 15,780 Bytes
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e978f7
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
"""
Issue Classifier β€” maps customer symptom utterances to specific issue types.

Runs in the LiveKit worker (pure keyword matching, no ML model, no API call).
Called by the orchestrator when the embedding-based workflow trigger returns
no match β€” customers describe symptoms ("My account is on KYC hold"), not
workflow names ("Fintech Support"), so embedding similarity often misses.

The result selects the correct workflow deterministically rather than leaving
the LLM in GENERAL mode where it improvises and offers escalation.
"""
from __future__ import annotations

import logging
import re
from dataclasses import dataclass, field
from enum import Enum

logger = logging.getLogger(__name__)


class IssueType(str, Enum):
    KYC_HOLD           = "kyc_hold"
    FRAUD              = "fraud"
    DISPUTE            = "dispute"
    REFUND             = "refund"
    ACCOUNT_LOCKED     = "account_locked"
    TRANSACTION_STATUS = "transaction_status"
    GENERAL_SUPPORT    = "general_support"


@dataclass
class IssueClassification:
    issue_type:       IssueType
    confidence:       float
    matched_keywords: list[str] = field(default_factory=list)


# Each row: (issue_type, list_of_keyword_phrases, confidence_score)
# Longer / more specific phrases are listed before shorter ones so the first
# match wins the confidence bucket for that issue type.
_PATTERNS: list[tuple[IssueType, list[str], float]] = [
    # ── KYC ──────────────────────────────────────────────────────────────────
    (IssueType.KYC_HOLD, [
        "kyc hold", "kyc verification", "kyc rejected", "kyc pending",
        "kyc failed", "kyc status", "kyc issue", "kyc problem",
        "know your customer", "on hold due to kyc", "account on hold",
        "transaction on hold", "compliance hold",
    ], 0.95),
    (IssueType.KYC_HOLD, ["kyc", "verification hold"], 0.80),

    # ── Fraud Hold Removal β€” MUST come before generic FRAUD patterns (higher priority) ──
    # Customer already has a fraud hold and wants it lifted β€” route to Account Unlock
    (IssueType.ACCOUNT_LOCKED, [
        "fraud hold", "fraud investigation hold", "fraud hold on my account",
        "account is hold for fraud", "account is on hold for fraud",
        "my account is hold for a fraud", "account held for fraud",
        "held for fraud investigation", "account under fraud hold",
        "remove fraud hold", "lift fraud hold", "clear fraud hold",
        "held due to fraud", "held because of fraud",
        "fraud is on hold", "account has a fraud hold",
    ], 0.95),

    # ── Fraud ─────────────────────────────────────────────────────────────────
    (IssueType.FRAUD, [
        "unauthorized transaction", "someone used my account", "i didn't make",
        "i did not make", "not authorized", "fraudulent", "account hacked",
        "account compromised", "someone else used", "report fraud",
        "didn't authorize", "did not authorize",
    ], 0.95),
    (IssueType.FRAUD, ["fraud", "unauthorized", "stolen card", "hacked", "suspicious"], 0.80),

    # ── Dispute ───────────────────────────────────────────────────────────────
    (IssueType.DISPUTE, [
        "raise a dispute", "file a dispute", "wrong amount charged",
        "charged twice", "duplicate charge", "merchant didn't deliver",
        "overcharged", "billing error", "wrong charge",
    ], 0.95),
    (IssueType.DISPUTE, ["dispute", "overcharged", "incorrect amount"], 0.75),

    # ── Refund ────────────────────────────────────────────────────────────────
    (IssueType.REFUND, [
        "refund request", "want a refund", "need a refund", "money back",
        "money not returned", "money was deducted", "failed transaction refund",
        "get my money back", "send my money back", "give me a refund",
        "process the refund", "can you refund", "i want my money",
        "process a refund", "initiate a refund", "raise a refund",
        "transaction failed refund", "payment failed refund",
        "amount was deducted", "amount got deducted", "money got deducted",
        "deducted from my account", "deducted but", "got debited",
        "debited but not", "debited and failed",
        # Colloquial: "money got deducted and it got failed"
        "deducted and got failed", "deducted and it failed",
        "got failed and money", "failed and money deducted",
    ], 0.95),
    (IssueType.REFUND, ["refund", "reimburs"], 0.75),

    # ── Account locked ────────────────────────────────────────────────────────
    (IssueType.ACCOUNT_LOCKED, [
        "can't log in", "cannot login", "account locked", "account blocked",
        "account suspended", "account disabled", "locked out of",
        "access denied", "restore access", "unable to access",
    ], 0.95),
    (IssueType.ACCOUNT_LOCKED, ["locked", "blocked", "suspended"], 0.70),

    # ── Transaction status ────────────────────────────────────────────────────
    (IssueType.TRANSACTION_STATUS, [
        "transaction status", "payment status", "did my payment go through",
        "payment not received", "transfer pending", "transaction pending",
        "where is my money", "money not received", "payment stuck",
        "transfer stuck", "transaction stuck",
    ], 0.90),
    (IssueType.TRANSACTION_STATUS, [
        "transaction failed", "payment failed", "transfer failed",
        # Colloquial Indian English: "it got failed", "transaction got failed"
        "got failed", "it failed", "transaction got failed",
        "payment got failed", "transfer got failed",
    ], 0.80),
    (IssueType.TRANSACTION_STATUS, ["transaction", "payment"], 0.65),

    # ── General (lowest priority catch-all) ───────────────────────────────────
    (IssueType.GENERAL_SUPPORT, [
        "help with my account", "account issue", "account problem",
        "having trouble", "need support", "something wrong with my account",
    ], 0.70),
    (IssueType.GENERAL_SUPPORT, ["account", "issue", "problem"], 0.50),
]

# Precompile all keyword phrases as \bphrase\b regex patterns.
# Word boundaries prevent "unlocked" from matching "locked",
# "unblocked" from matching "blocked", etc.
_COMPILED_PATTERNS: list[tuple[IssueType, list[tuple[str, re.Pattern]], float]] = [
    (
        issue_type,
        [(kw, re.compile(r"\b" + re.escape(kw) + r"\b")) for kw in keywords],
        confidence,
    )
    for issue_type, keywords, confidence in _PATTERNS
]

# Workflow UUIDs from seed.py β€” deterministic mapping from issue to entry point
_ISSUE_TO_WORKFLOW_ID: dict[IssueType, str] = {
    IssueType.KYC_HOLD:             "00000000-0000-0000-0000-000000000001",  # Fintech Support
    IssueType.FRAUD:                "00000000-0000-0000-0000-000000000005",  # Fraud Report
    IssueType.DISPUTE:              "00000000-0000-0000-0000-000000000004",  # Dispute Filing
    IssueType.REFUND:               "00000000-0000-0000-0000-000000000003",  # Refund Request
    IssueType.ACCOUNT_LOCKED:       "00000000-0000-0000-0000-000000000006",  # Account Unlock
    IssueType.TRANSACTION_STATUS:   "00000000-0000-0000-0000-000000000002",  # Transaction Status
    IssueType.GENERAL_SUPPORT:      "00000000-0000-0000-0000-000000000001",  # Fintech Support
}

# Minimum confidence required to act on a classification
CONFIDENCE_THRESHOLD = 0.60

# Completion-context signals per issue type.
# If ANY signal is found in the utterance, the keyword match is suppressed β€”
# the customer is describing a RESOLVED state, not making a new request.
# This prevents "refund has been initiated" from triggering a Refund workflow,
# and "account is now unlocked" from triggering an Account Unlock workflow.
_COMPLETION_SIGNALS: dict[IssueType, list[str]] = {
    IssueType.REFUND: [
        "refund has been",
        "refund was",
        "refund is done",
        "refund is complete",
        "refund processed",
        "refund initiated",
        "refund reference",
        "already initiated",
        "already got my refund",
        "already refunded",
        "refund in progress",
        "refund is in progress",
        "got the refund",
        "received the refund",
    ],
    IssueType.ACCOUNT_LOCKED: [
        "account is now unlocked",
        "account has been unlocked",
        "account was unlocked",
        "now have access",
        "able to log in",
        "able to login",
        "can log in now",
        "can login now",
        "account unblocked",
    ],
    IssueType.DISPUTE: [
        "dispute has been",
        "dispute was filed",
        "dispute filed",
        "dispute raised",
        "dispute reference",
        "already raised a dispute",
        "already filed",
    ],
    IssueType.FRAUD: [
        "fraud case",
        "fraud has been reported",
        "fraud was reported",
        "fraud report filed",
        "already reported",
        "case has been opened",
        "fraud reference",
    ],
}

# Precompile completion signals as substring checks (no word boundary needed β€”
# these are full phrases, not single keywords).
_COMPILED_COMPLETION_SIGNALS: dict[IssueType, list[re.Pattern]] = {
    issue_type: [re.compile(re.escape(signal)) for signal in signals]
    for issue_type, signals in _COMPLETION_SIGNALS.items()
}


# Universal closure phrases β€” positive-sentiment wrap-up language that signals
# the conversation is ending, not starting a new request.  Only applied when
# the best keyword match scores ≀ 0.75 (weak signal).  A 0.95-confidence
# specific request phrase ("I want a refund for a different transaction") still
# routes correctly even if the customer also says "thank you".
_UNIVERSAL_CLOSURE_PHRASES: frozenset[str] = frozenset([
    "thank you", "thanks", "thank you so much", "many thanks",
    "that's all", "thats all", "that's everything", "nothing else",
    "all set", "all good", "i'm good", "im good", "we're done", "were done",
    "got it", "got the message", "got the reference",
    "perfect", "great", "wonderful", "excellent", "awesome",
    "resolved", "fixed", "sorted", "done", "it's working", "its working",
    "working now", "it works", "it worked",
    "have a good day", "have a great day", "goodbye", "bye", "bye bye",
    "take care", "see you",
])

_COMPILED_CLOSURE_PHRASES: list[re.Pattern] = [
    re.compile(r"\b" + re.escape(phrase) + r"\b")
    for phrase in _UNIVERSAL_CLOSURE_PHRASES
]

# Negation words β€” used to suppress single-word keyword matches that appear in
# a negated context ("no fraud", "I don't see any fraud", "there is no fraud").
# Only applied to single-word keywords (no spaces) β€” multi-word intent phrases
# like "I didn't make this transaction" are inherently intentional and unambiguous.
_NEGATION_WORDS: frozenset[str] = frozenset([
    "no", "not", "never", "none", "without",
    "don't", "dont", "doesn't", "doesnt", "didn't", "didnt",
    "isn't", "isnt", "wasn't", "wasnt", "weren't", "werent",
    "haven't", "havent", "hasn't", "hasnt", "hadn't", "hadnt",
    "won't", "wont", "wouldn't", "wouldnt", "can't", "cant", "couldn't", "couldnt",
])

_STRIP_PUNCT = re.compile(r"[^\w']+")


def _is_single_keyword_negated(lower: str, keyword: str) -> bool:
    """Return True if `keyword` (no spaces) appears within 8 words of a negation."""
    if ' ' in keyword:
        return False  # phrase keywords are intentional β€” skip negation check
    kw_re = re.compile(r'\b' + re.escape(keyword) + r'\b')
    for m in kw_re.finditer(lower):
        prefix = lower[max(0, m.start() - 60):m.start()]
        preceding = [_STRIP_PUNCT.sub('', w) for w in prefix.split()[-8:]]
        if any(w in _NEGATION_WORDS for w in preceding):
            return True
    return False


def _is_completion_context(lower: str, issue_type: IssueType) -> bool:
    """Return True if the utterance describes a resolved outcome, not a new request."""
    for pattern in _COMPILED_COMPLETION_SIGNALS.get(issue_type, []):
        if pattern.search(lower):
            return True
    return False


def _is_closure_utterance(lower: str) -> bool:
    """Return True if the utterance is primarily wrap-up / gratitude language."""
    return any(p.search(lower) for p in _COMPILED_CLOSURE_PHRASES)


def classify_issue(text: str) -> IssueClassification | None:
    """Classify a customer utterance into a known issue type.

    Scans _PATTERNS in order β€” higher-confidence rows first per issue type.
    Returns the highest-confidence match, or None if nothing clears
    CONFIDENCE_THRESHOLD.

    Never raises.
    """
    lower = text.lower()
    best: tuple[IssueType, float, list[str]] | None = None

    for issue_type, kw_patterns, confidence in _COMPILED_PATTERNS:
        matched = [kw for kw, pattern in kw_patterns if pattern.search(lower)]
        if not matched:
            continue
        # Remove single-word keywords that appear in a negated context.
        # "I don't see any fraud" / "there is no fraud" β†’ suppress "fraud" match.
        # Multi-word phrases (e.g. "i didn't make this") are kept β€” they encode intent.
        active = [kw for kw in matched if not _is_single_keyword_negated(lower, kw)]
        if not active:
            logger.debug(
                "issue_classifier: suppressed %s β€” all keywords negated in: %r",
                issue_type.value, text[:80],
            )
            continue
        matched = active
        if best is None or confidence > best[1]:
            best = (issue_type, confidence, matched)

    if best is None or best[1] < CONFIDENCE_THRESHOLD:
        logger.debug("issue_classifier: no match above threshold for: %r", text[:80])
        return None

    issue_type, confidence, matched_keywords = best

    # Suppress if the utterance is describing a completed/resolved state, not a new request.
    if _is_completion_context(lower, issue_type):
        logger.debug(
            "issue_classifier: suppressed %s β€” completion context detected: %r",
            issue_type.value, text[:80],
        )
        return None

    # Suppress weak matches (≀ 0.75) when the utterance is primarily closure language.
    # Strong matches (0.95) survive even with gratitude β€” e.g. "thank you but I also
    # want a refund for a different transaction" still routes correctly.
    if confidence <= 0.75 and _is_closure_utterance(lower):
        logger.debug(
            "issue_classifier: suppressed %s (confidence=%.2f) β€” closure language: %r",
            issue_type.value, confidence, text[:80],
        )
        return None

    logger.info(
        "issue_classifier: type=%s confidence=%.2f keywords=%s utterance=%r",
        issue_type.value, confidence, matched_keywords, text[:80],
    )
    return IssueClassification(
        issue_type=issue_type,
        confidence=confidence,
        matched_keywords=matched_keywords,
    )


def get_workflow_id_for_issue(issue_type: IssueType) -> str | None:
    """Return the workflow UUID for a classified issue type."""
    return _ISSUE_TO_WORKFLOW_ID.get(issue_type)