jan/web/hooks/useEngineSettings.ts
Meta Spartan 0348aa3321
feat: Groq Inference Extension (#2263)
* feat: Groq Inference Extension

* Add Groq supported models

* Fix folder typo

* Add Groq options to interface and new API Key saving, tested working

* Fix linting
2024-03-18 06:40:20 +07:00

79 lines
2.0 KiB
TypeScript

import { useCallback } from 'react'
import { fs, joinPath, events, AppConfigurationEventName } from '@janhq/core'
export const useEngineSettings = () => {
const readOpenAISettings = useCallback(async () => {
if (
!(await fs.existsSync(await joinPath(['file://engines', 'openai.json'])))
)
return {}
const settings = await fs.readFileSync(
await joinPath(['file://engines', 'openai.json']),
'utf-8'
)
if (settings) {
return typeof settings === 'object' ? settings : JSON.parse(settings)
}
return {}
}, [])
const saveOpenAISettings = async ({
apiKey,
}: {
apiKey: string | undefined
}) => {
const settings = await readOpenAISettings()
const settingFilePath = await joinPath(['file://engines', 'openai.json'])
settings.api_key = apiKey
await fs.writeFileSync(settingFilePath, JSON.stringify(settings))
// Sec: Don't attach the settings data to the event
events.emit(
AppConfigurationEventName.OnConfigurationUpdate,
settingFilePath
)
}
const readGroqSettings = useCallback(async () => {
if (!(await fs.existsSync(await joinPath(['file://engines', 'groq.json']))))
return {}
const settings = await fs.readFileSync(
await joinPath(['file://engines', 'groq.json']),
'utf-8'
)
if (settings) {
return typeof settings === 'object' ? settings : JSON.parse(settings)
}
return {}
}, [])
const saveGroqSettings = async ({
apiKey,
}: {
apiKey: string | undefined
}) => {
const settings = await readGroqSettings()
const settingFilePath = await joinPath(['file://engines', 'groq.json'])
settings.api_key = apiKey
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,
readGroqSettings,
saveGroqSettings,
}
}