-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
681 lines (563 loc) · 25.2 KB
/
Copy pathmain.py
File metadata and controls
681 lines (563 loc) · 25.2 KB
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
"""
FastAPI server for the dental rescheduling agent.
Combines Eddy's brain logic with Spencer's Claude orchestrator.
Endpoints:
- POST /cancellation → Triggers the Claude agent to start filling the slot
- POST /call-outcome → Receives webhook from ElevenLabs/Twilio voice
- POST /sms-reply → Receives webhook from Twilio SMS
- POST /reset → Resets demo state
- GET / → Health check
Run with:
uvicorn main:app --reload --port 8000
"""
import os
import asyncio
import uuid
from contextlib import asynccontextmanager
from dotenv import load_dotenv
from fastapi import FastAPI, BackgroundTasks, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import Response
from pydantic import BaseModel
from agent.firestore import init_firestore, reset_session
from agent.mock_data import DEMO_SLOT
from orchestrator import Orchestrator
load_dotenv()
# --- Orchestrator singleton ---
_orchestrator: Orchestrator | None = None
_session_id: str = "" # Tracks current session; old polling threads check this
def get_orchestrator() -> Orchestrator:
global _orchestrator
if _orchestrator is None:
api_key = os.environ.get("ANTHROPIC_API_KEY", "")
_orchestrator = Orchestrator(api_key)
# Wire up comms stubs — UI/Voice person replaces these
_orchestrator.on_voice_call = trigger_voice_call
_orchestrator.on_sms = send_sms
return _orchestrator
# --- App Setup ---
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Initialize Firebase on startup."""
import base64
import json
# Option 1: Base64-encoded service account (Railway)
sa_base64 = os.environ.get("FIREBASE_SERVICE_ACCOUNT_BASE64")
if sa_base64:
sa_dict = json.loads(base64.b64decode(sa_base64), strict=False)
init_firestore(sa_dict)
print("Firebase initialized from FIREBASE_SERVICE_ACCOUNT_BASE64")
else:
# Option 2: File path
sa_path = os.environ.get("GOOGLE_APPLICATION_CREDENTIALS", "serviceAccountKey.json")
if os.path.exists(sa_path):
init_firestore(sa_path)
print(f"Firebase initialized from {sa_path}")
else:
print("WARNING: No Firebase credentials found. Set FIREBASE_SERVICE_ACCOUNT_BASE64 or GOOGLE_APPLICATION_CREDENTIALS.")
yield
app = FastAPI(
title="Dental Rescheduling Agent",
description="AI agent that fills cancelled appointment slots",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --- Agent state ---
_is_running = False
# --- Pydantic Models ---
class CallOutcome(BaseModel):
"""Webhook payload when a voice call ends."""
patient_name: str
outcome: str # "confirmed", "declined", "no_answer"
class SMSReply(BaseModel):
"""Webhook payload when an SMS reply is received."""
patient_name: str
reply: str # The actual text message
# --- Endpoints ---
_last_error: str = ""
@app.get("/")
async def root():
"""Health check."""
return {"status": "ok", "agent_running": _is_running, "last_error": _last_error}
@app.post("/cancellation")
async def trigger_cancellation(background_tasks: BackgroundTasks):
"""Trigger the Claude agent to start filling a cancelled slot."""
global _is_running, _session_id
if _is_running:
return {"status": "already_running"}
_is_running = True
_session_id = uuid.uuid4().hex[:8]
background_tasks.add_task(run_orchestrator)
return {"status": "agent_started"}
@app.post("/call-outcome")
async def call_outcome(body: CallOutcome, background_tasks: BackgroundTasks):
"""Receive voice call outcome from ElevenLabs/Twilio webhook.
UI/Voice person's code POSTs here when a call ends.
"""
if not _is_running:
return {"status": "agent_not_running"}
# Map outcome string to candidate status
status_map = {
"confirmed": "confirmed",
"declined": "declined",
"no_answer": "no_answer",
}
new_status = status_map.get(body.outcome, "no_answer")
# Let the orchestrator handle it (Claude decides next steps)
background_tasks.add_task(
handle_outcome_async, body.patient_name, new_status
)
return {"status": "received", "patient": body.patient_name, "outcome": body.outcome}
@app.post("/sms-reply")
async def sms_reply(body: SMSReply, background_tasks: BackgroundTasks):
"""Receive SMS reply from Twilio webhook.
UI/Voice person's code POSTs here when a patient texts back.
"""
if not _is_running:
return {"status": "agent_not_running"}
# Parse reply
reply_lower = body.reply.lower().strip()
if reply_lower in ("yes", "y", "yeah", "sure", "ok", "okay"):
new_status = "confirmed"
elif reply_lower in ("no", "n", "nope", "can't", "cannot"):
new_status = "declined"
else:
new_status = "declined"
background_tasks.add_task(
handle_outcome_async, body.patient_name, new_status
)
return {"status": "received", "patient": body.patient_name, "reply": body.reply}
@app.post("/reset")
async def reset_demo():
"""Reset demo to initial state."""
global _is_running, _orchestrator, _session_id
_is_running = False
_session_id = "" # Invalidate old polling threads
if _orchestrator:
_orchestrator.cancelled = True # Stop old orchestrator's run_step loop
_orchestrator = None
_phone_to_patient.clear()
reset_session()
return {"status": "reset_complete"}
# --- Background tasks ---
async def run_orchestrator():
"""Start the Claude orchestrator in background."""
global _is_running, _last_error, _current_slot
try:
_current_slot = DEMO_SLOT.copy()
orch = get_orchestrator()
# run_step is sync (calls Anthropic API) — run in thread pool
await asyncio.to_thread(orch.start, DEMO_SLOT)
except Exception as e:
import traceback
_last_error = f"{str(e)}\n{traceback.format_exc()}"
print(f"Orchestrator error: {_last_error}")
try:
from agent.firestore import update_agent_status, add_activity, update_slot_status
update_agent_status("failed")
update_slot_status("exhausted")
add_activity("error", f"Agent error: {str(e)[:100]}")
except Exception as fe:
print(f"Firestore error while reporting failure: {fe}")
_is_running = False
async def handle_outcome_async(patient_name: str, outcome: str):
"""Handle webhook outcome — re-invoke Claude to decide next steps."""
global _is_running
try:
orch = get_orchestrator()
await asyncio.to_thread(orch.handle_outcome, patient_name, outcome)
# Check if agent is done
if orch.candidates:
from agent.state import TERMINAL_CANDIDATE_STATUSES
all_terminal = all(
c["status"] in TERMINAL_CANDIDATE_STATUSES
for c in orch.candidates
)
any_confirmed = any(c["status"] == "confirmed" for c in orch.candidates)
if any_confirmed or all_terminal:
_is_running = False
except Exception as e:
print(f"Outcome handler error: {e}")
_is_running = False
# --- Twilio SMS ---
TWILIO_SID = os.environ.get("TWILIO_ACCOUNT_SID", "")
TWILIO_AUTH = os.environ.get("TWILIO_AUTH_TOKEN", "")
TWILIO_PHONE = os.environ.get("TWILIO_PHONE_NUMBER", "") # voice calls
TWILIO_SMS_PHONE = os.environ.get("TWILIO_SMS_NUMBER", "") or TWILIO_PHONE # toll-free for SMS
TWILIO_MESSAGING_SERVICE_SID = os.environ.get("TWILIO_MESSAGING_SERVICE_SID", "")
# Phone-to-patient lookup (populated when we send SMS)
_phone_to_patient: dict[str, str] = {}
_twilio_client = None
def get_twilio():
global _twilio_client
if _twilio_client is None and TWILIO_SID and TWILIO_AUTH:
from twilio.rest import Client
_twilio_client = Client(TWILIO_SID, TWILIO_AUTH)
return _twilio_client
def send_sms(patient: dict, slot: dict):
"""Send SMS via Twilio to the patient about the open slot."""
client = get_twilio()
if not client:
print(f"[NO TWILIO] Would SMS {patient['name']} at {patient['phone']}")
return
body = (
f"Hi {patient['name'].split()[0]}, this is your dental office. "
f"We have an opening at {slot.get('time', 'today')} for a "
f"{slot.get('treatment', 'cleaning')}. Would you like to book it? "
f"Reply YES or NO."
)
# Try messaging service first, fall back to direct number
try:
if TWILIO_MESSAGING_SERVICE_SID:
msg = client.messages.create(
body=body,
messaging_service_sid=TWILIO_MESSAGING_SERVICE_SID,
to=patient["phone"],
)
else:
msg = client.messages.create(
body=body,
from_=TWILIO_SMS_PHONE,
to=patient["phone"],
)
except Exception as sms_err:
print(f"[TWILIO] SMS error: {sms_err}")
# Try the local number as last resort
try:
msg = client.messages.create(
body=body,
from_=TWILIO_PHONE,
to=patient["phone"],
)
except Exception as e2:
print(f"[TWILIO] All SMS attempts failed: {e2}")
return
# Track phone→patient mapping so we can match inbound replies
# Store both raw and E.164 format for matching
_phone_to_patient[patient["phone"]] = patient["name"]
digits = ''.join(c for c in patient["phone"] if c.isdigit())[-10:]
_phone_to_patient[f"+1{digits}"] = patient["name"]
print(f"[TWILIO] SMS sent to {patient['name']}: {msg.sid}")
@app.post("/webhooks/twilio-sms")
async def twilio_sms_webhook(request: Request):
"""Twilio inbound SMS webhook.
Configure in Twilio Console:
Phone Numbers → your number → Messaging →
'A MESSAGE COMES IN' → Webhook URL:
https://dental-agent-production.up.railway.app/webhooks/twilio-sms (POST)
"""
form = await request.form()
from_number = str(form.get("From", ""))
body_text = str(form.get("Body", ""))
print(f"[TWILIO INBOUND] From={from_number} Body='{body_text}'")
# Look up patient by phone number — check in-memory map first
patient_name = _phone_to_patient.get(from_number, "")
# Fuzzy match: strip formatting and compare last 10 digits
if not patient_name:
from_digits = ''.join(c for c in from_number if c.isdigit())[-10:]
for phone, name in _phone_to_patient.items():
phone_digits = ''.join(c for c in phone if c.isdigit())[-10:]
if from_digits == phone_digits:
patient_name = name
break
# If still no match, check orchestrator's candidates directly
if not patient_name and _orchestrator and _orchestrator.candidates:
from_digits = ''.join(c for c in from_number if c.isdigit())[-10:]
for c in _orchestrator.candidates:
c_digits = ''.join(ch for ch in c.get("phone", "") if ch.isdigit())[-10:]
if from_digits == c_digits:
patient_name = c["name"]
_phone_to_patient[from_number] = patient_name
break
# Parse yes/no
reply_lower = body_text.lower().strip()
if reply_lower in ("yes", "y", "yeah", "sure", "ok", "okay", "yep", "absolutely", "yes please", "yea"):
new_status = "confirmed"
elif reply_lower in ("no", "n", "nope", "can't", "cannot", "no thanks", "no thank you"):
new_status = "declined"
else:
new_status = "declined"
if patient_name and _is_running:
print(f"[TWILIO] Matched: {patient_name} → {new_status}")
asyncio.create_task(handle_outcome_async(patient_name, new_status))
elif not patient_name:
print(f"[TWILIO] Could not match sender {from_number} to any patient")
# Twilio expects TwiML response
if patient_name and new_status == "confirmed":
twiml = '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Great! You\'re booked. We\'ll see you soon!</Message></Response>'
elif patient_name and new_status == "declined":
twiml = '<?xml version="1.0" encoding="UTF-8"?><Response><Message>No worries, thanks for letting us know!</Message></Response>'
elif not patient_name:
twiml = '<?xml version="1.0" encoding="UTF-8"?><Response><Message>Hi! This is the dental office. If you have questions, please call us during business hours.</Message></Response>'
else:
twiml = '<?xml version="1.0" encoding="UTF-8"?><Response></Response>'
return Response(content=twiml, media_type="application/xml")
@app.post("/webhooks/twilio-status")
async def twilio_status_callback(request: Request):
"""Twilio voice call status callback.
Fires when a call's status changes (initiated, ringing, answered, completed, etc.)
"""
form = await request.form()
call_status = str(form.get("CallStatus", ""))
call_sid = str(form.get("CallSid", ""))
to_number = str(form.get("To", ""))
duration = str(form.get("CallDuration", "0"))
print(f"[TWILIO STATUS] CallSid={call_sid} Status={call_status} To={to_number} Duration={duration}s")
# If call completed/failed/busy/no-answer, we can use this as a fallback
# for the ElevenLabs polling (in case polling misses it)
if call_status in ("completed", "failed", "busy", "no-answer", "canceled"):
# Look up patient by phone number
patient_name = ""
to_digits = ''.join(c for c in to_number if c.isdigit())[-10:]
for phone, name in _phone_to_patient.items():
phone_digits = ''.join(c for c in phone if c.isdigit())[-10:]
if to_digits == phone_digits:
patient_name = name
break
if patient_name and call_status in ("failed", "busy", "no-answer", "canceled"):
print(f"[TWILIO STATUS] Call to {patient_name} ended: {call_status} — posting no_answer")
if _is_running:
asyncio.create_task(handle_outcome_async(patient_name, "no_answer"))
return Response(content="", status_code=200)
# --- ElevenLabs Voice Calls ---
ELEVENLABS_API_KEY = os.environ.get("ELEVENLABS_API_KEY", "")
ELEVENLABS_AGENT_ID = os.environ.get("ELEVENLABS_AGENT_ID", "")
ELEVENLABS_PHONE_NUMBER_ID = os.environ.get("ELEVENLABS_PHONE_NUMBER_ID", "")
# Store the current slot globally so all calls can access it
_current_slot: dict = {}
def trigger_voice_call(patient: dict):
"""Trigger outbound voice call via ElevenLabs Conversational AI + Twilio."""
if not ELEVENLABS_API_KEY or not ELEVENLABS_AGENT_ID or not ELEVENLABS_PHONE_NUMBER_ID:
print(f"[NO ELEVENLABS] Would call {patient['name']} at {patient['phone']}")
print(f"[MOCK] Simulating 'no_answer' after 3 seconds...")
# Simulate a call outcome so the demo can continue
import threading
session = _session_id
def send_mock_outcome():
import time
time.sleep(3)
if _session_id == session:
_post_outcome(patient['name'], 'no_answer')
threading.Thread(target=send_mock_outcome, daemon=True).start()
return
import requests
# Get slot from global store or orchestrator
slot = _current_slot.copy()
if not slot and _orchestrator:
slot = getattr(_orchestrator, 'slot', {}) or {}
print(f"[ELEVENLABS] Calling {patient['name']} with slot={slot}")
resp = requests.post(
"https://api.elevenlabs.io/v1/convai/twilio/outbound-call",
headers={
"xi-api-key": ELEVENLABS_API_KEY,
"Content-Type": "application/json",
},
json={
"agent_id": ELEVENLABS_AGENT_ID,
"agent_phone_number_id": ELEVENLABS_PHONE_NUMBER_ID,
"to_number": patient["phone"],
"conversation_initiation_client_data": {
"dynamic_variables": {
"patient_name": patient["name"],
"patient_dob": patient.get("dob", "on file"),
"slot_time": slot.get("time", "today"),
"slot_treatment": slot.get("treatment", "cleaning"),
"slot_date": slot.get("date", "Today"),
}
},
},
)
if resp.ok:
data = resp.json()
conv_id = data.get("conversation_id", "")
print(f"[ELEVENLABS] Call initiated to {patient['name']}: {data}")
_phone_to_patient[patient["phone"]] = patient["name"]
# Poll for call outcome in background — pass session_id so stale threads stop
import threading
threading.Thread(
target=_poll_call_outcome,
args=(conv_id, patient["name"], _session_id),
daemon=True,
).start()
else:
print(f"[ELEVENLABS] Call failed: {resp.status_code} {resp.text}")
# Treat failed call as no_answer so orchestrator falls back to SMS
if _is_running:
import threading
threading.Thread(
target=lambda: _post_outcome(patient["name"], "no_answer"),
daemon=True,
).start()
def _parse_call_outcome(
call_successful: str,
transcript_summary: str,
full_transcript_text: str,
data_collection: dict,
) -> str:
"""Parse ElevenLabs call analysis into confirmed/declined/no_answer.
ElevenLabs' call_successful means the call *completed*, NOT that the
appointment was confirmed. We check EVERYTHING — summary, full transcript,
and data collection results — before deciding.
"""
# Combine summary + full transcript for comprehensive checking
all_text = f"{transcript_summary} {full_transcript_text}".lower()
print(f"[PARSE] call_successful={call_successful!r}")
print(f"[PARSE] all_text length={len(all_text)}")
print(f"[PARSE] data_collection={data_collection}")
# Normalize call_successful (ElevenLabs uses different formats)
cs = str(call_successful).lower().strip()
is_successful = cs in ("success", "true", "yes")
# --- REJECTION CHECKS (any match → NOT confirmed) ---
# 1. Voicemail
voicemail_phrases = [
"voicemail", "voice mail", "leave a message", "mailbox",
"after the beep", "after the tone", "not available to take",
"please leave", "no one answered", "went to voicemail",
"recording", "answering machine",
]
if any(phrase in all_text for phrase in voicemail_phrases):
print(f"[PARSE] → VOICEMAIL detected")
return "no_answer"
# 2. Wrong number
wrong_number_phrases = [
"wrong number", "wrong person", "not the right person",
"who is this", "you have the wrong", "no one by that name",
"doesn't live here", "not here", "don't know who",
"never heard of", "no such person", "not who you",
"i'm not", "i am not", "that's not me", "thats not me",
"you've got the wrong", "you got the wrong",
"i don't know any", "don't know any",
]
if any(phrase in all_text for phrase in wrong_number_phrases):
print(f"[PARSE] → WRONG NUMBER detected")
return "no_answer"
# 3. Explicit decline
decline_phrases = [
"can't make it", "cannot make it", "can't do", "cannot do",
"not available", "doesn't work", "won't work", "i'm busy",
"decline", "not interested", "no thank", "can't come",
"unable to", "that time doesn't", "that doesn't work",
"won't be able", "not going to work", "i can't",
"doesn't suit", "i have plans", "already have",
"refused", "said no", "patient declined", "they declined",
"did not confirm", "unable to confirm", "not confirm",
"did not accept", "not able to make", "no i",
"that won't", "that will not", "i will not",
"don't want", "do not want", "not for me",
]
if any(phrase in all_text for phrase in decline_phrases):
print(f"[PARSE] → DECLINE detected")
return "declined"
# 4. Check data_collection_results for structured confirmation/denial
for key, val in data_collection.items():
result = str(val.get("result", "") if isinstance(val, dict) else val).lower()
if "confirmed" in key.lower() or "accepted" in key.lower() or "booked" in key.lower():
if result in ("false", "failure", "no", "failed"):
print(f"[PARSE] → DECLINE from data_collection[{key}]={result}")
return "declined"
# --- CONFIRMATION CHECK (must pass ALL gates) ---
confirm_phrases = [
"confirmed the appointment", "appointment confirmed",
"agreed to come in", "patient accepted", "patient confirmed",
"they confirmed", "booked the appointment", "appointment booked",
"appointment was scheduled", "scheduled the appointment",
"accepted the appointment", "will attend",
]
has_strong_confirmation = any(phrase in all_text for phrase in confirm_phrases)
# Also check data_collection for structured confirmation
has_data_confirmation = False
for key, val in data_collection.items():
result = str(val.get("result", "") if isinstance(val, dict) else val).lower()
if "confirmed" in key.lower() or "accepted" in key.lower() or "booked" in key.lower():
if result in ("true", "success", "yes"):
has_data_confirmation = True
break
if is_successful and (has_strong_confirmation or has_data_confirmation):
print(f"[PARSE] → CONFIRMED (call_successful + strong confirmation evidence)")
return "confirmed"
elif is_successful:
# Call "succeeded" per ElevenLabs but no strong confirmation found.
# DO NOT auto-book. Fall through to SMS follow-up.
print(f"[PARSE] → NO_ANSWER (call_successful but no confirmation evidence in transcript)")
return "no_answer"
else:
print(f"[PARSE] → NO_ANSWER (call_successful={call_successful!r})")
return "no_answer"
def _poll_call_outcome(conversation_id: str, patient_name: str, session_id: str):
"""Poll ElevenLabs conversation status until the call ends, then feed outcome back."""
import time
import requests as req
print(f"[POLL] Watching conversation {conversation_id} for {patient_name} (session={session_id})")
for attempt in range(60): # poll for up to 5 minutes
time.sleep(5)
# Stop if session was reset (user hit reset)
if _session_id != session_id:
print(f"[POLL] Session changed ({session_id} → {_session_id}), stopping poll for {patient_name}")
return
try:
r = req.get(
f"https://api.elevenlabs.io/v1/convai/conversations/{conversation_id}",
headers={"xi-api-key": ELEVENLABS_API_KEY},
)
if not r.ok:
print(f"[POLL] Status check failed: {r.status_code}")
continue
data = r.json()
status = data.get("status", "")
if status not in ("done", "failed"):
continue
# Call is over — extract ALL available data for outcome parsing
analysis = data.get("analysis", {})
call_successful = analysis.get("call_successful", "")
transcript_summary = analysis.get("transcript_summary", "")
data_collection = analysis.get("data_collection_results", {})
# Get FULL transcript text (not just summary)
transcript_entries = data.get("transcript", [])
full_transcript_text = " ".join(
entry.get("message", "")
for entry in transcript_entries
if isinstance(entry, dict)
)
print(f"[POLL] Call ended: status={status}, successful={call_successful}")
print(f"[POLL] Summary: {transcript_summary}")
print(f"[POLL] Full transcript ({len(transcript_entries)} entries): {full_transcript_text[:500]}")
print(f"[POLL] Data collection: {data_collection}")
outcome = _parse_call_outcome(
call_successful, transcript_summary, full_transcript_text, data_collection
)
print(f"[POLL] Final parsed outcome: {outcome}")
# Only post if session is still current
if _session_id == session_id:
_post_outcome(patient_name, outcome)
else:
print(f"[POLL] Session stale, discarding outcome for {patient_name}")
return
except Exception as e:
print(f"[POLL] Error: {e}")
# Timeout — treat as no_answer
print(f"[POLL] Timeout waiting for {patient_name}")
if _session_id == session_id:
_post_outcome(patient_name, "no_answer")
def _post_outcome(patient_name: str, outcome: str):
"""Post call outcome back to our own /call-outcome endpoint."""
import requests as req
# Use localhost when running locally, deployed URL otherwise
server_url = os.environ.get("SERVER_URL", "http://localhost:8000")
try:
r = req.post(
f"{server_url}/call-outcome",
json={"patient_name": patient_name, "outcome": outcome},
)
print(f"[POLL] Posted outcome: {patient_name} → {outcome} (status={r.status_code})")
except Exception as e:
print(f"[POLL] Failed to post outcome: {e}")
# --- Run directly ---
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)