Line data Source code
1 : import type { ComfyDownloadProgress } from '@comfyorg/comfyui-desktop-bridge-types'
2 : import { reportError } from '@comfyorg/telemetry/reportError'
3 : import { defineStore } from 'pinia'
4 : import { ref, shallowReactive, watch } from 'vue'
5 :
6 : import { getModelFileKey } from '@/platform/workflow/core/utils/modelRequirements'
7 : import {
8 : dispatchModelDownload,
9 : downloadModel,
10 : hostReportsModelDownloadProgress
11 : } from '@/platform/workflowErrors/missingModel/missingModelDownload'
12 : import type { ModelWithUrl } from '@/platform/workflowErrors/missingModel/missingModelDownload'
13 : import { useMissingModelStore } from '@/platform/workflowErrors/missingModel/missingModelStore'
14 : import type { MissingModelCandidate } from '@/platform/workflowErrors/missingModel/types'
15 :
16 : export type MissingModelDownloadState =
17 : | { status: 'idle' }
18 : | { status: 'requested' }
19 : | { status: 'pending' }
20 : | {
21 : status: 'downloading'
22 : paused: boolean
23 : receivedBytes: number | null
24 : totalBytes: number | null
25 : fraction: number | null
26 : }
27 : | { status: 'done' }
28 : | { status: 'failed' }
29 :
30 : interface HostDownload {
31 : name: string
32 : url: string
33 : state: MissingModelDownloadState
34 : jobId?: string
35 : jobEnded: boolean
36 : request?: number
37 : endedJobs: ReadonlySet<string>
38 : }
39 :
40 2355 : const IDLE: MissingModelDownloadState = { status: 'idle' }
41 :
42 0 : function validCount(value: number | undefined): number | null {
43 0 : return value !== undefined && Number.isFinite(value) && value >= 0
44 : ? value
45 : : null
46 : }
47 :
48 0 : function progressFraction(progress: ComfyDownloadProgress): number | null {
49 0 : const received = validCount(progress.receivedBytes)
50 0 : const total = validCount(progress.totalBytes)
51 0 : if (received !== null && total !== null && total > 0 && received <= total) {
52 0 : return received / total
53 : }
54 0 : return Number.isFinite(progress.progress) &&
55 : progress.progress >= 0 &&
56 : progress.progress <= 1
57 : ? progress.progress
58 : : null
59 : }
60 :
61 0 : function toDownloadState(
62 : progress: ComfyDownloadProgress
63 : ): MissingModelDownloadState | undefined {
64 0 : switch (progress.status) {
65 : case 'pending':
66 0 : return { status: 'pending' }
67 : case 'downloading':
68 : case 'paused':
69 0 : return {
70 : status: 'downloading',
71 : paused: progress.status === 'paused',
72 : receivedBytes: validCount(progress.receivedBytes),
73 : totalBytes: validCount(progress.totalBytes),
74 : fraction: progressFraction(progress)
75 : }
76 : case 'completed':
77 0 : return { status: 'done' }
78 : case 'error':
79 0 : return { status: 'failed' }
80 : case 'cancelled':
81 0 : return IDLE
82 : default:
83 0 : return undefined
84 : }
85 : }
86 :
87 0 : function isActive(state: MissingModelDownloadState): boolean {
88 0 : return (
89 : state.status === 'requested' ||
90 : state.status === 'pending' ||
91 : state.status === 'downloading'
92 : )
93 : }
94 :
95 0 : function isHostTerminal(status: ComfyDownloadProgress['status']): boolean {
96 0 : return status === 'completed' || status === 'error' || status === 'cancelled'
97 : }
98 :
99 0 : function acceptsEvent(
100 : current: HostDownload | undefined,
101 : progress: ComfyDownloadProgress
102 : ): boolean {
103 0 : if (!current) return true
104 0 : if (progress.id === undefined) {
105 0 : if (current.jobEnded && progress.status !== 'pending') return false
106 0 : } else if (current.endedJobs.has(progress.id)) {
107 0 : return false
108 : }
109 0 : return (
110 : !isActive(current.state) ||
111 : current.state.status === 'requested' ||
112 : current.url === progress.url
113 : )
114 : }
115 :
116 0 : function isBusy(download: HostDownload | undefined): boolean {
117 0 : return (
118 : !!download && (isActive(download.state) || download.state.status === 'done')
119 : )
120 : }
121 :
122 0 : function isMissingCandidate(
123 : candidates: readonly MissingModelCandidate[] | null,
124 : progress: Pick<ComfyDownloadProgress, 'filename' | 'directory'>
125 : ): boolean {
126 0 : return !!candidates?.some(
127 : (candidate) =>
128 : candidate.name === progress.filename &&
129 : (progress.directory === undefined ||
130 : candidate.directory === progress.directory)
131 : )
132 : }
133 :
134 0 : function endedJobsAfter(
135 : current: HostDownload | undefined,
136 : progress: ComfyDownloadProgress
137 : ): ReadonlySet<string> {
138 0 : const ended = current?.endedJobs ?? new Set<string>()
139 0 : return isHostTerminal(progress.status) && progress.id !== undefined
140 : ? new Set([...ended, progress.id])
141 : : ended
142 : }
143 :
144 0 : function nextHostDownload(
145 : current: HostDownload | undefined,
146 : progress: ComfyDownloadProgress,
147 : state: MissingModelDownloadState
148 : ): HostDownload {
149 0 : return {
150 : name: progress.filename,
151 : url: progress.url,
152 : state,
153 : jobId:
154 : isHostTerminal(progress.status) && progress.id === undefined
155 : ? undefined
156 : : (progress.id ?? current?.jobId),
157 : jobEnded: isHostTerminal(progress.status),
158 : endedJobs: endedJobsAfter(current, progress)
159 : }
160 : }
161 :
162 : type DownloadControl = 'pauseDownload' | 'resumeDownload' | 'cancelDownload'
163 :
164 : /** Desktop 2 model downloads, mirrored by destination wherever they started. */
165 2355 : export const useMissingModelDownloadStore = defineStore(
166 : 'missingModelDownload',
167 : () => {
168 2355 : const missingModelStore = useMissingModelStore()
169 2355 : const downloads = shallowReactive(new Map<string, HostDownload>())
170 2355 : const controlsInFlight = shallowReactive(new Set<string>())
171 2355 : let refreshPending = false
172 2355 : const tracking = ref(false)
173 2355 : let trackingGeneration = 0
174 2355 : let nextRequest = 0
175 :
176 33 : function stateFor(model: ModelWithUrl): MissingModelDownloadState {
177 33 : return downloads.get(getModelFileKey(model))?.state ?? IDLE
178 : }
179 :
180 0 : function resetWhere(matches: (download: HostDownload) => boolean) {
181 0 : for (const [key, download] of downloads) {
182 0 : if (matches(download)) downloads.set(key, { ...download, state: IDLE })
183 : }
184 : }
185 :
186 11 : function flushRefresh() {
187 11 : if (!refreshPending || missingModelStore.isRefreshingMissingModels) return
188 0 : refreshPending = false
189 0 : void missingModelStore.refreshMissingModels()
190 : }
191 :
192 7093 : function reopenFinished(
193 : models: readonly { name: string; directory?: string }[],
194 : only: (download: HostDownload) => boolean = () => true
195 : ) {
196 7093 : const missing = new Set(
197 : models.flatMap(({ name, directory }) =>
198 : directory ? [getModelFileKey({ name, directory })] : []
199 : )
200 : )
201 7093 : for (const [key, download] of downloads) {
202 0 : if (
203 : download.state.status === 'done' &&
204 : missing.has(key) &&
205 : only(download)
206 : ) {
207 0 : downloads.set(key, { ...download, state: IDLE })
208 : }
209 : }
210 : }
211 :
212 7085 : function reopenStillMissing(
213 : candidates: readonly { name: string; directory?: string }[] | null
214 : ) {
215 7085 : if (refreshPending || missingModelStore.isRefreshingMissingModels) return
216 7085 : reopenFinished(candidates ?? [])
217 : }
218 :
219 2355 : watch(() => missingModelStore.missingModelCandidates, reopenStillMissing)
220 :
221 2355 : let finishedBeforeRefresh = new Set<HostDownload>()
222 2355 : watch(
223 : () => missingModelStore.isRefreshingMissingModels,
224 : (refreshing) => {
225 22 : if (refreshing) {
226 11 : finishedBeforeRefresh = new Set(
227 : [...downloads.values()].filter(
228 : (download) => download.state.status === 'done'
229 : )
230 : )
231 11 : return
232 : }
233 11 : const finished = finishedBeforeRefresh
234 11 : finishedBeforeRefresh = new Set()
235 22 : reopenFinished(
236 : missingModelStore.missingModelCandidates ?? [],
237 : (download) => finished.has(download)
238 : )
239 22 : flushRefresh()
240 : }
241 : )
242 :
243 0 : function keyFor(progress: ComfyDownloadProgress): string | undefined {
244 0 : if (progress.directory !== undefined) {
245 0 : return getModelFileKey({
246 : name: progress.filename,
247 : directory: progress.directory
248 : })
249 : }
250 0 : const matches = [...downloads].filter(
251 : ([, download]) =>
252 : download.url === progress.url && download.name === progress.filename
253 : )
254 0 : return matches.length === 1 ? matches[0][0] : undefined
255 : }
256 :
257 0 : function applyHostProgress(progress: ComfyDownloadProgress) {
258 0 : const state = toDownloadState(progress)
259 0 : const key = keyFor(progress)
260 0 : if (!state || key === undefined) return
261 0 : const current = downloads.get(key)
262 0 : if (!acceptsEvent(current, progress)) return
263 :
264 0 : const isMissing = isMissingCandidate(
265 : missingModelStore.missingModelCandidates,
266 : progress
267 : )
268 0 : downloads.set(key, nextHostDownload(current, progress, state))
269 0 : if (state.status === 'done' && current?.state.status !== 'done') {
270 0 : requestRefresh(isMissing)
271 : }
272 : }
273 :
274 0 : function requestRefresh(isMissing: boolean) {
275 0 : if (!isMissing) return
276 0 : refreshPending = true
277 0 : flushRefresh()
278 : }
279 :
280 2355 : function startTracking(): () => void {
281 2355 : if (!hostReportsModelDownloadProgress()) return () => undefined
282 0 : const subscription = ++trackingGeneration
283 2355 : const stop = window.__comfyDesktop2?.onDownloadProgress?.((progress) => {
284 0 : if (subscription === trackingGeneration) applyHostProgress(progress)
285 : })
286 2355 : if (typeof stop !== 'function') {
287 0 : trackingGeneration++
288 0 : return () => undefined
289 : }
290 0 : tracking.value = true
291 0 : return () => {
292 0 : tracking.value = false
293 0 : trackingGeneration++
294 0 : try {
295 0 : stop()
296 : } catch (error) {
297 0 : reportError(error, {
298 : surface: 'platform',
299 : errorType: 'error_stopping_model_download_tracking'
300 : })
301 : } finally {
302 0 : resetWhere(() => true)
303 : }
304 : }
305 : }
306 :
307 0 : function unreportedRequest(model: ModelWithUrl, request: number) {
308 0 : const current = downloads.get(getModelFileKey(model))
309 0 : return current?.request === request &&
310 : current.state.status === 'requested'
311 : ? current
312 : : undefined
313 : }
314 :
315 0 : function failRequest(model: ModelWithUrl, request: number) {
316 0 : const current = unreportedRequest(model, request)
317 0 : if (!current) return
318 0 : downloads.set(getModelFileKey(model), {
319 : ...current,
320 : state: { status: 'failed' }
321 : })
322 : }
323 :
324 0 : function finishUnreportedRequest(model: ModelWithUrl, request: number) {
325 0 : const current = unreportedRequest(model, request)
326 0 : if (!current) return
327 0 : downloads.set(getModelFileKey(model), {
328 : ...current,
329 : state: { status: 'done' },
330 : jobEnded: true
331 : })
332 0 : requestRefresh(
333 : isMissingCandidate(missingModelStore.missingModelCandidates, {
334 : filename: model.name,
335 : directory: model.directory
336 : })
337 : )
338 : }
339 :
340 0 : function reportStartFailure(error: unknown) {
341 0 : reportError(error, {
342 : surface: 'platform',
343 : errorType: 'error_starting_model_download',
344 : tags: { host: 'desktop2' }
345 : })
346 : }
347 :
348 0 : function download(model: ModelWithUrl): void {
349 0 : if (!tracking.value || !hostReportsModelDownloadProgress()) {
350 0 : downloadModel(model, missingModelStore.folderPaths)
351 0 : return
352 : }
353 0 : const key = getModelFileKey(model)
354 0 : const current = downloads.get(key)
355 0 : if (current?.state.status === 'downloading' && current.state.paused) {
356 0 : if (current.url === model.url) control(model, 'resumeDownload')
357 0 : return
358 : }
359 0 : if (isBusy(current)) return
360 :
361 0 : const request = ++nextRequest
362 0 : downloads.set(key, {
363 : name: model.name,
364 : url: model.url,
365 : state: { status: 'requested' },
366 : jobEnded: false,
367 : request,
368 : endedJobs: current?.endedJobs ?? new Set<string>()
369 : })
370 0 : dispatchRequest(model, request)
371 : }
372 :
373 0 : function dispatchRequest(model: ModelWithUrl, request: number) {
374 0 : const outcome = dispatchModelDownload(model, {})
375 0 : if (outcome.status === 'dispatch-failed') {
376 0 : reportStartFailure(outcome.error)
377 : }
378 0 : if (outcome.status !== 'host-requested') {
379 0 : failRequest(model, request)
380 0 : return
381 : }
382 0 : void outcome.hostResult.then(
383 : (accepted) => {
384 0 : if (accepted) finishUnreportedRequest(model, request)
385 0 : else failRequest(model, request)
386 : },
387 : (error: unknown) => {
388 0 : reportStartFailure(error)
389 0 : failRequest(model, request)
390 : }
391 : )
392 : }
393 :
394 0 : function controlRef(model: ModelWithUrl): string | undefined {
395 0 : const download = downloads.get(getModelFileKey(model))
396 0 : if (!download || !isActive(download.state)) return undefined
397 0 : if (
398 : download.jobId !== undefined &&
399 : !download.endedJobs.has(download.jobId)
400 : )
401 0 : return download.jobId
402 : const sharingUrl = [...downloads.values()].filter(
403 : (other) => other.url === download.url && isActive(other.state)
404 : )
405 0 : return sharingUrl.length === 1 ? download.url : undefined
406 : }
407 :
408 0 : function canControl(model: ModelWithUrl, action: DownloadControl): boolean {
409 0 : return (
410 : tracking.value &&
411 : typeof window.__comfyDesktop2?.[action] === 'function' &&
412 : !controlsInFlight.has(getModelFileKey(model)) &&
413 : controlRef(model) !== undefined
414 : )
415 : }
416 :
417 0 : function reportControlFailure(error: unknown, action: DownloadControl) {
418 0 : reportError(error, {
419 : surface: 'platform',
420 : errorType: 'error_controlling_model_download',
421 : tags: { action }
422 : })
423 : }
424 :
425 0 : function withdrawUnreportedRequest(
426 : model: ModelWithUrl,
427 : request: number | undefined
428 : ) {
429 0 : const current =
430 : request === undefined ? undefined : unreportedRequest(model, request)
431 0 : if (!current) return
432 0 : downloads.set(getModelFileKey(model), { ...current, state: IDLE })
433 : }
434 :
435 0 : function control(model: ModelWithUrl, action: DownloadControl) {
436 0 : const key = getModelFileKey(model)
437 0 : const send = window.__comfyDesktop2?.[action]
438 0 : const target = controlRef(model)
439 0 : if (
440 : typeof send !== 'function' ||
441 : target === undefined ||
442 : controlsInFlight.has(key)
443 : )
444 0 : return
445 0 : const request = downloads.get(key)?.request
446 0 : controlsInFlight.add(key)
447 0 : try {
448 0 : void Promise.resolve(send(target))
449 : .then(
450 : (accepted: unknown) => {
451 0 : if (accepted === false) {
452 0 : reportControlFailure(
453 : new Error(`Host refused ${action}`),
454 : action
455 : )
456 0 : } else if (accepted === true && action === 'cancelDownload') {
457 0 : withdrawUnreportedRequest(model, request)
458 : }
459 : },
460 : (error: unknown) => reportControlFailure(error, action)
461 : )
462 : .finally(() => controlsInFlight.delete(key))
463 : } catch (error) {
464 0 : controlsInFlight.delete(key)
465 0 : reportControlFailure(error, action)
466 : }
467 : }
468 :
469 2355 : return {
470 : stateFor,
471 : download,
472 : startTracking,
473 : tracksHostProgress: () => tracking.value,
474 : reopenFinished: reopenStillMissing,
475 : canControl,
476 : canResume: (model: ModelWithUrl) =>
477 : downloads.get(getModelFileKey(model))?.url === model.url &&
478 : canControl(model, 'resumeDownload'),
479 : pause: (model: ModelWithUrl) => control(model, 'pauseDownload'),
480 : resume: (model: ModelWithUrl) => control(model, 'resumeDownload'),
481 : cancel: (model: ModelWithUrl) => control(model, 'cancelDownload')
482 : }
483 : }
484 : )
|