File size: 2,685 Bytes
80ef840
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from app.agents.intent_agent import IntentAgent
from app.agents.order_agent import OrderAgent
from app.agents.policy_agent import PolicyAgent
from app.agents.reasoning_agent import ReasoningAgent
from app.agents.supervisor_agent import SupervisorAgent
from app.agents.validation_agent import ValidationAgent
from app.services.escalation_service import EscalationService
from app.services.ticket_service import TicketService


class CustomerSupportWorkflow:
    def __init__(self):
        self.supervisor = SupervisorAgent()
        self.intent_agent = IntentAgent()
        self.order_agent = OrderAgent()
        self.policy_agent = PolicyAgent()
        self.reasoning_agent = ReasoningAgent()
        self.validation_agent = ValidationAgent()
        self.escalation_service = EscalationService()
        self.ticket_service = TicketService()

    def run(self, customer_id: str, message: str, order_id: str | None = None) -> dict:
        supervisor = self.supervisor.plan(message, order_id)
        intent = self.intent_agent.classify(message)
        order = self.order_agent.fetch(message, order_id) if supervisor["run_order_agent"] else None
        policies = self.policy_agent.retrieve(message, intent["intent"]) if supervisor["run_policy_agent"] else []
        reasoning = self.reasoning_agent.respond(message, intent, order, policies)
        validation = self.validation_agent.validate(reasoning["response"], policies)
        escalated, escalation_reason = self.escalation_service.should_escalate(
            reasoning["confidence"], validation, order
        )
        ticket_id = self.ticket_service.save(
            {
                "customer_id": customer_id,
                "message": message,
                "order_id": order_id,
                "response": reasoning["response"],
                "escalated": escalated,
                "trace": {
                    "supervisor": supervisor,
                    "intent": intent,
                    "order": order,
                    "policies": policies,
                    "reasoning": reasoning,
                    "validation": validation,
                },
            }
        )
        return {
            "ticket_id": ticket_id,
            "response": reasoning["response"],
            "confidence": reasoning["confidence"],
            "escalated": escalated,
            "escalation_reason": escalation_reason,
            "trace": {
                "supervisor": supervisor,
                "intent": intent,
                "order": order,
                "policies": policies,
                "reasoning": reasoning,
                "validation": validation,
            },
        }