Skip to content

Commit a90b240

Browse files
authored
scrub/delete workflows: share conversation deletion logic (#9643)
* scrub/delete workflows: share conversation deletion logic * nit
1 parent 337094a commit a90b240

File tree

3 files changed

+9
-150
lines changed

3 files changed

+9
-150
lines changed

front/lib/api/assistant/conversation/destroy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,14 @@ async function destroyContentFragments(
127127
}
128128

129129
async function destroyConversationDataSource({
130-
workspaceId,
130+
workspace,
131131
conversationId,
132132
}: {
133-
workspaceId: string;
133+
workspace: LightWorkspaceType;
134134
conversationId: string;
135135
}) {
136136
// We need an authenticator to interact with the data source resource.
137-
const auth = await Authenticator.internalAdminForWorkspace(workspaceId);
137+
const auth = await Authenticator.internalAdminForWorkspace(workspace.sId);
138138
const conversation = await getConversationWithoutContent(
139139
auth,
140140
conversationId,
@@ -216,7 +216,7 @@ export async function destroyConversation(
216216
});
217217

218218
await destroyConversationDataSource({
219-
workspaceId: workspace.sId,
219+
workspace,
220220
conversationId: conversation.sId,
221221
});
222222

front/poke/temporal/activities.ts

Lines changed: 3 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ import { TrackerConfigurationResource } from "@app/lib/resources/tracker_resourc
6969
import { UserResource } from "@app/lib/resources/user_resource";
7070
import { renderLightWorkspaceType } from "@app/lib/workspace";
7171
import logger from "@app/logger/logger";
72+
import { deleteAllConversations } from "@app/temporal/scrub_workspace/activities";
7273

7374
const hardDeleteLogger = logger.child({ activity: "hard-delete" });
7475

@@ -172,7 +173,7 @@ export async function isWorkflowDeletableActivity({
172173
workspaceId: string;
173174
}) {
174175
const auth = await Authenticator.internalAdminForWorkspace(workspaceId);
175-
const workspace = await auth.getNonNullableWorkspace();
176+
const workspace = auth.getNonNullableWorkspace();
176177

177178
return areAllSubscriptionsCanceled(renderLightWorkspaceType({ workspace }));
178179
}
@@ -183,146 +184,7 @@ export async function deleteConversationsActivity({
183184
workspaceId: string;
184185
}) {
185186
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);
326188
}
327189

328190
export async function deleteAgentsActivity({

front/temporal/scrub_workspace/activities.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ export async function pauseAllConnectors({
115115
}
116116
}
117117

118-
async function deleteAllConversations(auth: Authenticator) {
119-
const workspace = auth.workspace();
120-
if (!workspace) {
121-
throw new Error("No workspace found");
122-
}
118+
export async function deleteAllConversations(auth: Authenticator) {
119+
const workspace = auth.getNonNullableWorkspace();
123120
const conversations = await Conversation.findAll({
124121
where: { workspaceId: workspace.id },
125122
});

0 commit comments

Comments
 (0)