Spaces:
Sleeping
Sleeping
File size: 7,939 Bytes
4450790 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 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 |
import { api } from "../../scripts/api.js";
import { getResolver } from "./shared_utils.js";
export class PromptExecution {
constructor(id) {
this.promptApi = null;
this.executedNodeIds = [];
this.totalNodes = 0;
this.currentlyExecuting = null;
this.errorDetails = null;
this.apiPrompt = getResolver();
this.id = id;
}
setPrompt(prompt) {
this.promptApi = prompt.output;
this.totalNodes = Object.keys(this.promptApi).length;
this.apiPrompt.resolve(null);
}
getApiNode(nodeId) {
var _a;
return ((_a = this.promptApi) === null || _a === void 0 ? void 0 : _a[String(nodeId)]) || null;
}
getNodeLabel(nodeId) {
var _a, _b;
const apiNode = this.getApiNode(nodeId);
let label = ((_a = apiNode === null || apiNode === void 0 ? void 0 : apiNode._meta) === null || _a === void 0 ? void 0 : _a.title) || (apiNode === null || apiNode === void 0 ? void 0 : apiNode.class_type) || undefined;
if (!label) {
const graphNode = (_b = this.maybeGetComfyGraph()) === null || _b === void 0 ? void 0 : _b.getNodeById(Number(nodeId));
label = (graphNode === null || graphNode === void 0 ? void 0 : graphNode.title) || (graphNode === null || graphNode === void 0 ? void 0 : graphNode.type) || undefined;
}
return label;
}
executing(nodeId, step, maxSteps) {
var _a;
if (nodeId == null) {
this.currentlyExecuting = null;
return;
}
if (((_a = this.currentlyExecuting) === null || _a === void 0 ? void 0 : _a.nodeId) !== nodeId) {
if (this.currentlyExecuting != null) {
this.executedNodeIds.push(nodeId);
}
this.currentlyExecuting = { nodeId, nodeLabel: this.getNodeLabel(nodeId), pass: 0 };
this.apiPrompt.promise.then(() => {
var _a;
if (this.currentlyExecuting == null) {
return;
}
const apiNode = this.getApiNode(nodeId);
if (!this.currentlyExecuting.nodeLabel) {
this.currentlyExecuting.nodeLabel = this.getNodeLabel(nodeId);
}
if ((apiNode === null || apiNode === void 0 ? void 0 : apiNode.class_type) === "UltimateSDUpscale") {
this.currentlyExecuting.pass--;
this.currentlyExecuting.maxPasses = -1;
}
else if ((apiNode === null || apiNode === void 0 ? void 0 : apiNode.class_type) === "IterativeImageUpscale") {
this.currentlyExecuting.maxPasses = (_a = apiNode === null || apiNode === void 0 ? void 0 : apiNode.inputs["steps"]) !== null && _a !== void 0 ? _a : -1;
}
});
}
if (step != null) {
if (!this.currentlyExecuting.step || step < this.currentlyExecuting.step) {
this.currentlyExecuting.pass++;
}
this.currentlyExecuting.step = step;
this.currentlyExecuting.maxSteps = maxSteps;
}
}
error(details) {
this.errorDetails = details;
}
maybeGetComfyGraph() {
var _a;
return ((_a = window === null || window === void 0 ? void 0 : window.app) === null || _a === void 0 ? void 0 : _a.graph) || null;
}
}
class PromptService extends EventTarget {
constructor(api) {
super();
this.promptsMap = new Map();
this.currentExecution = null;
this.lastQueueRemaining = 0;
const that = this;
const queuePrompt = api.queuePrompt;
api.queuePrompt = async function (num, prompt) {
let response;
try {
response = await queuePrompt.apply(api, [...arguments]);
}
catch (e) {
const promptExecution = that.getOrMakePrompt("error");
promptExecution.error({ exception_type: "Unknown." });
throw e;
}
const promptExecution = that.getOrMakePrompt(response.prompt_id);
promptExecution.setPrompt(prompt);
if (!that.currentExecution) {
that.currentExecution = promptExecution;
}
that.promptsMap.set(response.prompt_id, promptExecution);
that.dispatchEvent(new CustomEvent("queue-prompt", {
detail: {
prompt: promptExecution,
},
}));
return response;
};
api.addEventListener("status", (e) => {
var _a;
if (!((_a = e.detail) === null || _a === void 0 ? void 0 : _a.exec_info))
return;
this.lastQueueRemaining = e.detail.exec_info.queue_remaining;
this.dispatchProgressUpdate();
});
api.addEventListener("execution_start", (e) => {
if (!this.promptsMap.has(e.detail.prompt_id)) {
console.warn("'execution_start' fired before prompt was made.");
}
const prompt = this.getOrMakePrompt(e.detail.prompt_id);
this.currentExecution = prompt;
this.dispatchProgressUpdate();
});
api.addEventListener("executing", (e) => {
if (!this.currentExecution) {
this.currentExecution = this.getOrMakePrompt("unknown");
console.warn("'executing' fired before prompt was made.");
}
this.currentExecution.executing(e.detail);
this.dispatchProgressUpdate();
if (e.detail == null) {
this.currentExecution = null;
}
});
api.addEventListener("progress", (e) => {
if (!this.currentExecution) {
this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id);
console.warn("'progress' fired before prompt was made.");
}
this.currentExecution.executing(e.detail.node, e.detail.value, e.detail.max);
this.dispatchProgressUpdate();
});
api.addEventListener("execution_cached", (e) => {
if (!this.currentExecution) {
this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id);
console.warn("'execution_cached' fired before prompt was made.");
}
for (const cached of e.detail.nodes) {
this.currentExecution.executing(cached);
}
this.dispatchProgressUpdate();
});
api.addEventListener("executed", (e) => {
if (!this.currentExecution) {
this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id);
console.warn("'executed' fired before prompt was made.");
}
});
api.addEventListener("execution_error", (e) => {
var _a;
if (!this.currentExecution) {
this.currentExecution = this.getOrMakePrompt(e.detail.prompt_id);
console.warn("'execution_error' fired before prompt was made.");
}
(_a = this.currentExecution) === null || _a === void 0 ? void 0 : _a.error(e.detail);
this.dispatchProgressUpdate();
});
}
async queuePrompt(prompt) {
return await api.queuePrompt(-1, prompt);
}
dispatchProgressUpdate() {
this.dispatchEvent(new CustomEvent("progress-update", {
detail: {
queue: this.lastQueueRemaining,
prompt: this.currentExecution,
},
}));
}
getOrMakePrompt(id) {
let prompt = this.promptsMap.get(id);
if (!prompt) {
prompt = new PromptExecution(id);
this.promptsMap.set(id, prompt);
}
return prompt;
}
}
export const SERVICE = new PromptService(api);
|