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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Dict, Any, Optional, List
from fastapi import Request, HTTPException
from core.utils.logger import logger
from core.services.supabase import DBConnection
from core.billing.shared.config import TOKEN_PRICE_MULTIPLIER
from core.config.vapi_config import vapi_config
from decimal import Decimal
import json
import hmac
import hashlib

class VapiWebhookHandler:

    def __init__(self):
        self.db = DBConnection()
    
    async def verify_signature(self, request: Request, secret: Optional[str] = None) -> bool:
        if not secret:
            return True
        
        try:
            signature = request.headers.get("x-vapi-signature")
            if not signature:
                return False
            
            body = await request.body()
            expected_signature = hmac.new(
                secret.encode(),
                body,
                hashlib.sha256
            ).hexdigest()
            return hmac.compare_digest(signature, expected_signature)
        except Exception as e:
            logger.error(f"Error verifying webhook signature: {e}")
            return False
    
    def _extract_call_id(self, payload: Dict[str, Any]) -> Optional[str]:
        call = payload.get("call", {})
        if call and call.get("id"):
            return call.get("id")
        
        message = payload.get("message", {})
        if message:
            message_call = message.get("call", {})
            if isinstance(message_call, dict) and message_call.get("id"):
                return message_call.get("id")
            
            artifact = message.get("artifact", {})
            artifact_call = artifact.get("call", {})
            if isinstance(artifact_call, dict) and artifact_call.get("id"):
                return artifact_call.get("id")
        
        return payload.get("callId") or payload.get("call_id")
    
    def _extract_call_data(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call = payload.get("call", {})
        if call:
            return call
        
        message = payload.get("message", {})
        if message:
            message_call = message.get("call", {})
            if isinstance(message_call, dict):
                return message_call
            
            artifact = message.get("artifact", {})
            artifact_call = artifact.get("call", {})
            if isinstance(artifact_call, dict):
                return artifact_call
        
        return {}
    
    async def handle_webhook(self, event_type: str, payload: Dict[str, Any]) -> Dict[str, Any]:
        try:
            handlers = {
                "conversation-update": self._handle_conversation_update,
                "status-update": self._handle_status_update,
                "end-of-call-report": self._handle_end_of_call_report,
                "call.started": self._handle_call_started,
                "call.ended": self._handle_call_ended,
                "transcript.updated": self._handle_transcript_updated,
                "assistant-request": self._handle_assistant_request,
                "speech-update": lambda p: {"status": "ok"}
            }
            
            handler = handlers.get(event_type)
            if handler:
                if event_type == "speech-update":
                    return handler(payload)
                return await handler(payload)
            else:
                return {"status": "unhandled"}
        
        except Exception as e:
            logger.error(f"Error handling webhook event {event_type}: {e}")
            raise HTTPException(status_code=500, detail=str(e))
    
    async def _handle_conversation_update(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        from core.agentpress.thread_manager import ThreadManager
        
        call_id = self._extract_call_id(payload)
        call = self._extract_call_data(payload)
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        client = await self.db.client
        
        message_data = payload.get("message", {})
        artifact = message_data.get("artifact", {})
        messages = artifact.get("messages", [])
        conversation = message_data.get("conversation", [])
        
        transcript_data = self._process_messages(messages) or self._process_conversation(conversation)
        
        status = "in-progress" if transcript_data else call.get("status", "in-progress")
        
        update_data = {
            "transcript": transcript_data,
            "status": status,
            "updated_at": "now()"
        }
        
        try:
            result = await client.table("vapi_calls")\
                .select("*")\
                .eq("call_id", call_id)\
                .execute()
            
            if result.data:
                await client.table("vapi_calls")\
                    .update(update_data)\
                    .eq("call_id", call_id)\
                    .execute()
            else:
                new_call = {
                    "call_id": call_id,
                    "phone_number": call.get("customer", {}).get("number"),
                    "direction": "outbound" if call.get("type") == "outboundPhoneCall" else "inbound",
                    "status": status,
                    "transcript": transcript_data,
                    "started_at": call.get("createdAt")
                }
                await client.table("vapi_calls").insert(new_call).execute()
        
        except Exception as e:
            logger.error(f"Database operation failed for call {call_id}: {e}")
            return {"status": "error", "message": str(e)}
        
        if transcript_data:
            await self._stream_transcript_to_thread(call_id, transcript_data)
        
        return {"status": "success"}
    
    def _process_messages(self, messages: List[Dict]) -> List[Dict]:
        transcript = []
        for msg in messages:
            role = msg.get("role", "")
            if role in ["bot", "assistant", "user"] and msg.get("message"):
                transcript.append({
                    "role": "assistant" if role in ["bot", "assistant"] else "user",
                    "message": msg.get("message", ""),
                    "timestamp": msg.get("time")
                })
        return transcript
    
    def _process_conversation(self, conversation: List[Dict]) -> List[Dict]:
        transcript = []
        for msg in conversation:
            role = msg.get("role", "")
            if role in ["assistant", "bot", "user"] and msg.get("content"):
                transcript.append({
                    "role": "assistant" if role in ["assistant", "bot"] else "user",
                    "message": msg.get("content", ""),
                    "timestamp": None
                })
        return transcript
    
    async def _handle_status_update(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call_id = self._extract_call_id(payload)
        call = self._extract_call_data(payload)
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        message_data = payload.get("message", {})
        status = message_data.get("status") or call.get("status") or payload.get("status") or ""
        
        client = await self.db.client
        
        update_data = {"status": status}
        
        if status in ["completed", "ended", "failed", "no-answer", "busy", "cancelled"]:
            update_data["ended_at"] = message_data.get("timestamp") or call.get("endedAt") or "now()"
        
        await client.table("vapi_calls").update(update_data).eq("call_id", call_id).execute()
        
        return {"status": "success"}
    
    async def _handle_end_of_call_report(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call_id = self._extract_call_id(payload)
        call = self._extract_call_data(payload)
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        client = await self.db.client
        message_data = payload.get("message", {})
        
        artifact = message_data.get("artifact", {})
        messages = artifact.get("messages", []) or message_data.get("messages", [])
        
        transcript_data = self._process_messages(messages)
        
        if not transcript_data and message_data.get("transcript"):
            transcript_data = self._parse_transcript_text(message_data.get("transcript", ""))
        
        duration = message_data.get("durationSeconds", 0)
        cost = message_data.get("cost", 0)
        
        thread_id = await self._get_thread_id_for_call(call_id)
        user_id = None
        credit_deducted = False
        actual_cost_deducted = 0
        
        if thread_id:
            user_id = await self._get_user_id_from_thread(thread_id)
            
            if user_id and cost > 0:
                try:
                    from core.billing.credits.manager import credit_manager
                    cost_in_credits = Decimal(str(cost)) * TOKEN_PRICE_MULTIPLIER
                    
                    deduct_result = await credit_manager.use_credits(
                        account_id=user_id,
                        amount=cost_in_credits,
                        description=f"Vapi voice call ({duration}s)",
                        thread_id=thread_id
                    )
                    
                    if deduct_result.get('success'):
                        credit_deducted = True
                        actual_cost_deducted = float(cost_in_credits)
                        logger.info(f"Successfully deducted {cost_in_credits} credits for call {call_id}")
                    else:
                        logger.warning(f"Failed to deduct credits for call {call_id}: {deduct_result.get('error', 'Unknown error')}")
                except Exception as e:
                    logger.error(f"Error deducting credits for call {call_id}: {e}")
        
        update_data = {
            "status": "completed",
            "duration_seconds": int(duration) if duration else None,
            "transcript": transcript_data,
            "started_at": message_data.get("startedAt"),
            "ended_at": message_data.get("endedAt"),
            "cost": actual_cost_deducted if credit_deducted else cost,
            "updated_at": "now()"
        }
        
        try:
            result = await client.table("vapi_calls").update(update_data).eq("call_id", call_id).execute()
            
            if not result.data:
                new_call = {
                    "call_id": call_id,
                    "phone_number": call.get("customer", {}).get("number"),
                    "direction": "outbound" if call.get("type") == "outboundPhoneCall" else "inbound",
                    **update_data
                }
                await client.table("vapi_calls").insert(new_call).execute()
        except Exception as e:
            logger.error(f"Failed to update call {call_id}: {e}")
        
        await self._notify_call_completion(call_id, duration, actual_cost_deducted if credit_deducted else cost)
        
        return {"status": "success"}
    
    def _parse_transcript_text(self, transcript_text: str) -> List[Dict]:
        transcript_data = []
        for line in transcript_text.split('\n'):
            if line.startswith("AI:"):
                transcript_data.append({
                    "role": "assistant",
                    "message": line.replace("AI:", "").strip()
                })
            elif line.startswith("User:"):
                transcript_data.append({
                    "role": "user",
                    "message": line.replace("User:", "").strip()
                })
        return transcript_data
    
    async def _notify_call_completion(self, call_id: str, duration: int, cost: float) -> None:
        thread_id = await self._get_thread_id_for_call(call_id)
        if not thread_id:
            return
        
        from core.agentpress.thread_manager import ThreadManager
        thread_manager = ThreadManager()
        
        duration_text = f"{duration}s" if duration else "unknown"
        cost_text = f"${cost:.4f}" if cost and cost > 0 else "$0.00"
        
        await thread_manager.add_message(
            thread_id=thread_id,
            type="assistant",
            content=f"📞 **Call Completed** - Duration: {duration_text}, Credits Used: {cost_text}",
            is_llm_message=False,
            metadata={
                "call_id": call_id,
                "type": "call_completed",
                "duration": duration,
                "cost": cost,
                "credits_deducted": cost,
                "source": "vapi_webhook"
            }
        )
    
    async def _handle_call_started(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call_id = self._extract_call_id(payload)
        call = self._extract_call_data(payload)
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        client = await self.db.client
        
        update_data = {
            "status": "in-progress",
            "started_at": call.get("startedAt") or call.get("createdAt")
        }
        
        result = await client.table("vapi_calls").update(update_data).eq("call_id", call_id).execute()
        
        if not result.data:
            new_call = {
                "call_id": call_id,
                "phone_number": call.get("customer", {}).get("number"),
                "direction": "inbound" if call.get("type") == "inboundPhoneCall" else "outbound",
                "status": "in-progress",
                "started_at": call.get("startedAt"),
                "transcript": []
            }
            await client.table("vapi_calls").insert(new_call).execute()
        
        await self._notify_call_started(call_id, call.get("customer", {}).get("number", "Unknown"))
        
        return {"status": "success"}
    
    async def _notify_call_started(self, call_id: str, phone_number: str) -> None:
        thread_id = await self._get_thread_id_for_call(call_id)
        if not thread_id:
            return
        
        from core.agentpress.thread_manager import ThreadManager
        thread_manager = ThreadManager()
        
        await thread_manager.add_message(
            thread_id=thread_id,
            type="assistant",
            content=f"📞 **Call Started** - Connecting to {phone_number}...",
            is_llm_message=False,
            metadata={
                "call_id": call_id,
                "type": "call_started",
                "phone_number": phone_number,
                "source": "vapi_webhook"
            }
        )
    
    async def _handle_call_ended(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call_id = self._extract_call_id(payload)
        call = self._extract_call_data(payload)
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        client = await self.db.client
        
        transcript_data = call.get("transcript", [])
        duration = call.get("duration") or call.get("durationSeconds")
        cost = call.get("cost", 0)
        
        thread_id = await self._get_thread_id_for_call(call_id)
        user_id = None
        credit_deducted = False
        actual_cost_deducted = 0
        
        if thread_id:
            user_id = await self._get_user_id_from_thread(thread_id)
            
            if user_id and cost > 0:
                try:
                    from core.billing.credits.manager import CreditManager
                    # credit_manager instance is already available from the import
                    
                    cost_in_credits = Decimal(str(cost)) * TOKEN_PRICE_MULTIPLIER
                    
                    from core.billing.credits.manager import credit_manager
                    deduct_result = await credit_manager.use_credits(
                        account_id=user_id,
                        amount=cost_in_credits,
                        description=f"Vapi voice call ({duration}s)",
                        thread_id=thread_id
                    )
                    
                    if deduct_result.get('success'):
                        credit_deducted = True
                        actual_cost_deducted = float(cost_in_credits)
                        logger.info(f"Successfully deducted {cost_in_credits} credits for call {call_id}")
                    else:
                        logger.warning(f"Failed to deduct credits for call {call_id}: {deduct_result.get('error', 'Unknown error')}")
                except Exception as e:
                    logger.error(f"Error deducting credits for call {call_id}: {e}")
        
        update_data = {
            "status": "ended",
            "ended_at": call.get("endedAt") or call.get("endTime") or "now()",
            "duration_seconds": int(duration) if duration else None,
            "transcript": transcript_data if transcript_data else None,
            "cost": actual_cost_deducted if credit_deducted else cost
        }
        
        await client.table("vapi_calls").update(update_data).eq("call_id", call_id).execute()
        
        if thread_id and transcript_data:
            await self._save_transcript_to_thread(thread_id, call_id, transcript_data)
        
        return {"status": "success"}
    
    async def _handle_transcript_updated(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        call_id = self._extract_call_id(payload)
        transcript = payload.get("transcript", [])
        
        if not call_id:
            return {"status": "error", "message": "Missing call ID"}
        
        client = await self.db.client
        await client.table("vapi_calls").update({
            "transcript": json.dumps(transcript)
        }).eq("call_id", call_id).execute()
        
        return {"status": "success"}
    
    
    async def _handle_assistant_request(self, payload: Dict[str, Any]) -> Dict[str, Any]:
        return {
            "assistant": vapi_config.get_assistant_config()
        }
    
    async def _get_thread_id_for_call(self, call_id: str) -> Optional[str]:
        try:
            client = await self.db.client
            result = await client.table("vapi_calls").select("thread_id").eq("call_id", call_id).single().execute()
            return result.data.get("thread_id") if result.data else None
        except Exception as e:
            logger.error(f"Error getting thread_id for call {call_id}: {e}")
            return None
    
    async def _get_user_id_from_thread(self, thread_id: str) -> Optional[str]:
        try:
            client = await self.db.client
            result = await client.table("threads").select("account_id").eq("thread_id", thread_id).single().execute()
            return result.data.get("account_id") if result.data else None
        except Exception as e:
            logger.error(f"Error getting user_id for thread {thread_id}: {e}")
            return None
    
    async def _stream_transcript_to_thread(self, call_id: str, transcript_data: List[Dict[str, Any]]) -> None:
        try:
            thread_id = await self._get_thread_id_for_call(call_id)
            if not thread_id:
                return
            
            client = await self.db.client
            existing_result = await client.table("messages").select("metadata").eq("thread_id", thread_id).execute()
            
            existing_indices = set()
            if existing_result.data:
                for msg in existing_result.data:
                    metadata = msg.get("metadata", {})
                    if (metadata.get("call_id") == call_id and 
                        metadata.get("message_index") is not None):
                        existing_indices.add(metadata["message_index"])
            
            from core.agentpress.thread_manager import ThreadManager
            thread_manager = ThreadManager()
            
            for idx, msg in enumerate(transcript_data):
                if idx not in existing_indices:
                    role = msg.get("role", "")
                    message_text = msg.get("message", "")
                    
                    if not message_text.strip() or role == "system":
                        continue
                    
                    formatted_content = (
                        f"🤖 **AI Assistant**: {message_text}"
                        if role == "assistant"
                        else f"👤 **Caller**: {message_text}"
                    )
                    
                    await thread_manager.add_message(
                        thread_id=thread_id,
                        type="assistant",
                        content=formatted_content,
                        is_llm_message=False,
                        metadata={
                            "call_id": call_id,
                            "message_index": idx,
                            "role": role,
                            "timestamp": msg.get("timestamp"),
                            "is_realtime_transcript": True,
                            "source": "vapi_webhook"
                        }
                    )
        except Exception as e:
            logger.error(f"Failed to stream transcript to thread: {e}")
    
    async def _save_transcript_to_thread(self, thread_id: str, call_id: str, transcript: list) -> None:
        try:
            client = await self.db.client
            
            conversation_text = "\n".join([
                f"{msg.get('role', 'unknown')}: {msg.get('message', '')}"
                for msg in transcript
            ])
            
            message_data = {
                "thread_id": thread_id,
                "role": "assistant",
                "content": f"Call completed (ID: {call_id})\n\nTranscript:\n{conversation_text}",
                "metadata": {
                    "type": "voice_call_transcript",
                    "call_id": call_id
                }
            }
            
            await client.table("messages").insert(message_data).execute()
        except Exception as e:
            logger.error(f"Error saving transcript to thread: {e}")