Skip to content

The open alternative to Salesforce, designed for AI.

Pull and apply the builder's AI agent step settings (#29365)

Félix Malfait0f6e07a8832c
diff --git a/packages/twenty-agent-skills/references/develop-app/workflows.md b/packages/twenty-agent-skills/references/develop-app/workflows.md
index 6062062ae5e..78ddd3ed080 100644
--- a/packages/twenty-agent-skills/references/develop-app/workflows.md
+++ b/packages/twenty-agent-skills/references/develop-app/workflows.md
@@ -20,6 +20,11 @@ Workflows created at runtime through the workspace API are a `Workflow` plus at
- `position: null` means no position. `outputSchema: {}` means an empty schema; omit `outputSchema` to derive it.
- `twenty app pull` on the workspace Custom application writes builder workflows in this form, keeping the builder's trigger name, positions and output schemas.
+## AI Agent Steps
+
+- `AI_AGENT` runs one of the app's agents, named with `agentUniversalIdentifier`, and takes the step's `prompt`, `responseFormat`, `humanInputInstructions`, `conversation` and `workspaceMemberId` (the member the agent's conversation is with).
+- A step can only name an agent the app exports: `twenty app pull` reports a workflow on any other agent as unsupported.
+
## Email and Calendar Steps
- `SEND_EMAIL`, `DRAFT_EMAIL` and `CREATE_CALENDAR_EVENT` name their account with exactly one of `connectedAccountId` or `connectedAccountHandle`.
diff --git a/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx b/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx
index c59acb00d3e..4fe7c278c5d 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/workflows.mdx
@@ -107,6 +107,7 @@ Their inputs match the steps of the [workflow builder](/user-guide/workflows/cap
- Filter operands take `ViewFilterOperand` values, exported by `twenty-sdk/define`. Filter group operators take string values such as `'AND'` or `'OR'`.
- Form fields of type `RECORD` name their object with `settings.objectUniversalIdentifier`. Fields of type `SELECT` and `MULTI_SELECT` offer the options of an existing field of the same type, named with `settings.selectedFieldUniversalIdentifier`.
- To run your own code, use a `LOGIC_FUNCTION` step. The builder's Code step is not available.
+- `AI_AGENT` steps run one of your app's agents, named with `agentUniversalIdentifier`, and take the builder's step settings: `prompt`, `responseFormat`, `humanInputInstructions`, `conversation`, and `workspaceMemberId` for the member the agent's conversation is with.
- Email and calendar steps run only if your app's role has the matching permission flag: `SystemPermissionFlag.SEND_EMAIL_TOOL` for `SEND_EMAIL` and `DRAFT_EMAIL`, `SystemPermissionFlag.CREATE_CALENDAR_EVENT_TOOL` for `CREATE_CALENDAR_EVENT`. The workspace grants it by installing the app, and the step can then use any connected account in the workspace, like a workflow built in the app.
- Connected account ids are local to each workspace, so don't hardcode one. Name the account in one of two ways:
- `connectedAccountHandle`: the account's email address, such as `'[email protected]'`. When the app is synced, the handle resolves to an account with that address that is shared with the workspace (`WORKSPACE` scope) and can run the step. A member's private account can't be named by handle: pull keeps it as its `connectedAccountId`. If several accounts share the address, for example one mailbox connected by two members or through two providers, the oldest one wins. If none matches, the sync fails and names the handle. This is not `fromHandle`, which picks an alias to send from.
diff --git a/packages/twenty-front-component-renderer/scripts/front-component-stories/build-source-examples.ts b/packages/twenty-front-component-renderer/scripts/front-component-stories/build-source-examples.ts
index c4086e035b9..d1a5e5edfce 100644
--- a/packages/twenty-front-component-renderer/scripts/front-component-stories/build-source-examples.ts
+++ b/packages/twenty-front-component-renderer/scripts/front-component-stories/build-source-examples.ts
@@ -179,12 +179,37 @@ const collectBundleSizes = (): BundleSizeEntry[] =>
};
});
+// esbuild memory grows with entries x import graph and is only released when its service stops
+const ENTRY_POINT_BATCH_SIZE = 16;
+
+const buildInBatches = async (
+ entryPoints: Record<string, string>,
+ options: esbuild.BuildOptions,
+): Promise<void> => {
+ const entryPointList = Object.entries(entryPoints);
+
+ for (
+ let batchStart = 0;
+ batchStart < entryPointList.length;
+ batchStart += ENTRY_POINT_BATCH_SIZE
+ ) {
+ const batchEntryPoints = Object.fromEntries(
+ entryPointList.slice(batchStart, batchStart + ENTRY_POINT_BATCH_SIZE),
+ );
+
+ try {
+ await esbuild.build({ ...options, entryPoints: batchEntryPoints });
+ } finally {
+ await esbuild.stop();
+ }
+ }
+};
+
const buildSourceExamples = async (): Promise<void> => {
const entryPoints = resolveEntryPoints();
const tsconfigPath = path.join(dirname, '../../tsconfig.json');
const commonOptions: esbuild.BuildOptions = {
- entryPoints,
bundle: true,
splitting: false,
format: 'esm',
@@ -200,7 +225,7 @@ const buildSourceExamples = async (): Promise<void> => {
fs.mkdirSync(exampleSourcesBuiltDir, { recursive: true });
- await esbuild.build({
+ await buildInBatches(entryPoints, {
...commonOptions,
outdir: exampleSourcesBuiltDir,
plugins: getFrontComponentBuildPlugins(),
@@ -212,7 +237,7 @@ const buildSourceExamples = async (): Promise<void> => {
fs.mkdirSync(exampleSourcesBuiltPreactDir, { recursive: true });
- await esbuild.build({
+ await buildInBatches(entryPoints, {
...commonOptions,
outdir: exampleSourcesBuiltPreactDir,
plugins: getFrontComponentBuildPlugins({ usePreact: true }),
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-flat-workflow-to-workflow-manifest.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-flat-workflow-to-workflow-manifest.util.spec.ts
index 66d94d2d4f5..da1bbaaf774 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-flat-workflow-to-workflow-manifest.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/converters/__tests__/from-flat-workflow-to-workflow-manifest.util.spec.ts
@@ -739,13 +739,13 @@ describe('fromFlatWorkflowToWorkflowManifest', () => {
'workflow step on a logic function that is not a workflow action of this application',
},
{
- description: 'an agent of another application',
+ description: 'an agent the application does not export',
step: buildStep({ type: 'AI_AGENT' }),
exportReferences: {
...references,
agentIdByUniversalIdentifier: new Map(),
},
- reason: 'workflow step on an agent of another application',
+ reason: 'workflow step on an agent the application does not export',
},
])(
'reports a step on $description',
@@ -1075,6 +1075,38 @@ describe('fromFlatWorkflowToWorkflowManifest on versions built in the workflow b
expect(step.outputSchema).toEqual({});
});
+ it('exports an agent step with the conversation and member the builder stores', () => {
+ const input = {
+ prompt: '{{trigger.properties.after.summary}}',
+ responseFormat: { type: 'text' },
+ humanInputInstructions: 'Ask when the company is unclear',
+ conversation: {
+ key: 'mail:{{trigger.properties.after.id}}',
+ scope: 'KEY',
+ },
+ workspaceMemberId: WORKSPACE_MEMBER_ID,
+ };
+ const [step] = exportBuilderVersion({
+ triggers: [recordCreatedTrigger()],
+ steps: [
+ createRecordStep({
+ name: 'Analyse the thread',
+ type: 'AI_AGENT',
+ settings: {
+ outputSchema: {},
+ errorHandlingOptions: BUILDER_ERROR_HANDLING_OPTIONS,
+ input: { ...input, agentId: 'agent-id' },
+ },
+ }),
+ ],
+ }).steps;
+
+ expect(step).toMatchObject({
+ type: 'AI_AGENT',
+ input: { ...input, agentUniversalIdentifier: AGENT_ID },
+ });
+ });
+
it('exports an email step written without files', () => {
const input = {
connectedAccountId: '{{trigger.properties.after.accountId}}',
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-manifest-export.service.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-manifest-export.service.ts
index a825513461a..cf35b062739 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-manifest-export.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/services/application-manifest-export.service.ts
@@ -164,25 +164,28 @@ export class ApplicationManifestExportService {
await this.connectedAccountMetadataService.findAllForWorkflowStepResolution(
{ workspaceId },
);
+ const { agents, coverage: agentsCoverage } = reconstructAgentsManifest({
+ applicationAllFlatEntityMaps,
+ allFlatEntityMaps,
+ resolvableRoleUniversalIdentifiers:
+ getResolvableReferenceUniversalIdentifiers({
+ coverage: rolesCoverage,
+ metadataName: 'role',
+ }),
+ });
const { workflows, coverage: workflowsCoverage } =
reconstructWorkflowsManifest({
applicationAllFlatEntityMaps,
allFlatEntityMaps,
exportedObjectUniversalIdentifiers,
resolvableFieldUniversalIdentifiers,
+ exportedAgentUniversalIdentifiers: new Set(
+ agents.map(({ universalIdentifier }) => universalIdentifier),
+ ),
isWorkspaceCustomApplication:
flatApplication.id === workspaceCustomFlatApplication.id,
connectedAccounts,
});
- const { agents, coverage: agentsCoverage } = reconstructAgentsManifest({
- applicationAllFlatEntityMaps,
- allFlatEntityMaps,
- resolvableRoleUniversalIdentifiers:
- getResolvableReferenceUniversalIdentifiers({
- coverage: rolesCoverage,
- metadataName: 'role',
- }),
- });
const { skills, coverage: skillsCoverage } = reconstructSkillsManifest({
applicationAllFlatEntityMaps,
});
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-workflow-manifest-export-references.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-workflow-manifest-export-references.util.spec.ts
new file mode 100644
index 00000000000..03ce9402a87
--- /dev/null
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/build-workflow-manifest-export-references.util.spec.ts
@@ -0,0 +1,61 @@
+import { addAllFlatEntitiesToFlatEntityMaps } from 'src/engine/core-modules/application/application-manifest/utils/__tests__/add-all-flat-entities-to-flat-entity-maps.test-util';
+import { buildWorkflowManifestExportReferences } from 'src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-export-references.util';
+import { fromAgentManifestToUniversalFlatAgent } from 'src/engine/core-modules/application/utils/from-agent-manifest-to-universal-flat-agent.util';
+import { type FlatAgent } from 'src/engine/metadata-modules/flat-agent/types/flat-agent.type';
+import { createEmptyAllFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/constant/create-empty-all-flat-entity-maps.constant';
+
+const EXPORTED_AGENT_UNIVERSAL_IDENTIFIER =
+ '30000000-0000-4000-8000-000000000001';
+const UNEXPORTED_AGENT_UNIVERSAL_IDENTIFIER =
+ '30000000-0000-4000-8000-000000000002';
+
+const buildFlatAgent = (universalIdentifier: string): FlatAgent => ({
+ ...fromAgentManifestToUniversalFlatAgent({
+ agentManifest: {
+ universalIdentifier,
+ name: `agent-${universalIdentifier.slice(-1)}`,
+ label: 'Agent',
+ prompt: 'Help the user.',
+ responseFormat: { type: 'text' },
+ },
+ applicationUniversalIdentifier: '10000000-0000-4000-8000-000000000001',
+ now: '2026-10-31T10:00:00.000Z',
+ }),
+ id: `${universalIdentifier}-id`,
+ workspaceId: 'workspace-id',
+ applicationId: 'application-id',
+});
+
+describe('buildWorkflowManifestExportReferences', () => {
+ it('resolves only the agents the application exports, the ones apply will know', () => {
+ const maps = createEmptyAllFlatEntityMaps();
+ const applicationAllFlatEntityMaps = {
+ ...maps,
+ flatAgentMaps: addAllFlatEntitiesToFlatEntityMaps({
+ flatEntities: [
+ buildFlatAgent(EXPORTED_AGENT_UNIVERSAL_IDENTIFIER),
+ buildFlatAgent(UNEXPORTED_AGENT_UNIVERSAL_IDENTIFIER),
+ ],
+ flatEntityMaps: maps.flatAgentMaps,
+ }),
+ };
+
+ const references = buildWorkflowManifestExportReferences({
+ applicationAllFlatEntityMaps,
+ allFlatEntityMaps: applicationAllFlatEntityMaps,
+ exportedObjectUniversalIdentifiers: new Set(),
+ resolvableFieldUniversalIdentifiers: new Set(),
+ exportedAgentUniversalIdentifiers: new Set([
+ EXPORTED_AGENT_UNIVERSAL_IDENTIFIER,
+ ]),
+ canReferenceOtherApplicationsWorkflowActions: false,
+ });
+
+ expect([
+ ...(references.agentIdByUniversalIdentifier?.keys() ?? []),
+ ]).toEqual([EXPORTED_AGENT_UNIVERSAL_IDENTIFIER]);
+ expect([
+ ...(references.agentOutputSchemaByUniversalIdentifier?.keys() ?? []),
+ ]).toEqual([EXPORTED_AGENT_UNIVERSAL_IDENTIFIER]);
+ });
+});
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/reconstruct-workflows-manifest.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/reconstruct-workflows-manifest.util.spec.ts
index 97383318d90..3173b68fdc3 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/reconstruct-workflows-manifest.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/__tests__/reconstruct-workflows-manifest.util.spec.ts
@@ -253,6 +253,7 @@ const reconstruct = ({
}),
exportedObjectUniversalIdentifiers: new Set([TICKET_UID]),
resolvableFieldUniversalIdentifiers: new Set(),
+ exportedAgentUniversalIdentifiers: new Set(),
isWorkspaceCustomApplication,
connectedAccounts: [],
});
@@ -417,6 +418,7 @@ describe('reconstructWorkflowsManifest', () => {
allFlatEntityMaps: mapsWithWorkflow,
exportedObjectUniversalIdentifiers: new Set([TICKET_UID]),
resolvableFieldUniversalIdentifiers: new Set(),
+ exportedAgentUniversalIdentifiers: new Set(),
isWorkspaceCustomApplication: false,
connectedAccounts: [],
}).coverage,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-export-references.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-export-references.util.ts
index 3d4515e93c0..cab748838fb 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-export-references.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-export-references.util.ts
@@ -16,6 +16,7 @@ export const buildWorkflowManifestExportReferences = ({
allFlatEntityMaps,
exportedObjectUniversalIdentifiers,
resolvableFieldUniversalIdentifiers,
+ exportedAgentUniversalIdentifiers,
canReferenceOtherApplicationsWorkflowActions,
}: {
applicationAllFlatEntityMaps: Pick<
@@ -31,6 +32,7 @@ export const buildWorkflowManifestExportReferences = ({
>;
exportedObjectUniversalIdentifiers: ReadonlySet<string>;
resolvableFieldUniversalIdentifiers: ReadonlySet<string>;
+ exportedAgentUniversalIdentifiers: ReadonlySet<string>;
canReferenceOtherApplicationsWorkflowActions: boolean;
}): WorkflowManifestReferences => {
const objectByUniversalIdentifier = new Map<
@@ -125,9 +127,14 @@ export const buildWorkflowManifestExportReferences = ({
}
}
+ // Apply only knows the agents of the manifest, so a step on any other agent can't be exported
for (const flatAgent of Object.values(
applicationAllFlatEntityMaps.flatAgentMaps.byUniversalIdentifier,
- ).filter(isDefined)) {
+ )
+ .filter(isDefined)
+ .filter((flatAgent) =>
+ exportedAgentUniversalIdentifiers.has(flatAgent.universalIdentifier),
+ )) {
agentIdByUniversalIdentifier.set(
flatAgent.universalIdentifier,
flatAgent.id,
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-inverse-reference-resolvers.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-inverse-reference-resolvers.util.ts
index 7b939e9fa93..6650e046711 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-inverse-reference-resolvers.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/build-workflow-manifest-inverse-reference-resolvers.util.ts
@@ -165,7 +165,7 @@ export const buildWorkflowManifestInverseReferenceResolvers = (
typeof agentId === 'string'
? agentUniversalIdentifierById.get(agentId)
: undefined,
- reason: 'workflow step on an agent of another application',
+ reason: 'workflow step on an agent the application does not export',
});
// Values that are not a shared account of the workspace (variables, member ids, private accounts) are kept as they are
diff --git a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/reconstruct-workflows-manifest.util.ts b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/reconstruct-workflows-manifest.util.ts
index d05665699ea..8b6ffae2d37 100644
--- a/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/reconstruct-workflows-manifest.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/application/application-manifest/utils/reconstruct-workflows-manifest.util.ts
@@ -88,6 +88,7 @@ export const reconstructWorkflowsManifest = ({
allFlatEntityMaps,
exportedObjectUniversalIdentifiers,
resolvableFieldUniversalIdentifiers,
+ exportedAgentUniversalIdentifiers,
isWorkspaceCustomApplication,
connectedAccounts,
}: {
@@ -95,6 +96,7 @@ export const reconstructWorkflowsManifest = ({
allFlatEntityMaps: AllFlatEntityMaps;
exportedObjectUniversalIdentifiers: ReadonlySet<string>;
resolvableFieldUniversalIdentifiers: ReadonlySet<string>;
+ exportedAgentUniversalIdentifiers: ReadonlySet<string>;
isWorkspaceCustomApplication: boolean;
connectedAccounts: readonly ConnectedAccountForWorkflowStepResolution[];
}): {
@@ -109,6 +111,7 @@ export const reconstructWorkflowsManifest = ({
allFlatEntityMaps,
exportedObjectUniversalIdentifiers,
resolvableFieldUniversalIdentifiers,
+ exportedAgentUniversalIdentifiers,
canReferenceOtherApplicationsWorkflowActions:
isWorkspaceCustomApplication,
}),
diff --git a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
index c45171aff59..313e7291f78 100644
--- a/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
+++ b/packages/twenty-shared/src/application/__tests__/workflowManifestType.spec.ts
@@ -153,6 +153,20 @@ describe('workflow manifest step types', () => {
addConferencing: false,
},
},
+ {
+ type: 'AI_AGENT',
+ input: {
+ agentUniversalIdentifier: '66666666-6666-4666-8666-666666666666',
+ prompt: '{{trigger.properties.after.summary}}',
+ responseFormat: { type: 'text' },
+ humanInputInstructions: 'Ask when the company is unclear',
+ workspaceMemberId: ASSIGNEE,
+ conversation: {
+ key: 'mail:{{trigger.properties.after.threadId}}',
+ scope: 'KEY',
+ },
+ },
+ },
{
type: 'SEND_CHAT_MESSAGE',
input: {
diff --git a/packages/twenty-shared/src/application/workflowStepManifestType.ts b/packages/twenty-shared/src/application/workflowStepManifestType.ts
index 004dbc39636..6eb1e5761e1 100644
--- a/packages/twenty-shared/src/application/workflowStepManifestType.ts
+++ b/packages/twenty-shared/src/application/workflowStepManifestType.ts
@@ -2,8 +2,6 @@ import { z } from 'zod';
import { isDefined } from '@/utils/validation/isDefined';
-import { agentResponseFormatSchema } from '@/workflow/schemas/agent-response-format-schema';
-
import { workflowHttpRequestActionSettingsSchema } from '@/workflow/schemas/http-request-action-settings-schema';
import { workflowClassifyActionSettingsSchema } from '@/workflow/schemas/classify-action-settings-schema';
import { workflowIteratorActionSettingsSchema } from '@/workflow/schemas/iterator-action-settings-schema';
@@ -21,6 +19,7 @@ import { workflowFilterActionSettingsSchema } from '@/workflow/schemas/filter-ac
import { workflowIfElseActionSettingsSchema } from '@/workflow/schemas/if-else-action-settings-schema';
import { workflowSendEmailActionSettingsSchema } from '@/workflow/schemas/send-email-action-settings-schema';
import { workflowCreateCalendarEventActionSettingsSchema } from '@/workflow/schemas/create-calendar-event-action-settings-schema';
+import { workflowAiAgentActionSettingsSchema } from '@/workflow/schemas/ai-agent-action-settings-schema';
import { workflowSendChatMessageActionSettingsSchema } from '@/workflow/schemas/send-chat-message-action-settings-schema';
import { recordFilterGroupSchema } from '@/workflow/schemas/record-filter-group-schema';
import { recordFilterSchema } from '@/workflow/schemas/record-filter-schema';
@@ -255,11 +254,10 @@ const workflowStepManifestOptions = [
),
stepSchema(
'AI_AGENT',
- z.strictObject({
- agentUniversalIdentifier: z.uuid(),
- prompt: z.string(),
- responseFormat: agentResponseFormatSchema.optional(),
- }),
+ workflowAiAgentActionSettingsSchema.shape.input
+ .omit({ agentId: true })
+ .extend({ agentUniversalIdentifier: z.uuid() })
+ .strict(),
),
] as const;