api.js 7.57 KB
Newer Older
pythongosssss's avatar
pythongosssss committed
1
class ComfyApi extends EventTarget {
2
3
	#registered = new Set();

pythongosssss's avatar
pythongosssss committed
4
	constructor() {
pythongosssss's avatar
pythongosssss committed
5
		super();
pythongosssss's avatar
pythongosssss committed
6
7
	}

8
9
10
11
12
	addEventListener(type, callback, options) {
		super.addEventListener(type, callback, options);
		this.#registered.add(type);
	}

pythongosssss's avatar
pythongosssss committed
13
14
15
	/**
	 * Poll status  for colab and other things that don't support websockets.
	 */
pythongosssss's avatar
pythongosssss committed
16
17
18
	#pollQueue() {
		setInterval(async () => {
			try {
19
				const resp = await fetch("./prompt");
pythongosssss's avatar
pythongosssss committed
20
21
22
23
24
25
26
27
				const status = await resp.json();
				this.dispatchEvent(new CustomEvent("status", { detail: status }));
			} catch (error) {
				this.dispatchEvent(new CustomEvent("status", { detail: null }));
			}
		}, 1000);
	}

pythongosssss's avatar
pythongosssss committed
28
29
30
31
	/**
	 * Creates and connects a WebSocket for realtime updates
	 * @param {boolean} isReconnect If the socket is connection is a reconnect attempt
	 */
pythongosssss's avatar
pythongosssss committed
32
33
34
35
36
37
	#createSocket(isReconnect) {
		if (this.socket) {
			return;
		}

		let opened = false;
38
		let existingSession = window.name;
39
40
41
42
		if (existingSession) {
			existingSession = "?clientId=" + existingSession;
		}
		this.socket = new WebSocket(
43
			`ws${window.location.protocol === "https:" ? "s" : ""}://${location.host}${location.pathname}ws${existingSession}`
44
		);
space-nuko's avatar
space-nuko committed
45
		this.socket.binaryType = "arraybuffer";
pythongosssss's avatar
pythongosssss committed
46
47
48
49
50
51
52
53
54
55

		this.socket.addEventListener("open", () => {
			opened = true;
			if (isReconnect) {
				this.dispatchEvent(new CustomEvent("reconnected"));
			}
		});

		this.socket.addEventListener("error", () => {
			if (this.socket) this.socket.close();
pythongosssss's avatar
pythongosssss committed
56
			if (!isReconnect && !opened) {
57
58
				this.#pollQueue();
			}
pythongosssss's avatar
pythongosssss committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
		});

		this.socket.addEventListener("close", () => {
			setTimeout(() => {
				this.socket = null;
				this.#createSocket(true);
			}, 300);
			if (opened) {
				this.dispatchEvent(new CustomEvent("status", { detail: null }));
				this.dispatchEvent(new CustomEvent("reconnecting"));
			}
		});

		this.socket.addEventListener("message", (event) => {
			try {
space-nuko's avatar
space-nuko committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
				if (event.data instanceof ArrayBuffer) {
					const view = new DataView(event.data);
					const eventType = view.getUint32(0);
					const buffer = event.data.slice(4);
					switch (eventType) {
					case 1:
						const view2 = new DataView(event.data);
						const imageType = view2.getUint32(0)
						let imageMime
						switch (imageType) {
							case 1:
							default:
								imageMime = "image/jpeg";
								break;
							case 2:
								imageMime = "image/png"
pythongosssss's avatar
pythongosssss committed
90
						}
space-nuko's avatar
Fix  
space-nuko committed
91
92
						const imageBlob = new Blob([buffer.slice(4)], { type: imageMime });
						this.dispatchEvent(new CustomEvent("b_preview", { detail: imageBlob }));
93
						break;
pythongosssss's avatar
pythongosssss committed
94
					default:
space-nuko's avatar
space-nuko committed
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
						throw new Error(`Unknown binary websocket message of type ${eventType}`);
					}
				}
				else {
				    const msg = JSON.parse(event.data);
				    switch (msg.type) {
					    case "status":
						    if (msg.data.sid) {
							    this.clientId = msg.data.sid;
							    window.name = this.clientId;
						    }
						    this.dispatchEvent(new CustomEvent("status", { detail: msg.data.status }));
						    break;
					    case "progress":
						    this.dispatchEvent(new CustomEvent("progress", { detail: msg.data }));
						    break;
					    case "executing":
						    this.dispatchEvent(new CustomEvent("executing", { detail: msg.data.node }));
						    break;
					    case "executed":
						    this.dispatchEvent(new CustomEvent("executed", { detail: msg.data }));
						    break;
					    case "execution_start":
						    this.dispatchEvent(new CustomEvent("execution_start", { detail: msg.data }));
						    break;
					    case "execution_error":
						    this.dispatchEvent(new CustomEvent("execution_error", { detail: msg.data }));
						    break;
123
124
125
					    case "execution_cached":
						    this.dispatchEvent(new CustomEvent("execution_cached", { detail: msg.data }));
						    break;
space-nuko's avatar
space-nuko committed
126
127
128
129
130
131
132
					    default:
						    if (this.#registered.has(msg.type)) {
							    this.dispatchEvent(new CustomEvent(msg.type, { detail: msg.data }));
						    } else {
							    throw new Error(`Unknown message type ${msg.type}`);
						    }
				    }
pythongosssss's avatar
pythongosssss committed
133
134
				}
			} catch (error) {
space-nuko's avatar
space-nuko committed
135
				console.warn("Unhandled message:", event.data, error);
pythongosssss's avatar
pythongosssss committed
136
137
138
139
			}
		});
	}

pythongosssss's avatar
pythongosssss committed
140
141
142
	/**
	 * Initialises sockets and realtime updates
	 */
pythongosssss's avatar
pythongosssss committed
143
144
145
	init() {
		this.#createSocket();
	}
pythongosssss's avatar
pythongosssss committed
146

147
148
149
150
151
	/**
	 * Gets a list of extension urls
	 * @returns An array of script urls to import
	 */
	async getExtensions() {
152
		const resp = await fetch("./extensions", { cache: "no-store" });
153
154
155
		return await resp.json();
	}

156
157
158
159
160
	/**
	 * Gets a list of embedding names
	 * @returns An array of script urls to import
	 */
	async getEmbeddings() {
161
		const resp = await fetch("./embeddings", { cache: "no-store" });
162
163
164
		return await resp.json();
	}

pythongosssss's avatar
pythongosssss committed
165
166
167
168
	/**
	 * Loads node object definitions for the graph
	 * @returns The node definitions
	 */
pythongosssss's avatar
pythongosssss committed
169
	async getNodeDefs() {
170
		const resp = await fetch("./object_info", { cache: "no-store" });
pythongosssss's avatar
pythongosssss committed
171
172
173
		return await resp.json();
	}

pythongosssss's avatar
pythongosssss committed
174
175
176
177
178
	/**
	 *
	 * @param {number} number The index at which to queue the prompt, passing -1 will insert the prompt at the front of the queue
	 * @param {object} prompt The prompt data to queue
	 */
pythongosssss's avatar
pythongosssss committed
179
180
181
182
183
184
185
186
187
188
189
190
191
	async queuePrompt(number, { output, workflow }) {
		const body = {
			client_id: this.clientId,
			prompt: output,
			extra_data: { extra_pnginfo: { workflow } },
		};

		if (number === -1) {
			body.front = true;
		} else if (number != 0) {
			body.number = number;
		}

192
		const res = await fetch("./prompt", {
pythongosssss's avatar
pythongosssss committed
193
194
195
196
197
198
199
200
201
			method: "POST",
			headers: {
				"Content-Type": "application/json",
			},
			body: JSON.stringify(body),
		});

		if (res.status !== 200) {
			throw {
202
				response: await res.json(),
pythongosssss's avatar
pythongosssss committed
203
204
			};
		}
205
206

		return await res.json();
pythongosssss's avatar
pythongosssss committed
207
	}
pythongosssss's avatar
pythongosssss committed
208

pythongosssss's avatar
pythongosssss committed
209
210
211
212
213
	/**
	 * Loads a list of items (queue or history)
	 * @param {string} type The type of items to load, queue or history
	 * @returns The items of the specified type grouped by their status
	 */
pythongosssss's avatar
pythongosssss committed
214
215
216
217
218
219
220
	async getItems(type) {
		if (type === "queue") {
			return this.getQueue();
		}
		return this.getHistory();
	}

pythongosssss's avatar
pythongosssss committed
221
222
223
224
	/**
	 * Gets the current state of the queue
	 * @returns The currently running and queued items
	 */
pythongosssss's avatar
pythongosssss committed
225
226
	async getQueue() {
		try {
227
			const res = await fetch("./queue");
pythongosssss's avatar
pythongosssss committed
228
229
230
			const data = await res.json();
			return {
				// Running action uses a different endpoint for cancelling
pythongosssss's avatar
pythongosssss committed
231
232
233
234
				Running: data.queue_running.map((prompt) => ({
					prompt,
					remove: { name: "Cancel", cb: () => api.interrupt() },
				})),
pythongosssss's avatar
pythongosssss committed
235
236
237
238
239
240
241
242
				Pending: data.queue_pending.map((prompt) => ({ prompt })),
			};
		} catch (error) {
			console.error(error);
			return { Running: [], Pending: [] };
		}
	}

pythongosssss's avatar
pythongosssss committed
243
244
245
246
	/**
	 * Gets the prompt execution history
	 * @returns Prompt history including node outputs
	 */
pythongosssss's avatar
pythongosssss committed
247
248
	async getHistory() {
		try {
249
			const res = await fetch("./history");
pythongosssss's avatar
pythongosssss committed
250
251
252
253
254
255
256
			return { History: Object.values(await res.json()) };
		} catch (error) {
			console.error(error);
			return { History: [] };
		}
	}

pythongosssss's avatar
pythongosssss committed
257
258
259
260
261
	/**
	 * Sends a POST request to the API
	 * @param {*} type The endpoint to post to
	 * @param {*} body Optional POST data
	 */
pythongosssss's avatar
pythongosssss committed
262
263
	async #postItem(type, body) {
		try {
264
			await fetch("./" + type, {
pythongosssss's avatar
pythongosssss committed
265
266
267
268
269
270
271
272
273
274
275
				method: "POST",
				headers: {
					"Content-Type": "application/json",
				},
				body: body ? JSON.stringify(body) : undefined,
			});
		} catch (error) {
			console.error(error);
		}
	}

pythongosssss's avatar
pythongosssss committed
276
277
278
279
280
	/**
	 * Deletes an item from the specified list
	 * @param {string} type The type of item to delete, queue or history
	 * @param {number} id The id of the item to delete
	 */
pythongosssss's avatar
pythongosssss committed
281
282
283
284
	async deleteItem(type, id) {
		await this.#postItem(type, { delete: [id] });
	}

pythongosssss's avatar
pythongosssss committed
285
286
287
288
	/**
	 * Clears the specified list
	 * @param {string} type The type of list to clear, queue or history
	 */
pythongosssss's avatar
pythongosssss committed
289
290
291
292
	async clearItems(type) {
		await this.#postItem(type, { clear: true });
	}

pythongosssss's avatar
pythongosssss committed
293
294
295
	/**
	 * Interrupts the execution of the running prompt
	 */
pythongosssss's avatar
pythongosssss committed
296
297
298
	async interrupt() {
		await this.#postItem("interrupt", null);
	}
pythongosssss's avatar
pythongosssss committed
299
300
301
}

export const api = new ComfyApi();