fred-dev/comfy_ui_ali
0
1/**2 * Attaches metadata to the workflow on save3 * - custom node pack version to all custom nodes used in the workflow4 *5 * Example metadata:6 * "nodes": {7 * "1": {8 * type: "CheckpointLoaderSimple",9 * ...10 * properties: {11 * cnr_id: "comfy-core",12 * version: "0.3.8",13 * },14 * },15 * }16 *17 * @typedef {Object} NodeInfo18 * @property {string} ver - Version (git hash or semantic version)19 * @property {string} cnr_id - ComfyRegistry node ID20 * @property {boolean} enabled - Whether the node is enabled21 */22 23import { app } from "../../scripts/app.js";24import { api } from "../../scripts/api.js";25 26class WorkflowMetadataExtension {27 constructor() {28 this.name = "Comfy.CustomNodesManager.WorkflowMetadata";29 this.installedNodes = {};30 this.comfyCoreVersion = null;31 }32 33 /**34 * Get the installed nodes info35 * @returns {Promise<Record<string, NodeInfo>>} The mapping from node name to its info.36 * ver can either be a git commit hash or a semantic version such as "1.0.0"37 * cnr_id is the id of the node in the ComfyRegistry38 * enabled is true if the node is enabled, false if it is disabled39 */40 async getInstalledNodes() {41 const res = await api.fetchApi("/customnode/installed");42 return await res.json();43 }44 45 async init() {46 this.installedNodes = await this.getInstalledNodes();47 this.comfyCoreVersion = (await api.getSystemStats()).system.comfyui_version;48 }49 50 /**51 * Called when any node is created52 * @param {LGraphNode} node The newly created node53 */54 nodeCreated(node) {55 try {56 // nodeData doesn't exist if node is missing or node is frontend only node57 if (!node?.constructor?.nodeData?.python_module) return;58 59 const nodeProperties = (node.properties ??= {});60 const modules = node.constructor.nodeData.python_module.split(".");61 const moduleType = modules[0];62 63 if (moduleType === "custom_nodes") {64 const nodePackageName = modules[1];65 const { cnr_id, aux_id, ver } =66 this.installedNodes[nodePackageName] ??67 this.installedNodes[nodePackageName.toLowerCase()] ??68 {};69 70 if (cnr_id === "comfy-core") return; // don't allow hijacking comfy-core name71 if (cnr_id) nodeProperties.cnr_id = cnr_id;72 else nodeProperties.aux_id = aux_id;73 if (ver) nodeProperties.ver = ver;74 } else if (["nodes", "comfy_extras"].includes(moduleType)) {75 nodeProperties.cnr_id = "comfy-core";76 nodeProperties.ver = this.comfyCoreVersion;77 }78 } catch (e) {79 console.error(e);80 }81 }82}83 84app.registerExtension(new WorkflowMetadataExtension());85 