@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.js CHANGED
@@ -22,6 +22,18 @@ var __spreadValues = (a, b) => {
22
22
  return a;
23
23
  };
24
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
+ };
25
37
  var __commonJS = (cb, mod) => function __require() {
26
38
  return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
27
39
  };
@@ -168,9 +180,9 @@ var require_reconnect = __commonJS({
168
180
  };
169
181
  log("init");
170
182
  connect();
171
- this.close = (code = 1e3, reason = "", { keepClosed = false, fastClose = true, delay: delay2 = 0 } = {}) => {
172
- if (delay2) {
173
- reconnectDelay = delay2;
183
+ this.close = (code = 1e3, reason = "", { keepClosed = false, fastClose = true, delay: delay3 = 0 } = {}) => {
184
+ if (delay3) {
185
+ reconnectDelay = delay3;
174
186
  }
175
187
  shouldRetry = !keepClosed;
176
188
  ws.close(code, reason);
@@ -262,50 +274,50 @@ var EControlMode = /* @__PURE__ */ ((EControlMode2) => {
262
274
  EControlMode2["CONTROL_NET"] = "controlnet";
263
275
  return EControlMode2;
264
276
  })(EControlMode || {});
265
- var EPreProcessorGroup = /* @__PURE__ */ ((EPreProcessorGroup3) => {
266
- EPreProcessorGroup3["canny"] = "canny";
267
- EPreProcessorGroup3["depth"] = "depth";
268
- EPreProcessorGroup3["mlsd"] = "mlsd";
269
- EPreProcessorGroup3["normalbae"] = "normalbae";
270
- EPreProcessorGroup3["openpose"] = "openpose";
271
- EPreProcessorGroup3["tile"] = "tile";
272
- EPreProcessorGroup3["seg"] = "seg";
273
- EPreProcessorGroup3["lineart"] = "lineart";
274
- EPreProcessorGroup3["lineart_anime"] = "lineart_anime";
275
- EPreProcessorGroup3["shuffle"] = "shuffle";
276
- EPreProcessorGroup3["scribble"] = "scribble";
277
- EPreProcessorGroup3["softedge"] = "softedge";
278
- return EPreProcessorGroup3;
277
+ var EPreProcessorGroup = /* @__PURE__ */ ((EPreProcessorGroup2) => {
278
+ EPreProcessorGroup2["canny"] = "canny";
279
+ EPreProcessorGroup2["depth"] = "depth";
280
+ EPreProcessorGroup2["mlsd"] = "mlsd";
281
+ EPreProcessorGroup2["normalbae"] = "normalbae";
282
+ EPreProcessorGroup2["openpose"] = "openpose";
283
+ EPreProcessorGroup2["tile"] = "tile";
284
+ EPreProcessorGroup2["seg"] = "seg";
285
+ EPreProcessorGroup2["lineart"] = "lineart";
286
+ EPreProcessorGroup2["lineart_anime"] = "lineart_anime";
287
+ EPreProcessorGroup2["shuffle"] = "shuffle";
288
+ EPreProcessorGroup2["scribble"] = "scribble";
289
+ EPreProcessorGroup2["softedge"] = "softedge";
290
+ return EPreProcessorGroup2;
279
291
  })(EPreProcessorGroup || {});
280
- var EPreProcessor = /* @__PURE__ */ ((EPreProcessor3) => {
281
- EPreProcessor3["canny"] = "canny";
282
- EPreProcessor3["depth_leres"] = "depth_leres";
283
- EPreProcessor3["depth_midas"] = "depth_midas";
284
- EPreProcessor3["depth_zoe"] = "depth_zoe";
285
- EPreProcessor3["inpaint_global_harmonious"] = "inpaint_global_harmonious";
286
- EPreProcessor3["lineart_anime"] = "lineart_anime";
287
- EPreProcessor3["lineart_coarse"] = "lineart_coarse";
288
- EPreProcessor3["lineart_realistic"] = "lineart_realistic";
289
- EPreProcessor3["lineart_standard"] = "lineart_standard";
290
- EPreProcessor3["mlsd"] = "mlsd";
291
- EPreProcessor3["normal_bae"] = "normal_bae";
292
- EPreProcessor3["scribble_hed"] = "scribble_hed";
293
- EPreProcessor3["scribble_pidinet"] = "scribble_pidinet";
294
- EPreProcessor3["seg_ofade20k"] = "seg_ofade20k";
295
- EPreProcessor3["seg_ofcoco"] = "seg_ofcoco";
296
- EPreProcessor3["seg_ufade20k"] = "seg_ufade20k";
297
- EPreProcessor3["shuffle"] = "shuffle";
298
- EPreProcessor3["softedge_hed"] = "softedge_hed";
299
- EPreProcessor3["softedge_hedsafe"] = "softedge_hedsafe";
300
- EPreProcessor3["softedge_pidinet"] = "softedge_pidinet";
301
- EPreProcessor3["softedge_pidisafe"] = "softedge_pidisafe";
302
- EPreProcessor3["tile_gaussian"] = "tile_gaussian";
303
- EPreProcessor3["openpose"] = "openpose";
304
- EPreProcessor3["openpose_face"] = "openpose_face";
305
- EPreProcessor3["openpose_faceonly"] = "openpose_faceonly";
306
- EPreProcessor3["openpose_full"] = "openpose_full";
307
- EPreProcessor3["openpose_hand"] = "openpose_hand";
308
- return EPreProcessor3;
292
+ var EPreProcessor = /* @__PURE__ */ ((EPreProcessor2) => {
293
+ EPreProcessor2["canny"] = "canny";
294
+ EPreProcessor2["depth_leres"] = "depth_leres";
295
+ EPreProcessor2["depth_midas"] = "depth_midas";
296
+ EPreProcessor2["depth_zoe"] = "depth_zoe";
297
+ EPreProcessor2["inpaint_global_harmonious"] = "inpaint_global_harmonious";
298
+ EPreProcessor2["lineart_anime"] = "lineart_anime";
299
+ EPreProcessor2["lineart_coarse"] = "lineart_coarse";
300
+ EPreProcessor2["lineart_realistic"] = "lineart_realistic";
301
+ EPreProcessor2["lineart_standard"] = "lineart_standard";
302
+ EPreProcessor2["mlsd"] = "mlsd";
303
+ EPreProcessor2["normal_bae"] = "normal_bae";
304
+ EPreProcessor2["scribble_hed"] = "scribble_hed";
305
+ EPreProcessor2["scribble_pidinet"] = "scribble_pidinet";
306
+ EPreProcessor2["seg_ofade20k"] = "seg_ofade20k";
307
+ EPreProcessor2["seg_ofcoco"] = "seg_ofcoco";
308
+ EPreProcessor2["seg_ufade20k"] = "seg_ufade20k";
309
+ EPreProcessor2["shuffle"] = "shuffle";
310
+ EPreProcessor2["softedge_hed"] = "softedge_hed";
311
+ EPreProcessor2["softedge_hedsafe"] = "softedge_hedsafe";
312
+ EPreProcessor2["softedge_pidinet"] = "softedge_pidinet";
313
+ EPreProcessor2["softedge_pidisafe"] = "softedge_pidisafe";
314
+ EPreProcessor2["tile_gaussian"] = "tile_gaussian";
315
+ EPreProcessor2["openpose"] = "openpose";
316
+ EPreProcessor2["openpose_face"] = "openpose_face";
317
+ EPreProcessor2["openpose_faceonly"] = "openpose_faceonly";
318
+ EPreProcessor2["openpose_full"] = "openpose_full";
319
+ EPreProcessor2["openpose_hand"] = "openpose_hand";
320
+ return EPreProcessor2;
309
321
  })(EPreProcessor || {});
310
322
  var EOpenPosePreProcessor = /* @__PURE__ */ ((EOpenPosePreProcessor2) => {
311
323
  EOpenPosePreProcessor2["openpose"] = "openpose";
@@ -319,6 +331,7 @@ var EOpenPosePreProcessor = /* @__PURE__ */ ((EOpenPosePreProcessor2) => {
319
331
  // Runware/utils.ts
320
332
  var import_uuid = require("uuid");
321
333
  var TIMEOUT_DURATION = 6e4;
334
+ var MINIMUM_TIMEOUT_DURATION = 1e3;
322
335
  var POLLING_INTERVAL = 100;
323
336
  var BASE_RUNWARE_URLS = {
324
337
  ["PRODUCTION" /* PRODUCTION */]: "wss://ws-api.runware.ai/v1",
@@ -336,20 +349,20 @@ var removeFromAray = (col, targetElem) => {
336
349
  };
337
350
  var getIntervalWithPromise = (callback, {
338
351
  debugKey = "debugKey",
339
- timeOutDuration = TIMEOUT_DURATION,
352
+ timeoutDuration = TIMEOUT_DURATION,
340
353
  shouldThrowError = true
341
354
  }) => {
355
+ timeoutDuration = timeoutDuration < MINIMUM_TIMEOUT_DURATION ? MINIMUM_TIMEOUT_DURATION : timeoutDuration;
342
356
  return new Promise((resolve, reject) => {
343
357
  const timeoutId = setTimeout(() => {
344
358
  if (intervalId) {
345
359
  clearInterval(intervalId);
346
360
  if (shouldThrowError) {
347
- reject(`Message could not be received for ${debugKey}`);
348
- console.error("Message could not be received for ", debugKey);
361
+ reject(`Response could not be received from server for ${debugKey}`);
349
362
  }
350
363
  }
351
364
  clearTimeout(timeoutId);
352
- }, timeOutDuration);
365
+ }, timeoutDuration);
353
366
  let intervalId = setInterval(async () => {
354
367
  const shouldClear = callback({ resolve, reject, intervalId });
355
368
  if (shouldClear) {
@@ -439,7 +452,9 @@ var RunwareBase = class {
439
452
  constructor({
440
453
  apiKey,
441
454
  url = BASE_RUNWARE_URLS.PRODUCTION,
442
- shouldReconnect = true
455
+ shouldReconnect = true,
456
+ globalMaxRetries = 2,
457
+ timeoutDuration = TIMEOUT_DURATION
443
458
  }) {
444
459
  this._listeners = [];
445
460
  // _globalMessages: any[] = [];
@@ -488,51 +503,68 @@ var RunwareBase = class {
488
503
  lowThresholdCanny,
489
504
  includeHandsAndFaceOpenPose,
490
505
  includeCost,
491
- customTaskUUID
506
+ customTaskUUID,
507
+ retry
492
508
  }) => {
509
+ const totalRetry = retry || this._globalMaxRetries;
510
+ let lis = void 0;
493
511
  try {
494
- const image = await this.uploadImage(inputImage);
495
- if (!(image == null ? void 0 : image.imageUUID))
496
- return null;
497
- const taskUUID = customTaskUUID || getUUID();
498
- this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
499
- inputImage: image.imageUUID,
500
- taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
501
- taskUUID,
502
- preProcessor
503
- }, 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({
504
- key: "highThresholdCanny",
505
- value: highThresholdCanny
506
- })), evaluateNonTrue({
507
- key: "lowThresholdCanny",
508
- value: lowThresholdCanny
509
- })), evaluateNonTrue({
510
- key: "includeHandsAndFaceOpenPose",
511
- value: includeHandsAndFaceOpenPose
512
- })));
513
- const lis = this.globalListener({
514
- taskUUID
515
- });
516
- const guideImage = await getIntervalWithPromise(
517
- ({ resolve, reject }) => {
518
- const uploadedImage = this.getSingleMessage({
512
+ return await asyncRetry(
513
+ async () => {
514
+ await this.ensureConnection();
515
+ const image = await this.uploadImage(inputImage);
516
+ if (!(image == null ? void 0 : image.imageUUID))
517
+ return null;
518
+ const taskUUID = customTaskUUID || getUUID();
519
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
520
+ inputImage: image.imageUUID,
521
+ taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
522
+ taskUUID,
523
+ preProcessor
524
+ }, 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({
525
+ key: "highThresholdCanny",
526
+ value: highThresholdCanny
527
+ })), evaluateNonTrue({
528
+ key: "lowThresholdCanny",
529
+ value: lowThresholdCanny
530
+ })), evaluateNonTrue({
531
+ key: "includeHandsAndFaceOpenPose",
532
+ value: includeHandsAndFaceOpenPose
533
+ })));
534
+ lis = this.globalListener({
519
535
  taskUUID
520
536
  });
521
- if (!uploadedImage)
522
- return;
523
- if (uploadedImage == null ? void 0 : uploadedImage.error) {
524
- reject(uploadedImage);
525
- return true;
526
- }
527
- if (uploadedImage) {
528
- resolve(uploadedImage);
529
- return true;
530
- }
537
+ const guideImage = await getIntervalWithPromise(
538
+ ({ resolve, reject }) => {
539
+ const uploadedImage = this.getSingleMessage({
540
+ taskUUID
541
+ });
542
+ if (!uploadedImage)
543
+ return;
544
+ if (uploadedImage == null ? void 0 : uploadedImage.error) {
545
+ reject(uploadedImage);
546
+ return true;
547
+ }
548
+ if (uploadedImage) {
549
+ resolve(uploadedImage);
550
+ return true;
551
+ }
552
+ },
553
+ {
554
+ debugKey: "unprocessed-image",
555
+ timeoutDuration: this._timeoutDuration
556
+ }
557
+ );
558
+ lis.destroy();
559
+ return guideImage;
531
560
  },
532
- { debugKey: "unprocessed-image" }
561
+ {
562
+ maxRetries: totalRetry,
563
+ callback: () => {
564
+ lis == null ? void 0 : lis.destroy();
565
+ }
566
+ }
533
567
  );
534
- lis.destroy();
535
- return guideImage;
536
568
  } catch (e) {
537
569
  throw e;
538
570
  }
@@ -540,43 +572,57 @@ var RunwareBase = class {
540
572
  this.requestImageToText = async ({
541
573
  inputImage,
542
574
  includeCost,
543
- customTaskUUID
575
+ customTaskUUID,
576
+ retry
544
577
  }) => {
578
+ const totalRetry = retry || this._globalMaxRetries;
579
+ let lis = void 0;
545
580
  try {
546
- await this.ensureConnection();
547
- return await asyncRetry(async () => {
548
- const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
549
- const taskUUID = customTaskUUID || getUUID();
550
- this.send(__spreadValues({
551
- taskUUID,
552
- taskType: "imageCaption" /* IMAGE_CAPTION */,
553
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
554
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })));
555
- const lis = this.globalListener({
556
- taskUUID
557
- });
558
- const response = await getIntervalWithPromise(
559
- ({ resolve, reject }) => {
560
- const newReverseClip = this.getSingleMessage({
561
- taskUUID
562
- });
563
- if (!newReverseClip)
564
- return;
565
- if (newReverseClip == null ? void 0 : newReverseClip.error) {
566
- reject(newReverseClip);
567
- return true;
568
- }
569
- if (newReverseClip) {
570
- delete this._globalMessages[taskUUID];
571
- resolve(newReverseClip);
572
- return true;
581
+ return await asyncRetry(
582
+ async () => {
583
+ await this.ensureConnection();
584
+ const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
585
+ const taskUUID = customTaskUUID || getUUID();
586
+ this.send(__spreadValues({
587
+ taskUUID,
588
+ taskType: "imageCaption" /* IMAGE_CAPTION */,
589
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
590
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })));
591
+ lis = this.globalListener({
592
+ taskUUID
593
+ });
594
+ const response = await getIntervalWithPromise(
595
+ ({ resolve, reject }) => {
596
+ const newReverseClip = this.getSingleMessage({
597
+ taskUUID
598
+ });
599
+ if (!newReverseClip)
600
+ return;
601
+ if (newReverseClip == null ? void 0 : newReverseClip.error) {
602
+ reject(newReverseClip);
603
+ return true;
604
+ }
605
+ if (newReverseClip) {
606
+ delete this._globalMessages[taskUUID];
607
+ resolve(newReverseClip);
608
+ return true;
609
+ }
610
+ },
611
+ {
612
+ debugKey: "remove-image-background",
613
+ timeoutDuration: this._timeoutDuration
573
614
  }
574
- },
575
- { debugKey: "remove-image-background" }
576
- );
577
- lis.destroy();
578
- return response;
579
- });
615
+ );
616
+ lis.destroy();
617
+ return response;
618
+ },
619
+ {
620
+ maxRetries: totalRetry,
621
+ callback: () => {
622
+ lis == null ? void 0 : lis.destroy();
623
+ }
624
+ }
625
+ );
580
626
  } catch (e) {
581
627
  throw e;
582
628
  }
@@ -593,53 +639,70 @@ var RunwareBase = class {
593
639
  alphaMattingBackgroundThreshold,
594
640
  alphaMattingErodeSize,
595
641
  includeCost,
596
- customTaskUUID
642
+ customTaskUUID,
643
+ retry
597
644
  }) => {
645
+ const totalRetry = retry || this._globalMaxRetries;
646
+ let lis = void 0;
598
647
  try {
599
- await this.ensureConnection();
600
- return await asyncRetry(async () => {
601
- const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
602
- const taskUUID = customTaskUUID || getUUID();
603
- this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
604
- taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
605
- taskUUID,
606
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
607
- }, evaluateNonTrue({ key: "rgba", value: rgba })), evaluateNonTrue({
608
- key: "postProcessMask",
609
- value: postProcessMask
610
- })), evaluateNonTrue({ key: "returnOnlyMask", value: returnOnlyMask })), evaluateNonTrue({ key: "alphaMatting", value: alphaMatting })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
611
- key: "alphaMattingForegroundThreshold",
612
- value: alphaMattingForegroundThreshold
613
- })), evaluateNonTrue({
614
- key: "alphaMattingBackgroundThreshold",
615
- value: alphaMattingBackgroundThreshold
616
- })), evaluateNonTrue({
617
- key: "alphaMattingErodeSize",
618
- value: alphaMattingErodeSize
619
- })), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })));
620
- const lis = this.globalListener({
621
- taskUUID
622
- });
623
- const response = await getIntervalWithPromise(
624
- ({ resolve, reject }) => {
625
- const newRemoveBackground = this.getSingleMessage({ taskUUID });
626
- if (!newRemoveBackground)
627
- return;
628
- if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
629
- reject(newRemoveBackground);
630
- return true;
631
- }
632
- if (newRemoveBackground) {
633
- delete this._globalMessages[taskUUID];
634
- resolve(newRemoveBackground);
635
- return true;
648
+ return await asyncRetry(
649
+ async () => {
650
+ await this.ensureConnection();
651
+ const imageUploaded = inputImage ? await this.uploadImage(inputImage) : null;
652
+ const taskUUID = customTaskUUID || getUUID();
653
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
654
+ taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
655
+ taskUUID,
656
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID
657
+ }, evaluateNonTrue({ key: "rgba", value: rgba })), evaluateNonTrue({
658
+ key: "postProcessMask",
659
+ value: postProcessMask
660
+ })), evaluateNonTrue({
661
+ key: "returnOnlyMask",
662
+ value: returnOnlyMask
663
+ })), evaluateNonTrue({ key: "alphaMatting", value: alphaMatting })), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({
664
+ key: "alphaMattingForegroundThreshold",
665
+ value: alphaMattingForegroundThreshold
666
+ })), evaluateNonTrue({
667
+ key: "alphaMattingBackgroundThreshold",
668
+ value: alphaMattingBackgroundThreshold
669
+ })), evaluateNonTrue({
670
+ key: "alphaMattingErodeSize",
671
+ value: alphaMattingErodeSize
672
+ })), evaluateNonTrue({ key: "outputType", value: outputType })), evaluateNonTrue({ key: "outputFormat", value: outputFormat })));
673
+ lis = this.globalListener({
674
+ taskUUID
675
+ });
676
+ const response = await getIntervalWithPromise(
677
+ ({ resolve, reject }) => {
678
+ const newRemoveBackground = this.getSingleMessage({ taskUUID });
679
+ if (!newRemoveBackground)
680
+ return;
681
+ if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
682
+ reject(newRemoveBackground);
683
+ return true;
684
+ }
685
+ if (newRemoveBackground) {
686
+ delete this._globalMessages[taskUUID];
687
+ resolve(newRemoveBackground);
688
+ return true;
689
+ }
690
+ },
691
+ {
692
+ debugKey: "remove-image-background",
693
+ timeoutDuration: this._timeoutDuration
636
694
  }
637
- },
638
- { debugKey: "remove-image-background" }
639
- );
640
- lis.destroy();
641
- return response;
642
- });
695
+ );
696
+ lis.destroy();
697
+ return response;
698
+ },
699
+ {
700
+ maxRetries: totalRetry,
701
+ callback: () => {
702
+ lis == null ? void 0 : lis.destroy();
703
+ }
704
+ }
705
+ );
643
706
  } catch (e) {
644
707
  throw e;
645
708
  }
@@ -650,43 +713,54 @@ var RunwareBase = class {
650
713
  outputType,
651
714
  outputFormat,
652
715
  includeCost,
653
- customTaskUUID
716
+ customTaskUUID,
717
+ retry
654
718
  }) => {
719
+ const totalRetry = retry || this._globalMaxRetries;
720
+ let lis = void 0;
655
721
  try {
656
- await this.ensureConnection();
657
- return await asyncRetry(async () => {
658
- let imageUploaded;
659
- imageUploaded = await this.uploadImage(inputImage);
660
- const taskUUID = customTaskUUID || getUUID();
661
- this.send(__spreadValues(__spreadValues(__spreadValues({
662
- taskUUID,
663
- inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
664
- taskType: "imageUpscale" /* IMAGE_UPSCALE */,
665
- upscaleFactor
666
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
667
- const lis = this.globalListener({
668
- taskUUID
669
- });
670
- const response = await getIntervalWithPromise(
671
- ({ resolve, reject }) => {
672
- const newUpscaleGan = this.getSingleMessage({ taskUUID });
673
- if (!newUpscaleGan)
674
- return;
675
- if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
676
- reject(newUpscaleGan);
677
- return true;
678
- }
679
- if (newUpscaleGan) {
680
- delete this._globalMessages[taskUUID];
681
- resolve(newUpscaleGan);
682
- return true;
683
- }
684
- },
685
- { debugKey: "upscale-gan" }
686
- );
687
- lis.destroy();
688
- return response;
689
- });
722
+ return await asyncRetry(
723
+ async () => {
724
+ await this.ensureConnection();
725
+ let imageUploaded;
726
+ imageUploaded = await this.uploadImage(inputImage);
727
+ const taskUUID = customTaskUUID || getUUID();
728
+ this.send(__spreadValues(__spreadValues(__spreadValues({
729
+ taskUUID,
730
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
731
+ taskType: "imageUpscale" /* IMAGE_UPSCALE */,
732
+ upscaleFactor
733
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
734
+ lis = this.globalListener({
735
+ taskUUID
736
+ });
737
+ const response = await getIntervalWithPromise(
738
+ ({ resolve, reject }) => {
739
+ const newUpscaleGan = this.getSingleMessage({ taskUUID });
740
+ if (!newUpscaleGan)
741
+ return;
742
+ if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
743
+ reject(newUpscaleGan);
744
+ return true;
745
+ }
746
+ if (newUpscaleGan) {
747
+ delete this._globalMessages[taskUUID];
748
+ resolve(newUpscaleGan);
749
+ return true;
750
+ }
751
+ },
752
+ { debugKey: "upscale-gan", timeoutDuration: this._timeoutDuration }
753
+ );
754
+ lis.destroy();
755
+ return response;
756
+ },
757
+ {
758
+ maxRetries: totalRetry,
759
+ callback: () => {
760
+ lis == null ? void 0 : lis.destroy();
761
+ }
762
+ }
763
+ );
690
764
  } catch (e) {
691
765
  throw e;
692
766
  }
@@ -696,41 +770,55 @@ var RunwareBase = class {
696
770
  promptMaxLength = 380,
697
771
  promptVersions = 1,
698
772
  includeCost,
699
- customTaskUUID
773
+ customTaskUUID,
774
+ retry
700
775
  }) => {
776
+ const totalRetry = retry || this._globalMaxRetries;
777
+ let lis = void 0;
701
778
  try {
702
- await this.ensureConnection();
703
- return await asyncRetry(async () => {
704
- const taskUUID = customTaskUUID || getUUID();
705
- this.send(__spreadProps(__spreadValues({
706
- prompt,
707
- taskUUID,
708
- promptMaxLength,
709
- promptVersions
710
- }, evaluateNonTrue({ key: "includeCost", value: includeCost })), {
711
- taskType: "promptEnhance" /* PROMPT_ENHANCE */
712
- }));
713
- const lis = this.globalListener({
714
- taskUUID
715
- });
716
- const response = await getIntervalWithPromise(
717
- ({ resolve, reject }) => {
718
- const reducedPrompt = this._globalMessages[taskUUID];
719
- if (reducedPrompt == null ? void 0 : reducedPrompt.error) {
720
- reject(reducedPrompt);
721
- return true;
722
- }
723
- if ((reducedPrompt == null ? void 0 : reducedPrompt.length) >= promptVersions) {
724
- delete this._globalMessages[taskUUID];
725
- resolve(reducedPrompt);
726
- return true;
779
+ return await asyncRetry(
780
+ async () => {
781
+ await this.ensureConnection();
782
+ const taskUUID = customTaskUUID || getUUID();
783
+ this.send(__spreadProps(__spreadValues({
784
+ prompt,
785
+ taskUUID,
786
+ promptMaxLength,
787
+ promptVersions
788
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })), {
789
+ taskType: "promptEnhance" /* PROMPT_ENHANCE */
790
+ }));
791
+ lis = this.globalListener({
792
+ taskUUID
793
+ });
794
+ const response = await getIntervalWithPromise(
795
+ ({ resolve, reject }) => {
796
+ const reducedPrompt = this._globalMessages[taskUUID];
797
+ if (reducedPrompt == null ? void 0 : reducedPrompt.error) {
798
+ reject(reducedPrompt);
799
+ return true;
800
+ }
801
+ if ((reducedPrompt == null ? void 0 : reducedPrompt.length) >= promptVersions) {
802
+ delete this._globalMessages[taskUUID];
803
+ resolve(reducedPrompt);
804
+ return true;
805
+ }
806
+ },
807
+ {
808
+ debugKey: "enhance-prompt",
809
+ timeoutDuration: this._timeoutDuration
727
810
  }
728
- },
729
- { debugKey: "enhance-prompt" }
730
- );
731
- lis.destroy();
732
- return response;
733
- });
811
+ );
812
+ lis.destroy();
813
+ return response;
814
+ },
815
+ {
816
+ maxRetries: totalRetry,
817
+ callback: () => {
818
+ lis == null ? void 0 : lis.destroy();
819
+ }
820
+ }
821
+ );
734
822
  } catch (e) {
735
823
  throw e;
736
824
  }
@@ -753,6 +841,17 @@ var RunwareBase = class {
753
841
  this._url = url;
754
842
  this._sdkType = "CLIENT" /* CLIENT */;
755
843
  this._shouldReconnect = shouldReconnect;
844
+ this._globalMaxRetries = globalMaxRetries;
845
+ this._timeoutDuration = timeoutDuration;
846
+ }
847
+ static async initialize(props) {
848
+ try {
849
+ const instance = new this(props);
850
+ await instance.ensureConnection();
851
+ return instance;
852
+ } catch (e) {
853
+ throw e;
854
+ }
756
855
  }
757
856
  // protected addListener({
758
857
  // lis,
@@ -824,7 +923,7 @@ var RunwareBase = class {
824
923
  lis: (m) => {
825
924
  var _a, _b;
826
925
  if (m == null ? void 0 : m.error) {
827
- this._invalidAPIkey = "Invalid API key";
926
+ this._invalidAPIkey = m;
828
927
  return;
829
928
  }
830
929
  this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
@@ -843,7 +942,6 @@ var RunwareBase = class {
843
942
  };
844
943
  this._ws.onclose = (e) => {
845
944
  if (this._invalidAPIkey) {
846
- console.error(this._invalidAPIkey);
847
945
  return;
848
946
  }
849
947
  };
@@ -951,13 +1049,13 @@ var RunwareBase = class {
951
1049
  onPartialImages,
952
1050
  includeCost,
953
1051
  customTaskUUID,
954
- retry = 2
1052
+ retry
955
1053
  }) {
956
- await this.ensureConnection();
957
1054
  let lis = void 0;
958
1055
  let requestObject = void 0;
959
1056
  let taskUUIDs = [];
960
1057
  let retryCount = 0;
1058
+ const totalRetry = retry || this._globalMaxRetries;
961
1059
  try {
962
1060
  await this.ensureConnection();
963
1061
  let seedImageUUID = null;
@@ -1047,7 +1145,7 @@ var RunwareBase = class {
1047
1145
  return promise;
1048
1146
  },
1049
1147
  {
1050
- maxRetries: retry,
1148
+ maxRetries: totalRetry,
1051
1149
  callback: () => {
1052
1150
  lis == null ? void 0 : lis.destroy();
1053
1151
  }
@@ -1057,7 +1155,7 @@ var RunwareBase = class {
1057
1155
  if (e.taskUUID) {
1058
1156
  throw e;
1059
1157
  }
1060
- if (retryCount >= retry) {
1158
+ if (retryCount >= totalRetry) {
1061
1159
  return this.handleIncompleteImages({ taskUUIDs, error: e });
1062
1160
  }
1063
1161
  }
@@ -1109,18 +1207,15 @@ var RunwareBase = class {
1109
1207
  if (hasConnected) {
1110
1208
  clearAllIntervals();
1111
1209
  resolve(true);
1210
+ return;
1112
1211
  }
1113
1212
  if (!!this._invalidAPIkey) {
1114
1213
  clearAllIntervals();
1115
- reject(new Error("Invalid API key"));
1214
+ reject(this._invalidAPIkey);
1116
1215
  return;
1117
1216
  }
1118
1217
  }, pollingInterval);
1119
1218
  });
1120
- if (!isConnected) {
1121
- this.connect();
1122
- await delay(2);
1123
- }
1124
1219
  } catch (e) {
1125
1220
  throw (_a = this._invalidAPIkey) != null ? _a : "Could not connect to server. Ensure your API key is correct";
1126
1221
  }
@@ -1152,7 +1247,11 @@ var RunwareBase = class {
1152
1247
  return true;
1153
1248
  }
1154
1249
  },
1155
- { debugKey: "getting images", shouldThrowError }
1250
+ {
1251
+ debugKey: "getting images",
1252
+ shouldThrowError,
1253
+ timeoutDuration: this._timeoutDuration
1254
+ }
1156
1255
  );
1157
1256
  }
1158
1257
  handleIncompleteImages({
@@ -1177,22 +1276,21 @@ var RunwareBase = class {
1177
1276
  // Runware/Runware-client.ts
1178
1277
  var import_reconnect = __toESM(require_reconnect());
1179
1278
  var RunwareClient = class extends RunwareBase {
1180
- constructor({ apiKey, url }) {
1181
- super({ apiKey, url });
1182
- if (apiKey) {
1183
- this._ws = new import_reconnect.default(
1184
- this._url
1185
- );
1186
- this.connect();
1187
- }
1279
+ constructor(props) {
1280
+ const _a = props, { shouldReconnect } = _a, rest = __objRest(_a, ["shouldReconnect"]);
1281
+ super(rest);
1282
+ this._ws = new import_reconnect.default(
1283
+ this._url
1284
+ );
1285
+ this.connect();
1188
1286
  }
1189
1287
  };
1190
1288
 
1191
1289
  // Runware/Runware-server.ts
1192
1290
  var import_ws = __toESM(require("ws"));
1193
1291
  var RunwareServer = class extends RunwareBase {
1194
- constructor({ apiKey, url, shouldReconnect }) {
1195
- super({ apiKey, url, shouldReconnect });
1292
+ constructor(props) {
1293
+ super(props);
1196
1294
  this._instantiated = false;
1197
1295
  this._listeners = [];
1198
1296
  this._reconnectingIntervalId = null;
@@ -1213,9 +1311,7 @@ var RunwareServer = class extends RunwareBase {
1213
1311
  }
1214
1312
  };
1215
1313
  this._sdkType = "SERVER" /* SERVER */;
1216
- if (apiKey) {
1217
- this.connect();
1218
- }
1314
+ this.connect();
1219
1315
  }
1220
1316
  // protected addListener({
1221
1317
  // lis,
@@ -1277,7 +1373,7 @@ var RunwareServer = class extends RunwareBase {
1277
1373
  lis: (m) => {
1278
1374
  var _a, _b;
1279
1375
  if (m == null ? void 0 : m.error) {
1280
- this._invalidAPIkey = "Invalid API key";
1376
+ this._invalidAPIkey = m;
1281
1377
  return;
1282
1378
  }
1283
1379
  this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
@@ -1300,7 +1396,6 @@ var RunwareServer = class extends RunwareBase {
1300
1396
  }
1301
1397
  handleClose() {
1302
1398
  if (this._invalidAPIkey) {
1303
- console.error(this._invalidAPIkey);
1304
1399
  return;
1305
1400
  }
1306
1401
  if (this._reconnectingIntervalId) {