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 './appConfigEntity'
|
||||||
|
export * from './appConfigEvent'
|
||||||
|
|||||||
@ -19,6 +19,8 @@ import {
|
|||||||
MessageEvent,
|
MessageEvent,
|
||||||
ModelEvent,
|
ModelEvent,
|
||||||
InferenceEvent,
|
InferenceEvent,
|
||||||
|
AppConfigurationEventName,
|
||||||
|
joinPath,
|
||||||
} from "@janhq/core";
|
} from "@janhq/core";
|
||||||
import { requestInference } from "./helpers/sse";
|
import { requestInference } from "./helpers/sse";
|
||||||
import { ulid } from "ulid";
|
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.
|
* It also subscribes to events emitted by the @janhq/core package and handles new message requests.
|
||||||
*/
|
*/
|
||||||
export default class JanInferenceOpenAIExtension extends BaseExtension {
|
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 readonly _engineMetadataFileName = "openai.json";
|
||||||
|
|
||||||
private static _currentModel: OpenAIModel;
|
private static _currentModel: OpenAIModel;
|
||||||
@ -47,9 +49,9 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
|
|||||||
* Subscribes to events emitted by the @janhq/core package.
|
* Subscribes to events emitted by the @janhq/core package.
|
||||||
*/
|
*/
|
||||||
async onLoad() {
|
async onLoad() {
|
||||||
if (!(await fs.existsSync(JanInferenceOpenAIExtension._homeDir))) {
|
if (!(await fs.existsSync(JanInferenceOpenAIExtension._engineDir))) {
|
||||||
await fs
|
await fs
|
||||||
.mkdirSync(JanInferenceOpenAIExtension._homeDir)
|
.mkdirSync(JanInferenceOpenAIExtension._engineDir)
|
||||||
.catch((err) => console.debug(err));
|
.catch((err) => console.debug(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +59,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
|
|||||||
|
|
||||||
// Events subscription
|
// Events subscription
|
||||||
events.on(MessageEvent.OnMessageSent, (data) =>
|
events.on(MessageEvent.OnMessageSent, (data) =>
|
||||||
JanInferenceOpenAIExtension.handleMessageRequest(data, this)
|
JanInferenceOpenAIExtension.handleMessageRequest(data, this),
|
||||||
);
|
);
|
||||||
|
|
||||||
events.on(ModelEvent.OnModelInit, (model: OpenAIModel) => {
|
events.on(ModelEvent.OnModelInit, (model: OpenAIModel) => {
|
||||||
@ -70,6 +72,20 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
|
|||||||
events.on(InferenceEvent.OnInferenceStopped, () => {
|
events.on(InferenceEvent.OnInferenceStopped, () => {
|
||||||
JanInferenceOpenAIExtension.handleInferenceStopped(this);
|
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() {
|
static async writeDefaultEngineSettings() {
|
||||||
try {
|
try {
|
||||||
const engineFile = join(
|
const engineFile = join(
|
||||||
JanInferenceOpenAIExtension._homeDir,
|
JanInferenceOpenAIExtension._engineDir,
|
||||||
JanInferenceOpenAIExtension._engineMetadataFileName
|
JanInferenceOpenAIExtension._engineMetadataFileName,
|
||||||
);
|
);
|
||||||
if (await fs.existsSync(engineFile)) {
|
if (await fs.existsSync(engineFile)) {
|
||||||
const engine = await fs.readFileSync(engineFile, "utf-8");
|
const engine = await fs.readFileSync(engineFile, "utf-8");
|
||||||
@ -90,7 +106,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
|
|||||||
} else {
|
} else {
|
||||||
await fs.writeFileSync(
|
await fs.writeFileSync(
|
||||||
engineFile,
|
engineFile,
|
||||||
JSON.stringify(JanInferenceOpenAIExtension._engineSettings, null, 2)
|
JSON.stringify(JanInferenceOpenAIExtension._engineSettings, null, 2),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -116,7 +132,7 @@ export default class JanInferenceOpenAIExtension extends BaseExtension {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static async handleInferenceStopped(
|
private static async handleInferenceStopped(
|
||||||
instance: JanInferenceOpenAIExtension
|
instance: JanInferenceOpenAIExtension,
|
||||||
) {
|
) {
|
||||||
instance.isCancelled = true;
|
instance.isCancelled = true;
|
||||||
instance.controller?.abort();
|
instance.controller?.abort();
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import { fs, joinPath } from '@janhq/core'
|
import { fs, joinPath, events, AppConfigurationEventName } from '@janhq/core'
|
||||||
|
|
||||||
export const useEngineSettings = () => {
|
export const useEngineSettings = () => {
|
||||||
const readOpenAISettings = async () => {
|
const readOpenAISettings = async () => {
|
||||||
@ -21,10 +21,16 @@ export const useEngineSettings = () => {
|
|||||||
apiKey: string | undefined
|
apiKey: string | undefined
|
||||||
}) => {
|
}) => {
|
||||||
const settings = await readOpenAISettings()
|
const settings = await readOpenAISettings()
|
||||||
|
const settingFilePath = await joinPath(['file://engines', 'openai.json'])
|
||||||
|
|
||||||
settings.api_key = apiKey
|
settings.api_key = apiKey
|
||||||
await fs.writeFileSync(
|
|
||||||
await joinPath(['file://engines', 'openai.json']),
|
await fs.writeFileSync(settingFilePath, JSON.stringify(settings))
|
||||||
JSON.stringify(settings)
|
|
||||||
|
// Sec: Don't attach the settings data to the event
|
||||||
|
events.emit(
|
||||||
|
AppConfigurationEventName.OnConfigurationUpdate,
|
||||||
|
settingFilePath
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return { readOpenAISettings, saveOpenAISettings }
|
return { readOpenAISettings, saveOpenAISettings }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user