@gradio/client 0.9.2 → 0.9.4
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 +12 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/index.js +398 -412
- package/package.json +1 -1
- package/src/client.ts +432 -444
package/src/client.ts
CHANGED
@@ -287,6 +287,7 @@ export function api_factory(
|
|
287
287
|
const session_hash = Math.random().toString(36).substring(2);
|
288
288
|
const last_status: Record<string, Status["stage"]> = {};
|
289
289
|
let stream_open = false;
|
290
|
+
let pending_stream_messages: Record<string, any[]> = {}; // Event messages may be received by the SSE stream before the initial data POST request is complete. To resolve this race condition, we store the messages in a dictionary and process them when the POST request is complete.
|
290
291
|
let event_stream: EventSource | null = null;
|
291
292
|
const event_callbacks: Record<string, () => Promise<void>> = {};
|
292
293
|
let config: Config;
|
@@ -460,486 +461,484 @@ export function api_factory(
|
|
460
461
|
url_params = new URLSearchParams(window.location.search).toString();
|
461
462
|
}
|
462
463
|
|
463
|
-
handle_blob(
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
).then((_payload) => {
|
469
|
-
payload = { data: _payload || [], event_data, fn_index, trigger_id };
|
470
|
-
if (skip_queue(fn_index, config)) {
|
471
|
-
fire_event({
|
472
|
-
type: "status",
|
473
|
-
endpoint: _endpoint,
|
474
|
-
stage: "pending",
|
475
|
-
queue: false,
|
464
|
+
handle_blob(`${config.root}`, data, api_info, hf_token).then(
|
465
|
+
(_payload) => {
|
466
|
+
payload = {
|
467
|
+
data: _payload || [],
|
468
|
+
event_data,
|
476
469
|
fn_index,
|
477
|
-
|
478
|
-
}
|
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
|
+
});
|
479
481
|
|
480
|
-
|
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
|
-
|
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
|
+
});
|
507
509
|
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
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) => {
|
518
532
|
fire_event({
|
519
533
|
type: "status",
|
520
534
|
stage: "error",
|
535
|
+
message: e.message,
|
521
536
|
endpoint: _endpoint,
|
522
537
|
fn_index,
|
523
|
-
message: output.error,
|
524
538
|
queue: false,
|
525
539
|
time: new Date()
|
526
540
|
});
|
527
|
-
}
|
528
|
-
})
|
529
|
-
.catch((e) => {
|
530
|
-
fire_event({
|
531
|
-
type: "status",
|
532
|
-
stage: "error",
|
533
|
-
message: e.message,
|
534
|
-
endpoint: _endpoint,
|
535
|
-
fn_index,
|
536
|
-
queue: false,
|
537
|
-
time: new Date()
|
538
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()
|
539
550
|
});
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
endpoint: _endpoint,
|
546
|
-
fn_index,
|
547
|
-
time: new Date()
|
548
|
-
});
|
549
|
-
let url = new URL(`${ws_protocol}://${resolve_root(
|
550
|
-
host,
|
551
|
-
config.path,
|
552
|
-
true
|
553
|
-
)}
|
551
|
+
let url = new URL(`${ws_protocol}://${resolve_root(
|
552
|
+
host,
|
553
|
+
config.path,
|
554
|
+
true
|
555
|
+
)}
|
554
556
|
/queue/join${url_params ? "?" + url_params : ""}`);
|
555
557
|
|
556
|
-
|
557
|
-
|
558
|
-
}
|
559
|
-
|
560
|
-
websocket = new WebSocket(url);
|
561
|
-
|
562
|
-
websocket.onclose = (evt) => {
|
563
|
-
if (!evt.wasClean) {
|
564
|
-
fire_event({
|
565
|
-
type: "status",
|
566
|
-
stage: "error",
|
567
|
-
broken: true,
|
568
|
-
message: BROKEN_CONNECTION_MSG,
|
569
|
-
queue: true,
|
570
|
-
endpoint: _endpoint,
|
571
|
-
fn_index,
|
572
|
-
time: new Date()
|
573
|
-
});
|
558
|
+
if (jwt) {
|
559
|
+
url.searchParams.set("__sign", jwt);
|
574
560
|
}
|
575
|
-
};
|
576
561
|
|
577
|
-
|
578
|
-
const _data = JSON.parse(event.data);
|
579
|
-
const { type, status, data } = handle_message(
|
580
|
-
_data,
|
581
|
-
last_status[fn_index]
|
582
|
-
);
|
562
|
+
websocket = new WebSocket(url);
|
583
563
|
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
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
|
+
});
|
595
576
|
}
|
596
|
-
}
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
fire_event({
|
605
|
-
type: "log",
|
606
|
-
log: data.log,
|
607
|
-
level: data.level,
|
608
|
-
endpoint: _endpoint,
|
609
|
-
fn_index
|
610
|
-
});
|
611
|
-
} else if (type === "generating") {
|
612
|
-
fire_event({
|
613
|
-
type: "status",
|
614
|
-
time: new Date(),
|
615
|
-
...status,
|
616
|
-
stage: status?.stage!,
|
617
|
-
queue: true,
|
618
|
-
endpoint: _endpoint,
|
619
|
-
fn_index
|
620
|
-
});
|
621
|
-
}
|
622
|
-
if (data) {
|
623
|
-
fire_event({
|
624
|
-
type: "data",
|
625
|
-
time: new Date(),
|
626
|
-
data: transform_files
|
627
|
-
? transform_output(
|
628
|
-
data.data,
|
629
|
-
api_info,
|
630
|
-
config.root,
|
631
|
-
config.root_url
|
632
|
-
)
|
633
|
-
: data.data,
|
634
|
-
endpoint: _endpoint,
|
635
|
-
fn_index
|
636
|
-
});
|
577
|
+
};
|
578
|
+
|
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
|
+
);
|
637
585
|
|
638
|
-
if (complete) {
|
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") {
|
639
614
|
fire_event({
|
640
615
|
type: "status",
|
641
616
|
time: new Date(),
|
642
|
-
...
|
617
|
+
...status,
|
643
618
|
stage: status?.stage!,
|
644
619
|
queue: true,
|
645
620
|
endpoint: _endpoint,
|
646
621
|
fn_index
|
647
622
|
});
|
648
|
-
websocket.close();
|
649
623
|
}
|
650
|
-
|
651
|
-
|
652
|
-
|
653
|
-
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
|
660
|
-
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
endpoint: _endpoint,
|
666
|
-
fn_index,
|
667
|
-
time: new Date()
|
668
|
-
});
|
669
|
-
var params = new URLSearchParams({
|
670
|
-
fn_index: fn_index.toString(),
|
671
|
-
session_hash: session_hash
|
672
|
-
}).toString();
|
673
|
-
let url = new URL(
|
674
|
-
`${http_protocol}//${resolve_root(
|
675
|
-
host,
|
676
|
-
config.path,
|
677
|
-
true
|
678
|
-
)}/queue/join?${url_params ? url_params + "&" : ""}${params}`
|
679
|
-
);
|
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
|
+
});
|
680
639
|
|
681
|
-
|
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
|
+
};
|
682
654
|
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
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}`
|
688
679
|
);
|
689
680
|
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
...status
|
698
|
-
});
|
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
|
-
`${http_protocol}//${resolve_root(
|
706
|
-
host,
|
707
|
-
config.path,
|
708
|
-
true
|
709
|
-
)}/queue/data`,
|
710
|
-
{
|
711
|
-
...payload,
|
712
|
-
session_hash,
|
713
|
-
event_id
|
714
|
-
},
|
715
|
-
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]
|
716
688
|
);
|
717
|
-
|
689
|
+
|
690
|
+
if (type === "update" && status && !complete) {
|
691
|
+
// call 'status' listeners
|
718
692
|
fire_event({
|
719
693
|
type: "status",
|
720
|
-
stage: "error",
|
721
|
-
message: BROKEN_CONNECTION_MSG,
|
722
|
-
queue: true,
|
723
694
|
endpoint: _endpoint,
|
724
695
|
fn_index,
|
725
|
-
time: new Date()
|
696
|
+
time: new Date(),
|
697
|
+
...status
|
726
698
|
});
|
727
|
-
|
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
|
-
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") {
|
767
736
|
fire_event({
|
768
737
|
type: "status",
|
769
738
|
time: new Date(),
|
770
|
-
...
|
739
|
+
...status,
|
771
740
|
stage: status?.stage!,
|
772
741
|
queue: true,
|
773
742
|
endpoint: _endpoint,
|
774
743
|
fn_index
|
775
744
|
});
|
776
|
-
eventSource.close();
|
777
745
|
}
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
793
|
-
config.path,
|
794
|
-
true
|
795
|
-
)}/queue/join?${url_params}`,
|
796
|
-
{
|
797
|
-
...payload,
|
798
|
-
session_hash
|
799
|
-
},
|
800
|
-
hf_token
|
801
|
-
).then(([response, status]) => {
|
802
|
-
if (status === 503) {
|
803
|
-
fire_event({
|
804
|
-
type: "status",
|
805
|
-
stage: "error",
|
806
|
-
message: QUEUE_FULL_MSG,
|
807
|
-
queue: true,
|
808
|
-
endpoint: _endpoint,
|
809
|
-
fn_index,
|
810
|
-
time: new Date()
|
811
|
-
});
|
812
|
-
} else if (status !== 200) {
|
813
|
-
fire_event({
|
814
|
-
type: "status",
|
815
|
-
stage: "error",
|
816
|
-
message: BROKEN_CONNECTION_MSG,
|
817
|
-
queue: true,
|
818
|
-
endpoint: _endpoint,
|
819
|
-
fn_index,
|
820
|
-
time: new Date()
|
821
|
-
});
|
822
|
-
} else {
|
823
|
-
event_id = response.event_id as string;
|
824
|
-
let callback = async function (_data: object): void {
|
825
|
-
try {
|
826
|
-
const { type, status, data } = handle_message(
|
827
|
-
_data,
|
828
|
-
last_status[fn_index]
|
829
|
-
);
|
830
|
-
|
831
|
-
// TODO: Find out how to print this information
|
832
|
-
// only during testing
|
833
|
-
// 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
|
+
});
|
834
761
|
|
835
|
-
|
836
|
-
|
837
|
-
|
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
|
+
});
|
838
785
|
|
839
|
-
|
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
|
-
|
883
|
-
type: "data",
|
884
|
-
time: new Date(),
|
885
|
-
data: transform_files
|
886
|
-
? transform_output(
|
887
|
-
data.data,
|
888
|
-
api_info,
|
889
|
-
config.root,
|
890
|
-
config.root_url
|
891
|
-
)
|
892
|
-
: data.data,
|
893
|
-
endpoint: _endpoint,
|
894
|
-
fn_index
|
895
|
-
});
|
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
|
+
// TODO: Find out how to print this information
|
824
|
+
// only during testing
|
825
|
+
// console.info("data", type, status, data);
|
826
|
+
|
827
|
+
if (type == "heartbeat") {
|
828
|
+
return;
|
829
|
+
}
|
896
830
|
|
897
|
-
if (complete) {
|
831
|
+
if (type === "update" && status && !complete) {
|
832
|
+
// call 'status' listeners
|
898
833
|
fire_event({
|
899
834
|
type: "status",
|
835
|
+
endpoint: _endpoint,
|
836
|
+
fn_index,
|
900
837
|
time: new Date(),
|
901
|
-
...
|
838
|
+
...status
|
839
|
+
});
|
840
|
+
} else if (type === "complete") {
|
841
|
+
complete = status;
|
842
|
+
} else if (type == "unexpected_error") {
|
843
|
+
console.error("Unexpected error", status?.message);
|
844
|
+
fire_event({
|
845
|
+
type: "status",
|
846
|
+
stage: "error",
|
847
|
+
message: "An Unexpected Error Occurred!",
|
848
|
+
queue: true,
|
849
|
+
endpoint: _endpoint,
|
850
|
+
fn_index,
|
851
|
+
time: new Date()
|
852
|
+
});
|
853
|
+
} else if (type === "log") {
|
854
|
+
fire_event({
|
855
|
+
type: "log",
|
856
|
+
log: data.log,
|
857
|
+
level: data.level,
|
858
|
+
endpoint: _endpoint,
|
859
|
+
fn_index
|
860
|
+
});
|
861
|
+
return;
|
862
|
+
} else if (type === "generating") {
|
863
|
+
fire_event({
|
864
|
+
type: "status",
|
865
|
+
time: new Date(),
|
866
|
+
...status,
|
902
867
|
stage: status?.stage!,
|
903
868
|
queue: true,
|
904
869
|
endpoint: _endpoint,
|
905
870
|
fn_index
|
906
871
|
});
|
907
872
|
}
|
908
|
-
|
873
|
+
if (data) {
|
874
|
+
fire_event({
|
875
|
+
type: "data",
|
876
|
+
time: new Date(),
|
877
|
+
data: transform_files
|
878
|
+
? transform_output(
|
879
|
+
data.data,
|
880
|
+
api_info,
|
881
|
+
config.root,
|
882
|
+
config.root_url
|
883
|
+
)
|
884
|
+
: data.data,
|
885
|
+
endpoint: _endpoint,
|
886
|
+
fn_index
|
887
|
+
});
|
888
|
+
|
889
|
+
if (complete) {
|
890
|
+
fire_event({
|
891
|
+
type: "status",
|
892
|
+
time: new Date(),
|
893
|
+
...complete,
|
894
|
+
stage: status?.stage!,
|
895
|
+
queue: true,
|
896
|
+
endpoint: _endpoint,
|
897
|
+
fn_index
|
898
|
+
});
|
899
|
+
}
|
900
|
+
}
|
909
901
|
|
910
|
-
|
911
|
-
|
912
|
-
|
913
|
-
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
|
902
|
+
if (
|
903
|
+
status?.stage === "complete" ||
|
904
|
+
status?.stage === "error"
|
905
|
+
) {
|
906
|
+
if (event_callbacks[event_id]) {
|
907
|
+
delete event_callbacks[event_id];
|
908
|
+
if (Object.keys(event_callbacks).length === 0) {
|
909
|
+
close_stream();
|
910
|
+
}
|
918
911
|
}
|
919
912
|
}
|
913
|
+
} catch (e) {
|
914
|
+
console.error("Unexpected client exception", e);
|
915
|
+
fire_event({
|
916
|
+
type: "status",
|
917
|
+
stage: "error",
|
918
|
+
message: "An Unexpected Error Occurred!",
|
919
|
+
queue: true,
|
920
|
+
endpoint: _endpoint,
|
921
|
+
fn_index,
|
922
|
+
time: new Date()
|
923
|
+
});
|
924
|
+
close_stream();
|
920
925
|
}
|
921
|
-
}
|
922
|
-
|
923
|
-
|
924
|
-
|
925
|
-
|
926
|
-
|
927
|
-
|
928
|
-
|
929
|
-
|
930
|
-
|
931
|
-
});
|
932
|
-
close_stream();
|
926
|
+
};
|
927
|
+
if (event_id in pending_stream_messages) {
|
928
|
+
pending_stream_messages[event_id].forEach((msg) =>
|
929
|
+
callback(msg)
|
930
|
+
);
|
931
|
+
delete pending_stream_messages[event_id];
|
932
|
+
}
|
933
|
+
event_callbacks[event_id] = callback;
|
934
|
+
if (!stream_open) {
|
935
|
+
open_stream();
|
933
936
|
}
|
934
|
-
};
|
935
|
-
event_callbacks[event_id] = callback;
|
936
|
-
if (!stream_open) {
|
937
|
-
open_stream();
|
938
937
|
}
|
939
|
-
}
|
940
|
-
}
|
938
|
+
});
|
939
|
+
}
|
941
940
|
}
|
942
|
-
|
941
|
+
);
|
943
942
|
|
944
943
|
function fire_event<K extends EventType>(event: Event<K>): void {
|
945
944
|
const narrowed_listener_map: ListenerMap<K> = listener_map;
|
@@ -1001,18 +1000,11 @@ export function api_factory(
|
|
1001
1000
|
}
|
1002
1001
|
|
1003
1002
|
try {
|
1004
|
-
await fetch_implementation(
|
1005
|
-
|
1006
|
-
|
1007
|
-
|
1008
|
-
|
1009
|
-
)}/reset`,
|
1010
|
-
{
|
1011
|
-
headers: { "Content-Type": "application/json" },
|
1012
|
-
method: "POST",
|
1013
|
-
body: JSON.stringify(cancel_request)
|
1014
|
-
}
|
1015
|
-
);
|
1003
|
+
await fetch_implementation(`${config.root}/reset`, {
|
1004
|
+
headers: { "Content-Type": "application/json" },
|
1005
|
+
method: "POST",
|
1006
|
+
body: JSON.stringify(cancel_request)
|
1007
|
+
});
|
1016
1008
|
} catch (e) {
|
1017
1009
|
console.warn(
|
1018
1010
|
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
@@ -1041,25 +1033,25 @@ export function api_factory(
|
|
1041
1033
|
let params = new URLSearchParams({
|
1042
1034
|
session_hash: session_hash
|
1043
1035
|
}).toString();
|
1044
|
-
let url = new URL(
|
1045
|
-
`${http_protocol}//${resolve_root(
|
1046
|
-
host,
|
1047
|
-
config.path,
|
1048
|
-
true
|
1049
|
-
)}/queue/data?${params}`
|
1050
|
-
);
|
1036
|
+
let url = new URL(`${config.root}/queue/data?${params}`);
|
1051
1037
|
event_stream = new EventSource(url);
|
1052
1038
|
event_stream.onmessage = async function (event) {
|
1053
1039
|
let _data = JSON.parse(event.data);
|
1054
|
-
|
1040
|
+
const event_id = _data.event_id;
|
1041
|
+
if (!event_id) {
|
1055
1042
|
await Promise.all(
|
1056
1043
|
Object.keys(event_callbacks).map((event_id) =>
|
1057
1044
|
event_callbacks[event_id](_data)
|
1058
1045
|
)
|
1059
1046
|
);
|
1060
|
-
|
1047
|
+
} else if (event_callbacks[event_id]) {
|
1048
|
+
await event_callbacks[event_id](_data);
|
1049
|
+
} else {
|
1050
|
+
if (!pending_stream_messages[event_id]) {
|
1051
|
+
pending_stream_messages[event_id] = [];
|
1052
|
+
}
|
1053
|
+
pending_stream_messages[event_id].push(_data);
|
1061
1054
|
}
|
1062
|
-
await event_callbacks[_data.event_id](_data);
|
1063
1055
|
};
|
1064
1056
|
}
|
1065
1057
|
|
@@ -1087,14 +1079,10 @@ export function api_factory(
|
|
1087
1079
|
if (component?.props?.root_url) {
|
1088
1080
|
root_url = component.props.root_url;
|
1089
1081
|
} else {
|
1090
|
-
root_url =
|
1091
|
-
host,
|
1092
|
-
config.path,
|
1093
|
-
true
|
1094
|
-
)}/`;
|
1082
|
+
root_url = config.root;
|
1095
1083
|
}
|
1096
1084
|
const response = await fetch_implementation(
|
1097
|
-
`${root_url}component_server/`,
|
1085
|
+
`${root_url}/component_server/`,
|
1098
1086
|
{
|
1099
1087
|
method: "POST",
|
1100
1088
|
body: JSON.stringify({
|
@@ -1701,8 +1689,7 @@ function handle_message(
|
|
1701
1689
|
message: !data.success ? data.output.error : undefined,
|
1702
1690
|
stage: data.success ? "complete" : "error",
|
1703
1691
|
code: data.code,
|
1704
|
-
progress_data: data.progress_data
|
1705
|
-
eta: data.output.average_duration
|
1692
|
+
progress_data: data.progress_data
|
1706
1693
|
},
|
1707
1694
|
data: data.success ? data.output : null
|
1708
1695
|
};
|
@@ -1716,7 +1703,8 @@ function handle_message(
|
|
1716
1703
|
code: data.code,
|
1717
1704
|
size: data.rank,
|
1718
1705
|
position: 0,
|
1719
|
-
success: data.success
|
1706
|
+
success: data.success,
|
1707
|
+
eta: data.eta
|
1720
1708
|
}
|
1721
1709
|
};
|
1722
1710
|
}
|