Skip to content

The open alternative to Salesforce, designed for AI.

AI chat: tool calls render as widgets named by the tool (#26179)

Félix Malfaitfac2e97292e9
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
index 60598df5c12..4b798a13226 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
@@ -3155,6 +3155,8 @@ type ToolIndexEntry {
category: String!
objectName: String
icon: String
+ widgetName: String
+ frontComponentId: String
inputSchema: JSON
}
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.ts b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
index 3431c16d35c..67a0f055338 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
@@ -2737,6 +2737,8 @@ export interface ToolIndexEntry {
category: Scalars['String']
objectName?: Scalars['String']
icon?: Scalars['String']
+ widgetName?: Scalars['String']
+ frontComponentId?: Scalars['String']
inputSchema?: Scalars['JSON']
__typename: 'ToolIndexEntry'
}
@@ -6452,6 +6454,8 @@ export interface ToolIndexEntryGenqlSelection{
category?: boolean | number
objectName?: boolean | number
icon?: boolean | number
+ widgetName?: boolean | number
+ frontComponentId?: boolean | number
inputSchema?: boolean | number
__typename?: boolean | number
__scalar?: boolean | number
diff --git a/packages/twenty-client-sdk/src/metadata/generated/types.ts b/packages/twenty-client-sdk/src/metadata/generated/types.ts
index 21f3038a9ad..f6e18afa911 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/types.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/types.ts
@@ -6131,6 +6131,12 @@ export default {
"icon": [
1
],
+ "widgetName": [
+ 1
+ ],
+ "frontComponentId": [
+ 1
+ ],
"inputSchema": [
9
],
diff --git a/packages/twenty-docs/developers/extend/apps/layout/front-components.mdx b/packages/twenty-docs/developers/extend/apps/layout/front-components.mdx
index 1ecbf982ac3..413f0181d9e 100644
--- a/packages/twenty-docs/developers/extend/apps/layout/front-components.mdx
+++ b/packages/twenty-docs/developers/extend/apps/layout/front-components.mdx
@@ -12,17 +12,19 @@ Front components are still under active development. Your code runs against a pa
## Where front components can be used
-Front components can render in three locations within Twenty:
+Front components can render in four locations within Twenty:
- **Side panel** — Non-headless front components open in the right-hand side panel. This is the default behavior when a front component is triggered from the command menu.
- **Widgets (dashboards and record pages)** — Front components can be embedded as widgets inside [page layouts](/developers/extend/apps/layout/page-layouts). When configuring a dashboard or a record page layout, users can add a front component widget.
- **App settings** — Defined with [`defineSettingsFrontComponent()`](#custom-settings-component), the front component is the whole content of the app's **Settings** tab, in place of the default **Variables** tab.
+- **AI chat** — A [logic function exposed as an AI tool](/developers/extend/apps/logic/logic-functions#exposing-a-function-as-an-ai-tool-or-workflow-action) can name a front component, which then renders that tool's calls inline in the chat in place of the default step row.
-A front component on its own isn't reachable from the UI — you need to *surface* it. The three ways to do that are:
+A front component on its own isn't reachable from the UI — you need to *surface* it. The four ways to do that are:
- **Pair it with a [command menu item](/developers/extend/apps/layout/command-menu-items)** — registers it in the command menu (Cmd+K) and, optionally, as a pinned quick-action.
- **Embed it as a widget in a [page layout](/developers/extend/apps/layout/page-layouts)** — places it on a record's detail page or dashboard.
- **Define it with [`defineSettingsFrontComponent()`](#custom-settings-component)** — makes it the whole content of the app's **Settings** tab, in place of the default **Variables** tab.
+- **Name it from a [logic function's `toolTriggerSettings`](/developers/extend/apps/logic/logic-functions#exposing-a-function-as-an-ai-tool-or-workflow-action)** — renders the tool's calls in the AI chat.
## Basic example
@@ -419,6 +421,7 @@ Available hooks:
| `useFrontComponentId()` | `string` | This component instance's ID |
| `useTimelineActivityId()` | `string` or `null` | The current timeline activity ID when rendering a custom timeline row |
| `useColorScheme()` | `'light'` or `'dark'` | The host UI's active color scheme (`System` is already resolved) |
+| `useToolCall()` | `FrontComponentToolCall` or `null` | The AI tool call this component is rendering, when it was surfaced from a logic function's `toolTriggerSettings` |
| `useFrontComponentExecutionContext(selector)` | varies | Access the full execution context with a selector function |
## Application variables
diff --git a/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx b/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
index ad1b5c73d0e..2650f2f3e0b 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/logic-functions.mdx
@@ -668,6 +668,60 @@ export default defineLogicFunction({
});
```
+##### Rendering a tool call with a front component
+
+By default a tool call shows up in the AI chat as a step row: the tool's name, and a spinner until it returns. Point `toolTriggerSettings.frontComponentUniversalIdentifier` at one of your [front components](/developers/extend/apps/layout/front-components) and the chat renders that component inline instead, for every call to this tool.
+
+```ts
+export default defineLogicFunction({
+ ...,
+ toolTriggerSettings: {
+ frontComponentUniversalIdentifier: '74c526eb-cb68-4cf7-b05c-0dd8c288d948',
+ },
+});
+```
+
+The component reads the call it is rendering with `useToolCall()`:
+
+```tsx src/front-components/invoice-preview.tsx
+import { defineFrontComponent } from 'twenty-sdk/define';
+import { useToolCall } from 'twenty-sdk/front-component';
+
+const InvoicePreview = () => {
+ const toolCall = useToolCall();
+
+ if (toolCall === null) {
+ return null;
+ }
+
+ if (toolCall.status !== 'output-available') {
+ return <p>Generating the invoice...</p>;
+ }
+
+ const { invoiceUrl } = toolCall.output ?? {};
+
+ return typeof invoiceUrl === 'string' ? (
+ <a href={invoiceUrl}>Open the invoice</a>
+ ) : (
+ <p>The invoice could not be generated.</p>
+ );
+};
+
+export default defineFrontComponent({
+ universalIdentifier: '74c526eb-cb68-4cf7-b05c-0dd8c288d948',
+ name: 'invoice-preview',
+ component: InvoicePreview,
+});
+```
+
+The component mounts as soon as the agent has finished streaming its arguments, so it owns the whole call, not just the result. `toolCall.status` walks through `input-available`, the approval states when the tool needs one, and then `output-available`, `output-denied` or `output-error`; `input` is what the agent proposed, `output` what your handler returned. Render each state you care about, since the step row is no longer there to cover them.
+
+Both `input` and `output` are typed `Record<string, unknown>`, so narrow each field before you use it rather than casting. That matters most for anything you hand to the DOM: a value you drop into an `href` without checking it is a string is a link the tool's output controls.
+
+<Note>
+If the identifier matches no front component in the app, the chat falls back to the default step row. Front components in the AI chat run under the same [role your app declares](/developers/extend/apps/config/roles), so a widget can only read and write what the app itself is allowed to.
+</Note>
+
##### A complete workflow action example
`workflowActionTriggerSettings` accepts four fields:
diff --git a/packages/twenty-front-component-renderer/src/types/FrontComponentExecutionContext.ts b/packages/twenty-front-component-renderer/src/types/FrontComponentExecutionContext.ts
index 9515c8883f2..4b9686ff49a 100644
--- a/packages/twenty-front-component-renderer/src/types/FrontComponentExecutionContext.ts
+++ b/packages/twenty-front-component-renderer/src/types/FrontComponentExecutionContext.ts
@@ -1,4 +1,5 @@
import { type AppLocale } from 'twenty-shared/translations';
+import { type FrontComponentToolCall } from 'twenty-sdk/front-component';
export type FrontComponentExecutionContext = {
frontComponentId: string;
@@ -13,4 +14,6 @@ export type FrontComponentExecutionContext = {
/** Resolved color scheme of the host UI ('System' is already resolved) */
colorScheme: 'light' | 'dark';
locale?: AppLocale;
+ /** Set when the component renders an AI chat tool call */
+ toolCall?: FrontComponentToolCall;
};
diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.ts
index c29a4f36af7..6f63f413b96 100644
--- a/packages/twenty-front/src/generated-metadata/graphql.ts
+++ b/packages/twenty-front/src/generated-metadata/graphql.ts
@@ -6140,11 +6140,13 @@ export type ToolIndexEntry = {
__typename?: 'ToolIndexEntry';
category: Scalars['String']['output'];
description: Scalars['String']['output'];
+ frontComponentId?: Maybe<Scalars['String']['output']>;
icon?: Maybe<Scalars['String']['output']>;
inputSchema?: Maybe<Scalars['JSON']['output']>;
label: Scalars['String']['output'];
name: Scalars['String']['output'];
objectName?: Maybe<Scalars['String']['output']>;
+ widgetName?: Maybe<Scalars['String']['output']>;
};
export type TransientToken = {
@@ -7813,7 +7815,7 @@ export type GetChatThreadsQuery = { __typename?: 'Query', chatThreads: Array<{ _
export type GetToolIndexQueryVariables = Exact<{ [key: string]: never; }>;
-export type GetToolIndexQuery = { __typename?: 'Query', getToolIndex: Array<{ __typename?: 'ToolIndexEntry', name: string, label: string, description: string, category: string, objectName?: string | null, icon?: string | null }> };
+export type GetToolIndexQuery = { __typename?: 'Query', getToolIndex: Array<{ __typename?: 'ToolIndexEntry', name: string, label: string, description: string, category: string, objectName?: string | null, icon?: string | null, widgetName?: string | null, frontComponentId?: string | null }> };
export type GetToolInputSchemaQueryVariables = Exact<{
toolName: Scalars['String']['input'];
@@ -10151,7 +10153,7 @@ export const GetAgentTurnsDocument = {"kind":"Document","definitions":[{"kind":"
export const GetAiChatUsageDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetAiChatUsage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"aiChatUsage"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"limitValue"}},{"kind":"Field","name":{"kind":"Name","value":"consumedValue"}},{"kind":"Field","name":{"kind":"Name","value":"periodEnd"}}]}}]}}]} as unknown as DocumentNode<GetAiChatUsageQuery, GetAiChatUsageQueryVariables>;
export const GetChatMessagesDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetChatMessages"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"threadId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UUID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"chatMessages"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"threadId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"threadId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"threadId"}},{"kind":"Field","name":{"kind":"Name","value":"turnId"}},{"kind":"Field","name":{"kind":"Name","value":"role"}},{"kind":"Field","name":{"kind":"Name","value":"status"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"parts"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"messageId"}},{"kind":"Field","name":{"kind":"Name","value":"orderIndex"}},{"kind":"Field","name":{"kind":"Name","value":"type"}},{"kind":"Field","name":{"kind":"Name","value":"textContent"}},{"kind":"Field","name":{"kind":"Name","value":"reasoningContent"}},{"kind":"Field","name":{"kind":"Name","value":"toolName"}},{"kind":"Field","name":{"kind":"Name","value":"toolCallId"}},{"kind":"Field","name":{"kind":"Name","value":"toolInput"}},{"kind":"Field","name":{"kind":"Name","value":"toolOutput"}},{"kind":"Field","name":{"kind":"Name","value":"state"}},{"kind":"Field","name":{"kind":"Name","value":"providerExecuted"}},{"kind":"Field","name":{"kind":"Name","value":"errorMessage"}},{"kind":"Field","name":{"kind":"Name","value":"errorDetails"}},{"kind":"Field","name":{"kind":"Name","value":"sourceUrlSourceId"}},{"kind":"Field","name":{"kind":"Name","value":"sourceUrlUrl"}},{"kind":"Field","name":{"kind":"Name","value":"sourceUrlTitle"}},{"kind":"Field","name":{"kind":"Name","value":"sourceDocumentSourceId"}},{"kind":"Field","name":{"kind":"Name","value":"sourceDocumentMediaType"}},{"kind":"Field","name":{"kind":"Name","value":"sourceDocumentTitle"}},{"kind":"Field","name":{"kind":"Name","value":"sourceDocumentFilename"}},{"kind":"Field","name":{"kind":"Name","value":"fileMediaType"}},{"kind":"Field","name":{"kind":"Name","value":"fileFilename"}},{"kind":"Field","name":{"kind":"Name","value":"fileUrl"}},{"kind":"Field","name":{"kind":"Name","value":"fileId"}},{"kind":"Field","name":{"kind":"Name","value":"providerMetadata"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}}]}}]}},{"kind":"Field","name":{"kind":"Name","value":"chatStreamCatchupChunks"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"threadId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"threadId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"chunks"}},{"kind":"Field","name":{"kind":"Name","value":"maxSeq"}},{"kind":"Field","name":{"kind":"Name","value":"error"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"code"}},{"kind":"Field","name":{"kind":"Name","value":"message"}}]}}]}}]}}]} as unknown as DocumentNode<GetChatMessagesQuery, GetChatMessagesQueryVariables>;
export const GetChatThreadsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetChatThreads"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"chatThreads"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"id"}},{"kind":"Field","name":{"kind":"Name","value":"title"}},{"kind":"Field","name":{"kind":"Name","value":"totalCacheReadTokens"}},{"kind":"Field","name":{"kind":"Name","value":"totalInputTokens"}},{"kind":"Field","name":{"kind":"Name","value":"totalOutputTokens"}},{"kind":"Field","name":{"kind":"Name","value":"contextWindowTokens"}},{"kind":"Field","name":{"kind":"Name","value":"conversationSize"}},{"kind":"Field","name":{"kind":"Name","value":"totalInputCredits"}},{"kind":"Field","name":{"kind":"Name","value":"totalOutputCredits"}},{"kind":"Field","name":{"kind":"Name","value":"deletedAt"}},{"kind":"Field","name":{"kind":"Name","value":"lastMessageAt"}},{"kind":"Field","name":{"kind":"Name","value":"createdAt"}},{"kind":"Field","name":{"kind":"Name","value":"updatedAt"}}]}}]}}]} as unknown as DocumentNode<GetChatThreadsQuery, GetChatThreadsQueryVariables>;
-export const GetToolIndexDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetToolIndex"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"getToolIndex"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"label"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"category"}},{"kind":"Field","name":{"kind":"Name","value":"objectName"}},{"kind":"Field","name":{"kind":"Name","value":"icon"}}]}}]}}]} as unknown as DocumentNode<GetToolIndexQuery, GetToolIndexQueryVariables>;
+export const GetToolIndexDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetToolIndex"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"getToolIndex"},"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"name"}},{"kind":"Field","name":{"kind":"Name","value":"label"}},{"kind":"Field","name":{"kind":"Name","value":"description"}},{"kind":"Field","name":{"kind":"Name","value":"category"}},{"kind":"Field","name":{"kind":"Name","value":"objectName"}},{"kind":"Field","name":{"kind":"Name","value":"icon"}},{"kind":"Field","name":{"kind":"Name","value":"widgetName"}},{"kind":"Field","name":{"kind":"Name","value":"frontComponentId"}}]}}]}}]} as unknown as DocumentNode<GetToolIndexQuery, GetToolIndexQueryVariables>;
export const GetToolInputSchemaDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"query","name":{"kind":"Name","value":"GetToolInputSchema"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"toolName"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"getToolInputSchema"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"toolName"},"value":{"kind":"Variable","name":{"kind":"Name","value":"toolName"}}}]}]}}]} as unknown as DocumentNode<GetToolInputSchemaQuery, GetToolInputSchemaQueryVariables>;
export const OnAgentChatEventDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"subscription","name":{"kind":"Name","value":"OnAgentChatEvent"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"threadId"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"UUID"}}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"onAgentChatEvent"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"threadId"},"value":{"kind":"Variable","name":{"kind":"Name","value":"threadId"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"threadId"}},{"kind":"Field","name":{"kind":"Name","value":"event"}}]}}]}}]} as unknown as DocumentNode<OnAgentChatEventSubscription, OnAgentChatEventSubscriptionVariables>;
export const TrackAnalyticsDocument = {"kind":"Document","definitions":[{"kind":"OperationDefinition","operation":"mutation","name":{"kind":"Name","value":"TrackAnalytics"},"variableDefinitions":[{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"type"}},"type":{"kind":"NonNullType","type":{"kind":"NamedType","name":{"kind":"Name","value":"AnalyticsType"}}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"event"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"name"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"String"}}},{"kind":"VariableDefinition","variable":{"kind":"Variable","name":{"kind":"Name","value":"properties"}},"type":{"kind":"NamedType","name":{"kind":"Name","value":"JSON"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"trackAnalytics"},"arguments":[{"kind":"Argument","name":{"kind":"Name","value":"type"},"value":{"kind":"Variable","name":{"kind":"Name","value":"type"}}},{"kind":"Argument","name":{"kind":"Name","value":"event"},"value":{"kind":"Variable","name":{"kind":"Name","value":"event"}}},{"kind":"Argument","name":{"kind":"Name","value":"name"},"value":{"kind":"Variable","name":{"kind":"Name","value":"name"}}},{"kind":"Argument","name":{"kind":"Name","value":"properties"},"value":{"kind":"Variable","name":{"kind":"Name","value":"properties"}}}],"selectionSet":{"kind":"SelectionSet","selections":[{"kind":"Field","name":{"kind":"Name","value":"success"}}]}}]}}]} as unknown as DocumentNode<TrackAnalyticsMutation, TrackAnalyticsMutationVariables>;
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
index abb201f7e14..2273c6f4c0d 100644
--- a/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
+++ b/packages/twenty-front/src/modules/ai/components/AiChatAssistantMessageRenderer.tsx
@@ -5,8 +5,13 @@ import { RoutingStatusDisplay } from '@/ai/components/RoutingStatusDisplay';
import { ThinkingStepsDisplay } from '@/ai/components/ThinkingStepsDisplay';
import { AiChatQuestionStatusRenderer } from '@/ai/components/AiChatQuestionStatusRenderer';
+import { AiChatToolPartRenderer } from '@/ai/components/AiChatToolPartRenderer';
import { LazyMarkdownContent } from '@/ai/components/LazyMarkdownRenderer';
import { ToolStepRenderer } from '@/ai/components/ToolStepRenderer';
+import { useToolWidgetByName } from '@/ai/hooks/useToolWidgetByName';
+import { type ToolWidget } from '@/ai/types/tool-widget.type';
+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 { isHiddenCompleteWorkspaceSetupToolPart } from '@/ai/utils/isHiddenCompleteWorkspaceSetupToolPart';
@@ -27,9 +32,11 @@ const StyledMessagePartsContainer = styled.div`
const MessagePartRenderer = ({
part,
+ widgetByToolName,
isStreaming,
}: {
part: ExtendedUIMessagePart;
+ widgetByToolName: Map<string, ToolWidget>;
isStreaming: boolean;
}) => {
switch (part.type) {
@@ -63,7 +70,19 @@ const MessagePartRenderer = ({
);
}
- return <ToolStepRenderer toolPart={part} isStreaming={isStreaming} />;
+ const widget = widgetByToolName.get(getEffectiveToolName(part));
+
+ if (!shouldToolPartRenderStandalone(part, widget)) {
+ return <ToolStepRenderer toolPart={part} isStreaming={isStreaming} />;
+ }
+
+ return (
+ <AiChatToolPartRenderer
+ toolPart={part}
+ widget={widget}
+ isStreaming={isStreaming}
+ />
+ );
}
return null;
}
@@ -78,6 +97,8 @@ export const AiChatAssistantMessageRenderer = ({
isLastMessageStreaming: boolean;
hasError?: boolean;
}) => {
+ const widgetByToolName = useToolWidgetByName();
+
const hasCodeExecutionData = messageParts.some(
(part) => part.type === 'data-code-execution',
);
@@ -90,7 +111,15 @@ export const AiChatAssistantMessageRenderer = ({
!isHiddenCompleteWorkspaceSetupToolPart(part) &&
!(hasCodeExecutionData && isCodeInterpreterToolPart(part)),
);
- const renderItems = groupContiguousThinkingStepParts(filteredParts);
+ const renderItems = groupContiguousThinkingStepParts(
+ filteredParts,
+ (part) =>
+ isToolUIPart(part) &&
+ shouldToolPartRenderStandalone(
+ part,
+ widgetByToolName.get(getEffectiveToolName(part)),
+ ),
+ );
const lastRenderItemIndex = renderItems.length - 1;
@@ -127,6 +156,7 @@ export const AiChatAssistantMessageRenderer = ({
<MessagePartRenderer
key={index}
part={renderItem.part}
+ widgetByToolName={widgetByToolName}
isStreaming={isLastMessageStreaming}
/>
),
diff --git a/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx b/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx
new file mode 100644
index 00000000000..85c70f4a661
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/AiChatToolPartRenderer.tsx
@@ -0,0 +1,45 @@
+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/tool-widget.type';
+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);
+
+ // A record tool that matched nothing, or failed, has no records to show and
+ // reads better as the step row it has always been.
+ 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/AiChatToolWidget.tsx b/packages/twenty-front/src/modules/ai/components/AiChatToolWidget.tsx
new file mode 100644
index 00000000000..d0f8ee00c2b
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/AiChatToolWidget.tsx
@@ -0,0 +1,71 @@
+import { styled } from '@linaria/react';
+import { Suspense, lazy } from 'react';
+
+import { type DynamicToolUIPart, getToolName, type ToolUIPart } from 'ai';
+import { themeCssVariables } from 'twenty-ui/theme-constants';
+
+import { ToolStepRenderer } from '@/ai/components/ToolStepRenderer';
+import { type ToolInput } from '@/ai/types/ToolInput';
+import { unwrapToolInput } from '@/ai/utils/tool-display/unwrap-tool-input.util';
+import { FrontComponentSkeletonLoader } from '@/front-components/components/FrontComponentSkeletonLoader';
+
+const FrontComponentRenderer = lazy(() =>
+ import('@/front-components/components/FrontComponentRenderer').then(
+ (module) => ({ default: module.FrontComponentRenderer }),
+ ),
+);
+
+const StyledContainer = styled.div`
+ background: ${themeCssVariables.background.secondary};
+ border: 1px solid ${themeCssVariables.border.color.medium};
+ border-radius: ${themeCssVariables.border.radius.md};
+ overflow: hidden;
+`;
+
+type AiChatToolWidgetProps = {
+ toolPart: ToolUIPart | DynamicToolUIPart;
+ frontComponentId: string;
+ isStreaming: boolean;
+};
+
+export const AiChatToolWidget = ({
+ toolPart,
+ frontComponentId,
+ isStreaming,
+}: AiChatToolWidgetProps) => {
+ // A call dispatched through execute_tool carries the real tool and its
+ // arguments inside the wrapper; the widget is the dispatched tool's, so it
+ // gets that identity rather than the dispatcher's.
+ const { toolName, toolInput } = unwrapToolInput({
+ input: toolPart.input as ToolInput,
+ toolName: getToolName(toolPart),
+ });
+
+ const toolCall = {
+ toolCallId: toolPart.toolCallId,
+ toolName,
+ status: toolPart.state,
+ input: toolInput as Record<string, unknown> | undefined,
+ output:
+ toolPart.state === 'output-available'
+ ? (toolPart.output as Record<string, unknown> | undefined)
+ : undefined,
+ errorText:
+ toolPart.state === 'output-error' ? toolPart.errorText : undefined,
+ };
+
+ return (
+ <StyledContainer>
+ <Suspense fallback={<FrontComponentSkeletonLoader />}>
+ <FrontComponentRenderer
+ frontComponentId={frontComponentId}
+ toolCall={toolCall}
+ loadingFallback={<FrontComponentSkeletonLoader />}
+ unavailableFallback={
+ <ToolStepRenderer toolPart={toolPart} isStreaming={isStreaming} />
+ }
+ />
+ </Suspense>
+ </StyledContainer>
+ );
+};
diff --git a/packages/twenty-front/src/modules/ai/components/ToolRecordsWidget.tsx b/packages/twenty-front/src/modules/ai/components/ToolRecordsWidget.tsx
new file mode 100644
index 00000000000..60799d70198
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/ToolRecordsWidget.tsx
@@ -0,0 +1,86 @@
+import { styled } from '@linaria/react';
+import { useLingui } from '@lingui/react/macro';
+
+import { type ToolRecordReference } from 'twenty-shared/ai';
+import { AppPath } from 'twenty-shared/types';
+import { getAppPath, isDefined } from 'twenty-shared/utils';
+import { useIcons } from 'twenty-ui/icon';
+import { LinkChip } from 'twenty-ui/primitives/data-display';
+import { themeCssVariables } from 'twenty-ui/theme-constants';
+
+import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
+
+const MAX_VISIBLE_RECORDS = 12;
+
+const StyledContainer = styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: ${themeCssVariables.spacing[2]};
+ padding: ${themeCssVariables.spacing[2]} 0;
+`;
+
+const StyledHeader = styled.div`
+ color: ${themeCssVariables.font.color.tertiary};
+ font-family: ${themeCssVariables.font.family};
+ font-size: ${themeCssVariables.font.size.sm};
+`;
+
+const StyledChips = styled.div`
+ display: flex;
+ flex-wrap: wrap;
+ gap: ${themeCssVariables.spacing[1]};
+`;
+
+const StyledOverflow = styled.span`
+ align-self: center;
+ color: ${themeCssVariables.font.color.tertiary};
+ font-family: ${themeCssVariables.font.family};
+ font-size: ${themeCssVariables.font.size.sm};
+`;
+
+type ToolRecordsWidgetProps = {
+ message: string;
+ recordReferences: ToolRecordReference[];
+};
+
+export const ToolRecordsWidget = ({
+ message,
+ recordReferences,
+}: ToolRecordsWidgetProps) => {
+ const { t } = useLingui();
+ const { objectMetadataItems } = useObjectMetadataItems();
+ const { getIcon } = useIcons();
+
+ const visibleReferences = recordReferences.slice(0, MAX_VISIBLE_RECORDS);
+ const hiddenCount = recordReferences.length - visibleReferences.length;
+
+ return (
+ <StyledContainer>
+ <StyledHeader>{message}</StyledHeader>
+ <StyledChips>
+ {visibleReferences.map((recordReference) => {
+ const objectMetadataItem = objectMetadataItems.find(
+ (item) => item.nameSingular === recordReference.objectNameSingular,
+ );
+ const Icon = getIcon(objectMetadataItem?.icon);
+
+ return (
+ <LinkChip
+ key={recordReference.recordId}
+ to={getAppPath(AppPath.RecordShowPage, {
+ objectNameSingular: recordReference.objectNameSingular,
+ objectRecordId: recordReference.recordId,
+ })}
+ startElement={isDefined(Icon) ? <Icon size={14} /> : undefined}
+ >
+ {recordReference.displayName}
+ </LinkChip>
+ );
+ })}
+ {hiddenCount > 0 && (
+ <StyledOverflow>{t`and ${hiddenCount} more`}</StyledOverflow>
+ )}
+ </StyledChips>
+ </StyledContainer>
+ );
+};
diff --git a/packages/twenty-front/src/modules/ai/components/__stories__/ToolRecordsWidget.stories.tsx b/packages/twenty-front/src/modules/ai/components/__stories__/ToolRecordsWidget.stories.tsx
new file mode 100644
index 00000000000..09f584d6590
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/__stories__/ToolRecordsWidget.stories.tsx
@@ -0,0 +1,117 @@
+import { type Meta, type StoryObj } from '@storybook/react-vite';
+import { expect, within } from 'storybook/test';
+import { ComponentDecorator, RouterDecorator } from 'twenty-ui/testing';
+
+import { ToolRecordsWidget } from '@/ai/components/ToolRecordsWidget';
+import { IconsProviderDecorator } from '~/testing/decorators/IconsProviderDecorator';
+import { ObjectMetadataItemsDecorator } from '~/testing/decorators/ObjectMetadataItemsDecorator';
+import { WorkspaceDecorator } from '~/testing/decorators/WorkspaceDecorator';
+import { graphqlMocks } from '~/testing/graphqlMocks';
+
+const meta: Meta<typeof ToolRecordsWidget> = {
+ title: 'Modules/AI/ToolRecordsWidget',
+ component: ToolRecordsWidget,
+ decorators: [
+ IconsProviderDecorator,
+ WorkspaceDecorator,
+ ObjectMetadataItemsDecorator,
+ RouterDecorator,
+ ComponentDecorator,
+ ],
+ parameters: {
+ msw: graphqlMocks,
+ container: { width: 600 },
+ },
+};
+
+export default meta;
+
+type Story = StoryObj<typeof ToolRecordsWidget>;
+
+export const FoundCompanies: Story = {
+ args: {
+ message: 'Found 4 company records',
+ recordReferences: [
+ {
+ objectNameSingular: 'company',
+ recordId: '20202020-a225-4b3d-a89c-7f6c30df998a',
+ displayName: 'Microsoft',
+ },
+ {
+ objectNameSingular: 'company',
+ recordId: '20202020-a8b0-422c-8fcf-5b7496f94975',
+ displayName: 'Meta',
+ },
+ {
+ objectNameSingular: 'company',
+ recordId: '20202020-aaf7-41d6-87a9-7add07bebfd8',
+ displayName: 'SLB',
+ },
+ {
+ objectNameSingular: 'company',
+ recordId: '20202020-a305-41e7-8c72-ba44072a4c58',
+ displayName: 'Google',
+ },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await canvas.findByText('Found 4 company records');
+ await canvas.findByText('Microsoft');
+
+ const googleLink = await canvas.findByRole('link', { name: /Google/ });
+
+ await expect(googleLink).toHaveAttribute(
+ 'href',
+ '/object/company/20202020-a305-41e7-8c72-ba44072a4c58',
+ );
+ },
+};
+
+export const CreatedOneRecord: Story = {
+ args: {
+ message: 'Created 1 person record',
+ recordReferences: [
+ {
+ objectNameSingular: 'person',
+ recordId: '20202020-3ec3-4fe3-8997-b76aa0bfa408',
+ displayName: 'Sylvie Palmer',
+ },
+ ],
+ },
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await canvas.findByText('Created 1 person record');
+
+ const personLink = await canvas.findByRole('link', {
+ name: /Sylvie Palmer/,
+ });
+
+ await expect(personLink).toHaveAttribute(
+ 'href',
+ '/object/person/20202020-3ec3-4fe3-8997-b76aa0bfa408',
+ );
+ },
+};
+
+export const ManyRecordsAreTruncated: Story = {
+ args: {
+ message: 'Found 20 company records',
+ recordReferences: Array.from({ length: 20 }, (_, index) => ({
+ objectNameSingular: 'company',
+ recordId: `20202020-0000-4000-8000-${String(index).padStart(12, '0')}`,
+ displayName: `Company ${index + 1}`,
+ })),
+ },
+ // The overflow label is translated, and Storybook renders a pseudo locale,
+ // so the count of rendered links is what can be asserted here.
+ play: async ({ canvasElement }) => {
+ const canvas = within(canvasElement);
+
+ await canvas.findByText('Company 12');
+
+ await expect(canvas.getAllByRole('link')).toHaveLength(12);
+ },
+};
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 502b7d062d4..24978fbee8d 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,6 +42,18 @@ jest.mock('@/ai/components/CodeExecutionDisplay', () => ({
CodeExecutionDisplay: () => <div data-testid="code-execution-display" />,
}));
+jest.mock('@/ai/components/AiChatToolPartRenderer', () => ({
+ AiChatToolPartRenderer: ({ toolPart }: { toolPart: { type: string } }) => (
+ <div data-testid="tool-widget">{toolPart.type}</div>
+ ),
+}));
+
+const mockUseToolWidgetByName = jest.fn(() => new Map());
+
+jest.mock('@/ai/hooks/useToolWidgetByName', () => ({
+ useToolWidgetByName: () => mockUseToolWidgetByName(),
+}));
+
const renderAssistantRenderer = (
messageParts: ExtendedUIMessagePart[],
{ isLastMessageStreaming = false }: { isLastMessageStreaming?: boolean } = {},
@@ -57,6 +69,10 @@ const renderAssistantRenderer = (
};
describe('AiChatAssistantMessageRenderer', () => {
+ beforeEach(() => {
+ mockUseToolWidgetByName.mockReturnValue(new Map());
+ });
+
it('should group reasoning and tool steps into ThinkingStepsDisplay', () => {
const messageParts = [
{
@@ -424,4 +440,60 @@ 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', () => {
+ mockUseToolWidgetByName.mockReturnValue(
+ new Map([['find_many_companies', { kind: 'builtin', name: 'records' }]]),
+ );
+
+ renderAssistantRenderer([
+ {
+ type: 'tool-find_many_companies',
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input: {},
+ output: { recordReferences: [] },
+ },
+ ] as unknown as ExtendedUIMessagePart[]);
+
+ expect(screen.getByTestId('tool-widget')).toHaveTextContent(
+ 'tool-find_many_companies',
+ );
+ 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' }]]),
+ );
+
+ renderAssistantRenderer([
+ {
+ type: 'tool-execute_tool',
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input: { toolName: 'create_one_task', arguments: { title: 'Ship it' } },
+ output: { recordReferences: [] },
+ },
+ ] 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', () => {
+ mockUseToolWidgetByName.mockReturnValue(
+ new Map([['find_many_companies', { kind: 'builtin', name: 'records' }]]),
+ );
+
+ renderAssistantRenderer([
+ {
+ type: 'tool-find_many_companies',
+ toolCallId: 'call_1',
+ state: 'input-available',
+ input: {},
+ },
+ ] as unknown as ExtendedUIMessagePart[]);
+
+ expect(screen.getByTestId('thinking-steps-display')).toBeInTheDocument();
+ expect(screen.queryByTestId('tool-widget')).toBeNull();
+ });
});
diff --git a/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolWidget.test.tsx b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolWidget.test.tsx
new file mode 100644
index 00000000000..60d1bbd29a9
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/components/__tests__/AiChatToolWidget.test.tsx
@@ -0,0 +1,66 @@
+import { render, screen } from '@testing-library/react';
+import { type ToolUIPart } from 'ai';
+
+import { AiChatToolWidget } from '@/ai/components/AiChatToolWidget';
+
+jest.mock('@/ai/components/ToolStepRenderer', () => ({
+ ToolStepRenderer: () => <div data-testid="tool-step-renderer" />,
+}));
+
+jest.mock('@/front-components/components/FrontComponentRenderer', () => ({
+ FrontComponentRenderer: ({
+ toolCall,
+ }: {
+ toolCall: { toolName: string; input?: Record<string, unknown> };
+ }) => (
+ <div data-testid="front-component">
+ {`${toolCall.toolName}:${JSON.stringify(toolCall.input)}`}
+ </div>
+ ),
+}));
+
+jest.mock('@/front-components/components/FrontComponentSkeletonLoader', () => ({
+ FrontComponentSkeletonLoader: () => <div data-testid="skeleton" />,
+}));
+
+const renderWidget = (toolPart: ToolUIPart) =>
+ render(
+ <AiChatToolWidget
+ toolPart={toolPart}
+ frontComponentId="20202020-0000-4000-8000-000000000001"
+ isStreaming={false}
+ />,
+ );
+
+describe('AiChatToolWidget', () => {
+ it('hands the component the call it renders', async () => {
+ renderWidget({
+ type: 'tool-app_draft_reply',
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input: { subject: 'Hello' },
+ output: {},
+ } as unknown as ToolUIPart);
+
+ expect(await screen.findByTestId('front-component')).toHaveTextContent(
+ 'app_draft_reply:{"subject":"Hello"}',
+ );
+ });
+
+ it('hands the component the dispatched tool when the call went through execute_tool', async () => {
+ renderWidget({
+ type: 'tool-execute_tool',
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input: {
+ toolName: 'app_draft_reply',
+ arguments: { subject: 'Hello' },
+ },
+ output: {},
+ } as unknown as ToolUIPart);
+
+ expect(await screen.findByTestId('front-component')).toHaveTextContent(
+ 'app_draft_reply:{"subject":"Hello"}',
+ );
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/graphql/queries/getToolIndex.ts b/packages/twenty-front/src/modules/ai/graphql/queries/getToolIndex.ts
index 1d6fb7b8de0..734aa0cc7d0 100644
--- a/packages/twenty-front/src/modules/ai/graphql/queries/getToolIndex.ts
+++ b/packages/twenty-front/src/modules/ai/graphql/queries/getToolIndex.ts
@@ -9,6 +9,8 @@ export const GET_TOOL_INDEX = gql`
category
objectName
icon
+ widgetName
+ frontComponentId
}
}
`;
diff --git a/packages/twenty-front/src/modules/ai/hooks/useToolWidgetByName.ts b/packages/twenty-front/src/modules/ai/hooks/useToolWidgetByName.ts
new file mode 100644
index 00000000000..0bcb4212f0e
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/hooks/useToolWidgetByName.ts
@@ -0,0 +1,42 @@
+import { useMemo } from 'react';
+
+import { isToolWidgetName } from 'twenty-shared/ai';
+import { isDefined } from 'twenty-shared/utils';
+
+import { useGetToolIndex } from '@/ai/hooks/useGetToolIndex';
+import { type ToolWidget } from '@/ai/types/tool-widget.type';
+
+const EMPTY_TOOL_WIDGETS: Map<string, ToolWidget> = new Map();
+
+// A tool names the widget that renders its calls; the chat resolves that
+// pointer instead of switching on tool names it would have to know about.
+export const useToolWidgetByName = (): Map<string, ToolWidget> => {
+ const { toolIndex } = useGetToolIndex();
+
+ return useMemo(() => {
+ if (toolIndex.length === 0) {
+ return EMPTY_TOOL_WIDGETS;
+ }
+
+ const widgetByName = new Map<string, ToolWidget>();
+
+ for (const entry of toolIndex) {
+ if (isDefined(entry.widgetName) && isToolWidgetName(entry.widgetName)) {
+ widgetByName.set(entry.name, {
+ kind: 'builtin',
+ name: entry.widgetName,
+ });
+ continue;
+ }
+
+ if (isDefined(entry.frontComponentId)) {
+ widgetByName.set(entry.name, {
+ kind: 'front-component',
+ frontComponentId: entry.frontComponentId,
+ });
+ }
+ }
+
+ return widgetByName;
+ }, [toolIndex]);
+};
diff --git a/packages/twenty-front/src/modules/ai/types/tool-widget.type.ts b/packages/twenty-front/src/modules/ai/types/tool-widget.type.ts
new file mode 100644
index 00000000000..9cc876e3d34
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/types/tool-widget.type.ts
@@ -0,0 +1,5 @@
+import { type ToolWidgetName } from 'twenty-shared/ai';
+
+export type ToolWidget =
+ | { kind: 'builtin'; name: ToolWidgetName }
+ | { kind: 'front-component'; frontComponentId: string };
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/getEffectiveToolName.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/getEffectiveToolName.test.ts
new file mode 100644
index 00000000000..594a961adac
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/getEffectiveToolName.test.ts
@@ -0,0 +1,40 @@
+import { type ToolUIPart } from 'ai';
+
+import { getEffectiveToolName } from '@/ai/utils/getEffectiveToolName';
+
+const buildToolPart = (
+ type: string,
+ input: Record<string, unknown>,
+): ToolUIPart =>
+ ({
+ type,
+ toolCallId: 'call_1',
+ state: 'output-available',
+ input,
+ output: {},
+ }) as unknown as ToolUIPart;
+
+describe('getEffectiveToolName', () => {
+ it('returns the tool name of a direct call', () => {
+ expect(
+ getEffectiveToolName(buildToolPart('tool-find_many_companies', {})),
+ ).toBe('find_many_companies');
+ });
+
+ it('returns the dispatched tool name of an execute_tool call', () => {
+ expect(
+ getEffectiveToolName(
+ buildToolPart('tool-execute_tool', {
+ toolName: 'create_one_task',
+ arguments: { title: 'Follow up' },
+ }),
+ ),
+ ).toBe('create_one_task');
+ });
+
+ it('falls back to execute_tool when the dispatch payload is unreadable', () => {
+ expect(
+ getEffectiveToolName(buildToolPart('tool-execute_tool', { oops: true })),
+ ).toBe('execute_tool');
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/__tests__/getToolRecordOutput.test.ts b/packages/twenty-front/src/modules/ai/utils/__tests__/getToolRecordOutput.test.ts
new file mode 100644
index 00000000000..008b7b9b95a
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/getToolRecordOutput.test.ts
@@ -0,0 +1,72 @@
+import { type ToolUIPart } from 'ai';
+
+import { getToolRecordOutput } from '@/ai/utils/getToolRecordOutput';
+
+const buildToolPart = (
+ state: string,
+ output: Record<string, unknown> | undefined,
+): ToolUIPart =>
+ ({
+ type: 'tool-find_many_companies',
+ toolCallId: 'call_1',
+ state,
+ input: {},
+ output,
+ }) as unknown as ToolUIPart;
+
+const recordReference = {
+ objectNameSingular: 'company',
+ recordId: '20202020-a305-41e7-8c72-ba44072a4c58',
+ displayName: 'Google',
+};
+
+describe('getToolRecordOutput', () => {
+ it('reads the message and record references a record tool answered with', () => {
+ expect(
+ getToolRecordOutput(
+ buildToolPart('output-available', {
+ success: true,
+ message: 'Found 1 company record',
+ recordReferences: [recordReference],
+ }),
+ ),
+ ).toEqual({
+ message: 'Found 1 company record',
+ recordReferences: [recordReference],
+ });
+ });
+
+ it('reads the references of an output that carries no message', () => {
+ expect(
+ getToolRecordOutput(
+ buildToolPart('output-available', {
+ recordReferences: [recordReference],
+ }),
+ ),
+ ).toEqual({ message: undefined, recordReferences: [recordReference] });
+ });
+
+ it('returns nothing while the call has not produced output', () => {
+ expect(
+ getToolRecordOutput(buildToolPart('input-available', undefined)),
+ ).toEqual({ message: undefined, recordReferences: [] });
+ });
+
+ it('returns nothing when the output carries no record references', () => {
+ expect(
+ getToolRecordOutput(
+ buildToolPart('output-available', { success: true, result: { a: 1 } }),
+ ),
+ ).toEqual({ message: undefined, recordReferences: [] });
+ });
+
+ it('returns nothing when a reference is malformed', () => {
+ expect(
+ getToolRecordOutput(
+ buildToolPart('output-available', {
+ recordReferences: [{ objectNameSingular: 'company' }],
+ }),
+ ),
+ ).toEqual({ message: undefined, recordReferences: [] });
+ });
+});
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
new file mode 100644
index 00000000000..0d43b91f4e5
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/__tests__/shouldToolPartRenderStandalone.test.ts
@@ -0,0 +1,73 @@
+import { type ToolUIPart } from 'ai';
+
+import { type ToolWidget } from '@/ai/types/tool-widget.type';
+import { shouldToolPartRenderStandalone } from '@/ai/utils/shouldToolPartRenderStandalone';
+
+const buildToolPart = (state: string): ToolUIPart =>
+ ({
+ type: 'tool-find_many_companies',
+ toolCallId: 'call_1',
+ state,
+ input: {},
+ }) as unknown as ToolUIPart;
+
+const BUILTIN_WIDGET: ToolWidget = { kind: 'builtin', name: 'records' };
+const APP_WIDGET: ToolWidget = {
+ kind: 'front-component',
+ frontComponentId: '20202020-0000-4000-8000-000000000001',
+};
+
+describe('shouldToolPartRenderStandalone', () => {
+ it('leaves a call with no widget in the step group', () => {
+ expect(
+ shouldToolPartRenderStandalone(
+ buildToolPart('output-available'),
+ undefined,
+ ),
+ ).toBe(false);
+ });
+
+ describe('a built-in widget', () => {
+ it('renders once the call has produced output', () => {
+ 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);
+ },
+ );
+ });
+
+ describe('an app widget', () => {
+ it.each([
+ 'input-available',
+ 'approval-requested',
+ 'approval-responded',
+ 'output-available',
+ 'output-denied',
+ 'output-error',
+ ])('owns the call in the %s state', (state) => {
+ expect(
+ shouldToolPartRenderStandalone(buildToolPart(state), APP_WIDGET),
+ ).toBe(true);
+ });
+
+ it('waits for the input to settle before mounting', () => {
+ expect(
+ shouldToolPartRenderStandalone(
+ buildToolPart('input-streaming'),
+ APP_WIDGET,
+ ),
+ ).toBe(false);
+ });
+ });
+});
diff --git a/packages/twenty-front/src/modules/ai/utils/getEffectiveToolName.ts b/packages/twenty-front/src/modules/ai/utils/getEffectiveToolName.ts
new file mode 100644
index 00000000000..56ec46dc6b3
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/getEffectiveToolName.ts
@@ -0,0 +1,14 @@
+import { getToolName, type DynamicToolUIPart, type ToolUIPart } from 'ai';
+
+import { type ToolInput } from '@/ai/types/ToolInput';
+import { unwrapToolInput } from '@/ai/utils/tool-display/unwrap-tool-input.util';
+
+// A call dispatched through execute_tool carries the real tool name in its
+// input; presentation belongs to that tool, not to the dispatcher.
+export const getEffectiveToolName = (
+ toolPart: ToolUIPart | DynamicToolUIPart,
+): string =>
+ unwrapToolInput({
+ input: toolPart.input as ToolInput,
+ toolName: getToolName(toolPart),
+ }).toolName;
diff --git a/packages/twenty-front/src/modules/ai/utils/getToolRecordOutput.ts b/packages/twenty-front/src/modules/ai/utils/getToolRecordOutput.ts
new file mode 100644
index 00000000000..62700cf4713
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/getToolRecordOutput.ts
@@ -0,0 +1,30 @@
+import { type DynamicToolUIPart, type ToolUIPart } from 'ai';
+import { z } from 'zod';
+
+const toolRecordOutputSchema = z.object({
+ message: z.string().optional(),
+ recordReferences: z.array(
+ z.object({
+ objectNameSingular: z.string(),
+ recordId: z.string(),
+ displayName: z.string(),
+ }),
+ ),
+});
+
+const EMPTY_TOOL_RECORD_OUTPUT = {
+ message: undefined,
+ recordReferences: [],
+} satisfies z.infer<typeof toolRecordOutputSchema>;
+
+export const getToolRecordOutput = (
+ toolPart: ToolUIPart | DynamicToolUIPart,
+) => {
+ if (toolPart.state !== 'output-available') {
+ return EMPTY_TOOL_RECORD_OUTPUT;
+ }
+
+ const parsed = toolRecordOutputSchema.safeParse(toolPart.output);
+
+ return parsed.success ? parsed.data : EMPTY_TOOL_RECORD_OUTPUT;
+};
diff --git a/packages/twenty-front/src/modules/ai/utils/groupContiguousThinkingStepParts.ts b/packages/twenty-front/src/modules/ai/utils/groupContiguousThinkingStepParts.ts
index f63d92f73b2..dc6ef61ff66 100644
--- a/packages/twenty-front/src/modules/ai/utils/groupContiguousThinkingStepParts.ts
+++ b/packages/twenty-front/src/modules/ai/utils/groupContiguousThinkingStepParts.ts
@@ -7,6 +7,8 @@ import { type ThinkingStepPart } from '@/ai/utils/thinkingStepPart';
export const groupContiguousThinkingStepParts = (
parts: ExtendedUIMessagePart[],
+ shouldRenderStandalone: (part: ExtendedUIMessagePart) => boolean = () =>
+ false,
): AssistantMessageRenderItem[] => {
const renderItems: AssistantMessageRenderItem[] = [];
let currentThinkingParts: ThinkingStepPart[] = [];
@@ -26,7 +28,11 @@ export const groupContiguousThinkingStepParts = (
continue;
}
- if (isThinkingStepPart(part) && !isAskQuestionsToolPart(part)) {
+ if (
+ isThinkingStepPart(part) &&
+ !isAskQuestionsToolPart(part) &&
+ !shouldRenderStandalone(part)
+ ) {
currentThinkingParts.push(part);
continue;
}
diff --git a/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts b/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts
new file mode 100644
index 00000000000..f815c1a696f
--- /dev/null
+++ b/packages/twenty-front/src/modules/ai/utils/shouldToolPartRenderStandalone.ts
@@ -0,0 +1,22 @@
+import { type DynamicToolUIPart, type ToolUIPart } from 'ai';
+
+import { type ToolWidget } from '@/ai/types/tool-widget.type';
+
+export const shouldToolPartRenderStandalone = (
+ toolPart: ToolUIPart | DynamicToolUIPart,
+ widget: ToolWidget | undefined,
+): widget is ToolWidget => {
+ if (widget === undefined) {
+ return false;
+ }
+
+ // A record widget draws what the call returned, 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 as soon as the input it renders has settled.
+ return toolPart.state !== 'input-streaming';
+};
diff --git a/packages/twenty-front/src/modules/ai/utils/tool-display/unwrap-tool-input.util.ts b/packages/twenty-front/src/modules/ai/utils/tool-display/unwrap-tool-input.util.ts
index 05252a9c1d2..893fbfec3ad 100644
--- a/packages/twenty-front/src/modules/ai/utils/tool-display/unwrap-tool-input.util.ts
+++ b/packages/twenty-front/src/modules/ai/utils/tool-display/unwrap-tool-input.util.ts
@@ -2,8 +2,10 @@ import { z } from 'zod';
import { type ToolInput } from '@/ai/types/ToolInput';
+// Coercing here would turn a missing name into the string "undefined", which
+// then reads as a real tool name downstream.
const ExecuteToolSchema = z.object({
- toolName: z.coerce.string(),
+ toolName: z.string().min(1),
arguments: z.unknown(),
});
diff --git a/packages/twenty-front/src/modules/front-components/components/FrontComponentRenderer.tsx b/packages/twenty-front/src/modules/front-components/components/FrontComponentRenderer.tsx
index 60506d57b76..2856f0181b4 100644
--- a/packages/twenty-front/src/modules/front-components/components/FrontComponentRenderer.tsx
+++ b/packages/twenty-front/src/modules/front-components/components/FrontComponentRenderer.tsx
@@ -12,6 +12,7 @@ import { useQuery } from '@apollo/client/react';
import { t } from '@lingui/core/macro';
import { type ReactNode, useCallback, useContext, useMemo } from 'react';
import { FrontComponentRenderer as SharedFrontComponentRenderer } from 'twenty-front-component-renderer';
+import { type FrontComponentToolCall } from 'twenty-sdk/front-component';
import { isDefined } from 'twenty-shared/utils';
import { useToast } from 'twenty-ui/primitives/feedback';
import { ThemeContext } from 'twenty-ui/theme-constants';
@@ -27,6 +28,7 @@ type FrontComponentRendererProps = {
commandMenuItemId?: string;
selectedRecordIds?: string[];
timelineActivityId?: string;
+ toolCall?: FrontComponentToolCall;
loadingFallback?: ReactNode;
unavailableFallback?: ReactNode;
};
@@ -40,6 +42,7 @@ type FrontComponentRendererContentProps = {
commandMenuItemId?: string;
selectedRecordIds?: string[];
timelineActivityId?: string;
+ toolCall?: FrontComponentToolCall;
loadingFallback?: ReactNode;
};
@@ -48,6 +51,7 @@ export const FrontComponentRenderer = ({
commandMenuItemId,
selectedRecordIds,
timelineActivityId,
+ toolCall,
loadingFallback,
unavailableFallback,
}: FrontComponentRendererProps) => {
@@ -74,6 +78,7 @@ export const FrontComponentRenderer = ({
commandMenuItemId={commandMenuItemId}
selectedRecordIds={selectedRecordIds}
timelineActivityId={timelineActivityId}
+ toolCall={toolCall}
loadingFallback={loadingFallback}
/>
)}
@@ -86,6 +91,7 @@ const FrontComponentRendererContent = ({
commandMenuItemId,
selectedRecordIds,
timelineActivityId,
+ toolCall,
loadingFallback,
}: FrontComponentRendererContentProps) => {
const { colorScheme } = useContext(ThemeContext);
@@ -109,6 +115,7 @@ const FrontComponentRendererContent = ({
commandMenuItemId,
selectedRecordIds,
timelineActivityId,
+ toolCall,
colorScheme,
});
diff --git a/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts b/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts
index 199a32e2083..76f4b2ee9d0 100644
--- a/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts
+++ b/packages/twenty-front/src/modules/front-components/hooks/useFrontComponentExecutionContext.ts
@@ -2,6 +2,7 @@ import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadat
import { getFieldMetadataItemById } from '@/object-metadata/utils/getFieldMetadataItemById';
import { resolveOpenRecordIn } from '@/object-record/record-index/utils/resolveOpenRecordIn';
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
+import { type FrontComponentToolCall } from 'twenty-sdk/front-component';
import { useLingui } from '@lingui/react/macro';
import { isNonEmptyString } from '@sniptt/guards';
import { useRef } from 'react';
@@ -119,6 +120,7 @@ export const useFrontComponentExecutionContext = ({
commandMenuItemId,
selectedRecordIds,
timelineActivityId,
+ toolCall,
colorScheme,
}: {
frontComponentId: string;
@@ -126,6 +128,7 @@ export const useFrontComponentExecutionContext = ({
commandMenuItemId?: string;
selectedRecordIds?: string[];
timelineActivityId?: string;
+ toolCall?: FrontComponentToolCall;
colorScheme: 'light' | 'dark';
}): {
executionContext: FrontComponentExecutionContext;
@@ -422,6 +425,7 @@ export const useFrontComponentExecutionContext = ({
recordId: selectedRecordIds?.length === 1 ? selectedRecordIds[0] : null,
selectedRecordIds: selectedRecordIds ?? [],
timelineActivityId: timelineActivityId ?? null,
+ toolCall,
colorScheme,
// i18n.locale is a Lingui string; the host is always configured with the
// APP_LOCALES set, so it is a valid AppLocale.
diff --git a/packages/twenty-sdk/src/sdk/front-component/hooks/useToolCall.ts b/packages/twenty-sdk/src/sdk/front-component/hooks/useToolCall.ts
new file mode 100644
index 00000000000..3e03cb44918
--- /dev/null
+++ b/packages/twenty-sdk/src/sdk/front-component/hooks/useToolCall.ts
@@ -0,0 +1,11 @@
+import { type FrontComponentExecutionContext } from '../types/FrontComponentExecutionContext';
+import { type FrontComponentToolCall } from '../types/FrontComponentToolCall';
+import { useFrontComponentExecutionContext } from './useFrontComponentExecutionContext';
+
+const selectToolCall = (
+ context: FrontComponentExecutionContext,
+): FrontComponentToolCall | null => context.toolCall ?? null;
+
+export const useToolCall = (): FrontComponentToolCall | null => {
+ return useFrontComponentExecutionContext(selectToolCall);
+};
diff --git a/packages/twenty-sdk/src/sdk/front-component/index.ts b/packages/twenty-sdk/src/sdk/front-component/index.ts
index f629fc91ee9..98753eb20e3 100644
--- a/packages/twenty-sdk/src/sdk/front-component/index.ts
+++ b/packages/twenty-sdk/src/sdk/front-component/index.ts
@@ -17,6 +17,7 @@ export { useTranslate } from './hooks/useTranslate';
export type { UseTranslateResult } from './hooks/useTranslate';
export { useLocale } from './hooks/useLocale';
export { useRecordId } from './hooks/useRecordId';
+export { useToolCall } from './hooks/useToolCall';
export { useSelectedRecordIds } from './hooks/useSelectedRecordIds';
export { useTimelineActivityId } from './hooks/useTimelineActivityId';
export { useUserId } from './hooks/useUserId';
@@ -27,6 +28,8 @@ export type {
TranslationValues,
} from './translations/message';
export type { FrontComponentExecutionContext } from './types/FrontComponentExecutionContext';
+export type { FrontComponentToolCall } from './types/FrontComponentToolCall';
+export type { FrontComponentToolCallStatus } from './types/FrontComponentToolCallStatus';
export type { FrontComponentStorageType } from './types/FrontComponentStorageType';
export { getFrontComponentCommandErrorDedupeKey } from './utils/getFrontComponentCommandErrorDedupeKey';
export type {
diff --git a/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentExecutionContext.ts b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentExecutionContext.ts
index 9515c8883f2..fd0b17e3ffa 100644
--- a/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentExecutionContext.ts
+++ b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentExecutionContext.ts
@@ -1,5 +1,7 @@
import { type AppLocale } from 'twenty-shared/translations';
+import { type FrontComponentToolCall } from './FrontComponentToolCall';
+
export type FrontComponentExecutionContext = {
frontComponentId: string;
userId: string | null;
@@ -13,4 +15,6 @@ export type FrontComponentExecutionContext = {
/** Resolved color scheme of the host UI ('System' is already resolved) */
colorScheme: 'light' | 'dark';
locale?: AppLocale;
+ /** Set when the component renders an AI chat tool call */
+ toolCall?: FrontComponentToolCall;
};
diff --git a/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCall.ts b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCall.ts
new file mode 100644
index 00000000000..91674474318
--- /dev/null
+++ b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCall.ts
@@ -0,0 +1,12 @@
+import { type FrontComponentToolCallStatus } from './FrontComponentToolCallStatus';
+
+export type FrontComponentToolCall = {
+ toolCallId: string;
+ toolName: string;
+ status: FrontComponentToolCallStatus;
+ // What the agent proposed. Absent while the model is still streaming it.
+ input?: Record<string, unknown>;
+ // What the tool returned. Absent until the call has run.
+ output?: Record<string, unknown>;
+ errorText?: string;
+};
diff --git a/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCallStatus.ts b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCallStatus.ts
new file mode 100644
index 00000000000..b235378f70e
--- /dev/null
+++ b/packages/twenty-sdk/src/sdk/front-component/types/FrontComponentToolCallStatus.ts
@@ -0,0 +1,10 @@
+// Mirrors the tool part states the chat runtime emits, approval states
+// included, so a widget can render a call at any point in its life.
+export type FrontComponentToolCallStatus =
+ | 'input-streaming'
+ | 'input-available'
+ | 'approval-requested'
+ | 'approval-responded'
+ | 'output-available'
+ | 'output-denied'
+ | 'output-error';
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/database-tool.provider.spec.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/database-tool.provider.spec.ts
index e6b529e3612..2e34a2d1408 100644
--- a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/database-tool.provider.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/database-tool.provider.spec.ts
@@ -299,6 +299,26 @@ describe('DatabaseToolProvider', () => {
}
});
+ it('names the records widget on every record descriptor but group_by', async () => {
+ const descriptors = await generateDescriptors([
+ createFlatObject({
+ nameSingular: 'task',
+ namePlural: 'tasks',
+ labelSingular: 'Task',
+ labelPlural: 'Tasks',
+ }),
+ ]);
+
+ for (const descriptor of descriptors) {
+ if (descriptor.operation === 'group_by') {
+ expect(descriptor.widgetName).toBeUndefined();
+ continue;
+ }
+
+ expect(descriptor.widgetName).toBe('records');
+ }
+ });
+
describe('requireExplicitObjectGrants', () => {
const personObject = createFlatObject({
nameSingular: 'person',
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/logic-function-tool.provider.spec.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/logic-function-tool.provider.spec.ts
new file mode 100644
index 00000000000..aaef7ca52cc
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/__tests__/logic-function-tool.provider.spec.ts
@@ -0,0 +1,114 @@
+import { LogicFunctionToolProvider } from 'src/engine/core-modules/tool-provider/providers/logic-function-tool.provider';
+import { type ToolIndexEntry } from 'src/engine/core-modules/tool-provider/types/tool-index-entry.type';
+import { createEmptyFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-flat-entity-maps.constant';
+import { type WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
+import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
+import { type FlatFrontComponent } from 'src/engine/metadata-modules/flat-front-component/types/flat-front-component.type';
+import { type FlatLogicFunction } from 'src/engine/metadata-modules/logic-function/types/flat-logic-function.type';
+
+const workspaceId = 'workspace-id';
+const roleId = 'role-id';
+
+const FRONT_COMPONENT_UNIVERSAL_IDENTIFIER = 'invoice-preview-component';
+const FRONT_COMPONENT_ID = 'front-component-id';
+
+const createFlatLogicFunction = (
+ overrides: Partial<FlatLogicFunction> & Pick<FlatLogicFunction, 'name'>,
+) =>
+ ({
+ id: `${overrides.name}-id`,
+ universalIdentifier: overrides.name,
+ description: null,
+ deletedAt: null,
+ toolTriggerSettings: {},
+ ...overrides,
+ }) as FlatLogicFunction;
+
+const generateDescriptors = async (logicFunctions: FlatLogicFunction[]) => {
+ const flatLogicFunctionMaps =
+ createEmptyFlatEntityMaps() as FlatEntityMaps<FlatLogicFunction>;
+
+ for (const logicFunction of logicFunctions) {
+ flatLogicFunctionMaps.byUniversalIdentifier[
+ logicFunction.universalIdentifier
+ ] = logicFunction;
+ flatLogicFunctionMaps.universalIdentifierById[logicFunction.id] =
+ logicFunction.universalIdentifier;
+ }
+
+ const flatFrontComponentMaps =
+ createEmptyFlatEntityMaps() as FlatEntityMaps<FlatFrontComponent>;
+
+ flatFrontComponentMaps.byUniversalIdentifier[
+ FRONT_COMPONENT_UNIVERSAL_IDENTIFIER
+ ] = {
+ id: FRONT_COMPONENT_ID,
+ universalIdentifier: FRONT_COMPONENT_UNIVERSAL_IDENTIFIER,
+ } as FlatFrontComponent;
+ flatFrontComponentMaps.universalIdentifierById[FRONT_COMPONENT_ID] =
+ FRONT_COMPONENT_UNIVERSAL_IDENTIFIER;
+
+ const flatEntityMapsCacheService = {
+ getOrRecomputeManyOrAllFlatEntityMaps: jest.fn().mockResolvedValue({
+ flatLogicFunctionMaps,
+ flatObjectMetadataMaps: createEmptyFlatEntityMaps(),
+ flatFrontComponentMaps,
+ }),
+ } as unknown as WorkspaceManyOrAllFlatEntityMapsCacheService;
+
+ const provider = new LogicFunctionToolProvider(flatEntityMapsCacheService);
+
+ return (await provider.generateDescriptors(
+ { workspaceId, roleId, rolePermissionConfig: { unionOf: [roleId] } },
+ { includeSchemas: false },
+ )) as ToolIndexEntry[];
+};
+
+describe('LogicFunctionToolProvider', () => {
+ it('should resolve the declared front component to the id the chat renders', async () => {
+ const [descriptor] = await generateDescriptors([
+ createFlatLogicFunction({
+ name: 'generate-invoice',
+ toolTriggerSettings: {
+ frontComponentUniversalIdentifier:
+ FRONT_COMPONENT_UNIVERSAL_IDENTIFIER,
+ },
+ }),
+ ]);
+
+ expect(descriptor.name).toBe('app_generate_invoice');
+ expect(descriptor.frontComponentId).toBe(FRONT_COMPONENT_ID);
+ });
+
+ it('should leave the front component unset when the app declares none', async () => {
+ const [descriptor] = await generateDescriptors([
+ createFlatLogicFunction({ name: 'generate-invoice' }),
+ ]);
+
+ expect(descriptor.frontComponentId).toBeUndefined();
+ });
+
+ it('should leave the front component unset when the declared identifier matches nothing', async () => {
+ const [descriptor] = await generateDescriptors([
+ createFlatLogicFunction({
+ name: 'generate-invoice',
+ toolTriggerSettings: {
+ frontComponentUniversalIdentifier: 'component-that-does-not-exist',
+ },
+ }),
+ ]);
+
+ expect(descriptor.frontComponentId).toBeUndefined();
+ });
+
+ it('should skip logic functions that are not exposed as tools', async () => {
+ const descriptors = await generateDescriptors([
+ createFlatLogicFunction({
+ name: 'internal-helper',
+ toolTriggerSettings: null,
+ }),
+ ]);
+
+ expect(descriptors).toEqual([]);
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/database-tool.provider.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/database-tool.provider.ts
index 967845fbffa..dda980e6ce1 100644
--- a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/database-tool.provider.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/database-tool.provider.ts
@@ -36,7 +36,7 @@ import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadat
import { getObjectsPermissionsFromRolePermissionConfig } from 'src/engine/twenty-orm/utils/get-objects-permissions-from-role-permission-config.util';
import { getRoleIdsFromRolePermissionConfig } from 'src/engine/twenty-orm/utils/get-role-ids-from-role-permission-config.util';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
-import { ToolCategory } from 'twenty-shared/ai';
+import { RECORDS_TOOL_WIDGET_NAME, ToolCategory } from 'twenty-shared/ai';
import { type FlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/types/flat-entity-maps.type';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
@@ -467,7 +467,13 @@ export class DatabaseToolProvider implements ToolProvider {
}
}
- return descriptors;
+ // Every record operation but group_by answers with recordReferences, which
+ // the records widget turns into links; group_by answers with aggregates.
+ return descriptors.map((descriptor) =>
+ descriptor.operation === 'group_by'
+ ? descriptor
+ : { ...descriptor, widgetName: RECORDS_TOOL_WIDGET_NAME },
+ );
}
private async buildFieldsResolver({
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/logic-function-tool.provider.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/logic-function-tool.provider.ts
index 669f05fcc0b..85021328e41 100644
--- a/packages/twenty-server/src/engine/core-modules/tool-provider/providers/logic-function-tool.provider.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/providers/logic-function-tool.provider.ts
@@ -49,14 +49,31 @@ export class LogicFunctionToolProvider implements ToolProvider {
): Promise<(ToolIndexEntry | ToolDescriptor)[]> {
const includeSchemas = options?.includeSchemas ?? true;
- const { flatLogicFunctionMaps, flatObjectMetadataMaps } =
+ const {
+ flatLogicFunctionMaps,
+ flatObjectMetadataMaps,
+ flatFrontComponentMaps,
+ } =
await this.flatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
{
workspaceId: context.workspaceId,
- flatMapsKeys: ['flatLogicFunctionMaps', 'flatObjectMetadataMaps'],
+ flatMapsKeys: [
+ 'flatLogicFunctionMaps',
+ 'flatObjectMetadataMaps',
+ 'flatFrontComponentMaps',
+ ],
},
);
+ const resolveWidgetFrontComponentId = (
+ frontComponentUniversalIdentifier: string | undefined,
+ ) =>
+ isDefined(frontComponentUniversalIdentifier)
+ ? flatFrontComponentMaps.byUniversalIdentifier[
+ frontComponentUniversalIdentifier
+ ]?.id
+ : undefined;
+
const resolveObjectLabel = (objectUniversalIdentifier: string) =>
flatObjectMetadataMaps.byUniversalIdentifier[objectUniversalIdentifier]
?.labelSingular;
@@ -86,6 +103,9 @@ export class LogicFunctionToolProvider implements ToolProvider {
kind: 'logic_function',
logicFunctionId: logicFunction.id,
},
+ frontComponentId: resolveWidgetFrontComponentId(
+ logicFunction.toolTriggerSettings?.frontComponentUniversalIdentifier,
+ ),
};
if (includeSchemas) {
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/resolvers/tool-index.resolver.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/resolvers/tool-index.resolver.ts
index 515dcfd27f8..886e20ddafd 100644
--- a/packages/twenty-server/src/engine/core-modules/tool-provider/resolvers/tool-index.resolver.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/resolvers/tool-index.resolver.ts
@@ -36,6 +36,12 @@ export class ToolIndexEntryDTO {
@Field({ nullable: true })
icon?: string;
+ @Field({ nullable: true })
+ widgetName?: string;
+
+ @Field({ nullable: true })
+ frontComponentId?: string;
+
@Field(() => graphqlTypeJson, { nullable: true })
inputSchema?: object;
}
diff --git a/packages/twenty-server/src/engine/core-modules/tool-provider/types/tool-index-entry.type.ts b/packages/twenty-server/src/engine/core-modules/tool-provider/types/tool-index-entry.type.ts
index 4993bbc7085..3c6ce4ae7a4 100644
--- a/packages/twenty-server/src/engine/core-modules/tool-provider/types/tool-index-entry.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/tool-provider/types/tool-index-entry.type.ts
@@ -1,4 +1,4 @@
-import { type ToolCategory } from 'twenty-shared/ai';
+import { type ToolCategory, type ToolWidgetName } from 'twenty-shared/ai';
import { type ToolExecutionRef } from 'src/engine/core-modules/tool-provider/types/tool-execution-ref.type';
@@ -11,4 +11,8 @@ export type ToolIndexEntry = {
objectName?: string;
operation?: string;
icon?: string;
+ // Built-in widget that renders this tool's calls in the AI chat.
+ widgetName?: ToolWidgetName;
+ // App-supplied front component that renders this tool's calls instead.
+ frontComponentId?: string;
};
diff --git a/packages/twenty-shared/src/ai/index.ts b/packages/twenty-shared/src/ai/index.ts
index 1ab0f634a96..910a20b59d2 100644
--- a/packages/twenty-shared/src/ai/index.ts
+++ b/packages/twenty-shared/src/ai/index.ts
@@ -60,6 +60,12 @@ export type {
export type { ExtendedUIMessagePart } from './types/ExtendedUIMessagePart';
export type { ModelConfiguration } from './types/model-configuration.type';
export type { NavigateAppToolOutput } from './types/NavigateAppToolOutput';
+export type { ToolWidgetName, ToolRecordReference } from './types/ToolWidget';
+export {
+ RECORDS_TOOL_WIDGET_NAME,
+ TOOL_WIDGET_NAMES,
+ isToolWidgetName,
+} from './types/ToolWidget';
export { formatRecordReference } from './utils/format-record-reference.util';
export { formatSkillReference } from './utils/format-skill-reference.util';
export { getAiModelTierFromModelId } from './utils/get-ai-model-tier-from-model-id.util';
diff --git a/packages/twenty-shared/src/ai/types/ToolWidget.ts b/packages/twenty-shared/src/ai/types/ToolWidget.ts
new file mode 100644
index 00000000000..89e02b5b685
--- /dev/null
+++ b/packages/twenty-shared/src/ai/types/ToolWidget.ts
@@ -0,0 +1,19 @@
+// A tool names the widget that renders its calls in the AI chat. The client
+// resolves the name it is given instead of switching on tool names, so a tool
+// can change its presentation without a front-end release.
+export const RECORDS_TOOL_WIDGET_NAME = 'records';
+
+export const TOOL_WIDGET_NAMES = [RECORDS_TOOL_WIDGET_NAME] as const;
+
+export type ToolWidgetName = (typeof TOOL_WIDGET_NAMES)[number];
+
+export const isToolWidgetName = (value: string): value is ToolWidgetName =>
+ TOOL_WIDGET_NAMES.includes(value as ToolWidgetName);
+
+// Emitted by every record tool on its output, and rendered by the `records`
+// widget as links to the real record.
+export type ToolRecordReference = {
+ objectNameSingular: string;
+ recordId: string;
+ displayName: string;
+};
diff --git a/packages/twenty-shared/src/application/toolTriggerSettingsType.ts b/packages/twenty-shared/src/application/toolTriggerSettingsType.ts
index 68d294dea6e..d26177f5435 100644
--- a/packages/twenty-shared/src/application/toolTriggerSettingsType.ts
+++ b/packages/twenty-shared/src/application/toolTriggerSettingsType.ts
@@ -6,4 +6,7 @@ import { type InputJsonSchema } from '@/logic-function/input-json-schema.type';
// builder fills it in by inferring from the handler source code when omitted.
export type ToolTriggerSettings = {
inputSchema?: InputJsonSchema;
+ // Universal identifier of a front component that renders this tool's calls
+ // in the AI chat. Resolved to a front component id when descriptors are built.
+ frontComponentUniversalIdentifier?: string;
};