Skip to content

Commit

Permalink
fix: api settings are not applied on changes (janhq#1789)
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan authored Jan 25, 2024
1 parent f4bf8ba commit 7ed523e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
6 changes: 6 additions & 0 deletions core/src/types/config/appConfigEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* App configuration event name
*/
export enum AppConfigurationEventName {
OnConfigurationUpdate = 'OnConfigurationUpdate',
}
1 change: 1 addition & 0 deletions core/src/types/config/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './appConfigEntity'
export * from './appConfigEvent'
32 changes: 24 additions & 8 deletions extensions/inference-openai-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
MessageEvent,
ModelEvent,
InferenceEvent,
AppConfigurationEventName,
joinPath,
} from "@janhq/core";
import { requestInference } from "./helpers/sse";
import { ulid } from "ulid";
Expand All @@ -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;
Expand All @@ -47,17 +49,17 @@ 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));
}

JanInferenceOpenAIExtension.writeDefaultEngineSettings();

// Events subscription
events.on(MessageEvent.OnMessageSent, (data) =>
JanInferenceOpenAIExtension.handleMessageRequest(data, this)
JanInferenceOpenAIExtension.handleMessageRequest(data, this),
);

events.on(ModelEvent.OnModelInit, (model: OpenAIModel) => {
Expand All @@ -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();
},
);
}

/**
Expand All @@ -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");
Expand All @@ -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) {
Expand All @@ -116,7 +132,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
}

private static async handleInferenceStopped(
instance: JanInferenceOpenAIExtension
instance: JanInferenceOpenAIExtension,
) {
instance.isCancelled = true;
instance.controller?.abort();
Expand Down
14 changes: 10 additions & 4 deletions web/hooks/useEngineSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fs, joinPath } from '@janhq/core'
import { fs, joinPath, events, AppConfigurationEventName } from '@janhq/core'

export const useEngineSettings = () => {
const readOpenAISettings = async () => {
Expand All @@ -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 }
Expand Down

0 comments on commit 7ed523e

Please sign in to comment.