Line data Source code
1 : import { computed, onScopeDispose, ref, shallowRef, watch } from 'vue'
2 : import type { WatchStopHandle } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
6 : import type { UploadModelSuccess } from '@/platform/assets/composables/useUploadModelWizard'
7 : import { modelFileUploadApi } from '@/platform/workspace/api/modelFileUploadApi'
8 : import type { ModelFileUploadResult } from '@/platform/workspace/api/modelFileUploadApi'
9 : import { reportError } from '@comfyorg/telemetry/reportError'
10 : import {
11 : isDownloadCancelled,
12 : useAssetDownloadStore
13 : } from '@/stores/assetDownloadStore'
14 : import type { AssetDownloadStatus } from '@/stores/assetDownloadStore'
15 :
16 2355 : const MODEL_FILE_EXTENSION = '.safetensors'
17 2355 : const DEFAULT_MODEL_TYPE = 'loras'
18 :
19 : type UploadStopped = Extract<ModelFileUploadResult, { ok: false }>
20 :
21 2355 : const FAILURE_COPY: Partial<Record<UploadStopped['reason'], string>> = {
22 : cancelled: 'assetBrowser.fileUpload.cancelled',
23 : sessionExpired: 'assetBrowser.fileUpload.sessionExpired',
24 : unsupportedFile: 'assetBrowser.fileUpload.notSafetensors',
25 : unsupportedType: 'assetBrowser.fileUpload.unsupportedType',
26 : privateModelsDisabled: 'assetBrowser.fileUpload.privateModelsDisabled',
27 : planUnavailable: 'assetBrowser.fileUpload.planUnavailable'
28 : }
29 :
30 : /** Where one model file upload stands. */
31 : export type ModelFileUpload =
32 : | { status: 'idle'; error?: string }
33 : | { status: 'chosen'; file: File }
34 : | { status: 'stopping'; file: File }
35 : | {
36 : status: 'uploading' | 'reconnecting'
37 : file: File
38 : sent: number
39 : bytesPerSecond?: number
40 : }
41 : | { status: 'saving'; file: File }
42 : | { status: 'done'; file: File; modelType: string }
43 : | { status: 'failed'; file: File; error: string; taskId?: string }
44 :
45 : type ReadyToSend = Extract<ModelFileUpload, { status: 'chosen' | 'failed' }>
46 :
47 : interface FollowedTask {
48 : id: string
49 : file: File
50 : modelType: string
51 : status?: AssetDownloadStatus
52 : error?: string
53 : }
54 :
55 : /**
56 : * One model file chosen from the computer and uploaded into the workspace
57 : * library: choose it, pick its type, upload with progress or cancel.
58 : */
59 0 : export function useUploadModelFile() {
60 0 : const { t } = useI18n()
61 0 : const { flags } = useFeatureFlags()
62 0 : const downloads = useAssetDownloadStore()
63 0 : const uploadFolders = computed(() => flags.modelUploadFolders)
64 0 : const isAvailable = computed(() => uploadFolders.value.length > 0)
65 :
66 0 : function defaultModelType() {
67 0 : return uploadFolders.value.includes(DEFAULT_MODEL_TYPE)
68 : ? DEFAULT_MODEL_TYPE
69 : : (uploadFolders.value[0] ?? '')
70 : }
71 :
72 0 : const state = shallowRef<ModelFileUpload>({ status: 'idle' })
73 0 : const modelType = ref(defaultModelType())
74 0 : let controller: AbortController | undefined
75 0 : let task: FollowedTask | undefined
76 0 : let stopTaskWatch: WatchStopHandle | undefined
77 0 : let generation = 0
78 :
79 0 : function stopFollowing() {
80 0 : stopTaskWatch?.()
81 0 : stopTaskWatch = undefined
82 0 : task = undefined
83 : }
84 :
85 0 : function stop() {
86 0 : generation++
87 0 : controller?.abort()
88 0 : stopFollowing()
89 : }
90 :
91 0 : onScopeDispose(stop)
92 :
93 0 : function choose(file: File) {
94 0 : stop()
95 0 : if (!file.name.toLowerCase().endsWith(MODEL_FILE_EXTENSION)) {
96 0 : state.value = {
97 : status: 'idle',
98 : error: t('assetBrowser.fileUpload.notSafetensors')
99 : }
100 0 : return
101 : }
102 0 : modelType.value = defaultModelType()
103 0 : state.value = { status: 'chosen', file }
104 : }
105 :
106 0 : function failureText(failure: UploadStopped) {
107 0 : const copy = FAILURE_COPY[failure.reason]
108 0 : if (copy) return t(copy)
109 0 : return failure.message ?? t('assetBrowser.fileUpload.failed')
110 : }
111 :
112 0 : function progressRow(taskId: string) {
113 0 : return downloads.downloadList.find((d) => d.taskId === taskId)
114 : }
115 :
116 0 : function taskOutcome(followed: FollowedTask): ModelFileUpload | undefined {
117 0 : const { status, file, id: taskId } = followed
118 0 : if (status === 'completed') {
119 0 : return { status: 'done', file, modelType: followed.modelType }
120 : }
121 0 : if (status === 'failed') {
122 0 : return {
123 : status: 'failed',
124 : file,
125 : taskId,
126 : error: followed.error || t('assetBrowser.fileUpload.failed')
127 : }
128 : }
129 0 : if (status && isDownloadCancelled(status)) {
130 0 : return {
131 : status: 'failed',
132 : file,
133 : taskId,
134 : error: t('assetBrowser.fileUpload.cancelled')
135 : }
136 : }
137 : return undefined
138 : }
139 :
140 0 : function heldBack(followed: FollowedTask, error: string): ModelFileUpload {
141 0 : const outcome = taskOutcome(followed)
142 0 : if (outcome?.status === 'done') return outcome
143 0 : return { status: 'failed', file: followed.file, taskId: followed.id, error }
144 : }
145 :
146 0 : function watchTask(followed: FollowedTask) {
147 0 : return watch(
148 : () => {
149 0 : if (downloads.lastCompletedDownload?.taskId === followed.id) {
150 0 : return { status: 'completed' as const, error: undefined }
151 : }
152 0 : const row = progressRow(followed.id)
153 0 : return { status: row?.status, error: row?.error }
154 : },
155 : ({ status, error }) => {
156 0 : if (!status || followed.status === 'completed') return
157 0 : if (status === followed.status && error === followed.error) return
158 0 : followed.status = status
159 0 : followed.error = error
160 0 : const outcome = taskOutcome(followed)
161 0 : if (task === followed && state.value.status !== 'stopping' && outcome) {
162 0 : state.value = outcome
163 : }
164 : },
165 : { immediate: true, flush: 'sync' }
166 : )
167 : }
168 :
169 0 : function followTask(taskId: string, file: File, type: string) {
170 0 : downloads.trackDownload(taskId, type, file.name, { source: 'upload' })
171 0 : state.value = { status: 'saving', file }
172 0 : const followed: FollowedTask = { id: taskId, file, modelType: type }
173 0 : task = followed
174 0 : stopTaskWatch = watchTask(followed)
175 : }
176 :
177 0 : function cancellationSettled(
178 : taskId: string,
179 : signal: AbortSignal
180 : ): Promise<void> {
181 0 : const pending = () => progressRow(taskId)?.status === 'cancellation_pending'
182 0 : if (signal.aborted || !pending()) return Promise.resolve()
183 0 : return new Promise((resolve) => {
184 0 : const settle = () => {
185 0 : unwatch()
186 0 : signal.removeEventListener('abort', settle)
187 0 : resolve()
188 : }
189 0 : const unwatch = watch(pending, (stillPending) => {
190 0 : if (!stillPending) settle()
191 : })
192 0 : signal.addEventListener('abort', settle)
193 : })
194 : }
195 :
196 0 : function hasStopped(followed: FollowedTask) {
197 0 : return followed.status === 'cancelled' || followed.status === 'failed'
198 : }
199 :
200 0 : function followedTaskOf(previous: ReadyToSend) {
201 0 : return previous.status === 'failed' && previous.taskId === task?.id
202 : ? task
203 : : undefined
204 : }
205 :
206 0 : async function requestStop(followed: FollowedTask, signal: AbortSignal) {
207 0 : if (!progressRow(followed.id)) {
208 0 : downloads.trackDownload(
209 : followed.id,
210 : followed.modelType,
211 : followed.file.name,
212 : { source: 'upload' }
213 : )
214 : }
215 0 : const cancelled = await downloads.cancelDownload(followed.id)
216 0 : if (!cancelled.ok) {
217 0 : reportError(cancelled.error, {
218 : surface: 'assets',
219 : errorType: 'asset_download_cancellation_failure',
220 : level: 'warning'
221 : })
222 0 : return false
223 : }
224 0 : await cancellationSettled(followed.id, signal)
225 0 : return true
226 : }
227 :
228 0 : async function settleFollowedTask(
229 : followed: FollowedTask,
230 : run: number,
231 : signal: AbortSignal
232 : ): Promise<boolean> {
233 0 : if (followed.status === 'cancelled') return true
234 0 : state.value = { status: 'stopping', file: followed.file }
235 0 : const requested = await requestStop(followed, signal)
236 0 : if (run !== generation) return false
237 0 : if (signal.aborted) {
238 0 : state.value = heldBack(followed, t('assetBrowser.fileUpload.cancelled'))
239 0 : return false
240 : }
241 0 : if (requested && hasStopped(followed)) return true
242 0 : state.value = heldBack(followed, t('assetBrowser.fileUpload.notStopped'))
243 0 : return false
244 : }
245 :
246 0 : function beginAttempt() {
247 0 : generation++
248 0 : controller?.abort()
249 0 : controller = new AbortController()
250 0 : return { run: generation, signal: controller.signal }
251 : }
252 :
253 0 : async function upload(): Promise<UploadModelSuccess | null> {
254 0 : const current = state.value
255 0 : if (current.status !== 'chosen' && current.status !== 'failed') return null
256 :
257 0 : const { run, signal } = beginAttempt()
258 0 : const { file } = current
259 0 : const type = modelType.value
260 :
261 0 : const followed = followedTaskOf(current)
262 0 : if (followed && !(await settleFollowedTask(followed, run, signal))) {
263 0 : return null
264 : }
265 0 : stopFollowing()
266 0 : state.value = { status: 'uploading', file, sent: 0 }
267 0 : let sampleTime = performance.now()
268 0 : let sampleBytes = 0
269 :
270 0 : const result = await modelFileUploadApi.uploadModelFile(file, {
271 : tags: ['models', type],
272 : signal,
273 : onProgress: (sent) => {
274 0 : if (run !== generation || signal.aborted) return
275 0 : const current = state.value
276 0 : if (current.status !== 'uploading' && current.status !== 'reconnecting')
277 0 : return
278 0 : const seconds = (performance.now() - sampleTime) / 1000
279 0 : const bytesPerSecond =
280 : current.status === 'uploading' && seconds > 0 && sent >= sampleBytes
281 : ? (sent - sampleBytes) / seconds
282 : : undefined
283 0 : state.value = { ...current, sent, bytesPerSecond }
284 : },
285 : onReconnecting: (reconnecting) => {
286 0 : if (run !== generation || signal.aborted) return
287 0 : const current = state.value
288 0 : if (current.status !== 'uploading' && current.status !== 'reconnecting')
289 0 : return
290 0 : const status = reconnecting ? 'reconnecting' : 'uploading'
291 0 : if (current.status === status) return
292 0 : sampleTime = performance.now()
293 0 : sampleBytes = current.sent
294 0 : state.value = { status, file, sent: current.sent }
295 : }
296 : })
297 0 : if (run !== generation) return null
298 :
299 0 : if (!result.ok) {
300 0 : if (result.reason === 'failed') {
301 0 : reportError(result.cause, {
302 : surface: 'assets',
303 : errorType: 'error_uploading_model_file',
304 : level: 'warning'
305 : })
306 : }
307 0 : state.value = { status: 'failed', file, error: failureText(result) }
308 0 : return null
309 : }
310 :
311 0 : followTask(result.taskId, file, type)
312 0 : return {
313 : filename: file.name,
314 : modelType: type,
315 : taskId: result.taskId,
316 : status: 'processing'
317 : }
318 : }
319 :
320 0 : function cancel() {
321 0 : controller?.abort()
322 : }
323 :
324 0 : function reset() {
325 0 : stop()
326 0 : state.value = { status: 'idle' }
327 0 : modelType.value = defaultModelType()
328 : }
329 :
330 0 : return {
331 : isAvailable,
332 : uploadFolders,
333 : state,
334 : modelType,
335 : choose,
336 : upload,
337 : cancel,
338 : reset
339 : }
340 : }
|