Skip to content

Commit

Permalink
Properly delete conversation data source / data source view (#9639)
Browse files Browse the repository at this point in the history
  • Loading branch information
spolu authored Dec 28, 2024
1 parent 4ba6c69 commit c64990c
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion front/lib/api/assistant/conversation/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { LightWorkspaceType, ModelId } from "@dust-tt/types";
import { removeNulls } from "@dust-tt/types";
import { chunk } from "lodash";

import { Authenticator } from "@app/lib/auth";
import { AgentBrowseAction } from "@app/lib/models/assistant/actions/browse";
import { AgentConversationIncludeFileAction } from "@app/lib/models/assistant/actions/conversation/include_file";
import { AgentDustAppRunAction } from "@app/lib/models/assistant/actions/dust_app_run";
Expand All @@ -21,8 +22,11 @@ import {
UserMessage,
} from "@app/lib/models/assistant/conversation";
import { ContentFragmentResource } from "@app/lib/resources/content_fragment_resource";
import { DataSourceResource } from "@app/lib/resources/data_source_resource";
import { RetrievalDocumentResource } from "@app/lib/resources/retrieval_document_resource";

import { getConversationWithoutContent } from "./without_content";

const DESTROY_MESSAGE_BATCH = 50;

async function destroyActionsRelatedResources(agentMessageIds: Array<ModelId>) {
Expand Down Expand Up @@ -122,6 +126,33 @@ async function destroyContentFragments(
}
}

async function destroyConversationDataSource({
workspaceId,
conversationId,
}: {
workspaceId: string;
conversationId: string;
}) {
// We need an authenticator to interact with the data source resource.
const auth = await Authenticator.internalAdminForWorkspace(workspaceId);
const conversation = await getConversationWithoutContent(
auth,
conversationId
);
if (conversation.isErr()) {
throw conversation.error;
}

const dataSource = await DataSourceResource.fetchByConversation(
auth,
conversation.value
);

if (dataSource) {
await dataSource.delete(auth, { hardDelete: true });
}
}

// This belongs to the ConversationResource.
export async function destroyConversation(
workspace: LightWorkspaceType,
Expand Down Expand Up @@ -172,8 +203,8 @@ export async function destroyConversation(
});

await destroyContentFragments(messageAndContentFragmentIds, {
conversationId: conversation.sId,
workspaceId: workspace.sId,
conversationId: conversation.sId,
});

await destroyMessageRelatedResources(messageIds);
Expand All @@ -183,5 +214,10 @@ export async function destroyConversation(
where: { conversationId: conversationId },
});

await destroyConversationDataSource({
workspaceId: workspace.sId,
conversationId: conversation.sId,
});

await conversation.destroy();
}

0 comments on commit c64990c

Please sign in to comment.