@runware/sdk-js 1.0.30 → 1.1.0

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
@@ -223,6 +223,17 @@ var SdkType = /* @__PURE__ */ ((SdkType2) => {
223
223
  SdkType2["SERVER"] = "SERVER";
224
224
  return SdkType2;
225
225
  })(SdkType || {});
226
+ var ETaskType = /* @__PURE__ */ ((ETaskType2) => {
227
+ ETaskType2["IMAGE_INFERENCE"] = "imageInference";
228
+ ETaskType2["IMAGE_UPLOAD"] = "imageUpload";
229
+ ETaskType2["IMAGE_UPSCALE"] = "imageUpscale";
230
+ ETaskType2["IMAGE_BACKGROUND_REMOVAL"] = "imageBackgroundRemoval";
231
+ ETaskType2["IMAGE_CAPTION"] = "imageCaption";
232
+ ETaskType2["IMAGE_CONTROL_NET_PRE_PROCESS"] = "imageControlNetPreProcess";
233
+ ETaskType2["PROMPT_ENHANCE"] = "promptEnhance";
234
+ ETaskType2["AUTHENTICATION"] = "authentication";
235
+ return ETaskType2;
236
+ })(ETaskType || {});
226
237
  var EControlMode = /* @__PURE__ */ ((EControlMode2) => {
227
238
  EControlMode2["BALANCED"] = "balanced";
228
239
  EControlMode2["PROMPT"] = "prompt";
@@ -335,82 +346,6 @@ var fileToBase64 = (file) => new Promise((resolve) => {
335
346
  });
336
347
  var getUUID = () => uuidv4();
337
348
  var isValidUUID = (uuid) => validateUUID(uuid);
338
- var getTaskType = ({
339
- prompt,
340
- controlNet,
341
- imageMaskInitiator,
342
- imageInitiator
343
- }) => {
344
- if (evaluateToBoolean(prompt, !controlNet, !imageMaskInitiator, !imageInitiator)) {
345
- return 1;
346
- }
347
- if (evaluateToBoolean(prompt, !controlNet, !imageMaskInitiator, imageInitiator)) {
348
- return 2;
349
- }
350
- if (evaluateToBoolean(prompt, !controlNet, imageMaskInitiator, imageInitiator)) {
351
- return 3;
352
- }
353
- if (evaluateToBoolean(prompt, controlNet, !imageMaskInitiator, !imageInitiator)) {
354
- return 9;
355
- }
356
- if (evaluateToBoolean(prompt, controlNet, !imageMaskInitiator, imageInitiator)) {
357
- return 10;
358
- }
359
- if (evaluateToBoolean(prompt, controlNet, imageMaskInitiator, imageInitiator)) {
360
- return 10;
361
- }
362
- };
363
- var evaluateToBoolean = (...args) => [...args].every((e) => !!e);
364
- var compact = (value, data) => !!value ? data : {};
365
- var getPreprocessorType = (processor) => {
366
- const processorGroup = Object.keys(
367
- EPreProcessorGroup
368
- );
369
- switch (processor) {
370
- case "canny" /* canny */:
371
- return "canny" /* canny */;
372
- case "depth_leres" /* depth_leres */:
373
- case "depth_midas" /* depth_midas */:
374
- case "depth_zoe" /* depth_zoe */:
375
- return "depth" /* depth */;
376
- case "inpaint_global_harmonious" /* inpaint_global_harmonious */:
377
- return "depth" /* depth */;
378
- case "lineart_anime" /* lineart_anime */:
379
- return "lineart_anime" /* lineart_anime */;
380
- case "lineart_coarse" /* lineart_coarse */:
381
- case "lineart_realistic" /* lineart_realistic */:
382
- case "lineart_standard" /* lineart_standard */:
383
- return "lineart" /* lineart */;
384
- case "mlsd" /* mlsd */:
385
- return "mlsd" /* mlsd */;
386
- case "normal_bae" /* normal_bae */:
387
- return "normalbae" /* normalbae */;
388
- case "openpose_face" /* openpose_face */:
389
- case "openpose_faceonly" /* openpose_faceonly */:
390
- case "openpose_full" /* openpose_full */:
391
- case "openpose_hand" /* openpose_hand */:
392
- case "openpose" /* openpose */:
393
- return "openpose" /* openpose */;
394
- case "scribble_hed" /* scribble_hed */:
395
- case "scribble_pidinet" /* scribble_pidinet */:
396
- return "scribble" /* scribble */;
397
- case "seg_ofade20k" /* seg_ofade20k */:
398
- case "seg_ofcoco" /* seg_ofcoco */:
399
- case "seg_ufade20k" /* seg_ufade20k */:
400
- return "seg" /* seg */;
401
- case "shuffle" /* shuffle */:
402
- return "shuffle" /* shuffle */;
403
- case "softedge_hed" /* softedge_hed */:
404
- case "softedge_hedsafe" /* softedge_hedsafe */:
405
- case "softedge_pidinet" /* softedge_pidinet */:
406
- case "softedge_pidisafe" /* softedge_pidisafe */:
407
- return "softedge" /* softedge */;
408
- case "tile_gaussian" /* tile_gaussian */:
409
- return "tile" /* tile */;
410
- default:
411
- return "canny" /* canny */;
412
- }
413
- };
414
349
  var accessDeepObject = ({
415
350
  key,
416
351
  data,
@@ -488,34 +423,36 @@ var RunwareBase = class {
488
423
  var _a;
489
424
  return ((_a = this._ws) == null ? void 0 : _a.readyState) === 1;
490
425
  };
491
- this.send = (msg) => this._ws.send(JSON.stringify(msg));
426
+ // We moving to an array format, it make sense to consolidate all request to an array here
427
+ this.send = (msg) => this._ws.send(JSON.stringify([msg]));
492
428
  this.uploadImage = async (file) => {
493
429
  try {
494
430
  return await asyncRetry(async () => {
495
431
  const taskUUID = getUUID();
496
432
  if (typeof file === "string" && isValidUUID(file)) {
497
433
  return {
498
- newImageUUID: file,
499
- newImageSrc: file,
500
- taskUUID
434
+ imageURL: file,
435
+ imageUUID: file,
436
+ taskUUID,
437
+ taskType: "imageUpload" /* IMAGE_UPLOAD */
501
438
  };
502
439
  }
503
440
  const imageBase64 = typeof file === "string" ? file : await fileToBase64(file);
504
441
  this.send({
505
- newImageUpload: {
506
- imageBase64,
507
- taskUUID,
508
- taskType: 7
509
- }
442
+ taskType: "imageUpload" /* IMAGE_UPLOAD */,
443
+ image: imageBase64,
444
+ taskUUID
510
445
  });
511
446
  const lis = this.globalListener({
512
- responseKey: "newUploadedImageUUID",
513
- taskKey: "newUploadedImageUUID",
514
447
  taskUUID
515
448
  });
516
449
  const image = await getIntervalWithPromise(
517
450
  ({ resolve, reject }) => {
518
- const uploadedImage = this._globalMessages[taskUUID];
451
+ const uploadedImage = this.getSingleMessage({
452
+ taskUUID
453
+ });
454
+ if (!uploadedImage)
455
+ return;
519
456
  if (uploadedImage == null ? void 0 : uploadedImage.error) {
520
457
  reject(uploadedImage);
521
458
  return true;
@@ -535,44 +472,54 @@ var RunwareBase = class {
535
472
  throw e;
536
473
  }
537
474
  };
538
- this.uploadUnprocessedImage = async ({
539
- file,
540
- preProcessorType,
541
- width,
475
+ this.controlNetPreProcess = async ({
476
+ inputImage,
477
+ preProcessor,
542
478
  height,
543
- lowThresholdCanny,
479
+ width,
480
+ outputType,
481
+ outputFormat,
544
482
  highThresholdCanny,
545
- includeHandsAndFaceOpenPose = true,
546
- returnBase64Image
483
+ lowThresholdCanny,
484
+ includeHandsAndFaceOpenPose,
485
+ includeCost
547
486
  }) => {
548
487
  try {
549
- const image = await this.uploadImage(file);
550
- if (!image)
488
+ const image = await this.uploadImage(inputImage);
489
+ if (!(image == null ? void 0 : image.imageUUID))
551
490
  return null;
552
491
  const taskUUID = getUUID();
553
- this.send({
554
- newPreProcessControlNet: __spreadValues(__spreadValues(__spreadValues({
555
- taskUUID,
556
- preProcessorType,
557
- guideImageUUID: image.newImageUUID,
558
- includeHandsAndFaceOpenPose
559
- }, returnBase64Image ? { returnBase64Image } : {}), compact(lowThresholdCanny, { lowThresholdCanny })), compact(highThresholdCanny, { highThresholdCanny }))
560
- });
492
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
493
+ inputImage: image.imageUUID,
494
+ taskType: "imageControlNetPreProcess" /* IMAGE_CONTROL_NET_PRE_PROCESS */,
495
+ taskUUID,
496
+ preProcessor
497
+ }, 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({
498
+ key: "highThresholdCanny",
499
+ value: highThresholdCanny
500
+ })), evaluateNonTrue({
501
+ key: "lowThresholdCanny",
502
+ value: lowThresholdCanny
503
+ })), evaluateNonTrue({
504
+ key: "includeHandsAndFaceOpenPose",
505
+ value: includeHandsAndFaceOpenPose
506
+ })));
561
507
  const lis = this.globalListener({
562
- responseKey: "newPreProcessControlNet",
563
- taskKey: "newPreProcessControlNet",
564
508
  taskUUID
565
509
  });
566
510
  const guideImage = await getIntervalWithPromise(
567
511
  ({ resolve, reject }) => {
568
- const uploadedImage = this._globalMessages[taskUUID];
512
+ const uploadedImage = this.getSingleMessage({
513
+ taskUUID
514
+ });
515
+ if (!uploadedImage)
516
+ return;
569
517
  if (uploadedImage == null ? void 0 : uploadedImage.error) {
570
518
  reject(uploadedImage);
571
519
  return true;
572
520
  }
573
521
  if (uploadedImage) {
574
- delete this._globalMessages[taskUUID];
575
- resolve(uploadedImage == null ? void 0 : uploadedImage.newPreProcessControlNet);
522
+ resolve(uploadedImage);
576
523
  return true;
577
524
  }
578
525
  },
@@ -585,38 +532,40 @@ var RunwareBase = class {
585
532
  }
586
533
  };
587
534
  this.requestImageToText = async ({
588
- imageInitiator
535
+ inputImage,
536
+ includeCost
589
537
  }) => {
590
538
  try {
591
539
  await this.ensureConnection();
592
540
  return await asyncRetry(async () => {
593
541
  const imageUploaded = await this.uploadImage(
594
- imageInitiator
542
+ inputImage
595
543
  );
596
- if (!(imageUploaded == null ? void 0 : imageUploaded.newImageUUID))
544
+ if (!(imageUploaded == null ? void 0 : imageUploaded.imageUUID))
597
545
  return null;
598
546
  const taskUUID = getUUID();
599
- this.send({
600
- newReverseImageClip: {
601
- imageUUID: imageUploaded.newImageUUID,
602
- taskUUID
603
- }
604
- });
547
+ this.send(__spreadValues({
548
+ taskUUID,
549
+ taskType: "imageCaption" /* IMAGE_CAPTION */,
550
+ inputImage: imageUploaded.imageUUID
551
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })));
605
552
  const lis = this.globalListener({
606
- responseKey: "newReverseClip",
607
- taskKey: "newReverseClip.texts",
608
553
  taskUUID
609
554
  });
610
555
  const response = await getIntervalWithPromise(
611
556
  ({ resolve, reject }) => {
612
- const newReverseClip = this._globalMessages[taskUUID];
557
+ const newReverseClip = this.getSingleMessage({
558
+ taskUUID
559
+ });
560
+ if (!newReverseClip)
561
+ return;
613
562
  if (newReverseClip == null ? void 0 : newReverseClip.error) {
614
563
  reject(newReverseClip);
615
564
  return true;
616
565
  }
617
566
  if (newReverseClip) {
618
567
  delete this._globalMessages[taskUUID];
619
- resolve(newReverseClip[0]);
568
+ resolve(newReverseClip);
620
569
  return true;
621
570
  }
622
571
  },
@@ -630,39 +579,59 @@ var RunwareBase = class {
630
579
  }
631
580
  };
632
581
  this.removeImageBackground = async ({
633
- imageInitiator
582
+ inputImage,
583
+ outputType,
584
+ outputFormat,
585
+ rgba,
586
+ postProcessMask,
587
+ returnOnlyMask,
588
+ alphaMatting,
589
+ alphaMattingForegroundThreshold,
590
+ alphaMattingBackgroundThreshold,
591
+ alphaMattingErodeSize,
592
+ includeCost
634
593
  }) => {
635
594
  try {
636
595
  await this.ensureConnection();
637
596
  return await asyncRetry(async () => {
638
597
  const imageUploaded = await this.uploadImage(
639
- imageInitiator
598
+ inputImage
640
599
  );
641
- if (!(imageUploaded == null ? void 0 : imageUploaded.newImageUUID))
600
+ if (!(imageUploaded == null ? void 0 : imageUploaded.imageUUID))
642
601
  return null;
643
602
  const taskUUID = getUUID();
644
- this.send({
645
- newRemoveBackground: {
646
- imageUUID: imageUploaded.newImageUUID,
647
- taskUUID,
648
- taskType: 8
649
- }
650
- });
603
+ this.send(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
604
+ taskType: "imageBackgroundRemoval" /* IMAGE_BACKGROUND_REMOVAL */,
605
+ taskUUID,
606
+ inputImage: 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 })));
651
620
  const lis = this.globalListener({
652
- responseKey: "newRemoveBackground",
653
- taskKey: "newRemoveBackground.images",
654
621
  taskUUID
655
622
  });
656
623
  const response = await getIntervalWithPromise(
657
624
  ({ resolve, reject }) => {
658
- const newRemoveBackground = this._globalMessages[taskUUID];
625
+ const newRemoveBackground = this.getSingleMessage({ taskUUID });
626
+ if (!newRemoveBackground)
627
+ return;
659
628
  if (newRemoveBackground == null ? void 0 : newRemoveBackground.error) {
660
629
  reject(newRemoveBackground);
661
630
  return true;
662
631
  }
663
632
  if (newRemoveBackground) {
664
633
  delete this._globalMessages[taskUUID];
665
- resolve(newRemoveBackground);
634
+ resolve([newRemoveBackground]);
666
635
  return true;
667
636
  }
668
637
  },
@@ -676,39 +645,41 @@ var RunwareBase = class {
676
645
  }
677
646
  };
678
647
  this.upscaleGan = async ({
679
- imageInitiator,
680
- upscaleFactor
648
+ inputImage,
649
+ upscaleFactor,
650
+ outputType,
651
+ outputFormat,
652
+ includeCost
681
653
  }) => {
682
654
  try {
683
655
  await this.ensureConnection();
684
656
  return await asyncRetry(async () => {
685
657
  let imageUploaded;
686
- imageUploaded = await this.uploadImage(imageInitiator);
687
- if (!(imageUploaded == null ? void 0 : imageUploaded.newImageUUID))
658
+ imageUploaded = await this.uploadImage(inputImage);
659
+ if (!(imageUploaded == null ? void 0 : imageUploaded.imageUUID))
688
660
  return null;
689
661
  const taskUUID = getUUID();
690
- this.send({
691
- newUpscaleGan: {
692
- imageUUID: imageUploaded == null ? void 0 : imageUploaded.newImageUUID,
693
- taskUUID,
694
- upscaleFactor
695
- }
696
- });
662
+ this.send(__spreadValues(__spreadValues(__spreadValues({
663
+ taskUUID,
664
+ inputImage: imageUploaded == null ? void 0 : imageUploaded.imageUUID,
665
+ taskType: "imageUpscale" /* IMAGE_UPSCALE */,
666
+ upscaleFactor
667
+ }, evaluateNonTrue({ key: "includeCost", value: includeCost })), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}));
697
668
  const lis = this.globalListener({
698
- responseKey: "newUpscaleGan",
699
- taskKey: "newUpscaleGan.images",
700
669
  taskUUID
701
670
  });
702
671
  const response = await getIntervalWithPromise(
703
672
  ({ resolve, reject }) => {
704
- const newUpscaleGan = this._globalMessages[taskUUID];
673
+ const newUpscaleGan = this.getSingleMessage({ taskUUID });
674
+ if (!newUpscaleGan)
675
+ return;
705
676
  if (newUpscaleGan == null ? void 0 : newUpscaleGan.error) {
706
677
  reject(newUpscaleGan);
707
678
  return true;
708
679
  }
709
680
  if (newUpscaleGan) {
710
681
  delete this._globalMessages[taskUUID];
711
- resolve(newUpscaleGan);
682
+ resolve([newUpscaleGan]);
712
683
  return true;
713
684
  }
714
685
  },
@@ -724,25 +695,22 @@ var RunwareBase = class {
724
695
  this.enhancePrompt = async ({
725
696
  prompt,
726
697
  promptMaxLength = 380,
727
- promptLanguageId = 1,
728
- promptVersions = 1
698
+ promptVersions = 1,
699
+ includeCost
729
700
  }) => {
730
701
  try {
731
702
  await this.ensureConnection();
732
703
  return await asyncRetry(async () => {
733
704
  const taskUUID = getUUID();
734
- this.send({
735
- newPromptEnhance: {
736
- prompt,
737
- taskUUID,
738
- promptMaxLength,
739
- promptVersions,
740
- promptLanguageId
741
- }
742
- });
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
+ }));
743
713
  const lis = this.globalListener({
744
- responseKey: "newPromptEnhancer",
745
- taskKey: "newPromptEnhancer.texts",
746
714
  taskUUID
747
715
  });
748
716
  const response = await getIntervalWithPromise(
@@ -767,6 +735,12 @@ var RunwareBase = class {
767
735
  throw e;
768
736
  }
769
737
  };
738
+ this.getSingleMessage = ({ taskUUID }) => {
739
+ var _a, _b;
740
+ if (!((_a = this._globalMessages[taskUUID]) == null ? void 0 : _a[0]))
741
+ return null;
742
+ return (_b = this._globalMessages[taskUUID]) == null ? void 0 : _b[0];
743
+ };
770
744
  this.connected = () => this.isWebsocketReadyState() && !!this._connectionSessionUUID;
771
745
  this._apiKey = apiKey;
772
746
  this._url = url;
@@ -794,14 +768,27 @@ var RunwareBase = class {
794
768
  // }
795
769
  addListener({
796
770
  lis,
797
- check,
798
- groupKey
771
+ // check,
772
+ groupKey,
773
+ taskUUID
799
774
  }) {
800
775
  const listener = (msg) => {
801
- if (msg == null ? void 0 : msg.error) {
802
- lis(msg);
803
- } else if (check(msg)) {
804
- lis(msg);
776
+ var _a;
777
+ const arrayMessage = Array.isArray(msg == null ? void 0 : msg.data) ? msg.data : [msg.data];
778
+ const arrayErrors = Array.isArray(msg == null ? void 0 : msg.errors) ? msg.errors : [msg.errors];
779
+ const filteredMessage = arrayMessage.filter(
780
+ (v) => (v == null ? void 0 : v.taskUUID) === taskUUID
781
+ );
782
+ const filteredErrors = arrayErrors.filter(
783
+ (v) => (v == null ? void 0 : v.taskUUID) === taskUUID
784
+ );
785
+ if (filteredErrors.length) {
786
+ lis({ error: __spreadValues({}, (_a = arrayErrors[0]) != null ? _a : {}) });
787
+ return;
788
+ }
789
+ if (filteredMessage.length) {
790
+ lis({ [taskUUID]: arrayMessage });
791
+ return;
805
792
  }
806
793
  };
807
794
  const groupListener = { key: getUUID(), listener, groupKey };
@@ -817,28 +804,24 @@ var RunwareBase = class {
817
804
  this._ws.onopen = (e) => {
818
805
  if (this._connectionSessionUUID) {
819
806
  this.send({
820
- newConnection: {
821
- apiKey: this._apiKey,
822
- connectionSessionUUID: this._connectionSessionUUID
823
- }
807
+ taskType: "authentication" /* AUTHENTICATION */,
808
+ apiKey: this._apiKey,
809
+ connectionSessionUUID: this._connectionSessionUUID
824
810
  });
825
811
  } else {
826
- this.send({ newConnection: { apiKey: this._apiKey } });
812
+ this.send({ apiKey: this._apiKey, taskType: "authentication" /* AUTHENTICATION */ });
827
813
  }
828
814
  this.addListener({
829
- check: (m) => {
830
- var _a;
831
- return (_a = m == null ? void 0 : m.newConnectionSessionUUID) == null ? void 0 : _a.connectionSessionUUID;
832
- },
815
+ taskUUID: "authentication" /* AUTHENTICATION */,
833
816
  lis: (m) => {
834
- var _a;
817
+ var _a, _b;
835
818
  if (m == null ? void 0 : m.error) {
836
819
  if (m.errorId === 19) {
837
820
  this._invalidAPIkey = "Invalid API key";
838
821
  }
839
822
  return;
840
823
  }
841
- this._connectionSessionUUID = (_a = m == null ? void 0 : m.newConnectionSessionUUID) == null ? void 0 : _a.connectionSessionUUID;
824
+ this._connectionSessionUUID = (_b = (_a = m == null ? void 0 : m["authentication" /* AUTHENTICATION */]) == null ? void 0 : _a[0]) == null ? void 0 : _b.connectionSessionUUID;
842
825
  this._invalidAPIkey = void 0;
843
826
  }
844
827
  });
@@ -868,13 +851,10 @@ var RunwareBase = class {
868
851
  groupKey
869
852
  }) {
870
853
  return this.addListener({
871
- check: (m) => {
872
- var _a;
873
- return (_a = m.newImages) == null ? void 0 : _a.images;
874
- },
854
+ taskUUID,
875
855
  lis: (m) => {
876
- var _a, _b, _c;
877
- const images = (_b = (_a = m.newImages) == null ? void 0 : _a.images) == null ? void 0 : _b.filter(
856
+ var _a, _b;
857
+ const images = (_a = m == null ? void 0 : m[taskUUID]) == null ? void 0 : _a.filter(
878
858
  (img) => img.taskUUID === taskUUID
879
859
  );
880
860
  onPartialImages == null ? void 0 : onPartialImages(images, (m == null ? void 0 : m.error) && m);
@@ -884,7 +864,7 @@ var RunwareBase = class {
884
864
  if (this._sdkType === "CLIENT" /* CLIENT */) {
885
865
  this._globalImages = [
886
866
  ...this._globalImages,
887
- ...(_c = m.newImages) == null ? void 0 : _c.images
867
+ ...(_b = m == null ? void 0 : m[taskUUID]) != null ? _b : []
888
868
  ];
889
869
  } else {
890
870
  this._globalImages = [...this._globalImages, ...images];
@@ -894,27 +874,25 @@ var RunwareBase = class {
894
874
  groupKey
895
875
  });
896
876
  }
897
- globalListener({
898
- responseKey,
899
- taskKey,
900
- taskUUID
901
- }) {
877
+ globalListener({ taskUUID }) {
902
878
  return this.addListener({
903
- check: (m) => {
904
- const value = accessDeepObject({
905
- key: responseKey,
906
- data: m,
907
- useZero: false
908
- });
909
- return !!value;
910
- },
879
+ // check: (m) => {
880
+ // const value = accessDeepObject({
881
+ // key: responseKey,
882
+ // data: m,
883
+ // useZero: false,
884
+ // });
885
+ // return !!value;
886
+ // },
887
+ // check: responseKey,
888
+ taskUUID,
911
889
  lis: (m) => {
912
890
  if (m.error) {
913
891
  this._globalMessages[taskUUID] = m;
914
892
  return;
915
893
  }
916
894
  const value = accessDeepObject({
917
- key: taskKey,
895
+ key: taskUUID,
918
896
  data: m,
919
897
  useZero: false
920
898
  });
@@ -933,24 +911,30 @@ var RunwareBase = class {
933
911
  });
934
912
  }
935
913
  async requestImages({
936
- modelId,
914
+ outputType,
915
+ outputFormat,
916
+ uploadEndpoint,
917
+ checkNsfw,
937
918
  positivePrompt,
938
- imageSize,
939
919
  negativePrompt,
940
- numberOfImages = 1,
941
- useCache = false,
942
- imageInitiator,
943
- controlNet,
944
- imageMaskInitiator,
920
+ seedImage,
921
+ maskImage,
922
+ strength,
923
+ height,
924
+ width,
925
+ model,
945
926
  steps,
946
- onPartialImages,
947
- lora,
948
- seed,
949
- gScale,
950
- checkNsfw,
951
- returnBase64Image,
952
927
  scheduler,
953
- usePromptWeighting
928
+ seed,
929
+ CFGScale,
930
+ clipSkip,
931
+ usePromptWeighting,
932
+ numberResults = 1,
933
+ controlNet,
934
+ lora,
935
+ useCache,
936
+ onPartialImages,
937
+ includeCost
954
938
  }) {
955
939
  let lis = void 0;
956
940
  let requestObject = void 0;
@@ -958,86 +942,68 @@ var RunwareBase = class {
958
942
  let retryCount = 0;
959
943
  try {
960
944
  await this.ensureConnection();
961
- let imageInitiatorUUID = null;
962
- let imageMaskInitiatorUUID = null;
945
+ let seedImageUUID = null;
946
+ let maskImageUUID = null;
963
947
  let controlNetData = [];
964
- if (imageInitiator) {
965
- const uploadedImage = await this.uploadImage(imageInitiator);
948
+ if (seedImage) {
949
+ const uploadedImage = await this.uploadImage(seedImage);
966
950
  if (!uploadedImage)
967
951
  return [];
968
- imageInitiatorUUID = uploadedImage.newImageUUID;
952
+ seedImageUUID = uploadedImage.imageUUID;
969
953
  }
970
- if (imageMaskInitiator) {
971
- const uploadedMaskInitiator = await this.uploadImage(
972
- imageMaskInitiator
973
- );
954
+ if (maskImage) {
955
+ const uploadedMaskInitiator = await this.uploadImage(maskImage);
974
956
  if (!uploadedMaskInitiator)
975
957
  return [];
976
- imageMaskInitiatorUUID = uploadedMaskInitiator.newImageUUID;
958
+ maskImageUUID = uploadedMaskInitiator.imageUUID;
977
959
  }
978
960
  if (controlNet == null ? void 0 : controlNet.length) {
979
961
  for (let i = 0; i < controlNet.length; i++) {
980
962
  const controlData = controlNet[i];
981
- const anyControlData = controlData;
982
963
  const {
983
964
  endStep,
984
- preprocessor,
985
965
  startStep,
986
966
  weight,
987
967
  guideImage,
988
- guideImageUnprocessed,
989
968
  controlMode,
990
- returnBase64Image: returnBase64Image2
969
+ startStepPercentage,
970
+ endStepPercentage,
971
+ model: controlNetModel
991
972
  } = controlData;
992
- const getCannyObject = () => {
993
- if (controlData.preprocessor === "canny") {
994
- return {
995
- lowThresholdCanny: anyControlData.lowThresholdCanny,
996
- highThresholdCanny: anyControlData.highThresholdCanny
997
- };
998
- } else
999
- return {};
1000
- };
1001
- const imageUploaded = await (guideImageUnprocessed ? this.uploadUnprocessedImage(__spreadProps(__spreadValues({
1002
- file: guideImageUnprocessed,
1003
- preProcessorType: getPreprocessorType(
1004
- preprocessor
1005
- ),
1006
- includeHandsAndFaceOpenPose: anyControlData.includeHandsAndFaceOpenPose
1007
- }, getCannyObject()), {
1008
- returnBase64Image: returnBase64Image2
1009
- })) : this.uploadImage(guideImage));
973
+ if (!guideImage)
974
+ return;
975
+ const imageUploaded = await this.uploadImage(
976
+ guideImage
977
+ );
1010
978
  if (!imageUploaded)
1011
- return [];
1012
- controlNetData.push(__spreadValues({
1013
- guideImageUUID: imageUploaded.newImageUUID,
979
+ return;
980
+ controlNetData.push(__spreadProps(__spreadValues(__spreadValues({
981
+ guideImage: imageUploaded.imageUUID,
982
+ model: controlNetModel,
1014
983
  endStep,
1015
- preprocessor,
1016
984
  startStep,
1017
- weight,
985
+ weight
986
+ }, evaluateNonTrue({
987
+ key: "startStepPercentage",
988
+ value: startStepPercentage
989
+ })), evaluateNonTrue({
990
+ key: "endStepPercentage",
991
+ value: endStepPercentage
992
+ })), {
1018
993
  controlMode: controlMode || "controlnet" /* CONTROL_NET */
1019
- }, getCannyObject()));
994
+ }));
1020
995
  }
1021
996
  }
1022
- const prompt = `${positivePrompt} ${negativePrompt ? `-no ${negativePrompt}` : ""}`.trim();
1023
- gScale = gScale != null ? gScale : 7;
1024
- requestObject = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
1025
- offset: 0,
1026
- modelId,
1027
- promptText: prompt,
1028
- numberResults: numberOfImages,
1029
- sizeId: imageSize,
1030
- taskType: getTaskType({
1031
- prompt,
1032
- controlNet,
1033
- imageInitiator,
1034
- imageMaskInitiator
1035
- }),
1036
- useCache
1037
- }, scheduler ? { scheduler } : {}), returnBase64Image ? { returnBase64Image } : {}), evaluateNonTrue({ key: "checkNsfw", value: checkNsfw })), evaluateNonTrue({ key: "gScale", value: gScale })), evaluateNonTrue({ key: "steps", value: steps })), evaluateNonTrue({
997
+ requestObject = __spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadProps(__spreadValues(__spreadValues(__spreadValues({
998
+ taskType: "imageInference" /* IMAGE_INFERENCE */,
999
+ model,
1000
+ positivePrompt
1001
+ }, negativePrompt ? { negativePrompt } : {}), height ? { height } : {}), width ? { width } : {}), {
1002
+ numberResults
1003
+ }), (lora == null ? void 0 : lora.length) ? { lora } : {}), outputType ? { outputType } : {}), outputFormat ? { outputFormat } : {}), uploadEndpoint ? { uploadEndpoint } : {}), evaluateNonTrue({ key: "checkNsfw", value: checkNsfw })), evaluateNonTrue({ key: "strength", value: strength })), evaluateNonTrue({ key: "CFGScale", value: CFGScale })), evaluateNonTrue({ key: "clipSkip", value: clipSkip })), evaluateNonTrue({
1038
1004
  key: "usePromptWeighting",
1039
1005
  value: usePromptWeighting
1040
- })), imageInitiatorUUID ? { imageInitiatorUUID } : {}), imageMaskInitiatorUUID ? { imageMaskInitiatorUUID } : {}), controlNetData.length ? { controlNet: controlNetData } : {}), (lora == null ? void 0 : lora.length) ? { lora } : {}), seed ? { seed } : {});
1006
+ })), evaluateNonTrue({ key: "steps", value: steps })), controlNetData.length ? { controlNet: controlNetData } : {}), seed ? { seed } : {}), scheduler ? { scheduler } : {}), evaluateNonTrue({ key: "includeCost", value: includeCost })), evaluateNonTrue({ key: "useCache", value: useCache })), seedImageUUID ? { seedImage: seedImageUUID } : {}), maskImageUUID ? { maskImage: maskImageUUID } : {});
1041
1007
  return await asyncRetry(
1042
1008
  async () => {
1043
1009
  retryCount++;
@@ -1047,22 +1013,20 @@ var RunwareBase = class {
1047
1013
  );
1048
1014
  const taskUUID = getUUID();
1049
1015
  taskUUIDs.push(taskUUID);
1050
- const imageRemaining = numberOfImages - imagesWithSimilarTask.length;
1051
- const newRequestObject = {
1052
- newTask: __spreadProps(__spreadValues({}, requestObject), {
1053
- taskUUID,
1054
- numberResults: imageRemaining
1055
- })
1056
- };
1016
+ const imageRemaining = numberResults - imagesWithSimilarTask.length;
1017
+ const newRequestObject = __spreadProps(__spreadValues({}, requestObject), {
1018
+ taskUUID,
1019
+ numberResults: imageRemaining
1020
+ });
1057
1021
  this.send(newRequestObject);
1058
1022
  lis = this.listenToImages({
1059
1023
  onPartialImages,
1060
1024
  taskUUID,
1061
1025
  groupKey: "REQUEST_IMAGES" /* REQUEST_IMAGES */
1062
1026
  });
1063
- const promise = await this.getSimililarImage({
1027
+ const promise = await this.getSimilarImages({
1064
1028
  taskUUID: taskUUIDs,
1065
- numberOfImages,
1029
+ numberResults,
1066
1030
  lis
1067
1031
  });
1068
1032
  return promise;
@@ -1080,25 +1044,9 @@ var RunwareBase = class {
1080
1044
  }
1081
1045
  }
1082
1046
  }
1083
- handleIncompleteImages({
1084
- taskUUIDs,
1085
- error
1086
- }) {
1087
- const imagesWithSimilarTask = this._globalImages.filter(
1088
- (img) => taskUUIDs.includes(img.taskUUID)
1089
- );
1090
- if (imagesWithSimilarTask.length > 1) {
1091
- this._globalImages = this._globalImages.filter(
1092
- (img) => !taskUUIDs.includes(img.taskUUID)
1093
- );
1094
- return imagesWithSimilarTask;
1095
- } else {
1096
- throw error;
1097
- }
1098
- }
1099
1047
  async ensureConnection() {
1100
1048
  var _a;
1101
- let isConnected = this.connected() && this._ws.readyState === 1;
1049
+ let isConnected = this.connected();
1102
1050
  try {
1103
1051
  if (this._invalidAPIkey)
1104
1052
  throw this._invalidAPIkey;
@@ -1110,9 +1058,9 @@ var RunwareBase = class {
1110
1058
  throw (_a = this._invalidAPIkey) != null ? _a : "Could not connect to server. Ensure your API key is correct";
1111
1059
  }
1112
1060
  }
1113
- async getSimililarImage({
1061
+ async getSimilarImages({
1114
1062
  taskUUID,
1115
- numberOfImages,
1063
+ numberResults,
1116
1064
  shouldThrowError,
1117
1065
  lis
1118
1066
  }) {
@@ -1128,20 +1076,34 @@ var RunwareBase = class {
1128
1076
  clearInterval(intervalId);
1129
1077
  reject == null ? void 0 : reject(newData);
1130
1078
  return true;
1131
- } else if (imagesWithSimilarTask.length >= numberOfImages) {
1079
+ } else if (imagesWithSimilarTask.length >= numberResults) {
1132
1080
  clearInterval(intervalId);
1133
1081
  this._globalImages = this._globalImages.filter(
1134
1082
  (img) => !taskUUIDs.includes(img.taskUUID)
1135
1083
  );
1136
- resolve(
1137
- [...imagesWithSimilarTask].slice(0, numberOfImages)
1138
- );
1084
+ resolve([...imagesWithSimilarTask].slice(0, numberResults));
1139
1085
  return true;
1140
1086
  }
1141
1087
  },
1142
1088
  { debugKey: "getting images", shouldThrowError }
1143
1089
  );
1144
1090
  }
1091
+ handleIncompleteImages({
1092
+ taskUUIDs,
1093
+ error
1094
+ }) {
1095
+ const imagesWithSimilarTask = this._globalImages.filter(
1096
+ (img) => taskUUIDs.includes(img.taskUUID)
1097
+ );
1098
+ if (imagesWithSimilarTask.length > 1) {
1099
+ this._globalImages = this._globalImages.filter(
1100
+ (img) => !taskUUIDs.includes(img.taskUUID)
1101
+ );
1102
+ return imagesWithSimilarTask;
1103
+ } else {
1104
+ throw error;
1105
+ }
1106
+ }
1145
1107
  //end of data
1146
1108
  };
1147
1109
 
@@ -1168,7 +1130,7 @@ var RunwareServer = class extends RunwareBase {
1168
1130
  this._listeners = [];
1169
1131
  this._reconnectingIntervalId = null;
1170
1132
  this.send = (msg) => {
1171
- this._ws.send(JSON.stringify(msg));
1133
+ this._ws.send(JSON.stringify([msg]));
1172
1134
  };
1173
1135
  this._sdkType = "SERVER" /* SERVER */;
1174
1136
  if (apiKey) {
@@ -1211,51 +1173,23 @@ var RunwareServer = class extends RunwareBase {
1211
1173
  });
1212
1174
  this._ws.on("close", () => this.handleClose());
1213
1175
  this._ws.on("open", () => {
1214
- var _a;
1215
1176
  if (this._reconnectingIntervalId) {
1216
1177
  clearInterval(this._reconnectingIntervalId);
1217
1178
  }
1218
1179
  if (this._connectionSessionUUID && this.isWebsocketReadyState()) {
1219
1180
  this.send({
1220
- newConnection: {
1221
- apiKey: this._apiKey,
1222
- connectionSessionUUID: this._connectionSessionUUID
1223
- }
1181
+ taskType: "authentication" /* AUTHENTICATION */,
1182
+ apiKey: this._apiKey,
1183
+ connectionSessionUUID: this._connectionSessionUUID
1224
1184
  });
1225
1185
  } else {
1226
1186
  if (this.isWebsocketReadyState()) {
1227
- this.send({ newConnection: { apiKey: this._apiKey } });
1187
+ this.send({
1188
+ apiKey: this._apiKey,
1189
+ taskType: "authentication" /* AUTHENTICATION */
1190
+ });
1228
1191
  }
1229
1192
  }
1230
- this.addListener({
1231
- check: (m) => {
1232
- var _a2;
1233
- return (_a2 = m == null ? void 0 : m.newConnectionSessionUUID) == null ? void 0 : _a2.connectionSessionUUID;
1234
- },
1235
- lis: (m) => {
1236
- var _a2;
1237
- if (m == null ? void 0 : m.error) {
1238
- if (m.errorId === 19) {
1239
- this._invalidAPIkey = "Invalid API key";
1240
- } else {
1241
- this._invalidAPIkey = "Error connection ";
1242
- }
1243
- return;
1244
- }
1245
- this._connectionSessionUUID = (_a2 = m == null ? void 0 : m.newConnectionSessionUUID) == null ? void 0 : _a2.connectionSessionUUID;
1246
- this._invalidAPIkey = void 0;
1247
- this.heartBeat();
1248
- }
1249
- });
1250
- (_a = this._pongListener) == null ? void 0 : _a.destroy();
1251
- this._pongListener = this.addListener({
1252
- check: (m) => m == null ? void 0 : m.pong,
1253
- lis: (m) => {
1254
- if (m.pong) {
1255
- this.heartBeat();
1256
- }
1257
- }
1258
- });
1259
1193
  });
1260
1194
  this._ws.on("message", (e, isBinary) => {
1261
1195
  const data = isBinary ? e : e == null ? void 0 : e.toString();
@@ -1295,6 +1229,7 @@ export {
1295
1229
  EOpenPosePreProcessor,
1296
1230
  EPreProcessor,
1297
1231
  EPreProcessorGroup,
1232
+ ETaskType,
1298
1233
  Environment,
1299
1234
  Runware,
1300
1235
  RunwareServer,