Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
566 lines (445 loc) · 21.5 KB
/
Copy pathserver.py
File metadata and controls
566 lines (445 loc) · 21.5 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
"""FastAPI backend for the OutboundAI dashboard."""
import asyncio
import json
import logging
import os
import random
import ssl
import certifi
import aiohttp
from pathlib import Path
from typing import Optional
from dotenv import load_dotenv
from fastapi import FastAPI, HTTPException, Query, Request
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel
_orig_ssl = ssl.create_default_context
def _certifi_ssl(purpose=ssl.Purpose.SERVER_AUTH, **kwargs):
if not kwargs.get("cafile") and not kwargs.get("capath") and not kwargs.get("cadata"):
kwargs["cafile"] = certifi.where()
return _orig_ssl(purpose, **kwargs)
ssl.create_default_context = _certifi_ssl
from db import (
SENSITIVE_KEYS, cancel_appointment, clear_errors, create_campaign, delete_campaign,
get_all_appointments, get_all_calls, get_all_campaigns, get_all_settings,
get_all_agent_profiles, get_agent_profile, create_agent_profile, update_agent_profile,
delete_agent_profile, set_default_agent_profile, get_calls_by_phone, get_campaign,
get_contacts, get_errors, get_logs, get_setting, get_stats, init_db, log_error,
save_settings, set_setting, update_call_notes, update_campaign_run_stats, update_campaign_status,
)
from prompts import DEFAULT_SYSTEM_PROMPT
load_dotenv(".env", override=True)
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("server")
init_db()
try:
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
_scheduler = AsyncIOScheduler()
except ImportError:
_scheduler = None
logger.warning("APScheduler not installed — campaign scheduling disabled")
app = FastAPI(title="OutboundAI Dashboard", version="1.0.0")
@app.on_event("startup")
async def _startup():
if _scheduler:
_scheduler.start()
await _reschedule_all_campaigns()
@app.on_event("shutdown")
async def _shutdown():
if _scheduler and _scheduler.running:
_scheduler.shutdown(wait=False)
async def eff(key: str) -> str:
val = await get_setting(key, "")
return val if val else os.getenv(key, "")
# ── Request models ────────────────────────────────────────────────────────────
class CallRequest(BaseModel):
phone: str
lead_name: str = "there"
business_name: str = "our company"
service_type: str = "our service"
system_prompt: Optional[str] = None
agent_profile_id: Optional[str] = None
class AgentProfileRequest(BaseModel):
name: str
voice: str = "Aoede"
model: str = "gemini-3.1-flash-live-preview"
system_prompt: Optional[str] = None
enabled_tools: str = "[]"
is_default: bool = False
class PromptRequest(BaseModel):
prompt: str
class SettingsRequest(BaseModel):
settings: dict
class NotesRequest(BaseModel):
notes: str
class CampaignRequest(BaseModel):
name: str
contacts: list
schedule_type: str = "once"
schedule_time: str = "09:00"
call_delay_seconds: int = 3
system_prompt: Optional[str] = None
agent_profile_id: Optional[str] = None
class StatusRequest(BaseModel):
status: str
# ── Dashboard ─────────────────────────────────────────────────────────────────
@app.get("/", response_class=HTMLResponse)
async def serve_dashboard():
html_path = Path(__file__).parent / "ui" / "index.html"
if html_path.exists():
return HTMLResponse(content=html_path.read_text(encoding="utf-8"))
return HTMLResponse("<h1>Dashboard not found — place index.html in ui/</h1>", status_code=404)
# ── Call dispatch ─────────────────────────────────────────────────────────────
@app.post("/api/call")
async def api_dispatch_call(req: CallRequest):
url = await eff("LIVEKIT_URL")
key = await eff("LIVEKIT_API_KEY")
secret = await eff("LIVEKIT_API_SECRET")
if not all([url, key, secret]):
raise HTTPException(400, "LiveKit credentials not configured. Go to Settings → LiveKit.")
phone = req.phone.strip()
if not phone.startswith("+"):
raise HTTPException(400, "Phone must be in E.164 format: +919876543210")
effective_prompt = req.system_prompt
effective_voice = None
effective_model = None
effective_tools = None
if req.agent_profile_id:
profile = await get_agent_profile(req.agent_profile_id)
if profile:
if not effective_prompt and profile.get("system_prompt"):
effective_prompt = profile["system_prompt"]
effective_voice = profile.get("voice")
effective_model = profile.get("model")
effective_tools = profile.get("enabled_tools")
if not effective_prompt:
effective_prompt = await get_setting("system_prompt", "") or None
room_name = f"call-{phone.replace('+', '')}-{random.randint(1000, 9999)}"
metadata: dict = {
"phone_number": phone,
"lead_name": req.lead_name,
"business_name": req.business_name,
"service_type": req.service_type,
"system_prompt": effective_prompt,
}
if effective_voice: metadata["voice_override"] = effective_voice
if effective_model: metadata["model_override"] = effective_model
if effective_tools: metadata["tools_override"] = effective_tools
try:
from livekit import api as lk_api
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ctx))
lk = lk_api.LiveKitAPI(url=url, api_key=key, api_secret=secret, session=session)
await lk.room.create_room(lk_api.CreateRoomRequest(name=room_name, empty_timeout=300, max_participants=5))
await lk.agent_dispatch.create_dispatch(
lk_api.CreateAgentDispatchRequest(
agent_name="outbound-caller", room=room_name, metadata=json.dumps(metadata)
)
)
await lk.aclose()
await session.close()
await log_error("server", f"Call dispatched to {phone}", f"room={room_name}", "info")
return {"status": "dispatched", "room": room_name, "phone": phone}
except Exception as exc:
logger.error("Dispatch error: %s", exc)
raise HTTPException(500, f"Dispatch failed: {exc}")
# ── Calls ─────────────────────────────────────────────────────────────────────
@app.get("/api/calls")
async def api_get_calls(page: int = 1, limit: int = 20):
return await get_all_calls(page=page, limit=limit)
@app.patch("/api/calls/{call_id}/notes")
async def api_update_notes(call_id: str, req: NotesRequest):
ok = await update_call_notes(call_id, req.notes)
if not ok:
raise HTTPException(404, "Call not found")
return {"status": "updated"}
# ── Stats ─────────────────────────────────────────────────────────────────────
@app.get("/api/stats")
async def api_get_stats():
return await get_stats()
# ── Appointments ──────────────────────────────────────────────────────────────
@app.get("/api/appointments")
async def api_get_appointments(date: Optional[str] = None):
return await get_all_appointments(date_filter=date)
@app.delete("/api/appointments/{appointment_id}")
async def api_cancel_appointment(appointment_id: str):
ok = await cancel_appointment(appointment_id)
if not ok:
raise HTTPException(404, "Appointment not found or already cancelled")
return {"status": "cancelled"}
# ── Prompt ────────────────────────────────────────────────────────────────────
@app.get("/api/prompt")
async def api_get_prompt():
saved = await get_setting("system_prompt", "")
return {"prompt": saved or DEFAULT_SYSTEM_PROMPT, "is_custom": bool(saved)}
@app.post("/api/prompt")
async def api_save_prompt(req: PromptRequest):
await set_setting("system_prompt", req.prompt)
return {"status": "saved"}
@app.delete("/api/prompt")
async def api_reset_prompt():
await set_setting("system_prompt", "")
return {"status": "reset", "prompt": DEFAULT_SYSTEM_PROMPT}
# ── Settings ──────────────────────────────────────────────────────────────────
@app.get("/api/settings")
async def api_get_settings():
return await get_all_settings()
@app.post("/api/settings")
async def api_save_settings(req: SettingsRequest):
filtered = {k: v for k, v in req.settings.items() if v is not None and v != ""}
await save_settings(filtered)
for k, v in filtered.items():
os.environ[k] = str(v)
return {"status": "saved", "count": len(filtered)}
# ── SIP trunk setup ───────────────────────────────────────────────────────────
@app.post("/api/setup/trunk")
async def api_setup_trunk():
url = await eff("LIVEKIT_URL")
key = await eff("LIVEKIT_API_KEY")
secret = await eff("LIVEKIT_API_SECRET")
sip_domain = await eff("VOBIZ_SIP_DOMAIN")
username = await eff("VOBIZ_USERNAME")
password = await eff("VOBIZ_PASSWORD")
phone = await eff("VOBIZ_OUTBOUND_NUMBER")
if not all([url, key, secret, sip_domain, username, password, phone]):
raise HTTPException(400, "Configure LiveKit and Vobiz credentials in Settings first.")
try:
from livekit import api as lk_api
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ctx))
lk = lk_api.LiveKitAPI(url=url, api_key=key, api_secret=secret, session=session)
trunk = await lk.sip.create_sip_outbound_trunk(
lk_api.CreateSIPOutboundTrunkRequest(
trunk=lk_api.SIPOutboundTrunkInfo(
name="Vobiz Outbound Trunk",
address=sip_domain,
auth_username=username,
auth_password=password,
numbers=[phone],
)
)
)
trunk_id = trunk.sip_trunk_id
await set_setting("OUTBOUND_TRUNK_ID", trunk_id)
os.environ["OUTBOUND_TRUNK_ID"] = trunk_id
await lk.aclose()
await session.close()
return {"status": "created", "trunk_id": trunk_id}
except Exception as exc:
raise HTTPException(500, f"Trunk creation failed: {exc}")
# ── Logs ──────────────────────────────────────────────────────────────────────
@app.get("/api/logs")
async def api_get_logs(limit: int = 200, level: Optional[str] = None, source: Optional[str] = None):
return await get_logs(level=level, source=source, limit=limit)
@app.delete("/api/logs")
async def api_clear_logs():
await clear_errors()
return {"status": "cleared"}
# ── CRM ───────────────────────────────────────────────────────────────────────
@app.get("/api/crm")
async def api_get_contacts():
return {"data": await get_contacts()}
@app.get("/api/crm/calls")
async def api_get_contact_calls(phone: str = Query(...)):
return {"data": await get_calls_by_phone(phone)}
# ── Agent Profiles ────────────────────────────────────────────────────────────
@app.get("/api/agent-profiles")
async def api_list_agent_profiles():
try:
return await get_all_agent_profiles()
except Exception as exc:
raise HTTPException(500, str(exc))
@app.post("/api/agent-profiles")
async def api_create_agent_profile(req: AgentProfileRequest):
try:
profile_id = await create_agent_profile(
name=req.name, voice=req.voice, model=req.model,
system_prompt=req.system_prompt, enabled_tools=req.enabled_tools, is_default=req.is_default,
)
return {"status": "created", "id": profile_id}
except Exception as exc:
raise HTTPException(500, str(exc))
@app.get("/api/agent-profiles/{profile_id}")
async def api_get_agent_profile(profile_id: str):
profile = await get_agent_profile(profile_id)
if not profile:
raise HTTPException(404, "Profile not found")
return profile
@app.put("/api/agent-profiles/{profile_id}")
async def api_update_agent_profile(profile_id: str, req: AgentProfileRequest):
ok = await update_agent_profile(profile_id, {
"name": req.name, "voice": req.voice, "model": req.model,
"system_prompt": req.system_prompt, "enabled_tools": req.enabled_tools,
"is_default": 1 if req.is_default else 0,
})
if not ok:
raise HTTPException(404, "Profile not found")
return {"status": "updated"}
@app.delete("/api/agent-profiles/{profile_id}")
async def api_delete_agent_profile(profile_id: str):
ok = await delete_agent_profile(profile_id)
if not ok:
raise HTTPException(404, "Profile not found")
return {"status": "deleted"}
@app.post("/api/agent-profiles/{profile_id}/set-default")
async def api_set_default_profile(profile_id: str):
try:
await set_default_agent_profile(profile_id)
return {"status": "default set"}
except Exception as exc:
raise HTTPException(500, str(exc))
# ── Campaigns ─────────────────────────────────────────────────────────────────
async def _dispatch_one(lk, lk_api, contact: dict, room_name: str,
prompt: Optional[str], profile: Optional[dict] = None) -> bool:
try:
saved_prompt = prompt or (await get_setting("system_prompt", "")) or None
metadata: dict = {
"phone_number": contact["phone"],
"lead_name": contact.get("lead_name", "there"),
"business_name": contact.get("business_name", "our company"),
"service_type": contact.get("service_type", "our service"),
"system_prompt": saved_prompt,
}
if profile:
if not metadata["system_prompt"] and profile.get("system_prompt"):
metadata["system_prompt"] = profile["system_prompt"]
if profile.get("voice"): metadata["voice_override"] = profile["voice"]
if profile.get("model"): metadata["model_override"] = profile["model"]
if profile.get("enabled_tools"): metadata["tools_override"] = profile["enabled_tools"]
await lk.agent_dispatch.create_dispatch(
lk_api.CreateAgentDispatchRequest(agent_name="outbound-caller", room=room_name, metadata=json.dumps(metadata))
)
return True
except Exception as exc:
logger.error("Campaign dispatch error for %s: %s", contact.get("phone"), exc)
return False
async def _run_campaign(campaign_id: str) -> None:
campaign = await get_campaign(campaign_id)
if not campaign:
return
contacts = json.loads(campaign.get("contacts_json") or "[]")
if not contacts:
return
delay = int(campaign.get("call_delay_seconds") or 3)
prompt = campaign.get("system_prompt")
agent_profile_id = campaign.get("agent_profile_id")
profile = None
if agent_profile_id:
profile = await get_agent_profile(agent_profile_id)
url = await eff("LIVEKIT_URL")
key = await eff("LIVEKIT_API_KEY")
secret = await eff("LIVEKIT_API_SECRET")
if not (url and key and secret):
logger.error("Campaign %s: LiveKit not configured", campaign_id)
return
from livekit import api as lk_api_module
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
session = aiohttp.ClientSession(connector=aiohttp.TCPConnector(ssl=ctx))
ok_count = fail_count = 0
try:
lk = lk_api_module.LiveKitAPI(url=url, api_key=key, api_secret=secret, session=session)
for i, contact in enumerate(contacts):
phone = contact.get("phone", "")
if not phone.startswith("+"):
fail_count += 1
continue
room_name = f"camp-{campaign_id[:8]}-{phone.replace('+','')}-{random.randint(100,999)}"
success = await _dispatch_one(lk, lk_api_module, contact, room_name, prompt, profile)
if success:
ok_count += 1
else:
fail_count += 1
if i < len(contacts) - 1:
await asyncio.sleep(delay)
await lk.aclose()
except Exception as exc:
logger.error("Campaign run error: %s", exc)
finally:
await session.close()
await update_campaign_run_stats(campaign_id, ok_count, fail_count)
logger.info("Campaign %s done — %d dispatched, %d failed", campaign_id, ok_count, fail_count)
async def _reschedule_all_campaigns() -> None:
if not _scheduler:
return
try:
campaigns = await get_all_campaigns()
for c in campaigns:
if c.get("status") == "active" and c.get("schedule_type") in ("daily", "weekdays"):
_schedule_campaign(c["id"], c["schedule_type"], c.get("schedule_time", "09:00"))
except Exception as exc:
logger.warning("Could not reschedule campaigns: %s", exc)
def _schedule_campaign(campaign_id: str, schedule_type: str, schedule_time: str) -> None:
if not _scheduler:
return
job_id = f"campaign_{campaign_id}"
if _scheduler.get_job(job_id):
_scheduler.remove_job(job_id)
try:
hour, minute = map(int, schedule_time.split(":"))
except (ValueError, AttributeError):
hour, minute = 9, 0
if schedule_type == "daily":
trigger = CronTrigger(hour=hour, minute=minute)
else:
trigger = CronTrigger(day_of_week="mon-fri", hour=hour, minute=minute)
_scheduler.add_job(_run_campaign, trigger=trigger, args=[campaign_id], id=job_id, replace_existing=True)
logger.info("Scheduled campaign %s (%s at %02d:%02d)", campaign_id, schedule_type, hour, minute)
@app.post("/api/campaigns")
async def api_create_campaign(req: CampaignRequest):
if not req.contacts:
raise HTTPException(400, "contacts list cannot be empty")
if req.schedule_type not in ("once", "daily", "weekdays"):
raise HTTPException(400, "schedule_type must be: once | daily | weekdays")
campaign_id = await create_campaign(
name=req.name, contacts_json=json.dumps(req.contacts),
schedule_type=req.schedule_type, schedule_time=req.schedule_time,
call_delay_seconds=req.call_delay_seconds, system_prompt=req.system_prompt,
agent_profile_id=req.agent_profile_id,
)
campaign = await get_campaign(campaign_id)
if req.schedule_type == "once":
asyncio.create_task(_run_campaign(campaign_id))
else:
_schedule_campaign(campaign_id, req.schedule_type, req.schedule_time)
return {"status": "created", "campaign_id": campaign_id, "campaign": campaign}
@app.get("/api/campaigns")
async def api_list_campaigns():
return await get_all_campaigns()
@app.delete("/api/campaigns/{campaign_id}")
async def api_delete_campaign(campaign_id: str):
ok = await delete_campaign(campaign_id)
if not ok:
raise HTTPException(404, "Campaign not found")
job_id = f"campaign_{campaign_id}"
if _scheduler and _scheduler.get_job(job_id):
_scheduler.remove_job(job_id)
return {"status": "deleted"}
@app.post("/api/campaigns/{campaign_id}/run")
async def api_run_campaign_now(campaign_id: str):
campaign = await get_campaign(campaign_id)
if not campaign:
raise HTTPException(404, "Campaign not found")
asyncio.create_task(_run_campaign(campaign_id))
return {"status": "dispatching", "campaign_id": campaign_id}
@app.patch("/api/campaigns/{campaign_id}/status")
async def api_update_campaign_status(campaign_id: str, req: StatusRequest):
if req.status not in ("active", "paused", "completed"):
raise HTTPException(400, "status must be: active | paused | completed")
ok = await update_campaign_status(campaign_id, req.status)
if not ok:
raise HTTPException(404, "Campaign not found")
job_id = f"campaign_{campaign_id}"
if req.status == "paused" and _scheduler and _scheduler.get_job(job_id):
_scheduler.remove_job(job_id)
elif req.status == "active":
campaign = await get_campaign(campaign_id)
if campaign and campaign.get("schedule_type") in ("daily", "weekdays"):
_schedule_campaign(campaign_id, campaign["schedule_type"], campaign.get("schedule_time", "09:00"))
return {"status": req.status}