File size: 5,907 Bytes
823cf00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**

 * File: debug.js

 * Project: comfy_mtb

 * Author: Mel Massadian

 *

 * Copyright (c) 2023 Mel Massadian

 *

 */

// Reference the shared typedefs file
/// <reference path="../types/typedefs.js" />

import { app } from '../../scripts/app.js'
import * as shared from './comfy_shared.js'
import * as mtb_ui from './mtb_ui.js'

function escapeHtml(unsafe) {
  return unsafe
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#039;')
}

function createDebugSection(title) {
  const section = mtb_ui.makeElement('div', {
    margin: '8px 0',
    padding: '8px',
    borderRadius: '4px',
    backgroundColor: 'rgba(0,0,0,0.2)',
  })

  const header = mtb_ui.makeElement('h3', {
    margin: '0 0 8px 0',
    padding: '4px 0',
    borderBottom: '1px solid rgba(255,255,255,0.1)',
    fontSize: '14px',
    fontWeight: 'bold',
    color: '#9f9',
  })
  header.textContent = title
  section.appendChild(header)

  return section
}

function createDebugContent(content, type) {
  const wrapper = mtb_ui.makeElement('div', {
    margin: '4px 0',
  })

  if (type === 'text') {
    const text = mtb_ui.makeElement('p', {
      margin: '2px 0',
      fontFamily: 'monospace',
      whiteSpace: 'pre-wrap',
    })
    text.innerHTML = content
    wrapper.appendChild(text)
  } else if (type === 'image') {
    const img = mtb_ui.makeElement('img', {
      width: '100%',
      borderRadius: '2px',
    })
    img.src = content
    wrapper.appendChild(img)
  }

  return wrapper
}

app.registerExtension({
  name: 'mtb.Debug',

  /**

   * @param {NodeType} nodeType

   * @param {NodeData} nodeData

   * @param {*} app

   */
  async beforeRegisterNodeDef(nodeType, nodeData, app) {
    if (nodeData.name === 'Debug (mtb)') {
      const onNodeCreated = nodeType.prototype.onNodeCreated
      nodeType.prototype.onNodeCreated = function (...args) {
        this.options = {}
        const r = onNodeCreated ? onNodeCreated.apply(this, args) : undefined
        this.addInput('anything_1', '*')
        return r
      }

      const onConnectionsChange = nodeType.prototype.onConnectionsChange
      /**

       * @param {OnConnectionsChangeParams} args

       */
      nodeType.prototype.onConnectionsChange = function (...args) {
        const [_type, index, connected, link_info, ioSlot] = args
        const r = onConnectionsChange
          ? onConnectionsChange.apply(this, args)
          : undefined
        // TODO: remove all widgets on disconnect once computed
        shared.dynamic_connection(this, index, connected, 'anything_', '*', {
          link: link_info,
          ioSlot: ioSlot,
        })

        //- infer type
        if (link_info) {
          // const fromNode = this.graph._nodes.find(
          // (otherNode) => otherNode.id === link_info.origin_id,
          // )
          // const fromNode = app.graph.getNodeById(link_info.origin_id)
          const { from } = shared.nodesFromLink(this, link_info)
          if (!from || this.inputs.length === 0) return
          const type = from.outputs[link_info.origin_slot].type
          this.inputs[index].type = type
          // this.inputs[index].label = type.toLowerCase()
        }
        //- restore dynamic input
        if (!connected) {
          this.inputs[index].type = '*'
          this.inputs[index].label = `anything_${index + 1}`
        }
        return r
      }

      const onExecuted = nodeType.prototype.onExecuted
      nodeType.prototype.onExecuted = function (...args) {
        onExecuted?.apply(this, args)
        const [data, ..._rest] = args

        if (this.widgets) {
          let tgt_len = this.widgets.length
          for (let i = 0; i < this.widgets.length; i++) {
            if (
              this.widgets[i].name !== 'output_to_console' &&
              this.widgets[i].name !== 'as_detailed_types'
            ) {
              this.widgets[i].onRemove?.()
              this.widgets[i].onRemoved?.()
              tgt_len -= 1
            }
          }
          this.widgets.length = tgt_len
        }

        const inputData = {}

        const uiData = data.ui || data

        if (uiData.items) {
          uiData.items.forEach((item) => {
            const inputName = item.input
            if (!inputData[inputName]) {
              inputData[inputName] = { text: [], b64_images: [] }
            }
            if (item.text) {
              inputData[inputName].text.push(...item.text)
            }
            if (item.b64_images) {
              inputData[inputName].b64_images.push(...item.b64_images)
            }
          })
        }

        let widgetI = 1
        for (const [inputName, content] of Object.entries(inputData)) {
          if (content.text.length === 0 && content.b64_images.length === 0) {
            continue
          }

          const section = createDebugSection(inputName)

          if (content.text.length > 0) {
            content.text.forEach((text) => {
              section.appendChild(createDebugContent(text, 'text'))
            })
          }

          if (content.b64_images.length > 0) {
            content.b64_images.forEach((img) => {
              section.appendChild(createDebugContent(img, 'image'))
            })
          }

          this.addDOMWidget(`debug_section_${widgetI}`, 'CUSTOM', section, {})
          widgetI++
        }

        this.onRemoved = function () {
          for (const widget of this.widgets) {
            if (widget.canvas) {
              widget.canvas.remove()
            }
            widget.onRemoved?.()
            widget.onRemove?.()
          }
          shared.cleanupNode(this)
        }
      }
    }
  },
})