The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Let members send inbox messages when no AI provider is configured (#28136)
Félix Malfait221bfbfacfb1
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatEditorSection.tsx b/packages/twenty-front/src/modules/ai/components/AiChatEditorSection.tsxindex 85d254fa3ea..1fc6e63e6dd 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatEditorSection.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatEditorSection.tsx@@ -32,6 +32,7 @@ import { agentChatHasMessageSelector } from '@/ai/states/selectors/agentChatHasMimport { aiModelsState } from '@/client-config/states/aiModelsState';import { useIsMobile } from 'twenty-ui/utilities';import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';+import { useCanSendAgentChatMessage } from '@/ai/hooks/useCanSendAgentChatMessage';const StyledInputArea = styled(StyledAiChatContentContainer)<{isMobile: boolean;@@ -141,6 +142,7 @@ const EditableAiChatEditorSection = () => {!hasReachedAiChatCreditsCap && hasReachedAiChatUsageLimit;const aiModels = useAtomStateValue(aiModelsState);const hasNoEnabledModels = aiModels.length === 0;+ const canSendAgentChatMessage = useCanSendAgentChatMessage();const { editor } = useAiChatEditor();@@ -161,7 +163,7 @@ const EditableAiChatEditorSection = () => {</>}modelTierDropdownId="ai-chat-model-tier-dropdown"- sendButton={<SendMessageButton isDisabled={hasNoEnabledModels} />}+ sendButton={<SendMessageButton isDisabled={!canSendAgentChatMessage} />}/></StyledInputBox>);diff --git a/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChat.test.tsx b/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChat.test.tsxindex 6aab2f2159c..47c231939b2 100644--- a/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChat.test.tsx+++ b/packages/twenty-front/src/modules/ai/hooks/__tests__/useAgentChat.test.tsx@@ -12,14 +12,20 @@ import { agentChatDraftsByThreadIdState } from '@/ai/states/agentChatDraftsByThrimport { agentChatSelectedFilesState } from '@/ai/states/agentChatSelectedFilesState';import { currentAiChatThreadState } from '@/ai/states/currentAiChatThreadState';import { newAiChatThreadIdState } from '@/ai/states/newAiChatThreadIdState';+import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';import { aiModelsState } from '@/client-config/states/aiModelsState';import { serializeMentionTagAsAdvancedTextEditorDocument } from '@/mention/utils/serializeMentionTagAsAdvancedTextEditorDocument';import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';import { jotaiStore } from '@/ui/utilities/state/jotai/jotaiStore';-import { SendChatMessageDocument } from '~/generated-metadata/graphql';+import {+ FeatureFlagKey,+ SendChatMessageDocument,+} from '~/generated-metadata/graphql';import { getJestMetadataAndApolloMocksWrapper } from '~/testing/jest/getJestMetadataAndApolloMocksWrapper';+import { mockCurrentWorkspace } from '~/testing/mock-data/users';const attachChatThreadToRecord = jest.fn();+const sentShouldAssistantReply = jest.fn();jest.mock('@/ai/hooks/useAttachChatThreadToRecord', () => ({useAttachChatThreadToRecord: () => ({ attachChatThreadToRecord }),@@ -46,7 +52,14 @@ const DRAFT_MENTIONING_COMPANY =const buildSendChatMessageMock = (outcome: 'sent' | 'failed',): MockedResponse => ({- request: { query: SendChatMessageDocument, variables: () => true },+ request: {+ query: SendChatMessageDocument,+ variables: (variables) => {+ sentShouldAssistantReply(variables.shouldAssistantReply);++ return true;+ },+ },...(outcome === 'sent'? {result: {@@ -68,15 +81,31 @@ const readPersistedDrafts = () =>const renderAgentChat = ({persistedDrafts,sendChatMessageOutcomes,+ hasAiModels = true,+ isAiChatInboxEnabled = false,}: {persistedDrafts: Record<string, string>;sendChatMessageOutcomes: ('sent' | 'failed')[];+ hasAiModels?: boolean;+ isAiChatInboxEnabled?: boolean;}) => {const MetadataAndApolloMocksWrapper = getJestMetadataAndApolloMocksWrapper({apolloMocks: sendChatMessageOutcomes.map(buildSendChatMessageMock),onInitializeJotaiStore: (store) => {- store.set(aiModelsState.atom, [{ modelId: 'model', label: 'Model' }]);+ store.set(+ aiModelsState.atom,+ hasAiModels ? [{ modelId: 'model', label: 'Model' }] : [],+ );store.set(newAiChatThreadIdState.atom, THREAD_ID);+ store.set(currentWorkspaceState.atom, {+ ...mockCurrentWorkspace,+ featureFlags: [+ {+ key: FeatureFlagKey.IS_AI_CHAT_INBOX_ENABLED,+ value: isAiChatInboxEnabled,+ },+ ],+ });},});@@ -237,4 +266,35 @@ describe('useAgentChat', () => {expect(attachChatThreadToRecord).not.toHaveBeenCalled();});++ it('sends without asking for a reply in the inbox when no AI is configured', async () => {+ const result = renderAgentChat({+ persistedDrafts: {+ [THREAD_ID]: serializePlainTextAsAdvancedTextEditorDocument('Hello'),+ },+ sendChatMessageOutcomes: ['sent'],+ hasAiModels: false,+ isAiChatInboxEnabled: true,+ });++ await send(result);++ expect(sentShouldAssistantReply).toHaveBeenCalledTimes(1);+ expect(sentShouldAssistantReply).toHaveBeenCalledWith(false);+ expect(readPersistedDrafts()).toEqual({ [THREAD_ID]: '' });+ });++ it('does not send without AI outside the inbox', async () => {+ const result = renderAgentChat({+ persistedDrafts: {+ [THREAD_ID]: serializePlainTextAsAdvancedTextEditorDocument('Hello'),+ },+ sendChatMessageOutcomes: ['sent'],+ hasAiModels: false,+ });++ await send(result);++ expect(sentShouldAssistantReply).not.toHaveBeenCalled();+ });});diff --git a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.tsindex 9124fcdd4f5..83e6070248c 100644--- a/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts+++ b/packages/twenty-front/src/modules/ai/hooks/useAgentChat.ts@@ -15,6 +15,7 @@ import { AGENT_CHAT_SEND_MESSAGE_EVENT_NAME } from '@/ai/constants/AgentChatSendimport { AGENT_CHAT_STOP_EVENT_NAME } from '@/ai/constants/AgentChatStopEventName';import { useAgentChatModelId } from '@/ai/hooks/useAgentChatModelId';import { useAttachChatThreadToRecord } from '@/ai/hooks/useAttachChatThreadToRecord';+import { useCanSendAgentChatMessage } from '@/ai/hooks/useCanSendAgentChatMessage';import { useGetBrowsingContext } from '@/ai/hooks/useGetBrowsingContext';import { useOptimisticallyRestoreOnSend } from '@/ai/hooks/useOptimisticallyRestoreOnSend';import { useProjectAiChatThreadToUrl } from '@/ai/hooks/useProjectAiChatThreadToUrl';@@ -54,6 +55,7 @@ import {export const useAgentChat = () => {const { modelIdForRequest } = useAgentChatModelId();const aiModels = useAtomStateValue(aiModelsState);+ const canSendAgentChatMessage = useCanSendAgentChatMessage();const { getBrowsingContext } = useGetBrowsingContext();const { applyOptimisticRestore } = useOptimisticallyRestoreOnSend();const { attachChatThreadToRecord } = useAttachChatThreadToRecord();@@ -90,7 +92,7 @@ export const useAgentChat = () => {return;}- if (aiModels.length === 0) {+ if (!canSendAgentChatMessage) {enqueueToast({status: 'error',children: t`No AI provider is configured on this instance.`,@@ -214,10 +216,11 @@ export const useAgentChat = () => {): undefined,shouldAssistantReply:- !store.get(agentChatIsAssistantOffState.atom) ||- isAgentChatAssistantMentionedInSerializedDocument(- serializedContentToSend,- ),+ aiModels.length > 0 &&+ (!store.get(agentChatIsAssistantOffState.atom) ||+ isAgentChatAssistantMentionedInSerializedDocument(+ serializedContentToSend,+ )),},});@@ -297,6 +300,7 @@ export const useAgentChat = () => {setAgentChatDraftsByThreadId,modelIdForRequest,aiModels,+ canSendAgentChatMessage,enqueueToast,setCurrentAiChatThread,projectAiChatThreadToUrl,diff --git a/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts b/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.tsindex 40337aea63e..4a9e377057f 100644--- a/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts+++ b/packages/twenty-front/src/modules/ai/hooks/useAiChatEditor.ts@@ -14,6 +14,7 @@ import { currentAiChatThreadState } from '@/ai/states/currentAiChatThreadState';import { newAiChatThreadIdState } from '@/ai/states/newAiChatThreadIdState';import { AGENT_CHAT_SEND_MESSAGE_EVENT_NAME } from '@/ai/constants/AgentChatSendMessageEventName';import { dispatchBrowserEvent } from '@/browser-event/utils/dispatchBrowserEvent';+import { aiModelsState } from '@/client-config/states/aiModelsState';import { MENTION_SUGGESTION_PLUGIN_KEY } from '@/mention/constants/MentionSuggestionPluginKey';import { useMentionSearch } from '@/mention/hooks/useMentionSearch';import { getAgentChatAssistantMentionSearchResults } from '@/ai/utils/getAgentChatAssistantMentionSearchResults';@@ -32,6 +33,7 @@ import { turnIntoEmptyStringIfWhitespacesOnly } from '~/utils/string/turnIntoEmpexport const useAiChatEditor = () => {const currentAiChatThread = useAtomStateValue(currentAiChatThreadState);+ const aiModels = useAtomStateValue(aiModelsState);const newAiChatThreadId = useAtomStateValue(newAiChatThreadIdState);const [agentChatDraftsByThreadId, setAgentChatDraftsByThreadId] =useAtomState(agentChatDraftsByThreadIdState);@@ -136,11 +138,13 @@ export const useAiChatEditor = () => {mentionStorage.searchMentionRecords = searchMentionRecords;mentionStorage.searchWorkspaceMembers = isAiChatInboxEnabled? (query) => [- ...getAgentChatAssistantMentionSearchResults({- query,- objectLabelSingular: t`Assistant`,- objectLabelPlural: t`Teammates`,- }),+ ...(aiModels.length > 0+ ? getAgentChatAssistantMentionSearchResults({+ query,+ objectLabelSingular: t`Assistant`,+ objectLabelPlural: t`Teammates`,+ })+ : []),...searchWorkspaceMembers(query),]: () => [];diff --git a/packages/twenty-front/src/modules/ai/hooks/useCanSendAgentChatMessage.ts b/packages/twenty-front/src/modules/ai/hooks/useCanSendAgentChatMessage.tsnew file mode 100644index 00000000000..576b8ed0cab--- /dev/null+++ b/packages/twenty-front/src/modules/ai/hooks/useCanSendAgentChatMessage.ts@@ -0,0 +1,13 @@+import { aiModelsState } from '@/client-config/states/aiModelsState';+import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';+import { useIsFeatureEnabled } from '@/workspace/hooks/useIsFeatureEnabled';+import { FeatureFlagKey } from '~/generated-metadata/graphql';++export const useCanSendAgentChatMessage = () => {+ const aiModels = useAtomStateValue(aiModelsState);+ const isAiChatInboxEnabled = useIsFeatureEnabled(+ FeatureFlagKey.IS_AI_CHAT_INBOX_ENABLED,+ );++ return aiModels.length > 0 || isAiChatInboxEnabled;+};