Merge pull request #844 from Hexastack/feat/change-getDefaultSettings-to-async

feat: change getDefaultSettings to be async
This commit is contained in:
Med Marrouchi 2025-03-24 12:10:44 +01:00 committed by GitHub
commit 816e4ee9e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 52 additions and 46 deletions

View File

@ -93,7 +93,7 @@ export class BlockController extends BaseController<
* @returns An array containing the settings of the specified plugin. * @returns An array containing the settings of the specified plugin.
*/ */
@Get('customBlocks/settings') @Get('customBlocks/settings')
findSettings(@Query('plugin') pluginName: PluginName) { async findSettings(@Query('plugin') pluginName: PluginName) {
try { try {
if (!pluginName) { if (!pluginName) {
throw new BadRequestException( throw new BadRequestException(
@ -110,7 +110,7 @@ export class BlockController extends BaseController<
throw new NotFoundException('Plugin Not Found'); throw new NotFoundException('Plugin Not Found');
} }
return plugin.getDefaultSettings(); return await plugin.getDefaultSettings();
} catch (e) { } catch (e) {
this.logger.error('Unable to fetch plugin settings', e); this.logger.error('Unable to fetch plugin settings', e);
throw e; throw e;
@ -123,29 +123,34 @@ export class BlockController extends BaseController<
* @returns An array containing available custom blocks. * @returns An array containing available custom blocks.
*/ */
@Get('customBlocks') @Get('customBlocks')
findAll() { async findAll() {
try { try {
const plugins = this.pluginsService const plugins = this.pluginsService
.getAllByType(PluginType.block) .getAllByType(PluginType.block)
.map((p) => ({ .map(async (p) => {
id: p.getName(), const defaultSettings = await p.getDefaultSettings();
namespace: p.getNamespace(),
template: { return {
...p.template, id: p.getName(),
message: { namespace: p.getNamespace(),
plugin: p.name, template: {
args: p.getDefaultSettings().reduce( ...p.template,
(acc, setting) => { message: {
acc[setting.label] = setting.value; plugin: p.name,
return acc; args: defaultSettings.reduce(
}, (acc, setting) => {
{} as { [key: string]: any }, acc[setting.label] = setting.value;
), return acc;
},
{} as { [key: string]: any },
),
},
}, },
}, effects:
effects: typeof p.effects === 'object' ? Object.keys(p.effects) : [], typeof p.effects === 'object' ? Object.keys(p.effects) : [],
})); };
return plugins; });
return await Promise.all(plugins);
} catch (e) { } catch (e) {
this.logger.error(e); this.logger.error(e);
throw e; throw e;

View File

@ -132,7 +132,7 @@ describe('TranslationService', () => {
expect(strings).toEqual(['Test message', 'Fallback message']); expect(strings).toEqual(['Test message', 'Fallback message']);
}); });
it('should return plugin-related strings from block message with translatable args', () => { it('should return plugin-related strings from block message with translatable args', async () => {
const block: Block = { const block: Block = {
name: 'Ollama Plugin', name: 'Ollama Plugin',
patterns: [], patterns: [],
@ -203,7 +203,7 @@ describe('TranslationService', () => {
return '/mock/path'; return '/mock/path';
} }
getDefaultSettings() { async getDefaultSettings() {
return this.settings; return this.settings;
} }
} }
@ -215,8 +215,7 @@ describe('TranslationService', () => {
.spyOn(pluginService, 'getPlugin') .spyOn(pluginService, 'getPlugin')
.mockImplementation(() => mockedPlugin); .mockImplementation(() => mockedPlugin);
const result = service.getBlockStrings(block); const result = await service.getBlockStrings(block);
expect(result).toEqual(['String 2', 'String 3']); expect(result).toEqual(['String 2', 'String 3']);
}); });
@ -225,7 +224,7 @@ describe('TranslationService', () => {
expect(strings).toEqual(['Global fallback message']); 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', async () => {
const block = { const block = {
id: 'blockId', id: 'blockId',
name: 'Test Block', name: 'Test Block',
@ -252,7 +251,7 @@ describe('TranslationService', () => {
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
} as Block; } as Block;
const strings = service.getBlockStrings(block); const strings = await service.getBlockStrings(block);
expect(strings).toEqual([ expect(strings).toEqual([
'Test message', 'Test message',
'Quick reply 1', 'Quick reply 1',
@ -261,7 +260,7 @@ describe('TranslationService', () => {
]); ]);
}); });
it('should return an array of strings from a block with a button message', () => { it('should return an array of strings from a block with a button message', async () => {
const block = { const block = {
id: 'blockId', id: 'blockId',
name: 'Test Block', name: 'Test Block',
@ -288,7 +287,7 @@ describe('TranslationService', () => {
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
} as Block; } as Block;
const strings = service.getBlockStrings(block); const strings = await service.getBlockStrings(block);
expect(strings).toEqual([ expect(strings).toEqual([
'Test message', 'Test message',
'Button 1', 'Button 1',
@ -297,7 +296,7 @@ describe('TranslationService', () => {
]); ]);
}); });
it('should return an array of strings from a block with a text message', () => { it('should return an array of strings from a block with a text message', async () => {
const block = { const block = {
id: 'blockId', id: 'blockId',
name: 'Test Block', name: 'Test Block',
@ -314,11 +313,11 @@ describe('TranslationService', () => {
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
} as Block; } as Block;
const strings = service.getBlockStrings(block); const strings = await service.getBlockStrings(block);
expect(strings).toEqual(['Test message', 'Fallback message']); expect(strings).toEqual(['Test message', 'Fallback message']);
}); });
it('should return an array of strings from a block with a nested message object', () => { it('should return an array of strings from a block with a nested message object', async () => {
const block = { const block = {
id: 'blockId', id: 'blockId',
name: 'Test Block', name: 'Test Block',
@ -337,11 +336,11 @@ describe('TranslationService', () => {
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
} as Block; } as Block;
const strings = service.getBlockStrings(block); const strings = await service.getBlockStrings(block);
expect(strings).toEqual(['Test message', 'Fallback message']); expect(strings).toEqual(['Test message', 'Fallback message']);
}); });
it('should handle different message formats in getBlockStrings', () => { it('should handle different message formats in getBlockStrings', async () => {
// Covers lines 54-60, 65 // Covers lines 54-60, 65
// Test with an array message (line 54-57) // Test with an array message (line 54-57)
@ -350,7 +349,7 @@ describe('TranslationService', () => {
message: ['This is a text message'], message: ['This is a text message'],
options: { fallback: { message: ['Fallback message'] } }, options: { fallback: { message: ['Fallback message'] } },
} as Block; } as Block;
const strings1 = service.getBlockStrings(block1); const strings1 = await service.getBlockStrings(block1);
expect(strings1).toEqual(['This is a text message', 'Fallback message']); expect(strings1).toEqual(['This is a text message', 'Fallback message']);
// Test with an object message (line 58-60) // Test with an object message (line 58-60)
@ -359,7 +358,7 @@ describe('TranslationService', () => {
message: { text: 'Another text message' }, message: { text: 'Another text message' },
options: { fallback: { message: ['Fallback message'] } }, options: { fallback: { message: ['Fallback message'] } },
} as Block; } as Block;
const strings2 = service.getBlockStrings(block2); const strings2 = await service.getBlockStrings(block2);
expect(strings2).toEqual(['Another text message', 'Fallback message']); expect(strings2).toEqual(['Another text message', 'Fallback message']);
// Test a block without a fallback (line 65) // Test a block without a fallback (line 65)
@ -368,7 +367,7 @@ describe('TranslationService', () => {
message: { text: 'Another test message' }, message: { text: 'Another test message' },
options: {}, options: {},
} as Block; } as Block;
const strings3 = service.getBlockStrings(block3); const strings3 = await service.getBlockStrings(block3);
expect(strings3).toEqual(['Another test message']); expect(strings3).toEqual(['Another test message']);
}); });
}); });

View File

@ -45,8 +45,9 @@ export class TranslationService extends BaseService<Translation> {
* *
* @returns An array of strings * @returns An array of strings
*/ */
getBlockStrings(block: Block): string[] { async getBlockStrings(block: Block): Promise<string[]> {
let strings: string[] = []; let strings: string[] = [];
if (Array.isArray(block.message)) { if (Array.isArray(block.message)) {
// Text Messages // Text Messages
strings = strings.concat(block.message); strings = strings.concat(block.message);
@ -56,12 +57,11 @@ export class TranslationService extends BaseService<Translation> {
PluginType.block, PluginType.block,
block.message.plugin, block.message.plugin,
); );
const defaultSettings = await plugin?.getDefaultSettings();
// plugin // plugin
Object.entries(block.message.args).forEach(([l, arg]) => { Object.entries(block.message.args).forEach(([l, arg]) => {
const setting = plugin const setting = defaultSettings?.find(({ label }) => label === l);
?.getDefaultSettings()
.find(({ label }) => label === l);
if (setting?.translatable) { if (setting?.translatable) {
if (Array.isArray(arg)) { if (Array.isArray(arg)) {
// array of text // array of text
@ -123,10 +123,12 @@ export class TranslationService extends BaseService<Translation> {
if (blocks.length === 0) { if (blocks.length === 0) {
return []; return [];
} }
return blocks.reduce((acc, block) => { const allStrings: string[] = [];
const strings = this.getBlockStrings(block); for (const block of blocks) {
return acc.concat(strings); const strings = await this.getBlockStrings(block);
}, [] as string[]); allStrings.push(...strings);
}
return allStrings;
} }
/** /**

View File

@ -38,7 +38,7 @@ export abstract class BaseBlockPlugin<
this.settings = require(path.join(this.getPath(), 'settings')).default; this.settings = require(path.join(this.getPath(), 'settings')).default;
} }
getDefaultSettings(): T { getDefaultSettings(): Promise<T> | T {
return this.settings; return this.settings;
} }