59672e3e34
# 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>
89 lines
2.6 KiB
TypeScript
89 lines
2.6 KiB
TypeScript
import { isNonEmptyString } from '@sniptt/guards';
|
|
import {
|
|
isDefined,
|
|
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties,
|
|
} from 'twenty-shared/utils';
|
|
import { v4 } from 'uuid';
|
|
import { computeMetadataNameFromLabel } from 'twenty-shared/metadata';
|
|
|
|
import { type CreateAgentInput } from 'src/engine/metadata-modules/ai/ai-agent/dtos/create-agent.input';
|
|
import { type FlatAgent } from 'src/engine/metadata-modules/flat-agent/types/flat-agent.type';
|
|
import { type FlatRoleTarget } from 'src/engine/metadata-modules/flat-role-target/types/flat-role-target.type';
|
|
|
|
export type FromCreateAgentInputToFlatAgentArgs = {
|
|
createAgentInput: CreateAgentInput & { applicationId: string };
|
|
workspaceId: string;
|
|
};
|
|
|
|
export const fromCreateAgentInputToFlatAgent = ({
|
|
createAgentInput: rawCreateAgentInput,
|
|
workspaceId,
|
|
}: FromCreateAgentInputToFlatAgentArgs): {
|
|
flatAgentToCreate: FlatAgent;
|
|
flatRoleTargetToCreate: FlatRoleTarget | null;
|
|
} => {
|
|
const { roleId, ...createAgentInput } =
|
|
trimAndRemoveDuplicatedWhitespacesFromObjectStringProperties(
|
|
rawCreateAgentInput,
|
|
[
|
|
'name',
|
|
'label',
|
|
'icon',
|
|
'description',
|
|
'prompt',
|
|
'modelId',
|
|
'standardId',
|
|
'applicationId',
|
|
'roleId',
|
|
],
|
|
);
|
|
|
|
const createdAt = new Date();
|
|
const agentId = v4();
|
|
const standardId = createAgentInput.standardId ?? null;
|
|
const universalIdentifier = standardId ?? agentId;
|
|
|
|
const flatAgentToCreate: FlatAgent = {
|
|
id: agentId,
|
|
standardId,
|
|
name: isNonEmptyString(createAgentInput.name)
|
|
? createAgentInput.name
|
|
: computeMetadataNameFromLabel(createAgentInput.label),
|
|
label: createAgentInput.label,
|
|
icon: createAgentInput.icon ?? null,
|
|
description: createAgentInput.description ?? null,
|
|
prompt: createAgentInput.prompt,
|
|
modelId: createAgentInput.modelId,
|
|
responseFormat: createAgentInput.responseFormat ?? { type: 'text' },
|
|
workspaceId,
|
|
isCustom: true,
|
|
universalIdentifier,
|
|
applicationId: createAgentInput.applicationId,
|
|
modelConfiguration: createAgentInput.modelConfiguration ?? null,
|
|
evaluationInputs: createAgentInput.evaluationInputs ?? [],
|
|
createdAt,
|
|
updatedAt: createdAt,
|
|
deletedAt: null,
|
|
};
|
|
|
|
const flatRoleTargetToCreate: FlatRoleTarget | null = isDefined(roleId)
|
|
? {
|
|
id: v4(),
|
|
roleId,
|
|
userWorkspaceId: null,
|
|
agentId,
|
|
apiKeyId: null,
|
|
createdAt,
|
|
updatedAt: createdAt,
|
|
universalIdentifier: v4(),
|
|
workspaceId,
|
|
applicationId: createAgentInput.applicationId,
|
|
}
|
|
: null;
|
|
|
|
return {
|
|
flatAgentToCreate,
|
|
flatRoleTargetToCreate,
|
|
};
|
|
};
|