Newer
Older
cortex-hub / ai-hub / app / db / file_retriever_models.py
from datetime import datetime
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Boolean, JSON
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
from sqlalchemy.orm import relationship
import uuid

# Assuming Base is imported from your database.py
from .database import Base

class FileRetrievalRequest(Base):
    """
    SQLAlchemy model for the 'file_retrieval_requests' table.

    Each entry represents a single user request to process a directory
    for the AI coding assistant.
    """
    __tablename__ = 'file_retrieval_requests'

    # Primary key, a UUID for unique and secure identification.
    id = Column(PG_UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, index=True)
    # The client's main question or instruction for the AI.
    question = Column(Text, nullable=False)
    # The path or name of the directory provided by the client.
    directory_path = Column(String, nullable=False)
    # Foreign key to link this request to a specific chat session.
    session_id = Column(Integer, ForeignKey('sessions.id'), nullable=False)
    # Timestamp for when the request was made.
    created_at = Column(DateTime, default=datetime.now, nullable=False)

    # Defines a one-to-many relationship with the RetrievedFile table.
    # 'cascade' ensures that all associated files are deleted when the request is deleted.
    retrieved_files = relationship(
        "RetrievedFile",
        back_populates="retrieval_request",
        cascade="all, delete-orphan"
    )

    def __repr__(self):
        return f"<FileRetrievalRequest(id={self.id}, directory_path='{self.directory_path}')>"

class RetrievedFile(Base):
    """
    SQLAlchemy model for the 'retrieved_files' table.

    This table stores the content and metadata for each file retrieved
    during a file processing request.
    """
    __tablename__ = 'retrieved_files'

    # Primary key for the file entry.
    id = Column(Integer, primary_key=True, index=True)
    # Foreign key linking this file back to its parent retrieval request.
    request_id = Column(PG_UUID(as_uuid=True), ForeignKey('file_retrieval_requests.id'), nullable=False)
    # The full path to the file.
    file_path = Column(String, nullable=False)
    # The name of the file.
    file_name = Column(String, nullable=False)
    # The actual content of the file.
    content = Column(Text, nullable=False)
    # The type of the file content (e.g., 'original', 'updated').
    type = Column(String, nullable=False, default='original')
    # Timestamp for when the file was last modified.
    last_updated = Column(DateTime, default=datetime.now)
    # Timestamp for when this entry was created in the database.
    created_at = Column(DateTime, default=datetime.now, nullable=False)
    
    # Defines a many-to-one relationship back to the FileRetrievalRequest.
    retrieval_request = relationship(
        "FileRetrievalRequest",
        back_populates="retrieved_files"
    )

    def __repr__(self):
        return f"<RetrievedFile(id={self.id}, file_path='{self.file_path}', type='{self.type}')>"