The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(ai): bound silent stream recovery and surface a terminal CONNECTION_LOST state (#22486)
Félix Malfait4a8679327bca
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsxindex a71f03f59d8..0719a6b8ea6 100644--- a/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx+++ b/packages/twenty-front/src/modules/ai/components/AgentChatStreamKeepAliveEffect.tsx@@ -5,16 +5,24 @@ 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 { agentChatErrorComponentFamilyState } from '@/ai/states/agentChatErrorComponentFamilyState';+import { agentChatIsAwaitingFirstChunkComponentFamilyState } from '@/ai/states/agentChatIsAwaitingFirstChunkComponentFamilyState';import { agentChatIsStreamingComponentFamilyState } from '@/ai/states/agentChatIsStreamingComponentFamilyState';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 { useAtomComponentFamilyStateCallbackState } from '@/ui/utilities/state/jotai/hooks/useAtomComponentFamilyStateCallbackState';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);@@ -22,23 +30,64 @@ export const AgentChatStreamKeepAliveEffect = () => {const isStreamingFamilyCallback = useAtomComponentFamilyStateCallbackState(agentChatIsStreamingComponentFamilyState,);+ const isAwaitingFirstChunkFamilyCallback =+ useAtomComponentFamilyStateCallbackState(+ agentChatIsAwaitingFirstChunkComponentFamilyState,+ );+ const errorFamilyCallback = useAtomComponentFamilyStateCallbackState(+ agentChatErrorComponentFamilyState,+ );const hasActiveSubscription =isDefined(currentAiChatThread) && isValidUuid(currentAiChatThread);- const recoverStreamIfStreaming = useCallback(() => {- const isStreaming = store.get(- isStreamingFamilyCallback({ threadId: currentAiChatThread }),+ const recoverStreamIfStalled = useCallback(() => {+ const familyKey = { threadId: currentAiChatThread };+ const isStreaming = store.get(isStreamingFamilyCallback(familyKey));+ const isAwaitingFirstChunk = store.get(+ isAwaitingFirstChunkFamilyCallback(familyKey),);- if (!isStreaming) {+ if (!isStreaming && !isAwaitingFirstChunk) {+ store.set(agentChatStreamRecoveryAttemptsState.atom, 0);+return;}+ const recoveryAttempts = store.get(+ agentChatStreamRecoveryAttemptsState.atom,+ );++ if (recoveryAttempts >= MAX_SILENT_RECOVERY_ATTEMPTS) {+ store.set(+ errorFamilyCallback(familyKey),+ createAiChatCodedError(+ 'Connection to the assistant was lost. Reload to see the response.',+ AiChatErrorCode.CONNECTION_LOST,+ ),+ );+ store.set(isStreamingFamilyCallback(familyKey), false);+ store.set(isAwaitingFirstChunkFamilyCallback(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, isStreamingFamilyCallback, currentAiChatThread]);+ }, [+ store,+ isStreamingFamilyCallback,+ isAwaitingFirstChunkFamilyCallback,+ errorFamilyCallback,+ currentAiChatThread,+ ]);useEffect(() => {if (!hasActiveSubscription) {@@ -57,18 +106,38 @@ export const AgentChatStreamKeepAliveEffect = () => {const timeSinceLastEventInMs = Date.now() - lastTimestamp;if (timeSinceLastEventInMs <= AGENT_CHAT_STREAM_LIVENESS_TIMEOUT_IN_MS) {+ store.set(agentChatStreamRecoveryAttemptsState.atom, 0);++ const errorAtom = errorFamilyCallback({+ threadId: currentAiChatThread,+ });+ const currentError = store.get(errorAtom);++ if (+ isDefined(currentError) &&+ isGraphqlErrorOfType(currentError, AiChatErrorCode.CONNECTION_LOST)+ ) {+ store.set(errorAtom, null);+ }+return;}- recoverStreamIfStreaming();+ recoverStreamIfStalled();}, AGENT_CHAT_STREAM_LIVENESS_CHECK_INTERVAL_IN_MS);return () => clearInterval(interval);- }, [hasActiveSubscription, store, recoverStreamIfStreaming]);+ }, [+ hasActiveSubscription,+ store,+ recoverStreamIfStalled,+ errorFamilyCallback,+ currentAiChatThread,+ ]);useListenToBrowserEvent({eventName: SSE_CLIENT_RECONNECTED_EVENT_NAME,- onBrowserEvent: recoverStreamIfStreaming,+ onBrowserEvent: recoverStreamIfStalled,});return null;diff --git a/packages/twenty-front/src/modules/ai/components/AiChatErrorRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatErrorRenderer.tsxindex c5f43eb96c8..b4f6e7af1ad 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatErrorRenderer.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatErrorRenderer.tsx@@ -22,5 +22,9 @@ export const AiChatErrorRenderer = ({return <AiChatApiKeyNotConfiguredMessage />;}+ if (isGraphqlErrorOfType(error, AiChatErrorCode.CONNECTION_LOST)) {+ return <AiChatErrorMessage error={error} />;+ }+return <AiChatErrorMessage error={error} onRetry={onRetry} />;};diff --git a/packages/twenty-front/src/modules/ai/states/agentChatStreamRecoveryAttemptsState.ts b/packages/twenty-front/src/modules/ai/states/agentChatStreamRecoveryAttemptsState.tsnew file mode 100644index 00000000000..900d8999bde--- /dev/null+++ b/packages/twenty-front/src/modules/ai/states/agentChatStreamRecoveryAttemptsState.ts@@ -0,0 +1,6 @@+import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';++export const agentChatStreamRecoveryAttemptsState = createAtomState<number>({+ key: 'agentChatStreamRecoveryAttemptsState',+ defaultValue: 0,+});diff --git a/packages/twenty-front/src/modules/ai/utils/aiChatErrorCode.ts b/packages/twenty-front/src/modules/ai/utils/aiChatErrorCode.tsindex ebeae4f5304..b106c8c9788 100644--- a/packages/twenty-front/src/modules/ai/utils/aiChatErrorCode.ts+++ b/packages/twenty-front/src/modules/ai/utils/aiChatErrorCode.ts@@ -1,5 +1,5 @@-// Error codes matching backend AgentExceptionCode and BillingExceptionCodeexport const AiChatErrorCode = {BILLING_CREDITS_EXHAUSTED: 'BILLING_CREDITS_EXHAUSTED',API_KEY_NOT_CONFIGURED: 'API_KEY_NOT_CONFIGURED',+ CONNECTION_LOST: 'CONNECTION_LOST',} as const;