"experiments/vscode:/vscode.git/clone" did not exist on "f5e6109acb620063e9fbfccddb7dd9229d8212bc"
api.js 5.48 KB
Newer Older
pythongosssss's avatar
pythongosssss committed
1
2
class ComfyApi extends EventTarget {
	constructor() {
pythongosssss's avatar
pythongosssss committed
3
		super();
pythongosssss's avatar
pythongosssss committed
4
5
	}

pythongosssss's avatar
pythongosssss committed
6
7
8
	/**
	 * Poll status  for colab and other things that don't support websockets.
	 */
pythongosssss's avatar
pythongosssss committed
9
10
11
12
13
14
15
16
17
18
19
20
	#pollQueue() {
		setInterval(async () => {
			try {
				const resp = await fetch("/prompt");
				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
21
22
23
24
	/**
	 * 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
	#createSocket(isReconnect) {
		if (this.socket) {
			return;
		}

		let opened = false;
		this.socket = new WebSocket(`ws${window.location.protocol === "https:" ? "s" : ""}://${location.host}/ws`);

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

		this.socket.addEventListener("error", () => {
			if (this.socket) this.socket.close();
			this.#pollQueue();
		});

		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 {
				const msg = JSON.parse(event.data);
				switch (msg.type) {
					case "status":
						if (msg.data.sid) {
							this.clientId = msg.data.sid;
						}
						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;
					default:
						throw new Error("Unknown message type");
				}
			} catch (error) {
				console.warn("Unhandled message:", event.data);
			}
		});
	}

pythongosssss's avatar
pythongosssss committed
84
85
86
	/**
	 * Initialises sockets and realtime updates
	 */
pythongosssss's avatar
pythongosssss committed
87
88
89
	init() {
		this.#createSocket();
	}
pythongosssss's avatar
pythongosssss committed
90

91
92
93
94
95
96
97
98
99
	/**
	 * Gets a list of extension urls
	 * @returns An array of script urls to import
	 */
	async getExtensions() {
		const resp = await fetch("/extensions", { cache: "no-store" });
		return await resp.json();
	}

pythongosssss's avatar
pythongosssss committed
100
101
102
103
	/**
	 * Loads node object definitions for the graph
	 * @returns The node definitions
	 */
pythongosssss's avatar
pythongosssss committed
104
105
106
107
108
	async getNodeDefs() {
		const resp = await fetch("object_info", { cache: "no-store" });
		return await resp.json();
	}

pythongosssss's avatar
pythongosssss committed
109
110
111
112
113
	/**
	 *
	 * @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
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
	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;
		}

		const res = await fetch("/prompt", {
			method: "POST",
			headers: {
				"Content-Type": "application/json",
			},
			body: JSON.stringify(body),
		});

		if (res.status !== 200) {
			throw {
				response: await res.text(),
			};
		}
	}
pythongosssss's avatar
pythongosssss committed
141

pythongosssss's avatar
pythongosssss committed
142
143
144
145
146
	/**
	 * 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
147
148
149
150
151
152
153
	async getItems(type) {
		if (type === "queue") {
			return this.getQueue();
		}
		return this.getHistory();
	}

pythongosssss's avatar
pythongosssss committed
154
155
156
157
	/**
	 * Gets the current state of the queue
	 * @returns The currently running and queued items
	 */
pythongosssss's avatar
pythongosssss committed
158
159
160
161
162
163
	async getQueue() {
		try {
			const res = await fetch("/queue");
			const data = await res.json();
			return {
				// Running action uses a different endpoint for cancelling
pythongosssss's avatar
pythongosssss committed
164
165
166
167
				Running: data.queue_running.map((prompt) => ({
					prompt,
					remove: { name: "Cancel", cb: () => api.interrupt() },
				})),
pythongosssss's avatar
pythongosssss committed
168
169
170
171
172
173
174
175
				Pending: data.queue_pending.map((prompt) => ({ prompt })),
			};
		} catch (error) {
			console.error(error);
			return { Running: [], Pending: [] };
		}
	}

pythongosssss's avatar
pythongosssss committed
176
177
178
179
	/**
	 * Gets the prompt execution history
	 * @returns Prompt history including node outputs
	 */
pythongosssss's avatar
pythongosssss committed
180
181
182
183
184
185
186
187
188
189
	async getHistory() {
		try {
			const res = await fetch("/history");
			return { History: Object.values(await res.json()) };
		} catch (error) {
			console.error(error);
			return { History: [] };
		}
	}

pythongosssss's avatar
pythongosssss committed
190
191
192
193
194
	/**
	 * Sends a POST request to the API
	 * @param {*} type The endpoint to post to
	 * @param {*} body Optional POST data
	 */
pythongosssss's avatar
pythongosssss committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
	async #postItem(type, body) {
		try {
			await fetch("/" + type, {
				method: "POST",
				headers: {
					"Content-Type": "application/json",
				},
				body: body ? JSON.stringify(body) : undefined,
			});
		} catch (error) {
			console.error(error);
		}
	}

pythongosssss's avatar
pythongosssss committed
209
210
211
212
213
	/**
	 * 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
214
215
216
217
	async deleteItem(type, id) {
		await this.#postItem(type, { delete: [id] });
	}

pythongosssss's avatar
pythongosssss committed
218
219
220
221
	/**
	 * Clears the specified list
	 * @param {string} type The type of list to clear, queue or history
	 */
pythongosssss's avatar
pythongosssss committed
222
223
224
225
	async clearItems(type) {
		await this.#postItem(type, { clear: true });
	}

pythongosssss's avatar
pythongosssss committed
226
227
228
	/**
	 * Interrupts the execution of the running prompt
	 */
pythongosssss's avatar
pythongosssss committed
229
230
231
	async interrupt() {
		await this.#postItem("interrupt", null);
	}
pythongosssss's avatar
pythongosssss committed
232
233
234
}

export const api = new ComfyApi();