Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactor - clean out useEngines - mutate swr to update #4476

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 Expand Up @@ -49,9 +50,9 @@
: 'primary'
}
onClick={() => {
stopModel()
window.core?.api?.stopServer()
setServerEnabled(false)

Check warning on line 55 in web/containers/Layout/BottomPanel/SystemMonitor/TableActiveModel/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

53-55 lines are not covered with tests
}}
>
Stop
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 { 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 { 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 @@
null
)

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

const downloadStates = useAtomValue(modelDownloadStateAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
Expand Down Expand Up @@ -123,8 +123,8 @@

const handleChangeStateOpen = useCallback(
(state: boolean) => {
setOpen(state)
setModelDropdownState(state)

Check warning on line 127 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

126-127 lines are not covered with tests
},
[setModelDropdownState]
)
Expand All @@ -134,7 +134,7 @@
configuredModels
.concat(
downloadedModels.filter(
(e) => !configuredModels.some((x) => x.id === e.id)

Check warning on line 137 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

137 line is not covered with tests
)
)
.filter((e) =>
Expand All @@ -143,26 +143,26 @@
.filter((e) => {
if (searchFilter === 'local') {
return (
engineList.find((t) => t.engine?.engine === e.engine)?.type ===

Check warning on line 146 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

146 line is not covered with tests
'local'
)
}
return true

Check warning on line 150 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

150 line is not covered with tests
})
.sort((a, b) => a.name.localeCompare(b.name))

Check warning on line 152 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

152 line is not covered with tests
.sort((a, b) => {
const aInDownloadedModels = downloadedModels.some(
(item) => item.id === a.id

Check warning on line 155 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

154-155 lines are not covered with tests
)
const bInDownloadedModels = downloadedModels.some(
(item) => item.id === b.id

Check warning on line 158 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

157-158 lines are not covered with tests
)
if (aInDownloadedModels && !bInDownloadedModels) {
return -1
} else if (!aInDownloadedModels && bInDownloadedModels) {
return 1

Check warning on line 163 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

160-163 lines are not covered with tests
} else {
return 0

Check warning on line 165 in web/containers/ModelDropdown/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

165 line is not covered with tests
}
}),
[configuredModels, searchText, searchFilter, downloadedModels, engineList]
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 @@ -2,18 +2,18 @@

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 @@ -2,7 +2,7 @@

import { Model } from '@janhq/core'
import { Badge, Button, Tooltip, useClickOutside } from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai'

Check warning on line 5 in web/screens/Settings/MyModels/MyModelList/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/MyModels/MyModelList/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/MyModels/MyModelList/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/MyModels/MyModelList/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'useAtomValue' is defined but never used
import {
MoreVerticalIcon,
PlayIcon,
Expand All @@ -14,11 +14,12 @@
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 { 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
Loading