The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Render chips and markdown in AI reasoning and approval summaries (#27231)
Félix Malfaitdca2ad29dbd4
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatAskStatusRow.tsx b/packages/twenty-front/src/modules/ai/components/AiChatAskStatusRow.tsxindex 38f3e2113cf..92a4c39ef84 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatAskStatusRow.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatAskStatusRow.tsx@@ -28,7 +28,7 @@ const StyledContent = styled.div`type AiChatAskStatusRowProps = {Icon: IconComponent;- message: string;+ message: ReactNode;isShimmering: boolean;children?: ReactNode;};diff --git a/packages/twenty-front/src/modules/ai/components/AiChatQuestionStatusRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatQuestionStatusRenderer.tsxindex 225232a4caa..98c50ffa57d 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatQuestionStatusRenderer.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatQuestionStatusRenderer.tsx@@ -1,6 +1,6 @@import { styled } from '@linaria/react';import { useLingui } from '@lingui/react/macro';-import { isNonEmptyString } from '@sniptt/guards';+import { isNonEmptyString, isString } from '@sniptt/guards';import { type DynamicToolUIPart, type ToolUIPart } from 'ai';import {type AskQuestionToolResult,@@ -10,7 +10,9 @@ import { IconHelpCircle } from 'twenty-ui/icon';import { themeCssVariables } from 'twenty-ui/theme';import { AiChatAskStatusRow } from '@/ai/components/AiChatAskStatusRow';+import { TextWithChatReferences } from '@/ai/components/TextWithChatReferences';import { getAskedQuestionEntries } from '@/ai/utils/getAskedQuestionEntries';+import { getChatReferenceSegments } from '@/ai/utils/getChatReferenceSegments';import {StyledAiChatAskStatusDetail,StyledAiChatAskStatusMessage,@@ -58,13 +60,32 @@ export const AiChatQuestionStatusRenderer = ({const entries = getAskedQuestionEntries(result);if (status === 'pending') {+ if (entries.length !== 1) {+ return (+ <AiChatAskStatusRow+ Icon={IconHelpCircle}+ message={t`Asking questions...`}+ isShimmering={isStreaming}+ />+ );+ }++ const pendingQuestion = entries[0].question.question;++ // the shimmer clips text to a transparent fill, which would hide chip labelsreturn (<AiChatAskStatusRowIcon={IconHelpCircle}message={- entries.length === 1- ? entries[0].question.question- : t`Asking questions...`+ isStreaming ? (+ getChatReferenceSegments(pendingQuestion)+ .map((segment) =>+ isString(segment) ? segment : segment.displayName,+ )+ .join('')+ ) : (+ <TextWithChatReferences text={pendingQuestion} />+ )}isShimmering={isStreaming}/>@@ -102,8 +123,12 @@ export const AiChatQuestionStatusRenderer = ({return (<StyledAnswerBlock key={index}>- <StyledAnswerQuestion>{question.question}</StyledAnswerQuestion>- <StyledAiChatAskStatusDetail>{value}</StyledAiChatAskStatusDetail>+ <StyledAnswerQuestion>+ <TextWithChatReferences text={question.question} />+ </StyledAnswerQuestion>+ <StyledAiChatAskStatusDetail>+ <TextWithChatReferences text={value} />+ </StyledAiChatAskStatusDetail></StyledAnswerBlock>);})}diff --git a/packages/twenty-front/src/modules/ai/components/AiChatToolCallApprovalStatusRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatToolCallApprovalStatusRenderer.tsxindex 6642482690f..8ffbf680f31 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatToolCallApprovalStatusRenderer.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatToolCallApprovalStatusRenderer.tsx@@ -10,6 +10,7 @@ import { IconMail, IconTool } from 'twenty-ui/icon';import { AiChatAskStatusRow } from '@/ai/components/AiChatAskStatusRow';import { StyledAiChatAskStatusDetail } from '@/ai/components/AiChatAskStyledComponents';+import { TextWithChatReferences } from '@/ai/components/TextWithChatReferences';export const AiChatToolCallApprovalStatusRenderer = ({toolPart,@@ -71,7 +72,9 @@ export const AiChatToolCallApprovalStatusRenderer = ({}>{isNonEmptyString(detail) && (- <StyledAiChatAskStatusDetail>{detail}</StyledAiChatAskStatusDetail>+ <StyledAiChatAskStatusDetail>+ <TextWithChatReferences text={detail} />+ </StyledAiChatAskStatusDetail>)}</AiChatAskStatusRow>);diff --git a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsxindex 80f2f5f4198..382bf71e27c 100644--- a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx+++ b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx@@ -5,6 +5,8 @@ import { IconChevronRight, IconCpu } from 'twenty-ui/icon';import { themeCssVariables } from 'twenty-ui/theme';import { AiChatThinkingRow } from '@/ai/components/AiChatThinkingRow';+import { LazyMarkdownRenderer } from '@/ai/components/LazyMarkdownRenderer';+import { StyledParagraph } from '@/ai/components/LazyMarkdownRendererStyledComponents';import { ThinkingToolStepRow } from '@/ai/components/ThinkingToolStepRow';import { getActiveReasoningContent } from '@/ai/utils/getActiveReasoningContent';import { getLastReasoningContent } from '@/ai/utils/getLastReasoningContent';@@ -98,13 +100,16 @@ const StyledReasoningContainer = styled.div`);`;-const StyledReasoningText = styled.p`+const StyledReasoningText = styled.div`color: ${themeCssVariables.font.color.tertiary};font-size: ${themeCssVariables.font.size.md};font-weight: ${themeCssVariables.font.weight.regular};line-height: ${themeCssVariables.text.lineHeight.lg};- margin: 0;- white-space: pre-wrap;++ // reasoning uses single newlines as line breaks, which markdown would collapse+ ${StyledParagraph} {+ white-space: pre-line;+ }`;const StyledIconContainer = styled.div`@@ -220,7 +225,9 @@ export const ThinkingStepsDisplay = ({</StyledRowsContainer>{!!shouldDisplayReasoningContent && (<StyledReasoningContainer>- <StyledReasoningText>{reasoningContent}</StyledReasoningText>+ <StyledReasoningText>+ <LazyMarkdownRenderer text={reasoningContent} />+ </StyledReasoningText></StyledReasoningContainer>)}</StyledStepsContentContainer>diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolCallApprovalStatusRenderer.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolCallApprovalStatusRenderer.test.tsxindex 52b96172203..01eb8a6ae2f 100644--- a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolCallApprovalStatusRenderer.test.tsx+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolCallApprovalStatusRenderer.test.tsx@@ -4,6 +4,13 @@ import { render, screen } from '@testing-library/react';import { type ToolUIPart } from 'ai';import { AiChatToolCallApprovalStatusRenderer } from '@/ai/components/AiChatToolCallApprovalStatusRenderer';+import { type ChatReferenceMatch } from '@/ai/types/ChatReferenceMatch';++jest.mock('@/ai/components/ChatReferenceChip', () => ({+ ChatReferenceChip: ({ reference }: { reference: ChatReferenceMatch }) => (+ <span data-testid="chat-reference-chip">{reference.displayName}</span>+ ),+}));const renderStatus = (toolPart: Partial<ToolUIPart>) =>render(@@ -71,4 +78,30 @@ describe('AiChatToolCallApprovalStatusRenderer', () => {expect(screen.getByText('Approved')).toBeInTheDocument();expect(screen.getByText('Follow up')).toBeInTheDocument();});++ it('renders record references in the summary as chips', () => {+ const summary =+ 'Rename [[record:task:3aa53376-dbbc-4fb1-8cd6-5f7d8e916496:dede]]';++ renderStatus({+ state: 'output-available',+ input: { toolName: 'update_one_task', arguments: {}, summary },+ output: {+ success: true,+ result: {+ status: 'approved',+ proposal: {+ toolName: 'update_one_task',+ toolLabel: 'Update Task',+ summary,+ arguments: {},+ template: 'recordUpdate',+ },+ },+ },+ });++ expect(screen.getByTestId('chat-reference-chip')).toHaveTextContent('dede');+ expect(screen.queryByText(/\[\[record:/)).not.toBeInTheDocument();+ });});diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/ThinkingStepsDisplay.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/ThinkingStepsDisplay.test.tsxindex 25aed281575..3a3019c02cb 100644--- a/packages/twenty-front/src/modules/ai/components/__tests__/ThinkingStepsDisplay.test.tsx+++ b/packages/twenty-front/src/modules/ai/components/__tests__/ThinkingStepsDisplay.test.tsx@@ -48,6 +48,12 @@ jest.mock('@/ui/layout/tab-list/components/TabList', () => ({),}));+jest.mock('@/ai/components/LazyMarkdownRenderer', () => ({+ LazyMarkdownRenderer: ({ text }: { text: string }) => (+ <div data-testid="markdown-renderer">{text}</div>+ ),+}));+jest.mock('@/ai/components/ToolRecordsWidget', () => ({ToolRecordsWidget: ({recordReferences,@@ -134,7 +140,9 @@ describe('ThinkingStepsDisplay', () => {expect(screen.queryByRole('button', { name: /steps/i })).toBeNull();expect(screen.getByText('Thinking')).toBeInTheDocument();- expect(screen.getByText('Active reasoning content')).toBeInTheDocument();+ expect(screen.getByTestId('markdown-renderer')).toHaveTextContent(+ 'Active reasoning content',+ );expect(screen.getByText('Searched the web for crm software'),).toBeInTheDocument();diff --git a/packages/twenty-front/src/modules/ai/components/internal/AiChatToolCallApprovalCardLayout.tsx b/packages/twenty-front/src/modules/ai/components/internal/AiChatToolCallApprovalCardLayout.tsxindex bb6acfe31d8..c7ab1d34f8c 100644--- a/packages/twenty-front/src/modules/ai/components/internal/AiChatToolCallApprovalCardLayout.tsx+++ b/packages/twenty-front/src/modules/ai/components/internal/AiChatToolCallApprovalCardLayout.tsx@@ -5,6 +5,7 @@ import { LightButton } from 'twenty-ui/components';import { themeCssVariables } from 'twenty-ui/theme';import { StyledAiChatAskCard } from '@/ai/components/AiChatAskStyledComponents';+import { TextWithChatReferences } from '@/ai/components/TextWithChatReferences';import { AiChatToolCallApprovalFeedbackInput } from '@/ai/components/internal/AiChatToolCallApprovalFeedbackInput';import { useAiChatAskCardFieldFocus } from '@/ai/hooks/useAiChatAskCardFieldFocus';import { type useAnswerToolCallApproval } from '@/ai/hooks/useAnswerToolCallApproval';@@ -71,7 +72,9 @@ export const AiChatToolCallApprovalCardLayout = ({<StyledAiChatAskCard><StyledHeader><StyledLabel>{label}</StyledLabel>- <StyledSummary>{summary}</StyledSummary>+ <StyledSummary>+ <TextWithChatReferences text={summary} />+ </StyledSummary></StyledHeader><StyledBody onFocus={handleFieldFocus} onBlur={handleFieldBlur}>{children}