The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(ai-chat): show streaming activity during and between steps (#23581)
Etienne8f9f2f390e7b
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsxindex 43f86141448..48ee249322c 100644--- a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx+++ b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx@@ -86,6 +86,8 @@ export const AiChatAssistantMessageRenderer = ({);const renderItems = groupContiguousThinkingStepParts(filteredParts);+ const lastRenderItemIndex = renderItems.length - 1;+if (!renderItems.length && !hasError) {return <AiChatInitialLoadingIndicator />;}@@ -107,6 +109,11 @@ export const AiChatAssistantMessageRenderer = ({nextRenderItem.part.type === 'text' &&nextRenderItem.part.text.trim().length > 0,)}+ isTrailingWhileStreaming={+ isLastMessageStreaming &&+ !hasError &&+ index === lastRenderItemIndex+ }/>) : (<MessagePartRendererdiff --git a/packages/twenty-front/src/modules/ai/components/AiChatThinkingRow.tsx b/packages/twenty-front/src/modules/ai/components/AiChatThinkingRow.tsxnew file mode 100644index 00000000000..a05483fc086--- /dev/null+++ b/packages/twenty-front/src/modules/ai/components/AiChatThinkingRow.tsx@@ -0,0 +1,36 @@+import { styled } from '@linaria/react';+import { t } from '@lingui/core/macro';+import { ThinkingOrbitLoaderIcon } from 'twenty-ui/icon';+import { themeCssVariables } from 'twenty-ui/theme-constants';++const StyledRow = styled.div`+ align-items: center;+ color: ${themeCssVariables.font.color.tertiary};+ display: flex;+ gap: ${themeCssVariables.spacing[2]};+ min-height: ${themeCssVariables.spacing[6]};+`;++const StyledLoaderIconContainer = styled.div`+ align-items: center;+ display: flex;+ justify-content: center;+ min-width: calc(${themeCssVariables.icon.size.sm} * 1px);+`;++const StyledLabel = styled.span`+ font-size: ${themeCssVariables.font.size.md};+ font-weight: ${themeCssVariables.font.weight.regular};+ line-height: ${themeCssVariables.text.lineHeight.md};+`;++export const AiChatThinkingRow = () => {+ return (+ <StyledRow>+ <StyledLoaderIconContainer>+ <ThinkingOrbitLoaderIcon />+ </StyledLoaderIconContainer>+ <StyledLabel>{t`Thinking`}</StyledLabel>+ </StyledRow>+ );+};diff --git a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsxindex 8712d2a5d64..61220ceb4a7 100644--- a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx+++ b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx@@ -3,17 +3,15 @@ import { plural, t } from '@lingui/core/macro';import { useState } from 'react';import { type DynamicToolUIPart, getToolName, type ToolUIPart } from 'ai';import { isDefined } from 'twenty-shared/utils';-import {- IconChevronRight,- IconCpu,- ThinkingOrbitLoaderIcon,-} from 'twenty-ui/icon';+import { IconChevronRight, IconCpu } from 'twenty-ui/icon';import { OverflowingTextWithTooltip, TooltipDelay } from 'twenty-ui/surfaces';import { JsonTree } from 'twenty-ui/json-visualizer';import { AnimatedExpandableContainer } from 'twenty-ui/layout';import { themeCssVariables } from 'twenty-ui/theme-constants';import { type JsonValue } from 'type-fest';+import { AiChatThinkingRow } from '@/ai/components/AiChatThinkingRow';+import { ShimmeringText } from '@/ai/components/ShimmeringText';import { useToolDisplayContext } from '@/ai/hooks/useToolDisplayContext';import { getToolIcon } from '@/ai/utils/getToolIcon';import { getToolDisplayMessage } from '@/ai/utils/tool-display/get-tool-display-message';@@ -137,11 +135,6 @@ const StyledReasoningText = styled.p`white-space: pre-wrap;`;-const StyledOrbitLoaderIconContainer = styled.span`- color: ${themeCssVariables.font.color.tertiary};- display: flex;-`;-const StyledIconContainer = styled.div`align-items: center;color: ${themeCssVariables.font.color.light};@@ -203,6 +196,12 @@ const StyledToolRowButton = styled.button<{ isExpandable: boolean }>`}`;+const StyledShimmeringLabel = styled(ShimmeringText)`+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+`;+const StyledToolDetailsContainer = styled.div`background: ${themeCssVariables.background.transparent.lighter};border: 1px solid ${themeCssVariables.border.color.light};@@ -317,10 +316,14 @@ const ThinkingToolStepRow = ({</StyledIconContainer><StyledRowLabelContainer><StyledToolRowLabel>- <OverflowingTextWithTooltip- text={displayMessage}- tooltipDelay={TooltipDelay.shortDelay}- />+ {isActive ? (+ <StyledShimmeringLabel>{displayMessage}</StyledShimmeringLabel>+ ) : (+ <OverflowingTextWithTooltip+ text={displayMessage}+ tooltipDelay={TooltipDelay.shortDelay}+ />+ )}</StyledToolRowLabel>{isExpandable && (<StyledChevronContainer isExpanded={isExpanded}>@@ -390,19 +393,17 @@ const ThinkingStepRow = ({);}+ if (isActive) {+ return <AiChatThinkingRow />;+ }+return (<StyledRow><StyledIconContainer>- {isActive ? (- <StyledOrbitLoaderIconContainer>- <ThinkingOrbitLoaderIcon />- </StyledOrbitLoaderIconContainer>- ) : (- <IconCpu size={14} />- )}+ <IconCpu size={14} /></StyledIconContainer><StyledRowLabelContainer>- <StyledRowLabel>{isActive ? t`Thinking` : t`Thought`}</StyledRowLabel>+ <StyledRowLabel>{t`Thought`}</StyledRowLabel></StyledRowLabelContainer></StyledRow>);@@ -412,30 +413,32 @@ export const ThinkingStepsDisplay = ({parts,isLastMessageStreaming,hasAssistantTextResponseStarted,+ isTrailingWhileStreaming = false,}: {parts: ThinkingStepPart[];isLastMessageStreaming: boolean;hasAssistantTextResponseStarted: boolean;+ isTrailingWhileStreaming?: boolean;}) => {const [isExpanded, setIsExpanded] = useState(false);const stepCount = parts.length;- const isThinking = parts.some((part) =>+ const hasActiveStep = parts.some((part) =>isThinkingStepPartActive(part, isLastMessageStreaming),);const activeReasoningContent = getActiveReasoningContent(parts);const finalReasoningContent = getLastReasoningContent(parts);- const reasoningContent = isThinking+ const reasoningContent = hasActiveStep? activeReasoningContent: finalReasoningContent;const shouldDisplayReasoningContent = reasoningContent?.trim().length;const shouldKeepExpandedBeforeAnswer = !hasAssistantTextResponseStarted;const shouldShowSummaryButton =- !isThinking && !shouldKeepExpandedBeforeAnswer;+ !hasActiveStep && !shouldKeepExpandedBeforeAnswer;const shouldRenderRows =- isThinking || isExpanded || shouldKeepExpandedBeforeAnswer;+ hasActiveStep || isExpanded || shouldKeepExpandedBeforeAnswer;return (<StyledContainer>@@ -471,6 +474,9 @@ export const ThinkingStepsDisplay = ({)}/>))}+ {isTrailingWhileStreaming && !hasActiveStep && (+ <AiChatThinkingRow />+ )}</StyledRowsContainer>{!!shouldDisplayReasoningContent && (<StyledReasoningContainer>diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatAssistantMessageRenderer.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatAssistantMessageRenderer.test.tsxindex a7f22ec15cf..6ac31eb56e1 100644--- a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatAssistantMessageRenderer.test.tsx+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatAssistantMessageRenderer.test.tsx@@ -8,12 +8,14 @@ jest.mock('@/ai/components/ThinkingStepsDisplay', () => ({ThinkingStepsDisplay: ({hasAssistantTextResponseStarted,parts,+ isTrailingWhileStreaming,}: {parts: unknown[];hasAssistantTextResponseStarted: boolean;+ isTrailingWhileStreaming?: boolean;}) => (<div data-testid="thinking-steps-display">- {`thinking-${parts.length}-${hasAssistantTextResponseStarted ? 'answer-started' : 'answer-pending'}`}+ {`thinking-${parts.length}-${hasAssistantTextResponseStarted ? 'answer-started' : 'answer-pending'}${isTrailingWhileStreaming ? '-trailing-while-streaming' : ''}`}</div>),}));@@ -40,12 +42,15 @@ jest.mock('@/ai/components/CodeExecutionDisplay', () => ({CodeExecutionDisplay: () => <div data-testid="code-execution-display" />,}));-const renderAssistantRenderer = (messageParts: ExtendedUIMessagePart[]) => {+const renderAssistantRenderer = (+ messageParts: ExtendedUIMessagePart[],+ { isLastMessageStreaming = false }: { isLastMessageStreaming?: boolean } = {},+) => {return render(<ThemeProvider colorScheme="light"><AiChatAssistantMessageRenderermessageParts={messageParts}- isLastMessageStreaming={false}+ isLastMessageStreaming={isLastMessageStreaming}/></ThemeProvider>,);@@ -234,6 +239,70 @@ describe('AiChatAssistantMessageRenderer', () => {expect(screen.getByTestId('code-execution-display')).toBeInTheDocument();});+ it('should flag the trailing thinking steps group while streaming', () => {+ const messageParts = [+ {+ type: 'reasoning',+ text: 'Reasoning content',+ state: 'done',+ },+ {+ type: 'tool-web_search',+ toolCallId: 'tool-1',+ input: { query: 'crm software' },+ output: { result: { ok: true } },+ state: 'output-available',+ },+ ] as ExtendedUIMessagePart[];++ renderAssistantRenderer(messageParts, { isLastMessageStreaming: true });++ expect(screen.getByTestId('thinking-steps-display')).toHaveTextContent(+ 'trailing-while-streaming',+ );+ });++ it('should not flag a thinking steps group when answer text follows it', () => {+ const messageParts = [+ {+ type: 'tool-web_search',+ toolCallId: 'tool-1',+ input: { query: 'crm software' },+ output: { result: { ok: true } },+ state: 'output-available',+ },+ {+ type: 'text',+ text: 'Partial answer',+ state: 'streaming',+ },+ ] as ExtendedUIMessagePart[];++ renderAssistantRenderer(messageParts, { isLastMessageStreaming: true });++ expect(screen.getByTestId('thinking-steps-display')).not.toHaveTextContent(+ 'trailing-while-streaming',+ );+ });++ it('should not flag the trailing thinking steps group when the message is not streaming', () => {+ const messageParts = [+ {+ type: 'tool-web_search',+ toolCallId: 'tool-1',+ input: { query: 'crm software' },+ output: { result: { ok: true } },+ state: 'output-available',+ },+ ] as ExtendedUIMessagePart[];++ renderAssistantRenderer(messageParts);++ expect(screen.getByTestId('thinking-steps-display')).not.toHaveTextContent(+ 'trailing-while-streaming',+ );+ });+it('should group a dynamic-tool part (native web search) into ThinkingStepsDisplay', () => {const messageParts = [{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 2e4d68695be..47d10bfb4b8 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@@ -64,28 +64,32 @@ const createReasoningPart = ({const createToolPart = ({input = { query: 'crm software' },output = { result: { ok: true } },+ state = 'output-available',type = 'tool-web_search',}: {type?: `tool-${string}`;input?: Record<string, unknown>;output?: unknown;+ state?: string;} = {}): ThinkingStepPart =>({type,toolCallId: `${type}-call-id`,input,output,- state: 'output-available',+ state,}) as ThinkingStepPart;const renderThinkingStepsDisplay = ({hasAssistantTextResponseStarted = false,isLastMessageStreaming,parts,+ isTrailingWhileStreaming = false,}: {parts: ThinkingStepPart[];isLastMessageStreaming: boolean;hasAssistantTextResponseStarted?: boolean;+ isTrailingWhileStreaming?: boolean;}) => {return render(<ThemeProvider colorScheme="light">@@ -93,6 +97,7 @@ const renderThinkingStepsDisplay = ({parts={parts}isLastMessageStreaming={isLastMessageStreaming}hasAssistantTextResponseStarted={hasAssistantTextResponseStarted}+ isTrailingWhileStreaming={isTrailingWhileStreaming}/></ThemeProvider>,);@@ -120,6 +125,49 @@ describe('ThinkingStepsDisplay', () => {expect(document.querySelector('svg[viewBox="0 0 14 14"]')).not.toBeNull();});+ it('should render the loading label for a tool step awaiting its output while streaming', () => {+ renderThinkingStepsDisplay({+ isLastMessageStreaming: true,+ parts: [createToolPart({ output: null, state: 'input-available' })],+ });++ expect(+ screen.getByText('Searching the web for crm software'),+ ).toBeInTheDocument();+ });++ it('should append the pending thinking row after completed steps when requested', () => {+ renderThinkingStepsDisplay({+ isLastMessageStreaming: true,+ isTrailingWhileStreaming: true,+ parts: [createToolPart()],+ });++ expect(screen.getByText('Thinking')).toBeInTheDocument();+ });++ it('should not render a thinking row for completed steps by default', () => {+ renderThinkingStepsDisplay({+ isLastMessageStreaming: true,+ parts: [createToolPart()],+ });++ expect(screen.queryByText('Thinking')).toBeNull();+ });++ it('should not append the pending thinking row while a tool step is still running', () => {+ renderThinkingStepsDisplay({+ isLastMessageStreaming: true,+ isTrailingWhileStreaming: true,+ parts: [createToolPart({ output: null, state: 'input-available' })],+ });++ expect(screen.queryByText('Thinking')).toBeNull();+ expect(+ screen.getByText('Searching the web for crm software'),+ ).toBeInTheDocument();+ });+it('should render done state collapsed by default', () => {renderThinkingStepsDisplay({isLastMessageStreaming: false,diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/thinkingStepsDisplayState.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/thinkingStepsDisplayState.test.tsindex b094a8114b8..2c0ea8a129b 100644--- a/packages/twenty-front/src/modules/ai/utils/__tests__/thinkingStepsDisplayState.test.ts+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/thinkingStepsDisplayState.test.ts@@ -23,12 +23,14 @@ const createToolPart = ({errorText,input = {},output,+ state = 'output-available',type = 'tool-web_search',}: {type?: `tool-${string}`;input?: Record<string, unknown>;output?: unknown;errorText?: string;+ state?: string;} = {}): ThinkingStepPart =>({type,@@ -36,7 +38,7 @@ const createToolPart = ({input,output,errorText,- state: 'output-available',+ state,}) as ThinkingStepPart;describe('thinkingStepsDisplayState', () => {@@ -91,27 +93,32 @@ describe('thinkingStepsDisplayState', () => {expect(isThinkingStepPartActive(reasoningPart, false)).toBe(true);});- it('should mark tool parts without output as active while message is streaming', () => {+ it('should mark tool parts awaiting their output as active while message is streaming', () => {const toolPart = createToolPart({type: 'tool-web_search',- output: undefined,- errorText: undefined,+ state: 'input-available',});expect(isThinkingStepPartActive(toolPart, true)).toBe(true);expect(isThinkingStepPartActive(toolPart, false)).toBe(false);});- it('should mark tool parts with output or error as inactive', () => {+ it('should mark completed and failed tool parts as inactive', () => {const completedToolPart = createToolPart({output: { result: { ok: true } },});+ const completedNullOutputToolPart = createToolPart({+ output: null,+ });const failedToolPart = createToolPart({- output: undefined,errorText: 'Tool failed',+ state: 'output-error',});expect(isThinkingStepPartActive(completedToolPart, true)).toBe(false);+ expect(isThinkingStepPartActive(completedNullOutputToolPart, true)).toBe(+ false,+ );expect(isThinkingStepPartActive(failedToolPart, true)).toBe(false);});});diff --git a/packages/twenty-front/src/modules/ai/utils/isThinkingStepPartActive.ts b/packages/twenty-front/src/modules/ai/utils/isThinkingStepPartActive.tsindex 24c02c19579..a037724235a 100644--- a/packages/twenty-front/src/modules/ai/utils/isThinkingStepPartActive.ts+++ b/packages/twenty-front/src/modules/ai/utils/isThinkingStepPartActive.ts@@ -1,5 +1,3 @@-import { isDefined } from 'twenty-shared/utils';-import { type ThinkingStepPart } from '@/ai/utils/thinkingStepPart';export const isThinkingStepPartActive = (@@ -12,7 +10,6 @@ export const isThinkingStepPartActive = (return (isLastMessageStreaming &&- !isDefined(part.output) &&- !isDefined(part.errorText)+ (part.state === 'input-streaming' || part.state === 'input-available'));};