Line data Source code
1 : import { runWhenGlobalIdle } from '@/base/common/async'
2 : import type { SlotTypeDefaultNodeOpts } from '@/lib/litegraph/src/LiteGraphGlobal'
3 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
4 : import { collectRegistrableSlotTypes } from '@/extensions/core/slotDefaultTypes'
5 : import { reportError } from '@comfyorg/telemetry/reportError'
6 : import type { ComfyNodeDef } from '@comfyorg/object-info-parser'
7 : import type { ComfyExtension } from '@/types/comfy'
8 :
9 : import { app } from '@/scripts/app'
10 :
11 : interface SlotDefaultsExtension extends ComfyExtension {
12 : suggestionsNumber: { value: number } | null
13 : slot_types_default_out: Record<
14 : string,
15 : Array<string | SlotTypeDefaultNodeOpts>
16 : >
17 : slot_types_default_in: Record<string, Array<string | SlotTypeDefaultNodeOpts>>
18 : setDefaults(maxNum?: number | null): void
19 : }
20 :
21 2355 : const pendingNodeDefs: ComfyNodeDef[] = []
22 2355 : let pendingIdleFill: ReturnType<typeof runWhenGlobalIdle> | undefined
23 :
24 2439372 : function snapshotSlotSpecs(nodeData: ComfyNodeDef): ComfyNodeDef {
25 2439372 : const { input, output } = nodeData
26 2439372 : return {
27 : ...nodeData,
28 : input: input && {
29 : ...input,
30 : required: input.required && { ...input.required },
31 : optional: input.optional && { ...input.optional }
32 : },
33 : output: Array.isArray(output) ? [...output] : output
34 : }
35 : }
36 :
37 2362 : function registerPendingSlotTypes(extension: SlotDefaultsExtension) {
38 2362 : pendingIdleFill?.dispose()
39 2362 : pendingIdleFill = undefined
40 2362 : for (const nodeData of pendingNodeDefs.splice(0)) {
41 2424937 : try {
42 2424937 : registerSlotTypes(extension, nodeData)
43 : } catch (cause) {
44 0 : reportError(cause, {
45 : errorType: 'error_registering_slot_defaults',
46 : surface: 'graph',
47 : context: { nodeType: nodeData.name }
48 : })
49 : }
50 : }
51 2362 : extension.setDefaults(extension.suggestionsNumber?.value)
52 : }
53 :
54 2424937 : function registerSlotTypes(
55 : extension: SlotDefaultsExtension,
56 : nodeData: ComfyNodeDef
57 : ) {
58 2424937 : const nodeId = nodeData.name
59 2424937 : const { inputTypes, outputTypes } = collectRegistrableSlotTypes(nodeData)
60 :
61 2424937 : for (const type of inputTypes) {
62 2568411 : if (!(type in extension.slot_types_default_out)) {
63 206276 : extension.slot_types_default_out[type] = ['Reroute']
64 : }
65 2568411 : if (extension.slot_types_default_out[type].includes(nodeId)) continue
66 2559675 : extension.slot_types_default_out[type].push(nodeId)
67 :
68 2559675 : const lowerType = type.toLocaleLowerCase()
69 2568411 : if (!(lowerType in LiteGraph.registered_slot_in_types)) {
70 206266 : LiteGraph.registered_slot_in_types[lowerType] = { nodes: [] }
71 : }
72 2559675 : const inNodes = LiteGraph.registered_slot_in_types[lowerType].nodes
73 2559675 : if (!inNodes.includes(nodeId)) inNodes.push(nodeId)
74 : }
75 :
76 2424937 : for (const type of outputTypes) {
77 3349279 : if (!(type in extension.slot_types_default_in)) {
78 220340 : extension.slot_types_default_in[type] = ['Reroute']
79 : }
80 3349279 : if (extension.slot_types_default_in[type].includes(nodeId)) continue
81 2979251 : extension.slot_types_default_in[type].push(nodeId)
82 :
83 3349279 : if (!(type in LiteGraph.registered_slot_out_types)) {
84 220340 : LiteGraph.registered_slot_out_types[type] = { nodes: [] }
85 : }
86 2979251 : const outNodes = LiteGraph.registered_slot_out_types[type].nodes
87 2979251 : if (!outNodes.includes(nodeId)) outNodes.push(nodeId)
88 :
89 3349279 : if (!LiteGraph.slot_types_out.includes(type)) {
90 220340 : LiteGraph.slot_types_out.push(type)
91 : }
92 : }
93 : }
94 :
95 2355 : export const slotDefaultsExtension: SlotDefaultsExtension = {
96 : name: 'Comfy.SlotDefaults',
97 : suggestionsNumber: null,
98 : init(this: SlotDefaultsExtension) {
99 2355 : LiteGraph.search_filter_enabled = true
100 2355 : this.suggestionsNumber = app.ui.settings.addSetting({
101 : id: 'Comfy.NodeSuggestions.number',
102 : category: ['Comfy', 'Node Search Box', 'NodeSuggestions'],
103 : name: 'Number of nodes suggestions',
104 : tooltip: 'Only for litegraph searchbox/context menu',
105 : type: 'slider',
106 : attrs: {
107 : min: 1,
108 : max: 100,
109 : step: 1
110 : },
111 : defaultValue: 5,
112 : onChange: (newVal) => {
113 2356 : this.setDefaults(newVal)
114 : }
115 : })
116 : },
117 : slot_types_default_out: {},
118 : slot_types_default_in: {},
119 : beforeRegisterNodeDef(this: SlotDefaultsExtension, _nodeType, nodeData) {
120 2439372 : pendingNodeDefs.push(snapshotSlotSpecs(nodeData))
121 2439372 : LiteGraph.deferSlotTypeTables(() => registerPendingSlotTypes(this))
122 2439372 : pendingIdleFill ??= runWhenGlobalIdle(() => LiteGraph.flushSlotTypeTables())
123 : },
124 : setDefaults(this: SlotDefaultsExtension, maxNum?: number | null) {
125 4718 : try {
126 4718 : LiteGraph.flushSlotTypeTables()
127 4718 : const max = maxNum ?? undefined
128 4718 : const nextOut: typeof LiteGraph.slot_types_default_out = {}
129 4718 : const nextIn: typeof LiteGraph.slot_types_default_in = {}
130 :
131 4718 : for (const type in this.slot_types_default_out) {
132 207596 : nextOut[type] = this.slot_types_default_out[type].slice(0, max)
133 : }
134 4718 : for (const type in this.slot_types_default_in) {
135 221750 : nextIn[type] = this.slot_types_default_in[type].slice(0, max)
136 : }
137 :
138 4718 : LiteGraph.slot_types_default_out = nextOut
139 4718 : LiteGraph.slot_types_default_in = nextIn
140 : } catch (cause) {
141 0 : reportError(cause, {
142 : errorType: 'error_setting_slot_defaults',
143 : surface: 'graph'
144 : })
145 : }
146 : }
147 : }
|