Skip to content

Commit

Permalink
chore: update engines list on changes
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-jan committed Jan 8, 2025
1 parent 2e2ca6c commit 947488d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 2 additions & 0 deletions extensions/engine-management-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
joinPath,
events,
ModelEvent,
EngineEvent,
} from '@janhq/core'
import ky, { HTTPError } from 'ky'
import PQueue from 'p-queue'
Expand Down Expand Up @@ -295,6 +296,7 @@ export default class JSONEngineManagementExtension extends EngineManagementExten
await this.addRemoteEngine(data).catch(console.error)
})
)
events.emit(EngineEvent.OnEngineUpdate, {})
DEFAULT_REMOTE_MODELS.forEach(async (data: Model) => {
await this.addRemoteModel(data).catch(() => {})
})
Expand Down
4 changes: 3 additions & 1 deletion web/containers/ModelDropdown/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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 @@ -85,7 +86,8 @@ const ModelDropdown = ({
const [dropdownOptions, setDropdownOptions] = useState<HTMLDivElement | null>(
null
)
const { engines } = useGetEngines()

const engines = useAtomValue(installedEnginesAtom)

const downloadStates = useAtomValue(modelDownloadStateAtom)
const setThreadModelParams = useSetAtom(setThreadModelParamsAtom)
Expand Down
3 changes: 3 additions & 0 deletions web/containers/Providers/DataLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { AppConfiguration, getUserHomePath } from '@janhq/core'
import { useSetAtom } from 'jotai'

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

useThreads()
useAssistants()
Expand All @@ -33,6 +35,7 @@ const DataLoader: React.FC = () => {
useEffect(() => {
// Load data once
loadModels()
loadEngines()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

Expand Down
7 changes: 7 additions & 0 deletions web/helpers/atoms/Engines.atom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { EngineConfig, Engines } from '@janhq/core'

Check warning on line 1 in web/helpers/atoms/Engines.atom.ts

View workflow job for this annotation

GitHub Actions / test-on-macos

'EngineConfig' is defined but never used

Check warning on line 1 in web/helpers/atoms/Engines.atom.ts

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'EngineConfig' is defined but never used

Check warning on line 1 in web/helpers/atoms/Engines.atom.ts

View workflow job for this annotation

GitHub Actions / coverage-check

'EngineConfig' is defined but never used

Check warning on line 1 in web/helpers/atoms/Engines.atom.ts

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'EngineConfig' is defined but never used
import { atom } from 'jotai'

/**
* Store all of the installed engines including local and remote engines
*/
export const installedEnginesAtom = atom<Engines>()
57 changes: 57 additions & 0 deletions web/hooks/useEngines.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { useCallback, useEffect } from 'react'

import {
ExtensionTypeEnum,
Model,

Check warning on line 5 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-macos

'Model' is defined but never used

Check warning on line 5 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'Model' is defined but never used

Check warning on line 5 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / coverage-check

'Model' is defined but never used

Check warning on line 5 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'Model' is defined but never used
ModelEvent,

Check warning on line 6 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-macos

'ModelEvent' is defined but never used

Check warning on line 6 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'ModelEvent' is defined but never used

Check warning on line 6 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / coverage-check

'ModelEvent' is defined but never used

Check warning on line 6 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'ModelEvent' is defined but never used
ModelExtension,

Check warning on line 7 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-macos

'ModelExtension' is defined but never used

Check warning on line 7 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'ModelExtension' is defined but never used

Check warning on line 7 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / coverage-check

'ModelExtension' is defined but never used

Check warning on line 7 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'ModelExtension' is defined but never used
events,
ModelManager,

Check warning on line 9 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-macos

'ModelManager' is defined but never used

Check warning on line 9 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'ModelManager' is defined but never used

Check warning on line 9 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / coverage-check

'ModelManager' is defined but never used

Check warning on line 9 in web/hooks/useEngines.ts

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'ModelManager' is defined but never used
EngineEvent,
EngineManagementExtension,
Engines,
} from '@janhq/core'

import { useSetAtom } from 'jotai'

import { useDebouncedCallback } from 'use-debounce'

import { extensionManager } from '@/extension'

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

/**
* useModels hook - Handles the state of models
* It fetches the downloaded models, configured models and default model from Model Extension
* and updates the atoms accordingly.
*/
const useEngines = () => {
const setInstalledEngines = useSetAtom(installedEnginesAtom)

const getData = useCallback(() => {
getEngines().then(setInstalledEngines)
}, [setInstalledEngines])

const reloadData = useDebouncedCallback(() => getData(), 300)

const getEngines = async (): Promise<Engines> =>
extensionManager
.get<EngineManagementExtension>(ExtensionTypeEnum.Engine)
?.getEngines()
.catch(() => ({}) as Engines) ?? ({} as Engines)

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

return {
getData,
}
}

export default useEngines
6 changes: 3 additions & 3 deletions web/hooks/useStarterScreen.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { useMemo } from 'react'

import { InferenceEngine, EngineConfig } from '@janhq/core'
import { useAtomValue } from 'jotai'

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

import { installedEnginesAtom } from '@/helpers/atoms/Engines.atom'
import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom'
import { threadsAtom } from '@/helpers/atoms/Thread.atom'
import { InferenceEngine, EngineConfig } from '@janhq/core/.'
import { useGetEngines } from './useEngineManagement'

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

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

const remoteEngines =
engines &&
Expand Down
6 changes: 3 additions & 3 deletions web/screens/Settings/SettingLeftPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ 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,
showSettingActiveRemoteEngineAtom,
} from '@/helpers/atoms/Extension.atom'
import { janSettingScreenAtom } from '@/helpers/atoms/Setting.atom'

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

const showSettingActiveLocalEngine = useAtomValue(
Expand Down

0 comments on commit 947488d

Please sign in to comment.