|
| 1 | +import { HttpService } from '@nestjs/axios'; |
| 2 | +import { OAIEngineExtension } from '../domain/abstracts/oai.abstract'; |
| 3 | +import { ConfigsUsecases } from '@/usecases/configs/configs.usecase'; |
| 4 | +import { EventEmitter2 } from '@nestjs/event-emitter'; |
| 5 | +import { EngineStatus } from '@/domain/abstracts/engine.abstract'; |
| 6 | + |
| 7 | +/** |
| 8 | + * A class that implements the InferenceExtension interface from the @janhq/core package. |
| 9 | + * The class provides methods for initializing and stopping a model, and for making inference requests. |
| 10 | + * It also subscribes to events emitted by the @janhq/core package and handles new message requests. |
| 11 | + */ |
| 12 | +export default class MartianEngineExtension extends OAIEngineExtension { |
| 13 | + apiUrl = 'https://withmartian.com/api/openai/v1/chat/completions'; |
| 14 | + name = 'martian'; |
| 15 | + productName = 'Martian Inference Engine'; |
| 16 | + description = 'This extension enables Martian chat completion API calls'; |
| 17 | + version = '0.0.1'; |
| 18 | + apiKey?: string; |
| 19 | + |
| 20 | + constructor( |
| 21 | + protected readonly httpService: HttpService, |
| 22 | + protected readonly configsUsecases: ConfigsUsecases, |
| 23 | + protected readonly eventEmmitter: EventEmitter2, |
| 24 | + ) { |
| 25 | + super(httpService); |
| 26 | + |
| 27 | + eventEmmitter.on('config.updated', async (data) => { |
| 28 | + if (data.engine === this.name) { |
| 29 | + this.apiKey = data.value; |
| 30 | + this.status = |
| 31 | + (this.apiKey?.length ?? 0) > 0 |
| 32 | + ? EngineStatus.READY |
| 33 | + : EngineStatus.MISSING_CONFIGURATION; |
| 34 | + } |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + async onLoad() { |
| 39 | + const configs = (await this.configsUsecases.getGroupConfigs( |
| 40 | + this.name, |
| 41 | + )) as unknown as { apiKey: string }; |
| 42 | + |
| 43 | + this.apiKey = configs?.apiKey; |
| 44 | + this.status = |
| 45 | + (this.apiKey?.length ?? 0) > 0 |
| 46 | + ? EngineStatus.READY |
| 47 | + : EngineStatus.MISSING_CONFIGURATION; |
| 48 | + } |
| 49 | +} |
0 commit comments