Line data Source code
1 : import type { ListAssetsData } from '@comfyorg/ingest-types'
2 : import { uniq } from 'es-toolkit'
3 : import { fromZodError } from 'zod-validation-error'
4 : import { z } from 'zod'
5 :
6 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
7 : import { st } from '@/i18n'
8 : import { ASSETS_SEED_FAST_COMPLETE_EVENT } from '@/platform/assets/constants/assetEvents'
9 :
10 : import {
11 : assetFilenameSchema,
12 : assetItemSchema,
13 : assetResponseSchema,
14 : asyncUploadResponseSchema,
15 : tagsOperationResultSchema
16 : } from '@/platform/assets/schemas/assetSchema'
17 : import type {
18 : AssetId,
19 : AssetItem,
20 : AssetMetadata,
21 : AssetResponse,
22 : AssetUpdatePayload,
23 : AsyncUploadResponse,
24 : ModelFile,
25 : TagsOperationResult
26 : } from '@/platform/assets/schemas/assetSchema'
27 : import {
28 : getAssetCategories,
29 : getAssetFilename,
30 : isModelTypeCovered
31 : } from '@/platform/assets/utils/assetMetadataUtils'
32 : import { isCloud } from '@/platform/distribution/types'
33 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
34 : import { api } from '@/scripts/api'
35 : import { useModelToNodeStore } from '@/stores/modelToNodeStore'
36 : import { parseErrorResponse } from '@/platform/remote/comfyui/errors'
37 :
38 : export interface PaginationOptions {
39 : limit?: number
40 : offset?: number
41 : }
42 :
43 : export interface AssetPaginationOptions extends PaginationOptions {
44 : /**
45 : * Opaque keyset cursor from a prior response's `next_cursor`. When set, the
46 : * server resumes after that cursor and `offset` is ignored.
47 : */
48 : after?: string
49 : signal?: AbortSignal
50 : }
51 :
52 : interface AssetRequestOptions extends PaginationOptions {
53 : includeTags: string[]
54 : excludeTags?: string[]
55 : includePublic?: boolean
56 : workspaceLibrary?: boolean
57 : after?: string
58 : signal?: AbortSignal
59 : }
60 :
61 : interface AssetExportOptions {
62 : job_ids?: string[]
63 : asset_ids?: AssetId[]
64 : naming_strategy?:
65 : | 'group_by_job_id'
66 : | 'group_by_job_time'
67 : | 'preserve'
68 : | 'asset_id'
69 : job_asset_name_filters?: Record<string, string[]>
70 : include_previews?: boolean
71 : }
72 :
73 : /**
74 : * Maps CivitAI validation error codes to localized error messages
75 : */
76 0 : function getLocalizedErrorMessage(errorCode: string): string {
77 : const errorMessages: Record<string, string> = {
78 : // Validation errors
79 : FILE_TOO_LARGE: st('assetBrowser.errorFileTooLarge', 'File too large'),
80 : FORMAT_NOT_ALLOWED: st(
81 : 'assetBrowser.errorFormatNotAllowed',
82 : 'Format not allowed'
83 : ),
84 : UNSAFE_PICKLE_SCAN: st(
85 : 'assetBrowser.errorUnsafePickleScan',
86 : 'Unsafe pickle scan'
87 : ),
88 : UNSAFE_VIRUS_SCAN: st(
89 : 'assetBrowser.errorUnsafeVirusScan',
90 : 'Unsafe virus scan'
91 : ),
92 : MODEL_TYPE_NOT_SUPPORTED: st(
93 : 'assetBrowser.errorModelTypeNotSupported',
94 : 'Model type not supported'
95 : ),
96 :
97 : // HTTP 400 - Bad Request
98 : INVALID_URL: st('assetBrowser.errorInvalidUrl', 'Please provide a URL.'),
99 : INVALID_URL_FORMAT: st(
100 : 'assetBrowser.errorInvalidUrlFormat',
101 : 'The URL format is invalid. Please check and try again.'
102 : ),
103 : UNSUPPORTED_SOURCE: st(
104 : 'assetBrowser.errorUnsupportedSource',
105 : 'This URL is not supported. Only Hugging Face and Civitai URLs are allowed.'
106 : ),
107 :
108 : // HTTP 401 - Unauthorized
109 : UNAUTHORIZED: st(
110 : 'assetBrowser.errorUnauthorized',
111 : 'Please sign in to continue.'
112 : ),
113 :
114 : // HTTP 422 - External Source Errors
115 : USER_TOKEN_INVALID: st(
116 : 'assetBrowser.errorUserTokenInvalid',
117 : 'Your stored API token is invalid or expired. Please update your token in settings.'
118 : ),
119 : USER_TOKEN_ACCESS_DENIED: st(
120 : 'assetBrowser.errorUserTokenAccessDenied',
121 : 'Your API token does not have access to this resource. Please check your token permissions.'
122 : ),
123 : UNAUTHORIZED_SOURCE: st(
124 : 'assetBrowser.errorUnauthorizedSource',
125 : 'This resource requires authentication. Please add your API token in settings.'
126 : ),
127 : ACCESS_FORBIDDEN: st(
128 : 'assetBrowser.errorAccessForbidden',
129 : 'Access to this resource is forbidden.'
130 : ),
131 : RESOURCE_NOT_FOUND: st(
132 : 'assetBrowser.errorResourceNotFound',
133 : 'The file was not found. Please check the URL and try again.'
134 : ),
135 : RATE_LIMITED: st(
136 : 'assetBrowser.errorRateLimited',
137 : 'Too many requests. Please try again in a few minutes.'
138 : ),
139 : SOURCE_SERVER_ERROR: st(
140 : 'assetBrowser.errorSourceServerError',
141 : 'The source server is experiencing issues. Please try again later.'
142 : ),
143 : NETWORK_TIMEOUT: st(
144 : 'assetBrowser.errorNetworkTimeout',
145 : 'Request timed out. Please try again.'
146 : ),
147 : CONNECTION_REFUSED: st(
148 : 'assetBrowser.errorConnectionRefused',
149 : 'Unable to connect to the source. Please try again later.'
150 : ),
151 : INVALID_HOST: st(
152 : 'assetBrowser.errorInvalidHost',
153 : 'The source URL hostname could not be resolved.'
154 : ),
155 : NETWORK_ERROR: st(
156 : 'assetBrowser.errorNetworkError',
157 : 'A network error occurred. Please check your connection and try again.'
158 : ),
159 : REQUEST_CANCELLED: st(
160 : 'assetBrowser.errorRequestCancelled',
161 : 'Request was cancelled.'
162 : ),
163 : DOWNLOAD_CANCELLED: st(
164 : 'assetBrowser.errorDownloadCancelled',
165 : 'Download was cancelled.'
166 : ),
167 : METADATA_FETCH_FAILED: st(
168 : 'assetBrowser.errorMetadataFetchFailed',
169 : 'Failed to fetch file information from the source.'
170 : ),
171 : HTTP_ERROR: st(
172 : 'assetBrowser.errorHttpError',
173 : 'An error occurred while fetching metadata.'
174 : ),
175 :
176 : // HTTP 500 - Internal Server Errors
177 : SERVICE_UNAVAILABLE: st(
178 : 'assetBrowser.errorServiceUnavailable',
179 : 'Service temporarily unavailable. Please try again later.'
180 : ),
181 : INTERNAL_ERROR: st(
182 : 'assetBrowser.errorInternalError',
183 : 'An unexpected error occurred. Please try again.'
184 : )
185 : }
186 0 : return (
187 : errorMessages[errorCode] ||
188 : st('assetBrowser.errorUnknown', 'Unknown error') ||
189 : 'Unknown error'
190 : )
191 : }
192 :
193 2356 : const ASSETS_ENDPOINT = '/assets'
194 2356 : const ASSETS_SEED_ENDPOINT = '/assets/seed'
195 2356 : const ASSETS_DOWNLOAD_ENDPOINT = '/assets/download'
196 2356 : const ASSETS_EXPORT_ENDPOINT = '/assets/export'
197 2356 : const DEFAULT_LIMIT = 500
198 : // Defensive backstop against a server that never signals exhaustion (e.g. an
199 : // unbounded stream of unique cursors); mirrors assetsStore's walk cap. At
200 : // DEFAULT_LIMIT per page this allows 500k assets per walk, so it only ever
201 : // trips on pathological pagination.
202 2356 : const MAX_PAGINATION_BATCHES = 1000
203 :
204 2356 : export const MODELS_TAG = 'models'
205 : export const INPUT_TAG = 'input'
206 : export const OUTPUT_TAG = 'output'
207 : /** Asset tag used by the backend for placeholder records that are not installed. */
208 2356 : export const MISSING_TAG = 'missing'
209 2356 : const DEFAULT_EXCLUDED_ASSET_TAGS = [MISSING_TAG]
210 2356 : const EMPTY_PAGE: AssetResponse = { assets: [], total: 0, has_more: false }
211 :
212 2356 : const uploadedAssetResponseSchema = assetItemSchema.extend({
213 : created_new: z.boolean()
214 : })
215 :
216 52 : function normalizeAssetTags(tags: string[]): string[] {
217 52 : return tags.map((tag) => tag.trim()).filter(Boolean)
218 : }
219 :
220 : /**
221 : * Orders loader paths as subdirectories before files at every level,
222 : * alphabetical within each group. The asset API returns models in storage
223 : * order, which would otherwise interleave root-level files with folder
224 : * contents in the sidebar tree.
225 : */
226 70 : function compareLoaderPaths(a: string, b: string): number {
227 70 : const aSegments = a.split('/')
228 70 : const bSegments = b.split('/')
229 70 : const sharedDepth = Math.min(aSegments.length, bSegments.length)
230 70 : for (let i = 0; i < sharedDepth; i++) {
231 70 : const aIsFile = i === aSegments.length - 1
232 : const bIsFile = i === bSegments.length - 1
233 70 : if (aIsFile !== bIsFile) return aIsFile ? 1 : -1
234 38 : const order = aSegments[i].localeCompare(bSegments[i], undefined, {
235 : numeric: true
236 : })
237 38 : if (order !== 0) return order
238 : }
239 0 : return 0
240 : }
241 :
242 2356 : const WORKSPACE_LIBRARY_PARAM = 'workspace_library' satisfies keyof NonNullable<
243 : ListAssetsData['query']
244 : >
245 :
246 : /**
247 : * Validates asset response data using Zod schema
248 : */
249 23 : function validateAssetResponse(data: unknown): AssetResponse {
250 23 : const result = assetResponseSchema.safeParse(data)
251 23 : if (result.success) return result.data
252 :
253 0 : const error = fromZodError(result.error)
254 0 : throw new Error(`Invalid asset response against zod schema:\n${error}`)
255 : }
256 :
257 0 : function validateUploadedAssetResponse(
258 : data: unknown
259 : ): AssetItem & { created_new: boolean } {
260 0 : const result = uploadedAssetResponseSchema.safeParse(data)
261 0 : if (result.success) {
262 0 : return result.data
263 : }
264 :
265 0 : console.error('Invalid asset upload response:', fromZodError(result.error))
266 0 : throw new Error(
267 : st(
268 : 'assetBrowser.errorUploadFailed',
269 : 'Failed to upload asset. Please try again.'
270 : )
271 : )
272 : }
273 :
274 : /**
275 : * Private service for asset-related network requests
276 : * Not exposed globally - used internally by ComfyApi
277 : */
278 2356 : function createAssetService() {
279 2356 : const assetChangeListeners = new Set<() => void>()
280 :
281 : /**
282 : * Model assets bucketed by folder category, built from a single walk of the
283 : * `models` tag rather than a fetch per category. Shared by the folder list
284 : * and per-folder listings so the sidebar loads every model in one pass.
285 : */
286 2356 : let modelBuckets: Map<string, AssetItem[]> | null = null
287 2356 : let modelBucketsRequestId = 0
288 2356 : let pendingModelBuckets: Promise<Map<string, AssetItem[]>> | null = null
289 : // The supports_model_type_tags value the cached buckets were built under.
290 : // The flag arrives asynchronously over the websocket handshake, so a cache
291 : // built before it lands (or surviving a reconnect that flips it) groups tags
292 : // by the wrong scheme and must be discarded.
293 2356 : let modelBucketsMode: boolean | null = null
294 :
295 : /**
296 : * Discards the cached model buckets so the next read re-walks the models
297 : * tag. Bumping the request id keeps a walk that was already in flight from
298 : * repopulating the cache with pre-invalidation data.
299 : */
300 42 : function invalidateModelBuckets(): void {
301 42 : modelBucketsRequestId++
302 42 : modelBuckets = null
303 42 : pendingModelBuckets = null
304 : }
305 :
306 : /**
307 : * Handles API response with consistent error handling and Zod validation
308 : */
309 26 : async function handleAssetRequest(
310 : options: AssetRequestOptions,
311 : context: string
312 : ): Promise<AssetResponse> {
313 26 : const {
314 : includeTags,
315 : excludeTags = DEFAULT_EXCLUDED_ASSET_TAGS,
316 : limit = DEFAULT_LIMIT,
317 : offset,
318 : after,
319 : includePublic,
320 : workspaceLibrary,
321 : signal
322 : } = options
323 26 : const normalizedIncludeTags = normalizeAssetTags(includeTags)
324 26 : const normalizedExcludeTags = normalizeAssetTags(excludeTags)
325 :
326 26 : const queryParams = new URLSearchParams({
327 : include_tags: normalizedIncludeTags.join(','),
328 : limit: limit.toString()
329 : })
330 26 : if (normalizedExcludeTags.length > 0) {
331 26 : queryParams.set('exclude_tags', normalizedExcludeTags.join(','))
332 : }
333 : // `after` (keyset cursor) takes precedence over `offset`; the server ignores
334 : // `offset` when a cursor is supplied, so we avoid sending a redundant param.
335 26 : if (after !== undefined) {
336 0 : queryParams.set('after', after)
337 26 : } else if (offset !== undefined && offset > 0) {
338 0 : queryParams.set('offset', offset.toString())
339 : }
340 26 : if (includePublic !== undefined) {
341 26 : queryParams.set('include_public', includePublic ? 'true' : 'false')
342 : }
343 26 : if (workspaceLibrary) {
344 0 : queryParams.set(WORKSPACE_LIBRARY_PARAM, 'true')
345 : }
346 :
347 26 : const url = `${ASSETS_ENDPOINT}?${queryParams.toString()}`
348 26 : const res = signal
349 : ? await api.fetchApi(url, { signal })
350 : : await api.fetchApi(url)
351 25 : if (!res.ok) {
352 2 : throw new Error(
353 : `Unable to load ${context}: Server returned ${res.status}. Please try again.`
354 : )
355 : }
356 : const data = await res.json()
357 23 : return validateAssetResponse(data)
358 : }
359 : /**
360 : * Walks every `models`-tagged asset once and buckets each into the folder
361 : * categories `getAssetCategories` resolves for it. A single asset lands in
362 : * every category it is tagged with (e.g. a shared-root model in both
363 : * `checkpoints` and `diffusion_models`); an asset covered by `model_type:`
364 : * tags is grouped by those alone, so a legacy bare-tag twin left over from a
365 : * partial re-tagging cannot also cross-list it into another folder. Which
366 : * folders are actually shown is decided by `/experiment/models`; models with
367 : * no category tag are dropped with a warning rather than hidden silently.
368 : */
369 25 : async function buildModelBuckets(
370 : modelTypeMode: boolean
371 : ): Promise<Map<string, AssetItem[]>> {
372 : // Private-only, matching the legacy per-folder listing this walk
373 : // replaces: the local sidebar never surfaced public/community assets.
374 25 : const assets = await getAllAssetsByTag(MODELS_TAG, false)
375 22 : const buckets = new Map<string, AssetItem[]>()
376 :
377 23 : for (const asset of assets) {
378 106 : const folders = [...new Set(getAssetCategories(asset, modelTypeMode))]
379 :
380 106 : if (folders.length === 0) {
381 0 : console.warn(
382 : `Asset ${asset.id} (${asset.name}) is tagged '${MODELS_TAG}' but has no model category; skipping.`
383 : )
384 0 : continue
385 : }
386 :
387 : // On loader_path-contract backends a null loader_path marks an
388 : // unloadable asset (e.g. an orphan): it must not mint a widget value,
389 : // and `name` is deprecated for path semantics.
390 106 : if (modelTypeMode && !asset.loader_path) {
391 17 : console.warn(
392 : `Asset ${asset.id} (${asset.name}) has no loader_path; skipping.`
393 : )
394 17 : continue
395 : }
396 :
397 : // The loader value flows into viewMetadata URLs and widget values, so a
398 : // traversal-shaped path must not pass through even if the backend's own
399 : // validation ever regresses.
400 106 : const loaderValue = asset.loader_path ?? getAssetFilename(asset)
401 106 : if (!assetFilenameSchema.safeParse(loaderValue).success) {
402 0 : console.warn(
403 : `Asset ${asset.id} (${asset.name}) has an unsafe loader path ('${loaderValue}'); skipping.`
404 : )
405 0 : continue
406 : }
407 :
408 89 : for (const folder of folders) {
409 89 : const bucket = buckets.get(folder)
410 89 : if (bucket) bucket.push(asset)
411 37 : else buckets.set(folder, [asset])
412 : }
413 : }
414 :
415 23 : for (const bucket of buckets.values()) {
416 37 : bucket.sort((a, b) =>
417 : compareLoaderPaths(
418 : a.loader_path ?? getAssetFilename(a),
419 : b.loader_path ?? getAssetFilename(b)
420 : )
421 : )
422 : }
423 :
424 22 : return buckets
425 : }
426 :
427 : /** Returns the memoized model buckets, walking the models tag on first read. */
428 69 : async function loadModelBuckets(): Promise<Map<string, AssetItem[]>> {
429 69 : const modelTypeMode = useFeatureFlags().flags.supportsModelTypeTags
430 : // Discard a cache (or in-flight walk) built under a different flag value:
431 : // the buckets would key tags by the wrong scheme otherwise.
432 69 : if (modelBucketsMode !== modelTypeMode) invalidateModelBuckets()
433 69 : if (modelBuckets) return modelBuckets
434 69 : if (pendingModelBuckets) return pendingModelBuckets
435 :
436 25 : modelBucketsMode = modelTypeMode
437 25 : const requestId = ++modelBucketsRequestId
438 25 : const walk = async () => {
439 25 : try {
440 25 : const buckets = await buildModelBuckets(modelTypeMode)
441 22 : if (requestId === modelBucketsRequestId) {
442 21 : modelBuckets = buckets
443 : }
444 22 : return buckets
445 : } finally {
446 24 : if (requestId === modelBucketsRequestId) {
447 23 : pendingModelBuckets = null
448 : }
449 : }
450 : }
451 :
452 25 : pendingModelBuckets = walk()
453 25 : return pendingModelBuckets
454 : }
455 :
456 : /**
457 : * Gets the models in the specified folder from the single models walk.
458 : * @param folder The folder to list models from, such as 'checkpoints'
459 : * @returns The list of model filenames within the specified folder
460 : */
461 69 : async function getAssetModels(folder: string): Promise<ModelFile[]> {
462 69 : const modelTypeMode = useFeatureFlags().flags.supportsModelTypeTags
463 69 : const buckets = await loadModelBuckets()
464 : const assets =
465 : buckets.get(folder) ??
466 : buckets
467 : .get(folder.split('/')[0])
468 : ?.filter(
469 : (asset) =>
470 : !(modelTypeMode && isModelTypeCovered(asset)) &&
471 : asset.tags.some(
472 : (tag) => tag === folder || tag.startsWith(`${folder}/`)
473 : )
474 : )
475 :
476 69 : return (assets ?? []).map((asset) => ({
477 : // `loader_path` is the category-relative path the loader widget expects
478 : // and the source for the sidebar tree. Backends that predate it (bare-tag
479 : // mode; today's cloud) fall back to the filename metadata — the same
480 : // value the asset browser serializes — rather than `name`, which is a
481 : // content hash on cloud.
482 : name: asset.loader_path ?? getAssetFilename(asset),
483 : // Asset records carry no root identity, so every model reports root 0.
484 : // Known limitation on multi-root categories (extra_model_paths.yaml):
485 : // preview reads target root 0 (wrong file or 404 for secondary-root
486 : // files), and same-relative-path files in different roots collapse
487 : // onto one sidebar row. Metadata is unaffected unless relative paths
488 : // collide (/view_metadata searches roots in order without an index),
489 : // as are loader widget values; lifting this needs the backend to carry
490 : // root identity on assets.
491 : pathIndex: 0
492 : }))
493 : }
494 :
495 : /**
496 : * Asks the backend to rescan the model roots on disk so newly added files
497 : * become assets. Fire-and-forget: the scan's fast (insert) phase already
498 : * writes the category tags and filenames the sidebar needs and is announced
499 : * by an `assets.seed.fast_complete` websocket event. A 409 means a scan is
500 : * already running, which will emit the same event, so it is not an error.
501 : */
502 1 : async function seedModelAssets(): Promise<void> {
503 1 : const res = await api.fetchApi(ASSETS_SEED_ENDPOINT, {
504 : method: 'POST',
505 : headers: { 'Content-Type': 'application/json' },
506 : body: JSON.stringify({ roots: ['models'] })
507 : })
508 1 : if (!res.ok && res.status !== 409) {
509 0 : throw new Error(
510 : `Unable to start asset scan: Server returned ${res.status}`
511 : )
512 : }
513 : }
514 :
515 : /**
516 : * Subscribes to the backend's scan fast-phase completion broadcast — the
517 : * moment newly scanned files' tags and loader paths become queryable. The
518 : * wire-level event name is shared via `ASSETS_SEED_FAST_COMPLETE_EVENT`;
519 : * consumers receive a callback and an unsubscribe function.
520 : */
521 2355 : function onModelsScanned(callback: () => void | Promise<void>): () => void {
522 2355 : const handler = () => {
523 2 : void callback()
524 : }
525 2355 : api.addCustomEventListener(ASSETS_SEED_FAST_COMPLETE_EVENT, handler)
526 2355 : return () => {
527 0 : api.removeCustomEventListener(ASSETS_SEED_FAST_COMPLETE_EVENT, handler)
528 : }
529 : }
530 :
531 : /**
532 : * Checks if a widget input should use the asset browser based on both input name and node comfyClass
533 : *
534 : * @param nodeType - The ComfyUI node comfyClass (e.g., 'CheckpointLoaderSimple', 'LoraLoader')
535 : * @param widgetName - The name of the widget to check (e.g., 'ckpt_name')
536 : * @returns true if this input should use asset browser
537 : */
538 5339 : function isAssetBrowserEligible(
539 : nodeType: string | undefined,
540 : widgetName: string
541 : ): boolean {
542 5339 : if (!nodeType || !widgetName) return false
543 5339 : return useModelToNodeStore().isModelWidget(nodeType, widgetName)
544 : }
545 :
546 : /**
547 : * Whether widget-embedded asset pickers (canvas + Vue node model widgets) are
548 : * enabled on Comfy Cloud without a deployment: MODEL_NODE_MAPPINGS is maintained for
549 : * cloud asset tagging. NOT the asset-API gate — that is
550 : * useFeatureFlags().flags.assetsEnabled.
551 : */
552 14397 : function isWidgetAssetPickerEnabled(): boolean {
553 14397 : return isCloud && !useDeploymentPickStore().bootDeployment
554 : }
555 :
556 : /**
557 : * Checks if the asset browser should be used for a given node input.
558 : * Combines the cloud environment check, user setting, and eligibility check.
559 : *
560 : * @param nodeType - The ComfyUI node comfyClass
561 : * @param widgetName - The name of the widget to check
562 : * @returns true if this input should use the asset browser
563 : */
564 8701 : function shouldUseWidgetAssetPicker(
565 : nodeType: string | undefined,
566 : widgetName: string
567 : ): boolean {
568 8701 : return (
569 : isWidgetAssetPickerEnabled() &&
570 : isAssetBrowserEligible(nodeType, widgetName)
571 : )
572 : }
573 :
574 : /**
575 : * Gets assets for a specific node type by finding the matching category
576 : * and fetching all assets with that category tag
577 : *
578 : * @param nodeType - The ComfyUI node type (e.g., 'CheckpointLoaderSimple')
579 : * @param options - Pagination options
580 : * @param options.limit - Maximum number of assets to return (default: 500)
581 : * @param options.offset - Number of assets to skip (default: 0)
582 : * @returns Promise<AssetItem[]> - Full asset objects with preserved metadata
583 : */
584 0 : async function getAssetsForNodeType(
585 : nodeType: string,
586 : options: PaginationOptions = {}
587 : ): Promise<AssetItem[]> {
588 : const data = await getAssetsPageForNodeType(nodeType, options)
589 :
590 : // Return full AssetItem[] objects (don't strip like getAssetModels does)
591 0 : return data.assets
592 : }
593 :
594 : /**
595 : * Gets one paginated asset response for a specific node type by finding the
596 : * matching category and fetching assets with that category tag.
597 : *
598 : * Unlike {@link getAssetsForNodeType}, the full response envelope is
599 : * returned so callers can drive keyset cursor pagination from
600 : * `next_cursor`/`has_more`.
601 : *
602 : * @param nodeType - The ComfyUI node type (e.g., 'CheckpointLoaderSimple')
603 : * @param options - Pagination options
604 : * @param options.limit - Maximum number of assets to return (default: 500)
605 : * @param options.offset - Number of assets to skip (ignored when `after` is set)
606 : * @param options.after - Keyset cursor from a prior response's `next_cursor`
607 : * @param options.signal - Optional abort signal for cancelling the request
608 : * @returns Promise<AssetResponse> - Page of assets plus pagination metadata
609 : */
610 0 : async function getAssetsPageForNodeType(
611 : nodeType: string,
612 : {
613 : limit = DEFAULT_LIMIT,
614 : offset = 0,
615 : after,
616 : signal
617 : }: AssetPaginationOptions = {}
618 : ): Promise<AssetResponse> {
619 0 : if (!nodeType || typeof nodeType !== 'string') {
620 0 : return EMPTY_PAGE
621 : }
622 :
623 : const modelToNodeStore = useModelToNodeStore()
624 0 : const category = modelToNodeStore.getCategoryForNodeType(nodeType)
625 :
626 0 : if (!category) {
627 0 : return EMPTY_PAGE
628 : }
629 :
630 0 : return await handleAssetRequest(
631 : {
632 : includeTags: [MODELS_TAG, category],
633 : limit,
634 : offset,
635 : after,
636 : includePublic: true,
637 : signal
638 : },
639 : `assets for ${nodeType}`
640 : )
641 : }
642 :
643 : /**
644 : * Gets complete details for a specific asset by ID
645 : * Calls the detail endpoint which includes user_metadata and all fields
646 : *
647 : * @param id - The asset ID
648 : * @returns Promise<AssetItem> - Complete asset object with user_metadata
649 : */
650 0 : async function getAssetDetails(id: AssetId): Promise<AssetItem> {
651 0 : const res = await api.fetchApi(`${ASSETS_ENDPOINT}/${id}`)
652 0 : if (!res.ok) {
653 0 : throw new Error(
654 : `Unable to load asset details for ${id}: Server returned ${res.status}. Please try again.`
655 : )
656 : }
657 0 : const data = await res.json()
658 :
659 0 : const result = assetItemSchema.safeParse(data)
660 0 : if (result.success) return result.data
661 :
662 0 : const error = fromZodError(result.error)
663 0 : throw new Error(`Invalid asset response against zod schema:\n${error}`)
664 : }
665 :
666 : /**
667 : * Gets assets filtered by a specific tag
668 : *
669 : * @param tag - The tag to filter by (e.g., 'models', 'input')
670 : * @param includePublic - Whether to include public assets (default: true)
671 : * @param options - Pagination options
672 : * @param options.limit - Maximum number of assets to return (default: 500)
673 : * @param options.offset - Number of assets to skip (default: 0)
674 : * @param options.signal - Optional abort signal for cancelling the request
675 : * @returns Promise<AssetItem[]> - Full asset objects filtered by tag, excluding missing assets
676 : */
677 0 : async function getAssetsByTag(
678 : tag: string,
679 : includePublic: boolean = true,
680 : {
681 : limit = DEFAULT_LIMIT,
682 : offset = 0,
683 : after,
684 : signal
685 : }: AssetPaginationOptions = {}
686 : ): Promise<AssetItem[]> {
687 : const data = await getAssetsPageByTag(tag, includePublic, {
688 : limit,
689 : offset,
690 : after,
691 : signal
692 : })
693 :
694 0 : return data.assets
695 : }
696 :
697 : /**
698 : * Gets one paginated asset response filtered by a specific tag.
699 : */
700 26 : async function getAssetsPageByTag(
701 : tag: string,
702 : includePublic: boolean = true,
703 : {
704 : limit = DEFAULT_LIMIT,
705 : offset = 0,
706 : after,
707 : signal
708 : }: AssetPaginationOptions = {}
709 : ): Promise<AssetResponse> {
710 26 : return await handleAssetRequest(
711 : { includeTags: [tag], limit, offset, after, includePublic, signal },
712 : `assets for tag ${tag}`
713 : )
714 : }
715 :
716 0 : async function getWorkspaceLibraryAssetsPage(
717 : options: AssetPaginationOptions = {}
718 : ): Promise<AssetResponse> {
719 0 : return handleAssetRequest(
720 : {
721 : ...options,
722 : includeTags: [MODELS_TAG],
723 : includePublic: false,
724 : workspaceLibrary: true
725 : },
726 : 'workspace library models'
727 : )
728 : }
729 :
730 : /**
731 : * Gets every asset for a tag by walking paginated asset API responses.
732 : *
733 : * Uses keyset (cursor) pagination: each page is fetched with the prior
734 : * response's `next_cursor`, which is stable under concurrent inserts/deletes
735 : * and avoids the duplicate/skip drift that offset paging exhibits when the
736 : * underlying set changes mid-walk. Falls back to terminating on `has_more`
737 : * when the server omits `next_cursor`.
738 : *
739 : * @param tag - The tag to filter by (e.g., 'models', 'input')
740 : * @param includePublic - Whether to include public assets (default: true)
741 : * @param options - Pagination options
742 : * @param options.limit - Page size for each request (default: 500)
743 : * @param options.signal - Optional abort signal for cancelling requests
744 : * @returns Promise<AssetItem[]> - Full asset objects filtered by tag
745 : */
746 25 : async function getAllAssetsByTag(
747 : tag: string,
748 : includePublic: boolean = true,
749 : {
750 : limit = DEFAULT_LIMIT,
751 : signal
752 : }: Pick<AssetPaginationOptions, 'limit' | 'signal'> = {}
753 : ): Promise<AssetItem[]> {
754 25 : return walkAssetPages(`tag '${tag}'`, signal, (after) =>
755 : getAssetsPageByTag(tag, includePublic, {
756 : limit: limit > 0 ? limit : DEFAULT_LIMIT,
757 : after,
758 : signal
759 : })
760 : )
761 : }
762 :
763 25 : async function walkAssetPages(
764 : label: string,
765 : signal: AbortSignal | undefined,
766 : fetchPage: (after: string | undefined) => Promise<AssetResponse>
767 : ): Promise<AssetItem[]> {
768 25 : const assets: AssetItem[] = []
769 25 : let after: string | undefined
770 25 : let batchCount = 0
771 :
772 25 : for (;;) {
773 25 : if (signal?.aborted) throw new DOMException('Aborted', 'AbortError')
774 25 : if (batchCount++ >= MAX_PAGINATION_BATCHES) {
775 0 : console.warn(
776 : `Paginated walk for ${label} hit the ${MAX_PAGINATION_BATCHES}-batch backstop; returning a truncated listing.`
777 : )
778 0 : return assets
779 : }
780 :
781 25 : const data = await fetchPage(after)
782 22 : const batch = data.assets
783 23 : if (batch.length === 0) {
784 1 : return assets
785 : }
786 :
787 21 : assets.push(...batch)
788 :
789 : // A server that returns a non-advancing cursor would loop forever.
790 25 : if (!data.has_more || !data.next_cursor || data.next_cursor === after) {
791 21 : return assets
792 : }
793 :
794 0 : after = data.next_cursor
795 : }
796 : }
797 :
798 : /**
799 : * Lists the unique loader names in one folder of the workspace library.
800 : */
801 0 : async function getWorkspaceLibraryModels(folder: string): Promise<string[]> {
802 0 : const assets = await walkAssetPages(
803 : `workspace library folder '${folder}'`,
804 : undefined,
805 : (after) =>
806 : handleAssetRequest(
807 : {
808 : includeTags: [MODELS_TAG, folder],
809 : includePublic: false,
810 : workspaceLibrary: true,
811 : limit: DEFAULT_LIMIT,
812 : after
813 : },
814 : `workspace library models in ${folder}`
815 : )
816 : )
817 0 : return uniq(
818 : assets
819 : .map((asset) => asset.loader_path ?? getAssetFilename(asset))
820 : .filter((name) => assetFilenameSchema.safeParse(name).success)
821 : )
822 : }
823 :
824 0 : async function deleteAsset(id: AssetId): Promise<void> {
825 0 : const res = await api.fetchApi(`${ASSETS_ENDPOINT}/${id}`, {
826 : method: 'DELETE'
827 : })
828 :
829 0 : if (!res.ok && res.status !== 404) {
830 0 : throw new Error(
831 : `Unable to delete asset ${id}: Server returned ${res.status}`
832 : )
833 : }
834 0 : notifyAssetsChanged()
835 : }
836 :
837 0 : function notifyAssetsChanged() {
838 0 : for (const listener of assetChangeListeners) listener()
839 : }
840 :
841 : /**
842 : * Calls back after this client deletes an asset, adds or removes an asset's tags, or an import finishes at once; returns the unsubscribe.
843 : */
844 0 : function onAssetsChanged(callback: () => void): () => void {
845 0 : assetChangeListeners.add(callback)
846 0 : return () => {
847 0 : assetChangeListeners.delete(callback)
848 : }
849 : }
850 :
851 : /**
852 : * Update metadata of an asset by ID
853 : * Only available in cloud environment
854 : *
855 : * @param id - The asset ID (UUID)
856 : * @param newData - The data to update
857 : * @returns Promise<AssetItem>
858 : * @throws Error if update fails
859 : */
860 0 : async function updateAsset(
861 : id: AssetId,
862 : newData: AssetUpdatePayload
863 : ): Promise<AssetItem> {
864 0 : const res = await api.fetchApi(`${ASSETS_ENDPOINT}/${id}`, {
865 : method: 'PUT',
866 : headers: {
867 : 'Content-Type': 'application/json'
868 : },
869 : body: JSON.stringify(newData)
870 : })
871 :
872 0 : if (!res.ok) {
873 0 : throw new Error(
874 : `Unable to update asset ${id}: Server returned ${res.status}`
875 : )
876 : }
877 :
878 0 : const newAsset = assetItemSchema.safeParse(await res.json())
879 0 : if (newAsset.success) {
880 0 : return newAsset.data
881 : }
882 :
883 0 : throw new Error(
884 : `Unable to update asset ${id}: Invalid response - ${newAsset.error}`
885 : )
886 : }
887 :
888 : /**
889 : * Retrieves metadata from a download URL without downloading the file
890 : *
891 : * @param url - Download URL to retrieve metadata from (will be URL-encoded)
892 : * @returns Promise with metadata including content_length, final_url, filename, etc.
893 : * @throws Error if metadata retrieval fails
894 : */
895 0 : async function getAssetMetadata(url: string): Promise<AssetMetadata> {
896 0 : const encodedUrl = encodeURIComponent(url)
897 0 : const res = await api.fetchApi(
898 : `${ASSETS_ENDPOINT}/remote-metadata?url=${encodedUrl}`
899 : )
900 :
901 0 : if (!res.ok) {
902 0 : const { code } = await parseErrorResponse(res)
903 0 : throw new Error(getLocalizedErrorMessage(code))
904 : }
905 :
906 0 : const data: AssetMetadata = await res.json()
907 0 : if (data.validation?.is_valid === false) {
908 0 : throw new Error(
909 : getLocalizedErrorMessage(
910 : data.validation.errors?.[0]?.code || 'UNKNOWN_ERROR'
911 : )
912 : )
913 : }
914 :
915 0 : return data
916 : }
917 :
918 : /**
919 : * Uploads an asset by providing a URL to download from
920 : *
921 : * @param params - Upload parameters
922 : * @param params.url - HTTP/HTTPS URL to download from
923 : * @param params.name - Display name (determines extension)
924 : * @param params.tags - Optional freeform tags
925 : * @param params.user_metadata - Optional custom metadata object
926 : * @param params.preview_id - Optional UUID for preview asset
927 : * @returns Promise<AssetItem & { created_new: boolean }> - Asset object with created_new flag
928 : * @throws Error if upload fails
929 : */
930 0 : async function uploadAssetFromUrl(params: {
931 : url: string
932 : name: string
933 : tags?: string[]
934 : user_metadata?: Record<string, unknown>
935 : preview_id?: string
936 : }): Promise<AssetItem & { created_new: boolean }> {
937 0 : const res = await api.fetchApi(ASSETS_ENDPOINT, {
938 : method: 'POST',
939 : headers: {
940 : 'Content-Type': 'application/json'
941 : },
942 : body: JSON.stringify(params)
943 : })
944 :
945 0 : if (!res.ok) {
946 0 : throw new Error(
947 : st(
948 : 'assetBrowser.errorUploadFailed',
949 : 'Failed to upload asset. Please try again.'
950 : )
951 : )
952 : }
953 :
954 : const asset = validateUploadedAssetResponse(await res.json())
955 0 : return asset
956 : }
957 :
958 : /**
959 : * Uploads an asset from base64 data
960 : *
961 : * @param params - Upload parameters
962 : * @param params.data - Base64 data URL (e.g., "data:image/png;base64,...")
963 : * @param params.name - Display name (determines extension)
964 : * @param params.tags - Optional freeform tags
965 : * @param params.user_metadata - Optional custom metadata object
966 : * @returns Promise<AssetItem & { created_new: boolean }> - Asset object with created_new flag
967 : * @throws Error if upload fails
968 : */
969 0 : async function uploadAssetFromBase64(params: {
970 : data: string
971 : name: string
972 : tags?: string[]
973 : user_metadata?: Record<string, unknown>
974 : }): Promise<AssetItem & { created_new: boolean }> {
975 : // Validate that data is a data URL
976 0 : if (!params.data || !params.data.startsWith('data:')) {
977 0 : throw new Error(
978 : 'Invalid data URL: expected a string starting with "data:"'
979 : )
980 : }
981 :
982 : // Convert base64 data URL to Blob
983 0 : const blob = await fetch(params.data).then((r) => r.blob())
984 :
985 : // Create FormData and append the blob
986 0 : const formData = new FormData()
987 0 : formData.append('file', blob, params.name)
988 :
989 0 : if (params.tags) {
990 0 : formData.append('tags', JSON.stringify(params.tags))
991 : }
992 :
993 0 : if (params.user_metadata) {
994 0 : formData.append('user_metadata', JSON.stringify(params.user_metadata))
995 : }
996 :
997 0 : const res = await api.fetchApi(ASSETS_ENDPOINT, {
998 : method: 'POST',
999 : body: formData
1000 : })
1001 :
1002 0 : if (!res.ok) {
1003 0 : throw new Error(
1004 : `Failed to upload asset from base64: ${res.status} ${res.statusText}`
1005 : )
1006 : }
1007 :
1008 : const asset = validateUploadedAssetResponse(await res.json())
1009 0 : return asset
1010 : }
1011 :
1012 : /**
1013 : * Add tags to an asset
1014 : * @param id - The asset ID (UUID)
1015 : * @param tags - Tags to add
1016 : * @returns Promise<TagsOperationResult>
1017 : */
1018 0 : async function addAssetTags(
1019 : id: string,
1020 : tags: string[]
1021 : ): Promise<TagsOperationResult> {
1022 0 : const res = await api.fetchApi(`${ASSETS_ENDPOINT}/${id}/tags`, {
1023 : method: 'POST',
1024 : headers: { 'Content-Type': 'application/json' },
1025 : body: JSON.stringify({ tags })
1026 : })
1027 :
1028 0 : if (!res.ok) {
1029 0 : throw new Error(
1030 : `Unable to add tags to asset ${id}: Server returned ${res.status}`
1031 : )
1032 : }
1033 0 : notifyAssetsChanged()
1034 :
1035 0 : const result = await res.json()
1036 0 : const parseResult = tagsOperationResultSchema.safeParse(result)
1037 0 : if (!parseResult.success) {
1038 0 : throw fromZodError(parseResult.error)
1039 : }
1040 0 : return parseResult.data
1041 : }
1042 :
1043 : /**
1044 : * Remove tags from an asset
1045 : * @param id - The asset ID (UUID)
1046 : * @param tags - Tags to remove
1047 : * @returns Promise<TagsOperationResult>
1048 : */
1049 0 : async function removeAssetTags(
1050 : id: string,
1051 : tags: string[]
1052 : ): Promise<TagsOperationResult> {
1053 0 : const res = await api.fetchApi(`${ASSETS_ENDPOINT}/${id}/tags`, {
1054 : method: 'DELETE',
1055 : headers: { 'Content-Type': 'application/json' },
1056 : body: JSON.stringify({ tags })
1057 : })
1058 :
1059 0 : if (!res.ok) {
1060 0 : throw new Error(
1061 : `Unable to remove tags from asset ${id}: Server returned ${res.status}`
1062 : )
1063 : }
1064 0 : notifyAssetsChanged()
1065 :
1066 0 : const result = await res.json()
1067 0 : const parseResult = tagsOperationResultSchema.safeParse(result)
1068 0 : if (!parseResult.success) {
1069 0 : throw fromZodError(parseResult.error)
1070 : }
1071 0 : return parseResult.data
1072 : }
1073 :
1074 : /**
1075 : * Uploads an asset asynchronously using the /api/assets/download endpoint
1076 : * Returns immediately with either the asset (if already exists) or a task to track
1077 : *
1078 : * @param params - Upload parameters
1079 : * @param params.source_url - HTTP/HTTPS URL to download from
1080 : * @param params.tags - Optional freeform tags
1081 : * @param params.user_metadata - Optional custom metadata object
1082 : * @param params.preview_id - Optional UUID for preview asset
1083 : * @returns Promise<AsyncUploadResponse> - Either sync asset or async task info
1084 : * @throws Error if upload fails
1085 : */
1086 0 : async function uploadAssetAsync(params: {
1087 : source_url: string
1088 : tags?: string[]
1089 : user_metadata?: Record<string, unknown>
1090 : preview_id?: string
1091 : }): Promise<AsyncUploadResponse> {
1092 0 : const res = await api.fetchApi(ASSETS_DOWNLOAD_ENDPOINT, {
1093 : method: 'POST',
1094 : headers: { 'Content-Type': 'application/json' },
1095 : body: JSON.stringify(params)
1096 : })
1097 :
1098 0 : if (!res.ok) {
1099 0 : throw new Error(
1100 : st(
1101 : 'assetBrowser.errorUploadFailed',
1102 : 'Failed to upload asset. Please try again.'
1103 : )
1104 : )
1105 : }
1106 :
1107 0 : const data = await res.json()
1108 :
1109 0 : if (res.status === 202) {
1110 0 : const result = asyncUploadResponseSchema.safeParse({
1111 : type: 'async',
1112 : task: data
1113 : })
1114 0 : if (!result.success) {
1115 0 : throw new Error(
1116 : st(
1117 : 'assetBrowser.errorUploadFailed',
1118 : 'Failed to parse async upload response. Please try again.'
1119 : )
1120 : )
1121 : }
1122 0 : if (
1123 : result.data.type === 'async' &&
1124 : result.data.task.status === 'completed'
1125 : ) {
1126 0 : notifyAssetsChanged()
1127 : }
1128 0 : return result.data
1129 : }
1130 :
1131 0 : const result = asyncUploadResponseSchema.safeParse({
1132 : type: 'sync',
1133 : asset: data
1134 : })
1135 0 : if (!result.success) {
1136 0 : throw new Error(
1137 : st(
1138 : 'assetBrowser.errorUploadFailed',
1139 : 'Failed to parse sync upload response. Please try again.'
1140 : )
1141 : )
1142 : }
1143 0 : notifyAssetsChanged()
1144 0 : return result.data
1145 : }
1146 :
1147 0 : async function createAssetExport(
1148 : params: AssetExportOptions
1149 : ): Promise<{ task_id: string; status: string; message?: string }> {
1150 0 : const res = await api.fetchApi(ASSETS_EXPORT_ENDPOINT, {
1151 : method: 'POST',
1152 : headers: { 'Content-Type': 'application/json' },
1153 : body: JSON.stringify(params)
1154 : })
1155 :
1156 0 : if (!res.ok) {
1157 0 : throw new Error(`Failed to create asset export: ${res.status}`)
1158 : }
1159 :
1160 0 : return await res.json()
1161 : }
1162 :
1163 0 : async function getExportDownloadUrl(
1164 : exportName: string
1165 : ): Promise<{ url: string; expires_at?: string }> {
1166 0 : const res = await api.fetchApi(`/assets/exports/${exportName}`)
1167 :
1168 0 : if (!res.ok) {
1169 0 : throw new Error(`Failed to get export download URL: ${res.status}`)
1170 : }
1171 :
1172 0 : return await res.json()
1173 : }
1174 :
1175 2356 : return {
1176 : getAssetModels,
1177 : invalidateModelBuckets,
1178 : onModelsScanned,
1179 : seedModelAssets,
1180 : isWidgetAssetPickerEnabled,
1181 : isAssetBrowserEligible,
1182 : shouldUseWidgetAssetPicker,
1183 : getAssetsForNodeType,
1184 : getAssetsPageForNodeType,
1185 : getAssetDetails,
1186 : getAssetsByTag,
1187 : getAssetsPageByTag,
1188 : getAllAssetsByTag,
1189 : getWorkspaceLibraryModels,
1190 : getWorkspaceLibraryAssetsPage,
1191 : deleteAsset,
1192 : onAssetsChanged,
1193 : updateAsset,
1194 : addAssetTags,
1195 : removeAssetTags,
1196 : getAssetMetadata,
1197 : uploadAssetFromUrl,
1198 : uploadAssetFromBase64,
1199 : uploadAssetAsync,
1200 : createAssetExport,
1201 : getExportDownloadUrl
1202 : }
1203 : }
1204 :
1205 2356 : export const assetService = createAssetService()
|