Line data Source code
1 : import { debounce } from 'es-toolkit'
2 : import { defineStore } from 'pinia'
3 : import { computed, onScopeDispose, reactive, ref, watch } from 'vue'
4 :
5 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
6 : import type { ModelFile } from '@/platform/assets/schemas/assetSchema'
7 : import { assetService } from '@/platform/assets/services/assetService'
8 : import { isCloud } from '@/platform/distribution/types'
9 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
10 : import { api } from '@/scripts/api'
11 :
12 : /** (Internal helper) finds a value in a metadata object from any of a list of keys. */
13 27 : function _findInMetadata(
14 : metadata: Record<string, string | null>,
15 : ...keys: string[]
16 : ): string | null {
17 27 : for (const key of keys) {
18 65 : if (key in metadata) {
19 : const value = metadata[key]
20 1 : return value || null
21 : }
22 65 : for (const k in metadata) {
23 20 : if (k.endsWith(key)) {
24 : const value = metadata[k]
25 0 : return value || null
26 : }
27 : }
28 : }
29 26 : return null
30 : }
31 :
32 : /** Defines and holds metadata for a model */
33 2355 : export class ComfyModelDef {
34 : /** Path to the model */
35 : readonly path_index: number
36 : /** Proper filename of the model */
37 : readonly file_name: string
38 : /** Normalized filename of the model, with all backslashes replaced with forward slashes */
39 : readonly normalized_file_name: string
40 : /** Directory containing the model, eg 'checkpoints' */
41 : readonly directory: string
42 : /** Simplified copy of name, used as a default title. Excludes the directory and the '.safetensors' file extension */
43 : readonly simplified_file_name: string
44 : /** Key for the model, used to uniquely identify the model. */
45 : readonly key: string
46 : /** Title / display name of the model, sometimes same as the name but not always */
47 : title: string
48 : /** Metadata: architecture ID for the model, such as 'stable-diffusion-xl-v1-base' */
49 : architecture_id: string = ''
50 : /** Metadata: author of the model */
51 : author: string = ''
52 : /** Metadata: resolution of the model, eg '1024x1024' */
53 : resolution: string = ''
54 : /** Metadata: description of the model */
55 : description: string = ''
56 : /** Metadata: usage hint for the model */
57 : usage_hint: string = ''
58 : /** Metadata: trigger phrase for the model */
59 : trigger_phrase: string = ''
60 : /** Metadata: tags list for the model */
61 : tags: string[] = []
62 : /** Metadata: image for the model */
63 : image: string = ''
64 : /** Whether the model metadata has been loaded from the server, used for `load()` */
65 : has_loaded_metadata: boolean = false
66 : /** If true, a metadata load request has been triggered, but may or may not yet have finished loading */
67 : is_load_requested: boolean = false
68 : /** A string full of auto-computed lowercase-only searchable text for this model */
69 : searchable: string = ''
70 :
71 : constructor(name: string, directory: string, pathIndex: number) {
72 116 : this.path_index = pathIndex
73 116 : this.file_name = name
74 116 : this.normalized_file_name = name.replaceAll('\\', '/')
75 116 : this.simplified_file_name = this.normalized_file_name.split('/').pop() ?? ''
76 116 : if (this.simplified_file_name.endsWith('.safetensors')) {
77 100 : this.simplified_file_name = this.simplified_file_name.slice(
78 : 0,
79 : -'.safetensors'.length
80 : )
81 : }
82 116 : this.title = this.simplified_file_name
83 116 : this.directory = directory
84 116 : this.key = `${directory}/${this.normalized_file_name}`
85 116 : this.updateSearchable()
86 : }
87 :
88 : updateSearchable() {
89 119 : this.searchable = [
90 : this.file_name,
91 : this.title,
92 : this.author,
93 : this.description,
94 : this.trigger_phrase,
95 : this.tags.join(', ')
96 : ]
97 : .join('\n')
98 : .toLowerCase()
99 : }
100 :
101 : /** Loads the model metadata from the server, filling in this object if data is available */
102 : async load(): Promise<void> {
103 71 : if (this.has_loaded_metadata || this.is_load_requested) {
104 16 : return
105 : }
106 : // viewMetadata reads the safetensors header off local disk; on Cloud the
107 : // model bytes live in object storage so there is nothing to read.
108 : if (isCloud) {
109 : return
110 : }
111 55 : this.is_load_requested = true
112 71 : try {
113 55 : const metadata = await api.viewMetadata(this.directory, this.file_name)
114 52 : if (!metadata) {
115 49 : return
116 : }
117 71 : this.title =
118 : _findInMetadata(
119 : metadata,
120 : 'modelspec.title',
121 : 'title',
122 : 'display_name',
123 : 'name'
124 : ) || this.title
125 71 : this.architecture_id =
126 : _findInMetadata(metadata, 'modelspec.architecture', 'architecture') ||
127 : ''
128 71 : this.author =
129 : _findInMetadata(metadata, 'modelspec.author', 'author') || ''
130 71 : this.description =
131 : _findInMetadata(metadata, 'modelspec.description', 'description') || ''
132 71 : this.resolution =
133 : _findInMetadata(metadata, 'modelspec.resolution', 'resolution') || ''
134 71 : this.usage_hint =
135 : _findInMetadata(metadata, 'modelspec.usage_hint', 'usage_hint') || ''
136 71 : this.trigger_phrase =
137 : _findInMetadata(
138 : metadata,
139 : 'modelspec.trigger_phrase',
140 : 'trigger_phrase'
141 : ) || ''
142 71 : this.image =
143 : _findInMetadata(
144 : metadata,
145 : 'modelspec.thumbnail',
146 : 'thumbnail',
147 : 'image',
148 : 'icon'
149 : ) || ''
150 71 : const tagsCommaSeparated =
151 : _findInMetadata(metadata, 'modelspec.tags', 'tags') || ''
152 71 : this.tags = tagsCommaSeparated.split(',').map((tag) => tag.trim())
153 71 : this.has_loaded_metadata = true
154 71 : this.updateSearchable()
155 : } catch (error) {
156 0 : console.error('Error loading model metadata', this.file_name, this, error)
157 : }
158 : }
159 : }
160 :
161 2355 : export enum ResourceState {
162 2355 : Uninitialized,
163 2355 : Loading,
164 2355 : Loaded
165 : }
166 :
167 : /**
168 : * Resolves the preview image for a model: embedded metadata thumbnail when
169 : * loaded, otherwise the server-rendered `.webp` preview. The preview endpoint
170 : * reads a rendered thumbnail off local disk, which is unavailable on Cloud
171 : * (model bytes live in object storage), so Cloud resolves to no preview.
172 : */
173 116 : export function getModelPreviewUrl(model: ComfyModelDef): string {
174 116 : if (model.image) return model.image
175 : if (isCloud) return ''
176 116 : const extension = model.file_name.split('.').pop()
177 116 : const filename = model.file_name.replace(`.${extension}`, '.webp')
178 116 : const encodedFilename = encodeURIComponent(filename).replace(/%2F/g, '/')
179 116 : return `/api/experiment/models/preview/${encodeURIComponent(model.directory)}/${model.path_index}/${encodedFilename}`
180 : }
181 :
182 : /**
183 : * FE-owned copy of core's default `supported_pt_extensions`, applied to
184 : * match-all folders (empty registered allowlist) so they don't surface
185 : * README/config noise. Accepted to go stale across core version bumps; the
186 : * whole surface is expected to be short-lived.
187 : */
188 2355 : const DEFAULT_MODEL_EXTENSIONS = [
189 : '.ckpt',
190 : '.pt',
191 : '.pt2',
192 : '.bin',
193 : '.pth',
194 : '.safetensors',
195 : '.pkl',
196 : '.sft'
197 : ]
198 :
199 : /**
200 : * Real (`.`-prefixed) entries of a registered allowlist. Non-prefixed
201 : * entries (`'folder'`, `''`) are backend sentinels, not extensions — a list
202 : * containing only sentinels means match-all, the same as an empty list.
203 : */
204 250 : function realExtensions(extensions: string[]): string[] {
205 250 : return extensions.filter((ext) => ext.startsWith('.'))
206 : }
207 :
208 : /**
209 : * Resolves a folder's display allowlist from its raw registered `extensions`
210 : * (`/experiment/models`): a list with real extensions is used verbatim; a
211 : * match-all list (empty, sentinel-only, or absent on older backends) takes
212 : * the FE default list, reproducing the legacy sidebar's global-set behavior
213 : * so nothing that used to be hidden starts showing.
214 : */
215 118 : export function effectiveModelExtensions(
216 : extensions: string[] | undefined
217 : ): string[] {
218 118 : if (extensions && realExtensions(extensions).length > 0) return extensions
219 28 : return DEFAULT_MODEL_EXTENSIONS
220 : }
221 :
222 : /**
223 : * Whether a model file belongs in a folder given its display allowlist. A
224 : * list with no real extensions leaves the folder unfiltered.
225 : */
226 132 : export function matchesModelExtension(
227 : fileName: string,
228 : extensions: string[]
229 : ): boolean {
230 132 : const real = realExtensions(extensions)
231 132 : if (real.length === 0) return true
232 89 : const lower = fileName.toLowerCase()
233 89 : return real.some((ext) => lower.endsWith(ext.toLowerCase()))
234 : }
235 :
236 2355 : export class ModelFolder {
237 : /** Models in this folder */
238 : models: Record<string, ComfyModelDef> = {}
239 : state: ResourceState = ResourceState.Uninitialized
240 :
241 : constructor(
242 53918 : public directory: string,
243 53918 : private getModelsFunc: (folder: string) => Promise<ModelFile[]>,
244 53918 : public readonly extensions: string[] = []
245 : ) {}
246 :
247 : get key(): string {
248 313 : return this.directory + '/'
249 : }
250 :
251 : /**
252 : * Loads the models in this folder from the server
253 : */
254 : async load() {
255 121 : if (this.state !== ResourceState.Uninitialized) {
256 32 : return this
257 : }
258 89 : this.state = ResourceState.Loading
259 121 : try {
260 89 : const models = await this.getModelsFunc(this.directory)
261 116 : for (const model of models) {
262 132 : if (!matchesModelExtension(model.name, this.extensions)) continue
263 116 : this.models[`${model.pathIndex}/${model.name}`] = new ComfyModelDef(
264 : model.name,
265 : this.directory,
266 : model.pathIndex
267 : )
268 : }
269 83 : this.state = ResourceState.Loaded
270 83 : return this
271 : } catch (error) {
272 : // Reset instead of sticking at Loading so the guard above does not
273 : // permanently block a retry after a transient failure.
274 3 : this.state = ResourceState.Uninitialized
275 3 : throw error
276 : }
277 : }
278 : }
279 :
280 : /** Model store handler, wraps individual per-folder model stores */
281 2355 : export const useModelStore = defineStore('models', () => {
282 2355 : const { flags } = useFeatureFlags()
283 : const deploymentPick = isCloud ? useDeploymentPickStore() : undefined
284 2355 : const useAssetModels = computed(
285 : () => flags.assetsEnabled && !deploymentPick?.bootDeployment
286 : )
287 2355 : const modelFolderNames = ref<string[]>([])
288 2355 : const modelFolderByName = ref<Record<string, ModelFolder>>({})
289 2355 : const modelFolders = computed<ModelFolder[]>(() =>
290 : modelFolderNames.value.map(
291 : (folderName) => modelFolderByName.value[folderName]
292 : )
293 : )
294 2355 : const models = computed<ComfyModelDef[]>(() =>
295 : modelFolders.value.flatMap((folder) => Object.values(folder.models))
296 : )
297 : /**
298 : * Folders worth showing in the sidebar. Asset mode hides folders that
299 : * loaded empty — the registry lists every registered folder type, most of
300 : * which have no models on a given install; they reappear live when a scan
301 : * discovers a first model. The legacy path keeps its historical
302 : * all-registered-folders view.
303 : */
304 2355 : const visibleModelFolders = computed<ModelFolder[]>(() =>
305 : useAssetModels.value
306 : ? modelFolders.value.filter(
307 : (folder) =>
308 : folder.state !== ResourceState.Loaded ||
309 : Object.keys(folder.models).length > 0
310 : )
311 : : modelFolders.value
312 : )
313 :
314 2195 : function createGetModelsFunc(): (folder: string) => Promise<ModelFile[]> {
315 2195 : return useAssetModels.value
316 : ? (folder) => assetService.getAssetModels(folder)
317 : : (folder) => api.getModels(folder)
318 : }
319 :
320 2355 : const modelSource = computed(() =>
321 : !useAssetModels.value
322 : ? 'legacy-api'
323 : : flags.supportsModelTypeTags
324 : ? 'asset-model-type-tags'
325 : : 'asset-legacy-tags'
326 : )
327 2355 : let committedSource = modelSource.value
328 2355 : let modelFoldersRequestId = 0
329 :
330 : /**
331 : * Whether anything has consumed this store's model data (sidebar loads,
332 : * folder refreshes). The boot-registered scan subscription uses this to
333 : * skip background reloads nothing is displaying.
334 : */
335 2355 : let modelDataConsumed = false
336 :
337 : interface PreparedModelFolders {
338 : requestId: number
339 : source: typeof modelSource.value
340 : names: string[]
341 : folders: Record<string, ModelFolder>
342 : }
343 :
344 : /**
345 : * Fetches the folder structure and builds a fresh, uncommitted folder map.
346 : *
347 : * The folder list (and its registration order) always comes from
348 : * `/experiment/models`, the source of truth for which model folders exist;
349 : * only the per-folder contents differ between the asset API and legacy paths.
350 : * Concurrent loads (manual refresh racing the scan-complete reload) commit
351 : * only the newest request, so returns null when superseded — a slow stale
352 : * response cannot overwrite a fresher folder structure.
353 : */
354 2324 : async function prepareModelFolders(): Promise<PreparedModelFolders | null> {
355 2324 : const requestId = ++modelFoldersRequestId
356 2324 : const resData = await api.getModelFolders()
357 2198 : if (requestId !== modelFoldersRequestId) return null
358 2195 : const getModelsFunc = createGetModelsFunc()
359 2195 : const folders = reactive<Record<string, ModelFolder>>({})
360 2199 : for (const folder of resData) {
361 53918 : folders[folder.name] = new ModelFolder(
362 : folder.name,
363 : getModelsFunc,
364 : // Display filtering applies to the asset walk only; the legacy
365 : // listing keeps its historical server-side (global-set) filtering.
366 : useAssetModels.value ? effectiveModelExtensions(folder.extensions) : []
367 : )
368 : }
369 2195 : return {
370 : requestId,
371 : source: modelSource.value,
372 : names: resData.map((folder) => folder.name),
373 : folders
374 : }
375 : }
376 :
377 2195 : function commitModelFolders({
378 : names,
379 : folders,
380 : source
381 : }: PreparedModelFolders): void {
382 2195 : modelFolderNames.value = names
383 2195 : modelFolderByName.value = folders
384 2195 : committedSource = source
385 : }
386 :
387 : /** Loads the model folder structure from the server; false when superseded. */
388 2324 : async function loadModelFolders(): Promise<boolean> {
389 2324 : const prepared = await prepareModelFolders()
390 2198 : if (!prepared) return false
391 2195 : while (prepared.requestId === modelFoldersRequestId) {
392 2201 : const pendingFolders = modelFolders.value
393 : .filter(
394 : (folder) =>
395 : folder.state !== ResourceState.Uninitialized &&
396 : folder.directory in prepared.folders
397 : )
398 : .map((folder) => prepared.folders[folder.directory])
399 : .filter((folder) => folder.state === ResourceState.Uninitialized)
400 2201 : if (pendingFolders.length === 0) {
401 2194 : commitModelFolders(prepared)
402 2194 : return true
403 : }
404 155 : if (prepared.source !== committedSource) commitModelFolders(prepared)
405 7 : await Promise.all(pendingFolders.map((folder) => folder.load()))
406 : }
407 0 : return false
408 : }
409 :
410 35 : async function getLoadedModelFolder(
411 : folderName: string
412 : ): Promise<ModelFolder | null> {
413 35 : modelDataConsumed = true
414 35 : const folder = Object.hasOwn(modelFolderByName.value, folderName)
415 : ? modelFolderByName.value[folderName]
416 : : undefined
417 35 : return folder ? await folder.load() : null
418 : }
419 :
420 : /**
421 : * Loads all model folders' contents from the server. Loads the folder
422 : * structure first when it has not arrived yet — eager loading can run
423 : * before app boot's own loadModelFolders call resolves, and iterating an
424 : * empty folder list would silently load nothing.
425 : */
426 24 : async function loadModels() {
427 24 : modelDataConsumed = true
428 : // A load superseded by a newer concurrent one commits nothing, which
429 : // would leave the folder list empty and silently load no models; retry
430 : // until a load of ours commits (even a genuinely empty result) or a
431 : // concurrent one has populated the list. Bounded as a safety net.
432 24 : for (let attempt = 0; attempt < 3; attempt++) {
433 24 : if (modelFolderNames.value.length > 0) break
434 1 : if (await loadModelFolders()) break
435 : }
436 24 : return Promise.all(modelFolders.value.map((folder) => folder.load()))
437 : }
438 :
439 : /**
440 : * Discards the cache for a single folder and re-loads its contents.
441 : * Use when on-disk contents of that folder have changed (e.g. after upload).
442 : * Falls back to refreshing the whole library when the folder is unknown so
443 : * a newly-introduced folder type is picked up without dropping other
444 : * folders' loaded contents.
445 : */
446 2355 : const folderRefreshIds = new Map<string, number>()
447 :
448 0 : async function refreshModelFolder(folderName: string) {
449 0 : modelDataConsumed = true
450 0 : assetService.invalidateModelBuckets()
451 0 : if (!(folderName in modelFolderByName.value)) {
452 0 : await refresh()
453 0 : return
454 : }
455 0 : const requestId = modelFoldersRequestId
456 0 : const refreshId = (folderRefreshIds.get(folderName) ?? 0) + 1
457 0 : folderRefreshIds.set(folderName, refreshId)
458 0 : const priorFolder = modelFolderByName.value[folderName]
459 0 : const folder = new ModelFolder(
460 : folderName,
461 : createGetModelsFunc(),
462 : priorFolder.extensions
463 : )
464 0 : await folder.load()
465 : // A full reload may have rebuilt the folder structure while this folder
466 : // refreshed, and a newer refresh of the same folder may have already
467 : // committed; committing then would resurrect a stale folder object. The
468 : // identity check also covers a rebuild whose folder load started before
469 : // this refresh snapshotted the request id.
470 0 : if (requestId !== modelFoldersRequestId) return
471 0 : if (folderRefreshIds.get(folderName) !== refreshId) return
472 0 : if (modelFolderByName.value[folderName] !== priorFolder) return
473 0 : modelFolderByName.value[folderName] = folder
474 : }
475 :
476 22 : async function reloadModels(): Promise<boolean> {
477 22 : assetService.invalidateModelBuckets()
478 22 : return loadModelFolders()
479 : }
480 :
481 : /**
482 : * Asks the backend to rescan the model roots so files added on disk since
483 : * startup become assets. Skipped on Cloud (models are ingested via uploads,
484 : * not scanned from disk) and on the legacy listing path (which reads the
485 : * filesystem live on every request).
486 : */
487 16 : async function requestModelScan() {
488 : if (isCloud) return
489 16 : if (!flags.assetsEnabled) return
490 1 : try {
491 1 : await assetService.seedModelAssets()
492 : } catch (error) {
493 0 : console.warn('Unable to start model asset scan', error)
494 : }
495 : }
496 :
497 : /**
498 : * Manual refresh ("r" key, sidebar refresh button): kicks off a backend
499 : * rescan and immediately re-loads the currently known server state; the
500 : * scan completion subscription below re-loads again with whatever the
501 : * scan discovered. The scan is deliberately not awaited so it runs
502 : * concurrently with the reload. False means a newer concurrent load
503 : * superseded this one, not a failure.
504 : */
505 16 : async function refresh(): Promise<boolean> {
506 16 : modelDataConsumed = true
507 16 : void requestModelScan()
508 16 : return await reloadModels()
509 : }
510 :
511 : /**
512 : * Scan completions and capability changes can land in bursts; coalesce
513 : * them into one trailing reload instead of one full library walk per event.
514 : */
515 : const MODEL_RELOAD_DEBOUNCE_MS = 500
516 :
517 2355 : const scheduleModelReload = debounce(async () => {
518 6 : try {
519 6 : await reloadModels()
520 : } catch (error) {
521 0 : console.error('Failed to reload the model library', error)
522 : }
523 : }, MODEL_RELOAD_DEBOUNCE_MS)
524 :
525 2355 : const unsubscribeModelsScanned = assetService.onModelsScanned(() => {
526 : // A scan changes bucket contents even when no UI has read this store
527 : // yet; drop the cache so the eventual first read walks fresh data, but
528 : // skip the reload nothing is displaying.
529 2 : assetService.invalidateModelBuckets()
530 2 : if (!modelDataConsumed) return
531 2 : scheduleModelReload()
532 : })
533 2355 : onScopeDispose(() => {
534 0 : scheduleModelReload.cancel()
535 0 : unsubscribeModelsScanned()
536 : })
537 :
538 : /**
539 : * The deployment listing and feature handshake can arrive after a folder
540 : * captured its source, so reload when the source changes.
541 : */
542 2355 : watch(modelSource, () => scheduleModelReload())
543 :
544 2355 : return {
545 : models,
546 : modelFolders,
547 : visibleModelFolders,
548 : loadModelFolders,
549 : loadModels,
550 : getLoadedModelFolder,
551 : refreshModelFolder,
552 : refresh
553 : }
554 : })
|