mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: restore files
This commit is contained in:
parent
0ce7894628
commit
68835bd306
@ -1,5 +1,4 @@
|
||||
/*
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
@ -12,6 +11,9 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
|
||||
import { AttachmentModule } from '@/attachment/attachment.module';
|
||||
import { AttachmentRepository } from '@/attachment/repositories/attachment.repository';
|
||||
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
|
||||
import { AttachmentService } from '@/attachment/services/attachment.service';
|
||||
import { ChannelModule } from '@/channel/channel.module';
|
||||
import { CmsModule } from '@/cms/cms.module';
|
||||
import { NlpModule } from '@/nlp/nlp.module';
|
||||
@ -60,6 +62,7 @@ import { SubscriberService } from './services/subscriber.service';
|
||||
SubscriberModel,
|
||||
ConversationModel,
|
||||
SubscriberModel,
|
||||
AttachmentModel,
|
||||
]),
|
||||
forwardRef(() => ChannelModule),
|
||||
CmsModule,
|
||||
@ -95,6 +98,8 @@ import { SubscriberService } from './services/subscriber.service';
|
||||
ConversationService,
|
||||
ChatService,
|
||||
BotService,
|
||||
AttachmentService,
|
||||
AttachmentRepository,
|
||||
],
|
||||
exports: [
|
||||
SubscriberService,
|
||||
|
@ -37,6 +37,7 @@ import { NlpValueRepository } from '@/nlp/repositories/nlp-value.repository';
|
||||
import { NlpEntityModel } from '@/nlp/schemas/nlp-entity.schema';
|
||||
import { NlpSampleEntityModel } from '@/nlp/schemas/nlp-sample-entity.schema';
|
||||
import { NlpValueModel } from '@/nlp/schemas/nlp-value.schema';
|
||||
import { NlpCacheMap } from '@/nlp/schemas/types';
|
||||
import { NlpEntityService } from '@/nlp/services/nlp-entity.service';
|
||||
import { NlpValueService } from '@/nlp/services/nlp-value.service';
|
||||
import { PluginService } from '@/plugins/plugins.service';
|
||||
@ -51,8 +52,11 @@ import {
|
||||
blockGetStarted,
|
||||
blockProductListMock,
|
||||
blocks,
|
||||
mockModifiedNlpBlock,
|
||||
mockNlpBlock,
|
||||
nlpBlocks,
|
||||
mockNlpPatternsSetOne,
|
||||
mockNlpPatternsSetThree,
|
||||
mockNlpPatternsSetTwo,
|
||||
} from '@/utils/test/mocks/block';
|
||||
import {
|
||||
contextBlankInstance,
|
||||
@ -81,25 +85,6 @@ import { CategoryRepository } from './../repositories/category.repository';
|
||||
import { BlockService } from './block.service';
|
||||
import { CategoryService } from './category.service';
|
||||
|
||||
// Create a mock for the NlpEntityService
|
||||
const mockNlpEntityService = {
|
||||
findOne: jest.fn().mockImplementation((query) => {
|
||||
if (query.name === 'intent') {
|
||||
return Promise.resolve({
|
||||
lookups: ['trait'],
|
||||
id: '67e3e41eff551ca5be70559c',
|
||||
});
|
||||
}
|
||||
if (query.name === 'firstname') {
|
||||
return Promise.resolve({
|
||||
lookups: ['trait'],
|
||||
id: '67e3e41eff551ca5be70559d',
|
||||
});
|
||||
}
|
||||
return Promise.resolve(null); // Default response if the entity isn't found
|
||||
}),
|
||||
};
|
||||
|
||||
describe('BlockService', () => {
|
||||
let blockRepository: BlockRepository;
|
||||
let categoryRepository: CategoryRepository;
|
||||
@ -110,8 +95,6 @@ describe('BlockService', () => {
|
||||
let contentService: ContentService;
|
||||
let contentTypeService: ContentTypeService;
|
||||
let nlpEntityService: NlpEntityService;
|
||||
let settingService: SettingService;
|
||||
let settings: Settings;
|
||||
|
||||
beforeAll(async () => {
|
||||
const { getMocks } = await buildTestingMocks({
|
||||
@ -129,8 +112,8 @@ describe('BlockService', () => {
|
||||
LabelModel,
|
||||
LanguageModel,
|
||||
NlpEntityModel,
|
||||
NlpValueModel,
|
||||
NlpSampleEntityModel,
|
||||
NlpValueModel,
|
||||
]),
|
||||
],
|
||||
providers: [
|
||||
@ -140,19 +123,19 @@ describe('BlockService', () => {
|
||||
ContentRepository,
|
||||
AttachmentRepository,
|
||||
LanguageRepository,
|
||||
NlpEntityRepository,
|
||||
NlpSampleEntityRepository,
|
||||
NlpValueRepository,
|
||||
BlockService,
|
||||
CategoryService,
|
||||
ContentTypeService,
|
||||
ContentService,
|
||||
AttachmentService,
|
||||
LanguageService,
|
||||
NlpValueService,
|
||||
NlpEntityRepository,
|
||||
NlpValueRepository,
|
||||
NlpSampleEntityRepository,
|
||||
NlpEntityService,
|
||||
{
|
||||
provide: NlpEntityService, // Mocking NlpEntityService
|
||||
useValue: mockNlpEntityService,
|
||||
provide: NlpValueService,
|
||||
useValue: {},
|
||||
},
|
||||
{
|
||||
provide: PluginService,
|
||||
@ -186,14 +169,22 @@ describe('BlockService', () => {
|
||||
},
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
blockService = module.get<BlockService>(BlockService);
|
||||
contentService = module.get<ContentService>(ContentService);
|
||||
settingService = module.get<SettingService>(SettingService);
|
||||
contentTypeService = module.get<ContentTypeService>(ContentTypeService);
|
||||
categoryRepository = module.get<CategoryRepository>(CategoryRepository);
|
||||
blockRepository = module.get<BlockRepository>(BlockRepository);
|
||||
nlpEntityService = module.get<NlpEntityService>(NlpEntityService);
|
||||
});
|
||||
[
|
||||
blockService,
|
||||
contentService,
|
||||
contentTypeService,
|
||||
categoryRepository,
|
||||
blockRepository,
|
||||
nlpEntityService,
|
||||
] = await getMocks([
|
||||
BlockService,
|
||||
ContentService,
|
||||
ContentTypeService,
|
||||
CategoryRepository,
|
||||
BlockRepository,
|
||||
NlpEntityService,
|
||||
]);
|
||||
category = (await categoryRepository.findOne({ label: 'default' }))!;
|
||||
hasPreviousBlocks = (await blockRepository.findOne({
|
||||
name: 'hasPreviousBlocks',
|
||||
@ -554,59 +545,6 @@ describe('BlockService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchBestNLP', () => {
|
||||
it('should return undefined if blocks is empty', async () => {
|
||||
const result = await blockService.matchBestNLP([]);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return the only block if there is one', async () => {
|
||||
const result = await blockService.matchBestNLP([blockEmpty]);
|
||||
expect(result).toBe(blockEmpty);
|
||||
});
|
||||
|
||||
it('should correctly select the best block based on NLP scores', async () => {
|
||||
const result = await blockService.matchBestNLP(nlpBlocks);
|
||||
expect(result).toBe(mockNlpBlock);
|
||||
|
||||
// Iterate over each block
|
||||
for (const block of nlpBlocks) {
|
||||
// Flatten the patterns array and filter valid NLP patterns
|
||||
block.patterns
|
||||
.flatMap((pattern) => (Array.isArray(pattern) ? pattern : []))
|
||||
.filter((p) => typeof p === 'object' && 'entity' in p && 'match' in p) // Filter only valid patterns with entity and match
|
||||
.forEach((p) => {
|
||||
// Check if findOne was called with the correct entity
|
||||
expect(nlpEntityService.findOne).toHaveBeenCalledWith(
|
||||
{ name: p.entity },
|
||||
undefined,
|
||||
{ _id: 0, lookups: 1 },
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
it('should return the block with the highest combined score', async () => {
|
||||
const result = await blockService.matchBestNLP(nlpBlocks);
|
||||
expect(result).toBe(mockNlpBlock);
|
||||
// Iterate over each block
|
||||
for (const block of nlpBlocks) {
|
||||
// Flatten the patterns array and filter valid NLP patterns
|
||||
block.patterns
|
||||
.flatMap((pattern) => (Array.isArray(pattern) ? pattern : []))
|
||||
.filter((p) => typeof p === 'object' && 'entity' in p && 'match' in p) // Filter only valid patterns with entity and match
|
||||
.forEach((p) => {
|
||||
// Check if findOne was called with the correct entity
|
||||
expect(nlpEntityService.findOne).toHaveBeenCalledWith(
|
||||
{ name: p.entity },
|
||||
undefined,
|
||||
{ _id: 0, lookups: 1 },
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('matchText', () => {
|
||||
it('should return false for matching an empty text', () => {
|
||||
const result = blockService.matchText('', blockGetStarted);
|
||||
|
@ -16,6 +16,7 @@ import { CONSOLE_CHANNEL_NAME } from '@/extensions/channels/console/settings';
|
||||
import { NLU } from '@/helper/types';
|
||||
import { I18nService } from '@/i18n/services/i18n.service';
|
||||
import { LanguageService } from '@/i18n/services/language.service';
|
||||
import { NlpCacheMap } from '@/nlp/schemas/types';
|
||||
import { NlpEntityService } from '@/nlp/services/nlp-entity.service';
|
||||
import { PluginService } from '@/plugins/plugins.service';
|
||||
import { PluginType } from '@/plugins/types';
|
||||
@ -37,6 +38,7 @@ import {
|
||||
StdOutgoingSystemEnvelope,
|
||||
} from '../schemas/types/message';
|
||||
import {
|
||||
isNlpPattern,
|
||||
NlpPattern,
|
||||
NlpPatternMatchResult,
|
||||
PayloadPattern,
|
||||
@ -376,77 +378,62 @@ export class BlockService extends BaseService<
|
||||
}
|
||||
|
||||
/**
|
||||
* Identifies and returns the best-matching block based on NLP entity scores.
|
||||
* Selects the best-matching block based on NLP pattern scoring.
|
||||
*
|
||||
* This function evaluates a list of blocks by analyzing their associated NLP entities
|
||||
* and scoring them based on predefined lookup entities. The block with the highest
|
||||
* score is selected as the best match.
|
||||
* @param blocks - Blocks on which to perform the filtering
|
||||
* This function evaluates each block by calculating a score derived from its matched NLP patterns,
|
||||
* the parsed NLP entities, and a penalty factor. It compares the scores across all blocks and
|
||||
* returns the one with the highest calculated score.
|
||||
*
|
||||
* @returns The best block
|
||||
* @param blocks - An array of candidate blocks to evaluate.
|
||||
* @param matchedPatterns - A two-dimensional array of matched NLP patterns corresponding to each block.
|
||||
* @param nlp - The parsed NLP entities used for scoring.
|
||||
* @param nlpPenaltyFactor - A numeric penalty factor applied during scoring to influence block selection.
|
||||
* @returns The block with the highest NLP score, or undefined if no valid block is found.
|
||||
*/
|
||||
async matchBestNLP(
|
||||
blocks: Block[] | BlockFull[] | undefined,
|
||||
blocks: (Block | BlockFull)[] | undefined,
|
||||
matchedPatterns: NlpPattern[][],
|
||||
nlp: NLU.ParseEntities,
|
||||
nlpPenaltyFactor: number,
|
||||
): Promise<Block | BlockFull | undefined> {
|
||||
// @TODO make lookup scores configurable in hexabot settings
|
||||
const lookupScores: { [key: string]: number } = {
|
||||
trait: 2,
|
||||
keywords: 1,
|
||||
};
|
||||
if (!blocks || blocks.length === 0) return undefined;
|
||||
if (blocks.length === 1) return blocks[0];
|
||||
|
||||
// No blocks to check against
|
||||
if (blocks?.length === 0 || !blocks) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// If there's only one block, return it immediately.
|
||||
if (blocks.length === 1) {
|
||||
return blocks[0];
|
||||
}
|
||||
let bestBlock: Block | BlockFull | undefined;
|
||||
let highestScore = 0;
|
||||
|
||||
// Iterate over each block in blocks
|
||||
for (const block of blocks) {
|
||||
let nlpScore = 0;
|
||||
|
||||
// Gather all entity lookups for patterns that include an entity
|
||||
const entityLookups = await Promise.all(
|
||||
block.patterns
|
||||
.flatMap((pattern) => (Array.isArray(pattern) ? pattern : []))
|
||||
.filter((p) => typeof p === 'object' && 'entity' in p && 'match' in p)
|
||||
.map(async (pattern) => {
|
||||
const entityName = pattern.entity;
|
||||
return await this.entityService.findOne(
|
||||
{ name: entityName },
|
||||
undefined,
|
||||
{ lookups: 1, _id: 0 },
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
nlpScore += entityLookups.reduce((score, entityLookup) => {
|
||||
if (
|
||||
entityLookup &&
|
||||
entityLookup.lookups[0] &&
|
||||
lookupScores[entityLookup.lookups[0]]
|
||||
) {
|
||||
return score + lookupScores[entityLookup.lookups[0]]; // Add points based on the lookup type
|
||||
const entityNames: string[] = blocks.flatMap((block) =>
|
||||
block.patterns.flatMap((patternGroup) => {
|
||||
if (Array.isArray(patternGroup)) {
|
||||
return patternGroup.flatMap((pattern) =>
|
||||
isNlpPattern(pattern) ? [pattern.entity] : [],
|
||||
);
|
||||
}
|
||||
return score; // Return the current score if no match
|
||||
}, 0);
|
||||
|
||||
// Update the best block if the current block has a higher NLP score
|
||||
return []; // Skip non-array patternGroups
|
||||
}),
|
||||
);
|
||||
const uniqueEntityNames: string[] = [...new Set(entityNames)];
|
||||
const nlpCacheMap: NlpCacheMap =
|
||||
await this.entityService.getNlpMap(uniqueEntityNames);
|
||||
// Iterate through all blocks and calculate their NLP score
|
||||
for (let i = 0; i < blocks.length; i++) {
|
||||
const block = blocks[i];
|
||||
const patterns = matchedPatterns[i];
|
||||
// If compatible, calculate the NLP score for this block
|
||||
const nlpScore: number = this.calculateBlockScore(
|
||||
patterns,
|
||||
nlp,
|
||||
nlpCacheMap,
|
||||
nlpPenaltyFactor,
|
||||
);
|
||||
if (nlpScore > highestScore) {
|
||||
highestScore = nlpScore;
|
||||
bestBlock = block;
|
||||
}
|
||||
}
|
||||
|
||||
this.logger.debug(`Best Nlp Score obtained ${highestScore}`);
|
||||
this.logger.debug(
|
||||
`Best retrieved block based on NLP entities ${JSON.stringify(bestBlock)}`,
|
||||
);
|
||||
this.logger.debug(`Best NLP score obtained: ${highestScore}`);
|
||||
this.logger.debug(`Best block selected: ${JSON.stringify(bestBlock)}`);
|
||||
|
||||
return bestBlock;
|
||||
}
|
||||
|
||||
@ -465,12 +452,12 @@ export class BlockService extends BaseService<
|
||||
* @param nlpPenaltyFactor - A multiplier applied to scores when the pattern match type is 'entity'.
|
||||
* @returns A numeric score representing how well the block matches the given NLP context.
|
||||
*/
|
||||
async calculateBlockScore(
|
||||
calculateBlockScore(
|
||||
patterns: NlpPattern[],
|
||||
nlp: NLU.ParseEntities,
|
||||
nlpCacheMap: NlpCacheMap,
|
||||
nlpPenaltyFactor: number,
|
||||
): Promise<number> {
|
||||
): number {
|
||||
// Compute individual pattern scores using the cache
|
||||
const patternScores: number[] = patterns.map((pattern) => {
|
||||
const entityData = nlpCacheMap.get(pattern.entity);
|
||||
|
@ -1,5 +1,4 @@
|
||||
/*
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
* Copyright © 2025 Hexastack. All rights reserved.
|
||||
*
|
||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||
|
@ -16,7 +16,7 @@ import { ButtonType, PayloadType } from '@/chat/schemas/types/button';
|
||||
import { CaptureVar } from '@/chat/schemas/types/capture-var';
|
||||
import { OutgoingMessageFormat } from '@/chat/schemas/types/message';
|
||||
import { BlockOptions, ContentOptions } from '@/chat/schemas/types/options';
|
||||
import { Pattern } from '@/chat/schemas/types/pattern';
|
||||
import { NlpPattern, Pattern } from '@/chat/schemas/types/pattern';
|
||||
import { QuickReplyType } from '@/chat/schemas/types/quick-reply';
|
||||
|
||||
import { modelInstance } from './misc';
|
||||
@ -246,7 +246,45 @@ export const blockGetStarted = {
|
||||
message: ['Welcome! How are you ? '],
|
||||
} as unknown as BlockFull;
|
||||
|
||||
export const mockNlpBlock = {
|
||||
export const mockNlpPatternsSetOne: NlpPattern[] = [
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'greeting',
|
||||
},
|
||||
{
|
||||
entity: 'firstname',
|
||||
match: 'value',
|
||||
value: 'jhon',
|
||||
},
|
||||
];
|
||||
|
||||
export const mockNlpPatternsSetTwo: NlpPattern[] = [
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'affirmation',
|
||||
},
|
||||
{
|
||||
entity: 'firstname',
|
||||
match: 'value',
|
||||
value: 'mark',
|
||||
},
|
||||
];
|
||||
|
||||
export const mockNlpPatternsSetThree: NlpPattern[] = [
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'greeting',
|
||||
},
|
||||
{
|
||||
entity: 'firstname',
|
||||
match: 'entity',
|
||||
},
|
||||
];
|
||||
|
||||
export const mockNlpBlock: BlockFull = {
|
||||
...baseBlockInstance,
|
||||
name: 'Mock Nlp',
|
||||
patterns: [
|
||||
@ -254,27 +292,38 @@ export const mockNlpBlock = {
|
||||
'/we*lcome/',
|
||||
{ label: 'Mock Nlp', value: 'MOCK_NLP' },
|
||||
[
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'greeting',
|
||||
},
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'want',
|
||||
},
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'affirmative',
|
||||
},
|
||||
...mockNlpPatternsSetOne,
|
||||
[
|
||||
{
|
||||
entity: 'intent',
|
||||
match: 'value',
|
||||
value: 'greeting',
|
||||
},
|
||||
{
|
||||
entity: 'firstname',
|
||||
match: 'value',
|
||||
value: 'doe',
|
||||
},
|
||||
],
|
||||
],
|
||||
],
|
||||
trigger_labels: customerLabelsMock,
|
||||
message: ['Good to see you again '],
|
||||
} as unknown as BlockFull;
|
||||
|
||||
export const mockModifiedNlpBlock: BlockFull = {
|
||||
...baseBlockInstance,
|
||||
name: 'Modified Mock Nlp',
|
||||
patterns: [
|
||||
'Hello',
|
||||
'/we*lcome/',
|
||||
{ label: 'Modified Mock Nlp', value: 'MODIFIED_MOCK_NLP' },
|
||||
[...mockNlpPatternsSetThree],
|
||||
],
|
||||
trigger_labels: customerLabelsMock,
|
||||
message: ['Hello there'],
|
||||
} as unknown as BlockFull;
|
||||
|
||||
const patternsProduct: Pattern[] = [
|
||||
'produit',
|
||||
[
|
||||
@ -316,5 +365,3 @@ export const blockCarouselMock = {
|
||||
export const blocks: BlockFull[] = [blockGetStarted, blockEmpty];
|
||||
|
||||
export const nlpBlocks: BlockFull[] = [blockGetStarted, mockNlpBlock];
|
||||
|
||||
export const nlpBlocks: BlockFull[] = [blockGetStarted, mockNlpBlock];
|
||||
|
Loading…
Reference in New Issue
Block a user