Skip to content

The open alternative to Salesforce, designed for AI.

perf(ai-chat): spot changed chat messages by reference instead of deep comparison (#27212)

Félix Malfaite8e8ac2c64d2
diff --git a/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateStreamingPartsWithDiff.test.tsx b/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateStreamingPartsWithDiff.test.tsx
new file mode 100644
index 00000000000..70a3b5d5fd1
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/hooks/__tests__/useUpdateStreamingPartsWithDiff.test.tsx
@@ -0,0 +1,98 @@
+import { renderHook } from '@testing-library/react';
+import { Provider as JotaiProvider } from 'jotai';
+import { type ReactNode } from 'react';
+import { Temporal } from 'temporal-polyfill';
+import { type ExtendedUIMessage } from 'twenty-shared/ai';
+
+import { useUpdateStreamingPartsWithDiff } from '@/ai/hooks/useUpdateStreamingPartsWithDiff';
+import { agentChatUISessionStartTimeState } from '@/ai/states/agentChatUISessionStartTimeState';
+import {
+ jotaiStore,
+ resetJotaiStore,
+} from '@/ui/utilities/state/jotai/jotaiStore';
+
+const processUIToolCallMessage = jest.fn();
+const processWorkspaceSetupCompletion = jest.fn();
+
+jest.mock('@/ai/hooks/useProcessUIToolCallMessage', () => ({
+ useProcessUIToolCallMessage: () => ({ processUIToolCallMessage }),
+}));
+
+jest.mock('@/ai/hooks/useProcessWorkspaceSetupCompletion', () => ({
+ useProcessWorkspaceSetupCompletion: () => ({
+ processWorkspaceSetupCompletion,
+ }),
+}));
+
+const SESSION_START = '2026-01-01T10:00:00.000Z';
+
+const buildMessage = (
+ id: string,
+ text: string,
+ createdAt = '2026-01-01T10:00:01.000Z',
+): ExtendedUIMessage => ({
+ id,
+ role: 'assistant',
+ parts: [{ type: 'text', text }],
+ metadata: { createdAt },
+});
+
+const renderUpdateStreamingPartsWithDiff = () => {
+ const Wrapper = ({ children }: { children: ReactNode }) => (
+ <JotaiProvider store={jotaiStore}>{children}</JotaiProvider>
+ );
+
+ return renderHook(() => useUpdateStreamingPartsWithDiff(), {
+ wrapper: Wrapper,
+ }).result.current.updateStreamingPartsWithDiff;
+};
+
+describe('useUpdateStreamingPartsWithDiff', () => {
+ beforeEach(() => {
+ resetJotaiStore();
+ jest.clearAllMocks();
+ jotaiStore.set(
+ agentChatUISessionStartTimeState.atom,
+ Temporal.Instant.from(SESSION_START),
+ );
+ });
+
+ it('processes each message once until it is replaced', () => {
+ const updateStreamingPartsWithDiff = renderUpdateStreamingPartsWithDiff();
+ const settledMessage = buildMessage('settled', 'Done');
+ const streamingMessage = buildMessage('streaming', 'Hel');
+
+ updateStreamingPartsWithDiff([settledMessage, streamingMessage]);
+ expect(processUIToolCallMessage).toHaveBeenCalledTimes(2);
+
+ const nextStreamingMessage = buildMessage('streaming', 'Hello');
+
+ updateStreamingPartsWithDiff([settledMessage, nextStreamingMessage]);
+
+ expect(processUIToolCallMessage).toHaveBeenCalledTimes(3);
+ expect(processUIToolCallMessage).toHaveBeenLastCalledWith(
+ nextStreamingMessage,
+ );
+ expect(processWorkspaceSetupCompletion).toHaveBeenCalledTimes(3);
+ });
+
+ it('does not process again a refetched message whose content did not change', () => {
+ const updateStreamingPartsWithDiff = renderUpdateStreamingPartsWithDiff();
+
+ updateStreamingPartsWithDiff([buildMessage('answer', 'Done')]);
+ updateStreamingPartsWithDiff([buildMessage('answer', 'Done')]);
+
+ expect(processUIToolCallMessage).toHaveBeenCalledTimes(1);
+ });
+
+ it('leaves messages written before this session alone', () => {
+ const updateStreamingPartsWithDiff = renderUpdateStreamingPartsWithDiff();
+
+ updateStreamingPartsWithDiff([
+ buildMessage('earlier', 'Done', '2026-01-01T09:59:59.000Z'),
+ ]);
+
+ expect(processUIToolCallMessage).not.toHaveBeenCalled();
+ expect(processWorkspaceSetupCompletion).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/hooks/useUpdateStreamingPartsWithDiff.ts b/packages/twenty-front/src/modules/ai/hooks/useUpdateStreamingPartsWithDiff.ts
index c422fb143b7..63228402870 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useUpdateStreamingPartsWithDiff.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useUpdateStreamingPartsWithDiff.ts
@@ -17,6 +17,7 @@ export const useUpdateStreamingPartsWithDiff = () => {
const { processWorkspaceSetupCompletion } =
useProcessWorkspaceSetupCompletion();
+ // messages are replaced, never mutated, so the last one seen can be kept by reference
const [lastSeenMessageById] = useState(
() => new Map<string, ExtendedUIMessage>(),
);
@@ -38,21 +39,28 @@ export const useUpdateStreamingPartsWithDiff = () => {
const updateStreamingPartsWithDiff = (
incomingMessages: ExtendedUIMessage[],
) => {
- for (const incomingMessage of incomingMessages) {
+ const lastSeenMessages = incomingMessages.map((message) =>
+ lastSeenMessageById.get(message.id),
+ );
+
+ // only the messages on screen are kept, so other threads' messages are released
+ lastSeenMessageById.clear();
+
+ for (const message of incomingMessages) {
+ lastSeenMessageById.set(message.id, message);
+ }
+
+ for (const [index, incomingMessage] of incomingMessages.entries()) {
+ const lastSeenMessage = lastSeenMessages[index];
+
+ // a stream flush keeps the unchanged messages, a refetch rebuilds them all
if (
- isDeeplyEqual(
- lastSeenMessageById.get(incomingMessage.id),
- incomingMessage,
- )
+ lastSeenMessage === incomingMessage ||
+ isDeeplyEqual(lastSeenMessage, incomingMessage)
) {
continue;
}
- lastSeenMessageById.set(
- incomingMessage.id,
- structuredClone(incomingMessage),
- );
-
if (!isMessageFromCurrentSession(incomingMessage)) {
continue;
}