The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Close the ask_questions calls still waiting on an answer (#27532)
Félix Malfaitf1c77a12cda5
diff --git a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.tsindex 191498e7c8b..a3d3f53c5f7 100644--- a/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-upgrade-version-command.module.ts@@ -15,6 +15,7 @@ import { SuspendPausedAgentStepsCommand } from 'src/database/commands/upgrade-veimport { AddAgentChatThreadSubscriptionsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791322315043-add-agent-chat-thread-subscriptions.command';import { DeleteOrphanedWorkflowRunsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791369613345-delete-orphaned-workflow-runs.command';import { AddAgentChatThreadAssigneeCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791385745099-add-agent-chat-thread-assignee.command';+import { ClosePendingAskQuestionsCallsCommand } from 'src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command';import { ApplicationModule } from 'src/engine/core-modules/application/application.module';import { WorkspaceCacheModule } from 'src/engine/workspace-cache/workspace-cache.module';import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspace-migration/workspace-migration.module';@@ -41,6 +42,7 @@ import { WorkspaceMigrationModule } from 'src/engine/workspace-manager/workspaceAddAgentChatThreadSubscriptionsCommand,DeleteOrphanedWorkflowRunsCommand,AddAgentChatThreadAssigneeCommand,+ ClosePendingAskQuestionsCallsCommand,],})export class V2_46_UpgradeVersionCommandModule {}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.tsnew file mode 100644index 00000000000..d6c8a4769a5--- /dev/null+++ b/packages/twenty-server/src/database/commands/upgrade-version-command/2-46/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.command.ts@@ -0,0 +1,192 @@+import { Command } from 'nest-commander';+import { STANDARD_OBJECTS } from 'twenty-shared/metadata';+import { isDefined } from 'twenty-shared/utils';++import { AgentHistoryUpgradeStorageService } from 'src/database/commands/agent-history/agent-history-upgrade-storage.service';+import { ProvisionedWorkspaceCommandRunner } from 'src/database/commands/command-runners/provisioned-workspace.command-runner';+import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';+import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';+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 { 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';++const SKIPPED_MESSAGE = 'The questions were closed without an answer.';++// ask_questions has not been offered since ask_question replaced it, and its remaining pending+// calls are closed before its code goes. A conversation left with nothing pending stops waiting,+// and an agent run suspended on it goes on, as when a member moves on without answering+@RegisteredWorkspaceCommand('2.46.0', 1791398841984)+@Command({+ name: 'upgrade:2-46:close-pending-ask-questions-calls',+ description: 'Close the ask_questions calls still waiting on an answer',+})+export class ClosePendingAskQuestionsCallsCommand extends ProvisionedWorkspaceCommandRunner {+ constructor(+ protected readonly workspaceIteratorService: WorkspaceIteratorService,+ private readonly workspaceCacheService: WorkspaceCacheService,+ private readonly storage: AgentHistoryUpgradeStorageService,+ @InjectMessageQueue(MessageQueue.aiQueue)+ private readonly messageQueueService: MessageQueueService,+ ) {+ super(workspaceIteratorService);+ }++ override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {+ await this.up(args);+ }++ async up({ workspaceId, options }: RunOnWorkspaceArgs): Promise<void> {+ const { flatFieldMetadataMaps } =+ await this.workspaceCacheService.getOrRecompute(workspaceId, [+ 'flatFieldMetadataMaps',+ ]);++ const hasRequiredFields = [+ STANDARD_OBJECTS.agentMessagePart.fields.toolOutput.universalIdentifier,+ STANDARD_OBJECTS.agentChatThread.fields.pendingQuestionMessageId+ .universalIdentifier,+ STANDARD_OBJECTS.agentTurn.fields.status.universalIdentifier,+ ].every((universalIdentifier) =>+ isDefined(+ findFlatEntityByUniversalIdentifier<FlatFieldMetadata>({+ flatEntityMaps: flatFieldMetadataMaps,+ universalIdentifier,+ }),+ ),+ );++ if (!hasRequiredFields) {+ this.logger.log(+ `Chat history fields not found for workspace ${workspaceId}, skipping`,+ );++ return;+ }++ const isDryRun = options.dryRun ?? false;++ const { closedThreadCount, suspensionsToContinue } = await this.storage.run(+ workspaceId,+ async ({ manager, table }) => {+ const pendingCalls: { threadId: string }[] = await manager.query(+ `SELECT DISTINCT message."threadId"+ FROM ${table('agentMessagePart')} part+ JOIN ${table('agentMessage')} message ON message.id = part."messageId"+ WHERE part."toolName" = 'ask_questions'+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'`,+ );++ if (isDryRun) {+ return {+ closedThreadCount: pendingCalls.length,+ suspensionsToContinue: [],+ };+ }++ const threadIds = pendingCalls.map(({ threadId }) => threadId);++ await manager.query(+ `UPDATE ${table('agentMessagePart')} part+ SET "toolOutput" = jsonb_set(part."toolOutput", '{result,status}', '"skipped"')+ || jsonb_build_object('success', true, 'message', $2::text),+ "updatedAt" = now()+ FROM ${table('agentMessage')} message+ WHERE message.id = part."messageId"+ AND message."threadId" = ANY($1::uuid[])+ AND part."toolName" = 'ask_questions'+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'`,+ [threadIds, SKIPPED_MESSAGE],+ );++ const settledThreads: { id: string }[] = await manager.query(+ `SELECT thread.id+ FROM ${table('agentChatThread')} thread+ WHERE thread.id = ANY($1::uuid[])+ AND NOT EXISTS (+ SELECT 1+ FROM ${table('agentMessagePart')} part+ JOIN ${table('agentMessage')} message ON message.id = part."messageId"+ WHERE message."threadId" = thread.id+ AND part."toolOutput" -> 'result' ->> 'status' = 'pending'+ )`,+ [threadIds],+ );++ await manager.query(+ `WITH cleared AS (+ SELECT id, "pendingQuestionMessageId" AS "messageId"+ FROM ${table('agentChatThread')}+ WHERE id = ANY($1::uuid[]) AND "pendingQuestionMessageId" IS NOT NULL+ ), thread AS (+ UPDATE ${table('agentChatThread')} thread+ SET "pendingQuestionMessageId" = NULL, "updatedAt" = now()+ FROM cleared WHERE thread.id = cleared.id+ )+ UPDATE ${table('agentTurn')} turn+ SET "status" = 'completed', "endedAt" = now(), "updatedAt" = now()+ FROM cleared+ JOIN ${table('agentMessage')} message ON message.id = cleared."messageId"+ WHERE turn.id = message."turnId" AND turn."status" = 'waiting_for_input'`,+ [settledThreads.map(({ id }) => id)],+ );++ // 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],+ );++ return {+ closedThreadCount: pendingCalls.length,+ suspensionsToContinue: suspensions,+ };+ },+ );++ // 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,+ },+ );+ }++ 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)`,+ );+ }++ async down(_args: RunOnWorkspaceArgs): Promise<void> {+ // A closed call has no answer to bring back+ }+}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.tsnew file mode 100644index 00000000000..c04fdeba12c--- /dev/null+++ b/packages/twenty-server/test/integration/upgrade/suites/2-46-workspace-command-1791398841984-close-pending-ask-questions-calls.integration-spec.ts@@ -0,0 +1,231 @@+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 { 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';+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';++const RUN_ON_WORKSPACE_ARGS = {+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ options: {},+ index: 0,+ total: 1,+};++const SCHEMA = getWorkspaceSchemaName(SEED_APPLE_WORKSPACE_ID);++const PENDING_QUESTIONS_OUTPUT = {+ success: true,+ message: 'Waiting for the user to answer.',+ result: {+ questions: [{ header: 'Tone', question: 'Which tone?', options: [] }],+ status: 'pending',+ },+};++type SeededThread = {+ threadId: string;+ turnId: string;+ messageId: string;+};++describe('2-46 workspace command - close pending ask_questions calls (integration)', () => {+ let command: ClosePendingAskQuestionsCallsCommand;+ let workspaceOrmManager: WorkspaceOrmManager;+ let addJob: jest.SpyInstance;++ const chatThread: SeededThread = {+ threadId: randomUUID(),+ turnId: randomUUID(),+ messageId: randomUUID(),+ };+ const suspendedThread: SeededThread = {+ threadId: randomUUID(),+ turnId: randomUUID(),+ messageId: randomUUID(),+ };+ const stillWaitingThread: SeededThread = {+ threadId: randomUUID(),+ turnId: randomUUID(),+ messageId: randomUUID(),+ };+ const suspensionId = randomUUID();+ const seededThreads = [chatThread, suspendedThread, stillWaitingThread];++ const runCommand = () =>+ workspaceOrmManager.executeInWorkspaceContext(+ () => command.up(RUN_ON_WORKSPACE_ARGS),+ buildSystemAuthContext(SEED_APPLE_WORKSPACE_ID),+ );++ const seedThread = async ({ threadId, turnId, messageId }: SeededThread) => {+ await global.testDataSource.query(+ `INSERT INTO ${SCHEMA}."agentChatThread" (id, title, "workspaceMemberId", "pendingQuestionMessageId")+ VALUES ($1, 'Legacy questions', $2, $3)`,+ [threadId, WORKSPACE_MEMBER_DATA_SEED_IDS.JANE, messageId],+ );+ await global.testDataSource.query(+ `INSERT INTO ${SCHEMA}."agentTurn" (id, "threadId", status) VALUES ($1, $2, 'waiting_for_input')`,+ [turnId, threadId],+ );+ await global.testDataSource.query(+ `INSERT INTO ${SCHEMA}."agentMessage" (id, "threadId", "turnId", role, status)+ VALUES ($1, $2, $3, 'assistant', 'sent')`,+ [messageId, threadId, turnId],+ );+ await global.testDataSource.query(+ `INSERT INTO ${SCHEMA}."agentMessagePart" (id, "messageId", "orderIndex", type, "toolName", "toolCallId", "toolInput", "toolOutput", state)+ VALUES ($1, $2, 0, 'tool-ask_questions', 'ask_questions', $3, '{}'::jsonb, $4::jsonb, 'output-available')`,+ [+ randomUUID(),+ messageId,+ randomUUID(),+ JSON.stringify(PENDING_QUESTIONS_OUTPUT),+ ],+ );+ };++ const readThread = async ({ threadId, turnId }: SeededThread) => {+ const [thread] = await global.testDataSource.query(+ `SELECT "pendingQuestionMessageId" FROM ${SCHEMA}."agentChatThread" WHERE id = $1`,+ [threadId],+ );+ const [turn] = await global.testDataSource.query(+ `SELECT status FROM ${SCHEMA}."agentTurn" WHERE id = $1`,+ [turnId],+ );+ const parts = await global.testDataSource.query(+ `SELECT part."toolName", part."toolOutput" -> 'result' ->> 'status' AS status+ FROM ${SCHEMA}."agentMessagePart" part+ JOIN ${SCHEMA}."agentMessage" message ON message.id = part."messageId"+ WHERE message."threadId" = $1+ ORDER BY part."orderIndex"`,+ [threadId],+ );++ return {+ pendingQuestionMessageId: thread.pendingQuestionMessageId,+ turnStatus: turn.status,+ parts,+ };+ };++ beforeAll(async () => {+ command = getAppProviderByClassName<ClosePendingAskQuestionsCallsCommand>(+ 'ClosePendingAskQuestionsCallsCommand',+ );+ workspaceOrmManager = getAppProviderByClassName<WorkspaceOrmManager>(+ 'WorkspaceOrmManager',+ );+ addJob = jest+ .spyOn(command['messageQueueService'], 'add')+ .mockResolvedValue(undefined);++ for (const seededThread of seededThreads) {+ await seedThread(seededThread);+ }++ await global.testDataSource.query(+ `INSERT INTO ${SCHEMA}."agentMessagePart" (id, "messageId", "orderIndex", type, "toolName", "toolCallId", "toolInput", "toolOutput", state)+ VALUES ($1, $2, 1, 'tool-ask_question', 'ask_question', $3, '{}'::jsonb,+ '{"success": true, "result": {"status": "pending"}}'::jsonb, 'output-available')`,+ [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)`,+ [+ suspensionId,+ SEED_APPLE_WORKSPACE_ID,+ suspendedThread.threadId,+ JSON.stringify({+ type: 'WORKFLOW_STEP',+ ref: { workflowRunId: randomUUID(), stepId: 'step' },+ }),+ ],+ );+ });++ afterAll(async () => {+ addJob.mockRestore();++ await global.testDataSource.query(+ `DELETE FROM "core"."agentRunSuspension" WHERE id = $1`,+ [suspensionId],+ );++ for (const { threadId } of seededThreads) {+ await global.testDataSource.query(+ `DELETE FROM ${SCHEMA}."agentMessagePart" WHERE "messageId" IN+ (SELECT id FROM ${SCHEMA}."agentMessage" WHERE "threadId" = $1)`,+ [threadId],+ );++ for (const table of ['agentMessage', 'agentTurn']) {+ await global.testDataSource.query(+ `DELETE FROM ${SCHEMA}."${table}" WHERE "threadId" = $1`,+ [threadId],+ );+ }++ await global.testDataSource.query(+ `DELETE FROM ${SCHEMA}."agentChatThread" WHERE id = $1`,+ [threadId],+ );+ }+ });++ it('closes the calls and stops the conversations waiting when nothing else is pending', async () => {+ await runCommand();++ for (const seededThread of [chatThread, suspendedThread]) {+ expect(await readThread(seededThread)).toEqual({+ pendingQuestionMessageId: null,+ turnStatus: 'completed',+ parts: [{ toolName: 'ask_questions', status: 'skipped' }],+ });+ }+ });++ it('keeps a conversation waiting on another pending call', async () => {+ expect(await readThread(stillWaitingThread)).toEqual({+ pendingQuestionMessageId: stillWaitingThread.messageId,+ turnStatus: 'waiting_for_input',+ parts: [+ { toolName: 'ask_questions', status: 'skipped' },+ { toolName: 'ask_question', status: 'pending' },+ ],+ });+ });++ it('continues the agent run suspended on the closed call', async () => {+ expect(addJob).toHaveBeenCalledTimes(1);+ expect(addJob).toHaveBeenCalledWith('ContinueAgentRunJob', {+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ suspensionId,+ resumeCount: 2,+ });+ });++ it('queues the continuation again on a rerun, as long as the run has not moved on', async () => {+ addJob.mockClear();++ await runCommand();++ expect(addJob).toHaveBeenCalledTimes(1);+ expect(addJob).toHaveBeenCalledWith('ContinueAgentRunJob', {+ workspaceId: SEED_APPLE_WORKSPACE_ID,+ suspensionId,+ resumeCount: 2,+ });+ expect(await readThread(chatThread)).toEqual({+ pendingQuestionMessageId: null,+ turnStatus: 'completed',+ parts: [{ toolName: 'ask_questions', status: 'skipped' }],+ });+ });+});