Spaces:
Paused

File size: 2,158 Bytes
5eb4857
 
 
 
b7980f7
 
4405ca9
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7980f7
 
 
 
 
 
 
 
 
 
 
 
5eb4857
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
Attachment model — files attached to tickets or Knowledge Base articles.
"""

from sqlalchemy import Column, String, ForeignKey, Integer, Text, Boolean, Index, Computed
from sqlalchemy.dialects.postgresql import UUID, TSVECTOR
from sqlalchemy.orm import relationship
from pgvector.sqlalchemy import Vector
from core.database import Base
from core.config import get_settings
from models.base import PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin

settings = get_settings()

class Attachment(Base, PrimaryKeyMixin, TimestampMixin, SoftDeleteMixin):
    __tablename__ = "attachments"

    workspace_id = Column(UUID(as_uuid=True), ForeignKey("workspaces.id"), nullable=False, index=True)
    ticket_id = Column(UUID(as_uuid=True), ForeignKey("tickets.id"), nullable=True, index=True)
    article_id = Column(UUID(as_uuid=True), ForeignKey("kb_articles.id"), nullable=True, index=True)
    uploader_id = Column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=True)
    
    filename = Column(String(500), nullable=False)
    storage_path = Column(String(1024), nullable=False)
    storage_backend = Column(String(20), default="local") # "local" or "s3"
    content_type = Column(String(100), nullable=True)
    file_size = Column(Integer, nullable=True)
    source_url = Column(String(2048), nullable=True) # For Markdown-extracted images
    
    # Multimodal AI Fields
    embedding = Column(Vector(settings.AI_IMAGE_EMBEDDING_DIM), nullable=True) # Vision Vector (e.g. CLIP)
    ai_caption = Column(Text, nullable=True) # AI-generated description
    is_processed = Column(Boolean, default=False, index=True)
    
    # --- Search Optimization ---
    # 1. Computed TSVector for GIN search
    search_vector = Column(
        TSVECTOR, 
        Computed("to_tsvector('english', coalesce(ai_caption, ''))", persisted=True)
    )
    
    # 2. GIN Index for visual forensic search
    __table_args__ = (
        Index('idx_att_search_vector', "search_vector", postgresql_using='gin'),
    )

    # Relationships
    ticket = relationship("Ticket", back_populates="attachments")
    article = relationship("KBArticle", back_populates="attachments")