diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index ad9a28778..212a5573a 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -44,6 +44,10 @@ class ChatForm(BaseModel): chat: dict +class ChatTitleForm(BaseModel): + title: str + + class ChatResponse(BaseModel): id: str user_id: str @@ -93,6 +97,20 @@ class ChatTable: except: return None + def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]: + try: + query = Chat.update( + chat=json.dumps(chat), + title=chat["title"] if "title" in chat else "New Chat", + timestamp=int(time.time()), + ).where(Chat.id == id) + query.execute() + + chat = Chat.get(Chat.id == id) + return ChatModel(**model_to_dict(chat)) + except: + return None + def get_chat_lists_by_user_id( self, user_id: str, skip: int = 0, limit: int = 50 ) -> List[ChatModel]: diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index 3191db8be..ccb3a32b7 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -11,6 +11,7 @@ from apps.web.models.users import Users from apps.web.models.chats import ( ChatModel, ChatResponse, + ChatTitleForm, ChatForm, ChatTitleIdResponse, Chats, @@ -95,7 +96,9 @@ async def update_chat_by_id(id: str, form_data: ChatForm, cred=Depends(bearer_sc if user: chat = Chats.get_chat_by_id_and_user_id(id, user.id) if chat: - chat = Chats.update_chat_by_id(id, form_data.chat) + updated_chat = {**json.loads(chat.chat), **form_data.chat} + + chat = Chats.update_chat_by_id(id, updated_chat) return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) else: raise HTTPException( diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index 072ade46e..6b6e66ebd 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -254,6 +254,7 @@ }; const rateMessage = async (messageIdx, rating) => { + // TODO: Move this function to parent messages = messages.map((message, idx) => { if (messageIdx === idx) { message.rating = rating;