Skip to content

The open alternative to Salesforce, designed for AI.

fix(ask-ai): resolve stream subscription race condition on new thread… (#21916)

Akash!87329c881025
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
index e1cbed6694f..e381c5c13b7 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
@@ -1,7 +1,7 @@
import { CombinedGraphQLErrors } from '@apollo/client/errors';
import { useApolloClient } from '@apollo/client/react';
import { useStore } from 'jotai';
-import { useCallback, useState } from 'react';
+import { useCallback } from 'react';
import { type ExtendedUIMessage } from 'twenty-shared/ai';
import { isDefined, isValidUuid } from 'twenty-shared/utils';
import { v4 } from 'uuid';
@@ -44,8 +44,6 @@ export const useAgentChat = (
const setCurrentAiChatThread = useSetAtomState(currentAiChatThreadState);
const store = useStore();
- const [, setPendingThreadIdAfterFirstSend] = useState<string | null>(null);
-
const setAgentChatUploadedFiles = useSetAtomState(
agentChatUploadedFilesState,
);
@@ -87,7 +85,7 @@ export const useAgentChat = (
}
if (draftKey === AGENT_CHAT_NEW_THREAD_DRAFT_KEY) {
- setPendingThreadIdAfterFirstSend(threadId);
+ setCurrentAiChatThread(threadId);
}
setAgentChatInput('');
@@ -184,14 +182,6 @@ export const useAgentChat = (
}
dispatchBrowserEvent(AGENT_CHAT_REFETCH_MESSAGES_EVENT_NAME);
-
- setPendingThreadIdAfterFirstSend((pendingId) => {
- if (isDefined(pendingId)) {
- setCurrentAiChatThread(pendingId);
- }
-
- return null;
- });
} catch (error) {
const restoredDraftKey =
draftKey === AGENT_CHAT_NEW_THREAD_DRAFT_KEY ? threadId : draftKey;
@@ -225,12 +215,6 @@ export const useAgentChat = (
dispatchBrowserEvent(AGENT_CHAT_RESTORE_EDITOR_CONTENT_EVENT_NAME, {
content: contentToSend,
});
-
- if (draftKey === AGENT_CHAT_NEW_THREAD_DRAFT_KEY) {
- setCurrentAiChatThread(threadId);
- }
-
- setPendingThreadIdAfterFirstSend(null);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [