@runware/sdk-js 1.1.48-beta-vectorize → 1.1.48

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.
Files changed (46) hide show
  1. package/dist/esm/Runware/Runware-base.d.ts +70 -0
  2. package/dist/esm/Runware/Runware-base.js +1002 -0
  3. package/dist/esm/Runware/Runware-client.d.ts +5 -0
  4. package/dist/esm/Runware/Runware-client.js +28 -0
  5. package/dist/esm/Runware/Runware-server.d.ts +15 -0
  6. package/dist/esm/Runware/Runware-server.js +145 -0
  7. package/dist/esm/Runware/Runware.d.ts +4 -0
  8. package/dist/esm/Runware/Runware.js +13 -0
  9. package/dist/esm/Runware/async-retry.d.ts +5 -0
  10. package/dist/esm/Runware/async-retry.js +30 -0
  11. package/dist/esm/Runware/index.d.ts +4 -0
  12. package/dist/esm/Runware/index.js +20 -0
  13. package/dist/esm/Runware/reconnect.d.ts +11 -0
  14. package/dist/esm/Runware/reconnect.js +175 -0
  15. package/dist/esm/Runware/types.d.ts +644 -0
  16. package/dist/esm/Runware/types.js +156 -0
  17. package/dist/esm/Runware/utils.d.ts +57 -0
  18. package/dist/esm/Runware/utils.js +262 -0
  19. package/dist/esm/tests/Runware/enhance-prompt.test.d.ts +1 -0
  20. package/dist/esm/tests/Runware/enhance-prompt.test.js +58 -0
  21. package/dist/esm/tests/Runware/remove-image-background.test.d.ts +1 -0
  22. package/dist/esm/tests/Runware/remove-image-background.test.js +37 -0
  23. package/dist/esm/tests/Runware/request-image-to-text.test.d.ts +1 -0
  24. package/dist/esm/tests/Runware/request-image-to-text.test.js +37 -0
  25. package/dist/esm/tests/Runware/request-images.test.d.ts +1 -0
  26. package/dist/esm/tests/Runware/request-images.test.js +84 -0
  27. package/dist/esm/tests/Runware/runware-server.test.d.ts +1 -0
  28. package/dist/esm/tests/Runware/runware-server.test.js +26 -0
  29. package/dist/esm/tests/Runware/upload-image.test.d.ts +1 -0
  30. package/dist/esm/tests/Runware/upload-image.test.js +28 -0
  31. package/dist/esm/tests/Runware/upscale-gan.test.d.ts +1 -0
  32. package/dist/esm/tests/Runware/upscale-gan.test.js +41 -0
  33. package/dist/esm/tests/mockServer.d.ts +12 -0
  34. package/dist/esm/tests/mockServer.js +38 -0
  35. package/dist/esm/tests/script.d.ts +8 -0
  36. package/dist/esm/tests/script.js +570 -0
  37. package/dist/esm/tests/test-utils.d.ts +40 -0
  38. package/dist/esm/tests/test-utils.js +45 -0
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.cjs.map +1 -1
  41. package/dist/index.d.cts +3 -39
  42. package/dist/index.d.ts +3 -39
  43. package/dist/index.js +1 -1
  44. package/dist/index.js.map +1 -1
  45. package/package.json +1 -1
  46. package/readme.md +29 -10
@@ -0,0 +1,1002 @@
1
+ "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.RunwareBase = void 0;
15
+ // @ts-ignore
16
+ const async_retry_1 = require("./async-retry");
17
+ const types_1 = require("./types");
18
+ const utils_1 = require("./utils");
19
+ // let allImages: IImage[] = [];
20
+ class RunwareBase {
21
+ constructor({ apiKey, url = utils_1.BASE_RUNWARE_URLS.PRODUCTION, shouldReconnect = true, globalMaxRetries = 2, timeoutDuration = utils_1.TIMEOUT_DURATION, }) {
22
+ this._listeners = [];
23
+ // _globalMessages: any[] = [];
24
+ this._globalMessages = {};
25
+ this._globalImages = [];
26
+ this.ensureConnectionUUID = null;
27
+ this.isWebsocketReadyState = () => { var _a; return ((_a = this._ws) === null || _a === void 0 ? void 0 : _a.readyState) === 1; };
28
+ // protected addListener({
29
+ // lis,
30
+ // check,
31
+ // }: {
32
+ // lis: (v: any) => any;
33
+ // check: (v: any) => any;
34
+ // groupKey?: string;
35
+ // }): { destroy: Function } {
36
+ // this._ws.onmessage = (e: any) => {
37
+ // const m = JSON.parse(e.data);
38
+ // if (m?.error) {
39
+ // lis(m);
40
+ // } else if (check(m)) {
41
+ // lis(m);
42
+ // }
43
+ // };
44
+ // return
45
+ // destroy: () => {},
46
+ // };
47
+ // }
48
+ this.isInvalidAPIKey = () => {
49
+ var _a, _b;
50
+ return ((_b = (_a = this._connectionError) === null || _a === void 0 ? void 0 : _a.error) === null || _b === void 0 ? void 0 : _b.code) === "invalidApiKey";
51
+ };
52
+ // We moving to an array format, it make sense to consolidate all request to an array here
53
+ this.send = (msg) => {
54
+ this._ws.send(JSON.stringify([msg]));
55
+ };
56
+ this.uploadImage = async (file) => {
57
+ try {
58
+ return await (0, async_retry_1.asyncRetry)(async () => {
59
+ const taskUUID = (0, utils_1.getUUID)();
60
+ if (typeof file === "string" && (0, utils_1.isValidUUID)(file)) {
61
+ return {
62
+ imageURL: file,
63
+ imageUUID: file,
64
+ taskUUID,
65
+ taskType: types_1.ETaskType.IMAGE_UPLOAD,
66
+ };
67
+ }
68
+ const imageBase64 = typeof file === "string" ? file : await (0, utils_1.fileToBase64)(file);
69
+ return {
70
+ imageURL: imageBase64,
71
+ imageUUID: imageBase64,
72
+ taskUUID,
73
+ taskType: types_1.ETaskType.IMAGE_UPLOAD,
74
+ };
75
+ });
76
+ }
77
+ catch (e) {
78
+ throw e;
79
+ }
80
+ };
81
+ this.controlNetPreProcess = async ({ inputImage, preProcessorType, height, width, outputType, outputFormat, highThresholdCanny, lowThresholdCanny, includeHandsAndFaceOpenPose, includeCost, outputQuality, customTaskUUID, retry, includeGenerationTime, includePayload, }) => {
82
+ const totalRetry = retry || this._globalMaxRetries;
83
+ let lis = undefined;
84
+ const startTime = Date.now();
85
+ try {
86
+ return await (0, async_retry_1.asyncRetry)(async () => {
87
+ await this.ensureConnection();
88
+ const image = await this.uploadImage(inputImage);
89
+ if (!(image === null || image === void 0 ? void 0 : image.imageUUID))
90
+ return null;
91
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
92
+ const payload = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ inputImage: image.imageUUID, taskType: types_1.ETaskType.IMAGE_CONTROL_NET_PRE_PROCESS, taskUUID,
93
+ preProcessorType }, (0, utils_1.evaluateNonTrue)({ key: "height", value: height })), (0, utils_1.evaluateNonTrue)({ key: "width", value: width })), (0, utils_1.evaluateNonTrue)({ key: "outputType", value: outputType })), (0, utils_1.evaluateNonTrue)({ key: "outputFormat", value: outputFormat })), (0, utils_1.evaluateNonTrue)({ key: "includeCost", value: includeCost })), (0, utils_1.evaluateNonTrue)({
94
+ key: "highThresholdCanny",
95
+ value: highThresholdCanny,
96
+ })), (0, utils_1.evaluateNonTrue)({
97
+ key: "lowThresholdCanny",
98
+ value: lowThresholdCanny,
99
+ })), (0, utils_1.evaluateNonTrue)({
100
+ key: "includeHandsAndFaceOpenPose",
101
+ value: includeHandsAndFaceOpenPose,
102
+ })), (outputQuality ? { outputQuality } : {}));
103
+ this.send(Object.assign({}, payload));
104
+ lis = this.globalListener({
105
+ taskUUID,
106
+ });
107
+ const guideImage = (await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
108
+ const uploadedImage = this.getSingleMessage({
109
+ taskUUID,
110
+ });
111
+ if (!uploadedImage)
112
+ return;
113
+ if (uploadedImage === null || uploadedImage === void 0 ? void 0 : uploadedImage.error) {
114
+ reject(uploadedImage);
115
+ return true;
116
+ }
117
+ if (uploadedImage) {
118
+ // delete this._globalMessages[taskUUID];
119
+ resolve(uploadedImage);
120
+ return true;
121
+ }
122
+ }, {
123
+ debugKey: "unprocessed-image",
124
+ timeoutDuration: this._timeoutDuration,
125
+ }));
126
+ lis.destroy();
127
+ this.insertAdditionalResponse({
128
+ response: guideImage,
129
+ payload: includePayload ? payload : undefined,
130
+ startTime: includeGenerationTime ? startTime : undefined,
131
+ });
132
+ return guideImage;
133
+ }, {
134
+ maxRetries: totalRetry,
135
+ callback: () => {
136
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
137
+ },
138
+ });
139
+ }
140
+ catch (e) {
141
+ throw e;
142
+ }
143
+ };
144
+ this.requestImageToText = async ({ inputImage, includeCost, customTaskUUID, retry, includePayload, includeGenerationTime, }) => {
145
+ const totalRetry = retry || this._globalMaxRetries;
146
+ let lis = undefined;
147
+ const startTime = Date.now();
148
+ try {
149
+ return await (0, async_retry_1.asyncRetry)(async () => {
150
+ await this.ensureConnection();
151
+ const imageUploaded = inputImage
152
+ ? await this.uploadImage(inputImage)
153
+ : null;
154
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
155
+ const payload = Object.assign({ taskUUID, taskType: types_1.ETaskType.IMAGE_CAPTION, inputImage: imageUploaded === null || imageUploaded === void 0 ? void 0 : imageUploaded.imageUUID }, (0, utils_1.evaluateNonTrue)({ key: "includeCost", value: includeCost }));
156
+ this.send(payload);
157
+ lis = this.globalListener({
158
+ taskUUID,
159
+ });
160
+ const response = await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
161
+ const newReverseClip = this.getSingleMessage({
162
+ taskUUID,
163
+ });
164
+ if (!newReverseClip)
165
+ return;
166
+ if (newReverseClip === null || newReverseClip === void 0 ? void 0 : newReverseClip.error) {
167
+ reject(newReverseClip);
168
+ return true;
169
+ }
170
+ if (newReverseClip) {
171
+ delete this._globalMessages[taskUUID];
172
+ resolve(newReverseClip);
173
+ return true;
174
+ }
175
+ }, {
176
+ debugKey: "remove-image-background",
177
+ timeoutDuration: this._timeoutDuration,
178
+ });
179
+ lis.destroy();
180
+ this.insertAdditionalResponse({
181
+ response: response,
182
+ payload: includePayload ? payload : undefined,
183
+ startTime: includeGenerationTime ? startTime : undefined,
184
+ });
185
+ return response;
186
+ }, {
187
+ maxRetries: totalRetry,
188
+ callback: () => {
189
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
190
+ },
191
+ });
192
+ }
193
+ catch (e) {
194
+ throw e;
195
+ }
196
+ };
197
+ this.removeImageBackground = async (payload) => {
198
+ return this.baseSingleRequest({
199
+ payload: Object.assign(Object.assign({}, payload), { taskType: types_1.ETaskType.IMAGE_BACKGROUND_REMOVAL }),
200
+ debugKey: "remove-image-background",
201
+ });
202
+ };
203
+ this.removeBackground = async (payload) => {
204
+ var _a;
205
+ const { skipResponse } = payload, rest = __rest(payload, ["skipResponse"]);
206
+ try {
207
+ const deliveryMethod = rest.deliveryMethod;
208
+ const request = await this.baseSingleRequest({
209
+ payload: Object.assign(Object.assign({}, rest), { taskType: types_1.ETaskType.REMOVE_BACKGROUND }),
210
+ debugKey: "remove-background",
211
+ });
212
+ if (skipResponse) {
213
+ return request;
214
+ }
215
+ if (deliveryMethod === "async") {
216
+ const taskUUID = request === null || request === void 0 ? void 0 : request.taskUUID;
217
+ const numberResults = (_a = payload === null || payload === void 0 ? void 0 : payload.numberResults) !== null && _a !== void 0 ? _a : 1;
218
+ return this.pollForAsyncResults({
219
+ taskUUID,
220
+ numberResults,
221
+ });
222
+ }
223
+ // If not async, just return the initial result
224
+ return request;
225
+ }
226
+ catch (e) {
227
+ throw e;
228
+ }
229
+ };
230
+ this.videoInference = async (payload) => {
231
+ var _a;
232
+ const { skipResponse, inputAudios, referenceVideos } = payload, rest = __rest(payload, ["skipResponse", "inputAudios", "referenceVideos"]);
233
+ try {
234
+ const request = await this.baseSingleRequest({
235
+ payload: Object.assign(Object.assign(Object.assign(Object.assign({}, rest), ((inputAudios === null || inputAudios === void 0 ? void 0 : inputAudios.length) && { inputAudios })), ((referenceVideos === null || referenceVideos === void 0 ? void 0 : referenceVideos.length) && { referenceVideos })), { deliveryMethod: "async", taskType: types_1.ETaskType.VIDEO_INFERENCE }),
236
+ debugKey: "video-inference",
237
+ });
238
+ if (skipResponse) {
239
+ return request;
240
+ }
241
+ const taskUUID = request === null || request === void 0 ? void 0 : request.taskUUID;
242
+ const numberResults = (_a = payload === null || payload === void 0 ? void 0 : payload.numberResults) !== null && _a !== void 0 ? _a : 1;
243
+ return this.pollForAsyncResults({
244
+ taskUUID,
245
+ numberResults,
246
+ resultKey: "videoUUID"
247
+ });
248
+ }
249
+ catch (e) {
250
+ throw e;
251
+ }
252
+ };
253
+ this.getResponse = async (payload) => {
254
+ const taskUUID = payload.taskUUID;
255
+ // const mock = getRandomTaskResponses({ count: 2, taskUUID });
256
+ return this.baseSingleRequest({
257
+ payload: Object.assign(Object.assign({}, payload), { customTaskUUID: taskUUID, taskType: types_1.ETaskType.GET_RESPONSE }),
258
+ isMultiple: true,
259
+ debugKey: "async-results",
260
+ });
261
+ };
262
+ this.upscaleGan = async ({ inputImage, upscaleFactor, outputType, outputFormat, includeCost, outputQuality, customTaskUUID, retry, includeGenerationTime, includePayload, }) => {
263
+ const totalRetry = retry || this._globalMaxRetries;
264
+ let lis = undefined;
265
+ const startTime = Date.now();
266
+ try {
267
+ return await (0, async_retry_1.asyncRetry)(async () => {
268
+ await this.ensureConnection();
269
+ let imageUploaded;
270
+ imageUploaded = await this.uploadImage(inputImage);
271
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
272
+ const payload = Object.assign(Object.assign(Object.assign(Object.assign({ taskUUID, inputImage: imageUploaded === null || imageUploaded === void 0 ? void 0 : imageUploaded.imageUUID, taskType: types_1.ETaskType.IMAGE_UPSCALE, upscaleFactor }, (0, utils_1.evaluateNonTrue)({ key: "includeCost", value: includeCost })), (outputType ? { outputType } : {})), (outputQuality ? { outputQuality } : {})), (outputFormat ? { outputFormat } : {}));
273
+ this.send(payload);
274
+ lis = this.globalListener({
275
+ taskUUID,
276
+ });
277
+ const response = await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
278
+ const newUpscaleGan = this.getSingleMessage({ taskUUID });
279
+ if (!newUpscaleGan)
280
+ return;
281
+ if (newUpscaleGan === null || newUpscaleGan === void 0 ? void 0 : newUpscaleGan.error) {
282
+ reject(newUpscaleGan);
283
+ return true;
284
+ }
285
+ if (newUpscaleGan) {
286
+ delete this._globalMessages[taskUUID];
287
+ resolve(newUpscaleGan);
288
+ return true;
289
+ }
290
+ }, { debugKey: "upscale-gan", timeoutDuration: this._timeoutDuration });
291
+ lis.destroy();
292
+ this.insertAdditionalResponse({
293
+ response,
294
+ payload: includePayload ? payload : undefined,
295
+ startTime: includeGenerationTime ? startTime : undefined,
296
+ });
297
+ return response;
298
+ }, {
299
+ maxRetries: totalRetry,
300
+ callback: () => {
301
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
302
+ },
303
+ });
304
+ }
305
+ catch (e) {
306
+ throw e;
307
+ }
308
+ };
309
+ this.enhancePrompt = async ({ prompt, promptMaxLength = 380, promptVersions = 1, includeCost, customTaskUUID, retry, includeGenerationTime, includePayload, }) => {
310
+ const totalRetry = retry || this._globalMaxRetries;
311
+ let lis = undefined;
312
+ const startTime = Date.now();
313
+ try {
314
+ return await (0, async_retry_1.asyncRetry)(async () => {
315
+ await this.ensureConnection();
316
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
317
+ const payload = Object.assign(Object.assign({ prompt,
318
+ taskUUID,
319
+ promptMaxLength,
320
+ promptVersions }, (0, utils_1.evaluateNonTrue)({ key: "includeCost", value: includeCost })), { taskType: types_1.ETaskType.PROMPT_ENHANCE });
321
+ this.send(payload);
322
+ lis = this.globalListener({
323
+ taskUUID,
324
+ });
325
+ const response = await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
326
+ const reducedPrompt = this._globalMessages[taskUUID];
327
+ if (reducedPrompt === null || reducedPrompt === void 0 ? void 0 : reducedPrompt.error) {
328
+ reject(reducedPrompt);
329
+ return true;
330
+ }
331
+ if ((reducedPrompt === null || reducedPrompt === void 0 ? void 0 : reducedPrompt.length) >= promptVersions) {
332
+ delete this._globalMessages[taskUUID];
333
+ resolve(reducedPrompt);
334
+ return true;
335
+ }
336
+ }, {
337
+ debugKey: "enhance-prompt",
338
+ timeoutDuration: this._timeoutDuration,
339
+ });
340
+ lis.destroy();
341
+ this.insertAdditionalResponse({
342
+ response: response,
343
+ payload: includePayload ? payload : undefined,
344
+ startTime: includeGenerationTime ? startTime : undefined,
345
+ });
346
+ return response;
347
+ }, {
348
+ maxRetries: totalRetry,
349
+ callback: () => {
350
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
351
+ },
352
+ });
353
+ }
354
+ catch (e) {
355
+ throw e;
356
+ }
357
+ };
358
+ this.modelUpload = async (payload) => {
359
+ // This is written to destructure the payload from the additional parameters
360
+ const { onUploadStream, retry, customTaskUUID } = payload, addModelPayload = __rest(payload, ["onUploadStream", "retry", "customTaskUUID"]);
361
+ const totalRetry = retry || this._globalMaxRetries;
362
+ let lis = undefined;
363
+ try {
364
+ return await (0, async_retry_1.asyncRetry)(async () => {
365
+ await this.ensureConnection();
366
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
367
+ this.send(Object.assign(Object.assign({}, addModelPayload), { taskUUID, taskType: types_1.ETaskType.MODEL_UPLOAD }));
368
+ let result;
369
+ let errorResult;
370
+ lis = this.listenToUpload({
371
+ taskUUID,
372
+ onUploadStream: (response, error) => {
373
+ onUploadStream === null || onUploadStream === void 0 ? void 0 : onUploadStream(response, error);
374
+ if ((response === null || response === void 0 ? void 0 : response.status) === "ready") {
375
+ result = response;
376
+ }
377
+ else if (error) {
378
+ errorResult = error;
379
+ }
380
+ },
381
+ });
382
+ const modelUploadResponse = await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
383
+ if (result) {
384
+ resolve(result);
385
+ return true;
386
+ }
387
+ else if (errorResult) {
388
+ reject(errorResult);
389
+ return false;
390
+ }
391
+ }, {
392
+ shouldThrowError: false,
393
+ timeoutDuration: 60 * 60 * 1000,
394
+ });
395
+ return modelUploadResponse;
396
+ }, {
397
+ maxRetries: totalRetry,
398
+ callback: () => {
399
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
400
+ },
401
+ });
402
+ }
403
+ catch (e) {
404
+ throw e;
405
+ }
406
+ };
407
+ this.photoMaker = async (payload, moreOptions) => {
408
+ // This is written to destructure the payload from the additional parameters
409
+ const { onPartialImages, retry, customTaskUUID, numberResults, includeGenerationTime, includePayload } = payload, photoMakerPayload = __rest(payload, ["onPartialImages", "retry", "customTaskUUID", "numberResults", "includeGenerationTime", "includePayload"]);
410
+ const totalRetry = retry || this._globalMaxRetries;
411
+ let lis = undefined;
412
+ let taskUUIDs = [];
413
+ let retryCount = 0;
414
+ const startTime = Date.now();
415
+ try {
416
+ return await (0, async_retry_1.asyncRetry)(async () => {
417
+ await this.ensureConnection();
418
+ retryCount++;
419
+ const imagesWithSimilarTask = this._globalImages.filter((img) => taskUUIDs.includes(img.taskUUID));
420
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
421
+ taskUUIDs.push(taskUUID);
422
+ const imageRemaining = numberResults - imagesWithSimilarTask.length;
423
+ const payload = Object.assign(Object.assign(Object.assign(Object.assign({}, photoMakerPayload), (photoMakerPayload.seed
424
+ ? { seed: photoMakerPayload.seed }
425
+ : { seed: (0, utils_1.getRandomSeed)() })), (moreOptions !== null && moreOptions !== void 0 ? moreOptions : {})), { taskUUID, taskType: types_1.ETaskType.PHOTO_MAKER, numberResults });
426
+ this.send(Object.assign(Object.assign({}, payload), { numberResults: imageRemaining }));
427
+ lis = this.listenToImages({
428
+ onPartialImages,
429
+ taskUUID: taskUUID,
430
+ groupKey: utils_1.LISTEN_TO_IMAGES_KEY.REQUEST_IMAGES,
431
+ requestPayload: includePayload ? payload : undefined,
432
+ startTime: includeGenerationTime ? startTime : undefined,
433
+ });
434
+ const promise = await this.getSimilarImages({
435
+ taskUUID: taskUUIDs,
436
+ numberResults,
437
+ lis,
438
+ });
439
+ lis.destroy();
440
+ return promise;
441
+ }, {
442
+ maxRetries: totalRetry,
443
+ callback: () => {
444
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
445
+ },
446
+ });
447
+ }
448
+ catch (e) {
449
+ if (e.taskUUID) {
450
+ throw e;
451
+ }
452
+ if (retryCount >= totalRetry) {
453
+ return this.handleIncompleteImages({
454
+ taskUUIDs,
455
+ error: e,
456
+ });
457
+ }
458
+ }
459
+ };
460
+ this.modelSearch = async (payload) => {
461
+ return this.baseSingleRequest({
462
+ payload: Object.assign(Object.assign({}, payload), { taskType: types_1.ETaskType.MODEL_SEARCH }),
463
+ debugKey: "model-search",
464
+ });
465
+ };
466
+ this.imageMasking = async (payload) => {
467
+ return this.baseSingleRequest({
468
+ payload: Object.assign(Object.assign({}, payload), { taskType: types_1.ETaskType.IMAGE_MASKING }),
469
+ debugKey: "image-masking",
470
+ });
471
+ };
472
+ this.imageUpload = async (payload) => {
473
+ return this.baseSingleRequest({
474
+ payload: Object.assign(Object.assign({}, payload), { taskType: types_1.ETaskType.IMAGE_UPLOAD }),
475
+ debugKey: "image-upload",
476
+ });
477
+ };
478
+ this.baseSingleRequest = async ({ payload, debugKey, isMultiple, }) => {
479
+ const { retry, customTaskUUID, includePayload, includeGenerationTime } = payload, restPayload = __rest(payload, ["retry", "customTaskUUID", "includePayload", "includeGenerationTime"]);
480
+ const totalRetry = retry || this._globalMaxRetries;
481
+ let lis = undefined;
482
+ const startTime = Date.now();
483
+ try {
484
+ return await (0, async_retry_1.asyncRetry)(async () => {
485
+ await this.ensureConnection();
486
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
487
+ const payload = Object.assign(Object.assign({}, restPayload), { taskUUID });
488
+ this.send(payload);
489
+ lis = this.globalListener({
490
+ taskUUID,
491
+ });
492
+ const response = await (0, utils_1.getIntervalWithPromise)(({ resolve, reject }) => {
493
+ // console.log("multiple", isMultiple);
494
+ const response = isMultiple
495
+ ? this.getMultipleMessages({ taskUUID })
496
+ : this.getSingleMessage({ taskUUID });
497
+ if (!response)
498
+ return;
499
+ if (response === null || response === void 0 ? void 0 : response.error) {
500
+ reject(response);
501
+ return true;
502
+ }
503
+ if (response) {
504
+ delete this._globalMessages[taskUUID];
505
+ resolve(response);
506
+ return true;
507
+ }
508
+ }, {
509
+ debugKey,
510
+ timeoutDuration: this._timeoutDuration,
511
+ });
512
+ this.insertAdditionalResponse({
513
+ response: response,
514
+ payload: includePayload ? payload : undefined,
515
+ startTime: includeGenerationTime ? startTime : undefined,
516
+ });
517
+ lis.destroy();
518
+ return response;
519
+ }, {
520
+ maxRetries: totalRetry,
521
+ callback: () => {
522
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
523
+ },
524
+ });
525
+ }
526
+ catch (e) {
527
+ throw e;
528
+ }
529
+ };
530
+ this.getSingleMessage = ({ taskUUID }) => {
531
+ var _a;
532
+ const value = (_a = this._globalMessages[taskUUID]) === null || _a === void 0 ? void 0 : _a[0];
533
+ const errorValue = this._globalMessages[taskUUID];
534
+ if (!value && !errorValue)
535
+ return null;
536
+ return (errorValue === null || errorValue === void 0 ? void 0 : errorValue.error) ? errorValue : value;
537
+ };
538
+ this.getMultipleMessages = ({ taskUUID }) => {
539
+ var _a;
540
+ // console.log("global", this._globalMessages);
541
+ const value = (_a = this._globalMessages[taskUUID]) === null || _a === void 0 ? void 0 : _a[0];
542
+ const mainValue = this._globalMessages[taskUUID];
543
+ if (!value && !mainValue)
544
+ return null;
545
+ return mainValue;
546
+ };
547
+ this.insertAdditionalResponse = ({ response, payload, startTime, }) => {
548
+ if (!payload && !startTime)
549
+ return;
550
+ const res = response;
551
+ res.additionalResponse = {};
552
+ if (!!payload) {
553
+ response.additionalResponse.payload = payload;
554
+ }
555
+ if (!!startTime) {
556
+ response.additionalResponse.generationTime =
557
+ Date.now() - startTime;
558
+ }
559
+ };
560
+ this.disconnect = async () => {
561
+ var _a, _b, _c, _d;
562
+ this._shouldReconnect = false;
563
+ (_b = (_a = this._ws) === null || _a === void 0 ? void 0 : _a.terminate) === null || _b === void 0 ? void 0 : _b.call(_a);
564
+ (_d = (_c = this._ws) === null || _c === void 0 ? void 0 : _c.close) === null || _d === void 0 ? void 0 : _d.call(_c);
565
+ };
566
+ this.connected = () => this.isWebsocketReadyState() && !!this._connectionSessionUUID;
567
+ this._apiKey = apiKey;
568
+ this._url = url;
569
+ this._sdkType = types_1.SdkType.CLIENT;
570
+ this._shouldReconnect = shouldReconnect;
571
+ this._globalMaxRetries = globalMaxRetries;
572
+ this._timeoutDuration = timeoutDuration;
573
+ }
574
+ /**
575
+ * Shared polling logic for async results.
576
+ * @param taskUUID - The task UUID to poll for.
577
+ * @param numberResults - Number of results expected.
578
+ * @param resultKey - Key to identify results (default is "mediaUUID" - "videoUUID" is legacy).
579
+ * @returns Promise resolving to array of results.
580
+ */
581
+ async pollForAsyncResults({ taskUUID, numberResults, resultKey = "mediaUUID" }) {
582
+ const allResults = new Map();
583
+ await (0, utils_1.getIntervalAsyncWithPromise)(async ({ resolve, reject }) => {
584
+ try {
585
+ const results = await this.getResponse({ taskUUID });
586
+ // Add results to the collection
587
+ for (const result of results || []) {
588
+ const key = result[resultKey];
589
+ if (typeof key === "string") {
590
+ allResults.set(key, result);
591
+ }
592
+ }
593
+ // Check completion AFTER updating the collection
594
+ const isComplete = allResults.size === numberResults;
595
+ if (isComplete) {
596
+ resolve(Array.from(allResults.values()));
597
+ return true; // Signal to clear the interval
598
+ }
599
+ return false;
600
+ }
601
+ catch (err) {
602
+ reject(err);
603
+ return true;
604
+ }
605
+ }, {
606
+ debugKey: "async-response",
607
+ pollingInterval: 2 * 1000,
608
+ timeoutDuration: 10 * 60 * 1000,
609
+ });
610
+ return Array.from(allResults.values());
611
+ }
612
+ static async initialize(props) {
613
+ try {
614
+ const instance = new this(props);
615
+ await instance.ensureConnection();
616
+ return instance;
617
+ }
618
+ catch (e) {
619
+ throw e;
620
+ }
621
+ }
622
+ addListener({ lis,
623
+ // check,
624
+ groupKey, taskUUID, }) {
625
+ const listener = (msg) => {
626
+ var _a, _b, _c;
627
+ const arrayMessage = Array.isArray(msg === null || msg === void 0 ? void 0 : msg.data) ? msg.data : [msg.data];
628
+ const arrayErrors = ((_a = msg === null || msg === void 0 ? void 0 : msg[0]) === null || _a === void 0 ? void 0 : _a.errors)
629
+ ? (_b = msg === null || msg === void 0 ? void 0 : msg[0]) === null || _b === void 0 ? void 0 : _b.errors
630
+ : Array.isArray(msg === null || msg === void 0 ? void 0 : msg.errors)
631
+ ? msg.errors
632
+ : [msg.errors];
633
+ const filteredMessage = arrayMessage.filter((v) => ((v === null || v === void 0 ? void 0 : v.taskUUID) || (v === null || v === void 0 ? void 0 : v.taskType)) === taskUUID);
634
+ const filteredErrors = arrayErrors.filter((v) => ((v === null || v === void 0 ? void 0 : v.taskUUID) || (v === null || v === void 0 ? void 0 : v.taskType)) === taskUUID);
635
+ if (filteredErrors.length) {
636
+ lis({ error: Object.assign({}, ((_c = arrayErrors[0]) !== null && _c !== void 0 ? _c : {})) });
637
+ return;
638
+ }
639
+ if (filteredMessage.length) {
640
+ lis({ [taskUUID]: arrayMessage });
641
+ return;
642
+ }
643
+ };
644
+ const groupListener = { key: taskUUID || (0, utils_1.getUUID)(), listener, groupKey };
645
+ this._listeners.push(groupListener);
646
+ const destroy = () => {
647
+ this._listeners = (0, utils_1.removeListener)(this._listeners, groupListener);
648
+ };
649
+ return {
650
+ destroy,
651
+ };
652
+ }
653
+ connect() {
654
+ this._ws.onopen = (e) => {
655
+ if (this._connectionSessionUUID) {
656
+ this.send({
657
+ taskType: types_1.ETaskType.AUTHENTICATION,
658
+ apiKey: this._apiKey,
659
+ connectionSessionUUID: this._connectionSessionUUID,
660
+ });
661
+ }
662
+ else {
663
+ this.send({ apiKey: this._apiKey, taskType: types_1.ETaskType.AUTHENTICATION });
664
+ }
665
+ this.addListener({
666
+ taskUUID: types_1.ETaskType.AUTHENTICATION,
667
+ lis: (m) => {
668
+ var _a, _b;
669
+ if (m === null || m === void 0 ? void 0 : m.error) {
670
+ this._connectionError = m;
671
+ return;
672
+ }
673
+ this._connectionSessionUUID =
674
+ (_b = (_a = m === null || m === void 0 ? void 0 : m[types_1.ETaskType.AUTHENTICATION]) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.connectionSessionUUID;
675
+ this._connectionError = undefined;
676
+ },
677
+ });
678
+ };
679
+ this._ws.onmessage = (e) => {
680
+ var _a;
681
+ const data = JSON.parse(e.data);
682
+ for (const lis of this._listeners) {
683
+ const result = (_a = lis === null || lis === void 0 ? void 0 : lis.listener) === null || _a === void 0 ? void 0 : _a.call(lis, data);
684
+ if (result)
685
+ return;
686
+ }
687
+ };
688
+ this._ws.onclose = (e) => {
689
+ // console.log("closing");
690
+ // console.log("invalid", this._invalidAPIkey);
691
+ if (this.isInvalidAPIKey()) {
692
+ return;
693
+ }
694
+ };
695
+ }
696
+ destroy(lis) {
697
+ (0, utils_1.removeFromAray)(this._listeners, lis);
698
+ }
699
+ listenToImages({ onPartialImages, taskUUID, groupKey, requestPayload, startTime, }) {
700
+ return this.addListener({
701
+ taskUUID: taskUUID,
702
+ lis: (m) => {
703
+ var _a, _b;
704
+ let images = (_a = m === null || m === void 0 ? void 0 : m[taskUUID]) === null || _a === void 0 ? void 0 : _a.filter((img) => img.taskUUID === taskUUID);
705
+ if (m.error) {
706
+ onPartialImages === null || onPartialImages === void 0 ? void 0 : onPartialImages(images, (m === null || m === void 0 ? void 0 : m.error) && m);
707
+ this._globalError = m;
708
+ }
709
+ else {
710
+ images = images.map((image) => {
711
+ this.insertAdditionalResponse({
712
+ response: image,
713
+ payload: requestPayload ? requestPayload : undefined,
714
+ startTime: startTime ? startTime : undefined,
715
+ });
716
+ return Object.assign({}, image);
717
+ });
718
+ onPartialImages === null || onPartialImages === void 0 ? void 0 : onPartialImages(images, (m === null || m === void 0 ? void 0 : m.error) && m);
719
+ if (this._sdkType === types_1.SdkType.CLIENT) {
720
+ // this._globalImages = [...this._globalImages, ...m.images];
721
+ this._globalImages = [
722
+ ...this._globalImages,
723
+ ...((_b = m === null || m === void 0 ? void 0 : m[taskUUID]) !== null && _b !== void 0 ? _b : []).map((image) => {
724
+ this.insertAdditionalResponse({
725
+ response: image,
726
+ payload: requestPayload ? requestPayload : undefined,
727
+ startTime: startTime ? startTime : undefined,
728
+ });
729
+ return Object.assign({}, image);
730
+ }),
731
+ ];
732
+ }
733
+ else {
734
+ this._globalImages = [...this._globalImages, ...images];
735
+ }
736
+ }
737
+ },
738
+ groupKey,
739
+ });
740
+ }
741
+ listenToUpload({ onUploadStream, taskUUID, }) {
742
+ return this.addListener({
743
+ taskUUID: taskUUID,
744
+ lis: (m) => {
745
+ var _a;
746
+ const error = m === null || m === void 0 ? void 0 : m.error;
747
+ const result = (_a = m === null || m === void 0 ? void 0 : m[taskUUID]) === null || _a === void 0 ? void 0 : _a[0];
748
+ let response = (result === null || result === void 0 ? void 0 : result.taskUUID) === taskUUID ? result : null;
749
+ if (response || error) {
750
+ onUploadStream === null || onUploadStream === void 0 ? void 0 : onUploadStream(response || undefined, error);
751
+ }
752
+ },
753
+ });
754
+ }
755
+ globalListener({ taskUUID }) {
756
+ return this.addListener({
757
+ // check: (m) => {
758
+ // const value = accessDeepObject({
759
+ // key: responseKey,
760
+ // data: m,
761
+ // useZero: false,
762
+ // });
763
+ // return !!value;
764
+ // },
765
+ // check: responseKey,
766
+ taskUUID: taskUUID,
767
+ lis: (m) => {
768
+ if (m.error) {
769
+ this._globalMessages[taskUUID] = m;
770
+ return;
771
+ }
772
+ // console.log("m", m);
773
+ const value = (0, utils_1.accessDeepObject)({
774
+ key: taskUUID,
775
+ data: m,
776
+ useZero: false,
777
+ });
778
+ // console.log({ value });
779
+ if (Array.isArray(value)) {
780
+ value.forEach((v) => {
781
+ var _a;
782
+ this._globalMessages[v.taskUUID] = [
783
+ ...((_a = this._globalMessages[v.taskUUID]) !== null && _a !== void 0 ? _a : []),
784
+ v,
785
+ ];
786
+ });
787
+ }
788
+ else {
789
+ this._globalMessages[value.taskUUID] = value;
790
+ }
791
+ },
792
+ });
793
+ }
794
+ // includePayload?: boolean
795
+ // includeGenerationTime?: boolean
796
+ async requestImages(_a, moreOptions) {
797
+ var { outputType, outputFormat, uploadEndpoint, checkNSFW, positivePrompt, negativePrompt, seedImage, maskImage, strength, height, width, model, steps, scheduler, seed, CFGScale, clipSkip, usePromptWeighting, promptWeighting, numberResults = 1, onPartialImages, includeCost, customTaskUUID, retry, refiner, maskMargin, outputQuality, controlNet, lora, embeddings, ipAdapters, providerSettings, outpaint, acceleratorOptions, advancedFeatures, referenceImages, includeGenerationTime, includePayload } = _a, rest = __rest(_a, ["outputType", "outputFormat", "uploadEndpoint", "checkNSFW", "positivePrompt", "negativePrompt", "seedImage", "maskImage", "strength", "height", "width", "model", "steps", "scheduler", "seed", "CFGScale", "clipSkip", "usePromptWeighting", "promptWeighting", "numberResults", "onPartialImages", "includeCost", "customTaskUUID", "retry", "refiner", "maskMargin", "outputQuality", "controlNet", "lora", "embeddings", "ipAdapters", "providerSettings", "outpaint", "acceleratorOptions", "advancedFeatures", "referenceImages", "includeGenerationTime", "includePayload"]);
798
+ let lis = undefined;
799
+ let requestObject = undefined;
800
+ let taskUUIDs = [];
801
+ let retryCount = 0;
802
+ const totalRetry = retry || this._globalMaxRetries;
803
+ try {
804
+ await this.ensureConnection();
805
+ let seedImageUUID = null;
806
+ let maskImageUUID = null;
807
+ let controlNetData = [];
808
+ if (seedImage) {
809
+ const uploadedImage = await this.uploadImage(seedImage);
810
+ if (!uploadedImage)
811
+ return [];
812
+ seedImageUUID = uploadedImage.imageUUID;
813
+ }
814
+ if (maskImage) {
815
+ const uploadedMaskInitiator = await this.uploadImage(maskImage);
816
+ if (!uploadedMaskInitiator)
817
+ return [];
818
+ maskImageUUID = uploadedMaskInitiator.imageUUID;
819
+ }
820
+ if (controlNet === null || controlNet === void 0 ? void 0 : controlNet.length) {
821
+ for (let i = 0; i < controlNet.length; i++) {
822
+ const controlData = controlNet[i];
823
+ const { endStep, startStep, weight, guideImage, controlMode, startStepPercentage, endStepPercentage, model: controlNetModel, } = controlData;
824
+ const imageUploaded = guideImage
825
+ ? await this.uploadImage(guideImage)
826
+ : null;
827
+ controlNetData.push(Object.assign(Object.assign(Object.assign({ guideImage: imageUploaded === null || imageUploaded === void 0 ? void 0 : imageUploaded.imageUUID, model: controlNetModel, endStep,
828
+ startStep,
829
+ weight }, (0, utils_1.evaluateNonTrue)({
830
+ key: "startStepPercentage",
831
+ value: startStepPercentage,
832
+ })), (0, utils_1.evaluateNonTrue)({
833
+ key: "endStepPercentage",
834
+ value: endStepPercentage,
835
+ })), { controlMode: controlMode || types_1.EControlMode.CONTROL_NET }));
836
+ }
837
+ }
838
+ requestObject = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({ taskType: types_1.ETaskType.IMAGE_INFERENCE, model, positivePrompt: positivePrompt }, (negativePrompt ? { negativePrompt } : {})), (height ? { height } : {})), (width ? { width } : {})), { numberResults }), (outputType ? { outputType } : {})), (outputFormat ? { outputFormat } : {})), (uploadEndpoint ? { uploadEndpoint } : {})), (0, utils_1.evaluateNonTrue)({ key: "checkNSFW", value: checkNSFW })), (0, utils_1.evaluateNonTrue)({ key: "strength", value: strength })), (0, utils_1.evaluateNonTrue)({ key: "CFGScale", value: CFGScale })), (0, utils_1.evaluateNonTrue)({ key: "clipSkip", value: clipSkip })), (0, utils_1.evaluateNonTrue)({ key: "maskMargin", value: maskMargin })), (0, utils_1.evaluateNonTrue)({
839
+ key: "usePromptWeighting",
840
+ value: usePromptWeighting,
841
+ })), (0, utils_1.evaluateNonTrue)({ key: "steps", value: steps })), (promptWeighting ? { promptWeighting } : {})), (seed ? { seed: seed } : {})), (scheduler ? { scheduler } : {})), (refiner ? { refiner } : {})), (outpaint ? { outpaint } : {})), (0, utils_1.evaluateNonTrue)({ key: "includeCost", value: includeCost })), (seedImageUUID ? { seedImage: seedImageUUID } : {})), (maskImageUUID ? { maskImage: maskImageUUID } : {})), (outputQuality ? { outputQuality } : {})), (controlNetData.length ? { controlNet: controlNetData } : {})), ((lora === null || lora === void 0 ? void 0 : lora.length) ? { lora: lora } : {})), ((embeddings === null || embeddings === void 0 ? void 0 : embeddings.length) ? { embeddings } : {})), ((ipAdapters === null || ipAdapters === void 0 ? void 0 : ipAdapters.length) ? { ipAdapters } : {})), (providerSettings ? { providerSettings } : {})), (acceleratorOptions ? { acceleratorOptions } : {})), (advancedFeatures ? { advancedFeatures } : {})), ((referenceImages === null || referenceImages === void 0 ? void 0 : referenceImages.length) ? { referenceImages } : {})), rest), (moreOptions !== null && moreOptions !== void 0 ? moreOptions : {}));
842
+ const startTime = Date.now();
843
+ return await (0, async_retry_1.asyncRetry)(async () => {
844
+ retryCount++;
845
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
846
+ const imagesWithSimilarTask = this._globalImages.filter((img) => taskUUIDs.includes(img.taskUUID));
847
+ const taskUUID = customTaskUUID || (0, utils_1.getUUID)();
848
+ taskUUIDs.push(taskUUID);
849
+ const imageRemaining = numberResults - imagesWithSimilarTask.length;
850
+ const newRequestObject = Object.assign(Object.assign({}, requestObject), { taskUUID: taskUUID, numberResults: imageRemaining });
851
+ this.send(newRequestObject);
852
+ // const generationTime = endTime - startTime;
853
+ lis = this.listenToImages({
854
+ onPartialImages,
855
+ taskUUID: taskUUID,
856
+ groupKey: utils_1.LISTEN_TO_IMAGES_KEY.REQUEST_IMAGES,
857
+ requestPayload: includePayload ? newRequestObject : undefined,
858
+ startTime: includeGenerationTime ? startTime : undefined,
859
+ });
860
+ const promise = await this.getSimilarImages({
861
+ taskUUID: taskUUIDs,
862
+ numberResults,
863
+ lis,
864
+ });
865
+ lis.destroy();
866
+ return promise;
867
+ }, {
868
+ maxRetries: totalRetry,
869
+ callback: () => {
870
+ lis === null || lis === void 0 ? void 0 : lis.destroy();
871
+ },
872
+ });
873
+ }
874
+ catch (e) {
875
+ if (retryCount >= totalRetry) {
876
+ return this.handleIncompleteImages({ taskUUIDs, error: e });
877
+ }
878
+ throw e;
879
+ }
880
+ }
881
+ async ensureConnection() {
882
+ var _a;
883
+ let isConnected = this.connected();
884
+ if (isConnected || this._url === utils_1.BASE_RUNWARE_URLS.TEST)
885
+ return;
886
+ const retryInterval = 2000;
887
+ const pollingInterval = 200;
888
+ // const pollingInterval = this._sdkType === SdkType.CLIENT ? 200 : 2000;
889
+ try {
890
+ if (this.isInvalidAPIKey()) {
891
+ throw this._connectionError;
892
+ }
893
+ return new Promise((resolve, reject) => {
894
+ // const isConnected =
895
+ let retry = 0;
896
+ const MAX_RETRY = 30;
897
+ const localConnectionUUID = (0, utils_1.getUUID)();
898
+ let retryIntervalId;
899
+ let pollingIntervalId;
900
+ const clearAllIntervals = () => {
901
+ this.ensureConnectionUUID = null;
902
+ clearInterval(retryIntervalId);
903
+ clearInterval(pollingIntervalId);
904
+ };
905
+ if (this._sdkType === types_1.SdkType.SERVER) {
906
+ retryIntervalId = setInterval(async () => {
907
+ try {
908
+ const hasConnected = this.connected();
909
+ // only one instance should be responsible for making the call again, not other ensureConnection
910
+ let shouldCallServer = false;
911
+ if (!this.ensureConnectionUUID ||
912
+ localConnectionUUID === this.ensureConnectionUUID) {
913
+ if (!this.ensureConnectionUUID) {
914
+ this.ensureConnectionUUID = localConnectionUUID;
915
+ }
916
+ shouldCallServer = true;
917
+ }
918
+ // Retry every (retryInterval % retry) => 60s
919
+ // every 20 seconds (ie. => retry is 10 (20s), retry is 20 (40s))
920
+ const SHOULD_RETRY = retry % 10 === 0 && shouldCallServer;
921
+ if (hasConnected) {
922
+ clearAllIntervals();
923
+ resolve(true);
924
+ }
925
+ else if (retry >= MAX_RETRY) {
926
+ clearAllIntervals();
927
+ reject(new Error("Retry timed out"));
928
+ }
929
+ else {
930
+ if (SHOULD_RETRY) {
931
+ this.connect();
932
+ }
933
+ retry++;
934
+ }
935
+ }
936
+ catch (error) {
937
+ clearAllIntervals();
938
+ reject(error);
939
+ }
940
+ }, retryInterval);
941
+ }
942
+ pollingIntervalId = setInterval(async () => {
943
+ const hasConnected = this.connected();
944
+ if (hasConnected) {
945
+ clearAllIntervals();
946
+ resolve(true);
947
+ return;
948
+ }
949
+ if (!!this.isInvalidAPIKey()) {
950
+ clearAllIntervals();
951
+ reject(this._connectionError);
952
+ return;
953
+ }
954
+ }, pollingInterval);
955
+ });
956
+ }
957
+ catch (e) {
958
+ this.ensureConnectionUUID = null;
959
+ this._connectionError = undefined;
960
+ throw ((_a = this._connectionError) !== null && _a !== void 0 ? _a : "Could not connect to server. Ensure your API key is correct");
961
+ }
962
+ }
963
+ async getSimilarImages({ taskUUID, numberResults, shouldThrowError, lis, }) {
964
+ return (await (0, utils_1.getIntervalWithPromise)(({ resolve, reject, intervalId }) => {
965
+ var _a;
966
+ const taskUUIDs = Array.isArray(taskUUID) ? taskUUID : [taskUUID];
967
+ const imagesWithSimilarTask = this._globalImages.filter((img) => taskUUIDs.includes(img.taskUUID));
968
+ if (this._globalError) {
969
+ const newData = this._globalError;
970
+ this._globalError = undefined;
971
+ // throw errorData[0]
972
+ clearInterval(intervalId);
973
+ (_a = (reject)) === null || _a === void 0 ? void 0 : _a(newData);
974
+ return true;
975
+ }
976
+ // onPartialImages?.(imagesWithSimilarTask)
977
+ else if (imagesWithSimilarTask.length >= numberResults) {
978
+ // lis?.destroy();
979
+ clearInterval(intervalId);
980
+ this._globalImages = this._globalImages.filter((img) => !taskUUIDs.includes(img.taskUUID));
981
+ resolve([...imagesWithSimilarTask].slice(0, numberResults));
982
+ return true;
983
+ // Resolve the promise with the data
984
+ }
985
+ }, {
986
+ debugKey: "getting images",
987
+ shouldThrowError,
988
+ timeoutDuration: this._timeoutDuration,
989
+ }));
990
+ }
991
+ handleIncompleteImages({ taskUUIDs, error, }) {
992
+ const imagesWithSimilarTask = this._globalImages.filter((img) => taskUUIDs.includes(img.taskUUID));
993
+ if (imagesWithSimilarTask.length > 1) {
994
+ this._globalImages = this._globalImages.filter((img) => !taskUUIDs.includes(img.taskUUID));
995
+ return imagesWithSimilarTask;
996
+ }
997
+ else {
998
+ throw error;
999
+ }
1000
+ }
1001
+ }
1002
+ exports.RunwareBase = RunwareBase;