mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-07-18 09:48:09 +00:00
When bypassing or muting a subgraph the contents are no longer bypassed along with it. This behaviour was less than ideal because it meant that toggling the bypass of a single subgraph node twice could change the behaviour of the node. It is entirely intended that a subgraph node which is bypassed does not have it's children execute. As part of testing this behaviour, it was found that nodes inside of a bypassed subgraph are still considered for execution even if boundry links are treated as disconnected. The following example would execute even if the subgraph is muted. <img width="826" height="476" alt="image" src="https://github.com/user-attachments/assets/7b282873-e114-494d-b8f1-74c373859151" /> To resolve this, the PR does not add the contents of a subgraphNode which is muted or bypassed to the execution map. Resolves #8489 ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8494-Don-t-bypass-subgraph-contents-with-subgraph-2f86d73d365081aeba8dd2990b6ba0ad) by [Unito](https://www.unito.io)
158 lines
4.6 KiB
TypeScript
158 lines
4.6 KiB
TypeScript
import type {
|
|
ExecutableLGraphNode,
|
|
ExecutionId,
|
|
LGraph
|
|
} from '@/lib/litegraph/src/litegraph'
|
|
import {
|
|
ExecutableNodeDTO,
|
|
LGraphEventMode
|
|
} from '@/lib/litegraph/src/litegraph'
|
|
import type {
|
|
ComfyApiWorkflow,
|
|
ComfyWorkflowJSON
|
|
} from '@/platform/workflow/validation/schemas/workflowSchema'
|
|
|
|
import { ExecutableGroupNodeDTO, isGroupNode } from './executableGroupNodeDto'
|
|
import { compressWidgetInputSlots } from './litegraphUtil'
|
|
|
|
/**
|
|
* Converts the current graph workflow for sending to the API.
|
|
* @note Node widgets are updated before serialization to prepare queueing.
|
|
*
|
|
* @param graph The graph to convert.
|
|
* @param options The options for the conversion.
|
|
* - `sortNodes`: Whether to sort the nodes by execution order.
|
|
* @returns The workflow and node links
|
|
*/
|
|
export const graphToPrompt = async (
|
|
graph: LGraph,
|
|
options: { sortNodes?: boolean } = {}
|
|
): Promise<{ workflow: ComfyWorkflowJSON; output: ComfyApiWorkflow }> => {
|
|
const { sortNodes = false } = options
|
|
|
|
for (const node of graph.computeExecutionOrder(false)) {
|
|
const innerNodes = node.getInnerNodes
|
|
? node.getInnerNodes(new Map())
|
|
: [node]
|
|
for (const innerNode of innerNodes) {
|
|
if (innerNode.isVirtualNode) {
|
|
innerNode.applyToGraph?.()
|
|
}
|
|
}
|
|
}
|
|
|
|
const workflow = graph.serialize({ sortNodes })
|
|
|
|
// Remove localized_name from the workflow
|
|
for (const node of workflow.nodes) {
|
|
for (const slot of node.inputs ?? []) {
|
|
delete slot.localized_name
|
|
}
|
|
for (const slot of node.outputs ?? []) {
|
|
delete slot.localized_name
|
|
}
|
|
}
|
|
|
|
compressWidgetInputSlots(workflow)
|
|
workflow.extra ??= {}
|
|
workflow.extra.frontendVersion = __COMFYUI_FRONTEND_VERSION__
|
|
|
|
const nodeDtoMap = new Map<ExecutionId, ExecutableLGraphNode>()
|
|
for (const node of graph.computeExecutionOrder(false)) {
|
|
const dto: ExecutableLGraphNode = isGroupNode(node)
|
|
? new ExecutableGroupNodeDTO(node, [], nodeDtoMap)
|
|
: new ExecutableNodeDTO(node, [], nodeDtoMap)
|
|
|
|
nodeDtoMap.set(dto.id, dto)
|
|
|
|
if (
|
|
node.mode === LGraphEventMode.NEVER ||
|
|
node.mode === LGraphEventMode.BYPASS
|
|
) {
|
|
continue
|
|
}
|
|
|
|
for (const innerNode of dto.getInnerNodes()) {
|
|
nodeDtoMap.set(innerNode.id, innerNode)
|
|
}
|
|
}
|
|
|
|
const output: ComfyApiWorkflow = {}
|
|
// Process nodes in order of execution
|
|
for (const node of nodeDtoMap.values()) {
|
|
// Don't serialize muted nodes
|
|
if (
|
|
node.isVirtualNode ||
|
|
node.mode === LGraphEventMode.NEVER ||
|
|
node.mode === LGraphEventMode.BYPASS
|
|
) {
|
|
continue
|
|
}
|
|
|
|
const inputs: ComfyApiWorkflow[string]['inputs'] = {}
|
|
const { widgets } = node
|
|
|
|
// Store all widget values
|
|
if (widgets) {
|
|
for (const [i, widget] of widgets.entries()) {
|
|
if (!widget.name || widget.options?.serialize === false) continue
|
|
|
|
const widgetValue = widget.serializeValue
|
|
? await widget.serializeValue(node, i)
|
|
: widget.value
|
|
// By default, Array values are reserved to represent node connections.
|
|
// We need to wrap the array as an object to avoid the misinterpretation
|
|
// of the array as a node connection.
|
|
// The backend automatically unwraps the object to an array during
|
|
// execution.
|
|
inputs[widget.name] = Array.isArray(widgetValue)
|
|
? {
|
|
__value__: widgetValue
|
|
}
|
|
: widgetValue
|
|
}
|
|
}
|
|
|
|
// Store all node links
|
|
for (const [i, input] of node.inputs.entries()) {
|
|
const resolvedInput = node.resolveInput(i)
|
|
if (!resolvedInput) continue
|
|
|
|
// Resolved to an actual widget value rather than a node connection
|
|
if (resolvedInput.widgetInfo) {
|
|
const { value } = resolvedInput.widgetInfo
|
|
inputs[input.name] = Array.isArray(value) ? { __value__: value } : value
|
|
continue
|
|
}
|
|
|
|
inputs[input.name] = [
|
|
String(resolvedInput.origin_id),
|
|
// @ts-expect-error link.origin_slot is already number.
|
|
parseInt(resolvedInput.origin_slot)
|
|
]
|
|
}
|
|
|
|
output[String(node.id)] = {
|
|
inputs,
|
|
// TODO(huchenlei): Filter out all nodes that cannot be mapped to a
|
|
// comfyClass.
|
|
class_type: node.comfyClass!,
|
|
// Ignored by the backend.
|
|
_meta: {
|
|
title: node.title
|
|
}
|
|
}
|
|
}
|
|
|
|
// Remove inputs connected to removed nodes
|
|
for (const { inputs } of Object.values(output)) {
|
|
for (const [i, input] of Object.entries(inputs)) {
|
|
if (Array.isArray(input) && input.length === 2 && !output[input[0]]) {
|
|
delete inputs[i]
|
|
}
|
|
}
|
|
}
|
|
|
|
return { workflow: workflow as ComfyWorkflowJSON, output }
|
|
}
|