The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(ai-chat): write each assistant checkpoint in one transaction (#27213)
Félix Malfaitb6e8b9a2799d
diff --git a/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.tsindex 66d05ca0a2c..a9ce5319c6b 100644--- a/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts+++ b/packages/twenty-server/src/database/commands/agent-history/__tests__/agent-history-migration.postgres.spec.ts@@ -768,23 +768,26 @@ const SCHEMA = getWorkspaceSchemaName(WORKSPACE_ID);expect(created.totalInputCredits).toBe('0');expect(created).not.toHaveProperty('workspaceId');const checkpointId = '20202020-8888-4888-8888-888888888888';+ const checkpointChatService = createChatService(messages);await Promise.all(- [1, 2].map(() =>- messages.upsert(- WORKSPACE_ID,- {- id: checkpointId,- threadId: THREAD_ID,- turnId: TURN_ID,- role: 'assistant' as AgentMessageWorkspaceEntity['role'],- },- ['id'],- ),+ ['First checkpoint', 'Second checkpoint'].map((text) =>+ checkpointChatService.upsertAssistantMessage({+ id: checkpointId,+ threadId: THREAD_ID,+ turnId: TURN_ID,+ parts: [{ type: 'text', text }],+ workspaceId: WORKSPACE_ID,+ }),),);expect(await messages.count(WORKSPACE_ID, { where: { id: checkpointId } }),).toBe(1);+ expect(+ await messageParts.count(WORKSPACE_ID, {+ where: { messageId: checkpointId },+ }),+ ).toBe(1);expect(emitDatabaseBatchEvent).not.toHaveBeenCalled();});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.tsindex a852899a893..e5d0e74c97a 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@@ -8,7 +8,6 @@ import { Injectable } from '@nestjs/common';import { ExtendedUIMessage } from 'twenty-shared/ai';import { isDefined, isNonEmptyArray } from 'twenty-shared/utils';import { type FindOptionsWhere, In } from 'typeorm';-import type { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';import { FileEntity } from 'src/engine/core-modules/file/entities/file.entity';import { AgentMessagePartWorkspaceEntity } from 'src/engine/metadata-modules/ai/ai-history/standard-objects/agent-message-part.workspace-entity';@@ -140,28 +139,30 @@ export class AgentChatService {parts: ExtendedUIMessage['parts'];workspaceId: string;}): Promise<void> {- await this.messageRepository.upsert(+ const dbParts = mapUIMessagePartsToDBParts(parts, id);++ // the message is replaced whole, so no reader or crash ever finds it without its parts+ await this.conversationWriterService.runInTransaction(workspaceId,- {- id,- threadId,- turnId,- role: AgentMessageRole.ASSISTANT,- processedAt: new Date().toISOString(),+ async (scope) => {+ await scope.upsert(+ 'agentMessage',+ {+ id,+ threadId,+ turnId,+ role: AgentMessageRole.ASSISTANT,+ processedAt: new Date().toISOString(),+ },+ ['id'],+ );+ await scope.delete('agentMessagePart', { messageId: id });++ if (dbParts.length > 0) {+ await scope.insert('agentMessagePart', dbParts);+ }},- ['id'],);-- await this.messagePartRepository.delete(workspaceId, { messageId: id });-- const dbParts = mapUIMessagePartsToDBParts(parts, id);-- if (dbParts.length > 0) {- await this.messagePartRepository.insert(- workspaceId,- dbParts as QueryDeepPartialEntity<AgentMessagePartWorkspaceEntity>[],- );- }}async findLatestSentUserMessage({diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository.tsindex 6bd14d300be..b3e824934f5 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/repositories/agent-history-repository.ts@@ -7,7 +7,7 @@ import {} from 'src/engine/metadata-modules/ai/ai.exception';import { AgentHistoryStorageException } from 'src/engine/metadata-modules/ai/ai-history/exceptions/agent-history-storage.exception';import { type AgentHistoryObjectName } from 'src/engine/metadata-modules/ai/ai-history/types/agent-history-object-name.type';-import { type FindOptionsWhere, type ObjectLiteral } from 'typeorm';+import { type FindOptionsWhere } from 'typeorm';import { type QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';import { isDefined } from 'twenty-shared/utils';@@ -198,35 +198,6 @@ export class AgentHistoryRepository<TRecord extends { id: string }> {});}- upsert(- workspaceId: string,- values: QueryDeepPartialEntity<TRecord>,- conflictPaths: string[],- ) {- return this.run(workspaceId, async (repository, context) => {- // workspace upsert selects before inserting, so serialize concurrent checkpoints per identity- const valuesByField: ObjectLiteral = values;- const identity = [...conflictPaths]- .sort()- .map((field) => [field, valuesByField[field]]);- await context.manager.query(- 'SELECT pg_advisory_xact_lock(hashtextextended($1, 0))',- [- `agent-history-upsert:${workspaceId}:${this.name}:${JSON.stringify(identity)}`,- ],- );- return repository.upsert(- (await addAgentMessageSenderWorkspaceMember({- name: this.name,- values,- workspaceId,- context,- })) as QueryDeepPartialEntity<TRecord>,- conflictPaths,- );- });- }-query<TResult>(workspaceId: string,work: (context: AgentHistoryStorageContext) => Promise<TResult>,diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-history-transaction.service.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-history-transaction.service.tsindex 81e99207edd..57010558b51 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-history-transaction.service.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/services/agent-history-transaction.service.ts@@ -49,6 +49,31 @@ export class AgentHistoryTransactionService {return result.generatedMaps.length;},+ upsert: async (name, values, conflictPaths) => {+ // workspace upsert selects before inserting, so concurrent writers of one identity take turns+ const identity = [...conflictPaths]+ .sort()+ .map((field) => [field, values[field]]);++ await transactionScope.executeRawQuery(+ 'SELECT pg_advisory_xact_lock(hashtextextended($1, 0))',+ [+ `agent-history-upsert:${workspaceId}:${name}:${JSON.stringify(identity)}`,+ ],+ );+ await getRepository(name).upsert(+ await addAgentMessageSenderWorkspaceMember({+ name,+ values,+ workspaceId,+ context,+ }),+ conflictPaths,+ );+ },+ delete: async (name, where) => {+ await getRepository(name).delete(where);+ },});},),diff --git a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/agent-history-transaction-scope.type.ts b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/agent-history-transaction-scope.type.tsindex 2115c5aee7a..3f3e097f6e4 100644--- a/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/agent-history-transaction-scope.type.ts+++ b/packages/twenty-server/src/engine/metadata-modules/ai/ai-history/types/agent-history-transaction-scope.type.ts@@ -12,4 +12,10 @@ export type AgentHistoryTransactionScope = {where: ObjectLiteral,values: ObjectLiteral,) => Promise<number>;+ upsert: (+ name: AgentHistoryObjectName,+ values: ObjectLiteral,+ conflictPaths: string[],+ ) => Promise<void>;+ delete: (name: AgentHistoryObjectName, where: ObjectLiteral) => Promise<void>;};