Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update block plugin settings visibility #618

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/src/chat/controllers/block.controller.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
74 changes: 53 additions & 21 deletions api/src/i18n/services/translation.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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';

Expand Down Expand Up @@ -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')
Expand Down
6 changes: 4 additions & 2 deletions api/src/i18n/services/translation.service.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -59,7 +59,9 @@ export class TranslationService extends BaseService<Translation> {

// 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
Expand Down
26 changes: 22 additions & 4 deletions api/src/plugins/base-block-plugin.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -30,14 +30,18 @@ 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<BasePlugin>) {
super(name, pluginService);
// eslint-disable-next-line @typescript-eslint/no-var-requires
this.settings = require(path.join(this.getPath(), 'settings')).default;
}

getDefaultSettings(): T {
return this.settings;
}

abstract template: PluginBlockTemplate;

effects?: PluginEffects;
Expand All @@ -50,8 +54,22 @@ export abstract class BaseBlockPlugin<

protected getArguments(block: Block) {
if ('args' in block.message) {
return block.message.args as SettingObject<T>;
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<T>,
)
);
}
throw new Error(`Block "${block.name}" does not have any arguments.`);
throw new Error(`Block ${block.name} does not have any arguments.`);
}
}
Loading