feat(ModelSetting): #1065 update state of model setting between threads (#1090)

Signed-off-by: James <james@jan.ai>
Co-authored-by: James <james@jan.ai>
This commit is contained in:
NamH 2023-12-19 13:15:15 +07:00 committed by GitHub
parent c1800b8f57
commit 7a148ea025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 81 deletions

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ import { FieldValues, UseFormRegister } from 'react-hook-form'
import { useEffect, useState } from 'react'
import { ModelRuntimeParams } from '@janhq/core'
import { Switch } from '@janhq/uikit' import { Switch } from '@janhq/uikit'
import { useAtomValue } from 'jotai' import { useAtomValue } from 'jotai'
@ -16,44 +16,32 @@ type Props = {
name: string name: string
title: string title: string
checked: boolean checked: boolean
register: any register: UseFormRegister<FieldValues>
} }
const Checkbox: React.FC<Props> = ({ name, title, checked, register }) => { const Checkbox: React.FC<Props> = ({ name, title, checked, register }) => {
const [currentChecked, setCurrentChecked] = useState<boolean>(checked)
const { updateModelParameter } = useUpdateModelParameters() const { updateModelParameter } = useUpdateModelParameters()
const threadId = useAtomValue(getActiveThreadIdAtom) const threadId = useAtomValue(getActiveThreadIdAtom)
const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom)
useEffect(() => { const onCheckedChange = (checked: boolean) => {
setCurrentChecked(checked) if (!threadId || !activeModelParams) return
}, [checked])
useEffect(() => { const updatedModelParams: ModelRuntimeParams = {
updateSetting()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentChecked])
const updateValue = [name].reduce((accumulator, value) => {
return { ...accumulator, [value]: currentChecked }
}, {})
const updateSetting = () => {
return updateModelParameter(String(threadId), {
...activeModelParams, ...activeModelParams,
...updateValue, [name]: checked,
}) }
updateModelParameter(threadId, updatedModelParams)
} }
return ( return (
<div className="flex justify-between"> <div className="flex justify-between">
<label>{title}</label> <label>{title}</label>
<Switch <Switch
checked={currentChecked} checked={checked}
{...register(name)} {...register(name)}
onCheckedChange={(e) => { onCheckedChange={onCheckedChange}
setCurrentChecked(e)
}}
/> />
</div> </div>
) )

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ import { FieldValues, UseFormRegister } from 'react-hook-form'
import { useEffect, useState } from 'react'
import { ModelRuntimeParams } from '@janhq/core'
import { Slider, Input } from '@janhq/uikit' import { Slider, Input } from '@janhq/uikit'
import { useAtomValue } from 'jotai' import { useAtomValue } from 'jotai'
@ -18,7 +18,7 @@ type Props = {
max: number max: number
step: number step: number
value: number value: number
register: any register: UseFormRegister<FieldValues>
} }
const SliderRightPanel: React.FC<Props> = ({ const SliderRightPanel: React.FC<Props> = ({
@ -30,30 +30,19 @@ const SliderRightPanel: React.FC<Props> = ({
value, value,
register, register,
}) => { }) => {
const [currentValue, setCurrentValue] = useState<number>(value)
const { updateModelParameter } = useUpdateModelParameters() const { updateModelParameter } = useUpdateModelParameters()
const threadId = useAtomValue(getActiveThreadIdAtom) const threadId = useAtomValue(getActiveThreadIdAtom)
const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom) const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom)
useEffect(() => { const onValueChanged = (e: number[]) => {
setCurrentValue(value) if (!threadId || !activeModelParams) return
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [value])
useEffect(() => { const updatedModelParams: ModelRuntimeParams = {
updateSetting()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [currentValue])
const updateValue = [name].reduce((accumulator, value) => {
return { ...accumulator, [value]: currentValue }
}, {})
const updateSetting = () => {
return updateModelParameter(String(threadId), {
...activeModelParams, ...activeModelParams,
...updateValue, [name]: Number(e[0]),
}) }
updateModelParameter(threadId, updatedModelParams)
} }
return ( return (
@ -63,14 +52,10 @@ const SliderRightPanel: React.FC<Props> = ({
<div className="relative w-full"> <div className="relative w-full">
<Slider <Slider
{...register(name, { {...register(name, {
setValueAs: (v: any) => parseInt(v), setValueAs: (v: string) => parseInt(v),
})} })}
value={[currentValue]} value={[value]}
onValueChange={async (e) => { onValueChange={onValueChanged}
setCurrentValue(Number(e[0]))
await updateSetting()
}}
type="range"
min={min} min={min}
max={max} max={max}
step={step} step={step}
@ -87,11 +72,8 @@ const SliderRightPanel: React.FC<Props> = ({
className="-mt-4 h-8 w-16" className="-mt-4 h-8 w-16"
min={min} min={min}
max={max} max={max}
value={String(currentValue)} value={String(value)}
onChange={async (e) => { onChange={(e) => onValueChanged([Number(e.target.value)])}
setCurrentValue(Number(e.target.value))
await updateSetting()
}}
/> />
</div> </div>
</div> </div>

View File

@ -1,5 +1,3 @@
import { useEffect, useState } from 'react'
import { useForm } from 'react-hook-form' import { useForm } from 'react-hook-form'
import { ModelRuntimeParams } from '@janhq/core' import { ModelRuntimeParams } from '@janhq/core'
@ -11,42 +9,31 @@ import settingComponentBuilder, {
SettingComponentData, SettingComponentData,
} from './settingComponentBuilder' } from './settingComponentBuilder'
import { import { getActiveThreadModelRuntimeParamsAtom } from '@/helpers/atoms/Thread.atom'
getActiveThreadIdAtom,
getActiveThreadModelRuntimeParamsAtom,
} from '@/helpers/atoms/Thread.atom'
export default function ModelSetting() { export default function ModelSetting() {
const threadId = useAtomValue(getActiveThreadIdAtom)
const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom)
const [modelParams, setModelParams] = useState<
ModelRuntimeParams | undefined
>(activeModelParams)
const { register } = useForm() const { register } = useForm()
const activeModelParams = useAtomValue(getActiveThreadModelRuntimeParamsAtom)
useEffect(() => { if (!activeModelParams) {
setModelParams(activeModelParams) return null
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [threadId])
if (!modelParams) {
return <div>This thread has no model parameters</div>
} }
const componentData: SettingComponentData[] = [] const componentData: SettingComponentData[] = []
Object.keys(modelParams).forEach((key) => { Object.keys(activeModelParams).forEach((key) => {
const componentSetting = presetConfiguration[key] const componentSetting = presetConfiguration[key]
if (componentSetting) { if (componentSetting) {
if ('value' in componentSetting.controllerData) { if ('value' in componentSetting.controllerData) {
componentSetting.controllerData.value = Number( componentSetting.controllerData.value = Number(
modelParams[key as keyof ModelRuntimeParams] activeModelParams[key as keyof ModelRuntimeParams]
) )
} else if ('checked' in componentSetting.controllerData) { } else if ('checked' in componentSetting.controllerData) {
componentSetting.controllerData.checked = modelParams[ const checked = activeModelParams[
key as keyof ModelRuntimeParams key as keyof ModelRuntimeParams
] as boolean ] as boolean
componentSetting.controllerData.checked = checked
} }
componentData.push(componentSetting) componentData.push(componentSetting)
} }

View File

@ -1,5 +1,5 @@
/* eslint-disable no-case-declarations */ /* eslint-disable no-case-declarations */
/* eslint-disable @typescript-eslint/no-explicit-any */ import { FieldValues, UseFormRegister } from 'react-hook-form'
import Checkbox from '@/containers/Checkbox' import Checkbox from '@/containers/Checkbox'
import Slider from '@/containers/Slider' import Slider from '@/containers/Slider'
@ -27,7 +27,7 @@ type CheckboxData = {
const settingComponentBuilder = ( const settingComponentBuilder = (
componentData: SettingComponentData[], componentData: SettingComponentData[],
register: any register: UseFormRegister<FieldValues>
) => { ) => {
const components = componentData.map((data) => { const components = componentData.map((data) => {
switch (data.controllerType) { switch (data.controllerType) {