move patch to setup hook

This commit is contained in:
bymyself 2025-02-21 06:25:44 -07:00
parent 0fae537e48
commit 929d0f5bdf

View File

@ -42,100 +42,49 @@ class WorkflowMetadataExtension {
return await res.json(); return await res.json();
} }
/** async init() {
* Set node versions for the given graph this.installedNodes = await this.getInstalledNodes();
* @param {LGraph} graph The graph to process this.comfyCoreVersion = (await api.getSystemStats()).system.comfyui_version;
* @param {Object} workflow The serialized workflow object }
*/
setGraphNodeVersions(graph, workflow) {
if (!graph?.nodes || !workflow?.nodes) return;
// Create a map of workflow nodes by ID setup() {
const workflowNodesById = {}; const extension = this;
const originalSerialize = LGraph.prototype.serialize;
LGraph.prototype.serialize = function () {
const workflow = originalSerialize.apply(this, arguments);
const graph = this;
try {
// Add version metadata to each node in the workflow
for (const node of workflow.nodes) { for (const node of workflow.nodes) {
if (node.id != null) { const graphNode = graph.getNodeById(node.id);
workflowNodesById[node.id] = node; if (!graphNode?.constructor?.nodeData?.python_module) continue;
}
}
// Process each graph node and find its corresponding workflow node by ID const nodeProperties = (node.properties ??= {});
for (const graphNode of graph.nodes) { const modules =
const workflowNode = workflowNodesById[graphNode.id]; graphNode.constructor.nodeData.python_module.split(".");
if (!workflowNode) continue;
this.setNodeVersion(graphNode, workflowNode);
}
}
/**
* Set version information for a single node
* @param {Object} graphNode The graph node
* @param {Object} workflowNode The workflow node
*/
setNodeVersion(graphNode, workflowNode) {
const nodeProperties = (workflowNode.properties ??= {});
const nodeData = graphNode.constructor?.nodeData;
// The node is missing or is a frontend only node
// (node was not constructed in registerNodes closure where nodeData is set)
if (!nodeData) {
return;
}
const modules = nodeData.python_module.split(".");
const moduleType = modules[0]; const moduleType = modules[0];
if (moduleType === "custom_nodes") { if (moduleType === "custom_nodes") {
this.setCustomNodeVersion(modules[1], nodeProperties); const nodePackageName = modules[1];
const { cnr_id, ver } =
extension.installedNodes[nodePackageName] ??
extension.installedNodes[nodePackageName.toLowerCase()] ??
{};
if (cnr_id === "comfy-core") continue; // reserved name
if (cnr_id) nodeProperties.cnr_id = cnr_id;
if (ver) nodeProperties.ver = ver;
} else if (["nodes", "comfy_extras"].includes(moduleType)) { } else if (["nodes", "comfy_extras"].includes(moduleType)) {
this.setCoreNodeVersion(nodeProperties); nodeProperties.cnr_id = "comfy-core";
} else { nodeProperties.ver = extension.comfyCoreVersion;
console.warn(`Unknown node source: ${nodeData.python_module}`);
} }
} }
/**
* Set version for custom nodes
* @private
*/
setCustomNodeVersion(nodePackageName, nodeProperties) {
const nodeInfo =
this.installedNodes[nodePackageName] ??
this.installedNodes[nodePackageName.toLowerCase()];
if (nodeInfo) {
if (nodeInfo.cnr_id === "comfy-core") return; // reserved package name
// Preserve workflow cnr_id and version if they exist
nodeProperties.cnr_id ??= nodeInfo.cnr_id;
nodeProperties.version ??= nodeInfo.ver;
}
}
/**
* Set version for core nodes
* @private
*/
setCoreNodeVersion(nodeProperties) {
nodeProperties.cnr_id ??= "comfy-core";
nodeProperties.version ??= this.comfyCoreVersion;
}
async init() {
const extension = this;
this.installedNodes = await this.getInstalledNodes();
this.comfyCoreVersion = (await api.getSystemStats()).system.comfyui_version;
// Attach metadata when app.graphToPrompt is called.
const originalSerialize = LGraph.prototype.serialize;
LGraph.prototype.serialize = function () {
const workflow = originalSerialize.apply(this, arguments);
// Add metadata to the nodes
const graph = this;
try {
extension.setGraphNodeVersions(graph, workflow);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
} }
return workflow; return workflow;
}; };
} }