Line data Source code
1 : import { ref, toValue } from 'vue'
2 :
3 : import MultiSelectWidget from '@/components/graph/widgets/MultiSelectWidget.vue'
4 : import { registerComboWidgetInventory } from '@/core/graph/widgets/comboWidgetInventory'
5 : import { syncPromotedComboHostOptions } from '@/core/graph/widgets/syncPromotedComboHostOptions'
6 : import { t } from '@/i18n'
7 : import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph'
8 : import { isComboWidget } from '@/lib/litegraph/src/litegraph'
9 : import type { IBaseWidget, IComboWidget } from '@comfyorg/graph-model/widgets'
10 : import { assetService } from '@/platform/assets/services/assetService'
11 : import type { AssetItem } from '@/platform/assets/schemas/assetSchema'
12 : import { getAssetFilename } from '@/platform/assets/utils/assetMetadataUtils'
13 : import { createAssetWidget } from '@/platform/assets/utils/createAssetWidget'
14 : import { isCloud } from '@/platform/distribution/types'
15 : import { useDeploymentLibraryModelsStore } from '@/platform/workspace/stores/deploymentLibraryModelsStore'
16 : import type {
17 : ComboInputSpec,
18 : InputSpec
19 : } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
20 : import { isComboInputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
21 : import { transformInputSpecV2ToV1 } from '@comfyorg/object-info-parser/migration'
22 : import { ComponentWidgetImpl, addWidget } from '@/scripts/domWidget'
23 : import type { BaseDOMWidget } from '@/types/domWidget'
24 : import type { ComfyWidgetConstructorV2 } from '@/types/widgetConstructor'
25 : import { addValueControlWidgets } from '@/core/graph/widgets/valueControlWidgets'
26 : import { useAssetsStore } from '@/stores/assetsStore'
27 : import { getMediaTypeFromFilename } from '@/utils/formatUtil'
28 :
29 : import { useRemoteWidget } from './useRemoteWidget'
30 :
31 2356 : const getDefaultValue = (inputSpec: ComboInputSpec) => {
32 10618 : if (inputSpec.default) return inputSpec.default
33 10618 : if (inputSpec.options?.length) return inputSpec.options[0]
34 10618 : if (inputSpec.remote) return 'Loading...'
35 : return undefined
36 : }
37 :
38 : // Map node types to expected media types
39 : const NODE_MEDIA_TYPE_MAP: Partial<
40 : Record<string, 'image' | 'video' | 'audio'>
41 : > = {
42 : LoadImage: 'image',
43 : LoadVideo: 'video',
44 : LoadAudio: 'audio'
45 : }
46 :
47 : // Map node types to placeholder i18n keys
48 : const NODE_PLACEHOLDER_MAP: Record<string, string> = {
49 : LoadImage: 'widgets.uploadSelect.placeholderImage',
50 : LoadVideo: 'widgets.uploadSelect.placeholderVideo',
51 : LoadAudio: 'widgets.uploadSelect.placeholderAudio'
52 : }
53 :
54 2356 : const bindDynamicValuesOption = (
55 : widget: IBaseWidget,
56 : getValues: (assignedValues: unknown[]) => unknown,
57 : onAssign?: () => void
58 : ) => {
59 33 : const options = widget.options
60 33 : let fallbackValues = Array.isArray(options.values)
61 : ? options.values
62 : : ([] as unknown[])
63 :
64 33 : Object.defineProperty(options, 'values', {
65 : configurable: true,
66 : enumerable: true,
67 : get: () => {
68 220 : const values = getValues(fallbackValues)
69 220 : if (values === undefined || values === null) return fallbackValues
70 220 : return values
71 : },
72 : set: (values: unknown[]) => {
73 0 : fallbackValues = Array.isArray(values) ? values : fallbackValues
74 0 : onAssign?.()
75 : }
76 : })
77 : }
78 :
79 : const rootGraphsToSync = new Set<LGraph>()
80 :
81 : function syncPromotedHostsOnce(rootGraph: LGraph) {
82 : if (!rootGraphsToSync.size) {
83 : queueMicrotask(() => {
84 : const graphs = [...rootGraphsToSync]
85 : rootGraphsToSync.clear()
86 : for (const graph of graphs) syncPromotedComboHostOptions(graph)
87 : })
88 : }
89 : rootGraphsToSync.add(rootGraph)
90 : }
91 :
92 : interface LibraryWatch {
93 : folder: string
94 : stop?: () => void
95 : }
96 :
97 : const libraryWatchesByNode = new WeakMap<LGraphNode, Set<LibraryWatch>>()
98 :
99 : function startLibraryWatch(node: LGraphNode, libraryWatch: LibraryWatch) {
100 : libraryWatch.stop ??= useDeploymentLibraryModelsStore().watchFolder(
101 : libraryWatch.folder,
102 : () => {
103 : if (node.graph) syncPromotedHostsOnce(node.graph.rootGraph)
104 : else stopLibraryWatch(libraryWatch)
105 : }
106 : )
107 : }
108 :
109 : function stopLibraryWatch(libraryWatch: LibraryWatch) {
110 : libraryWatch.stop?.()
111 : libraryWatch.stop = undefined
112 : }
113 :
114 : function libraryWatchesOf(node: LGraphNode): Set<LibraryWatch> {
115 : const existing = libraryWatchesByNode.get(node)
116 : if (existing) return existing
117 : const libraryWatches = new Set<LibraryWatch>()
118 : libraryWatchesByNode.set(node, libraryWatches)
119 : const { onAdded, onRemoved } = node
120 : node.onAdded = function (graph) {
121 : onAdded?.call(this, graph)
122 : for (const libraryWatch of libraryWatches) {
123 : startLibraryWatch(node, libraryWatch)
124 : }
125 : }
126 : node.onRemoved = function () {
127 : onRemoved?.call(this)
128 : for (const libraryWatch of libraryWatches) stopLibraryWatch(libraryWatch)
129 : }
130 : return libraryWatches
131 : }
132 :
133 : function addLibraryModelOptions(
134 : node: LGraphNode,
135 : widget: IComboWidget,
136 : folder: string
137 : ) {
138 : const library = useDeploymentLibraryModelsStore()
139 : const libraryWatch: LibraryWatch = { folder }
140 : const libraryWatches = libraryWatchesOf(node)
141 : libraryWatches.add(libraryWatch)
142 : startLibraryWatch(node, libraryWatch)
143 : const { onRemove } = widget
144 : widget.onRemove = function () {
145 : onRemove?.call(this)
146 : libraryWatches.delete(libraryWatch)
147 : stopLibraryWatch(libraryWatch)
148 : }
149 : registerComboWidgetInventory(widget, {
150 : getStatus: () => library.statusOf(folder),
151 : waitForSettled: () => library.waitForSettled(folder)
152 : })
153 : let joined:
154 : | { release: unknown[]; names: readonly string[]; values: unknown[] }
155 : | undefined
156 : bindDynamicValuesOption(
157 : widget,
158 : (release) => {
159 : const names = library.namesIn(folder)
160 : if (!names.length) return release
161 : if (joined?.release !== release || joined.names !== names) {
162 : const listed = new Set(release)
163 : joined = {
164 : release,
165 : names,
166 : values: [...release, ...names.filter((name) => !listed.has(name))]
167 : }
168 : }
169 : return joined.values
170 : },
171 : () => library.refresh(folder)
172 : )
173 : }
174 :
175 2356 : const addMultiSelectWidget = (
176 : node: LGraphNode,
177 : inputSpec: ComboInputSpec
178 : ): IBaseWidget => {
179 0 : const widgetValue = ref<string[]>([])
180 0 : const widget = new ComponentWidgetImpl({
181 : node,
182 : name: inputSpec.name,
183 : component: MultiSelectWidget,
184 : inputSpec,
185 : options: {
186 : getValue: () => widgetValue.value,
187 : setValue: (value: string[]) => {
188 0 : widgetValue.value = value
189 : }
190 : }
191 : })
192 0 : addWidget(node, widget as BaseDOMWidget)
193 : // TODO: Add remote support to multi-select widget
194 : // https://github.com/Comfy-Org/ComfyUI_frontend/issues/3003
195 0 : if (inputSpec.control_after_generate) {
196 : const defaultType =
197 : typeof inputSpec.control_after_generate === 'string'
198 : ? inputSpec.control_after_generate
199 : : 'fixed'
200 0 : widget.linkedWidgets = addValueControlWidgets(
201 : node,
202 : widget,
203 : defaultType,
204 : undefined,
205 : transformInputSpecV2ToV1(inputSpec)
206 : )
207 : }
208 :
209 0 : return widget
210 : }
211 :
212 : /**
213 : * Resolve the default value for a cloud asset widget.
214 : * Priority: inputSpec.default (if present in cloud assets) → first cloud
215 : * asset → undefined (shows placeholder).
216 : */
217 : function resolveCloudDefault(
218 : nodeType: string,
219 : specDefault: string | undefined
220 : ): string | undefined {
221 : const assets = useAssetsStore().getAssets(nodeType)
222 : if (specDefault != null) {
223 : const inAssets = assets.some((a) => getAssetFilename(a) === specDefault)
224 : if (inAssets) return specDefault
225 : }
226 : // empty filename → undefined (shows placeholder)
227 : const filename = assets.length ? getAssetFilename(assets[0]) : undefined
228 : return filename || undefined
229 : }
230 :
231 : function getCloudInputAssets(nodeType: string | undefined): AssetItem[] {
232 : const mediaType = NODE_MEDIA_TYPE_MAP[nodeType ?? '']
233 : if (!mediaType) return []
234 :
235 : return toValue(useAssetsStore().inputAssets.items).filter(
236 : (asset) =>
237 : getCloudInputAssetValue(asset) &&
238 : getMediaTypeFromFilename(asset.name) === mediaType
239 : )
240 : }
241 :
242 : function getCloudInputAssetValue(asset: AssetItem): string | undefined {
243 : return asset.hash ?? undefined
244 : }
245 :
246 : function getCloudInputAssetValues(nodeType: string | undefined): string[] {
247 : return getCloudInputAssets(nodeType)
248 : .map(getCloudInputAssetValue)
249 : .filter((value): value is string => !!value)
250 : }
251 :
252 : function resolveCloudInputDefault(
253 : nodeType: string | undefined,
254 : specDefault: string | undefined
255 : ): string | undefined {
256 : const assets = getCloudInputAssets(nodeType)
257 : if (specDefault != null) {
258 : const matchingAsset =
259 : assets.find((asset) => getCloudInputAssetValue(asset) === specDefault) ??
260 : assets.find((asset) => asset.name === specDefault)
261 : if (matchingAsset) return getCloudInputAssetValue(matchingAsset)
262 : }
263 :
264 : return assets[0] ? getCloudInputAssetValue(assets[0]) : undefined
265 : }
266 :
267 : function createAssetBrowserWidget(
268 : node: LGraphNode,
269 : inputSpec: ComboInputSpec,
270 : defaultValue: string | undefined
271 : ): IBaseWidget {
272 : return createAssetWidget({
273 : node,
274 : widgetName: inputSpec.name,
275 : nodeTypeForBrowser: node.comfyClass ?? '',
276 : inputNameForBrowser: inputSpec.name,
277 : defaultValue
278 : })
279 : }
280 :
281 : const createInputMappingWidget = (
282 : node: LGraphNode,
283 : inputSpec: ComboInputSpec,
284 : defaultValue: string | undefined
285 : ): IBaseWidget => {
286 : const assetsStore = useAssetsStore()
287 :
288 : const widget = node.addWidget(
289 : 'combo',
290 : inputSpec.name,
291 : defaultValue ?? '',
292 : () => {},
293 : {
294 : values: [],
295 : getOptionLabel: (value?: string | null) => {
296 : if (!value) {
297 : const placeholderKey =
298 : NODE_PLACEHOLDER_MAP[node.comfyClass ?? ''] ??
299 : 'widgets.uploadSelect.placeholder'
300 : return t(placeholderKey)
301 : }
302 : return assetsStore.getInputName(value)
303 : }
304 : }
305 : )
306 :
307 : async function loadAll() {
308 : while (toValue(assetsStore.inputAssets.hasMore)) {
309 : if (!(await assetsStore.inputAssets.loadMore())) break
310 : node.setDirtyCanvas(true, false)
311 : }
312 : }
313 : void loadAll()
314 :
315 : bindDynamicValuesOption(widget, () =>
316 : getCloudInputAssetValues(node.comfyClass)
317 : )
318 :
319 : if (inputSpec.control_after_generate) {
320 : if (!isComboWidget(widget)) {
321 : throw new Error(`Expected combo widget but received ${widget.type}`)
322 : }
323 : const defaultType =
324 : typeof inputSpec.control_after_generate === 'string'
325 : ? inputSpec.control_after_generate
326 : : 'randomize'
327 : widget.linkedWidgets = addValueControlWidgets(
328 : node,
329 : widget,
330 : defaultType,
331 : undefined,
332 : transformInputSpecV2ToV1(inputSpec)
333 : )
334 : }
335 :
336 : return widget
337 : }
338 :
339 2356 : const addComboWidget = (
340 : node: LGraphNode,
341 : inputSpec: ComboInputSpec
342 : ): IBaseWidget => {
343 10618 : const defaultValue = getDefaultValue(inputSpec)
344 :
345 : if (isCloud) {
346 : if (
347 : assetService.shouldUseWidgetAssetPicker(node.comfyClass, inputSpec.name)
348 : ) {
349 : // Default from cloud assets, not from server combo options.
350 : // Server options list local files that may not exist in the user's
351 : // cloud asset library, leading to missing-model errors on undo/reload.
352 : const cloudDefault = resolveCloudDefault(
353 : node.comfyClass ?? '',
354 : inputSpec.default
355 : )
356 : return createAssetBrowserWidget(node, inputSpec, cloudDefault)
357 : }
358 :
359 : if (NODE_MEDIA_TYPE_MAP[node.comfyClass ?? '']) {
360 : return createInputMappingWidget(
361 : node,
362 : inputSpec,
363 : resolveCloudInputDefault(node.comfyClass, inputSpec.default)
364 : )
365 : }
366 : }
367 :
368 : const libraryFolder = isCloud
369 : ? useDeploymentLibraryModelsStore().folderFor(
370 : node.comfyClass ?? '',
371 : inputSpec.name
372 : )
373 : : undefined
374 :
375 : // Standard combo widget
376 10618 : const widget = node.addWidget(
377 : 'combo',
378 : inputSpec.name,
379 : defaultValue,
380 : () => {},
381 : {
382 : values: inputSpec.options ?? []
383 : }
384 : )
385 :
386 10618 : if (inputSpec.remote) {
387 33 : if (!isComboWidget(widget)) {
388 0 : throw new Error(`Expected combo widget but received ${widget.type}`)
389 : }
390 :
391 33 : const remoteWidget = useRemoteWidget({
392 : remoteConfig: inputSpec.remote,
393 : defaultValue,
394 : node,
395 : widget
396 : })
397 33 : if (inputSpec.remote.refresh_button) remoteWidget.addRefreshButton()
398 :
399 33 : registerComboWidgetInventory(widget, {
400 : getStatus: remoteWidget.getInventoryStatus,
401 : waitForSettled: remoteWidget.waitForInventory
402 : })
403 33 : bindDynamicValuesOption(widget, () => remoteWidget.getValue())
404 : } else if (libraryFolder && isComboWidget(widget)) {
405 : addLibraryModelOptions(node, widget, libraryFolder)
406 : }
407 :
408 10618 : if (inputSpec.control_after_generate) {
409 0 : if (!isComboWidget(widget)) {
410 0 : throw new Error(`Expected combo widget but received ${widget.type}`)
411 : }
412 :
413 : const defaultType =
414 : typeof inputSpec.control_after_generate === 'string'
415 : ? inputSpec.control_after_generate
416 : : 'randomize'
417 0 : widget.linkedWidgets = addValueControlWidgets(
418 : node,
419 : widget,
420 : defaultType,
421 : undefined,
422 : transformInputSpecV2ToV1(inputSpec)
423 : )
424 : }
425 :
426 10618 : return widget
427 : }
428 :
429 2356 : export const useComboWidget = () => {
430 2356 : const widgetConstructor: ComfyWidgetConstructorV2 = (
431 : node: LGraphNode,
432 : inputSpec: InputSpec
433 : ) => {
434 10618 : if (!isComboInputSpec(inputSpec)) {
435 0 : throw new Error(`Invalid input data: ${inputSpec}`)
436 : }
437 10618 : return inputSpec.multi_select
438 : ? addMultiSelectWidget(node, inputSpec)
439 : : addComboWidget(node, inputSpec)
440 : }
441 :
442 2356 : return widgetConstructor
443 : }
|