Migrate agent v2 (#16214)

# Introduction
Closes https://github.com/twentyhq/core-team-issues/issues/1980

In this PR we migrate the agent from v1 to v2.

## New FlatRoleTargetByAgentIdMaps
Derivated the `flatRoleTargetMaps` to be building a
`flatRoleTargetByAgentIdMaps` to ease retrieving a roleId to associate
to an agent

## Coverage
Added strong coverage on both failing and successful CRU agents
operations

---------

Co-authored-by: Weiko <corentin@twenty.com>
This commit is contained in:
Paul Rastoin
2025-12-03 11:51:19 +01:00
committed by GitHub
parent a7c5969ede
commit 59672e3e34
110 changed files with 4864 additions and 849 deletions
@@ -0,0 +1,40 @@
import { type AgentDTO } from 'src/engine/metadata-modules/ai/ai-agent/dtos/agent.dto';
import { type FlatAgentWithRoleId } from 'src/engine/metadata-modules/flat-agent/types/flat-agent.type';
export const fromFlatAgentWithRoleIdToAgentDto = ({
applicationId,
createdAt,
description,
evaluationInputs,
icon,
id,
isCustom,
label,
modelConfiguration,
modelId,
name,
prompt,
responseFormat,
standardId,
updatedAt,
workspaceId,
roleId,
}: FlatAgentWithRoleId): AgentDTO => ({
createdAt,
description: description ?? undefined,
evaluationInputs,
id,
isCustom,
label,
modelConfiguration: modelConfiguration ?? undefined,
modelId,
name,
prompt,
responseFormat,
standardId,
updatedAt,
workspaceId,
applicationId: applicationId ?? undefined,
icon: icon ?? undefined,
roleId: roleId ?? undefined,
});
@@ -5,6 +5,9 @@ export const transformAgentEntityToFlatAgent = (
agentEntity: AgentEntity,
): FlatAgent => {
return {
createdAt: agentEntity.createdAt,
deletedAt: agentEntity.deletedAt,
updatedAt: agentEntity.updatedAt,
id: agentEntity.id,
standardId: agentEntity.standardId,
name: agentEntity.name,
@@ -1,22 +1,35 @@
import { v4 } from 'uuid';
import { type AgentEntity } from 'src/engine/metadata-modules/ai/ai-agent/entities/agent.entity';
import { type FlatAgent } from 'src/engine/metadata-modules/flat-agent/types/flat-agent.type';
import { type StandardAgentDefinition } from 'src/engine/workspace-manager/workspace-sync-metadata/standard-agents/types/standard-agent-definition.interface';
export const transformStandardAgentDefinitionToFlatAgent = (
standardAgentDefinition: StandardAgentDefinition,
workspaceId: string,
): FlatAgent => {
export const transformStandardAgentDefinitionToFlatAgent = ({
standardAgentDefinition,
workspaceId,
existingAgentEntity,
}: {
standardAgentDefinition: StandardAgentDefinition;
workspaceId: string;
existingAgentEntity?: Pick<
AgentEntity,
'createdAt' | 'deletedAt' | 'updatedAt' | 'id'
>;
}): FlatAgent => {
const {
standardRoleId: _standardRoleId,
outputStrategy: _outputStrategy,
...agentData
} = standardAgentDefinition;
const createdAt = new Date();
return {
id: existingAgentEntity?.id ?? v4(),
createdAt: existingAgentEntity?.createdAt ?? createdAt,
updatedAt: existingAgentEntity?.updatedAt ?? createdAt,
deletedAt: existingAgentEntity?.deletedAt ?? null,
...agentData,
id: v4(),
workspaceId,
universalIdentifier: standardAgentDefinition.standardId || v4(),
universalIdentifier: standardAgentDefinition.standardId,
};
};