From 3dd77ca2f710b246f3cadffca050aa3fcc1de358 Mon Sep 17 00:00:00 2001 From: r-vage Date: Thu, 4 Dec 2025 07:25:38 +0100 Subject: [PATCH 1/5] Fix workflow loading from PNG images with both workflow and parameters metadata - Reorder handleFile() to check workflow before parameters - Add validation to prevent JSON parse errors from crashing imports - Fix loadGraphData() to use explicit type validation instead of falsy check - Ensures ComfyUI-generated PNGs with both metadata types load the workflow, not parameters Fixes issue where large workflows (e.g., 634 nodes) were replaced with basic A1111 format when importing PNG files. --- src/scripts/app.ts | 48 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 728e35d05d..b7c0914375 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1057,7 +1057,13 @@ export class ComfyApp { } let reset_invalid_values = false - if (!graphData) { + // Use explicit validation instead of falsy check to avoid replacing + // valid but falsy values (empty objects, 0, false, etc.) + if ( + !graphData || + typeof graphData !== 'object' || + Array.isArray(graphData) + ) { graphData = defaultGraph reset_invalid_values = true } @@ -1432,6 +1438,37 @@ export class ComfyApp { this.loadTemplateData({ templates }) } + // Check workflow first - it should take priority over parameters + // when both are present (e.g., in ComfyUI-generated PNGs) + if (workflow) { + let workflowObj: ComfyWorkflowJSON + try { + workflowObj = + typeof workflow === 'string' ? JSON.parse(workflow) : workflow + } catch (err) { + console.error('Failed to parse workflow:', err) + this.showErrorOnFileLoad(file) + return + } + + // Validate workflow is a proper object + if ( + !workflowObj || + typeof workflowObj !== 'object' || + Array.isArray(workflowObj) + ) { + console.error('Invalid workflow structure') + this.showErrorOnFileLoad(file) + return + } + + await this.loadGraphData(workflowObj, true, true, fileName, { + openSource + }) + return + } + + // Use parameters as fallback when no workflow exists if (parameters) { // Note: Not putting this in `importA1111` as it is mostly not used // by external callers, and `importA1111` has no access to `app`. @@ -1444,15 +1481,6 @@ export class ComfyApp { return } - if (workflow) { - const workflowObj = - typeof workflow === 'string' ? JSON.parse(workflow) : workflow - await this.loadGraphData(workflowObj, true, true, fileName, { - openSource - }) - return - } - if (prompt) { const promptObj = typeof prompt === 'string' ? JSON.parse(prompt) : prompt this.loadApiJson(promptObj, fileName) From d14ddcb31c18f863bef67ca223ed00bf78adaa8e Mon Sep 17 00:00:00 2001 From: Rvage Date: Thu, 4 Dec 2025 08:50:44 +0100 Subject: [PATCH 2/5] Update src/scripts/app.ts Co-authored-by: Alexander Brown --- src/scripts/app.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index b7c0914375..4d3a5b059a 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1448,24 +1448,20 @@ export class ComfyApp { } catch (err) { console.error('Failed to parse workflow:', err) this.showErrorOnFileLoad(file) - return } // Validate workflow is a proper object if ( - !workflowObj || typeof workflowObj !== 'object' || Array.isArray(workflowObj) ) { console.error('Invalid workflow structure') - this.showErrorOnFileLoad(file) + } else { + await this.loadGraphData(workflowObj, true, true, fileName, { + openSource + }) return } - - await this.loadGraphData(workflowObj, true, true, fileName, { - openSource - }) - return } // Use parameters as fallback when no workflow exists From 849bdc1570a00c4364701748b76be491a500ff1d Mon Sep 17 00:00:00 2001 From: Rvage Date: Thu, 4 Dec 2025 09:02:27 +0100 Subject: [PATCH 3/5] Update workflow object handling and validation Refactor workflow object handling to allow undefined type and improve validation checks. --- src/scripts/app.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 4d3a5b059a..718df9a123 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1441,26 +1441,29 @@ export class ComfyApp { // Check workflow first - it should take priority over parameters // when both are present (e.g., in ComfyUI-generated PNGs) if (workflow) { - let workflowObj: ComfyWorkflowJSON + let workflowObj: ComfyWorkflowJSON | undefined try { workflowObj = typeof workflow === 'string' ? JSON.parse(workflow) : workflow } catch (err) { console.error('Failed to parse workflow:', err) this.showErrorOnFileLoad(file) + // Fall through to check parameters as fallback } - // Validate workflow is a proper object + // Only load workflow if parsing succeeded AND validation passed if ( - typeof workflowObj !== 'object' || - Array.isArray(workflowObj) + workflowObj && + typeof workflowObj === 'object' && + !Array.isArray(workflowObj) ) { - console.error('Invalid workflow structure') - } else { await this.loadGraphData(workflowObj, true, true, fileName, { openSource }) return + } else if (workflowObj !== undefined) { + console.error('Invalid workflow structure, trying parameters fallback') + this.showErrorOnFileLoad(file) } } From 3689d05cbb67b24313cd026ec65c71b96f815090 Mon Sep 17 00:00:00 2001 From: Rvage Date: Thu, 4 Dec 2025 09:06:59 +0100 Subject: [PATCH 4/5] Refactor workflow parsing and validation logic --- src/scripts/app.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index 718df9a123..051b76048e 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1441,30 +1441,30 @@ export class ComfyApp { // Check workflow first - it should take priority over parameters // when both are present (e.g., in ComfyUI-generated PNGs) if (workflow) { - let workflowObj: ComfyWorkflowJSON | undefined + let workflowObj: ComfyWorkflowJSON | undefined = undefined try { workflowObj = typeof workflow === 'string' ? JSON.parse(workflow) : workflow + + // Only load workflow if parsing succeeded AND validation passed + if ( + workflowObj && + typeof workflowObj === 'object' && + !Array.isArray(workflowObj) + ) { + await this.loadGraphData(workflowObj, true, true, fileName, { + openSource + }) + return + } else { + console.error('Invalid workflow structure, trying parameters fallback') + this.showErrorOnFileLoad(file) + } } catch (err) { console.error('Failed to parse workflow:', err) this.showErrorOnFileLoad(file) // Fall through to check parameters as fallback } - - // Only load workflow if parsing succeeded AND validation passed - if ( - workflowObj && - typeof workflowObj === 'object' && - !Array.isArray(workflowObj) - ) { - await this.loadGraphData(workflowObj, true, true, fileName, { - openSource - }) - return - } else if (workflowObj !== undefined) { - console.error('Invalid workflow structure, trying parameters fallback') - this.showErrorOnFileLoad(file) - } } // Use parameters as fallback when no workflow exists From 1525716d7b52608d8980addb9a9774e29316f75d Mon Sep 17 00:00:00 2001 From: r-vage Date: Thu, 4 Dec 2025 09:15:56 +0100 Subject: [PATCH 5/5] Fix workflow loading from PNG images with both workflow and parameters metadata - Reorder handleFile() to check workflow before parameters - Add validation to prevent JSON parse errors from crashing imports, fallback to parameters or prompt - Fix loadGraphData() to use explicit type validation instead of falsy check - Ensures ComfyUI-generated PNGs with both metadata types load the workflow, not parameters Fixes issue where large workflows (e.g., 634 nodes) were replaced with basic A1111 format when importing PNG files. --- src/scripts/app.ts | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/scripts/app.ts b/src/scripts/app.ts index b7c0914375..051b76048e 100644 --- a/src/scripts/app.ts +++ b/src/scripts/app.ts @@ -1441,31 +1441,30 @@ export class ComfyApp { // Check workflow first - it should take priority over parameters // when both are present (e.g., in ComfyUI-generated PNGs) if (workflow) { - let workflowObj: ComfyWorkflowJSON + let workflowObj: ComfyWorkflowJSON | undefined = undefined try { workflowObj = typeof workflow === 'string' ? JSON.parse(workflow) : workflow + + // Only load workflow if parsing succeeded AND validation passed + if ( + workflowObj && + typeof workflowObj === 'object' && + !Array.isArray(workflowObj) + ) { + await this.loadGraphData(workflowObj, true, true, fileName, { + openSource + }) + return + } else { + console.error('Invalid workflow structure, trying parameters fallback') + this.showErrorOnFileLoad(file) + } } catch (err) { console.error('Failed to parse workflow:', err) this.showErrorOnFileLoad(file) - return + // Fall through to check parameters as fallback } - - // Validate workflow is a proper object - if ( - !workflowObj || - typeof workflowObj !== 'object' || - Array.isArray(workflowObj) - ) { - console.error('Invalid workflow structure') - this.showErrorOnFileLoad(file) - return - } - - await this.loadGraphData(workflowObj, true, true, fileName, { - openSource - }) - return } // Use parameters as fallback when no workflow exists