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: add caching for engines to improve load time #4481

Merged
merged 2 commits into from
Jan 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@
<div className="w-1/2">
<div className="overflow-hidden border-b border-[hsla(var(--app-border))]">
<table className="w-full px-8">
{activeModel &&
engines &&
isLocalEngine(engines, activeModel.engine) ? (
{activeModel && isLocalEngine(engines, activeModel.engine) ? (
<tbody>
<tr>
<td
Expand All @@ -50,9 +48,9 @@
: 'primary'
}
onClick={() => {
stopModel()
window.core?.api?.stopServer()
setServerEnabled(false)

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

View workflow job for this annotation

GitHub Actions / coverage-check

51-53 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 @@ -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 Expand Up @@ -196,8 +196,8 @@
const model = downloadedModels.find((model) => model.id === modelId)
if (model) {
if (
engines?.[model.engine]?.[0].type === 'local' ||
(engines?.[model.engine]?.[0].api_key?.length ?? 0) > 0
engines?.[model.engine]?.[0]?.type === 'local' ||
(engines?.[model.engine]?.[0]?.api_key?.length ?? 0) > 0
)
setSelectedModel(model)
} else {
Expand Down
30 changes: 30 additions & 0 deletions web/containers/Providers/SWRConfigProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import * as React from 'react'

import { SWRConfig } from 'swr'

function SWRConfigProvider({ children }: { children: React.ReactNode }) {
// https://swr.vercel.app/docs/advanced/cache#localstorage-based-persistent-cache
// When initializing, we restore the data from `localStorage` into a map.

const map = React.useMemo(() => new Map<string, object>(), [])
React.useEffect(() => {
const savedCache = JSON.parse(
window.localStorage.getItem('app-cache') || '[]'
)
savedCache.forEach(([key, value]: [string, object]) => {
map.set(key, value)
})

// Before unloading the app, we write back all the data into `localStorage`.
window.addEventListener('beforeunload', () => {
const appCache = JSON.stringify(Array.from(map.entries()))
window.localStorage.setItem('app-cache', appCache)
})
}, [map])

return <SWRConfig value={{ provider: () => map }}>{children}</SWRConfig>
}

export default SWRConfigProvider
39 changes: 22 additions & 17 deletions web/containers/Providers/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import { Toaster } from 'react-hot-toast'

import { SWRConfig } from 'swr'

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-ubuntu

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-macos

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / test-on-windows-pr

'SWRConfig' is defined but never used

Check warning on line 7 in web/containers/Providers/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

'SWRConfig' is defined but never used

import EventListener from '@/containers/Providers/EventListener'
import JotaiWrapper from '@/containers/Providers/Jotai'

Expand All @@ -18,27 +20,30 @@
import KeyListener from './KeyListener'
import Responsive from './Responsive'

import SWRConfigProvider from './SWRConfigProvider'
import SettingsHandler from './SettingsHandler'

const Providers = ({ children }: PropsWithChildren) => {
return (
<ThemeWrapper>
<JotaiWrapper>
<Umami />
<CoreConfigurator>
<>
<Responsive />
<KeyListener />
<EventListener />
<DataLoader />
<SettingsHandler />
<DeepLinkListener />
<Toaster />
{children}
</>
</CoreConfigurator>
</JotaiWrapper>
</ThemeWrapper>
<SWRConfigProvider>
<ThemeWrapper>
<JotaiWrapper>
<Umami />
<CoreConfigurator>
<>
<Responsive />
<KeyListener />
<EventListener />
<DataLoader />
<SettingsHandler />
<DeepLinkListener />
<Toaster />
{children}
</>
</CoreConfigurator>
</JotaiWrapper>
</ThemeWrapper>
</SWRConfigProvider>
)
}

Expand Down
2 changes: 1 addition & 1 deletion web/screens/Settings/MyModels/MyModelList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { memo, useState } from 'react'

import { Model } from '@janhq/core'
import { Badge, Button, Tooltip, useClickOutside } from '@janhq/joi'
import { useAtom, useAtomValue } from 'jotai'
import { useAtom } from 'jotai'
import {
MoreVerticalIcon,
PlayIcon,
Expand Down
Loading