Skip to content

The open alternative to Salesforce, designed for AI.

Move the AI chat stall watchdog into the subscription it watches (#27722)

Félix Malfaited81fd663fe2
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
index 63aedcb5e28..d003eba8103 100644
--- a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx
@@ -1,6 +1,5 @@
import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect';
import { AgentChatPrepromptEffect } from '@/ai/components/AgentChatPrepromptEffect';
-import { AgentChatStreamKeepAliveEffect } from '@/ai/components/AgentChatStreamKeepAliveEffect';
import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect';
import { agentChatUISessionStartTimeState } from '@/ai/states/agentChatUISessionStartTimeState';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
@@ -26,7 +25,6 @@ export const AgentChatRuntimeEffects = () => {
<AgentChatMessagesFetchEffect />
<AgentChatStreamSubscriptionEffect />
<AgentChatPrepromptEffect />
- <AgentChatStreamKeepAliveEffect />
</>
);
};
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx
deleted file mode 100644
index e23f1331cfd..00000000000
--- a/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx
+++ /dev/null
@@ -1,131 +0,0 @@
-import { t } from '@lingui/core/macro';
-import { useStore } from 'jotai';
-import { useCallback, useEffect } from 'react';
-import { isDefined, isValidUuid } from 'twenty-shared/utils';
-
-import { AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME } from '@/ai/constants/AgentChatRefetchMessagesEventName';
-import { AGENT_CHAT_STREAM_LIVENESS_CHECK_INTERVAL_IN_MS } from '@/ai/constants/AgentChatStreamLivenessCheckIntervalInMs';
-import { AGENT_CHAT_STREAM_LIVENESS_TIMEOUT_IN_MS } from '@/ai/constants/AgentChatStreamLivenessTimeoutInMs';
-import { agentChatErrorFamilyState } from '@/ai/states/agentChatErrorFamilyState';
-import { agentChatIsAwaitingFirstChunkFamilyState } from '@/ai/states/agentChatIsAwaitingFirstChunkFamilyState';
-import { agentChatIsStreamingFamilyState } from '@/ai/states/agentChatIsStreamingFamilyState';
-import { agentChatStreamLastEventTimestampState } from '@/ai/states/agentChatStreamLastEventTimestampState';
-import { agentChatStreamRecoveryAttemptsState } from '@/ai/states/agentChatStreamRecoveryAttemptsState';
-import { agentChatStreamResubscribeNonceState } from '@/ai/states/agentChatStreamResubscribeNonceState';
-import { currentAiChatThreadState } from '@/ai/states/currentAiChatThreadState';
-import { AiChatErrorCode } from '@/ai/utils/aiChatErrorCode';
-import { createAiChatCodedError } from '@/ai/utils/createAiChatCodedError';
-import { useListenToBrowserEvent } from '@/browser-event/hooks/useListenToBrowserEvent';
-import { isGraphqlErrorOfType } from '~/utils/is-graphql-error-of-type.util';
-import { dispatchBrowserEvent } from '@/browser-event/utils/dispatchBrowserEvent';
-import { SSE_CLIENT_RECONNECTED_EVENT_NAME } from '@/sse-db-event/constants/SseClientReconnectedEventName';
-import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
-
-const MAX_SILENT_RECOVERY_ATTEMPTS = 3;
-
-export const AgentChatStreamKeepAliveEffect = () => {
- const store = useStore();
- const currentAiChatThread = useAtomStateValue(currentAiChatThreadState);
-
- const hasActiveSubscription =
- isDefined(currentAiChatThread) && isValidUuid(currentAiChatThread);
-
- const recoverStreamIfStalled = useCallback(() => {
- const familyKey = { threadId: currentAiChatThread };
- const isStreaming = store.get(
- agentChatIsStreamingFamilyState.atomFamily(familyKey),
- );
- const isAwaitingFirstChunk = store.get(
- agentChatIsAwaitingFirstChunkFamilyState.atomFamily(familyKey),
- );
-
- if (!isStreaming && !isAwaitingFirstChunk) {
- store.set(agentChatStreamRecoveryAttemptsState.atom, 0);
-
- return;
- }
-
- const recoveryAttempts = store.get(
- agentChatStreamRecoveryAttemptsState.atom,
- );
-
- if (recoveryAttempts >= MAX_SILENT_RECOVERY_ATTEMPTS) {
- store.set(
- agentChatErrorFamilyState.atomFamily(familyKey),
- createAiChatCodedError(
- t`Connection to the assistant was lost. Reload to see the response.`,
- AiChatErrorCode.CONNECTION_LOST,
- ),
- );
- store.set(agentChatIsStreamingFamilyState.atomFamily(familyKey), false);
- store.set(
- agentChatIsAwaitingFirstChunkFamilyState.atomFamily(familyKey),
- false,
- );
- store.set(agentChatStreamRecoveryAttemptsState.atom, 0);
- store.set(agentChatStreamLastEventTimestampState.atom, Date.now());
-
- return;
- }
-
- store.set(
- agentChatStreamRecoveryAttemptsState.atom,
- (attempts) => attempts + 1,
- );
- store.set(agentChatStreamLastEventTimestampState.atom, Date.now());
- store.set(agentChatStreamResubscribeNonceState.atom, (nonce) => nonce + 1);
- dispatchBrowserEvent(AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME);
- }, [store, currentAiChatThread]);
-
- useEffect(() => {
- if (!hasActiveSubscription) {
- return;
- }
-
- const interval = setInterval(() => {
- const lastTimestamp = store.get(
- agentChatStreamLastEventTimestampState.atom,
- );
-
- if (!isDefined(lastTimestamp)) {
- return;
- }
-
- const timeSinceLastEventInMs = Date.now() - lastTimestamp;
-
- if (timeSinceLastEventInMs <= AGENT_CHAT_STREAM_LIVENESS_TIMEOUT_IN_MS) {
- store.set(agentChatStreamRecoveryAttemptsState.atom, 0);
-
- const errorAtom = agentChatErrorFamilyState.atomFamily({
- threadId: currentAiChatThread,
- });
- const currentError = store.get(errorAtom);
-
- if (
- isDefined(currentError) &&
- isGraphqlErrorOfType(currentError, AiChatErrorCode.CONNECTION_LOST)
- ) {
- store.set(errorAtom, null);
- }
-
- return;
- }
-
- recoverStreamIfStalled();
- }, AGENT_CHAT_STREAM_LIVENESS_CHECK_INTERVAL_IN_MS);
-
- return () => clearInterval(interval);
- }, [
- hasActiveSubscription,
- store,
- recoverStreamIfStalled,
- currentAiChatThread,
- ]);
-
- useListenToBrowserEvent({
- eventName: SSE_CLIENT_RECONNECTED_EVENT_NAME,
- onBrowserEvent: recoverStreamIfStalled,
- });
-
- return null;
-};
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx
index 2a01192be01..cd984965c7d 100644
--- a/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AgentChatRuntimeEffects.test.tsx
@@ -22,9 +22,6 @@ jest.mock('@/ai/components/AgentChatStreamSubscriptionEffect', () => ({
jest.mock('@/ai/components/AgentChatPrepromptEffect', () => ({
AgentChatPrepromptEffect: () => null,
}));
-jest.mock('@/ai/components/AgentChatStreamKeepAliveEffect', () => ({
- AgentChatStreamKeepAliveEffect: () => null,
-}));
const renderRuntimeEffects = () =>
render(
diff --git a/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChatSubscription.test.tsx b/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChatSubscription.test.tsx
index d9888f9dfb9..bd71667bd82 100644
--- a/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChatSubscription.test.tsx
+++ b/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChatSubscription.test.tsx
@@ -8,6 +8,7 @@ import { agentChatMessagesFamilyState } from '@/ai/states/agentChatMessagesFamil
import { agentChatFetchedMessagesFamilyState } from '@/ai/states/agentChatFetchedMessagesFamilyState';
import { agentChatQueuedMessagesFamilyState } from '@/ai/states/agentChatQueuedMessagesFamilyState';
import { agentChatErrorFamilyState } from '@/ai/states/agentChatErrorFamilyState';
+import { agentChatIsAwaitingFirstChunkFamilyState } from '@/ai/states/agentChatIsAwaitingFirstChunkFamilyState';
import { sseClientState } from '@/sse-db-event/states/sseClientState';
import {
jotaiStore,
@@ -235,3 +236,47 @@ describe('Shared conversation access revocation', () => {
expect(refreshAgentChatThreads).not.toHaveBeenCalled();
});
});
+
+describe('Stream liveness', () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ jest.useFakeTimers();
+ resetJotaiStore();
+ subscribe.mockReturnValue(disconnect);
+ jotaiStore.set(sseClientState.atom, { subscribe } as never);
+ jotaiStore.set(
+ agentChatIsAwaitingFirstChunkFamilyState.atomFamily(key),
+ true,
+ );
+ });
+
+ afterEach(() => {
+ jest.useRealTimers();
+ });
+
+ it('reports a lost connection when resubscribing only yields the initial keepalive, then clears it once broadcasts arrive', () => {
+ const keepalive = {
+ data: {
+ onAgentChatEvent: { threadId: 'thread', event: { type: 'keepalive' } },
+ },
+ };
+ subscribe.mockImplementation((_request, sink) => {
+ sink.next(keepalive);
+ return disconnect;
+ });
+ renderHook(() => useAgentChatSubscription('thread'), { wrapper: Wrapper });
+
+ for (let index = 0; index < 15; index++) {
+ act(() => jest.advanceTimersByTime(2_000));
+ }
+
+ expect(subscribe).toHaveBeenCalledTimes(4);
+ expect(jotaiStore.get(errorAtom)).toMatchObject({
+ code: 'CONNECTION_LOST',
+ });
+
+ act(() => subscribe.mock.calls[3][1].next(keepalive));
+
+ expect(jotaiStore.get(errorAtom)).toBeNull();
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
index a4ed0c42543..cc0f0367b0f 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChatSubscription.ts
@@ -5,7 +5,7 @@ import { agentChatFetchedMessagesFamilyState } from '@/ai/states/agentChatFetche
import { agentChatQueuedMessagesFamilyState } from '@/ai/states/agentChatQueuedMessagesFamilyState';
import { useRefreshAgentChatThreads } from '@/ai/hooks/useRefreshAgentChatThreads';
import { isChatAccessDenied } from '@/ai/utils/isChatAccessDenied';
-import { useEffect } from 'react';
+import { useEffect, useState } from 'react';
import { t } from '@lingui/core/macro';
import { readUIMessageStream, type UIMessageChunk } from 'ai';
@@ -18,6 +18,8 @@ import {
import { isDefined, isNonEmptyString } from 'twenty-shared/utils';
import { AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME } from '@/ai/constants/AgentChatRefetchMessagesEventName';
+import { AGENT_CHAT_STREAM_LIVENESS_CHECK_INTERVAL_IN_MS } from '@/ai/constants/AgentChatStreamLivenessCheckIntervalInMs';
+import { AGENT_CHAT_STREAM_LIVENESS_TIMEOUT_IN_MS } from '@/ai/constants/AgentChatStreamLivenessTimeoutInMs';
import { useApplyAgentChatThreadUpdate } from '@/ai/hooks/useApplyAgentChatThreadUpdate';
import { agentChatErrorFamilyState } from '@/ai/states/agentChatErrorFamilyState';
import { agentChatFirstLiveSeqFamilyState } from '@/ai/states/agentChatFirstLiveSeqFamilyState';
@@ -26,8 +28,7 @@ import { agentChatIsAwaitingFirstChunkFamilyState } from '@/ai/states/agentChatI
import { agentChatIsAwaitingPersistedRefetchFamilyState } from '@/ai/states/agentChatIsAwaitingPersistedRefetchFamilyState';
import { agentChatIsStreamingFamilyState } from '@/ai/states/agentChatIsStreamingFamilyState';
import { agentChatMessagesFamilyState } from '@/ai/states/agentChatMessagesFamilyState';
-import { agentChatStreamLastEventTimestampState } from '@/ai/states/agentChatStreamLastEventTimestampState';
-import { agentChatStreamResubscribeNonceState } from '@/ai/states/agentChatStreamResubscribeNonceState';
+import { agentChatStreamRecoveryAttemptsState } from '@/ai/states/agentChatStreamRecoveryAttemptsState';
import { agentChatUsageFamilyState } from '@/ai/states/agentChatUsageFamilyState';
import { agentChatThreadRecordFamilySelector } from '@/ai/states/selectors/agentChatThreadRecordFamilySelector';
import { accumulateAgentChatUsage } from '@/ai/utils/accumulateAgentChatUsage';
@@ -49,6 +50,7 @@ import {
const THROTTLE_MS = 100;
const PERMISSIONS_REFRESH_INTERVAL_MS = 30_000;
+const MAX_SILENT_RECOVERY_ATTEMPTS = 3;
export const useAgentChatSubscription = (threadId: string | null) => {
const store = useStore();
@@ -59,9 +61,7 @@ export const useAgentChatSubscription = (threadId: string | null) => {
const { refreshAgentChatThreads } = useRefreshAgentChatThreads();
const { applyAgentChatThreadUpdate } = useApplyAgentChatThreadUpdate();
const sseClient = useAtomStateValue(sseClientState);
- const agentChatStreamResubscribeNonce = useAtomStateValue(
- agentChatStreamResubscribeNonceState,
- );
+ const [resubscribeNonce, setResubscribeNonce] = useState(0);
useEffect(() => {
if (!isDefined(threadId) || !isDefined(sseClient)) {
@@ -95,9 +95,10 @@ export const useAgentChatSubscription = (threadId: string | null) => {
let disposed = false;
let accessDenied = false;
let lastPermissionsRefreshAt: number | undefined;
+ let lastBroadcastAt = Date.now();
+ let hasReceivedInitialKeepalive = false;
store.set(firstLiveSeqAtom, null);
- store.set(agentChatStreamLastEventTimestampState.atom, Date.now());
const closeWriter = () => {
if (isDefined(writer)) {
@@ -351,13 +352,23 @@ export const useAgentChatSubscription = (threadId: string | null) => {
handleAccessDenied();
return;
}
- store.set(agentChatStreamLastEventTimestampState.atom, Date.now());
+ // The server yields the first keepalive itself on subscribe, so only later events prove pub/sub delivers
+ if (hasReceivedInitialKeepalive) {
+ lastBroadcastAt = Date.now();
+ store.set(agentChatStreamRecoveryAttemptsState.atom, 0);
+ }
+ hasReceivedInitialKeepalive = true;
const event: AgentChatSubscriptionEvent | undefined =
value.data?.onAgentChatEvent?.event;
if (isDefined(event)) {
- if (isGraphqlErrorOfType(store.get(errorAtom), 'NOT_FOUND')) {
+ const error = store.get(errorAtom);
+
+ if (
+ isGraphqlErrorOfType(error, 'NOT_FOUND') ||
+ isGraphqlErrorOfType(error, AiChatErrorCode.CONNECTION_LOST)
+ ) {
store.set(errorAtom, null);
dispatchBrowserEvent(AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME);
}
@@ -385,8 +396,47 @@ export const useAgentChatSubscription = (threadId: string | null) => {
dispose();
}
+ const livenessInterval = setInterval(() => {
+ const isPending =
+ store.get(isStreamingAtom) || store.get(isAwaitingFirstChunkAtom);
+ const recoveryAttempts = store.get(
+ agentChatStreamRecoveryAttemptsState.atom,
+ );
+
+ // Resubscribing clears the pending flags, so a started recovery runs until a broadcast arrives or it gives up
+ if (
+ accessDenied ||
+ (!isPending && recoveryAttempts === 0) ||
+ Date.now() - lastBroadcastAt <= AGENT_CHAT_STREAM_LIVENESS_TIMEOUT_IN_MS
+ ) {
+ return;
+ }
+
+ if (recoveryAttempts >= MAX_SILENT_RECOVERY_ATTEMPTS) {
+ store.set(agentChatStreamRecoveryAttemptsState.atom, 0);
+ store.set(
+ errorAtom,
+ createAiChatCodedError(
+ t`Connection to the assistant was lost. Reload to see the response.`,
+ AiChatErrorCode.CONNECTION_LOST,
+ ),
+ );
+ resetStreamProcessing();
+ store.set(isStreamingAtom, false);
+ return;
+ }
+
+ store.set(
+ agentChatStreamRecoveryAttemptsState.atom,
+ recoveryAttempts + 1,
+ );
+ setResubscribeNonce((nonce) => nonce + 1);
+ dispatchBrowserEvent(AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME);
+ }, AGENT_CHAT_STREAM_LIVENESS_CHECK_INTERVAL_IN_MS);
+
return () => {
disposed = true;
+ clearInterval(livenessInterval);
chunkSequencer.reset();
store.set(isAwaitingFirstChunkAtom, false);
store.set(handleEventCallbackAtom, null);
@@ -401,7 +451,7 @@ export const useAgentChatSubscription = (threadId: string | null) => {
}, [
threadId,
sseClient,
- agentChatStreamResubscribeNonce,
+ resubscribeNonce,
store,
refreshAgentChatThreads,
refreshAgentChatThreadPermissions,
diff --git a/packages/twenty-front/src/modules/ai/states/agentChatStreamLastEventTimestampState.ts b/packages/twenty-front/src/modules/ai/states/agentChatStreamLastEventTimestampState.ts
deleted file mode 100644
index 2d9d2b967ea..00000000000
--- a/packages/twenty-front/src/modules/ai/states/agentChatStreamLastEventTimestampState.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';
-
-export const agentChatStreamLastEventTimestampState = createAtomState<
- number | null
->({
- key: 'agentChatStreamLastEventTimestampState',
- defaultValue: null,
-});
diff --git a/packages/twenty-front/src/modules/ai/states/agentChatStreamResubscribeNonceState.ts b/packages/twenty-front/src/modules/ai/states/agentChatStreamResubscribeNonceState.ts
deleted file mode 100644
index f5322287ba3..00000000000
--- a/packages/twenty-front/src/modules/ai/states/agentChatStreamResubscribeNonceState.ts
+++ /dev/null
@@ -1,6 +0,0 @@
-import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';
-
-export const agentChatStreamResubscribeNonceState = createAtomState<number>({
- key: 'agentChatStreamResubscribeNonceState',
- defaultValue: 0,
-});