This commit is contained in:
XIE7654
2025-07-18 10:39:05 +08:00
parent 66d5971570
commit aef25112f6
16 changed files with 83 additions and 74 deletions

View File

@@ -4,10 +4,13 @@ from datetime import datetime
from models.ai import ChatConversation, ChatMessage, MessageType
class ChatDBService:
@staticmethod
def get_conversation(db: Session, conversation_id: int):
return db.query(ChatConversation).filter(ChatConversation.id == conversation_id).first()
@staticmethod
def get_or_create_conversation(db: Session, conversation_id: int | None, user_id: int, model: str, content: str) -> ChatConversation:
if not conversation_id:
print(conversation_id, 'conversation_id')
conversation = ChatConversation(
title=content,
user_id=user_id,
@@ -32,6 +35,17 @@ class ChatDBService:
raise ValueError("无效的conversation_id")
return conversation
@staticmethod
def update_conversation_title(db, conversation_id: int, title: str):
conversation = db.query(ChatConversation).filter(ChatConversation.id == conversation_id).first()
if conversation:
conversation.title = title[:255] # 保证不超过255字符
db.add(conversation)
db.commit()
return conversation
else:
raise ValueError("Conversation not found")
@staticmethod
def add_message(db: Session, conversation: ChatConversation, user_id: int, content: str) -> ChatMessage:
message = ChatMessage(