From b53a7d530fa50a6e76b70f6df4909f6e9a7cb4b9 Mon Sep 17 00:00:00 2001 From: Jules Belveze <32683010+JulesBelveze@users.noreply.github.com> Date: Tue, 10 Dec 2024 11:04:57 +0100 Subject: [PATCH] [front] - chore: agent message deletion relation (#9152) * [front/lib/api/assistant/conversation] - feature: add agent message content deletion in conversation destruction - Include AgentMessageContent destruction in the `destroyConversation` flow to ensure clean-up of message contents [front/lib/models/assistant] - refactor: change delete behavior for AgentMessageContent associations - Modify onDelete and onUpdate behavior from "CASCADE" to "RESTRICT" to prevent automatic deletion or updates [front/poke/temporal] - feature: integrate agent message content deletion in temporal activities - Implement deletion of AgentMessageContent within the deleteConversationsActivity to maintain consistency * [db] - fix: update foreign key constraint on agent_message_contents - Drop the old foreign key constraint on agentMessageId - Re-add the foreign key constraint to enforce referential integrity with agent_messages table with both DELETE and UPDATE actions restricted * [migrations] - refactor: rename migration file from 125 to 126 - Incremented migration file to reflect the correct sequencing in migrations directory * [migrations] - refactor: rename migration file from 126 to 128 - Incremented migration file to reflect the correct sequencing in migrations directory --------- Co-authored-by: Aubin --- front/lib/api/assistant/conversation/destroy.ts | 4 ++++ front/lib/models/assistant/agent_message_content.ts | 6 ++++-- front/migrations/db/migration_128.sql | 7 +++++++ front/poke/temporal/activities.ts | 6 ++++++ 4 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 front/migrations/db/migration_128.sql diff --git a/front/lib/api/assistant/conversation/destroy.ts b/front/lib/api/assistant/conversation/destroy.ts index dab4315716f9..3f8cec95d33a 100644 --- a/front/lib/api/assistant/conversation/destroy.ts +++ b/front/lib/api/assistant/conversation/destroy.ts @@ -8,6 +8,7 @@ import { AgentProcessAction } from "@app/lib/models/assistant/actions/process"; import { AgentRetrievalAction } from "@app/lib/models/assistant/actions/retrieval"; import { AgentTablesQueryAction } from "@app/lib/models/assistant/actions/tables_query"; import { AgentWebsearchAction } from "@app/lib/models/assistant/actions/websearch"; +import { AgentMessageContent } from "@app/lib/models/assistant/agent_message_content"; import type { Conversation } from "@app/lib/models/assistant/conversation"; import { AgentMessage, @@ -156,6 +157,9 @@ export async function destroyConversation( await UserMessage.destroy({ where: { id: userMessageIds }, }); + await AgentMessageContent.destroy({ + where: { agentMessageId: agentMessageIds }, + }); await AgentMessageFeedback.destroy({ where: { agentMessageId: agentMessageIds }, }); diff --git a/front/lib/models/assistant/agent_message_content.ts b/front/lib/models/assistant/agent_message_content.ts index 0fe209be3eee..dff73e28ef48 100644 --- a/front/lib/models/assistant/agent_message_content.ts +++ b/front/lib/models/assistant/agent_message_content.ts @@ -58,7 +58,8 @@ AgentMessageContent.belongsTo(AgentMessage, { name: "agentMessageId", allowNull: false, }, - onDelete: "CASCADE", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", }); AgentMessage.hasMany(AgentMessageContent, { @@ -67,5 +68,6 @@ AgentMessage.hasMany(AgentMessageContent, { name: "agentMessageId", allowNull: false, }, - onDelete: "CASCADE", + onDelete: "RESTRICT", + onUpdate: "RESTRICT", }); diff --git a/front/migrations/db/migration_128.sql b/front/migrations/db/migration_128.sql new file mode 100644 index 000000000000..f37b194c38d5 --- /dev/null +++ b/front/migrations/db/migration_128.sql @@ -0,0 +1,7 @@ +ALTER TABLE "agent_message_contents" + DROP CONSTRAINT "agent_message_contents_agentMessageId_fkey", + ADD CONSTRAINT "agent_message_contents_agentMessageId_fkey" + FOREIGN KEY ("agentMessageId") + REFERENCES "agent_messages"("id") + ON DELETE RESTRICT + ON UPDATE RESTRICT; \ No newline at end of file diff --git a/front/poke/temporal/activities.ts b/front/poke/temporal/activities.ts index 6d9511ad0703..4473b3bd79e7 100644 --- a/front/poke/temporal/activities.ts +++ b/front/poke/temporal/activities.ts @@ -63,6 +63,7 @@ import { UserMetadataModel } from "@app/lib/resources/storage/models/user"; import { UserResource } from "@app/lib/resources/user_resource"; import { renderLightWorkspaceType } from "@app/lib/workspace"; import logger from "@app/logger/logger"; +import { AgentMessageContent } from "@app/lib/models/assistant/agent_message_content"; const hardDeleteLogger = logger.child({ activity: "hard-delete" }); @@ -237,6 +238,11 @@ export async function deleteConversationsActivity({ }); } + await AgentMessageContent.destroy({ + where: { agentMessageId: agentMessage.id }, + transaction: t, + }); + await AgentMessageFeedback.destroy({ where: { agentMessageId: agentMessage.id }, transaction: t,