The gateway was the composition root, device registry, inbound-call policy, and call-operations service in one class, with core↔services circular imports papered over by function-local imports, wiring done by assigning private attributes, and MCP tools duplicating REST query logic against their own sessions. Composition: - main.py's lifespan now builds every service and wires them by constructor/registration. gateway.from_config() is gone; core/ no longer imports services/ anywhere — the cycle is dead. - Inbound-call policy moved to ReceptionistService.on_inbound_call (routing evaluation, reject/answer, screening dispatch); wired as the engine's on_incoming_call by the lifespan. Receptionist deps (tts/transcription/recording/routing) are constructor-injected — no more gateway._tts reach-through or importing hold_slayer's private _get_llm (now services.llm_client.get_llm, shared). - Hold-slayer launch goes through a mode-handler registry (register_mode_handler); the gateway no longer knows the service's type. CallManager takes on_call_ended in its constructor. - build_sip_engine() is a pure function taking explicit callbacks. - api/routing.py uses the routing service from app.state via a proper dependency instead of gateway._routing. Shared data layer: - db.session_scope() is the one session convention (get_db wraps it). - services/call_persistence.py gains the query/write functions and the single StoredCallFlow→CallFlow mapper; api/call_flows.py, api/call_history.py, and the six DB-touching MCP tools are thin wrappers over them — the two surfaces can't drift. - legs_for_call() replaces the three private _call_legs scans (gateway transfer/hangup, REST dtmf, MCP dtmf). 7 new tests (mode-handler launch, on_call_ended hook, receptionist inbound answer/reject, call-flow CRUD round-trip and history routes against real SQLite through the shared layer). aiosqlite added to dev deps for that. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
181 lines
5.4 KiB
Python
181 lines
5.4 KiB
Python
"""
|
|
Call Management API — Place calls, check status, transfer, hold-slay.
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
from api.deps import get_gateway
|
|
from core.gateway import AIPSTNGateway
|
|
from models.call import (
|
|
CallMode,
|
|
CallRequest,
|
|
CallResponse,
|
|
CallStatusResponse,
|
|
HoldSlayerRequest,
|
|
TransferRequest,
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/outbound", response_model=CallResponse)
|
|
async def make_call(
|
|
request: CallRequest,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""
|
|
Place an outbound call.
|
|
|
|
Modes:
|
|
- **direct**: Call and connect to your device immediately
|
|
- **hold_slayer**: Navigate IVR, wait on hold, transfer when human detected
|
|
- **ai_assisted**: Connect with noise cancel, transcription, recording
|
|
"""
|
|
try:
|
|
call = await gateway.make_call(
|
|
number=request.number,
|
|
mode=request.mode,
|
|
intent=request.intent,
|
|
device=request.device,
|
|
call_flow_id=request.call_flow_id,
|
|
services=request.services,
|
|
)
|
|
return CallResponse(
|
|
call_id=call.id,
|
|
status=call.status.value,
|
|
number=request.number,
|
|
mode=request.mode.value,
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/hold-slayer", response_model=CallResponse)
|
|
async def hold_slayer(
|
|
request: HoldSlayerRequest,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""
|
|
🗡️ The Hold Slayer endpoint.
|
|
|
|
Give it a number and intent, it calls, navigates the IVR,
|
|
waits on hold, and rings you when a human picks up.
|
|
|
|
Example:
|
|
POST /api/calls/hold-slayer
|
|
{
|
|
"number": "+18005551234",
|
|
"intent": "cancel my credit card",
|
|
"call_flow_id": "chase_bank_main",
|
|
"transfer_to": "sip_phone",
|
|
"notify": ["sms", "push"]
|
|
}
|
|
"""
|
|
try:
|
|
call = await gateway.make_call(
|
|
number=request.number,
|
|
mode=CallMode.HOLD_SLAYER,
|
|
intent=request.intent,
|
|
call_flow_id=request.call_flow_id,
|
|
device=request.transfer_to,
|
|
)
|
|
return CallResponse(
|
|
call_id=call.id,
|
|
status="navigating_ivr",
|
|
number=request.number,
|
|
mode="hold_slayer",
|
|
message="Hold Slayer activated. I'll ring you when a human picks up. ☕",
|
|
)
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=400, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/active")
|
|
async def list_active_calls(
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""List all active calls with their current status."""
|
|
calls = gateway.call_manager.active_calls
|
|
return [call.summary() for call in calls.values()]
|
|
|
|
|
|
@router.get("/{call_id}", response_model=CallStatusResponse)
|
|
async def get_call(
|
|
call_id: str,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""Get current call status, transcript so far, classification history."""
|
|
call = gateway.get_call(call_id)
|
|
if not call:
|
|
raise HTTPException(status_code=404, detail=f"Call {call_id} not found")
|
|
|
|
return CallStatusResponse(
|
|
call_id=call.id,
|
|
status=call.status.value,
|
|
direction=call.direction,
|
|
remote_number=call.remote_number,
|
|
mode=call.mode.value,
|
|
duration=call.duration,
|
|
hold_time=call.hold_time,
|
|
audio_type=call.current_classification.value,
|
|
intent=call.intent,
|
|
transcript_excerpt=call.transcript[-500:] if call.transcript else None,
|
|
classification_history=call.classification_history[-50:],
|
|
current_step=call.current_step_id,
|
|
services=call.services,
|
|
)
|
|
|
|
|
|
@router.post("/{call_id}/transfer")
|
|
async def transfer_call(
|
|
call_id: str,
|
|
request: TransferRequest,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""Transfer an active call to a device."""
|
|
try:
|
|
await gateway.transfer_call(call_id, request.device)
|
|
return {"status": "transferred", "target": request.device}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/{call_id}/hangup")
|
|
async def hangup_call(
|
|
call_id: str,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""Hang up a call."""
|
|
try:
|
|
await gateway.hangup_call(call_id)
|
|
return {"status": "hung_up", "call_id": call_id}
|
|
except ValueError as e:
|
|
raise HTTPException(status_code=404, detail=str(e))
|
|
except Exception as e:
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.post("/{call_id}/dtmf")
|
|
async def send_dtmf(
|
|
call_id: str,
|
|
digits: str,
|
|
gateway: AIPSTNGateway = Depends(get_gateway),
|
|
):
|
|
"""Send DTMF tones on an active call."""
|
|
call = gateway.get_call(call_id)
|
|
if not call:
|
|
raise HTTPException(status_code=404, detail=f"Call {call_id} not found")
|
|
|
|
legs = gateway.call_manager.legs_for_call(call_id)
|
|
if not legs:
|
|
raise HTTPException(status_code=409, detail="No active SIP leg found for this call")
|
|
|
|
await gateway.sip_engine.send_dtmf(legs[0], digits)
|
|
return {"status": "sent", "digits": digits}
|