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>
110 lines
3.5 KiB
Python
110 lines
3.5 KiB
Python
"""
|
|
Call History API — Read-only access to persisted call records,
|
|
transcript chunks, and recording files for the dashboard.
|
|
Thin HTTP layer over the shared data functions in call_persistence.
|
|
"""
|
|
|
|
import os
|
|
from datetime import datetime
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from fastapi.responses import FileResponse
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from db.database import get_db
|
|
from services import call_persistence as store
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/history")
|
|
async def list_history(
|
|
limit: int = Query(50, ge=1, le=500),
|
|
offset: int = Query(0, ge=0),
|
|
number: str | None = None,
|
|
status: str | None = None,
|
|
since: datetime | None = None,
|
|
until: datetime | None = None,
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
"""Paged list of past calls, newest first."""
|
|
rows = await store.search_history(
|
|
db,
|
|
number=number,
|
|
status=status,
|
|
since=since,
|
|
until=until,
|
|
limit=limit,
|
|
offset=offset,
|
|
)
|
|
return [
|
|
{
|
|
"id": r.id,
|
|
"direction": r.direction,
|
|
"remote_number": r.remote_number,
|
|
"status": r.status,
|
|
"mode": r.mode,
|
|
"intent": r.intent,
|
|
"started_at": r.started_at.isoformat() if r.started_at else None,
|
|
"ended_at": r.ended_at.isoformat() if r.ended_at else None,
|
|
"duration": r.duration,
|
|
"hold_time": r.hold_time,
|
|
"device_used": r.device_used,
|
|
"summary": r.summary,
|
|
}
|
|
for r in rows
|
|
]
|
|
|
|
|
|
@router.get("/{call_id}/record")
|
|
async def get_record(call_id: str, db: AsyncSession = Depends(get_db)):
|
|
"""Full CallRecord with classification_timeline."""
|
|
row = await store.get_record(db, call_id)
|
|
if not row:
|
|
raise HTTPException(status_code=404, detail=f"Call {call_id} not found")
|
|
return {
|
|
"id": row.id,
|
|
"direction": row.direction,
|
|
"remote_number": row.remote_number,
|
|
"status": row.status,
|
|
"mode": row.mode,
|
|
"intent": row.intent,
|
|
"started_at": row.started_at.isoformat() if row.started_at else None,
|
|
"ended_at": row.ended_at.isoformat() if row.ended_at else None,
|
|
"duration": row.duration,
|
|
"hold_time": row.hold_time,
|
|
"device_used": row.device_used,
|
|
"summary": row.summary,
|
|
"action_items": row.action_items,
|
|
"sentiment": row.sentiment,
|
|
"call_flow_id": row.call_flow_id,
|
|
"classification_timeline": row.classification_timeline,
|
|
}
|
|
|
|
|
|
@router.get("/{call_id}/transcript")
|
|
async def get_transcript(call_id: str, db: AsyncSession = Depends(get_db)):
|
|
"""Ordered transcript chunks for a call."""
|
|
rows = await store.get_transcript_chunks(db, call_id)
|
|
return [
|
|
{
|
|
"seq": c.seq,
|
|
"t_offset_ms": c.t_offset_ms,
|
|
"speaker": c.speaker,
|
|
"text": c.text,
|
|
"confidence": c.confidence,
|
|
}
|
|
for c in rows
|
|
]
|
|
|
|
|
|
@router.get("/{call_id}/recording")
|
|
async def get_recording(call_id: str, db: AsyncSession = Depends(get_db)):
|
|
"""Stream the WAV recording for a call."""
|
|
row = await store.latest_recording(db, call_id)
|
|
if not row or not row.path:
|
|
raise HTTPException(status_code=404, detail="Recording not found")
|
|
if not os.path.exists(row.path):
|
|
raise HTTPException(status_code=404, detail="Recording file missing on disk")
|
|
return FileResponse(row.path, media_type="audio/wav", filename=os.path.basename(row.path))
|