@gradio/client 0.9.1 → 0.9.2

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # @gradio/client
2
2
 
3
+ ## 0.9.2
4
+
5
+ ### Features
6
+
7
+ - [#6798](https://github.com/gradio-app/gradio/pull/6798) [`245d58e`](https://github.com/gradio-app/gradio/commit/245d58eff788e8d44a59d37a2d9b26d0f08a62b4) - Improve how server/js client handle unexpected errors. Thanks [@freddyaboulton](https://github.com/freddyaboulton)!
8
+
3
9
  ## 0.9.1
4
10
 
5
11
  ### Fixes
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAIA,OAAO,EAQN,cAAc,EAEd,MAAM,YAAY,CAAC;AAEpB,OAAO,KAAK,EACX,SAAS,EACT,aAAa,EAIb,YAAY,EACZ,cAAc,EAGd,mBAAmB,EACnB,MAAM,YAAY,CAAC;AAIpB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEzC,KAAK,KAAK,GAAG,CAAC,CAAC,SAAS,SAAS,EAChC,SAAS,EAAE,CAAC,EACZ,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,KACtB,YAAY,CAAC;AAClB,KAAK,OAAO,GAAG,CACd,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,KAChB,OAAO,CAAC,OAAO,CAAC,CAAC;AAEtB,KAAK,aAAa,GAAG;IACpB,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,CACP,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,CAAC,EAAE,OAAO,EAAE,EAChB,UAAU,CAAC,EAAE,OAAO,EACpB,UAAU,CAAC,EAAE,MAAM,G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 +878,107 @@ function api_factory(fetch_implementation, EventSource_factory) {
878
878
  });
879
879
  } else {
880
880
  event_id = response.event_id;
881
- if (!stream_open) {
882
- open_stream();
883
- }
884
881
  let callback = async function(_data) {
885
- const { type, status: status2, data: data2 } = handle_message(
886
- _data,
887
- last_status[fn_index]
888
- );
889
- if (type === "update" && status2 && !complete) {
890
- fire_event({
891
- type: "status",
892
- endpoint: _endpoint,
893
- fn_index,
894
- time: /* @__PURE__ */ new Date(),
895
- ...status2
896
- });
897
- } else if (type === "complete") {
898
- complete = status2;
899
- } else if (type === "log") {
900
- fire_event({
901
- type: "log",
902
- log: data2.log,
903
- level: data2.level,
904
- endpoint: _endpoint,
905
- fn_index
906
- });
907
- } else if (type === "generating") {
908
- fire_event({
909
- type: "status",
910
- time: /* @__PURE__ */ new Date(),
911
- ...status2,
912
- stage: status2 == null ? void 0 : status2.stage,
913
- queue: true,
914
- endpoint: _endpoint,
915
- fn_index
916
- });
917
- }
918
- if (data2) {
919
- fire_event({
920
- type: "data",
921
- time: /* @__PURE__ */ new Date(),
922
- data: transform_files ? transform_output(
923
- data2.data,
924
- api_info,
925
- config.root,
926
- config.root_url
927
- ) : data2.data,
928
- endpoint: _endpoint,
929
- fn_index
930
- });
931
- if (complete) {
882
+ try {
883
+ const { type, status: status2, data: data2 } = handle_message(
884
+ _data,
885
+ last_status[fn_index]
886
+ );
887
+ if (type == "heartbeat") {
888
+ return;
889
+ }
890
+ if (type === "update" && status2 && !complete) {
891
+ fire_event({
892
+ type: "status",
893
+ endpoint: _endpoint,
894
+ fn_index,
895
+ time: /* @__PURE__ */ new Date(),
896
+ ...status2
897
+ });
898
+ } else if (type === "complete") {
899
+ complete = status2;
900
+ } else if (type == "unexpected_error") {
901
+ console.error("Unexpected error", status2 == null ? void 0 : status2.message);
902
+ fire_event({
903
+ type: "status",
904
+ stage: "error",
905
+ message: "An Unexpected Error Occurred!",
906
+ queue: true,
907
+ endpoint: _endpoint,
908
+ fn_index,
909
+ time: /* @__PURE__ */ new Date()
910
+ });
911
+ } else if (type === "log") {
912
+ fire_event({
913
+ type: "log",
914
+ log: data2.log,
915
+ level: data2.level,
916
+ endpoint: _endpoint,
917
+ fn_index
918
+ });
919
+ return;
920
+ } else if (type === "generating") {
932
921
  fire_event({
933
922
  type: "status",
934
923
  time: /* @__PURE__ */ new Date(),
935
- ...complete,
924
+ ...status2,
936
925
  stage: status2 == null ? void 0 : status2.stage,
937
926
  queue: true,
938
927
  endpoint: _endpoint,
939
928
  fn_index
940
929
  });
941
930
  }
942
- }
943
- if (status2.stage === "complete" || status2.stage === "error") {
944
- if (event_callbacks[event_id]) {
945
- delete event_callbacks[event_id];
946
- if (Object.keys(event_callbacks).length === 0) {
947
- close_stream();
931
+ if (data2) {
932
+ fire_event({
933
+ type: "data",
934
+ time: /* @__PURE__ */ new Date(),
935
+ data: transform_files ? transform_output(
936
+ data2.data,
937
+ api_info,
938
+ config.root,
939
+ config.root_url
940
+ ) : data2.data,
941
+ endpoint: _endpoint,
942
+ fn_index
943
+ });
944
+ if (complete) {
945
+ fire_event({
946
+ type: "status",
947
+ time: /* @__PURE__ */ new Date(),
948
+ ...complete,
949
+ stage: status2 == null ? void 0 : status2.stage,
950
+ queue: true,
951
+ endpoint: _endpoint,
952
+ fn_index
953
+ });
954
+ }
955
+ }
956
+ if (status2.stage === "complete" || status2.stage === "error") {
957
+ if (event_callbacks[event_id]) {
958
+ delete event_callbacks[event_id];
959
+ if (Object.keys(event_callbacks).length === 0) {
960
+ close_stream();
961
+ }
948
962
  }
949
963
  }
964
+ } catch (e) {
965
+ console.error("Unexpected client exception", e);
966
+ fire_event({
967
+ type: "status",
968
+ stage: "error",
969
+ message: "An Unexpected Error Occurred!",
970
+ queue: true,
971
+ endpoint: _endpoint,
972
+ fn_index,
973
+ time: /* @__PURE__ */ new Date()
974
+ });
975
+ close_stream();
950
976
  }
951
977
  };
952
978
  event_callbacks[event_id] = callback;
979
+ if (!stream_open) {
980
+ open_stream();
981
+ }
953
982
  }
954
983
  });
955
984
  }
@@ -1048,6 +1077,14 @@ function api_factory(fetch_implementation, EventSource_factory) {
1048
1077
  event_stream = new EventSource(url);
1049
1078
  event_stream.onmessage = async function(event) {
1050
1079
  let _data = JSON.parse(event.data);
1080
+ if (!("event_id" in _data)) {
1081
+ await Promise.all(
1082
+ Object.keys(event_callbacks).map(
1083
+ (event_id) => event_callbacks[event_id](_data)
1084
+ )
1085
+ );
1086
+ return;
1087
+ }
1051
1088
  await event_callbacks[_data.event_id](_data);
1052
1089
  };
1053
1090
  }
@@ -1445,6 +1482,20 @@ function handle_message(data, last_status) {
1445
1482
  success: data.success
1446
1483
  }
1447
1484
  };
1485
+ case "heartbeat":
1486
+ return {
1487
+ type: "heartbeat"
1488
+ };
1489
+ case "unexpected_error":
1490
+ return {
1491
+ type: "unexpected_error",
1492
+ status: {
1493
+ queue,
1494
+ message: data.message,
1495
+ stage: "error",
1496
+ success: false
1497
+ }
1498
+ };
1448
1499
  case "estimation":
1449
1500
  return {
1450
1501
  type: "update",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gradio/client",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "Gradio API client",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
package/src/client.ts CHANGED
@@ -821,84 +821,121 @@ export function api_factory(
821
821
  });
822
822
  } else {
823
823
  event_id = response.event_id as string;
824
- if (!stream_open) {
825
- open_stream();
826
- }
827
824
  let callback = async function (_data: object): void {
828
- const { type, status, data } = handle_message(
829
- _data,
830
- last_status[fn_index]
831
- );
832
-
833
- if (type === "update" && status && !complete) {
834
- // call 'status' listeners
835
- fire_event({
836
- type: "status",
837
- endpoint: _endpoint,
838
- fn_index,
839
- time: new Date(),
840
- ...status
841
- });
842
- } else if (type === "complete") {
843
- complete = status;
844
- } else if (type === "log") {
845
- fire_event({
846
- type: "log",
847
- log: data.log,
848
- level: data.level,
849
- endpoint: _endpoint,
850
- fn_index
851
- });
852
- } else if (type === "generating") {
853
- fire_event({
854
- type: "status",
855
- time: new Date(),
856
- ...status,
857
- stage: status?.stage!,
858
- queue: true,
859
- endpoint: _endpoint,
860
- fn_index
861
- });
862
- }
863
- if (data) {
864
- fire_event({
865
- type: "data",
866
- time: new Date(),
867
- data: transform_files
868
- ? transform_output(
869
- data.data,
870
- api_info,
871
- config.root,
872
- config.root_url
873
- )
874
- : data.data,
875
- endpoint: _endpoint,
876
- fn_index
877
- });
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);
834
+
835
+ if (type == "heartbeat") {
836
+ return;
837
+ }
878
838
 
879
- if (complete) {
839
+ if (type === "update" && status && !complete) {
840
+ // call 'status' listeners
841
+ fire_event({
842
+ type: "status",
843
+ endpoint: _endpoint,
844
+ fn_index,
845
+ time: new Date(),
846
+ ...status
847
+ });
848
+ } else if (type === "complete") {
849
+ complete = status;
850
+ } else if (type == "unexpected_error") {
851
+ console.error("Unexpected error", status?.message);
852
+ fire_event({
853
+ type: "status",
854
+ stage: "error",
855
+ message: "An Unexpected Error Occurred!",
856
+ queue: true,
857
+ endpoint: _endpoint,
858
+ fn_index,
859
+ time: new Date()
860
+ });
861
+ } else if (type === "log") {
862
+ fire_event({
863
+ type: "log",
864
+ log: data.log,
865
+ level: data.level,
866
+ endpoint: _endpoint,
867
+ fn_index
868
+ });
869
+ return;
870
+ } else if (type === "generating") {
880
871
  fire_event({
881
872
  type: "status",
882
873
  time: new Date(),
883
- ...complete,
874
+ ...status,
884
875
  stage: status?.stage!,
885
876
  queue: true,
886
877
  endpoint: _endpoint,
887
878
  fn_index
888
879
  });
889
880
  }
890
- }
881
+ if (data) {
882
+ fire_event({
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
+ });
896
+
897
+ if (complete) {
898
+ fire_event({
899
+ type: "status",
900
+ time: new Date(),
901
+ ...complete,
902
+ stage: status?.stage!,
903
+ queue: true,
904
+ endpoint: _endpoint,
905
+ fn_index
906
+ });
907
+ }
908
+ }
891
909
 
892
- if (status.stage === "complete" || status.stage === "error") {
893
- if (event_callbacks[event_id]) {
894
- delete event_callbacks[event_id];
895
- if (Object.keys(event_callbacks).length === 0) {
896
- close_stream();
910
+ if (
911
+ status.stage === "complete" ||
912
+ status.stage === "error"
913
+ ) {
914
+ if (event_callbacks[event_id]) {
915
+ delete event_callbacks[event_id];
916
+ if (Object.keys(event_callbacks).length === 0) {
917
+ close_stream();
918
+ }
897
919
  }
898
920
  }
921
+ } catch (e) {
922
+ console.error("Unexpected client exception", e);
923
+ fire_event({
924
+ type: "status",
925
+ stage: "error",
926
+ message: "An Unexpected Error Occurred!",
927
+ queue: true,
928
+ endpoint: _endpoint,
929
+ fn_index,
930
+ time: new Date()
931
+ });
932
+ close_stream();
899
933
  }
900
934
  };
901
935
  event_callbacks[event_id] = callback;
936
+ if (!stream_open) {
937
+ open_stream();
938
+ }
902
939
  }
903
940
  });
904
941
  }
@@ -1014,6 +1051,14 @@ export function api_factory(
1014
1051
  event_stream = new EventSource(url);
1015
1052
  event_stream.onmessage = async function (event) {
1016
1053
  let _data = JSON.parse(event.data);
1054
+ if (!("event_id" in _data)) {
1055
+ await Promise.all(
1056
+ Object.keys(event_callbacks).map((event_id) =>
1057
+ event_callbacks[event_id](_data)
1058
+ )
1059
+ );
1060
+ return;
1061
+ }
1017
1062
  await event_callbacks[_data.event_id](_data);
1018
1063
  };
1019
1064
  }
@@ -1583,6 +1628,20 @@ function handle_message(
1583
1628
  success: data.success
1584
1629
  }
1585
1630
  };
1631
+ case "heartbeat":
1632
+ return {
1633
+ type: "heartbeat"
1634
+ };
1635
+ case "unexpected_error":
1636
+ return {
1637
+ type: "unexpected_error",
1638
+ status: {
1639
+ queue,
1640
+ message: data.message,
1641
+ stage: "error",
1642
+ success: false
1643
+ }
1644
+ };
1586
1645
  case "estimation":
1587
1646
  return {
1588
1647
  type: "update",