Skip to content

The open alternative to Salesforce, designed for AI.

File a member's own AI chat once they leave it caught up (#28583)

Félix Malfaitd7499bf4ba11
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatThreadMarkAsReadEffect.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatThreadMarkAsReadEffect.tsx
index 2fe3a8e600c..54e0acb4762 100644
--- a/packages/twenty-front/src/modules/ai/components/AgentChatThreadMarkAsReadEffect.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AgentChatThreadMarkAsReadEffect.tsx
@@ -9,6 +9,8 @@ import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadPar
import { agentChatThreadVisitState } from '@/ai/states/agentChatThreadVisitState';
import { agentChatThreadRecordFamilySelector } from '@/ai/states/selectors/agentChatThreadRecordFamilySelector';
import { getAgentChatThreadInboxStatus } from '@/ai/utils/getAgentChatThreadInboxStatus';
+import { isAgentChatThreadSettledForMember } from '@/ai/utils/isAgentChatThreadSettledForMember';
+import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';
import { useAtomFamilySelectorValue } from '@/ui/utilities/state/jotai/hooks/useAtomFamilySelectorValue';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { AgentChatInboxAction } from '~/generated-metadata/graphql';
@@ -56,13 +58,38 @@ export const AgentChatThreadMarkAsReadEffect = () => {
return () => {
store.set(agentChatThreadOpenViewCountState.atom, (count) => count - 1);
- if (store.get(agentChatThreadOpenViewCountState.atom) === 0) {
- store.set(agentChatThreadVisitState.atom, (visit) =>
- visit?.threadId === threadId ? null : visit,
- );
+ if (store.get(agentChatThreadOpenViewCountState.atom) !== 0) {
+ return;
+ }
+
+ store.set(agentChatThreadVisitState.atom, (visit) =>
+ visit?.threadId === threadId ? null : visit,
+ );
+
+ // Filed on leave, not on read, so the chat stays put while being read
+ const thread = store.get(
+ agentChatThreadRecordFamilySelector.selectorFamily(threadId),
+ );
+ const workspaceMemberId = store.get(currentWorkspaceMemberState.atom)?.id;
+
+ if (
+ isDefined(thread) &&
+ isDefined(workspaceMemberId) &&
+ isAgentChatThreadSettledForMember({
+ thread,
+ participant: store.get(agentChatThreadParticipantsState.atom)?.[
+ threadId
+ ],
+ workspaceMemberId,
+ })
+ ) {
+ void updateAgentChatThreadInboxState({
+ threadIds: [threadId],
+ action: AgentChatInboxAction.ARCHIVE,
+ });
}
};
- }, [store, threadId]);
+ }, [store, threadId, updateAgentChatThreadInboxState]);
useEffect(() => {
if (
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadMarkAsReadEffect.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadMarkAsReadEffect.test.tsx
index b2b5a7f00c0..dde515a9d8d 100644
--- a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadMarkAsReadEffect.test.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadMarkAsReadEffect.test.tsx
@@ -6,10 +6,15 @@ import { agentChatDisplayedThreadState } from '@/ai/states/agentChatDisplayedThr
import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';
import { agentChatThreadVisitState } from '@/ai/states/agentChatThreadVisitState';
import { setAgentChatThreadList } from '@/ai/testing/setAgentChatThreadList';
+import {
+ type CurrentWorkspaceMember,
+ currentWorkspaceMemberState,
+} from '@/auth/states/currentWorkspaceMemberState';
import {
jotaiStore,
resetJotaiStore,
} from '@/ui/utilities/state/jotai/jotaiStore';
+import { type AgentChatThreadParticipantFieldsFragment } from '~/generated-metadata/graphql';
const updateAgentChatThreadInboxState = jest.fn();
@@ -20,6 +25,27 @@ jest.mock('@/ai/hooks/useUpdateAgentChatThreadInboxState', () => ({
}));
const THREAD_ID = '6f1c2b0e-7a4d-4e8b-9c3f-2d5a1b8e7c60';
+const WORKSPACE_MEMBER_ID = '20202020-0687-4c41-b707-ed1bfca972a7';
+const LAST_ACTIVITY_AT = '2026-10-01T10:00:00.000Z';
+
+const CURRENT_WORKSPACE_MEMBER: CurrentWorkspaceMember = {
+ id: WORKSPACE_MEMBER_ID,
+ name: { firstName: 'Tim', lastName: 'Apple' },
+ locale: 'en',
+ colorScheme: 'Light',
+ userEmail: '[email protected]',
+};
+
+const PARTICIPANT: AgentChatThreadParticipantFieldsFragment = {
+ id: 'participant-id',
+ threadId: THREAD_ID,
+ lastReadAt: '2026-10-01T09:00:00.000Z',
+ archivedAt: null,
+ snoozedUntil: null,
+ isSubscribed: true,
+ lastMentionedAt: null,
+ updatedAt: LAST_ACTIVITY_AT,
+};
const renderChatView = () =>
render(
@@ -37,20 +63,16 @@ describe('AgentChatThreadMarkAsReadEffect', () => {
__typename: 'AgentChatThread',
id: THREAD_ID,
deletedAt: null,
- lastActivityAt: '2026-10-01T10:00:00.000Z',
+ lastActivityAt: LAST_ACTIVITY_AT,
+ lastMessageSenderWorkspaceMemberId: null,
+ workspaceMemberId: WORKSPACE_MEMBER_ID,
+ assigneeId: null,
+ pendingQuestionMessageId: null,
} as never,
]);
+ jotaiStore.set(currentWorkspaceMemberState.atom, CURRENT_WORKSPACE_MEMBER);
jotaiStore.set(agentChatThreadParticipantsState.atom, {
- [THREAD_ID]: {
- threadId: THREAD_ID,
- lastReadAt: '2026-10-01T09:00:00.000Z',
- archivedAt: null,
- snoozedUntil: null,
- isSubscribed: true,
- lastMentionedAt: null,
- id: 'participant-id',
- updatedAt: '2026-10-01T10:00:00.000Z',
- },
+ [THREAD_ID]: PARTICIPANT,
});
jotaiStore.set(agentChatDisplayedThreadState.atom, THREAD_ID);
});
@@ -102,4 +124,50 @@ describe('AgentChatThreadMarkAsReadEffect', () => {
expect(jotaiStore.get(agentChatThreadVisitState.atom)).toBeNull();
});
+
+ describe('on leaving the chat', () => {
+ const setReadUpTo = (lastReadAt: string) =>
+ jotaiStore.set(agentChatThreadParticipantsState.atom, {
+ [THREAD_ID]: { ...PARTICIPANT, lastReadAt },
+ });
+
+ it('files a chat the member read up to the agent reply', () => {
+ setReadUpTo(LAST_ACTIVITY_AT);
+
+ renderChatView().unmount();
+
+ expect(updateAgentChatThreadInboxState).toHaveBeenCalledWith({
+ threadIds: [THREAD_ID],
+ action: 'ARCHIVE',
+ });
+ });
+
+ it('keeps a chat in the inbox while another view still shows it', () => {
+ setReadUpTo(LAST_ACTIVITY_AT);
+
+ const sidePanelView = renderChatView();
+ const recordPageView = renderChatView();
+
+ sidePanelView.unmount();
+
+ expect(updateAgentChatThreadInboxState).not.toHaveBeenCalledWith(
+ expect.objectContaining({ action: 'ARCHIVE' }),
+ );
+
+ recordPageView.unmount();
+
+ expect(updateAgentChatThreadInboxState).toHaveBeenCalledWith({
+ threadIds: [THREAD_ID],
+ action: 'ARCHIVE',
+ });
+ });
+
+ it('keeps a chat whose reply the member has not read', () => {
+ renderChatView().unmount();
+
+ expect(updateAgentChatThreadInboxState).not.toHaveBeenCalledWith(
+ expect.objectContaining({ action: 'ARCHIVE' }),
+ );
+ });
+ });
});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/isAgentChatThreadSettledForMember.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/isAgentChatThreadSettledForMember.test.ts
new file mode 100644
index 00000000000..889a2231b95
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/isAgentChatThreadSettledForMember.test.ts
@@ -0,0 +1,89 @@
+import { isAgentChatThreadSettledForMember } from '@/ai/utils/isAgentChatThreadSettledForMember';
+import { type AgentChatThreadParticipantFieldsFragment } from '~/generated-metadata/graphql';
+
+const MEMBER_ID = 'member';
+const OTHER_MEMBER_ID = 'other-member';
+const LAST_ACTIVITY_AT = '2026-10-01T10:00:00.000Z';
+
+const SETTLED_THREAD: Parameters<
+ typeof isAgentChatThreadSettledForMember
+>[0]['thread'] = {
+ deletedAt: null,
+ lastActivityAt: LAST_ACTIVITY_AT,
+ lastMessageSenderWorkspaceMemberId: null,
+ workspaceMemberId: MEMBER_ID,
+ assigneeId: null,
+ pendingQuestionMessageId: null,
+};
+
+const READ_PARTICIPANT: AgentChatThreadParticipantFieldsFragment = {
+ id: 'participant',
+ threadId: 'thread',
+ lastReadAt: LAST_ACTIVITY_AT,
+ archivedAt: null,
+ snoozedUntil: null,
+ isSubscribed: true,
+ lastMentionedAt: null,
+ updatedAt: LAST_ACTIVITY_AT,
+};
+
+const isSettled = ({
+ thread = {},
+ participant = {},
+}: {
+ thread?: Partial<typeof SETTLED_THREAD>;
+ participant?: Partial<typeof READ_PARTICIPANT>;
+} = {}) =>
+ isAgentChatThreadSettledForMember({
+ thread: { ...SETTLED_THREAD, ...thread },
+ participant: { ...READ_PARTICIPANT, ...participant },
+ workspaceMemberId: MEMBER_ID,
+ });
+
+describe('isAgentChatThreadSettledForMember', () => {
+ it('settles a read chat of the member where the agent had the last word', () => {
+ expect(isSettled()).toBe(true);
+ });
+
+ it('keeps a chat the member has not read yet', () => {
+ expect(
+ isSettled({ participant: { lastReadAt: '2026-10-01T09:00:00.000Z' } }),
+ ).toBe(false);
+ });
+
+ it('keeps a chat waiting on an answer from the member', () => {
+ expect(isSettled({ thread: { pendingQuestionMessageId: 'message' } })).toBe(
+ false,
+ );
+ });
+
+ it('keeps a chat where a member wrote last', () => {
+ expect(
+ isSettled({ thread: { lastMessageSenderWorkspaceMemberId: MEMBER_ID } }),
+ ).toBe(false);
+ });
+
+ it('keeps a chat started by another member', () => {
+ expect(isSettled({ thread: { workspaceMemberId: OTHER_MEMBER_ID } })).toBe(
+ false,
+ );
+ });
+
+ it('keeps a chat assigned to the member', () => {
+ expect(isSettled({ thread: { assigneeId: MEMBER_ID } })).toBe(false);
+ });
+
+ it('leaves alone a chat already out of the inbox', () => {
+ expect(
+ isSettled({ participant: { archivedAt: '2026-10-01T11:00:00.000Z' } }),
+ ).toBe(false);
+ expect(
+ isSettled({
+ participant: {
+ isSubscribed: false,
+ archivedAt: '2026-10-01T11:00:00.000Z',
+ },
+ }),
+ ).toBe(false);
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/isAgentChatThreadSettledForMember.ts b/packages/twenty-front/src/modules/ai/utils/isAgentChatThreadSettledForMember.ts
new file mode 100644
index 00000000000..6873fe9444f
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/isAgentChatThreadSettledForMember.ts
@@ -0,0 +1,39 @@
+import { isDefined } from 'twenty-shared/utils';
+
+import { type AgentChatThreadRecord } from '@/ai/types/AgentChatThreadRecord';
+import { getAgentChatThreadInboxStatus } from '@/ai/utils/getAgentChatThreadInboxStatus';
+import { type AgentChatThreadParticipantFieldsFragment } from '~/generated-metadata/graphql';
+
+export const isAgentChatThreadSettledForMember = ({
+ thread,
+ participant,
+ workspaceMemberId,
+}: {
+ thread: Pick<
+ AgentChatThreadRecord,
+ | 'deletedAt'
+ | 'lastActivityAt'
+ | 'lastMessageSenderWorkspaceMemberId'
+ | 'workspaceMemberId'
+ | 'assigneeId'
+ | 'pendingQuestionMessageId'
+ >;
+ participant: AgentChatThreadParticipantFieldsFragment | undefined;
+ workspaceMemberId: string;
+}) => {
+ const { scope, isUnread } = getAgentChatThreadInboxStatus({
+ lastActivityAt: thread.lastActivityAt,
+ participant,
+ });
+
+ return (
+ scope === 'INBOX' &&
+ !isUnread &&
+ !isDefined(thread.deletedAt) &&
+ isDefined(thread.lastActivityAt) &&
+ thread.workspaceMemberId === workspaceMemberId &&
+ !isDefined(thread.lastMessageSenderWorkspaceMemberId) &&
+ !isDefined(thread.pendingQuestionMessageId) &&
+ thread.assigneeId !== workspaceMemberId
+ );
+};