|
| 1 | +#!/usr/bin/env bun |
| 2 | + |
| 3 | +import { LGraph, LGraphNode, LiteGraph } from '@comfyorg/litegraph' |
| 4 | +import { ComfyWorkflowJSON, validateComfyWorkflow } from './schemas/comfyWorkflowSchema' |
| 5 | + |
| 6 | +// Mock node classes based on the object info |
| 7 | +function createMockNodeClass(nodeType: string, nodeInfo: any) { |
| 8 | + class MockNode extends LGraphNode { |
| 9 | + static override title = nodeInfo.display_name || nodeType |
| 10 | + static comfyClass = nodeType |
| 11 | + override comfyClass: string |
| 12 | + |
| 13 | + constructor() { |
| 14 | + super(nodeType) |
| 15 | + this.comfyClass = nodeType |
| 16 | + this.title = nodeInfo.display_name || nodeType |
| 17 | + |
| 18 | + // Add inputs in the correct order |
| 19 | + if (nodeInfo.input?.required) { |
| 20 | + const inputOrder = nodeInfo.input_order?.required || Object.keys(nodeInfo.input.required) |
| 21 | + inputOrder.forEach((inputName: string) => { |
| 22 | + const inputDef = nodeInfo.input.required[inputName] |
| 23 | + if (inputDef) { |
| 24 | + this.addInput(inputName, inputDef[0]) |
| 25 | + } |
| 26 | + }) |
| 27 | + } |
| 28 | + |
| 29 | + // Add outputs |
| 30 | + if (nodeInfo.output) { |
| 31 | + nodeInfo.output.forEach((outputType: string, index: number) => { |
| 32 | + const outputName = nodeInfo.output_name?.[index] || outputType |
| 33 | + this.addOutput(outputName, outputType) |
| 34 | + }) |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + return MockNode |
| 39 | +} |
| 40 | + |
| 41 | +function registerObjectInfoNodeDefinitions(comfyObjectInfo: any) { |
| 42 | + Object.entries(comfyObjectInfo).forEach(([nodeType, nodeInfo]: [string, any]) => { |
| 43 | + const MockNodeClass = createMockNodeClass(nodeType, nodeInfo) |
| 44 | + LiteGraph.registerNodeType(nodeType, MockNodeClass) |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +function createNodeFromData(nodeData: any, graph: LGraph, nodeMap: Map<string | number, LGraphNode>, comfyObjectInfo: any) { |
| 49 | + const node = LiteGraph.createNode(nodeData.type) |
| 50 | + if (!node) { |
| 51 | + console.error(`Failed to create node of type: ${nodeData.type}`) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + node.id = nodeData.id |
| 56 | + node.pos = [nodeData.pos[0], nodeData.pos[1]] as [number, number] |
| 57 | + |
| 58 | + graph.add(node) |
| 59 | + nodeMap.set(nodeData.id, node) |
| 60 | +} |
| 61 | + |
| 62 | +function resolveNodeInput(node: any, inputIndex: number, graph: LGraph) { |
| 63 | + console.log(` resolveNodeInput: node ${node.id}, inputIndex ${inputIndex}`) |
| 64 | + |
| 65 | + const input = node.inputs[inputIndex] |
| 66 | + if (!input || !input.link) { |
| 67 | + console.log(` No input or link: input=${!!input}, link=${input?.link}`) |
| 68 | + return null |
| 69 | + } |
| 70 | + console.log(` Input name: ${input.name}, link: ${input.link}`) |
| 71 | + |
| 72 | + // Find the link in the graph |
| 73 | + const link = graph.links?.get ? graph.links.get(input.link) : null |
| 74 | + if (!link) { |
| 75 | + console.log(` No link found in graph.links`) |
| 76 | + console.log(` graph.links type: ${typeof graph.links}`) |
| 77 | + console.log(` graph.links instanceof Map: ${graph.links instanceof Map}`) |
| 78 | + console.log(` graph.links size: ${graph.links?.size}`) |
| 79 | + if (graph.links instanceof Map) { |
| 80 | + console.log(` Available link IDs: [${Array.from(graph.links.keys()).slice(0, 10).join(', ')}]`) |
| 81 | + } |
| 82 | + return null |
| 83 | + } |
| 84 | + console.log(` Found link:`, link) |
| 85 | + |
| 86 | + // Get the source node |
| 87 | + const sourceNode = graph._nodes_by_id[link.origin_id] |
| 88 | + if (!sourceNode) { |
| 89 | + console.log(` No source node found for origin_id ${link.origin_id}`) |
| 90 | + console.log(` Available node IDs: [${Object.keys(graph._nodes_by_id).slice(0, 10).join(', ')}]`) |
| 91 | + return null |
| 92 | + } |
| 93 | + console.log(` Source node found: ${sourceNode.id} (${sourceNode.type})`) |
| 94 | + |
| 95 | + return { |
| 96 | + origin_id: link.origin_id, |
| 97 | + origin_slot: link.origin_slot |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function createWorkflowGraph(comfyObjectInfo: any, workflow: ComfyWorkflowJSON) { |
| 102 | + const graph = new LGraph() |
| 103 | + const nodeMap = new Map<string | number, LGraphNode>() |
| 104 | + |
| 105 | + // First pass: Create all nodes |
| 106 | + for (const nodeData of workflow.nodes) { |
| 107 | + createNodeFromData(nodeData, graph, nodeMap, comfyObjectInfo) |
| 108 | + } |
| 109 | + |
| 110 | + // Second pass: Create connections based on links |
| 111 | + if (workflow.links && Array.isArray(workflow.links)) { |
| 112 | + const linkMap = new Map() |
| 113 | + for (const link of workflow.links) { |
| 114 | + if (Array.isArray(link) && link.length >= 6) { |
| 115 | + linkMap.set(link[0], link) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Connect nodes based on the node inputs that reference links |
| 120 | + for (const nodeData of workflow.nodes) { |
| 121 | + const targetNode = nodeMap.get(nodeData.id) |
| 122 | + if (!targetNode || !nodeData.inputs) continue |
| 123 | + |
| 124 | + for (const input of nodeData.inputs) { |
| 125 | + if (input.link && linkMap.has(input.link)) { |
| 126 | + const link = linkMap.get(input.link) |
| 127 | + const [, sourceNodeId, sourceSlot] = link |
| 128 | + const sourceNode = nodeMap.get(sourceNodeId) |
| 129 | + |
| 130 | + const targetInputIndex = targetNode.inputs.findIndex( |
| 131 | + (nodeInput) => nodeInput.name === input.name |
| 132 | + ) |
| 133 | + |
| 134 | + if (sourceNode && targetInputIndex !== -1) { |
| 135 | + try { |
| 136 | + sourceNode.connect(sourceSlot, targetNode, targetInputIndex) |
| 137 | + } catch (error) { |
| 138 | + console.error(`Connection failed: ${error.message ?? error}`) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return graph |
| 147 | +} |
| 148 | + |
| 149 | +async function testGraphToPrompt(graph: LGraph) { |
| 150 | + console.log(`\nTesting graph to prompt conversion...`) |
| 151 | + |
| 152 | + // Find KSampler node |
| 153 | + const kSamplerNode = graph._nodes.find((n: any) => n.type === 'KSampler') |
| 154 | + if (!kSamplerNode) { |
| 155 | + console.log(`No KSampler node found in graph`) |
| 156 | + return |
| 157 | + } |
| 158 | + |
| 159 | + console.log(`Found KSampler node ${kSamplerNode.id} with ${kSamplerNode.inputs?.length} inputs`) |
| 160 | + |
| 161 | + // Test resolveNodeInput for each input |
| 162 | + for (let i = 0; i < kSamplerNode.inputs.length; i++) { |
| 163 | + console.log(`\nTesting input ${i}:`) |
| 164 | + const resolved = resolveNodeInput(kSamplerNode, i, graph) |
| 165 | + console.log(` Resolved: ${resolved ? `${resolved.origin_id}:${resolved.origin_slot}` : 'null'}`) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +;(async () => { |
| 170 | + const cliArgs = Bun.argv.slice(2) |
| 171 | + |
| 172 | + if (cliArgs.length != 2) { |
| 173 | + console.log(`USAGE ${import.meta.file} <path-to-object_info.json> <path-to-workflow.json>`) |
| 174 | + process.exit(1); |
| 175 | + } |
| 176 | + |
| 177 | + try { |
| 178 | + const objectInfoFile = Bun.file(cliArgs[0]) |
| 179 | + const objectInfoJson = await objectInfoFile.text() |
| 180 | + const objectInfo = JSON.parse(objectInfoJson) |
| 181 | + |
| 182 | + const workflowFile = Bun.file(cliArgs[1]) |
| 183 | + const workflowJson = await workflowFile.text() |
| 184 | + const workflowData = JSON.parse(workflowJson) |
| 185 | + |
| 186 | + const workflow = await validateComfyWorkflow(workflowData) |
| 187 | + if (!workflow) { |
| 188 | + throw new Error(`${cliArgs[1]} is not a valid workflow`) |
| 189 | + } |
| 190 | + |
| 191 | + registerObjectInfoNodeDefinitions(objectInfo) |
| 192 | + const graph = createWorkflowGraph(objectInfo, workflow) |
| 193 | + |
| 194 | + await testGraphToPrompt(graph) |
| 195 | + |
| 196 | + } catch (e) { |
| 197 | + console.error(`Error: ${e.message ?? e}`) |
| 198 | + console.error(`Stack: ${e.stack}`) |
| 199 | + process.exit(1); |
| 200 | + } |
| 201 | +})() |
0 commit comments