Skip to content

Commit

Permalink
fix(ui): initialize the persisted model name properly (#3385)
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung authored Nov 7, 2024
1 parent ab8d187 commit 6ddb893
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ee/tabby-ui/app/(home)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Image from 'next/image'
import { useRouter } from 'next/navigation'
import tabbyUrl from '@/assets/logo-dark.png'
import { useQuery } from 'urql'
import { useStore } from 'zustand'

import { SESSION_STORAGE_KEY } from '@/lib/constants'
import { useHealth } from '@/lib/hooks/use-health'
import { useMe } from '@/lib/hooks/use-me'
import { useSelectedModel } from '@/lib/hooks/use-models'
import { useIsChatEnabled } from '@/lib/hooks/use-server-info'
import { useStore } from '@/lib/hooks/use-store'
import { updateSelectedModel } from '@/lib/stores/chat-actions'
import {
clearHomeScrollPosition,
Expand Down
16 changes: 6 additions & 10 deletions ee/tabby-ui/lib/hooks/use-models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import { useEffect } from 'react'
import { Maybe } from 'graphql/jsutils/Maybe'
import useSWR, { SWRResponse } from 'swr'
import { useStore } from 'zustand'

import fetcher from '@/lib/tabby/fetcher'

import { updateSelectedModel } from '../stores/chat-actions'
import { useChatStore } from '../stores/chat-store'
import { useStore } from './use-store'

export interface ModelInfo {
completion: Maybe<Array<string>>
Expand All @@ -33,24 +33,20 @@ export function useModels(): SWRResponse<ModelInfo> {

export function useSelectedModel() {
const { data: modelData, isLoading: isFetchingModel } = useModels()
const isModelHydrated = useStore(useChatStore, state => state._hasHydrated)

const selectedModel = useStore(useChatStore, state => state.selectedModel)

// once model hydrated, try to init model
useEffect(() => {
if (isModelHydrated && !isFetchingModel) {
// check if current model is valid
if (!isFetchingModel) {
// init model
const validModel = getModelFromModelInfo(selectedModel, modelData?.chat)
if (selectedModel !== validModel) {
updateSelectedModel(validModel)
}
updateSelectedModel(validModel)
}
}, [isModelHydrated, isFetchingModel])
}, [isFetchingModel])

return {
// fetching model data or trying to get selected model from localstorage
isModelLoading: isFetchingModel || !isModelHydrated,
isModelLoading: isFetchingModel,
selectedModel,
models: modelData?.chat
}
Expand Down

0 comments on commit 6ddb893

Please sign in to comment.