Skip to content

The open alternative to Salesforce, designed for AI.

Order chat participant writes (#29212)

Raphaël Bosib4e09d0f44d7
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-participant.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-participant.service.ts
index e22fdface93..6dbffccd5c7 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-participant.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-participant.service.ts
@@ -43,6 +43,8 @@ type BuildParticipantWriteQuery = (tables: {
threadTable: string;
}) => string;
+const NEXT_PARTICIPANT_UPDATED_AT = `GREATEST(clock_timestamp(), participant."updatedAt" + interval '1 millisecond')`;
+
// A snooze whose end is already due, which its queued end may have checked
// before this write, is saved as ended. Filing a thread, done or snoozed,
// follows it, so new activity or the snooze's end brings it back
@@ -53,7 +55,7 @@ const buildArchiveQuery: BuildParticipantWriteQuery = ({ participantTable }) =>
"archivedAt" = EXCLUDED."archivedAt",
"snoozedUntil" = EXCLUDED."snoozedUntil",
"isSubscribed" = true,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`;
// The member's change for each action, written by the database so its
@@ -70,14 +72,14 @@ const INBOX_ACTION_QUERIES: Record<
SELECT thread.id, $2, thread."lastActivityAt", false FROM ${threadTable} thread WHERE thread.id = $1
ON CONFLICT ("threadId", "workspaceMemberId") DO UPDATE SET
"lastReadAt" = GREATEST(participant."lastReadAt", EXCLUDED."lastReadAt"),
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
[AgentChatInboxAction.UNREAD]: ({ participantTable }) =>
`INSERT INTO ${participantTable} AS participant ("threadId", "workspaceMemberId", "lastReadAt", "isSubscribed")
VALUES ($1, $2, NULL, false)
ON CONFLICT ("threadId", "workspaceMemberId") DO UPDATE SET
"lastReadAt" = NULL,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
[AgentChatInboxAction.ARCHIVE]: buildArchiveQuery,
[AgentChatInboxAction.SNOOZE]: buildArchiveQuery,
@@ -88,14 +90,14 @@ const INBOX_ACTION_QUERIES: Record<
"archivedAt" = NULL,
"snoozedUntil" = NULL,
"isSubscribed" = true,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
[AgentChatInboxAction.SUBSCRIBE]: ({ participantTable }) =>
`INSERT INTO ${participantTable} AS participant ("threadId", "workspaceMemberId")
VALUES ($1, $2)
ON CONFLICT ("threadId", "workspaceMemberId") DO UPDATE SET
"isSubscribed" = true,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
// Files the chat under done and keeps it there whatever happens in it,
// until the member is mentioned or writes in it again
@@ -106,7 +108,7 @@ const INBOX_ACTION_QUERIES: Record<
"isSubscribed" = false,
"archivedAt" = EXCLUDED."archivedAt",
"snoozedUntil" = NULL,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
};
@@ -223,7 +225,7 @@ export class AgentChatThreadParticipantService {
"snoozedUntil" = NULL,
"isSubscribed" = true,
"lastMentionedAt" = EXCLUDED."lastMentionedAt",
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
);
}
@@ -252,7 +254,7 @@ export class AgentChatThreadParticipantService {
"archivedAt" = NULL,
"snoozedUntil" = NULL,
"isSubscribed" = true,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING *`,
[isSelfAssigned],
writeAssignment,
@@ -303,8 +305,8 @@ export class AgentChatThreadParticipantService {
threadIds: [args.threadId],
},
({ participantTable }) =>
- `UPDATE ${participantTable}
- SET "archivedAt" = NULL, "updatedAt" = now()
+ `UPDATE ${participantTable} AS participant
+ SET "archivedAt" = NULL, "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
WHERE "threadId" = $1 AND "workspaceMemberId" = $2
AND "snoozedUntil" = $3 AND "archivedAt" IS NOT NULL
RETURNING *`,
@@ -383,7 +385,7 @@ export class AgentChatThreadParticipantService {
"archivedAt" = NULL,
"snoozedUntil" = NULL,
"isSubscribed" = true,
- "updatedAt" = now()
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
RETURNING id, "workspaceMemberId"
), owner_share AS (
${buildAgentChatThreadParticipantOwnerShareInsert({ workspaceId, participantSource: 'participant', objectMetadataIdParameter: '$4' })}