diff --git a/.fallowrc.jsonc b/.fallowrc.jsonc index 6d258e6c429..91b721e9ec5 100644 --- a/.fallowrc.jsonc +++ b/.fallowrc.jsonc @@ -212,6 +212,12 @@ "dupesBaseline": ".fallow-baselines/dupes.json" }, + "usedClassMembers": [ + // ExecutableNodeDTO calls this hook through the entity-registry + // LGraphNode interface, which Fallow does not link to subclass overrides. + { "extends": "LGraphNode", "members": ["resolveVirtualValue"] } + ], + "rules": { "boundary-violation": "warn", "dev-dependencies-in-production": "error" diff --git a/browser_tests/assets/primitive/primitive_node_force_input.json b/browser_tests/assets/primitive/primitive_node_force_input.json new file mode 100644 index 00000000000..05411e22188 --- /dev/null +++ b/browser_tests/assets/primitive/primitive_node_force_input.json @@ -0,0 +1,68 @@ +{ + "last_node_id": 2, + "last_link_id": 1, + "nodes": [ + { + "id": 1, + "type": "PrimitiveNode", + "pos": [50, 50], + "size": [300, 110], + "flags": {}, + "order": 0, + "mode": 0, + "inputs": [], + "outputs": [ + { + "name": "INT", + "type": "INT", + "links": [1], + "slot_index": 0, + "widget": { + "name": "int_input" + } + } + ], + "properties": { + "Run widget replace on values": false + }, + "widgets_values": [3, "fixed"] + }, + { + "id": 2, + "type": "DevToolsNodeWithForceInput", + "pos": [450, 50], + "size": [315, 110], + "flags": {}, + "order": 1, + "mode": 0, + "inputs": [ + { + "name": "int_input", + "type": "INT", + "link": 1 + }, + { + "name": "float_input", + "type": "FLOAT", + "link": null, + "shape": 7 + } + ], + "outputs": [], + "properties": { + "Node name for S&R": "DevToolsNodeWithForceInput" + }, + "widgets_values": [1] + } + ], + "links": [[1, 1, 0, 2, 0, "INT"]], + "groups": [], + "config": {}, + "extra": { + "ds": { + "scale": 1, + "offset": [0, 0] + } + }, + "version": 0.4 +} diff --git a/browser_tests/tests/primitiveNode.spec.ts b/browser_tests/tests/primitiveNode.spec.ts index 9b8df272c7b..c539be93c29 100644 --- a/browser_tests/tests/primitiveNode.spec.ts +++ b/browser_tests/tests/primitiveNode.spec.ts @@ -150,3 +150,33 @@ test.describe('Primitive Node', { tag: ['@screenshot', '@node'] }, () => { }) }) }) + +test.describe( + 'Primitive Node forced inputs', + { tag: ['@node', '@vue-nodes'] }, + () => { + test('Can serialize to forced inputs', async ({ comfyPage }) => { + await comfyPage.workflow.loadWorkflow( + 'primitive/primitive_node_force_input' + ) + const input = comfyPage.vueNodes + .getNodeByTitle('Primitive') + .getByRole('spinbutton', { name: 'value', exact: true }) + await expect(input).toHaveValue('3') + + await input.fill('5') + await input.blur() + + await expect + .poll( + async () => + Object.values( + await comfyPage.workflow.getExportedWorkflow({ api: true }) + ).find((entry) => entry.class_type === 'DevToolsNodeWithForceInput') + ?.inputs.int_input, + { message: 'Serialized prompt has primitive value' } + ) + .toBe(5) + }) + } +) diff --git a/src/extensions/core/widgetInputs.test.ts b/src/extensions/core/widgetInputs.test.ts index 408e6d7e8b0..ce4f308ce19 100644 --- a/src/extensions/core/widgetInputs.test.ts +++ b/src/extensions/core/widgetInputs.test.ts @@ -3,6 +3,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest' import { LGraph, + LGraphEventMode, LGraphNode, LiteGraph, LLink @@ -39,6 +40,7 @@ beforeEach(() => { }) const { rerouteNodeExtension } = await import('./rerouteNode') +const { graphToPrompt } = await import('@/utils/executionUtil') /** * Applies the extension's `beforeRegisterNodeDef` to a throwaway node class. @@ -402,6 +404,110 @@ describe('PrimitiveNode', () => { expect(primitive.widgets?.[0].value).toBe(111) }) + + function stringTargetFixture() { + const graph = new LGraph() + const source = new LGraphNode('Source') + graph.add(source) + source.addWidget('text', 'subject', 'cat', () => {}) + const target = new LGraphNode('Target') + target.comfyClass = 'Target' + graph.add(target) + target.addInput('text', 'STRING') + const primitive = new PrimitiveNode('Primitive') + graph.add(primitive) + return { graph, primitive, target } + } + + it.for([ + { + label: 'sends its value', + mode: LGraphEventMode.ALWAYS, + replace: false, + value: 'a photo of %Source.subject%', + expected: 'a photo of %Source.subject%' + }, + { + label: 'applies text replacements to its value', + mode: LGraphEventMode.ALWAYS, + replace: true, + value: 'a photo of %Source.subject%', + expected: 'a photo of cat' + }, + { + label: 'sends nothing while muted', + mode: LGraphEventMode.NEVER, + replace: false, + value: 'a photo of cat', + expected: undefined + }, + { + label: 'sends nothing while bypassed', + mode: LGraphEventMode.BYPASS, + replace: false, + value: 'a photo of cat', + expected: undefined + } + ])( + '$label to a forceInput without a widget', + async ({ mode, replace, value, expected }) => { + const { graph, primitive, target } = stringTargetFixture() + primitive.connect(0, target, 0) + primitive.mode = mode + primitive.properties['Run widget replace on values'] = replace + primitive.widgets![0].value = value + + const { output } = await graphToPrompt(graph) + + expect(output[String(target.id)].inputs.text).toBe(expected) + } + ) + + it("keeps the target widget's serialized value on a widget input", async () => { + const { graph, primitive, target } = stringTargetFixture() + target.inputs[0].widget = { + name: 'text', + [GET_CONFIG]: () => ['STRING', {}] + } + const widget = target.addWidget('text', 'text', '', () => {}) + widget.serializeValue = () => String(widget.value).toUpperCase() + primitive.connect(0, target, 0) + primitive.widgets![0].value = 'a photo of cat' + + const { output } = await graphToPrompt(graph) + + expect(output[String(target.id)].inputs.text).toBe('A PHOTO OF CAT') + }) + + it('sends its value through a bypassed node only to inputs without a widget', async () => { + const { graph, primitive, target } = stringTargetFixture() + target.inputs[0].widget = { + name: 'text', + [GET_CONFIG]: () => ['STRING', {}] + } + const widget = target.addWidget('text', 'text', 'own', () => {}) + widget.serializeValue = () => String(widget.value).toUpperCase() + const forceInputTarget = new LGraphNode('ForceInputTarget') + forceInputTarget.comfyClass = 'ForceInputTarget' + graph.add(forceInputTarget) + forceInputTarget.addInput('text', 'STRING') + const bypassed = new LGraphNode('Bypassed') + graph.add(bypassed) + bypassed.addInput('text', 'STRING') + bypassed.addOutput('text', 'STRING') + bypassed.mode = LGraphEventMode.BYPASS + primitive.connect(0, bypassed, 0) + bypassed.connect(0, target, 0) + bypassed.connect(0, forceInputTarget, 0) + primitive.widgets![0].value = 'a photo of cat' + + const { output } = await graphToPrompt(graph) + + expect([ + output[String(target.id)].inputs.text, + output[String(forceInputTarget.id)].inputs.text + ]).toEqual(['OWN', 'a photo of cat']) + }) }) describe('getWidgetConfig', () => { diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index 1e0225f61d7..fbbc3a7aa5b 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -53,15 +53,25 @@ export class PrimitiveNode extends LGraphNode { } override applyToGraph(extraLinks: LLink[] = []) { - const sourceWidget = this.widgets?.[0] - const graph = this.graph - if (!sourceWidget || !graph) return + applyFirstWidgetValueToGraph(this, extraLinks, (value) => + this._applyTextReplacements(value) + ) + } - let v = sourceWidget.value - if (v && this.properties[replacePropertyName]) { - v = applyTextReplacements(graph, v as string) - } - applyFirstWidgetValueToGraph(this, extraLinks, () => v) + override resolveVirtualValue() { + const widget = this.widgets?.[0] + if (!widget) return + return this._applyTextReplacements( + widget.serializeValue ? widget.serializeValue(this, 0) : widget.value + ) + } + + private _applyTextReplacements(value: T): T | string { + return typeof value === 'string' && + this.properties[replacePropertyName] && + this.graph + ? applyTextReplacements(this.graph, value) + : value } override refreshComboInNode(defs?: Record): void { diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 0dbf6f3feb9..f7645fb74c8 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -1681,6 +1681,12 @@ export class LGraphNode slot: number ): { node: LGraphNode; slot: number } | undefined + /** + * Returns the prompt value a virtual node sends from output {@link slot} + * to an input that has no widget to receive it. + */ + resolveVirtualValue?(slot: number): unknown + /** * Returns the link info in the connection of an input slot * @returns object or null diff --git a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts index 154ccc9ae35..84b92060b19 100644 --- a/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts +++ b/src/lib/litegraph/src/subgraph/ExecutableNodeDTO.ts @@ -1,6 +1,7 @@ import type { LGraph, LGraphNode, + LLink, Subgraph, SubgraphNode } from '@comfyorg/graph-model/entityRegistry' @@ -41,6 +42,8 @@ type ResolvedInput = { origin_slot: number /** Boxed widget value (e.g. for widgets). If this box is `undefined`, then an input link is connected, and widget values from the subgraph node are ignored. */ widgetInfo?: { value: unknown } + /** `true` when {@link widgetInfo} is a virtual node's value, which only inputs without a widget accept. */ + virtualValue?: true } /** @@ -231,10 +234,19 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { } // Not part of a subgraph; use the original link + return this._resolveGraphLink(link, input, visited, type) + } + + private _resolveGraphLink( + link: LLink, + input: ExecutableNodeDTO['inputs'][number], + visited: Set, + type?: ISlotType + ): ResolvedInput | undefined { const outputNode = this.graph.getNodeById(link.origin_id) if (!outputNode) throw new InvalidLinkError( - `No input node found for id [${this.id}] slot [${slot}] ${input.name}` + `No input node found for id [${this.id}] slot [${link.target_slot}] ${input.name}` ) const outputNodeExecutionId: ExecutionId = [ @@ -247,11 +259,40 @@ export class ExecutableNodeDTO implements ExecutableLGraphNode { `No output node DTO found for id [${outputNodeExecutionId}]` ) - return outputNodeDto.resolveOutput( + const resolved = outputNodeDto.resolveOutput( link.origin_slot, type ?? input.type, visited ) + if (this.node.inputs[link.target_slot]?.widget) + return resolved?.virtualValue ? undefined : resolved + + return ( + resolved ?? + this._resolveWidgetlessVirtualValue(link, outputNode, outputNodeDto) + ) + } + + private _resolveWidgetlessVirtualValue( + link: LLink, + outputNode: LGraphNode, + outputNodeDto: ExecutableLGraphNode + ): ResolvedInput | undefined { + if ( + outputNodeDto.mode === LGraphEventMode.NEVER || + outputNodeDto.mode === LGraphEventMode.BYPASS + ) + return + + const value = outputNode.resolveVirtualValue?.(link.origin_slot) + if (value === undefined) return + return { + node: outputNodeDto, + origin_id: outputNodeDto.id, + origin_slot: -1, + widgetInfo: { value }, + virtualValue: true + } } /**