Spaces:
Runtime error

File size: 2,019 Bytes
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
"""
EscalationType β€” classifies why an escalation happened.
Used to shape the Case Intelligence output and Handoff Card presentation.
"""
from __future__ import annotations

from enum import Enum


class EscalationType(str, Enum):
    AUTHORITY  = "authority"   # AI knew the answer, lacked permission to act
    EMOTION    = "emotion"     # sentiment collapse or explicit human request
    AMBIGUITY  = "ambiguity"   # conflicting evidence; AI couldn't determine ground truth
    NOVELTY    = "novelty"     # no workflow covers this case


def classify_escalation_type(session) -> EscalationType:
    """
    Classify the escalation from session state.
    Priority: explicit_handoff > technical_failures > comprehension_failures > sentiment > default.
    """
    esc_scores = getattr(session, "escalation_scores", None)
    failure_reason = getattr(session, "escalation_reason", None) or ""

    # Explicit customer request or sentiment collapse β†’ EMOTION
    if failure_reason in ("customer_request", "sentiment"):
        return EscalationType.EMOTION
    if esc_scores and esc_scores.explicit_handoff:
        return EscalationType.EMOTION

    # Tool/technical failure β†’ AI had the answer, execution failed β†’ AUTHORITY
    if failure_reason == "tool_failure":
        return EscalationType.AUTHORITY
    if esc_scores and esc_scores.technical_failures > 0:
        return EscalationType.AUTHORITY

    # Repeated comprehension failures β†’ AMBIGUITY (AI couldn't parse the situation)
    if esc_scores and esc_scores.comprehension_failures >= 2:
        return EscalationType.AMBIGUITY

    # Sentiment scores trending bad (last 2 below 0.3) β†’ EMOTION
    scores = getattr(session, "sentiment_scores", []) or []
    if len(scores) >= 2 and scores[-1] < 0.3 and scores[-2] < 0.3:
        return EscalationType.EMOTION

    # Unknown threshold β€” not enough context β†’ NOVELTY
    if failure_reason in ("unknown_threshold", ""):
        return EscalationType.NOVELTY

    return EscalationType.NOVELTY