11import uuid
22from datetime import datetime
3- from sqlalchemy import Column , String , Text , ForeignKey , DateTime , select
3+ from sqlalchemy import Column , String , Text , ForeignKey , DateTime , select , func
44from sqlalchemy .dialects .postgresql import JSON
55# Remove PostgreSQL UUID import as we're standardizing on String
66from sqlalchemy .orm import relationship
@@ -14,6 +14,7 @@ class Conversation(Base, TimestampMixin):
1414 user_id = Column (String , ForeignKey ("users.id" ), nullable = False )
1515 title = Column (String )
1616 page_context = Column (String ) # e.g., 'home', 'editor', 'chatbot_lab'
17+ page_id = Column (String (32 ), nullable = True ) # NEW - specific page ID for page-specific conversations
1718 model = Column (String ) # Store which model was used
1819 server = Column (String ) # Store which server was used
1920 conversation_type = Column (String (100 ), nullable = True , default = "chat" ) # New field for categorization
@@ -104,6 +105,36 @@ async def get_by_user_id(cls, db, user_id, skip=0, limit=100):
104105 result = await db .execute (query )
105106 return result .scalars ().all ()
106107
108+ @classmethod
109+ async def get_by_user_id_and_page (cls , db , user_id , page_id = None , conversation_type = None , skip = 0 , limit = 100 ):
110+ """Get conversations for user, optionally filtered by page_id and conversation_type."""
111+ formatted_user_id = str (user_id ).replace ('-' , '' )
112+ query = select (cls ).where (cls .user_id == formatted_user_id )
113+
114+ # Filter by page_id if provided
115+ if page_id :
116+ query = query .where (cls .page_id == page_id )
117+
118+ # Filter by conversation_type if provided
119+ if conversation_type :
120+ query = query .where (cls .conversation_type == conversation_type )
121+
122+ query = query .order_by (cls .updated_at .desc ()).offset (skip ).limit (limit )
123+ result = await db .execute (query )
124+ return result .scalars ().all ()
125+
126+ @classmethod
127+ async def count_by_user_and_page (cls , db , user_id , page_id = None ):
128+ """Count conversations for user, optionally filtered by page_id."""
129+ formatted_user_id = str (user_id ).replace ('-' , '' )
130+ query = select (func .count (cls .id )).where (cls .user_id == formatted_user_id )
131+
132+ if page_id :
133+ query = query .where (cls .page_id == page_id )
134+
135+ result = await db .execute (query )
136+ return result .scalar ()
137+
107138 async def get_messages (self , db , skip = 0 , limit = 100 ):
108139 """Get all messages for this conversation with pagination."""
109140 from app .models .message import Message
0 commit comments