Skip to content

Commit

Permalink
chore: correct engine reload on config save (#797)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo authored Jun 28, 2024
1 parent e7bc983 commit e020982
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 19 deletions.
24 changes: 14 additions & 10 deletions cortex-js/src/extensions/extensions.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@ import MistralEngineExtension from './mistral.engine';
import OpenAIEngineExtension from './openai.engine';
import { HttpModule, HttpService } from '@nestjs/axios';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { FileManagerModule } from '@/infrastructure/services/file-manager/file-manager.module';
import { ConfigsModule } from '@/usecases/configs/configs.module';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';

const provider = {
provide: 'EXTENSIONS_PROVIDER',
inject: [HttpService, ConfigsUsecases],
useFactory: (httpService: HttpService, configUsecases: ConfigsUsecases) => [
new OpenAIEngineExtension(httpService, configUsecases),
new GroqEngineExtension(httpService, configUsecases),
new MistralEngineExtension(httpService, configUsecases),
inject: [HttpService, ConfigsUsecases, EventEmitter2],
useFactory: (
httpService: HttpService,
configUsecases: ConfigsUsecases,
eventEmitter: EventEmitter2,
) => [
new OpenAIEngineExtension(httpService, configUsecases, eventEmitter),
new GroqEngineExtension(httpService, configUsecases, eventEmitter),
new MistralEngineExtension(httpService, configUsecases, eventEmitter),
],
};

@Module({
// Do not import ConfigsModule here to avoid circular dependency
imports: [HttpModule, FileManagerModule],
imports: [HttpModule, ConfigsModule],
controllers: [],
providers: [ConfigsUsecases, provider],
exports: [ConfigsUsecases, provider],
providers: [provider],
exports: [provider],
})
export class ExtensionsModule {}
10 changes: 9 additions & 1 deletion cortex-js/src/extensions/groq.engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { EventEmitter2 } from '@nestjs/event-emitter';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
Expand All @@ -18,16 +19,23 @@ export default class GroqEngineExtension extends OAIEngineExtension {
constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
protected readonly eventEmmitter: EventEmitter2,
) {
super(httpService);

eventEmmitter.on('config.updated', async (data) => {
if (data.group === this.name) {
this.apiKey = data.value;
}
});
}

async onLoad() {
const configs = (await this.configsUsecases.getGroupConfigs(
this.name,
)) as unknown as { apiKey: string };

this.apiKey = configs?.apiKey
this.apiKey = configs?.apiKey;
if (!configs?.apiKey)
await this.configsUsecases.saveConfig('apiKey', '', this.name);
}
Expand Down
8 changes: 8 additions & 0 deletions cortex-js/src/extensions/mistral.engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { EventEmitter2 } from '@nestjs/event-emitter';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
Expand All @@ -18,8 +19,15 @@ export default class MistralEngineExtension extends OAIEngineExtension {
constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
protected readonly eventEmmitter: EventEmitter2,
) {
super(httpService);

eventEmmitter.on('config.updated', async (data) => {
if (data.group === this.name) {
this.apiKey = data.value;
}
});
}

async onLoad() {
Expand Down
8 changes: 8 additions & 0 deletions cortex-js/src/extensions/openai.engine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { HttpService } from '@nestjs/axios';
import { OAIEngineExtension } from '../domain/abstracts/oai.abstract';
import { ConfigsUsecases } from '@/usecases/configs/configs.usecase';
import { EventEmitter2 } from '@nestjs/event-emitter';

/**
* A class that implements the InferenceExtension interface from the @janhq/core package.
Expand All @@ -18,8 +19,15 @@ export default class OpenAIEngineExtension extends OAIEngineExtension {
constructor(
protected readonly httpService: HttpService,
protected readonly configsUsecases: ConfigsUsecases,
protected readonly eventEmmitter: EventEmitter2,
) {
super(httpService);

eventEmmitter.on('config.updated', async (data) => {
if (data.group === this.name) {
this.apiKey = data.value;
}
});
}

async onLoad() {
Expand Down
3 changes: 1 addition & 2 deletions cortex-js/src/usecases/configs/configs.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Module } from '@nestjs/common';
import { FileManagerModule } from '@/infrastructure/services/file-manager/file-manager.module';
import { ConfigsUsecases } from './configs.usecase';
import { ExtensionModule } from '@/infrastructure/repositories/extensions/extension.module';

@Module({
imports: [FileManagerModule, ExtensionModule],
imports: [FileManagerModule],
controllers: [],
providers: [ConfigsUsecases],
exports: [ConfigsUsecases],
Expand Down
27 changes: 21 additions & 6 deletions cortex-js/src/usecases/configs/configs.usecase.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { CommonResponseDto } from '@/infrastructure/dtos/common/common-response.dto';
import { FileManagerService } from '@/infrastructure/services/file-manager/file-manager.service';
import { Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';

@Injectable()
export class ConfigsUsecases {
constructor(private readonly fileManagerService: FileManagerService) {}
constructor(
private readonly fileManagerService: FileManagerService,
private readonly eventEmitter: EventEmitter2,
) {}

/**
* Save a configuration to the .cortexrc file.
Expand Down Expand Up @@ -33,11 +37,22 @@ export class ConfigsUsecases {
: {}),
};

return this.fileManagerService.writeConfigFile(newConfigs).then(() => {
return {
message: 'The config has been successfully updated.',
};
});
return this.fileManagerService
.writeConfigFile(newConfigs)
.then(async () => {
if (group) {
this.eventEmitter.emit('config.updated', {
group,
key,
value,
});
}
})
.then(() => {
return {
message: 'The config has been successfully updated.',
};
});
}

/**
Expand Down

0 comments on commit e020982

Please sign in to comment.