@gradio/client 0.9.3 → 0.10.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/CHANGELOG.md +13 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +396 -412
- package/package.json +1 -1
- package/src/client.ts +428 -445
package/src/client.ts
CHANGED
@@ -461,492 +461,481 @@ export function api_factory(
|
|
461
461
|
url_params = new URLSearchParams(window.location.search).toString();
|
462
462
|
}
|
463
463
|
|
464
|
-
handle_blob(
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
).then((_payload) => {
|
470
|
-
payload = { data: _payload || [], event_data, fn_index, trigger_id };
|
471
|
-
if (skip_queue(fn_index, config)) {
|
472
|
-
fire_event({
|
473
|
-
type: "status",
|
474
|
-
endpoint: _endpoint,
|
475
|
-
stage: "pending",
|
476
|
-
queue: false,
|
464
|
+
handle_blob(`${config.root}`, data, api_info, hf_token).then(
|
465
|
+
(_payload) => {
|
466
|
+
payload = {
|
467
|
+
data: _payload || [],
|
468
|
+
event_data,
|
477
469
|
fn_index,
|
478
|
-
|
479
|
-
}
|
470
|
+
trigger_id
|
471
|
+
};
|
472
|
+
if (skip_queue(fn_index, config)) {
|
473
|
+
fire_event({
|
474
|
+
type: "status",
|
475
|
+
endpoint: _endpoint,
|
476
|
+
stage: "pending",
|
477
|
+
queue: false,
|
478
|
+
fn_index,
|
479
|
+
time: new Date()
|
480
|
+
});
|
480
481
|
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
482
|
+
post_data(
|
483
|
+
`${config.root}/run${
|
484
|
+
_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`
|
485
|
+
}${url_params ? "?" + url_params : ""}`,
|
486
|
+
{
|
487
|
+
...payload,
|
488
|
+
session_hash
|
489
|
+
},
|
490
|
+
hf_token
|
491
|
+
)
|
492
|
+
.then(([output, status_code]) => {
|
493
|
+
const data = transform_files
|
494
|
+
? transform_output(
|
495
|
+
output.data,
|
496
|
+
api_info,
|
497
|
+
config.root,
|
498
|
+
config.root_url
|
499
|
+
)
|
500
|
+
: output.data;
|
501
|
+
if (status_code == 200) {
|
502
|
+
fire_event({
|
503
|
+
type: "data",
|
504
|
+
endpoint: _endpoint,
|
505
|
+
fn_index,
|
506
|
+
data: data,
|
507
|
+
time: new Date()
|
508
|
+
});
|
508
509
|
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
510
|
+
fire_event({
|
511
|
+
type: "status",
|
512
|
+
endpoint: _endpoint,
|
513
|
+
fn_index,
|
514
|
+
stage: "complete",
|
515
|
+
eta: output.average_duration,
|
516
|
+
queue: false,
|
517
|
+
time: new Date()
|
518
|
+
});
|
519
|
+
} else {
|
520
|
+
fire_event({
|
521
|
+
type: "status",
|
522
|
+
stage: "error",
|
523
|
+
endpoint: _endpoint,
|
524
|
+
fn_index,
|
525
|
+
message: output.error,
|
526
|
+
queue: false,
|
527
|
+
time: new Date()
|
528
|
+
});
|
529
|
+
}
|
530
|
+
})
|
531
|
+
.catch((e) => {
|
519
532
|
fire_event({
|
520
533
|
type: "status",
|
521
534
|
stage: "error",
|
535
|
+
message: e.message,
|
522
536
|
endpoint: _endpoint,
|
523
537
|
fn_index,
|
524
|
-
message: output.error,
|
525
538
|
queue: false,
|
526
539
|
time: new Date()
|
527
540
|
});
|
528
|
-
}
|
529
|
-
})
|
530
|
-
.catch((e) => {
|
531
|
-
fire_event({
|
532
|
-
type: "status",
|
533
|
-
stage: "error",
|
534
|
-
message: e.message,
|
535
|
-
endpoint: _endpoint,
|
536
|
-
fn_index,
|
537
|
-
queue: false,
|
538
|
-
time: new Date()
|
539
541
|
});
|
542
|
+
} else if (protocol == "ws") {
|
543
|
+
fire_event({
|
544
|
+
type: "status",
|
545
|
+
stage: "pending",
|
546
|
+
queue: true,
|
547
|
+
endpoint: _endpoint,
|
548
|
+
fn_index,
|
549
|
+
time: new Date()
|
540
550
|
});
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
endpoint: _endpoint,
|
547
|
-
fn_index,
|
548
|
-
time: new Date()
|
549
|
-
});
|
550
|
-
let url = new URL(`${ws_protocol}://${resolve_root(
|
551
|
-
host,
|
552
|
-
config.path,
|
553
|
-
true
|
554
|
-
)}
|
551
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
552
|
+
host,
|
553
|
+
config.path,
|
554
|
+
true
|
555
|
+
)}
|
555
556
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
556
557
|
|
557
|
-
|
558
|
-
|
559
|
-
}
|
560
|
-
|
561
|
-
websocket = new WebSocket(url);
|
562
|
-
|
563
|
-
websocket.onclose = (evt) => {
|
564
|
-
if (!evt.wasClean) {
|
565
|
-
fire_event({
|
566
|
-
type: "status",
|
567
|
-
stage: "error",
|
568
|
-
broken: true,
|
569
|
-
message: BROKEN_CONNECTION_MSG,
|
570
|
-
queue: true,
|
571
|
-
endpoint: _endpoint,
|
572
|
-
fn_index,
|
573
|
-
time: new Date()
|
574
|
-
});
|
558
|
+
if (jwt) {
|
559
|
+
url.searchParams.set("__sign", jwt);
|
575
560
|
}
|
576
|
-
};
|
577
561
|
|
578
|
-
|
579
|
-
const _data = JSON.parse(event.data);
|
580
|
-
const { type, status, data } = handle_message(
|
581
|
-
_data,
|
582
|
-
last_status[fn_index]
|
583
|
-
);
|
562
|
+
websocket = new WebSocket(url);
|
584
563
|
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
564
|
+
websocket.onclose = (evt) => {
|
565
|
+
if (!evt.wasClean) {
|
566
|
+
fire_event({
|
567
|
+
type: "status",
|
568
|
+
stage: "error",
|
569
|
+
broken: true,
|
570
|
+
message: BROKEN_CONNECTION_MSG,
|
571
|
+
queue: true,
|
572
|
+
endpoint: _endpoint,
|
573
|
+
fn_index,
|
574
|
+
time: new Date()
|
575
|
+
});
|
596
576
|
}
|
597
|
-
}
|
598
|
-
websocket.send(JSON.stringify({ fn_index, session_hash }));
|
599
|
-
return;
|
600
|
-
} else if (type === "data") {
|
601
|
-
websocket.send(JSON.stringify({ ...payload, session_hash }));
|
602
|
-
} else if (type === "complete") {
|
603
|
-
complete = status;
|
604
|
-
} else if (type === "log") {
|
605
|
-
fire_event({
|
606
|
-
type: "log",
|
607
|
-
log: data.log,
|
608
|
-
level: data.level,
|
609
|
-
endpoint: _endpoint,
|
610
|
-
fn_index
|
611
|
-
});
|
612
|
-
} else if (type === "generating") {
|
613
|
-
fire_event({
|
614
|
-
type: "status",
|
615
|
-
time: new Date(),
|
616
|
-
...status,
|
617
|
-
stage: status?.stage!,
|
618
|
-
queue: true,
|
619
|
-
endpoint: _endpoint,
|
620
|
-
fn_index
|
621
|
-
});
|
622
|
-
}
|
623
|
-
if (data) {
|
624
|
-
fire_event({
|
625
|
-
type: "data",
|
626
|
-
time: new Date(),
|
627
|
-
data: transform_files
|
628
|
-
? transform_output(
|
629
|
-
data.data,
|
630
|
-
api_info,
|
631
|
-
config.root,
|
632
|
-
config.root_url
|
633
|
-
)
|
634
|
-
: data.data,
|
635
|
-
endpoint: _endpoint,
|
636
|
-
fn_index
|
637
|
-
});
|
577
|
+
};
|
638
578
|
|
639
|
-
|
579
|
+
websocket.onmessage = function (event) {
|
580
|
+
const _data = JSON.parse(event.data);
|
581
|
+
const { type, status, data } = handle_message(
|
582
|
+
_data,
|
583
|
+
last_status[fn_index]
|
584
|
+
);
|
585
|
+
|
586
|
+
if (type === "update" && status && !complete) {
|
587
|
+
// call 'status' listeners
|
588
|
+
fire_event({
|
589
|
+
type: "status",
|
590
|
+
endpoint: _endpoint,
|
591
|
+
fn_index,
|
592
|
+
time: new Date(),
|
593
|
+
...status
|
594
|
+
});
|
595
|
+
if (status.stage === "error") {
|
596
|
+
websocket.close();
|
597
|
+
}
|
598
|
+
} else if (type === "hash") {
|
599
|
+
websocket.send(JSON.stringify({ fn_index, session_hash }));
|
600
|
+
return;
|
601
|
+
} else if (type === "data") {
|
602
|
+
websocket.send(JSON.stringify({ ...payload, session_hash }));
|
603
|
+
} else if (type === "complete") {
|
604
|
+
complete = status;
|
605
|
+
} else if (type === "log") {
|
606
|
+
fire_event({
|
607
|
+
type: "log",
|
608
|
+
log: data.log,
|
609
|
+
level: data.level,
|
610
|
+
endpoint: _endpoint,
|
611
|
+
fn_index
|
612
|
+
});
|
613
|
+
} else if (type === "generating") {
|
640
614
|
fire_event({
|
641
615
|
type: "status",
|
642
616
|
time: new Date(),
|
643
|
-
...
|
617
|
+
...status,
|
644
618
|
stage: status?.stage!,
|
645
619
|
queue: true,
|
646
620
|
endpoint: _endpoint,
|
647
621
|
fn_index
|
648
622
|
});
|
649
|
-
websocket.close();
|
650
623
|
}
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
endpoint: _endpoint,
|
667
|
-
fn_index,
|
668
|
-
time: new Date()
|
669
|
-
});
|
670
|
-
var params = new URLSearchParams({
|
671
|
-
fn_index: fn_index.toString(),
|
672
|
-
session_hash: session_hash
|
673
|
-
}).toString();
|
674
|
-
let url = new URL(
|
675
|
-
`${http_protocol}//${resolve_root(
|
676
|
-
host,
|
677
|
-
config.path,
|
678
|
-
true
|
679
|
-
)}/queue/join?${url_params ? url_params + "&" : ""}${params}`
|
680
|
-
);
|
624
|
+
if (data) {
|
625
|
+
fire_event({
|
626
|
+
type: "data",
|
627
|
+
time: new Date(),
|
628
|
+
data: transform_files
|
629
|
+
? transform_output(
|
630
|
+
data.data,
|
631
|
+
api_info,
|
632
|
+
config.root,
|
633
|
+
config.root_url
|
634
|
+
)
|
635
|
+
: data.data,
|
636
|
+
endpoint: _endpoint,
|
637
|
+
fn_index
|
638
|
+
});
|
681
639
|
|
682
|
-
|
640
|
+
if (complete) {
|
641
|
+
fire_event({
|
642
|
+
type: "status",
|
643
|
+
time: new Date(),
|
644
|
+
...complete,
|
645
|
+
stage: status?.stage!,
|
646
|
+
queue: true,
|
647
|
+
endpoint: _endpoint,
|
648
|
+
fn_index
|
649
|
+
});
|
650
|
+
websocket.close();
|
651
|
+
}
|
652
|
+
}
|
653
|
+
};
|
683
654
|
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
655
|
+
// different ws contract for gradio versions older than 3.6.0
|
656
|
+
//@ts-ignore
|
657
|
+
if (semiver(config.version || "2.0.0", "3.6") < 0) {
|
658
|
+
addEventListener("open", () =>
|
659
|
+
websocket.send(JSON.stringify({ hash: session_hash }))
|
660
|
+
);
|
661
|
+
}
|
662
|
+
} else if (protocol == "sse") {
|
663
|
+
fire_event({
|
664
|
+
type: "status",
|
665
|
+
stage: "pending",
|
666
|
+
queue: true,
|
667
|
+
endpoint: _endpoint,
|
668
|
+
fn_index,
|
669
|
+
time: new Date()
|
670
|
+
});
|
671
|
+
var params = new URLSearchParams({
|
672
|
+
fn_index: fn_index.toString(),
|
673
|
+
session_hash: session_hash
|
674
|
+
}).toString();
|
675
|
+
let url = new URL(
|
676
|
+
`${config.root}/queue/join?${
|
677
|
+
url_params ? url_params + "&" : ""
|
678
|
+
}${params}`
|
689
679
|
);
|
690
680
|
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
...status
|
699
|
-
});
|
700
|
-
if (status.stage === "error") {
|
701
|
-
eventSource.close();
|
702
|
-
}
|
703
|
-
} else if (type === "data") {
|
704
|
-
event_id = _data.event_id as string;
|
705
|
-
let [_, status] = await post_data(
|
706
|
-
`${http_protocol}//${resolve_root(
|
707
|
-
host,
|
708
|
-
config.path,
|
709
|
-
true
|
710
|
-
)}/queue/data`,
|
711
|
-
{
|
712
|
-
...payload,
|
713
|
-
session_hash,
|
714
|
-
event_id
|
715
|
-
},
|
716
|
-
hf_token
|
681
|
+
eventSource = EventSource_factory(url);
|
682
|
+
|
683
|
+
eventSource.onmessage = async function (event) {
|
684
|
+
const _data = JSON.parse(event.data);
|
685
|
+
const { type, status, data } = handle_message(
|
686
|
+
_data,
|
687
|
+
last_status[fn_index]
|
717
688
|
);
|
718
|
-
|
689
|
+
|
690
|
+
if (type === "update" && status && !complete) {
|
691
|
+
// call 'status' listeners
|
719
692
|
fire_event({
|
720
693
|
type: "status",
|
721
|
-
stage: "error",
|
722
|
-
message: BROKEN_CONNECTION_MSG,
|
723
|
-
queue: true,
|
724
694
|
endpoint: _endpoint,
|
725
695
|
fn_index,
|
726
|
-
time: new Date()
|
696
|
+
time: new Date(),
|
697
|
+
...status
|
727
698
|
});
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
});
|
766
|
-
|
767
|
-
if (complete) {
|
699
|
+
if (status.stage === "error") {
|
700
|
+
eventSource.close();
|
701
|
+
}
|
702
|
+
} else if (type === "data") {
|
703
|
+
event_id = _data.event_id as string;
|
704
|
+
let [_, status] = await post_data(
|
705
|
+
`${config.root}/queue/data`,
|
706
|
+
{
|
707
|
+
...payload,
|
708
|
+
session_hash,
|
709
|
+
event_id
|
710
|
+
},
|
711
|
+
hf_token
|
712
|
+
);
|
713
|
+
if (status !== 200) {
|
714
|
+
fire_event({
|
715
|
+
type: "status",
|
716
|
+
stage: "error",
|
717
|
+
message: BROKEN_CONNECTION_MSG,
|
718
|
+
queue: true,
|
719
|
+
endpoint: _endpoint,
|
720
|
+
fn_index,
|
721
|
+
time: new Date()
|
722
|
+
});
|
723
|
+
eventSource.close();
|
724
|
+
}
|
725
|
+
} else if (type === "complete") {
|
726
|
+
complete = status;
|
727
|
+
} else if (type === "log") {
|
728
|
+
fire_event({
|
729
|
+
type: "log",
|
730
|
+
log: data.log,
|
731
|
+
level: data.level,
|
732
|
+
endpoint: _endpoint,
|
733
|
+
fn_index
|
734
|
+
});
|
735
|
+
} else if (type === "generating") {
|
768
736
|
fire_event({
|
769
737
|
type: "status",
|
770
738
|
time: new Date(),
|
771
|
-
...
|
739
|
+
...status,
|
772
740
|
stage: status?.stage!,
|
773
741
|
queue: true,
|
774
742
|
endpoint: _endpoint,
|
775
743
|
fn_index
|
776
744
|
});
|
777
|
-
eventSource.close();
|
778
745
|
}
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
|
794
|
-
config.path,
|
795
|
-
true
|
796
|
-
)}/queue/join?${url_params}`,
|
797
|
-
{
|
798
|
-
...payload,
|
799
|
-
session_hash
|
800
|
-
},
|
801
|
-
hf_token
|
802
|
-
).then(([response, status]) => {
|
803
|
-
if (status === 503) {
|
804
|
-
fire_event({
|
805
|
-
type: "status",
|
806
|
-
stage: "error",
|
807
|
-
message: QUEUE_FULL_MSG,
|
808
|
-
queue: true,
|
809
|
-
endpoint: _endpoint,
|
810
|
-
fn_index,
|
811
|
-
time: new Date()
|
812
|
-
});
|
813
|
-
} else if (status !== 200) {
|
814
|
-
fire_event({
|
815
|
-
type: "status",
|
816
|
-
stage: "error",
|
817
|
-
message: BROKEN_CONNECTION_MSG,
|
818
|
-
queue: true,
|
819
|
-
endpoint: _endpoint,
|
820
|
-
fn_index,
|
821
|
-
time: new Date()
|
822
|
-
});
|
823
|
-
} else {
|
824
|
-
event_id = response.event_id as string;
|
825
|
-
let callback = async function (_data: object): void {
|
826
|
-
try {
|
827
|
-
const { type, status, data } = handle_message(
|
828
|
-
_data,
|
829
|
-
last_status[fn_index]
|
830
|
-
);
|
831
|
-
|
832
|
-
// TODO: Find out how to print this information
|
833
|
-
// only during testing
|
834
|
-
// console.info("data", type, status, data);
|
746
|
+
if (data) {
|
747
|
+
fire_event({
|
748
|
+
type: "data",
|
749
|
+
time: new Date(),
|
750
|
+
data: transform_files
|
751
|
+
? transform_output(
|
752
|
+
data.data,
|
753
|
+
api_info,
|
754
|
+
config.root,
|
755
|
+
config.root_url
|
756
|
+
)
|
757
|
+
: data.data,
|
758
|
+
endpoint: _endpoint,
|
759
|
+
fn_index
|
760
|
+
});
|
835
761
|
|
836
|
-
|
837
|
-
|
838
|
-
|
762
|
+
if (complete) {
|
763
|
+
fire_event({
|
764
|
+
type: "status",
|
765
|
+
time: new Date(),
|
766
|
+
...complete,
|
767
|
+
stage: status?.stage!,
|
768
|
+
queue: true,
|
769
|
+
endpoint: _endpoint,
|
770
|
+
fn_index
|
771
|
+
});
|
772
|
+
eventSource.close();
|
773
|
+
}
|
774
|
+
}
|
775
|
+
};
|
776
|
+
} else if (protocol == "sse_v1") {
|
777
|
+
fire_event({
|
778
|
+
type: "status",
|
779
|
+
stage: "pending",
|
780
|
+
queue: true,
|
781
|
+
endpoint: _endpoint,
|
782
|
+
fn_index,
|
783
|
+
time: new Date()
|
784
|
+
});
|
839
785
|
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
|
849
|
-
|
850
|
-
|
851
|
-
|
852
|
-
|
853
|
-
|
854
|
-
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
860
|
-
|
861
|
-
|
862
|
-
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
|
871
|
-
|
872
|
-
|
873
|
-
|
874
|
-
|
875
|
-
|
876
|
-
|
877
|
-
|
878
|
-
|
879
|
-
|
880
|
-
});
|
881
|
-
}
|
882
|
-
if (data) {
|
883
|
-
fire_event({
|
884
|
-
type: "data",
|
885
|
-
time: new Date(),
|
886
|
-
data: transform_files
|
887
|
-
? transform_output(
|
888
|
-
data.data,
|
889
|
-
api_info,
|
890
|
-
config.root,
|
891
|
-
config.root_url
|
892
|
-
)
|
893
|
-
: data.data,
|
894
|
-
endpoint: _endpoint,
|
895
|
-
fn_index
|
896
|
-
});
|
786
|
+
post_data(
|
787
|
+
`${config.root}/queue/join?${url_params}`,
|
788
|
+
{
|
789
|
+
...payload,
|
790
|
+
session_hash
|
791
|
+
},
|
792
|
+
hf_token
|
793
|
+
).then(([response, status]) => {
|
794
|
+
if (status === 503) {
|
795
|
+
fire_event({
|
796
|
+
type: "status",
|
797
|
+
stage: "error",
|
798
|
+
message: QUEUE_FULL_MSG,
|
799
|
+
queue: true,
|
800
|
+
endpoint: _endpoint,
|
801
|
+
fn_index,
|
802
|
+
time: new Date()
|
803
|
+
});
|
804
|
+
} else if (status !== 200) {
|
805
|
+
fire_event({
|
806
|
+
type: "status",
|
807
|
+
stage: "error",
|
808
|
+
message: BROKEN_CONNECTION_MSG,
|
809
|
+
queue: true,
|
810
|
+
endpoint: _endpoint,
|
811
|
+
fn_index,
|
812
|
+
time: new Date()
|
813
|
+
});
|
814
|
+
} else {
|
815
|
+
event_id = response.event_id as string;
|
816
|
+
let callback = async function (_data: object): void {
|
817
|
+
try {
|
818
|
+
const { type, status, data } = handle_message(
|
819
|
+
_data,
|
820
|
+
last_status[fn_index]
|
821
|
+
);
|
822
|
+
|
823
|
+
if (type == "heartbeat") {
|
824
|
+
return;
|
825
|
+
}
|
897
826
|
|
898
|
-
if (complete) {
|
827
|
+
if (type === "update" && status && !complete) {
|
828
|
+
// call 'status' listeners
|
829
|
+
fire_event({
|
830
|
+
type: "status",
|
831
|
+
endpoint: _endpoint,
|
832
|
+
fn_index,
|
833
|
+
time: new Date(),
|
834
|
+
...status
|
835
|
+
});
|
836
|
+
} else if (type === "complete") {
|
837
|
+
complete = status;
|
838
|
+
} else if (type == "unexpected_error") {
|
839
|
+
console.error("Unexpected error", status?.message);
|
840
|
+
fire_event({
|
841
|
+
type: "status",
|
842
|
+
stage: "error",
|
843
|
+
message:
|
844
|
+
status?.message || "An Unexpected Error Occurred!",
|
845
|
+
queue: true,
|
846
|
+
endpoint: _endpoint,
|
847
|
+
fn_index,
|
848
|
+
time: new Date()
|
849
|
+
});
|
850
|
+
} else if (type === "log") {
|
851
|
+
fire_event({
|
852
|
+
type: "log",
|
853
|
+
log: data.log,
|
854
|
+
level: data.level,
|
855
|
+
endpoint: _endpoint,
|
856
|
+
fn_index
|
857
|
+
});
|
858
|
+
return;
|
859
|
+
} else if (type === "generating") {
|
899
860
|
fire_event({
|
900
861
|
type: "status",
|
901
862
|
time: new Date(),
|
902
|
-
...
|
863
|
+
...status,
|
903
864
|
stage: status?.stage!,
|
904
865
|
queue: true,
|
905
866
|
endpoint: _endpoint,
|
906
867
|
fn_index
|
907
868
|
});
|
908
869
|
}
|
909
|
-
|
870
|
+
if (data) {
|
871
|
+
fire_event({
|
872
|
+
type: "data",
|
873
|
+
time: new Date(),
|
874
|
+
data: transform_files
|
875
|
+
? transform_output(
|
876
|
+
data.data,
|
877
|
+
api_info,
|
878
|
+
config.root,
|
879
|
+
config.root_url
|
880
|
+
)
|
881
|
+
: data.data,
|
882
|
+
endpoint: _endpoint,
|
883
|
+
fn_index
|
884
|
+
});
|
885
|
+
|
886
|
+
if (complete) {
|
887
|
+
fire_event({
|
888
|
+
type: "status",
|
889
|
+
time: new Date(),
|
890
|
+
...complete,
|
891
|
+
stage: status?.stage!,
|
892
|
+
queue: true,
|
893
|
+
endpoint: _endpoint,
|
894
|
+
fn_index
|
895
|
+
});
|
896
|
+
}
|
897
|
+
}
|
910
898
|
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
918
|
-
|
899
|
+
if (
|
900
|
+
status?.stage === "complete" ||
|
901
|
+
status?.stage === "error"
|
902
|
+
) {
|
903
|
+
if (event_callbacks[event_id]) {
|
904
|
+
delete event_callbacks[event_id];
|
905
|
+
if (Object.keys(event_callbacks).length === 0) {
|
906
|
+
close_stream();
|
907
|
+
}
|
919
908
|
}
|
920
909
|
}
|
910
|
+
} catch (e) {
|
911
|
+
console.error("Unexpected client exception", e);
|
912
|
+
fire_event({
|
913
|
+
type: "status",
|
914
|
+
stage: "error",
|
915
|
+
message: "An Unexpected Error Occurred!",
|
916
|
+
queue: true,
|
917
|
+
endpoint: _endpoint,
|
918
|
+
fn_index,
|
919
|
+
time: new Date()
|
920
|
+
});
|
921
|
+
close_stream();
|
921
922
|
}
|
922
|
-
}
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
|
932
|
-
});
|
933
|
-
close_stream();
|
923
|
+
};
|
924
|
+
if (event_id in pending_stream_messages) {
|
925
|
+
pending_stream_messages[event_id].forEach((msg) =>
|
926
|
+
callback(msg)
|
927
|
+
);
|
928
|
+
delete pending_stream_messages[event_id];
|
929
|
+
}
|
930
|
+
event_callbacks[event_id] = callback;
|
931
|
+
if (!stream_open) {
|
932
|
+
open_stream();
|
934
933
|
}
|
935
|
-
};
|
936
|
-
if (event_id in pending_stream_messages) {
|
937
|
-
pending_stream_messages[event_id].forEach((msg) =>
|
938
|
-
callback(msg)
|
939
|
-
);
|
940
|
-
delete pending_stream_messages[event_id];
|
941
|
-
}
|
942
|
-
event_callbacks[event_id] = callback;
|
943
|
-
if (!stream_open) {
|
944
|
-
open_stream();
|
945
934
|
}
|
946
|
-
}
|
947
|
-
}
|
935
|
+
});
|
936
|
+
}
|
948
937
|
}
|
949
|
-
|
938
|
+
);
|
950
939
|
|
951
940
|
function fire_event<K extends EventType>(event: Event<K>): void {
|
952
941
|
const narrowed_listener_map: ListenerMap<K> = listener_map;
|
@@ -1008,18 +997,11 @@ export function api_factory(
|
|
1008
997
|
}
|
1009
998
|
|
1010
999
|
try {
|
1011
|
-
await fetch_implementation(
|
1012
|
-
|
1013
|
-
|
1014
|
-
|
1015
|
-
|
1016
|
-
)}/reset`,
|
1017
|
-
{
|
1018
|
-
headers: { "Content-Type": "application/json" },
|
1019
|
-
method: "POST",
|
1020
|
-
body: JSON.stringify(cancel_request)
|
1021
|
-
}
|
1022
|
-
);
|
1000
|
+
await fetch_implementation(`${config.root}/reset`, {
|
1001
|
+
headers: { "Content-Type": "application/json" },
|
1002
|
+
method: "POST",
|
1003
|
+
body: JSON.stringify(cancel_request)
|
1004
|
+
});
|
1023
1005
|
} catch (e) {
|
1024
1006
|
console.warn(
|
1025
1007
|
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
@@ -1048,14 +1030,8 @@ export function api_factory(
|
|
1048
1030
|
let params = new URLSearchParams({
|
1049
1031
|
session_hash: session_hash
|
1050
1032
|
}).toString();
|
1051
|
-
let url = new URL(
|
1052
|
-
|
1053
|
-
host,
|
1054
|
-
config.path,
|
1055
|
-
true
|
1056
|
-
)}/queue/data?${params}`
|
1057
|
-
);
|
1058
|
-
event_stream = new EventSource(url);
|
1033
|
+
let url = new URL(`${config.root}/queue/data?${params}`);
|
1034
|
+
event_stream = EventSource_factory(url);
|
1059
1035
|
event_stream.onmessage = async function (event) {
|
1060
1036
|
let _data = JSON.parse(event.data);
|
1061
1037
|
const event_id = _data.event_id;
|
@@ -1074,6 +1050,17 @@ export function api_factory(
|
|
1074
1050
|
pending_stream_messages[event_id].push(_data);
|
1075
1051
|
}
|
1076
1052
|
};
|
1053
|
+
event_stream.onerror = async function (event) {
|
1054
|
+
await Promise.all(
|
1055
|
+
Object.keys(event_callbacks).map((event_id) =>
|
1056
|
+
event_callbacks[event_id]({
|
1057
|
+
msg: "unexpected_error",
|
1058
|
+
message: BROKEN_CONNECTION_MSG
|
1059
|
+
})
|
1060
|
+
)
|
1061
|
+
);
|
1062
|
+
close_stream();
|
1063
|
+
};
|
1077
1064
|
}
|
1078
1065
|
|
1079
1066
|
function close_stream(): void {
|
@@ -1100,14 +1087,10 @@ export function api_factory(
|
|
1100
1087
|
if (component?.props?.root_url) {
|
1101
1088
|
root_url = component.props.root_url;
|
1102
1089
|
} else {
|
1103
|
-
root_url =
|
1104
|
-
host,
|
1105
|
-
config.path,
|
1106
|
-
true
|
1107
|
-
)}/`;
|
1090
|
+
root_url = config.root;
|
1108
1091
|
}
|
1109
1092
|
const response = await fetch_implementation(
|
1110
|
-
`${root_url}component_server/`,
|
1093
|
+
`${root_url}/component_server/`,
|
1111
1094
|
{
|
1112
1095
|
method: "POST",
|
1113
1096
|
body: JSON.stringify({
|