Skip to content

The open alternative to Salesforce, designed for AI.

Let app-driven agents reach dashboard and workflow tools (#25685)

Abdul Rahmandf76a28e700f
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/open-ended-agent-registry-tool-categories.const.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/open-ended-agent-registry-tool-categories.const.ts
new file mode 100644
index 00000000000..22e63232bdb
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/constants/open-ended-agent-registry-tool-categories.const.ts
@@ -0,0 +1,8 @@
+import { ToolCategory } from 'twenty-shared/ai';
+
+export const OPEN_ENDED_AGENT_REGISTRY_TOOL_CATEGORIES: ToolCategory[] = [
+ ToolCategory.DATABASE_CRUD,
+ ToolCategory.ACTION,
+ ToolCategory.DASHBOARD,
+ ToolCategory.WORKFLOW,
+];
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts
index bca2f2828d8..acee738bf6d 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-async-executor.service.ts
@@ -43,6 +43,7 @@ import { isToolOutputSuccessful } from 'src/engine/core-modules/tool-provider/ut
import { OUTPUT_NAVIGATION_TOOL_NAMES } from 'src/engine/core-modules/tool/tools/output-navigation-tool/constants/output-navigation-tool-names.constant';
import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
+import { OPEN_ENDED_AGENT_REGISTRY_TOOL_CATEGORIES } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/open-ended-agent-registry-tool-categories.const';
import { WORKFLOW_AGENT_REGISTRY_TOOL_CATEGORIES } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/workflow-agent-registry-tool-categories.const';
import { type AgentExecutionResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-execution-result.type';
import { type AgentToolLoadingStrategy } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-tool-loading-strategy.type';
@@ -215,7 +216,9 @@ export class AgentAsyncExecutorService {
{ userId, userWorkspaceId, rolePermissionConfig },
);
- const allowedCategories = new Set(WORKFLOW_AGENT_REGISTRY_TOOL_CATEGORIES);
+ const allowedCategories = new Set(
+ OPEN_ENDED_AGENT_REGISTRY_TOOL_CATEGORIES,
+ );
const excludedToolNames = new Set<string>(OUTPUT_NAVIGATION_TOOL_NAMES);
const catalog = fullCatalog.filter(
diff --git a/packages/twenty-server/src/modules/dashboard/tools/__tests__/create-complete-dashboard.tool.spec.ts b/packages/twenty-server/src/modules/dashboard/tools/__tests__/create-complete-dashboard.tool.spec.ts
new file mode 100644
index 00000000000..4f2ca9b3f01
--- /dev/null
+++ b/packages/twenty-server/src/modules/dashboard/tools/__tests__/create-complete-dashboard.tool.spec.ts
@@ -0,0 +1,69 @@
+import { createCreateCompleteDashboardTool } from 'src/modules/dashboard/tools/create-complete-dashboard.tool';
+import { type DashboardToolDependencies } from 'src/modules/dashboard/tools/types/dashboard-tool-dependencies.type';
+
+const WORKSPACE_ID = '20202020-aaaa-4d02-bf25-6aeccf7ea419';
+const ROLE_ID = '20202020-bbbb-4d02-bf25-6aeccf7ea419';
+const PAGE_LAYOUT_ID = '20202020-cccc-4d02-bf25-6aeccf7ea419';
+
+const buildDeps = ({ insert }: { insert: jest.Mock }) => {
+ const destroy = jest.fn().mockResolvedValue(true);
+
+ return {
+ destroy,
+ deps: {
+ pageLayoutService: {
+ create: jest.fn().mockResolvedValue({ id: PAGE_LAYOUT_ID }),
+ destroy,
+ },
+ pageLayoutTabService: {
+ create: jest.fn().mockResolvedValue({ id: 'tab-id' }),
+ },
+ recordPositionService: {
+ buildRecordPosition: jest.fn().mockResolvedValue(1),
+ },
+ workspaceOrmManager: {
+ executeInWorkspaceContext: jest
+ .fn()
+ .mockImplementation(async (fn) => fn()),
+ getRepository: jest.fn().mockReturnValue({ insert }),
+ },
+ } as unknown as DashboardToolDependencies,
+ };
+};
+
+describe('createCreateCompleteDashboardTool', () => {
+ it('destroys the page layout when the permission-checked dashboard insert fails', async () => {
+ const insert = jest.fn().mockRejectedValue(new Error('Permission denied'));
+ const { deps, destroy } = buildDeps({ insert });
+
+ const tool = createCreateCompleteDashboardTool(deps, {
+ workspaceId: WORKSPACE_ID,
+ rolePermissionConfig: { intersectionOf: [ROLE_ID] },
+ });
+
+ const result = await tool.execute({ title: 'Revenue' });
+
+ expect(result.success).toBe(false);
+ expect(destroy).toHaveBeenCalledTimes(1);
+ expect(destroy).toHaveBeenCalledWith({
+ id: PAGE_LAYOUT_ID,
+ workspaceId: WORKSPACE_ID,
+ isLinkedDashboardAlreadyDestroyed: true,
+ });
+ });
+
+ it('leaves the page layout in place when the dashboard insert succeeds', async () => {
+ const insert = jest.fn().mockResolvedValue(undefined);
+ const { deps, destroy } = buildDeps({ insert });
+
+ const tool = createCreateCompleteDashboardTool(deps, {
+ workspaceId: WORKSPACE_ID,
+ rolePermissionConfig: { intersectionOf: [ROLE_ID] },
+ });
+
+ const result = await tool.execute({ title: 'Revenue' });
+
+ expect(result.success).toBe(true);
+ expect(destroy).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/twenty-server/src/modules/dashboard/tools/__tests__/get-dashboard.tool.spec.ts b/packages/twenty-server/src/modules/dashboard/tools/__tests__/get-dashboard.tool.spec.ts
index a2ebce459d3..5658405c5e3 100644
--- a/packages/twenty-server/src/modules/dashboard/tools/__tests__/get-dashboard.tool.spec.ts
+++ b/packages/twenty-server/src/modules/dashboard/tools/__tests__/get-dashboard.tool.spec.ts
@@ -143,6 +143,7 @@ describe('get_dashboard tool', () => {
>,
{
workspaceId: WORKSPACE_ID,
+ rolePermissionConfig: { shouldBypassPermissionChecks: true },
},
);
diff --git a/packages/twenty-server/src/modules/dashboard/tools/__tests__/list-dashboards.tool.spec.ts b/packages/twenty-server/src/modules/dashboard/tools/__tests__/list-dashboards.tool.spec.ts
new file mode 100644
index 00000000000..be3c25f98ad
--- /dev/null
+++ b/packages/twenty-server/src/modules/dashboard/tools/__tests__/list-dashboards.tool.spec.ts
@@ -0,0 +1,42 @@
+import { createListDashboardsTool } from 'src/modules/dashboard/tools/list-dashboards.tool';
+import { type DashboardToolDependencies } from 'src/modules/dashboard/tools/types/dashboard-tool-dependencies.type';
+
+const WORKSPACE_ID = '20202020-aaaa-4d02-bf25-6aeccf7ea419';
+const ROLE_ID = '20202020-bbbb-4d02-bf25-6aeccf7ea419';
+
+const buildDeps = () => {
+ const getRepository = jest.fn().mockReturnValue({
+ find: jest.fn().mockResolvedValue([]),
+ });
+
+ return {
+ getRepository,
+ deps: {
+ workspaceOrmManager: {
+ executeInWorkspaceContext: jest
+ .fn()
+ .mockImplementation(async (fn) => fn()),
+ getRepository,
+ },
+ } as unknown as Pick<DashboardToolDependencies, 'workspaceOrmManager'>,
+ };
+};
+
+describe('createListDashboardsTool', () => {
+ it('reads dashboards through the caller role rather than bypassing permissions', async () => {
+ const { deps, getRepository } = buildDeps();
+
+ const tool = createListDashboardsTool(deps, {
+ workspaceId: WORKSPACE_ID,
+ rolePermissionConfig: { intersectionOf: [ROLE_ID] },
+ });
+
+ const result = await tool.execute({});
+
+ expect(result.success).toBe(true);
+ expect(getRepository).toHaveBeenCalledTimes(1);
+ expect(getRepository).toHaveBeenCalledWith('dashboard', {
+ intersectionOf: [ROLE_ID],
+ });
+ });
+});
diff --git a/packages/twenty-server/src/modules/dashboard/tools/create-complete-dashboard.tool.ts b/packages/twenty-server/src/modules/dashboard/tools/create-complete-dashboard.tool.ts
index ec026dfac58..a88cc249772 100644
--- a/packages/twenty-server/src/modules/dashboard/tools/create-complete-dashboard.tool.ts
+++ b/packages/twenty-server/src/modules/dashboard/tools/create-complete-dashboard.tool.ts
@@ -14,7 +14,7 @@ import {
widgetTypeSchema,
} from 'src/modules/dashboard/tools/schemas/widget.schema';
import {
- type DashboardToolContext,
+ type DashboardToolContextWithPermissions,
type DashboardToolDependencies,
} from 'src/modules/dashboard/tools/types/dashboard-tool-dependencies.type';
import { type WidgetConfigurationInput } from 'src/modules/dashboard/tools/types/widget-configuration-input.type';
@@ -56,7 +56,7 @@ const createCompleteDashboardSchema = z.object({
export const createCreateCompleteDashboardTool = (
deps: DashboardToolDependencies,
- context: DashboardToolContext,
+ context: DashboardToolContextWithPermissions,
) => ({
name: 'create_complete_dashboard' as const,
description: `Create a dashboard with layout, tab, and widgets.
@@ -177,12 +177,12 @@ AGGREGATION OPERATIONS: COUNT, SUM, AVG, MIN, MAX, COUNT_EMPTY, COUNT_NOT_EMPTY`
}
}
- const dashboardId = await createDashboardRecord(
+ const dashboardId = await createDashboardRecordOrRollBackLayout({
deps,
context,
- parameters.title,
- pageLayout.id,
- );
+ title: parameters.title,
+ pageLayoutId: pageLayout.id,
+ });
const result = {
dashboardId,
@@ -230,20 +230,43 @@ AGGREGATION OPERATIONS: COUNT, SUM, AVG, MIN, MAX, COUNT_EMPTY, COUNT_NOT_EMPTY`
},
});
-const createDashboardRecord = async (
- deps: DashboardToolDependencies,
- context: DashboardToolContext,
- title: string,
- pageLayoutId: string,
+type CreateDashboardRecordParams = {
+ deps: DashboardToolDependencies;
+ context: DashboardToolContextWithPermissions;
+ title: string;
+ pageLayoutId: string;
+};
+
+const createDashboardRecordOrRollBackLayout = async (
+ params: CreateDashboardRecordParams,
): Promise<string> => {
+ try {
+ return await createDashboardRecord(params);
+ } catch (error) {
+ await params.deps.pageLayoutService
+ .destroy({
+ id: params.pageLayoutId,
+ workspaceId: params.context.workspaceId,
+ isLinkedDashboardAlreadyDestroyed: true,
+ })
+ .catch(() => undefined);
+
+ throw error;
+ }
+};
+
+const createDashboardRecord = async ({
+ deps,
+ context,
+ title,
+ pageLayoutId,
+}: CreateDashboardRecordParams): Promise<string> => {
const authContext = buildSystemAuthContext(context.workspaceId);
return deps.workspaceOrmManager.executeInWorkspaceContext(async () => {
const dashboardRepository = deps.workspaceOrmManager.getRepository(
'dashboard',
- {
- shouldBypassPermissionChecks: true,
- },
+ context.rolePermissionConfig,
);
const position = await deps.recordPositionService.buildRecordPosition({
diff --git a/packages/twenty-server/src/modules/dashboard/tools/get-dashboard.tool.ts b/packages/twenty-server/src/modules/dashboard/tools/get-dashboard.tool.ts
index f56414f2e0f..75ae64dfdb8 100644
--- a/packages/twenty-server/src/modules/dashboard/tools/get-dashboard.tool.ts
+++ b/packages/twenty-server/src/modules/dashboard/tools/get-dashboard.tool.ts
@@ -8,7 +8,7 @@ import { findActiveFlatFieldMetadataById } from 'src/engine/metadata-modules/pag
import { isChartReferencingFieldInConfiguration } from 'src/engine/metadata-modules/page-layout-widget/utils/is-chart-referencing-field-in-configuration.util';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import {
- type DashboardToolContext,
+ type DashboardToolContextWithPermissions,
type DashboardToolDependencies,
} from 'src/modules/dashboard/tools/types/dashboard-tool-dependencies.type';
import { buildResolvedGroupBy } from 'src/modules/dashboard/tools/utils/build-resolved-group-by.util';
@@ -22,7 +22,7 @@ export const createGetDashboardTool = (
DashboardToolDependencies,
'pageLayoutService' | 'workspaceOrmManager' | 'flatEntityMapsCacheService'
>,
- context: DashboardToolContext,
+ context: DashboardToolContextWithPermissions,
) => ({
name: 'get_dashboard' as const,
description: `Get a dashboard with its full layout structure including tabs and widgets.`,
@@ -70,9 +70,10 @@ export const createGetDashboardTool = (
const dashboard =
await deps.workspaceOrmManager.executeInWorkspaceContext(async () => {
- const repo = deps.workspaceOrmManager.getRepository('dashboard', {
- shouldBypassPermissionChecks: true,
- });
+ const repo = deps.workspaceOrmManager.getRepository(
+ 'dashboard',
+ context.rolePermissionConfig,
+ );
return repo.findOne({ where: { id: parameters.dashboardId } });
}, authContext);
diff --git a/packages/twenty-server/src/modules/dashboard/tools/list-dashboards.tool.ts b/packages/twenty-server/src/modules/dashboard/tools/list-dashboards.tool.ts
index bf56be6344f..f49f101e521 100644
--- a/packages/twenty-server/src/modules/dashboard/tools/list-dashboards.tool.ts
+++ b/packages/twenty-server/src/modules/dashboard/tools/list-dashboards.tool.ts
@@ -2,7 +2,7 @@ import { z } from 'zod';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import {
- type DashboardToolContext,
+ type DashboardToolContextWithPermissions,
type DashboardToolDependencies,
} from 'src/modules/dashboard/tools/types/dashboard-tool-dependencies.type';
@@ -18,7 +18,7 @@ const listDashboardsSchema = z.object({
export const createListDashboardsTool = (
deps: Pick<DashboardToolDependencies, 'workspaceOrmManager'>,
- context: DashboardToolContext,
+ context: DashboardToolContextWithPermissions,
) => ({
name: 'list_dashboards' as const,
description: `List all dashboards in the workspace. Use get_dashboard to retrieve full layout structure.`,
@@ -30,9 +30,10 @@ export const createListDashboardsTool = (
const dashboards =
await deps.workspaceOrmManager.executeInWorkspaceContext(async () => {
- const repo = deps.workspaceOrmManager.getRepository('dashboard', {
- shouldBypassPermissionChecks: true,
- });
+ const repo = deps.workspaceOrmManager.getRepository(
+ 'dashboard',
+ context.rolePermissionConfig,
+ );
return repo.find({ take: limit, order: { position: 'ASC' } });
}, authContext);
diff --git a/packages/twenty-server/src/modules/dashboard/tools/services/dashboard-tool.workspace-service.ts b/packages/twenty-server/src/modules/dashboard/tools/services/dashboard-tool.workspace-service.ts
index dfc2b549ba5..036b89a2e72 100644
--- a/packages/twenty-server/src/modules/dashboard/tools/services/dashboard-tool.workspace-service.ts
+++ b/packages/twenty-server/src/modules/dashboard/tools/services/dashboard-tool.workspace-service.ts
@@ -45,16 +45,23 @@ export class DashboardToolWorkspaceService {
generateDashboardTools(
workspaceId: string,
- _rolePermissionConfig: RolePermissionConfig,
+ rolePermissionConfig: RolePermissionConfig,
): ToolSet {
const context = { workspaceId };
+ const contextWithPermissions = { workspaceId, rolePermissionConfig };
const createCompleteDashboard = createCreateCompleteDashboardTool(
this.deps,
- context,
+ contextWithPermissions,
+ );
+ const listDashboards = createListDashboardsTool(
+ this.deps,
+ contextWithPermissions,
+ );
+ const getDashboard = createGetDashboardTool(
+ this.deps,
+ contextWithPermissions,
);
- const listDashboards = createListDashboardsTool(this.deps, context);
- const getDashboard = createGetDashboardTool(this.deps, context);
const addDashboardTab = createAddDashboardTabTool(this.deps, context);
const addDashboardWidget = createAddDashboardWidgetTool(this.deps, context);
const updateDashboardWidget = createUpdateDashboardWidgetTool(