Skip to content

Commit 3dd03a8

Browse files
committed
update the chat state version with messages
1 parent a153669 commit 3dd03a8

2 files changed

Lines changed: 44 additions & 27 deletions

File tree

surfsense_backend/app/routes/chats_routes.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,10 @@ async def update_chat(
259259
db_chat = await read_chat(chat_id, session, user)
260260
update_data = chat_update.model_dump(exclude_unset=True)
261261
for key, value in update_data.items():
262+
if key == "messages":
263+
db_chat.state_version = len(update_data["messages"])
262264
setattr(db_chat, key, value)
263-
# Increment state_version when chat is modified
264-
db_chat.state_version = (db_chat.state_version or 0) + 1
265+
265266
await session.commit()
266267
await session.refresh(db_chat)
267268
return db_chat

surfsense_backend/app/tasks/podcast_tasks.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,51 @@ async def generate_chat_podcast(
139139
},
140140
)
141141

142-
podcast = Podcast(
143-
title=f"{podcast_title}",
144-
podcast_transcript=serializable_transcript,
145-
file_location=result["final_podcast_file_path"],
146-
search_space_id=search_space_id,
147-
chat_state_version=chat.state_version,
148-
chat_id=chat.id,
142+
# check if podcast already exists for this chat with the same title (re-generation)
143+
existing_podcast = await session.execute(
144+
select(Podcast).filter(
145+
Podcast.chat_id == chat_id, Podcast.title == podcast_title
146+
)
149147
)
148+
existing_podcast = existing_podcast.scalars().first()
149+
150+
if existing_podcast:
151+
existing_podcast.podcast_transcript = serializable_transcript
152+
existing_podcast.file_location = result["final_podcast_file_path"]
153+
existing_podcast.chat_state_version = chat.state_version
154+
await session.commit()
155+
await session.refresh(existing_podcast)
156+
return existing_podcast
157+
else:
158+
podcast = Podcast(
159+
title=f"{podcast_title}",
160+
podcast_transcript=serializable_transcript,
161+
file_location=result["final_podcast_file_path"],
162+
search_space_id=search_space_id,
163+
chat_state_version=chat.state_version,
164+
chat_id=chat.id,
165+
)
150166

151-
# Add to session and commit
152-
session.add(podcast)
153-
await session.commit()
154-
await session.refresh(podcast)
167+
# Add to session and commit
168+
session.add(podcast)
169+
await session.commit()
170+
await session.refresh(podcast)
155171

156-
# Log success
157-
await task_logger.log_task_success(
158-
log_entry,
159-
f"Successfully generated podcast for chat {chat_id}",
160-
{
161-
"podcast_id": podcast.id,
162-
"podcast_title": podcast_title,
163-
"transcript_entries": len(serializable_transcript),
164-
"file_location": result.get("final_podcast_file_path"),
165-
"processed_messages": processed_messages,
166-
"content_length": len(chat_history_str),
167-
},
168-
)
172+
# Log success
173+
await task_logger.log_task_success(
174+
log_entry,
175+
f"Successfully generated podcast for chat {chat_id}",
176+
{
177+
"podcast_id": podcast.id,
178+
"podcast_title": podcast_title,
179+
"transcript_entries": len(serializable_transcript),
180+
"file_location": result.get("final_podcast_file_path"),
181+
"processed_messages": processed_messages,
182+
"content_length": len(chat_history_str),
183+
},
184+
)
169185

170-
return podcast
186+
return podcast
171187

172188
except ValueError as ve:
173189
# ValueError is already logged above for chat not found

0 commit comments

Comments
 (0)