feat: implement skills system for AI agents (#16865)
## Summary This PR introduces a Skills system for AI agents, inspired by the [Agent Skills specification](https://agentskills.io/specification). ## Changes ### Backend - **SkillEntity**: New database entity with migration for storing skills - **V2 Sync Mechanism**: Implemented FlatSkill, builders, validators, and action handlers following the v2 flat entity pattern - **Standard Skills**: Pre-defined skills (workflow-building, data-manipulation, dashboard-building, metadata-building, research, code-interpreter, xlsx, pdf, docx, pptx) - **GraphQL API**: CRUD operations for skills with proper guards and permissions - **Workspace Cache**: Integrated skills into the workspace cache system ### Frontend - **Skills Table**: Searchable table in AI settings showing all skills - **Skill Form**: Create/edit page with Label (primary), Description, and Content (markdown editor) - **API Name**: Following existing patterns, name is derived from label with advanced settings toggle for custom API names - **Standard vs Custom**: Standard skills are read-only, custom skills can be edited/deleted ## Key Design Decisions - Skills are stored in the database (Salesforce-like approach) rather than files - Name is derived from Label by default (isLabelSyncedWithName pattern) - Skills reference functions/files via @ mentions in markdown content rather than explicit relations - Standard skills are synced from code, custom skills are created via UI ## Screenshots Skills table and form UI follow existing settings patterns. ## Testing - [x] Lint passes - [x] Typecheck passes - [ ] CI tests
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
import { Field, InputType } from '@nestjs/graphql';
|
||||
|
||||
import { IsNotEmpty, IsOptional, IsString } from 'class-validator';
|
||||
|
||||
@InputType()
|
||||
export class CreateSkillInput {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
name: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
label: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
icon?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
description?: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
content: string;
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { Field, HideField, ObjectType } from '@nestjs/graphql';
|
||||
|
||||
import {
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsNotEmpty,
|
||||
IsString,
|
||||
IsUUID,
|
||||
} from 'class-validator';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
|
||||
@ObjectType('Skill')
|
||||
export class SkillDTO {
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
@Field(() => UUIDScalarType)
|
||||
id: string;
|
||||
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
standardId?: string | null;
|
||||
|
||||
@IsString()
|
||||
@Field()
|
||||
name: string;
|
||||
|
||||
@IsString()
|
||||
@Field()
|
||||
label: string;
|
||||
|
||||
@IsString()
|
||||
@Field({ nullable: true })
|
||||
icon?: string;
|
||||
|
||||
@IsString()
|
||||
@Field({ nullable: true })
|
||||
description?: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
content: string;
|
||||
|
||||
@IsBoolean()
|
||||
@Field()
|
||||
isCustom: boolean;
|
||||
|
||||
@IsBoolean()
|
||||
@Field()
|
||||
isActive: boolean;
|
||||
|
||||
@HideField()
|
||||
workspaceId: string;
|
||||
|
||||
@Field(() => UUIDScalarType, { nullable: true })
|
||||
applicationId?: string;
|
||||
|
||||
@IsDateString()
|
||||
@Field()
|
||||
createdAt: Date;
|
||||
|
||||
@IsDateString()
|
||||
@Field()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Field, InputType } from '@nestjs/graphql';
|
||||
|
||||
import {
|
||||
IsBoolean,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
} from 'class-validator';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
|
||||
@InputType()
|
||||
export class UpdateSkillInput {
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
@Field(() => UUIDScalarType)
|
||||
id: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
name?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
label?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
icon?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
description?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
content?: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
isActive?: boolean;
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
Index,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
import { SyncableEntity } from 'src/engine/workspace-manager/workspace-sync/types/syncable-entity.interface';
|
||||
|
||||
@Entity('skill')
|
||||
@Index('IDX_SKILL_ID_IS_ACTIVE', ['id', 'isActive'])
|
||||
@Index('IDX_SKILL_NAME_WORKSPACE_ID_UNIQUE', ['name', 'workspaceId'], {
|
||||
unique: true,
|
||||
where: '"isActive" = true',
|
||||
})
|
||||
export class SkillEntity
|
||||
extends SyncableEntity
|
||||
implements Required<SkillEntity>
|
||||
{
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: true, type: 'uuid' })
|
||||
standardId: string | null;
|
||||
|
||||
@Column({ nullable: false })
|
||||
name: string;
|
||||
|
||||
@Column({ nullable: false })
|
||||
label: string;
|
||||
|
||||
@Column({ nullable: true, type: 'varchar' })
|
||||
icon: string | null;
|
||||
|
||||
@Column({ nullable: true, type: 'text' })
|
||||
description: string | null;
|
||||
|
||||
@Column({ nullable: false, type: 'text' })
|
||||
content: string;
|
||||
|
||||
@Column({ default: false })
|
||||
isCustom: boolean;
|
||||
|
||||
@Column({ default: true })
|
||||
isActive: boolean;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamptz' })
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamptz' })
|
||||
updatedAt: Date;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
import {
|
||||
type CallHandler,
|
||||
type ExecutionContext,
|
||||
Injectable,
|
||||
type NestInterceptor,
|
||||
} from '@nestjs/common';
|
||||
|
||||
import { type Observable, catchError } from 'rxjs';
|
||||
|
||||
import { skillGraphqlApiExceptionHandler } from 'src/engine/metadata-modules/skill/utils/skill-graphql-api-exception-handler.util';
|
||||
|
||||
@Injectable()
|
||||
export class SkillGraphqlApiExceptionInterceptor implements NestInterceptor {
|
||||
intercept(
|
||||
_context: ExecutionContext,
|
||||
next: CallHandler,
|
||||
): Observable<unknown> {
|
||||
return next.handle().pipe(catchError(skillGraphqlApiExceptionHandler));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { type MessageDescriptor } from '@lingui/core';
|
||||
import { msg } from '@lingui/core/macro';
|
||||
import { assertUnreachable } from 'twenty-shared/utils';
|
||||
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
export enum SkillExceptionCode {
|
||||
SKILL_NOT_FOUND = 'SKILL_NOT_FOUND',
|
||||
SKILL_ALREADY_EXISTS = 'SKILL_ALREADY_EXISTS',
|
||||
SKILL_IS_STANDARD = 'SKILL_IS_STANDARD',
|
||||
INVALID_SKILL_INPUT = 'INVALID_SKILL_INPUT',
|
||||
}
|
||||
|
||||
const getSkillExceptionUserFriendlyMessage = (code: SkillExceptionCode) => {
|
||||
switch (code) {
|
||||
case SkillExceptionCode.SKILL_NOT_FOUND:
|
||||
return msg`Skill not found.`;
|
||||
case SkillExceptionCode.SKILL_ALREADY_EXISTS:
|
||||
return msg`A skill with this name already exists.`;
|
||||
case SkillExceptionCode.SKILL_IS_STANDARD:
|
||||
return msg`Standard skills cannot be modified.`;
|
||||
case SkillExceptionCode.INVALID_SKILL_INPUT:
|
||||
return msg`Invalid skill input.`;
|
||||
default:
|
||||
assertUnreachable(code);
|
||||
}
|
||||
};
|
||||
|
||||
export class SkillException extends CustomException<SkillExceptionCode> {
|
||||
constructor(
|
||||
message: string,
|
||||
code: SkillExceptionCode,
|
||||
{ userFriendlyMessage }: { userFriendlyMessage?: MessageDescriptor } = {},
|
||||
) {
|
||||
super(message, code, {
|
||||
userFriendlyMessage:
|
||||
userFriendlyMessage ?? getSkillExceptionUserFriendlyMessage(code),
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ApplicationModule } from 'src/engine/core-modules/application/application.module';
|
||||
import { WorkspaceManyOrAllFlatEntityMapsCacheModule } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.module';
|
||||
import { FlatSkillModule } from 'src/engine/metadata-modules/flat-skill/flat-skill.module';
|
||||
import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permissions.module';
|
||||
import { SkillGraphqlApiExceptionInterceptor } from 'src/engine/metadata-modules/skill/interceptors/skill-graphql-api-exception.interceptor';
|
||||
import { SkillResolver } from 'src/engine/metadata-modules/skill/skill.resolver';
|
||||
import { SkillService } from 'src/engine/metadata-modules/skill/skill.service';
|
||||
import { WorkspaceMigrationBuilderGraphqlApiExceptionInterceptor } from 'src/engine/workspace-manager/workspace-migration-v2/interceptors/workspace-migration-builder-graphql-api-exception.interceptor';
|
||||
import { WorkspaceMigrationV2Module } from 'src/engine/workspace-manager/workspace-migration-v2/workspace-migration-v2.module';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
WorkspaceManyOrAllFlatEntityMapsCacheModule,
|
||||
WorkspaceMigrationV2Module,
|
||||
ApplicationModule,
|
||||
PermissionsModule,
|
||||
FlatSkillModule,
|
||||
],
|
||||
providers: [
|
||||
SkillService,
|
||||
SkillResolver,
|
||||
SkillGraphqlApiExceptionInterceptor,
|
||||
WorkspaceMigrationBuilderGraphqlApiExceptionInterceptor,
|
||||
],
|
||||
exports: [SkillService],
|
||||
})
|
||||
export class SkillModule {}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { UseGuards, UseInterceptors } from '@nestjs/common';
|
||||
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
|
||||
|
||||
import { PermissionFlagType } from 'twenty-shared/constants';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
|
||||
import { SettingsPermissionGuard } from 'src/engine/guards/settings-permission.guard';
|
||||
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
|
||||
import { CreateSkillInput } from 'src/engine/metadata-modules/skill/dtos/create-skill.input';
|
||||
import { SkillDTO } from 'src/engine/metadata-modules/skill/dtos/skill.dto';
|
||||
import { UpdateSkillInput } from 'src/engine/metadata-modules/skill/dtos/update-skill.input';
|
||||
import { SkillGraphqlApiExceptionInterceptor } from 'src/engine/metadata-modules/skill/interceptors/skill-graphql-api-exception.interceptor';
|
||||
import { SkillService } from 'src/engine/metadata-modules/skill/skill.service';
|
||||
import { WorkspaceMigrationBuilderGraphqlApiExceptionInterceptor } from 'src/engine/workspace-manager/workspace-migration-v2/interceptors/workspace-migration-builder-graphql-api-exception.interceptor';
|
||||
|
||||
@UseGuards(WorkspaceAuthGuard, SettingsPermissionGuard(PermissionFlagType.AI))
|
||||
@UseInterceptors(
|
||||
WorkspaceMigrationBuilderGraphqlApiExceptionInterceptor,
|
||||
SkillGraphqlApiExceptionInterceptor,
|
||||
)
|
||||
@Resolver(() => SkillDTO)
|
||||
export class SkillResolver {
|
||||
constructor(private readonly skillService: SkillService) {}
|
||||
|
||||
@Query(() => [SkillDTO])
|
||||
async skills(
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO[]> {
|
||||
return this.skillService.findAll(workspace.id);
|
||||
}
|
||||
|
||||
@Query(() => SkillDTO, { nullable: true })
|
||||
async skill(
|
||||
@Args('id', { type: () => UUIDScalarType }) id: string,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO | null> {
|
||||
return this.skillService.findById(id, workspace.id);
|
||||
}
|
||||
|
||||
@Mutation(() => SkillDTO)
|
||||
async createSkill(
|
||||
@Args('input') input: CreateSkillInput,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO> {
|
||||
return this.skillService.create(input, workspace.id);
|
||||
}
|
||||
|
||||
@Mutation(() => SkillDTO)
|
||||
async updateSkill(
|
||||
@Args('input') input: UpdateSkillInput,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO> {
|
||||
return this.skillService.update(input, workspace.id);
|
||||
}
|
||||
|
||||
@Mutation(() => SkillDTO)
|
||||
async deleteSkill(
|
||||
@Args('id', { type: () => UUIDScalarType }) id: string,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO> {
|
||||
return this.skillService.delete(id, workspace.id);
|
||||
}
|
||||
|
||||
@Mutation(() => SkillDTO)
|
||||
async activateSkill(
|
||||
@Args('id', { type: () => UUIDScalarType }) id: string,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO> {
|
||||
return this.skillService.activate(id, workspace.id);
|
||||
}
|
||||
|
||||
@Mutation(() => SkillDTO)
|
||||
async deactivateSkill(
|
||||
@Args('id', { type: () => UUIDScalarType }) id: string,
|
||||
@AuthWorkspace() workspace: WorkspaceEntity,
|
||||
): Promise<SkillDTO> {
|
||||
return this.skillService.deactivate(id, workspace.id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,381 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
|
||||
import { WorkspaceManyOrAllFlatEntityMapsCacheService } from 'src/engine/metadata-modules/flat-entity/services/workspace-many-or-all-flat-entity-maps-cache.service';
|
||||
import { findFlatEntityByIdInFlatEntityMapsOrThrow } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps-or-throw.util';
|
||||
import { findFlatEntityByIdInFlatEntityMaps } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-id-in-flat-entity-maps.util';
|
||||
import { type FlatSkill } from 'src/engine/metadata-modules/flat-skill/types/flat-skill.type';
|
||||
import { fromCreateSkillInputToFlatSkillToCreate } from 'src/engine/metadata-modules/flat-skill/utils/from-create-skill-input-to-flat-skill-to-create.util';
|
||||
import { fromDeleteSkillInputToFlatSkillOrThrow } from 'src/engine/metadata-modules/flat-skill/utils/from-delete-skill-input-to-flat-skill-or-throw.util';
|
||||
import { fromFlatSkillToSkillDto } from 'src/engine/metadata-modules/flat-skill/utils/from-flat-skill-to-skill-dto.util';
|
||||
import { fromUpdateSkillInputToFlatSkillToUpdateOrThrow } from 'src/engine/metadata-modules/flat-skill/utils/from-update-skill-input-to-flat-skill-to-update-or-throw.util';
|
||||
import { type CreateSkillInput } from 'src/engine/metadata-modules/skill/dtos/create-skill.input';
|
||||
import { type SkillDTO } from 'src/engine/metadata-modules/skill/dtos/skill.dto';
|
||||
import { type UpdateSkillInput } from 'src/engine/metadata-modules/skill/dtos/update-skill.input';
|
||||
import {
|
||||
SkillException,
|
||||
SkillExceptionCode,
|
||||
} from 'src/engine/metadata-modules/skill/skill.exception';
|
||||
import { WorkspaceMigrationBuilderExceptionV2 } from 'src/engine/workspace-manager/workspace-migration-v2/exceptions/workspace-migration-builder-exception-v2';
|
||||
import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration-v2/services/workspace-migration-validate-build-and-run-service';
|
||||
|
||||
@Injectable()
|
||||
export class SkillService {
|
||||
constructor(
|
||||
private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,
|
||||
private readonly workspaceManyOrAllFlatEntityMapsCacheService: WorkspaceManyOrAllFlatEntityMapsCacheService,
|
||||
private readonly applicationService: ApplicationService,
|
||||
) {}
|
||||
|
||||
async findAll(workspaceId: string): Promise<SkillDTO[]> {
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return Object.values(flatSkillMaps.byId)
|
||||
.filter(isDefined)
|
||||
.sort((a, b) => a.label.localeCompare(b.label))
|
||||
.map(fromFlatSkillToSkillDto);
|
||||
}
|
||||
|
||||
async findById(id: string, workspaceId: string): Promise<SkillDTO | null> {
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
const flatSkill = findFlatEntityByIdInFlatEntityMaps({
|
||||
flatEntityId: id,
|
||||
flatEntityMaps: flatSkillMaps,
|
||||
});
|
||||
|
||||
if (!isDefined(flatSkill)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return fromFlatSkillToSkillDto(flatSkill);
|
||||
}
|
||||
|
||||
async create(
|
||||
input: CreateSkillInput,
|
||||
workspaceId: string,
|
||||
): Promise<SkillDTO> {
|
||||
const { workspaceCustomFlatApplication } =
|
||||
await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
|
||||
{ workspaceId },
|
||||
);
|
||||
|
||||
const flatSkillToCreate = fromCreateSkillInputToFlatSkillToCreate({
|
||||
createSkillInput: input,
|
||||
workspaceId,
|
||||
applicationId: workspaceCustomFlatApplication.id,
|
||||
});
|
||||
|
||||
const validateAndBuildResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
allFlatEntityOperationByMetadataName: {
|
||||
skill: {
|
||||
flatEntityToCreate: [flatSkillToCreate],
|
||||
flatEntityToDelete: [],
|
||||
flatEntityToUpdate: [],
|
||||
},
|
||||
},
|
||||
workspaceId,
|
||||
isSystemBuild: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
'Multiple validation errors occurred while creating skill',
|
||||
);
|
||||
}
|
||||
|
||||
const { flatSkillMaps: recomputedFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return fromFlatSkillToSkillDto(
|
||||
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: flatSkillToCreate.id,
|
||||
flatEntityMaps: recomputedFlatSkillMaps,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async update(
|
||||
input: UpdateSkillInput,
|
||||
workspaceId: string,
|
||||
): Promise<SkillDTO> {
|
||||
const { flatSkillMaps: existingFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
const flatSkillToUpdate = fromUpdateSkillInputToFlatSkillToUpdateOrThrow({
|
||||
flatSkillMaps: existingFlatSkillMaps,
|
||||
updateSkillInput: input,
|
||||
});
|
||||
|
||||
const validateAndBuildResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
allFlatEntityOperationByMetadataName: {
|
||||
skill: {
|
||||
flatEntityToCreate: [],
|
||||
flatEntityToDelete: [],
|
||||
flatEntityToUpdate: [flatSkillToUpdate],
|
||||
},
|
||||
},
|
||||
workspaceId,
|
||||
isSystemBuild: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
'Multiple validation errors occurred while updating skill',
|
||||
);
|
||||
}
|
||||
|
||||
const { flatSkillMaps: recomputedFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return fromFlatSkillToSkillDto(
|
||||
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: input.id,
|
||||
flatEntityMaps: recomputedFlatSkillMaps,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async delete(id: string, workspaceId: string): Promise<SkillDTO> {
|
||||
const { flatSkillMaps: existingFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
const flatSkillToDelete = fromDeleteSkillInputToFlatSkillOrThrow({
|
||||
flatSkillMaps: existingFlatSkillMaps,
|
||||
skillId: id,
|
||||
});
|
||||
|
||||
const validateAndBuildResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
allFlatEntityOperationByMetadataName: {
|
||||
skill: {
|
||||
flatEntityToCreate: [],
|
||||
flatEntityToDelete: [flatSkillToDelete],
|
||||
flatEntityToUpdate: [],
|
||||
},
|
||||
},
|
||||
workspaceId,
|
||||
isSystemBuild: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
'Multiple validation errors occurred while deleting skill',
|
||||
);
|
||||
}
|
||||
|
||||
return fromFlatSkillToSkillDto(flatSkillToDelete);
|
||||
}
|
||||
|
||||
async findAllFlatSkills(workspaceId: string): Promise<FlatSkill[]> {
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return Object.values(flatSkillMaps.byId)
|
||||
.filter(isDefined)
|
||||
.filter((flatSkill) => flatSkill.isActive)
|
||||
.sort((a, b) => a.label.localeCompare(b.label));
|
||||
}
|
||||
|
||||
async findFlatSkillsByNames(
|
||||
names: string[],
|
||||
workspaceId: string,
|
||||
): Promise<FlatSkill[]> {
|
||||
if (names.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return Object.values(flatSkillMaps.byId)
|
||||
.filter(isDefined)
|
||||
.filter(
|
||||
(flatSkill) => names.includes(flatSkill.name) && flatSkill.isActive,
|
||||
);
|
||||
}
|
||||
|
||||
async activate(id: string, workspaceId: string): Promise<SkillDTO> {
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
const existingFlatSkill = findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: id,
|
||||
flatEntityMaps: flatSkillMaps,
|
||||
});
|
||||
|
||||
const flatSkillToUpdate: FlatSkill = {
|
||||
...existingFlatSkill,
|
||||
isActive: true,
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const validateAndBuildResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
allFlatEntityOperationByMetadataName: {
|
||||
skill: {
|
||||
flatEntityToCreate: [],
|
||||
flatEntityToDelete: [],
|
||||
flatEntityToUpdate: [flatSkillToUpdate],
|
||||
},
|
||||
},
|
||||
workspaceId,
|
||||
isSystemBuild: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
'Multiple validation errors occurred while activating skill',
|
||||
);
|
||||
}
|
||||
|
||||
const { flatSkillMaps: recomputedFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return fromFlatSkillToSkillDto(
|
||||
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: id,
|
||||
flatEntityMaps: recomputedFlatSkillMaps,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async deactivate(id: string, workspaceId: string): Promise<SkillDTO> {
|
||||
const { flatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
const existingFlatSkill = findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: id,
|
||||
flatEntityMaps: flatSkillMaps,
|
||||
});
|
||||
|
||||
const flatSkillToUpdate: FlatSkill = {
|
||||
...existingFlatSkill,
|
||||
isActive: false,
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const validateAndBuildResult =
|
||||
await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunWorkspaceMigration(
|
||||
{
|
||||
allFlatEntityOperationByMetadataName: {
|
||||
skill: {
|
||||
flatEntityToCreate: [],
|
||||
flatEntityToDelete: [],
|
||||
flatEntityToUpdate: [flatSkillToUpdate],
|
||||
},
|
||||
},
|
||||
workspaceId,
|
||||
isSystemBuild: false,
|
||||
},
|
||||
);
|
||||
|
||||
if (isDefined(validateAndBuildResult)) {
|
||||
throw new WorkspaceMigrationBuilderExceptionV2(
|
||||
validateAndBuildResult,
|
||||
'Multiple validation errors occurred while deactivating skill',
|
||||
);
|
||||
}
|
||||
|
||||
const { flatSkillMaps: recomputedFlatSkillMaps } =
|
||||
await this.workspaceManyOrAllFlatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
|
||||
{
|
||||
workspaceId,
|
||||
flatMapsKeys: ['flatSkillMaps'],
|
||||
},
|
||||
);
|
||||
|
||||
return fromFlatSkillToSkillDto(
|
||||
findFlatEntityByIdInFlatEntityMapsOrThrow({
|
||||
flatEntityId: id,
|
||||
flatEntityMaps: recomputedFlatSkillMaps,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
async findByIdOrThrow(id: string, workspaceId: string): Promise<SkillDTO> {
|
||||
const skill = await this.findById(id, workspaceId);
|
||||
|
||||
if (!isDefined(skill)) {
|
||||
throw new SkillException(
|
||||
'Skill not found',
|
||||
SkillExceptionCode.SKILL_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
return skill;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
import { assertUnreachable } from 'twenty-shared/utils';
|
||||
|
||||
import {
|
||||
ConflictError,
|
||||
ForbiddenError,
|
||||
NotFoundError,
|
||||
UserInputError,
|
||||
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
import {
|
||||
SkillException,
|
||||
SkillExceptionCode,
|
||||
} from 'src/engine/metadata-modules/skill/skill.exception';
|
||||
|
||||
export const skillGraphqlApiExceptionHandler = (error: Error) => {
|
||||
if (error instanceof SkillException) {
|
||||
switch (error.code) {
|
||||
case SkillExceptionCode.SKILL_NOT_FOUND:
|
||||
throw new NotFoundError(error);
|
||||
case SkillExceptionCode.INVALID_SKILL_INPUT:
|
||||
throw new UserInputError(error);
|
||||
case SkillExceptionCode.SKILL_ALREADY_EXISTS:
|
||||
throw new ConflictError(error);
|
||||
case SkillExceptionCode.SKILL_IS_STANDARD:
|
||||
throw new ForbiddenError(error);
|
||||
default: {
|
||||
return assertUnreachable(error.code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
};
|
||||
Reference in New Issue
Block a user