Files
hold-slayer/db/database.py
Robert Helewka 67a00defc3 refactor: composition root in lifespan, break core↔services cycle, shared data layer
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>
2026-07-09 20:29:01 -04:00

246 lines
8.2 KiB
Python

"""
Database connection and session management.
PostgreSQL via asyncpg + SQLAlchemy async.
"""
from collections.abc import AsyncIterator
from contextlib import asynccontextmanager
from datetime import datetime
from sqlalchemy import (
JSON,
Boolean,
Column,
DateTime,
Float,
Integer,
String,
Text,
func,
)
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase
from config import get_settings
class Base(DeclarativeBase):
"""SQLAlchemy declarative base for all ORM models."""
pass
# ============================================================
# ORM Models
# ============================================================
class CallRecord(Base):
__tablename__ = "call_records"
id = Column(String, primary_key=True)
direction = Column(String, nullable=False) # inbound / outbound
remote_number = Column(String, index=True, nullable=False)
status = Column(String, nullable=False) # completed / missed / failed / active / on_hold
mode = Column(String, nullable=False) # direct / hold_slayer / ai_assisted
intent = Column(Text) # What the user wanted (for hold_slayer)
started_at = Column(DateTime, default=func.now())
ended_at = Column(DateTime, nullable=True)
duration = Column(Integer, default=0) # seconds
hold_time = Column(Integer, default=0) # seconds spent on hold
device_used = Column(String)
recording_path = Column(String, nullable=True)
transcript = Column(Text, nullable=True)
summary = Column(Text, nullable=True)
action_items = Column(JSON, nullable=True)
sentiment = Column(String, nullable=True)
call_flow_id = Column(String, nullable=True) # which flow was used
classification_timeline = Column(JSON, nullable=True) # [{time, type, confidence}, ...]
metadata_ = Column("metadata", JSON, nullable=True)
def __repr__(self) -> str:
return f"<CallRecord {self.id} {self.remote_number} {self.status}>"
class StoredCallFlow(Base):
__tablename__ = "call_flows"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
phone_number = Column(String, index=True, nullable=False)
description = Column(Text)
steps = Column(JSON, nullable=False) # Serialized list[CallFlowStep]
last_verified = Column(DateTime, nullable=True)
avg_hold_time = Column(Integer, nullable=True)
success_rate = Column(Float, nullable=True)
times_used = Column(Integer, default=0)
last_used = Column(DateTime, nullable=True)
notes = Column(Text, nullable=True)
tags = Column(JSON, default=list)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
def __repr__(self) -> str:
return f"<StoredCallFlow {self.id} {self.phone_number}>"
class Contact(Base):
__tablename__ = "contacts"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
phone_numbers = Column(JSON, nullable=False) # [{number, label, primary}, ...]
category = Column(String) # personal / business / service
routing_preference = Column(String, nullable=True) # how to handle their calls
notes = Column(Text, nullable=True)
call_count = Column(Integer, default=0)
last_call = Column(DateTime, nullable=True)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
def __repr__(self) -> str:
return f"<Contact {self.id} {self.name}>"
class Device(Base):
__tablename__ = "devices"
id = Column(String, primary_key=True)
name = Column(String, nullable=False) # "Office SIP Phone"
type = Column(String, nullable=False) # sip_phone / cell / tablet / softphone
sip_uri = Column(String, nullable=True) # sip:robert@gateway.helu.ca
phone_number = Column(String, nullable=True) # For PSTN devices
priority = Column(Integer, default=10) # Routing priority (lower = higher priority)
is_online = Column(String, default="false")
capabilities = Column(JSON, default=list) # ["voice", "video", "sms"]
dnd = Column(Boolean, default=False, nullable=False)
last_seen = Column(DateTime, nullable=True)
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
def __repr__(self) -> str:
return f"<Device {self.id} {self.name} ({self.type})>"
class RoutingRuleRecord(Base):
__tablename__ = "routing_rules"
id = Column(String, primary_key=True)
name = Column(String, nullable=False)
priority = Column(Integer, default=100, nullable=False) # lower runs first
enabled = Column(Boolean, default=True, nullable=False)
match = Column(JSON, nullable=False) # caller_pattern, dnis, time_range, days
action = Column(JSON, nullable=False) # {type, ...}
created_at = Column(DateTime, default=func.now())
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
def __repr__(self) -> str:
return f"<RoutingRule {self.id} {self.name} p={self.priority}>"
class TranscriptChunk(Base):
__tablename__ = "transcript_chunks"
id = Column(String, primary_key=True)
call_id = Column(String, index=True, nullable=False)
seq = Column(Integer, nullable=False)
t_offset_ms = Column(Integer, default=0) # offset from call start
speaker = Column(String, default="unknown") # caller / agent / receptionist / unknown
text = Column(Text, nullable=False)
confidence = Column(Float, nullable=True)
created_at = Column(DateTime, default=func.now())
def __repr__(self) -> str:
return f"<TranscriptChunk {self.call_id}#{self.seq}>"
class RecordingRecord(Base):
__tablename__ = "recordings"
id = Column(String, primary_key=True)
call_id = Column(String, index=True, nullable=False)
path = Column(String, nullable=False)
format = Column(String, default="wav")
duration_s = Column(Float, default=0.0)
size_bytes = Column(Integer, default=0)
channels = Column(Integer, default=1)
started_at = Column(DateTime, default=func.now())
ended_at = Column(DateTime, nullable=True)
def __repr__(self) -> str:
return f"<Recording {self.id} call={self.call_id} {self.path}>"
# ============================================================
# Engine & Session
# ============================================================
_engine = None
_session_factory = None
def get_engine():
"""Get or create the async engine."""
global _engine
if _engine is None:
settings = get_settings()
_engine = create_async_engine(
settings.database_url,
echo=settings.debug,
pool_size=10,
max_overflow=20,
)
return _engine
def get_session_factory() -> async_sessionmaker[AsyncSession]:
"""Get or create the session factory."""
global _session_factory
if _session_factory is None:
_session_factory = async_sessionmaker(
get_engine(),
class_=AsyncSession,
expire_on_commit=False,
)
return _session_factory
@asynccontextmanager
async def session_scope() -> AsyncIterator[AsyncSession]:
"""A commit-on-success session — the one session-lifecycle convention.
REST handlers get it via the get_db dependency; services and MCP
tools use it directly.
"""
factory = get_session_factory()
async with factory() as session:
try:
yield session
await session.commit()
except Exception:
await session.rollback()
raise
async def get_db() -> AsyncIterator[AsyncSession]:
"""FastAPI dependency: yield an async database session."""
async with session_scope() as session:
yield session
async def init_db():
"""Create all tables. For development; use Alembic migrations in production."""
engine = get_engine()
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
async def close_db():
"""Close the database engine."""
global _engine, _session_factory
if _engine is not None:
await _engine.dispose()
_engine = None
_session_factory = None