The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(ai): stop refetching the tool index on every chat step mount (#27689)
Félix Malfait73b5b5eb975f
diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatProvider.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatProvider.tsxindex cee09919748..b88dddaeed4 100644--- a/packages/twenty-front/src/modules/ai/components/AgentChatProvider.tsx+++ b/packages/twenty-front/src/modules/ai/components/AgentChatProvider.tsx@@ -4,6 +4,7 @@ import { AgentChatRuntimeEffects } from '@/ai/components/AgentChatRuntimeEffectsimport { AgentChatThreadInitializationEffect } from '@/ai/components/AgentChatThreadInitializationEffect';import { AgentChatThreadParticipantOperationsEffect } from '@/ai/components/AgentChatThreadParticipantOperationsEffect';import { AgentChatThreadRecordOperationsEffect } from '@/ai/components/AgentChatThreadRecordOperationsEffect';+import { AgentChatToolIndexInvalidationEffect } from '@/ai/components/AgentChatToolIndexInvalidationEffect';type AgentChatProviderProps = {children: ReactNode;@@ -15,6 +16,7 @@ export const AgentChatProvider = ({ children }: AgentChatProviderProps) => (<AgentChatThreadRecordOperationsEffect /><AgentChatThreadParticipantOperationsEffect /><AgentChatRuntimeEffects />+ <AgentChatToolIndexInvalidationEffect />{children}</Suspense>);diff --git a/packages/twenty-front/src/modules/ai/components/AgentChatToolIndexInvalidationEffect.tsx b/packages/twenty-front/src/modules/ai/components/AgentChatToolIndexInvalidationEffect.tsxnew file mode 100644index 00000000000..1a28261b4b2--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/AgentChatToolIndexInvalidationEffect.tsx@@ -0,0 +1,44 @@+import { useCallback } from 'react';+import { useDebouncedCallback } from 'use-debounce';++import { TOOL_INDEX_DEPENDENT_METADATA_NAMES } from '@/ai/constants/ToolIndexDependentMetadataNames';+import { TOOL_INDEX_INVALIDATION_DEBOUNCE_TIME_IN_MS } from '@/ai/constants/ToolIndexInvalidationDebounceTimeInMs';+import { useInvalidateToolIndex } from '@/ai/hooks/useInvalidateToolIndex';+import { useListenToBrowserEvent } from '@/browser-event/hooks/useListenToBrowserEvent';+import { useListenToMetadataOperationBrowserEvent } from '@/browser-event/hooks/useListenToMetadataOperationBrowserEvent';+import { type MetadataOperationBrowserEventDetail } from '@/browser-event/types/MetadataOperationBrowserEventDetail';+import { SSE_CLIENT_RECONNECTED_EVENT_NAME } from '@/sse-db-event/constants/SseClientReconnectedEventName';++export const AgentChatToolIndexInvalidationEffect = () => {+ const { invalidateToolIndex } = useInvalidateToolIndex();++ // Installing an app broadcasts many entities at once; one rebuild is enough+ const debouncedInvalidateToolIndex = useDebouncedCallback(+ invalidateToolIndex,+ TOOL_INDEX_INVALIDATION_DEBOUNCE_TIME_IN_MS,+ { leading: false },+ );++ const handleMetadataOperation = useCallback(+ ({+ metadataName,+ }: MetadataOperationBrowserEventDetail<Record<string, unknown>>) => {+ if (TOOL_INDEX_DEPENDENT_METADATA_NAMES.includes(metadataName)) {+ debouncedInvalidateToolIndex();+ }+ },+ [debouncedInvalidateToolIndex],+ );++ useListenToMetadataOperationBrowserEvent({+ onMetadataOperationBrowserEvent: handleMetadataOperation,+ });++ // Changes broadcast while disconnected are not replayed+ useListenToBrowserEvent({+ eventName: SSE_CLIENT_RECONNECTED_EVENT_NAME,+ onBrowserEvent: debouncedInvalidateToolIndex,+ });++ return null;+};diff --git a/packages/twenty-front/src/modules/ai/constants/ToolIndexDependentMetadataNames.ts b/packages/twenty-front/src/modules/ai/constants/ToolIndexDependentMetadataNames.tsnew file mode 100644index 00000000000..cc1845b5e2b--- /dev/null+++ b/packages/twenty-front/src/modules/ai/constants/ToolIndexDependentMetadataNames.ts@@ -0,0 +1,14 @@+import { type BroadcastEntityName } from '@/browser-event/types/BroadcastEntityName';++// What the server reads to build the role-scoped tool index+export const TOOL_INDEX_DEPENDENT_METADATA_NAMES: BroadcastEntityName[] = [+ 'objectMetadata',+ 'logicFunction',+ 'frontComponent',+ 'application',+ 'role',+ 'roleTarget',+ 'objectPermission',+ 'permissionFlag',+ 'rolePermissionFlag',+];diff --git a/packages/twenty-front/src/modules/ai/constants/ToolIndexInvalidationDebounceTimeInMs.ts b/packages/twenty-front/src/modules/ai/constants/ToolIndexInvalidationDebounceTimeInMs.tsnew file mode 100644index 00000000000..d34f16955c0--- /dev/null+++ b/packages/twenty-front/src/modules/ai/constants/ToolIndexInvalidationDebounceTimeInMs.ts@@ -0,0 +1 @@+export const TOOL_INDEX_INVALIDATION_DEBOUNCE_TIME_IN_MS = 500;diff --git a/packages/twenty-front/src/modules/ai/hooks/__tests__/useGetToolIndex.test.tsx b/packages/twenty-front/src/modules/ai/hooks/__tests__/useGetToolIndex.test.tsxnew file mode 100644index 00000000000..bf14b3f3d73--- /dev/null+++ b/packages/twenty-front/src/modules/ai/hooks/__tests__/useGetToolIndex.test.tsx@@ -0,0 +1,132 @@+import { MockedProvider } from '@apollo/client/testing/react';+import { act, render, screen, waitFor } from '@testing-library/react';++import { AgentChatToolIndexInvalidationEffect } from '@/ai/components/AgentChatToolIndexInvalidationEffect';+import { TOOL_INDEX_INVALIDATION_DEBOUNCE_TIME_IN_MS } from '@/ai/constants/ToolIndexInvalidationDebounceTimeInMs';+import { useGetToolIndex } from '@/ai/hooks/useGetToolIndex';+import { dispatchMetadataOperationBrowserEvent } from '@/browser-event/utils/dispatchMetadataOperationBrowserEvent';+import { GetToolIndexDocument } from '~/generated-metadata/graphql';++const TOOL_LABEL = 'Find companies';++const toolIndexResult = jest.fn(() => ({+ data: {+ getToolIndex: [+ {+ __typename: 'ToolIndexEntry' as const,+ name: 'find_companies',+ label: TOOL_LABEL,+ description: 'Search companies',+ category: 'DATABASE_CRUD',+ objectName: 'company',+ icon: null,+ widgetName: null,+ frontComponentId: null,+ },+ ],+ },+}));++const TOOL_INDEX_MOCKS = [1, 2, 3].map(() => ({+ request: { query: GetToolIndexDocument },+ delay: 0,+ result: toolIndexResult,+}));++const ToolStepLabel = () => {+ const { toolIndex } = useGetToolIndex();++ return <span>{toolIndex[0]?.label}</span>;+};++// Mirrors the metadata client, whose default policy is cache-and-network+const buildChat = (toolStepCount: number) => (+ <MockedProvider+ mocks={TOOL_INDEX_MOCKS}+ defaultOptions={{ watchQuery: { fetchPolicy: 'cache-and-network' } }}+ >+ <>+ <AgentChatToolIndexInvalidationEffect />+ {Array.from({ length: toolStepCount }, (_, index) => (+ <ToolStepLabel key={index} />+ ))}+ </>+ </MockedProvider>+);++const wait = (durationInMs: number) =>+ act(async () => {+ await new Promise((resolve) => setTimeout(resolve, durationInMs));+ });++const flushPendingRequests = () => wait(0);++const waitPastInvalidationDebounce = () =>+ wait(TOOL_INDEX_INVALIDATION_DEBOUNCE_TIME_IN_MS + 100);++const dispatchMetadataUpdate = (metadataName: 'objectMetadata' | 'view') =>+ act(() => {+ dispatchMetadataOperationBrowserEvent({+ metadataName,+ operation: { type: 'update', updatedRecord: { id: 'record-id' } },+ });+ });++describe('useGetToolIndex', () => {+ beforeEach(() => {+ toolIndexResult.mockClear();+ });++ it('should fetch the tool index once while tool steps keep mounting', async () => {+ const { rerender } = render(buildChat(1));++ await screen.findByText(TOOL_LABEL);++ rerender(buildChat(3));++ expect(await screen.findAllByText(TOOL_LABEL)).toHaveLength(3);+ await flushPendingRequests();+ expect(toolIndexResult).toHaveBeenCalledTimes(1);+ });++ it('should fetch the tool index again when something it is built from changes', async () => {+ render(buildChat(2));++ await screen.findAllByText(TOOL_LABEL);++ dispatchMetadataUpdate('objectMetadata');++ await waitFor(() => expect(toolIndexResult).toHaveBeenCalledTimes(2));+ await flushPendingRequests();+ expect(toolIndexResult).toHaveBeenCalledTimes(2);+ });++ it('should fetch the tool index once after a burst of changes', async () => {+ render(buildChat(2));++ await screen.findAllByText(TOOL_LABEL);++ dispatchMetadataUpdate('objectMetadata');+ dispatchMetadataUpdate('objectMetadata');+ dispatchMetadataUpdate('objectMetadata');+ await flushPendingRequests();++ expect(toolIndexResult).toHaveBeenCalledTimes(1);++ await waitPastInvalidationDebounce();+ await flushPendingRequests();++ expect(toolIndexResult).toHaveBeenCalledTimes(2);+ });++ it('should keep the cached tool index when unrelated metadata changes', async () => {+ render(buildChat(2));++ await screen.findAllByText(TOOL_LABEL);++ dispatchMetadataUpdate('view');+ await waitPastInvalidationDebounce();++ expect(toolIndexResult).toHaveBeenCalledTimes(1);+ });+});diff --git a/packages/twenty-front/src/modules/ai/hooks/useGetToolIndex.ts b/packages/twenty-front/src/modules/ai/hooks/useGetToolIndex.tsindex 9422956b1ce..38dd4fad707 100644--- a/packages/twenty-front/src/modules/ai/hooks/useGetToolIndex.ts+++ b/packages/twenty-front/src/modules/ai/hooks/useGetToolIndex.ts@@ -7,8 +7,14 @@ import {const EMPTY_TOOL_INDEX: NonNullable<GetToolIndexQuery['getToolIndex']> = [];-export const useGetToolIndex = () => {- const { data, loading, error } = useQuery(GetToolIndexDocument);+export const useGetToolIndex = ({ skip = false }: { skip?: boolean } = {}) => {+ // The metadata client defaults to cache-and-network, which would rebuild the+ // whole index server-side each time a chat step row mounts; freshness comes+ // from AgentChatToolIndexInvalidationEffect instead+ const { data, loading, error } = useQuery(GetToolIndexDocument, {+ fetchPolicy: 'cache-first',+ skip,+ });return {toolIndex: data?.getToolIndex ?? EMPTY_TOOL_INDEX,diff --git a/packages/twenty-front/src/modules/ai/hooks/useInvalidateToolIndex.ts b/packages/twenty-front/src/modules/ai/hooks/useInvalidateToolIndex.tsnew file mode 100644index 00000000000..e77dbda5873--- /dev/null+++ b/packages/twenty-front/src/modules/ai/hooks/useInvalidateToolIndex.ts@@ -0,0 +1,20 @@+import { useApolloClient } from '@apollo/client/react';+import { useCallback } from 'react';++// The tool index is read cache-first: dropping it refetches mounted readers+// once, and later readers fetch it on mount+export const useInvalidateToolIndex = () => {+ const client = useApolloClient();++ const invalidateToolIndex = useCallback(() => {+ client+ .refetchQueries({+ updateCache: (cache) => {+ cache.evict({ id: 'ROOT_QUERY', fieldName: 'getToolIndex' });+ },+ })+ .catch(() => undefined);+ }, [client]);++ return { invalidateToolIndex };+};diff --git a/packages/twenty-front/src/pages/settings/ai/SettingsToolDetail.tsx b/packages/twenty-front/src/pages/settings/ai/SettingsToolDetail.tsxindex f94b90c9072..0d2ed1ccd06 100644--- a/packages/twenty-front/src/pages/settings/ai/SettingsToolDetail.tsx+++ b/packages/twenty-front/src/pages/settings/ai/SettingsToolDetail.tsx@@ -1,3 +1,4 @@+import { useGetToolIndex } from '@/ai/hooks/useGetToolIndex';import { SkeletonLine } from '@/ui/feedback/skeleton/components/SkeletonLine';import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';import { useGetOneLogicFunction } from '@/logic-functions/hooks/useGetOneLogicFunction';@@ -21,10 +22,7 @@ import { Section } from 'twenty-ui/components/layout';import { IconTrash } from 'twenty-ui/icon';import { Button } from 'twenty-ui/primitives/input';import { useDebouncedCallback } from 'use-debounce';-import {- GetToolIndexDocument,- GetToolInputSchemaDocument,-} from '~/generated-metadata/graphql';+import { GetToolInputSchemaDocument } from '~/generated-metadata/graphql';import { SettingsToolIcon } from '~/pages/settings/ai/components/SettingsToolIcon';import { SettingsToolParameterTable } from '@/settings/ai/components/SettingsToolParameterTable';@@ -52,14 +50,11 @@ export const SettingsToolDetail = () => {skip: !isCustomTool,});- const { data: toolIndexData, loading: toolIndexLoading } = useQuery(- GetToolIndexDocument,- { skip: isCustomTool },- );+ const { toolIndex, loading: toolIndexLoading } = useGetToolIndex({+ skip: isCustomTool,+ });- const systemTool = toolIndexData?.getToolIndex.find(- (entry) => entry.name === toolIdentifier,- );+ const systemTool = toolIndex.find((entry) => entry.name === toolIdentifier);const { data: schemaData, loading: schemaLoading } = useQuery(GetToolInputSchemaDocument,diff --git a/packages/twenty-front/src/pages/settings/profile/appearance/components/LocalePicker.tsx b/packages/twenty-front/src/pages/settings/profile/appearance/components/LocalePicker.tsxindex 8cbf64adc40..4b7a1ac61c2 100644--- a/packages/twenty-front/src/pages/settings/profile/appearance/components/LocalePicker.tsx+++ b/packages/twenty-front/src/pages/settings/profile/appearance/components/LocalePicker.tsx@@ -6,6 +6,7 @@ import { getDateFnsLocale } from '@/ui/field/display/utils/getDateFnsLocale';import { Select } from '@/ui/input/components/Select';import { useUpdateWorkspaceMemberSettings } from '@/workspace-member/hooks/useUpdateWorkspaceMemberSettings';+import { useInvalidateToolIndex } from '@/ai/hooks/useInvalidateToolIndex';import { useInvalidateMetadataStore } from '@/metadata-store/hooks/useInvalidateMetadataStore';import { useStore } from 'jotai';import { useLingui } from '@lingui/react/macro';@@ -34,6 +35,7 @@ export const LocalePicker = () => {const localeOptions = useLocaleOptions();const { invalidateMetadataStore } = useInvalidateMetadataStore();+ const { invalidateToolIndex } = useInvalidateToolIndex();const updateWorkspaceMember = async (changedFields: any) => {if (!currentWorkspaceMember?.id) {@@ -74,6 +76,8 @@ export const LocalePicker = () => {console.log('Failed to save locale to localStorage:', error);}invalidateMetadataStore();+ // Tool labels are translated server-side in the member's locale+ invalidateToolIndex();};return (