Skip to content

Commit

Permalink
[front] - chore: agent message deletion relation (#9152)
Browse files Browse the repository at this point in the history
* [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 <aubin@dust.tt>
  • Loading branch information
JulesBelveze and aubin-tchoi authored Dec 10, 2024
1 parent 7e4d33f commit b53a7d5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions front/lib/api/assistant/conversation/destroy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 },
});
Expand Down
6 changes: 4 additions & 2 deletions front/lib/models/assistant/agent_message_content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ AgentMessageContent.belongsTo(AgentMessage, {
name: "agentMessageId",
allowNull: false,
},
onDelete: "CASCADE",
onDelete: "RESTRICT",
onUpdate: "RESTRICT",
});

AgentMessage.hasMany(AgentMessageContent, {
Expand All @@ -67,5 +68,6 @@ AgentMessage.hasMany(AgentMessageContent, {
name: "agentMessageId",
allowNull: false,
},
onDelete: "CASCADE",
onDelete: "RESTRICT",
onUpdate: "RESTRICT",
});
7 changes: 7 additions & 0 deletions front/migrations/db/migration_128.sql
Original file line number Diff line number Diff line change
@@ -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;
6 changes: 6 additions & 0 deletions front/poke/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit b53a7d5

Please sign in to comment.