fix: api settings are not applied on changes (#1789)
This commit is contained in:
parent
f4bf8badc8
commit
7ed523e183
6
core/src/types/config/appConfigEvent.ts
Normal file
6
core/src/types/config/appConfigEvent.ts
Normal file
@ -0,0 +1,6 @@
|
||||
/**
|
||||
* App configuration event name
|
||||
*/
|
||||
export enum AppConfigurationEventName {
|
||||
OnConfigurationUpdate = 'OnConfigurationUpdate',
|
||||
}
|
||||
@ -1 +1,2 @@
|
||||
export * from './appConfigEntity'
|
||||
export * from './appConfigEvent'
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -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 }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user