Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix: model download state update (#6882)
Louisb266f5e0e799
diff --git a/web-app/package.json b/web-app/package.jsonindex f34e6d75b..47bfaffb5 100644--- a/web-app/package.json+++ b/web-app/package.json@@ -82,7 +82,7 @@"remark-math": "6.0.0","sonner": "2.0.5","tailwindcss": "4.1.4",- "token.js": "npm:[email protected]",+ "token.js": "npm:[email protected]","tw-animate-css": "1.2.8","ulidx": "2.4.1","unified": "11.0.5",diff --git a/web-app/src/containers/DownloadButton.tsx b/web-app/src/containers/DownloadButton.tsxindex 7d4db703b..9e1912f69 100644--- a/web-app/src/containers/DownloadButton.tsx+++ b/web-app/src/containers/DownloadButton.tsx@@ -8,7 +8,8 @@ import { useTranslation } from '@/i18n'import { extractModelName } from '@/lib/models'import { cn, sanitizeModelId } from '@/lib/utils'import { CatalogModel } from '@/services/models/types'-import { useCallback, useMemo } from 'react'+import { DownloadEvent, DownloadState, events } from '@janhq/core'+import { useCallback, useEffect, useMemo, useState } from 'react'import { useShallow } from 'zustand/shallow'type ModelProps = {@@ -35,6 +36,7 @@ export function DownloadButtonPlaceholder({const serviceHub = useServiceHub()const huggingfaceToken = useGeneralSetting((state) => state.huggingfaceToken)+ const [isDownloaded, setDownloaded] = useState<boolean>(false)const quant =model.quants.find((e) =>@@ -57,6 +59,26 @@ export function DownloadButtonPlaceholder({[downloads])+ useEffect(() => {+ const isDownloaded = llamaProvider?.models.some(+ (m: { id: string }) =>+ m.id === modelId ||+ m.id === `${model.developer}/${sanitizeModelId(modelId)}`+ )+ if (isDownloaded) {+ setDownloaded(true)+ }+ }, [llamaProvider])++ useEffect(() => {+ events.on(+ DownloadEvent.onFileDownloadAndVerificationSuccess,+ (state: DownloadState) => {+ if (state.modelId === modelId) setDownloaded(true)+ }+ )+ }, [])+const isRecommendedModel = useCallback((modelId: string) => {return (extractModelName(modelId)?.toLowerCase() ==='jan-nano-gguf') as boolean@@ -84,11 +106,7 @@ export function DownloadButtonPlaceholder({const downloadProgress =downloadProcesses.find((e) => e.id === modelId)?.progress || 0- const isDownloaded = llamaProvider?.models.some(- (m: { id: string }) =>- m.id === modelId ||- m.id === `${model.developer}/${sanitizeModelId(modelId)}`- )+const isRecommended = isRecommendedModel(model.model_name)const handleDownload = () => {diff --git a/web-app/src/containers/ModelDownloadAction.tsx b/web-app/src/containers/ModelDownloadAction.tsxnew file mode 100644index 000000000..f74bc4aa6--- /dev/null+++ b/web-app/src/containers/ModelDownloadAction.tsx@@ -0,0 +1,120 @@+import { Button } from '@/components/ui/button'+import { Progress } from '@/components/ui/progress'+import { route } from '@/constants/routes'+import { useDownloadStore } from '@/hooks/useDownloadStore'+import { useGeneralSetting } from '@/hooks/useGeneralSetting'+import { useModelProvider } from '@/hooks/useModelProvider'+import { useServiceHub } from '@/hooks/useServiceHub'+import { useTranslation } from '@/i18n'+import { CatalogModel } from '@/services/models/types'+import { IconDownload } from '@tabler/icons-react'+import { useNavigate } from '@tanstack/react-router'+import { useCallback, useMemo } from 'react'++export const ModelDownloadAction = ({+ variant,+ model,+}: {+ variant: { model_id: string; path: string }+ model: CatalogModel+}) => {+ const serviceHub = useServiceHub()++ const { t } = useTranslation()+ const huggingfaceToken = useGeneralSetting((state) => state.huggingfaceToken)+ const { downloads, localDownloadingModels, addLocalDownloadingModel } =+ useDownloadStore()+ const downloadProcesses = useMemo(+ () =>+ Object.values(downloads).map((download) => ({+ id: download.name,+ name: download.name,+ progress: download.progress,+ current: download.current,+ total: download.total,+ })),+ [downloads]+ )++ const navigate = useNavigate()++ const handleUseModel = useCallback(+ (modelId: string) => {+ navigate({+ to: route.home,+ params: {},+ search: {+ model: {+ id: modelId,+ provider: 'llamacpp',+ },+ },+ })+ },+ [navigate]+ )++ const isDownloading =+ localDownloadingModels.has(variant.model_id) ||+ downloadProcesses.some((e) => e.id === variant.model_id)+ const downloadProgress =+ downloadProcesses.find((e) => e.id === variant.model_id)?.progress || 0+ const isDownloaded = useModelProvider+ .getState()+ .getProviderByName('llamacpp')+ ?.models.some((m: { id: string }) => m.id === variant.model_id)++ if (isDownloading) {+ return (+ <>+ <div className="flex items-center gap-2 w-20">+ <Progress value={downloadProgress * 100} />+ <span className="text-xs text-center text-main-view-fg/70">+ {Math.round(downloadProgress * 100)}%+ </span>+ </div>+ </>+ )+ }++ if (isDownloaded) {+ return (+ <div+ className="flex items-center justify-center rounded bg-main-view-fg/10"+ title={t('hub:useModel')}+ >+ <Button+ variant="link"+ size="sm"+ onClick={() => handleUseModel(variant.model_id)}+ >+ {t('hub:use')}+ </Button>+ </div>+ )+ }++ return (+ <div+ className="size-6 cursor-pointer flex items-center justify-center rounded hover:bg-main-view-fg/10 transition-all duration-200 ease-in-out"+ title={t('hub:downloadModel')}+ onClick={() => {+ addLocalDownloadingModel(variant.model_id)+ serviceHub+ .models()+ .pullModelWithMetadata(+ variant.model_id,+ variant.path,+ (+ model.mmproj_models?.find(+ (e) => e.model_id.toLowerCase() === 'mmproj-f16'+ ) || model.mmproj_models?.[0]+ )?.path,+ huggingfaceToken+ )+ }}+ >+ <IconDownload size={16} className="text-main-view-fg/80" />+ </div>+ )+}diff --git a/web-app/src/hooks/useModelProvider.ts b/web-app/src/hooks/useModelProvider.tsindex 7909b1fe7..f9bf8ded2 100644--- a/web-app/src/hooks/useModelProvider.ts+++ b/web-app/src/hooks/useModelProvider.ts@@ -444,9 +444,14 @@ export const useModelProvider = create<ModelProviderState>()(}})}+ if (version <= 6 && state?.providers) {+ // Most of legacy cohere models are deprecated now, remove the provider entirely+ // TODO: Default providers should be updated automatically somehow later+ state.providers = state.providers.filter((provider) => provider.provider !== 'cohere')+ }return state},- version: 6,+ version: 7,}))diff --git a/web-app/src/routes/hub/index.tsx b/web-app/src/routes/hub/index.tsxindex be63c49b6..1fddee787 100644--- a/web-app/src/routes/hub/index.tsx+++ b/web-app/src/routes/hub/index.tsx@@ -14,7 +14,6 @@ import {useCallback,useRef,} from 'react'-import { Button } from '@/components/ui/button'import { useModelProvider } from '@/hooks/useModelProvider'import { Card, CardItem } from '@/containers/Card'import { RenderMarkdown } from '@/containers/RenderMarkdown'@@ -42,8 +41,6 @@ import {} from '@/components/ui/dropdown-menu'import { useServiceHub } from '@/hooks/useServiceHub'import type { CatalogModel } from '@/services/models/types'-import { useDownloadStore } from '@/hooks/useDownloadStore'-import { Progress } from '@/components/ui/progress'import HeaderPage from '@/containers/HeaderPage'import { Loader } from 'lucide-react'import { useTranslation } from '@/i18n/react-i18next-compat'@@ -51,6 +48,7 @@ import Fuse from 'fuse.js'import { useGeneralSetting } from '@/hooks/useGeneralSetting'import { DownloadButtonPlaceholder } from '@/containers/DownloadButton'import { useShallow } from 'zustand/shallow'+import { ModelDownloadAction } from '@/containers/ModelDownloadAction'type SearchParams = {repo: string@@ -247,21 +245,6 @@ function HubContent() {}}- const { downloads, localDownloadingModels, addLocalDownloadingModel } =- useDownloadStore()-- const downloadProcesses = useMemo(- () =>- Object.values(downloads).map((download) => ({- id: download.name,- name: download.name,- progress: download.progress,- current: download.current,- total: download.total,- })),- [downloads]- )-const navigate = useNavigate()const isRecommendedModel = useCallback((modelId: string) => {@@ -702,105 +685,12 @@ function HubContent() {checkModelSupport}/>- {(() => {- const isDownloading =- localDownloadingModels.has(- variant.model_id- ) ||- downloadProcesses.some(- (e) => e.id === variant.model_id- )- const downloadProgress =- downloadProcesses.find(- (e) => e.id === variant.model_id- )?.progress || 0- const isDownloaded =- useModelProvider- .getState()- .getProviderByName('llamacpp')- ?.models.some(- (m: { id: string }) =>- m.id === variant.model_id- )-- if (isDownloading) {- return (- <>- <div className="flex items-center gap-2 w-20">- <Progress- value={- downloadProgress * 100- }- />- <span className="text-xs text-center text-main-view-fg/70">- {Math.round(- downloadProgress * 100- )}- %- </span>- </div>- </>- )- }-- if (isDownloaded) {- return (- <div- className="flex items-center justify-center rounded bg-main-view-fg/10"- title={t('hub:useModel')}- >- <Button- variant="link"- size="sm"- onClick={() =>- handleUseModel(- variant.model_id- )- }- >- {t('hub:use')}- </Button>- </div>- )+ <ModelDownloadAction+ variant={variant}+ model={+ filteredModels[virtualItem.index]}-- return (- <div- className="size-6 cursor-pointer flex items-center justify-center rounded hover:bg-main-view-fg/10 transition-all duration-200 ease-in-out"- title={t('hub:downloadModel')}- onClick={() => {- addLocalDownloadingModel(- variant.model_id- )- serviceHub- .models()- .pullModelWithMetadata(- variant.model_id,- variant.path,-- (- filteredModels[- virtualItem.index- ].mmproj_models?.find(- (e) =>- e.model_id.toLowerCase() ===- 'mmproj-f16'- ) ||- filteredModels[- virtualItem.index- ].mmproj_models?.[0]- )?.path,- huggingfaceToken- )- }}- >- <IconDownload- size={16}- className="text-main-view-fg/80"- />- </div>- )- })()}+ /></div>}/>diff --git a/web-app/src/services/providers/tauri.ts b/web-app/src/services/providers/tauri.tsindex 4e08ee751..d5c171fce 100644--- a/web-app/src/services/providers/tauri.ts+++ b/web-app/src/services/providers/tauri.ts@@ -22,6 +22,7 @@ export class TauriProvidersService extends DefaultProvidersService {try {const builtinProviders = predefinedProviders.map((provider) => {let models = provider.models as Model[]+ if (!models.length) return undefinedif (Object.keys(providerModels).includes(provider.provider)) {const builtInModels = providerModels[provider.provider as unknown as keyof typeof providerModels@@ -43,7 +44,7 @@ export class TauriProvidersService extends DefaultProvidersService {...provider,models,}- })+ }).filter(Boolean)const runtimeProviders: ModelProvider[] = []for (const [providerName, value] of EngineManager.instance().engines) {