Skip to content

Commit 8da535b

Browse files
committed
Add support for new Comfy Workflow Parser and Executor
1 parent 2b9f2ac commit 8da535b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+62223
-331
lines changed

AiServer.ServiceInterface/AppExtensions.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Data;
22
using System.Security.Cryptography;
3+
using System.Text.Json;
34
using AiServer.ServiceModel;
45
using AiServer.ServiceModel.Types;
56
using ServiceStack;
@@ -92,5 +93,47 @@ public static string ComputeSha256(this Stream stream)
9293
using var sha256Hash = SHA256.Create();
9394
byte[] hashBytes = sha256Hash.ComputeHash(stream);
9495
return BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
95-
}
96+
}
97+
98+
public static object? AsObject(this JsonElement element)
99+
{
100+
switch (element.ValueKind)
101+
{
102+
case JsonValueKind.String:
103+
return element.GetString();
104+
case JsonValueKind.Number:
105+
// For numbers, try to parse as different numeric types
106+
if (element.TryGetInt32(out int intValue))
107+
return intValue;
108+
if (element.TryGetInt64(out long longValue))
109+
return longValue;
110+
if (element.TryGetDouble(out double doubleValue))
111+
return doubleValue;
112+
return element.GetRawText(); // Fallback
113+
case JsonValueKind.True:
114+
return true;
115+
case JsonValueKind.False:
116+
return false;
117+
case JsonValueKind.Null:
118+
return null;
119+
case JsonValueKind.Object:
120+
// For objects, create a Dictionary
121+
var obj = new Dictionary<string, object?>();
122+
foreach (var property in element.EnumerateObject())
123+
{
124+
obj[property.Name] = AsObject(property.Value);
125+
}
126+
return obj;
127+
case JsonValueKind.Array:
128+
// For arrays, create a List
129+
var array = new List<object?>();
130+
foreach (var item in element.EnumerateArray())
131+
{
132+
array.Add(AsObject(item));
133+
}
134+
return array;
135+
default:
136+
return element.GetRawText();
137+
}
138+
}
96139
}

AiServer.ServiceInterface/Comfy/ComfyClient.Parsing.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace AiServer.ServiceInterface.Comfy;
55

66
public partial class ComfyClient
77
{
8-
98
public async Task<string> ConvertWorkflowToApiAsync(string rawWorkflow, CancellationToken token)
109
{
1110
JsonObject workflow;
@@ -93,8 +92,9 @@ public async Task<string> ConvertWorkflowToApiAsync(string rawWorkflow, Cancella
9392
var classType = node["type"].ToString();
9493
await AddToMappingAsync(classType, token);
9594

96-
var apiNode = new JsonObject();
97-
apiNode["inputs"] = new JsonObject();
95+
var apiNode = new JsonObject {
96+
["inputs"] = new JsonObject()
97+
};
9898

9999
if (metadataMapping.TryGetValue(classType, out var currentClass))
100100
{
@@ -131,14 +131,22 @@ public async Task<string> ConvertWorkflowToApiAsync(string rawWorkflow, Cancella
131131
widgetIndex++;
132132
}
133133
if (propName == "seed")
134+
{
134135
widgetIndex++;
136+
}
135137
}
136138
}
137139
}
138140
apiNode["class_type"] = classType;
139141
apiNodes[nodeId] = apiNode;
140142
}
141-
return new JsonObject { ["prompt"] = apiNodes, ["client_id"] = clientId }.ToJsonString();
143+
144+
var ret = new JsonObject
145+
{
146+
["prompt"] = apiNodes,
147+
["client_id"] = clientId,
148+
}.ToJsonString();
149+
return ret;
142150
}
143151

144152
private async Task AddToMappingAsync(string classType, CancellationToken token)
@@ -262,5 +270,4 @@ private ComfyWorkflowStatus ParseWorkflowStatus(JsonObject statusJson, string jo
262270

263271
return result;
264272
}
265-
266-
}
273+
}

AiServer.ServiceInterface/Comfy/ComfyClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ public interface IComfyClient : IDisposable
3535
public partial class ComfyClient(HttpClient httpClient) : IComfyClient
3636
{
3737
private readonly Dictionary<string, JsonObject> metadataMapping = new();
38-
private readonly List<string> excludedNodeTypes = ["PrimitiveNode"];
38+
private readonly List<string> excludedNodeTypes = [
39+
"PrimitiveNode",
40+
"Note",
41+
];
3942
private static ScriptContext context = new ScriptContext().Init();
4043

4144
public string WorkflowTemplatePath { get; set; } = "workflows";
@@ -398,7 +401,7 @@ private async Task AddOnGenerationCompleteAsync(string innerPromptId, Action<str
398401
{
399402
if (!string.IsNullOrEmpty(comfyPromptId) && missedGenerationCompleteMapping.ContainsKey(comfyPromptId))
400403
{
401-
Console.WriteLine("Missed AddOnGenerationComplete");
404+
Logger.LogInformation("Missed AddOnGenerationComplete");
402405
missedGenerationCompleteMapping[comfyPromptId] = innerPromptId;
403406
missedPromptId = innerPromptId;
404407
fireMissed = true;

0 commit comments

Comments
 (0)