@runware/sdk-js 1.1.19 → 1.1.20-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1510 -0
- package/dist/index.d.cts +371 -0
- package/dist/index.d.mts +23 -2
- package/dist/index.d.ts +56 -2
- package/dist/index.js +86 -37
- package/dist/index.mjs +51 -2
- package/package.json +5 -2
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1510 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
7
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
8
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
9
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
10
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
12
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
13
|
+
var __spreadValues = (a, b) => {
|
|
14
|
+
for (var prop in b || (b = {}))
|
|
15
|
+
if (__hasOwnProp.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
if (__getOwnPropSymbols)
|
|
18
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
19
|
+
if (__propIsEnum.call(b, prop))
|
|
20
|
+
__defNormalProp(a, prop, b[prop]);
|
|
21
|
+
}
|
|
22
|
+
return a;
|
|
23
|
+
};
|
|
24
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
25
|
+
var __objRest = (source, exclude) => {
|
|
26
|
+
var target = {};
|
|
27
|
+
for (var prop in source)
|
|
28
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
29
|
+
target[prop] = source[prop];
|
|
30
|
+
if (source != null && __getOwnPropSymbols)
|
|
31
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
32
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
33
|
+
target[prop] = source[prop];
|
|
34
|
+
}
|
|
35
|
+
return target;
|
|
36
|
+
};
|
|
37
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
38
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
39
|
+
};
|
|
40
|
+
var __export = (target, all) => {
|
|
41
|
+
for (var name in all)
|
|
42
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
43
|
+
};
|
|
44
|
+
var __copyProps = (to, from, except, desc) => {
|
|
45
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
46
|
+
for (let key of __getOwnPropNames(from))
|
|
47
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
48
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
49
|
+
}
|
|
50
|
+
return to;
|
|
51
|
+
};
|
|
52
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
53
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
54
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
55
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
56
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
57
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
58
|
+
mod
|
|
59
|
+
));
|
|
60
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
61
|
+
|
|
62
|
+
// Runware/reconnect.ts
|
|
63
|
+
var require_reconnect = __commonJS({
|
|
64
|
+
"Runware/reconnect.ts"(exports, module) {
|
|
65
|
+
"use strict";
|
|
66
|
+
var isWebSocket = (constructor) => constructor && constructor.CLOSING === 2;
|
|
67
|
+
var isGlobalWebSocket = () => typeof WebSocket !== "undefined" && isWebSocket(WebSocket);
|
|
68
|
+
var getDefaultOptions = () => ({
|
|
69
|
+
constructor: isGlobalWebSocket() ? WebSocket : null,
|
|
70
|
+
maxReconnectionDelay: 1e4,
|
|
71
|
+
minReconnectionDelay: 1500,
|
|
72
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
73
|
+
connectionTimeout: 4e3,
|
|
74
|
+
maxRetries: Infinity,
|
|
75
|
+
debug: false
|
|
76
|
+
});
|
|
77
|
+
var bypassProperty = (src, dst, name) => {
|
|
78
|
+
Object.defineProperty(dst, name, {
|
|
79
|
+
get: () => src[name],
|
|
80
|
+
set: (value) => {
|
|
81
|
+
src[name] = value;
|
|
82
|
+
},
|
|
83
|
+
enumerable: true,
|
|
84
|
+
configurable: true
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
var initReconnectionDelay = (config) => config.minReconnectionDelay + Math.random() * config.minReconnectionDelay;
|
|
88
|
+
var updateReconnectionDelay = (config, previousDelay) => {
|
|
89
|
+
const newDelay = previousDelay * config.reconnectionDelayGrowFactor;
|
|
90
|
+
return newDelay > config.maxReconnectionDelay ? config.maxReconnectionDelay : newDelay;
|
|
91
|
+
};
|
|
92
|
+
var LEVEL_0_EVENTS = ["onopen", "onclose", "onmessage", "onerror"];
|
|
93
|
+
var reassignEventListeners = (ws, oldWs, listeners) => {
|
|
94
|
+
Object.keys(listeners).forEach((type) => {
|
|
95
|
+
listeners[type].forEach(([listener, options]) => {
|
|
96
|
+
ws.addEventListener(type, listener, options);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
if (oldWs) {
|
|
100
|
+
LEVEL_0_EVENTS.forEach((name) => {
|
|
101
|
+
ws[name] = oldWs[name];
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var ReconnectingWebsocket2 = function(url, protocols, options = {}) {
|
|
106
|
+
let ws;
|
|
107
|
+
let connectingTimeout;
|
|
108
|
+
let reconnectDelay = 0;
|
|
109
|
+
let retriesCount = 0;
|
|
110
|
+
let shouldRetry = true;
|
|
111
|
+
const listeners = {};
|
|
112
|
+
if (!(this instanceof ReconnectingWebsocket2)) {
|
|
113
|
+
throw new TypeError(
|
|
114
|
+
"Failed to construct 'ReconnectingWebSocket': Please use the 'new' operator"
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
const config = getDefaultOptions();
|
|
118
|
+
Object.keys(config).filter((key) => options.hasOwnProperty(key)).forEach((key) => config[key] = options[key]);
|
|
119
|
+
if (!isWebSocket(config.constructor)) {
|
|
120
|
+
throw new TypeError(
|
|
121
|
+
"Invalid WebSocket constructor. Set `options.constructor`"
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
const log = config.debug ? (...params) => console.log("RWS:", ...params) : () => {
|
|
125
|
+
};
|
|
126
|
+
const emitError = (code, msg) => setTimeout(() => {
|
|
127
|
+
const err = new Error(msg);
|
|
128
|
+
err.code = code;
|
|
129
|
+
if (Array.isArray(listeners.error)) {
|
|
130
|
+
listeners.error.forEach(([fn]) => fn(err));
|
|
131
|
+
}
|
|
132
|
+
if (ws.onerror) {
|
|
133
|
+
ws.onerror(err);
|
|
134
|
+
}
|
|
135
|
+
}, 0);
|
|
136
|
+
const handleClose = () => {
|
|
137
|
+
log("close");
|
|
138
|
+
retriesCount++;
|
|
139
|
+
log("retries count:", retriesCount);
|
|
140
|
+
if (retriesCount > config.maxRetries) {
|
|
141
|
+
emitError("EHOSTDOWN", "Too many failed connection attempts");
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
if (!reconnectDelay) {
|
|
145
|
+
reconnectDelay = initReconnectionDelay(config);
|
|
146
|
+
} else {
|
|
147
|
+
reconnectDelay = updateReconnectionDelay(config, reconnectDelay);
|
|
148
|
+
}
|
|
149
|
+
log("reconnectDelay:", reconnectDelay);
|
|
150
|
+
if (shouldRetry) {
|
|
151
|
+
setTimeout(connect, reconnectDelay);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const connect = () => {
|
|
155
|
+
log("connect");
|
|
156
|
+
const oldWs = ws;
|
|
157
|
+
ws = new config.constructor(url, protocols);
|
|
158
|
+
connectingTimeout = setTimeout(() => {
|
|
159
|
+
log("timeout");
|
|
160
|
+
ws.close();
|
|
161
|
+
emitError("ETIMEDOUT", "Connection timeout");
|
|
162
|
+
}, config.connectionTimeout);
|
|
163
|
+
log("bypass properties");
|
|
164
|
+
for (let key in ws) {
|
|
165
|
+
if (["addEventListener", "removeEventListener", "close", "send"].indexOf(
|
|
166
|
+
key
|
|
167
|
+
) < 0) {
|
|
168
|
+
bypassProperty(ws, this, key);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
ws.addEventListener("open", () => {
|
|
172
|
+
clearTimeout(connectingTimeout);
|
|
173
|
+
log("open");
|
|
174
|
+
reconnectDelay = initReconnectionDelay(config);
|
|
175
|
+
log("reconnectDelay:", reconnectDelay);
|
|
176
|
+
retriesCount = 0;
|
|
177
|
+
});
|
|
178
|
+
ws.addEventListener("close", handleClose);
|
|
179
|
+
reassignEventListeners(ws, oldWs, listeners);
|
|
180
|
+
};
|
|
181
|
+
log("init");
|
|
182
|
+
connect();
|
|
183
|
+
this.close = (code = 1e3, reason = "", { keepClosed = false, fastClose = true, delay: delay3 = 0 } = {}) => {
|
|
184
|
+
if (delay3) {
|
|
185
|
+
reconnectDelay = delay3;
|
|
186
|
+
}
|
|
187
|
+
shouldRetry = !keepClosed;
|
|
188
|
+
ws.close(code, reason);
|
|
189
|
+
if (fastClose) {
|
|
190
|
+
const fakeCloseEvent = {
|
|
191
|
+
code,
|
|
192
|
+
reason,
|
|
193
|
+
wasClean: true
|
|
194
|
+
};
|
|
195
|
+
handleClose();
|
|
196
|
+
if (Array.isArray(listeners.close)) {
|
|
197
|
+
listeners.close.forEach(([listener, options2]) => {
|
|
198
|
+
listener(fakeCloseEvent);
|
|
199
|
+
ws.removeEventListener("close", listener, options2);
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
if (ws.onclose) {
|
|
203
|
+
ws.onclose(fakeCloseEvent);
|
|
204
|
+
ws.onclose = null;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
this.send = (data) => {
|
|
209
|
+
ws.send(data);
|
|
210
|
+
};
|
|
211
|
+
this.addEventListener = (type, listener, options2) => {
|
|
212
|
+
if (Array.isArray(listeners[type])) {
|
|
213
|
+
if (!listeners[type].some(([l]) => l === listener)) {
|
|
214
|
+
listeners[type].push([listener, options2]);
|
|
215
|
+
}
|
|
216
|
+
} else {
|
|
217
|
+
listeners[type] = [[listener, options2]];
|
|
218
|
+
}
|
|
219
|
+
ws.addEventListener(type, listener, options2);
|
|
220
|
+
};
|
|
221
|
+
this.removeEventListener = (type, listener, options2) => {
|
|
222
|
+
if (Array.isArray(listeners[type])) {
|
|
223
|
+
listeners[type] = listeners[type].filter(([l]) => l !== listener);
|
|
224
|
+
}
|
|
225
|
+
ws.removeEventListener(type, listener, options2);
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
module.exports = ReconnectingWebsocket2;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Runware/index.ts
|
|
233
|
+
var Runware_exports = {};
|
|
234
|
+
__export(Runware_exports, {
|
|
235
|
+
EControlMode: () => EControlMode,
|
|
236
|
+
EOpenPosePreProcessor: () => EOpenPosePreProcessor,
|
|
237
|
+
EPreProcessor: () => EPreProcessor,
|
|
238
|
+
EPreProcessorGroup: () => EPreProcessorGroup,
|
|
239
|
+
ETaskType: () => ETaskType,
|
|
240
|
+
Environment: () => Environment,
|
|
241
|
+
Runware: () => Runware,
|
|
242
|
+
RunwareClient: () => RunwareClient,
|
|
243
|
+
RunwareServer: () => RunwareServer,
|
|
244
|
+
SdkType: () => SdkType
|
|
245
|
+
});
|
|
246
|
+
module.exports = __toCommonJS(Runware_exports);
|
|
247
|
+
|
|
248
|
+
// Runware/types.ts
|
|
249
|
+
var Environment = /* @__PURE__ */ ((Environment2) => {
|
|
250
|
+
Environment2["PRODUCTION"] = "PRODUCTION";
|
|
251
|
+
Environment2["DEVELOPMENT"] = "DEVELOPMENT";
|
|
252
|
+
Environment2["TEST"] = "TEST";
|
|
253
|
+
return Environment2;
|
|
254
|
+
})(Environment || {});
|
|
255
|
+
var SdkType = /* @__PURE__ */ ((SdkType2) => {
|
|
256
|
+
SdkType2["CLIENT"] = "CLIENT";
|
|
257
|
+
SdkType2["SERVER"] = "SERVER";
|
|
258
|
+
return SdkType2;
|
|
259
|
+
})(SdkType || {});
|
|
260
|
+
var ETaskType = /* @__PURE__ */ ((ETaskType2) => {
|
|
261
|
+
ETaskType2["IMAGE_INFERENCE"] = "imageInference";
|
|
262
|
+
ETaskType2["IMAGE_UPLOAD"] = "imageUpload";
|
|
263
|
+
ETaskType2["IMAGE_UPSCALE"] = "imageUpscale";
|
|
264
|
+
ETaskType2["IMAGE_BACKGROUND_REMOVAL"] = "imageBackgroundRemoval";
|
|
265
|
+
ETaskType2["IMAGE_CAPTION"] = "imageCaption";
|
|
266
|
+
ETaskType2["IMAGE_CONTROL_NET_PRE_PROCESS"] = "imageControlNetPreProcess";
|
|
267
|
+
ETaskType2["PROMPT_ENHANCE"] = "promptEnhance";
|
|
268
|
+
ETaskType2["AUTHENTICATION"] = "authentication";
|
|
269
|
+
ETaskType2["MODEL_UPLOAD"] = "modelUpload";
|
|
270
|
+
return ETaskType2;
|
|
271
|
+
})(ETaskType || {});
|
|
272
|
+
var EControlMode = /* @__PURE__ */ ((EControlMode2) => {
|
|
273
|
+
EControlMode2["BALANCED"] = "balanced";
|
|
274
|
+
EControlMode2["PROMPT"] = "prompt";
|
|
275
|
+
EControlMode2["CONTROL_NET"] = "controlnet";
|
|
276
|
+
return EControlMode2;
|
|
277
|
+
})(EControlMode || {});
|
|
278
|
+
var EPreProcessorGroup = /* @__PURE__ */ ((EPreProcessorGroup2) => {
|
|
279
|
+
EPreProcessorGroup2["canny"] = "canny";
|
|
280
|
+
EPreProcessorGroup2["depth"] = "depth";
|
|
281
|
+
EPreProcessorGroup2["mlsd"] = "mlsd";
|
|
282
|
+
EPreProcessorGroup2["normalbae"] = "normalbae";
|
|
283
|
+
EPreProcessorGroup2["openpose"] = "openpose";
|
|
284
|
+
EPreProcessorGroup2["tile"] = "tile";
|
|
285
|
+
EPreProcessorGroup2["seg"] = "seg";
|
|
286
|
+
EPreProcessorGroup2["lineart"] = "lineart";
|
|
287
|
+
EPreProcessorGroup2["lineart_anime"] = "lineart_anime";
|
|
288
|
+
EPreProcessorGroup2["shuffle"] = "shuffle";
|
|
289
|
+
EPreProcessorGroup2["scribble"] = "scribble";
|
|
290
|
+
EPreProcessorGroup2["softedge"] = "softedge";
|
|
291
|
+
return EPreProcessorGroup2;
|
|
292
|
+
})(EPreProcessorGroup || {});
|
|
293
|
+
var EPreProcessor = /* @__PURE__ */ ((EPreProcessor2) => {
|
|
294
|
+
EPreProcessor2["canny"] = "canny";
|
|
295
|
+
EPreProcessor2["depth_leres"] = "depth_leres";
|
|
296
|
+
EPreProcessor2["depth_midas"] = "depth_midas";
|
|
297
|
+
EPreProcessor2["depth_zoe"] = "depth_zoe";
|
|
298
|
+
EPreProcessor2["inpaint_global_harmonious"] = "inpaint_global_harmonious";
|
|
299
|
+
EPreProcessor2["lineart_anime"] = "lineart_anime";
|
|
300
|
+
EPreProcessor2["lineart_coarse"] = "lineart_coarse";
|
|
301
|
+
EPreProcessor2["lineart_realistic"] = "lineart_realistic";
|
|
302
|
+
EPreProcessor2["lineart_standard"] = "lineart_standard";
|
|
303
|
+
EPreProcessor2["mlsd"] = "mlsd";
|
|
304
|
+
EPreProcessor2["normal_bae"] = "normal_bae";
|
|
305
|
+
EPreProcessor2["scribble_hed"] = "scribble_hed";
|
|
306
|
+
EPreProcessor2["scribble_pidinet"] = "scribble_pidinet";
|
|
307
|
+
EPreProcessor2["seg_ofade20k"] = "seg_ofade20k";
|
|
308
|
+
EPreProcessor2["seg_ofcoco"] = "seg_ofcoco";
|
|
309
|
+
EPreProcessor2["seg_ufade20k"] = "seg_ufade20k";
|
|
310
|
+
EPreProcessor2["shuffle"] = "shuffle";
|
|
311
|
+
EPreProcessor2["softedge_hed"] = "softedge_hed";
|
|
312
|
+
EPreProcessor2["softedge_hedsafe"] = "softedge_hedsafe";
|
|
313
|
+
EPreProcessor2["softedge_pidinet"] = "softedge_pidinet";
|
|
314
|
+
EPreProcessor2["softedge_pidisafe"] = "softedge_pidisafe";
|
|
315
|
+
EPreProcessor2["tile_gaussian"] = "tile_gaussian";
|
|
316
|
+
EPreProcessor2["openpose"] = "openpose";
|
|
317
|
+
EPreProcessor2["openpose_face"] = "openpose_face";
|
|
318
|
+
EPreProcessor2["openpose_faceonly"] = "openpose_faceonly";
|
|
319
|
+
EPreProcessor2["openpose_full"] = "openpose_full";
|
|
320
|
+
EPreProcessor2["openpose_hand"] = "openpose_hand";
|
|
321
|
+
return EPreProcessor2;
|
|
322
|
+
})(EPreProcessor || {});
|
|
323
|
+
var EOpenPosePreProcessor = /* @__PURE__ */ ((EOpenPosePreProcessor2) => {
|
|
324
|
+
EOpenPosePreProcessor2["openpose"] = "openpose";
|
|
325
|
+
EOpenPosePreProcessor2["openpose_face"] = "openpose_face";
|
|
326
|
+
EOpenPosePreProcessor2["openpose_faceonly"] = "openpose_faceonly";
|
|
327
|
+
EOpenPosePreProcessor2["openpose_full"] = "openpose_full";
|
|
328
|
+
EOpenPosePreProcessor2["openpose_hand"] = "openpose_hand";
|
|
329
|
+
return EOpenPosePreProcessor2;
|
|
330
|
+
})(EOpenPosePreProcessor || {});
|
|
331
|
+
|
|
332
|
+
// Runware/utils.ts
|
|
333
|
+
var import_uuid = require("uuid");
|
|
334
|
+
var TIMEOUT_DURATION = 6e4;
|
|
335
|
+
var MINIMUM_TIMEOUT_DURATION = 1e3;
|
|
336
|
+
var POLLING_INTERVAL = 100;
|
|
337
|
+
var BASE_RUNWARE_URLS = {
|
|
338
|
+
["PRODUCTION" /* PRODUCTION */]: "wss://ws-api.runware.ai/v1",
|
|
339
|
+
["TEST" /* TEST */]: "ws://localhost:8080"
|
|
340
|
+
};
|
|
341
|
+
var removeFromAray = (col, targetElem) => {
|
|
342
|
+
if (col == null) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
let i = col.indexOf(targetElem);
|
|
346
|
+
if (i === -1) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
col.splice(i, 1);
|
|
350
|
+
};
|
|
351
|
+
var getIntervalWithPromise = (callback, {
|
|
352
|
+
debugKey = "debugKey",
|
|
353
|
+
timeoutDuration = TIMEOUT_DURATION,
|
|
354
|
+
shouldThrowError = true
|
|
355
|
+
}) => {
|
|
356
|
+
timeoutDuration = timeoutDuration < MINIMUM_TIMEOUT_DURATION ? MINIMUM_TIMEOUT_DURATION : timeoutDuration;
|
|
357
|
+
return new Promise((resolve, reject) => {
|
|
358
|
+
const timeoutId = setTimeout(() => {
|
|
359
|
+
if (intervalId) {
|
|
360
|
+
clearInterval(intervalId);
|
|
361
|
+
if (shouldThrowError) {
|
|
362
|
+
reject(`Response could not be received from server for ${debugKey}`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
clearTimeout(timeoutId);
|
|
366
|
+
}, timeoutDuration);
|
|
367
|
+
let intervalId = setInterval(async () => {
|
|
368
|
+
const shouldClear = callback({ resolve, reject, intervalId });
|
|
369
|
+
if (shouldClear) {
|
|
370
|
+
clearInterval(intervalId);
|
|
371
|
+
clearTimeout(timeoutId);
|
|
372
|
+
}
|
|
373
|
+
}, POLLING_INTERVAL);
|
|
374
|
+
});
|
|
375
|
+
};
|
|
376
|
+
var fileToBase64 = (file) => new Promise((resolve) => {
|
|
377
|
+
const reader = new FileReader();
|
|
378
|
+
reader.readAsDataURL(file);
|
|
379
|
+
reader.onload = function() {
|
|
380
|
+
resolve(reader.result);
|
|
381
|
+
};
|
|
382
|
+
});
|
|
383
|
+
var getUUID = () => (0, import_uuid.v4)();
|
|
384
|
+
var isValidUUID = (uuid) => (0, import_uuid.validate)(uuid);
|
|
385
|
+
var accessDeepObject = ({
|
|
386
|
+
key,
|
|
387
|
+
data,
|
|
388
|
+
useZero = true,
|
|
389
|
+
shouldReturnString = false
|
|
390
|
+
}) => {
|
|
391
|
+
const splittedKeys = key.split(/\.|\[/).map((key2) => key2.replace(/\]$/, ""));
|
|
392
|
+
const value = splittedKeys.reduce((acc, curr) => {
|
|
393
|
+
var _a, _b;
|
|
394
|
+
const returnZero = useZero ? 0 : void 0;
|
|
395
|
+
const currentValue = acc == null ? void 0 : acc[curr];
|
|
396
|
+
if (!currentValue) {
|
|
397
|
+
return returnZero;
|
|
398
|
+
}
|
|
399
|
+
if (Array.isArray(currentValue) && /^\d+$/.test(curr)) {
|
|
400
|
+
const index = parseInt(curr, 10);
|
|
401
|
+
if (index >= 0 && index < currentValue.length) {
|
|
402
|
+
return acc[curr] = currentValue[index];
|
|
403
|
+
} else {
|
|
404
|
+
return (_a = acc[curr]) != null ? _a : returnZero;
|
|
405
|
+
}
|
|
406
|
+
} else {
|
|
407
|
+
return (_b = acc[curr]) != null ? _b : returnZero;
|
|
408
|
+
}
|
|
409
|
+
}, data || {});
|
|
410
|
+
return value != null ? value : {};
|
|
411
|
+
};
|
|
412
|
+
var delay = (time, milliseconds = 1e3) => {
|
|
413
|
+
return new Promise((resolve) => setTimeout(resolve, time * milliseconds));
|
|
414
|
+
};
|
|
415
|
+
var removeListener = (listeners, listener) => {
|
|
416
|
+
return listeners.filter((lis) => lis.key !== listener.key);
|
|
417
|
+
};
|
|
418
|
+
var evaluateNonTrue = ({
|
|
419
|
+
key,
|
|
420
|
+
value
|
|
421
|
+
}) => {
|
|
422
|
+
if (!!value || value === 0 || value === false) {
|
|
423
|
+
return { [key]: value };
|
|
424
|
+
} else {
|
|
425
|
+
return {};
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
// Runware/async-retry.ts
|
|
430
|
+
var asyncRetry = async (apiCall, options = {}) => {
|
|
431
|
+
var _a;
|
|
432
|
+
const { delayInSeconds = 1, callback } = options;
|
|
433
|
+
let maxRetries = (_a = options.maxRetries) != null ? _a : 1;
|
|
434
|
+
while (maxRetries) {
|
|
435
|
+
try {
|
|
436
|
+
const result = await apiCall();
|
|
437
|
+
return result;
|
|
438
|
+
} catch (error) {
|
|
439
|
+
callback == null ? void 0 : callback();
|
|
440
|
+
maxRetries--;
|
|
441
|
+
if (maxRetries > 0) {
|
|
442
|
+
await delay(delayInSeconds);
|
|
443
|
+
await asyncRetry(apiCall, __spreadProps(__spreadValues({}, options), { maxRetries }));
|
|
444
|
+
} else {
|
|
445
|
+
throw error;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
// Runware/Runware-base.ts
|
|
452
|
+
var RunwareBase = class {
|
|
453
|
+
constructor({
|
|
454
|
+
apiKey,
|
|
455
|
+
url = BASE_RUNWARE_URLS.PRODUCTION,
|
|
456
|
+
shouldReconnect = true,
|
|
457
|
+
globalMaxRetries = 2,
|
|
458
|
+
timeoutDuration = TIMEOUT_DURATION
|
|
459
|
+
}) {
|
|
460
|
+
this._listeners = [];
|
|
461
|
+
// _globalMessages: any[] = [];
|
|
462
|
+
this._globalMessages = {};
|
|
463
|
+
this._globalImages = [];
|
|
464
|
+
this.isWebsocketReadyState = () => {
|
|
465
|
+
var _a;
|
|
466
|
+
return ((_a = this._ws) == null ? void 0 : _a.readyState) === 1;
|
|
467
|
+
};
|
|
468
|
+
// We moving to an array format, it make sense to consolidate all request to an array here
|
|
469
|
+
this.send = (msg) => {
|
|
470
|
+
this._ws.send(JSON.stringify([msg]));
|
|
471
|
+
};
|
|
472
|
+
this.uploadImage = async (file) => {
|
|
473
|
+
try {
|
|
474
|
+
return await asyncRetry(async () => {
|
|
475
|
+
const taskUUID = getUUID();
|
|
476
|
+
if (typeof file === "string" && isValidUUID(file)) {
|
|
477
|
+
return {
|
|
478
|
+
imageURL: file,
|
|
479
|
+
imageUUID: file,
|
|
480
|
+
taskUUID,
|
|
481
|
+
taskType: "imageUpload" /* IMAGE_UPLOAD */
|
|
482
|
+
};
|
|
483
|
+
}
|
|
484
|
+
const imageBase64 = typeof file === "string" ? file : await fileToBase64(file);
|
|
485
|
+
return {
|
|
486
|
+
imageURL: imageBase64,
|
|
487
|
+
imageUUID: imageBase64,
|
|
488
|
+
taskUUID,
|
|
489
|
+
taskType: "imageUpload" /* IMAGE_UPLOAD */
|
|
490
|
+
};
|
|
491
|
+
});
|
|
492
|
+
} catch (e) {
|
|
493
|
+
throw e;
|
|
494
|
+
}
|
|
495
|
+
};
|
|
496
|
+
this.controlNetPreProcess = async ({
|
|
497
|
+
inputImage,
|
|
498
|
+
preProcessorType,
|
|
499
|
+
height,
|
|
500
|
+
width,
|
|
501
|
+
outputType,
|
|
502
|
+
outputFormat,
|
|
503
|
+
highThresholdCanny,
|
|
504
|
+
lowThresholdCanny,
|
|
505
|
+
includeHandsAndFaceOpenPose,
|
|
506
|
+
includeCost,
|
|
507
|
+
customTaskUUID,
|
|
508
|
+
retry
|
|
509
|
+
}) => {
|
|
510
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
511
|
+
let lis = void 0;
|
|
512
|
+
try {
|
|
513
|
+
return await asyncRetry(
|
|
514
|
+
async () => {
|
|
515
|
+
await this.ensureConnection();
|
|
516
|
+
const image = await this.uploadImage(inputImage);
|
|
517
|
+
if (!(image == null ? void 0 : image.imageUUID))
|
|
518
|
+
return null;
|
|
519
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
520
|
+
this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
521
|
+
inputImage: image.imageUUID,
|
|
522
|
+
taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
|
|
523
|
+
taskUUID,
|
|
524
|
+
preProcessorType
|
|
525
|
+
}, evaluateNonTrue({ key: "height", value: height })), evaluateNonTrue({ key: "width", value: width })), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
|
|
526
|
+
key: "highThresholdCanny",
|
|
527
|
+
value: highThresholdCanny
|
|
528
|
+
})), evaluateNonTrue({
|
|
529
|
+
key: "lowThresholdCanny",
|
|
530
|
+
value: lowThresholdCanny
|
|
531
|
+
})), evaluateNonTrue({
|
|
532
|
+
key: "includeHandsAndFaceOpenPose",
|
|
533
|
+
value: includeHandsAndFaceOpenPose
|
|
534
|
+
})));
|
|
535
|
+
lis = this.globalListener({
|
|
536
|
+
taskUUID
|
|
537
|
+
});
|
|
538
|
+
const guideImage = await getIntervalWithPromise(
|
|
539
|
+
({ resolve, reject }) => {
|
|
540
|
+
const uploadedImage = this.getSingleMessage({
|
|
541
|
+
taskUUID
|
|
542
|
+
});
|
|
543
|
+
if (!uploadedImage)
|
|
544
|
+
return;
|
|
545
|
+
if (uploadedImage == null ? void 0 : uploadedImage.error) {
|
|
546
|
+
reject(uploadedImage);
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
if (uploadedImage) {
|
|
550
|
+
resolve(uploadedImage);
|
|
551
|
+
return true;
|
|
552
|
+
}
|
|
553
|
+
},
|
|
554
|
+
{
|
|
555
|
+
debugKey: "unprocessed-image",
|
|
556
|
+
timeoutDuration: this._timeoutDuration
|
|
557
|
+
}
|
|
558
|
+
);
|
|
559
|
+
lis.destroy();
|
|
560
|
+
return guideImage;
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
maxRetries: totalRetry,
|
|
564
|
+
callback: () => {
|
|
565
|
+
lis == null ? void 0 : lis.destroy();
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
);
|
|
569
|
+
} catch (e) {
|
|
570
|
+
throw e;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
this.requestImageToText = async ({
|
|
574
|
+
inputImage,
|
|
575
|
+
includeCost,
|
|
576
|
+
customTaskUUID,
|
|
577
|
+
retry
|
|
578
|
+
}) => {
|
|
579
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
580
|
+
let lis = void 0;
|
|
581
|
+
try {
|
|
582
|
+
return await asyncRetry(
|
|
583
|
+
async () => {
|
|
584
|
+
await this.ensureConnection();
|
|
585
|
+
const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
|
|
586
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
587
|
+
this.send(__spreadValues({
|
|
588
|
+
taskUUID,
|
|
589
|
+
taskType: "imageCaption" /* IMAGE_CAPTION */,
|
|
590
|
+
inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
|
|
591
|
+
}, evaluateNonTrue({ key: "includeCost", value: includeCost })));
|
|
592
|
+
lis = this.globalListener({
|
|
593
|
+
taskUUID
|
|
594
|
+
});
|
|
595
|
+
const response = await getIntervalWithPromise(
|
|
596
|
+
({ resolve, reject }) => {
|
|
597
|
+
const newReverseClip = this.getSingleMessage({
|
|
598
|
+
taskUUID
|
|
599
|
+
});
|
|
600
|
+
if (!newReverseClip)
|
|
601
|
+
return;
|
|
602
|
+
if (newReverseClip == null ? void 0 : newReverseClip.error) {
|
|
603
|
+
reject(newReverseClip);
|
|
604
|
+
return true;
|
|
605
|
+
}
|
|
606
|
+
if (newReverseClip) {
|
|
607
|
+
delete this._globalMessages[taskUUID];
|
|
608
|
+
resolve(newReverseClip);
|
|
609
|
+
return true;
|
|
610
|
+
}
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
debugKey: "remove-image-background",
|
|
614
|
+
timeoutDuration: this._timeoutDuration
|
|
615
|
+
}
|
|
616
|
+
);
|
|
617
|
+
lis.destroy();
|
|
618
|
+
return response;
|
|
619
|
+
},
|
|
620
|
+
{
|
|
621
|
+
maxRetries: totalRetry,
|
|
622
|
+
callback: () => {
|
|
623
|
+
lis == null ? void 0 : lis.destroy();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
);
|
|
627
|
+
} catch (e) {
|
|
628
|
+
throw e;
|
|
629
|
+
}
|
|
630
|
+
};
|
|
631
|
+
this.removeImageBackground = async ({
|
|
632
|
+
inputImage,
|
|
633
|
+
outputType,
|
|
634
|
+
outputFormat,
|
|
635
|
+
rgba,
|
|
636
|
+
postProcessMask,
|
|
637
|
+
returnOnlyMask,
|
|
638
|
+
alphaMatting,
|
|
639
|
+
alphaMattingForegroundThreshold,
|
|
640
|
+
alphaMattingBackgroundThreshold,
|
|
641
|
+
alphaMattingErodeSize,
|
|
642
|
+
includeCost,
|
|
643
|
+
customTaskUUID,
|
|
644
|
+
retry
|
|
645
|
+
}) => {
|
|
646
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
647
|
+
let lis = void 0;
|
|
648
|
+
try {
|
|
649
|
+
return await asyncRetry(
|
|
650
|
+
async () => {
|
|
651
|
+
await this.ensureConnection();
|
|
652
|
+
const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
|
|
653
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
654
|
+
this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
|
|
655
|
+
taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
|
|
656
|
+
taskUUID,
|
|
657
|
+
inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
|
|
658
|
+
}, evaluateNonTrue({ key: "rgba", value: rgba })), evaluateNonTrue({
|
|
659
|
+
key: "postProcessMask",
|
|
660
|
+
value: postProcessMask
|
|
661
|
+
})), evaluateNonTrue({
|
|
662
|
+
key: "returnOnlyMask",
|
|
663
|
+
value: returnOnlyMask
|
|
664
|
+
})), evaluateNonTrue({ key: "alphaMatting", value: alphaMatting })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
|
|
665
|
+
key: "alphaMattingForegroundThreshold",
|
|
666
|
+
value: alphaMattingForegroundThreshold
|
|
667
|
+
})), evaluateNonTrue({
|
|
668
|
+
key: "alphaMattingBackgroundThreshold",
|
|
669
|
+
value: alphaMattingBackgroundThreshold
|
|
670
|
+
})), evaluateNonTrue({
|
|
671
|
+
key: "alphaMattingErodeSize",
|
|
672
|
+
value: alphaMattingErodeSize
|
|
673
|
+
})), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })));
|
|
674
|
+
lis = this.globalListener({
|
|
675
|
+
taskUUID
|
|
676
|
+
});
|
|
677
|
+
const response = await getIntervalWithPromise(
|
|
678
|
+
({ resolve, reject }) => {
|
|
679
|
+
const newRemoveBackground = this.getSingleMessage({ taskUUID });
|
|
680
|
+
if (!newRemoveBackground)
|
|
681
|
+
return;
|
|
682
|
+
if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
|
|
683
|
+
reject(newRemoveBackground);
|
|
684
|
+
return true;
|
|
685
|
+
}
|
|
686
|
+
if (newRemoveBackground) {
|
|
687
|
+
delete this._globalMessages[taskUUID];
|
|
688
|
+
resolve(newRemoveBackground);
|
|
689
|
+
return true;
|
|
690
|
+
}
|
|
691
|
+
},
|
|
692
|
+
{
|
|
693
|
+
debugKey: "remove-image-background",
|
|
694
|
+
timeoutDuration: this._timeoutDuration
|
|
695
|
+
}
|
|
696
|
+
);
|
|
697
|
+
lis.destroy();
|
|
698
|
+
return response;
|
|
699
|
+
},
|
|
700
|
+
{
|
|
701
|
+
maxRetries: totalRetry,
|
|
702
|
+
callback: () => {
|
|
703
|
+
lis == null ? void 0 : lis.destroy();
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
);
|
|
707
|
+
} catch (e) {
|
|
708
|
+
throw e;
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
this.upscaleGan = async ({
|
|
712
|
+
inputImage,
|
|
713
|
+
upscaleFactor,
|
|
714
|
+
outputType,
|
|
715
|
+
outputFormat,
|
|
716
|
+
includeCost,
|
|
717
|
+
customTaskUUID,
|
|
718
|
+
retry
|
|
719
|
+
}) => {
|
|
720
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
721
|
+
let lis = void 0;
|
|
722
|
+
try {
|
|
723
|
+
return await asyncRetry(
|
|
724
|
+
async () => {
|
|
725
|
+
await this.ensureConnection();
|
|
726
|
+
let imageUploaded;
|
|
727
|
+
imageUploaded = await this.uploadImage(inputImage);
|
|
728
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
729
|
+
this.send(__spreadValues(__spreadValues(__spreadValues({
|
|
730
|
+
taskUUID,
|
|
731
|
+
inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
|
|
732
|
+
taskType: "imageUpscale" /* IMAGE_UPSCALE */,
|
|
733
|
+
upscaleFactor
|
|
734
|
+
}, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
|
|
735
|
+
lis = this.globalListener({
|
|
736
|
+
taskUUID
|
|
737
|
+
});
|
|
738
|
+
const response = await getIntervalWithPromise(
|
|
739
|
+
({ resolve, reject }) => {
|
|
740
|
+
const newUpscaleGan = this.getSingleMessage({ taskUUID });
|
|
741
|
+
if (!newUpscaleGan)
|
|
742
|
+
return;
|
|
743
|
+
if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
|
|
744
|
+
reject(newUpscaleGan);
|
|
745
|
+
return true;
|
|
746
|
+
}
|
|
747
|
+
if (newUpscaleGan) {
|
|
748
|
+
delete this._globalMessages[taskUUID];
|
|
749
|
+
resolve(newUpscaleGan);
|
|
750
|
+
return true;
|
|
751
|
+
}
|
|
752
|
+
},
|
|
753
|
+
{ debugKey: "upscale-gan", timeoutDuration: this._timeoutDuration }
|
|
754
|
+
);
|
|
755
|
+
lis.destroy();
|
|
756
|
+
return response;
|
|
757
|
+
},
|
|
758
|
+
{
|
|
759
|
+
maxRetries: totalRetry,
|
|
760
|
+
callback: () => {
|
|
761
|
+
lis == null ? void 0 : lis.destroy();
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
);
|
|
765
|
+
} catch (e) {
|
|
766
|
+
throw e;
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
this.enhancePrompt = async ({
|
|
770
|
+
prompt,
|
|
771
|
+
promptMaxLength = 380,
|
|
772
|
+
promptVersions = 1,
|
|
773
|
+
includeCost,
|
|
774
|
+
customTaskUUID,
|
|
775
|
+
retry
|
|
776
|
+
}) => {
|
|
777
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
778
|
+
let lis = void 0;
|
|
779
|
+
try {
|
|
780
|
+
return await asyncRetry(
|
|
781
|
+
async () => {
|
|
782
|
+
await this.ensureConnection();
|
|
783
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
784
|
+
this.send(__spreadProps(__spreadValues({
|
|
785
|
+
prompt,
|
|
786
|
+
taskUUID,
|
|
787
|
+
promptMaxLength,
|
|
788
|
+
promptVersions
|
|
789
|
+
}, evaluateNonTrue({ key: "includeCost", value: includeCost })), {
|
|
790
|
+
taskType: "promptEnhance" /* PROMPT_ENHANCE */
|
|
791
|
+
}));
|
|
792
|
+
lis = this.globalListener({
|
|
793
|
+
taskUUID
|
|
794
|
+
});
|
|
795
|
+
const response = await getIntervalWithPromise(
|
|
796
|
+
({ resolve, reject }) => {
|
|
797
|
+
const reducedPrompt = this._globalMessages[taskUUID];
|
|
798
|
+
if (reducedPrompt == null ? void 0 : reducedPrompt.error) {
|
|
799
|
+
reject(reducedPrompt);
|
|
800
|
+
return true;
|
|
801
|
+
}
|
|
802
|
+
if ((reducedPrompt == null ? void 0 : reducedPrompt.length) >= promptVersions) {
|
|
803
|
+
delete this._globalMessages[taskUUID];
|
|
804
|
+
resolve(reducedPrompt);
|
|
805
|
+
return true;
|
|
806
|
+
}
|
|
807
|
+
},
|
|
808
|
+
{
|
|
809
|
+
debugKey: "enhance-prompt",
|
|
810
|
+
timeoutDuration: this._timeoutDuration
|
|
811
|
+
}
|
|
812
|
+
);
|
|
813
|
+
lis.destroy();
|
|
814
|
+
return response;
|
|
815
|
+
},
|
|
816
|
+
{
|
|
817
|
+
maxRetries: totalRetry,
|
|
818
|
+
callback: () => {
|
|
819
|
+
lis == null ? void 0 : lis.destroy();
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
);
|
|
823
|
+
} catch (e) {
|
|
824
|
+
throw e;
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
this.modelUpload = async (payload) => {
|
|
828
|
+
const _a = payload, { onUploadStream, retry, customTaskUUID } = _a, addModelPayload = __objRest(_a, ["onUploadStream", "retry", "customTaskUUID"]);
|
|
829
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
830
|
+
let lis = void 0;
|
|
831
|
+
try {
|
|
832
|
+
return await asyncRetry(
|
|
833
|
+
async () => {
|
|
834
|
+
await this.ensureConnection();
|
|
835
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
836
|
+
this.send(__spreadProps(__spreadValues({}, addModelPayload), {
|
|
837
|
+
taskUUID,
|
|
838
|
+
taskType: "modelUpload" /* MODEL_UPLOAD */
|
|
839
|
+
}));
|
|
840
|
+
let result;
|
|
841
|
+
let errorResult;
|
|
842
|
+
lis = this.listenToUpload({
|
|
843
|
+
taskUUID,
|
|
844
|
+
onUploadStream: (response, error) => {
|
|
845
|
+
onUploadStream == null ? void 0 : onUploadStream(response, error);
|
|
846
|
+
if ((response == null ? void 0 : response.statusId) === 1) {
|
|
847
|
+
result = response;
|
|
848
|
+
} else if (error) {
|
|
849
|
+
errorResult = error;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
});
|
|
853
|
+
const modelUploadResponse = await getIntervalWithPromise(
|
|
854
|
+
({ resolve, reject }) => {
|
|
855
|
+
if (result) {
|
|
856
|
+
resolve(result);
|
|
857
|
+
return true;
|
|
858
|
+
} else if (errorResult) {
|
|
859
|
+
reject(errorResult);
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
},
|
|
863
|
+
{
|
|
864
|
+
shouldThrowError: false,
|
|
865
|
+
timeoutDuration: 60 * 60 * 1e3
|
|
866
|
+
}
|
|
867
|
+
);
|
|
868
|
+
return modelUploadResponse;
|
|
869
|
+
},
|
|
870
|
+
{
|
|
871
|
+
maxRetries: totalRetry,
|
|
872
|
+
callback: () => {
|
|
873
|
+
lis == null ? void 0 : lis.destroy();
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
);
|
|
877
|
+
} catch (e) {
|
|
878
|
+
throw e;
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
this.getSingleMessage = ({ taskUUID }) => {
|
|
882
|
+
var _a;
|
|
883
|
+
const value = (_a = this._globalMessages[taskUUID]) == null ? void 0 : _a[0];
|
|
884
|
+
const errorValue = this._globalMessages[taskUUID];
|
|
885
|
+
if (!value && !errorValue)
|
|
886
|
+
return null;
|
|
887
|
+
return (errorValue == null ? void 0 : errorValue.error) ? errorValue : value;
|
|
888
|
+
};
|
|
889
|
+
this.disconnect = async () => {
|
|
890
|
+
var _a, _b, _c, _d;
|
|
891
|
+
(_b = (_a = this._ws) == null ? void 0 : _a.terminate) == null ? void 0 : _b.call(_a);
|
|
892
|
+
(_d = (_c = this._ws) == null ? void 0 : _c.close) == null ? void 0 : _d.call(_c);
|
|
893
|
+
};
|
|
894
|
+
this.connected = () => this.isWebsocketReadyState() && !!this._connectionSessionUUID;
|
|
895
|
+
this._apiKey = apiKey;
|
|
896
|
+
this._url = url;
|
|
897
|
+
this._sdkType = "CLIENT" /* CLIENT */;
|
|
898
|
+
this._shouldReconnect = shouldReconnect;
|
|
899
|
+
this._globalMaxRetries = globalMaxRetries;
|
|
900
|
+
this._timeoutDuration = timeoutDuration;
|
|
901
|
+
}
|
|
902
|
+
static async initialize(props) {
|
|
903
|
+
try {
|
|
904
|
+
const instance = new this(props);
|
|
905
|
+
await instance.ensureConnection();
|
|
906
|
+
return instance;
|
|
907
|
+
} catch (e) {
|
|
908
|
+
throw e;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
// protected addListener({
|
|
912
|
+
// lis,
|
|
913
|
+
// check,
|
|
914
|
+
// }: {
|
|
915
|
+
// lis: (v: any) => any;
|
|
916
|
+
// check: (v: any) => any;
|
|
917
|
+
// groupKey?: string;
|
|
918
|
+
// }): { destroy: Function } {
|
|
919
|
+
// this._ws.onmessage = (e: any) => {
|
|
920
|
+
// const m = JSON.parse(e.data);
|
|
921
|
+
// if (m?.error) {
|
|
922
|
+
// lis(m);
|
|
923
|
+
// } else if (check(m)) {
|
|
924
|
+
// lis(m);
|
|
925
|
+
// }
|
|
926
|
+
// };
|
|
927
|
+
// return {
|
|
928
|
+
// destroy: () => {},
|
|
929
|
+
// };
|
|
930
|
+
// }
|
|
931
|
+
addListener({
|
|
932
|
+
lis,
|
|
933
|
+
// check,
|
|
934
|
+
groupKey,
|
|
935
|
+
taskUUID
|
|
936
|
+
}) {
|
|
937
|
+
const listener = (msg) => {
|
|
938
|
+
var _a, _b, _c;
|
|
939
|
+
const arrayMessage = Array.isArray(msg == null ? void 0 : msg.data) ? msg.data : [msg.data];
|
|
940
|
+
const arrayErrors = ((_a = msg == null ? void 0 : msg[0]) == null ? void 0 : _a.errors) ? (_b = msg == null ? void 0 : msg[0]) == null ? void 0 : _b.errors : Array.isArray(msg == null ? void 0 : msg.errors) ? msg.errors : [msg.errors];
|
|
941
|
+
const filteredMessage = arrayMessage.filter(
|
|
942
|
+
(v) => ((v == null ? void 0 : v.taskUUID) || (v == null ? void 0 : v.taskType)) === taskUUID
|
|
943
|
+
);
|
|
944
|
+
const filteredErrors = arrayErrors.filter(
|
|
945
|
+
(v) => ((v == null ? void 0 : v.taskUUID) || (v == null ? void 0 : v.taskType)) === taskUUID
|
|
946
|
+
);
|
|
947
|
+
if (filteredErrors.length) {
|
|
948
|
+
lis({ error: __spreadValues({}, (_c = arrayErrors[0]) != null ? _c : {}) });
|
|
949
|
+
return;
|
|
950
|
+
}
|
|
951
|
+
if (filteredMessage.length) {
|
|
952
|
+
lis({ [taskUUID]: arrayMessage });
|
|
953
|
+
return;
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
const groupListener = { key: taskUUID || getUUID(), listener, groupKey };
|
|
957
|
+
this._listeners.push(groupListener);
|
|
958
|
+
const destroy = () => {
|
|
959
|
+
this._listeners = removeListener(this._listeners, groupListener);
|
|
960
|
+
};
|
|
961
|
+
return {
|
|
962
|
+
destroy
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
connect() {
|
|
966
|
+
this._ws.onopen = (e) => {
|
|
967
|
+
if (this._connectionSessionUUID) {
|
|
968
|
+
this.send({
|
|
969
|
+
taskType: "authentication" /* AUTHENTICATION */,
|
|
970
|
+
apiKey: this._apiKey,
|
|
971
|
+
connectionSessionUUID: this._connectionSessionUUID
|
|
972
|
+
});
|
|
973
|
+
} else {
|
|
974
|
+
this.send({ apiKey: this._apiKey, taskType: "authentication" /* AUTHENTICATION */ });
|
|
975
|
+
}
|
|
976
|
+
this.addListener({
|
|
977
|
+
taskUUID: "authentication" /* AUTHENTICATION */,
|
|
978
|
+
lis: (m) => {
|
|
979
|
+
var _a, _b;
|
|
980
|
+
if (m == null ? void 0 : m.error) {
|
|
981
|
+
this._invalidAPIkey = m;
|
|
982
|
+
return;
|
|
983
|
+
}
|
|
984
|
+
this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
|
|
985
|
+
this._invalidAPIkey = void 0;
|
|
986
|
+
}
|
|
987
|
+
});
|
|
988
|
+
};
|
|
989
|
+
this._ws.onmessage = (e) => {
|
|
990
|
+
var _a;
|
|
991
|
+
const data = JSON.parse(e.data);
|
|
992
|
+
for (const lis of this._listeners) {
|
|
993
|
+
const result = (_a = lis == null ? void 0 : lis.listener) == null ? void 0 : _a.call(lis, data);
|
|
994
|
+
if (result)
|
|
995
|
+
return;
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
this._ws.onclose = (e) => {
|
|
999
|
+
if (this._invalidAPIkey) {
|
|
1000
|
+
return;
|
|
1001
|
+
}
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
destroy(lis) {
|
|
1005
|
+
removeFromAray(this._listeners, lis);
|
|
1006
|
+
}
|
|
1007
|
+
listenToImages({
|
|
1008
|
+
onPartialImages,
|
|
1009
|
+
taskUUID,
|
|
1010
|
+
groupKey,
|
|
1011
|
+
positivePrompt,
|
|
1012
|
+
negativePrompt
|
|
1013
|
+
}) {
|
|
1014
|
+
return this.addListener({
|
|
1015
|
+
taskUUID,
|
|
1016
|
+
lis: (m) => {
|
|
1017
|
+
var _a, _b;
|
|
1018
|
+
let images = (_a = m == null ? void 0 : m[taskUUID]) == null ? void 0 : _a.filter(
|
|
1019
|
+
(img) => img.taskUUID === taskUUID
|
|
1020
|
+
);
|
|
1021
|
+
if (m.error) {
|
|
1022
|
+
onPartialImages == null ? void 0 : onPartialImages(images, (m == null ? void 0 : m.error) && m);
|
|
1023
|
+
this._globalError = m;
|
|
1024
|
+
} else {
|
|
1025
|
+
images = images.map((image) => __spreadProps(__spreadValues({}, image), {
|
|
1026
|
+
positivePrompt,
|
|
1027
|
+
negativePrompt
|
|
1028
|
+
}));
|
|
1029
|
+
onPartialImages == null ? void 0 : onPartialImages(images, (m == null ? void 0 : m.error) && m);
|
|
1030
|
+
if (this._sdkType === "CLIENT" /* CLIENT */) {
|
|
1031
|
+
this._globalImages = [
|
|
1032
|
+
...this._globalImages,
|
|
1033
|
+
...((_b = m == null ? void 0 : m[taskUUID]) != null ? _b : []).map((image) => __spreadProps(__spreadValues({}, image), {
|
|
1034
|
+
positivePrompt,
|
|
1035
|
+
negativePrompt
|
|
1036
|
+
}))
|
|
1037
|
+
];
|
|
1038
|
+
} else {
|
|
1039
|
+
this._globalImages = [...this._globalImages, ...images];
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
},
|
|
1043
|
+
groupKey
|
|
1044
|
+
});
|
|
1045
|
+
}
|
|
1046
|
+
listenToUpload({
|
|
1047
|
+
onUploadStream,
|
|
1048
|
+
taskUUID
|
|
1049
|
+
}) {
|
|
1050
|
+
return this.addListener({
|
|
1051
|
+
taskUUID,
|
|
1052
|
+
lis: (m) => {
|
|
1053
|
+
var _a;
|
|
1054
|
+
const error = m == null ? void 0 : m.error;
|
|
1055
|
+
const result = (_a = m == null ? void 0 : m[taskUUID]) == null ? void 0 : _a[0];
|
|
1056
|
+
let response = (result == null ? void 0 : result.taskUUID) === taskUUID ? result : null;
|
|
1057
|
+
if (response || error) {
|
|
1058
|
+
onUploadStream == null ? void 0 : onUploadStream(response || void 0, error);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
});
|
|
1062
|
+
}
|
|
1063
|
+
globalListener({ taskUUID }) {
|
|
1064
|
+
return this.addListener({
|
|
1065
|
+
// check: (m) => {
|
|
1066
|
+
// const value = accessDeepObject({
|
|
1067
|
+
// key: responseKey,
|
|
1068
|
+
// data: m,
|
|
1069
|
+
// useZero: false,
|
|
1070
|
+
// });
|
|
1071
|
+
// return !!value;
|
|
1072
|
+
// },
|
|
1073
|
+
// check: responseKey,
|
|
1074
|
+
taskUUID,
|
|
1075
|
+
lis: (m) => {
|
|
1076
|
+
if (m.error) {
|
|
1077
|
+
this._globalMessages[taskUUID] = m;
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
const value = accessDeepObject({
|
|
1081
|
+
key: taskUUID,
|
|
1082
|
+
data: m,
|
|
1083
|
+
useZero: false
|
|
1084
|
+
});
|
|
1085
|
+
if (Array.isArray(value)) {
|
|
1086
|
+
value.forEach((v) => {
|
|
1087
|
+
var _a;
|
|
1088
|
+
this._globalMessages[v.taskUUID] = [
|
|
1089
|
+
...(_a = this._globalMessages[v.taskUUID]) != null ? _a : [],
|
|
1090
|
+
v
|
|
1091
|
+
];
|
|
1092
|
+
});
|
|
1093
|
+
} else {
|
|
1094
|
+
this._globalMessages[value.taskUUID] = value;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
async requestImages({
|
|
1100
|
+
outputType,
|
|
1101
|
+
outputFormat,
|
|
1102
|
+
uploadEndpoint,
|
|
1103
|
+
checkNsfw,
|
|
1104
|
+
positivePrompt,
|
|
1105
|
+
negativePrompt,
|
|
1106
|
+
seedImage,
|
|
1107
|
+
maskImage,
|
|
1108
|
+
strength,
|
|
1109
|
+
height,
|
|
1110
|
+
width,
|
|
1111
|
+
model,
|
|
1112
|
+
steps,
|
|
1113
|
+
scheduler,
|
|
1114
|
+
seed,
|
|
1115
|
+
CFGScale,
|
|
1116
|
+
clipSkip,
|
|
1117
|
+
usePromptWeighting,
|
|
1118
|
+
numberResults = 1,
|
|
1119
|
+
controlNet,
|
|
1120
|
+
lora,
|
|
1121
|
+
onPartialImages,
|
|
1122
|
+
includeCost,
|
|
1123
|
+
customTaskUUID,
|
|
1124
|
+
retry
|
|
1125
|
+
}) {
|
|
1126
|
+
let lis = void 0;
|
|
1127
|
+
let requestObject = void 0;
|
|
1128
|
+
let taskUUIDs = [];
|
|
1129
|
+
let retryCount = 0;
|
|
1130
|
+
const totalRetry = retry || this._globalMaxRetries;
|
|
1131
|
+
try {
|
|
1132
|
+
await this.ensureConnection();
|
|
1133
|
+
let seedImageUUID = null;
|
|
1134
|
+
let maskImageUUID = null;
|
|
1135
|
+
let controlNetData = [];
|
|
1136
|
+
if (seedImage) {
|
|
1137
|
+
const uploadedImage = await this.uploadImage(seedImage);
|
|
1138
|
+
if (!uploadedImage)
|
|
1139
|
+
return [];
|
|
1140
|
+
seedImageUUID = uploadedImage.imageUUID;
|
|
1141
|
+
}
|
|
1142
|
+
if (maskImage) {
|
|
1143
|
+
const uploadedMaskInitiator = await this.uploadImage(maskImage);
|
|
1144
|
+
if (!uploadedMaskInitiator)
|
|
1145
|
+
return [];
|
|
1146
|
+
maskImageUUID = uploadedMaskInitiator.imageUUID;
|
|
1147
|
+
}
|
|
1148
|
+
if (controlNet == null ? void 0 : controlNet.length) {
|
|
1149
|
+
for (let i = 0; i < controlNet.length; i++) {
|
|
1150
|
+
const controlData = controlNet[i];
|
|
1151
|
+
const {
|
|
1152
|
+
endStep,
|
|
1153
|
+
startStep,
|
|
1154
|
+
weight,
|
|
1155
|
+
guideImage,
|
|
1156
|
+
controlMode,
|
|
1157
|
+
startStepPercentage,
|
|
1158
|
+
endStepPercentage,
|
|
1159
|
+
model: controlNetModel
|
|
1160
|
+
} = controlData;
|
|
1161
|
+
const imageUploaded = guideImage ? await this.uploadImage(guideImage) : null;
|
|
1162
|
+
controlNetData.push(__spreadProps(__spreadValues(__spreadValues({
|
|
1163
|
+
guideImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
|
|
1164
|
+
model: controlNetModel,
|
|
1165
|
+
endStep,
|
|
1166
|
+
startStep,
|
|
1167
|
+
weight
|
|
1168
|
+
}, evaluateNonTrue({
|
|
1169
|
+
key: "startStepPercentage",
|
|
1170
|
+
value: startStepPercentage
|
|
1171
|
+
})), evaluateNonTrue({
|
|
1172
|
+
key: "endStepPercentage",
|
|
1173
|
+
value: endStepPercentage
|
|
1174
|
+
})), {
|
|
1175
|
+
controlMode: controlMode || "controlnet" /* CONTROL_NET */
|
|
1176
|
+
}));
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
requestObject = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadProps(__spreadValues(__spreadValues(__spreadValues({
|
|
1180
|
+
taskType: "imageInference" /* IMAGE_INFERENCE */,
|
|
1181
|
+
model,
|
|
1182
|
+
positivePrompt
|
|
1183
|
+
}, negativePrompt ? { negativePrompt } : {}), height ? { height } : {}), width ? { width } : {}), {
|
|
1184
|
+
numberResults
|
|
1185
|
+
}), (lora == null ? void 0 : lora.length) ? { lora } : {}), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}), uploadEndpoint ? { uploadEndpoint } : {}), evaluateNonTrue({ key: "checkNsfw", value: checkNsfw })), evaluateNonTrue({ key: "strength", value: strength })), evaluateNonTrue({ key: "CFGScale", value: CFGScale })), evaluateNonTrue({ key: "clipSkip", value: clipSkip })), evaluateNonTrue({
|
|
1186
|
+
key: "usePromptWeighting",
|
|
1187
|
+
value: usePromptWeighting
|
|
1188
|
+
})), evaluateNonTrue({ key: "steps", value: steps })), controlNetData.length ? { controlNet: controlNetData } : {}), seed ? { seed } : {}), scheduler ? { scheduler } : {}), evaluateNonTrue({ key: "includeCost", value: includeCost })), seedImageUUID ? { seedImage: seedImageUUID } : {}), maskImageUUID ? { maskImage: maskImageUUID } : {});
|
|
1189
|
+
return await asyncRetry(
|
|
1190
|
+
async () => {
|
|
1191
|
+
retryCount++;
|
|
1192
|
+
lis == null ? void 0 : lis.destroy();
|
|
1193
|
+
const imagesWithSimilarTask = this._globalImages.filter(
|
|
1194
|
+
(img) => taskUUIDs.includes(img.taskUUID)
|
|
1195
|
+
);
|
|
1196
|
+
const taskUUID = customTaskUUID || getUUID();
|
|
1197
|
+
taskUUIDs.push(taskUUID);
|
|
1198
|
+
const imageRemaining = numberResults - imagesWithSimilarTask.length;
|
|
1199
|
+
const newRequestObject = __spreadProps(__spreadValues({}, requestObject), {
|
|
1200
|
+
taskUUID,
|
|
1201
|
+
numberResults: imageRemaining
|
|
1202
|
+
});
|
|
1203
|
+
this.send(newRequestObject);
|
|
1204
|
+
lis = this.listenToImages({
|
|
1205
|
+
onPartialImages,
|
|
1206
|
+
taskUUID,
|
|
1207
|
+
groupKey: "REQUEST_IMAGES" /* REQUEST_IMAGES */,
|
|
1208
|
+
positivePrompt,
|
|
1209
|
+
negativePrompt
|
|
1210
|
+
});
|
|
1211
|
+
const promise = await this.getSimilarImages({
|
|
1212
|
+
taskUUID: taskUUIDs,
|
|
1213
|
+
numberResults,
|
|
1214
|
+
lis
|
|
1215
|
+
});
|
|
1216
|
+
lis.destroy();
|
|
1217
|
+
return promise;
|
|
1218
|
+
},
|
|
1219
|
+
{
|
|
1220
|
+
maxRetries: totalRetry,
|
|
1221
|
+
callback: () => {
|
|
1222
|
+
lis == null ? void 0 : lis.destroy();
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
);
|
|
1226
|
+
} catch (e) {
|
|
1227
|
+
if (e.taskUUID) {
|
|
1228
|
+
throw e;
|
|
1229
|
+
}
|
|
1230
|
+
if (retryCount >= totalRetry) {
|
|
1231
|
+
return this.handleIncompleteImages({ taskUUIDs, error: e });
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
}
|
|
1235
|
+
async ensureConnection() {
|
|
1236
|
+
var _a;
|
|
1237
|
+
let isConnected = this.connected();
|
|
1238
|
+
if (isConnected || this._url === BASE_RUNWARE_URLS.TEST)
|
|
1239
|
+
return;
|
|
1240
|
+
const retryInterval = 2e3;
|
|
1241
|
+
const pollingInterval = 200;
|
|
1242
|
+
try {
|
|
1243
|
+
if (this._invalidAPIkey)
|
|
1244
|
+
throw this._invalidAPIkey;
|
|
1245
|
+
return new Promise((resolve, reject) => {
|
|
1246
|
+
let retry = 0;
|
|
1247
|
+
const MAX_RETRY = 30;
|
|
1248
|
+
const SHOULD_RETRY = 30 / 2 === retry;
|
|
1249
|
+
let retryIntervalId;
|
|
1250
|
+
let pollingIntervalId;
|
|
1251
|
+
const clearAllIntervals = () => {
|
|
1252
|
+
clearInterval(retryIntervalId);
|
|
1253
|
+
clearInterval(pollingIntervalId);
|
|
1254
|
+
};
|
|
1255
|
+
if (this._sdkType === "SERVER" /* SERVER */) {
|
|
1256
|
+
retryIntervalId = setInterval(async () => {
|
|
1257
|
+
try {
|
|
1258
|
+
const hasConnected = this.connected();
|
|
1259
|
+
if (hasConnected) {
|
|
1260
|
+
clearAllIntervals();
|
|
1261
|
+
resolve(true);
|
|
1262
|
+
} else if (retry >= MAX_RETRY) {
|
|
1263
|
+
clearAllIntervals();
|
|
1264
|
+
reject(new Error("Retry timed out"));
|
|
1265
|
+
} else {
|
|
1266
|
+
if (SHOULD_RETRY) {
|
|
1267
|
+
this.connect();
|
|
1268
|
+
}
|
|
1269
|
+
retry++;
|
|
1270
|
+
}
|
|
1271
|
+
} catch (error) {
|
|
1272
|
+
clearAllIntervals();
|
|
1273
|
+
reject(error);
|
|
1274
|
+
}
|
|
1275
|
+
}, retryInterval);
|
|
1276
|
+
}
|
|
1277
|
+
pollingIntervalId = setInterval(async () => {
|
|
1278
|
+
const hasConnected = this.connected();
|
|
1279
|
+
if (hasConnected) {
|
|
1280
|
+
clearAllIntervals();
|
|
1281
|
+
resolve(true);
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
if (!!this._invalidAPIkey) {
|
|
1285
|
+
clearAllIntervals();
|
|
1286
|
+
reject(this._invalidAPIkey);
|
|
1287
|
+
return;
|
|
1288
|
+
}
|
|
1289
|
+
}, pollingInterval);
|
|
1290
|
+
});
|
|
1291
|
+
} catch (e) {
|
|
1292
|
+
throw (_a = this._invalidAPIkey) != null ? _a : "Could not connect to server. Ensure your API key is correct";
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
async getSimilarImages({
|
|
1296
|
+
taskUUID,
|
|
1297
|
+
numberResults,
|
|
1298
|
+
shouldThrowError,
|
|
1299
|
+
lis
|
|
1300
|
+
}) {
|
|
1301
|
+
return await getIntervalWithPromise(
|
|
1302
|
+
({ resolve, reject, intervalId }) => {
|
|
1303
|
+
const taskUUIDs = Array.isArray(taskUUID) ? taskUUID : [taskUUID];
|
|
1304
|
+
const imagesWithSimilarTask = this._globalImages.filter(
|
|
1305
|
+
(img) => taskUUIDs.includes(img.taskUUID)
|
|
1306
|
+
);
|
|
1307
|
+
if (this._globalError) {
|
|
1308
|
+
const newData = this._globalError;
|
|
1309
|
+
this._globalError = void 0;
|
|
1310
|
+
clearInterval(intervalId);
|
|
1311
|
+
reject == null ? void 0 : reject(newData);
|
|
1312
|
+
return true;
|
|
1313
|
+
} else if (imagesWithSimilarTask.length >= numberResults) {
|
|
1314
|
+
clearInterval(intervalId);
|
|
1315
|
+
this._globalImages = this._globalImages.filter(
|
|
1316
|
+
(img) => !taskUUIDs.includes(img.taskUUID)
|
|
1317
|
+
);
|
|
1318
|
+
resolve([...imagesWithSimilarTask].slice(0, numberResults));
|
|
1319
|
+
return true;
|
|
1320
|
+
}
|
|
1321
|
+
},
|
|
1322
|
+
{
|
|
1323
|
+
debugKey: "getting images",
|
|
1324
|
+
shouldThrowError,
|
|
1325
|
+
timeoutDuration: this._timeoutDuration
|
|
1326
|
+
}
|
|
1327
|
+
);
|
|
1328
|
+
}
|
|
1329
|
+
handleIncompleteImages({
|
|
1330
|
+
taskUUIDs,
|
|
1331
|
+
error
|
|
1332
|
+
}) {
|
|
1333
|
+
const imagesWithSimilarTask = this._globalImages.filter(
|
|
1334
|
+
(img) => taskUUIDs.includes(img.taskUUID)
|
|
1335
|
+
);
|
|
1336
|
+
if (imagesWithSimilarTask.length > 1) {
|
|
1337
|
+
this._globalImages = this._globalImages.filter(
|
|
1338
|
+
(img) => !taskUUIDs.includes(img.taskUUID)
|
|
1339
|
+
);
|
|
1340
|
+
return imagesWithSimilarTask;
|
|
1341
|
+
} else {
|
|
1342
|
+
throw error;
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
//end of data
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
// Runware/Runware-client.ts
|
|
1349
|
+
var import_reconnect = __toESM(require_reconnect(), 1);
|
|
1350
|
+
var RunwareClient = class extends RunwareBase {
|
|
1351
|
+
constructor(props) {
|
|
1352
|
+
const _a = props, { shouldReconnect } = _a, rest = __objRest(_a, ["shouldReconnect"]);
|
|
1353
|
+
super(rest);
|
|
1354
|
+
this._ws = new import_reconnect.default(
|
|
1355
|
+
this._url
|
|
1356
|
+
);
|
|
1357
|
+
this.connect();
|
|
1358
|
+
}
|
|
1359
|
+
};
|
|
1360
|
+
|
|
1361
|
+
// Runware/Runware-server.ts
|
|
1362
|
+
var import_ws = __toESM(require("ws"), 1);
|
|
1363
|
+
var RunwareServer = class extends RunwareBase {
|
|
1364
|
+
constructor(props) {
|
|
1365
|
+
super(props);
|
|
1366
|
+
this._instantiated = false;
|
|
1367
|
+
this._listeners = [];
|
|
1368
|
+
this._reconnectingIntervalId = null;
|
|
1369
|
+
this.send = (msg) => {
|
|
1370
|
+
this._ws.send(JSON.stringify([msg]));
|
|
1371
|
+
};
|
|
1372
|
+
this.resetConnection = () => {
|
|
1373
|
+
if (this._ws) {
|
|
1374
|
+
this._listeners.forEach((list) => {
|
|
1375
|
+
var _a;
|
|
1376
|
+
(_a = list == null ? void 0 : list.destroy) == null ? void 0 : _a.call(list);
|
|
1377
|
+
});
|
|
1378
|
+
this._ws.removeAllListeners();
|
|
1379
|
+
this._ws.terminate();
|
|
1380
|
+
this._ws.close();
|
|
1381
|
+
this._ws = null;
|
|
1382
|
+
this._listeners = [];
|
|
1383
|
+
}
|
|
1384
|
+
};
|
|
1385
|
+
this._sdkType = "SERVER" /* SERVER */;
|
|
1386
|
+
this.connect();
|
|
1387
|
+
}
|
|
1388
|
+
// protected addListener({
|
|
1389
|
+
// lis,
|
|
1390
|
+
// check,
|
|
1391
|
+
// groupKey,
|
|
1392
|
+
// }: {
|
|
1393
|
+
// lis: (v: any) => any;
|
|
1394
|
+
// check: (v: any) => any;
|
|
1395
|
+
// groupKey?: string;
|
|
1396
|
+
// }) {
|
|
1397
|
+
// const listener = (msg: any) => {
|
|
1398
|
+
// if (msg?.error) {
|
|
1399
|
+
// lis(msg);
|
|
1400
|
+
// } else if (check(msg)) {
|
|
1401
|
+
// lis(msg);
|
|
1402
|
+
// }
|
|
1403
|
+
// };
|
|
1404
|
+
// const groupListener = { key: getUUID(), listener, groupKey };
|
|
1405
|
+
// this._listeners.push(groupListener);
|
|
1406
|
+
// const destroy = () => {
|
|
1407
|
+
// this._listeners = removeListener(this._listeners, groupListener);
|
|
1408
|
+
// };
|
|
1409
|
+
// return {
|
|
1410
|
+
// destroy,
|
|
1411
|
+
// };
|
|
1412
|
+
// }
|
|
1413
|
+
async connect() {
|
|
1414
|
+
if (!this._url)
|
|
1415
|
+
return;
|
|
1416
|
+
this.resetConnection();
|
|
1417
|
+
this._ws = new import_ws.default(this._url, {
|
|
1418
|
+
perMessageDeflate: false
|
|
1419
|
+
});
|
|
1420
|
+
this._ws.on("error", () => {
|
|
1421
|
+
});
|
|
1422
|
+
this._ws.on("close", () => {
|
|
1423
|
+
this.handleClose();
|
|
1424
|
+
});
|
|
1425
|
+
this._ws.on("open", () => {
|
|
1426
|
+
if (this._reconnectingIntervalId) {
|
|
1427
|
+
clearInterval(this._reconnectingIntervalId);
|
|
1428
|
+
}
|
|
1429
|
+
if (this._connectionSessionUUID && this.isWebsocketReadyState()) {
|
|
1430
|
+
this.send({
|
|
1431
|
+
taskType: "authentication" /* AUTHENTICATION */,
|
|
1432
|
+
apiKey: this._apiKey,
|
|
1433
|
+
connectionSessionUUID: this._connectionSessionUUID
|
|
1434
|
+
});
|
|
1435
|
+
} else {
|
|
1436
|
+
if (this.isWebsocketReadyState()) {
|
|
1437
|
+
this.send({
|
|
1438
|
+
apiKey: this._apiKey,
|
|
1439
|
+
taskType: "authentication" /* AUTHENTICATION */
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
this.addListener({
|
|
1444
|
+
taskUUID: "authentication" /* AUTHENTICATION */,
|
|
1445
|
+
lis: (m) => {
|
|
1446
|
+
var _a, _b;
|
|
1447
|
+
if (m == null ? void 0 : m.error) {
|
|
1448
|
+
this._invalidAPIkey = m;
|
|
1449
|
+
return;
|
|
1450
|
+
}
|
|
1451
|
+
this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
|
|
1452
|
+
this._invalidAPIkey = void 0;
|
|
1453
|
+
}
|
|
1454
|
+
});
|
|
1455
|
+
});
|
|
1456
|
+
this._ws.on("message", (e, isBinary) => {
|
|
1457
|
+
const data = isBinary ? e : e == null ? void 0 : e.toString();
|
|
1458
|
+
if (!data)
|
|
1459
|
+
return;
|
|
1460
|
+
const m = JSON.parse(data);
|
|
1461
|
+
this._listeners.forEach((lis) => {
|
|
1462
|
+
const result = lis.listener(m);
|
|
1463
|
+
if (result) {
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
handleClose() {
|
|
1470
|
+
if (this._invalidAPIkey) {
|
|
1471
|
+
return;
|
|
1472
|
+
}
|
|
1473
|
+
if (this._reconnectingIntervalId) {
|
|
1474
|
+
clearInterval(this._reconnectingIntervalId);
|
|
1475
|
+
}
|
|
1476
|
+
if (this._shouldReconnect) {
|
|
1477
|
+
setTimeout(() => this.connect(), 1e3);
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
heartBeat() {
|
|
1481
|
+
clearTimeout(this._pingTimeout);
|
|
1482
|
+
this._pingTimeout = setTimeout(() => {
|
|
1483
|
+
if (this.isWebsocketReadyState()) {
|
|
1484
|
+
this.send({ ping: true });
|
|
1485
|
+
}
|
|
1486
|
+
}, 5e3);
|
|
1487
|
+
}
|
|
1488
|
+
//end of data
|
|
1489
|
+
};
|
|
1490
|
+
|
|
1491
|
+
// Runware/Runware.ts
|
|
1492
|
+
var Runware;
|
|
1493
|
+
if (typeof window === "undefined") {
|
|
1494
|
+
Runware = RunwareServer;
|
|
1495
|
+
} else {
|
|
1496
|
+
Runware = RunwareClient;
|
|
1497
|
+
}
|
|
1498
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1499
|
+
0 && (module.exports = {
|
|
1500
|
+
EControlMode,
|
|
1501
|
+
EOpenPosePreProcessor,
|
|
1502
|
+
EPreProcessor,
|
|
1503
|
+
EPreProcessorGroup,
|
|
1504
|
+
ETaskType,
|
|
1505
|
+
Environment,
|
|
1506
|
+
Runware,
|
|
1507
|
+
RunwareClient,
|
|
1508
|
+
RunwareServer,
|
|
1509
|
+
SdkType
|
|
1510
|
+
});
|