Skip to content

The open alternative to Salesforce, designed for AI.

Keep agent steps in one group and surface reasoning summaries (#27138)

Félix Malfait977b43a6baff
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
index 1995449a064..4e974beb666 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
@@ -7,7 +7,7 @@ import { ThinkingStepsDisplay } from '@/ai/components/ThinkingStepsDisplay';
import { AiChatEmailApprovalStatusRenderer } from '@/ai/components/AiChatEmailApprovalStatusRenderer';
import { AiChatFormStatusRenderer } from '@/ai/components/AiChatFormStatusRenderer';
import { AiChatQuestionStatusRenderer } from '@/ai/components/AiChatQuestionStatusRenderer';
-import { AiChatToolPartRenderer } from '@/ai/components/AiChatToolPartRenderer';
+import { AiChatToolWidget } from '@/ai/components/AiChatToolWidget';
import { LazyMarkdownContent } from '@/ai/components/LazyMarkdownRenderer';
import { ToolStepRenderer } from '@/ai/components/ToolStepRenderer';
import { useToolWidgetByName } from '@/ai/hooks/useToolWidgetByName';
@@ -16,6 +16,7 @@ import { getEffectiveToolName } from '@/ai/utils/getEffectiveToolName';
import { shouldToolPartRenderStandalone } from '@/ai/utils/shouldToolPartRenderStandalone';
import { groupContiguousThinkingStepParts } from '@/ai/utils/groupContiguousThinkingStepParts';
import { isCodeInterpreterToolPart } from '@/ai/utils/isCodeInterpreterToolPart';
+import { isEmptyReasoningPart } from '@/ai/utils/isEmptyReasoningPart';
import { isHiddenCompleteWorkspaceSetupToolPart } from '@/ai/utils/isHiddenCompleteWorkspaceSetupToolPart';
import { styled } from '@linaria/react';
import { getToolName, isToolUIPart } from 'ai';
@@ -99,9 +100,9 @@ const MessagePartRenderer = ({
}
return (
- <AiChatToolPartRenderer
+ <AiChatToolWidget
toolPart={part}
- widget={widget}
+ frontComponentId={widget.frontComponentId}
isStreaming={isStreaming}
/>
);
@@ -130,6 +131,7 @@ export const AiChatAssistantMessageRenderer = ({
const filteredParts = messageParts.filter(
(part) =>
part.type !== 'data-thread-title' &&
+ !isEmptyReasoningPart(part) &&
!isHiddenCompleteWorkspaceSetupToolPart(part) &&
!(hasCodeExecutionData && isCodeInterpreterToolPart(part)),
);
@@ -146,7 +148,11 @@ export const AiChatAssistantMessageRenderer = ({
const lastRenderItemIndex = renderItems.length - 1;
if (!renderItems.length && !hasError) {
- return hasSucceededCompleteWorkspaceSetupToolPart ? null : (
+ const hasOnlyHiddenReasoning =
+ !isLastMessageStreaming && messageParts.some(isEmptyReasoningPart);
+
+ return hasSucceededCompleteWorkspaceSetupToolPart ||
+ hasOnlyHiddenReasoning ? null : (
<AiChatInitialLoadingIndicator />
);
}
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx
deleted file mode 100644
index ed50f94f712..00000000000
--- a/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx
+++ /dev/null
@@ -1,43 +0,0 @@
-import { type DynamicToolUIPart, type ToolUIPart } from 'ai';
-import { isNonEmptyArray } from 'twenty-shared/utils';
-
-import { AiChatToolWidget } from '@/ai/components/AiChatToolWidget';
-import { ToolRecordsWidget } from '@/ai/components/ToolRecordsWidget';
-import { ToolStepRenderer } from '@/ai/components/ToolStepRenderer';
-import { type ToolWidget } from '@/ai/types/ToolWidget';
-import { getToolRecordOutput } from '@/ai/utils/getToolRecordOutput';
-
-type AiChatToolPartRendererProps = {
- toolPart: ToolUIPart | DynamicToolUIPart;
- widget: ToolWidget;
- isStreaming: boolean;
-};
-
-export const AiChatToolPartRenderer = ({
- toolPart,
- widget,
- isStreaming,
-}: AiChatToolPartRendererProps) => {
- if (widget.kind === 'front-component') {
- return (
- <AiChatToolWidget
- toolPart={toolPart}
- frontComponentId={widget.frontComponentId}
- isStreaming={isStreaming}
- />
- );
- }
-
- const { message, recordReferences } = getToolRecordOutput(toolPart);
-
- if (!isNonEmptyArray(recordReferences)) {
- return <ToolStepRenderer toolPart={toolPart} isStreaming={isStreaming} />;
- }
-
- return (
- <ToolRecordsWidget
- message={message ?? ''}
- recordReferences={recordReferences}
- />
- );
-};
diff --git a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx
index 3e00cd64c2a..a657f5997a6 100644
--- a/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx
+++ b/packages/twenty-front/src/modules/ai/components/ThinkingStepsDisplay.tsx
@@ -3,7 +3,7 @@ import { styled } from '@linaria/react';
import { plural, t } from '@lingui/core/macro';
import { getToolName, type DynamicToolUIPart, type ToolUIPart } from 'ai';
import { useState } from 'react';
-import { isDefined } from 'twenty-shared/utils';
+import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
import { JsonTree } from 'twenty-ui/components';
import { OverflowingTextWithTooltip } from 'twenty-ui/primitives/typography';
import { IconChevronRight, IconCpu } from 'twenty-ui/icon';
@@ -14,10 +14,12 @@ import { type JsonValue } from 'type-fest';
import { AiChatThinkingRow } from '@/ai/components/AiChatThinkingRow';
import { ShimmeringText } from '@/ai/components/ShimmeringText';
+import { ToolRecordsWidget } from '@/ai/components/ToolRecordsWidget';
import { useToolDisplayContext } from '@/ai/hooks/useToolDisplayContext';
import { getActiveReasoningContent } from '@/ai/utils/getActiveReasoningContent';
import { getLastReasoningContent } from '@/ai/utils/getLastReasoningContent';
import { getToolIcon } from '@/ai/utils/getToolIcon';
+import { getToolRecordOutput } from '@/ai/utils/getToolRecordOutput';
import { isThinkingStepPartActive } from '@/ai/utils/isThinkingStepPartActive';
import { type ThinkingStepPart } from '@/ai/utils/thinkingStepPart';
import { getToolDisplayMessage } from '@/ai/utils/tool-display/get-tool-display-message';
@@ -214,6 +216,10 @@ const StyledToolDetailsContainer = styled.div`
overflow: hidden;
`;
+const StyledToolRecordsContainer = styled.div`
+ padding: 0 ${themeCssVariables.spacing[2]};
+`;
+
const StyledToolTabListContainer = styled.div`
background-color: ${themeCssVariables.background.secondary};
padding-left: ${themeCssVariables.spacing[1]};
@@ -279,6 +285,8 @@ const ThinkingToolStepRow = ({
output: part.output,
});
const hasError = isDefined(part.errorText);
+ const { message: recordsMessage, recordReferences } =
+ getToolRecordOutput(part);
const isExpandable = isDefined(part.output) || hasError;
const outputObj =
@@ -339,6 +347,14 @@ const ThinkingToolStepRow = ({
{isExpandable && (
<Collapsible isExpanded={isExpanded}>
<StyledToolDetailsContainer>
+ {isNonEmptyArray(recordReferences) && (
+ <StyledToolRecordsContainer>
+ <ToolRecordsWidget
+ message={recordsMessage ?? ''}
+ recordReferences={recordReferences}
+ />
+ </StyledToolRecordsContainer>
+ )}
{hasError ? (
<StyledToolErrorText>{part.errorText}</StyledToolErrorText>
) : (
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.tsx
index 741c777966d..d38c2723c2c 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
@@ -42,12 +42,17 @@ jest.mock('@/ai/components/CodeExecutionDisplay', () => ({
CodeExecutionDisplay: () => <div data-testid="code-execution-display" />,
}));
-jest.mock('@/ai/components/AiChatToolPartRenderer', () => ({
- AiChatToolPartRenderer: ({ toolPart }: { toolPart: { type: string } }) => (
+jest.mock('@/ai/components/AiChatToolWidget', () => ({
+ AiChatToolWidget: ({ toolPart }: { toolPart: { type: string } }) => (
<div data-testid="tool-widget">{toolPart.type}</div>
),
}));
+const APP_WIDGET = {
+ kind: 'front-component',
+ frontComponentId: '20202020-0000-4000-8000-000000000001',
+};
+
const mockUseToolWidgetByName = jest.fn(() => new Map());
jest.mock('@/ai/hooks/useToolWidgetByName', () => ({
@@ -440,12 +445,17 @@ describe('AiChatAssistantMessageRenderer', () => {
'thinking-1-answer-started',
);
});
- it('should render a tool call that has a widget on its own, not folded into the step group', () => {
+ it('should keep a records widget call in the step group so it does not split the steps', () => {
mockUseToolWidgetByName.mockReturnValue(
new Map([['find_many_companies', { kind: 'builtin', name: 'records' }]]),
);
renderAssistantRenderer([
+ {
+ type: 'reasoning',
+ text: 'Reasoning content',
+ state: 'done',
+ },
{
type: 'tool-find_many_companies',
toolCallId: 'call_1',
@@ -453,17 +463,46 @@ describe('AiChatAssistantMessageRenderer', () => {
input: {},
output: { recordReferences: [] },
},
+ {
+ type: 'tool-web_search',
+ toolCallId: 'call_2',
+ input: { query: 'crm software' },
+ output: { result: { ok: true } },
+ state: 'output-available',
+ },
+ ] as unknown as ExtendedUIMessagePart[]);
+
+ expect(screen.getAllByTestId('thinking-steps-display')).toHaveLength(1);
+ expect(screen.getByTestId('thinking-steps-display')).toHaveTextContent(
+ 'thinking-3',
+ );
+ expect(screen.queryByTestId('tool-widget')).toBeNull();
+ });
+
+ it('should render a call that has an app widget on its own, not folded into the step group', () => {
+ mockUseToolWidgetByName.mockReturnValue(
+ new Map([['app_show_chart', APP_WIDGET]]),
+ );
+
+ renderAssistantRenderer([
+ {
+ type: 'tool-app_show_chart',
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input: {},
+ output: {},
+ },
] as unknown as ExtendedUIMessagePart[]);
expect(screen.getByTestId('tool-widget')).toHaveTextContent(
- 'tool-find_many_companies',
+ 'tool-app_show_chart',
);
expect(screen.queryByTestId('thinking-steps-display')).toBeNull();
});
it('should resolve the widget of a call dispatched through execute_tool', () => {
mockUseToolWidgetByName.mockReturnValue(
- new Map([['create_one_task', { kind: 'builtin', name: 'records' }]]),
+ new Map([['app_show_chart', APP_WIDGET]]),
);
renderAssistantRenderer([
@@ -471,24 +510,24 @@ describe('AiChatAssistantMessageRenderer', () => {
type: 'tool-execute_tool',
toolCallId: 'call_1',
state: 'output-available',
- input: { toolName: 'create_one_task', arguments: { title: 'Ship it' } },
- output: { recordReferences: [] },
+ input: { toolName: 'app_show_chart', arguments: {} },
+ output: {},
},
] as unknown as ExtendedUIMessagePart[]);
expect(screen.getByTestId('tool-widget')).toBeInTheDocument();
});
- it('should keep a call that has a widget but has not run yet in the step group', () => {
+ it('should keep a call that has an app widget but is still streaming its input in the step group', () => {
mockUseToolWidgetByName.mockReturnValue(
- new Map([['find_many_companies', { kind: 'builtin', name: 'records' }]]),
+ new Map([['app_show_chart', APP_WIDGET]]),
);
renderAssistantRenderer([
{
- type: 'tool-find_many_companies',
+ type: 'tool-app_show_chart',
toolCallId: 'call_1',
- state: 'input-available',
+ state: 'input-streaming',
input: {},
},
] as unknown as ExtendedUIMessagePart[]);
@@ -496,4 +535,81 @@ describe('AiChatAssistantMessageRenderer', () => {
expect(screen.getByTestId('thinking-steps-display')).toBeInTheDocument();
expect(screen.queryByTestId('tool-widget')).toBeNull();
});
+
+ it('should drop finished reasoning that has no text', () => {
+ renderAssistantRenderer([
+ {
+ type: 'reasoning',
+ text: '',
+ state: 'done',
+ },
+ {
+ type: 'tool-web_search',
+ toolCallId: 'tool-1',
+ input: { query: 'crm software' },
+ output: { result: { ok: true } },
+ state: 'output-available',
+ },
+ {
+ type: 'reasoning',
+ text: ' ',
+ state: 'done',
+ },
+ {
+ type: 'text',
+ text: 'Final answer',
+ },
+ ] as ExtendedUIMessagePart[]);
+
+ expect(screen.getByTestId('thinking-steps-display')).toHaveTextContent(
+ 'thinking-1-answer-started',
+ );
+ });
+
+ it('should drop a step group made only of hidden reasoning', () => {
+ renderAssistantRenderer([
+ {
+ type: 'reasoning',
+ text: '',
+ state: 'done',
+ },
+ {
+ type: 'text',
+ text: 'Final answer',
+ },
+ ] as ExtendedUIMessagePart[]);
+
+ expect(screen.queryByTestId('thinking-steps-display')).toBeNull();
+ expect(screen.getByTestId('markdown-renderer')).toHaveTextContent(
+ 'Final answer',
+ );
+ });
+
+ it('should render nothing for a finished message whose only reasoning was hidden', () => {
+ const { container } = renderAssistantRenderer([
+ { type: 'step-start' },
+ {
+ type: 'reasoning',
+ text: '',
+ state: 'done',
+ },
+ ] as ExtendedUIMessagePart[]);
+
+ expect(container).toBeEmptyDOMElement();
+ });
+
+ it('should keep the loading indicator while a message with only hidden reasoning is still streaming', () => {
+ const { container } = renderAssistantRenderer(
+ [
+ {
+ type: 'reasoning',
+ text: '',
+ state: 'done',
+ },
+ ] as ExtendedUIMessagePart[],
+ { isLastMessageStreaming: true },
+ );
+
+ expect(container).not.toBeEmptyDOMElement();
+ });
});
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.tsx
index 68144839d81..e094e9a10df 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,22 @@ jest.mock('@/ui/layout/tab-list/components/TabList', () => ({
),
}));
+jest.mock('@/ai/components/ToolRecordsWidget', () => ({
+ ToolRecordsWidget: ({
+ recordReferences,
+ }: {
+ recordReferences: Array<{ displayName: string }>;
+ }) => (
+ <div>
+ {recordReferences.map((recordReference) => (
+ <span key={recordReference.displayName}>
+ {recordReference.displayName}
+ </span>
+ ))}
+ </div>
+ ),
+}));
+
const createReasoningPart = ({
state = 'done',
text = 'Reasoning content',
@@ -289,4 +305,37 @@ describe('ThinkingStepsDisplay', () => {
expect(screen.queryByRole('button', { name: 'Input' })).toBeNull();
});
});
+
+ it('should show the records a tool step found when its row is expanded', async () => {
+ renderThinkingStepsDisplay({
+ isLastMessageStreaming: false,
+ hasAssistantTextResponseStarted: true,
+ parts: [
+ createToolPart({
+ type: 'tool-find_many_companies',
+ input: {},
+ output: {
+ message: 'Found 1 company record',
+ recordReferences: [
+ {
+ objectNameSingular: 'company',
+ recordId: '20202020-0000-4000-8000-000000000001',
+ displayName: 'Clearstreet',
+ },
+ ],
+ },
+ }),
+ ],
+ });
+
+ await userEvent.click(screen.getByRole('button', { name: /1 step/i }));
+
+ expect(screen.queryByText('Clearstreet')).toBeNull();
+
+ await userEvent.click(
+ screen.getByRole('button', { name: /find_many_companies/i }),
+ );
+
+ expect(screen.getByText('Clearstreet')).toBeInTheDocument();
+ });
});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/mapDBPartToUIMessagePart.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/mapDBPartToUIMessagePart.test.ts
index ce28c720840..29e76d62405 100644
--- a/packages/twenty-front/src/modules/ai/utils/__tests__/mapDBPartToUIMessagePart.test.ts
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/mapDBPartToUIMessagePart.test.ts
@@ -25,4 +25,14 @@ describe('mapDBPartToUIMessagePart', () => {
providerMetadata,
});
});
+
+ it('maps reasoning stored without content to empty text', () => {
+ expect(
+ mapDBPartToUIMessagePart({
+ type: 'reasoning',
+ reasoningContent: null,
+ state: 'done',
+ } as AgentMessagePart),
+ ).toMatchObject({ type: 'reasoning', text: '' });
+ });
});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/shouldToolPartRenderStandalone.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/shouldToolPartRenderStandalone.test.ts
index 27771dd0614..9f96ac89c58 100644
--- a/packages/twenty-front/src/modules/ai/utils/__tests__/shouldToolPartRenderStandalone.test.ts
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/shouldToolPartRenderStandalone.test.ts
@@ -27,25 +27,19 @@ describe('shouldToolPartRenderStandalone', () => {
).toBe(false);
});
- describe('a built-in widget', () => {
- it('renders once the call has produced output', () => {
+ it.each([
+ 'input-streaming',
+ 'input-available',
+ 'output-available',
+ 'output-error',
+ ])(
+ 'leaves a built-in records widget in the step group in the %s state',
+ (state) => {
expect(
- shouldToolPartRenderStandalone(
- buildToolPart('output-available'),
- BUILTIN_WIDGET,
- ),
- ).toBe(true);
- });
-
- it.each(['input-streaming', 'input-available', 'output-error'])(
- 'has nothing to draw in the %s state',
- (state) => {
- expect(
- shouldToolPartRenderStandalone(buildToolPart(state), BUILTIN_WIDGET),
- ).toBe(false);
- },
- );
- });
+ shouldToolPartRenderStandalone(buildToolPart(state), BUILTIN_WIDGET),
+ ).toBe(false);
+ },
+ );
describe('an app widget', () => {
it.each([
diff --git a/packages/twenty-front/src/modules/ai/utils/isEmptyReasoningPart.ts b/packages/twenty-front/src/modules/ai/utils/isEmptyReasoningPart.ts
new file mode 100644
index 00000000000..0bb88b3662a
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/isEmptyReasoningPart.ts
@@ -0,0 +1,7 @@
+import { type ExtendedUIMessagePart } from 'twenty-shared/ai';
+
+// Providers that keep reasoning hidden still emit a finished reasoning part, with no text to show.
+export const isEmptyReasoningPart = (part: ExtendedUIMessagePart): boolean =>
+ part.type === 'reasoning' &&
+ part.state !== 'streaming' &&
+ part.text.trim().length === 0;
diff --git a/packages/twenty-front/src/modules/ai/utils/mapDBPartToUIMessagePart.ts b/packages/twenty-front/src/modules/ai/utils/mapDBPartToUIMessagePart.ts
index 6193fed81dc..b7d97d6fc54 100644
--- a/packages/twenty-front/src/modules/ai/utils/mapDBPartToUIMessagePart.ts
+++ b/packages/twenty-front/src/modules/ai/utils/mapDBPartToUIMessagePart.ts
@@ -21,7 +21,7 @@ export const mapDBPartToUIMessagePart = (
case 'reasoning':
return {
type: 'reasoning',
- text: part.reasoningContent!,
+ text: part.reasoningContent ?? '',
state: part.state as ReasoningUIPart['state'],
providerMetadata: part.providerMetadata ?? undefined,
};
diff --git a/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts b/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts
index 9a0c997e8d2..777c996eece 100644
--- a/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts
+++ b/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts
@@ -2,19 +2,15 @@ import { type DynamicToolUIPart, type ToolUIPart } from 'ai';
import { type ToolWidget } from '@/ai/types/ToolWidget';
+type FrontComponentToolWidget = Extract<
+ ToolWidget,
+ { kind: 'front-component' }
+>;
+
+// Record results stay in the step group, since the answer cites the records that matter.
+// An app widget owns the whole call, so it mounts on its own once its input has settled.
export const shouldToolPartRenderStandalone = (
toolPart: ToolUIPart | DynamicToolUIPart,
widget: ToolWidget | undefined,
-): widget is ToolWidget => {
- if (widget === undefined) {
- return false;
- }
-
- // A record widget draws the call's output, so it has nothing to show before the call has run.
- if (widget.kind === 'builtin') {
- return toolPart.state === 'output-available';
- }
-
- // An app widget owns the whole call, approval and failure included, so it mounts once its input has settled.
- return toolPart.state !== 'input-streaming';
-};
+): widget is FrontComponentToolWidget =>
+ widget?.kind === 'front-component' && toolPart.state !== 'input-streaming';
diff --git a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
index 1a219a8fb57..d8f96a87538 100644
--- a/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
+++ b/packages/twenty-server/src/engine/core-modules/twenty-config/config-variables.ts
@@ -2101,6 +2101,15 @@ export class ConfigVariables {
@IsOptional()
AI_MODELS_DEFAULT_DISABLED: string[] = DEFAULT_DISABLED_MODELS;
+ @ConfigVariablesMetadata({
+ group: ConfigVariablesGroup.LLM,
+ description:
+ 'Ask OpenAI and Azure reasoning models for reasoning summaries in AI chat. Disable it if your OpenAI organization is not verified, since OpenAI rejects these requests.',
+ type: ConfigVariableType.BOOLEAN,
+ })
+ @IsOptional()
+ IS_OPENAI_REASONING_SUMMARY_ENABLED = true;
+
@ConfigVariablesMetadata({
group: ConfigVariablesGroup.SERVER_CONFIG,
description: 'Enable or disable multi-workspace support',
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
index 88aff960b17..03e00b2988e 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/chat-execution.service.ts
@@ -613,10 +613,10 @@ export class ChatExecutionService {
}),
providerOptions: getCallLevelProviderOptions({
sdkPackage: registeredModel.sdkPackage,
- providerOptions:
- this.aiModelConfigService.getReasoningProviderOptions(
- registeredModel,
- ),
+ providerOptions: this.aiModelConfigService.getReasoningProviderOptions(
+ registeredModel,
+ { shouldIncludeReasoningSummary: true },
+ ),
promptCacheKey: threadId,
}),
prepareStep: async ({ messages }) => {
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/services/ai-model-config.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/services/ai-model-config.service.ts
index 116581860f9..d1f8c1f6b81 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/services/ai-model-config.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/services/ai-model-config.service.ts
@@ -13,6 +13,7 @@ import {
AiModelRegistryService,
RegisteredAiModel,
} from 'src/engine/metadata-modules/ai/ai-models/services/ai-model-registry.service';
+import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { SdkProviderFactoryService } from 'src/engine/metadata-modules/ai/ai-models/services/sdk-provider-factory.service';
import { type NativeModelToolOptions } from 'src/engine/metadata-modules/ai/ai-models/types/native-model-tool-options.type';
import { buildReasoningProviderOptions } from 'src/engine/metadata-modules/ai/ai-models/utils/build-reasoning-provider-options.util';
@@ -23,10 +24,19 @@ export class AiModelConfigService {
constructor(
private readonly aiModelRegistryService: AiModelRegistryService,
private readonly sdkProviderFactory: SdkProviderFactoryService,
+ private readonly twentyConfigService: TwentyConfigService,
) {}
- getReasoningProviderOptions(model: RegisteredAiModel): ProviderOptions {
- return buildReasoningProviderOptions(model);
+ getReasoningProviderOptions(
+ model: RegisteredAiModel,
+ options?: { shouldIncludeReasoningSummary?: boolean },
+ ): ProviderOptions {
+ return buildReasoningProviderOptions(model, {
+ shouldIncludeReasoningSummary: options?.shouldIncludeReasoningSummary,
+ isOpenAiReasoningSummaryEnabled: this.twentyConfigService.get(
+ 'IS_OPENAI_REASONING_SUMMARY_ENABLED',
+ ),
+ });
}
getNativeModelTools(
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/__tests__/build-reasoning-provider-options.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/__tests__/build-reasoning-provider-options.util.spec.ts
index ff7a6023a72..39e2f3a3eab 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/__tests__/build-reasoning-provider-options.util.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/__tests__/build-reasoning-provider-options.util.spec.ts
@@ -153,4 +153,100 @@ describe('buildReasoningProviderOptions', () => {
).toEqual(expected);
},
);
+
+ describe('with a reasoning summary', () => {
+ it.each([
+ [
+ AI_SDK_OPENAI,
+ 'openai/gpt-5.6-sol',
+ { openai: { reasoningSummary: 'auto' } },
+ ],
+ [
+ AI_SDK_AZURE,
+ 'azure/gpt-5.6-sol',
+ { azure: { reasoningSummary: 'auto' } },
+ ],
+ [
+ AI_SDK_GOOGLE,
+ 'google/gemini-3.7-flash',
+ { google: { thinkingConfig: { includeThoughts: true } } },
+ ],
+ ] as const)(
+ 'asks %s for a summary of its reasoning',
+ (sdkPackage, modelId, expected) => {
+ expect(
+ buildReasoningProviderOptions(
+ { modelId, sdkPackage, supportsReasoning: true },
+ {
+ shouldIncludeReasoningSummary: true,
+ isOpenAiReasoningSummaryEnabled: true,
+ },
+ ),
+ ).toEqual(expected);
+ },
+ );
+
+ it.each([
+ [AI_SDK_OPENAI, 'openai/gpt-5.6-sol'],
+ [AI_SDK_AZURE, 'azure/gpt-5.6-sol'],
+ ] as const)(
+ 'asks %s for no summary until OpenAI summaries are enabled',
+ (sdkPackage, modelId) => {
+ expect(
+ buildReasoningProviderOptions(
+ { modelId, sdkPackage, supportsReasoning: true },
+ { shouldIncludeReasoningSummary: true },
+ ),
+ ).toEqual({});
+ },
+ );
+
+ it('keeps the pinned effort next to the summary', () => {
+ expect(
+ buildReasoningProviderOptions(
+ {
+ modelId: 'google/gemini-3.7-flash@low',
+ sdkPackage: AI_SDK_GOOGLE,
+ supportsReasoning: true,
+ effort: 'low',
+ },
+ { shouldIncludeReasoningSummary: true },
+ ),
+ ).toEqual({
+ google: {
+ thinkingConfig: { thinkingLevel: 'low', includeThoughts: true },
+ },
+ });
+ });
+
+ it('asks for no summary when reasoning is turned off', () => {
+ expect(
+ buildReasoningProviderOptions(
+ {
+ modelId: 'openai/gpt-5.6-sol@none',
+ sdkPackage: AI_SDK_OPENAI,
+ supportsReasoning: true,
+ effort: 'none',
+ },
+ {
+ shouldIncludeReasoningSummary: true,
+ isOpenAiReasoningSummaryEnabled: true,
+ },
+ ),
+ ).toEqual({ openai: { reasoningEffort: 'none' } });
+ });
+
+ it('asks nothing of a model that does not reason', () => {
+ expect(
+ buildReasoningProviderOptions(
+ {
+ modelId: 'openai/gpt-4.1',
+ sdkPackage: AI_SDK_OPENAI,
+ supportsReasoning: false,
+ },
+ { shouldIncludeReasoningSummary: true },
+ ),
+ ).toEqual({});
+ });
+ });
});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/build-reasoning-provider-options.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/build-reasoning-provider-options.util.ts
index 8fb894f3d99..cb8870454d2 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/build-reasoning-provider-options.util.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-models/utils/build-reasoning-provider-options.util.ts
@@ -18,10 +18,22 @@ export const buildReasoningProviderOptions = (
RegisteredAiModel,
'modelId' | 'sdkPackage' | 'supportsReasoning' | 'effort'
>,
+ {
+ shouldIncludeReasoningSummary = false,
+ isOpenAiReasoningSummaryEnabled = false,
+ }: {
+ shouldIncludeReasoningSummary?: boolean;
+ isOpenAiReasoningSummaryEnabled?: boolean;
+ } = {},
): ProviderOptions => {
const { effort } = model;
const thinksAdaptively =
model.supportsReasoning && isAdaptiveThinkingClaudeModel(model.modelId);
+ // OpenAI and Gemini keep their reasoning hidden unless asked for a summary.
+ const includesReasoningSummary =
+ shouldIncludeReasoningSummary &&
+ model.supportsReasoning &&
+ effort !== 'none';
switch (model.sdkPackage) {
case AI_SDK_ANTHROPIC:
@@ -49,13 +61,40 @@ export const buildReasoningProviderOptions = (
},
};
case AI_SDK_OPENAI:
- return isDefined(effort) ? { openai: { reasoningEffort: effort } } : {};
- case AI_SDK_AZURE:
- return isDefined(effort) ? { azure: { reasoningEffort: effort } } : {};
+ case AI_SDK_AZURE: {
+ // OpenAI rejects summary requests from organizations it has not verified.
+ const includesOpenAiReasoningSummary =
+ includesReasoningSummary && isOpenAiReasoningSummaryEnabled;
+
+ if (!isDefined(effort) && !includesOpenAiReasoningSummary) {
+ return {};
+ }
+
+ const providerKey =
+ model.sdkPackage === AI_SDK_OPENAI ? 'openai' : 'azure';
+
+ return {
+ [providerKey]: {
+ ...(isDefined(effort) ? { reasoningEffort: effort } : {}),
+ ...(includesOpenAiReasoningSummary
+ ? { reasoningSummary: 'auto' }
+ : {}),
+ },
+ };
+ }
case AI_SDK_GOOGLE:
- return isDefined(effort)
- ? { google: { thinkingConfig: { thinkingLevel: effort } } }
- : {};
+ if (!isDefined(effort) && !includesReasoningSummary) {
+ return {};
+ }
+
+ return {
+ google: {
+ thinkingConfig: {
+ ...(isDefined(effort) ? { thinkingLevel: effort } : {}),
+ ...(includesReasoningSummary ? { includeThoughts: true } : {}),
+ },
+ },
+ };
case AI_SDK_MISTRAL:
return isDefined(effort) ? { mistral: { reasoningEffort: effort } } : {};
case AI_SDK_XAI: