mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-15 18:25:18 +02:00
update the chat state version with messages
This commit is contained in:
parent
13342eb823
commit
cc3e04031e
2 changed files with 44 additions and 27 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -139,35 +139,51 @@ async def generate_chat_podcast(
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
podcast = Podcast(
|
# check if podcast already exists for this chat with the same title (re-generation)
|
||||||
title=f"{podcast_title}",
|
existing_podcast = await session.execute(
|
||||||
podcast_transcript=serializable_transcript,
|
select(Podcast).filter(
|
||||||
file_location=result["final_podcast_file_path"],
|
Podcast.chat_id == chat_id, Podcast.title == podcast_title
|
||||||
search_space_id=search_space_id,
|
)
|
||||||
chat_state_version=chat.state_version,
|
|
||||||
chat_id=chat.id,
|
|
||||||
)
|
)
|
||||||
|
existing_podcast = existing_podcast.scalars().first()
|
||||||
|
|
||||||
# Add to session and commit
|
if existing_podcast:
|
||||||
session.add(podcast)
|
existing_podcast.podcast_transcript = serializable_transcript
|
||||||
await session.commit()
|
existing_podcast.file_location = result["final_podcast_file_path"]
|
||||||
await session.refresh(podcast)
|
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
|
# Add to session and commit
|
||||||
await task_logger.log_task_success(
|
session.add(podcast)
|
||||||
log_entry,
|
await session.commit()
|
||||||
f"Successfully generated podcast for chat {chat_id}",
|
await session.refresh(podcast)
|
||||||
{
|
|
||||||
"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
|
# 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:
|
except ValueError as ve:
|
||||||
# ValueError is already logged above for chat not found
|
# ValueError is already logged above for chat not found
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue