66 lines
1.8 KiB
JavaScript
66 lines
1.8 KiB
JavaScript
import { app } from '../../../scripts/app.js'
|
|
|
|
const MIN_WIDTH = 420
|
|
const MIN_HEIGHT = 400
|
|
|
|
function chainCallback(object, property, callback) {
|
|
const original = object[property]
|
|
object[property] = function() {
|
|
const result = original?.apply(this, arguments)
|
|
callback.apply(this, arguments)
|
|
return result
|
|
}
|
|
}
|
|
|
|
function findWidget(node, name) {
|
|
return node.widgets?.find(widget => widget.name === name)
|
|
}
|
|
|
|
function setNodeText(node, text) {
|
|
const widget = findWidget(node, 'text')
|
|
if (!widget) return
|
|
|
|
widget.value = text
|
|
|
|
// Style it if possible
|
|
if (widget.inputEl) {
|
|
widget.inputEl.readOnly = true;
|
|
widget.inputEl.style.opacity = "0.8";
|
|
}
|
|
|
|
node.setSize([
|
|
Math.max(node.size?.[0] ?? 0, MIN_WIDTH),
|
|
Math.max(node.size?.[1] ?? 0, MIN_HEIGHT),
|
|
])
|
|
app.graph.setDirtyCanvas(true, true)
|
|
}
|
|
|
|
app.registerExtension({
|
|
name: 'PromptBench.ListChatView',
|
|
|
|
async beforeRegisterNodeDef(nodeType, nodeData) {
|
|
if (nodeData?.name !== 'PBListChatView') return
|
|
|
|
chainCallback(nodeType.prototype, 'onNodeCreated', function() {
|
|
this.setSize([MIN_WIDTH, MIN_HEIGHT])
|
|
const widget = findWidget(this, 'text')
|
|
if (widget) {
|
|
// Ensure it's a multiline-style widget
|
|
widget.type = "customtext"
|
|
}
|
|
})
|
|
|
|
chainCallback(nodeType.prototype, 'onExecuted', function(message) {
|
|
// Handle both message and message.ui for robustness
|
|
const ui = message?.ui ?? message;
|
|
console.log("UI", message)
|
|
if (ui?.text) {
|
|
setNodeText(this, ui.text[0])
|
|
} else if (ui?.chat) {
|
|
// Fallback for previous implementation
|
|
setNodeText(this, ui.chat[0])
|
|
}
|
|
})
|
|
},
|
|
})
|