update the chat state version with messages

This commit is contained in:
CREDO23 2025-10-24 01:34:26 +02:00 committed by thierryverse
parent 13342eb823
commit cc3e04031e
2 changed files with 44 additions and 27 deletions

View file

@ -259,9 +259,10 @@ async def update_chat(
db_chat = await read_chat(chat_id, session, user) db_chat = await read_chat(chat_id, session, user)
update_data = chat_update.model_dump(exclude_unset=True) update_data = chat_update.model_dump(exclude_unset=True)
for key, value in update_data.items(): for key, value in update_data.items():
if key == "messages":
db_chat.state_version = len(update_data["messages"])
setattr(db_chat, key, value) 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.commit()
await session.refresh(db_chat) await session.refresh(db_chat)
return db_chat return db_chat

View file

@ -139,6 +139,22 @@ async def generate_chat_podcast(
}, },
) )
# 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()
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( podcast = Podcast(
title=f"{podcast_title}", title=f"{podcast_title}",
podcast_transcript=serializable_transcript, podcast_transcript=serializable_transcript,