Skip to content

The open alternative to Salesforce, designed for AI.

Find a waiting tool call by its id instead of scanning recent messages (#27527)

Félix Malfait4125324bb29e
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 c426682efe6..755d929bd22 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
@@ -14,16 +14,14 @@ import { AgentRunCallerHandlerRegistryService } from 'src/engine/metadata-module
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 { type AgentMessageWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message.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';
-const RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL = 50;
-
// An AGENT_RUN wake-up is owned by a suspended run and keyed by the wait call it paused on
@Injectable()
export class AgentRunPendingWakeUpHandlerService
@@ -33,8 +31,7 @@ export class AgentRunPendingWakeUpHandlerService
private readonly pendingWakeUpOwnerHandlerRegistryService: PendingWakeUpOwnerHandlerRegistryService,
private readonly agentRunSuspensionService: AgentRunSuspensionService,
private readonly callerHandlerRegistry: AgentRunCallerHandlerRegistryService,
- @InjectAgentHistoryRepository('agentMessage')
- private readonly messageRepository: AgentHistoryRepository<AgentMessageWorkspaceEntity>,
+ private readonly conversationReaderService: AgentConversationReaderService,
@InjectAgentHistoryRepository('agentMessagePart')
private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,
) {}
@@ -142,23 +139,16 @@ export class AgentRunPendingWakeUpHandlerService
toolCallId: string;
outcome: PendingWakeUpOutcome;
}): Promise<void> {
- // messages can follow the call while it waits, so the latest one may not carry it
- const recentMessages = await this.messageRepository.find(workspaceId, {
- where: { threadId },
- order: { createdAt: 'DESC' },
- take: RECENT_MESSAGES_TO_SEARCH_FOR_PENDING_CALL,
- relations: ['parts'],
+ const pendingPart = await this.conversationReaderService.findToolPart({
+ workspaceId,
+ threadId,
+ toolCallId,
});
- const pendingPart = recentMessages
- .flatMap((message) => message.parts ?? [])
- .find(
- (part) =>
- part.toolCallId === toolCallId &&
- isAwaitingPausingToolOutput(part.toolOutput),
- );
-
- if (!isDefined(pendingPart)) {
+ if (
+ !isDefined(pendingPart) ||
+ !isAwaitingPausingToolOutput(pendingPart.toolOutput)
+ ) {
throw new AiException(
'The waiting call could not be found in the conversation',
AiExceptionCode.TOOL_CALL_NOT_FOUND,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service.ts
index ea51ce6da7c..0a3f0dcc723 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service.ts
@@ -503,41 +503,6 @@ export class AgentChatService {
return savedTurnId;
}
- // tool call ids are only unique within a conversation
- async findToolPart({
- threadId,
- toolCallId,
- workspaceId,
- }: {
- threadId: string;
- toolCallId: string;
- workspaceId: string;
- }): Promise<Pick<
- AgentMessagePartWorkspaceEntity,
- 'id' | 'messageId' | 'toolName' | 'toolInput' | 'toolOutput'
- > | null> {
- const parts = await this.messagePartRepository.find(workspaceId, {
- where: { toolCallId },
- select: ['id', 'messageId', 'toolName', 'toolInput', 'toolOutput'],
- });
-
- if (!isNonEmptyArray(parts)) {
- return null;
- }
-
- const message = await this.messageRepository.findOne(workspaceId, {
- where: {
- id: In(parts.map((part) => part.messageId)),
- threadId,
- },
- select: ['id'],
- });
-
- return (
- parts.find((candidate) => candidate.messageId === message?.id) ?? null
- );
- }
-
async findAwaitingToolParts({
messageId,
workspaceId,
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/__tests__/agent-conversation-reader.service.spec.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/__tests__/agent-conversation-reader.service.spec.ts
index 071392f839e..adc70d8296c 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/__tests__/agent-conversation-reader.service.spec.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/__tests__/agent-conversation-reader.service.spec.ts
@@ -63,6 +63,7 @@ const buildService = (messages: unknown[], failedTurnIds: string[] = []) => {
messageRepository as never,
turnRepository as never,
fileUrlService as never,
+ {} as never,
);
return { service, messageRepository, fileUrlService };
@@ -249,3 +250,74 @@ describe('AgentConversationReaderService', () => {
expect(fileUrlService.signFileByIdUrl).not.toHaveBeenCalled();
});
});
+
+describe('AgentConversationReaderService.findToolPart', () => {
+ const buildToolPartService = ({
+ parts,
+ threadMessageId,
+ }: {
+ parts: { id: string; messageId: string }[];
+ threadMessageId: string | null;
+ }) => {
+ const messagePartRepository = { find: jest.fn().mockResolvedValue(parts) };
+ const messageRepository = {
+ findOne: jest
+ .fn()
+ .mockResolvedValue(
+ threadMessageId === null ? null : { id: threadMessageId },
+ ),
+ };
+
+ return new AgentConversationReaderService(
+ messageRepository as never,
+ {} as never,
+ {} as never,
+ messagePartRepository as never,
+ );
+ };
+
+ it('returns the part of the call made in the given thread', async () => {
+ const service = buildToolPartService({
+ parts: [
+ { id: 'other-thread-part', messageId: 'other-thread-message' },
+ { id: 'thread-part', messageId: 'thread-message' },
+ ],
+ threadMessageId: 'thread-message',
+ });
+
+ const part = await service.findToolPart({
+ workspaceId: WORKSPACE_ID,
+ threadId: THREAD_ID,
+ toolCallId: 'call-id',
+ });
+
+ expect(part?.id).toBe('thread-part');
+ });
+
+ it('returns null when the call was made in another thread only', async () => {
+ const service = buildToolPartService({
+ parts: [{ id: 'other-thread-part', messageId: 'other-thread-message' }],
+ threadMessageId: null,
+ });
+
+ const part = await service.findToolPart({
+ workspaceId: WORKSPACE_ID,
+ threadId: THREAD_ID,
+ toolCallId: 'call-id',
+ });
+
+ expect(part).toBeNull();
+ });
+
+ it('returns null when no part carries the call', async () => {
+ const service = buildToolPartService({ parts: [], threadMessageId: null });
+
+ const part = await service.findToolPart({
+ workspaceId: WORKSPACE_ID,
+ threadId: THREAD_ID,
+ toolCallId: 'call-id',
+ });
+
+ expect(part).toBeNull();
+ });
+});
diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service.ts
index d617550fd8e..7be383be346 100644
--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service.ts
+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service.ts
@@ -6,7 +6,8 @@ import {
isExtendedFileUIPart,
} from 'twenty-shared/ai';
import { FileFolder } from 'twenty-shared/types';
-import { isDefined } from 'twenty-shared/utils';
+import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';
+import { In } from 'typeorm';
import { FileUrlService } from 'src/engine/core-modules/file/file-url/file-url.service';
import { finalizeDanglingToolParts } from 'src/engine/metadata-modules/ai/ai-history/utils/finalize-dangling-tool-parts.util';
@@ -14,6 +15,7 @@ import { mapDBPartsToUIMessageParts } from 'src/engine/metadata-modules/ai/ai-hi
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 { AgentTurnStatus } from 'src/engine/metadata-modules/ai/ai-history/enums/agent-turn-status.enum';
+import { type AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';
import { type AgentMessageWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message.workspace-entity';
import { type AgentTurnWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-turn.workspace-entity';
import { type AgentConversationActor } from 'src/engine/metadata-modules/ai/ai-history/types/agent-conversation-actor.type';
@@ -27,8 +29,45 @@ export class AgentConversationReaderService {
@InjectAgentHistoryRepository('agentTurn')
private readonly turnRepository: AgentHistoryRepository<AgentTurnWorkspaceEntity>,
private readonly fileUrlService: FileUrlService,
+ @InjectAgentHistoryRepository('agentMessagePart')
+ private readonly messagePartRepository: AgentHistoryRepository<AgentMessagePartWorkspaceEntity>,
) {}
+ // tool call ids are only unique within a conversation
+ async findToolPart({
+ threadId,
+ toolCallId,
+ workspaceId,
+ }: {
+ threadId: string;
+ toolCallId: string;
+ workspaceId: string;
+ }): Promise<Pick<
+ AgentMessagePartWorkspaceEntity,
+ 'id' | 'messageId' | 'toolName' | 'toolInput' | 'toolOutput'
+ > | null> {
+ const parts = await this.messagePartRepository.find(workspaceId, {
+ where: { toolCallId },
+ select: ['id', 'messageId', 'toolName', 'toolInput', 'toolOutput'],
+ });
+
+ if (!isNonEmptyArray(parts)) {
+ return null;
+ }
+
+ const message = await this.messageRepository.findOne(workspaceId, {
+ where: {
+ id: In(parts.map((part) => part.messageId)),
+ threadId,
+ },
+ select: ['id'],
+ });
+
+ return (
+ parts.find((candidate) => candidate.messageId === message?.id) ?? null
+ );
+ }
+
async loadMessages({
workspaceId,
threadId,
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 8598811a7a5..4dcf3439f99 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
@@ -24,6 +24,7 @@ import { AgentChatTurnPreflightService } from 'src/engine/metadata-modules/ai/ai
import { AgentChatService } from 'src/engine/metadata-modules/ai/ai-chat/services/agent-chat.service';
import { formatErrorWithCause } from 'src/engine/metadata-modules/ai/ai-chat/utils/format-error-with-cause.util';
import { AgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository';
+import { AgentConversationReaderService } from 'src/engine/metadata-modules/ai/ai-history/services/agent-conversation-reader.service';
import { InjectAgentHistoryRepository } from 'src/engine/metadata-modules/ai/ai-history/repositories/inject-agent-history-repository.decorator';
import { type AgentChatThreadWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-chat-thread.workspace-entity';
import {
@@ -70,6 +71,7 @@ export class ToolCallAnswerService {
private readonly turnPreflightService: AgentChatTurnPreflightService,
private readonly agentActorContextService: AgentActorContextService,
private readonly toolRegistryService: ToolRegistryService,
+ private readonly conversationReaderService: AgentConversationReaderService,
) {}
async answer(args: AnswerToolCallArgs): Promise<AnswerToolCallOutcome> {
@@ -81,7 +83,11 @@ export class ToolCallAnswerService {
where: { id: threadId },
select: ['id', 'activeStreamId', 'pendingQuestionMessageId'],
}),
- this.agentChatService.findToolPart({ threadId, toolCallId, workspaceId }),
+ this.conversationReaderService.findToolPart({
+ threadId,
+ toolCallId,
+ workspaceId,
+ }),
]);
const pausingToolCall = parsePausingToolCall(toolPart);