@@ -69,6 +69,7 @@ import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resourc
69
69
import { UserResource } from "@app/lib/resources/user_resource" ;
70
70
import { renderLightWorkspaceType } from "@app/lib/workspace" ;
71
71
import logger from "@app/logger/logger" ;
72
+ import { deleteAllConversations } from "@app/temporal/scrub_workspace/activities" ;
72
73
73
74
const hardDeleteLogger = logger . child ( { activity : "hard-delete" } ) ;
74
75
@@ -172,7 +173,7 @@ export async function isWorkflowDeletableActivity({
172
173
workspaceId : string ;
173
174
} ) {
174
175
const auth = await Authenticator . internalAdminForWorkspace ( workspaceId ) ;
175
- const workspace = await auth . getNonNullableWorkspace ( ) ;
176
+ const workspace = auth . getNonNullableWorkspace ( ) ;
176
177
177
178
return areAllSubscriptionsCanceled ( renderLightWorkspaceType ( { workspace } ) ) ;
178
179
}
@@ -183,146 +184,7 @@ export async function deleteConversationsActivity({
183
184
workspaceId : string ;
184
185
} ) {
185
186
const auth = await Authenticator . internalAdminForWorkspace ( workspaceId ) ;
186
- const workspace = auth . workspace ( ) ;
187
-
188
- if ( ! workspace ) {
189
- throw new Error ( "Could not find the workspace." ) ;
190
- }
191
-
192
- const conversations = await Conversation . findAll ( {
193
- where : {
194
- workspaceId : workspace . id ,
195
- } ,
196
- } ) ;
197
- const chunkSize = 8 ;
198
- const chunks : Conversation [ ] [ ] = [ ] ;
199
- for ( let i = 0 ; i < conversations . length ; i += chunkSize ) {
200
- chunks . push ( conversations . slice ( i , i + chunkSize ) ) ;
201
- }
202
-
203
- await frontSequelize . transaction ( async ( t ) => {
204
- for ( let i = 0 ; i < chunks . length ; i ++ ) {
205
- const chunk = chunks [ i ] ;
206
- if ( ! chunk ) {
207
- continue ;
208
- }
209
- await Promise . all (
210
- chunk . map ( ( c ) => {
211
- return ( async ( ) : Promise < void > => {
212
- const messages = await Message . findAll ( {
213
- where : { conversationId : c . id } ,
214
- transaction : t ,
215
- } ) ;
216
- for ( const msg of messages ) {
217
- if ( msg . userMessageId ) {
218
- await UserMessage . destroy ( {
219
- where : { id : msg . userMessageId } ,
220
- transaction : t ,
221
- } ) ;
222
- }
223
- if ( msg . agentMessageId ) {
224
- const agentMessage = await AgentMessage . findOne ( {
225
- where : { id : msg . agentMessageId } ,
226
- transaction : t ,
227
- } ) ;
228
- if ( agentMessage ) {
229
- const retrievalAction = await AgentRetrievalAction . findOne ( {
230
- where : {
231
- agentMessageId : agentMessage . id ,
232
- } ,
233
- transaction : t ,
234
- } ) ;
235
- if ( retrievalAction ) {
236
- await RetrievalDocumentResource . deleteAllForActions ( [
237
- retrievalAction . id ,
238
- ] ) ;
239
-
240
- await AgentRetrievalAction . destroy ( {
241
- where : { id : retrievalAction . id } ,
242
- transaction : t ,
243
- } ) ;
244
- }
245
-
246
- await AgentMessageContent . destroy ( {
247
- where : { agentMessageId : agentMessage . id } ,
248
- transaction : t ,
249
- } ) ;
250
-
251
- await AgentMessageFeedback . destroy ( {
252
- where : { agentMessageId : agentMessage . id } ,
253
- transaction : t ,
254
- } ) ;
255
-
256
- // Delete associated actions.
257
-
258
- await AgentBrowseAction . destroy ( {
259
- where : { agentMessageId : agentMessage . id } ,
260
- transaction : t ,
261
- } ) ;
262
-
263
- await AgentProcessAction . destroy ( {
264
- where : { agentMessageId : agentMessage . id } ,
265
- transaction : t ,
266
- } ) ;
267
-
268
- await AgentTablesQueryAction . destroy ( {
269
- where : { agentMessageId : agentMessage . id } ,
270
- transaction : t ,
271
- } ) ;
272
-
273
- await AgentWebsearchAction . destroy ( {
274
- where : { agentMessageId : agentMessage . id } ,
275
- transaction : t ,
276
- } ) ;
277
-
278
- await agentMessage . destroy ( { transaction : t } ) ;
279
- }
280
- }
281
- if ( msg . contentFragmentId ) {
282
- const contentFragment =
283
- await ContentFragmentResource . fetchByModelId (
284
- msg . contentFragmentId ,
285
- t
286
- ) ;
287
- if ( contentFragment ) {
288
- await contentFragment . destroy (
289
- {
290
- conversationId : c . sId ,
291
- messageId : msg . sId ,
292
- workspaceId : workspace . sId ,
293
- } ,
294
- t
295
- ) ;
296
- }
297
- }
298
- await MessageReaction . destroy ( {
299
- where : { messageId : msg . id } ,
300
- transaction : t ,
301
- } ) ;
302
- await Mention . destroy ( {
303
- where : { messageId : msg . id } ,
304
- transaction : t ,
305
- } ) ;
306
- await msg . destroy ( { transaction : t } ) ;
307
- }
308
- await ConversationParticipant . destroy ( {
309
- where : { conversationId : c . id } ,
310
- transaction : t ,
311
- } ) ;
312
-
313
- hardDeleteLogger . info (
314
- {
315
- conversationId : c . sId ,
316
- } ,
317
- "Deleting conversation"
318
- ) ;
319
-
320
- await c . destroy ( { transaction : t } ) ;
321
- } ) ( ) ;
322
- } )
323
- ) ;
324
- }
325
- } ) ;
187
+ await deleteAllConversations ( auth ) ;
326
188
}
327
189
328
190
export async function deleteAgentsActivity ( {
0 commit comments