Line data Source code
1 : import { useAsyncState, useEventListener, whenever } from '@vueuse/core'
2 : import { delay, difference, uniqBy } from 'es-toolkit'
3 : import { defineStore } from 'pinia'
4 : import { reportError } from '@comfyorg/telemetry/reportError'
5 : import {
6 : computed,
7 : effectScope,
8 : onScopeDispose,
9 : reactive,
10 : ref,
11 : shallowReactive,
12 : toValue,
13 : watch
14 : } from 'vue'
15 : import type { EffectScope } from 'vue'
16 :
17 : import { useToast } from '@/components/ui/toast/toastStore'
18 : import { t } from '@/i18n'
19 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
20 : import { deploymentDownloads } from '@/platform/workspace/stores/deploymentLibraryModelsStore'
21 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
22 : import {
23 : mapInputFileToAssetItem,
24 : unflattenOutputAssets
25 : } from '@/platform/assets/composables/media/assetMappers'
26 : import type {
27 : AssetItem,
28 : AssetResponse,
29 : ModelBrowserAsset,
30 : TagsOperationResult
31 : } from '@/platform/assets/schemas/assetSchema'
32 : import {
33 : useAssetsQuery,
34 : invalidateAll,
35 : loadNewAll
36 : } from '@/platform/assets/composables/useAssetsQuery'
37 : import { useJobOutputHistory } from '@/platform/assets/composables/useJobOutputHistory'
38 : import { getOutputAssetMetadata } from '@/platform/assets/schemas/assetMetadataSchema'
39 : import { resolveOutputAssetItems } from '@/platform/assets/utils/outputAssetUtil'
40 : import { isCloud } from '@/platform/distribution/types'
41 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
42 : import {
43 : getAssetCategories,
44 : getAssetFilename
45 : } from '@/platform/assets/utils/assetMetadataUtils'
46 : import { assetService } from '@/platform/assets/services/assetService'
47 : import type { AssetPaginationOptions } from '@/platform/assets/services/assetService'
48 : import type { AssetSeedCompletedWsMessage } from '@comfyorg/api-client/executionTypes'
49 : import { api } from '@/scripts/api'
50 : import { WrappedList } from '@/utils/pagedList'
51 : import type { PagedList } from '@/utils/pagedList'
52 :
53 : import { useAssetDownloadStore } from './assetDownloadStore'
54 : import { useModelToNodeStore } from './modelToNodeStore'
55 :
56 : /**
57 : * Fetch input files from the internal API (OSS version)
58 : */
59 2431 : async function fetchInputFilesFromAPI(): Promise<AssetItem[]> {
60 2431 : const response = await fetch(api.internalURL('/files/input'), {
61 : headers: {
62 : 'Comfy-User': api.user
63 : }
64 : })
65 :
66 2431 : if (!response.ok) {
67 0 : throw new Error('Failed to fetch input files')
68 : }
69 :
70 : const filenames: string[] = await response.json()
71 2431 : return filenames.map((name, index) =>
72 : mapInputFileToAssetItem(name, index, 'input')
73 : )
74 : }
75 :
76 2356 : const MAX_OUTPUT_LOOKUP_PAGES = 20
77 :
78 2356 : export const useAssetsStore = defineStore('assets', () => {
79 2355 : const assetDownloadStore = useAssetDownloadStore()
80 2355 : const modelToNodeStore = useModelToNodeStore()
81 2355 : const { flags } = useFeatureFlags()
82 2355 : const workspaceStore = useTeamWorkspaceStore()
83 2355 : const toast = useToast()
84 :
85 : // Track assets currently being deleted (for loading overlay)
86 2355 : const deletingAssetIds = shallowReactive(new Set<string>())
87 :
88 2355 : const setAssetDeleting = (assetId: string, isDeleting: boolean) => {
89 4 : if (isDeleting) {
90 2 : deletingAssetIds.add(assetId)
91 : } else {
92 2 : deletingAssetIds.delete(assetId)
93 : }
94 : }
95 :
96 2355 : const isAssetDeleting = (assetId: string): boolean => {
97 394 : return deletingAssetIds.has(assetId)
98 : }
99 :
100 : const fetchInputFiles = fetchInputFilesFromAPI
101 :
102 2355 : const {
103 : state: rawInputAssets,
104 : isLoading: inputLoading,
105 : execute: executeUpdateInputs
106 : } = useAsyncState(fetchInputFiles, [], {
107 : immediate: false,
108 : resetOnExecute: false,
109 : onError: (err) => {
110 0 : console.error('Error fetching input assets:', err)
111 : }
112 : })
113 :
114 2355 : const historyInputs: PagedList<AssetItem> = {
115 : hasMore: false,
116 : invalidate: async () => {
117 76 : await executeUpdateInputs()
118 : },
119 : isLoading: inputLoading,
120 : items: rawInputAssets,
121 : loadMore: async () => false,
122 : loadNew: async () => undefined
123 : }
124 :
125 2355 : function useHistoryAssets(): [PagedList<AssetItem>, PagedList<AssetItem>] {
126 2355 : const history = useJobOutputHistory()
127 2355 : const resolvedAssetsCache = ref<Partial<Record<string, AssetItem[]>>>({})
128 2355 : watch(
129 : () => toValue(history.items),
130 : (items, previousItems) => {
131 7366 : const currentAssetsCache = resolvedAssetsCache.value
132 7366 : const currentIds = new Set(items.map(({ id }) => id))
133 7366 : for (const jobId of Object.keys(currentAssetsCache)) {
134 2149 : if (!currentIds.has(jobId)) delete currentAssetsCache[jobId]
135 : }
136 7366 : for (const asset of items) {
137 3724 : const metadata = getOutputAssetMetadata(asset.user_metadata)
138 3724 : const jobId = metadata?.jobId
139 3724 : if (!jobId || previousItems?.includes(asset)) continue
140 3724 : void resolveOutputAssetItems(metadata, {
141 : createdAt: asset.created_at,
142 : fallbackItems: currentAssetsCache[jobId]
143 : }).then((assets) => {
144 3717 : if (toValue(history.items).includes(asset)) {
145 3686 : currentAssetsCache[jobId] = assets
146 : }
147 : })
148 : }
149 : },
150 : { immediate: true }
151 : )
152 : const flatOutputs = new WrappedList(history, (items) =>
153 : items.flatMap((asset) => {
154 961 : const jobId = getOutputAssetMetadata(asset.user_metadata)?.jobId
155 961 : if (!jobId) return [asset]
156 961 : return resolvedAssetsCache.value[jobId] ?? [asset]
157 : })
158 : )
159 2355 : return [history, flatOutputs]
160 : }
161 :
162 2355 : const inputAssets = ref<PagedList<AssetItem>>(undefined!)
163 2355 : const outputAssets = ref<PagedList<AssetItem>>(undefined!)
164 2355 : const flatOutputAssets = ref<PagedList<AssetItem>>(undefined!)
165 2355 : const allAssets = ref<PagedList<AssetItem>>(undefined!)
166 2355 : let assetsScope: EffectScope | undefined
167 2355 : onScopeDispose(() => assetsScope?.stop())
168 2355 : watch(
169 : () => flags.assetsEnabled,
170 : (isAssets) => {
171 2375 : assetsScope?.stop()
172 2375 : assetsScope = effectScope()
173 2375 : assetsScope.run(() => {
174 2375 : if (isAssets) {
175 20 : inputAssets.value = useAssetsQuery({ tags_any: ['input'] })
176 20 : flatOutputAssets.value = useAssetsQuery({
177 : tags_any: ['output', 'temp']
178 : })
179 20 : outputAssets.value = new WrappedList(
180 : flatOutputAssets.value,
181 : unflattenOutputAssets
182 : )
183 20 : allAssets.value = useAssetsQuery({
184 : tags_any: ['input', 'output', 'temp']
185 : })
186 : } else {
187 2355 : inputAssets.value = historyInputs
188 2355 : ;[outputAssets.value, flatOutputAssets.value] = useHistoryAssets()
189 2355 : allAssets.value = new WrappedList(
190 : flatOutputAssets.value,
191 : (outputItems) => [...toValue(historyInputs.items), ...outputItems]
192 : )
193 2355 : void executeUpdateInputs()
194 : }
195 : })
196 : },
197 : { immediate: true }
198 : )
199 :
200 0 : async function loadOutputAsset(assetId: string): Promise<boolean> {
201 0 : const assets = outputAssets.value
202 0 : const hasAsset = () =>
203 : toValue(assets.items).some(({ id }) => id === assetId)
204 :
205 0 : let pagesLoaded = 0
206 0 : while (
207 : !hasAsset() &&
208 : toValue(assets.hasMore) &&
209 : pagesLoaded++ < MAX_OUTPUT_LOOKUP_PAGES
210 : ) {
211 0 : if (!(await assets.loadMore())) break
212 : }
213 0 : return hasAsset()
214 : }
215 2355 : useEventListener(
216 : api,
217 : 'assets.seed.completed',
218 : async ({ detail }: CustomEvent<AssetSeedCompletedWsMessage>) => {
219 0 : if (flags.assetsEnabled) {
220 0 : if (detail.phase !== 'fast') await invalidateAll()
221 0 : else if (detail.created) await loadNewAll()
222 0 : return
223 : }
224 0 : if (detail.phase !== 'fast') {
225 0 : await outputAssets.value.invalidate()
226 0 : await inputAssets.value.invalidate()
227 0 : } else if (detail.created) {
228 0 : await outputAssets.value.loadNew()
229 0 : await inputAssets.value.loadNew()
230 : }
231 : }
232 : )
233 :
234 : /**
235 : * Map of asset hash filename to asset item for O(1) lookup
236 : * Cloud assets use hash for the hash-based filename
237 : */
238 2355 : const inputAssetsByFilename = computed(() => {
239 0 : const map = new Map<string, AssetItem>()
240 0 : for (const asset of toValue(inputAssets.value.items)) {
241 0 : const hash = asset.hash
242 0 : if (hash) {
243 0 : map.set(hash, asset)
244 : }
245 : }
246 0 : return map
247 : })
248 :
249 : /**
250 : * @param filename Hash-based filename (e.g., "72e786ff...efb7.png")
251 : * @returns Human-readable asset name or original filename if not found
252 : */
253 0 : function getInputName(filename: string): string {
254 0 : return inputAssetsByFilename.value.get(filename)?.name ?? filename
255 : }
256 :
257 2355 : const MODEL_BATCH_SIZE = 500
258 :
259 : // Defensive backstop against a server that never signals exhaustion (e.g.
260 : // emits an unbounded stream of unique cursors). At MODEL_BATCH_SIZE per
261 : // batch this caps a single walk at 500k assets — far beyond any real
262 : // category — so it only ever trips on pathological pagination.
263 2355 : const MAX_PAGINATION_BATCHES = 1000
264 :
265 : interface ModelPaginationState {
266 : assets: Map<string, AssetItem>
267 : /**
268 : * Count of assets fetched so far. Only drives the request `offset` when
269 : * the backend is cursor-less (no `next_cursor` in responses).
270 : */
271 : offset: number
272 : /**
273 : * Keyset cursor from the latest response's `next_cursor`. When set,
274 : * subsequent batches resume with `after` instead of `offset`.
275 : */
276 : nextCursor?: string
277 : hasMore: boolean
278 : isLoading: boolean
279 : error?: Error
280 : }
281 :
282 : interface ModelRefreshOptions {
283 : restart?: boolean
284 : }
285 :
286 : /**
287 : * Model assets cached by category (e.g., 'checkpoints', 'loras')
288 : * Multiple node types sharing the same category share the same cache entry.
289 : * Public API accepts nodeType for backwards compatibility but translates
290 : * to category internally using modelToNodeStore.getCategoryForNodeType().
291 : *
292 : * Runs on every distribution; whether anything fetches through it is
293 : * decided by consumers via `assetService.isWidgetAssetPickerEnabled()`,
294 : * which hard-gates widget surfaces to cloud.
295 : */
296 2355 : const getModelState = () => {
297 2355 : const libraryCategory = 'library:models'
298 2355 : const modelStateByCategory = ref(new Map<string, ModelPaginationState>())
299 :
300 6 : function includesLibrary(): boolean {
301 6 : return (
302 : isCloud &&
303 : flags.runtimeLibraryModelsEnabled &&
304 : !!workspaceStore.workspaceId &&
305 : !!useDeploymentPickStore().bootDeployment
306 : )
307 : }
308 :
309 2355 : const mergedAssets = computed<ModelBrowserAsset[]>(() => {
310 0 : const release = Array.from(
311 : modelStateByCategory.value.get('tag:models')?.assets.values() ?? []
312 : )
313 0 : const library = Array.from(
314 : modelStateByCategory.value.get(libraryCategory)?.assets.values() ?? []
315 : ).sort(
316 : (a, b) =>
317 : Date.parse(b.created_at) - Date.parse(a.created_at) ||
318 : (a.id < b.id ? 1 : a.id > b.id ? -1 : 0)
319 : )
320 0 : const keyFor = (category: string, asset: AssetItem) =>
321 : JSON.stringify([category, asset.loader_path ?? getAssetFilename(asset)])
322 0 : const releaseKeys = new Set(
323 : release.flatMap((asset) =>
324 : getAssetCategories(asset, flags.supportsModelTypeTags).map(
325 : (category) => keyFor(category, asset)
326 : )
327 : )
328 : )
329 0 : const releaseIds = new Set(release.map((asset) => asset.id))
330 0 : const libraryKeys = new Set<string>()
331 0 : const visibleLibrary = library.flatMap((asset): ModelBrowserAsset[] => {
332 0 : if (releaseIds.has(asset.id)) return []
333 0 : const filename = asset.loader_path ?? getAssetFilename(asset)
334 0 : const categories = getAssetCategories(
335 : asset,
336 : flags.supportsModelTypeTags
337 : )
338 0 : const browserCategories = categories.filter((category) => {
339 0 : const key = keyFor(category, asset)
340 0 : return (
341 : deploymentDownloads(category, filename) &&
342 : !releaseKeys.has(key) &&
343 : !libraryKeys.has(key)
344 : )
345 : })
346 0 : if (browserCategories.length === 0) return []
347 0 : browserCategories.forEach((category) =>
348 : libraryKeys.add(keyFor(category, asset))
349 : )
350 0 : return [
351 : {
352 : ...asset,
353 : isWorkspaceLibrary: true,
354 : ...(browserCategories.length < categories.length
355 : ? { browserCategories }
356 : : {})
357 : }
358 : ]
359 : })
360 0 : return [...release, ...visibleLibrary]
361 : })
362 :
363 2355 : const assetsArrayCache = new Map<
364 : string,
365 : { source: Map<string, AssetItem>; array: AssetItem[] }
366 : >()
367 :
368 2355 : const pendingRequestByCategory = new Map<string, ModelPaginationState>()
369 2355 : const pendingPromiseByCategory = new Map<string, Promise<void>>()
370 2355 : const abortByCategory = new Map<string, AbortController>()
371 2355 : const replacementBySupersededFetch = new WeakMap<
372 : AbortController,
373 : Promise<void>
374 : >()
375 :
376 2355 : watch(
377 : [
378 : () => workspaceStore.workspaceId,
379 : () => workspaceStore.identityGeneration
380 : ],
381 : () => {
382 22 : for (const controller of abortByCategory.values()) controller.abort()
383 22 : abortByCategory.clear()
384 22 : pendingRequestByCategory.clear()
385 22 : pendingPromiseByCategory.clear()
386 22 : assetsArrayCache.clear()
387 22 : modelStateByCategory.value.clear()
388 : },
389 : { flush: 'sync' }
390 : )
391 :
392 2355 : watch(
393 : () => flags.runtimeLibraryModelsEnabled,
394 : () => invalidateCategory(libraryCategory),
395 : { flush: 'sync' }
396 : )
397 :
398 1 : function createState(
399 : existingAssets?: Map<string, AssetItem>
400 : ): ModelPaginationState {
401 1 : const assets = new Map(existingAssets)
402 1 : return reactive({
403 : assets,
404 : offset: 0,
405 : nextCursor: undefined,
406 : hasMore: true,
407 : isLoading: true
408 : })
409 : }
410 :
411 1 : function isStale(category: string, state: ModelPaginationState): boolean {
412 1 : const committed = modelStateByCategory.value.get(category)
413 1 : const pending = pendingRequestByCategory.get(category)
414 1 : return committed !== state && pending !== state
415 : }
416 :
417 2355 : const EMPTY_ASSETS: AssetItem[] = []
418 :
419 : /**
420 : * Resolve a key to a category. Handles both nodeType and tag:xxx formats.
421 : * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models')
422 : * @returns The category or undefined if not resolvable
423 : */
424 5 : function resolveCategory(key: string): string | undefined {
425 5 : if (key.startsWith('tag:')) {
426 5 : return key
427 : }
428 0 : if (key === libraryCategory) return key
429 0 : return modelToNodeStore.getCategoryForNodeType(key)
430 : }
431 :
432 : /**
433 : * Get assets by nodeType or tag key.
434 : * Translates nodeType to category internally for cache lookup.
435 : * @param key Either a nodeType (e.g., 'CheckpointLoaderSimple') or tag key (e.g., 'tag:models')
436 : */
437 2 : function getAssets(key: string): ModelBrowserAsset[] {
438 2 : return key === 'tag:models' && includesLibrary()
439 : ? mergedAssets.value
440 : : getCachedAssets(key)
441 : }
442 :
443 2 : function getCachedAssets(key: string): AssetItem[] {
444 2 : const category = resolveCategory(key)
445 2 : if (!category) return EMPTY_ASSETS
446 :
447 : const state = modelStateByCategory.value.get(category)
448 2 : const assetsMap = state?.assets
449 2 : if (!assetsMap) return EMPTY_ASSETS
450 :
451 2 : const cached = assetsArrayCache.get(category)
452 2 : if (cached && cached.source === assetsMap) {
453 0 : return cached.array
454 : }
455 :
456 2 : const array = Array.from(assetsMap.values())
457 2 : assetsArrayCache.set(category, { source: assetsMap, array })
458 2 : return array
459 : }
460 :
461 4 : function sourceCategories(category: string): string[] {
462 4 : return category === 'tag:models' && includesLibrary()
463 : ? [category, libraryCategory]
464 : : [category]
465 : }
466 :
467 2 : function isLoading(key: string): boolean {
468 2 : const category = resolveCategory(key)
469 2 : if (!category) return false
470 2 : return sourceCategories(category).some(
471 : (source) => modelStateByCategory.value.get(source)?.isLoading ?? false
472 : )
473 : }
474 :
475 1 : function getError(key: string): Error | undefined {
476 1 : const category = resolveCategory(key)
477 1 : if (!category) return undefined
478 1 : return sourceCategories(category)
479 : .map((source) => modelStateByCategory.value.get(source)?.error)
480 : .find((error) => error !== undefined)
481 : }
482 :
483 0 : function hasMore(key: string): boolean {
484 0 : const category = resolveCategory(key)
485 0 : if (!category) return false
486 0 : return sourceCategories(category).some(
487 : (source) => modelStateByCategory.value.get(source)?.hasMore ?? false
488 : )
489 : }
490 :
491 0 : function hasAssetKey(key: string): boolean {
492 0 : const category = resolveCategory(key)
493 0 : if (!category) return false
494 0 : return modelStateByCategory.value.has(category)
495 : }
496 :
497 : /**
498 : * Check if a category exists in the cache.
499 : * Checks both direct category keys and tag-prefixed keys.
500 : * @param category The category to check (e.g., 'checkpoints', 'loras')
501 : */
502 0 : function hasCategory(category: string): boolean {
503 0 : return (
504 : modelStateByCategory.value.has(category) ||
505 : modelStateByCategory.value.has(`tag:${category}`)
506 : )
507 : }
508 :
509 1 : async function updateModelsForCategory(
510 : category: string,
511 : fetcher: (options: AssetPaginationOptions) => Promise<AssetResponse>,
512 : { restart = false }: ModelRefreshOptions = {}
513 : ): Promise<void> {
514 1 : if (!restart && pendingPromiseByCategory.has(category)) {
515 0 : return pendingPromiseByCategory.get(category)!
516 : }
517 :
518 1 : const existingState = modelStateByCategory.value.get(category)
519 1 : const state = createState(existingState?.assets)
520 :
521 1 : const supersededFetch = abortByCategory.get(category)
522 1 : supersededFetch?.abort()
523 1 : const controller = new AbortController()
524 1 : abortByCategory.set(category, controller)
525 :
526 1 : const seenIds = new Set<string>()
527 :
528 1 : const hasExistingData = existingState?.isLoading === false
529 1 : pendingRequestByCategory.set(category, state)
530 1 : if (!hasExistingData) {
531 1 : modelStateByCategory.value.set(category, state)
532 : }
533 :
534 1 : async function loadBatches(): Promise<void> {
535 1 : let isFirstBatch = true
536 1 : let walkCompleted = false
537 1 : const seenCursors = new Set<string>()
538 1 : let batchCount = 0
539 1 : while (state.hasMore) {
540 1 : if (batchCount++ >= MAX_PAGINATION_BATCHES) {
541 0 : state.hasMore = false
542 0 : break
543 : }
544 1 : try {
545 1 : const after = state.nextCursor
546 1 : const response = await fetcher(
547 : after !== undefined
548 : ? {
549 : limit: MODEL_BATCH_SIZE,
550 : after,
551 : signal: controller.signal
552 : }
553 : : {
554 : limit: MODEL_BATCH_SIZE,
555 : offset: state.offset,
556 : signal: controller.signal
557 : }
558 : )
559 :
560 1 : if (controller.signal.aborted || isStale(category, state)) return
561 :
562 1 : const newAssets = response.assets
563 :
564 1 : if (isFirstBatch) {
565 1 : assetsArrayCache.delete(category)
566 1 : if (hasExistingData) {
567 0 : pendingRequestByCategory.delete(category)
568 0 : modelStateByCategory.value.set(category, state)
569 : }
570 : }
571 :
572 1 : for (const asset of newAssets) {
573 0 : seenIds.add(asset.id)
574 0 : state.assets.set(asset.id, asset)
575 : }
576 1 : state.assets = new Map(state.assets)
577 :
578 1 : state.offset += newAssets.length
579 :
580 1 : if (!response.has_more) {
581 1 : state.hasMore = false
582 1 : walkCompleted = true
583 0 : } else if (newAssets.length === 0) {
584 0 : state.hasMore = false
585 0 : } else if (response.next_cursor !== undefined) {
586 0 : if (seenCursors.has(response.next_cursor)) {
587 0 : state.hasMore = false
588 : } else {
589 0 : seenCursors.add(response.next_cursor)
590 0 : state.nextCursor = response.next_cursor
591 : }
592 : } else {
593 0 : state.nextCursor = undefined
594 0 : if (newAssets.length < MODEL_BATCH_SIZE) {
595 0 : state.hasMore = false
596 0 : walkCompleted = true
597 : }
598 : }
599 :
600 1 : if (isFirstBatch) {
601 1 : state.isLoading = false
602 1 : isFirstBatch = false
603 : }
604 :
605 1 : if (state.hasMore) {
606 0 : await delay(50, { signal: controller.signal })
607 : }
608 : } catch (err) {
609 0 : if (controller.signal.aborted) {
610 0 : if (isFirstBatch) state.isLoading = false
611 0 : return
612 : }
613 0 : if (isStale(category, state)) return
614 0 : console.error(`Error loading batch for ${category}:`, err)
615 :
616 0 : if (category === libraryCategory) {
617 0 : reportError(err, {
618 : surface: 'assets',
619 : errorType: 'error_loading_workspace_library_models'
620 : })
621 : }
622 0 : state.error = err instanceof Error ? err : new Error(String(err))
623 0 : state.hasMore = false
624 0 : state.isLoading = false
625 0 : const committed = modelStateByCategory.value.get(category)
626 0 : if (committed && committed !== state) {
627 0 : committed.error = state.error
628 : }
629 0 : if (pendingRequestByCategory.get(category) === state) {
630 0 : pendingRequestByCategory.delete(category)
631 : }
632 :
633 0 : return
634 : }
635 : }
636 :
637 1 : if (walkCompleted) {
638 1 : const staleIds = [...state.assets.keys()].filter(
639 : (id) => !seenIds.has(id)
640 : )
641 1 : for (const id of staleIds) {
642 0 : state.assets.delete(id)
643 : }
644 : }
645 1 : assetsArrayCache.delete(category)
646 1 : if (pendingRequestByCategory.get(category) === state) {
647 1 : pendingRequestByCategory.delete(category)
648 : }
649 : }
650 :
651 1 : const promise = loadBatches().then(() =>
652 : replacementBySupersededFetch.get(controller)
653 : )
654 1 : if (supersededFetch) {
655 0 : replacementBySupersededFetch.set(supersededFetch, promise)
656 : }
657 1 : pendingPromiseByCategory.set(category, promise)
658 1 : try {
659 1 : await promise
660 : } finally {
661 1 : if (pendingPromiseByCategory.get(category) === promise) {
662 1 : pendingPromiseByCategory.delete(category)
663 : }
664 1 : if (abortByCategory.get(category) === controller) {
665 1 : abortByCategory.delete(category)
666 : }
667 : }
668 : }
669 :
670 : /**
671 : * Fetch and cache model assets for a specific node type.
672 : * Translates nodeType to category internally - multiple node types
673 : * sharing the same category will share the same cache entry.
674 : * @param nodeType The node type to fetch assets for (e.g., 'CheckpointLoaderSimple')
675 : * @param options.restart Supersede an in-flight fetch instead of joining it
676 : */
677 0 : async function updateModelsForNodeType(
678 : nodeType: string,
679 : options?: ModelRefreshOptions
680 : ): Promise<void> {
681 0 : const category = modelToNodeStore.getCategoryForNodeType(nodeType)
682 0 : if (!category) return
683 :
684 0 : await updateModelsForCategory(
685 : category,
686 : (opts) => assetService.getAssetsPageForNodeType(nodeType, opts),
687 : options
688 : )
689 : }
690 :
691 : /**
692 : * Fetch and cache model assets for a specific tag
693 : * @param tag The tag to fetch assets for (e.g., 'models')
694 : * @param options.restart Supersede an in-flight fetch instead of joining it
695 : */
696 1 : async function updateModelsForTag(
697 : tag: string,
698 : options?: ModelRefreshOptions
699 : ): Promise<void> {
700 1 : const category = `tag:${tag}`
701 1 : await Promise.all(
702 : sourceCategories(category).map((source) =>
703 : updateModelsForCategory(
704 : source,
705 : source === libraryCategory
706 : ? (opts) => assetService.getWorkspaceLibraryAssetsPage(opts)
707 : : (opts) => assetService.getAssetsPageByTag(tag, true, opts),
708 : options
709 : )
710 : )
711 : )
712 : }
713 :
714 : /**
715 : * Invalidate the cache for a specific category.
716 : * Forces a refetch on next access.
717 : * @param category The category to invalidate (e.g., 'checkpoints', 'loras')
718 : */
719 0 : function invalidateCategory(category: string): void {
720 0 : if (category === 'tag:models') invalidateCategory(libraryCategory)
721 0 : abortByCategory.get(category)?.abort()
722 0 : abortByCategory.delete(category)
723 0 : modelStateByCategory.value.delete(category)
724 0 : assetsArrayCache.delete(category)
725 0 : pendingRequestByCategory.delete(category)
726 0 : pendingPromiseByCategory.delete(category)
727 : }
728 :
729 : /**
730 : * Optimistically update an asset in the cache
731 : * @param assetId The asset ID to update
732 : * @param updates Partial asset data to merge
733 : * @param cacheKey Optional cache key to target (nodeType or 'tag:xxx')
734 : */
735 0 : function updateAssetInCache(
736 : assetId: string,
737 : updates: Partial<AssetItem>,
738 : cacheKey?: string
739 : ) {
740 0 : const category = cacheKey ? resolveCategory(cacheKey) : undefined
741 0 : if (cacheKey && !category) return
742 :
743 0 : const categoriesToCheck = category
744 : ? sourceCategories(category)
745 : : Array.from(modelStateByCategory.value.keys())
746 :
747 0 : for (const cat of categoriesToCheck) {
748 0 : const state = modelStateByCategory.value.get(cat)
749 0 : if (!state?.assets) continue
750 :
751 0 : const existingAsset = state.assets.get(assetId)
752 0 : if (existingAsset) {
753 0 : const updatedAsset = { ...existingAsset, ...updates }
754 0 : state.assets.set(assetId, updatedAsset)
755 0 : assetsArrayCache.delete(cat)
756 0 : if (cacheKey) return
757 : }
758 : }
759 : }
760 :
761 0 : async function updateAssetMetadata(
762 : asset: AssetItem,
763 : userMetadata: Record<string, unknown>,
764 : cacheKey?: string
765 : ) {
766 0 : const originalMetadata = asset.user_metadata
767 0 : updateAssetInCache(asset.id, { user_metadata: userMetadata }, cacheKey)
768 :
769 0 : try {
770 0 : const updatedAsset = await assetService.updateAsset(asset.id, {
771 : user_metadata: userMetadata
772 : })
773 0 : updateAssetInCache(asset.id, updatedAsset, cacheKey)
774 0 : return true
775 : } catch (error) {
776 0 : reportError(error, {
777 : surface: 'assets',
778 : errorType: 'error_updating_asset_metadata'
779 : })
780 0 : toast.error(t('assetBrowser.modelInfo.saveFailed'))
781 0 : updateAssetInCache(
782 : asset.id,
783 : { user_metadata: originalMetadata },
784 : cacheKey
785 : )
786 0 : return false
787 : }
788 : }
789 :
790 : /**
791 : * Update asset tags using add/remove endpoints
792 : * @param asset The asset to update (used to read current tags)
793 : * @param newTags The desired tags array
794 : * @param cacheKey Optional cache key to target for optimistic update
795 : */
796 0 : async function updateAssetTags(
797 : asset: AssetItem,
798 : newTags: string[],
799 : cacheKey?: string
800 : ) {
801 0 : const originalTags = asset.tags
802 0 : const tagsToAdd = difference(newTags, originalTags)
803 0 : const tagsToRemove = difference(originalTags, newTags)
804 :
805 0 : if (tagsToAdd.length === 0 && tagsToRemove.length === 0) return
806 :
807 0 : updateAssetInCache(asset.id, { tags: newTags }, cacheKey)
808 :
809 0 : let removedTagsOnServer: string[] = []
810 0 : try {
811 0 : let removeResult: TagsOperationResult | undefined
812 0 : if (tagsToRemove.length > 0) {
813 0 : removeResult = await assetService.removeAssetTags(
814 : asset.id,
815 : tagsToRemove
816 : )
817 0 : removedTagsOnServer = removeResult.removed ?? tagsToRemove
818 : }
819 :
820 : const addResult =
821 : tagsToAdd.length > 0
822 : ? await assetService.addAssetTags(asset.id, tagsToAdd)
823 : : undefined
824 :
825 0 : const finalTags = (addResult ?? removeResult)?.total_tags
826 0 : if (finalTags) {
827 0 : updateAssetInCache(asset.id, { tags: finalTags }, cacheKey)
828 : }
829 : } catch (error) {
830 0 : reportError(error, {
831 : surface: 'assets',
832 : errorType: 'error_updating_asset_tags'
833 : })
834 0 : toast.error(t('assetBrowser.modelInfo.saveFailed'))
835 0 : updateAssetInCache(asset.id, { tags: originalTags }, cacheKey)
836 :
837 0 : if (removedTagsOnServer.length > 0) {
838 0 : try {
839 0 : await assetService.addAssetTags(asset.id, removedTagsOnServer)
840 : } catch (compensationError) {
841 0 : console.error(
842 : 'Failed to restore tags after partial failure; invalidating cache to force refetch:',
843 : compensationError
844 : )
845 0 : const categoriesToInvalidate = new Set<string>()
846 0 : const resolved = cacheKey ? resolveCategory(cacheKey) : undefined
847 0 : if (resolved) {
848 0 : categoriesToInvalidate.add(resolved)
849 : }
850 0 : for (const [
851 : category,
852 : state
853 : ] of modelStateByCategory.value.entries()) {
854 0 : if (state.assets.has(asset.id)) {
855 0 : categoriesToInvalidate.add(category)
856 : }
857 : }
858 0 : for (const category of categoriesToInvalidate) {
859 0 : invalidateCategory(category)
860 : }
861 : }
862 : }
863 : }
864 : }
865 :
866 : /**
867 : * Invalidate model caches for a given category (e.g., 'checkpoints', 'loras')
868 : * Clears the category cache and tag-based caches so next access triggers refetch
869 : * @param category The model category to invalidate (e.g., 'checkpoints')
870 : */
871 0 : function invalidateModelsForCategory(category: string): void {
872 0 : invalidateCategory(category)
873 0 : invalidateCategory(`tag:${category}`)
874 0 : invalidateCategory('tag:models')
875 : }
876 :
877 2355 : return {
878 : getAssets,
879 : isLoading,
880 : getError,
881 : hasMore,
882 : hasAssetKey,
883 : hasCategory,
884 : updateModelsForNodeType,
885 : updateModelsForTag,
886 : invalidateCategory,
887 : updateAssetMetadata,
888 : updateAssetTags,
889 : invalidateModelsForCategory
890 : }
891 : }
892 :
893 2355 : const {
894 : getAssets,
895 : isLoading: isModelLoading,
896 : getError,
897 : hasMore,
898 : hasAssetKey,
899 : hasCategory,
900 : updateModelsForNodeType,
901 : updateModelsForTag,
902 : invalidateCategory,
903 : updateAssetMetadata,
904 : updateAssetTags,
905 : invalidateModelsForCategory
906 : } = getModelState()
907 :
908 : // Watch for completed downloads and refresh model caches
909 2355 : whenever(
910 : () => assetDownloadStore.lastCompletedDownload,
911 : async (latestDownload) => {
912 0 : const { modelType } = latestDownload
913 :
914 0 : const providers = modelToNodeStore
915 : .getAllNodeProviders(modelType)
916 : .filter((provider) => provider.nodeDef.name)
917 :
918 0 : const firstProviderOfEachCategory = new Set(
919 : uniqBy(providers, (provider) =>
920 : modelToNodeStore.getCategoryForNodeType(provider.nodeDef.name)
921 : )
922 : )
923 0 : const nodeTypeUpdates = providers.map((provider) =>
924 : updateModelsForNodeType(provider.nodeDef.name, {
925 : restart: firstProviderOfEachCategory.has(provider)
926 : }).then(() => provider.nodeDef.name)
927 : )
928 :
929 0 : const tagUpdates = [
930 : updateModelsForTag(modelType, { restart: true }),
931 : updateModelsForTag('models', { restart: true })
932 : ]
933 :
934 0 : const results = await Promise.allSettled([
935 : ...nodeTypeUpdates,
936 : ...tagUpdates
937 : ])
938 :
939 0 : for (const result of results) {
940 0 : if (result.status === 'rejected') {
941 0 : console.error(
942 : `Failed to refresh model cache for provider: ${result.reason}`
943 : )
944 : }
945 : }
946 : }
947 : )
948 :
949 2355 : return {
950 : // States
951 : allAssets,
952 : flatOutputAssets,
953 : inputAssets,
954 : outputAssets,
955 : invalidateAll,
956 : loadOutputAsset,
957 :
958 : // Deletion tracking
959 : deletingAssetIds,
960 : setAssetDeleting,
961 : isAssetDeleting,
962 :
963 : // Input mapping helpers
964 : inputAssetsByFilename,
965 : getInputName,
966 :
967 : // Model assets - accessors
968 : getAssets,
969 : isModelLoading,
970 : getError,
971 : hasMore,
972 : hasAssetKey,
973 : hasCategory,
974 :
975 : // Model assets - actions
976 : updateModelsForNodeType,
977 : updateModelsForTag,
978 : invalidateCategory,
979 : updateAssetMetadata,
980 : updateAssetTags,
981 : invalidateModelsForCategory
982 : }
983 : })
|