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55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// @ts-check

import { api } from "./api.js";
import { ChangeTracker } from "./changeTracker.js";
import { ComfyAsyncDialog } from "./ui/components/asyncDialog.js";
import { getStorageValue, setStorageValue } from "./utils.js";

function appendJsonExt(path) {
	if (!path.toLowerCase().endsWith(".json")) {
		path += ".json";
	}
	return path;
}

export function trimJsonExt(path) {
	return path?.replace(/\.json$/, "");
}

export class ComfyWorkflowManager extends EventTarget {
	/** @type {string | null} */
	#activePromptId = null;
	#unsavedCount = 0;
	#activeWorkflow;

	/** @type {Record<string, ComfyWorkflow>} */
	workflowLookup = {};
	/** @type {Array<ComfyWorkflow>} */
	workflows = [];
	/** @type {Array<ComfyWorkflow>} */
	openWorkflows = [];
	/** @type {Record<string, {workflow?: ComfyWorkflow, nodes?: Record<string, boolean>}>} */
	queuedPrompts = {};

	get activeWorkflow() {
		return this.#activeWorkflow ?? this.openWorkflows[0];
	}

	get activePromptId() {
		return this.#activePromptId;
	}

	get activePrompt() {
		return this.queuedPrompts[this.#activePromptId];
	}

	/**

	 * @param {import("./app.js").ComfyApp} app

	 */
	constructor(app) {
		super();
		this.app = app;
		ChangeTracker.init(app);

		this.#bindExecutionEvents();
	}

	#bindExecutionEvents() {
		// TODO: on reload, set active prompt based on the latest ws message

		const emit = () => this.dispatchEvent(new CustomEvent("execute", { detail: this.activePrompt }));
		let executing = null;
		api.addEventListener("execution_start", (e) => {
			this.#activePromptId = e.detail.prompt_id;

			// This event can fire before the event is stored, so put a placeholder
			this.queuedPrompts[this.#activePromptId] ??= { nodes: {} };
			emit();
		});
		api.addEventListener("execution_cached", (e) => {
			if (!this.activePrompt) return;
			for (const n of e.detail.nodes) {
				this.activePrompt.nodes[n] = true;
			}
			emit();
		});
		api.addEventListener("executed", (e) => {
			if (!this.activePrompt) return;
			this.activePrompt.nodes[e.detail.node] = true;
			emit();
		});
		api.addEventListener("executing", (e) => {
			if (!this.activePrompt) return;

			if (executing) {
				// Seems sometimes nodes that are cached fire executing but not executed
				this.activePrompt.nodes[executing] = true;
			}
			executing = e.detail;
			if (!executing) {
				delete this.queuedPrompts[this.#activePromptId];
				this.#activePromptId = null;
			}
			emit();
		});
	}

	async loadWorkflows() {
		try {
			let favorites;
			const resp = await api.getUserData("workflows/.index.json");
			let info;
			if (resp.status === 200) {
				info = await resp.json();
				favorites = new Set(info?.favorites ?? []);
			} else {
				favorites = new Set();
			}

			const workflows = (await api.listUserData("workflows", true, true)).map((w) => {
				let workflow = this.workflowLookup[w[0]];
				if (!workflow) {
					workflow = new ComfyWorkflow(this, w[0], w.slice(1), favorites.has(w[0]));
					this.workflowLookup[workflow.path] = workflow;
				}
				return workflow;
			});

			this.workflows = workflows;
		} catch (error) {
			alert("Error loading workflows: " + (error.message ?? error));
			this.workflows = [];
		}
	}

	async saveWorkflowMetadata() {
		await api.storeUserData("workflows/.index.json", {
			favorites: [...this.workflows.filter((w) => w.isFavorite).map((w) => w.path)],
		});
	}

	/**

	 * @param {string | ComfyWorkflow | null} workflow

	 */
	setWorkflow(workflow) {
		if (workflow && typeof workflow === "string") {
			// Selected by path, i.e. on reload of last workflow
			const found = this.workflows.find((w) => w.path === workflow);
			if (found) {
				workflow = found;
				workflow.unsaved = !workflow || getStorageValue("Comfy.PreviousWorkflowUnsaved") === "true";
			}
		}

		if (!(workflow instanceof ComfyWorkflow)) {
			// Still not found, either reloading a deleted workflow or blank
			workflow = new ComfyWorkflow(this, workflow || "Unsaved Workflow" + (this.#unsavedCount++ ? ` (${this.#unsavedCount})` : ""));
		}

		const index = this.openWorkflows.indexOf(workflow);
		if (index === -1) {
			// Opening a new workflow
			this.openWorkflows.push(workflow);
		}

		this.#activeWorkflow = workflow;

		setStorageValue("Comfy.PreviousWorkflow", this.activeWorkflow.path ?? "");
		this.dispatchEvent(new CustomEvent("changeWorkflow"));
	}

	storePrompt({ nodes, id }) {
		this.queuedPrompts[id] ??= {};
		this.queuedPrompts[id].nodes = {
			...nodes.reduce((p, n) => {
				p[n] = false;
				return p;
			}, {}),
			...this.queuedPrompts[id].nodes,
		};
		this.queuedPrompts[id].workflow = this.activeWorkflow;
	}

	/**

	 * @param {ComfyWorkflow} workflow

	 */
	async closeWorkflow(workflow, warnIfUnsaved = true) {
		if (!workflow.isOpen) {
			return true;
		}
		if (workflow.unsaved && warnIfUnsaved) {
			const res = await ComfyAsyncDialog.prompt({
				title: "Save Changes?",
				message: `Do you want to save changes to "${workflow.path ?? workflow.name}" before closing?`,
				actions: ["Yes", "No", "Cancel"],
			});
			if (res === "Yes") {
				const active = this.activeWorkflow;
				if (active !== workflow) {
					// We need to switch to the workflow to save it
					await workflow.load();
				}

				if (!(await workflow.save())) {
					// Save was canceled, restore the previous workflow
					if (active !== workflow) {
						await active.load();
					}
					return;
				}
			} else if (res === "Cancel") {
				return;
			}
		}
		workflow.changeTracker = null;
		this.openWorkflows.splice(this.openWorkflows.indexOf(workflow), 1);
		if (this.openWorkflows.length) {
			this.#activeWorkflow = this.openWorkflows[0];
			await this.#activeWorkflow.load();
		} else {
			// Load default
			await this.app.loadGraphData();
		}
	}
}

export class ComfyWorkflow {
	#name;
	#path;
	#pathParts;
	#isFavorite = false;
	/** @type {ChangeTracker | null} */
	changeTracker = null;
	unsaved = false;

	get name() {
		return this.#name;
	}

	get path() {
		return this.#path;
	}

	get pathParts() {
		return this.#pathParts;
	}

	get isFavorite() {
		return this.#isFavorite;
	}

	get isOpen() {
		return !!this.changeTracker;
	}

	/**

	 * @overload

	 * @param {ComfyWorkflowManager} manager

	 * @param {string} path

	 */
	/**

	 * @overload

	 * @param {ComfyWorkflowManager} manager

	 * @param {string} path

	 * @param {string[]} pathParts

	 * @param {boolean} isFavorite

	 */
	/**

	 * @param {ComfyWorkflowManager} manager

	 * @param {string} path

	 * @param {string[]} [pathParts]

	 * @param {boolean} [isFavorite]

	 */
	constructor(manager, path, pathParts, isFavorite) {
		this.manager = manager;
		if (pathParts) {
			this.#updatePath(path, pathParts);
			this.#isFavorite = isFavorite;
		} else {
			this.#name = path;
			this.unsaved = true;
		}
	}

	/**

	 * @param {string} path

	 * @param {string[]} [pathParts]

	 */
	#updatePath(path, pathParts) {
		this.#path = path;

		if (!pathParts) {
			if (!path.includes("\\")) {
				pathParts = path.split("/");
			} else {
				pathParts = path.split("\\");
			}
		}

		this.#pathParts = pathParts;
		this.#name = trimJsonExt(pathParts[pathParts.length - 1]);
	}

	async getWorkflowData() {
		const resp = await api.getUserData("workflows/" + this.path);
		if (resp.status !== 200) {
			alert(`Error loading workflow file '${this.path}': ${resp.status} ${resp.statusText}`);
			return;
		}
		return await resp.json();
	}

	load = async () => {
		if (this.isOpen) {
			await this.manager.app.loadGraphData(this.changeTracker.activeState, true, true, this);
		} else {
			const data = await this.getWorkflowData();
			if (!data) return;
			await this.manager.app.loadGraphData(data, true, true, this);
		}
	};

	async save(saveAs = false) {
		if (!this.path || saveAs) {
			return !!(await this.#save(null, false));
		} else {
			return !!(await this.#save(this.path, true));
		}
	}

	/**

	 * @param {boolean} value

	 */
	async favorite(value) {
		try {
			if (this.#isFavorite === value) return;
			this.#isFavorite = value;
			await this.manager.saveWorkflowMetadata();
			this.manager.dispatchEvent(new CustomEvent("favorite", { detail: this }));
		} catch (error) {
			alert("Error favoriting workflow " + this.path + "\n" + (error.message ?? error));
		}
	}

	/**

	 * @param {string} path

	 */
	async rename(path) {
		path = appendJsonExt(path);
		let resp = await api.moveUserData("workflows/" + this.path, "workflows/" + path);

		if (resp.status === 409) {
			if (!confirm(`Workflow '${path}' already exists, do you want to overwrite it?`)) return resp;
			resp = await api.moveUserData("workflows/" + this.path, "workflows/" + path, { overwrite: true });
		}

		if (resp.status !== 200) {
			alert(`Error renaming workflow file '${this.path}': ${resp.status} ${resp.statusText}`);
			return;
		}

		const isFav = this.isFavorite;
		if (isFav) {
			await this.favorite(false);
		}
		path = (await resp.json()).substring("workflows/".length);
		this.#updatePath(path, null);
		if (isFav) {
			await this.favorite(true);
		}
		this.manager.dispatchEvent(new CustomEvent("rename", { detail: this }));
		setStorageValue("Comfy.PreviousWorkflow", this.path ?? "");
	}

	async insert() {
		const data = await this.getWorkflowData();
		if (!data) return;

		const old = localStorage.getItem("litegrapheditor_clipboard");
		const graph = new LGraph(data);
		const canvas = new LGraphCanvas(null, graph, { skip_events: true, skip_render: true });
		canvas.selectNodes();
		canvas.copyToClipboard();
		this.manager.app.canvas.pasteFromClipboard();
		localStorage.setItem("litegrapheditor_clipboard", old);
	}

	async delete() {
		// TODO: fix delete of current workflow - should mark workflow as unsaved and when saving use old name by default

		try {
			if (this.isFavorite) {
				await this.favorite(false);
			}
			await api.deleteUserData("workflows/" + this.path);
			this.unsaved = true;
			this.#path = null;
			this.#pathParts = null;
			this.manager.workflows.splice(this.manager.workflows.indexOf(this), 1);
			this.manager.dispatchEvent(new CustomEvent("delete", { detail: this }));
		} catch (error) {
			alert(`Error deleting workflow: ${error.message || error}`);
		}
	}

	track() {
		if (this.changeTracker) {
			this.changeTracker.restore();
		} else {
			this.changeTracker = new ChangeTracker(this);
		}
	}

	/**

	 * @param {string|null} path

	 * @param {boolean} overwrite

	 */
	async #save(path, overwrite) {
		if (!path) {
			path = prompt("Save workflow as:", trimJsonExt(this.path) ?? this.name ?? "workflow");
			if (!path) return;
		}

		path = appendJsonExt(path);

		const p = await this.manager.app.graphToPrompt();
		const json = JSON.stringify(p.workflow, null, 2);
		let resp = await api.storeUserData("workflows/" + path, json, { stringify: false, throwOnError: false, overwrite });
		if (resp.status === 409) {
			if (!confirm(`Workflow '${path}' already exists, do you want to overwrite it?`)) return;
			resp = await api.storeUserData("workflows/" + path, json, { stringify: false });
		}

		if (resp.status !== 200) {
			alert(`Error saving workflow '${this.path}': ${resp.status} ${resp.statusText}`);
			return;
		}

		path = (await resp.json()).substring("workflows/".length);

		if (!this.path) {
			// Saved new workflow, patch this instance
			this.#updatePath(path, null);
			await this.manager.loadWorkflows();
			this.unsaved = false;
			this.manager.dispatchEvent(new CustomEvent("rename", { detail: this }));
			setStorageValue("Comfy.PreviousWorkflow", this.path ?? "");
		} else if (path !== this.path) {
			// Saved as, open the new copy
			await this.manager.loadWorkflows();
			const workflow = this.manager.workflowLookup[path];
			await workflow.load();
		} else {
			// Normal save
			this.unsaved = false;
			this.manager.dispatchEvent(new CustomEvent("save", { detail: this }));
		}

		return true;
	}
}