Skip to content

Commit 43d5fc2

Browse files
committed
example workflows for comfy Simple tab
1 parent 81d4dbe commit 43d5fc2

File tree

3 files changed

+1073
-12
lines changed

3 files changed

+1073
-12
lines changed

src/BuiltinExtensions/ComfyUIBackend/ComfyUIBackendExtension.cs

+20-8
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,33 @@ static string cleanup(string _, string val)
168168

169169
public static IEnumerable<ComfyUIAPIAbstractBackend> RunningComfyBackends => Program.Backends.RunningBackendsOfType<ComfyUIAPIAbstractBackend>();
170170

171+
public static string[] ExampleWorkflowNames;
172+
171173
public void LoadWorkflowFiles()
172174
{
173175
CustomWorkflows.Clear();
174-
if (Directory.Exists($"{FilePath}/CustomWorkflows"))
176+
Directory.CreateDirectory($"{FilePath}/CustomWorkflows");
177+
Directory.CreateDirectory($"{FilePath}/CustomWorkflows/Examples");
178+
string[] getCustomFlows(string path) => [.. Directory.EnumerateFiles($"{FilePath}/{path}", "*.*", new EnumerationOptions() { RecurseSubdirectories = true }).Select(f => f.Replace('\\', '/').After($"/{path}/")).Order()];
179+
ExampleWorkflowNames = getCustomFlows("ExampleWorkflows");
180+
string[] customFlows = getCustomFlows("CustomWorkflows");
181+
bool anyCopied = false;
182+
foreach (string workflow in ExampleWorkflowNames.Where(f => f.EndsWith(".json")))
175183
{
176-
foreach (string workflow in Directory.EnumerateFiles($"{FilePath}/CustomWorkflows", "*.json", new EnumerationOptions() { RecurseSubdirectories = true }).Order())
184+
if (!customFlows.Contains($"Examples/{workflow}") && !customFlows.Contains($"Examples/{workflow}.deleted"))
177185
{
178-
string fileName = workflow.Replace('\\', '/').After("/CustomWorkflows/");
179-
if (fileName.EndsWith(".json"))
180-
{
181-
string name = fileName.BeforeLast('.');
182-
CustomWorkflows.TryAdd(name, null);
183-
}
186+
File.Copy($"{FilePath}/ExampleWorkflows/{workflow}", $"{FilePath}/CustomWorkflows/Examples/{workflow}");
187+
anyCopied = true;
184188
}
185189
}
190+
if (anyCopied)
191+
{
192+
customFlows = getCustomFlows("CustomWorkflows");
193+
}
194+
foreach (string workflow in customFlows.Where(f => f.EndsWith(".json")))
195+
{
196+
CustomWorkflows.TryAdd(workflow.BeforeLast('.'), null);
197+
}
186198
}
187199

188200
public static ComfyCustomWorkflow GetWorkflowByName(string name)

src/BuiltinExtensions/ComfyUIBackend/ComfyUIWebAPI.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,21 @@ public static async Task<JObject> ComfyListWorkflows()
107107
public static async Task<JObject> ComfyDeleteWorkflow(string name)
108108
{
109109
string path = Utilities.StrictFilenameClean(name);
110-
ComfyUIBackendExtension.CustomWorkflows.Remove(path, out _);
111-
path = $"{ComfyUIBackendExtension.Folder}/CustomWorkflows/{path}.json";
112-
if (!File.Exists(path))
110+
if (!ComfyUIBackendExtension.CustomWorkflows.Remove(path, out _))
113111
{
114112
return new JObject() { ["error"] = "Unknown custom workflow name." };
115113
}
116-
File.Delete(path);
114+
string fullPath = $"{ComfyUIBackendExtension.Folder}/CustomWorkflows/{path}.json";
115+
if (!File.Exists(fullPath))
116+
{
117+
return new JObject() { ["error"] = "Unknown custom workflow name." };
118+
}
119+
File.Delete(fullPath);
120+
Logs.Debug($"check {path} against {ComfyUIBackendExtension.ExampleWorkflowNames.JoinString(", ")}");
121+
if (ComfyUIBackendExtension.ExampleWorkflowNames.Contains(path.After("Examples/") + ".json"))
122+
{
123+
File.WriteAllText($"{fullPath}.deleted", "deleted-by-user");
124+
}
117125
return new JObject() { ["success"] = true };
118126
}
119127

0 commit comments

Comments
 (0)