import React, { useCallback, useMemo } from 'react' import { Input, Textarea } from '@janhq/uikit' import { atom, useAtomValue, useSetAtom } from 'jotai' import { twMerge } from 'tailwind-merge' import LogoMark from '@/containers/Brand/Logo/Mark' import CardSidebar from '@/containers/CardSidebar' import DropdownListSidebar, { selectedModelAtom, } from '@/containers/DropdownListSidebar' import { useActiveModel } from '@/hooks/useActiveModel' import { useCreateNewThread } from '@/hooks/useCreateNewThread' import useUpdateModelParameters from '@/hooks/useUpdateModelParameters' import { getConfigurationsData } from '@/utils/componentSettings' import { toRuntimeParams, toSettingParams } from '@/utils/modelParam' import EngineSetting from '../EngineSetting' import ModelSetting from '../ModelSetting' import AssistantTool from './AssistantTool' import PromptTemplateSetting from './PromptTemplateSetting' import { activeThreadAtom, engineParamsUpdateAtom, getActiveThreadModelParamsAtom, } from '@/helpers/atoms/Thread.atom' export const showRightSideBarAtom = atom(true) const Sidebar: React.FC = () => { const showing = useAtomValue(showRightSideBarAtom) const activeThread = useAtomValue(activeThreadAtom) const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom) const selectedModel = useAtomValue(selectedModelAtom) const { updateThreadMetadata } = useCreateNewThread() const setEngineParamsUpdate = useSetAtom(engineParamsUpdateAtom) const { stopModel } = useActiveModel() const { updateModelParameter } = useUpdateModelParameters() const modelSettings = useMemo(() => { const modelRuntimeParams = toRuntimeParams(activeModelParams) const componentDataRuntimeSetting = getConfigurationsData( modelRuntimeParams, selectedModel ) return componentDataRuntimeSetting.filter( (x) => x.key !== 'prompt_template' ) }, [activeModelParams, selectedModel]) const engineSettings = useMemo(() => { const modelEngineParams = toSettingParams(activeModelParams) const componentDataEngineSetting = getConfigurationsData( modelEngineParams, selectedModel ) return componentDataEngineSetting.filter((x) => x.key !== 'prompt_template') }, [activeModelParams, selectedModel]) const promptTemplateSettings = useMemo(() => { const modelEngineParams = toSettingParams(activeModelParams) const componentDataEngineSetting = getConfigurationsData( modelEngineParams, selectedModel ) return componentDataEngineSetting.filter((x) => x.key === 'prompt_template') }, [activeModelParams, selectedModel]) const onThreadTitleChanged = useCallback( (e: React.ChangeEvent) => { if (activeThread) updateThreadMetadata({ ...activeThread, title: e.target.value || '', }) }, [activeThread, updateThreadMetadata] ) const onAssistantInstructionChanged = useCallback( (e: React.ChangeEvent) => { if (activeThread) updateThreadMetadata({ ...activeThread, assistants: [ { ...activeThread.assistants[0], instructions: e.target.value || '', }, ], }) }, [activeThread, updateThreadMetadata] ) const onValueChanged = useCallback( (key: string, value: string | number | boolean) => { if (!activeThread) { return } setEngineParamsUpdate(true) stopModel() updateModelParameter(activeThread, { params: { [key]: value }, }) }, [activeThread, setEngineParamsUpdate, stopModel, updateModelParameter] ) return (
{activeThread?.id || '-'}
{activeThread?.assistants[0].assistant_name ?? '-'}