Skip to content

The open alternative to Salesforce, designed for AI.

Deliver app inbox messages without a mention (#29213)

Raphaël Bosi307aa69f128d
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/resolvers/__tests__/agent-chat.resolver.sharing.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/resolvers/__tests__/agent-chat.resolver.sharing.spec.ts
index c1158a12f0c..759ae240a3f 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/resolvers/__tests__/agent-chat.resolver.sharing.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/resolvers/__tests__/agent-chat.resolver.sharing.spec.ts
@@ -416,8 +416,8 @@ describe('Shared conversation API boundaries', () => {
it('adds the mentioned members once the message is sent', async () => {
const { resolver, threadService, streaming } = buildResolver();
- const addParticipants = jest
- .spyOn(threadService, 'addParticipants')
+ const mentionParticipants = jest
+ .spyOn(threadService, 'mentionParticipants')
.mockResolvedValue(['jony']);
const result = await resolver.sendChatMessage(
@@ -435,7 +435,7 @@ describe('Shared conversation API boundaries', () => {
);
expect(streaming.streamAgentChat).toHaveBeenCalled();
- expect(addParticipants).toHaveBeenCalledWith({
+ expect(mentionParticipants).toHaveBeenCalledWith({
threadId: THREAD_ID,
workspaceMemberId: 'owner',
workspaceId: WORKSPACE_ID,
@@ -448,7 +448,7 @@ describe('Shared conversation API boundaries', () => {
const { resolver, threadService } = buildResolver();
jest
- .spyOn(threadService, 'addParticipants')
+ .spyOn(threadService, 'mentionParticipants')
.mockRejectedValue(new Error('share failed'));
const result = await resolver.sendChatMessage(
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-inbox.service.open-thread.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-inbox.service.open-thread.spec.ts
index 948c32cb33c..513a950e5ca 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-inbox.service.open-thread.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-inbox.service.open-thread.spec.ts
@@ -42,7 +42,7 @@ const buildService = () => {
.mockImplementation(({ id, workspaceMemberId }) =>
Promise.resolve({ id, workspaceMemberId }),
),
- addParticipants: jest
+ deliverToParticipants: jest
.fn()
.mockImplementation(({ participantWorkspaceMemberIds }) =>
Promise.resolve(participantWorkspaceMemberIds),
@@ -93,7 +93,7 @@ describe('AgentInboxService.openThread', () => {
service.openThread({ ...OPEN_ARGS, workspaceMemberIds: ['member-id'] }),
).resolves.toEqual({ thread: existingThread, isCreated: false });
expect(threadService.createThread).not.toHaveBeenCalled();
- expect(threadService.addParticipants).not.toHaveBeenCalled();
+ expect(threadService.deliverToParticipants).not.toHaveBeenCalled();
});
it('creates the conversation owned by the first member and adds the others', async () => {
@@ -114,7 +114,7 @@ describe('AgentInboxService.openThread', () => {
title: 'Draft the quote',
isArchived: true,
});
- expect(threadService.addParticipants).toHaveBeenCalledWith({
+ expect(threadService.deliverToParticipants).toHaveBeenCalledWith({
workspaceId: 'workspace-id',
threadId: THREAD_ID,
workspaceMemberId: 'member-id',
@@ -142,7 +142,7 @@ describe('AgentInboxService.openThread', () => {
],
});
- expect(threadService.addParticipants).toHaveBeenCalledWith(
+ expect(threadService.deliverToParticipants).toHaveBeenCalledWith(
expect.objectContaining({
participantWorkspaceMemberIds: ['third-member-id'],
}),
@@ -176,13 +176,13 @@ describe('AgentInboxService.openThread', () => {
}),
).rejects.toMatchObject({ code: AiExceptionCode.THREAD_NOT_FOUND });
expect(threadService.createThread).not.toHaveBeenCalled();
- expect(threadService.addParticipants).not.toHaveBeenCalled();
+ expect(threadService.deliverToParticipants).not.toHaveBeenCalled();
});
it('fails when a member cannot join the conversation', async () => {
const { service, threadService } = buildService();
- threadService.addParticipants.mockResolvedValue([]);
+ threadService.deliverToParticipants.mockResolvedValue([]);
await expect(
service.openThread({
@@ -220,7 +220,7 @@ describe('AgentInboxService.openThread', () => {
await expect(
service.openThread({ ...OPEN_ARGS, workspaceMemberIds: ['member-id'] }),
).rejects.toMatchObject({ code: AiExceptionCode.THREAD_NOT_FOUND });
- expect(threadService.addParticipants).not.toHaveBeenCalled();
+ expect(threadService.deliverToParticipants).not.toHaveBeenCalled();
});
it('returns a deleted conversation without adding anyone to it', async () => {
@@ -239,7 +239,7 @@ describe('AgentInboxService.openThread', () => {
workspaceMemberIds: ['member-id', 'other-member-id'],
}),
).resolves.toEqual({ thread: deletedThread, isCreated: false });
- expect(threadService.addParticipants).not.toHaveBeenCalled();
+ expect(threadService.deliverToParticipants).not.toHaveBeenCalled();
});
it('returns the conversation a concurrent open created first', async () => {
@@ -285,7 +285,7 @@ describe('AgentInboxService.sendMessage', () => {
}),
).resolves.toMatchObject({ threadId: THREAD_ID, isDismissed: false });
expect(threadService.createThread).not.toHaveBeenCalled();
- expect(threadService.addParticipants).toHaveBeenCalledWith(
+ expect(threadService.deliverToParticipants).toHaveBeenCalledWith(
expect.objectContaining({
threadId: THREAD_ID,
participantWorkspaceMemberIds: ['other-member-id'],
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 6dbffccd5c7..af5d3e35501 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
@@ -230,6 +230,22 @@ export class AgentChatThreadParticipantService {
);
}
+ async deliverToInbox(args: AgentChatThreadAccessArgs): Promise<void> {
+ await this.write(
+ { ...args, threadIds: [args.threadId] },
+ ({ participantTable }) =>
+ `INSERT INTO ${participantTable} AS participant ("threadId", "workspaceMemberId")
+ VALUES ($1, $2)
+ ON CONFLICT ("threadId", "workspaceMemberId") DO UPDATE SET
+ "lastReadAt" = NULL,
+ "archivedAt" = NULL,
+ "snoozedUntil" = NULL,
+ "isSubscribed" = true,
+ "updatedAt" = ${NEXT_PARTICIPANT_UPDATED_AT}
+ RETURNING *`,
+ );
+ }
+
// An assignee follows the chat and finds it in their inbox, unread unless
// they assigned it to themselves
// The assignment is written by writeAssignment in the same transaction, so
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service.ts
index 900128cb653..c6f85acb58b 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread.service.ts
@@ -323,11 +323,39 @@ export class AgentChatThreadService {
// A mentioned member follows the chat like one who wrote in it, and finds
// it unread in their inbox even if they had read or archived it
- async addParticipants({
+ async mentionParticipants(
+ args: AgentChatThreadAccessArgs & {
+ participantWorkspaceMemberIds: string[];
+ },
+ ): Promise<string[]> {
+ return this.addParticipants({
+ ...args,
+ writeParticipant: (participantArgs) =>
+ this.participantService.markAsMentioned(participantArgs),
+ });
+ }
+
+ async deliverToParticipants(
+ args: AgentChatThreadAccessArgs & {
+ participantWorkspaceMemberIds: string[];
+ },
+ ): Promise<string[]> {
+ return this.addParticipants({
+ ...args,
+ writeParticipant: (participantArgs) =>
+ this.participantService.deliverToInbox(participantArgs),
+ });
+ }
+
+ private async addParticipants({
participantWorkspaceMemberIds,
+ writeParticipant,
...args
}: AgentChatThreadAccessArgs & {
participantWorkspaceMemberIds: string[];
+ writeParticipant: (
+ participantArgs: AgentChatThreadAccessArgs,
+ ) => Promise<void>;
}): Promise<string[]> {
const thread = await this.getWritableThread(args);
const candidateMemberIds = [
@@ -363,7 +391,7 @@ export class AgentChatThreadService {
);
for (const participantMemberId of participantMemberIds) {
- await this.participantService.markAsMentioned({
+ await writeParticipant({
workspaceId: args.workspaceId,
threadId: args.threadId,
workspaceMemberId: participantMemberId,
@@ -547,7 +575,7 @@ export class AgentChatThreadService {
}
try {
- return await this.addParticipants({
+ return await this.mentionParticipants({
...args,
participantWorkspaceMemberIds: mentionedWorkspaceMemberIds,
});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
index bc4d7b46fbe..288d6b36346 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
@@ -323,7 +323,7 @@ export class AgentInboxService {
}
const participantWorkspaceMemberIds =
- await this.threadService.addParticipants({
+ await this.threadService.deliverToParticipants({
workspaceId,
threadId: thread.id,
workspaceMemberId: thread.workspaceMemberId,
diff --git a/packages/twenty-server/test/integration/graphql/suites/chat-thread-participant.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/chat-thread-participant.integration-spec.ts
index 524a117e340..9ab6f9c7ba4 100644
--- a/packages/twenty-server/test/integration/graphql/suites/chat-thread-participant.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/chat-thread-participant.integration-spec.ts
@@ -72,7 +72,7 @@ const addParticipants = (
) =>
getAppProviderByClassName<AgentChatThreadService>(
'AgentChatThreadService',
- ).addParticipants({
+ ).mentionParticipants({
workspaceId: SEED_APPLE_WORKSPACE_ID,
workspaceMemberId,
threadId,
@@ -1220,4 +1220,42 @@ describe('Chat thread participant state through the authenticated API', () => {
unreadOpenThreadCount: before.unreadOpenThreadCount + 1,
});
});
+
+ it('delivers app recipients without creating or clearing an explicit mention', async () => {
+ const threadId = await createTestThread();
+ const threadService = getAppProviderByClassName<AgentChatThreadService>(
+ 'AgentChatThreadService',
+ );
+ const delivery = {
+ workspaceId: SEED_APPLE_WORKSPACE_ID,
+ workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JANE,
+ threadId,
+ participantWorkspaceMemberIds: [WORKSPACE_MEMBER_DATA_SEED_IDS.JONY],
+ };
+
+ await threadService.deliverToParticipants(delivery);
+
+ expect(
+ await findMyParticipant(threadId, APPLE_JONY_MEMBER_ACCESS_TOKEN),
+ ).toMatchObject({
+ isSubscribed: true,
+ lastReadAt: null,
+ lastMentionedAt: null,
+ });
+
+ await addParticipants(threadId, [WORKSPACE_MEMBER_DATA_SEED_IDS.JONY]);
+
+ const mentionedParticipant = await findMyParticipant(
+ threadId,
+ APPLE_JONY_MEMBER_ACCESS_TOKEN,
+ );
+
+ await threadService.deliverToParticipants(delivery);
+
+ expect(
+ await findMyParticipant(threadId, APPLE_JONY_MEMBER_ACCESS_TOKEN),
+ ).toMatchObject({
+ lastMentionedAt: mentionedParticipant!.lastMentionedAt,
+ });
+ });
});