chore: read and write openai settings

This commit is contained in:
Louis 2023-12-15 14:05:06 +07:00 committed by Faisal Amir
parent 48195371e2
commit d9b0625b17
2 changed files with 38 additions and 4 deletions

View File

@ -21,6 +21,7 @@ import { twMerge } from 'tailwind-merge'
import { MainViewState } from '@/constants/screens'
import { useActiveModel } from '@/hooks/useActiveModel'
import { useEngineSettings } from '@/hooks/useEngineSettings'
import { getDownloadedModels } from '@/hooks/useGetDownloadedModels'
import { useMainViewState } from '@/hooks/useMainViewState'
@ -38,6 +39,16 @@ export default function DropdownListSidebar() {
const [selected, setSelected] = useState<Model | undefined>()
const { setMainViewState } = useMainViewState()
const { activeModel, stateModel } = useActiveModel()
const [opeenAISettings, setOpenAISettings] = useState<
{ api_key: string } | undefined
>(undefined)
const { readOpenAISettings, saveOpenAISettings } = useEngineSettings()
useEffect(() => {
readOpenAISettings().then((settings) => {
setOpenAISettings(settings)
})
}, [])
useEffect(() => {
getDownloadedModels().then((downloadedModels) => {
@ -129,20 +140,20 @@ export default function DropdownListSidebar() {
</SelectContent>
</Select>
{selected?.engine === 'openai' && (
{selected?.engine === InferenceEngine.openai && (
<div className="mt-4">
<label
id="thread-title"
className="mb-2 inline-block font-bold text-gray-600 dark:text-gray-300"
>
API_KEY
API Key
</label>
<Input
id="assistant-instructions"
placeholder="Enter your API_KEY"
value=""
defaultValue={opeenAISettings?.api_key}
onChange={(e) => {
console.log(e.target.value)
saveOpenAISettings({ apiKey: e.target.value })
}}
/>
</div>

View File

@ -0,0 +1,23 @@
import { join } from 'path'
import { fs } from '@janhq/core'
export const useEngineSettings = () => {
const readOpenAISettings = async () => {
const settings = await fs.readFile(join('engines', 'openai.json'))
if (settings) {
return JSON.parse(settings)
}
return {}
}
const saveOpenAISettings = async ({
apiKey,
}: {
apiKey: string | undefined
}) => {
const settings = await readOpenAISettings()
settings.api_key = apiKey
await fs.writeFile(join('engines', 'openai.json'), JSON.stringify(settings))
}
return { readOpenAISettings, saveOpenAISettings }
}