@@ -39,6 +39,10 @@ async function confirmOverwrite(name: string): Promise<boolean | null> {
3939 } )
4040}
4141
42+ type ValidSubgraphWorkflowJSON = ComfyWorkflowJSON & {
43+ definitions : NonNullable < ComfyWorkflowJSON [ 'definitions' ] >
44+ }
45+
4246export const useSubgraphStore = defineStore ( 'subgraph' , ( ) => {
4347 class SubgraphBlueprint extends ComfyWorkflow {
4448 static override readonly basePath = 'subgraphs/'
@@ -54,18 +58,20 @@ export const useSubgraphStore = defineStore('subgraph', () => {
5458 this . hasPromptedSave = ! confirmFirstSave
5559 }
5660
57- validateSubgraph ( ) {
58- if ( ! this . activeState ?. definitions )
61+ validateSubgraph ( ) : ValidSubgraphWorkflowJSON {
62+ const activeState = this . activeState
63+ if ( ! activeState ?. definitions )
5964 throw new Error (
6065 'The root graph of a subgraph blueprint must consist of only a single subgraph node'
6166 )
62- const { subgraphs } = this . activeState . definitions
63- const { nodes } = this . activeState
67+ const validState = activeState as ValidSubgraphWorkflowJSON
68+ const { subgraphs } = validState . definitions
69+ const { nodes } = validState
6470 //Instanceof doesn't function as nodes are serialized
6571 function isSubgraphNode ( node : ComfyNode ) {
6672 return node && subgraphs . some ( ( s ) => s . id === node . type )
6773 }
68- if ( nodes . length == 1 && isSubgraphNode ( nodes [ 0 ] ) ) return
74+ if ( nodes . length == 1 && isSubgraphNode ( nodes [ 0 ] ) ) return validState
6975 const errors : Record < SerializedNodeId , NodeError > = { }
7076 //mark errors for all but first subgraph node
7177 let firstSubgraphFound = false
@@ -88,7 +94,7 @@ export const useSubgraphStore = defineStore('subgraph', () => {
8894 }
8995
9096 override async save ( ) : Promise < UserFile > {
91- this . validateSubgraph ( )
97+ const activeState = this . validateSubgraph ( )
9298 if (
9399 ! this . hasPromptedSave &&
94100 useSettingStore ( ) . get ( 'Comfy.Workflow.WarnBlueprintOverwrite' )
@@ -97,7 +103,7 @@ export const useSubgraphStore = defineStore('subgraph', () => {
97103 this . hasPromptedSave = true
98104 }
99105 // Extract metadata from subgraph.extra to workflow.extra before saving
100- this . extractMetadataToWorkflowExtra ( )
106+ this . extractMetadataToWorkflowExtra ( activeState )
101107 const ret = await super . save ( )
102108 // Force reload to update initialState with saved metadata
103109 registerNodeDef ( await this . load ( { force : true } ) , {
@@ -110,13 +116,14 @@ export const useSubgraphStore = defineStore('subgraph', () => {
110116 * Moves all properties (except workflowRendererVersion) from subgraph.extra
111117 * to workflow.extra, then removes from subgraph.extra to avoid duplication.
112118 */
113- private extractMetadataToWorkflowExtra ( ) : void {
114- if ( ! this . activeState ) return
115- const subgraph = this . activeState . definitions ?. subgraphs ?. [ 0 ]
119+ private extractMetadataToWorkflowExtra (
120+ activeState : ValidSubgraphWorkflowJSON
121+ ) : void {
122+ const subgraph = activeState . definitions . subgraphs ?. [ 0 ]
116123 if ( ! subgraph ?. extra ) return
117124
118125 const sgExtra = subgraph . extra as Record < string , unknown >
119- const workflowExtra = ( this . activeState . extra ??= { } ) as Record <
126+ const workflowExtra = ( activeState . extra ??= { } ) as Record <
120127 string ,
121128 unknown
122129 >
@@ -129,10 +136,10 @@ export const useSubgraphStore = defineStore('subgraph', () => {
129136 }
130137
131138 override async saveAs ( path : string ) {
132- this . validateSubgraph ( )
139+ const activeState = this . validateSubgraph ( )
133140 this . hasPromptedSave = true
134141 // Extract metadata from subgraph.extra to workflow.extra before saving
135- this . extractMetadataToWorkflowExtra ( )
142+ this . extractMetadataToWorkflowExtra ( activeState )
136143 const ret = await super . saveAs ( path )
137144 // Force reload to update initialState with saved metadata
138145 registerNodeDef ( await this . load ( { force : true } ) , {
@@ -276,8 +283,8 @@ export const useSubgraphStore = defineStore('subgraph', () => {
276283 overrides : Partial < ComfyNodeDefV1 > = { } ,
277284 name : string = workflow . filename
278285 ) {
279- const subgraphNode = workflow . changeTracker . initialState . nodes [ 0 ]
280- if ( ! subgraphNode ) throw new Error ( 'Invalid Subgraph Blueprint' )
286+ const subgraphNode = workflow . changeTracker . initialState
287+ . nodes [ 0 ] as ComfyNode
281288 subgraphNode . inputs ??= [ ]
282289 subgraphNode . outputs ??= [ ]
283290 //NOTE: Types are cast to string. This is only used for input coloring on previews
0 commit comments