Skip to content

The open alternative to Salesforce, designed for AI.

Structured agent chat messages follow up (#14797)

Abdul Rahman3e61f916359e
diff --git a/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx b/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
index bc8200f7695..60fb33c92f1 100644
--- a/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AIChatTab.tsx
@@ -58,11 +58,9 @@ const StyledButtonsContainer = styled.div`
export const AIChatTab = ({
agentId,
- isWorkflowAgentNodeChat,
uiMessages,
}: {
agentId: string;
- isWorkflowAgentNodeChat?: boolean;
uiMessages: UIMessageWithMetadata[];
}) => {
const [isDraggingFile, setIsDraggingFile] = useState(false);
@@ -122,28 +120,24 @@ export const AIChatTab = ({
maxRows={20}
/>
<StyledButtonsContainer>
- {!isWorkflowAgentNodeChat && (
- <>
- <Button
- variant="secondary"
- size="small"
- Icon={IconHistory}
- onClick={() =>
- navigateCommandMenu({
- page: CommandMenuPages.ViewPreviousAIChats,
- pageTitle: t`View Previous AI Chats`,
- pageIcon: IconHistory,
- })
- }
- />
- <Button
- variant="secondary"
- size="small"
- Icon={IconMessageCirclePlus}
- onClick={() => createAgentChatThread()}
- />
- </>
- )}
+ <Button
+ variant="secondary"
+ size="small"
+ Icon={IconHistory}
+ onClick={() =>
+ navigateCommandMenu({
+ page: CommandMenuPages.ViewPreviousAIChats,
+ pageTitle: t`View Previous AI Chats`,
+ pageIcon: IconHistory,
+ })
+ }
+ />
+ <Button
+ variant="secondary"
+ size="small"
+ Icon={IconMessageCirclePlus}
+ onClick={() => createAgentChatThread()}
+ />
<AgentChatFileUploadButton agentId={agentId} />
<SendMessageButton
agentId={agentId}
diff --git a/packages/twenty-front/src/modules/ai/components/LazyAIChatTab.tsx b/packages/twenty-front/src/modules/ai/components/LazyAIChatTab.tsx
new file mode 100644
index 00000000000..342fe2c8718
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/LazyAIChatTab.tsx
@@ -0,0 +1,23 @@
+import { AIChatSkeletonLoader } from '@/ai/components/internal/AIChatSkeletonLoader';
+import { type UIMessageWithMetadata } from '@/ai/types/UIMessageWithMetadata';
+import { lazy, Suspense } from 'react';
+
+const AIChatTab = lazy(() =>
+ import('./AIChatTab').then((module) => ({
+ default: module.AIChatTab,
+ })),
+);
+
+export const LazyAIChatTab = ({
+ agentId,
+ uiMessages,
+}: {
+ agentId: string;
+ uiMessages: UIMessageWithMetadata[];
+}) => {
+ return (
+ <Suspense fallback={<AIChatSkeletonLoader />}>
+ <AIChatTab agentId={agentId} uiMessages={uiMessages} />
+ </Suspense>
+ );
+};
diff --git a/packages/twenty-front/src/modules/command-menu/pages/ask-ai/components/CommandMenuAskAIPage.tsx b/packages/twenty-front/src/modules/command-menu/pages/ask-ai/components/CommandMenuAskAIPage.tsx
index f44becbbb83..07f5623d0b2 100644
--- a/packages/twenty-front/src/modules/command-menu/pages/ask-ai/components/CommandMenuAskAIPage.tsx
+++ b/packages/twenty-front/src/modules/command-menu/pages/ask-ai/components/CommandMenuAskAIPage.tsx
@@ -1,4 +1,5 @@
-import { AIChatTab } from '@/ai/components/AIChatTab';
+import { LazyAIChatTab } from '@/ai/components/LazyAIChatTab';
+import { AIChatSkeletonLoader } from '@/ai/components/internal/AIChatSkeletonLoader';
import { currentAIChatThreadComponentState } from '@/ai/states/currentAIChatThreadComponentState';
import { mapDBMessagesToUIMessages } from '@/ai/utils/mapDBMessagesToUIMessages';
import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
@@ -46,25 +47,27 @@ export const CommandMenuAskAIPage = () => {
});
const { loading, data } = useGetAgentChatMessagesQuery({
- variables: { threadId: currentThreadId as string },
+ variables: { threadId: currentThreadId ?? '' },
skip: !isDefined(currentThreadId),
});
const isLoading = loading || !currentThreadId || threadsLoading;
- if (!agentId || isLoading) {
+ if (isLoading) {
+ return <AIChatSkeletonLoader />;
+ }
+
+ if (!agentId) {
return (
<StyledContainer>
- <StyledEmptyState>
- {isLoading ? t`Loading...` : t`No AI Agent found.`}
- </StyledEmptyState>
+ <StyledEmptyState>{t`No AI Agent found.`}</StyledEmptyState>
</StyledContainer>
);
}
return (
<StyledContainer>
- <AIChatTab
+ <LazyAIChatTab
agentId={agentId}
key={currentThreadId}
uiMessages={mapDBMessagesToUIMessages(data?.agentChatMessages || [])}
diff --git a/packages/twenty-front/src/modules/object-record/record-calendar/components/RecordCalendarAddNew.tsx b/packages/twenty-front/src/modules/object-record/record-calendar/components/RecordCalendarAddNew.tsx
index 13854ee91a0..358dc7005c8 100644
--- a/packages/twenty-front/src/modules/object-record/record-calendar/components/RecordCalendarAddNew.tsx
+++ b/packages/twenty-front/src/modules/object-record/record-calendar/components/RecordCalendarAddNew.tsx
@@ -3,8 +3,8 @@ import { hasAnySoftDeleteFilterOnViewComponentSelector } from '@/object-record/r
import { recordIndexCalendarFieldMetadataIdState } from '@/object-record/record-index/states/recordIndexCalendarFieldMetadataIdState';
import { useCreateNewIndexRecord } from '@/object-record/record-table/hooks/useCreateNewIndexRecord';
import { useRecoilComponentValue } from '@/ui/utilities/state/component-state/hooks/useRecoilComponentValue';
-import styled from '@emotion/styled';
import { useTheme } from '@emotion/react';
+import styled from '@emotion/styled';
import { useRecoilValue } from 'recoil';
import { IconPlus } from 'twenty-ui/display';
import { Button } from 'twenty-ui/input';
diff --git a/packages/twenty-server/src/engine/metadata-modules/agent/agent-chat.service.ts b/packages/twenty-server/src/engine/metadata-modules/agent/agent-chat.service.ts
index 2db43a37339..276ebfde3a4 100644
--- a/packages/twenty-server/src/engine/metadata-modules/agent/agent-chat.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/agent/agent-chat.service.ts
@@ -92,10 +92,13 @@ export class AgentChatService {
await this.messagePartRepository.save(dbParts);
}
- this.generateTitleIfNeeded(
- threadId,
- uiMessage.parts.find((part) => part.type === 'text')?.text,
- );
+ const messageContent = uiMessage.parts.find(
+ (part) => part.type === 'text',
+ )?.text;
+
+ if (messageContent) {
+ this.generateTitleIfNeeded(threadId, messageContent);
+ }
return savedMessage;
}
@@ -118,13 +121,13 @@ export class AgentChatService {
return this.messageRepository.find({
where: { threadId },
order: { createdAt: 'ASC' },
- relations: ['parts', 'files'],
+ relations: ['parts'],
});
}
private async generateTitleIfNeeded(
threadId: string,
- messageContent?: string | null,
+ messageContent: string,
) {
const thread = await this.threadRepository.findOne({
where: { id: threadId },