Skip to content

The open alternative to Salesforce, designed for AI.

feat(ai): collapse long user messages in the chat thread (#29191)

Etienneca92611e9cef
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatMessage.tsx b/packages/twenty-front/src/modules/ai/components/AiChatMessage.tsx
index f4596e23458..a664ce38b0c 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatMessage.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatMessage.tsx
@@ -14,6 +14,7 @@ import { AiChatMessageHeader } from '@/ai/components/AiChatMessageHeader';
import { AiChatMessageReactionPicker } from '@/ai/components/AiChatMessageReactionPicker';
import { AiChatMessageReactions } from '@/ai/components/AiChatMessageReactions';
import { AiChatErrorRenderer } from '@/ai/components/AiChatErrorRenderer';
+import { AiChatUserMessageCollapsibleContent } from '@/ai/components/AiChatUserMessageCollapsibleContent';
import { agentChatFirstUnreadMessageIdSelector } from '@/ai/states/selectors/agentChatFirstUnreadMessageIdSelector';
import { agentChatIsMessageBeforeFirstUserMessageFamilySelector } from '@/ai/states/selectors/agentChatIsMessageBeforeFirstUserMessageFamilySelector';
import { agentChatMessageFamilySelector } from '@/ai/states/selectors/agentChatMessageFamilySelector';
@@ -189,12 +190,14 @@ const StyledFilesContainer = styled.div`
type AiChatUserMessageTextProps = {
messageId: string;
+ text: string;
children: ReactNode;
isOtherMember: boolean;
};
const AiChatUserMessageText = ({
messageId,
+ text,
children,
isOtherMember,
}: AiChatUserMessageTextProps) => {
@@ -206,7 +209,9 @@ const AiChatUserMessageText = ({
isOtherMember={isOtherMember}
ref={sentMessageHandOffRef}
>
- {children}
+ <AiChatUserMessageCollapsibleContent text={text}>
+ {children}
+ </AiChatUserMessageCollapsibleContent>
</StyledMessageText>
);
};
@@ -276,6 +281,8 @@ export const AiChatMessage = ({
isDefined(error) && agentChatMessage.role === AGENT_MESSAGE_ROLE.ASSISTANT;
const fileParts = agentChatMessage.parts.filter(isExtendedFileUIPart);
+ const messageText =
+ agentChatMessage.parts.find((part) => part.type === 'text')?.text ?? '';
const messageContent = (
<AiChatAssistantMessageRenderer
isUserMessage={isUser}
@@ -329,6 +336,7 @@ export const AiChatMessage = ({
{isUser ? (
<AiChatUserMessageText
messageId={messageId}
+ text={messageText}
isOtherMember={isAiChatInboxEnabled && !isOwnMessage}
>
{messageContent}
@@ -365,12 +373,7 @@ export const AiChatMessage = ({
{isAiChatInboxEnabled && !isLastMessageStreaming && (
<AiChatMessageReactionPicker messageId={messageId} />
)}
- <LightCopyIconButton
- copyText={
- agentChatMessage.parts.find((part) => part.type === 'text')
- ?.text ?? ''
- }
- />
+ <LightCopyIconButton copyText={messageText} />
</StyledMessageActions>
</StyledMessageFooter>
)}
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatUserMessageCollapsibleContent.tsx b/packages/twenty-front/src/modules/ai/components/AiChatUserMessageCollapsibleContent.tsx
new file mode 100644
index 00000000000..fb62eb0ed92
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/AiChatUserMessageCollapsibleContent.tsx
@@ -0,0 +1,65 @@
+import { useLingui } from '@lingui/react/macro';
+import { styled } from '@linaria/react';
+import { type FocusEvent, type ReactNode, useState } from 'react';
+import { LightButton } from 'twenty-ui/components/input';
+import { themeCssVariables } from 'twenty-ui/theme';
+
+import { AI_CHAT_USER_MESSAGE_COLLAPSED_LINE_COUNT } from '@/ai/constants/AiChatUserMessageCollapsedLineCount';
+import { isAiChatUserMessageLong } from '@/ai/utils/isAiChatUserMessageLong';
+
+const StyledContent = styled.div<{ isCollapsed: boolean }>`
+ mask-image: ${({ isCollapsed }) =>
+ isCollapsed
+ ? 'linear-gradient(to bottom, black calc(100% - 2lh), transparent)'
+ : 'none'};
+ max-height: ${({ isCollapsed }) =>
+ isCollapsed ? `${AI_CHAT_USER_MESSAGE_COLLAPSED_LINE_COUNT}lh` : 'none'};
+ overflow: hidden;
+`;
+
+const StyledToggleContainer = styled.div`
+ display: flex;
+ justify-content: flex-end;
+ margin-inline: calc(-1 * ${themeCssVariables.spacing[1]});
+ padding-bottom: ${themeCssVariables.spacing[1]};
+`;
+
+type AiChatUserMessageCollapsibleContentProps = {
+ text: string;
+ children: ReactNode;
+};
+
+export const AiChatUserMessageCollapsibleContent = ({
+ text,
+ children,
+}: AiChatUserMessageCollapsibleContentProps) => {
+ const { t } = useLingui();
+ const [isExpanded, setIsExpanded] = useState(false);
+
+ if (!isAiChatUserMessageLong(text)) {
+ return children;
+ }
+
+ const handleToggleClick = () => {
+ setIsExpanded((previousIsExpanded) => !previousIsExpanded);
+ };
+
+ const handleContentFocus = (event: FocusEvent<HTMLDivElement>) => {
+ if (event.target.matches(':focus-visible')) {
+ setIsExpanded(true);
+ }
+ };
+
+ return (
+ <>
+ <StyledContent isCollapsed={!isExpanded} onFocus={handleContentFocus}>
+ {children}
+ </StyledContent>
+ <StyledToggleContainer>
+ <LightButton aria-expanded={isExpanded} onClick={handleToggleClick}>
+ {isExpanded ? t`Show less` : t`Show more`}
+ </LightButton>
+ </StyledToggleContainer>
+ </>
+ );
+};
diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/AiChatMessage.stories.tsx b/packages/twenty-front/src/modules/ai/components/__stories__/AiChatMessage.stories.tsx
index b35544c4732..bc8c4644210 100644
--- a/packages/twenty-front/src/modules/ai/components/__stories__/AiChatMessage.stories.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__stories__/AiChatMessage.stories.tsx
@@ -323,8 +323,38 @@ const mockNamedAssistantMessage: ExtendedUIMessage = {
parts: [{ type: 'text', text: 'The recorded sender remains visible.' }],
};
+const mockLongUserMessage: ExtendedUIMessage = {
+ ...mockUserMessage,
+ id: 'msg-user-long',
+ parts: [
+ {
+ type: 'text',
+ text: `Help me set up my workspace for our sales team.
+
+1. Create a Deal Stage field on Opportunities with the values Lead, Qualified, Proposal, Negotiation, Won and Lost.
+2. Import the companies from the spreadsheet I attached and link each one to its account owner.
+3. Build a Pipeline view grouped by Deal Stage, sorted by close date, showing amount and owner.
+4. Add a dashboard with won revenue per month and the number of open deals per owner.
+5. Create a workflow that assigns a follow-up task to the owner when a deal has had no activity for seven days.
+6. Draft a short welcome email I can send to the team explaining the new pipeline.
+7. Add a Lost Reason select field on Opportunities and make it required when a deal moves to Lost.
+8. Create a weekly view of deals closing this month for the Monday sales meeting.
+9. Tag the companies that have no open opportunity so we can plan outreach.
+10. Set up a reminder task for each owner to update their deal amounts every Friday.
+11. Merge duplicate companies that share the same domain and keep the most recent owner.
+12. Create a Renewal Date field on Companies and a view of renewals coming up in the next 90 days.
+13. Build a workflow that notifies the account owner when a renewal is 30 days away and no opportunity exists yet.
+14. Add a dashboard chart with the conversion rate from Qualified to Won per month for the last two quarters.
+15. Import last year's closed deals from the second sheet so the dashboard has history from day one.
+
+Before changing anything, list what you plan to do and ask me to confirm. Keep field names in English and reuse existing fields when they already match. Follow the [pipeline guide](https://twenty.com/user-guide) for naming.`,
+ },
+ ],
+};
+
const allMockMessages = [
mockUserMessage,
+ mockLongUserMessage,
mockAssistantWithCodeExecution,
mockSimpleTextResponse,
mockStreamingMessage,
@@ -451,6 +481,41 @@ export const UserMessage: Story = {
render: () => <AiChatMessage messageId={mockUserMessage.id} />,
};
+export const LongUserMessage: Story = {
+ render: () => (
+ <StyledConversationContainer>
+ <AiChatMessage messageId={mockLongUserMessage.id} />
+ <AiChatMessage messageId={mockUserMessage.id} />
+ </StyledConversationContainer>
+ ),
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+ const showMoreButton = await canvas.findByRole('button', {
+ name: 'Show more',
+ });
+
+ await expect(
+ canvas.getAllByRole('button', { name: 'Show more' }),
+ ).toHaveLength(1);
+ await expect(showMoreButton).toHaveAttribute('aria-expanded', 'false');
+
+ await userEvent.tab();
+
+ await expect(
+ canvas.getByRole('link', { name: 'pipeline guide' }),
+ ).toHaveFocus();
+ await expect(
+ await canvas.findByRole('button', { name: 'Show less' }),
+ ).toHaveAttribute('aria-expanded', 'true');
+
+ await userEvent.click(canvas.getByRole('button', { name: 'Show less' }));
+
+ await expect(
+ await canvas.findByRole('button', { name: 'Show more' }),
+ ).toHaveAttribute('aria-expanded', 'false');
+ },
+};
+
export const AssistantTextResponse: Story = {
render: () => <AiChatMessage messageId={mockSimpleTextResponse.id} />,
};
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatMessage.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatMessage.test.tsx
index 851e943f021..773b0942e26 100644
--- a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatMessage.test.tsx
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatMessage.test.tsx
@@ -96,6 +96,20 @@ const SILENT_MESSAGE: ExtendedUIMessage = {
metadata: { createdAt: '2026-10-15T08:00:10.000Z' },
};
+const LONG_TEXT = 'Set up my workspace. '.repeat(80);
+
+const LONG_USER_MESSAGE: ExtendedUIMessage = {
+ ...USER_MESSAGE,
+ id: 'long-user-message',
+ parts: [{ type: 'text', text: LONG_TEXT }],
+};
+
+const LONG_ASSISTANT_MESSAGE: ExtendedUIMessage = {
+ ...ASSISTANT_MESSAGE,
+ id: 'long-assistant-message',
+ parts: [{ type: 'text', text: LONG_TEXT }],
+};
+
const MESSAGE_WITHOUT_SENDER: ExtendedUIMessage = {
id: 'message-without-sender',
role: 'user',
@@ -149,6 +163,8 @@ const renderMessage = ({
NAMED_ASSISTANT_MESSAGE,
SILENT_MESSAGE,
MESSAGE_WITHOUT_SENDER,
+ LONG_USER_MESSAGE,
+ LONG_ASSISTANT_MESSAGE,
],
);
@@ -259,6 +275,27 @@ describe('AiChatMessage', () => {
expect(screen.getByText(/ago$/)).toBeInTheDocument();
});
+ it('collapses a long member message but never an assistant reply', () => {
+ const { unmount } = renderMessage({
+ messageId: LONG_USER_MESSAGE.id,
+ isAiChatInboxEnabled: false,
+ });
+
+ expect(
+ screen.getByRole('button', { name: 'Show more' }),
+ ).toBeInTheDocument();
+
+ unmount();
+ renderMessage({
+ messageId: LONG_ASSISTANT_MESSAGE.id,
+ isAiChatInboxEnabled: false,
+ });
+
+ expect(
+ screen.queryByRole('button', { name: 'Show more' }),
+ ).not.toBeInTheDocument();
+ });
+
it('hides a reply Ada chose not to send', () => {
const { container } = renderMessage({
messageId: SILENT_MESSAGE.id,
diff --git a/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageCollapsedLineCount.ts b/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageCollapsedLineCount.ts
new file mode 100644
index 00000000000..96089981975
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageCollapsedLineCount.ts
@@ -0,0 +1 @@
+export const AI_CHAT_USER_MESSAGE_COLLAPSED_LINE_COUNT = 10;
diff --git a/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageLongTextMinLength.ts b/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageLongTextMinLength.ts
new file mode 100644
index 00000000000..7426a21cdd0
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/constants/AiChatUserMessageLongTextMinLength.ts
@@ -0,0 +1 @@
+export const AI_CHAT_USER_MESSAGE_LONG_TEXT_MIN_LENGTH = 1500;
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/isAiChatUserMessageLong.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/isAiChatUserMessageLong.test.ts
new file mode 100644
index 00000000000..4fec541c555
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/isAiChatUserMessageLong.test.ts
@@ -0,0 +1,21 @@
+import { isAiChatUserMessageLong } from '@/ai/utils/isAiChatUserMessageLong';
+
+describe('isAiChatUserMessageLong', () => {
+ it('keeps a short message open', () => {
+ expect(isAiChatUserMessageLong('Which deals close this week?')).toBe(false);
+ });
+
+ it('keeps short lines open since markdown joins them into one paragraph', () => {
+ const shortLines = Array.from(
+ { length: 20 },
+ (_, index) => `Step ${index + 1}`,
+ ).join('\n');
+
+ expect(isAiChatUserMessageLong(shortLines)).toBe(false);
+ });
+
+ it('flags a message of 1500 characters or more', () => {
+ expect(isAiChatUserMessageLong('a'.repeat(1499))).toBe(false);
+ expect(isAiChatUserMessageLong('a'.repeat(1500))).toBe(true);
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/isAiChatUserMessageLong.ts b/packages/twenty-front/src/modules/ai/utils/isAiChatUserMessageLong.ts
new file mode 100644
index 00000000000..938b8d8f459
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/isAiChatUserMessageLong.ts
@@ -0,0 +1,4 @@
+import { AI_CHAT_USER_MESSAGE_LONG_TEXT_MIN_LENGTH } from '@/ai/constants/AiChatUserMessageLongTextMinLength';
+
+export const isAiChatUserMessageLong = (text: string) =>
+ text.length >= AI_CHAT_USER_MESSAGE_LONG_TEXT_MIN_LENGTH;