fix: minor enhancements

This commit is contained in:
MohamedAliBouhaouala
2025-05-06 19:03:00 +01:00
committed by Mohamed Marrouchi
parent 5dcd36be98
commit 5d8befacdf
11 changed files with 51 additions and 37 deletions

View File

@@ -20,7 +20,6 @@ import { LanguageRepository } from '@/i18n/repositories/language.repository';
import { LanguageModel } from '@/i18n/schemas/language.schema';
import { I18nService } from '@/i18n/services/i18n.service';
import { LanguageService } from '@/i18n/services/language.service';
import { LoggerService } from '@/logger/logger.service';
import { NlpEntityRepository } from '@/nlp/repositories/nlp-entity.repository';
import { NlpSampleEntityRepository } from '@/nlp/repositories/nlp-sample-entity.repository';
import { NlpValueRepository } from '@/nlp/repositories/nlp-value.repository';
@@ -128,7 +127,6 @@ describe('BlockController', () => {
PermissionService,
LanguageService,
PluginService,
LoggerService,
NlpEntityService,
NlpEntityRepository,
NlpSampleEntityRepository,

View File

@@ -395,22 +395,22 @@ export class BlockService extends BaseService<
const nlpCacheMap = await this.entityService.getNlpMap();
// @TODO Make nluPenaltyFactor configurable in UI settings
const nluPenaltyFactor = 0.95;
// Compute individual pattern scores using the cache
const patternScores: number[] = patterns.map((pattern) => {
const entityData = nlpCacheMap.get(pattern.entity);
if (!entityData) return 0;
const patternScores: number[] = patterns
.filter(({ entity }) => nlpCacheMap.has(entity))
.map((pattern) => {
const entityData = nlpCacheMap.get(pattern.entity);
const matchedEntity: NLU.ParseEntity | undefined = nlp.entities.find(
(e) => this.matchesEntityData(e, pattern, entityData),
);
const matchedEntity: NLU.ParseEntity | undefined = nlp.entities.find(
(e) => this.matchesEntityData(e, pattern, entityData!),
);
return this.computePatternScore(
matchedEntity,
pattern,
entityData,
nluPenaltyFactor,
);
});
return this.computePatternScore(
matchedEntity,
pattern,
entityData!,
nluPenaltyFactor,
);
});
// Sum the scores
return patternScores.reduce((sum, score) => sum + score, 0);

View File

@@ -9,6 +9,7 @@
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import {
BadRequestException,
ConflictException,
MethodNotAllowedException,
NotFoundException,
} from '@nestjs/common';
@@ -278,7 +279,7 @@ describe('NlpEntityController', () => {
};
await expect(
nlpEntityController.updateOne(buitInEntityId!, updateNlpEntity),
).rejects.toThrow(MethodNotAllowedException);
).rejects.toThrow(ConflictException);
});
it('should update weight if entity is builtin and weight is provided', async () => {

View File

@@ -9,6 +9,7 @@
import {
BadRequestException,
Body,
ConflictException,
Controller,
Delete,
Get,
@@ -166,7 +167,7 @@ export class NlpEntityController extends BaseController<
updateNlpEntityDto.weight,
);
} else {
throw new MethodNotAllowedException(
throw new ConflictException(
`Cannot update builtin NLP Entity ${nlpEntity.name} except for weight`,
);
}

View File

@@ -16,7 +16,7 @@ import {
IsOptional,
IsString,
Matches,
Min,
Validate,
} from 'class-validator';
import { DtoConfig } from '@/utils/types/dto.types';
@@ -55,8 +55,10 @@ export class NlpEntityCreateDto {
type: Number,
})
@IsOptional()
@Min(0.01, { message: 'Weight must be positive' })
@IsNumber()
@Validate((value) => value > 0, {
message: 'Weight must be a strictly positive number',
})
@IsNumber({ allowNaN: false, allowInfinity: false })
weight?: number;
}

View File

@@ -61,7 +61,14 @@ export class NlpEntityStub extends BaseSchema {
/**
* Entity's weight used to determine the next block to trigger in the conversational flow.
*/
@Prop({ type: Number, default: 1, min: 0 })
@Prop({
type: Number,
default: 1,
validate: {
validator: (value: number) => value > 0,
message: 'Weight must be a strictly positive number',
},
})
weight: number;
/**

View File

@@ -57,6 +57,8 @@ describe('nlpEntityService', () => {
provide: CACHE_MANAGER,
useValue: {
del: jest.fn(),
set: jest.fn(),
get: jest.fn(),
},
},
],
@@ -175,7 +177,7 @@ describe('nlpEntityService', () => {
await expect(
nlpEntityService.updateWeight(createdEntity.id, invalidWeight),
).rejects.toThrow('Weight must be a positive number');
).rejects.toThrow('Weight must be a strictly positive number');
});
afterEach(async () => {

View File

@@ -63,15 +63,11 @@ export class NlpEntityService extends BaseService<
* @returns A promise that resolves to the updated entity.
*/
async updateWeight(id: string, updatedWeight: number): Promise<NlpEntity> {
if (updatedWeight < 0) {
throw new Error('Weight must be a positive number');
if (updatedWeight <= 0) {
throw new Error('Weight must be a strictly positive number');
}
return await this.repository.updateOne(
id,
{ weight: updatedWeight },
{ new: true },
);
return await this.repository.updateOne(id, { weight: updatedWeight });
}
/**
@@ -139,7 +135,11 @@ export class NlpEntityService extends BaseService<
*/
@OnEvent('hook:nlpEntity:*')
async handleNlpEntityUpdateEvent() {
this.clearCache();
try {
await this.clearCache();
} catch (error) {
this.logger.error('Failed to clear NLP entity cache', error);
}
}
/**
@@ -148,7 +148,11 @@ export class NlpEntityService extends BaseService<
*/
@OnEvent('hook:nlpValue:*')
async handleNlpValueUpdateEvent() {
this.clearCache();
try {
await this.clearCache();
} catch (error) {
this.logger.error('Failed to clear NLP value cache', error);
}
}
/**

View File

@@ -123,7 +123,7 @@
"video_error": "Video not found",
"missing_fields_error": "Please make sure that all required fields are filled",
"weight_required_error": "Weight is required or invalid",
"weight_positive_number_error": "Weight must be a positive number"
"weight_positive_number_error": "Weight must be a strictly positive number"
},
"menu": {
"terms": "Terms of Use",

View File

@@ -122,7 +122,7 @@
"audio_error": "Audio introuvable",
"video_error": "Vidéo introuvable",
"missing_fields_error": "Veuillez vous assurer que tous les champs sont remplis correctement",
"weight_positive_number_error": "Le poids doit être un nombre positif",
"weight_positive_number_error": "Le poids doit être un nombre strictement positif",
"weight_required_error": "Le poids est requis ou bien invalide"
},
"menu": {

View File

@@ -145,16 +145,15 @@ export const NlpEntityVarForm: FC<ComponentFormProps<INlpEntity>> = ({
message: t("message.weight_positive_number_error"),
},
validate: (value) =>
value && value! > 0
value && value > 0
? true
: t("message.weight_positive_number_error"),
})}
type="number"
inputProps={{
min: 0.01,
min: 0,
step: 0.01,
inputMode: "numeric",
pattern: "[1-9][0-9]*",
}}
error={!!errors.weight}
helperText={errors.weight?.message}