Spaces:
Runtime error

File size: 6,926 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
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
"""
OrchestratorState β€” top-level call lifetime state for the Wavvy orchestrator.

ExecutionMode is the primary abstraction. Every call starts in GENERAL.
The orchestrator transitions to WORKFLOW when a customer utterance matches an
active workflow's intent. ESCALATION is terminal.

FAQ is NOT a mode β€” it is a capability invoked from any mode. A FAQ answer is
injected into the current turn's context without changing the execution mode.
"""
from __future__ import annotations

from dataclasses import dataclass, field
from enum import Enum
from typing import Optional


class ExecutionMode(Enum):
    GENERAL    = "general"     # default; open-ended LLM; conversational tools only
    WORKFLOW   = "workflow"    # active node graph; per-node scoped tools; slot monitor
    ESCALATION = "escalation"  # terminal; human handoff in progress


@dataclass
class EntitySlots:
    """Accumulated structured entities extracted from STT utterances.

    Phone and OTP are accumulated across multiple turns β€” a customer may say
    digits slowly or across two STT chunks. The extractor appends digits until
    a complete value is available.
    """
    phone:  Optional[str] = None   # accumulated phone digits; set when >= 10 digits
    otp:    Optional[str] = None   # 6-digit OTP code; set when exactly 6 digits found
    txn_id: Optional[str] = None   # TXN-XXXX; set on first match

    def clear_phone(self) -> None:
        self.phone = None

    def clear_otp(self) -> None:
        self.otp = None

    def clear_txn_id(self) -> None:
        self.txn_id = None


@dataclass
class OrchestratorState:
    """Per-call orchestrator state.

    Stored on CallSession alongside the existing conv_state (ConversationStateManager).
    conv_state remains the within-workflow stage tracker for the default fintech
    workflow only. OrchestratorState owns the top-level mode and node position.
    """
    mode: ExecutionMode = ExecutionMode.GENERAL

    # Active workflow fields β€” all None unless mode == WORKFLOW
    active_workflow_id: Optional[str] = None   # workflow_definitions.id
    active_node_id:     Optional[str] = None   # WorkflowNode.id within active workflow
    node_variables:     dict          = field(default_factory=dict)   # collected slot values
    node_attempts:      int           = 0      # attempts at current node (for max_attempts check)

    # Entity accumulation β€” populated by entity_extractor on every utterance
    entity_slots: EntitySlots = field(default_factory=EntitySlots)

    # FAQ dedup β€” prevents re-injecting the same RAG chunk two turns in a row
    last_faq_chunk_id: Optional[str] = None

    # Escalation confirmation gate β€” set when escalation intent first detected.
    # escalate_to_human is only offered to the LLM AFTER the customer explicitly
    # confirms.  This prevents the agent from transferring calls without consent.
    escalation_pending: bool = False

    # Name collection gate β€” set alongside escalation_pending when the customer has
    # NOT been session-verified.  The next non-negative response is treated as the
    # customer's name and immediately triggers escalation.  Skipped when
    # session_verified is True (we already know who they are).
    escalation_needs_name: bool = False

    # Name collected during pre-escalation identity check.  Set when the customer
    # responds to "Could you share your name?" before an unverified transfer.
    # Used by build_escalation_packet() to populate lead.name in the handoff bundle.
    escalation_caller_name: str = ""

    # Reason collection gate β€” set after name is collected.  The next customer turn
    # is treated as the call reason (payment, fraud, account access, general).
    escalation_needs_reason: bool = False

    # Human-readable call reason collected during pre-escalation (e.g. "Payment Issue").
    # Used as bundle.reason in the handoff so the agent console shows it immediately.
    escalation_call_reason: str = ""

    # LLM-action node advancement: when the LLM calls a tool at an action node
    # (auto_actions=[]), the tool's fast_response_key is stored here so the next
    # llm_node() call's advance() can evaluate the edge and move to the next node.
    # Tuple: (tool_name, fast_response_key).  Cleared after edge evaluation.
    pending_tool_result: Optional[tuple[str, str]] = None

    # Session-level identity verification flag.
    # Set to True after verify_otp succeeds for the first time in this call.
    # All subsequent workflows skip collect_phone β†’ send_otp β†’ verify_otp and
    # enter at the first post-verification node directly.
    session_verified: bool = False

    # Workflows completed this call β€” used to prevent the issue classifier from
    # re-entering a workflow that already ran to completion.
    completed_workflow_ids: set = field(default_factory=set)

    # Terminal tools that returned success this call (e.g. "initiate_refund",
    # "unlock_account").  Used to suppress cross-workflow pivots when the goal
    # is already achieved, even if exit_workflow() was never called (LLM tool path).
    completed_terminal_tools: set = field(default_factory=set)

    def enter_workflow(self, workflow_id: str, entry_node_id: str) -> None:
        self.mode               = ExecutionMode.WORKFLOW
        self.active_workflow_id = workflow_id
        self.active_node_id     = entry_node_id
        self.node_variables     = {}
        self.node_attempts      = 0

    def advance_to_node(self, node_id: str) -> None:
        """Move to the next node within the same workflow."""
        self.active_node_id = node_id
        self.node_variables  = {}
        self.node_attempts   = 0

    def exit_workflow(self) -> None:
        """Workflow completed (success or terminal failure) β€” return to GENERAL.

        Clears entity slots so a subsequent issue-classifier match for the same
        workflow can't skip the identity-collection steps by reusing the old phone.
        Records the workflow ID so the classifier won't re-enter it this call.
        """
        if self.active_workflow_id:
            self.completed_workflow_ids.add(self.active_workflow_id)
        self.mode               = ExecutionMode.GENERAL
        self.active_workflow_id = None
        self.active_node_id     = None
        self.node_variables     = {}
        self.node_attempts      = 0
        # Clear all entity slots β€” prevents the next turn from auto-firing
        # verify_account / verify_otp with stale values from the finished workflow.
        self.entity_slots.clear_phone()
        self.entity_slots.clear_otp()
        self.entity_slots.clear_txn_id()

    def enter_escalation(self) -> None:
        self.mode = ExecutionMode.ESCALATION

    def is_in_workflow(self) -> bool:
        return self.mode == ExecutionMode.WORKFLOW

    def is_escalating(self) -> bool:
        return self.mode == ExecutionMode.ESCALATION