diff --git a/core/src/types/config/appConfigEvent.ts b/core/src/types/config/appConfigEvent.ts new file mode 100644 index 0000000000..50e33cfa2e --- /dev/null +++ b/core/src/types/config/appConfigEvent.ts @@ -0,0 +1,6 @@ +/** + * App configuration event name + */ +export enum AppConfigurationEventName { + OnConfigurationUpdate = 'OnConfigurationUpdate', +} diff --git a/core/src/types/config/index.ts b/core/src/types/config/index.ts index 0fa3645aa2..d2e182b99d 100644 --- a/core/src/types/config/index.ts +++ b/core/src/types/config/index.ts @@ -1 +1,2 @@ export * from './appConfigEntity' +export * from './appConfigEvent' diff --git a/extensions/inference-openai-extension/src/index.ts b/extensions/inference-openai-extension/src/index.ts index 54572041d7..9abfc2c7d8 100644 --- a/extensions/inference-openai-extension/src/index.ts +++ b/extensions/inference-openai-extension/src/index.ts @@ -19,6 +19,8 @@ import { MessageEvent, ModelEvent, InferenceEvent, + AppConfigurationEventName, + joinPath, } from "@janhq/core"; import { requestInference } from "./helpers/sse"; import { ulid } from "ulid"; @@ -30,7 +32,7 @@ import { join } from "path"; * It also subscribes to events emitted by the @janhq/core package and handles new message requests. */ export default class JanInferenceOpenAIExtension extends BaseExtension { - private static readonly _homeDir = "file://engines"; + private static readonly _engineDir = "file://engines"; private static readonly _engineMetadataFileName = "openai.json"; private static _currentModel: OpenAIModel; @@ -47,9 +49,9 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { * Subscribes to events emitted by the @janhq/core package. */ async onLoad() { - if (!(await fs.existsSync(JanInferenceOpenAIExtension._homeDir))) { + if (!(await fs.existsSync(JanInferenceOpenAIExtension._engineDir))) { await fs - .mkdirSync(JanInferenceOpenAIExtension._homeDir) + .mkdirSync(JanInferenceOpenAIExtension._engineDir) .catch((err) => console.debug(err)); } @@ -57,7 +59,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { // Events subscription events.on(MessageEvent.OnMessageSent, (data) => - JanInferenceOpenAIExtension.handleMessageRequest(data, this) + JanInferenceOpenAIExtension.handleMessageRequest(data, this), ); events.on(ModelEvent.OnModelInit, (model: OpenAIModel) => { @@ -70,6 +72,20 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { events.on(InferenceEvent.OnInferenceStopped, () => { JanInferenceOpenAIExtension.handleInferenceStopped(this); }); + + const settingsFilePath = await joinPath([ + JanInferenceOpenAIExtension._engineDir, + JanInferenceOpenAIExtension._engineMetadataFileName, + ]); + + events.on( + AppConfigurationEventName.OnConfigurationUpdate, + (settingsKey: string) => { + // Update settings on changes + if (settingsKey === settingsFilePath) + JanInferenceOpenAIExtension.writeDefaultEngineSettings(); + }, + ); } /** @@ -80,8 +96,8 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { static async writeDefaultEngineSettings() { try { const engineFile = join( - JanInferenceOpenAIExtension._homeDir, - JanInferenceOpenAIExtension._engineMetadataFileName + JanInferenceOpenAIExtension._engineDir, + JanInferenceOpenAIExtension._engineMetadataFileName, ); if (await fs.existsSync(engineFile)) { const engine = await fs.readFileSync(engineFile, "utf-8"); @@ -90,7 +106,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { } else { await fs.writeFileSync( engineFile, - JSON.stringify(JanInferenceOpenAIExtension._engineSettings, null, 2) + JSON.stringify(JanInferenceOpenAIExtension._engineSettings, null, 2), ); } } catch (err) { @@ -116,7 +132,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension { } private static async handleInferenceStopped( - instance: JanInferenceOpenAIExtension + instance: JanInferenceOpenAIExtension, ) { instance.isCancelled = true; instance.controller?.abort(); diff --git a/web/hooks/useEngineSettings.ts b/web/hooks/useEngineSettings.ts index 258a89aa48..49e6607b3a 100644 --- a/web/hooks/useEngineSettings.ts +++ b/web/hooks/useEngineSettings.ts @@ -1,4 +1,4 @@ -import { fs, joinPath } from '@janhq/core' +import { fs, joinPath, events, AppConfigurationEventName } from '@janhq/core' export const useEngineSettings = () => { const readOpenAISettings = async () => { @@ -21,10 +21,16 @@ export const useEngineSettings = () => { apiKey: string | undefined }) => { const settings = await readOpenAISettings() + const settingFilePath = await joinPath(['file://engines', 'openai.json']) + settings.api_key = apiKey - await fs.writeFileSync( - await joinPath(['file://engines', 'openai.json']), - JSON.stringify(settings) + + await fs.writeFileSync(settingFilePath, JSON.stringify(settings)) + + // Sec: Don't attach the settings data to the event + events.emit( + AppConfigurationEventName.OnConfigurationUpdate, + settingFilePath ) } return { readOpenAISettings, saveOpenAISettings }