Line data Source code
1 : import type { ModelWithUrl } from '@/platform/workflowErrors/missingModel/missingModelDownload'
2 : import type {
3 : TemplateModelMetadataBatchResult,
4 : TemplateModelMetadataEntry
5 : } from '@/platform/workflow/templates/utils/templateModelMetadata'
6 : import type { ResolvedTemplateModelAvailability } from '@/platform/workflow/templates/utils/templateModelAvailability'
7 : import type { TemplateModelRequirementDetail } from '@/platform/workflow/templates/utils/templateModelRequirements'
8 : import type { ModelFile } from '@comfyorg/graph-model/workflowSchema'
9 : import { getModelFileKey } from '@/platform/workflow/core/utils/modelRequirements'
10 :
11 : type TemplateModelSetupStatus =
12 : | 'installed'
13 : | 'downloadable'
14 : | 'manual'
15 : | 'unavailable'
16 : | 'unknown'
17 :
18 : type TemplateModelSetupRowBase = {
19 : model: ModelFile
20 : usedBy: readonly string[]
21 : fileSize: number | null
22 : /** Unformatted; the view labels it. */
23 : modelDirectory: string
24 : }
25 :
26 : export type TemplateModelSetupRow =
27 : | (TemplateModelSetupRowBase & {
28 : status: 'manual'
29 : href: string
30 : })
31 : | (TemplateModelSetupRowBase & {
32 : status: Exclude<TemplateModelSetupStatus, 'manual'>
33 : })
34 :
35 : type TemplateModelDeclarationTotal = {
36 : bytes: number
37 : isComplete: boolean
38 : }
39 :
40 : export type TemplateModelSetupResult = {
41 : rows: readonly TemplateModelSetupRow[]
42 : declarationTotal: TemplateModelDeclarationTotal
43 : }
44 :
45 : type TemplateModelSetupOptions = {
46 : isDownloadable: (model: ModelWithUrl) => boolean
47 : }
48 :
49 0 : function indexByIdentity<T extends { model: ModelWithUrl }>(
50 : entries: readonly T[]
51 : ): Map<string, T> {
52 0 : const indexed = new Map<string, T>()
53 0 : for (const entry of entries) {
54 0 : const identity = getModelFileKey(entry.model)
55 0 : if (!indexed.has(identity)) indexed.set(identity, entry)
56 : }
57 0 : return indexed
58 : }
59 :
60 0 : function normalizeFileSize(fileSize: number | null | undefined): number | null {
61 0 : return typeof fileSize === 'number' &&
62 : Number.isFinite(fileSize) &&
63 : fileSize >= 0
64 : ? fileSize
65 : : null
66 : }
67 :
68 0 : function deriveRow(
69 : { model, usedBy }: TemplateModelRequirementDetail,
70 : availability: ResolvedTemplateModelAvailability | undefined,
71 : metadata: TemplateModelMetadataEntry | undefined,
72 : isDownloadable: (model: ModelWithUrl) => boolean
73 : ): TemplateModelSetupRow {
74 0 : const row = {
75 : model,
76 : usedBy,
77 : fileSize: normalizeFileSize(metadata?.fileSize),
78 : modelDirectory: model.directory.trim()
79 : }
80 :
81 0 : if (availability?.status === 'installed') {
82 0 : return { ...row, status: 'installed' }
83 : }
84 0 : if (availability?.status !== 'missing') {
85 0 : return { ...row, status: 'unknown' }
86 : }
87 0 : if (!metadata) {
88 0 : return { ...row, status: 'unknown' }
89 : }
90 0 : const gatedRepoUrl = metadata.gatedRepoUrl?.trim()
91 0 : if (gatedRepoUrl) {
92 0 : return { ...row, status: 'manual', href: gatedRepoUrl }
93 : }
94 0 : return {
95 : ...row,
96 : status: isDownloadable(model) ? 'downloadable' : 'unavailable'
97 : }
98 : }
99 :
100 0 : function totalDeclarations(
101 : rows: readonly TemplateModelSetupRow[],
102 : sizeOf: (row: TemplateModelSetupRow) => number | null = (row) => row.fileSize
103 : ): TemplateModelDeclarationTotal {
104 0 : const seen = new Set<string>()
105 0 : let bytes = 0
106 0 : let isComplete = true
107 :
108 0 : for (const row of rows) {
109 0 : const identity = getModelFileKey(row.model)
110 0 : if (seen.has(identity)) continue
111 :
112 0 : seen.add(identity)
113 0 : const size = sizeOf(row)
114 0 : if (size === null) {
115 0 : isComplete = false
116 : } else {
117 0 : bytes += size
118 : }
119 : }
120 :
121 0 : return { bytes, isComplete }
122 : }
123 :
124 : type RowDownloadStatus = {
125 : status: string
126 : activity?: string
127 : receivedBytes?: number | null
128 : totalBytes?: number | null
129 : fraction?: number | null
130 : }
131 :
132 : type RowDownloads = {
133 : stateFor: (model: ModelWithUrl) => RowDownloadStatus
134 : canResume: (model: ModelWithUrl) => boolean
135 : }
136 :
137 : /** A row the bulk action would actually start or resume. */
138 0 : export function isModelDownloadCandidate(
139 : row: TemplateModelSetupRow,
140 : { stateFor, canResume }: RowDownloads
141 : ): boolean {
142 0 : if (row.status !== 'downloadable') return false
143 0 : const state = stateFor(row.model)
144 0 : if (state.status === 'downloading' && state.activity === 'paused')
145 0 : return canResume(row.model)
146 0 : return state.status === 'idle' || state.status === 'failed'
147 : }
148 :
149 : /** Nothing left to satisfy: already on disk, or downloaded in this session. */
150 0 : export function isModelRowComplete(
151 : row: TemplateModelSetupRow,
152 : stateFor: (model: ModelWithUrl) => RowDownloadStatus
153 : ): boolean {
154 0 : if (row.status === 'installed') return true
155 0 : return stateFor(row.model).status === 'done'
156 : }
157 :
158 : /**
159 : * What a bulk click would transfer. Shares `totalDeclarations`' identity dedupe
160 : * so this and the group header cannot count the same model differently.
161 : */
162 0 : export function remainingModelDownloadTotal(
163 : rows: readonly TemplateModelSetupRow[],
164 : downloads: RowDownloads
165 : ): TemplateModelDeclarationTotal {
166 0 : return totalDeclarations(
167 : rows.filter((row) => isModelDownloadCandidate(row, downloads)),
168 : (row) => bytesLeft(row, downloads.stateFor(row.model))
169 : )
170 : }
171 :
172 0 : function bytesLeft(
173 : row: TemplateModelSetupRow,
174 : { receivedBytes, totalBytes, fraction }: RowDownloadStatus
175 : ): number | null {
176 0 : const received = receivedBytes ?? 0
177 0 : const size = totalBytes && totalBytes >= received ? totalBytes : row.fileSize
178 0 : if (size === null || received > size) return null
179 0 : const done = receivedBytes ?? (fraction ?? 0) * size
180 0 : return Math.ceil(size - done)
181 : }
182 :
183 0 : export function deriveTemplateModelSetup(
184 : requirements: readonly TemplateModelRequirementDetail[],
185 : availability: readonly ResolvedTemplateModelAvailability[],
186 : metadata: TemplateModelMetadataBatchResult,
187 : { isDownloadable }: TemplateModelSetupOptions
188 : ): TemplateModelSetupResult {
189 0 : const availabilityByIdentity = indexByIdentity(availability)
190 0 : const metadataByIdentity = indexByIdentity(
191 : metadata.status === 'completed' ? metadata.entries : []
192 : )
193 0 : const rows = requirements.map((requirement) => {
194 0 : const identity = getModelFileKey(requirement.model)
195 0 : return deriveRow(
196 : requirement,
197 : availabilityByIdentity.get(identity),
198 : metadataByIdentity.get(identity),
199 : isDownloadable
200 : )
201 : })
202 :
203 0 : return {
204 : rows,
205 : declarationTotal: totalDeclarations(rows)
206 : }
207 : }
|