The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
perf(ai): lazy-load agent chat runtime so it doesn't fetch/diff threads until opened (#21331)
Weiko55cbd3bfbf4a
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsxindex 70cedeaab9f..d8471610a38 100644--- a/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx+++ b/packages/twenty-front/src/modules/ai/components/AgentChatProviderContent.tsx@@ -1,9 +1,4 @@-import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect';-import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect';-import { AgentChatSessionStartTimeEffect } from '@/ai/components/AgentChatSessionStartTimeEffect';--import { AgentChatStreamingAutoScrollEffect } from '@/ai/components/AgentChatStreamingAutoScrollEffect';-import { AgentChatStreamingPartsDiffSyncEffect } from '@/ai/components/AgentChatStreamingPartsDiffSyncEffect';+import { AgentChatRuntimeEffects } from '@/ai/components/AgentChatRuntimeEffects';import { AgentChatThreadInitializationEffect } from '@/ai/components/AgentChatThreadInitializationEffect';import { AgentChatComponentInstanceContext } from '@/ai/contexts/AgentChatComponentInstanceContext';import { Suspense } from 'react';@@ -19,11 +14,7 @@ export const AgentChatProviderContent = ({value={{ instanceId: 'agentChatComponentInstance' }}><AgentChatThreadInitializationEffect />- <AgentChatMessagesFetchEffect />- <AgentChatStreamSubscriptionEffect />- <AgentChatStreamingPartsDiffSyncEffect />- <AgentChatSessionStartTimeEffect />- <AgentChatStreamingAutoScrollEffect />+ <AgentChatRuntimeEffects />{children}</AgentChatComponentInstanceContext.Provider></Suspense>diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsxnew file mode 100644index 00000000000..df4e0b0b46a--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/AgentChatRuntimeEffects.tsx@@ -0,0 +1,48 @@+import { AgentChatMessagesFetchEffect } from '@/ai/components/AgentChatMessagesFetchEffect';+import { AgentChatSessionStartTimeEffect } from '@/ai/components/AgentChatSessionStartTimeEffect';+import { AgentChatStreamSubscriptionEffect } from '@/ai/components/AgentChatStreamSubscriptionEffect';+import { AgentChatStreamingAutoScrollEffect } from '@/ai/components/AgentChatStreamingAutoScrollEffect';+import { AgentChatStreamingPartsDiffSyncEffect } from '@/ai/components/AgentChatStreamingPartsDiffSyncEffect';+import { hasAgentChatBeenOpenedState } from '@/ai/states/hasAgentChatBeenOpenedState';+import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';+import { sidePanelPageState } from '@/side-panel/states/sidePanelPageState';+import { useAtomState } from '@/ui/utilities/state/jotai/hooks/useAtomState';+import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';+import { useEffect } from 'react';+import { SidePanelPages } from 'twenty-shared/types';++export const AgentChatRuntimeEffects = () => {+ const isSidePanelOpened = useAtomStateValue(isSidePanelOpenedState);+ const sidePanelPage = useAtomStateValue(sidePanelPageState);++ const [hasAgentChatBeenOpened, setHasAgentChatBeenOpened] = useAtomState(+ hasAgentChatBeenOpenedState,+ );++ const isAgentChatOpen =+ isSidePanelOpened && sidePanelPage === SidePanelPages.AskAI;++ useEffect(() => {+ if (isAgentChatOpen && !hasAgentChatBeenOpened) {+ setHasAgentChatBeenOpened(true);+ }+ }, [isAgentChatOpen, hasAgentChatBeenOpened, setHasAgentChatBeenOpened]);++ if (!hasAgentChatBeenOpened) {+ return null;+ }++ return (+ <>+ <AgentChatMessagesFetchEffect />+ <AgentChatStreamSubscriptionEffect />+ <AgentChatSessionStartTimeEffect />+ {isAgentChatOpen && (+ <>+ <AgentChatStreamingPartsDiffSyncEffect />+ <AgentChatStreamingAutoScrollEffect />+ </>+ )}+ </>+ );+};diff --git a/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.ts b/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.tsnew file mode 100644index 00000000000..c2b6aa6ee1d--- /dev/null+++ b/packages/twenty-front/src/modules/ai/states/hasAgentChatBeenOpenedState.ts@@ -0,0 +1,6 @@+import { createAtomState } from '@/ui/utilities/state/jotai/utils/createAtomState';++export const hasAgentChatBeenOpenedState = createAtomState<boolean>({+ key: 'ai/hasAgentChatBeenOpenedState',+ defaultValue: false,+});