import { Fragment, useCallback, useEffect, useMemo, useState } from 'react' import { Button, Progress, Select } from '@janhq/joi' import { useAtomValue, useSetAtom } from 'jotai' import Spinner from '@/containers/Loader/Spinner' import { toaster } from '@/containers/Toast' import useAbortDownload from '@/hooks/useAbortDownload' import useAssistantQuery from '@/hooks/useAssistantQuery' import useCortex from '@/hooks/useCortex' import { addDownloadModelStateAtom, downloadStateListAtom, } from '@/hooks/useDownloadState' import useEngineQuery from '@/hooks/useEngineQuery' import useHfEngineToBranchesQuery from '@/hooks/useHfEngineToBranchesQuery' import useThreads from '@/hooks/useThreads' import { formatDownloadPercentage, toGibibytes } from '@/utils/converter' import { downloadProgress } from '@/utils/download' import { CortexHubModel, EngineType } from '@/utils/huggingface' import { MainViewState, mainViewStateAtom } from '@/helpers/atoms/App.atom' import { localModelModalStageAtom } from '@/helpers/atoms/DownloadLocalModel.atom' import { downloadedModelsAtom } from '@/helpers/atoms/Model.atom' type Props = { modelHandle: string onBranchSelected?: (availableSelections: string[]) => void } const ListModel: React.FC = ({ modelHandle, onBranchSelected }) => { const { data: engineData } = useEngineQuery() const { data, isLoading } = useHfEngineToBranchesQuery(modelHandle) const [engineFilter, setEngineFilter] = useState( undefined ) const engineSelections: { name: string; value: string }[] = useMemo(() => { if (!data || !engineData) return [] const isSupportTensorRt = engineData.find((engine) => engine.name === 'cortex.tensorrt-llm') ?.status !== 'not_supported' ?? false const isSupportOnnx = engineData.find((engine) => engine.name === 'cortex.onnx')?.status !== 'not_supported' ?? false const result: { name: string; value: string }[] = [] if (data.gguf.length > 0) result.push({ name: 'GGUF', value: 'gguf' }) if (isSupportOnnx && data.onnx.length > 0) result.push({ name: 'ONNX', value: 'onnx' }) if (isSupportTensorRt && data.tensorrtllm.length > 0) result.push({ name: 'TensorRT', value: 'tensorrtllm' }) return result }, [data, engineData]) const modelBranches: CortexHubModel[] = useMemo((): CortexHubModel[] => { if (!data) return [] return (data[engineFilter as EngineType] as CortexHubModel[]) ?? [] }, [data, engineFilter]) useEffect(() => { if (engineSelections.length === 0) return setEngineFilter(engineSelections[0].value as EngineType) }, [engineSelections]) useEffect(() => { const models = modelBranches.map((m) => m.name) onBranchSelected?.(models) }, [modelBranches, onBranchSelected]) const onSelectionChanged = useCallback( (selectionValue: string) => { setEngineFilter(selectionValue as EngineType) const models = modelBranches.map((m) => m.name) onBranchSelected?.(models) }, [setEngineFilter, onBranchSelected, modelBranches] ) if (isLoading) return (
) return (
Format: