The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Make agent run caller refs opaque to the engine (#27613)
Félix Malfaitb49c17624a62
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/crons/jobs/pending-wake-up-sweep.cron.job.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/crons/jobs/pending-wake-up-sweep.cron.job.tsindex 3d407ae95dd..121133b5c05 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/crons/jobs/pending-wake-up-sweep.cron.job.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/crons/jobs/pending-wake-up-sweep.cron.job.ts@@ -8,7 +8,6 @@ import { Process } from 'src/engine/core-modules/message-queue/decorators/procesimport { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator';import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';-import { type PendingWakeUpOwnerType } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type';export const PENDING_WAKE_UP_SWEEP_CRON_PATTERN = '*/5 * * * *';@@ -18,7 +17,7 @@ const OVERDUE_WAKE_UPS_BATCH_SIZE = 1000;type OverdueWakeUpRow = {id: string;workspaceId: string;- ownerType: PendingWakeUpOwnerType;+ ownerType: string;ownerId: string;};diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity.tsindex 97ba41ed9b3..25fce40ed1d 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity.ts@@ -11,7 +11,6 @@ import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';import { CREATE_PENDING_WAKE_UP_TABLE_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/create-pending-wake-up-table-upgrade-command-name.constant';import { ADD_PAYLOAD_TO_PENDING_WAKE_UP_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/add-payload-to-pending-wake-up-upgrade-command-name.constant';-import { type PendingWakeUpOwnerType } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type';import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';import { WorkspaceRelatedEntity } from 'src/engine/workspace-manager/types/workspace-related-entity.type';@@ -29,7 +28,7 @@ export class PendingWakeUpEntity extends WorkspaceRelatedEntity {id: string;@Column({ type: 'varchar' })- ownerType: PendingWakeUpOwnerType;+ ownerType: string;@Column({ type: 'uuid' })ownerId: string;diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service.tsindex fa9c4ed9745..bb5ce37d995 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service.ts@@ -1,11 +1,7 @@import { Injectable } from '@nestjs/common';import { type PendingWakeUpOwnerHandler } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';-import { type PendingWakeUpOwnerType } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type';import { HandlerRegistry } from 'src/engine/utils/handler-registry';@Injectable()-export class PendingWakeUpOwnerHandlerRegistryService extends HandlerRegistry<- PendingWakeUpOwnerType,- PendingWakeUpOwnerHandler-> {}+export class PendingWakeUpOwnerHandlerRegistryService extends HandlerRegistry<PendingWakeUpOwnerHandler> {}diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up.service.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up.service.tsindex cf9948939b2..6f8b05add6a 100644--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up.service.ts+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up.service.ts@@ -11,14 +11,13 @@ import { MessageQueueService } from 'src/engine/core-modules/message-queue/serviimport { RESUME_PENDING_WAKE_UP_JOB_NAME } from 'src/engine/core-modules/pending-wake-up/constants/resume-pending-wake-up-job-name.constant';import { PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';import { PendingWakeUpOwnerHandlerRegistryService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-owner-handler-registry.service';-import { type PendingWakeUpOwnerType } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type';import { type ResumePendingWakeUpJobData } from 'src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type';import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';// key tells apart the wake-ups one owner holds at once, like the steps of a workflow runtype PendingWakeUpOwner = {- type: PendingWakeUpOwnerType;+ type: string;id: string;key: string;};diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.tsdeleted file mode 100644index 01cae6b750f..00000000000--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-type.type.ts+++ /dev/null@@ -1 +0,0 @@-export type PendingWakeUpOwnerType = 'WORKFLOW_STEP' | 'AGENT_RUN';diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.tsindex dc668b87a3f..923a6317766 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service.ts@@ -1,11 +1,7 @@import { Injectable } from '@nestjs/common';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';import { type AgentRunCallerHandler } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type';import { HandlerRegistry } from 'src/engine/utils/handler-registry';@Injectable()-export class AgentRunCallerHandlerRegistryService extends HandlerRegistry<- AgentRunCaller['type'],- AgentRunCallerHandler-> {}+export class AgentRunCallerHandlerRegistryService extends HandlerRegistry<AgentRunCallerHandler> {}diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.tsindex 492de313c61..28dacb19405 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service.ts@@ -147,10 +147,8 @@ export class AgentRunSuspensionService {caller,}: {workspaceId: string;- caller: {- type: AgentRunCaller['type'];- ref: Partial<AgentRunCaller['ref']>;- };+ // matched by containment, so a partial ref releases every run it covers+ caller: AgentRunCaller;}): Promise<void> {const wakeUps = await this.wakeUpRepository.find(workspaceId, {where: {diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.tsindex b9ff8b02fd1..2c2235ec818 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run.service.ts@@ -14,7 +14,8 @@ import {type RunAgentThread,} from 'twenty-shared/application';import { type ActorMetadata } from 'twenty-shared/types';-import { isDefined } from 'twenty-shared/utils';+import { isDefined, isPlainObject } from 'twenty-shared/utils';+import { z } from 'zod';import { buildActorMetadataFromAuthContext } from 'src/engine/core-modules/actor/utils/build-actor-metadata-from-auth-context.util';import { buildCreatedByFromApplication } from 'src/engine/core-modules/actor/utils/build-created-by-from-application.util';@@ -54,14 +55,18 @@ type RunAgentServiceInput = {runAsWorkspaceMemberId?: string;};-type AgentApiRunCaller = Extract<AgentRunCaller, { type: 'AGENT_API_RUN' }>;+const agentApiRunCallerRefSchema = z.object({+ agentId: z.string(),+ runAsWorkspaceMemberId: z.string().nullable(),+ requestUserWorkspaceId: z.string().nullable(),+ // the request's own actor cannot be read back once it is over+ createdBy: z.custom<ActorMetadata>(isPlainObject),+});// Runs an agent for the runAgent API. A run that waits goes on later with the API call as its// caller, and its reply lands in its conversation@Injectable()-export class AgentRunService- implements AgentRunCallerHandler<AgentApiRunCaller>, OnModuleInit-{+export class AgentRunService implements AgentRunCallerHandler, OnModuleInit {private readonly logger = new Logger(AgentRunService.name);constructor(@@ -172,7 +177,7 @@ export class AgentRunService}): randomUUID();- const caller: AgentApiRunCaller = {+ const caller: AgentRunCaller = {type: 'AGENT_API_RUN',ref: {agentId: agent.id,@@ -250,8 +255,9 @@ export class AgentRunService// The member a run acts as is looked up again, so a waiting run goes on with their current roleasync buildExecutionContext({workspaceId,- caller: { ref },- }: AgentRunCallerInput<AgentApiRunCaller>): Promise<AgentRunExecutionContext> {+ caller,+ }: AgentRunCallerInput): Promise<AgentRunExecutionContext> {+ const ref = agentApiRunCallerRefSchema.parse(caller.ref);const agent = await this.agentRepository.findOne(workspaceId, {where: { id: ref.agentId },});@@ -301,9 +307,11 @@ export class AgentRunServiceasync getWaitingState({workspaceId,caller,- }: AgentRunCallerInput<AgentApiRunCaller>): Promise<AgentRunCallerWaitingState> {+ }: AgentRunCallerInput): Promise<AgentRunCallerWaitingState> {+ const { agentId, runAsWorkspaceMemberId } =+ agentApiRunCallerRefSchema.parse(caller.ref);const agent = await this.agentRepository.findOne(workspaceId, {- where: { id: caller.ref.agentId },+ where: { id: agentId },select: ['id'],});@@ -311,14 +319,14 @@ export class AgentRunServicereturn 'GONE';}- if (!isDefined(caller.ref.runAsWorkspaceMemberId)) {+ if (!isDefined(runAsWorkspaceMemberId)) {return 'WAITING';}// a run acting as a member who left can never continue, so it must release its threadtry {await this.agentActorContextService.buildRunAsWorkspaceMemberContext({- workspaceMemberId: caller.ref.runAsWorkspaceMemberId,+ workspaceMemberId: runAsWorkspaceMemberId,workspaceId,});diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.tsindex f47621d71f0..036b2a8d0e3 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type.ts@@ -1,25 +1,22 @@import { type AgentRunSummary } from 'twenty-shared/ai';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';import { type AgentRunCallerInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type';import { type AgentRunCallerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type';import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';import { type AgentRunExecutionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-execution-context.type';-export type AgentRunCallerHandler<- TCaller extends AgentRunCaller = AgentRunCaller,-> = {+export type AgentRunCallerHandler = {buildExecutionContext(- input: AgentRunCallerInput<TCaller>,+ input: AgentRunCallerInput,): Promise<AgentRunExecutionContext>;getWaitingState(- input: AgentRunCallerInput<TCaller>,+ input: AgentRunCallerInput,): Promise<AgentRunCallerWaitingState>;// absent for a caller that only records the run, whose outcome is already in its conversationonOutcome?(- input: AgentRunCallerInput<TCaller> & {+ input: AgentRunCallerInput & {threadId: string;outcome: AgentRunCallerOutcome;// null when the run recorded nonediff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type.tsindex c9f031c8792..7720ef860aa 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type.ts@@ -1,8 +1,6 @@import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';-export type AgentRunCallerInput<- TCaller extends AgentRunCaller = AgentRunCaller,-> = {+export type AgentRunCallerInput = {workspaceId: string;- caller: TCaller;+ caller: AgentRunCaller;};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.tsindex cee16756289..837e8ea6ed7 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type.ts@@ -1,23 +1,6 @@-import { type ActorMetadata } from 'twenty-shared/types';--// who started a run and waits on its outcome; stored with a suspended run, so the engine-// can call it back without knowing what the ref means-export type AgentRunCaller =- | {- type: 'WORKFLOW_STEP';- ref: { workflowRunId: string; stepId: string };- }- | {- type: 'AGENT_TRIGGER';- ref: { agentId: string; triggerId: string; dispatchedRoleId?: string };- }- | {- type: 'AGENT_API_RUN';- ref: {- agentId: string;- runAsWorkspaceMemberId: string | null;- requestUserWorkspaceId: string | null;- // the request's own actor cannot be read back once it is over- createdBy: ActorMetadata;- };- };+// who started a run and waits on its outcome; stored with a suspended run, so the engine can call+// back the handler registered for its type, which alone knows what the ref means+export type AgentRunCaller = {+ type: string;+ ref: Record<string, unknown>;+};diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-trigger/services/agent-trigger-runner.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-trigger/services/agent-trigger-runner.service.tsindex aac79e4f19d..cabb298fd1b 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-trigger/services/agent-trigger-runner.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-trigger/services/agent-trigger-runner.service.ts@@ -3,13 +3,13 @@ import { Injectable, Logger, type OnModuleInit } from '@nestjs/common';import { type AgentTrigger } from 'twenty-shared/application';import { isDefined } from 'twenty-shared/utils';import { v4 } from 'uuid';+import { z } from 'zod';import { buildCreatedByFromAgent } from 'src/engine/core-modules/actor/utils/build-created-by-from-agent.util';import { UsageOperationType } from 'src/engine/core-modules/usage/enums/usage-operation-type.enum';import { AgentActorContextService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service';import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';import { AgentRunnerService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';import { type AgentRunCallerInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type';import { type AgentRunCallerHandler } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type';import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';@@ -27,7 +27,13 @@ import {import { InjectWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/inject-workspace-scoped-repository.decorator';import { WorkspaceScopedRepository } from 'src/engine/twenty-orm/workspace-scoped-repository/workspace-scoped-repository';-type AgentTriggerCaller = Extract<AgentRunCaller, { type: 'AGENT_TRIGGER' }>;+const agentTriggerCallerRefSchema = z.object({+ agentId: z.string(),+ triggerId: z.string(),+ dispatchedRoleId: z.string().optional(),+});++type AgentTriggerCallerRef = z.infer<typeof agentTriggerCallerRefSchema>;type TriggeredRun = {agent: AgentEntity;@@ -39,7 +45,7 @@ type TriggeredRun = {// as long as the trigger could still start it@Injectable()export class AgentTriggerRunnerService- implements AgentRunCallerHandler<AgentTriggerCaller>, OnModuleInit+ implements AgentRunCallerHandler, OnModuleInit{private readonly logger = new Logger(AgentTriggerRunnerService.name);@@ -60,8 +66,7 @@ export class AgentTriggerRunnerServicepayload,...ref}: RunAgentTriggerJobData): Promise<void> {- const caller: AgentTriggerCaller = { type: 'AGENT_TRIGGER', ref };- const triggeredRun = await this.findTriggeredRun({ workspaceId, caller });+ const triggeredRun = await this.findTriggeredRun({ workspaceId, ref });if (!isDefined(triggeredRun)) {return;@@ -79,7 +84,7 @@ export class AgentTriggerRunnerService}),isCreated: true,},- caller,+ caller: { type: 'AGENT_TRIGGER', ref },spec: {agentId: agent.id,title: agent.label,@@ -104,25 +109,42 @@ export class AgentTriggerRunnerService});}- async buildExecutionContext(- input: AgentRunCallerInput<AgentTriggerCaller>,- ): Promise<AgentRunExecutionContext> {- return (await this.findTriggeredRunOrThrow(input)).executionContext;+ async buildExecutionContext({+ workspaceId,+ caller,+ }: AgentRunCallerInput): Promise<AgentRunExecutionContext> {+ const ref = agentTriggerCallerRefSchema.parse(caller.ref);+ const triggeredRun = await this.findTriggeredRun({ workspaceId, ref });++ if (!isDefined(triggeredRun)) {+ throw new AiException(+ `Trigger ${ref.triggerId} of agent ${ref.agentId} can no longer run`,+ AiExceptionCode.AGENT_EXECUTION_FAILED,+ );+ }++ return triggeredRun.executionContext;}- async getWaitingState(- input: AgentRunCallerInput<AgentTriggerCaller>,- ): Promise<AgentRunCallerWaitingState> {- return isDefined(await this.findTriggeredRun(input)) ? 'WAITING' : 'GONE';+ async getWaitingState({+ workspaceId,+ caller,+ }: AgentRunCallerInput): Promise<AgentRunCallerWaitingState> {+ const ref = agentTriggerCallerRefSchema.parse(caller.ref);++ return isDefined(await this.findTriggeredRun({ workspaceId, ref }))+ ? 'WAITING'+ : 'GONE';}// The trigger may have been turned off or removed, or the agent's role changed, since the run was queuedprivate async findTriggeredRun({workspaceId,- caller: {- ref: { agentId, triggerId, dispatchedRoleId },- },- }: AgentRunCallerInput<AgentTriggerCaller>): Promise<TriggeredRun | null> {+ ref: { agentId, triggerId, dispatchedRoleId },+ }: {+ workspaceId: string;+ ref: AgentTriggerCallerRef;+ }): Promise<TriggeredRun | null> {const agent = await this.agentRepository.findOne(workspaceId, {where: { id: agentId },});@@ -178,19 +200,4 @@ export class AgentTriggerRunnerService},};}-- private async findTriggeredRunOrThrow(- input: AgentRunCallerInput<AgentTriggerCaller>,- ): Promise<TriggeredRun> {- const triggeredRun = await this.findTriggeredRun(input);-- if (!isDefined(triggeredRun)) {- throw new AiException(- `Trigger ${input.caller.ref.triggerId} of agent ${input.caller.ref.agentId} can no longer run`,- AiExceptionCode.AGENT_EXECUTION_FAILED,- );- }-- return triggeredRun;- }}diff --git a/packages/twenty-server/src/engine/utils/handler-registry.ts b/packages/twenty-server/src/engine/utils/handler-registry.tsindex 1b8c718add7..61d26a9e039 100644--- a/packages/twenty-server/src/engine/utils/handler-registry.ts+++ b/packages/twenty-server/src/engine/utils/handler-registry.ts@@ -1,14 +1,14 @@import { isDefined } from 'twenty-shared/utils';// lets the engine call back code it must not import, such as workflow handlers, which register themselves here-export class HandlerRegistry<TKey extends string, THandler> {- private readonly handlers = new Map<TKey, THandler>();+export class HandlerRegistry<THandler> {+ private readonly handlers = new Map<string, THandler>();- register(key: TKey, handler: THandler): void {+ register(key: string, handler: THandler): void {this.handlers.set(key, handler);}- getHandlerOrThrow(key: TKey): THandler {+ getHandlerOrThrow(key: string): THandler {const handler = this.handlers.get(key);if (!isDefined(handler)) {diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util.tsindex 672a09410eb..82f8db09b4d 100644--- a/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util.ts@@ -1,7 +1,6 @@import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';-type WorkflowStepCaller = Extract<AgentRunCaller, { type: 'WORKFLOW_STEP' }>;--export const buildWorkflowStepCaller = (- ref: WorkflowStepCaller['ref'],-): WorkflowStepCaller => ({ type: 'WORKFLOW_STEP', ref });+export const buildWorkflowStepCaller = (ref: {+ workflowRunId: string;+ stepId: string;+}): AgentRunCaller => ({ type: 'WORKFLOW_STEP', ref });diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.tsindex 6b50395ce67..61ebd46ae88 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/workspace-services/workflow-agent-run-caller-handler.workspace-service.ts@@ -1,12 +1,12 @@import { Injectable, type OnModuleInit } from '@nestjs/common';import { isDefined } from 'twenty-shared/utils';+import { z } from 'zod';import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants';import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';import { type AgentRunCallerInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-input.type';import { type AgentRunCallerHandler } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-handler.type';import { type AgentRunCallerWaitingState } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-waiting-state.type';@@ -22,12 +22,15 @@ import { getWorkflowStepWaitingState } from 'src/modules/workflow/workflow-runneimport { WorkflowRunStepLogWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run-step-log.workspace-service';import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';-type WorkflowStepCaller = Extract<AgentRunCaller, { type: 'WORKFLOW_STEP' }>;+const workflowStepCallerRefSchema = z.object({+ workflowRunId: z.string(),+ stepId: z.string(),+});// A step waiting on a CALLBACK takes the outcome of the agent run the engine continued@Injectable()export class WorkflowAgentRunCallerHandlerWorkspaceService- implements AgentRunCallerHandler<WorkflowStepCaller>, OnModuleInit+ implements AgentRunCallerHandler, OnModuleInit{constructor(private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,@@ -46,8 +49,9 @@ export class WorkflowAgentRunCallerHandlerWorkspaceServiceasync buildExecutionContext({workspaceId,caller,- }: AgentRunCallerInput<WorkflowStepCaller>): Promise<AgentRunExecutionContext> {- const runInfo = { workflowRunId: caller.ref.workflowRunId, workspaceId };+ }: AgentRunCallerInput): Promise<AgentRunExecutionContext> {+ const { workflowRunId } = workflowStepCallerRefSchema.parse(caller.ref);+ const runInfo = { workflowRunId, workspaceId };return buildWorkflowAgentRunExecutionContext({executionContext:@@ -60,13 +64,17 @@ export class WorkflowAgentRunCallerHandlerWorkspaceServiceasync getWaitingState({workspaceId,caller,- }: AgentRunCallerInput<WorkflowStepCaller>): Promise<AgentRunCallerWaitingState> {+ }: AgentRunCallerInput): Promise<AgentRunCallerWaitingState> {+ const { workflowRunId, stepId } = workflowStepCallerRefSchema.parse(+ caller.ref,+ );+return getWorkflowStepWaitingState({workflowRun: await this.workflowRunWorkspaceService.getWorkflowRun({- workflowRunId: caller.ref.workflowRunId,+ workflowRunId,workspaceId,}),- stepId: caller.ref.stepId,+ stepId,});}@@ -74,15 +82,17 @@ export class WorkflowAgentRunCallerHandlerWorkspaceService// retried or continues on failure like any failed stepasync onOutcome({workspaceId,- caller: {- ref: { workflowRunId, stepId },- },+ caller,threadId,outcome,summary,}: Parameters<- NonNullable<AgentRunCallerHandler<WorkflowStepCaller>['onOutcome']>+ NonNullable<AgentRunCallerHandler['onOutcome']>>[0]): Promise<void> {+ const { workflowRunId, stepId } = workflowStepCallerRefSchema.parse(+ caller.ref,+ );+if (isDefined(summary)) {await this.workflowRunStepLogService.setAiAgentStepLog({workflowRunId,