3131from services .errors .conversation import ConversationNotExistsError
3232
3333
34-
3534class CompletionConversationApi (Resource ):
3635 @setup_required
3736 @login_required
@@ -114,103 +113,107 @@ def delete(self, app_model):
114113 raise Forbidden ()
115114
116115 parser = reqparse .RequestParser ()
117- parser .add_argument (' conversation_ids' , type = str , action = ' append' , location = ' json' )
116+ parser .add_argument (" conversation_ids" , type = str , action = " append" , location = " json" )
118117 args = parser .parse_args ()
119118
120119 # If specific conversation IDs provided, delete only those; otherwise delete all
121- if args ['conversation_ids' ]:
122- conversation_ids = [str (id ) for id in args ['conversation_ids' ]]
123- conversations = db .session .query (Conversation ).filter (
124- Conversation .app_id == app_model .id ,
125- Conversation .mode == "completion" ,
126- Conversation .id .in_ (conversation_ids )
127- ).all ()
120+ if args ["conversation_ids" ]:
121+ conversation_ids = [str (id ) for id in args ["conversation_ids" ]]
122+ conversations = (
123+ db .session .query (Conversation )
124+ .filter (
125+ Conversation .app_id == app_model .id ,
126+ Conversation .mode == "completion" ,
127+ Conversation .id .in_ (conversation_ids ),
128+ )
129+ .all ()
130+ )
128131 else :
129132 # Get all conversations for this app
130- conversations = db .session .query (Conversation ).filter (
131- Conversation .app_id == app_model .id ,
132- Conversation .mode == "completion"
133- ).all ()
133+ conversations = (
134+ db .session .query (Conversation )
135+ .filter (Conversation .app_id == app_model .id , Conversation .mode == "completion" )
136+ .all ()
137+ )
134138
135139 # Collect all message IDs and upload file IDs first
136140 all_message_ids = []
137141 upload_file_ids = []
138-
142+
139143 for conversation in conversations :
140- message_ids = db .session .query (Message .id ).filter (Message .conversation_id == conversation .id ).all ()
144+ message_ids = db .session .query (Message .id ).where (Message .conversation_id == conversation .id ).all ()
141145 all_message_ids .extend ([msg_id [0 ] for msg_id in message_ids ])
142146
143147 # Collect upload file IDs for async deletion
144148 if all_message_ids :
145- message_files = db .session .query (MessageFile ).filter (MessageFile .message_id .in_ (all_message_ids )).all ()
149+ message_files = db .session .query (MessageFile ).where (MessageFile .message_id .in_ (all_message_ids )).all ()
146150 upload_file_ids = [mf .upload_file_id for mf in message_files if mf .upload_file_id ]
147151
148152 # Delete all database records first (in transaction)
149153 try :
150154 # Delete message-related database records
151155 if all_message_ids :
152156 try :
153- db .session .query (MessageFeedback ).filter (MessageFeedback .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
157+ db .session .query (MessageFeedback ).where (MessageFeedback .message_id .in_ (all_message_ids )).delete (
158+ synchronize_session = False
159+ )
154160 except Exception :
155161 pass # Table might not exist in this version
156162 try :
157- db .session .query (MessageFile ).filter (MessageFile .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
163+ db .session .query (MessageFile ).where (MessageFile .message_id .in_ (all_message_ids )).delete (
164+ synchronize_session = False
165+ )
158166 except Exception :
159167 pass
160168 try :
161- db .session .query (MessageChain ).filter (MessageChain .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
169+ db .session .query (MessageChain ).where (MessageChain .message_id .in_ (all_message_ids )).delete (
170+ synchronize_session = False
171+ )
162172 except Exception :
163173 pass
164174 try :
165- db .session .query (MessageAgentThought ).filter (MessageAgentThought .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
175+ db .session .query (MessageAgentThought ).where (MessageAgentThought .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
166176 except Exception :
167177 pass
168178
169179 # Delete messages, annotations, and conversation variables
170180 for conversation in conversations :
171- db .session .query (Message ).filter (Message .conversation_id == conversation .id ).delete ()
172- db .session .query (MessageAnnotation ).filter (
173- MessageAnnotation .conversation_id == conversation .id
174- ).delete ()
181+ db .session .query (Message ).where (Message .conversation_id == conversation .id ).delete ()
182+ db .session .query (MessageAnnotation ).where (MessageAnnotation .conversation_id == conversation .id ).delete ()
175183 try :
176- db .session .query (ConversationVariable ).filter (
177- ConversationVariable .conversation_id == conversation .id
178- ).delete ()
184+ db .session .query (ConversationVariable ).where (ConversationVariable .conversation_id == conversation .id ).delete ()
179185 except Exception :
180186 pass # Table might not exist in this version
181187 try :
182- db .session .query (ToolConversationVariables ).filter (
183- ToolConversationVariables .conversation_id == conversation .id
184- ).delete ()
188+ db .session .query (ToolConversationVariables ).where (ToolConversationVariables .conversation_id == conversation .id ).delete ()
185189 except Exception :
186190 pass # Table might not exist in this version
187191 try :
188- db .session .query (ToolFile ).filter (ToolFile .conversation_id == conversation .id ).delete ()
192+ db .session .query (ToolFile ).where (ToolFile .conversation_id == conversation .id ).delete ()
189193 except Exception :
190194 pass # Table might not exist in this version
191195 try :
192- db .session .query (PinnedConversation ).filter (
193- PinnedConversation .conversation_id == conversation .id
194- ).delete ()
196+ db .session .query (PinnedConversation ).where (PinnedConversation .conversation_id == conversation .id ).delete ()
195197 except Exception :
196198 pass # Table might not exist in this version
197199
198200 # Delete upload file records
199201 if upload_file_ids :
200- db .session .query (UploadFile ).filter (UploadFile .id .in_ (upload_file_ids )).delete (synchronize_session = False )
202+ db .session .query (UploadFile ).where (UploadFile .id .in_ (upload_file_ids )).delete (
203+ synchronize_session = False
204+ )
201205
202206 # Delete conversations
203- if args [' conversation_ids' ]:
204- conversation_ids = [str (id ) for id in args [' conversation_ids' ]]
207+ if args [" conversation_ids" ]:
208+ conversation_ids = [str (id ) for id in args [" conversation_ids" ]]
205209 db .session .query (Conversation ).filter (
206210 Conversation .app_id == app_model .id ,
207211 Conversation .mode == "completion" ,
208- Conversation .id .in_ (conversation_ids )
212+ Conversation .id .in_ (conversation_ids ),
209213 ).delete (synchronize_session = False )
210214 else :
211215 db .session .query (Conversation ).filter (
212- Conversation .app_id == app_model .id ,
213- Conversation .mode == "completion"
216+ Conversation .app_id == app_model .id , Conversation .mode == "completion"
214217 ).delete ()
215218
216219 # Commit all database changes first
@@ -219,6 +222,7 @@ def delete(self, app_model):
219222 # Schedule async file cleanup after successful database deletion
220223 if upload_file_ids :
221224 from tasks .clean_uploaded_files_task import clean_uploaded_files_task
225+
222226 clean_uploaded_files_task .delay (upload_file_ids )
223227
224228 except Exception as e :
@@ -229,7 +233,7 @@ def delete(self, app_model):
229233 "result" : "success" ,
230234 "conversations_deleted" : len (conversations ),
231235 "messages_deleted" : len (all_message_ids ),
232- "files_deleted" : len (upload_file_ids )
236+ "files_deleted" : len (upload_file_ids ),
233237 }
234238
235239
@@ -400,103 +404,109 @@ def delete(self, app_model):
400404 raise Forbidden ()
401405
402406 parser = reqparse .RequestParser ()
403- parser .add_argument (' conversation_ids' , type = str , action = ' append' , location = ' json' )
407+ parser .add_argument (" conversation_ids" , type = str , action = " append" , location = " json" )
404408 args = parser .parse_args ()
405409
406410 # If specific conversation IDs provided, delete only those; otherwise delete all
407- if args ['conversation_ids' ]:
408- conversation_ids = [str (id ) for id in args ['conversation_ids' ]]
409- conversations = db .session .query (Conversation ).filter (
410- Conversation .app_id == app_model .id ,
411- Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ]),
412- Conversation .id .in_ (conversation_ids )
413- ).all ()
411+ if args ["conversation_ids" ]:
412+ conversation_ids = [str (id ) for id in args ["conversation_ids" ]]
413+ conversations = (
414+ db .session .query (Conversation )
415+ .filter (
416+ Conversation .app_id == app_model .id ,
417+ Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ]),
418+ Conversation .id .in_ (conversation_ids ),
419+ )
420+ .all ()
421+ )
414422 else :
415423 # Get all conversations for this app
416- conversations = db .session .query (Conversation ).filter (
417- Conversation .app_id == app_model .id ,
418- Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ])
419- ).all ()
424+ conversations = (
425+ db .session .query (Conversation )
426+ .filter (
427+ Conversation .app_id == app_model .id , Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ])
428+ )
429+ .all ()
430+ )
420431
421432 # Collect all message IDs and upload file IDs first
422433 all_message_ids = []
423434 upload_file_ids = []
424-
435+
425436 for conversation in conversations :
426- message_ids = db .session .query (Message .id ).filter (Message .conversation_id == conversation .id ).all ()
437+ message_ids = db .session .query (Message .id ).where (Message .conversation_id == conversation .id ).all ()
427438 all_message_ids .extend ([msg_id [0 ] for msg_id in message_ids ])
428439
429440 # Collect upload file IDs for async deletion
430441 if all_message_ids :
431- message_files = db .session .query (MessageFile ).filter (MessageFile .message_id .in_ (all_message_ids )).all ()
442+ message_files = db .session .query (MessageFile ).where (MessageFile .message_id .in_ (all_message_ids )).all ()
432443 upload_file_ids = [mf .upload_file_id for mf in message_files if mf .upload_file_id ]
433444
434445 # Delete all database records first (in transaction)
435446 try :
436447 # Delete message-related database records
437448 if all_message_ids :
438449 try :
439- db .session .query (MessageFeedback ).filter (MessageFeedback .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
450+ db .session .query (MessageFeedback ).where (MessageFeedback .message_id .in_ (all_message_ids )).delete (
451+ synchronize_session = False
452+ )
440453 except Exception :
441454 pass # Table might not exist in this version
442455 try :
443- db .session .query (MessageFile ).filter (MessageFile .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
456+ db .session .query (MessageFile ).where (MessageFile .message_id .in_ (all_message_ids )).delete (
457+ synchronize_session = False
458+ )
444459 except Exception :
445460 pass
446461 try :
447- db .session .query (MessageChain ).filter (MessageChain .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
462+ db .session .query (MessageChain ).where (MessageChain .message_id .in_ (all_message_ids )).delete (
463+ synchronize_session = False
464+ )
448465 except Exception :
449466 pass
450467 try :
451- db .session .query (MessageAgentThought ).filter (MessageAgentThought .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
468+ db .session .query (MessageAgentThought ).where (MessageAgentThought .message_id .in_ (all_message_ids )).delete (synchronize_session = False )
452469 except Exception :
453470 pass
454471
455472 # Delete messages, annotations, and conversation variables
456473 for conversation in conversations :
457- db .session .query (Message ).filter (Message .conversation_id == conversation .id ).delete ()
458- db .session .query (MessageAnnotation ).filter (
459- MessageAnnotation .conversation_id == conversation .id
460- ).delete ()
474+ db .session .query (Message ).where (Message .conversation_id == conversation .id ).delete ()
475+ db .session .query (MessageAnnotation ).where (MessageAnnotation .conversation_id == conversation .id ).delete ()
461476 try :
462- db .session .query (ConversationVariable ).filter (
463- ConversationVariable .conversation_id == conversation .id
464- ).delete ()
477+ db .session .query (ConversationVariable ).where (ConversationVariable .conversation_id == conversation .id ).delete ()
465478 except Exception :
466479 pass # Table might not exist in this version
467480 try :
468- db .session .query (ToolConversationVariables ).filter (
469- ToolConversationVariables .conversation_id == conversation .id
470- ).delete ()
481+ db .session .query (ToolConversationVariables ).where (ToolConversationVariables .conversation_id == conversation .id ).delete ()
471482 except Exception :
472483 pass # Table might not exist in this version
473484 try :
474- db .session .query (ToolFile ).filter (ToolFile .conversation_id == conversation .id ).delete ()
485+ db .session .query (ToolFile ).where (ToolFile .conversation_id == conversation .id ).delete ()
475486 except Exception :
476487 pass # Table might not exist in this version
477488 try :
478- db .session .query (PinnedConversation ).filter (
479- PinnedConversation .conversation_id == conversation .id
480- ).delete ()
489+ db .session .query (PinnedConversation ).where (PinnedConversation .conversation_id == conversation .id ).delete ()
481490 except Exception :
482491 pass # Table might not exist in this version
483492
484493 # Delete upload file records
485494 if upload_file_ids :
486- db .session .query (UploadFile ).filter (UploadFile .id .in_ (upload_file_ids )).delete (synchronize_session = False )
495+ db .session .query (UploadFile ).where (UploadFile .id .in_ (upload_file_ids )).delete (
496+ synchronize_session = False
497+ )
487498
488499 # Delete conversations
489- if args [' conversation_ids' ]:
490- conversation_ids = [str (id ) for id in args [' conversation_ids' ]]
500+ if args [" conversation_ids" ]:
501+ conversation_ids = [str (id ) for id in args [" conversation_ids" ]]
491502 db .session .query (Conversation ).filter (
492503 Conversation .app_id == app_model .id ,
493504 Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ]),
494- Conversation .id .in_ (conversation_ids )
505+ Conversation .id .in_ (conversation_ids ),
495506 ).delete (synchronize_session = False )
496507 else :
497508 db .session .query (Conversation ).filter (
498- Conversation .app_id == app_model .id ,
499- Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ])
509+ Conversation .app_id == app_model .id , Conversation .mode .in_ (["chat" , "agent-chat" , "advanced-chat" ])
500510 ).delete ()
501511
502512 # Commit all database changes first
@@ -505,6 +515,7 @@ def delete(self, app_model):
505515 # Schedule async file cleanup after successful database deletion
506516 if upload_file_ids :
507517 from tasks .clean_uploaded_files_task import clean_uploaded_files_task
518+
508519 clean_uploaded_files_task .delay (upload_file_ids )
509520
510521 except Exception as e :
@@ -515,7 +526,7 @@ def delete(self, app_model):
515526 "result" : "success" ,
516527 "conversations_deleted" : len (conversations ),
517528 "messages_deleted" : len (all_message_ids ),
518- "files_deleted" : len (upload_file_ids )
529+ "files_deleted" : len (upload_file_ids ),
519530 }
520531
521532
0 commit comments