|
import { app } from "../../scripts/app.js"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function stripComments(str) { |
|
return str.replace(/\/\*[\s\S]*?\*\/|\/\/.*/g,''); |
|
} |
|
|
|
app.registerExtension({ |
|
name: "Comfy.DynamicPrompts", |
|
nodeCreated(node) { |
|
if (node.widgets) { |
|
|
|
|
|
const widgets = node.widgets.filter( |
|
(n) => (n.type === "customtext" && n.dynamicPrompts !== false) || n.dynamicPrompts |
|
); |
|
for (const widget of widgets) { |
|
|
|
widget.serializeValue = (workflowNode, widgetIndex) => { |
|
let prompt = stripComments(widget.value); |
|
while (prompt.replace("\\{", "").includes("{") && prompt.replace("\\}", "").includes("}")) { |
|
const startIndex = prompt.replace("\\{", "00").indexOf("{"); |
|
const endIndex = prompt.replace("\\}", "00").indexOf("}"); |
|
|
|
const optionsString = prompt.substring(startIndex + 1, endIndex); |
|
const options = optionsString.split("|"); |
|
|
|
const randomIndex = Math.floor(Math.random() * options.length); |
|
const randomOption = options[randomIndex]; |
|
|
|
prompt = prompt.substring(0, startIndex) + randomOption + prompt.substring(endIndex + 1); |
|
} |
|
|
|
|
|
if (workflowNode?.widgets_values) |
|
workflowNode.widgets_values[widgetIndex] = prompt; |
|
|
|
return prompt; |
|
}; |
|
} |
|
} |
|
}, |
|
}); |
|
|