Skip to content

The open alternative to Salesforce, designed for AI.

fix: lagging issue in ask AI during message streaming (#16201)

Abdul Rahman19fc20173b48
diff --git a/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx b/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
index df76d907b17..c45a668cdd3 100644
--- a/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
@@ -19,10 +19,12 @@ import { AI_CHAT_INPUT_ID } from '@/ai/constants/AiChatInputId';
import { AI_CHAT_SCROLL_WRAPPER_ID } from '@/ai/constants/AiChatScrollWrapperId';
import { useAIChatFileUpload } from '@/ai/hooks/useAIChatFileUpload';
import { useAgentChatContextOrThrow } from '@/ai/hooks/useAgentChatContextOrThrow';
+import { agentChatInputState } from '@/ai/states/agentChatInputState';
import { contextStoreCurrentObjectMetadataItemIdComponentState } from '@/context-store/states/contextStoreCurrentObjectMetadataItemIdComponentState';
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
import { t } from '@lingui/core/macro';
import { useState } from 'react';
+import { useRecoilState } from 'recoil';
import { Button } from 'twenty-ui/input';
const StyledContainer = styled.div<{ isDraggingFile: boolean }>`
@@ -63,9 +65,12 @@ const StyledButtonsContainer = styled.div`
export const AIChatTab = () => {
const [isDraggingFile, setIsDraggingFile] = useState(false);
- const { isLoading, input, handleInputChange, messages, isStreaming, error } =
+ const { isLoading, messages, isStreaming, error } =
useAgentChatContextOrThrow();
+ const [agentChatInput, setAgentChatInput] =
+ useRecoilState(agentChatInputState);
+
const contextStoreCurrentObjectMetadataItemId = useRecoilComponentValue(
contextStoreCurrentObjectMetadataItemIdComponentState,
);
@@ -115,8 +120,8 @@ export const AIChatTab = () => {
<TextArea
textAreaId={AI_CHAT_INPUT_ID}
placeholder={t`Enter a question...`}
- value={input}
- onChange={handleInputChange}
+ value={agentChatInput}
+ onChange={(value) => setAgentChatInput(value)}
minRows={1}
maxRows={20}
/>
diff --git a/packages/twenty-front/src/modules/ai/components/internal/SendMessageButton.tsx b/packages/twenty-front/src/modules/ai/components/internal/SendMessageButton.tsx
index 392c4aa617c..09bbe5b35c9 100644
--- a/packages/twenty-front/src/modules/ai/components/internal/SendMessageButton.tsx
+++ b/packages/twenty-front/src/modules/ai/components/internal/SendMessageButton.tsx
@@ -1,8 +1,10 @@
import { AI_CHAT_INPUT_ID } from '@/ai/constants/AiChatInputId';
import { useAgentChatContextOrThrow } from '@/ai/hooks/useAgentChatContextOrThrow';
+import { agentChatInputState } from '@/ai/states/agentChatInputState';
import { type ObjectRecord } from '@/object-record/types/ObjectRecord';
import { useHotkeysOnFocusedElement } from '@/ui/utilities/hotkey/hooks/useHotkeysOnFocusedElement';
import { t } from '@lingui/core/macro';
+import { useRecoilValue } from 'recoil';
import { Key } from 'ts-key-enum';
import { Button } from 'twenty-ui/input';
@@ -11,7 +13,8 @@ export const SendMessageButton = ({
}: {
records?: ObjectRecord[];
}) => {
- const { handleSendMessage, isLoading, input } = useAgentChatContextOrThrow();
+ const agentChatInput = useRecoilValue(agentChatInputState);
+ const { handleSendMessage, isLoading } = useAgentChatContextOrThrow();
useHotkeysOnFocusedElement({
keys: [Key.Enter],
@@ -22,7 +25,7 @@ export const SendMessageButton = ({
}
},
focusId: AI_CHAT_INPUT_ID,
- dependencies: [input, isLoading],
+ dependencies: [agentChatInput, isLoading],
options: {
enableOnFormTags: true,
},
@@ -30,9 +33,9 @@ export const SendMessageButton = ({
return (
<Button
- hotkeys={input && !isLoading ? ['⏎'] : undefined}
+ hotkeys={agentChatInput && !isLoading ? ['⏎'] : undefined}
onClick={() => handleSendMessage(records)}
- disabled={!input || isLoading}
+ disabled={!agentChatInput || isLoading}
variant="primary"
accent="blue"
size="small"
diff --git a/packages/twenty-front/src/modules/ai/contexts/AgentChatContext.ts b/packages/twenty-front/src/modules/ai/contexts/AgentChatContext.ts
index d99525f3581..ca57cb44784 100644
--- a/packages/twenty-front/src/modules/ai/contexts/AgentChatContext.ts
+++ b/packages/twenty-front/src/modules/ai/contexts/AgentChatContext.ts
@@ -8,9 +8,6 @@ export type AgentChatContextValue = {
isLoading: boolean;
error?: Error;
- input: string;
- handleInputChange: (value: string) => void;
-
handleSendMessage: (records?: ObjectRecord[]) => Promise<void>;
handleRetry: () => void;
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
index 6a1273ffc80..7da5c0dcadc 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts
@@ -110,6 +110,7 @@ export const useAgentChat = (uiMessages: ExtendedUIMessage[]) => {
}),
messages: uiMessages,
id: `${currentAIChatThread}-${uiMessages.length}`,
+ experimental_throttle: 100,
});
const isStreaming = status === 'streaming';
@@ -156,9 +157,7 @@ export const useAgentChat = (uiMessages: ExtendedUIMessage[]) => {
};
return {
- handleInputChange: (value: string) => setAgentChatInput(value),
messages,
- input: agentChatInput,
handleSendMessage,
isLoading,
isStreaming,
diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChatScrollToBottom.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChatScrollToBottom.ts
index 3697ee2f6a0..51a724c3ace 100644
--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChatScrollToBottom.ts
+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChatScrollToBottom.ts
@@ -15,7 +15,6 @@ export const useAgentChatScrollToBottom = () => {
scrollWrapperElement.scrollTo({
top: scrollWrapperElement.scrollHeight,
- behavior: 'smooth',
});
};