Line data Source code
1 : import { computed, toValue } from 'vue'
2 : import type { MaybeRefOrGetter } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { isCloud } from '@/platform/distribution/types'
6 : import { useMissingModelDownload } from '@/platform/workflowErrors/missingModel/composables/useMissingModelDownload'
7 : import { isTrustedHuggingFaceUrl } from '@/platform/workflowErrors/missingModel/missingModelDownload'
8 : import { getDownloadableModels } from '@/platform/workflowErrors/missingModel/missingModelViewUtils'
9 : import type { MissingModelGroup } from '@/platform/workflowErrors/missingModel/types'
10 : import { formatSize } from '@/utils/formatUtil'
11 :
12 270 : export function useDownloadAllMissingModels(
13 : groups: MaybeRefOrGetter<MissingModelGroup[]>
14 : ) {
15 270 : const { t } = useI18n()
16 270 : const { downloadMissingModel, fileSizeFor, gatedRepoUrlFor } =
17 : useMissingModelDownload()
18 :
19 270 : const downloadableModels = computed(() =>
20 : isCloud ? [] : getDownloadableModels(toValue(groups))
21 : )
22 :
23 270 : const gatedModelCount = computed(
24 : () =>
25 : downloadableModels.value.filter((model) => {
26 191 : const repoUrl = gatedRepoUrlFor(model.url)
27 191 : return !!repoUrl && isTrustedHuggingFaceUrl(repoUrl)
28 : }).length
29 : )
30 :
31 270 : const downloadAllLabel = computed(() => {
32 34 : const base = t('rightSidePanel.missingModels.downloadAll')
33 34 : const sizes = downloadableModels.value.map((model) =>
34 : fileSizeFor(model.url)
35 : )
36 34 : if (!sizes.every((size): size is number => size !== undefined)) return base
37 28 : const total = sizes.reduce((sum, size) => sum + size, 0)
38 34 : return total > 0 ? `${base} (${formatSize(total)})` : base
39 : })
40 :
41 0 : function downloadAllModels() {
42 0 : for (const model of downloadableModels.value) {
43 0 : downloadMissingModel(model)
44 : }
45 : }
46 :
47 270 : return {
48 : downloadableModels,
49 : gatedModelCount,
50 : downloadAllLabel,
51 : downloadAllModels
52 : }
53 : }
|