1- using System ;
1+ using Microsoft . Extensions . Logging ;
2+ using System ;
23using System . Collections . Generic ;
34using System . Linq ;
45using System . Text . Json ;
78
89namespace SDMeta . Comfy
910{
10- public class ComfyUIParameterDecoder : IParameterDecoder
11+ public class ComfyUIParameterDecoder ( ILogger < ComfyUIParameterDecoder > logger ) : IParameterDecoder
1112 {
1213 private static readonly JsonSerializerOptions options = new ( )
1314 {
@@ -17,10 +18,20 @@ public class ComfyUIParameterDecoder : IParameterDecoder
1718
1819 public GenerationParams GetParameters ( ImageFile imageFile )
1920 {
20- var _parameters = imageFile . Prompt ;
21+ if ( imageFile . Prompt == null )
22+ {
23+ return GenerationParams . Empty ;
24+ }
25+
2126 try
2227 {
23- var nodes = JsonSerializer . Deserialize < Dictionary < string , UntypedBaseNode > > ( _parameters , options ) ;
28+ var nodes = JsonSerializer . Deserialize < Dictionary < string , UntypedBaseNode > > ( imageFile . Prompt , options ) ;
29+
30+ if ( nodes == null )
31+ {
32+ logger . LogWarning ( "No nodes found in prompt for file {filename}" , imageFile . FileName ) ;
33+ return GenerationParams . Empty ;
34+ }
2435
2536 var typedNodes = nodes . Select ( p => p . Value . GetInputs ( p . Key ) ) . ToList ( ) ;
2637
@@ -45,6 +56,7 @@ public GenerationParams GetParameters(ImageFile imageFile)
4556 catch ( Exception ex )
4657 {
4758 const string errorMessage = "Unable to decode Comfy prompt" ;
59+ logger . LogError ( ex , errorMessage + " for file {filename}" , imageFile . FileName ) ;
4860 return new GenerationParams ( )
4961 {
5062 Model = errorMessage ,
@@ -114,37 +126,39 @@ public sealed class CLIPTextEncodeSDXLRefinerNode : UntypedBaseNode<CLIPTextEnco
114126
115127 public class BaseInputs
116128 {
117- public string NodeId { get ; set ; }
129+ public string ? NodeId { get ; set ; }
118130 }
119131
120132 public class CheckpointLoaderSimpleInputs : BaseInputs
121133 {
122- public string ckpt_name { get ; set ; }
134+ public string ? ckpt_name { get ; set ; }
123135
124136 public string ? GetCheckpointName ( ) => ckpt_name ? . Replace ( ".safetensors" , "" ) ;
125137
126- public bool IsRefiner ( ) => ckpt_name . ToLower ( ) . Contains ( "refiner" ) ;
138+ public bool IsRefiner ( ) =>
139+ ckpt_name != null &&
140+ ckpt_name . Contains ( "refiner" , StringComparison . OrdinalIgnoreCase ) ;
127141 }
128142
129143 public abstract class BaseCLIPTestEncodeInputs : BaseInputs
130144 {
131- public JsonArray clip { get ; set ; }
132- public abstract string GetText ( ) ;
145+ public JsonArray ? clip { get ; set ; }
146+ public abstract string ? GetText ( ) ;
133147 }
134148
135149 public class CLIPTextEncodeInputs : BaseCLIPTestEncodeInputs
136150 {
137- public string text { get ; set ; }
151+ public string ? text { get ; set ; }
138152
139- public override string GetText ( ) => text ;
153+ public override string ? GetText ( ) => text ;
140154 }
141155
142156 public class KSamplerBase : BaseInputs
143157 {
144- public JsonArray model { get ; set ; }
145- public JsonArray positive { get ; set ; }
146- public JsonArray negative { get ; set ; }
147- public JsonArray latent_image { get ; set ; }
158+ public JsonArray ? model { get ; set ; }
159+ public JsonArray ? positive { get ; set ; }
160+ public JsonArray ? negative { get ; set ; }
161+ public JsonArray ? latent_image { get ; set ; }
148162
149163 public ( string ? positive , string ? negative ) GetClips ( IEnumerable < BaseCLIPTestEncodeInputs > clips )
150164 {
@@ -163,22 +177,22 @@ public class KSamplerInputs : KSamplerBase
163177 public long seed { get ; set ; }
164178 public int steps { get ; set ; }
165179 public float cfg { get ; set ; }
166- public string sampler_name { get ; set ; }
167- public string scheduler { get ; set ; }
180+ public string ? sampler_name { get ; set ; }
181+ public string ? scheduler { get ; set ; }
168182 public float denoise { get ; set ; }
169183 }
170184
171185 public class KSamplerAdvancedInputs : KSamplerBase
172186 {
173- public string add_noise { get ; set ; }
187+ public string ? add_noise { get ; set ; }
174188 public long noise_seed { get ; set ; }
175189 public int steps { get ; set ; }
176190 public float cfg { get ; set ; }
177- public string sampler_name { get ; set ; }
178- public string scheduler { get ; set ; }
191+ public string ? sampler_name { get ; set ; }
192+ public string ? scheduler { get ; set ; }
179193 public int start_at_step { get ; set ; }
180194 public int end_at_step { get ; set ; }
181- public string return_with_leftover_noise { get ; set ; }
195+ public string ? return_with_leftover_noise { get ; set ; }
182196 }
183197
184198 public class CLIPTextEncodeSDXL : BaseCLIPTestEncodeInputs
@@ -189,17 +203,17 @@ public class CLIPTextEncodeSDXL : BaseCLIPTestEncodeInputs
189203 public int crop_h { get ; set ; }
190204 public int target_width { get ; set ; }
191205 public int target_height { get ; set ; }
192- public string text_g { get ; set ; }
193- public string text_l { get ; set ; }
194- public override string GetText ( ) => ( text_g + " " + text_l ) . Trim ( ) ;
206+ public string ? text_g { get ; set ; }
207+ public string ? text_l { get ; set ; }
208+ public override string ? GetText ( ) => ( text_g + " " + text_l ) . Trim ( ) ;
195209 }
196210
197211 public class CLIPTextEncodeSDXLRefiner : BaseCLIPTestEncodeInputs
198212 {
199213 public float ascore { get ; set ; }
200214 public int width { get ; set ; }
201215 public int height { get ; set ; }
202- public string text { get ; set ; }
203- public override string GetText ( ) => text ;
216+ public string ? text { get ; set ; }
217+ public override string ? GetText ( ) => text ;
204218 }
205219}
0 commit comments