Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infra for conversation support #1

Merged
merged 3 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
from app.core.database import Base
from app.modules.users.model import User # Import all your models
from app.modules.projects.model import Project
from app.modules.conversations.conversation.model import Conversation
from app.modules.conversations.message.model import Message


from dotenv import load_dotenv

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""UTC Timestamps and Indexing

Revision ID: 20240812211350_bcc569077106
Revises: 20240812190934_5ceb460ac3ef
Create Date: 2024-08-12 21:13:50.136975

"""
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 = '20240812211350_bcc569077106'
down_revision: Union[str, None] = '20240812190934_5ceb460ac3ef'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('conversations',
sa.Column('id', sa.String(length=255), nullable=False),
sa.Column('user_id', sa.String(length=255), nullable=False),
sa.Column('title', sa.String(length=255), nullable=False),
sa.Column('status', sa.Enum('ACTIVE', 'ARCHIVED', 'DELETED', name='conversationstatus'), nullable=False),
sa.Column('project_ids', postgresql.ARRAY(sa.String()), nullable=False),
sa.Column('agent_ids', postgresql.ARRAY(sa.String()), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.Column('updated_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.ForeignKeyConstraint(['user_id'], ['users.uid'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_conversations_id'), 'conversations', ['id'], unique=False)
op.create_index(op.f('ix_conversations_user_id'), 'conversations', ['user_id'], unique=False)
op.create_table('messages',
sa.Column('id', sa.String(length=255), nullable=False),
sa.Column('conversation_id', sa.String(length=255), nullable=False),
sa.Column('content', sa.Text(), nullable=False),
sa.Column('sender_id', sa.String(length=255), nullable=True),
sa.Column('type', sa.Enum('AI_GENERATED', 'HUMAN', name='messagetype'), nullable=False),
sa.Column('created_at', sa.TIMESTAMP(timezone=True), nullable=False),
sa.CheckConstraint("(type = 'HUMAN' AND sender_id IS NOT NULL) OR (type = 'AI_GENERATED' AND sender_id IS NULL)", name='check_sender_id_for_type'),
sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_messages_conversation_id'), 'messages', ['conversation_id'], unique=False)
op.alter_column('projects', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.TIMESTAMP(timezone=True),
nullable=False)
op.alter_column('users', 'created_at',
existing_type=postgresql.TIMESTAMP(),
type_=sa.TIMESTAMP(timezone=True),
nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('users', 'created_at',
existing_type=sa.TIMESTAMP(timezone=True),
type_=postgresql.TIMESTAMP(),
nullable=True)
op.alter_column('projects', 'created_at',
existing_type=sa.TIMESTAMP(timezone=True),
type_=postgresql.TIMESTAMP(),
nullable=True)
op.drop_index(op.f('ix_messages_conversation_id'), table_name='messages')
op.drop_table('messages')
op.drop_index(op.f('ix_conversations_user_id'), table_name='conversations')
op.drop_index(op.f('ix_conversations_id'), table_name='conversations')
op.drop_table('conversations')
# ### end Alembic commands ###
Empty file.
29 changes: 29 additions & 0 deletions app/modules/conversations/conversation/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from sqlalchemy import Column, String, TIMESTAMP, func, ForeignKey, Enum as SQLAEnum, Index
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import relationship
from app.core.database import Base
import enum

# Define the Status Enum
class ConversationStatus(enum.Enum):
ACTIVE = "active"
ARCHIVED = "archived"
DELETED = "deleted"

class Conversation(Base):
__tablename__ = "conversations"

id = Column(String(255), primary_key=True, index=True)
user_id = Column(String(255), ForeignKey("users.uid"), nullable=False, index=True) # ForeignKey to User model with index
title = Column(String(255), nullable=False) # Title of the conversation
status = Column(SQLAEnum(ConversationStatus), default=ConversationStatus.ACTIVE, nullable=False) # Status of the conversation
project_ids = Column(ARRAY(String), nullable=False)
agent_ids = Column(ARRAY(String), nullable=False)
created_at = Column(TIMESTAMP(timezone=True), default=func.utcnow(), nullable=False) # Use UTC timestamp
updated_at = Column(TIMESTAMP(timezone=True), default=func.utcnow(), onupdate=func.utcnow(), nullable=False) # Use UTC timestamp

# Relationship to the Message model
messages = relationship("Message", back_populates="conversation")

# Optional: Relationship back to User model (if needed)
user = relationship("User", back_populates="conversations")
30 changes: 30 additions & 0 deletions app/modules/conversations/message/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from sqlalchemy import Column, String, ForeignKey, TIMESTAMP, func, Text, Enum as SQLAEnum, CheckConstraint
from sqlalchemy.orm import relationship
from app.core.database import Base
import enum

# Define the MessageType Enum
class MessageType(str, enum.Enum):
AI_GENERATED = "AI_GENERATED"
HUMAN = "HUMAN"

class Message(Base):
__tablename__ = "messages"

id = Column(String(255), primary_key=True)
vineetshar marked this conversation as resolved.
Show resolved Hide resolved
conversation_id = Column(String(255), ForeignKey("conversations.id"), nullable=False, index=True)
content = Column(Text, nullable=False)
sender_id = Column(String(255), nullable=True) # Allow sender_id to be nullable
type = Column(SQLAEnum(MessageType), nullable=False) # Type of message (AI_GENERATED or HUMAN)
created_at = Column(TIMESTAMP(timezone=True), default=func.utcnow(), nullable=False) # Use UTC timestamp

# Relationship to the Conversation model
conversation = relationship("Conversation", back_populates="messages")

# Add a CHECK constraint to enforce the sender_id logic
__table_args__ = (
CheckConstraint(
"(type = 'HUMAN' AND sender_id IS NOT NULL) OR (type = 'AI_GENERATED' AND sender_id IS NULL)",
name="check_sender_id_for_type"
),
)
2 changes: 1 addition & 1 deletion app/modules/projects/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Project(Base):
repo_name = Column(Text)
branch_name = Column(Text)
user_id = Column(String(255), nullable=False)
created_at = Column(TIMESTAMP, default=func.current_timestamp())
created_at = Column(TIMESTAMP(timezone=True), default=func.utcnow(), nullable=False) # Use UTC timestamp
commit_id = Column(String(255))
is_deleted = Column(Boolean, default=False)
updated_at = Column(
Expand Down
6 changes: 5 additions & 1 deletion app/modules/users/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ class User(Base):
email = Column(String(255), unique=True, nullable=False)
display_name = Column(String(255))
email_verified = Column(Boolean, default=False)
created_at = Column(TIMESTAMP, default=func.current_timestamp())
created_at = Column(TIMESTAMP(timezone=True), default=func.utcnow(), nullable=False) # Use UTC timestamp
last_login_at = Column(TIMESTAMP, default=func.current_timestamp())
provider_info = Column(JSONB)
provider_username = Column(String(255))


# Relationships
projects = relationship(
"Project", back_populates="user"
) # Assumes a 'Project' class exists
# Relationship to Conversation model
conversations = relationship("Conversation", back_populates="user")