Add visual ComfyUI workflow export

This commit is contained in:
Morpheus
2026-10-09 13:36:17 +00:00
parent b7e920998d
commit 787ecd8b08
3 changed files with 2648 additions and 0 deletions
+138
View File
@@ -0,0 +1,138 @@
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
const server = process.env.COMFYUI_URL || 'http://192.168.1.202:8188';
const source = path.resolve(process.argv[2] || 'optimized/R2V/minimax/sparse-latent-upscale/workflow.json');
const target = path.resolve(process.argv[3] || path.join(path.dirname(source), 'workflow-ui.json'));
const graph = JSON.parse(fs.readFileSync(source, 'utf8'));
const info = await fetch(`${server}/object_info`).then((r) => r.json());
const ids = Object.keys(graph);
const idSet = new Set(ids);
const schemas = new Map(ids.map((id) => [id, info[graph[id].class_type] || {}]));
const links = [];
let nextLink = 1;
const dependencies = new Map(ids.map((id) => [id, []]));
function connected(value) {
return Array.isArray(value) && value.length === 2 && idSet.has(String(value[0]));
}
function schemaInput(nodeId, name) {
const schema = schemas.get(nodeId) || {};
return schema.input?.required?.[name] || schema.input?.optional?.[name] || null;
}
function inputOrder(nodeId) {
const order = schemas.get(nodeId)?.input_order || {};
return [...(order.required || []), ...(order.optional || [])];
}
function typeOf(nodeId, name, value) {
const spec = schemaInput(nodeId, name);
if (spec?.[0]) return Array.isArray(spec[0]) ? 'COMBO' : spec[0];
if (connected(value)) return 'LINK';
if (typeof value === 'boolean') return 'BOOLEAN';
if (typeof value === 'number') return Number.isInteger(value) ? 'INT' : 'FLOAT';
return 'STRING';
}
function outputName(nodeId, slot) {
return schemas.get(nodeId)?.output_name?.[slot] || `output_${slot}`;
}
function outputType(nodeId, slot) {
return schemas.get(nodeId)?.output?.[slot] || '*';
}
for (const [targetId, node] of Object.entries(graph)) {
for (const [inputName, value] of Object.entries(node.inputs || {})) {
if (!connected(value)) continue;
const originId = String(value[0]);
const originSlot = Number(value[1]);
const linkId = nextLink++;
links.push([linkId, Number(originId), originSlot, Number(targetId), inputOrder(targetId).indexOf(inputName), outputType(originId, originSlot)]);
dependencies.get(targetId).push(originId);
}
}
const levels = new Map();
function level(id, visiting = new Set()) {
if (levels.has(id)) return levels.get(id);
if (visiting.has(id)) return 0;
visiting.add(id);
const value = Math.max(-1, ...(dependencies.get(id) || []).map((dep) => level(dep, visiting))) + 1;
levels.set(id, value);
return value;
}
ids.forEach((id) => level(id));
const byLevel = new Map();
for (const id of ids) {
const group = byLevel.get(level(id)) || [];
group.push(id);
byLevel.set(level(id), group);
}
function inputEntries(nodeId, node) {
const schemaNames = inputOrder(nodeId);
const names = [...new Set([...schemaNames, ...Object.keys(node.inputs || {})])];
const out = [];
for (const name of names) {
const value = node.inputs?.[name];
const link = links.find((item) => item[3] === Number(nodeId) && item[4] === schemaNames.indexOf(name));
const spec = schemaInput(nodeId, name);
const type = typeOf(nodeId, name, value);
if (link) out.push({ name, type, link: link[0] });
else if (schemaNames.includes(name) || Object.prototype.hasOwnProperty.call(node.inputs || {}, name)) out.push({ name, type, widget: { name } });
}
return out;
}
function widgetValues(nodeId, node) {
const schemaNames = inputOrder(nodeId);
const unknown = Object.keys(node.inputs || {}).filter((name) => !schemaNames.includes(name) && !connected(node.inputs[name]));
const names = [...schemaNames, ...unknown].filter((name) => Object.prototype.hasOwnProperty.call(node.inputs || {}, name) && !connected(node.inputs[name]));
return names.map((name) => node.inputs[name]);
}
const uiNodes = ids.map((id) => {
const node = graph[id];
const schema = schemas.get(id) || {};
const outputs = (schema.output || []).map((type, slot) => ({
name: outputName(id, slot),
type,
links: links.filter((item) => item[1] === Number(id) && item[2] === slot).map((item) => item[0]),
slot_index: slot,
}));
const column = level(id);
const row = (byLevel.get(column) || []).indexOf(id);
return {
id: Number(id),
type: node.class_type,
pos: [column * 360, row * 180],
size: { 0: 300, 1: 120 },
flags: {},
order: column,
mode: 0,
inputs: inputEntries(id, node),
outputs,
properties: { cnr_id: node._meta?.title || node.class_type },
widgets_values: widgetValues(id, node),
};
});
const workflow = {
last_node_id: Math.max(...ids.map(Number)),
last_link_id: links.length,
nodes: uiNodes,
links,
groups: [],
config: {},
extra: {
ds: { scale: 0.65, offset: [0, 0] },
workflow_stable_id: 'minimax-h3-sparse-latent-upscale-ui',
},
version: 0.4,
};
fs.writeFileSync(target, `${JSON.stringify(workflow, null, 2)}\n`);
console.log(JSON.stringify({ source, target, nodes: uiNodes.length, links: links.length }, null, 2));