diff --git a/surfsense_backend/app/routes/chats_routes.py b/surfsense_backend/app/routes/chats_routes.py index eec5228f3..032a461de 100644 --- a/surfsense_backend/app/routes/chats_routes.py +++ b/surfsense_backend/app/routes/chats_routes.py @@ -259,9 +259,10 @@ async def update_chat( db_chat = await read_chat(chat_id, session, user) update_data = chat_update.model_dump(exclude_unset=True) for key, value in update_data.items(): + if key == "messages": + db_chat.state_version = len(update_data["messages"]) setattr(db_chat, key, value) - # Increment state_version when chat is modified - db_chat.state_version = (db_chat.state_version or 0) + 1 + await session.commit() await session.refresh(db_chat) return db_chat diff --git a/surfsense_backend/app/tasks/podcast_tasks.py b/surfsense_backend/app/tasks/podcast_tasks.py index e333b4d1b..de65ca5e2 100644 --- a/surfsense_backend/app/tasks/podcast_tasks.py +++ b/surfsense_backend/app/tasks/podcast_tasks.py @@ -139,35 +139,51 @@ async def generate_chat_podcast( }, ) - podcast = Podcast( - title=f"{podcast_title}", - podcast_transcript=serializable_transcript, - file_location=result["final_podcast_file_path"], - search_space_id=search_space_id, - chat_state_version=chat.state_version, - chat_id=chat.id, + # check if podcast already exists for this chat with the same title (re-generation) + existing_podcast = await session.execute( + select(Podcast).filter( + Podcast.chat_id == chat_id, Podcast.title == podcast_title + ) ) + existing_podcast = existing_podcast.scalars().first() - # Add to session and commit - session.add(podcast) - await session.commit() - await session.refresh(podcast) + if existing_podcast: + existing_podcast.podcast_transcript = serializable_transcript + existing_podcast.file_location = result["final_podcast_file_path"] + existing_podcast.chat_state_version = chat.state_version + await session.commit() + await session.refresh(existing_podcast) + return existing_podcast + else: + podcast = Podcast( + title=f"{podcast_title}", + podcast_transcript=serializable_transcript, + file_location=result["final_podcast_file_path"], + search_space_id=search_space_id, + chat_state_version=chat.state_version, + chat_id=chat.id, + ) - # Log success - await task_logger.log_task_success( - log_entry, - f"Successfully generated podcast for chat {chat_id}", - { - "podcast_id": podcast.id, - "podcast_title": podcast_title, - "transcript_entries": len(serializable_transcript), - "file_location": result.get("final_podcast_file_path"), - "processed_messages": processed_messages, - "content_length": len(chat_history_str), - }, - ) + # Add to session and commit + session.add(podcast) + await session.commit() + await session.refresh(podcast) - return podcast + # Log success + await task_logger.log_task_success( + log_entry, + f"Successfully generated podcast for chat {chat_id}", + { + "podcast_id": podcast.id, + "podcast_title": podcast_title, + "transcript_entries": len(serializable_transcript), + "file_location": result.get("final_podcast_file_path"), + "processed_messages": processed_messages, + "content_length": len(chat_history_str), + }, + ) + + return podcast except ValueError as ve: # ValueError is already logged above for chat not found