Skip to content

The open alternative to Salesforce, designed for AI.

Represent a paused agent run as a pending wake-up (#27579)

Félix Malfaitc56bb44b190f
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
index e0ad4efc339..ff1e8d65fc2 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.graphql
@@ -4154,10 +4154,10 @@ enum RunAgentMessageRole {
type RunAgentResult {
error: String
- isWaiting: Boolean!
result: JSON
+ status: String!
success: Boolean!
- threadId: UUID
+ threadId: UUID!
}
input RunAgentThreadInput {
diff --git a/packages/twenty-client-sdk/src/metadata/generated/schema.ts b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
index 6d5bbd0ecfa..223bd44930b 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/schema.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/schema.ts
@@ -3010,10 +3010,10 @@ export type RunAgentMessageRole = 'assistant' | 'user'
export interface RunAgentResult {
error?: Scalars['String']
- isWaiting: Scalars['Boolean']
result?: Scalars['JSON']
+ status: Scalars['String']
success: Scalars['Boolean']
- threadId?: Scalars['UUID']
+ threadId: Scalars['UUID']
__typename: 'RunAgentResult'
}
@@ -7238,8 +7238,8 @@ export interface RunAgentMessageInput {attachments?: (RunAgentMessageAttachmentI
export interface RunAgentResultGenqlSelection{
error?: boolean | number
- isWaiting?: boolean | number
result?: boolean | number
+ status?: boolean | number
success?: boolean | number
threadId?: boolean | number
__typename?: 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 bf099684c8a..07b2752019f 100644
--- a/packages/twenty-client-sdk/src/metadata/generated/types.ts
+++ b/packages/twenty-client-sdk/src/metadata/generated/types.ts
@@ -11168,12 +11168,12 @@ export default {
"error": [
1
],
- "isWaiting": [
- 4
- ],
"result": [
287
],
+ "status": [
+ 1
+ ],
"success": [
4
],
diff --git a/packages/twenty-docs/developers/extend/apps/logic/skills-and-agents.mdx b/packages/twenty-docs/developers/extend/apps/logic/skills-and-agents.mdx
index 78a81ee5bf5..5286abf8e98 100644
--- a/packages/twenty-docs/developers/extend/apps/logic/skills-and-agents.mdx
+++ b/packages/twenty-docs/developers/extend/apps/logic/skills-and-agents.mdx
@@ -108,7 +108,7 @@ to `defineAgent()`, and pass what you want it to work on as `input`:
```ts src/logic-functions/run-enricher.ts
import { runAgent } from 'twenty-sdk/logic-function';
-const { result, error, success, isWaiting } = await runAgent({
+const { threadId, status, result, error, success } = await runAgent({
agentUniversalIdentifier: 'b3c4d5e6-f7a8-9012-bcde-f34567890123',
input: 'Enrich House Ad <recordId>: fill empty fields from its listing URL.',
});
@@ -122,7 +122,7 @@ Key points:
- The agent runs **synchronously** and can read/update records itself via its
own tools — `runAgent()` resolves once the run completes.
- The agent can also pause until a record changes or some time has passed.
- `runAgent()` then resolves with `success: true`, `isWaiting: true` and
+ `runAgent()` then resolves with `success: true`, `status: 'SUSPENDED'` and
`result: null`; the agent goes on by itself later, and its reply lands in the
`threadId` conversation. A call to a `thread` whose agent is still waiting
fails until it finishes.
@@ -136,10 +136,10 @@ Key points:
Without it, `runAgent()` fails with a permission error.
- Set a generous `timeoutSeconds` on the logic function — agent runs can take
several seconds.
-- `success` is `true` and `result` is non-null when the run completes without
- waiting; while it waits, `success` and `isWaiting` are `true` and `result` is
- `null`; on failure `success` is `false`, `result` is `null`, and `error` holds
- the reason (for example, when the workspace ran out of AI credits mid-run).
+- `status` is `COMPLETED` with a non-null `result` when the run completes
+ without waiting; `SUSPENDED` with `success: true` and `result: null` while it
+ waits; `FAILED` with `success: false` and `error` holding the reason (for
+ example, when the workspace ran out of AI credits mid-run).
```ts src/roles/default-role.ts
import { defineApplicationRole, SystemPermissionFlag } from 'twenty-sdk/define';
diff --git a/packages/twenty-front/src/generated-metadata/graphql.ts b/packages/twenty-front/src/generated-metadata/graphql.ts
index abff68b11e4..3a40800ab6d 100644
--- a/packages/twenty-front/src/generated-metadata/graphql.ts
+++ b/packages/twenty-front/src/generated-metadata/graphql.ts
@@ -6208,10 +6208,10 @@ export enum RunAgentMessageRole {
export type RunAgentResult = {
__typename?: 'RunAgentResult';
error?: Maybe<Scalars['String']['output']>;
- isWaiting: Scalars['Boolean']['output'];
result?: Maybe<Scalars['JSON']['output']>;
+ status: Scalars['String']['output'];
success: Scalars['Boolean']['output'];
- threadId?: Maybe<Scalars['UUID']['output']>;
+ threadId: Scalars['UUID']['output'];
};
export type RunAgentThreadInput = {
diff --git a/packages/twenty-sdk/src/sdk/logic-function/agents/__tests__/run-agent.spec.ts b/packages/twenty-sdk/src/sdk/logic-function/agents/__tests__/run-agent.spec.ts
index 8989e58ca80..8001a10c84f 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/agents/__tests__/run-agent.spec.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/agents/__tests__/run-agent.spec.ts
@@ -27,11 +27,11 @@ describe('runAgent', () => {
it('POSTs the runAgent mutation to /metadata and returns the result', async () => {
const payload = {
+ threadId: 'thread-id',
+ status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
- isWaiting: false,
- threadId: null,
};
fetchSpy.mockResolvedValue(
@@ -76,11 +76,11 @@ describe('runAgent', () => {
JSON.stringify({
data: {
runAgent: {
+ threadId: 'thread-id',
+ status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
- isWaiting: false,
- threadId: 'thread-id',
},
},
}),
@@ -111,10 +111,11 @@ describe('runAgent', () => {
it('POSTs messages when provided instead of prompt', async () => {
const payload = {
+ threadId: 'thread-id',
+ status: 'COMPLETED',
result: { response: 'done' },
error: null,
success: true,
- isWaiting: false,
};
fetchSpy.mockResolvedValue(
diff --git a/packages/twenty-sdk/src/sdk/logic-function/agents/run-agent.ts b/packages/twenty-sdk/src/sdk/logic-function/agents/run-agent.ts
index 0dbe0a37fae..987a305a2bc 100644
--- a/packages/twenty-sdk/src/sdk/logic-function/agents/run-agent.ts
+++ b/packages/twenty-sdk/src/sdk/logic-function/agents/run-agent.ts
@@ -9,11 +9,11 @@ import { postGraphqlRequest } from '@/sdk/logic-function/utils/post-graphql-requ
const RUN_AGENT_MUTATION = `
mutation RunAgent($input: RunAgentInput!) {
runAgent(input: $input) {
+ threadId
+ status
result
error
success
- isWaiting
- threadId
}
}
`;
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791438611736-add-payload-to-pending-wake-up.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791438611736-add-payload-to-pending-wake-up.ts
new file mode 100644
index 00000000000..72d10fec217
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791438611736-add-payload-to-pending-wake-up.ts
@@ -0,0 +1,19 @@
+import { type QueryRunner } from 'typeorm';
+
+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';
+import { type FastInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/fast-instance-command.interface';
+
+@RegisteredInstanceCommand('2.46.0', 1791438611736)
+export class AddPayloadToPendingWakeUpFastInstanceCommand implements FastInstanceCommand {
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ 'ALTER TABLE "core"."pendingWakeUp" ADD "payload" jsonb',
+ );
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ 'ALTER TABLE "core"."pendingWakeUp" DROP COLUMN "payload"',
+ );
+ }
+}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.ts
new file mode 100644
index 00000000000..698ee975be0
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.ts
@@ -0,0 +1,141 @@
+import { type DataSource, type QueryRunner } from 'typeorm';
+
+import { RegisteredInstanceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-instance-command.decorator';
+import { type SlowInstanceCommand } from 'src/engine/core-modules/upgrade/interfaces/slow-instance-command.interface';
+import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
+import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';
+
+const SUSPENSION_PAYLOAD_SQL = `jsonb_build_object('caller', suspension.caller, 'runSpec', suspension."runSpec",
+ 'summary', suspension.summary, 'continuationCount', suspension."resumeCount")`;
+
+// A suspended run becomes the AGENT_RUN wake-up of its conversation, keyed RUN so the conversation
+// holds one, with the run in its payload: a run waiting on time or an event keeps its wake-up, and one
+// paused on a question waits on an ANSWER wake-up. A workflow step waiting on a call it posted waits on its own ANSWER wake-up
+@RegisteredInstanceCommand('2.46.0', 1791438611737, { type: 'slow' })
+export class MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand implements SlowInstanceCommand {
+ // every statement can run again, so a migration that stopped halfway completes on the next run
+ async runDataMigration(dataSource: DataSource): Promise<void> {
+ // a run waits on its latest wait, and a conversation keeps one wake-up per run
+ await dataSource.query(
+ `DELETE FROM "core"."pendingWakeUp" wake_up USING "core"."pendingWakeUp" newer
+ WHERE wake_up."ownerType" = 'AGENT_RUN' AND newer."ownerType" = 'AGENT_RUN'
+ AND newer."ownerId" = wake_up."ownerId"
+ AND (newer."createdAt", newer.id) > (wake_up."createdAt", wake_up.id)`,
+ );
+ const waits: { id: string; workspaceId: string; threadId: string }[] =
+ await dataSource.query(
+ `SELECT wake_up.id, wake_up."workspaceId", suspension."threadId"
+ FROM "core"."pendingWakeUp" wake_up
+ JOIN "core"."agentRunSuspension" suspension ON suspension.id = wake_up."ownerId"
+ WHERE wake_up."ownerType" = 'AGENT_RUN' AND suspension."runSpec" IS NOT NULL`,
+ );
+
+ // a run paused on a question and a wait at once waits on the answer, as the runner now pauses it.
+ // A fresh id leaves the wait's scheduled resolution nothing to claim
+ for (const { id, workspaceId, threadId } of waits) {
+ const schemaName = escapeIdentifier(getWorkspaceSchemaName(workspaceId));
+
+ await dataSource.query(
+ `UPDATE "core"."pendingWakeUp" SET id = uuid_generate_v4(), "eventName" = NULL, "resumeAt" = NULL,
+ "condition" = jsonb_build_object('type', 'ANSWER', 'threadId', $2::uuid::text)
+ WHERE id = $1 AND EXISTS (
+ SELECT 1 FROM ${schemaName}."agentChatThread" thread
+ JOIN ${schemaName}."agentMessagePart" part ON part."messageId" = thread."pendingQuestionMessageId"
+ WHERE thread.id = $2::uuid AND part."toolName" NOT IN ('wait_for_event', 'wait_for_duration')
+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
+ )`,
+ [id, threadId],
+ );
+ }
+
+ await dataSource.query(
+ `UPDATE "core"."pendingWakeUp" wake_up
+ SET "ownerId" = suspension."threadId", "ownerKey" = 'RUN', "payload" = ${SUSPENSION_PAYLOAD_SQL}
+ FROM "core"."agentRunSuspension" suspension
+ WHERE wake_up."ownerType" = 'AGENT_RUN' AND wake_up."ownerId" = suspension.id
+ AND suspension."runSpec" IS NOT NULL`,
+ );
+ await dataSource.query(
+ `INSERT INTO "core"."pendingWakeUp" ("workspaceId", "ownerType", "ownerId", "ownerKey", "condition", "payload")
+ SELECT suspension."workspaceId", 'AGENT_RUN', suspension."threadId", 'RUN',
+ jsonb_build_object('type', 'ANSWER', 'threadId', suspension."threadId"), ${SUSPENSION_PAYLOAD_SQL}
+ FROM "core"."agentRunSuspension" suspension
+ WHERE suspension."runSpec" IS NOT NULL
+ ON CONFLICT ("ownerType", "ownerId", "ownerKey") DO NOTHING`,
+ );
+ await dataSource.query(
+ `INSERT INTO "core"."pendingWakeUp" ("workspaceId", "ownerType", "ownerId", "ownerKey", "condition")
+ SELECT suspension."workspaceId", 'WORKFLOW_STEP', (suspension.caller -> 'ref' ->> 'workflowRunId')::uuid,
+ suspension.caller -> 'ref' ->> 'stepId', jsonb_build_object('type', 'ANSWER', 'threadId', suspension."threadId")
+ FROM "core"."agentRunSuspension" suspension
+ WHERE suspension."runSpec" IS NULL AND suspension.caller ->> 'type' = 'WORKFLOW_STEP'
+ ON CONFLICT ("ownerType", "ownerId", "ownerKey") DO NOTHING`,
+ );
+ // a wait whose run is gone has nothing left to wake up
+ await dataSource.query(
+ `DELETE FROM "core"."pendingWakeUp" WHERE "ownerType" = 'AGENT_RUN' AND "payload" IS NULL`,
+ );
+ }
+
+ public async up(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query('DROP TABLE "core"."agentRunSuspension"');
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ await queryRunner.query(
+ 'CREATE TABLE "core"."agentRunSuspension" ("workspaceId" uuid NOT NULL, "id" uuid NOT NULL DEFAULT uuid_generate_v4(), "threadId" uuid NOT NULL, "caller" jsonb NOT NULL, "runSpec" jsonb, "summary" jsonb, "resumeCount" integer NOT NULL DEFAULT \'0\', "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "UQ_AGENT_RUN_SUSPENSION_THREAD_ID" UNIQUE ("threadId"), CONSTRAINT "PK_ffa8fcfd24c5a8e0122f5a682af" PRIMARY KEY ("id"))',
+ );
+ await queryRunner.query(
+ 'ALTER TABLE "core"."agentRunSuspension" ADD CONSTRAINT "FK_3005d9ef8e9f321e387a8ac0b6a" FOREIGN KEY ("workspaceId") REFERENCES "core"."workspace"("id") ON DELETE CASCADE ON UPDATE NO ACTION',
+ );
+
+ await queryRunner.query(
+ `INSERT INTO "core"."agentRunSuspension" ("workspaceId", "threadId", "caller", "runSpec", "summary", "resumeCount")
+ SELECT "workspaceId", "ownerId", "payload" -> 'caller', "payload" -> 'runSpec',
+ NULLIF("payload" -> 'summary', 'null'::jsonb), ("payload" ->> 'continuationCount')::integer
+ FROM "core"."pendingWakeUp" WHERE "ownerType" = 'AGENT_RUN'`,
+ );
+ await queryRunner.query(
+ `INSERT INTO "core"."agentRunSuspension" ("workspaceId", "threadId", "caller")
+ SELECT "workspaceId", ("condition" ->> 'threadId')::uuid, jsonb_build_object('type', 'WORKFLOW_STEP',
+ 'ref', jsonb_build_object('workflowRunId', "ownerId", 'stepId', "ownerKey"))
+ FROM "core"."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "condition" ->> 'type' = 'ANSWER'
+ ON CONFLICT ("threadId") DO NOTHING`,
+ );
+ await queryRunner.query(
+ `DELETE FROM "core"."pendingWakeUp" WHERE "condition" ->> 'type' = 'ANSWER'`,
+ );
+
+ await queryRunner.query(
+ `UPDATE "core"."pendingWakeUp" wake_up SET "ownerId" = suspension.id
+ FROM "core"."agentRunSuspension" suspension
+ WHERE wake_up."ownerType" = 'AGENT_RUN' AND wake_up."ownerId" = suspension."threadId"`,
+ );
+
+ const suspensions: { id: string; workspaceId: string; threadId: string }[] =
+ await queryRunner.query(
+ 'SELECT id, "workspaceId", "threadId" FROM "core"."agentRunSuspension"',
+ );
+
+ // the previous shape hands an answer to a run through a mark on its pending calls, and keys a wait by its call
+ for (const { id, workspaceId, threadId } of suspensions) {
+ const schemaName = escapeIdentifier(getWorkspaceSchemaName(workspaceId));
+ const isPendingPartOfThread = `message.id = part."messageId" AND message."threadId" = $1
+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'`;
+
+ await queryRunner.query(
+ `UPDATE ${schemaName}."agentMessagePart" part
+ SET "toolOutput" = part."toolOutput" || '{"awaitedByCaller": true}'::jsonb
+ FROM ${schemaName}."agentMessage" message WHERE ${isPendingPartOfThread}`,
+ [threadId],
+ );
+ await queryRunner.query(
+ `UPDATE "core"."pendingWakeUp" wake_up SET "ownerKey" = part."toolCallId"
+ FROM ${schemaName}."agentMessagePart" part, ${schemaName}."agentMessage" message
+ WHERE ${isPendingPartOfThread} AND part."toolName" IN ('wait_for_event', 'wait_for_duration')
+ AND wake_up."ownerType" = 'AGENT_RUN' AND wake_up."ownerId" = $2`,
+ [threadId, id],
+ );
+ }
+ }
+}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts
index 34f0e3d7768..179da2c6895 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791306663446-suspend-paused-agent-steps.command.ts
@@ -26,15 +26,14 @@ type PausedAgentStep = {
// An agent step that paused on a question before 2.46 is PENDING with its conversation on
// stepInfo.threadId, and was continued by running the step again. The engine now continues the
-// agent itself, so the step gets the suspension the engine continues it from, and its pending
-// calls the mark that hands their answer to it. A step whose question was
-// answered meanwhile has no call left to settle its suspension, which would then
-// block its conversation for good, so it gets none.
+// agent itself, so the step's conversation gets the ANSWER wake-up holding the run the engine
+// continues. A step whose question was answered meanwhile has no call left to resolve that
+// wake-up, which would then block its conversation for good, so it gets none.
@RegisteredWorkspaceCommand('2.46.0', 1791306663446)
@Command({
name: 'upgrade:2-46:suspend-paused-agent-steps',
description:
- 'Give agent steps paused on a question the suspension the engine continues them from',
+ 'Give agent steps paused on a question the wake-up the engine continues them from',
})
export class SuspendPausedAgentStepsCommand extends ProvisionedWorkspaceCommandRunner {
constructor(
@@ -102,32 +101,23 @@ export class SuspendPausedAgentStepsCommand extends ProvisionedWorkspaceCommandR
return pausedSteps.length;
}
- // an answer settled between the select and this update leaves its
- // thread unmarked, and that thread gets no suspension
- const [markedThreads]: [{ threadId: string }[], number] =
- await manager.query(
- `UPDATE ${table('agentMessagePart')} part
- SET "toolOutput" = part."toolOutput" || '{"awaitedByCaller": true}'::jsonb
- FROM ${table('agentMessage')} message
- WHERE part."messageId" = message.id
- AND message."threadId" = ANY($1::uuid[])
- AND jsonb_typeof(part."toolOutput") = 'object'
- AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
- RETURNING message."threadId" AS "threadId"`,
- [pausedSteps.map(({ threadId }) => threadId)],
- );
- const markedThreadIds = new Set(
- markedThreads.map(({ threadId }) => threadId),
- );
- const suspendedSteps = pausedSteps.filter(({ threadId }) =>
- markedThreadIds.has(threadId),
- );
+ let suspendedCount = 0;
- for (const pausedStep of suspendedSteps) {
- await manager.query(
- `INSERT INTO "core"."agentRunSuspension" ("workspaceId", "threadId", "caller", "runSpec", "summary")
- VALUES ($1, $2, $3, $4, $5)
- ON CONFLICT ("threadId") DO NOTHING`,
+ // an answer settled between the select and this insert leaves nothing pending, and no wake-up
+ for (const pausedStep of pausedSteps) {
+ const inserted: { id: string }[] = await manager.query(
+ `INSERT INTO "core"."pendingWakeUp" ("workspaceId", "ownerType", "ownerId", "ownerKey", "condition", "payload")
+ SELECT $1::uuid, 'AGENT_RUN', $2::uuid, 'RUN', jsonb_build_object('type', 'ANSWER', 'threadId', $2::uuid::text),
+ jsonb_build_object('caller', $3::jsonb, 'runSpec', $4::jsonb, 'summary', $5::jsonb, 'continuationCount', 0)
+ WHERE EXISTS (
+ SELECT 1 FROM ${table('agentMessagePart')} part
+ JOIN ${table('agentMessage')} message ON message.id = part."messageId"
+ WHERE message."threadId" = $2::uuid
+ AND jsonb_typeof(part."toolOutput") = 'object'
+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
+ )
+ ON CONFLICT ("ownerType", "ownerId", "ownerKey") DO NOTHING
+ RETURNING id`,
[
workspaceId,
pausedStep.threadId,
@@ -144,14 +134,14 @@ export class SuspendPausedAgentStepsCommand extends ProvisionedWorkspaceCommandR
isApplicationBound: isDefined(pausedStep.applicationId),
}),
),
- isDefined(pausedStep.summary)
- ? JSON.stringify(pausedStep.summary)
- : null,
+ JSON.stringify(pausedStep.summary),
],
);
+
+ suspendedCount += inserted.length;
}
- return suspendedSteps.length;
+ return suspendedCount;
},
);
@@ -161,6 +151,6 @@ export class SuspendPausedAgentStepsCommand extends ProvisionedWorkspaceCommandR
}
async down(_args: RunOnWorkspaceArgs): Promise<void> {
- // A step left with its suspension is still continued by the engine
+ // A step left with its wake-up is still continued by the engine
}
}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791369613345-delete-orphaned-workflow-runs.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791369613345-delete-orphaned-workflow-runs.command.ts
index efa0a9ebb4b..a85b298d6d4 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791369613345-delete-orphaned-workflow-runs.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791369613345-delete-orphaned-workflow-runs.command.ts
@@ -78,39 +78,24 @@ export class DeleteOrphanedWorkflowRunsCommand extends ProvisionedWorkspaceComma
return;
}
- const [{ hasChatHistory }]: [{ hasChatHistory: boolean }] =
- await dataSource.query(
- `SELECT to_regclass($1) IS NOT NULL AS "hasChatHistory"`,
- [`${escapeIdentifier(schemaName)}."agentMessagePart"`],
- );
-
- // suspend-paused-agent-steps runs first and may have suspended an agent
- // step of these runs. Its suspension would block the conversation for
- // good, and its pending calls would keep waiting on a caller that is
- // gone, so an answer goes back to being a chat reply
- const unmarkAwaitedCallsSql = hasChatHistory
- ? `, unmarked AS (
- UPDATE ${escapeIdentifier(schemaName)}."agentMessagePart" part
- SET "toolOutput" = part."toolOutput" - 'awaitedByCaller'
- FROM ${escapeIdentifier(schemaName)}."agentMessage" message, released
- WHERE part."messageId" = message.id
- AND message."threadId" = released."threadId"
- AND jsonb_typeof(part."toolOutput") = 'object'
- AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
- )`
- : '';
-
+ // suspend-paused-agent-steps runs first and may have given an agent step of these runs a wake-up.
+ // It would block the step's conversation for good, so it goes with its run, as do the runs' own waits
const deletedRuns: { id: string }[] = await dataSource.query(
`WITH deleted AS (
DELETE FROM ${workflowRunTable} run WHERE ${ORPHANED_WORKFLOW_RUN_CONDITION} RETURNING run.id
), released AS (
- DELETE FROM "core"."agentRunSuspension" suspension
+ DELETE FROM "core"."pendingWakeUp" wake_up
USING deleted
- WHERE suspension."workspaceId" = $1
- AND suspension.caller ->> 'type' = 'WORKFLOW_STEP'
- AND suspension.caller -> 'ref' ->> 'workflowRunId' = deleted.id::text
- RETURNING suspension."threadId"
- )${unmarkAwaitedCallsSql} SELECT id FROM deleted`,
+ WHERE wake_up."workspaceId" = $1
+ AND (
+ (wake_up."ownerType" = 'WORKFLOW_STEP' AND wake_up."ownerId" = deleted.id)
+ OR (
+ wake_up."ownerType" = 'AGENT_RUN'
+ AND wake_up.payload -> 'caller' ->> 'type' = 'WORKFLOW_STEP'
+ AND wake_up.payload -> 'caller' -> 'ref' ->> 'workflowRunId' = deleted.id::text
+ )
+ )
+ ) SELECT id FROM deleted`,
[workspaceId],
);
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command.ts
index d6c8a4769a5..c92245f5185 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command.ts
@@ -9,9 +9,9 @@ import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/w
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 { RESUME_PENDING_WAKE_UP_JOB_NAME } from 'src/engine/core-modules/pending-wake-up/constants/resume-pending-wake-up-job-name.constant';
+import { type ResumePendingWakeUpJobData } from 'src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type';
import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator';
-import { CONTINUE_AGENT_RUN_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant';
-import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';
import { findFlatEntityByUniversalIdentifier } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-universal-identifier.util';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
@@ -31,7 +31,7 @@ export class ClosePendingAskQuestionsCallsCommand extends ProvisionedWorkspaceCo
protected readonly workspaceIteratorService: WorkspaceIteratorService,
private readonly workspaceCacheService: WorkspaceCacheService,
private readonly storage: AgentHistoryUpgradeStorageService,
- @InjectMessageQueue(MessageQueue.aiQueue)
+ @InjectMessageQueue(MessageQueue.delayedJobsQueue)
private readonly messageQueueService: MessageQueueService,
) {
super(workspaceIteratorService);
@@ -71,7 +71,7 @@ export class ClosePendingAskQuestionsCallsCommand extends ProvisionedWorkspaceCo
const isDryRun = options.dryRun ?? false;
- const { closedThreadCount, suspensionsToContinue } = await this.storage.run(
+ const { closedThreadCount, wakeUpsToResolve } = await this.storage.run(
workspaceId,
async ({ manager, table }) => {
const pendingCalls: { threadId: string }[] = await manager.query(
@@ -85,7 +85,7 @@ export class ClosePendingAskQuestionsCallsCommand extends ProvisionedWorkspaceCo
if (isDryRun) {
return {
closedThreadCount: pendingCalls.length,
- suspensionsToContinue: [],
+ wakeUpsToResolve: [],
};
}
@@ -137,52 +137,49 @@ export class ClosePendingAskQuestionsCallsCommand extends ProvisionedWorkspaceCo
);
// read from what is stored rather than from this run's closures, so a run whose
- // continuation could not be queued last time is continued on the next one
- const suspensions: { id: string; resumeCount: number }[] =
- await manager.query(
- `SELECT suspension.id, suspension."resumeCount"
- FROM "core"."agentRunSuspension" suspension
- WHERE suspension."workspaceId" = $1
- AND suspension."runSpec" IS NOT NULL
- AND EXISTS (
- SELECT 1
- FROM ${table('agentMessagePart')} part
- JOIN ${table('agentMessage')} message ON message.id = part."messageId"
- WHERE message."threadId" = suspension."threadId"
- AND part."toolName" = 'ask_questions'
- AND part."toolOutput" ->> 'message' = $2
- )
- AND NOT EXISTS (
- SELECT 1
- FROM ${table('agentMessagePart')} part
- JOIN ${table('agentMessage')} message ON message.id = part."messageId"
- WHERE message."threadId" = suspension."threadId"
- AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
- )`,
- [workspaceId, SKIPPED_MESSAGE],
- );
+ // wake-up could not be resolved last time is continued on the next one
+ const wakeUps: { id: string }[] = await manager.query(
+ `SELECT wake_up.id
+ FROM "core"."pendingWakeUp" wake_up
+ WHERE wake_up."workspaceId" = $1
+ AND wake_up."ownerType" = 'AGENT_RUN'
+ AND wake_up."condition" ->> 'type' = 'ANSWER'
+ AND EXISTS (
+ SELECT 1
+ FROM ${table('agentMessagePart')} part
+ JOIN ${table('agentMessage')} message ON message.id = part."messageId"
+ WHERE message."threadId" = wake_up."ownerId"
+ AND part."toolName" = 'ask_questions'
+ AND part."toolOutput" ->> 'message' = $2
+ )
+ AND NOT EXISTS (
+ SELECT 1
+ FROM ${table('agentMessagePart')} part
+ JOIN ${table('agentMessage')} message ON message.id = part."messageId"
+ WHERE message."threadId" = wake_up."ownerId"
+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'
+ )`,
+ [workspaceId, SKIPPED_MESSAGE],
+ );
return {
closedThreadCount: pendingCalls.length,
- suspensionsToContinue: suspensions,
+ wakeUpsToResolve: wakeUps,
};
},
);
- // a duplicate finds the run moved on through its resume count, so continuing again is safe
- for (const suspension of suspensionsToContinue) {
- await this.messageQueueService.add<ContinueAgentRunJobData>(
- CONTINUE_AGENT_RUN_JOB_NAME,
- {
- workspaceId,
- suspensionId: suspension.id,
- resumeCount: suspension.resumeCount,
- },
+ // the closed questions count as the answer the run waits on. A duplicate finds the wake-up
+ // claimed, so resolving again is safe
+ for (const wakeUp of wakeUpsToResolve) {
+ await this.messageQueueService.add<ResumePendingWakeUpJobData>(
+ RESUME_PENDING_WAKE_UP_JOB_NAME,
+ { workspaceId, wakeUpId: wakeUp.id, answer: { result: {} } },
);
}
this.logger.log(
- `${isDryRun ? '[DRY RUN] Would close' : 'Closed'} pending ask_questions calls in ${closedThreadCount} thread(s) of workspace ${workspaceId}, continuing ${suspensionsToContinue.length} agent run(s)`,
+ `${isDryRun ? '[DRY RUN] Would close' : 'Closed'} pending ask_questions calls in ${closedThreadCount} thread(s) of workspace ${workspaceId}, continuing ${wakeUpsToResolve.length} agent run(s)`,
);
}
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/add-payload-to-pending-wake-up-upgrade-command-name.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/add-payload-to-pending-wake-up-upgrade-command-name.constant.ts
new file mode 100644
index 00000000000..dde9114e796
--- /dev/null
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/add-payload-to-pending-wake-up-upgrade-command-name.constant.ts
@@ -0,0 +1,4 @@
+// Referenced by @WasIntroducedInUpgrade on the pendingWakeUp "payload" column so
+// upgrade steps running before this command don't query the column.
+export const ADD_PAYLOAD_TO_PENDING_WAKE_UP_UPGRADE_COMMAND_NAME =
+ '2.46.0_AddPayloadToPendingWakeUpFastInstanceCommand_1791438611736';
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.ts
deleted file mode 100644
index ae700b8ff6f..00000000000
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-// Referenced by @WasIntroducedInUpgrade on AgentRunSuspensionEntity so upgrade
-// steps running below 2.46.0 don't query the table before this command creates it.
-export const CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME =
- '2.46.0_CreateAgentRunSuspensionTableFastInstanceCommand_1791301683545';
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
index 2f7cadc2080..5d058e644d1 100644
--- a/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/instance-commands.constant.ts
@@ -233,6 +233,8 @@ import { AddTwoFactorAuthenticationRecoveryCodeIndexFastInstanceCommand } from '
import { DropAgentEvaluationInputsDeferredFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-47/2-47-instance-command-fast-1791401524803-drop-agent-evaluation-inputs';
import { AddTwoFactorAuthenticationRecoveryCodeIndexConcurrentlySlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524804-add-two-factor-authentication-recovery-code-index-concurrently';
import { AddUpgradeMigrationWorkspaceIdCreatedAtIndexConcurrentlySlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791401524805-add-upgrade-migration-workspace-id-created-at-index-concurrently';
+import { AddPayloadToPendingWakeUpFastInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-fast-1791438611736-add-payload-to-pending-wake-up';
+import { MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups';
export const INSTANCE_COMMANDS = [
AddViewFieldGroupIdIndexOnViewFieldFastInstanceCommand,
@@ -468,4 +470,6 @@ export const INSTANCE_COMMANDS = [
DropAgentEvaluationInputsDeferredFastInstanceCommand,
AddTwoFactorAuthenticationRecoveryCodeIndexConcurrentlySlowInstanceCommand,
AddUpgradeMigrationWorkspaceIdCreatedAtIndexConcurrentlySlowInstanceCommand,
+ AddPayloadToPendingWakeUpFastInstanceCommand,
+ MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand,
];
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.ts
index 96314d841c6..97ba41ed9b3 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
@@ -10,11 +10,12 @@ import {
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';
-// Waits resolved by time or by a record event, so they survive a lost queue job or a restart.
+// Waits resolved by time, a record event or an answer, so they survive a lost queue job or a restart.
// Deleting the row is how a resolution claims it, so a wake-up resolves once.
@Entity({ name: 'pendingWakeUp', schema: 'core' })
@WasIntroducedInUpgrade({
@@ -39,6 +40,13 @@ export class PendingWakeUpEntity extends WorkspaceRelatedEntity {
@Column({ type: 'jsonb' })
condition: PendingWakeUpCondition;
+ // what the owner needs to resume, which only its handler reads
+ @WasIntroducedInUpgrade({
+ upgradeCommandName: ADD_PAYLOAD_TO_PENDING_WAKE_UP_UPGRADE_COMMAND_NAME,
+ })
+ @Column({ type: 'jsonb', nullable: true })
+ payload: object | null;
+
@Column({ type: 'varchar', nullable: true })
eventName: string | null;
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts
index 6d280586e9f..cbefb35e14a 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/pending-wake-up.module.ts
@@ -34,6 +34,7 @@ import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache
exports: [
PendingWakeUpService,
PendingWakeUpOwnerHandlerRegistryService,
+ PendingWakeUpResolverService,
PendingWakeUpSweepCronCommand,
],
})
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service.ts
index d6570602f6a..35e90cb22f8 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service.ts
@@ -34,6 +34,7 @@ export class PendingWakeUpResolverService {
workspaceId,
wakeUpId,
event,
+ answer,
attempt = 0,
recordReadAttempt = 0,
}: ResumePendingWakeUpJobData): Promise<void> {
@@ -56,6 +57,7 @@ export class PendingWakeUpResolverService {
await this.pendingWakeUpService.scheduleResolution({
wakeUp,
event,
+ answer,
attempt: attempt + 1,
delayMs: computeRetryDelayMs(attempt),
});
@@ -83,20 +85,12 @@ export class PendingWakeUpResolverService {
return;
}
- const claimedWakeUp = await this.pendingWakeUpService.claim({
- workspaceId,
- wakeUpId,
- });
-
- if (!isDefined(claimedWakeUp)) {
- return;
- }
-
await handler.resolve({
- claimedWakeUp,
+ wakeUp,
outcome: buildPendingWakeUpOutcome({
- condition: claimedWakeUp.condition,
+ condition: wakeUp.condition,
event: readableEvent,
+ answer,
}),
owner: ownerState.owner,
isOwnerGone: ownerState.status === 'GONE',
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.ts
index ff5954aa20b..cf9948939b2 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
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';
import { isDefined } from 'twenty-shared/utils';
+import { Raw } from 'typeorm';
import { v4 } from 'uuid';
import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator';
@@ -41,15 +42,17 @@ export class PendingWakeUpService {
workspaceId,
owner,
condition,
+ payload = null,
}: {
workspaceId: string;
owner: PendingWakeUpOwner;
condition: PendingWakeUpCondition;
+ payload?: object | null;
}): Promise<void> {
const resumeAt =
condition.type === 'TIME'
? new Date(condition.resumeAt)
- : isDefined(condition.expiresAt)
+ : condition.type === 'EVENT' && isDefined(condition.expiresAt)
? new Date(condition.expiresAt)
: null;
@@ -65,6 +68,7 @@ export class PendingWakeUpService {
ownerId: owner.id,
ownerKey: owner.key,
condition,
+ payload,
eventName: condition.type === 'EVENT' ? condition.eventName : null,
resumeAt,
},
@@ -89,13 +93,14 @@ export class PendingWakeUpService {
async scheduleResolution({
wakeUp,
event,
+ answer,
attempt,
recordReadAttempt,
delayMs = 0,
deduplicationId,
}: Pick<
ResumePendingWakeUpJobData,
- 'event' | 'attempt' | 'recordReadAttempt'
+ 'event' | 'answer' | 'attempt' | 'recordReadAttempt'
> & {
wakeUp: ScheduledWakeUp;
delayMs?: number;
@@ -107,6 +112,7 @@ export class PendingWakeUpService {
workspaceId: wakeUp.workspaceId,
wakeUpId: wakeUp.id,
event,
+ answer,
attempt,
recordReadAttempt,
},
@@ -161,6 +167,30 @@ export class PendingWakeUpService {
});
}
+ // the wake-up an answer to the call goes to: one waiting on that call, or one waiting on all the conversation's calls
+ async findAnswerWakeUp({
+ workspaceId,
+ threadId,
+ toolCallId,
+ }: {
+ workspaceId: string;
+ threadId: string;
+ toolCallId: string;
+ }): Promise<PendingWakeUpEntity | null> {
+ return this.pendingWakeUpRepository.findOne(workspaceId, {
+ where: {
+ condition: Raw(
+ (alias) =>
+ `${alias} @> :condition::jsonb AND COALESCE(${alias} ->> 'toolCallId', :toolCallId) = :toolCallId`,
+ {
+ condition: JSON.stringify({ type: 'ANSWER', threadId }),
+ toolCallId,
+ },
+ ),
+ },
+ });
+ }
+
// without a key, every wake-up the owner holds
async cancel({
workspaceId,
@@ -168,8 +198,8 @@ export class PendingWakeUpService {
}: {
workspaceId: string;
owner: Omit<PendingWakeUpOwner, 'key'> & { key?: string };
- }): Promise<void> {
- await this.pendingWakeUpRepository.delete(workspaceId, {
+ }): Promise<PendingWakeUpEntity[]> {
+ return this.pendingWakeUpRepository.deleteAndReturn(workspaceId, {
ownerType: type,
ownerId: id,
...(isDefined(key) ? { ownerKey: key } : {}),
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type.ts
index b6f5513d977..58b5a825070 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type.ts
@@ -3,4 +3,8 @@ import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up
export type PendingWakeUpOutcome =
| { type: 'TIME_ELAPSED' }
| { type: 'EVENT_RECEIVED'; event: PendingWakeUpEvent }
- | { type: 'EXPIRED' };
+ | { type: 'EXPIRED' }
+ | { type: 'ANSWERED'; answer: PendingWakeUpAnswer };
+
+// what the answer settled, or why it could not be delivered
+export type PendingWakeUpAnswer = { result: object } | { error: string };
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type.ts
index 08db3b5a368..b032472e534 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type.ts
@@ -20,8 +20,9 @@ export type PendingWakeUpOwnerHandler<TOwner = unknown> = {
rolePermissionConfig: RolePermissionConfig;
}>;
+ // the handler claims the wake-up once it takes the outcome, so the outcome is taken once
resolve(input: {
- claimedWakeUp: PendingWakeUpEntity;
+ wakeUp: PendingWakeUpEntity;
outcome: PendingWakeUpOutcome;
owner: TOwner | null;
isOwnerGone: boolean;
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type.ts
index cca7cc812e4..2d6fd7020dd 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/types/resume-pending-wake-up-job-data.type.ts
@@ -1,10 +1,13 @@
import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';
+import { type PendingWakeUpAnswer } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
export type ResumePendingWakeUpJobData = {
workspaceId: string;
wakeUpId: string;
// Absent when the wake-up's time came: a time condition elapsed or an event condition expired
event?: PendingWakeUpEvent;
+ // Present when an answer resolves an ANSWER wake-up
+ answer?: PendingWakeUpAnswer;
// How many times the owner put the resolution off because it was not ready yet
attempt?: number;
// How many times the owner failed to read the event's record
diff --git a/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/build-pending-wake-up-outcome.util.ts b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/build-pending-wake-up-outcome.util.ts
index 30e0e918897..bf509a8bee9 100644
--- a/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/build-pending-wake-up-outcome.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/pending-wake-up/utils/build-pending-wake-up-outcome.util.ts
@@ -2,15 +2,24 @@ import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';
import { isDefined } from 'twenty-shared/utils';
import { type PendingWakeUpEvent } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-event.type';
-import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
+import {
+ type PendingWakeUpAnswer,
+ type PendingWakeUpOutcome,
+} from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
export const buildPendingWakeUpOutcome = ({
condition,
event,
+ answer,
}: {
condition: PendingWakeUpCondition;
event?: PendingWakeUpEvent;
+ answer?: PendingWakeUpAnswer;
}): PendingWakeUpOutcome => {
+ if (isDefined(answer)) {
+ return { type: 'ANSWERED', answer };
+ }
+
if (isDefined(event)) {
return { type: 'EVENT_RECEIVED', event };
}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts
index 5afbd3d02bb..1bceddd77e5 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module.ts
@@ -6,11 +6,11 @@ import { CacheLockModule } from 'src/engine/core-modules/cache-lock/cache-lock.m
import { FileEntity } from 'src/engine/core-modules/file/entities/file.entity';
import { FileUrlModule } from 'src/engine/core-modules/file/file-url/file-url.module';
import { MetricsModule } from 'src/engine/core-modules/metrics/metrics.module';
+import { PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';
import { PendingWakeUpModule } from 'src/engine/core-modules/pending-wake-up/pending-wake-up.module';
import { ToolProviderModule } from 'src/engine/core-modules/tool-provider/tool-provider.module';
import { UserWorkspaceModule } from 'src/engine/core-modules/user-workspace/user-workspace.module';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
-import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
import { ContinueAgentRunJob } from 'src/engine/metadata-modules/ai/ai-agent-execution/jobs/continue-agent-run.job';
import { AgentMessagePartResolver } from 'src/engine/metadata-modules/ai/ai-agent-execution/resolvers/agent-message-part.resolver';
import { AgentMessageResolver } from 'src/engine/metadata-modules/ai/ai-agent-execution/resolvers/agent-message.resolver';
@@ -55,8 +55,8 @@ import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspac
ToolProviderModule,
TypeOrmModule.forFeature([
AgentEntity,
- AgentRunSuspensionEntity,
FileEntity,
+ PendingWakeUpEntity,
WorkspaceEntity,
]),
],
@@ -77,8 +77,8 @@ import { provideWorkspaceScopedRepository } from 'src/engine/twenty-orm/workspac
ContinueAgentRunJob,
RunAgentAttachmentService,
provideWorkspaceScopedRepository(AgentEntity),
- provideWorkspaceScopedRepository(AgentRunSuspensionEntity),
provideWorkspaceScopedRepository(FileEntity),
+ provideWorkspaceScopedRepository(PendingWakeUpEntity),
],
exports: [
AgentActorContextService,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/dtos/run-agent-result.dto.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/dtos/run-agent-result.dto.ts
index d6557399602..151d1b705cc 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/dtos/run-agent-result.dto.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/dtos/run-agent-result.dto.ts
@@ -7,6 +7,12 @@ import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/
@ObjectType('RunAgentResult')
export class RunAgentResultDTO implements RunAgentResult {
+ @Field(() => UUIDScalarType)
+ threadId: string;
+
+ @Field(() => String)
+ status: RunAgentResult['status'];
+
@Field(() => GraphQLJSON, { nullable: true })
result: object | null;
@@ -15,10 +21,4 @@ export class RunAgentResultDTO implements RunAgentResult {
@Field()
success: boolean;
-
- @Field()
- isWaiting: boolean;
-
- @Field(() => UUIDScalarType, { nullable: true })
- threadId: string | null;
}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.ts
deleted file mode 100644
index 963d48261b1..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity.ts
+++ /dev/null
@@ -1,50 +0,0 @@
-import { type AgentRunSummary } from 'twenty-shared/ai';
-import {
- Column,
- CreateDateColumn,
- Entity,
- PrimaryGeneratedColumn,
- Unique,
- UpdateDateColumn,
-} from 'typeorm';
-
-import { CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME } from 'src/database/commands/upgrade-version-command/2-46/create-agent-run-suspension-table-upgrade-command-name.constant';
-import { WasIntroducedInUpgrade } from 'src/engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator';
-import { type AgentRunCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller.type';
-import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';
-import { WorkspaceRelatedEntity } from 'src/engine/workspace-manager/types/workspace-related-entity.type';
-
-// A run paused on an answer or a wait, kept until the engine continues it and hands its
-// outcome to the caller. A conversation holds one at a time, as it waits on one thing at a time
-@Entity({ name: 'agentRunSuspension', schema: 'core' })
-@WasIntroducedInUpgrade({
- upgradeCommandName: CREATE_AGENT_RUN_SUSPENSION_TABLE_UPGRADE_COMMAND_NAME,
-})
-@Unique('UQ_AGENT_RUN_SUSPENSION_THREAD_ID', ['threadId'])
-export class AgentRunSuspensionEntity extends WorkspaceRelatedEntity {
- @PrimaryGeneratedColumn('uuid')
- id: string;
-
- @Column({ type: 'uuid' })
- threadId: string;
-
- @Column({ type: 'jsonb' })
- caller: AgentRunCaller;
-
- // null when the caller posted the awaited call itself: the answer is its outcome
- @Column({ type: 'jsonb', nullable: true })
- runSpec: AgentRunSpec | null;
-
- // summed across the run's segments, so the caller gets one total
- @Column({ type: 'jsonb', nullable: true })
- summary: AgentRunSummary | null;
-
- @Column({ type: 'integer', default: 0 })
- resumeCount: number;
-
- @CreateDateColumn({ type: 'timestamptz' })
- createdAt: Date;
-
- @UpdateDateColumn({ type: 'timestamptz' })
- updatedAt: Date;
-}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.ts
index a5356cfa9c6..18ca1d93adf 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util.ts
@@ -1,8 +1,10 @@
import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
export const buildWaitOutcomeToolOutput = (
- // a run dropped while it waits cancels its wait
- outcome: PendingWakeUpOutcome | { type: 'CANCELLED' },
+ // a run dropped, or continued on an answer, while it waits cancels its wait
+ outcome:
+ | Exclude<PendingWakeUpOutcome, { type: 'ANSWERED' }>
+ | { type: 'CANCELLED' },
): Record<string, unknown> => {
switch (outcome.type) {
case 'TIME_ELAPSED':
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts
index ab76cbfc389..f70ec8e76f8 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-caller-conversation.service.spec.ts
@@ -28,7 +28,7 @@ const buildService = ({ isCreated = true } = {}) => {
),
};
const agentRunSuspensionService = {
- findOne: jest.fn().mockResolvedValue(null),
+ isConversationWaiting: jest.fn().mockResolvedValue(false),
};
const service = new AgentCallerConversationService(
@@ -178,11 +178,9 @@ describe('AgentCallerConversationService', () => {
thread: { id: 'suspended-thread-id' },
isCreated: false,
});
- agentRunSuspensionService.findOne.mockImplementation(
- async ({ where }: { where: { threadId: string } }) =>
- where.threadId === 'suspended-thread-id'
- ? { id: 'suspension-id' }
- : null,
+ agentRunSuspensionService.isConversationWaiting.mockImplementation(
+ async ({ threadId }: { threadId: string }) =>
+ threadId === 'suspended-thread-id',
);
await expect(
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.close-awaited-calls.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.close-awaited-calls.spec.ts
deleted file mode 100644
index 196735f57d0..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.close-awaited-calls.spec.ts
+++ /dev/null
@@ -1,88 +0,0 @@
-import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
-import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
-
-const buildService = ({
- pendingQuestionMessageId = 'question-message-id' as string | null,
- isAwaitedByCaller = true,
-} = {}) => {
- const threadRepository = {
- findOne: jest
- .fn()
- .mockResolvedValue(
- pendingQuestionMessageId === null
- ? null
- : { id: 'thread-id', pendingQuestionMessageId },
- ),
- };
- const messagePartRepository = {
- query: jest.fn().mockResolvedValue(undefined),
- find: jest.fn().mockResolvedValue([
- {
- toolOutput: {
- result: { status: 'pending' },
- awaitedByCaller: isAwaitedByCaller,
- },
- },
- ]),
- };
- const threadLifecycleService = {
- closePendingQuestion: jest.fn().mockResolvedValue(undefined),
- };
-
- const service = new AgentRunSuspensionService(
- {} as never,
- threadRepository as never,
- messagePartRepository as never,
- threadLifecycleService as never,
- {} as never,
- {} as never,
- {} as never,
- );
-
- return { service, threadLifecycleService };
-};
-
-describe('AgentRunSuspensionService closeAwaitedCalls', () => {
- it('closes the question a dropped run waited on, cancelling the turn that asked it', async () => {
- const { service, threadLifecycleService } = buildService();
-
- await service.closeAwaitedCalls({
- workspaceId: 'workspace-id',
- threadId: 'thread-id',
- });
-
- expect(threadLifecycleService.closePendingQuestion).toHaveBeenCalledWith({
- workspaceId: 'workspace-id',
- threadId: 'thread-id',
- messageId: 'question-message-id',
- activeStreamId: null,
- turnStatus: AgentTurnStatus.CANCELLED,
- });
- });
-
- it('keeps open a question the member asked of their own', async () => {
- const { service, threadLifecycleService } = buildService({
- isAwaitedByCaller: false,
- });
-
- await service.closeAwaitedCalls({
- workspaceId: 'workspace-id',
- threadId: 'thread-id',
- });
-
- expect(threadLifecycleService.closePendingQuestion).not.toHaveBeenCalled();
- });
-
- it('does nothing when the conversation waits on no question', async () => {
- const { service, threadLifecycleService } = buildService({
- pendingQuestionMessageId: null,
- });
-
- await service.closeAwaitedCalls({
- workspaceId: 'workspace-id',
- threadId: 'thread-id',
- });
-
- expect(threadLifecycleService.closePendingQuestion).not.toHaveBeenCalled();
- });
-});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.spec.ts
index bf1a2799b7d..aff94e8d2c4 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run-suspension.service.spec.ts
@@ -1,125 +1,211 @@
-import { CONTINUE_AGENT_RUN_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant';
-import { type AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
+import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
const CALLER = {
type: 'WORKFLOW_STEP' as const,
ref: { workflowRunId: 'run-id', stepId: 'step-id' },
};
-const buildSuspension = (
- overrides: Partial<AgentRunSuspensionEntity> = {},
-): AgentRunSuspensionEntity =>
- ({
- id: 'suspension-id',
- workspaceId: 'workspace-id',
- threadId: 'thread-id',
- caller: CALLER,
- runSpec: null,
- summary: null,
- resumeCount: 0,
- ...overrides,
- }) as AgentRunSuspensionEntity;
-
-const buildService = () => {
+const CLOSED_QUESTION = {
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ messageId: 'question-message-id',
+ activeStreamId: null,
+ turnStatus: AgentTurnStatus.CANCELLED,
+};
+
+const buildService = ({
+ pendingQuestionMessageId = 'question-message-id' as string | null,
+ hasPostedCall = true,
+ wakeUps = [] as object[],
+} = {}) => {
+ const wakeUpRepository = { find: jest.fn().mockResolvedValue(wakeUps) };
+ const threadRepository = {
+ findOne: jest
+ .fn()
+ .mockResolvedValue(
+ pendingQuestionMessageId === null
+ ? null
+ : { id: 'thread-id', pendingQuestionMessageId },
+ ),
+ };
+ const messagePartRepository = {
+ query: jest.fn().mockResolvedValue(undefined),
+ existsBy: jest.fn().mockResolvedValue(hasPostedCall),
+ };
+ const threadLifecycleService = {
+ closePendingQuestion: jest.fn().mockResolvedValue(undefined),
+ };
+ const pendingWakeUpService = {
+ claim: jest
+ .fn()
+ .mockImplementation(async ({ wakeUpId }) =>
+ wakeUps.find((wakeUp) => (wakeUp as { id: string }).id === wakeUpId),
+ ),
+ };
const onOutcome = jest.fn().mockResolvedValue(undefined);
- const messageQueueService = { add: jest.fn().mockResolvedValue(undefined) };
const service = new AgentRunSuspensionService(
- { delete: jest.fn().mockResolvedValue(undefined) } as never,
- { findOne: jest.fn().mockResolvedValue(null) } as never,
- { query: jest.fn().mockResolvedValue(undefined) } as never,
- {} as never,
- { cancel: jest.fn().mockResolvedValue(undefined) } as never,
+ wakeUpRepository as never,
+ threadRepository as never,
+ messagePartRepository as never,
+ threadLifecycleService as never,
+ pendingWakeUpService as never,
{ getHandlerOrThrow: () => ({ onOutcome }) } as never,
- messageQueueService as never,
+ {} as never,
);
- return { service, onOutcome, messageQueueService };
+ return {
+ service,
+ threadLifecycleService,
+ messagePartRepository,
+ pendingWakeUpService,
+ onOutcome,
+ };
};
describe('AgentRunSuspensionService', () => {
- describe('deliverAnswer', () => {
- it('hands the answer to a call the caller posted itself as its completed outcome', async () => {
- const { service, onOutcome, messageQueueService } = buildService();
+ describe('closeAwaitedCalls', () => {
+ it('closes the question a run waiting on an answer asked, and its wait call', async () => {
+ const { service, threadLifecycleService, messagePartRepository } =
+ buildService();
- await service.deliverAnswer({
+ await service.closeAwaitedCalls({
workspaceId: 'workspace-id',
- suspension: buildSuspension(),
- toolResult: {
- success: true,
- result: {
- status: 'approved',
- proposal: {
- toolName: 'update_one_company',
- arguments: { id: 'company-id', employees: 25 },
- },
- output: { id: 'company-id' },
- },
- },
+ threadId: 'thread-id',
+ isAwaitingAnswer: true,
});
- expect(messageQueueService.add).not.toHaveBeenCalled();
- expect(onOutcome).toHaveBeenCalledTimes(1);
- expect(onOutcome).toHaveBeenCalledWith({
+ expect(messagePartRepository.query).toHaveBeenCalledTimes(1);
+ expect(threadLifecycleService.closePendingQuestion).toHaveBeenCalledWith(
+ CLOSED_QUESTION,
+ );
+ });
+
+ it('keeps open a question the run did not wait on', async () => {
+ const { service, threadLifecycleService } = buildService();
+
+ await service.closeAwaitedCalls({
workspaceId: 'workspace-id',
- caller: CALLER,
threadId: 'thread-id',
- outcome: {
- status: 'COMPLETED',
- result: {
- threadId: 'thread-id',
- outcome: 'executed',
- toolName: 'update_one_company',
- arguments: { id: 'company-id', employees: 25 },
- output: { id: 'company-id' },
- feedback: null,
- error: null,
- },
- },
- summary: null,
+ isAwaitingAnswer: false,
});
+
+ expect(
+ threadLifecycleService.closePendingQuestion,
+ ).not.toHaveBeenCalled();
});
- it('fails the caller when the answer cannot be read', async () => {
- const { service, onOutcome } = buildService();
+ it('does nothing more when the conversation waits on no question', async () => {
+ const { service, threadLifecycleService } = buildService({
+ pendingQuestionMessageId: null,
+ });
- await service.deliverAnswer({
+ await service.closeAwaitedCalls({
workspaceId: 'workspace-id',
- suspension: buildSuspension(),
- toolResult: { success: true, result: { status: 'pending' } },
+ threadId: 'thread-id',
+ isAwaitingAnswer: true,
});
- expect(onOutcome).toHaveBeenCalledTimes(1);
- expect(onOutcome).toHaveBeenCalledWith(
- expect.objectContaining({
- outcome: {
- status: 'FAILED',
- error: 'The answer to the proposed call could not be read',
- },
- }),
+ expect(
+ threadLifecycleService.closePendingQuestion,
+ ).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('closePostedCalls', () => {
+ const cancelledWakeUp = {
+ condition: { type: 'ANSWER', threadId: 'thread-id', toolCallId: 'call' },
+ };
+
+ it('closes the question that holds the call a cancelled wake-up waited on', async () => {
+ const { service, threadLifecycleService } = buildService();
+
+ await service.closePostedCalls({
+ workspaceId: 'workspace-id',
+ cancelledWakeUps: [cancelledWakeUp, { condition: { type: 'TIME' } }],
+ } as never);
+
+ expect(threadLifecycleService.closePendingQuestion).toHaveBeenCalledTimes(
+ 1,
+ );
+ expect(threadLifecycleService.closePendingQuestion).toHaveBeenCalledWith(
+ CLOSED_QUESTION,
);
});
- it('continues a suspended agent run instead of settling it', async () => {
- const { service, onOutcome, messageQueueService } = buildService();
+ it('keeps open a question that asks something else', async () => {
+ const { service, threadLifecycleService } = buildService({
+ hasPostedCall: false,
+ });
- await service.deliverAnswer({
+ await service.closePostedCalls({
workspaceId: 'workspace-id',
- suspension: buildSuspension({ runSpec: {} as never }),
- toolResult: {},
+ cancelledWakeUps: [cancelledWakeUp],
+ } as never);
+
+ expect(
+ threadLifecycleService.closePendingQuestion,
+ ).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('releaseForCaller', () => {
+ it('drops the runs the caller waits on and closes the questions they asked', async () => {
+ const { service, pendingWakeUpService, threadLifecycleService } =
+ buildService({
+ wakeUps: [
+ {
+ id: 'wake-up-id',
+ ownerId: 'thread-id',
+ condition: { type: 'ANSWER', threadId: 'thread-id' },
+ payload: { caller: CALLER },
+ },
+ ],
+ });
+
+ await service.releaseForCaller({
+ workspaceId: 'workspace-id',
+ caller: { type: 'WORKFLOW_STEP', ref: { workflowRunId: 'run-id' } },
});
- expect(messageQueueService.add).toHaveBeenCalledTimes(1);
- expect(messageQueueService.add).toHaveBeenCalledWith(
- CONTINUE_AGENT_RUN_JOB_NAME,
- {
- workspaceId: 'workspace-id',
- suspensionId: 'suspension-id',
- resumeCount: 0,
- },
+ expect(pendingWakeUpService.claim).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ wakeUpId: 'wake-up-id',
+ });
+ expect(threadLifecycleService.closePendingQuestion).toHaveBeenCalledWith(
+ CLOSED_QUESTION,
);
- expect(onOutcome).not.toHaveBeenCalled();
+ });
+ });
+
+ describe('settle', () => {
+ it('hands the outcome and the run so far to the caller', async () => {
+ const { service, onOutcome, threadLifecycleService } = buildService();
+ const summary = { totalCredits: 1 } as never;
+
+ await service.settle({
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ suspension: {
+ caller: CALLER,
+ runSpec: {} as never,
+ summary,
+ continuationCount: 2,
+ },
+ outcome: { status: 'FAILED', error: 'boom' },
+ });
+
+ expect(
+ threadLifecycleService.closePendingQuestion,
+ ).not.toHaveBeenCalled();
+ expect(onOutcome).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ caller: CALLER,
+ threadId: 'thread-id',
+ outcome: { status: 'FAILED', error: 'boom' },
+ summary,
+ });
});
});
});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts
index 9b2fc853c64..c5121c8f9b7 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-run.service.spec.ts
@@ -91,11 +91,11 @@ describe('AgentRunService', () => {
});
expect(result).toEqual({
+ threadId: expect.any(String),
+ status: 'COMPLETED',
result: { response: 'Acme is your biggest customer' },
error: null,
success: true,
- isWaiting: false,
- threadId: expect.any(String),
});
expect(secondResult.threadId).not.toBe(result.threadId);
@@ -245,6 +245,7 @@ describe('AgentRunService', () => {
await expect(
run(service, { input: userInput('Hello') }),
).resolves.toMatchObject({
+ status: 'FAILED',
success: false,
error: 'Agent stopped: no more available credits.',
});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts
index 67715c58c42..ad705e6af1e 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/__tests__/agent-runner.service.spec.ts
@@ -76,6 +76,20 @@ const RUN_INPUT: AgentRunnerRunInput = {
},
};
+const SUSPENSION = {
+ caller: RUN_INPUT.caller,
+ runSpec: RUN_INPUT.spec,
+ summary: null,
+ continuationCount: 0,
+};
+
+const CONTINUATION = {
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ wakeUpId: 'wake-up-id',
+ outcome: { type: 'ANSWERED' as const, answer: { result: {} } },
+};
+
const buildService = (execution = buildExecution()) => {
const agentAsyncExecutorService = {
executeAgent: jest.fn().mockResolvedValue(execution),
@@ -90,19 +104,37 @@ const buildService = (execution = buildExecution()) => {
loadMessages: jest.fn().mockResolvedValue(PRIOR_MESSAGES),
};
+ const agentRunSuspensionService = {
+ assertConversationNotSuspended: jest.fn().mockResolvedValue(undefined),
+ closeAwaitedCalls: jest.fn().mockResolvedValue(undefined),
+ suspend: jest.fn().mockResolvedValue(undefined),
+ settle: jest.fn().mockResolvedValue(undefined),
+ recordWaitOutcome: jest.fn().mockResolvedValue(undefined),
+ };
+ const callerHandler = {
+ getWaitingState: jest.fn().mockResolvedValue('WAITING'),
+ buildExecutionContext: jest
+ .fn()
+ .mockResolvedValue(RUN_INPUT.executionContext),
+ };
+
+ const pendingWakeUpService = {
+ claim: jest.fn().mockResolvedValue({
+ condition: { type: 'ANSWER', threadId: 'thread-id' },
+ payload: SUSPENSION,
+ }),
+ cancel: jest.fn().mockResolvedValue([]),
+ };
+
jest.spyOn(Logger.prototype, 'error').mockImplementation();
const service = new AgentRunnerService(
agentAsyncExecutorService as never,
agentRunConversationService as never,
conversationReaderService as never,
- {
- assertConversationNotSuspended: jest.fn().mockResolvedValue(undefined),
- closeAwaitedCalls: jest.fn().mockResolvedValue(undefined),
- } as never,
- {} as never,
- {} as never,
- {} as never,
+ agentRunSuspensionService as never,
+ { getHandlerOrThrow: () => callerHandler } as never,
+ pendingWakeUpService as never,
{} as never,
);
@@ -111,6 +143,9 @@ const buildService = (execution = buildExecution()) => {
agentAsyncExecutorService,
agentRunConversationService,
conversationReaderService,
+ agentRunSuspensionService,
+ callerHandler,
+ pendingWakeUpService,
};
};
@@ -254,4 +289,155 @@ describe('AgentRunnerService', () => {
},
});
});
+
+ it('suspends a run that asked a question on an answer to it', async () => {
+ const { service, agentRunConversationService, agentRunSuspensionService } =
+ buildService(buildExecution({ isPaused: true }));
+
+ agentRunConversationService.closeTurn.mockResolvedValue({
+ isAwaitingAnswer: true,
+ });
+
+ const { outcome } = await service.run(RUN_INPUT);
+
+ expect(outcome).toEqual({ status: 'SUSPENDED' });
+ expect(agentRunSuspensionService.suspend).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ condition: { type: 'ANSWER', threadId: 'thread-id' },
+ suspension: expect.objectContaining({
+ caller: RUN_INPUT.caller,
+ runSpec: RUN_INPUT.spec,
+ continuationCount: 0,
+ }),
+ });
+ expect(agentRunSuspensionService.closeAwaitedCalls).not.toHaveBeenCalled();
+ });
+
+ it('stops a continued run that keeps pausing and closes what it asked', async () => {
+ const {
+ service,
+ agentRunConversationService,
+ agentRunSuspensionService,
+ conversationReaderService,
+ pendingWakeUpService,
+ } = buildService(buildExecution({ isPaused: true }));
+
+ agentRunConversationService.closeTurn.mockResolvedValue({
+ isAwaitingAnswer: true,
+ });
+ pendingWakeUpService.claim.mockResolvedValue({
+ condition: { type: 'ANSWER', threadId: 'thread-id' },
+ payload: { ...SUSPENSION, continuationCount: 49 },
+ });
+
+ await service.continue(CONTINUATION);
+
+ expect(agentRunConversationService.withThreadLock).toHaveBeenCalledTimes(1);
+ expect(pendingWakeUpService.claim).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ wakeUpId: 'wake-up-id',
+ });
+ expect(conversationReaderService.loadMessages).toHaveBeenCalled();
+ expect(agentRunSuspensionService.suspend).not.toHaveBeenCalled();
+ expect(agentRunSuspensionService.closeAwaitedCalls).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ isAwaitingAnswer: true,
+ });
+ expect(agentRunSuspensionService.settle).toHaveBeenCalledWith(
+ expect.objectContaining({
+ threadId: 'thread-id',
+ outcome: {
+ status: 'FAILED',
+ error: 'Agent stopped: it paused more than 50 times in one run.',
+ },
+ }),
+ );
+ });
+
+ it('drops a continuation whose caller stopped waiting', async () => {
+ const { service, callerHandler, agentAsyncExecutorService } =
+ buildService();
+
+ callerHandler.getWaitingState.mockResolvedValue('GONE');
+
+ await service.continue(CONTINUATION);
+
+ expect(agentAsyncExecutorService.executeAgent).not.toHaveBeenCalled();
+ });
+
+ it('continues a pause once, as a second continuation finds its wake-up claimed', async () => {
+ const { service, pendingWakeUpService, agentAsyncExecutorService } =
+ buildService();
+
+ pendingWakeUpService.claim.mockResolvedValue(null);
+
+ await service.continue(CONTINUATION);
+
+ expect(agentAsyncExecutorService.executeAgent).not.toHaveBeenCalled();
+ });
+
+ it('fails the run when its answer could not be delivered', async () => {
+ const { service, agentRunSuspensionService, agentAsyncExecutorService } =
+ buildService();
+
+ await service.continue({
+ ...CONTINUATION,
+ outcome: { type: 'ANSWERED', answer: { error: 'answer lost' } },
+ });
+
+ expect(agentAsyncExecutorService.executeAgent).not.toHaveBeenCalled();
+ expect(agentRunSuspensionService.settle).toHaveBeenCalledWith(
+ expect.objectContaining({
+ outcome: { status: 'FAILED', error: 'answer lost' },
+ isAwaitingAnswer: true,
+ }),
+ );
+ });
+
+ it('fails a claimed run whose caller cannot be read, rather than dropping it', async () => {
+ const { service, callerHandler, agentRunSuspensionService } =
+ buildService();
+
+ callerHandler.getWaitingState.mockRejectedValue(new Error('db down'));
+
+ await service.continue(CONTINUATION);
+
+ expect(agentRunSuspensionService.settle).toHaveBeenCalledWith(
+ expect.objectContaining({
+ outcome: { status: 'FAILED', error: 'db down' },
+ }),
+ );
+ });
+
+ it('drops the pause of a continued run whose caller stopped waiting while it ran', async () => {
+ const {
+ service,
+ callerHandler,
+ agentRunConversationService,
+ agentRunSuspensionService,
+ pendingWakeUpService,
+ } = buildService(buildExecution({ isPaused: true }));
+
+ agentRunConversationService.closeTurn.mockResolvedValue({
+ isAwaitingAnswer: true,
+ });
+ callerHandler.getWaitingState
+ .mockResolvedValueOnce('WAITING')
+ .mockResolvedValueOnce('GONE');
+
+ await service.continue(CONTINUATION);
+
+ expect(agentRunSuspensionService.suspend).toHaveBeenCalled();
+ expect(pendingWakeUpService.cancel).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ owner: { type: 'AGENT_RUN', id: 'thread-id' },
+ });
+ expect(agentRunSuspensionService.closeAwaitedCalls).toHaveBeenCalledWith({
+ workspaceId: 'workspace-id',
+ threadId: 'thread-id',
+ isAwaitingAnswer: true,
+ });
+ });
});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts
index ab946245ca6..4463e42dda8 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-caller-conversation.service.ts
@@ -34,7 +34,7 @@ type AgentCallerAwaitedToolCall = {
// The conversations a caller, such as a workflow step, holds with a member's inbox: the one its agent
// run writes to, and the messages it sends itself, which may ask the member to approve a call. The
-// caller waits on that answer as on a suspended run, so it gets it through its handler
+// caller waits on that answer with an ANSWER wake-up, which the answer resolves
@Injectable()
export class AgentCallerConversationService {
constructor(
@@ -110,12 +110,10 @@ export class AgentCallerConversationService {
const isKeyedConversationUnavailable =
isDefined(keyedConversation.thread.deletedAt) ||
isDefined(keyedConversation.thread.pendingQuestionMessageId) ||
- isDefined(
- await this.agentRunSuspensionService.findOne({
- workspaceId,
- where: { threadId: keyedConversation.thread.id },
- }),
- );
+ (await this.agentRunSuspensionService.isConversationWaiting({
+ workspaceId,
+ threadId: keyedConversation.thread.id,
+ }));
const { thread, isCreated } = isKeyedConversationUnavailable
? await openThreadUnderKey(fallbackThreadKey)
: keyedConversation;
@@ -141,8 +139,8 @@ export class AgentCallerConversationService {
| { status: 'DELIVERED'; threadId: string }
// the member deleted the conversation, so the call can no longer be answered
| { status: 'DISMISSED'; threadId: string }
- // the caller waits, and gets the answer through its handler's onOutcome
- | { status: 'AWAITING'; threadId: string }
+ // the caller waits on the call with an ANSWER wake-up, which the answer resolves
+ | { status: 'AWAITING'; threadId: string; toolCallId: string }
// a message sent before already holds the answer
| { status: 'ANSWERED'; threadId: string; answer: ProposedToolCallAnswer }
> {
@@ -171,7 +169,7 @@ export class AgentCallerConversationService {
// one is reused so nothing runs twice, and one closed unanswered, such as by a run that ended and
// was retried, is asked again in a new message
for (let attempt = 0; attempt < MAX_ASK_ATTEMPTS; attempt++) {
- const { threadId, isDismissed, awaitedToolOutput } =
+ const { threadId, toolCallId, isDismissed, awaitedToolOutput } =
await this.agentInboxService.sendMessage({
workspaceId,
sender,
@@ -196,13 +194,7 @@ export class AgentCallerConversationService {
}
if (status === 'pending' || status === 'running') {
- await this.agentRunSuspensionService.awaitCallerCall({
- workspaceId,
- threadId,
- caller: awaitedToolCall.caller,
- });
-
- return { status: 'AWAITING', threadId };
+ return { status: 'AWAITING', threadId, toolCallId };
}
const answer = readProposedToolCallAnswer(awaitedToolOutput);
@@ -288,10 +280,7 @@ export class AgentCallerConversationService {
return {
toolName: PROPOSE_TOOL_CALL_TOOL_NAME,
input,
- output: {
- ...buildProposeToolCallPendingOutput(resolution.proposal),
- awaitedByCaller: true,
- },
+ output: buildProposeToolCallPendingOutput(resolution.proposal),
};
}
}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts
index 755d929bd22..ee153432dc8 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-pending-wake-up-handler.service.ts
@@ -1,39 +1,24 @@
import { Injectable, type OnModuleInit } from '@nestjs/common';
-import { isDefined } from 'twenty-shared/utils';
-
import { type QueueJobOptions } from 'src/engine/core-modules/message-queue/drivers/interfaces/job-options.interface';
import { type 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 PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
import { type PendingWakeUpOwnerHandler } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';
import { type PendingWakeUpOwnerState } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-state.type';
-import { type AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
-import { buildWaitOutcomeToolOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util';
import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-caller-handler-registry.service';
import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
-import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';
-import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';
-import { AgentConversationReaderService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service';
-import { type AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';
-import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';
-import {
- AiException,
- AiExceptionCode,
-} from 'src/engine/metadata-modules/ai/ai.exception';
+import { type AgentRunSuspension } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type';
-// An AGENT_RUN wake-up is owned by a suspended run and keyed by the wait call it paused on
+// An AGENT_RUN wake-up is owned by the conversation of a suspended run, and its payload is the run
@Injectable()
export class AgentRunPendingWakeUpHandlerService
- implements PendingWakeUpOwnerHandler<AgentRunSuspensionEntity>, OnModuleInit
+ implements PendingWakeUpOwnerHandler<AgentRunSuspension>, OnModuleInit
{
constructor(
private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,
private readonly agentRunSuspensionService: AgentRunSuspensionService,
private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,
- private readonly conversationReaderService: AgentConversationReaderService,
- @InjectAgentHistoryRepository('agentMessagePart')
- private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,
) {}
onModuleInit(): void {
@@ -46,19 +31,11 @@ export class AgentRunPendingWakeUpHandlerService
async getOwnerState({
workspaceId,
- ownerId: suspensionId,
+ payload,
}: PendingWakeUpEntity): Promise<
- PendingWakeUpOwnerState<AgentRunSuspensionEntity>
+ PendingWakeUpOwnerState<AgentRunSuspension>
> {
- const suspension = await this.agentRunSuspensionService.findOne({
- workspaceId,
- where: { id: suspensionId },
- });
-
- if (!isDefined(suspension)) {
- return { status: 'GONE', owner: null };
- }
-
+ const suspension = payload as AgentRunSuspension;
const status = await this.callerHandlerRegistry
.getHandlerOrThrow(suspension.caller.type)
.getWaitingState({ workspaceId, caller: suspension.caller });
@@ -72,93 +49,37 @@ export class AgentRunPendingWakeUpHandlerService
owner: { caller },
}: {
wakeUp: PendingWakeUpEntity;
- owner: AgentRunSuspensionEntity;
+ owner: AgentRunSuspension;
}) {
return this.callerHandlerRegistry
.getHandlerOrThrow(caller.type)
.buildExecutionContext({ workspaceId, caller });
}
+ // the run claims its wake-up once it holds its conversation, so the conversation waits until it goes on
async resolve({
- claimedWakeUp: { workspaceId, ownerKey: toolCallId },
+ wakeUp,
outcome,
- owner: suspension,
isOwnerGone,
}: {
- claimedWakeUp: PendingWakeUpEntity;
+ wakeUp: PendingWakeUpEntity;
outcome: PendingWakeUpOutcome;
- owner: AgentRunSuspensionEntity | null;
isOwnerGone: boolean;
}): Promise<void> {
- if (!isDefined(suspension)) {
- return;
- }
-
if (isOwnerGone) {
- await this.agentRunSuspensionService.release({ workspaceId, suspension });
-
- return;
- }
-
- // the claimed wake-up is gone, so a run that cannot continue would wait forever
- try {
- await this.recordWaitOutcome({
- workspaceId,
- threadId: suspension.threadId,
- toolCallId,
- outcome,
+ await this.agentRunSuspensionService.release({
+ workspaceId: wakeUp.workspaceId,
+ wakeUp,
});
- await this.agentRunSuspensionService.scheduleContinuation({
- workspaceId,
- suspension,
- });
- } catch (error) {
- await this.agentRunSuspensionService.settle({
- workspaceId,
- suspension,
- outcome: {
- status: 'FAILED',
- error: `A waiting agent could not continue: ${error instanceof Error ? error.message : String(error)}`,
- },
- });
-
- throw error;
+ return;
}
- }
- // The wait call stays pending in the conversation until its wake-up resolves it, then carries the outcome
- private async recordWaitOutcome({
- workspaceId,
- threadId,
- toolCallId,
- outcome,
- }: {
- workspaceId: string;
- threadId: string;
- toolCallId: string;
- outcome: PendingWakeUpOutcome;
- }): Promise<void> {
- const pendingPart = await this.conversationReaderService.findToolPart({
- workspaceId,
- threadId,
- toolCallId,
+ await this.agentRunSuspensionService.scheduleContinuation({
+ workspaceId: wakeUp.workspaceId,
+ threadId: wakeUp.ownerId,
+ wakeUpId: wakeUp.id,
+ outcome,
});
-
- if (
- !isDefined(pendingPart) ||
- !isAwaitingPausingToolOutput(pendingPart.toolOutput)
- ) {
- throw new AiException(
- 'The waiting call could not be found in the conversation',
- AiExceptionCode.TOOL_CALL_NOT_FOUND,
- );
- }
-
- await this.messagePartRepository.update(
- workspaceId,
- { id: pendingPart.id },
- { toolOutput: buildWaitOutcomeToolOutput(outcome) },
- );
}
}
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.ts
index 7faa7f9ed18..492de313c61 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
@@ -1,28 +1,24 @@
import { Injectable } from '@nestjs/common';
-import isEqual from 'lodash.isequal';
import { type AgentRunSummary } from 'twenty-shared/ai';
+import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';
import { isDefined } from 'twenty-shared/utils';
import { IsNull, Not, Raw } from 'typeorm';
-import { type QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
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 { PendingWakeUpEntity } from 'src/engine/core-modules/pending-wake-up/entities/pending-wake-up.entity';
import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';
import { CONTINUE_AGENT_RUN_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-agent-execution/constants/continue-agent-run-job-name.constant';
-import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
import { AGENT_WAIT_TOOL_NAMES } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-tool-names.constant';
-import { readProposedToolCallAnswer } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/read-proposed-tool-call-answer.util';
import { buildWaitOutcomeToolOutput } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/build-wait-outcome-tool-output.util';
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 AgentRunCallerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type';
-import { type AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';
+import { type AgentRunSuspension } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type';
import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';
-import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';
import { AgentChatThreadLifecycleService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-thread-lifecycle.service';
-import { isUniqueViolationError } from 'src/engine/metadata-modules/ai/ai-chat/utils/is-unique-violation-error.util';
import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';
import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';
@@ -35,12 +31,16 @@ 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';
-// The suspended runs callers wait on: what continues them, and who gets their outcome
+// one key for every run, so the owner's unique key leaves a conversation one suspended run
+const SUSPENDED_RUN_WAKE_UP_KEY = 'RUN';
+
+// A suspended run is the AGENT_RUN wake-up its conversation owns: what wakes it up, and in its
+// payload what continues it and who gets its outcome
@Injectable()
export class AgentRunSuspensionService {
constructor(
- @InjectWorkspaceScopedRepository(AgentRunSuspensionEntity)
- private readonly suspensionRepository: WorkspaceScopedRepository<AgentRunSuspensionEntity>,
+ @InjectWorkspaceScopedRepository(PendingWakeUpEntity)
+ private readonly wakeUpRepository: WorkspaceScopedRepository<PendingWakeUpEntity>,
@InjectAgentHistoryRepository('agentChatThread')
private readonly threadRepository: AgentHistoryRepository<AgentChatThreadWorkspaceEntity>,
@InjectAgentHistoryRepository('agentMessagePart')
@@ -52,38 +52,50 @@ export class AgentRunSuspensionService {
private readonly messageQueueService: MessageQueueService,
) {}
- async findOne({
+ async suspend({
workspaceId,
- where,
+ threadId,
+ condition,
+ suspension,
}: {
workspaceId: string;
- where: { id: string } | { threadId: string };
- }): Promise<AgentRunSuspensionEntity | null> {
- return this.suspensionRepository.findOne(workspaceId, { where });
+ threadId: string;
+ condition: PendingWakeUpCondition;
+ suspension: AgentRunSuspension;
+ }): Promise<void> {
+ await this.pendingWakeUpService.arm({
+ workspaceId,
+ owner: {
+ type: 'AGENT_RUN',
+ id: threadId,
+ key: SUSPENDED_RUN_WAKE_UP_KEY,
+ },
+ condition,
+ payload: suspension,
+ });
}
- async suspend({
+ // a run waiting in the conversation would read a new message on continuing, and a caller waiting
+ // on a call it posted there would see the message close it
+ async isConversationWaiting({
workspaceId,
threadId,
- caller,
- runSpec,
- summary,
}: {
workspaceId: string;
threadId: string;
- caller: AgentRunCaller;
- runSpec: AgentRunSpec | null;
- summary: AgentRunSummary | null;
- }): Promise<AgentRunSuspensionEntity> {
- return this.suspensionRepository.insertAndReturnOne(workspaceId, {
- threadId,
- caller,
- runSpec,
- summary,
- } as QueryDeepPartialEntity<AgentRunSuspensionEntity>);
+ }): Promise<boolean> {
+ return this.wakeUpRepository.exists(workspaceId, {
+ where: [
+ { ownerType: 'AGENT_RUN', ownerId: threadId },
+ {
+ condition: Raw((alias) => `${alias} @> :condition::jsonb`, {
+ condition: JSON.stringify({ type: 'ANSWER', threadId }),
+ }),
+ },
+ ],
+ });
}
- // a new message must not slip in while a run waits in the conversation: the run would read it on continuing
async assertConversationNotSuspended({
workspaceId,
threadId,
@@ -91,9 +103,7 @@ export class AgentRunSuspensionService {
workspaceId: string;
threadId: string;
}): Promise<void> {
- const suspension = await this.findOne({ workspaceId, where: { threadId } });
-
- if (isDefined(suspension)) {
+ if (await this.isConversationWaiting({ workspaceId, threadId })) {
throw new AiException(
'The conversation is waiting on an earlier run; send the next message once it has finished',
AiExceptionCode.THREAD_AWAITING_ANSWER,
@@ -101,142 +111,34 @@ export class AgentRunSuspensionService {
}
}
- // A call the caller posted itself, such as a workflow step asking for approval: there is no
- // agent to continue, so the answer goes straight back to the caller. Posting again is a no-op
- async awaitCallerCall({
- workspaceId,
- threadId,
- caller,
- }: {
- workspaceId: string;
- threadId: string;
- caller: AgentRunCaller;
- }): Promise<void> {
- try {
- await this.suspend({
- workspaceId,
- threadId,
- caller,
- runSpec: null,
- summary: null,
- });
- } catch (error) {
- if (!isUniqueViolationError(error)) {
- throw error;
- }
-
- const existingSuspension = await this.findOne({
- workspaceId,
- where: { threadId },
- });
-
- // jsonb stores keys in its own order, so the stored caller is compared by value
- if (!isEqual(existingSuspension?.caller, caller)) {
- throw new AiException(
- 'The conversation is waiting on another run',
- AiExceptionCode.THREAD_AWAITING_ANSWER,
- );
- }
- }
- }
-
- async scheduleContinuation({
- workspaceId,
- suspension,
- }: {
- workspaceId: string;
- suspension: Pick<AgentRunSuspensionEntity, 'id' | 'resumeCount'>;
- }): Promise<void> {
+ async scheduleContinuation(jobData: ContinueAgentRunJobData): Promise<void> {
await this.messageQueueService.add<ContinueAgentRunJobData>(
CONTINUE_AGENT_RUN_JOB_NAME,
- {
- workspaceId,
- suspensionId: suspension.id,
- resumeCount: suspension.resumeCount,
- },
+ jobData,
);
}
- // The caller marks its call before it records the suspension, so a call without one is not ready yet
- async findWaitingSuspension({
- workspaceId,
- threadId,
- }: {
- workspaceId: string;
- threadId: string;
- }): Promise<
- | { status: 'NOT_READY' }
- | { status: 'WAITING' | 'GONE'; suspension: AgentRunSuspensionEntity }
- > {
- const suspension = await this.findOne({ workspaceId, where: { threadId } });
-
- if (!isDefined(suspension)) {
- return { status: 'NOT_READY' };
- }
-
- const status = await this.callerHandlerRegistry
- .getHandlerOrThrow(suspension.caller.type)
- .getWaitingState({ workspaceId, caller: suspension.caller });
-
- return status === 'NOT_READY' ? { status } : { status, suspension };
- }
-
- // The last answer continues the agent, or is itself the outcome of the call the caller proposed
- async deliverAnswer({
- workspaceId,
- suspension,
- toolResult,
- }: {
- workspaceId: string;
- suspension: AgentRunSuspensionEntity;
- toolResult: Record<string, unknown>;
- }): Promise<void> {
- if (isDefined(suspension.runSpec)) {
- await this.scheduleContinuation({ workspaceId, suspension });
-
- return;
- }
-
- const answer = readProposedToolCallAnswer(toolResult);
-
- await this.settle({
- workspaceId,
- suspension,
- outcome: isDefined(answer)
- ? {
- status: 'COMPLETED',
- result: { threadId: suspension.threadId, ...answer },
- }
- : {
- status: 'FAILED',
- error: 'The answer to the proposed call could not be read',
- },
- });
- }
-
// A run that ended, or could not go on, leaves nothing to wait on, and its caller gets the outcome
async settle({
workspaceId,
- suspension,
+ threadId,
+ suspension: { caller, summary: suspendedSummary },
outcome,
- summary = suspension.summary,
+ summary = suspendedSummary,
+ isAwaitingAnswer = false,
}: {
workspaceId: string;
- suspension: AgentRunSuspensionEntity;
+ threadId: string;
+ suspension: AgentRunSuspension;
outcome: AgentRunCallerOutcome;
summary?: AgentRunSummary | null;
+ isAwaitingAnswer?: boolean;
}): Promise<void> {
- await this.release({ workspaceId, suspension });
+ await this.closeAwaitedCalls({ workspaceId, threadId, isAwaitingAnswer });
await this.callerHandlerRegistry
- .getHandlerOrThrow(suspension.caller.type)
- .onOutcome?.({
- workspaceId,
- caller: suspension.caller,
- threadId: suspension.threadId,
- outcome,
- summary,
- });
+ .getHandlerOrThrow(caller.type)
+ .onOutcome?.({ workspaceId, caller, threadId, outcome, summary });
}
// A caller that stops waiting drops its suspended runs, such as every step of a run that ended
@@ -250,48 +152,74 @@ export class AgentRunSuspensionService {
ref: Partial<AgentRunCaller['ref']>;
};
}): Promise<void> {
- const suspensions = await this.suspensionRepository.find(workspaceId, {
+ const wakeUps = await this.wakeUpRepository.find(workspaceId, {
where: {
- caller: Raw((alias) => `${alias} @> :callerFilter::jsonb`, {
- callerFilter: JSON.stringify(caller),
+ ownerType: 'AGENT_RUN',
+ payload: Raw((alias) => `${alias} @> :payload::jsonb`, {
+ payload: JSON.stringify({ caller }),
}),
},
});
- for (const suspension of suspensions) {
- await this.release({ workspaceId, suspension });
+ for (const wakeUp of wakeUps) {
+ await this.release({ workspaceId, wakeUp });
}
}
- // The calls the run waits on are closed too, so they no longer look waiting
+ // A run that is dropped no longer waits, nor do the calls it waited on
async release({
workspaceId,
- suspension,
+ wakeUp: { id, ownerId: threadId, condition },
}: {
workspaceId: string;
- suspension: Pick<AgentRunSuspensionEntity, 'id' | 'threadId'>;
+ wakeUp: PendingWakeUpEntity;
}): Promise<void> {
- await this.pendingWakeUpService.cancel({
- workspaceId,
- owner: { type: 'AGENT_RUN', id: suspension.id },
- });
- await this.suspensionRepository.delete(workspaceId, { id: suspension.id });
- await this.closeAwaitedCalls({
+ if (
+ isDefined(
+ await this.pendingWakeUpService.claim({ workspaceId, wakeUpId: id }),
+ )
+ ) {
+ await this.closeAwaitedCalls({
+ workspaceId,
+ threadId,
+ isAwaitingAnswer: condition.type === 'ANSWER',
+ });
+ }
+ }
+
+ // The calls a dropped run waited on are closed, so they no longer look waiting. An answer holding
+ // the conversation's claim closes its calls itself, and a question the member asked of their own
+ // stays open, as only a run that waited on an answer closes the pending question
+ async closeAwaitedCalls({
+ workspaceId,
+ threadId,
+ isAwaitingAnswer,
+ }: {
+ workspaceId: string;
+ threadId: string;
+ isAwaitingAnswer: boolean;
+ }): Promise<void> {
+ await this.recordWaitOutcome({
workspaceId,
- threadId: suspension.threadId,
+ threadId,
+ outcome: { type: 'CANCELLED' },
});
+
+ if (isAwaitingAnswer) {
+ await this.closePendingQuestion({ workspaceId, threadId });
+ }
}
- // An answer holding the conversation's claim closes its calls itself once it finds its caller gone,
- // and a question the member asked of their own in the conversation stays open
- async closeAwaitedCalls({
+ // A wait call stays pending in the conversation until its wake-up resolves it, then carries the outcome
+ async recordWaitOutcome({
workspaceId,
threadId,
+ outcome,
}: {
workspaceId: string;
threadId: string;
+ outcome: Parameters<typeof buildWaitOutcomeToolOutput>[0];
}): Promise<void> {
- // a wait call is not a question, so it stays pending until its wake-up resolves it or its run is dropped
await this.messagePartRepository.query(workspaceId, ({ manager, table }) =>
manager.query(
`UPDATE ${table('agentMessagePart')} part SET "toolOutput" = $2::jsonb, "updatedAt" = now()
@@ -300,12 +228,42 @@ export class AgentRunSuspensionService {
AND part."toolName" = ANY($3) AND part."toolOutput"->'result'->>'status' = 'pending'`,
[
threadId,
- JSON.stringify(buildWaitOutcomeToolOutput({ type: 'CANCELLED' })),
+ JSON.stringify(buildWaitOutcomeToolOutput(outcome)),
AGENT_WAIT_TOOL_NAMES,
],
),
);
+ }
+
+ // A call a caller posted stops waiting with the caller's wake-up
+ async closePostedCalls({
+ workspaceId,
+ cancelledWakeUps,
+ }: {
+ workspaceId: string;
+ cancelledWakeUps: PendingWakeUpEntity[];
+ }): Promise<void> {
+ for (const { condition } of cancelledWakeUps) {
+ if (condition.type === 'ANSWER') {
+ await this.closePendingQuestion({
+ workspaceId,
+ threadId: condition.threadId,
+ toolCallId: condition.toolCallId,
+ });
+ }
+ }
+ }
+ // without a call to look for, the pending question is the one waited on
+ private async closePendingQuestion({
+ workspaceId,
+ threadId,
+ toolCallId,
+ }: {
+ workspaceId: string;
+ threadId: string;
+ toolCallId?: string;
+ }): Promise<void> {
const thread = await this.threadRepository.findOne(workspaceId, {
where: {
id: threadId,
@@ -314,19 +272,15 @@ export class AgentRunSuspensionService {
},
select: ['id', 'pendingQuestionMessageId'],
});
- const pendingQuestionMessageId = thread?.pendingQuestionMessageId;
-
- if (!isDefined(pendingQuestionMessageId)) {
- return;
- }
-
- const pendingParts = await this.messagePartRepository.find(workspaceId, {
- where: { messageId: pendingQuestionMessageId },
- select: ['toolOutput'],
- });
+ const messageId = thread?.pendingQuestionMessageId;
if (
- !pendingParts.some((part) => isToolOutputAwaitedByCaller(part.toolOutput))
+ !isDefined(messageId) ||
+ (isDefined(toolCallId) &&
+ !(await this.messagePartRepository.existsBy(workspaceId, {
+ messageId,
+ toolCallId,
+ })))
) {
return;
}
@@ -334,7 +288,7 @@ export class AgentRunSuspensionService {
await this.threadLifecycleService.closePendingQuestion({
workspaceId,
threadId,
- messageId: pendingQuestionMessageId,
+ messageId,
activeStreamId: null,
turnStatus: AgentTurnStatus.CANCELLED,
});
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.ts
index 4e88f1a6b77..b9ff8b02fd1 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
@@ -217,11 +217,11 @@ export class AgentRunService
});
return {
+ threadId,
+ status: outcome.status,
result: outcome.status === 'COMPLETED' ? outcome.result : null,
error: outcome.status === 'FAILED' ? outcome.error : null,
success: outcome.status !== 'FAILED',
- isWaiting: outcome.status === 'SUSPENDED',
- threadId,
};
} catch (error) {
if (
@@ -238,11 +238,11 @@ export class AgentRunService
);
return {
+ threadId,
+ status: 'FAILED',
result: null,
error: 'Agent execution failed.',
success: false,
- isWaiting: false,
- threadId,
};
}
}
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts
index 08e3b21c699..c0dab820307 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/services/agent-runner.service.ts
@@ -2,11 +2,10 @@ import { Injectable, Logger } from '@nestjs/common';
import { isNonEmptyString } from '@sniptt/guards';
import { type AgentRunSummary } from 'twenty-shared/ai';
+import { type PendingWakeUpCondition } from 'twenty-shared/pending-wake-up';
import { isDefined } from 'twenty-shared/utils';
-import { type QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';
-import { AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
import { AGENT_WAIT_PROMPT } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/constants/agent-wait-prompt.constant';
import { createAgentWaitTools } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/create-agent-wait-tools.util';
import { findAgentRunWait } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/wait-tools/find-agent-run-wait.util';
@@ -17,6 +16,7 @@ import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-age
import { type AgentExecutionResult } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-execution-result.type';
import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type';
import { type AgentRunnerRunInput } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-run-input.type';
+import { type AgentRunSuspension } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type';
import { type ContinueAgentRunJobData } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type';
import { buildAgentRunSummary } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/build-agent-run-summary.util';
import { sumAgentRunSummaries } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/sum-agent-run-summaries.util';
@@ -53,123 +53,129 @@ export class AgentRunnerService {
private readonly agentRunSuspensionService: AgentRunSuspensionService,
private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,
private readonly pendingWakeUpService: PendingWakeUpService,
- @InjectWorkspaceScopedRepository(AgentRunSuspensionEntity)
- private readonly suspensionRepository: WorkspaceScopedRepository<AgentRunSuspensionEntity>,
@InjectWorkspaceScopedRepository(AgentEntity)
private readonly agentRepository: WorkspaceScopedRepository<AgentEntity>,
) {}
+ // nothing else writes to the conversation while the run goes on: another run would read it half
+ // written, and a run that suspends would read a member's message once it goes on
run(input: AgentRunnerRunInput): Promise<AgentRunnerResult> {
- return this.runSegment({ input, suspension: null });
+ return this.agentRunConversationService.withThreadLock({
+ workspaceId: input.workspaceId,
+ threadId: input.conversation.threadId,
+ work: () => this.runTurn({ input, suspension: null }),
+ });
}
- // Picks a suspended run up where it paused, with its answer or wait outcome already in its conversation
- async continue({
+ // Picks a suspended run up where it paused once its wake-up resolved. The run claims the wake-up
+ // under its conversation's lock, so each pause goes on once and the conversation waits until it does
+ continue({
workspaceId,
- suspensionId,
- resumeCount,
+ threadId,
+ wakeUpId,
+ outcome,
}: ContinueAgentRunJobData): Promise<void> {
- const suspension = await this.agentRunSuspensionService.findOne({
+ return this.agentRunConversationService.withThreadLock({
workspaceId,
- where: { id: suspensionId },
- });
-
- if (
- !isDefined(suspension) ||
- !isDefined(suspension.runSpec) ||
- suspension.resumeCount !== resumeCount
- ) {
- return;
- }
+ threadId,
+ work: async () => {
+ const wakeUp = await this.pendingWakeUpService.claim({
+ workspaceId,
+ wakeUpId,
+ });
- const { caller, runSpec, threadId } = suspension;
- const handler = this.callerHandlerRegistry.getHandlerOrThrow(caller.type);
+ if (!isDefined(wakeUp)) {
+ return;
+ }
- if ((await handler.getWaitingState({ workspaceId, caller })) === 'GONE') {
- await this.agentRunSuspensionService.release({ workspaceId, suspension });
+ const suspension = wakeUp.payload as AgentRunSuspension;
+ const isAwaitingAnswer = wakeUp.condition.type === 'ANSWER';
+ const { caller, runSpec } = suspension;
+ const handler = this.callerHandlerRegistry.getHandlerOrThrow(
+ caller.type,
+ );
- return;
- }
+ let result: AgentRunnerResult;
- // each continuation moves the count on, so of two deliveries of one continuation only the first runs
- const { affected } = await this.suspensionRepository.update(
- workspaceId,
- { id: suspensionId, resumeCount },
- { resumeCount: resumeCount + 1 },
- );
+ // the wake-up is claimed, so a run that cannot go on is settled rather than left paused
+ try {
+ if (
+ (await handler.getWaitingState({ workspaceId, caller })) === 'GONE'
+ ) {
+ await this.agentRunSuspensionService.closeAwaitedCalls({
+ workspaceId,
+ threadId,
+ isAwaitingAnswer,
+ });
- if (affected === 0) {
- return;
- }
+ return;
+ }
- let result: AgentRunnerResult;
+ if (outcome.type === 'ANSWERED' && 'error' in outcome.answer) {
+ throw new Error(outcome.answer.error);
+ }
- try {
- const agent = isDefined(runSpec.agentId)
- ? await this.agentRepository.findOne(workspaceId, {
- where: { id: runSpec.agentId },
- })
- : null;
-
- if (isDefined(runSpec.agentId) && !isDefined(agent)) {
- throw new AiException(
- `Agent with id ${runSpec.agentId} not found`,
- AiExceptionCode.AGENT_NOT_FOUND,
- );
- }
-
- result = await this.runSegment({
- input: {
- workspaceId,
- conversation: { threadId, isCreated: false },
- caller,
- spec: runSpec,
- agent,
- prompt: null,
- executionContext: await handler.buildExecutionContext({
+ // a wait the run called alongside a question is over once the question is answered
+ await this.agentRunSuspensionService.recordWaitOutcome({
workspaceId,
- caller,
- }),
- },
- suspension,
- });
- } catch (error) {
- await this.agentRunSuspensionService.settle({
- workspaceId,
- suspension,
- outcome: {
- status: 'FAILED',
- error: error instanceof Error ? error.message : String(error),
- },
- });
+ threadId,
+ outcome:
+ outcome.type === 'ANSWERED' ? { type: 'CANCELLED' } : outcome,
+ });
+
+ const agent = isDefined(runSpec.agentId)
+ ? await this.agentRepository.findOne(workspaceId, {
+ where: { id: runSpec.agentId },
+ })
+ : null;
+
+ if (isDefined(runSpec.agentId) && !isDefined(agent)) {
+ throw new AiException(
+ `Agent with id ${runSpec.agentId} not found`,
+ AiExceptionCode.AGENT_NOT_FOUND,
+ );
+ }
+
+ result = await this.runTurn({
+ input: {
+ workspaceId,
+ conversation: { threadId, isCreated: false },
+ caller,
+ spec: runSpec,
+ agent,
+ prompt: null,
+ executionContext: await handler.buildExecutionContext({
+ workspaceId,
+ caller,
+ }),
+ },
+ suspension,
+ });
+ } catch (error) {
+ await this.agentRunSuspensionService.settle({
+ workspaceId,
+ threadId,
+ suspension,
+ outcome: {
+ status: 'FAILED',
+ error: error instanceof Error ? error.message : String(error),
+ },
+ isAwaitingAnswer,
+ });
- return;
- }
+ return;
+ }
- if (result.outcome.status !== 'SUSPENDED') {
- await this.agentRunSuspensionService.settle({
- workspaceId,
- suspension,
- outcome: result.outcome,
- summary: result.summary,
- });
- }
- }
-
- // nothing else writes to the conversation while the run goes on: another run would read it half
- // written, and a run that suspends would read a member's message once it goes on
- private runSegment({
- input,
- suspension,
- }: {
- input: AgentRunnerRunInput;
- // the suspension a continued run resumes from
- suspension: AgentRunSuspensionEntity | null;
- }): Promise<AgentRunnerResult> {
- return this.agentRunConversationService.withThreadLock({
- workspaceId: input.workspaceId,
- threadId: input.conversation.threadId,
- work: () => this.runTurn({ input, suspension }),
+ if (result.outcome.status !== 'SUSPENDED') {
+ await this.agentRunSuspensionService.settle({
+ workspaceId,
+ threadId,
+ suspension,
+ outcome: result.outcome,
+ summary: result.summary,
+ });
+ }
+ },
});
}
@@ -178,7 +184,8 @@ export class AgentRunnerService {
suspension,
}: {
input: AgentRunnerRunInput;
- suspension: AgentRunSuspensionEntity | null;
+ // what a continued run resumes from
+ suspension: AgentRunSuspension | null;
}): Promise<AgentRunnerResult> {
const {
workspaceId,
@@ -309,7 +316,7 @@ export class AgentRunnerService {
// null when the turn could not be recorded
closedTurn: { isAwaitingAnswer: boolean } | null;
summary: AgentRunSummary;
- suspension: AgentRunSuspensionEntity | null;
+ suspension: AgentRunSuspension | null;
}): Promise<AgentRunnerOutcome> {
if (!execution.hasNoMoreAvailableCredits && !execution.isPaused) {
return { status: 'COMPLETED', result: execution.result };
@@ -319,90 +326,91 @@ export class AgentRunnerService {
const wait = findAgentRunWait(
execution.steps[execution.steps.length - 1]?.toolResults ?? [],
);
- const hasPausedTooOften =
- isDefined(suspension) &&
- suspension.resumeCount + 1 >= AGENT_RUN_MAX_CONTINUATIONS;
+ const continuationCount = isDefined(suspension)
+ ? suspension.continuationCount + 1
+ : 0;
+ const hasPausedTooOften = continuationCount >= AGENT_RUN_MAX_CONTINUATIONS;
+ // a run waits on the questions it asked rather than on a wait it called alongside them
+ const condition: PendingWakeUpCondition | undefined =
+ closedTurn?.isAwaitingAnswer === true
+ ? { type: 'ANSWER', threadId }
+ : wait?.condition;
// continuing reads the conversation, so a pause it does not hold could never be continued.
// A failure to suspend only fails the run, like a pause that could not be recorded
- const suspensionId =
+ const isSuspended =
!execution.hasNoMoreAvailableCredits &&
!hasPausedTooOften &&
isDefined(closedTurn) &&
- (closedTurn.isAwaitingAnswer || isDefined(wait))
- ? await this.tryRecording(
- `suspend the agent run in thread ${threadId}`,
- async () => {
- if (!isDefined(suspension)) {
- return (
- await this.agentRunSuspensionService.suspend({
- workspaceId,
- threadId,
- caller,
- runSpec: spec,
- summary,
- })
- ).id;
- }
-
- // a caller that stopped waiting while the run went on has released it
- const { affected } = await this.suspensionRepository.update(
- workspaceId,
- { id: suspension.id },
- { summary } as QueryDeepPartialEntity<AgentRunSuspensionEntity>,
- );
-
- return affected === 0 ? null : suspension.id;
- },
- )
- : null;
+ isDefined(condition) &&
+ (await this.tryRecording(
+ `suspend the agent run in thread ${threadId}`,
+ async () => {
+ await this.agentRunSuspensionService.suspend({
+ workspaceId,
+ threadId,
+ condition,
+ suspension: { caller, runSpec: spec, summary, continuationCount },
+ });
+
+ // a caller that stopped waiting while a continued run went on found no wake-up to release,
+ // and it stops waiting before it releases, so reading it after the pause is saved misses none.
+ // A caller that cannot be read fails the run, rather than leave a pause no one may release
+ const isCallerGone =
+ isDefined(suspension) &&
+ (await this.callerHandlerRegistry
+ .getHandlerOrThrow(caller.type)
+ .getWaitingState({ workspaceId, caller })
+ .catch(() => 'GONE')) === 'GONE';
+
+ if (isCallerGone) {
+ await this.pendingWakeUpService.cancel({
+ workspaceId,
+ owner: { type: 'AGENT_RUN', id: threadId },
+ });
+ }
- if (!isDefined(suspensionId)) {
- // a pause the run could not keep leaves nothing to answer
- if (!isDefined(suspension)) {
- await this.agentRunSuspensionService.closeAwaitedCalls({
- workspaceId,
- threadId,
- });
- }
+ return !isCallerGone;
+ },
+ )) === true;
- if (execution.hasNoMoreAvailableCredits) {
- return {
- status: 'FAILED',
- error: 'Agent stopped: no more available credits.',
- };
- }
+ if (isSuspended) {
+ return { status: 'SUSPENDED' };
+ }
- if (hasPausedTooOften) {
- return {
- status: 'FAILED',
- error: `Agent stopped: it paused more than ${AGENT_RUN_MAX_CONTINUATIONS} times in one run.`,
- };
- }
+ // a pause the run could not keep leaves nothing to answer
+ await this.agentRunSuspensionService.closeAwaitedCalls({
+ workspaceId,
+ threadId,
+ isAwaitingAnswer: closedTurn?.isAwaitingAnswer === true,
+ });
- if (isDefined(wait)) {
- return {
- status: 'FAILED',
- error:
- 'Agent paused to wait but its conversation could not be recorded.',
- };
- }
+ if (execution.hasNoMoreAvailableCredits) {
+ return {
+ status: 'FAILED',
+ error: 'Agent stopped: no more available credits.',
+ };
+ }
+ if (hasPausedTooOften) {
return {
status: 'FAILED',
- error: 'Agent asked a question that could not be recorded.',
+ error: `Agent stopped: it paused more than ${AGENT_RUN_MAX_CONTINUATIONS} times in one run.`,
};
}
if (isDefined(wait)) {
- await this.pendingWakeUpService.arm({
- workspaceId,
- owner: { type: 'AGENT_RUN', id: suspensionId, key: wait.toolCallId },
- condition: wait.condition,
- });
+ return {
+ status: 'FAILED',
+ error:
+ 'Agent paused to wait but its conversation could not be recorded.',
+ };
}
- return { status: 'SUSPENDED', suspensionId };
+ return {
+ status: 'FAILED',
+ error: 'Agent asked a question that could not be recorded.',
+ };
}
// A record of the run, not its outcome, so a write failure must not fail the run
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.ts
index 8d69b798c48..f47621d71f0 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
@@ -22,7 +22,7 @@ export type AgentRunCallerHandler<
input: AgentRunCallerInput<TCaller> & {
threadId: string;
outcome: AgentRunCallerOutcome;
- // null for a call the caller posted itself, which no agent ran
+ // null when the run recorded none
summary: AgentRunSummary | null;
},
): Promise<void>;
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type.ts
index 56dc1c7f6e3..fd424c6acd7 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-caller-outcome.type.ts
@@ -1,6 +1,6 @@
import { type AgentRunnerOutcome } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type';
-// what a caller gets once it stops waiting: how its run ended, or the answer to a call it posted itself
+// what a caller gets once it stops waiting: how its run ended
export type AgentRunCallerOutcome = Exclude<
AgentRunnerOutcome,
{ status: 'SUSPENDED' }
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type.ts
new file mode 100644
index 00000000000..214944dfc9d
--- /dev/null
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-suspension.type.ts
@@ -0,0 +1,14 @@
+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 AgentRunSpec } from 'src/engine/metadata-modules/ai/ai-agent-execution/types/agent-run-spec.type';
+
+// What a run paused on an answer or a wait keeps in its wake-up: what continues it, and who gets its outcome
+export type AgentRunSuspension = {
+ caller: AgentRunCaller;
+ runSpec: AgentRunSpec;
+ // summed across the run's segments, so the caller gets one total
+ summary: AgentRunSummary | null;
+ // how many times the run went on, so one that keeps pausing is stopped
+ continuationCount: number;
+};
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts
index 0531a28f514..40df170d51e 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/agent-runner-outcome.type.ts
@@ -1,5 +1,5 @@
export type AgentRunnerOutcome =
| { status: 'COMPLETED'; result: object }
// paused on an answer or a wait; the engine continues the run and calls its caller back
- | { status: 'SUSPENDED'; suspensionId: string }
+ | { status: 'SUSPENDED' }
| { status: 'FAILED'; error: string };
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts
index bf046a6f5f4..37a167aa9a7 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/types/continue-agent-run-job-data.type.ts
@@ -1,6 +1,9 @@
+import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
+
export type ContinueAgentRunJobData = {
workspaceId: string;
- suspensionId: string;
- // the suspension's resume count when scheduled, so a duplicate job finds it moved on
- resumeCount: number;
+ threadId: string;
+ // the run's wake-up, which the continuation claims, and what resolved it
+ wakeUpId: string;
+ outcome: PendingWakeUpOutcome;
};
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts
deleted file mode 100644
index 73ecbaa4b20..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-import { isPlainObject } from 'twenty-shared/utils';
-
-export const isToolOutputAwaitedByCaller = (toolOutput: unknown): boolean =>
- isPlainObject(toolOutput) && toolOutput.awaitedByCaller === true;
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts
index 4a98528e32e..0479b3f8d99 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.claim.spec.ts
@@ -1,17 +1,12 @@
import { AgentChatStreamRecoveryService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-stream-recovery.service';
import { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
-import { AiExceptionCode } from 'src/engine/metadata-modules/ai/ai.exception';
+import {
+ AiException,
+ AiExceptionCode,
+} from 'src/engine/metadata-modules/ai/ai.exception';
import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
import { AgentChatStreamingService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service';
-const QUESTIONS = [
- {
- header: 'Plan',
- question: 'Which plan?',
- options: [{ label: 'Pro' }, { label: 'Team' }],
- },
-];
-
describe('AgentChatStreamingService claim & reap', () => {
const workspace = { id: 'workspace-id' } as WorkspaceEntity;
@@ -35,12 +30,12 @@ describe('AgentChatStreamingService claim & reap', () => {
claimAffected = 1,
queuedMessages = [] as unknown[],
heartbeatAlive = true,
- pendingToolOutput = { result: { questions: QUESTIONS, status: 'pending' } },
+ isConversationAwaited = false,
}: {
thread?: typeof idleThread & {
pendingQuestionMessageId?: string;
};
- pendingToolOutput?: Record<string, unknown>;
+ isConversationAwaited?: boolean;
claimAffected?: number;
queuedMessages?: unknown[];
heartbeatAlive?: boolean;
@@ -93,14 +88,6 @@ describe('AgentChatStreamingService claim & reap', () => {
const threadLifecycleService = {
closePendingQuestion: jest.fn().mockResolvedValue(undefined),
};
- const messagePartRepository = {
- find: jest.fn().mockResolvedValue([
- {
- id: 'part-id',
- toolOutput: pendingToolOutput,
- },
- ]),
- };
const eventPublisherService = {
publish: jest.fn().mockImplementation(({ event }) => {
publishedEvents.push(event);
@@ -149,13 +136,21 @@ describe('AgentChatStreamingService claim & reap', () => {
},
}),
} as never,
- messagePartRepository as never,
{
findLatestTurn: jest.fn().mockResolvedValue(null),
markRunning: jest.fn().mockResolvedValue(true),
} as never,
{
- assertConversationNotSuspended: jest.fn().mockResolvedValue(undefined),
+ assertConversationNotSuspended: isConversationAwaited
+ ? jest
+ .fn()
+ .mockRejectedValue(
+ new AiException(
+ 'awaited',
+ AiExceptionCode.THREAD_AWAITING_ANSWER,
+ ),
+ )
+ : jest.fn().mockResolvedValue(undefined),
} as never,
{ withThreadLockForMessage: jest.fn(({ work }) => work()) } as never,
threadLifecycleService as never,
@@ -171,7 +166,6 @@ describe('AgentChatStreamingService claim & reap', () => {
thread: thread as never,
...overrides,
}),
- messagePartRepository,
threadRepository,
messageQueueService,
agentChatService,
@@ -298,10 +292,7 @@ describe('AgentChatStreamingService claim & reap', () => {
messageQueueService,
} = buildService({
thread: waitingThread,
- pendingToolOutput: {
- result: { questions: QUESTIONS, status: 'pending' },
- awaitedByCaller: true,
- },
+ isConversationAwaited: true,
});
await expect(send()).rejects.toMatchObject({
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.opening-turn.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.opening-turn.spec.ts
index 517b1ef11bb..af42ee4a1f6 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.opening-turn.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.opening-turn.spec.ts
@@ -71,7 +71,6 @@ describe('AgentChatStreamingService.startOpeningTurn', () => {
{} as never,
{} as never,
{} as never,
- {} as never,
);
return {
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.retry.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.retry.spec.ts
index 7b75df6185e..45ecfff0981 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.retry.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.retry.spec.ts
@@ -105,11 +105,6 @@ describe('AgentChatStreamingService.retryLastFailedTurn', () => {
.fn()
.mockResolvedValue({ message: turnUserMessage }),
} as never,
- {
- findPendingForThread: jest.fn().mockResolvedValue([]),
- hasPendingForThread: jest.fn().mockResolvedValue(false),
- cancel: jest.fn().mockResolvedValue(false),
- } as never,
turnRecorderService as never,
{} as never,
{} as never,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.sender.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.sender.spec.ts
index e640920a7f7..e79d729aeed 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.sender.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/__tests__/agent-chat-streaming.service.sender.spec.ts
@@ -59,10 +59,6 @@ const build = () => {
{ hasUpgradedAgentHistory: jest.fn().mockResolvedValue(true) } as never,
),
actors as never,
- {
- findPendingForThread: jest.fn().mockResolvedValue([]),
- hasPendingForThread: jest.fn().mockResolvedValue(false),
- } as never,
{} as never,
{} as never,
{} as never,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts
index db93f3c48fc..b187d13e114 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat-streaming.service.ts
@@ -33,11 +33,9 @@ import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enum
import { AgentMessageStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-status.enum';
import { AgentRunConversationService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-conversation.service';
import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
-import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';
import { mapDBPartsToUIMessageParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-db-parts-to-ui-message-parts.util';
import { type BrowsingContextType } from 'src/engine/metadata-modules/ai/ai-agent/types/browsing-context.type';
import { AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';
-import { AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';
import { STREAM_AGENT_CHAT_JOB_NAME } from 'src/engine/metadata-modules/ai/ai-chat/jobs/stream-agent-chat-job-name.constant';
import { type StreamAgentChatJobData } from 'src/engine/metadata-modules/ai/ai-chat/jobs/stream-agent-chat-job.types';
import { AgentChatEventPublisherService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-event-publisher.service';
@@ -93,8 +91,6 @@ export class AgentChatStreamingService {
private readonly metricsService: MetricsService,
private readonly streamRecoveryService: AgentChatStreamRecoveryService,
private readonly actorService: AgentChatActorService,
- @InjectAgentHistoryRepository('agentMessagePart')
- private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,
private readonly turnRecorderService: AgentTurnRecorderService,
private readonly agentRunSuspensionService: AgentRunSuspensionService,
private readonly agentRunConversationService: AgentRunConversationService,
@@ -630,8 +626,8 @@ export class AgentChatStreamingService {
);
}
- // a message sent while the agent waits on a person closes its pending calls as skipped, so the model sees why
- // an answer holding the stream keeps them, and calls a caller waits on gate that caller, so a chat message never closes them
+ // a message sent while the agent waits on a person closes its pending calls as skipped, so the model sees why.
+ // An answer holding the stream keeps them, and a chat message never closes calls a run or a caller waits on
private async settlePendingToolCallsBeforeSending({
thread,
workspaceId,
@@ -644,17 +640,6 @@ export class AgentChatStreamingService {
}): Promise<void> {
const messageId = thread.pendingQuestionMessageId;
- if (
- isDefined(messageId) &&
- (await this.isAwaitingCaller({ messageId, workspaceId }))
- ) {
- throw new AiException(
- 'This conversation is waiting on an answer to the run that asked',
- AiExceptionCode.THREAD_AWAITING_ANSWER,
- );
- }
-
- // a run waiting on an event or a duration asks nothing, yet reads the conversation when it goes on
await this.agentRunSuspensionService.assertConversationNotSuspended({
workspaceId,
threadId: thread.id,
@@ -674,22 +659,6 @@ export class AgentChatStreamingService {
});
}
- // a call a caller such as a workflow step waits on gates that caller until it is answered
- private async isAwaitingCaller({
- messageId,
- workspaceId,
- }: {
- messageId: string;
- workspaceId: string;
- }): Promise<boolean> {
- const parts = await this.messagePartRepository.find(workspaceId, {
- where: { messageId },
- select: ['id', 'toolOutput'],
- });
-
- return parts.some((part) => isToolOutputAwaitedByCaller(part.toolOutput));
- }
-
private async enqueueStreamJob({
thread,
turnId,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
index c4f709dca68..86d2c83ffce 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-inbox.service.ts
@@ -79,12 +79,13 @@ export class AgentInboxService {
// A member who deleted the conversation has dismissed it, and a message
// that exists was already delivered.
if (isDefined(existingThread?.deletedAt)) {
- return { threadId, isDismissed: true };
+ return { threadId, toolCallId, isDismissed: true };
}
if (await this.messageExists({ workspaceId, id: messageId })) {
return {
threadId,
+ toolCallId,
isDismissed: false,
awaitedToolOutput: isDefined(buildAwaitingToolCall)
? await this.findToolOutput({ workspaceId, toolCallId })
@@ -163,14 +164,14 @@ export class AgentInboxService {
}
if (!isDefined(awaitingToolCall)) {
- return { threadId, isDismissed: false };
+ return { threadId, toolCallId, isDismissed: false };
}
const awaitedToolOutput = isWritten
? awaitingToolCall.output
: await this.findToolOutput({ workspaceId, toolCallId });
- return { threadId, isDismissed: false, awaitedToolOutput };
+ return { threadId, toolCallId, isDismissed: false, awaitedToolOutput };
}
// The thread key picks the sender's conversation with the member, so every
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/types/agent-inbox-delivery.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/types/agent-inbox-delivery.type.ts
index a6c47f0dcdf..2b92156a92a 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/types/agent-inbox-delivery.type.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/types/agent-inbox-delivery.type.ts
@@ -1,6 +1,8 @@
import { type SendInboxMessageResult } from 'twenty-shared/application';
export type AgentInboxDelivery = SendInboxMessageResult & {
+ // the call a message posted for an answer is found again under this id
+ toolCallId: string;
isDismissed: boolean;
// the awaited call as it stands now, which a message delivered earlier may have settled
awaitedToolOutput?: unknown;
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts
index 3255201b906..cb1feaf5bb0 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-writer.service.ts
@@ -10,7 +10,6 @@ import { IsNull } from 'typeorm';
import { findAwaitingPausingToolParts } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/find-awaiting-pausing-tool-parts.util';
import { mapAiStepsToUIMessageParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-ai-steps-to-ui-message-parts.util';
import { mapUIMessagePartsToDBParts } from 'src/engine/metadata-modules/ai/ai-history/utils/map-ui-message-parts-to-db-parts.util';
-import { stampPendingToolPartsAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util';
import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-message-role.enum';
import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';
@@ -235,10 +234,7 @@ export class AgentConversationWriterService {
isAwaitingAnswer: boolean;
replyParts: ExtendedUIMessagePart[];
}> {
- // every executed run has a caller waiting on the calls it pauses on
- const replyParts = stampPendingToolPartsAwaitedByCaller(
- mapAiStepsToUIMessageParts(execution.steps ?? []),
- );
+ const replyParts = mapAiStepsToUIMessageParts(execution.steps ?? []);
if (replyParts.length === 0) {
return { isAwaitingAnswer: false, replyParts };
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.ts
deleted file mode 100644
index 51c863b7841..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/__tests__/stamp-pending-tool-parts-awaited-by-caller.util.spec.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { type ExtendedUIMessagePart } from 'twenty-shared/ai';
-
-import { stampPendingToolPartsAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util';
-
-const toolPart = (status: string) =>
- ({
- type: 'tool-ask_question',
- toolCallId: `call-${status}`,
- state: 'output-available',
- input: {},
- output: { result: { status } },
- }) as unknown as ExtendedUIMessagePart;
-
-describe('stampPendingToolPartsAwaitedByCaller', () => {
- it('marks pending calls only', () => {
- const textPart = { type: 'text', text: 'Hello' } as ExtendedUIMessagePart;
-
- expect(
- stampPendingToolPartsAwaitedByCaller([
- textPart,
- toolPart('pending'),
- toolPart('answered'),
- ]),
- ).toEqual([
- textPart,
- {
- ...toolPart('pending'),
- output: { result: { status: 'pending' }, awaitedByCaller: true },
- },
- toolPart('answered'),
- ]);
- });
-});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.ts
deleted file mode 100644
index 9f2f39b86d9..00000000000
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/utils/stamp-pending-tool-parts-awaited-by-caller.util.ts
+++ /dev/null
@@ -1,21 +0,0 @@
-import { isToolUIPart } from 'ai';
-import { type ExtendedUIMessagePart } from 'twenty-shared/ai';
-import { isPlainObject } from 'twenty-shared/utils';
-
-import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';
-
-// A pending call a caller waits on is marked, so a chat message cannot close it and its
-// answer goes to the caller instead of starting a chat turn. The answer replaces the mark
-export const stampPendingToolPartsAwaitedByCaller = (
- parts: ExtendedUIMessagePart[],
-): ExtendedUIMessagePart[] =>
- parts.map((part) =>
- isToolUIPart(part) &&
- isPlainObject(part.output) &&
- isAwaitingPausingToolOutput(part.output)
- ? ({
- ...part,
- output: { ...part.output, awaitedByCaller: true },
- } as ExtendedUIMessagePart)
- : part,
- );
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts
index 5695afa118d..dff81c823be 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/ai-tool-call-answer.module.ts
@@ -1,5 +1,6 @@
import { Module } from '@nestjs/common';
+import { PendingWakeUpModule } from 'src/engine/core-modules/pending-wake-up/pending-wake-up.module';
import { ToolProviderModule } from 'src/engine/core-modules/tool-provider/tool-provider.module';
import { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module';
import { AgentChatThreadLifecycleModule } from 'src/engine/metadata-modules/ai/ai-chat/agent-chat-thread-lifecycle.module';
@@ -10,7 +11,7 @@ import { ToolCallAnswerResolver } from 'src/engine/metadata-modules/ai/ai-tool-c
import { ToolCallAnswerService } from 'src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service';
import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permissions.module';
-// an answer resumes a chat here, or continues the suspended run its caller waits on
+// an answer resumes a chat here, or resolves the wake-up of the run or caller that waits on it
@Module({
imports: [
AgentHistoryModule,
@@ -18,6 +19,7 @@ import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permi
AiChatModule,
AgentChatStreamStateModule,
AgentChatThreadLifecycleModule,
+ PendingWakeUpModule,
PermissionsModule,
ToolProviderModule,
],
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts
index bd2c5c7098b..3b19639bb45 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-tool-call-answer/services/tool-call-answer.service.ts
@@ -7,6 +7,10 @@ import { isDefined } from 'twenty-shared/utils';
import { isUserAuthContext } from 'src/engine/core-modules/auth/guards/is-user-auth-context.guard';
import { workspaceAuthContextStorage } from 'src/engine/core-modules/auth/storage/workspace-auth-context.storage';
+import { type 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 { PendingWakeUpResolverService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up-resolver.service';
+import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';
import { ToolRegistryService } from 'src/engine/core-modules/tool-provider/services/tool-registry.service';
import { type ToolContext } from 'src/engine/core-modules/tool-provider/types/tool-context.type';
import { type WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
@@ -14,6 +18,7 @@ import { AgentMessageRole } from 'src/engine/metadata-modules/ai/ai-history/enum
import { type PausingToolCompletionContext } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/types/pausing-tool-completion-context.type';
import { isAwaitingPausingToolOutput } from 'src/engine/metadata-modules/ai/ai-history/utils/is-awaiting-pausing-tool-output.util';
import { parsePausingToolCall } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/parse-pausing-tool-call.util';
+import { readProposedToolCallAnswer } from 'src/engine/metadata-modules/ai/ai-agent-execution/pausing-tools/utils/read-proposed-tool-call-answer.util';
import { AgentActorContextService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-actor-context.service';
import { AgentChatActorService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-actor.service';
import { AgentChatEventPublisherService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat-event-publisher.service';
@@ -33,9 +38,6 @@ import {
AiExceptionCode,
} from 'src/engine/metadata-modules/ai/ai.exception';
import { PermissionsService } from 'src/engine/metadata-modules/permissions/permissions.service';
-import { type AgentRunSuspensionEntity } from 'src/engine/metadata-modules/ai/ai-agent-execution/entities/agent-run-suspension.entity';
-import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
-import { isToolOutputAwaitedByCaller } from 'src/engine/metadata-modules/ai/ai-agent-execution/utils/is-tool-output-awaited-by-caller.util';
import { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
type AnswerToolCallArgs = {
@@ -67,7 +69,9 @@ export class ToolCallAnswerService {
private readonly threadService: AgentChatThreadService,
private readonly actorService: AgentChatActorService,
private readonly eventPublisherService: AgentChatEventPublisherService,
- private readonly agentRunSuspensionService: AgentRunSuspensionService,
+ private readonly pendingWakeUpService: PendingWakeUpService,
+ private readonly pendingWakeUpOwnerHandlerRegistry: PendingWakeUpOwnerHandlerRegistryService,
+ private readonly pendingWakeUpResolverService: PendingWakeUpResolverService,
private readonly permissionsService: PermissionsService,
private readonly turnPreflightService: AgentChatTurnPreflightService,
private readonly agentActorContextService: AgentActorContextService,
@@ -104,9 +108,13 @@ export class ToolCallAnswerService {
);
}
- // a caller such as a workflow step that waits on the call takes the answer instead of a chat turn.
- // An answer replaces that output, so a call no longer pending starts nothing and is refused below
- const isAwaitedByCaller = isToolOutputAwaitedByCaller(toolPart.toolOutput);
+ // a suspended run, or a caller such as a workflow step, that waits on the call takes the answer
+ // instead of a chat turn. A call no longer pending starts nothing and is refused below
+ const wakeUp = await this.pendingWakeUpService.findAnswerWakeUp({
+ workspaceId,
+ threadId,
+ toolCallId,
+ });
const isToolCallPending =
thread.pendingQuestionMessageId === toolPart.messageId &&
isAwaitingPausingToolOutput(toolPart.toolOutput);
@@ -114,7 +122,7 @@ export class ToolCallAnswerService {
await this.assertCanAnswerInChat({
...args,
messageId: toolPart.messageId,
- isStartingChatTurn: !isAwaitedByCaller && isToolCallPending,
+ isStartingChatTurn: !isDefined(wakeUp) && isToolCallPending,
});
if (!isToolCallPending) {
@@ -153,7 +161,6 @@ export class ToolCallAnswerService {
);
}
- let awaitingSuspension: AgentRunSuspensionEntity | null = null;
let isClaimedAsRunning = false;
let isLastAnswer: boolean;
let answerText: string;
@@ -170,14 +177,12 @@ export class ToolCallAnswerService {
throw this.notPending();
}
- if (isAwaitedByCaller) {
- const waitingState =
- await this.agentRunSuspensionService.findWaitingSuspension({
- workspaceId,
- threadId,
- });
+ if (isDefined(wakeUp)) {
+ const { status } = await this.pendingWakeUpOwnerHandlerRegistry
+ .getHandlerOrThrow(wakeUp.ownerType)
+ .getOwnerState(wakeUp);
- if (waitingState.status === 'NOT_READY') {
+ if (status === 'NOT_READY') {
throw new AiException(
'The run waiting on this call is not ready for an answer yet',
AiExceptionCode.TOOL_CALL_NOT_PENDING,
@@ -187,8 +192,8 @@ export class ToolCallAnswerService {
);
}
- if (waitingState.status === 'GONE') {
- // the run waiting on the call was dropped, as when its suspension is released
+ if (status === 'GONE') {
+ // what waited on the call was dropped, such as a workflow run that ended
await this.threadLifecycleService.closePendingQuestion({
workspaceId,
threadId,
@@ -197,15 +202,13 @@ export class ToolCallAnswerService {
turnStatus: AgentTurnStatus.CANCELLED,
});
- await this.agentRunSuspensionService.release({
+ await this.pendingWakeUpResolverService.resolve({
workspaceId,
- suspension: waitingState.suspension,
+ wakeUpId: wakeUp.id,
});
throw this.notPending();
}
-
- awaitingSuspension = waitingState.suspension;
}
const runningToolResult = pausingToolCall.toRunningToolResult?.(
@@ -270,7 +273,7 @@ export class ToolCallAnswerService {
threadId,
workspaceId,
streamId,
- awaitingSuspension,
+ wakeUp,
error,
});
} else {
@@ -290,7 +293,7 @@ export class ToolCallAnswerService {
threadId,
userWorkspaceId,
turnStatus:
- isAwaitedByCaller || !isLastAnswer
+ isDefined(wakeUp) || !isLastAnswer
? AgentTurnStatus.COMPLETED
: AgentTurnStatus.RUNNING,
uiMessage: {
@@ -319,18 +322,25 @@ export class ToolCallAnswerService {
);
// a suspended run continues in its own job, and only the last answer resumes a chat
- if (isAwaitedByCaller || !isLastAnswer) {
+ if (isDefined(wakeUp) || !isLastAnswer) {
await this.streamRecoveryService.releaseStreamClaim({
threadId,
workspaceId,
streamId,
});
- if (isLastAnswer && isDefined(awaitingSuspension)) {
- await this.agentRunSuspensionService.deliverAnswer({
+ if (isLastAnswer && isDefined(wakeUp)) {
+ const proposedAnswer = readProposedToolCallAnswer(toolResult);
+
+ // a posted call is answered with the decision on what it proposed
+ await this.pendingWakeUpResolverService.resolve({
workspaceId,
- suspension: awaitingSuspension,
- toolResult,
+ wakeUpId: wakeUp.id,
+ answer: {
+ result: isDefined(proposedAnswer)
+ ? { threadId, ...proposedAnswer }
+ : toolResult,
+ },
});
}
@@ -354,7 +364,7 @@ export class ToolCallAnswerService {
threadId,
workspaceId,
streamId,
- awaitingSuspension,
+ wakeUp,
error,
});
@@ -366,16 +376,16 @@ export class ToolCallAnswerService {
threadId,
workspaceId,
streamId,
- awaitingSuspension,
+ wakeUp,
error,
}: {
threadId: string;
workspaceId: string;
streamId: string;
- awaitingSuspension: AgentRunSuspensionEntity | null;
+ wakeUp: PendingWakeUpEntity | null;
error: unknown;
}): Promise<void> {
- if (!isDefined(awaitingSuspension)) {
+ if (!isDefined(wakeUp)) {
await this.streamRecoveryService.failStream({
threadId,
workspaceId,
@@ -391,11 +401,10 @@ export class ToolCallAnswerService {
workspaceId,
streamId,
});
- await this.agentRunSuspensionService.settle({
+ await this.pendingWakeUpResolverService.resolve({
workspaceId,
- suspension: awaitingSuspension,
- outcome: {
- status: 'FAILED',
+ wakeUpId: wakeUp.id,
+ answer: {
error: 'The run could not resume after its question was answered',
},
});
diff --git a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts
index 2e57b1ea47d..b1643c0ba76 100644
--- a/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts
+++ b/packages/twenty-server/src/engine/workspace-manager/dev-seeder/data/services/dev-seeder-workflow-pending-input.workspace-service.ts
@@ -30,10 +30,12 @@ import { WorkflowRunStatus } from 'src/modules/workflow/common/standard-objects/
import { type WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { type WorkflowWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
import { buildWorkflowStepCaller } from 'src/modules/workflow/workflow-executor/utils/build-workflow-step-caller.util';
-import { isWorkflowAiAgentAction } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/guards/is-workflow-ai-agent-action.guard';
import { buildWorkflowAgentRunSpec } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/utils/build-workflow-agent-run-spec.util';
import { WorkflowAgentConversationWorkspaceService } from 'src/modules/workflow/workflow-executor/workflow-actions/ai-agent/services/workflow-agent-conversation.workspace-service';
-import { type WorkflowAction } from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
+import {
+ type WorkflowAction,
+ type WorkflowAiAgentAction,
+} from 'src/modules/workflow/workflow-executor/workflow-actions/types/workflow-action.type';
import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runner/workflow-run/workflow-run.workspace-service';
import {
type WorkflowManualTrigger,
@@ -189,6 +191,21 @@ export class DevSeederWorkflowPendingInputWorkspaceService {
applicationId: string;
}): Promise<void> {
for (const agentWorkflow of AGENT_WORKFLOWS_TO_SEED) {
+ const step: WorkflowAiAgentAction = {
+ id: seedId(agentWorkflow.stepKey, workspaceId),
+ name: agentWorkflow.stepName,
+ type: WorkflowActionType.AI_AGENT,
+ valid: true,
+ settings: {
+ input: {
+ prompt: agentWorkflow.stepPrompt,
+ humanInputInstructions: agentWorkflow.humanInputInstructions,
+ },
+ outputSchema: {},
+ errorHandlingOptions: ERROR_HANDLING_OPTIONS,
+ },
+ nextStepIds: [],
+ };
const workflow = await this.insertWorkflow({
workspaceId,
applicationId,
@@ -196,21 +213,7 @@ export class DevSeederWorkflowPendingInputWorkspaceService {
name: agentWorkflow.name,
position: agentWorkflow.position,
icon: agentWorkflow.icon,
- step: {
- id: seedId(agentWorkflow.stepKey, workspaceId),
- name: agentWorkflow.stepName,
- type: WorkflowActionType.AI_AGENT,
- valid: true,
- settings: {
- input: {
- prompt: agentWorkflow.stepPrompt,
- humanInputInstructions: agentWorkflow.humanInputInstructions,
- },
- outputSchema: {},
- errorHandlingOptions: ERROR_HANDLING_OPTIONS,
- },
- nextStepIds: [],
- },
+ step,
});
const workflowRunId = seedId(
`workflowRun:${agentWorkflow.runKey}`,
@@ -290,17 +293,19 @@ export class DevSeederWorkflowPendingInputWorkspaceService {
await this.agentRunSuspensionService.suspend({
workspaceId,
threadId,
- caller: buildWorkflowStepCaller({
- workflowRunId,
- stepId: workflow.step.id,
- }),
- runSpec: isWorkflowAiAgentAction(workflow.step)
- ? buildWorkflowAgentRunSpec({
- step: workflow.step,
- isApplicationBound: false,
- })
- : null,
- summary: null,
+ condition: { type: 'ANSWER', threadId },
+ suspension: {
+ caller: buildWorkflowStepCaller({
+ workflowRunId,
+ stepId: step.id,
+ }),
+ runSpec: buildWorkflowAgentRunSpec({
+ step,
+ isApplicationBound: false,
+ }),
+ summary: null,
+ continuationCount: 0,
+ },
});
await this.agentRunConversationService.closeTurn({
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts
index c222968f684..b5769f88c79 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/__tests__/send-chat-message.workflow-action.spec.ts
@@ -99,6 +99,36 @@ describe('SendChatMessageWorkflowAction', () => {
});
});
+ it('waits on the answer to the call it posted', async () => {
+ sendMessage.mockResolvedValue({
+ status: 'AWAITING',
+ threadId: 'thread-id',
+ toolCallId: 'call-id',
+ });
+
+ const output = await execute({
+ workspaceMemberId: WORKSPACE_MEMBER_ID,
+ text: 'Approve the update?',
+ toolCall: { toolName: 'update_one_company', arguments: { id: 'id' } },
+ });
+
+ expect(output).toEqual({
+ wait: { type: 'ANSWER', threadId: 'thread-id', toolCallId: 'call-id' },
+ });
+ expect(sendMessage).toHaveBeenCalledWith(
+ expect.objectContaining({
+ awaitedToolCall: {
+ toolName: 'update_one_company',
+ arguments: { id: 'id' },
+ caller: {
+ type: 'WORKFLOW_STEP',
+ ref: { workflowRunId: WORKFLOW_RUN_ID, stepId: 'step-1' },
+ },
+ },
+ }),
+ );
+ });
+
it('sends each iteration once, even when two iterations say the same thing', async () => {
const input = {
workspaceMemberId: WORKSPACE_MEMBER_ID,
diff --git a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts
index e4dfa8e3ca3..9a26aa6087e 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-executor/workflow-actions/send-chat-message/send-chat-message.workflow-action.ts
@@ -109,9 +109,15 @@ export class SendChatMessageWorkflowAction implements WorkflowAction {
'The recipient deleted this conversation, so the action cannot be approved',
WorkflowStepExecutorExceptionCode.INVALID_STEP_INPUT,
);
- // the step waits for the member, and the engine hands it their answer
+ // the step waits for the member, and their answer resolves the wait
case 'AWAITING':
- return { wait: { type: 'CALLBACK' } };
+ return {
+ wait: {
+ type: 'ANSWER',
+ threadId: delivery.threadId,
+ toolCallId: delivery.toolCallId,
+ },
+ };
case 'ANSWERED':
return { result: { threadId: delivery.threadId, ...delivery.answer } };
}
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.ts
index 711b09279f7..6b50395ce67 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
@@ -24,8 +24,7 @@ import { WorkflowRunWorkspaceService } from 'src/modules/workflow/workflow-runne
type WorkflowStepCaller = Extract<AgentRunCaller, { type: 'WORKFLOW_STEP' }>;
-// A step waiting on a CALLBACK takes the outcome of what it handed its work to: the agent run the
-// engine continued, or the answer to a call the step posted itself
+// A step waiting on a CALLBACK takes the outcome of the agent run the engine continued
@Injectable()
export class WorkflowAgentRunCallerHandlerWorkspaceService
implements AgentRunCallerHandler<WorkflowStepCaller>, OnModuleInit
diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts
index d21fd46114c..20ffba5279e 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/__tests__/workflow-step-pending-wake-up-handler.workspace-service.spec.ts
@@ -85,6 +85,7 @@ const buildService = ({
new WorkflowStepPendingWakeUpHandlerWorkspaceService(
registry,
+ pendingWakeUpService as never,
workflowRunWorkspaceService as never,
workflowExecutionContextService as never,
messageQueueService as never,
@@ -148,6 +149,62 @@ describe('WorkflowStepPendingWakeUpHandlerWorkspaceService', () => {
);
});
+ it('ends the step with the answer to the call it posted through the run job', async () => {
+ const { service, workflowRunWorkspaceService, messageQueueService } =
+ buildService({
+ storedWait: {
+ ...STORED_WAIT,
+ condition: {
+ type: 'ANSWER',
+ threadId: 'thread-id',
+ toolCallId: 'call-id',
+ },
+ },
+ });
+
+ await service.resolve({
+ workspaceId: WORKSPACE_ID,
+ wakeUpId: WAIT_ID,
+ answer: { result: { threadId: 'thread-id', outcome: 'executed' } },
+ });
+
+ expect(
+ workflowRunWorkspaceService.updateStepInfoIfPending,
+ ).not.toHaveBeenCalled();
+ expect(messageQueueService.add).toHaveBeenCalledWith(
+ RUN_WORKFLOW_JOB_NAME,
+ {
+ workspaceId: WORKSPACE_ID,
+ workflowRunId: WORKFLOW_RUN_ID,
+ awaitedStepOutput: {
+ stepId: STEP_ID,
+ actionOutput: {
+ result: { threadId: 'thread-id', outcome: 'executed' },
+ },
+ },
+ },
+ expect.anything(),
+ );
+ });
+
+ it('keeps an answer for later when the step does not wait yet', async () => {
+ const { service, pendingWakeUpService } = buildService({
+ stepStatus: StepStatus.RUNNING,
+ });
+ const answer = { error: 'The answer could not be delivered' };
+
+ await service.resolve({
+ workspaceId: WORKSPACE_ID,
+ wakeUpId: WAIT_ID,
+ answer,
+ });
+
+ expect(pendingWakeUpService.claim).not.toHaveBeenCalled();
+ expect(pendingWakeUpService.scheduleResolution).toHaveBeenCalledWith(
+ expect.objectContaining({ answer, attempt: 1 }),
+ );
+ });
+
it('keeps waiting when the run cannot read the record of the event', async () => {
const { service, pendingWakeUpService, workflowRunWorkspaceService } =
buildService({ readableRecords: [] });
diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts
index e348db270fd..35f07debf02 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-pending-wake-up-handler.workspace-service.ts
@@ -9,6 +9,7 @@ import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queu
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { type 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 { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';
import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
import { type PendingWakeUpOwnerHandler } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-handler.type';
import { type PendingWakeUpOwnerState } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-owner-state.type';
@@ -34,6 +35,7 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceService
{
constructor(
private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,
+ private readonly pendingWakeUpService: PendingWakeUpService,
private readonly workflowRunWorkspaceService: WorkflowRunWorkspaceService,
private readonly workflowExecutionContextService: WorkflowExecutionContextService,
@InjectMessageQueue(MessageQueue.workflowQueue)
@@ -81,28 +83,49 @@ export class WorkflowStepPendingWakeUpHandlerWorkspaceService
}
async resolve({
- claimedWakeUp,
+ wakeUp,
outcome,
owner: workflowRun,
isOwnerGone,
}: {
- claimedWakeUp: PendingWakeUpEntity;
+ wakeUp: PendingWakeUpEntity;
outcome: PendingWakeUpOutcome;
owner: WorkflowRunWorkspaceEntity | null;
isOwnerGone: boolean;
}): Promise<void> {
- if (!isDefined(workflowRun) || isOwnerGone) {
+ const { workspaceId, ownerId: workflowRunId, ownerKey: stepId } = wakeUp;
+
+ if (
+ !isDefined(
+ await this.pendingWakeUpService.claim({
+ workspaceId,
+ wakeUpId: wakeUp.id,
+ }),
+ ) ||
+ !isDefined(workflowRun) ||
+ isOwnerGone
+ ) {
return;
}
- const {
- workspaceId,
- ownerId: workflowRunId,
- ownerKey: stepId,
- } = claimedWakeUp;
-
// the claimed wait is gone, so a step that cannot resume would wait forever
try {
+ // an answered call ends the step through the executor's usual path, so a failure is retried
+ // or continues on failure like any failed step
+ if (outcome.type === 'ANSWERED') {
+ await this.messageQueueService.add<RunWorkflowJobData>(
+ RUN_WORKFLOW_JOB_NAME,
+ {
+ workspaceId,
+ workflowRunId,
+ awaitedStepOutput: { stepId, actionOutput: outcome.answer },
+ },
+ buildRunWorkflowJobOptions(workflowRunId),
+ );
+
+ return;
+ }
+
const hasCompletedStep =
await this.workflowRunWorkspaceService.updateStepInfoIfPending({
stepId,
diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts
index 15c5553ac30..fcc3606a227 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service.ts
@@ -3,10 +3,14 @@ import { Injectable } from '@nestjs/common';
import { type WorkflowStepWait } from 'twenty-shared/workflow';
import { PendingWakeUpService } from 'src/engine/core-modules/pending-wake-up/services/pending-wake-up.service';
+import { AgentRunSuspensionService } from 'src/engine/metadata-modules/ai/ai-agent-execution/services/agent-run-suspension.service';
@Injectable()
export class WorkflowStepWaitWorkspaceService {
- constructor(private readonly pendingWakeUpService: PendingWakeUpService) {}
+ constructor(
+ private readonly pendingWakeUpService: PendingWakeUpService,
+ private readonly agentRunSuspensionService: AgentRunSuspensionService,
+ ) {}
// A callback wait is resolved by what the step handed its work to, so it is not stored
async arm({
@@ -40,7 +44,7 @@ export class WorkflowStepWaitWorkspaceService {
workflowRunId: string;
stepId: string;
}): Promise<void> {
- await this.pendingWakeUpService.cancel({
+ await this.cancel({
workspaceId,
owner: { type: 'WORKFLOW_STEP', id: workflowRunId, key: stepId },
});
@@ -53,9 +57,19 @@ export class WorkflowStepWaitWorkspaceService {
workspaceId: string;
workflowRunId: string;
}): Promise<void> {
- await this.pendingWakeUpService.cancel({
+ await this.cancel({
workspaceId,
owner: { type: 'WORKFLOW_STEP', id: workflowRunId },
});
}
+
+ // a call a step posted no longer looks waiting once the step stops waiting on its answer
+ private async cancel(
+ args: Parameters<PendingWakeUpService['cancel']>[0],
+ ): Promise<void> {
+ await this.agentRunSuspensionService.closePostedCalls({
+ workspaceId: args.workspaceId,
+ cancelledWakeUps: await this.pendingWakeUpService.cancel(args),
+ });
+ }
}
diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/build-default-wait-result.util.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/utils/build-default-wait-result.util.ts
index 0f058ee5ebc..38ef2919aa1 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-wait/utils/build-default-wait-result.util.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/utils/build-default-wait-result.util.ts
@@ -1,7 +1,7 @@
import { type PendingWakeUpOutcome } from 'src/engine/core-modules/pending-wake-up/types/pending-wake-up-outcome.type';
export const buildDefaultWaitResult = (
- outcome: PendingWakeUpOutcome,
+ outcome: Exclude<PendingWakeUpOutcome, { type: 'ANSWERED' }>,
): object => {
switch (outcome.type) {
case 'TIME_ELAPSED':
diff --git a/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-step-wait.module.ts b/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-step-wait.module.ts
index 2a1edcec470..cba314634be 100644
--- a/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-step-wait.module.ts
+++ b/packages/twenty-server/src/modules/workflow/workflow-wait/workflow-step-wait.module.ts
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
import { PendingWakeUpModule } from 'src/engine/core-modules/pending-wake-up/pending-wake-up.module';
+import { AiAgentExecutionModule } from 'src/engine/metadata-modules/ai/ai-agent-execution/ai-agent-execution.module';
import { WorkflowStepWaitWorkspaceService } from 'src/modules/workflow/workflow-wait/services/workflow-step-wait.workspace-service';
@Module({
- imports: [PendingWakeUpModule],
+ imports: [AiAgentExecutionModule, PendingWakeUpModule],
providers: [WorkflowStepWaitWorkspaceService],
exports: [WorkflowStepWaitWorkspaceService],
})
diff --git a/packages/twenty-server/test/integration/ai/suites/agent-run-wait.integration-spec.ts b/packages/twenty-server/test/integration/ai/suites/agent-run-wait.integration-spec.ts
index 1a8ff3def48..9267ba92f60 100644
--- a/packages/twenty-server/test/integration/ai/suites/agent-run-wait.integration-spec.ts
+++ b/packages/twenty-server/test/integration/ai/suites/agent-run-wait.integration-spec.ts
@@ -101,9 +101,9 @@ const mockAgent = (...results: AgentExecutionResult[]) =>
const findSuspensions = (callerType: string, agentId: string) =>
global.testDataSource.query(
- `SELECT suspension.id, suspension."threadId", wake_up."ownerType" FROM core."agentRunSuspension" suspension
- LEFT JOIN core."pendingWakeUp" wake_up ON wake_up."ownerId" = suspension.id
- WHERE suspension.caller->>'type' = $1 AND suspension.caller->'ref'->>'agentId' = $2`,
+ `SELECT id, "ownerId" AS "threadId", "ownerType", condition FROM core."pendingWakeUp"
+ WHERE "ownerType" = 'AGENT_RUN' AND payload->'caller'->>'type' = $1
+ AND payload->'caller'->'ref'->>'agentId' = $2`,
[callerType, agentId],
);
@@ -184,7 +184,10 @@ describe('agent runs that wait (integration)', () => {
const [suspension] = await findSuspensions('AGENT_TRIGGER', agentId);
- expect(suspension).toMatchObject({ ownerType: 'AGENT_RUN' });
+ expect(suspension).toMatchObject({
+ ownerType: 'AGENT_RUN',
+ condition: { type: 'TIME' },
+ });
// the run reads its conversation when it goes on, so a chat message cannot slip in while it waits
await expect(
@@ -204,18 +207,18 @@ describe('agent runs that wait (integration)', () => {
).toEqual(['wait_for_event', 'wait_for_duration']);
await expectEventually(async () => {
- expect(await findSuspensions('AGENT_TRIGGER', agentId)).toEqual([]);
+ expect(await findTurnStatuses(suspension.threadId)).toEqual([
+ 'completed',
+ 'completed',
+ ]);
});
+ expect(await findSuspensions('AGENT_TRIGGER', agentId)).toEqual([]);
expect(executeAgent).toHaveBeenCalledTimes(2);
expect(executeAgent.mock.calls[1][0].messages).toEqual([]);
expect(
JSON.stringify(executeAgent.mock.calls[1][0].priorMessages),
).toContain('The wait is over.');
- expect(await findTurnStatuses(suspension.threadId)).toEqual([
- 'completed',
- 'completed',
- ]);
});
it('refuses a chat message while a run goes on in its conversation', async () => {
@@ -274,7 +277,7 @@ describe('agent runs that wait (integration)', () => {
await runTrigger();
- const [{ id: suspensionId, threadId }] = await findSuspensions(
+ const [{ id: wakeUpId, threadId }] = await findSuspensions(
'AGENT_TRIGGER',
agentId,
);
@@ -289,11 +292,6 @@ describe('agent runs that wait (integration)', () => {
},
});
- const [{ id: wakeUpId }] = await global.testDataSource.query(
- `SELECT id FROM core."pendingWakeUp" WHERE "ownerId" = $1`,
- [suspensionId],
- );
-
await getAppProviderByClassName<PendingWakeUpResolverService>(
'PendingWakeUpResolverService',
).resolve({ workspaceId, wakeUpId });
@@ -316,11 +314,11 @@ describe('agent runs that wait (integration)', () => {
query: gql`
mutation RunAgent($input: RunAgentInput!) {
runAgent(input: $input) {
+ threadId
+ status
result
error
success
- isWaiting
- threadId
}
}
`,
@@ -334,21 +332,21 @@ describe('agent runs that wait (integration)', () => {
expect(response.body.errors).toBeUndefined();
expect(response.body.data.runAgent).toEqual({
+ threadId: expect.any(String),
+ status: 'SUSPENDED',
result: null,
error: null,
success: true,
- isWaiting: true,
- threadId: expect.any(String),
});
await expectEventually(async () => {
- expect(await findSuspensions('AGENT_API_RUN', agentId)).toEqual([]);
+ expect(
+ await findTurnStatuses(response.body.data.runAgent.threadId),
+ ).toEqual(['completed', 'completed']);
});
+ expect(await findSuspensions('AGENT_API_RUN', agentId)).toEqual([]);
expect(executeAgent).toHaveBeenCalledTimes(2);
- expect(
- await findTurnStatuses(response.body.data.runAgent.threadId),
- ).toEqual(['completed', 'completed']);
});
it('refuses a message to a thread whose run waits, and takes it once the run finished', async () => {
@@ -384,7 +382,7 @@ describe('agent runs that wait (integration)', () => {
const waiting = await runOnSameThread('Follow up tomorrow');
- expect(waiting).toMatchObject({ success: true, isWaiting: true });
+ expect(waiting).toMatchObject({ success: true, status: 'SUSPENDED' });
await expect(runOnSameThread('Any news?')).rejects.toMatchObject({
code: AiExceptionCode.THREAD_AWAITING_ANSWER,
@@ -396,7 +394,7 @@ describe('agent runs that wait (integration)', () => {
await expect(runOnSameThread('Any news?')).resolves.toMatchObject({
success: true,
- isWaiting: false,
+ status: 'COMPLETED',
result: { response: 'Followed up' },
threadId: waiting.threadId,
});
diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
index a3e2fabf39f..82f5f296323 100644
--- a/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/core-workflow-execution.integration-spec.ts
@@ -1587,10 +1587,10 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
});
expect(
await global.testDataSource.query(
- `SELECT "ownerType", "ownerKey" FROM core."pendingWakeUp" WHERE "ownerId" = (SELECT id FROM core."agentRunSuspension" WHERE "threadId" = $1)`,
+ `SELECT "ownerType", condition->>'type' AS "conditionType" FROM core."pendingWakeUp" WHERE "ownerId" = $1`,
[threadId],
),
- ).toEqual([{ ownerType: 'AGENT_RUN', ownerKey: 'wait-1' }]);
+ ).toEqual([{ ownerType: 'AGENT_RUN', conditionType: 'TIME' }]);
const run = await waitForRun(runId, 'COMPLETED');
@@ -1605,7 +1605,7 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
).toContain('The wait is over.');
expect(
await global.testDataSource.query(
- `SELECT id FROM core."agentRunSuspension" WHERE "threadId" = $1`,
+ `SELECT id FROM core."pendingWakeUp" WHERE "ownerId" = $1`,
[threadId],
),
).toEqual([]);
@@ -1617,12 +1617,9 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
// the shape a step paused by 2.45 left behind
await global.testDataSource.query(
- `DELETE FROM core."agentRunSuspension" WHERE "threadId" = $1`,
+ `DELETE FROM core."pendingWakeUp" WHERE "ownerId" = $1`,
[threadId],
);
- await global.testDataSource.query(
- `UPDATE "${schema}"."agentMessagePart" SET "toolOutput" = "toolOutput" - 'awaitedByCaller' WHERE "toolCallId" = 'ask-1'`,
- );
await global.testDataSource.query(
`UPDATE "${schema}"."workflowRun" SET state = jsonb_set(state, ARRAY['stepInfos', $2::text], (state->'stepInfos'->$2::text) - 'wait' || jsonb_build_object('threadId', $3::text)) WHERE id = $1`,
[runId, agent.id, threadId],
@@ -1777,8 +1774,9 @@ describe('core workflow execution and queue compatibility (e2e)', () => {
expect(response.body.errors).toBeUndefined();
expect(continueJobData).toMatchObject({
workspaceId,
- suspensionId: expect.any(String),
- resumeCount: 0,
+ threadId,
+ wakeUpId: expect.any(String),
+ outcome: { type: 'ANSWERED' },
});
// The decision a parallel branch finishing in that window takes.
diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts
index 681376d7f50..1398d613221 100644
--- a/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts
+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/workflow-send-chat-message.integration-spec.ts
@@ -122,7 +122,7 @@ describe('Send chat message workflow step', () => {
await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');
const [{ threadId }] = await global.testDataSource.query(
- `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,
+ `SELECT condition->>'threadId' AS "threadId" FROM core."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "ownerId" = $1 AND "ownerKey" = $2 AND condition->>'type' = 'ANSWER'`,
[workflowRunId, stepId],
);
@@ -250,7 +250,7 @@ describe('Send chat message workflow step', () => {
await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');
const [{ threadId }] = await global.testDataSource.query(
- `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,
+ `SELECT condition->>'threadId' AS "threadId" FROM core."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "ownerId" = $1 AND "ownerKey" = $2 AND condition->>'type' = 'ANSWER'`,
[workflowRunId, stepId],
);
@@ -291,6 +291,7 @@ describe('Send chat message workflow step', () => {
expect(await postAgain()).toEqual({
status: 'AWAITING',
threadId,
+ toolCallId: expect.any(String),
});
await answerPostedCall({
@@ -412,7 +413,7 @@ describe('Send chat message workflow step', () => {
);
const [{ threadId }] = await global.testDataSource.query(
- `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,
+ `SELECT condition->>'threadId' AS "threadId" FROM core."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "ownerId" = $1 AND "ownerKey" = $2 AND condition->>'type' = 'ANSWER'`,
[workflowRunId, stepId],
);
@@ -473,7 +474,7 @@ describe('Send chat message workflow step', () => {
await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');
const [{ threadId }] = await global.testDataSource.query(
- `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,
+ `SELECT condition->>'threadId' AS "threadId" FROM core."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "ownerId" = $1 AND "ownerKey" = $2 AND condition->>'type' = 'ANSWER'`,
[workflowRunId, stepId],
);
@@ -536,7 +537,7 @@ describe('Send chat message workflow step', () => {
await waitForWorkflowRunStepStatus(workflowRunId, stepId, 'PENDING');
const [{ threadId }] = await global.testDataSource.query(
- `SELECT "threadId" FROM core."agentRunSuspension" WHERE caller @> jsonb_build_object('ref', jsonb_build_object('workflowRunId', $1::text, 'stepId', $2::text))`,
+ `SELECT condition->>'threadId' AS "threadId" FROM core."pendingWakeUp" WHERE "ownerType" = 'WORKFLOW_STEP' AND "ownerId" = $1 AND "ownerKey" = $2 AND condition->>'type' = 'ANSWER'`,
[workflowRunId, stepId],
);
diff --git a/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.integration-spec.ts
new file mode 100644
index 00000000000..1ef71690fe8
--- /dev/null
+++ b/packages/twenty-server/test/integration/upgrade/suites/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups.integration-spec.ts
@@ -0,0 +1,258 @@
+import { type DataSource, type QueryRunner } from 'typeorm';
+import { v4 } from 'uuid';
+
+import { MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-instance-command-slow-1791438611737-move-agent-run-suspensions-to-pending-wake-ups';
+import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
+import { SEED_APPLE_WORKSPACE_ID } from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
+import { WORKSPACE_MEMBER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/data/constants/workspace-member-data-seeds.constant';
+
+jest.useRealTimers();
+
+const CALLER = {
+ type: 'AGENT_TRIGGER',
+ ref: { agentId: v4(), triggerId: v4() },
+};
+
+const SCHEMA = getWorkspaceSchemaName(SEED_APPLE_WORKSPACE_ID);
+
+const SUSPENSION = {
+ caller: CALLER,
+ runSpec: { agentId: null, title: 'Follow up' },
+ summary: null,
+ continuationCount: 3,
+};
+
+describe('2-46 slow instance command 1791438611737 - MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand (integration)', () => {
+ const command =
+ new MoveAgentRunSuspensionsToPendingWakeUpsSlowInstanceCommand();
+ let queryRunner: QueryRunner;
+
+ const insertWakeUp = ({
+ ownerType,
+ ownerId,
+ ownerKey,
+ condition,
+ payload = null,
+ }: {
+ ownerType: string;
+ ownerId: string;
+ ownerKey: string;
+ condition: object;
+ payload?: object | null;
+ }) =>
+ queryRunner.query(
+ `INSERT INTO "core"."pendingWakeUp" ("workspaceId", "ownerType", "ownerId", "ownerKey", condition, payload)
+ VALUES ($1, $2, $3, $4, $5::jsonb, $6::jsonb)`,
+ [
+ SEED_APPLE_WORKSPACE_ID,
+ ownerType,
+ ownerId,
+ ownerKey,
+ JSON.stringify(condition),
+ payload === null ? null : JSON.stringify(payload),
+ ],
+ );
+
+ const readWakeUps = (ownerIds: string[]) =>
+ queryRunner.query(
+ `SELECT "ownerType", "ownerId", "ownerKey", condition, payload FROM "core"."pendingWakeUp"
+ WHERE "ownerId" = ANY($1::uuid[]) ORDER BY "ownerType", condition ->> 'type'`,
+ [ownerIds],
+ );
+
+ beforeEach(async () => {
+ queryRunner = global.testDataSource.createQueryRunner();
+ await queryRunner.connect();
+ await queryRunner.startTransaction();
+ });
+
+ afterEach(async () => {
+ await queryRunner.rollbackTransaction();
+ await queryRunner.release();
+ });
+
+ it('brings suspended runs and posted call waits back to the previous shape and moves them again', async () => {
+ const waitingThreadId = v4();
+ const askingThreadId = v4();
+ const postedThreadId = v4();
+ const workflowRunId = v4();
+ const timeCondition = { type: 'TIME', resumeAt: '2030-01-01T00:00:00Z' };
+
+ await insertWakeUp({
+ ownerType: 'AGENT_RUN',
+ ownerId: waitingThreadId,
+ ownerKey: 'RUN',
+ condition: timeCondition,
+ payload: SUSPENSION,
+ });
+ await insertWakeUp({
+ ownerType: 'AGENT_RUN',
+ ownerId: askingThreadId,
+ ownerKey: 'RUN',
+ condition: { type: 'ANSWER', threadId: askingThreadId },
+ payload: SUSPENSION,
+ });
+ await insertWakeUp({
+ ownerType: 'WORKFLOW_STEP',
+ ownerId: workflowRunId,
+ ownerKey: 'step',
+ condition: { type: 'ANSWER', threadId: postedThreadId },
+ });
+
+ const wakeUpsBefore = await readWakeUps([
+ waitingThreadId,
+ askingThreadId,
+ workflowRunId,
+ ]);
+
+ await command.down(queryRunner);
+
+ expect(
+ await queryRunner.query(
+ `SELECT "threadId", caller, "runSpec", "resumeCount" FROM "core"."agentRunSuspension"
+ WHERE "threadId" = ANY($1::uuid[]) ORDER BY "resumeCount" DESC, "runSpec" IS NULL`,
+ [[waitingThreadId, askingThreadId, postedThreadId]],
+ ),
+ ).toEqual([
+ expect.objectContaining({
+ caller: CALLER,
+ runSpec: SUSPENSION.runSpec,
+ resumeCount: 3,
+ }),
+ expect.objectContaining({
+ caller: CALLER,
+ runSpec: SUSPENSION.runSpec,
+ resumeCount: 3,
+ }),
+ {
+ threadId: postedThreadId,
+ caller: {
+ type: 'WORKFLOW_STEP',
+ ref: { workflowRunId, stepId: 'step' },
+ },
+ runSpec: null,
+ resumeCount: 0,
+ },
+ ]);
+ expect(
+ await queryRunner.query(
+ `SELECT condition FROM "core"."pendingWakeUp" WHERE "ownerId" =
+ (SELECT id FROM "core"."agentRunSuspension" WHERE "threadId" = $1)`,
+ [waitingThreadId],
+ ),
+ ).toEqual([{ condition: timeCondition }]);
+
+ await command.runDataMigration(
+ queryRunner.manager as unknown as DataSource,
+ );
+ await command.up(queryRunner);
+
+ expect(
+ await readWakeUps([waitingThreadId, askingThreadId, workflowRunId]),
+ ).toEqual(wakeUpsBefore);
+ expect(
+ await queryRunner.query(
+ `SELECT to_regclass('"core"."agentRunSuspension"') AS "table"`,
+ ),
+ ).toEqual([{ table: null }]);
+ });
+
+ it('moves a run paused on a question and a wait at once to an answer wait', async () => {
+ await command.down(queryRunner);
+
+ const threadId = v4();
+ const turnId = v4();
+ const messageId = v4();
+ const suspensionId = v4();
+ const pendingPart = (toolName: string, toolCallId: string, index: number) =>
+ queryRunner.query(
+ `INSERT INTO ${SCHEMA}."agentMessagePart" (id, "messageId", "orderIndex", type, "toolName", "toolCallId", "toolInput", "toolOutput", state)
+ VALUES ($1, $2, $3, $4, $5, $6, '{}'::jsonb, '{"success": true, "result": {"status": "pending"}}'::jsonb, 'output-available')`,
+ [v4(), messageId, index, `tool-${toolName}`, toolName, toolCallId],
+ );
+
+ await queryRunner.query(
+ `INSERT INTO ${SCHEMA}."agentChatThread" (id, title, "workspaceMemberId", "pendingQuestionMessageId")
+ VALUES ($1, 'Paused run', $2, $3)`,
+ [threadId, WORKSPACE_MEMBER_DATA_SEED_IDS.JANE, messageId],
+ );
+ await queryRunner.query(
+ `INSERT INTO ${SCHEMA}."agentTurn" (id, "threadId", status) VALUES ($1, $2, 'waiting_for_input')`,
+ [turnId, threadId],
+ );
+ await queryRunner.query(
+ `INSERT INTO ${SCHEMA}."agentMessage" (id, "threadId", "turnId", role, status)
+ VALUES ($1, $2, $3, 'assistant', 'sent')`,
+ [messageId, threadId, turnId],
+ );
+ await pendingPart('ask_question', 'ask-1', 0);
+ await pendingPart('wait_for_duration', 'wait-1', 1);
+ await queryRunner.query(
+ `INSERT INTO "core"."agentRunSuspension" (id, "workspaceId", "threadId", caller, "runSpec", "resumeCount")
+ VALUES ($1, $2, $3, $4::jsonb, $5::jsonb, 1)`,
+ [
+ suspensionId,
+ SEED_APPLE_WORKSPACE_ID,
+ threadId,
+ JSON.stringify(CALLER),
+ JSON.stringify(SUSPENSION.runSpec),
+ ],
+ );
+
+ const [{ id: waitId }] = await queryRunner.query(
+ `INSERT INTO "core"."pendingWakeUp" ("workspaceId", "ownerType", "ownerId", "ownerKey", condition, "resumeAt")
+ VALUES ($1, 'AGENT_RUN', $2, 'wait-1', $3::jsonb, '2030-01-01T00:00:00Z') RETURNING id`,
+ [
+ SEED_APPLE_WORKSPACE_ID,
+ suspensionId,
+ JSON.stringify({ type: 'TIME', resumeAt: '2030-01-01T00:00:00Z' }),
+ ],
+ );
+
+ await command.runDataMigration(
+ queryRunner.manager as unknown as DataSource,
+ );
+ await command.up(queryRunner);
+
+ const wakeUps = await queryRunner.query(
+ `SELECT id, "ownerKey", condition, "resumeAt", "eventName", payload FROM "core"."pendingWakeUp"
+ WHERE "ownerType" = 'AGENT_RUN' AND "ownerId" = $1`,
+ [threadId],
+ );
+
+ expect(wakeUps).toEqual([
+ {
+ id: expect.any(String),
+ ownerKey: 'RUN',
+ condition: { type: 'ANSWER', threadId },
+ resumeAt: null,
+ eventName: null,
+ payload: { ...SUSPENSION, continuationCount: 1 },
+ },
+ ]);
+ // the wait's scheduled resolution finds nothing to claim
+ expect(wakeUps[0].id).not.toBe(waitId);
+ });
+
+ it('keeps one wake-up per conversation', async () => {
+ const threadId = v4();
+
+ await insertWakeUp({
+ ownerType: 'AGENT_RUN',
+ ownerId: threadId,
+ ownerKey: 'RUN',
+ condition: { type: 'ANSWER', threadId },
+ payload: SUSPENSION,
+ });
+
+ await expect(
+ insertWakeUp({
+ ownerType: 'AGENT_RUN',
+ ownerId: threadId,
+ ownerKey: 'RUN',
+ condition: { type: 'TIME', resumeAt: '2030-01-01T00:00:00Z' },
+ payload: SUSPENSION,
+ }),
+ ).rejects.toThrow(/UQ_PENDING_WAKE_UP_OWNER/);
+ });
+});
diff --git a/packages/twenty-server/test/integration/upgrade/suites/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.integration-spec.ts b/packages/twenty-server/test/integration/upgrade/suites/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.integration-spec.ts
index c04fdeba12c..cde2d5b9ea9 100644
--- a/packages/twenty-server/test/integration/upgrade/suites/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.integration-spec.ts
+++ b/packages/twenty-server/test/integration/upgrade/suites/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.integration-spec.ts
@@ -3,6 +3,7 @@ import { randomUUID } from 'node:crypto';
import { getAppProviderByClassName } from 'test/integration/utils/get-app-provider-by-class-name.util';
import { type ClosePendingAskQuestionsCallsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command';
+import { RESUME_PENDING_WAKE_UP_JOB_NAME } from 'src/engine/core-modules/pending-wake-up/constants/resume-pending-wake-up-job-name.constant';
import { buildSystemAuthContext } from 'src/engine/twenty-orm/utils/build-system-auth-context.util';
import { type WorkspaceOrmManager } from 'src/engine/twenty-orm/workspace-orm.manager';
import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
@@ -53,7 +54,7 @@ describe('2-46 workspace command - close pending ask_questions calls (integratio
turnId: randomUUID(),
messageId: randomUUID(),
};
- const suspensionId = randomUUID();
+ const wakeUpId = randomUUID();
const seededThreads = [chatThread, suspendedThread, stillWaitingThread];
const runCommand = () =>
@@ -136,15 +137,21 @@ describe('2-46 workspace command - close pending ask_questions calls (integratio
[randomUUID(), stillWaitingThread.messageId, randomUUID()],
);
await global.testDataSource.query(
- `INSERT INTO "core"."agentRunSuspension" (id, "workspaceId", "threadId", caller, "runSpec", "resumeCount")
- VALUES ($1, $2, $3, $4::jsonb, '{}'::jsonb, 2)`,
+ `INSERT INTO "core"."pendingWakeUp" (id, "workspaceId", "ownerType", "ownerId", "ownerKey", condition, payload)
+ VALUES ($1, $2, 'AGENT_RUN', $3, 'RUN', $4::jsonb, $5::jsonb)`,
[
- suspensionId,
+ wakeUpId,
SEED_APPLE_WORKSPACE_ID,
suspendedThread.threadId,
+ JSON.stringify({ type: 'ANSWER', threadId: suspendedThread.threadId }),
JSON.stringify({
- type: 'WORKFLOW_STEP',
- ref: { workflowRunId: randomUUID(), stepId: 'step' },
+ caller: {
+ type: 'WORKFLOW_STEP',
+ ref: { workflowRunId: randomUUID(), stepId: 'step' },
+ },
+ runSpec: {},
+ summary: null,
+ continuationCount: 2,
}),
],
);
@@ -154,8 +161,8 @@ describe('2-46 workspace command - close pending ask_questions calls (integratio
addJob.mockRestore();
await global.testDataSource.query(
- `DELETE FROM "core"."agentRunSuspension" WHERE id = $1`,
- [suspensionId],
+ `DELETE FROM "core"."pendingWakeUp" WHERE id = $1`,
+ [wakeUpId],
);
for (const { threadId } of seededThreads) {
@@ -202,25 +209,25 @@ describe('2-46 workspace command - close pending ask_questions calls (integratio
});
});
- it('continues the agent run suspended on the closed call', async () => {
+ it('resolves the wake-up of the agent run suspended on the closed call', async () => {
expect(addJob).toHaveBeenCalledTimes(1);
- expect(addJob).toHaveBeenCalledWith('ContinueAgentRunJob', {
+ expect(addJob).toHaveBeenCalledWith(RESUME_PENDING_WAKE_UP_JOB_NAME, {
workspaceId: SEED_APPLE_WORKSPACE_ID,
- suspensionId,
- resumeCount: 2,
+ wakeUpId,
+ answer: { result: {} },
});
});
- it('queues the continuation again on a rerun, as long as the run has not moved on', async () => {
+ it('resolves the wake-up again on a rerun, as long as it was not claimed', async () => {
addJob.mockClear();
await runCommand();
expect(addJob).toHaveBeenCalledTimes(1);
- expect(addJob).toHaveBeenCalledWith('ContinueAgentRunJob', {
+ expect(addJob).toHaveBeenCalledWith(RESUME_PENDING_WAKE_UP_JOB_NAME, {
workspaceId: SEED_APPLE_WORKSPACE_ID,
- suspensionId,
- resumeCount: 2,
+ wakeUpId,
+ answer: { result: {} },
});
expect(await readThread(chatThread)).toEqual({
pendingQuestionMessageId: null,
diff --git a/packages/twenty-shared/src/application/runAgentType.ts b/packages/twenty-shared/src/application/runAgentType.ts
index f57444b1558..6f0621e8f55 100644
--- a/packages/twenty-shared/src/application/runAgentType.ts
+++ b/packages/twenty-shared/src/application/runAgentType.ts
@@ -39,10 +39,10 @@ export type RunAgentInput = {
);
export type RunAgentResult = {
+ threadId: string;
+ // SUSPENDED: the agent paused on a wait and goes on by itself; its reply lands in the thread
+ status: 'COMPLETED' | 'SUSPENDED' | 'FAILED';
result: object | null;
error: string | null;
success: boolean;
- // the agent paused on a wait and goes on by itself; its reply lands in the thread
- isWaiting: boolean;
- threadId: string | null;
};
diff --git a/packages/twenty-shared/src/pending-wake-up/schemas/pending-wake-up-condition-schema.ts b/packages/twenty-shared/src/pending-wake-up/schemas/pending-wake-up-condition-schema.ts
index 39e605c4045..427a739c4a3 100644
--- a/packages/twenty-shared/src/pending-wake-up/schemas/pending-wake-up-condition-schema.ts
+++ b/packages/twenty-shared/src/pending-wake-up/schemas/pending-wake-up-condition-schema.ts
@@ -9,4 +9,10 @@ export const pendingWakeUpConditionSchema = z.discriminatedUnion('type', [
updatedFields: z.array(z.string()).optional(),
expiresAt: z.string().optional(),
}),
+ // resolved by a member's answer in a conversation: to the call posted there, or to the last pending one
+ z.object({
+ type: z.literal('ANSWER'),
+ threadId: z.string(),
+ toolCallId: z.string().optional(),
+ }),
]);
diff --git a/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts b/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts
index ff80a1d3f3a..1e7aa7a94c0 100644
--- a/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts
+++ b/packages/twenty-shared/src/workflow/schemas/workflow-step-wait-schema.ts
@@ -3,7 +3,7 @@ import { z } from 'zod';
import { pendingWakeUpConditionSchema } from '@/pending-wake-up/schemas/pending-wake-up-condition-schema';
export const workflowStepWaitSchema = z.discriminatedUnion('type', [
- // resolved when what the step handed its work to calls it back, such as an answer or an agent run
+ // resolved when the agent run the step handed its work to calls it back
z.object({ type: z.literal('CALLBACK') }),
...pendingWakeUpConditionSchema.options,
]);