Skip to content

Commit

Permalink
chore: refactor - clean out useEngines - mutate swr to update
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-menlo committed Jan 17, 2025
1 parent 6041d61 commit a8cac58
Show file tree
Hide file tree
Showing 18 changed files with 64 additions and 96 deletions.
4 changes: 2 additions & 2 deletions extensions/engine-management-extension/models/anthropic.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"inference_params": {
"max_tokens": 4096,
"temperature": 0.7,
"stream": false
"stream": true
},
"engine": "anthropic"
},
Expand All @@ -21,7 +21,7 @@
"inference_params": {
"max_tokens": 8192,
"temperature": 0.7,
"stream": false
"stream": true
},
"engine": "anthropic"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Tooltip, Button, Badge } from '@janhq/joi'

import { useAtom, useAtomValue } from 'jotai'
import { useAtom } from 'jotai'

import { useActiveModel } from '@/hooks/useActiveModel'

import { useGetEngines } from '@/hooks/useEngineManagement'

import { toGibibytes } from '@/utils/converter'

import { isLocalEngine } from '@/utils/modelEngine'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'

const TableActiveModel = () => {
const { activeModel, stateModel, stopModel } = useActiveModel()
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom)

Expand Down
4 changes: 2 additions & 2 deletions web/containers/ModelDropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import SetupRemoteModel from '@/containers/SetupRemoteModel'
import { useCreateNewThread } from '@/hooks/useCreateNewThread'
import useDownloadModel from '@/hooks/useDownloadModel'
import { modelDownloadStateAtom } from '@/hooks/useDownloadState'
import { useGetEngines } from '@/hooks/useEngineManagement'

import useRecommendedModel from '@/hooks/useRecommendedModel'

Expand All @@ -42,7 +43,6 @@ import { manualRecommendationModel } from '@/utils/model'
import { getLogoEngine } from '@/utils/modelEngine'

import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import {
configuredModelsAtom,
getDownloadingModelAtom,
Expand Down Expand Up @@ -86,7 +86,7 @@ const ModelDropdown = ({
null
)

const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

const downloadStates = useAtomValue(modelDownloadStateAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
Expand Down
25 changes: 21 additions & 4 deletions web/containers/Providers/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import { Fragment, useEffect } from 'react'

import { AppConfiguration, getUserHomePath } from '@janhq/core'
import {
AppConfiguration,
EngineEvent,
events,
getUserHomePath,
} from '@janhq/core'
import { useSetAtom } from 'jotai'

import { useDebouncedCallback } from 'use-debounce'

import useAssistants from '@/hooks/useAssistants'
import useEngines from '@/hooks/useEngines'
import { useGetEngines } from '@/hooks/useEngineManagement'
import useGetSystemResources from '@/hooks/useGetSystemResources'
import useModels from '@/hooks/useModels'
import useThreads from '@/hooks/useThreads'
Expand All @@ -26,7 +33,7 @@ const DataLoader: React.FC = () => {
const setJanDefaultDataFolder = useSetAtom(defaultJanDataFolderAtom)
const setJanSettingScreen = useSetAtom(janSettingScreenAtom)
const { getData: loadModels } = useModels()
const { getData: loadEngines } = useEngines()
const { mutate } = useGetEngines()

useThreads()
useAssistants()
Expand All @@ -35,9 +42,19 @@ const DataLoader: React.FC = () => {
useEffect(() => {
// Load data once
loadModels()
loadEngines()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const reloadData = useDebouncedCallback(() => {
mutate()
}, 300)

useEffect(() => {
events.on(EngineEvent.OnEngineUpdate, reloadData)
return () => {
// Remove listener on unmount
events.off(EngineEvent.OnEngineUpdate, reloadData)
}
}, [reloadData])

useEffect(() => {
window.core?.api
Expand Down
5 changes: 3 additions & 2 deletions web/containers/Providers/ModelHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import { ulid } from 'ulidx'

import { activeModelAtom, stateModelAtom } from '@/hooks/useActiveModel'

import { useGetEngines } from '@/hooks/useEngineManagement'

import { isLocalEngine } from '@/utils/modelEngine'

import { extensionManager } from '@/extension'
Expand All @@ -34,7 +36,6 @@ import {
deleteMessageAtom,
subscribedGeneratingMessageAtom,
} from '@/helpers/atoms/ChatMessage.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import {
updateThreadWaitingForResponseAtom,
Expand Down Expand Up @@ -75,7 +76,7 @@ export default function ModelHandler() {
const activeModelParams = useAtomValue(getActiveThreadModelParamsAtom)
const activeModelParamsRef = useRef(activeModelParams)
const setTokenSpeed = useSetAtom(tokenSpeedAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

useEffect(() => {
activeThreadRef.current = activeThread
Expand Down
7 changes: 0 additions & 7 deletions web/helpers/atoms/Engines.atom.ts

This file was deleted.

53 changes: 0 additions & 53 deletions web/hooks/useEngines.ts

This file was deleted.

5 changes: 3 additions & 2 deletions web/hooks/useRecommendedModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { atom, useAtomValue } from 'jotai'

import { activeModelAtom } from './useActiveModel'

import { useGetEngines } from './useEngineManagement'

import { activeAssistantAtom } from '@/helpers/atoms/Assistant.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'

Expand All @@ -31,7 +32,7 @@ export default function useRecommendedModel() {
const activeThread = useAtomValue(activeThreadAtom)
const downloadedModels = useAtomValue(downloadedModelsAtom)
const activeAssistant = useAtomValue(activeAssistantAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

const getAndSortDownloadedModels = useCallback(async (): Promise<Model[]> => {
const models = downloadedModels.sort((a, b) =>
Expand Down
5 changes: 3 additions & 2 deletions web/hooks/useStarterScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import { useAtomValue } from 'jotai'

import { isLocalEngine } from '@/utils/modelEngine'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { useGetEngines } from './useEngineManagement'

import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import { threadsAtom } from '@/helpers/atoms/Thread.atom'

export function useStarterScreen() {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const threads = useAtomValue(threadsAtom)

const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

const remoteEngines =
engines &&
Expand Down
5 changes: 3 additions & 2 deletions web/screens/Hub/ModelList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Model } from '@janhq/core'

import { useAtomValue } from 'jotai'

import { useGetEngines } from '@/hooks/useEngineManagement'

import ModelItem from '@/screens/Hub/ModelList/ModelItem'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'

type Props = {
Expand All @@ -15,7 +16,7 @@ type Props = {

const ModelList = ({ models }: Props) => {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()
const sortedModels: Model[] = useMemo(() => {
const featuredModels: Model[] = []
const remoteModels: Model[] = []
Expand Down
6 changes: 3 additions & 3 deletions web/screens/Settings/Engines/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { InferenceEngine } from '@janhq/core'
import { ScrollArea } from '@janhq/joi'
import { useAtomValue } from 'jotai'

Check warning on line 5 in web/screens/Settings/Engines/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'useAtomValue' is defined but never used

Check warning on line 5 in web/screens/Settings/Engines/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'useAtomValue' is defined but never used

Check warning on line 5 in web/screens/Settings/Engines/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'useAtomValue' is defined but never used

Check warning on line 5 in web/screens/Settings/Engines/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'useAtomValue' is defined but never used

import { useGetEngines } from '@/hooks/useEngineManagement'

import { isLocalEngine } from '@/utils/modelEngine'

import LocalEngineItems from './LocalEngineItem'
import ModalAddRemoteEngine from './ModalAddRemoteEngine'
import RemoteEngineItems from './RemoteEngineItem'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'

const Engines = () => {
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

return (
<ScrollArea className="h-full w-full">
Expand Down
5 changes: 3 additions & 2 deletions web/screens/Settings/MyModels/MyModelList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { twMerge } from 'tailwind-merge'
import { useActiveModel } from '@/hooks/useActiveModel'
import useDeleteModel from '@/hooks/useDeleteModel'

import { useGetEngines } from '@/hooks/useEngineManagement'

import { toGibibytes } from '@/utils/converter'

import { isLocalEngine } from '@/utils/modelEngine'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { serverEnabledAtom } from '@/helpers/atoms/LocalServer.atom'

type Props = {
Expand All @@ -32,7 +33,7 @@ const MyModelList = ({ model }: Props) => {
const { deleteModel } = useDeleteModel()
const [more, setMore] = useState(false)
const [serverEnabled, setServerEnabled] = useAtom(serverEnabledAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

const [menu, setMenu] = useState<HTMLDivElement | null>(null)
const [toggle, setToggle] = useState<HTMLDivElement | null>(null)
Expand Down
5 changes: 3 additions & 2 deletions web/screens/Settings/SettingDetail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { InferenceEngine } from '@janhq/core'
import { useAtomValue } from 'jotai'

import { useGetEngines } from '@/hooks/useEngineManagement'

import Advanced from '@/screens/Settings/Advanced'
import AppearanceOptions from '@/screens/Settings/Appearance'
import ExtensionCatalog from '@/screens/Settings/CoreExtensions'
Expand All @@ -14,12 +16,11 @@ import Privacy from '@/screens/Settings/Privacy'

import { isLocalEngine } from '@/utils/modelEngine'

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { selectedSettingAtom } from '@/helpers/atoms/Setting.atom'

const SettingDetail = () => {
const selectedSetting = useAtomValue(selectedSettingAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()

switch (selectedSetting) {
case 'Engines':
Expand Down
5 changes: 3 additions & 2 deletions web/screens/Settings/SettingLeftPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import { useAtomValue } from 'jotai'

import LeftPanelContainer from '@/containers/LeftPanelContainer'

import { useGetEngines } from '@/hooks/useEngineManagement'

import { getTitleByEngine, isLocalEngine } from '@/utils/modelEngine'

import SettingItem from './SettingItem'

import { extensionManager } from '@/extension'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'

import {
showSettingActiveLocalEngineAtom,
Expand All @@ -20,7 +21,7 @@ import {
import { janSettingScreenAtom } from '@/helpers/atoms/Setting.atom'

const SettingLeftPanel = () => {
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()
const settingScreens = useAtomValue(janSettingScreenAtom)

const showSettingActiveLocalEngine = useAtomValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import LogoMark from '@/containers/Brand/Logo/Mark'

import { MainViewState } from '@/constants/screens'

import { useGetEngines } from '@/hooks/useEngineManagement'

import { isLocalEngine } from '@/utils/modelEngine'

import { mainViewStateAtom } from '@/helpers/atoms/App.atom'
import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'

const EmptyThread = () => {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const setMainViewState = useSetAtom(mainViewStateAtom)
const engines = useAtomValue(installedEnginesAtom)
const { engines } = useGetEngines()
const showOnboardingStep = useMemo(
() =>
!downloadedModels.some(
Expand Down
Loading

0 comments on commit a8cac58

Please sign in to comment.