Skip to content

Commit

Permalink
Conversations are marked as deleted rather than destroyed (#1766)
Browse files Browse the repository at this point in the history
* Prevent destroy + add "deleted" status

formatting

* Filter out deleted conversations unless explicitly mentioned

* formatting

* Review
  • Loading branch information
philipperolet authored Sep 25, 2023
1 parent cce01aa commit 03dc256
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
10 changes: 7 additions & 3 deletions connectors/src/lib/dust_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ const PostMessagesRequestBodySchemaIoTs = t.type({

const PostConversationsRequestBodySchemaIoTs = t.type({
title: t.union([t.string, t.null]),
visibility: t.union([t.literal("unlisted"), t.literal("workspace")]),
visibility: t.union([
t.literal("unlisted"),
t.literal("workspace"),
t.literal("deleted"),
]),
message: PostMessagesRequestBodySchemaIoTs,
});

Expand Down Expand Up @@ -190,7 +194,7 @@ export type GenerationSuccessEvent = {
text: string;
};

export type ConversationVisibility = "unlisted" | "workspace";
export type ConversationVisibility = "unlisted" | "workspace" | "deleted";

/**
* Expresses limits for usage of the product Any positive number enforces the limit, -1 means no
Expand Down Expand Up @@ -557,7 +561,7 @@ export class DustAPI {

async createConversation(
title: string | null,
visibility: "unlisted" | "workspace",
visibility: ConversationVisibility,
message: PostMessagesRequestBodySchema
) {
const requestPayload: PostConversationsRequestBodySchema = {
Expand Down
36 changes: 31 additions & 5 deletions front/lib/api/assistant/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Op } from "sequelize";

import {
cloneBaseConfig,
DustProdActionRegistry,
Expand Down Expand Up @@ -103,6 +105,7 @@ export async function updateConversation(
where: {
sId: conversationId,
workspaceId: auth.workspace()?.id,
visibility: { [Op.ne]: "deleted" },
},
});

Expand All @@ -124,9 +127,19 @@ export async function updateConversation(
return c;
}

/**
* Mark the conversation as deleted, but does not remove it from database
* unless destroy is explicitly set to true
*/
export async function deleteConversation(
auth: Authenticator,
conversationId: string
{
conversationId,
destroy,
}: {
conversationId: string;
destroy?: boolean;
}
): Promise<void> {
const owner = auth.workspace();
if (!owner) {
Expand All @@ -137,14 +150,21 @@ export async function deleteConversation(
where: {
sId: conversationId,
workspaceId: auth.workspace()?.id,
visibility: { [Op.ne]: "deleted" },
},
});

if (!conversation) {
throw new Error(`Conversation ${conversationId} not found`);
}

await conversation.destroy();
if (destroy) {
await conversation.destroy();
} else {
await conversation.update({
visibility: "deleted",
});
}
}

/**
Expand Down Expand Up @@ -279,7 +299,8 @@ async function renderAgentMessage(
}

export async function getUserConversations(
auth: Authenticator
auth: Authenticator,
includeDeleted?: boolean
): Promise<ConversationWithoutContentType[]> {
const owner = auth.workspace();
const user = auth.user();
Expand Down Expand Up @@ -311,7 +332,10 @@ export async function getUserConversations(
logger.error("Participation without conversation");
return acc;
}
if (p.conversation.workspaceId !== owner.id) {
if (
p.conversation.workspaceId !== owner.id ||
(p.conversation.visibility === "deleted" && !includeDeleted)
) {
return acc;
}

Expand All @@ -333,7 +357,8 @@ export async function getUserConversations(

export async function getConversation(
auth: Authenticator,
conversationId: string
conversationId: string,
includeDeleted?: boolean
): Promise<ConversationType | null> {
const owner = auth.workspace();
if (!owner) {
Expand All @@ -344,6 +369,7 @@ export async function getConversation(
where: {
sId: conversationId,
workspaceId: owner.id,
...(includeDeleted ? {} : { visibility: { [Op.ne]: "deleted" } }),
},
});

Expand Down
6 changes: 5 additions & 1 deletion front/pages/api/v1/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import { ConversationType } from "@app/types/assistant/conversation";

const PostConversationsRequestBodySchema = t.type({
title: t.union([t.string, t.null]),
visibility: t.union([t.literal("unlisted"), t.literal("workspace")]),
visibility: t.union([
t.literal("unlisted"),
t.literal("workspace"),
t.literal("deleted"),
]),
message: t.union([PostMessagesRequestBodySchema, t.null]),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { ConversationType } from "@app/types/assistant/conversation";

export const PatchConversationsRequestBodySchema = t.type({
title: t.union([t.string, t.null]),
visibility: t.union([t.literal("unlisted"), t.literal("workspace")]),
visibility: t.union([
t.literal("unlisted"),
t.literal("workspace"),
t.literal("deleted"),
]),
});

export type GetConversationsResponseBody = {
Expand Down Expand Up @@ -92,7 +96,7 @@ async function handler(
return;

case "DELETE":
await deleteConversation(auth, conversation.sId);
await deleteConversation(auth, { conversationId: conversation.sId });

res.status(200).end();
return;
Expand Down
6 changes: 5 additions & 1 deletion front/pages/api/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import {

export const PostConversationsRequestBodySchema = t.type({
title: t.union([t.string, t.null]),
visibility: t.union([t.literal("unlisted"), t.literal("workspace")]),
visibility: t.union([
t.literal("unlisted"),
t.literal("workspace"),
t.literal("deleted"),
]),
message: t.union([PostMessagesRequestBodySchema, t.null]),
});

Expand Down
2 changes: 1 addition & 1 deletion front/types/assistant/conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function isAgentMessageType(
* Conversations
*/

export type ConversationVisibility = "unlisted" | "workspace";
export type ConversationVisibility = "unlisted" | "workspace" | "deleted";

/**
* content [][] structure is intended to allow retries (of agent messages) or edits (of user
Expand Down

0 comments on commit 03dc256

Please sign in to comment.