Spaces:
Runtime error

File size: 42,732 Bytes
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
64fb1e7
 
93117c2
64fb1e7
93117c2
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
64fb1e7
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64fb1e7
 
6e978f7
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
"""
@function_tool implementations β€” config-driven, one factory per call.

make_agent_tools() reads the active tenant config and returns only the tools
that are enabled. Concurrent calls are isolated via closures.
"""
from __future__ import annotations

import asyncio
import logging
import re

import httpx

from config import settings
from config_loader import get_config
from livekit.agents import function_tool
from voice.tool_recovery import execute_with_recovery

logger = logging.getLogger(__name__)

_PLACEHOLDER_FRAGMENTS = {
    "your name", "your email", "your preferred time", "preferred time",
    "your time", "name here", "email here", "example.com", "test@",
    "placeholder", "unknown", "n/a", "none",
}


def _is_placeholder(value: str) -> bool:
    v = value.lower().strip()
    if not v:
        return True
    for fragment in _PLACEHOLDER_FRAGMENTS:
        if fragment in v:
            return True
    return False


def _is_valid_email(value: str) -> bool:
    at = value.find("@")
    return at > 0 and "." in value[at:]


def _normalize_email(raw: str) -> str:
    v = raw.strip()
    v = re.sub(r'\bat\s+the\s+rate\b', '@', v, flags=re.I)
    v = re.sub(r'\battherate\b', '@', v, flags=re.I)
    v = re.sub(r'@(?:at)?therate', '@', v, flags=re.I)
    v = re.sub(r'(\w)attherate(\w)', r'\1@\2', v, flags=re.I)
    v = re.sub(r'(\w)\s+at\s+(\w)', r'\1@\2', v, flags=re.I)
    v = re.sub(r'(\w)\s+dot\s+(\w)', r'\1.\2', v, flags=re.I)
    v = re.sub(r'\s*@\s*', '@', v)
    v = re.sub(r'\s*\.\s*', '.', v)
    return v.lower().strip()



_PHONE_WORD_MAP = {
    'zero': '0', 'oh': '0',
    'one': '1', 'two': '2', 'three': '3',
    'four': '4', 'five': '5', 'six': '6',
    'seven': '7', 'eight': '8', 'nine': '9',
}


def _normalize_otp(raw: str) -> str:
    """Convert spoken OTP to a clean digit string.

    Handles: word digits ("three seven"), multipliers ("double seven" β†’ "77"),
    hyphens, spaces. Returns digits only β€” caller validates length.
    """
    v = raw.lower().strip()
    # Expand multipliers before word→digit pass
    v = re.sub(r'\bdouble\s+(\w+)', lambda m: m.group(1) + ' ' + m.group(1), v)
    v = re.sub(r'\btriple\s+(\w+)', lambda m: ' '.join([m.group(1)] * 3), v)
    # Word-to-digit (reuse phone map β€” same vocabulary)
    for word, digit in _PHONE_WORD_MAP.items():
        v = re.sub(r'\b' + word + r'\b', digit, v)
    return re.sub(r'[^\d]', '', v)


def _normalize_txn_id(raw: str) -> str:
    """Normalize spoken/typed transaction ID to TXN-XXXX format.

    Handles: "1100", "TXN1100", "txn-1100", "TXN 1100", "T X N 1100", etc.
    Strips everything except the trailing digits and prefixes with "TXN-".
    """
    v = raw.strip().upper()
    # Remove any existing TXN prefix (with or without separator)
    v = re.sub(r'^T[\s\-_]?X[\s\-_]?N[\s\-_]?', '', v)
    # Keep only the digits
    digits = re.sub(r'[^\d]', '', v)
    if digits:
        return f"TXN-{digits}"
    return raw.upper()


def _normalize_phone(raw: str) -> str:
    """Convert spoken/written phone number to digit string with optional + prefix.

    Handles: word digits ("one two three"), multipliers ("double five", "triple six"),
    spoken country codes ("plus one"), and standard formatted strings.
    """
    v = raw.lower().strip()

    # Spoken "plus" prefix β†’ "+"
    v = re.sub(r'^plus\s*', '+', v)

    # Expand multipliers before word→digit conversion
    v = re.sub(r'\bdouble\s+(\w+)', lambda m: m.group(1) + ' ' + m.group(1), v)
    v = re.sub(r'\btriple\s+(\w+)', lambda m: ' '.join([m.group(1)] * 3), v)

    # Word-to-digit conversion
    for word, digit in _PHONE_WORD_MAP.items():
        v = re.sub(r'\b' + word + r'\b', digit, v)

    # Preserve leading +, strip everything else non-digit
    has_plus = v.startswith('+')
    digits = re.sub(r'[^\d]', '', v)

    if has_plus:
        return '+' + digits
    if len(digits) >= 10:
        return '+' + digits
    return digits


def make_agent_tools(call_id: str, session, publish_event) -> list:
    """Return function_tool list for this call based on the active tenant config."""
    try:
        cfg = get_config()
        enabled: set[str] = {
            name for name, tcfg in cfg.tool_configs.items()
            if tcfg.get("enabled", True)
        }
    except Exception:
        # Fallback: no tools except escalation
        enabled = {"escalate_to_human"}

    tools: list = []

    # One WorkflowRunner per call β€” all tool closures below share it.
    # Records every (step, result_key) pair in session.workflow.steps_taken
    # so the full resolution path is auditable after the call.
    from workflow.engine import WorkflowRunner
    runner = WorkflowRunner(session)

    # ── verify_account ────────────────────────────────────────────────────────
    if "verify_account" in enabled:
        @function_tool
        async def verify_account(phone: str) -> str:
            """Verify the caller's identity by phone number.
            phone: pass exactly what the customer said β€” digits only, no formatting.
            Example: customer says '9 8 7 6 5 4 3 2 1 0', pass '9876543210'.
            Include country code if spoken (e.g. '+91' prefix is fine)."""
            if getattr(session, 'verify_account_attempts', 0) >= 3:
                return runner.advance("verify_account", "not_found_max").message
            from tools.wavvy_tools import verify_account as _verify
            normalized = _normalize_phone(phone)
            # Guard against partial utterances: require at least 9 digits.
            # The suffix-match in the DB handles 10-digit numbers without country code.
            # The LLM sometimes fires on streaming partials β€” block those short calls.
            digits_only = re.sub(r'[^\d]', '', normalized)
            if len(digits_only) < 9:
                return (
                    "I didn't catch the full number. Please say your 10-digit mobile number "
                    "one digit at a time β€” for example: nine, eight, one, two, three…"
                )
            result = await _verify(normalized or phone, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "verify_account",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                name = result.get("first_name", "there")
                acct = result.get("account_type", "standard")
                # Build digit-by-digit speech for number readback (e.g. "9 8 1 2 3 4 5 6 0 1")
                digit_speech = " ".join(digits_only) if digits_only else ""
                profile = getattr(session, "customer_profile", {}) or {}

                # Build a grounded context summary from the eagerly-loaded data
                hints: list[str] = []
                txn_count = len(profile.get("transactions") or [])
                if txn_count:
                    hints.append(
                        f"{txn_count} transaction{'s' if txn_count != 1 else ''} on file"
                    )
                else:
                    hints.append("no transactions on file")

                holds = profile.get("account_holds") or []
                if holds:
                    hold_types = ", ".join(h.get("hold_type", "?") for h in holds)
                    hints.append(
                        f"{len(holds)} active hold{'s' if len(holds) != 1 else ''} "
                        f"({hold_types}) β€” use get_account_holds for details"
                    )

                refunds = profile.get("open_refunds") or []
                if refunds:
                    hints.append(
                        f"{len(refunds)} in-progress refund{'s' if len(refunds) != 1 else ''} "
                        f"β€” use get_refund_status"
                    )

                disputes = profile.get("open_disputes") or []
                if disputes:
                    hints.append(
                        f"{len(disputes)} open dispute{'s' if len(disputes) != 1 else ''} "
                        f"β€” use get_dispute_status"
                    )

                fraud_cases = profile.get("active_fraud_cases") or []
                if fraud_cases:
                    case_refs = ", ".join(fc["fraud_number"] for fc in fraud_cases)
                    hints.append(
                        f"{len(fraud_cases)} fraud case{'s' if len(fraud_cases) != 1 else ''} under review: "
                        f"{case_refs} β€” read this reference number to the customer immediately, do NOT call any tool"
                    )

                account_status = profile.get("account_status", "active")
                kyc_status = profile.get("kyc_status", "pending")
                status_note = ""
                if account_status != "active":
                    status_note = f" Account status: {account_status}."
                if kyc_status != "verified":
                    status_note += f" KYC status: {kyc_status}."

                context_line = " | ".join(hints)

                from session.conversation_state import ConversationStage
                from session.call_session import get_session as _get_session
                _s = _get_session(call_id)
                if _s:
                    if _s.conv_state.stage == ConversationStage.GREETING:
                        _s.conv_state.transition_to(ConversationStage.DISCOVERY)
                    ok = _s.conv_state.transition_to(ConversationStage.VERIFICATION)
                    logger.info(
                        "[%s] verify_account: VERIFICATION transition=%s stage=%s",
                        call_id, ok, _s.conv_state.stage.value,
                    )
                number_readback = (
                    f"Registered number: {digit_speech}. "
                    f"Read this number back to the customer one digit at a time to confirm. "
                    if digit_speech else ""
                )
                return (
                    f"Verified: {name} ({acct}).{status_note} "
                    f"{number_readback}"
                    f"{context_line}. "
                    f"After reading the number, immediately call send_otp β€” do not wait for the customer to ask."
                )
            effective_key = "not_found_max" if result.get("attempts", 0) >= 2 else "not_found_1"
            return runner.advance("verify_account", effective_key).message

        tools.append(verify_account)

    # ── lookup_transaction ────────────────────────────────────────────────────
    if "lookup_transaction" in enabled:
        @function_tool
        async def lookup_transaction(transaction_id: str) -> str:
            """Look up a transaction by ID (format: TXN-XXXX). Requires verify_account first."""
            from tools.wavvy_tools import lookup_transaction as _lookup
            normalized_txn = _normalize_txn_id(transaction_id)
            result = await _lookup(normalized_txn, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "lookup_transaction",
                "status": "done", "result": {"found": result.get("found")},
            }))
            if result.get("fast_response_key") == "verification_required":
                return "I need to verify the customer's identity first. Please call verify_account."
            if not result.get("found"):
                return runner.advance("lookup_transaction", "not_found").message
            # Route through runner based on transaction status
            status = result.get("status", "")
            status_key = f"status_{status}" if status else None
            if status_key and status_key in (
                "status_pending", "status_processing",
                "status_failed", "status_completed", "status_cancelled", "status_expired",
                "status_refund_initiated", "status_refund_processing", "status_refund_completed",
                "status_flagged", "status_kyc_hold", "status_compliance_hold",
                "status_disputed", "status_chargeback_initiated",
                "status_chargeback_won", "status_chargeback_lost",
                "status_fraud_reported", "status_fraud_confirmed", "status_fraud_reversed",
            ):
                return runner.advance("lookup_transaction", status_key).message
            # Unrecognised status β€” surface raw info
            return (
                f"Transaction {normalized_txn}: "
                f"{result.get('merchant', 'unknown merchant')}, "
                f"amount {result.get('amount')}, status is {status or 'unknown'}."
            )

        tools.append(lookup_transaction)

    # ── search_transactions ───────────────────────────────────────────────────
    # Enabled whenever lookup_transaction is β€” same auth tier, same data source.
    if "lookup_transaction" in enabled:
        @function_tool
        async def search_transactions(query: str) -> str:
            """Search the customer's transactions from session cache by merchant, amount, status, or date.
            Use when customer describes a transaction without knowing the exact ID.
            query: free-text keyword, e.g. 'Swiggy', 'failed', '450', '2026-05-26'."""
            if not session.customer_id:
                return "Account not verified yet. Call verify_account first."
            profile = getattr(session, "customer_profile", None) or {}
            txns = profile.get("transactions") or []
            if not txns:
                return "No transactions found on this account."

            tokens = [tok for tok in query.lower().strip().split() if len(tok) >= 2]
            if not tokens:
                tokens = [query.lower().strip()]

            def _amounts_match(query_tok: str, txn_amount) -> bool:
                """STT often formats '450 rupees' as '4.50' β€” check rounded variants."""
                try:
                    q = float(query_tok.replace(',', ''))
                    a = float(str(txn_amount))
                    # Direct match OR Γ—100 match (4.50 ↔ 450)
                    return abs(q - a) < 0.5 or abs(q * 100 - a) < 0.5 or abs(q - a * 100) < 0.5
                except (ValueError, TypeError):
                    return False

            def _txn_matches(t: dict) -> bool:
                haystack = " ".join([
                    str(t.get("merchant", "")),
                    str(t.get("amount", "")),
                    str(t.get("status", "")),
                    str(t.get("txn_date", "")),
                    str(t.get("txn_number", "")),
                    str(t.get("txn_type", "")),
                ]).lower()
                if any(tok in haystack for tok in tokens):
                    return True
                # Amount-specific fuzzy match for STT decimal misparse (β‚Ή450 β†’ 4.50)
                return any(_amounts_match(tok, t.get("amount")) for tok in tokens)

            matches = [t for t in txns if _txn_matches(t)]
            if not matches:
                # Semantically related: if query implies failure/refund/dispute,
                # surface transactions with adjacent statuses so the LLM can map
                # the customer's description to the right transaction.
                FAILURE_ADJACENT = {"refund_initiated", "refund_processing", "disputed",
                                     "flagged", "fraud_reported", "fraud_confirmed",
                                     "kyc_hold", "compliance_hold"}
                FAILURE_QUERY_TERMS = {"fail", "failed", "failure", "refund", "dispute",
                                       "fraud", "stuck", "pending", "not", "issue"}
                is_failure_query = any(t in FAILURE_QUERY_TERMS for t in tokens)
                related = [t for t in txns if t.get("status", "") in FAILURE_ADJACENT] if is_failure_query else []
                if related:
                    lines = [
                        f"{t.get('txn_number')}: {t.get('merchant','?')}, "
                        f"β‚Ή{t.get('amount','?')}, status={t.get('status','?')}"
                        for t in related
                    ]
                    return f"No exact match. Related: " + " | ".join(lines)
                all_ids = ", ".join(str(t.get("txn_number")) for t in txns)
                return f"No match for '{query}'. IDs on file: {all_ids}"
            lines = [
                f"{t.get('txn_number')}: {t.get('merchant','?')}, β‚Ή{t.get('amount','?')}, "
                f"status={t.get('status','?')}, date={t.get('txn_date','?')}"
                for t in matches
            ]
            return f"Found: " + " | ".join(lines)

        tools.append(search_transactions)

    # ── send_otp ──────────────────────────────────────────────────────────────
    if "send_otp" in enabled:
        @function_tool
        async def send_otp() -> str:
            """Send a one-time password to the customer's registered contact for stronger verification. Call after verify_account when an account action (unlock, refund) is needed."""
            if session.escalated:
                return (
                    "A transfer to a specialist is already in progress. "
                    "Tell the customer: 'I've already connected you with our specialist β€” "
                    "they'll handle this for you directly. No need to re-verify.'"
                )
            from tools.wavvy_tools import send_otp as _send
            result = await _send(call_id)
            key = result.get("fast_response_key")
            if key in ("otp_cooldown", "otp_resend_limit"):
                return runner.advance("send_otp", key).message
            if result.get("success"):
                otp_val = result["otp"]
                await publish_event({"type": "otp_sent", "otp": otp_val})
                await publish_event({
                    "type": "tool_call", "tool": "send_otp",
                    "status": "done", "result": {"otp_sent": True},
                })
                # Forward to agent console so it shows in the HITL activity timeline
                asyncio.create_task(_notify_fastapi(call_id, "otp_sent", {"otp": otp_val}))
                logger.info("[%s] otp_sent event published", call_id)
                return "OTP sent to your registered phone and email. Please share the 6-digit code."
            return "Unable to send OTP right now. Please try again in a moment."

        tools.append(send_otp)

    # ── verify_otp ────────────────────────────────────────────────────────────
    if "verify_otp" in enabled:
        @function_tool
        async def verify_otp(otp_code: str) -> str:
            """Verify the 6-digit OTP the customer provides. otp_code: the digits the customer spoke or typed."""
            from tools.wavvy_tools import verify_otp as _verify

            # Normalize spoken patterns ("double seven" β†’ "77", "three" β†’ "3", etc.)
            normalized = _normalize_otp(otp_code)
            if len(normalized) != 6:
                return (
                    f"I need exactly 6 digits. I heard '{otp_code}' but got {len(normalized)} digit{'s' if len(normalized) != 1 else ''}. "
                    "Ask the customer to say each digit separately β€” for example: three, seven, seven, three, six, zero."
                )

            result = await _verify(normalized, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "verify_otp",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                from session.conversation_state import ConversationStage
                from session.call_session import get_session as _get_session
                _s = _get_session(call_id)
                if _s:
                    ok = _s.conv_state.transition_to(ConversationStage.TOOL_EXECUTION)
                    logger.info(
                        "[%s] verify_otp: TOOL_EXECUTION transition=%s stage=%s",
                        call_id, ok, _s.conv_state.stage.value,
                    )
                return (
                    "OTP verified. Customer identity confirmed. "
                    "Proceed with the action the customer originally requested β€” "
                    "call the appropriate tool immediately without speaking first."
                )
            key = result.get("fast_response_key")
            if key == "no_otp_pending":
                return "No OTP has been sent yet. Call send_otp first."
            if key in ("otp_expired", "otp_max_attempts"):
                return runner.advance("verify_otp", key).message
            if key == "otp_wrong":
                remaining = result.get("attempts_remaining", 0)
                plural = "attempts" if remaining != 1 else "attempt"
                runner.advance("verify_otp", key, extras={"attempts_remaining": remaining})
                return f"That code doesn't match. You have {remaining} {plural} remaining."
            return "That OTP doesn't match. Please check the code and try again, or ask me to resend it."

        tools.append(verify_otp)

    # ── unlock_account ────────────────────────────────────────────────────────
    if "unlock_account" in enabled:
        @function_tool
        async def unlock_account() -> str:
            """Unlock the customer's account. Requires verify_account + verify_otp to have succeeded first."""
            from tools.wavvy_tools import unlock_account as _unlock
            result = await _unlock(call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "unlock_account",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                session.orchestrator_state.completed_terminal_tools.add("unlock_account")
                return "Account unlocked successfully. The customer should now be able to log in."
            if result.get("fast_response_key") == "verification_required":
                return "Cannot unlock β€” OTP verification required first. Please call send_otp and verify_otp."
            return "Unable to unlock account right now. Escalating to a specialist."

        tools.append(unlock_account)

    # ── initiate_refund ───────────────────────────────────────────────────────
    if "initiate_refund" in enabled:
        @function_tool
        async def initiate_refund(transaction_id: str) -> str:
            """Initiate a refund for a failed transaction. Requires verify_account + verify_otp. transaction_id: format TXN-XXXX."""
            from tools.wavvy_tools import initiate_refund as _refund
            result = await _refund(transaction_id.upper(), call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "initiate_refund",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                session.orchestrator_state.completed_terminal_tools.add("initiate_refund")
                rfn = result.get("rfn_number", "")
                rfn_part = f" Your reference number is {rfn}." if rfn else ""
                return f"Refund initiated for {transaction_id}.{rfn_part} Funds will appear within 3 to 5 business days."
            key = result.get("fast_response_key")
            if key == "verification_required":
                return "Cannot initiate refund β€” OTP verification required first."
            if key == "transaction_not_found":
                return f"Transaction {transaction_id} not found on this account. Please confirm the ID."
            if key in ("refund_already_initiated", "refund_already_completed"):
                # Refund already exists β€” goal is achieved; block any pivot back to this workflow
                session.orchestrator_state.completed_terminal_tools.add("initiate_refund")
                existing_rfn = result.get("rfn_number", "")
                base_msg = runner.advance("initiate_refund", key).message
                if existing_rfn:
                    return base_msg + f" The existing reference number is {existing_rfn}."
                return base_msg
            if key in ("refund_ineligible", "fraud_review_required",
                       "kyc_escalation_required", "session_duplicate"):
                return runner.advance("initiate_refund", key).message
            return "Unable to initiate refund right now. Let me connect you with a specialist."

        tools.append(initiate_refund)

    # ── raise_dispute ─────────────────────────────────────────────────────────
    if "raise_dispute" in enabled:
        @function_tool
        async def raise_dispute(
            transaction_id: str,
            reason: str = "unrecognised_transaction",
        ) -> str:
            """File a dispute for a completed transaction the customer doesn't recognise or received no service for. Requires verify_account + verify_otp. reason: e.g. 'no_service_received', 'unrecognised_transaction', 'wrong_amount'."""
            from tools.wavvy_tools import raise_dispute as _dispute
            normalized_txn = _normalize_txn_id(transaction_id)
            result = await _dispute(normalized_txn, reason, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "raise_dispute",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                session.orchestrator_state.completed_terminal_tools.add("raise_dispute")
                ref = result.get("dsp_number", "")
                ref_part = f" Your reference number is {ref}." if ref else ""
                return f"Dispute filed for {normalized_txn}.{ref_part} Our disputes team will review within 5–7 business days."
            key = result.get("fast_response_key")
            if key == "verification_required":
                return "Cannot file dispute β€” OTP verification required first. Call send_otp then verify_otp."
            if key == "transaction_not_found":
                return f"Transaction {normalized_txn} not found. Please confirm the ID."
            if key in ("dispute_duplicate", "dispute_window_expired", "dispute_ineligible",
                       "high_value_manual_required", "fraud_review_required",
                       "refund_already_initiated", "refund_already_completed"):
                return runner.advance("raise_dispute", key).message
            return "Unable to file dispute right now. Let me connect you with our disputes team."

        tools.append(raise_dispute)

    # ── report_fraud ──────────────────────────────────────────────────────────
    if "report_fraud" in enabled:
        @function_tool
        async def report_fraud(
            transaction_id: str,
            fraud_type: str = "unauthorized_transaction",
        ) -> str:
            """Report an unauthorised transaction as fraud. Requires verify_account + verify_otp. fraud_type: 'unauthorized_transaction', 'phishing', 'lost_stolen_card', 'account_takeover'."""
            from tools.wavvy_tools import report_fraud as _fraud
            normalized_txn = _normalize_txn_id(transaction_id)
            result = await _fraud(normalized_txn, fraud_type, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "report_fraud",
                "status": "done", "result": {"success": result.get("success")},
            }))
            if result.get("success"):
                session.orchestrator_state.completed_terminal_tools.add("report_fraud")
                ref = result.get("fraud_number", "")
                ref_part = f" Your reference number is {ref}." if ref else ""
                return f"Fraud report filed for {normalized_txn}.{ref_part} Our fraud team will review within 24 hours."
            key = result.get("fast_response_key")
            if key == "verification_required":
                return "Cannot file fraud report β€” OTP verification required first."
            if key in ("fraud_already_reported", "fraud_transaction_reversed", "transaction_not_found"):
                return runner.advance("report_fraud", key).message
            return "Unable to file fraud report right now. Let me connect you with our fraud team."

        tools.append(report_fraud)

    # ── check_payment_status ──────────────────────────────────────────────────
    if "check_payment_status" in enabled:
        @function_tool
        async def check_payment_status(transaction_id: str) -> str:
            """Check detailed payment status with SLA context. Use when customer says payment is stuck, pending too long, or money not credited. Requires verify_account."""
            from tools.wavvy_tools import check_payment_status as _status
            normalized_txn = _normalize_txn_id(transaction_id)
            result = await _status(normalized_txn, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "check_payment_status",
                "status": "done", "result": {"status": result.get("status")},
            }))
            key = result.get("fast_response_key")
            if key == "verification_required":
                return "Cannot check payment status β€” verify_account required first."
            if key == "transaction_not_found":
                return f"Transaction {normalized_txn} not found. Please confirm the ID."
            if key in ("payment_processing", "payment_processing_delayed",
                       "payment_settled", "payment_failed_post_debit",
                       "payment_returned", "gateway_error"):
                return runner.advance("check_payment_status", key).message
            return f"Payment status for {normalized_txn}: {result.get('status', 'unknown')}."

        tools.append(check_payment_status)

    # ── get_account_holds ─────────────────────────────────────────────────────
    if "get_account_holds" in enabled:
        @function_tool
        async def get_account_holds() -> str:
            """Check why the customer's account has a hold or restriction. Returns hold type and reason from verified session data. Call when customer asks why their account is locked, frozen, or restricted."""
            if not session.customer_id:
                return "Account not verified yet. Call verify_account first."
            from tools.wavvy_tools import get_account_holds as _holds
            result = await _holds(call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "get_account_holds",
                "status": "done", "result": {"count": result.get("count", 0)},
            }))
            if result.get("fast_response_key") == "verification_required":
                return "Account not verified yet. Call verify_account first."
            holds = result.get("holds") or []
            if not holds:
                return "No active holds on this account."
            lines = [
                f"{h.get('hold_type', 'unknown')} hold"
                + (f": {h['reason']}" if h.get("reason") else "")
                for h in holds
            ]
            return f"Active account hold{'s' if len(lines) != 1 else ''}: {'; '.join(lines)}."

        tools.append(get_account_holds)

    # ── get_refund_status ─────────────────────────────────────────────────────
    if "get_refund_status" in enabled:
        @function_tool
        async def get_refund_status(transaction_id: str | None = None) -> str:
            """Check the status of an existing refund. Returns RFN reference, current status and expected timeline. Call when customer asks about a refund they've already initiated. transaction_id: optional TXN-XXXX to filter by specific transaction."""
            if not session.customer_id:
                return "Account not verified yet. Call verify_account first."
            from tools.wavvy_tools import get_refund_status as _refund_status
            result = await _refund_status(transaction_id, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "get_refund_status",
                "status": "done", "result": {"count": result.get("count", 0)},
            }))
            if result.get("fast_response_key") == "verification_required":
                return "Account not verified yet. Call verify_account first."
            refunds = result.get("refunds") or []
            if not refunds:
                msg = "No in-progress refunds"
                if transaction_id:
                    msg += f" for {transaction_id.upper()}"
                return msg + "."
            lines = [
                f"{r.get('rfn_number','?')} for {r.get('merchant','?')} "
                f"β‚Ή{r.get('amount','?')} β€” status: {r.get('status','?')}"
                for r in refunds
            ]
            return (
                f"Refund{'s' if len(lines) != 1 else ''} in progress: "
                + "; ".join(lines)
                + ". Refunds typically arrive within 3–5 business days of initiation."
            )

        tools.append(get_refund_status)

    # ── get_dispute_status ────────────────────────────────────────────────────
    if "get_dispute_status" in enabled:
        @function_tool
        async def get_dispute_status(transaction_id: str | None = None) -> str:
            """Check the status of an open dispute. Returns DSP reference, current status and next steps. Call when customer asks about a dispute they've filed. transaction_id: optional TXN-XXXX to filter by specific transaction."""
            if not session.customer_id:
                return "Account not verified yet. Call verify_account first."
            from tools.wavvy_tools import get_dispute_status as _dispute_status
            result = await _dispute_status(transaction_id, call_id)
            asyncio.create_task(publish_event({
                "type": "tool_call", "tool": "get_dispute_status",
                "status": "done", "result": {"count": result.get("count", 0)},
            }))
            if result.get("fast_response_key") == "verification_required":
                return "Account not verified yet. Call verify_account first."
            disputes = result.get("disputes") or []
            if not disputes:
                msg = "No open disputes"
                if transaction_id:
                    msg += f" for {transaction_id.upper()}"
                return msg + "."
            lines = [
                f"{d.get('dsp_number','?')} for {d.get('merchant','?')} "
                f"β‚Ή{d.get('amount','?')} β€” reason: {d.get('reason','?')}, status: {d.get('status','?')}"
                for d in disputes
            ]
            return (
                f"Open dispute{'s' if len(lines) != 1 else ''}: "
                + "; ".join(lines)
                + ". Our disputes team reviews within 5–7 business days."
            )

        tools.append(get_dispute_status)

    # ── escalate_to_human ─────────────────────────────────────────────────────
    # Always available regardless of config.
    @function_tool
    async def escalate_to_human(reason: str = "customer_request") -> str:
        """Transfer the caller to a human support agent. Only call this when the customer has explicitly asked to speak with a human in this turn AND you have confirmed their permission. Never call this to avoid doing a support task β€” always try send_otp β†’ verify_otp β†’ the action tool first."""
        if session.escalated:
            return "Already transferring to a human agent."

        async def _check_availability() -> dict:
            try:
                async with httpx.AsyncClient(timeout=3.0) as _c:
                    _r = await _c.get(f"{settings.backend_internal_url}/api/agents/availability")
                    return _r.json() if _r.status_code == 200 else {}
            except Exception:
                return {"available": True}

        _avail = await _check_availability()
        if not _avail.get("available", True):
            # Retry once after 700ms β€” handles the reconnect window after backend reload
            # (agent desktop reconnects after ~1s backoff; first check may land during the gap)
            await asyncio.sleep(0.7)
            _avail = await _check_availability()

        if not _avail.get("available", True):
            return (
                "I'm sorry, all our specialists are currently busy. "
                "I'm right here to keep helping you β€” what would you like to do?"
            )

        parts = []
        for m in session.conversation_history[-6:]:
            role = "Customer" if m.get("role") == "user" else "Agent"
            parts.append(f"{role}: {m.get('content', '')[:120]}")
        summary = " | ".join(parts) or "support inquiry"

        # Deterministic escalation reason: use active workflow name so the
        # agent desktop always shows the real issue type (e.g. "Fraud Report")
        # instead of whatever generic string the LLM provides.
        try:
            _orch = getattr(session, "orchestrator_state", None)
            _wf_id = getattr(_orch, "active_workflow_id", None) if _orch else None
            if _wf_id:
                from config_loader import get_active_workflows
                for _wf in get_active_workflows():
                    if str(_wf.id) == str(_wf_id):
                        reason = _wf.name
                        break
        except Exception:
            pass

        result = await execute_with_recovery(
            tool_name="escalate_to_human",
            args={"reason": reason, "lead_summary": summary, "call_id": call_id},
            call_id=call_id,
            turn_id=session.turn_count,
            step_id="escalate",
            session=session,
        )
        if result.success:
            session.escalated = True
            session.conv_state.mark_escalated()
            handoff = result.data.get("handoff_bundle", {})
            session.handoff_bundle = handoff
            esc_room_name      = result.data.get("esc_room_name")
            customer_esc_token = result.data.get("customer_esc_token")
            agent_esc_token    = result.data.get("agent_esc_token")
            livekit_url        = result.data.get("livekit_url", settings.livekit_url)
            if esc_room_name:
                session.esc_room_name   = esc_room_name
            if agent_esc_token:
                session.agent_esc_token = agent_esc_token
            asyncio.create_task(publish_event({
                "type": "escalation",
                "handoff_bundle": handoff,
                "esc_room_name": esc_room_name,
                "customer_esc_token": customer_esc_token,
                "livekit_url": livekit_url,
            }))
            asyncio.create_task(_notify_fastapi(call_id, "escalation", {
                "handoff_bundle": handoff,
                "conversation_history": session.conversation_history,
                "agent_esc_token": agent_esc_token,
                "esc_room_name": esc_room_name,
                "livekit_url": livekit_url,
                "customer_id": session.customer_id,
            }))
            return "Transferring you to a support specialist now."

        # Blocked escalation β€” surface the exact guidance returned by the tool
        # instead of a generic "unable to connect" message.
        say_this = (result.data or {}).get("say_this")
        if say_this:
            return say_this
        return "Unable to connect right now. Please try again in a moment."

    tools.append(escalate_to_human)

    # ── cancel_escalation ─────────────────────────────────────────────────────
    @function_tool
    async def cancel_escalation() -> str:
        """Revert to AI when caller explicitly asks to stay with AI before a human joins."""
        if not session.escalated:
            return "I'm already here with you!"
        if session.human_joined:
            return "A human agent has already joined β€” I can't take back over right now."
        session.escalated = False
        asyncio.create_task(publish_event({"type": "escalation_cancelled"}))
        return "I'm back! Happy to keep helping you."

    tools.append(cancel_escalation)

    logger.info("[%s] tools registered: %s", call_id, [t.info.name for t in tools])
    return tools


async def _notify_fastapi(call_id: str, event_type: str, data: dict) -> None:
    url = f"{settings.backend_internal_url}/api/internal/worker-event"
    payload = {"call_id": call_id, "event_type": event_type, "data": data}
    try:
        async with httpx.AsyncClient(timeout=5.0) as client:
            await client.post(url, json=payload)
    except Exception as exc:
        logger.warning("[%s] worker→fastapi notify failed (%s): %s", call_id, event_type, exc)