@runware/sdk-js 1.1.17 → 1.1.18

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.mjs CHANGED
@@ -21,6 +21,18 @@ var __spreadValues = (a, b) => {
21
21
  return a;
22
22
  };
23
23
  var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __objRest = (source, exclude) => {
25
+ var target = {};
26
+ for (var prop in source)
27
+ if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
28
+ target[prop] = source[prop];
29
+ if (source != null && __getOwnPropSymbols)
30
+ for (var prop of __getOwnPropSymbols(source)) {
31
+ if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
32
+ target[prop] = source[prop];
33
+ }
34
+ return target;
35
+ };
24
36
  var __commonJS = (cb, mod) => function __require() {
25
37
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
26
38
  };
@@ -162,9 +174,9 @@ var require_reconnect = __commonJS({
162
174
  };
163
175
  log("init");
164
176
  connect();
165
- this.close = (code = 1e3, reason = "", { keepClosed = false, fastClose = true, delay: delay2 = 0 } = {}) => {
166
- if (delay2) {
167
- reconnectDelay = delay2;
177
+ this.close = (code = 1e3, reason = "", { keepClosed = false, fastClose = true, delay: delay3 = 0 } = {}) => {
178
+ if (delay3) {
179
+ reconnectDelay = delay3;
168
180
  }
169
181
  shouldRetry = !keepClosed;
170
182
  ws.close(code, reason);
@@ -240,50 +252,50 @@ var EControlMode = /* @__PURE__ */ ((EControlMode2) => {
240
252
  EControlMode2["CONTROL_NET"] = "controlnet";
241
253
  return EControlMode2;
242
254
  })(EControlMode || {});
243
- var EPreProcessorGroup = /* @__PURE__ */ ((EPreProcessorGroup3) => {
244
- EPreProcessorGroup3["canny"] = "canny";
245
- EPreProcessorGroup3["depth"] = "depth";
246
- EPreProcessorGroup3["mlsd"] = "mlsd";
247
- EPreProcessorGroup3["normalbae"] = "normalbae";
248
- EPreProcessorGroup3["openpose"] = "openpose";
249
- EPreProcessorGroup3["tile"] = "tile";
250
- EPreProcessorGroup3["seg"] = "seg";
251
- EPreProcessorGroup3["lineart"] = "lineart";
252
- EPreProcessorGroup3["lineart_anime"] = "lineart_anime";
253
- EPreProcessorGroup3["shuffle"] = "shuffle";
254
- EPreProcessorGroup3["scribble"] = "scribble";
255
- EPreProcessorGroup3["softedge"] = "softedge";
256
- return EPreProcessorGroup3;
255
+ var EPreProcessorGroup = /* @__PURE__ */ ((EPreProcessorGroup2) => {
256
+ EPreProcessorGroup2["canny"] = "canny";
257
+ EPreProcessorGroup2["depth"] = "depth";
258
+ EPreProcessorGroup2["mlsd"] = "mlsd";
259
+ EPreProcessorGroup2["normalbae"] = "normalbae";
260
+ EPreProcessorGroup2["openpose"] = "openpose";
261
+ EPreProcessorGroup2["tile"] = "tile";
262
+ EPreProcessorGroup2["seg"] = "seg";
263
+ EPreProcessorGroup2["lineart"] = "lineart";
264
+ EPreProcessorGroup2["lineart_anime"] = "lineart_anime";
265
+ EPreProcessorGroup2["shuffle"] = "shuffle";
266
+ EPreProcessorGroup2["scribble"] = "scribble";
267
+ EPreProcessorGroup2["softedge"] = "softedge";
268
+ return EPreProcessorGroup2;
257
269
  })(EPreProcessorGroup || {});
258
- var EPreProcessor = /* @__PURE__ */ ((EPreProcessor3) => {
259
- EPreProcessor3["canny"] = "canny";
260
- EPreProcessor3["depth_leres"] = "depth_leres";
261
- EPreProcessor3["depth_midas"] = "depth_midas";
262
- EPreProcessor3["depth_zoe"] = "depth_zoe";
263
- EPreProcessor3["inpaint_global_harmonious"] = "inpaint_global_harmonious";
264
- EPreProcessor3["lineart_anime"] = "lineart_anime";
265
- EPreProcessor3["lineart_coarse"] = "lineart_coarse";
266
- EPreProcessor3["lineart_realistic"] = "lineart_realistic";
267
- EPreProcessor3["lineart_standard"] = "lineart_standard";
268
- EPreProcessor3["mlsd"] = "mlsd";
269
- EPreProcessor3["normal_bae"] = "normal_bae";
270
- EPreProcessor3["scribble_hed"] = "scribble_hed";
271
- EPreProcessor3["scribble_pidinet"] = "scribble_pidinet";
272
- EPreProcessor3["seg_ofade20k"] = "seg_ofade20k";
273
- EPreProcessor3["seg_ofcoco"] = "seg_ofcoco";
274
- EPreProcessor3["seg_ufade20k"] = "seg_ufade20k";
275
- EPreProcessor3["shuffle"] = "shuffle";
276
- EPreProcessor3["softedge_hed"] = "softedge_hed";
277
- EPreProcessor3["softedge_hedsafe"] = "softedge_hedsafe";
278
- EPreProcessor3["softedge_pidinet"] = "softedge_pidinet";
279
- EPreProcessor3["softedge_pidisafe"] = "softedge_pidisafe";
280
- EPreProcessor3["tile_gaussian"] = "tile_gaussian";
281
- EPreProcessor3["openpose"] = "openpose";
282
- EPreProcessor3["openpose_face"] = "openpose_face";
283
- EPreProcessor3["openpose_faceonly"] = "openpose_faceonly";
284
- EPreProcessor3["openpose_full"] = "openpose_full";
285
- EPreProcessor3["openpose_hand"] = "openpose_hand";
286
- return EPreProcessor3;
270
+ var EPreProcessor = /* @__PURE__ */ ((EPreProcessor2) => {
271
+ EPreProcessor2["canny"] = "canny";
272
+ EPreProcessor2["depth_leres"] = "depth_leres";
273
+ EPreProcessor2["depth_midas"] = "depth_midas";
274
+ EPreProcessor2["depth_zoe"] = "depth_zoe";
275
+ EPreProcessor2["inpaint_global_harmonious"] = "inpaint_global_harmonious";
276
+ EPreProcessor2["lineart_anime"] = "lineart_anime";
277
+ EPreProcessor2["lineart_coarse"] = "lineart_coarse";
278
+ EPreProcessor2["lineart_realistic"] = "lineart_realistic";
279
+ EPreProcessor2["lineart_standard"] = "lineart_standard";
280
+ EPreProcessor2["mlsd"] = "mlsd";
281
+ EPreProcessor2["normal_bae"] = "normal_bae";
282
+ EPreProcessor2["scribble_hed"] = "scribble_hed";
283
+ EPreProcessor2["scribble_pidinet"] = "scribble_pidinet";
284
+ EPreProcessor2["seg_ofade20k"] = "seg_ofade20k";
285
+ EPreProcessor2["seg_ofcoco"] = "seg_ofcoco";
286
+ EPreProcessor2["seg_ufade20k"] = "seg_ufade20k";
287
+ EPreProcessor2["shuffle"] = "shuffle";
288
+ EPreProcessor2["softedge_hed"] = "softedge_hed";
289
+ EPreProcessor2["softedge_hedsafe"] = "softedge_hedsafe";
290
+ EPreProcessor2["softedge_pidinet"] = "softedge_pidinet";
291
+ EPreProcessor2["softedge_pidisafe"] = "softedge_pidisafe";
292
+ EPreProcessor2["tile_gaussian"] = "tile_gaussian";
293
+ EPreProcessor2["openpose"] = "openpose";
294
+ EPreProcessor2["openpose_face"] = "openpose_face";
295
+ EPreProcessor2["openpose_faceonly"] = "openpose_faceonly";
296
+ EPreProcessor2["openpose_full"] = "openpose_full";
297
+ EPreProcessor2["openpose_hand"] = "openpose_hand";
298
+ return EPreProcessor2;
287
299
  })(EPreProcessor || {});
288
300
  var EOpenPosePreProcessor = /* @__PURE__ */ ((EOpenPosePreProcessor2) => {
289
301
  EOpenPosePreProcessor2["openpose"] = "openpose";
@@ -297,6 +309,7 @@ var EOpenPosePreProcessor = /* @__PURE__ */ ((EOpenPosePreProcessor2) => {
297
309
  // Runware/utils.ts
298
310
  import { v4 as uuidv4, validate as validateUUID } from "uuid";
299
311
  var TIMEOUT_DURATION = 6e4;
312
+ var MINIMUM_TIMEOUT_DURATION = 1e3;
300
313
  var POLLING_INTERVAL = 100;
301
314
  var BASE_RUNWARE_URLS = {
302
315
  ["PRODUCTION" /* PRODUCTION */]: "wss://ws-api.runware.ai/v1",
@@ -314,20 +327,20 @@ var removeFromAray = (col, targetElem) => {
314
327
  };
315
328
  var getIntervalWithPromise = (callback, {
316
329
  debugKey = "debugKey",
317
- timeOutDuration = TIMEOUT_DURATION,
330
+ timeoutDuration = TIMEOUT_DURATION,
318
331
  shouldThrowError = true
319
332
  }) => {
333
+ timeoutDuration = timeoutDuration < MINIMUM_TIMEOUT_DURATION ? MINIMUM_TIMEOUT_DURATION : timeoutDuration;
320
334
  return new Promise((resolve, reject) => {
321
335
  const timeoutId = setTimeout(() => {
322
336
  if (intervalId) {
323
337
  clearInterval(intervalId);
324
338
  if (shouldThrowError) {
325
- reject(`Message could not be received for ${debugKey}`);
326
- console.error("Message could not be received for ", debugKey);
339
+ reject(`Response could not be received from server for ${debugKey}`);
327
340
  }
328
341
  }
329
342
  clearTimeout(timeoutId);
330
- }, timeOutDuration);
343
+ }, timeoutDuration);
331
344
  let intervalId = setInterval(async () => {
332
345
  const shouldClear = callback({ resolve, reject, intervalId });
333
346
  if (shouldClear) {
@@ -417,7 +430,9 @@ var RunwareBase = class {
417
430
  constructor({
418
431
  apiKey,
419
432
  url = BASE_RUNWARE_URLS.PRODUCTION,
420
- shouldReconnect = true
433
+ shouldReconnect = true,
434
+ globalMaxRetries = 2,
435
+ timeoutDuration = TIMEOUT_DURATION
421
436
  }) {
422
437
  this._listeners = [];
423
438
  // _globalMessages: any[] = [];
@@ -466,51 +481,68 @@ var RunwareBase = class {
466
481
  lowThresholdCanny,
467
482
  includeHandsAndFaceOpenPose,
468
483
  includeCost,
469
- customTaskUUID
484
+ customTaskUUID,
485
+ retry
470
486
  }) => {
487
+ const totalRetry = retry || this._globalMaxRetries;
488
+ let lis = void 0;
471
489
  try {
472
- const image = await this.uploadImage(inputImage);
473
- if (!(image == null ? void 0 : image.imageUUID))
474
- return null;
475
- const taskUUID = customTaskUUID || getUUID();
476
- this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
477
- inputImage: image.imageUUID,
478
- taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
479
- taskUUID,
480
- preProcessor
481
- }, 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({
482
- key: "highThresholdCanny",
483
- value: highThresholdCanny
484
- })), evaluateNonTrue({
485
- key: "lowThresholdCanny",
486
- value: lowThresholdCanny
487
- })), evaluateNonTrue({
488
- key: "includeHandsAndFaceOpenPose",
489
- value: includeHandsAndFaceOpenPose
490
- })));
491
- const lis = this.globalListener({
492
- taskUUID
493
- });
494
- const guideImage = await getIntervalWithPromise(
495
- ({ resolve, reject }) => {
496
- const uploadedImage = this.getSingleMessage({
490
+ return await asyncRetry(
491
+ async () => {
492
+ await this.ensureConnection();
493
+ const image = await this.uploadImage(inputImage);
494
+ if (!(image == null ? void 0 : image.imageUUID))
495
+ return null;
496
+ const taskUUID = customTaskUUID || getUUID();
497
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
498
+ inputImage: image.imageUUID,
499
+ taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
500
+ taskUUID,
501
+ preProcessor
502
+ }, 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({
503
+ key: "highThresholdCanny",
504
+ value: highThresholdCanny
505
+ })), evaluateNonTrue({
506
+ key: "lowThresholdCanny",
507
+ value: lowThresholdCanny
508
+ })), evaluateNonTrue({
509
+ key: "includeHandsAndFaceOpenPose",
510
+ value: includeHandsAndFaceOpenPose
511
+ })));
512
+ lis = this.globalListener({
497
513
  taskUUID
498
514
  });
499
- if (!uploadedImage)
500
- return;
501
- if (uploadedImage == null ? void 0 : uploadedImage.error) {
502
- reject(uploadedImage);
503
- return true;
504
- }
505
- if (uploadedImage) {
506
- resolve(uploadedImage);
507
- return true;
508
- }
515
+ const guideImage = await getIntervalWithPromise(
516
+ ({ resolve, reject }) => {
517
+ const uploadedImage = this.getSingleMessage({
518
+ taskUUID
519
+ });
520
+ if (!uploadedImage)
521
+ return;
522
+ if (uploadedImage == null ? void 0 : uploadedImage.error) {
523
+ reject(uploadedImage);
524
+ return true;
525
+ }
526
+ if (uploadedImage) {
527
+ resolve(uploadedImage);
528
+ return true;
529
+ }
530
+ },
531
+ {
532
+ debugKey: "unprocessed-image",
533
+ timeoutDuration: this._timeoutDuration
534
+ }
535
+ );
536
+ lis.destroy();
537
+ return guideImage;
509
538
  },
510
- { debugKey: "unprocessed-image" }
539
+ {
540
+ maxRetries: totalRetry,
541
+ callback: () => {
542
+ lis == null ? void 0 : lis.destroy();
543
+ }
544
+ }
511
545
  );
512
- lis.destroy();
513
- return guideImage;
514
546
  } catch (e) {
515
547
  throw e;
516
548
  }
@@ -518,43 +550,57 @@ var RunwareBase = class {
518
550
  this.requestImageToText = async ({
519
551
  inputImage,
520
552
  includeCost,
521
- customTaskUUID
553
+ customTaskUUID,
554
+ retry
522
555
  }) => {
556
+ const totalRetry = retry || this._globalMaxRetries;
557
+ let lis = void 0;
523
558
  try {
524
- await this.ensureConnection();
525
- return await asyncRetry(async () => {
526
- const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
527
- const taskUUID = customTaskUUID || getUUID();
528
- this.send(__spreadValues({
529
- taskUUID,
530
- taskType: "imageCaption" /* IMAGE_CAPTION */,
531
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
532
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })));
533
- const lis = this.globalListener({
534
- taskUUID
535
- });
536
- const response = await getIntervalWithPromise(
537
- ({ resolve, reject }) => {
538
- const newReverseClip = this.getSingleMessage({
539
- taskUUID
540
- });
541
- if (!newReverseClip)
542
- return;
543
- if (newReverseClip == null ? void 0 : newReverseClip.error) {
544
- reject(newReverseClip);
545
- return true;
546
- }
547
- if (newReverseClip) {
548
- delete this._globalMessages[taskUUID];
549
- resolve(newReverseClip);
550
- return true;
559
+ return await asyncRetry(
560
+ async () => {
561
+ await this.ensureConnection();
562
+ const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
563
+ const taskUUID = customTaskUUID || getUUID();
564
+ this.send(__spreadValues({
565
+ taskUUID,
566
+ taskType: "imageCaption" /* IMAGE_CAPTION */,
567
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
568
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })));
569
+ lis = this.globalListener({
570
+ taskUUID
571
+ });
572
+ const response = await getIntervalWithPromise(
573
+ ({ resolve, reject }) => {
574
+ const newReverseClip = this.getSingleMessage({
575
+ taskUUID
576
+ });
577
+ if (!newReverseClip)
578
+ return;
579
+ if (newReverseClip == null ? void 0 : newReverseClip.error) {
580
+ reject(newReverseClip);
581
+ return true;
582
+ }
583
+ if (newReverseClip) {
584
+ delete this._globalMessages[taskUUID];
585
+ resolve(newReverseClip);
586
+ return true;
587
+ }
588
+ },
589
+ {
590
+ debugKey: "remove-image-background",
591
+ timeoutDuration: this._timeoutDuration
551
592
  }
552
- },
553
- { debugKey: "remove-image-background" }
554
- );
555
- lis.destroy();
556
- return response;
557
- });
593
+ );
594
+ lis.destroy();
595
+ return response;
596
+ },
597
+ {
598
+ maxRetries: totalRetry,
599
+ callback: () => {
600
+ lis == null ? void 0 : lis.destroy();
601
+ }
602
+ }
603
+ );
558
604
  } catch (e) {
559
605
  throw e;
560
606
  }
@@ -571,53 +617,70 @@ var RunwareBase = class {
571
617
  alphaMattingBackgroundThreshold,
572
618
  alphaMattingErodeSize,
573
619
  includeCost,
574
- customTaskUUID
620
+ customTaskUUID,
621
+ retry
575
622
  }) => {
623
+ const totalRetry = retry || this._globalMaxRetries;
624
+ let lis = void 0;
576
625
  try {
577
- await this.ensureConnection();
578
- return await asyncRetry(async () => {
579
- const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
580
- const taskUUID = customTaskUUID || getUUID();
581
- this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
582
- taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
583
- taskUUID,
584
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
585
- }, evaluateNonTrue({ key: "rgba", value: rgba })), evaluateNonTrue({
586
- key: "postProcessMask",
587
- value: postProcessMask
588
- })), evaluateNonTrue({ key: "returnOnlyMask", value: returnOnlyMask })), evaluateNonTrue({ key: "alphaMatting", value: alphaMatting })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
589
- key: "alphaMattingForegroundThreshold",
590
- value: alphaMattingForegroundThreshold
591
- })), evaluateNonTrue({
592
- key: "alphaMattingBackgroundThreshold",
593
- value: alphaMattingBackgroundThreshold
594
- })), evaluateNonTrue({
595
- key: "alphaMattingErodeSize",
596
- value: alphaMattingErodeSize
597
- })), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })));
598
- const lis = this.globalListener({
599
- taskUUID
600
- });
601
- const response = await getIntervalWithPromise(
602
- ({ resolve, reject }) => {
603
- const newRemoveBackground = this.getSingleMessage({ taskUUID });
604
- if (!newRemoveBackground)
605
- return;
606
- if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
607
- reject(newRemoveBackground);
608
- return true;
609
- }
610
- if (newRemoveBackground) {
611
- delete this._globalMessages[taskUUID];
612
- resolve(newRemoveBackground);
613
- return true;
626
+ return await asyncRetry(
627
+ async () => {
628
+ await this.ensureConnection();
629
+ const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
630
+ const taskUUID = customTaskUUID || getUUID();
631
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
632
+ taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
633
+ taskUUID,
634
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
635
+ }, evaluateNonTrue({ key: "rgba", value: rgba })), evaluateNonTrue({
636
+ key: "postProcessMask",
637
+ value: postProcessMask
638
+ })), evaluateNonTrue({
639
+ key: "returnOnlyMask",
640
+ value: returnOnlyMask
641
+ })), evaluateNonTrue({ key: "alphaMatting", value: alphaMatting })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
642
+ key: "alphaMattingForegroundThreshold",
643
+ value: alphaMattingForegroundThreshold
644
+ })), evaluateNonTrue({
645
+ key: "alphaMattingBackgroundThreshold",
646
+ value: alphaMattingBackgroundThreshold
647
+ })), evaluateNonTrue({
648
+ key: "alphaMattingErodeSize",
649
+ value: alphaMattingErodeSize
650
+ })), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })));
651
+ lis = this.globalListener({
652
+ taskUUID
653
+ });
654
+ const response = await getIntervalWithPromise(
655
+ ({ resolve, reject }) => {
656
+ const newRemoveBackground = this.getSingleMessage({ taskUUID });
657
+ if (!newRemoveBackground)
658
+ return;
659
+ if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
660
+ reject(newRemoveBackground);
661
+ return true;
662
+ }
663
+ if (newRemoveBackground) {
664
+ delete this._globalMessages[taskUUID];
665
+ resolve(newRemoveBackground);
666
+ return true;
667
+ }
668
+ },
669
+ {
670
+ debugKey: "remove-image-background",
671
+ timeoutDuration: this._timeoutDuration
614
672
  }
615
- },
616
- { debugKey: "remove-image-background" }
617
- );
618
- lis.destroy();
619
- return response;
620
- });
673
+ );
674
+ lis.destroy();
675
+ return response;
676
+ },
677
+ {
678
+ maxRetries: totalRetry,
679
+ callback: () => {
680
+ lis == null ? void 0 : lis.destroy();
681
+ }
682
+ }
683
+ );
621
684
  } catch (e) {
622
685
  throw e;
623
686
  }
@@ -628,43 +691,54 @@ var RunwareBase = class {
628
691
  outputType,
629
692
  outputFormat,
630
693
  includeCost,
631
- customTaskUUID
694
+ customTaskUUID,
695
+ retry
632
696
  }) => {
697
+ const totalRetry = retry || this._globalMaxRetries;
698
+ let lis = void 0;
633
699
  try {
634
- await this.ensureConnection();
635
- return await asyncRetry(async () => {
636
- let imageUploaded;
637
- imageUploaded = await this.uploadImage(inputImage);
638
- const taskUUID = customTaskUUID || getUUID();
639
- this.send(__spreadValues(__spreadValues(__spreadValues({
640
- taskUUID,
641
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
642
- taskType: "imageUpscale" /* IMAGE_UPSCALE */,
643
- upscaleFactor
644
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
645
- const lis = this.globalListener({
646
- taskUUID
647
- });
648
- const response = await getIntervalWithPromise(
649
- ({ resolve, reject }) => {
650
- const newUpscaleGan = this.getSingleMessage({ taskUUID });
651
- if (!newUpscaleGan)
652
- return;
653
- if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
654
- reject(newUpscaleGan);
655
- return true;
656
- }
657
- if (newUpscaleGan) {
658
- delete this._globalMessages[taskUUID];
659
- resolve(newUpscaleGan);
660
- return true;
661
- }
662
- },
663
- { debugKey: "upscale-gan" }
664
- );
665
- lis.destroy();
666
- return response;
667
- });
700
+ return await asyncRetry(
701
+ async () => {
702
+ await this.ensureConnection();
703
+ let imageUploaded;
704
+ imageUploaded = await this.uploadImage(inputImage);
705
+ const taskUUID = customTaskUUID || getUUID();
706
+ this.send(__spreadValues(__spreadValues(__spreadValues({
707
+ taskUUID,
708
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
709
+ taskType: "imageUpscale" /* IMAGE_UPSCALE */,
710
+ upscaleFactor
711
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
712
+ lis = this.globalListener({
713
+ taskUUID
714
+ });
715
+ const response = await getIntervalWithPromise(
716
+ ({ resolve, reject }) => {
717
+ const newUpscaleGan = this.getSingleMessage({ taskUUID });
718
+ if (!newUpscaleGan)
719
+ return;
720
+ if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
721
+ reject(newUpscaleGan);
722
+ return true;
723
+ }
724
+ if (newUpscaleGan) {
725
+ delete this._globalMessages[taskUUID];
726
+ resolve(newUpscaleGan);
727
+ return true;
728
+ }
729
+ },
730
+ { debugKey: "upscale-gan", timeoutDuration: this._timeoutDuration }
731
+ );
732
+ lis.destroy();
733
+ return response;
734
+ },
735
+ {
736
+ maxRetries: totalRetry,
737
+ callback: () => {
738
+ lis == null ? void 0 : lis.destroy();
739
+ }
740
+ }
741
+ );
668
742
  } catch (e) {
669
743
  throw e;
670
744
  }
@@ -674,41 +748,55 @@ var RunwareBase = class {
674
748
  promptMaxLength = 380,
675
749
  promptVersions = 1,
676
750
  includeCost,
677
- customTaskUUID
751
+ customTaskUUID,
752
+ retry
678
753
  }) => {
754
+ const totalRetry = retry || this._globalMaxRetries;
755
+ let lis = void 0;
679
756
  try {
680
- await this.ensureConnection();
681
- return await asyncRetry(async () => {
682
- const taskUUID = customTaskUUID || getUUID();
683
- this.send(__spreadProps(__spreadValues({
684
- prompt,
685
- taskUUID,
686
- promptMaxLength,
687
- promptVersions
688
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })), {
689
- taskType: "promptEnhance" /* PROMPT_ENHANCE */
690
- }));
691
- const lis = this.globalListener({
692
- taskUUID
693
- });
694
- const response = await getIntervalWithPromise(
695
- ({ resolve, reject }) => {
696
- const reducedPrompt = this._globalMessages[taskUUID];
697
- if (reducedPrompt == null ? void 0 : reducedPrompt.error) {
698
- reject(reducedPrompt);
699
- return true;
700
- }
701
- if ((reducedPrompt == null ? void 0 : reducedPrompt.length) >= promptVersions) {
702
- delete this._globalMessages[taskUUID];
703
- resolve(reducedPrompt);
704
- return true;
757
+ return await asyncRetry(
758
+ async () => {
759
+ await this.ensureConnection();
760
+ const taskUUID = customTaskUUID || getUUID();
761
+ this.send(__spreadProps(__spreadValues({
762
+ prompt,
763
+ taskUUID,
764
+ promptMaxLength,
765
+ promptVersions
766
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })), {
767
+ taskType: "promptEnhance" /* PROMPT_ENHANCE */
768
+ }));
769
+ lis = this.globalListener({
770
+ taskUUID
771
+ });
772
+ const response = await getIntervalWithPromise(
773
+ ({ resolve, reject }) => {
774
+ const reducedPrompt = this._globalMessages[taskUUID];
775
+ if (reducedPrompt == null ? void 0 : reducedPrompt.error) {
776
+ reject(reducedPrompt);
777
+ return true;
778
+ }
779
+ if ((reducedPrompt == null ? void 0 : reducedPrompt.length) >= promptVersions) {
780
+ delete this._globalMessages[taskUUID];
781
+ resolve(reducedPrompt);
782
+ return true;
783
+ }
784
+ },
785
+ {
786
+ debugKey: "enhance-prompt",
787
+ timeoutDuration: this._timeoutDuration
705
788
  }
706
- },
707
- { debugKey: "enhance-prompt" }
708
- );
709
- lis.destroy();
710
- return response;
711
- });
789
+ );
790
+ lis.destroy();
791
+ return response;
792
+ },
793
+ {
794
+ maxRetries: totalRetry,
795
+ callback: () => {
796
+ lis == null ? void 0 : lis.destroy();
797
+ }
798
+ }
799
+ );
712
800
  } catch (e) {
713
801
  throw e;
714
802
  }
@@ -731,6 +819,17 @@ var RunwareBase = class {
731
819
  this._url = url;
732
820
  this._sdkType = "CLIENT" /* CLIENT */;
733
821
  this._shouldReconnect = shouldReconnect;
822
+ this._globalMaxRetries = globalMaxRetries;
823
+ this._timeoutDuration = timeoutDuration;
824
+ }
825
+ static async initialize(props) {
826
+ try {
827
+ const instance = new this(props);
828
+ await instance.ensureConnection();
829
+ return instance;
830
+ } catch (e) {
831
+ throw e;
832
+ }
734
833
  }
735
834
  // protected addListener({
736
835
  // lis,
@@ -802,7 +901,7 @@ var RunwareBase = class {
802
901
  lis: (m) => {
803
902
  var _a, _b;
804
903
  if (m == null ? void 0 : m.error) {
805
- this._invalidAPIkey = "Invalid API key";
904
+ this._invalidAPIkey = m;
806
905
  return;
807
906
  }
808
907
  this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
@@ -821,7 +920,6 @@ var RunwareBase = class {
821
920
  };
822
921
  this._ws.onclose = (e) => {
823
922
  if (this._invalidAPIkey) {
824
- console.error(this._invalidAPIkey);
825
923
  return;
826
924
  }
827
925
  };
@@ -929,13 +1027,13 @@ var RunwareBase = class {
929
1027
  onPartialImages,
930
1028
  includeCost,
931
1029
  customTaskUUID,
932
- retry = 2
1030
+ retry
933
1031
  }) {
934
- await this.ensureConnection();
935
1032
  let lis = void 0;
936
1033
  let requestObject = void 0;
937
1034
  let taskUUIDs = [];
938
1035
  let retryCount = 0;
1036
+ const totalRetry = retry || this._globalMaxRetries;
939
1037
  try {
940
1038
  await this.ensureConnection();
941
1039
  let seedImageUUID = null;
@@ -1025,7 +1123,7 @@ var RunwareBase = class {
1025
1123
  return promise;
1026
1124
  },
1027
1125
  {
1028
- maxRetries: retry,
1126
+ maxRetries: totalRetry,
1029
1127
  callback: () => {
1030
1128
  lis == null ? void 0 : lis.destroy();
1031
1129
  }
@@ -1035,7 +1133,7 @@ var RunwareBase = class {
1035
1133
  if (e.taskUUID) {
1036
1134
  throw e;
1037
1135
  }
1038
- if (retryCount >= retry) {
1136
+ if (retryCount >= totalRetry) {
1039
1137
  return this.handleIncompleteImages({ taskUUIDs, error: e });
1040
1138
  }
1041
1139
  }
@@ -1087,18 +1185,15 @@ var RunwareBase = class {
1087
1185
  if (hasConnected) {
1088
1186
  clearAllIntervals();
1089
1187
  resolve(true);
1188
+ return;
1090
1189
  }
1091
1190
  if (!!this._invalidAPIkey) {
1092
1191
  clearAllIntervals();
1093
- reject(new Error("Invalid API key"));
1192
+ reject(this._invalidAPIkey);
1094
1193
  return;
1095
1194
  }
1096
1195
  }, pollingInterval);
1097
1196
  });
1098
- if (!isConnected) {
1099
- this.connect();
1100
- await delay(2);
1101
- }
1102
1197
  } catch (e) {
1103
1198
  throw (_a = this._invalidAPIkey) != null ? _a : "Could not connect to server. Ensure your API key is correct";
1104
1199
  }
@@ -1130,7 +1225,11 @@ var RunwareBase = class {
1130
1225
  return true;
1131
1226
  }
1132
1227
  },
1133
- { debugKey: "getting images", shouldThrowError }
1228
+ {
1229
+ debugKey: "getting images",
1230
+ shouldThrowError,
1231
+ timeoutDuration: this._timeoutDuration
1232
+ }
1134
1233
  );
1135
1234
  }
1136
1235
  handleIncompleteImages({
@@ -1155,22 +1254,21 @@ var RunwareBase = class {
1155
1254
  // Runware/Runware-client.ts
1156
1255
  var import_reconnect = __toESM(require_reconnect());
1157
1256
  var RunwareClient = class extends RunwareBase {
1158
- constructor({ apiKey, url }) {
1159
- super({ apiKey, url });
1160
- if (apiKey) {
1161
- this._ws = new import_reconnect.default(
1162
- this._url
1163
- );
1164
- this.connect();
1165
- }
1257
+ constructor(props) {
1258
+ const _a = props, { shouldReconnect } = _a, rest = __objRest(_a, ["shouldReconnect"]);
1259
+ super(rest);
1260
+ this._ws = new import_reconnect.default(
1261
+ this._url
1262
+ );
1263
+ this.connect();
1166
1264
  }
1167
1265
  };
1168
1266
 
1169
1267
  // Runware/Runware-server.ts
1170
1268
  import WebSocket2 from "ws";
1171
1269
  var RunwareServer = class extends RunwareBase {
1172
- constructor({ apiKey, url, shouldReconnect }) {
1173
- super({ apiKey, url, shouldReconnect });
1270
+ constructor(props) {
1271
+ super(props);
1174
1272
  this._instantiated = false;
1175
1273
  this._listeners = [];
1176
1274
  this._reconnectingIntervalId = null;
@@ -1191,9 +1289,7 @@ var RunwareServer = class extends RunwareBase {
1191
1289
  }
1192
1290
  };
1193
1291
  this._sdkType = "SERVER" /* SERVER */;
1194
- if (apiKey) {
1195
- this.connect();
1196
- }
1292
+ this.connect();
1197
1293
  }
1198
1294
  // protected addListener({
1199
1295
  // lis,
@@ -1255,7 +1351,7 @@ var RunwareServer = class extends RunwareBase {
1255
1351
  lis: (m) => {
1256
1352
  var _a, _b;
1257
1353
  if (m == null ? void 0 : m.error) {
1258
- this._invalidAPIkey = "Invalid API key";
1354
+ this._invalidAPIkey = m;
1259
1355
  return;
1260
1356
  }
1261
1357
  this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
@@ -1278,7 +1374,6 @@ var RunwareServer = class extends RunwareBase {
1278
1374
  }
1279
1375
  handleClose() {
1280
1376
  if (this._invalidAPIkey) {
1281
- console.error(this._invalidAPIkey);
1282
1377
  return;
1283
1378
  }
1284
1379
  if (this._reconnectingIntervalId) {