Files
Skeen-CRM/alembic/versions/20260428_init.py
root d30b22b50c feat: initial Skeen-CRM AI Agent architecture
- FastAPI + Python 3.12 backend
- Meta WhatsApp Business API client (official)
- OpenAI GPT-4o with function calling
- RAG vector store with pgvector
- ERPNext Frappe REST client
- Celery + Redis async task queue
- PostgreSQL with migrations (Alembic)
- Docker Compose full stack
- Enterprise logging, metrics, health checks
2026-04-29 05:30:59 +00:00

101 lines
4.2 KiB
Python

"""Initial migration: conversations, messages, knowledge_chunks.
Revision ID: 001
Revises:
Create Date: 2026-04-28 00:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic.
revision: str = "001"
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# Enable pgvector extension
op.execute("CREATE EXTENSION IF NOT EXISTS vector")
# Conversations table
op.create_table(
"conversations",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("phone_number", sa.String(20), nullable=False, index=True),
sa.Column("patient_id", sa.String(100), nullable=True, index=True),
sa.Column("patient_name", sa.String(255), nullable=True),
sa.Column(
"status",
sa.Enum("active", "paused", "resolved", "escalated", "appointment_confirmed", name="conversationstatus"),
nullable=False,
server_default="active",
),
sa.Column("context", postgresql.JSONB(astext_type=sa.Text()), server_default="{}"),
sa.Column("last_message_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), onupdate=sa.func.now()),
)
# Messages table
op.create_table(
"messages",
sa.Column("id", sa.String(36), primary_key=True),
sa.Column("conversation_id", sa.String(36), nullable=False, index=True),
sa.Column("direction", sa.String(10), nullable=False),
sa.Column("role", sa.String(20), nullable=False),
sa.Column("message_type", sa.String(50), server_default="text"),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("whatsapp_message_id", sa.String(100), nullable=True),
sa.Column("tool_calls", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("tool_results", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column("tokens_used", sa.Integer(), server_default="0"),
sa.Column("metadata", postgresql.JSONB(astext_type=sa.Text()), server_default="{}"),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
)
# Knowledge chunks table (for RAG)
op.create_table(
"knowledge_chunks",
sa.Column("id", sa.String(36), primary_key=True, server_default=sa.text("gen_random_uuid()::text")),
sa.Column("content", sa.Text(), nullable=False),
sa.Column("metadata", postgresql.JSONB(astext_type=sa.Text()), server_default="{}"),
sa.Column("category", sa.String(50), server_default="general"),
sa.Column("source", sa.String(255), server_default=""),
sa.Column("embedding", sa.String(), nullable=True), # Stored as string; pgvector uses special type
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now()),
)
# Create pgvector column properly using raw SQL
op.execute("""
ALTER TABLE knowledge_chunks
ALTER COLUMN embedding TYPE vector(1536)
USING embedding::vector(1536)
""")
# Indexes
op.create_index("idx_knowledge_category", "knowledge_chunks", ["category"])
op.create_index("idx_knowledge_source", "knowledge_chunks", ["source"])
op.execute("""
CREATE INDEX idx_knowledge_embedding
ON knowledge_chunks
USING ivfflat (embedding vector_cosine_ops)
WITH (lists = 100)
""")
def downgrade() -> None:
op.drop_index("idx_knowledge_embedding", table_name="knowledge_chunks")
op.drop_index("idx_knowledge_source", table_name="knowledge_chunks")
op.drop_index("idx_knowledge_category", table_name="knowledge_chunks")
op.drop_table("knowledge_chunks")
op.drop_table("messages")
op.drop_table("conversations")
op.execute("DROP TYPE IF EXISTS conversationstatus")
op.execute("DROP EXTENSION IF EXISTS vector")