@@ -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