The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
AI chat: apply participant event rows instead of refetching (#27619)
Félix Malfait441d645b3aa2
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatThreadParticipantOperationsEffect.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatThreadParticipantOperationsEffect.tsxindex f092e8bec7e..fe12d633667 100644--- a/packages/twenty-front/src/modules/ai/components/AgentChatThreadParticipantOperationsEffect.tsx+++ b/packages/twenty-front/src/modules/ai/components/AgentChatThreadParticipantOperationsEffect.tsx@@ -5,7 +5,6 @@ import { isDefined } from 'twenty-shared/utils';import { useRefreshAgentChatOpenThreadsSummary } from '@/ai/hooks/useRefreshAgentChatOpenThreadsSummary';import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';-import { agentChatThreadRecordUpdateCountState } from '@/ai/states/agentChatThreadRecordUpdateCountState';import { agentChatThreadStreamedParticipantsState } from '@/ai/states/agentChatThreadStreamedParticipantsState';import { getAgentChatThreadParticipantFromRecord } from '@/ai/utils/getAgentChatThreadParticipantFromRecord';import { mergeAgentChatThreadParticipants } from '@/ai/utils/mergeAgentChatThreadParticipants';@@ -91,37 +90,13 @@ export const AgentChatThreadParticipantOperationsEffect = () => {operation.type === 'update-one'? [operation.result.updateInput]: operation.result.updateInputs;- const participantsById = new Map(- [- ...Object.values(- store.get(agentChatThreadStreamedParticipantsState.atom),- ),- ...Object.values(- store.get(agentChatThreadParticipantsState.atom) ?? {},- ),- ].map((participant) => [participant.id, participant]),- );- const updatedParticipants = updateInputs.map(- ({ recordId, updatedFields }) => {- const participant = participantsById.get(recordId);- return isDefined(participant)- ? { ...participant, ...Object.assign({}, ...updatedFields) }- : undefined;- },+ applyParticipants(+ updateInputs+ .map(({ updatedRecord }) => updatedRecord)+ .filter(isDefined)+ .map(getAgentChatThreadParticipantFromRecord),);-- // Updates only carry what changed, so a row not loaded yet comes- // whole with its thread, and a page on its way is read again in- // case it holds an older copy- if (!updatedParticipants.every(isDefined)) {- store.set(- agentChatThreadRecordUpdateCountState.atom,- (updateCount) => updateCount + 1,- );- }-- applyParticipants(updatedParticipants.filter(isDefined));return;}}diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadParticipantOperationsEffect.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadParticipantOperationsEffect.test.tsxindex 846a4ad0707..e4be495ca65 100644--- a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadParticipantOperationsEffect.test.tsx+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatThreadParticipantOperationsEffect.test.tsx@@ -5,7 +5,6 @@ import { CoreObjectNameSingular } from 'twenty-shared/types';import { AgentChatThreadParticipantOperationsEffect } from '@/ai/components/AgentChatThreadParticipantOperationsEffect';import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';-import { agentChatThreadRecordUpdateCountState } from '@/ai/states/agentChatThreadRecordUpdateCountState';import { agentChatThreadStreamedParticipantsState } from '@/ai/states/agentChatThreadStreamedParticipantsState';import { mergeAgentChatThreadParticipants } from '@/ai/utils/mergeAgentChatThreadParticipants';import { currentWorkspaceMemberState } from '@/auth/states/currentWorkspaceMemberState';@@ -85,8 +84,7 @@ const receiveCreatedParticipant = ();const receiveParticipantUpdate = (- recordId: string,- updatedFields: Partial<AgentChatThreadParticipantFieldsFragment>,+ participant: AgentChatThreadParticipantFieldsFragment,) =>act(() =>dispatchObjectRecordOperationBrowserEvent({@@ -95,10 +93,12 @@ const receiveParticipantUpdate = (type: 'update-one',result: {updateInput: {- recordId,- updatedFields: Object.entries(updatedFields).map(- ([fieldName, value]) => ({ [fieldName]: value }),- ),+ recordId: participant.id,+ updatedFields: [{ updatedAt: participant.updatedAt }],+ updatedRecord: {+ ...participant,+ workspaceMemberId: 'workspace-member-id',+ },},},},@@ -124,7 +124,7 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {updatedAt: '2026-10-01T10:05:00.000Z',};- receiveParticipantUpdate(PARTICIPANT_ID, change);+ receiveParticipantUpdate({ ...READ_PARTICIPANT, ...change });expect(store.get(agentChatThreadParticipantsState.atom)).toEqual({[THREAD_ID]: { ...READ_PARTICIPANT, ...change },@@ -146,7 +146,7 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {updatedAt: '2026-10-02T09:00:00.000Z',};- receiveParticipantUpdate(PARTICIPANT_ID, change);+ receiveParticipantUpdate({ ...snoozedParticipant, ...change });expect(store.get(agentChatThreadParticipantsState.atom)?.[THREAD_ID],@@ -167,7 +167,8 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {updatedAt: '2026-10-01T10:01:00.000Z',};- receiveParticipantUpdate(PARTICIPANT_ID, {+ receiveParticipantUpdate({+ ...READ_PARTICIPANT,archivedAt: null,updatedAt: '2026-10-01T10:02:00.000Z',});@@ -188,7 +189,8 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {it('ignores a change older than the version it has', () => {const { store } = renderEffect({ [THREAD_ID]: READ_PARTICIPANT });- receiveParticipantUpdate(PARTICIPANT_ID, {+ receiveParticipantUpdate({+ ...READ_PARTICIPANT,archivedAt: '2026-10-01T09:58:00.000Z',updatedAt: '2026-10-01T09:59:00.000Z',});@@ -198,22 +200,22 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {});});- it('leaves a change for a row it does not have to the page of its thread', () => {- const { store } = renderEffect({ [THREAD_ID]: READ_PARTICIPANT });+ it('applies a change to a row it has not loaded', () => {+ const { store } = renderEffect({});- receiveParticipantUpdate('another-participant-id', { lastReadAt: null });+ receiveParticipantUpdate(READ_PARTICIPANT);expect(store.get(agentChatThreadParticipantsState.atom)).toEqual({[THREAD_ID]: READ_PARTICIPANT,});- expect(store.get(agentChatThreadRecordUpdateCountState.atom)).toBe(1);});it('applies a change to a row that arrived before the first page', () => {const { store } = renderEffect(null);receiveCreatedParticipant(READ_PARTICIPANT);- receiveParticipantUpdate(PARTICIPANT_ID, {+ receiveParticipantUpdate({+ ...READ_PARTICIPANT,lastReadAt: null,updatedAt: '2026-10-01T10:05:00.000Z',});@@ -225,7 +227,6 @@ describe('AgentChatThreadParticipantOperationsEffect', () => {updatedAt: '2026-10-01T10:05:00.000Z',},});- expect(store.get(agentChatThreadRecordUpdateCountState.atom)).toBe(0);});it('ignores a copy older than the one it has', () => {diff --git a/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateAgentChatThreadInboxState.test.tsx b/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateAgentChatThreadInboxState.test.tsxindex 330e1f11b06..1d13eaaf955 100644--- a/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateAgentChatThreadInboxState.test.tsx+++ b/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateAgentChatThreadInboxState.test.tsx@@ -6,10 +6,7 @@ import { useUpdateAgentChatThreadInboxState } from '@/ai/hooks/useUpdateAgentChaimport { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';import { agentChatThreadVisitState } from '@/ai/states/agentChatThreadVisitState';import { recordStoreFamilyState } from '@/object-record/record-store/states/recordStoreFamilyState';-import {- AgentChatInboxAction,- GetAgentChatOpenThreadsSummaryDocument,-} from '~/generated-metadata/graphql';+import { AgentChatInboxAction } from '~/generated-metadata/graphql';const THREAD_ID = '20202020-0000-4000-8000-0000000000aa';const OTHER_THREAD_ID = '20202020-0000-4000-8000-0000000000bb';@@ -26,12 +23,11 @@ const READ_PARTICIPANT = {};const mutate = jest.fn();-const refetchQueries = jest.fn(() => Promise.resolve([]));const enqueueToast = jest.fn();jest.mock('@apollo/client/react', () => ({...jest.requireActual('@apollo/client/react'),- useApolloClient: () => ({ mutate, refetchQueries }),+ useApolloClient: () => ({ mutate }),}));jest.mock('twenty-ui/components/feedback', () => ({@@ -103,9 +99,6 @@ describe('useUpdateAgentChatThreadInboxState', () => {expect.any(String),);expect(mutate).toHaveBeenCalledTimes(1);- expect(refetchQueries).toHaveBeenCalledWith({- include: [GetAgentChatOpenThreadsSummaryDocument],- });});it('puts the member state back and reports a refused snooze', async () => {diff --git a/packages/twenty-front/src/modules/ai/hooks/useUpdateAgentChatThreadInboxState.ts b/packages/twenty-front/src/modules/ai/hooks/useUpdateAgentChatThreadInboxState.tsindex 303472e5313..fadb8c7dffd 100644--- a/packages/twenty-front/src/modules/ai/hooks/useUpdateAgentChatThreadInboxState.ts+++ b/packages/twenty-front/src/modules/ai/hooks/useUpdateAgentChatThreadInboxState.ts@@ -5,7 +5,6 @@ import { applyAgentChatInboxAction } from 'twenty-shared/ai';import { isDefined } from 'twenty-shared/utils';import { useToast } from 'twenty-ui/components/feedback';-import { useRefreshAgentChatOpenThreadsSummary } from '@/ai/hooks/useRefreshAgentChatOpenThreadsSummary';import { agentChatThreadParticipantsState } from '@/ai/states/agentChatThreadParticipantsState';import {type AgentChatThreadVisit,@@ -35,8 +34,6 @@ export const useUpdateAgentChatThreadInboxState = () => {const client = useApolloClient();const store = useStore();const { enqueueToast } = useToast();- const { refreshAgentChatOpenThreadsSummary } =- useRefreshAgentChatOpenThreadsSummary();// The change shows right away, and is put back if the server refuses itconst updateAgentChatThreadInboxState = useCallback(@@ -107,7 +104,6 @@ export const useUpdateAgentChatThreadInboxState = () => {snoozedUntil: snoozedUntil?.toISOString(),},});- refreshAgentChatOpenThreadsSummary();} catch (error) {enqueueToast(getToastOptionsFromError({ error }));@@ -142,7 +138,7 @@ export const useUpdateAgentChatThreadInboxState = () => {});}},- [client, enqueueToast, refreshAgentChatOpenThreadsSummary, store],+ [client, enqueueToast, store],);return { updateAgentChatThreadInboxState };diff --git a/packages/twenty-front/src/modules/object-record/types/ObjectRecordOperationUpdateInput.ts b/packages/twenty-front/src/modules/object-record/types/ObjectRecordOperationUpdateInput.tsindex 2ce44697ba4..8fe2a14ba39 100644--- a/packages/twenty-front/src/modules/object-record/types/ObjectRecordOperationUpdateInput.ts+++ b/packages/twenty-front/src/modules/object-record/types/ObjectRecordOperationUpdateInput.ts@@ -1,4 +1,7 @@+import { type ObjectRecord } from 'twenty-shared/types';+export type ObjectRecordOperationUpdateInput = {recordId: string;updatedFields: Record<string, unknown>[];+ updatedRecord?: ObjectRecord;};diff --git a/packages/twenty-front/src/modules/sse-db-event/utils/getObjectRecordOperationUpdateInputs.ts b/packages/twenty-front/src/modules/sse-db-event/utils/getObjectRecordOperationUpdateInputs.tsindex c01af55efb5..3c757dc4f44 100644--- a/packages/twenty-front/src/modules/sse-db-event/utils/getObjectRecordOperationUpdateInputs.ts+++ b/packages/twenty-front/src/modules/sse-db-event/utils/getObjectRecordOperationUpdateInputs.ts@@ -19,6 +19,7 @@ export const getObjectRecordOperationUpdateInputs = (updatedFields: updatedFieldNames.map((fieldName) => ({[fieldName]: updatedRecord[fieldName],})),+ updatedRecord,};}).filter(isDefined)