Files
ai-video-fullstack/backend/migrations/versions/20260710_0003_add_conversation_history.py
Xin Wang 059ad8162e Add conversation history management and API endpoints
- Introduce new database models for conversation sessions, messages, and artifacts to support conversation history tracking.
- Implement API routes for listing conversations and retrieving detailed conversation data, enhancing user interaction with historical records.
- Add a conversation recorder service to persist conversation messages in real-time without disrupting ongoing calls.
- Update the frontend to display conversation history, including filtering and sorting options, improving user experience.
- Enhance the pipeline to integrate conversation history recording seamlessly during interactions.
2026-07-10 16:25:06 +08:00

82 lines
4.4 KiB
Python

"""add conversation history
Revision ID: 20260710_0003
Revises: 20260710_0002
"""
from collections.abc import Sequence
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql
revision: str = "20260710_0003"
down_revision: str | Sequence[str] | None = "20260710_0002"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.create_table(
"conversation_sessions",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("assistant_id", sa.String(length=40), nullable=True),
sa.Column("assistant_name", sa.String(length=128), server_default="", nullable=False),
sa.Column("channel", sa.String(length=24), nullable=False),
sa.Column("runtime_mode", sa.String(length=16), server_default="pipeline", nullable=False),
sa.Column("status", sa.String(length=16), server_default="active", nullable=False),
sa.Column("message_count", sa.Integer(), server_default="0", nullable=False),
sa.Column("started_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("ended_at", sa.DateTime(timezone=True), nullable=True),
sa.Column("extra", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.ForeignKeyConstraint(["assistant_id"], ["assistants.id"], ondelete="SET NULL"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_conversation_sessions_assistant_id", "conversation_sessions", ["assistant_id"])
op.create_index("ix_conversation_sessions_channel", "conversation_sessions", ["channel"])
op.create_index("ix_conversation_sessions_status", "conversation_sessions", ["status"])
op.create_index("ix_conversation_sessions_started_at", "conversation_sessions", ["started_at"])
op.create_table(
"conversation_messages",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("session_id", sa.String(length=40), nullable=False),
sa.Column("sequence", sa.Integer(), nullable=False),
sa.Column("role", sa.String(length=16), nullable=False),
sa.Column("content_type", sa.String(length=16), server_default="text", nullable=False),
sa.Column("content", sa.Text(), server_default="", nullable=False),
sa.Column("occurred_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("extra", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.ForeignKeyConstraint(["session_id"], ["conversation_sessions.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("session_id", "sequence", name="uq_conversation_message_sequence"),
)
op.create_index("ix_conversation_messages_session_id", "conversation_messages", ["session_id"])
op.create_table(
"conversation_artifacts",
sa.Column("id", sa.String(length=40), nullable=False),
sa.Column("session_id", sa.String(length=40), nullable=False),
sa.Column("message_id", sa.String(length=40), nullable=True),
sa.Column("kind", sa.String(length=16), nullable=False),
sa.Column("storage_uri", sa.String(length=1024), nullable=False),
sa.Column("mime_type", sa.String(length=128), server_default="", nullable=False),
sa.Column("size_bytes", sa.Integer(), nullable=True),
sa.Column("duration_ms", sa.Integer(), nullable=True),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
sa.Column("extra", postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
sa.ForeignKeyConstraint(["message_id"], ["conversation_messages.id"], ondelete="SET NULL"),
sa.ForeignKeyConstraint(["session_id"], ["conversation_sessions.id"], ondelete="CASCADE"),
sa.PrimaryKeyConstraint("id"),
)
op.create_index("ix_conversation_artifacts_session_id", "conversation_artifacts", ["session_id"])
op.create_index("ix_conversation_artifacts_message_id", "conversation_artifacts", ["message_id"])
op.create_index("ix_conversation_artifacts_kind", "conversation_artifacts", ["kind"])
def downgrade() -> None:
op.drop_table("conversation_artifacts")
op.drop_table("conversation_messages")
op.drop_table("conversation_sessions")