File size: 52,972 Bytes
5236b40
 
 
460f249
5236b40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460f249
5236b40
 
 
 
 
 
 
 
 
 
 
 
 
 
460f249
 
 
5236b40
460f249
 
 
 
 
 
 
 
 
 
 
 
5236b40
460f249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5236b40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460f249
5236b40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
import logging
import asyncio
import json
import uuid
import os
from datetime import datetime
from zoneinfo import ZoneInfo
from typing import Annotated, Optional, AsyncIterable, Any, Dict
import random
import http.server
import socketserver
import threading

from dotenv import load_dotenv
from livekit import rtc
from livekit.agents import (
    AutoSubscribe,
    JobContext,
    JobProcess,
    WorkerOptions,
    cli,
    llm,
    AgentSession,
    metrics,
    MetricsCollectedEvent,
    Agent,
)
from livekit.agents.llm import function_tool
from livekit.agents.voice import (
    RunContext,
    ModelSettings,
)
from livekit.plugins import openai, deepgram, cartesia, silero, groq

# Groq SDK for summary generation
from groq import Groq as GroqClient

# Monitoring and validation imports
import sentry_sdk
from logger import logger
from validators import validate_phone_number, validate_appointment_time, validate_purpose, validate_appointment_id

# Try to import Beyond Presence plugin if available
try:
    from livekit.plugins import bey
    BEY_AVAILABLE = True
except ImportError:
    BEY_AVAILABLE = False
    logging.warning("Beyond Presence plugin not available. Install with: pip install \"livekit-agents[bey]\"")

from db import Database

load_dotenv()

# Initialize Sentry for error tracking
if os.getenv("SENTRY_DSN"):
    sentry_sdk.init(
        dsn=os.getenv("SENTRY_DSN"),
        traces_sample_rate=0.1,
        environment=os.getenv("ENVIRONMENT", "production")
    )
    print("βœ… Sentry error tracking enabled")

logger = logging.getLogger("voice-agent")
logger.setLevel(logging.INFO)

# Suppress noisy logs from libraries
logging.getLogger("hpack").setLevel(logging.WARNING)
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("livekit").setLevel(logging.INFO)
logging.getLogger("urllib3").setLevel(logging.WARNING)

def get_groq_api_key():
    """Rotate between multiple Groq API keys if available to avoid rate limits."""
    keys_str = os.getenv("GROQ_API_KEYS", "")
    if keys_str:
        keys = [k.strip() for k in keys_str.split(",") if k.strip()]
        if keys:
            chosen = random.choice(keys)
            print(f"DEBUG: Selected Groq Key from list of {len(keys)}. Prefix: {chosen[:5]}...")
            return chosen
    
    single_key = os.getenv("GROQ_API_KEY")
    if single_key:
         print(f"DEBUG: Using single GROQ_API_KEY. Prefix: {single_key[:5]}...")
         return single_key
    
    print("DEBUG: No Groq API Key found!")
    return None

try:
    from flagsmith import Flagsmith
    flagsmith = Flagsmith(environment_key=os.getenv("FLAGSMITH_ENVIRONMENT_KEY", "default"))
except Exception:
    flagsmith = None

# ... (omitting lines for brevity)





SYSTEM_PROMPT = """
You are the SkyTask Clinic Assistant, a friendly and capable voice receptionist.

# User: {name} | Status: {status} | Goal: {goal_instruction}
# Rules
- Voice response: Plain text only. Natural and polite.
- Be warm: Use "Good morning", "Thank you", "Please".
- Length: 1-3 sentences, but don't be robotic.
- Speak nums: "five five five". No emojis/markdown.
- Address user by name if known.
# Flow
1. Identify user (ask phone/name).
2. Tools: book_appointment, check_slots, retrieve_appointments, cancel/modify, summarize_call, end_conversation.
   - STRICT: Only call these tools. Do NOT invent new tools.
   - Do NOT speak tool names. Execute silently.
   - summarize_call: When user asks "summarize" or "recap" - gives summary but continues call
   - end_conversation: When user says "end call", "goodbye", "bye" - ends the call
3. Verify name mismatches.
# Guardrails
- Privacy protection active.
- Scope: Clinic appointments only.
"""

class Assistant(Agent):
    def __init__(self, db: Database, user_context: dict, room):
        current_time_ist = datetime.now(ZoneInfo("Asia/Kolkata")).strftime("%Y-%m-%d %I:%M %p")
        
        # Initialize with Guest state
        instructions = SYSTEM_PROMPT.format(
            name="Guest",
            status="Unidentified",
            goal_instruction="Ask for their phone number (and name) to pull up their file. Say: 'Hi! I'm the clinic assistant. May I have your phone number to get started?'"
        )
        instructions += f"\n\nCurrent time (IST): {current_time_ist}"
        
        super().__init__(instructions=instructions)
        self.db = db
        self.user_context = user_context
        self.room = room
        self.current_time_str = current_time_ist
        self.should_disconnect = False
        
        # References needed for summary generation (set later in entrypoint)
        self.usage_collector = None
        self.assistant = None
        self.start_time = datetime.now()
        self.avatar_type = None
        self.tts_provider = None
        
        # Prevent duplicate summaries
        self.summary_generated = False
        
        # Listen for data messages from frontend (e.g., End Call button)
        @room.on("data_received")
        def on_data_received(data_packet):
            try:
                payload = data_packet.data.decode('utf-8')
                data = json.loads(payload)
                
                if data.get("type") == "request_end_call":
                    logger.info("πŸ”΄ Frontend requested end call via button - triggering end_conversation")
                    # Trigger the end_conversation tool asynchronously
                    asyncio.create_task(self.end_conversation("User clicked End Call button"))
            except Exception as e:
                logger.warning(f"Error processing frontend data message: {e}")
    
    def update_instructions_with_name(self, name: str):
        """Update the agent's instructions to include the user's name"""
        try:
            # Re-format with REAL name
            new_instructions = SYSTEM_PROMPT.format(
                name=name,
                status="Authenticated",
                goal_instruction=f"Help {name} with appointments. Address them as {name}."
            )
            full_instructions = f"{new_instructions}\n\nCurrent time (IST): {self.current_time_str}"
            
            # Update the agent's instructions
            self._instructions = full_instructions
            
            print(f"βœ… Updated agent instructions with user name: {name}")
            print(f"πŸ” DEBUG - NEW PROMPT:\n{new_instructions}")
            return True
        except Exception as e:
            print(f"Failed to update instructions: {e}")
            return False

    # ... (omitting lines) ...

    @function_tool()
    async def identify_user(
        self, 
        contact_number: str
    ):
        """Identify the user by their phone number. Only call this when you have received a numeric phone number.
        
        Args:
            contact_number: The user's contact phone number (e.g. 555-0101). Do not provide an empty string.
        """
        if not contact_number or len(contact_number.strip()) < 3:
            return "Error: A valid contact number is required to identify the user."

        try:
            contact_number = validate_phone_number(contact_number)
        except ValueError as e:
            return f"Error: {str(e)}"

        await self._emit_frontend_event("identify_user", "started", {"contact_number": contact_number})
        logger.info(f"Identifying user with number: {contact_number}")
        user = self.db.get_user(contact_number)
        if not user:
            user = self.db.create_user(contact_number)
            is_new = True
        else:
            is_new = False
        
        self.user_context["contact_number"] = contact_number
        self.user_context["user_name"] = user.get("name", "User")
        
        name = user.get('name', 'User')
        
        # Update the agent's instructions to include the user's name
        self.update_instructions_with_name(name)
        
        # ALSO inject a system message into the chat context
        # This ensures the LLM knows the name in the conversation history
        if hasattr(self, 'chat_ctx') and self.chat_ctx:
            try:
                self.chat_ctx.items.append(
                    llm.ChatMessage(
                        role="system",
                        content=[f"IMPORTANT: The user's name is {name}. You MUST address them as {name} in all future responses. When they ask 'what's my name' or 'do you know my name', respond with 'Yes, {name}, your name is {name}.'"]
                    )
                )
                print(f"βœ… Injected name '{name}' into chat context")
            except Exception as e:
                print(f"Could not inject into chat context: {e}")
        
        # Return a message that FORCES the agent to say the name immediately
        result_msg = f"User identified successfully. Their name is {name}. You MUST immediately respond by saying: 'Great to meet you, {name}! How can I help you today?' Use their name {name} in your response right now."
        await self._emit_frontend_event("identify_user", "success", result={"name": name, "is_new": is_new})
        return result_msg

    @function_tool()
    async def verify_identity(
        self, 
        contact_number: str,
        stated_name: str
    ):
        """Verify the user's identity using both their phone number and stated name. 
        Use this when the user provides both pieces of information.
        
        Args:
            contact_number: The user's phone number (numeric).
            stated_name: The name the user introduced themselves with.
        """
        if not contact_number or len(contact_number.strip()) < 3:
            return "Error: A valid contact number is required."

        try:
            contact_number = validate_phone_number(contact_number)
        except ValueError as e:
            return f"Error: {str(e)}"

        await self._emit_frontend_event("verify_identity", "started", {"contact_number": contact_number, "name": stated_name})
        logger.info(f"Verifying identity: {stated_name} with {contact_number}")
        
        user = self.db.get_user(contact_number)
        
        if not user:
            # New user case with name provided
            user = self.db.create_user(contact_number, name=stated_name)
            is_new = True
            db_name = stated_name
            match = True
        else:
            is_new = False
            db_name = user.get("name", "User")
            # Simple fuzzy match check (case insensitive)
            match = stated_name.lower() in db_name.lower() or db_name.lower() in stated_name.lower()
        
        self.user_context["contact_number"] = contact_number
        self.user_context["user_name"] = db_name
        
        # Update system with the CORRECT name from DB (or new name)
        self.update_instructions_with_name(db_name)
        
        if match:
             # ALSO inject a system message into the chat context
             # NOTE: Disabled - chat_ctx is read-only, agent instructions are sufficient
            # if hasattr(self, 'chat_ctx') and self.chat_ctx:
            #     try:
            #         self.chat_ctx.items.append(
            #             llm.ChatMessage(
            #                 role="system",
            #                 content=[f"IMPORTANT: Identity verified. User is {db_name}. Address them as {db_name}."]
            #             )
            #         )
            #     except Exception:
            #         pass
            
            result_msg = f"Identity verified! The user is indeed {db_name}. Greet them naturally as {db_name}."
            await self._emit_frontend_event("verify_identity", "success", result={"name": db_name, "match": True})
            return result_msg
        else:
            # Name mismatch logic
            result_msg = f"Identity Mismatch Warning: The phone number belongs to '{db_name}', but user said '{stated_name}'. politely ask: 'I have this number registered under {db_name}. Are you {db_name}?'"
            await self._emit_frontend_event("verify_identity", "warning", result={"db_name": db_name, "stated_name": stated_name, "match": False})
            return result_msg

    async def _emit_frontend_event(self, tool_name: str, status: str, args: dict = None, result: dict = None):
        try:
            payload = json.dumps({
                "type": "tool_call",
                "tool": tool_name,
                "status": status,
                "args": args,
                "result": result
            })
            await self.room.local_participant.publish_data(payload, reliable=True)
        except Exception as e:
            logger.error(f"Failed to emit frontend event: {e}")

    @function_tool()
    async def hello(self, response: str = ""):
        """This tool is used for greetings.
        
        Args:
            response: The greeting response.
        """
        return "Hello! How can I help you today?"

    @function_tool()
    async def identify_user(
        self, 
        contact_number: str
    ):
        """Identify the user by their phone number. Only call this when you have received a numeric phone number.
        
        Args:
            contact_number: The user's contact phone number (e.g. 555-0101). Do not provide an empty string.
        """
        if not contact_number or len(contact_number.strip()) < 3:
            return "Error: A valid contact number is required to identify the user."

        try:
            contact_number = validate_phone_number(contact_number)
        except ValueError as e:
            return f"Error: {str(e)}"

        await self._emit_frontend_event("identify_user", "started", {"contact_number": contact_number})
        logger.info(f"Identifying user with number: {contact_number}")
        user = self.db.get_user(contact_number)
        if not user:
            user = self.db.create_user(contact_number)
            is_new = True
        else:
            is_new = False
        
        self.user_context["contact_number"] = contact_number
        self.user_context["user_name"] = user.get("name", "User")
        
        # Helper comment: Name will now be picked up by the LLM from the tool return value
        # and usage enforced by updated system prompts.
        
        result_msg = f"User identified. Name: {user.get('name')}. New user: {is_new}."
        await self._emit_frontend_event("identify_user", "success", result={"name": user.get('name'), "is_new": is_new})
        return result_msg

    @function_tool()
    async def fetch_slots(self, location: str):
        """Fetch available appointment slots.
        
        Args:
            location: The clinic location to check (e.g. 'main', 'downtown').
        """
        logger.info(f"Fetching available slots for {location}")
        await self._emit_frontend_event("fetch_slots", "started", {"location": location})
        
        # Use DB method to fetch slots (real or mock)
        available_slots = self.db.get_available_slots()
        slots_json = json.dumps(available_slots)
        
        await self._emit_frontend_event("fetch_slots", "success", result=available_slots)
        return slots_json

    @function_tool()
    async def book_appointment(
        self,
        time: str,
        purpose: str
    ):
        """Book an appointment for the identified user.
        
        Args:
            time: The ISO 8601 formatted date and time for the appointment.
            purpose: Purpose of the appointment.
        """
        await self._emit_frontend_event("book_appointment", "started", {"time": time, "purpose": purpose})
        contact_number = self.user_context.get("contact_number")
        if not contact_number:
            return "Error: User not identified. Please ask for phone number first."

        try:
            contact_number = validate_phone_number(contact_number)
        except ValueError as e:
            return f"Error validation phone: {str(e)}"

        logger.info(f"Booking appointment for {contact_number} at {time}")
        
        is_available = self.db.check_slot_availability(datetime.fromisoformat(time))
        if not is_available:
             return "Error: Slot not available."

        result = self.db.book_appointment(contact_number, time, purpose)
        if result:
            await self._emit_frontend_event("book_appointment", "success", result=result)
            return f"Appointment booked successfully. ID: {result.get('id')}"
        else:
            await self._emit_frontend_event("book_appointment", "failed")
            return "Failed to book appointment."

    @function_tool()
    async def retrieve_appointments(self, user_confirmation: str):
        """Retrieve past and upcoming appointments for the identified user.
        
        Args:
            user_confirmation: The user's confirmation to see their appointments (e.g. 'show them', 'yes').
        """
        await self._emit_frontend_event("retrieve_appointments", "started")
        contact_number = self.user_context.get("contact_number")
        if not contact_number:
            return "Error: User not identified."

        try:
            contact_number = validate_phone_number(contact_number)
        except ValueError as e:
            return f"Error: {str(e)}"
            
        appointments = self.db.get_user_appointments(contact_number)
        if not appointments:
            await self._emit_frontend_event("retrieve_appointments", "success", result=[])
            return "No appointments found."
        
        await self._emit_frontend_event("retrieve_appointments", "success", result=appointments)
        return json.dumps(appointments)

    @function_tool()
    async def cancel_appointment(
        self,
        appointment_id: str
    ):
        """Cancel an appointment.
        
        Args:
            appointment_id: The ID of the appointment to cancel.
        """
        await self._emit_frontend_event("cancel_appointment", "started", {"appointment_id": appointment_id})
        success = self.db.cancel_appointment(appointment_id)
        if success:
            await self._emit_frontend_event("cancel_appointment", "success", result={"id": appointment_id})
            return "Appointment cancelled successfully."
        else:
            await self._emit_frontend_event("cancel_appointment", "failed")
            return "Failed to cancel appointment."

    @function_tool()
    async def modify_appointment(
        self,
        appointment_id: str,
        new_time: str
    ):
        """Modify the date/time of an appointment.
        
        Args:
            appointment_id: The ID of the appointment to modify.
            new_time: The new ISO 8601 formatted date and time.
        """
        await self._emit_frontend_event("modify_appointment", "started", {"appointment_id": appointment_id, "new_time": new_time})
        success = self.db.modify_appointment(appointment_id, new_time)
        if success:
            await self._emit_frontend_event("modify_appointment", "success", result={"id": appointment_id, "new_time": new_time})
            return "Appointment modified successfully."
        else:
            await self._emit_frontend_event("modify_appointment", "failed")
            return "Failed to modify appointment."

    @function_tool()
    async def summarize_call(
        self,
        request: Annotated[str, "User's request for summary"] = "summarize"
    ) -> str:
        """Provide a summary of the current call without ending it.
        
        Use this when the user asks for a summary but wants to continue the conversation.
        Example triggers: "Can you summarize?", "What did we discuss?", "Recap please"
        
        Args:
            request: The user's request for a summary (e.g., "summarize", "recap")
        
        Returns:
            str: A spoken summary of the conversation so far.
        """
        logger.info(f"Generating mid-call summary (not ending): {request}")
        
        # Get context and metrics
        contact = self.user_context.get("contact_number")
        if not contact:
            return "So far, we've discussed your appointments. Is there anything else I can help you with?"
        
        # Collect usage metrics
        summary = self.usage_collector.get_summary()
        usage_stats = {
            "stt_duration": summary.stt_audio_duration,
            "llm_prompt_tokens": summary.llm_prompt_tokens,
            "llm_completion_tokens": summary.llm_completion_tokens,
            "tts_chars": summary.tts_characters_count
        }
        
        duration = (datetime.now() - self.start_time).total_seconds()
        user_name = self.user_context.get("user_name", "the patient")
        
        # Generate summary directly
        try:
            summary_data = await generate_and_save_summary(
                self.db, 
                self.assistant.chat_ctx, 
                contact, 
                duration, 
                self.avatar_type, 
                self.tts_provider, 
                user_name, 
                usage_stats
            )
            if summary_data and isinstance(summary_data, dict):
                spoken_summary = summary_data.get("spoken_text", "So far, we've discussed your appointments.")
                logger.info(f"Mid-call summary: {spoken_summary}")
                return spoken_summary
        except Exception as e:
            logger.error(f"Failed to generate mid-call summary: {e}")
        
        return "So far, we've discussed your appointments. Is there anything else I can help you with?"

    @function_tool()
    async def end_conversation(self, summary_request: str):
        """End the current conversation session and generate a final summary.
        
        Args:
            summary_request: The user's request to end or wrap up (e.g. 'bye', 'summarize', 'we're done').
        """
        logger.info("Ending conversation - generating summary first")
        
        # GUARD: Prevent duplicate summaries
        if self.summary_generated:
            logger.warning("Summary already generated - skipping duplicate generation")
            return "Thank you for calling. Goodbye!"
        
        spoken_text = "Thank you for calling. Have a great day!"
        summary_sent = False
        
        # Get context and metrics
        contact = self.user_context.get("contact_number")
        if contact:
            # Collect usage metrics
            summary = self.usage_collector.get_summary()
            usage_stats = {
                "stt_duration": summary.stt_audio_duration,
                "llm_prompt_tokens": summary.llm_prompt_tokens,
                "llm_completion_tokens": summary.llm_completion_tokens,
                "tts_chars": summary.tts_characters_count
            }
            
            duration = (datetime.now() - self.start_time).total_seconds()
            user_name = self.user_context.get("user_name", "the patient")
            
            # Generate summary directly
            try:
                summary_data = await generate_and_save_summary(
                    self.db,
                    self.assistant.chat_ctx,
                    contact,
                    duration,
                    self.avatar_type,
                    self.tts_provider,
                    user_name,
                    usage_stats
                )
                if summary_data and isinstance(summary_data, dict):
                    # 1. Get spoken summary
                    spoken_text = summary_data.get("spoken_text", spoken_text)
                    
                    # 2. Publish structured data to frontend
                    payload = json.dumps({
                        "type": "summary",
                        "summary": summary_data
                    })
                    await self.room.local_participant.publish_data(payload, reliable=True)
                    logger.info("Summary sent to frontend")
                    summary_sent = True
                    
                    # Mark summary as generated to prevent duplicates
                    self.summary_generated = True
                    
                    # CRITICAL: Send close_session to trigger auto-disconnect for voice UX
                    # Small delay to ensure summary is received first
                    await asyncio.sleep(0.1)
                    close_payload = json.dumps({"type": "close_session"})
                    await self.room.local_participant.publish_data(close_payload, reliable=True)
                    logger.info("βœ… close_session sent - UI will auto-disconnect")
                    
            except Exception as e:
                logger.error(f"Failed to process summary: {e}")
        
        # CRITICAL: If summary wasn't sent, send fallback with at least cost structure
        if not summary_sent:
            logger.warning("Sending fallback summary with cost placeholder")
            fallback = {
                "content": "Call ended. See cost breakdown below.",
                "spoken_text": spoken_text,
                "costs": {"stt": 0.0, "tts": 0.0, "llm": 0.0, "avatar": 0.0, "total": 0.0},
                "status": "fallback"
            }
            try:
                payload = json.dumps({"type": "summary", "summary": fallback})
                await self.room.local_participant.publish_data(payload, reliable=True)
                logger.info("Fallback summary sent to frontend")
            except Exception as e:
                logger.error(f"Failed to send fallback: {e}")
        
        # NOTE: Don't send close_session here - let frontend's 2-second timer handle disconnect
        # This ensures the summary data channel message is received before disconnect
        
        # 4. Request disconnect implicitly by setting flag
        # The session listener will handle the actual disconnect after speech ends
        self.should_disconnect = True
        logger.info("Disconnect requested - waiting for speech to finish")
        
        # Start safeguard immediately
        asyncio.create_task(self.safeguard_disconnect())
        
        # Return the simplified spoken text for the agent to say immediately
        return spoken_text

    async def safeguard_disconnect(self):
        """Force disconnect if normal flow fails."""
        logger.info("Safeguard: Timer started (10s)...")
        await asyncio.sleep(10.0)
        
        state = self.room.connection_state
        logger.info(f"Safeguard: Timeout reached. Room state is: {state}")
        
        if state == "connected":
            logger.warning("Safeguard: Timed out. Sending close_session event.")
            try:
                payload = json.dumps({"type": "close_session"})
                await self.room.local_participant.publish_data(payload, reliable=True)
                logger.info("Safeguard: close_session event sent.")
            except Exception as e:
                logger.warning(f"Safeguard: Failed to send event: {e}")
            
            await asyncio.sleep(3.0) # Give frontend more time to process
            
            if self.room.connection_state == "connected":
                logger.warning("Safeguard: Force disconnecting room now.")
                await self.room.disconnect()
        else:
            logger.info("Safeguard: Room already disconnected, taking no action.")

def calculate_costs(duration_seconds: float, tts_chars: int, avatar_type: str, tts_provider: str, prompt_tokens: int = 0, completion_tokens: int = 0):
    # Rates per unit
    stt_rate = 0.006      # Deepgram Nova-2 ($0.006/min)
    # Rates per unit (USD)
    stt_rate = 0.006      # Deepgram Nova-2 ($0.006/min)
    
    # LLM Pricing: OpenAI GPT-OSS-120B (used for main conversation)
    # Input: $0.15 / 1M tokens
    # Output: $0.60 / 1M tokens
    llm_rate_input = 0.15 / 1_000_000 
    llm_rate_output = 0.60 / 1_000_000
    
    # TTS Rates
    if tts_provider == "cartesia":
        tts_rate = 0.050 / 1000 # Cartesia (~$0.05/1k chars)
        tts_label = "Cartesia"
    elif tts_provider == "deepgram":
        tts_rate = 0.015 / 1000 # Deepgram Aura ($0.015/1k chars)
        tts_label = "Deepgram"
    else: # Groq / Other
        tts_rate = 0.000        # Assume Free/Included
        tts_label = "Groq"

    # Avatar Rates
    avatar_rate = 0.05 if avatar_type == 'bey' else 0 # Beyond Presence (~$0.05/min)

    # Calculate Standard Costs
    stt_cost = (duration_seconds / 60) * stt_rate
    tts_cost = tts_chars * tts_rate

    # Use real counts if provided, otherwise estimate (fallback)
    if prompt_tokens == 0 and completion_tokens == 0:
         # Usage estimates (simplified)
         # Assume 150 words/min -> ~200 tokens/min input
        estimated_input_tokens = (duration_seconds / 60) * 200
        estimated_output_tokens = (tts_chars / 4) # Rough char-to-token ratio
        llm_cost = (estimated_input_tokens * llm_rate_input) + (estimated_output_tokens * llm_rate_output)
    else:
        llm_cost = (prompt_tokens * llm_rate_input) + (completion_tokens * llm_rate_output)
    avatar_cost = (duration_seconds / 60) * avatar_rate
    
    total = stt_cost + tts_cost + llm_cost + avatar_cost
    
    # Log for debugging
    logger.info(f"Cost calculation: duration={duration_seconds}s, tts_chars={tts_chars}, provider={tts_provider}")
    logger.info(f"Costs: STT=${stt_cost:.6f}, TTS=${tts_cost:.6f}, LLM=${llm_cost:.6f}, Avatar=${avatar_cost:.6f}")
    
    return {
        "stt": round(stt_cost, 6),
        "tts": round(tts_cost, 6),
        "llm": round(llm_cost, 6),
        "avatar": round(avatar_cost, 6),
        "total": round(total, 6),
        "currency": "USD",
        "labels": {
            "tts": tts_label,
            "stt": "Deepgram",
            "llm": "Groq/OpenAI",
            "avatar": "Beyond Presence" if avatar_type == 'bey' else "3D Avatar"
        }
    }

async def generate_and_save_summary(db: Database, chat_ctx: llm.ChatContext, contact_number: str, duration: float, avatar_type: str, tts_provider: str, user_name: str = "the patient", usage_stats: dict = None) -> Optional[Dict[str, Any]]:
    if not contact_number:
        logger.warning("No contact number to save summary for.")
        return

    logger.info("Generating conversation summary...")
    
    transcript = ""
    messages_to_save = []
    
    # Try to extract messages from chat context
    try:
        if hasattr(chat_ctx, 'items'):
             items = chat_ctx.items
        elif hasattr(chat_ctx, 'messages'):
             items = chat_ctx.messages
        else:
             items = []
        
        for item in items:
            if isinstance(item, llm.ChatMessage):
                role = item.role
                content = item.content
                
                # Format content for string manipulation
                content_str = content
                if isinstance(content, list):
                    content_str = " ".join([str(c) for c in content])
                
                if isinstance(content_str, str):
                    transcript += f"{role}: {content_str}\n"

                # Prepare for DB
                msg_data = {
                    "role": role,
                    "content": content_str,
                    "tool_name": None,
                    "tool_args": None
                }
                
                # Attempt to extract tool info safely
                if hasattr(item, 'tool_calls') and item.tool_calls:
                     try:
                         tc = item.tool_calls[0]
                         # Handle both object and dict (depending on underlying library version)
                         if isinstance(tc, dict):
                             msg_data["tool_name"] = tc.get('function', {}).get('name')
                             msg_data["tool_args"] = tc.get('function', {}).get('arguments')
                         else:
                             # accessing attributes of ToolCall object
                             fn = getattr(tc, 'function', None)
                             if fn:
                                 msg_data["tool_name"] = getattr(fn, 'name', None)
                                 msg_data["tool_args"] = getattr(fn, 'arguments', None)
                     except Exception:
                         pass # Ignore tool extraction errors
                
                if role == "tool":
                    msg_data["tool_name"] = getattr(item, 'name', getattr(item, 'tool_call_id', None))

                messages_to_save.append(msg_data)
        
        # Save transcript to DB
        if messages_to_save:
            try:
                # Generate a session ID for this conversation batch
                session_id = str(uuid.uuid4())
                db.save_chat_transcript(session_id, contact_number, messages_to_save)
            except Exception as e:
                logger.error(f"Failed to save chat transcript to DB: {e}")

    except Exception as e:
        logger.error(f"Error extracting transcript: {e}")
    
    # Calculate costs using official metrics if available, otherwise fallback
    logger.info(f"Calculating costs with usage_stats: {usage_stats}")
    if usage_stats:
        tts_chars = usage_stats.get("tts_chars", 0)
        prompt_tokens = usage_stats.get("llm_prompt_tokens", 0)
        completion_tokens = usage_stats.get("llm_completion_tokens", 0)
        costs = calculate_costs(duration, tts_chars, avatar_type, tts_provider, prompt_tokens, completion_tokens)
    else:
        # Fallback estimation
        tts_chars = len(transcript) // 2 
        costs = calculate_costs(duration, tts_chars, avatar_type, tts_provider)
    
    logger.info(f"Calculated costs: {costs}")
    
    prompt = (
        f"Summarize the conversation with {user_name} in JSON format.\n"
        f"Transcript:\n{transcript}\n\n"
        "CRITICAL: Use natural time formats like '9 AM' or '2:30 PM', NOT 'nine zero zero hours'\n"
        "Return a valid JSON object with exactly two keys:\n"
        "1. 'spoken': A 1-2 sentence spoken closing for TTS. Natural, human-like, polite. No special chars. Start with 'To recap,'.\n"
        "2. 'written': A detailed bulleted summary for the user interface. Include topics, appointments booked, and outcome.\n"
        "IMPORTANT: Ensure the JSON is valid. Do NOT use unescaped newlines in the 'written' string or 'spoken' string. Use \\n for line breaks.\n"
    )

    max_retries = 3
    retry_delay = 1
    
    for attempt in range(max_retries):
        try:
            # Use Groq SDK directly instead of livekit wrapper for reliability
            api_key = os.getenv("GROQ_API_KEY_SUMMARY") or get_groq_api_key()
            client = GroqClient(api_key=api_key)
            
            # Use llama-3.3-70b-versatile for JSON reliability
            response = client.chat.completions.create(
                model="llama-3.3-70b-versatile",
                messages=[
                    {"role": "system", "content": "You are a helpful assistant. Output valid JSON only. Do not output markdown blocks."},
                    {"role": "user", "content": prompt}
                ],
                temperature=0.7,
                max_tokens=500
            )
            
            full_response = response.choices[0].message.content
            # Summary uses Llama-3.3-70B-Versatile
            # Pricing: Input $0.59/1M, Output $0.79/1M
            summary_input_cost = response.usage.prompt_tokens * (0.59 / 1_000_000)
            summary_output_cost = response.usage.completion_tokens * (0.79 / 1_000_000)
            summary_cost = summary_input_cost + summary_output_cost
            
            logger.info(f"πŸ” RAW LLM RESPONSE: {full_response}")
            logger.info(f"πŸ’° Summary LLM cost: ${summary_cost:.6f} ({response.usage.prompt_tokens} + {response.usage.completion_tokens} tokens)")
            
            # Attempt to parse JSON
            spoken = "To recap, we discussed your appointments. Have a great day!"
            written = ""
            
            try:
                # Clean up markdown code blocks if present
                clean_json = full_response.replace("```json", "").replace("```", "").strip()
                
                # Regex heuristic to find the JSON object { ... }
                import re
                match = re.search(r"\{.*\}", clean_json, re.DOTALL)
                if match:
                    clean_json = match.group(0)
                
                data = json.loads(clean_json)
                spoken = data.get("spoken", spoken)
                written = data.get("written", "")
                
            except (json.JSONDecodeError, AttributeError) as e:
                logger.warning(f"Failed to parse JSON summary (standard): {e}. Retrying with Regex Fallback.")
                # Fallback: Regex extraction for common invalid JSON issues (newlines in strings)
                try:
                    import re
                    # Extract spoken
                    s_match = re.search(r'"spoken"\s*:\s*"(.*?)"', clean_json, re.DOTALL)
                    if s_match:
                        spoken = s_match.group(1)
                    
                    # Extract written (greedy to catch multi-line content)
                    w_match = re.search(r'"written"\s*:\s*"(.*?)(?<!\\)"', clean_json, re.DOTALL)
                    if w_match:
                        written = w_match.group(1).replace("\\n", "\n") # Unescape manual newlines
                    else:
                         # Fallback for written if regex fails but we have cleaned string
                         written = clean_json
                except Exception as ex:
                     logger.error(f"Regex fallback failed: {ex}")
                     written = clean_json # Last resort: just show the cleaned text

            # Fallback if written summary is empty
            if not written.strip():
                written = f"Summary: {spoken.strip()}"

            logger.info(f"Spoken Summary: {spoken.strip()}")
            logger.info(f"πŸ“ WRITTEN SUMMARY:\\n{written.strip()}")
            logger.info(f"=" * 80)
            db.save_summary(contact_number, written.strip())
            
            # CRITICAL: Add summary LLM cost to total costs
            costs['llm'] += summary_cost
            costs['total'] += summary_cost
            
            # CRITICAL: Always return costs
            summary_result = {
                "text": written.strip(),
                "content": written.strip(),
                "spoken_text": spoken.strip(),
                "costs": costs,
                "status": "completed"
            }
            logger.info(f"πŸ“Š Summary with costs: {summary_result}")
            
            # Print prominently to CLI
            print(f"\\n{'='*80}")
            print(f"πŸ“‹ CALL SUMMARY GENERATED")
            print(f"{'='*80}")
            print(f"Contact: {contact_number}")
            print(f"Summary: {written.strip()}")
            print(f"Costs: STT=${costs['stt']:.4f} | TTS=${costs['tts']:.4f} | LLM=${costs['llm']:.6f} | Total=${costs['total']:.4f}")
            print(f"{'='*80}\\n")
            
            return summary_result
        
        except Exception as e:
            logger.warning(f"Summary generation attempt {attempt+1} failed: {e}")
            if attempt < max_retries - 1:
                await asyncio.sleep(retry_delay * (2 ** attempt)) # Exponential backoff
            else:
                logger.error("All summary generation attempts failed.")
                return {
                    "text": "Call summary unavailable.",
                    "content": "Call summary unavailable.", 
                    "spoken_text": "Thank you for calling. Have a great day!",
                    "costs": costs,
                    "status": "failed"
                }
    

def prewarm(proc: JobProcess):
    """Prewarm worker to reduce cold start latency"""
    from logger import logger as struct_logger
    from db import Database
    
    struct_logger.info("Prewarming worker...")
    
    try:
        # 1. Initialize database connection
        db = Database()
        proc.userdata["db"] = db
        struct_logger.info("βœ… Database connection prewarmed")
        
        # 2. Load VAD model into memory
        proc.userdata["vad"] = silero.VAD.load()
        struct_logger.info("βœ… VAD model prewarmed")
        
        # 3. Cache available slots
        proc.userdata["slots"] = db.get_available_slots()
        struct_logger.info("βœ… Appointment slots cached")
        
        # 4. Initialize cache connection
        from cache import cache
        proc.userdata["cache"] = cache
        struct_logger.info(f"βœ… Redis cache prewarmed (enabled: {cache.enabled})")
        
        struct_logger.info("πŸš€ Worker prewarmed successfully - ready for calls!")
        
    except Exception as e:
        struct_logger.error(f"Prewarming failed: {e}", error=str(e))

async def entrypoint(ctx: JobContext):
    # 1. Connect immediately to acknowledge assignment (Fixes AssignmentTimeoutError)
    await ctx.connect(auto_subscribe=AutoSubscribe.AUDIO_ONLY)
    
    # 2. Retrieve prewarmed resources or initialize if missing
    if "db" in ctx.proc.userdata:
        db = ctx.proc.userdata["db"]
        logger.info("Using prewarmed Database connection")
    else:
        db = Database()
        logger.info("Initialized new Database connection")

    user_context = {}
    participant = await ctx.wait_for_participant()

    avatar_type = '3d'
    user_tts_pref = None
    if participant.metadata:
        try:
            metadata = json.loads(participant.metadata)
            avatar_type = metadata.get('avatarType', '3d')
            user_tts_pref = metadata.get('ttsProvider')
        except Exception as e:
            logger.warning(f"Failed to parse participant metadata: {e}")
    
    logger.info(f"Avatar type requested by {participant.identity}: {avatar_type}")

    # TTS Provider Selection (Feature Flag via Flagsmith)
    tts_provider = os.getenv("TTS_PROVIDER", "deepgram") # Default fallback from ENV
    try:
        flags = flagsmith.get_environment_flags()
        # Flagsmith Python SDK v3+ uses get_feature_value() not get_flag_value()
        tts_provider_flag = flags.get_feature_value("tts_provider")
        if tts_provider_flag:
            tts_provider = tts_provider_flag
        logger.info(f"Flagsmith: tts_provider={tts_provider}")
    except Exception as e:
        logger.warning(f"Failed to fetch feature flags from Flagsmith: {e}. Using default: {tts_provider}")

    if tts_provider == "cartesia":
        logger.info("Using Cartesia TTS")
        agent_tts = cartesia.TTS()
    elif tts_provider == "groq":
        logger.info("Using Groq TTS")
        agent_tts = groq.TTS(model="canopylabs/orpheus-v1-english")
    else:
        logger.info("Using Deepgram TTS (Default)")
        agent_tts = deepgram.TTS()

    # Initialize metrics collector
    usage_collector = metrics.UsageCollector()

    # Initialize the AgentSession with a faster model and optimized VAD
    session = AgentSession(
        stt=deepgram.STT(),
        llm=groq.LLM(
            model="openai/gpt-oss-120b",
            api_key=get_groq_api_key(),
            temperature=0.5,
        ),
        tts=agent_tts,
        vad=silero.VAD.load(
            min_speech_duration=0.1,
            min_silence_duration=0.5, # Prevents cutting off mid-sentence
            prefix_padding_duration=0.2,     # Fixed deprecated argument
        ),
    )

    @session.on("metrics_collected")
    def _on_metrics_collected(ev: MetricsCollectedEvent):
        # logger.info(f"Metrics collected: {ev.metrics}")
        usage_collector.collect(ev.metrics)

    assistant = Assistant(db, user_context, ctx.room)
    start_time = datetime.now()
    assistant.usage_collector = usage_collector
    assistant.assistant = assistant
    assistant.avatar_type = avatar_type
    assistant.tts_provider = tts_provider

    @session.on("agent_speech_stopped")
    def _on_agent_speech_stopped(ev: Any = None):
        """Disconnect if the agent has finished speaking and a disconnect was requested."""
        if assistant.should_disconnect:
            async def _disconnect_sequence():
                logger.info("Agent finished speaking. Sending close_session event then closing room.")
                try:
                    payload = json.dumps({"type": "close_session"})
                    await ctx.room.local_participant.publish_data(payload, reliable=True)
                    logger.info("close_session event sent to frontend")
                except Exception as e:
                    logger.warning(f"Failed to publish close_session: {e}")
                
                # Give frontend time to process the event and disconnect gracefully
                await asyncio.sleep(2.0)
                
                # Only force disconnect if still connected
                if ctx.room.connection_state == "connected":
                    logger.info("Frontend didn't disconnect, forcing disconnect")
                    await ctx.room.disconnect()
                else:
                    logger.info("Frontend disconnected gracefully")

            asyncio.create_task(_disconnect_sequence())

    @session.on("agent_speech_interrupted")
    def _on_agent_speech_interrupted(ev: Any = None):
        """Handle case where agent summary/goodbye is interrupted by noise/user."""
        if assistant.should_disconnect:
            logger.info("Agent speech interrupted during disconnect phase. Triggering disconnect sequence.")
            # Reuse the same disconnect logic
            _on_agent_speech_stopped(ev)

    @session.on("agent_speech_started")
    def _on_agent_speech_started(ev: Any = None):
        logger.info("Agent speech STARTED.")


    await session.start(room=ctx.room, agent=assistant)
    
    
    # NOTE: Session ready signal will be sent after avatar + greeting (line ~1051)
    # This ensures UI doesn't show 'Ready' before system is actually ready
    # NOTE: Moved session_ready to after avatar + greeting (line ~1051)
    # This ensures UI doesn't show 'Ready' before system is actually ready


    # If Beyond Presence avatar is requested and available, initialize it
    if avatar_type == 'bey' and BEY_AVAILABLE:
        logger.info("Initializing Beyond Presence avatar...")
        
        # Send initializing signal repeatedly to ensure frontend gets it
        # (Data channel might not be fully established for 'User' yet)
        async def send_init_signal():
            for _ in range(5):
                try:
                    await ctx.room.local_participant.publish_data(
                        json.dumps({"type": "avatar_initializing"}), 
                        reliable=True
                    )
                except: pass
                await asyncio.sleep(0.5)
        
        asyncio.create_task(send_init_signal())
            
        try:
            bey_session = bey.AvatarSession(
                api_key=os.environ.get("BEYOND_PRESENCE_API_KEY"),
                avatar_id=os.environ.get("BEYOND_PRESENCE_AVATAR_ID", "b9be11b8-89fb-4227-8f86-4a881393cbdb"), 
            )
            await bey_session.start(session, room=ctx.room)
            logger.info("Beyond Presence avatar started successfully (API level)")
            
            # Wait for the avatar participant to actually join the room and publish tracks
            # This ensures we don't greet while the user is still looking at a loading screen
            logger.info("Waiting for avatar participant to join room...")
            avatar_joined = False
            for _ in range(40): # Wait up to 40 seconds
                # Check if avatar is in remote participants
                # Note: identity might vary setup, but usually 'bey-avatar-agent' or similar
                # We check for ANY new participant that looks like an avatar if specific ID fails?
                # For now assume 'bey-avatar-agent'
                p = ctx.room.remote_participants.get("bey-avatar-agent")
                if p:
                    # Check if they have video track
                    video_tracks = [t for t in p.track_publications.values() if t.kind == rtc.TrackKind.KIND_VIDEO]
                    if video_tracks:
                        logger.info("βœ… Avatar participant joined and video track found!")
                        avatar_joined = True
                        break
                await asyncio.sleep(1)
            
            if not avatar_joined:
                logger.warning("Timed out waiting for avatar participant to join - proceeding anyway")
                
        except Exception as e:
            logger.error(f"Failed to start Beyond Presence avatar: {e}")
            logger.info("Falling back to audio-only mode")

    # Time-aware greeting
    hour = datetime.now(ZoneInfo("Asia/Kolkata")).hour
    if 5 <= hour < 12:
        greeting = "Good morning"
    elif 12 <= hour < 17:
        greeting = "Good afternoon"
    else:
        greeting = "Good evening"
        
    # Generate greeting ONLY if session is still active
    # (Beyond Presence avatar takes ~12s to init, user might disconnect)
    # Generate greeting
    # We use room connection state as the truth, since session._state might be internal/laggy
    # Check against the Enum value properly
    if ctx.room.connection_state == rtc.ConnectionState.CONN_CONNECTED:
        try:
            logger.info(f"Speaking greeting: {greeting}...")
            # Use .say() directly for instant response
            await session.say(
                f"{greeting}, thank you for calling SkyTask Clinic. May I have your phone number?", 
                allow_interruptions=True
            )
        except RuntimeError as e:
            logger.warning(f"Could not speak greeting - error: {e}")
    else:
        logger.warning("Session not running - skipping greeting (user may have disconnected)")

    # Always send session_ready if we reached here
    try:
        payload = json.dumps({"type": "session_ready"})
        await ctx.room.local_participant.publish_data(payload, reliable=True)
        logger.info("βœ… Session ready signal sent to frontend")
    except Exception as e:
        logger.warning(f"Failed to send session_ready: {e}")



    # CRITICAL: Keep the agent alive while connected
    while ctx.room.connection_state == "connected":
        await asyncio.sleep(1)

    contact_number = user_context.get("contact_number")
    if contact_number and not assistant.summary_generated:
         logger.info("Disconnect summary generation (backup)...")
         duration = (datetime.now() - start_time).total_seconds()
         user_name = user_context.get("user_name", "the patient")
         await generate_and_save_summary(db, assistant.chat_ctx, contact_number, duration, avatar_type, tts_provider, user_name)


def start_health_check_server():
    """Starts a simple HTTP server for health checks."""
    try:
        port = int(os.getenv("PORT", 8080))
        
        class HealthCheckHandler(http.server.BaseHTTPRequestHandler):
            def do_GET(self):
                if self.path == "/health" or self.path == "/":
                    self.send_response(200)
                    self.send_header("Content-type", "application/json")
                    self.end_headers()
                    self.wfile.write(b'{"status": "healthy"}')
                else:
                    self.send_response(404)
                    self.end_headers()
            
            def log_message(self, format, *args):
                pass  # Suppress logs to keep console clean

        # Allow reuse of address to prevent 'Address already in use' errors
        socketserver.TCPServer.allow_reuse_address = True
        
        httpd = socketserver.TCPServer(("", port), HealthCheckHandler)
        print(f"βœ… Health check server listening on port {port}")
        
        # Run in a daemon thread so it doesn't block program exit
        thread = threading.Thread(target=httpd.serve_forever, daemon=True)
        thread.start()
    except Exception as e:
        print(f"⚠️ Failed to start health check server: {e}")

if __name__ == "__main__":
    start_health_check_server()
    # HF Space has ~16GB RAM, so we can enable prewarming for faster response times
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint, prewarm_fnc=prewarm))

    # Low resource configuration (Render Free Tier 512MB RAM):
    # options = WorkerOptions(
    #    entrypoint_fnc=entrypoint,
    #    prewarm_fnc=None,  # Disable prewarming to save RAM
    #    num_idle_processes=0, # Do not keep any processes waiting
    # )
    # cli.run_app(options)