Skip to content

Commit

Permalink
fix: local server blank parameters if there is no thread selected (ja…
Browse files Browse the repository at this point in the history
…nhq#2028)

* fix: local server blank parameters if there is no thread selected

* fix: show errors only when the selected model is started while starting the server

* fix: show loading progress of selected model only
  • Loading branch information
louis-jan authored Feb 15, 2024
1 parent 9cc9b4d commit 3ab23d5
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 79 deletions.
14 changes: 9 additions & 5 deletions web/containers/DropdownListSidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const DropdownListSidebar = ({

// This is fake loader please fix this when we have realtime percentage when load model
useEffect(() => {
if (stateModel.loading) {
if (stateModel.model === selectedModel?.id && stateModel.loading) {
if (loader === 24) {
setTimeout(() => {
setLoader(loader + 1)
Expand All @@ -115,7 +115,7 @@ const DropdownListSidebar = ({
} else {
setLoader(0)
}
}, [stateModel.loading, loader])
}, [stateModel.loading, loader, selectedModel, stateModel.model])

const onValueSelected = useCallback(
async (modelId: string) => {
Expand Down Expand Up @@ -159,12 +159,16 @@ const DropdownListSidebar = ({
return null
}

const selectedModelLoading =
stateModel.model === selectedModel?.id && stateModel.loading

return (
<>
<div
className={twMerge(
'relative w-full overflow-hidden rounded-md',
stateModel.loading && 'pointer-events-none bg-blue-200 text-blue-600'
stateModel.loading && 'pointer-events-none',
selectedModelLoading && 'bg-blue-200 text-blue-600'
)}
>
<Select
Expand All @@ -174,7 +178,7 @@ const DropdownListSidebar = ({
>
<SelectTrigger className="relative w-full">
<SelectValue placeholder="Choose model to start">
{stateModel.loading && (
{selectedModelLoading && (
<div
className="z-5 absolute left-0 top-0 h-full w-full rounded-md bg-blue-100/80"
style={{ width: `${loader}%` }}
Expand All @@ -183,7 +187,7 @@ const DropdownListSidebar = ({
<span
className={twMerge(
'relative z-20',
stateModel.loading && 'font-medium'
selectedModelLoading && 'font-medium'
)}
>
{selectedModel?.name}
Expand Down
4 changes: 4 additions & 0 deletions web/helpers/atoms/Thread.atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export const isGeneratingResponseAtom = atom<boolean | undefined>(undefined)
* Stores all thread states for the current user
*/
export const threadStatesAtom = atom<Record<string, ThreadState>>({})

// Whether thread data is ready or not
export const threadDataReadyAtom = atom<boolean>(false)

export const activeThreadStateAtom = atom<ThreadState | undefined>((get) => {
const threadId = get(activeThreadIdAtom)
if (!threadId) {
Expand Down
4 changes: 4 additions & 0 deletions web/hooks/useThreads.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import useSetActiveThread from './useSetActiveThread'
import { extensionManager } from '@/extension/ExtensionManager'
import {
ModelParams,
threadDataReadyAtom,
threadModelParamsAtom,
threadStatesAtom,
threadsAtom,
Expand All @@ -24,6 +25,7 @@ const useThreads = () => {
const setThreads = useSetAtom(threadsAtom)
const setThreadModelRuntimeParams = useSetAtom(threadModelParamsAtom)
const { setActiveThread } = useSetActiveThread()
const setThreadDataReady = useSetAtom(threadDataReadyAtom)

useEffect(() => {
const getThreads = async () => {
Expand Down Expand Up @@ -58,6 +60,7 @@ const useThreads = () => {
if (localThreads.length > 0) {
setActiveThread(localThreads[0])
}
setThreadDataReady(true)
}

getThreads()
Expand All @@ -66,6 +69,7 @@ const useThreads = () => {
setThreadModelRuntimeParams,
setThreadStates,
setThreads,
setThreadDataReady,
])
}

Expand Down
53 changes: 21 additions & 32 deletions web/screens/Chat/EngineSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { useAtomValue } from 'jotai'

import { selectedModelAtom } from '@/containers/DropdownListSidebar'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toSettingParams } from '@/utils/modelParam'

import SettingComponentBuilder from '../ModelSetting/SettingComponent'

import { getActiveThreadModelParamsAtom } from '@/helpers/atoms/Thread.atom'

const EngineSetting = ({ enabled = true }: { enabled?: boolean }) => {
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const selectedModel = useAtomValue(selectedModelAtom)

if (!selectedModel || !activeModelParams) return null

const modelSettingParams = toSettingParams(activeModelParams)

const componentData = getConfigurationsData(
modelSettingParams,
selectedModel
).toSorted((a, b) => a.title.localeCompare(b.title))

import SettingComponentBuilder from '../../Chat/ModelSetting/SettingComponent'
import { SettingComponentData } from '../ModelSetting/SettingComponent'

const EngineSetting = ({
componentData,
enabled = true,
}: {
componentData: SettingComponentData[]
enabled?: boolean
}) => {
return (
<div className="flex flex-col">
<SettingComponentBuilder
componentData={componentData}
enabled={enabled}
selector={(e) => e.name !== 'prompt_template'}
/>
</div>
<>
{componentData.filter((e) => e.name !== 'prompt_template').length && (
<div className="flex flex-col">
<SettingComponentBuilder
componentData={componentData}
enabled={enabled}
selector={(e) => e.name !== 'prompt_template'}
/>
</div>
)}
</>
)
}

Expand Down
49 changes: 19 additions & 30 deletions web/screens/Chat/ModelSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import React from 'react'

import { useAtomValue } from 'jotai'

import { selectedModelAtom } from '@/containers/DropdownListSidebar'

import { getConfigurationsData } from '@/utils/componentSettings'
import { toRuntimeParams } from '@/utils/modelParam'

import SettingComponentBuilder from './SettingComponent'

import { getActiveThreadModelParamsAtom } from '@/helpers/atoms/Thread.atom'

const ModelSetting = () => {
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const selectedModel = useAtomValue(selectedModelAtom)

if (!selectedModel || !activeModelParams) return null

const modelRuntimeParams = toRuntimeParams(activeModelParams)

const componentData = getConfigurationsData(
modelRuntimeParams,
selectedModel
).toSorted((a, b) => a.title.localeCompare(b.title))

import SettingComponentBuilder, {
SettingComponentData,
} from './SettingComponent'

const ModelSetting = ({
componentData,
}: {
componentData: SettingComponentData[]
}) => {
return (
<div className="flex flex-col">
<SettingComponentBuilder
componentData={componentData}
selector={(e) => e.name !== 'prompt_template'}
/>
</div>
<>
{componentData.filter((e) => e.name !== 'prompt_template').length && (
<div className="flex flex-col">
<SettingComponentBuilder
componentData={componentData}
selector={(e) => e.name !== 'prompt_template'}
/>
</div>
)}
</>
)
}

Expand Down
14 changes: 10 additions & 4 deletions web/screens/Chat/Sidebar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ const Sidebar: React.FC = () => {
activeThread?.assistants[0]?.tools[0]?.settings) ??
{}
)
const componentDataEngineSetting = getConfigurationsData(modelEngineParams)
const componentDataRuntimeSetting = getConfigurationsData(modelRuntimeParams)
const componentDataEngineSetting = getConfigurationsData(
modelEngineParams,
selectedModel
)
const componentDataRuntimeSetting = getConfigurationsData(
modelRuntimeParams,
selectedModel
)

return (
<div
Expand Down Expand Up @@ -319,7 +325,7 @@ const Sidebar: React.FC = () => {
<div className="mt-6">
<CardSidebar title="Inference Parameters" asChild>
<div className="px-2 py-4">
<ModelSetting />
<ModelSetting componentData={componentDataRuntimeSetting} />
</div>
</CardSidebar>
</div>
Expand All @@ -344,7 +350,7 @@ const Sidebar: React.FC = () => {
<div className="my-4">
<CardSidebar title="Engine Parameters" asChild>
<div className="px-2 py-4">
<EngineSetting />
<EngineSetting componentData={componentDataEngineSetting} />
</div>
</CardSidebar>
</div>
Expand Down
19 changes: 18 additions & 1 deletion web/screens/Chat/ThreadList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback } from 'react'
import { useCallback, useEffect } from 'react'

import { Thread } from '@janhq/core/'

Expand All @@ -8,6 +8,7 @@ import { GalleryHorizontalEndIcon, MoreVerticalIcon } from 'lucide-react'

import { twMerge } from 'tailwind-merge'

import { useCreateNewThread } from '@/hooks/useCreateNewThread'
import useSetActiveThread from '@/hooks/useSetActiveThread'

import { displayDate } from '@/utils/datetime'
Expand All @@ -16,8 +17,10 @@ import CleanThreadModal from '../CleanThreadModal'

import DeleteThreadModal from '../DeleteThreadModal'

import { assistantsAtom } from '@/helpers/atoms/Assistant.atom'
import {
getActiveThreadIdAtom,
threadDataReadyAtom,
threadStatesAtom,
threadsAtom,
} from '@/helpers/atoms/Thread.atom'
Expand All @@ -27,6 +30,9 @@ export default function ThreadList() {
const threads = useAtomValue(threadsAtom)
const activeThreadId = useAtomValue(getActiveThreadIdAtom)
const { setActiveThread } = useSetActiveThread()
const assistants = useAtomValue(assistantsAtom)
const threadDataReady = useAtomValue(threadDataReadyAtom)
const { requestCreateNewThread } = useCreateNewThread()

const onThreadClick = useCallback(
(thread: Thread) => {
Expand All @@ -35,6 +41,17 @@ export default function ThreadList() {
[setActiveThread]
)

/**
* Auto create thread
* This will create a new thread if there are assistants available
* and there are no threads available
*/
useEffect(() => {
if (threadDataReady && assistants.length > 0 && threads.length === 0) {
requestCreateNewThread(assistants[0])
}
}, [assistants, threads, threadDataReady, requestCreateNewThread])

return (
<div className="px-3 py-4">
{threads.length === 0 ? (
Expand Down
15 changes: 8 additions & 7 deletions web/screens/LocalServer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import SettingComponentBuilder from '../Chat/ModelSetting/SettingComponent'
import { showRightSideBarAtom } from '../Chat/Sidebar'

import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'
import { getActiveThreadModelParamsAtom } from '@/helpers/atoms/Thread.atom'

const corsEnabledAtom = atom(true)
const verboseEnabledAtom = atom(true)
Expand All @@ -63,16 +62,15 @@ const LocalServerScreen = () => {
const [errorRangePort, setErrorRangePort] = useState(false)
const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom)
const showRightSideBar = useAtomValue(showRightSideBarAtom)
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const setModalTroubleShooting = useSetAtom(modalTroubleShootingAtom)

const modelEngineParams = toSettingParams(activeModelParams)
const componentDataEngineSetting = getConfigurationsData(modelEngineParams)

const { openServerLog, clearServerLog } = useLogs()
const { startModel, stateModel } = useActiveModel()
const selectedModel = useAtomValue(selectedModelAtom)

const modelEngineParams = toSettingParams(selectedModel?.settings)
const componentDataEngineSetting = getConfigurationsData(modelEngineParams)

const [isCorsEnabled, setIsCorsEnabled] = useAtom(corsEnabledAtom)
const [isVerboseEnabled, setIsVerboseEnabled] = useAtom(verboseEnabledAtom)
const [host, setHost] = useAtom(hostAtom)
Expand Down Expand Up @@ -396,7 +394,7 @@ const LocalServerScreen = () => {
</p>
</div>
<DropdownListSidebar strictedThread={false} />
{loadModelError && (
{loadModelError && serverEnabled && (
<div className="mt-3 flex space-x-2 text-xs">
<AlertTriangleIcon size={16} className="text-danger" />
<span>
Expand Down Expand Up @@ -431,7 +429,10 @@ const LocalServerScreen = () => {
<div className="my-4">
<CardSidebar title="Engine Parameters" asChild>
<div className="px-2 py-4">
<EngineSetting enabled={!serverEnabled} />
<EngineSetting
enabled={!serverEnabled}
componentData={componentDataEngineSetting}
/>
</div>
</CardSidebar>
</div>
Expand Down

0 comments on commit 3ab23d5

Please sign in to comment.