File size: 2,699 Bytes
6793beb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
LangGraph workflow definition for the Customer Support Email Agent.

Flow:
    classify β†’ retrieve β†’ respond β†’ check_escalation
                                           β”‚
                                β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                             escalate              send_reply
                             (β†’ END)                   β”‚
                                                check_follow_up
                                                       β”‚
                                                      END
"""

from langgraph.graph import END, StateGraph

from src.nodes.classifier import classify_email
from src.nodes.escalation import check_escalation
from src.nodes.follow_up import check_follow_up
from src.nodes.responder import generate_response
from src.nodes.retriever import retrieve_context
from src.nodes.send_reply import send_email_reply
from src.schemas.state import SupportState


def should_escalate(state: SupportState) -> str:
    """Conditional edge: route to escalation (human review) or auto-send."""
    return "escalate" if state.needs_escalation else "send_reply"


def build_graph() -> StateGraph:
    """
    Construct and compile the support agent graph.

    The graph implements the full pipeline:
    1. Classify the email intent
    2. Retrieve relevant knowledge base context
    3. Generate a draft response
    4. Check if human review is needed
       - If yes: end (awaits human approval via API)
       - If no: send the reply automatically
    5. Check if a follow-up should be scheduled
    """
    graph = StateGraph(SupportState)

    # -- Add nodes --
    graph.add_node("classify", classify_email)
    graph.add_node("retrieve", retrieve_context)
    graph.add_node("respond", generate_response)
    graph.add_node("check_escalation", check_escalation)
    graph.add_node("send_reply", send_email_reply)
    graph.add_node("check_follow_up", check_follow_up)

    # -- Define edges --
    graph.set_entry_point("classify")
    graph.add_edge("classify", "retrieve")
    graph.add_edge("retrieve", "respond")
    graph.add_edge("respond", "check_escalation")

    # Conditional: escalate β†’ END (human review), or auto-send
    graph.add_conditional_edges(
        "check_escalation",
        should_escalate,
        {
            "escalate": END,  # Stops here; human reviews via API
            "send_reply": "send_reply",
        },
    )

    # After sending, check for follow-up
    graph.add_edge("send_reply", "check_follow_up")
    graph.add_edge("check_follow_up", END)

    return graph.compile()


# Pre-built compiled graph instance
workflow = build_graph()