From 3051e84251debb619f9844d6c71138c2043385fc Mon Sep 17 00:00:00 2001 From: hexastack Date: Wed, 22 Jan 2025 18:11:45 +0100 Subject: [PATCH 1/2] feat: fix unit test --- api/src/chat/controllers/block.controller.ts | 6 +- .../i18n/services/translation.service.spec.ts | 74 +++++++++++++------ api/src/i18n/services/translation.service.ts | 6 +- api/src/plugins/base-block-plugin.ts | 26 ++++++- 4 files changed, 82 insertions(+), 30 deletions(-) diff --git a/api/src/chat/controllers/block.controller.ts b/api/src/chat/controllers/block.controller.ts index d6a87d67..65535702 100644 --- a/api/src/chat/controllers/block.controller.ts +++ b/api/src/chat/controllers/block.controller.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 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: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -110,7 +110,7 @@ export class BlockController extends BaseController< throw new NotFoundException('Plugin Not Found'); } - return plugin.settings; + return plugin.getDefaultSettings(); } catch (e) { this.logger.error('Unable to fetch plugin settings', e); throw e; @@ -134,7 +134,7 @@ export class BlockController extends BaseController< ...p.template, message: { plugin: p.name, - args: p.settings.reduce( + args: p.getDefaultSettings().reduce( (acc, setting) => { acc[setting.label] = setting.value; return acc; diff --git a/api/src/i18n/services/translation.service.spec.ts b/api/src/i18n/services/translation.service.spec.ts index 1667a1e8..508d544e 100644 --- a/api/src/i18n/services/translation.service.spec.ts +++ b/api/src/i18n/services/translation.service.spec.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 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: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -10,7 +10,9 @@ import { EventEmitter2 } from '@nestjs/event-emitter'; import { Test, TestingModule } from '@nestjs/testing'; import { I18nService } from '@/i18n/services/i18n.service'; +import { BasePlugin } from '@/plugins/base-plugin.service'; import { PluginService } from '@/plugins/plugins.service'; +import { PluginBlockTemplate } from '@/plugins/types'; import { SettingType } from '@/setting/schemas/types'; import { SettingService } from '@/setting/services/setting.service'; @@ -157,26 +159,56 @@ describe('TranslationService', () => { attachedBlock: null, }; - 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, - }, - ], - }; + class MockPlugin extends BasePlugin { + template: PluginBlockTemplate = { name: 'Ollama Plugin' }; + + name: `${string}-plugin`; + + type: any; + + private settings: { + label: string; + group: string; + type: SettingType; + value: any; + translatable: boolean; + }[]; + + constructor() { + super('ollama-plugin', pluginService); + this.name = 'ollama-plugin'; + this.type = 'block'; + this.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, + }, + ]; + } + + // Implementing the 'getPath' method (with a mock return value) + getPath() { + // Return a mock path + return '/mock/path'; + } + + getDefaultSettings() { + return this.settings; + } + } + + // Create an instance of the mock plugin + const mockedPlugin = new MockPlugin(); jest .spyOn(pluginService, 'getPlugin') diff --git a/api/src/i18n/services/translation.service.ts b/api/src/i18n/services/translation.service.ts index 117411c6..93bea9af 100644 --- a/api/src/i18n/services/translation.service.ts +++ b/api/src/i18n/services/translation.service.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 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: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -59,7 +59,9 @@ export class TranslationService extends BaseService { // plugin Object.entries(block.message.args).forEach(([l, arg]) => { - const setting = plugin?.settings.find(({ label }) => label === l); + const setting = plugin + ?.getDefaultSettings() + .find(({ label }) => label === l); if (setting?.translatable) { if (Array.isArray(arg)) { // array of text diff --git a/api/src/plugins/base-block-plugin.ts b/api/src/plugins/base-block-plugin.ts index 1a12ebf9..0755cab3 100644 --- a/api/src/plugins/base-block-plugin.ts +++ b/api/src/plugins/base-block-plugin.ts @@ -1,5 +1,5 @@ /* - * Copyright © 2024 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: * 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. @@ -30,7 +30,7 @@ export abstract class BaseBlockPlugin< > extends BasePlugin { public readonly type: PluginType = PluginType.block; - public readonly settings: T; + private readonly settings: T; constructor(name: PluginName, pluginService: PluginService) { super(name, pluginService); @@ -38,6 +38,10 @@ export abstract class BaseBlockPlugin< this.settings = require(path.join(this.getPath(), 'settings')).default; } + getDefaultSettings(): T { + return this.settings; + } + abstract template: PluginBlockTemplate; effects?: PluginEffects; @@ -50,8 +54,22 @@ export abstract class BaseBlockPlugin< protected getArguments(block: Block) { if ('args' in block.message) { - return block.message.args as SettingObject; + return ( + Object.entries(block.message.args) + // Filter out old settings + .filter( + ([argKey]) => + this.settings.findIndex(({ label }) => label === argKey) !== -1, + ) + .reduce( + (acc, [k, v]) => ({ + ...acc, + [k]: v, + }), + {} as SettingObject, + ) + ); } - throw new Error(`Block "${block.name}" does not have any arguments.`); + throw new Error(`Block ${block.name} does not have any arguments.`); } } From dee2eb5c81151309a84980848d8811a2050970e3 Mon Sep 17 00:00:00 2001 From: Mohamed Marrouchi Date: Wed, 22 Jan 2025 18:32:36 +0100 Subject: [PATCH 2/2] build: v2.2.1 --- api/package-lock.json | 4 ++-- api/package.json | 2 +- frontend/package.json | 2 +- package-lock.json | 8 ++++---- package.json | 2 +- widget/package.json | 2 +- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/api/package-lock.json b/api/package-lock.json index edff421b..0d0d56ef 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -1,12 +1,12 @@ { "name": "hexabot", - "version": "2.2.0", + "version": "2.2.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hexabot", - "version": "2.2.0", + "version": "2.2.1", "hasInstallScript": true, "license": "AGPL-3.0-only", "dependencies": { diff --git a/api/package.json b/api/package.json index 227d3800..48fec971 100644 --- a/api/package.json +++ b/api/package.json @@ -1,6 +1,6 @@ { "name": "hexabot", - "version": "2.2.0", + "version": "2.2.1", "description": "Hexabot is a solution for creating and managing chatbots across multiple channels, leveraging AI for advanced conversational capabilities. It provides a user-friendly interface for building, training, and deploying chatbots with integrated support for various messaging platforms.", "author": "Hexastack", "license": "AGPL-3.0-only", diff --git a/frontend/package.json b/frontend/package.json index beb2aafa..3827e2ee 100755 --- a/frontend/package.json +++ b/frontend/package.json @@ -1,7 +1,7 @@ { "name": "hexabot-ui", "private": true, - "version": "2.2.0", + "version": "2.2.1", "description": "Hexabot is a solution for creating and managing chatbots across multiple channels, leveraging AI for advanced conversational capabilities. It provides a user-friendly interface for building, training, and deploying chatbots with integrated support for various messaging platforms.", "author": "Hexastack", "license": "AGPL-3.0-only", diff --git a/package-lock.json b/package-lock.json index 6fb4641e..a0bd0763 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hexabot", - "version": "2.1.9", + "version": "2.2.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hexabot", - "version": "2.1.9", + "version": "2.2.0", "license": "AGPL-3.0-only", "workspaces": [ "frontend", @@ -45,7 +45,7 @@ }, "frontend": { "name": "hexabot-ui", - "version": "2.2.0", + "version": "2.2.1", "license": "AGPL-3.0-only", "dependencies": { "@chatscope/chat-ui-kit-react": "^2.0.3", @@ -10304,7 +10304,7 @@ }, "widget": { "name": "hexabot-chat-widget", - "version": "2.2.0", + "version": "2.2.1", "license": "AGPL-3.0-only", "dependencies": { "@types/emoji-js": "^3.5.2", diff --git a/package.json b/package.json index 7e2aea6f..ac3f9cfa 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "frontend", "widget" ], - "version": "2.2.0", + "version": "2.2.1", "description": "Hexabot is a solution for creating and managing chatbots across multiple channels, leveraging AI for advanced conversational capabilities. It provides a user-friendly interface for building, training, and deploying chatbots with integrated support for various messaging platforms.", "author": "Hexastack", "license": "AGPL-3.0-only", diff --git a/widget/package.json b/widget/package.json index 01516fa9..f27ba896 100644 --- a/widget/package.json +++ b/widget/package.json @@ -1,6 +1,6 @@ { "name": "hexabot-chat-widget", - "version": "2.2.0", + "version": "2.2.1", "description": "Hexabot is a solution for creating and managing chatbots across multiple channels, leveraging AI for advanced conversational capabilities. It provides a user-friendly interface for building, training, and deploying chatbots with integrated support for various messaging platforms.", "author": "Hexastack", "license": "AGPL-3.0-only",