mirror of
https://github.com/hexastack/hexabot
synced 2024-11-24 04:53:41 +00:00
Merge pull request #360 from Hexastack/fix/api-translation-refresh
fix: display translatable settings only in translations UI #109
This commit is contained in:
commit
485744e1e5
@ -7,6 +7,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
||||||
|
import { PluginName } from '@/plugins/types';
|
||||||
|
|
||||||
import { Message } from '../message.schema';
|
import { Message } from '../message.schema';
|
||||||
|
|
||||||
@ -107,7 +108,7 @@ export type StdOutgoingAttachmentMessage<
|
|||||||
};
|
};
|
||||||
|
|
||||||
export type StdPluginMessage = {
|
export type StdPluginMessage = {
|
||||||
plugin: string;
|
plugin: PluginName;
|
||||||
args: { [key: string]: any };
|
args: { [key: string]: any };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -45,6 +45,7 @@ export default [
|
|||||||
label: Web.SettingLabel.greeting_message,
|
label: Web.SettingLabel.greeting_message,
|
||||||
value: 'Welcome! Ready to start a conversation with our chatbot?',
|
value: 'Welcome! Ready to start a conversation with our chatbot?',
|
||||||
type: SettingType.textarea,
|
type: SettingType.textarea,
|
||||||
|
translatable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
group: WEB_CHANNEL_NAMESPACE,
|
group: WEB_CHANNEL_NAMESPACE,
|
||||||
@ -58,6 +59,7 @@ export default [
|
|||||||
label: Web.SettingLabel.window_title,
|
label: Web.SettingLabel.window_title,
|
||||||
value: 'Widget Title',
|
value: 'Widget Title',
|
||||||
type: SettingType.text,
|
type: SettingType.text,
|
||||||
|
translatable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
group: WEB_CHANNEL_NAMESPACE,
|
group: WEB_CHANNEL_NAMESPACE,
|
||||||
|
@ -10,6 +10,8 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
|
||||||
import { I18nService } from '@/i18n/services/i18n.service';
|
import { I18nService } from '@/i18n/services/i18n.service';
|
||||||
|
import { PluginService } from '@/plugins/plugins.service';
|
||||||
|
import { SettingType } from '@/setting/schemas/types';
|
||||||
import { SettingService } from '@/setting/services/setting.service';
|
import { SettingService } from '@/setting/services/setting.service';
|
||||||
|
|
||||||
import { Block } from '../../chat/schemas/block.schema';
|
import { Block } from '../../chat/schemas/block.schema';
|
||||||
@ -20,8 +22,8 @@ import { TranslationService } from '../services/translation.service';
|
|||||||
|
|
||||||
describe('TranslationService', () => {
|
describe('TranslationService', () => {
|
||||||
let service: TranslationService;
|
let service: TranslationService;
|
||||||
let settingService: SettingService;
|
|
||||||
let i18nService: I18nService;
|
let i18nService: I18nService;
|
||||||
|
let pluginService: PluginService;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
@ -39,6 +41,12 @@ describe('TranslationService', () => {
|
|||||||
]),
|
]),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
provide: PluginService,
|
||||||
|
useValue: {
|
||||||
|
getPlugin: jest.fn(),
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
provide: BlockService,
|
provide: BlockService,
|
||||||
useValue: {
|
useValue: {
|
||||||
@ -64,6 +72,30 @@ describe('TranslationService', () => {
|
|||||||
fallback_message: ['Global fallback message'],
|
fallback_message: ['Global fallback message'],
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
|
find: jest
|
||||||
|
.fn()
|
||||||
|
.mockImplementation((criteria: { translatable?: boolean }) =>
|
||||||
|
[
|
||||||
|
{
|
||||||
|
translatable: false,
|
||||||
|
group: 'default',
|
||||||
|
value: 'Global fallback',
|
||||||
|
label: 'global_fallback',
|
||||||
|
type: SettingType.checkbox,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
translatable: true,
|
||||||
|
group: 'default',
|
||||||
|
value: 'Global fallback message',
|
||||||
|
label: 'fallback_message',
|
||||||
|
type: SettingType.text,
|
||||||
|
},
|
||||||
|
].filter((s) =>
|
||||||
|
criteria && 'translatable' in criteria
|
||||||
|
? s.translatable === criteria.translatable
|
||||||
|
: true,
|
||||||
|
),
|
||||||
|
),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -77,8 +109,8 @@ describe('TranslationService', () => {
|
|||||||
}).compile();
|
}).compile();
|
||||||
|
|
||||||
service = module.get<TranslationService>(TranslationService);
|
service = module.get<TranslationService>(TranslationService);
|
||||||
settingService = module.get<SettingService>(SettingService);
|
|
||||||
i18nService = module.get<I18nService>(I18nService);
|
i18nService = module.get<I18nService>(I18nService);
|
||||||
|
pluginService = module.get<PluginService>(PluginService);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should call refreshDynamicTranslations with translations from findAll', async () => {
|
it('should call refreshDynamicTranslations with translations from findAll', async () => {
|
||||||
@ -98,21 +130,65 @@ describe('TranslationService', () => {
|
|||||||
expect(strings).toEqual(['Test message', 'Fallback message']);
|
expect(strings).toEqual(['Test message', 'Fallback message']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an array of strings from the settings when global fallback is enabled', async () => {
|
it('should return plugin-related strings from block message with translatable args', () => {
|
||||||
const strings = await service.getSettingStrings();
|
const block: Block = {
|
||||||
expect(strings).toEqual(['Global fallback message']);
|
name: 'Ollama Plugin',
|
||||||
|
patterns: [],
|
||||||
|
assign_labels: [],
|
||||||
|
trigger_channels: [],
|
||||||
|
trigger_labels: [],
|
||||||
|
nextBlocks: [],
|
||||||
|
category: '673f82f4bafd1e2a00e7e53e',
|
||||||
|
starts_conversation: false,
|
||||||
|
builtin: false,
|
||||||
|
capture_vars: [],
|
||||||
|
createdAt: new Date(),
|
||||||
|
updatedAt: new Date(),
|
||||||
|
id: '673f8724007f1087c96d30d0',
|
||||||
|
position: { x: 702, y: 321.8333282470703 },
|
||||||
|
message: {
|
||||||
|
plugin: 'ollama-plugin',
|
||||||
|
args: {
|
||||||
|
model: 'String 1',
|
||||||
|
context: ['String 2', 'String 3'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
options: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
const mockedPlugin: any = {
|
||||||
|
name: 'ollama-plugin',
|
||||||
|
type: 'block',
|
||||||
|
settings: [
|
||||||
|
{
|
||||||
|
label: 'model',
|
||||||
|
group: 'default',
|
||||||
|
type: SettingType.text,
|
||||||
|
value: 'llama3.2',
|
||||||
|
translatable: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'context',
|
||||||
|
group: 'default',
|
||||||
|
type: SettingType.multiple_text,
|
||||||
|
value: ['Answer the user QUESTION using the DOCUMENTS text above.'],
|
||||||
|
translatable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
jest
|
||||||
|
.spyOn(pluginService, 'getPlugin')
|
||||||
|
.mockImplementation(() => mockedPlugin);
|
||||||
|
|
||||||
|
const result = service.getBlockStrings(block);
|
||||||
|
|
||||||
|
expect(result).toEqual(['String 2', 'String 3']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an empty array from the settings when global fallback is disabled', async () => {
|
it('should return the settings translation strings', async () => {
|
||||||
jest.spyOn(settingService, 'getSettings').mockResolvedValueOnce({
|
|
||||||
chatbot_settings: {
|
|
||||||
global_fallback: false,
|
|
||||||
fallback_message: ['Global fallback message'],
|
|
||||||
},
|
|
||||||
} as Settings);
|
|
||||||
|
|
||||||
const strings = await service.getSettingStrings();
|
const strings = await service.getSettingStrings();
|
||||||
expect(strings).toEqual([]);
|
expect(strings).toEqual(['Global fallback message']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an array of strings from a block with a quick reply message', () => {
|
it('should return an array of strings from a block with a quick reply message', () => {
|
||||||
|
@ -10,6 +10,8 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { OnEvent } from '@nestjs/event-emitter';
|
import { OnEvent } from '@nestjs/event-emitter';
|
||||||
|
|
||||||
import { I18nService } from '@/i18n/services/i18n.service';
|
import { I18nService } from '@/i18n/services/i18n.service';
|
||||||
|
import { PluginService } from '@/plugins/plugins.service';
|
||||||
|
import { PluginType } from '@/plugins/types';
|
||||||
import { SettingService } from '@/setting/services/setting.service';
|
import { SettingService } from '@/setting/services/setting.service';
|
||||||
import { BaseService } from '@/utils/generics/base-service';
|
import { BaseService } from '@/utils/generics/base-service';
|
||||||
|
|
||||||
@ -24,6 +26,7 @@ export class TranslationService extends BaseService<Translation> {
|
|||||||
readonly repository: TranslationRepository,
|
readonly repository: TranslationRepository,
|
||||||
private readonly blockService: BlockService,
|
private readonly blockService: BlockService,
|
||||||
private readonly settingService: SettingService,
|
private readonly settingService: SettingService,
|
||||||
|
private readonly pluginService: PluginService,
|
||||||
private readonly i18n: I18nService,
|
private readonly i18n: I18nService,
|
||||||
) {
|
) {
|
||||||
super(repository);
|
super(repository);
|
||||||
@ -49,8 +52,15 @@ export class TranslationService extends BaseService<Translation> {
|
|||||||
strings = strings.concat(block.message);
|
strings = strings.concat(block.message);
|
||||||
} else if (typeof block.message === 'object') {
|
} else if (typeof block.message === 'object') {
|
||||||
if ('plugin' in block.message) {
|
if ('plugin' in block.message) {
|
||||||
|
const plugin = this.pluginService.getPlugin(
|
||||||
|
PluginType.block,
|
||||||
|
block.message.plugin,
|
||||||
|
);
|
||||||
|
|
||||||
// plugin
|
// plugin
|
||||||
Object.values(block.message.args).forEach((arg) => {
|
Object.entries(block.message.args).forEach(([l, arg]) => {
|
||||||
|
const setting = plugin.settings.find(({ label }) => label === l);
|
||||||
|
if (setting?.translatable) {
|
||||||
if (Array.isArray(arg)) {
|
if (Array.isArray(arg)) {
|
||||||
// array of text
|
// array of text
|
||||||
strings = strings.concat(arg);
|
strings = strings.concat(arg);
|
||||||
@ -58,6 +68,7 @@ export class TranslationService extends BaseService<Translation> {
|
|||||||
// text
|
// text
|
||||||
strings.push(arg);
|
strings.push(arg);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else if ('text' in block.message && Array.isArray(block.message.text)) {
|
} else if ('text' in block.message && Array.isArray(block.message.text)) {
|
||||||
// array of text
|
// array of text
|
||||||
@ -121,12 +132,18 @@ export class TranslationService extends BaseService<Translation> {
|
|||||||
* @returns A promise of all strings available in a array
|
* @returns A promise of all strings available in a array
|
||||||
*/
|
*/
|
||||||
async getSettingStrings(): Promise<string[]> {
|
async getSettingStrings(): Promise<string[]> {
|
||||||
let strings: string[] = [];
|
const translatableSettings = await this.settingService.find({
|
||||||
|
translatable: true,
|
||||||
|
});
|
||||||
const settings = await this.settingService.getSettings();
|
const settings = await this.settingService.getSettings();
|
||||||
if (settings.chatbot_settings.global_fallback) {
|
return Object.values(settings)
|
||||||
strings = strings.concat(settings.chatbot_settings.fallback_message);
|
.map((group: Record<string, string | string[]>) => Object.entries(group))
|
||||||
}
|
.flat()
|
||||||
return strings;
|
.filter(([l]) => {
|
||||||
|
return translatableSettings.find(({ label }) => label === l);
|
||||||
|
})
|
||||||
|
.map(([, v]) => v)
|
||||||
|
.flat();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -82,7 +82,13 @@ describe('SettingController', () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
expect(settingService.find).toHaveBeenCalled();
|
expect(settingService.find).toHaveBeenCalled();
|
||||||
expect(result).toEqualPayload(settingFixtures);
|
expect(result).toEqualPayload(settingFixtures, [
|
||||||
|
'id',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
'subgroup',
|
||||||
|
'translatable',
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -97,12 +103,15 @@ describe('SettingController', () => {
|
|||||||
const result = await settingController.updateOne(id, payload);
|
const result = await settingController.updateOne(id, payload);
|
||||||
|
|
||||||
expect(settingService.updateOne).toHaveBeenCalledWith(id, payload);
|
expect(settingService.updateOne).toHaveBeenCalledWith(id, payload);
|
||||||
expect(result).toEqualPayload({
|
expect(result).toEqualPayload(
|
||||||
|
{
|
||||||
...settingFixtures.find(
|
...settingFixtures.find(
|
||||||
(settingFixture) => settingFixture.value === 'admin@example.com',
|
(settingFixture) => settingFixture.value === 'admin@example.com',
|
||||||
),
|
),
|
||||||
value: payload.value,
|
value: payload.value,
|
||||||
});
|
},
|
||||||
|
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
|
||||||
import {
|
import {
|
||||||
IsArray,
|
IsArray,
|
||||||
|
IsBoolean,
|
||||||
IsIn,
|
IsIn,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
IsOptional,
|
IsOptional,
|
||||||
@ -65,8 +66,20 @@ export class SettingCreateDto {
|
|||||||
//TODO: adding swagger decorators
|
//TODO: adding swagger decorators
|
||||||
config?: Record<string, any>;
|
config?: Record<string, any>;
|
||||||
|
|
||||||
//TODO: adding swagger decorators
|
@ApiPropertyOptional({
|
||||||
|
description:
|
||||||
|
'Defines the display order of the setting in the user interface',
|
||||||
|
type: Number,
|
||||||
|
})
|
||||||
weight: number;
|
weight: number;
|
||||||
|
|
||||||
|
@ApiPropertyOptional({
|
||||||
|
description: 'Indicates whether this setting supports translation',
|
||||||
|
type: Boolean,
|
||||||
|
})
|
||||||
|
@IsBoolean()
|
||||||
|
@IsOptional()
|
||||||
|
translatable?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SettingUpdateDto {
|
export class SettingUpdateDto {
|
||||||
|
@ -58,6 +58,12 @@ export class Setting extends BaseSchema {
|
|||||||
default: 0,
|
default: 0,
|
||||||
})
|
})
|
||||||
weight?: number;
|
weight?: number;
|
||||||
|
|
||||||
|
@Prop({
|
||||||
|
type: Boolean,
|
||||||
|
default: false,
|
||||||
|
})
|
||||||
|
translatable?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SettingModel: ModelDefinition = LifecycleHookManager.attach({
|
export const SettingModel: ModelDefinition = LifecycleHookManager.attach({
|
||||||
|
@ -69,6 +69,7 @@ export const DEFAULT_SETTINGS = [
|
|||||||
] as string[],
|
] as string[],
|
||||||
type: SettingType.multiple_text,
|
type: SettingType.multiple_text,
|
||||||
weight: 5,
|
weight: 5,
|
||||||
|
translatable: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
group: 'contact',
|
group: 'contact',
|
||||||
|
@ -84,6 +84,7 @@ describe('SettingService', () => {
|
|||||||
expect(settingRepository.findAll).toHaveBeenCalled();
|
expect(settingRepository.findAll).toHaveBeenCalled();
|
||||||
expect(result).toEqualPayload(
|
expect(result).toEqualPayload(
|
||||||
settingService.group(settingFixtures as Setting[]),
|
settingService.group(settingFixtures as Setting[]),
|
||||||
|
['id', 'createdAt', 'updatedAt', 'subgroup', 'translatable'],
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user