Jul 9 - Chat - save chat sessions

This commit is contained in:
2026-07-09 13:40:56 -04:00
parent 71aef4fe8e
commit 5cf85c564b
12 changed files with 471 additions and 50 deletions
+51
View File
@@ -39,3 +39,54 @@ class SupportTicketReply(db.Model):
def __repr__(self):
return f'<SupportTicketReply {self.id} ticket={self.ticket_id}>'
# ── AI support-chat persistence (phase37) ─────────────────────────────────────
class SupportChatSession(db.Model):
"""One saved AI-chat conversation for a customer. Persisted so both the
customer and staff can reference past conversations and maintain continuity
(prior messages are reloaded into the chat window on return)."""
__tablename__ = 'support_chat_sessions'
id = db.Column(db.Integer, primary_key=True)
customer_id = db.Column(db.Integer, db.ForeignKey('users.id', ondelete='CASCADE'),
nullable=False, index=True)
created_at = db.Column(db.DateTime, default=now_eastern, nullable=False)
updated_at = db.Column(db.DateTime, default=now_eastern, nullable=False, index=True)
customer = db.relationship('User', foreign_keys=[customer_id])
messages = db.relationship(
'SupportChatMessage', backref='session',
cascade='all, delete-orphan',
order_by='SupportChatMessage.created_at',
lazy='dynamic',
)
@property
def message_count(self):
return self.messages.count()
@property
def preview(self):
"""First user message, for list views."""
first = self.messages.filter_by(role='user').first()
return first.content if first else '(no messages)'
def __repr__(self):
return f'<SupportChatSession {self.id} customer={self.customer_id}>'
class SupportChatMessage(db.Model):
"""A single turn in a SupportChatSession. role = 'user' | 'assistant'."""
__tablename__ = 'support_chat_messages'
id = db.Column(db.Integer, primary_key=True)
session_id = db.Column(db.Integer, db.ForeignKey('support_chat_sessions.id', ondelete='CASCADE'),
nullable=False, index=True)
role = db.Column(db.String(16), nullable=False) # 'user' | 'assistant'
content = db.Column(db.Text, nullable=False)
created_at = db.Column(db.DateTime, default=now_eastern, nullable=False)
def __repr__(self):
return f'<SupportChatMessage {self.id} session={self.session_id} role={self.role}>'