@gradio/client 2.0.0-dev.0 → 2.0.0-dev.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/dist/index.js CHANGED
@@ -1,14 +1,3 @@
1
- var __defProp = Object.defineProperty;
2
- var __typeError = (msg) => {
3
- throw TypeError(msg);
4
- };
5
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
- var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
8
- var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
9
- var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
10
- var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
11
- var _currentLine;
12
1
  const HOST_URL = `host`;
13
2
  const SSE_URL = `queue/data`;
14
3
  const SSE_DATA_URL = `queue/join`;
@@ -99,24 +88,23 @@ async function resolve_config(endpoint) {
99
88
  throw new Error(CONFIG_ERROR_MSG);
100
89
  }
101
90
  async function handleConfigResponse(response, endpoint, authorized) {
102
- var _a, _b;
103
- if ((response == null ? void 0 : response.status) === 401 && !authorized) {
91
+ if (response?.status === 401 && !authorized) {
104
92
  const error_data = await response.json();
105
- const auth_message = (_a = error_data == null ? void 0 : error_data.detail) == null ? void 0 : _a.auth_message;
93
+ const auth_message = error_data?.detail?.auth_message;
106
94
  throw new Error(auth_message || MISSING_CREDENTIALS_MSG);
107
- } else if ((response == null ? void 0 : response.status) === 401 && authorized) {
95
+ } else if (response?.status === 401 && authorized) {
108
96
  throw new Error(INVALID_CREDENTIALS_MSG);
109
97
  }
110
- if ((response == null ? void 0 : response.status) === 200) {
98
+ if (response?.status === 200) {
111
99
  let config = await response.json();
112
100
  config.root = endpoint;
113
- (_b = config.dependencies) == null ? void 0 : _b.forEach((dep, i) => {
101
+ config.dependencies?.forEach((dep, i) => {
114
102
  if (dep.id === void 0) {
115
103
  dep.id = i;
116
104
  }
117
105
  });
118
106
  return config;
119
- } else if ((response == null ? void 0 : response.status) === 401) {
107
+ } else if (response?.status === 401) {
120
108
  throw new Error(UNAUTHORIZED_MSG);
121
109
  }
122
110
  throw new Error(CONFIG_ERROR_MSG);
@@ -143,8 +131,8 @@ async function resolve_cookies() {
143
131
  }
144
132
  async function get_cookie_header(http_protocol, host, auth, _fetch, token) {
145
133
  const formData = new FormData();
146
- formData.append("username", auth == null ? void 0 : auth[0]);
147
- formData.append("password", auth == null ? void 0 : auth[1]);
134
+ formData.append("username", auth?.[0]);
135
+ formData.append("password", auth?.[1]);
148
136
  let headers = {};
149
137
  if (token) {
150
138
  headers.Authorization = `Bearer ${token}`;
@@ -247,17 +235,13 @@ function transform_api_info(api_info, config, api_map) {
247
235
  transformed_info[category] = {};
248
236
  Object.entries(api_info[category]).forEach(
249
237
  ([endpoint, { parameters, returns }]) => {
250
- var _a, _b, _c, _d;
251
- const dependencyIndex = ((_a = config.dependencies.find(
238
+ const dependencyIndex = config.dependencies.find(
252
239
  (dep) => dep.api_name === endpoint || dep.api_name === endpoint.replace("/", "")
253
- )) == null ? void 0 : _a.id) || api_map[endpoint.replace("/", "")] || -1;
254
- const dependencyTypes = dependencyIndex !== -1 ? (_b = config.dependencies.find((dep) => dep.id == dependencyIndex)) == null ? void 0 : _b.types : { generator: false, cancel: false };
255
- if (dependencyIndex !== -1 && ((_d = (_c = config.dependencies.find((dep) => dep.id == dependencyIndex)) == null ? void 0 : _c.inputs) == null ? void 0 : _d.length) !== parameters.length) {
240
+ )?.id || api_map[endpoint.replace("/", "")] || -1;
241
+ const dependencyTypes = dependencyIndex !== -1 ? config.dependencies.find((dep) => dep.id == dependencyIndex)?.types : { generator: false, cancel: false };
242
+ if (dependencyIndex !== -1 && config.dependencies.find((dep) => dep.id == dependencyIndex)?.inputs?.length !== parameters.length) {
256
243
  const components = config.dependencies.find((dep) => dep.id == dependencyIndex).inputs.map(
257
- (input) => {
258
- var _a2;
259
- return (_a2 = config.components.find((c) => c.id === input)) == null ? void 0 : _a2.type;
260
- }
244
+ (input) => config.components.find((c) => c.id === input)?.type
261
245
  );
262
246
  try {
263
247
  components.forEach((comp, idx) => {
@@ -279,15 +263,15 @@ function transform_api_info(api_info, config, api_map) {
279
263
  }
280
264
  const transform_type = (data, component, serializer, signature_type) => ({
281
265
  ...data,
282
- description: get_description(data == null ? void 0 : data.type, serializer),
283
- type: get_type(data == null ? void 0 : data.type, component, serializer, signature_type) || ""
266
+ description: get_description(data?.type, serializer),
267
+ type: get_type(data?.type, component, serializer, signature_type) || ""
284
268
  });
285
269
  transformed_info[category][endpoint] = {
286
270
  parameters: parameters.map(
287
- (p) => transform_type(p, p == null ? void 0 : p.component, p == null ? void 0 : p.serializer, "parameter")
271
+ (p) => transform_type(p, p?.component, p?.serializer, "parameter")
288
272
  ),
289
273
  returns: returns.map(
290
- (r) => transform_type(r, r == null ? void 0 : r.component, r == null ? void 0 : r.serializer, "return")
274
+ (r) => transform_type(r, r?.component, r?.serializer, "return")
291
275
  ),
292
276
  type: dependencyTypes
293
277
  };
@@ -299,7 +283,7 @@ function transform_api_info(api_info, config, api_map) {
299
283
  }
300
284
  function get_type(type, component, serializer, signature_type) {
301
285
  if (component === "Api") return type.type;
302
- switch (type == null ? void 0 : type.type) {
286
+ switch (type?.type) {
303
287
  case "string":
304
288
  return "string";
305
289
  case "boolean":
@@ -314,7 +298,7 @@ function get_type(type, component, serializer, signature_type) {
314
298
  } else if (component === "Image") {
315
299
  return signature_type === "parameter" ? "Blob | File | Buffer" : "string";
316
300
  } else if (serializer === "FileSerializable") {
317
- if ((type == null ? void 0 : type.type) === "array") {
301
+ if (type?.type === "array") {
318
302
  return signature_type === "parameter" ? "(Blob | File | Buffer)[]" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
319
303
  }
320
304
  return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
@@ -330,7 +314,7 @@ function get_description(type, serializer) {
330
314
  } else if (serializer === "FileSerializable") {
331
315
  return "array of files or single file";
332
316
  }
333
- return type == null ? void 0 : type.description;
317
+ return type?.description;
334
318
  }
335
319
  function handle_message(data, last_status) {
336
320
  const queue = true;
@@ -435,8 +419,8 @@ function handle_message(data, last_status) {
435
419
  type: "update",
436
420
  status: {
437
421
  queue,
438
- title: data.output.title,
439
- message: data.output.error,
422
+ title: data.output.title ?? "Error",
423
+ message: data.output.error ?? "An error occurred",
440
424
  visible: data.output.visible,
441
425
  duration: data.output.duration,
442
426
  stage: "error",
@@ -550,9 +534,8 @@ async function view_api() {
550
534
  }
551
535
  }
552
536
  async function upload_files(root_url, files, upload_id) {
553
- var _a;
554
537
  const headers = {};
555
- if ((_a = this == null ? void 0 : this.options) == null ? void 0 : _a.token) {
538
+ if (this?.options?.token) {
556
539
  headers.Authorization = `Bearer ${this.options.token}`;
557
540
  }
558
541
  const chunkSize = 1e3;
@@ -586,6 +569,33 @@ async function upload_files(root_url, files, upload_id) {
586
569
  }
587
570
  return { files: uploadResponses };
588
571
  }
572
+ const si = {
573
+ radix: 1e3,
574
+ unit: ["b", "kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"]
575
+ };
576
+ const iec = {
577
+ radix: 1024,
578
+ unit: ["b", "Kib", "Mib", "Gib", "Tib", "Pib", "Eib", "Zib", "Yib"]
579
+ };
580
+ const jedec = {
581
+ radix: 1024,
582
+ unit: ["b", "Kb", "Mb", "Gb", "Tb", "Pb", "Eb", "Zb", "Yb"]
583
+ };
584
+ const SPECS = {
585
+ si,
586
+ iec,
587
+ jedec
588
+ };
589
+ function filesize(bytes, fixed = 1, spec = "jedec") {
590
+ bytes = Math.abs(bytes);
591
+ const { radix, unit } = SPECS[spec] || SPECS.jedec;
592
+ let loop = 0;
593
+ while (bytes >= radix) {
594
+ bytes /= radix;
595
+ ++loop;
596
+ }
597
+ return `${bytes.toFixed(fixed)} ${unit[loop]}`;
598
+ }
589
599
  async function upload(file_data, root_url, upload_id, max_file_size) {
590
600
  let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
591
601
  (file_data2) => file_data2.blob
@@ -595,7 +605,7 @@ async function upload(file_data, root_url, upload_id, max_file_size) {
595
605
  );
596
606
  if (oversized_files.length) {
597
607
  throw new Error(
598
- `File size exceeds the maximum allowed size of ${max_file_size} bytes: ${oversized_files.map((f) => f.name).join(", ")}`
608
+ `File(s) exceed the maximum allowed size of ${filesize(max_file_size || Infinity)}: ${oversized_files.map((f) => `"${f.name}"`).join(", ")}`
599
609
  );
600
610
  }
601
611
  return await Promise.all(
@@ -633,6 +643,16 @@ async function prepare_files(files, is_stream) {
633
643
  );
634
644
  }
635
645
  class FileData {
646
+ path;
647
+ url;
648
+ orig_name;
649
+ size;
650
+ blob;
651
+ is_stream;
652
+ mime_type;
653
+ alt_text;
654
+ b64;
655
+ meta = { _type: "gradio.FileData" };
636
656
  constructor({
637
657
  path,
638
658
  url,
@@ -644,16 +664,6 @@ class FileData {
644
664
  alt_text,
645
665
  b64
646
666
  }) {
647
- __publicField(this, "path");
648
- __publicField(this, "url");
649
- __publicField(this, "orig_name");
650
- __publicField(this, "size");
651
- __publicField(this, "blob");
652
- __publicField(this, "is_stream");
653
- __publicField(this, "mime_type");
654
- __publicField(this, "alt_text");
655
- __publicField(this, "b64");
656
- __publicField(this, "meta", { _type: "gradio.FileData" });
657
667
  this.path = path;
658
668
  this.url = url;
659
669
  this.orig_name = orig_name;
@@ -666,11 +676,11 @@ class FileData {
666
676
  }
667
677
  }
668
678
  class Command {
679
+ type;
680
+ command;
681
+ meta;
682
+ fileData;
669
683
  constructor(command, meta) {
670
- __publicField(this, "type");
671
- __publicField(this, "command");
672
- __publicField(this, "meta");
673
- __publicField(this, "fileData");
674
684
  this.type = "command";
675
685
  this.command = command;
676
686
  this.meta = meta;
@@ -698,12 +708,11 @@ async function walk_and_store_blobs(data, type = void 0, path = [], root = false
698
708
  let blob_refs = [];
699
709
  await Promise.all(
700
710
  data.map(async (_, index) => {
701
- var _a;
702
711
  let new_path = path.slice();
703
712
  new_path.push(String(index));
704
713
  const array_refs = await walk_and_store_blobs(
705
714
  data[index],
706
- root ? ((_a = endpoint_info == null ? void 0 : endpoint_info.parameters[index]) == null ? void 0 : _a.component) || void 0 : type,
715
+ root ? endpoint_info?.parameters[index]?.component || void 0 : type,
707
716
  new_path,
708
717
  false,
709
718
  endpoint_info
@@ -740,8 +749,7 @@ async function walk_and_store_blobs(data, type = void 0, path = [], root = false
740
749
  return [];
741
750
  }
742
751
  function skip_queue(id, config) {
743
- var _a, _b;
744
- let fn_queue = (_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a.find((dep) => dep.id == id)) == null ? void 0 : _b.queue;
752
+ let fn_queue = config?.dependencies?.find((dep) => dep.id == id)?.queue;
745
753
  if (fn_queue != null) {
746
754
  return !fn_queue;
747
755
  }
@@ -750,10 +758,10 @@ function skip_queue(id, config) {
750
758
  function post_message(message, origin) {
751
759
  return new Promise((res, _rej) => {
752
760
  const channel = new MessageChannel();
753
- channel.port1.onmessage = ({ data }) => {
761
+ channel.port1.onmessage = (({ data }) => {
754
762
  channel.port1.close();
755
763
  res(data);
756
- };
764
+ });
757
765
  window.parent.postMessage(message, origin, [channel.port2]);
758
766
  });
759
767
  }
@@ -798,7 +806,7 @@ function handle_payload(resolved_payload, dependency, components, type, with_nul
798
806
  for (let i = 0; i < deps.length; i++) {
799
807
  const input_id = deps[i];
800
808
  const component = components.find((c) => c.id === input_id);
801
- if ((component == null ? void 0 : component.type) === "state") {
809
+ if (component?.type === "state") {
802
810
  if (with_null_state) {
803
811
  if (resolved_payload.length === deps.length) {
804
812
  const value = resolved_payload[payload_index];
@@ -839,7 +847,7 @@ async function handle_blob(endpoint, data, api_info) {
839
847
  path,
840
848
  file_url,
841
849
  type,
842
- name: typeof File !== "undefined" && blob instanceof File ? blob == null ? void 0 : blob.name : void 0
850
+ name: typeof File !== "undefined" && blob instanceof File ? blob?.name : void 0
843
851
  };
844
852
  })
845
853
  );
@@ -854,8 +862,7 @@ async function handle_blob(endpoint, data, api_info) {
854
862
  return data;
855
863
  }
856
864
  async function process_local_file_commands(client2, data) {
857
- var _a, _b;
858
- const root = ((_a = client2.config) == null ? void 0 : _a.root) || ((_b = client2.config) == null ? void 0 : _b.root_url);
865
+ const root = client2.config?.root || client2.config?.root_url;
859
866
  if (!root) {
860
867
  throw new Error(ROOT_URL_ERROR_MSG);
861
868
  }
@@ -871,9 +878,8 @@ async function recursively_process_commands(client2, data, path = []) {
871
878
  }
872
879
  }
873
880
  async function process_single_command(client2, data, key) {
874
- var _a, _b;
875
881
  let cmd_item = data[key];
876
- const root = ((_a = client2.config) == null ? void 0 : _a.root) || ((_b = client2.config) == null ? void 0 : _b.root_url);
882
+ const root = client2.config?.root || client2.config?.root_url;
877
883
  if (!root) {
878
884
  throw new Error(ROOT_URL_ERROR_MSG);
879
885
  }
@@ -888,7 +894,9 @@ async function process_single_command(client2, data, key) {
888
894
  } else {
889
895
  throw new Error(NODEJS_FS_ERROR_MSG);
890
896
  }
891
- const file = new Blob([fileBuffer], { type: "application/octet-stream" });
897
+ const file = new Blob([fileBuffer], {
898
+ type: "application/octet-stream"
899
+ });
892
900
  const response = await client2.upload_files(root, [file]);
893
901
  const file_url = response.files && response.files[0];
894
902
  if (file_url) {
@@ -1240,11 +1248,12 @@ function get_space_reference(url) {
1240
1248
  }
1241
1249
  }
1242
1250
  class TextLineStream extends TransformStream {
1251
+ #currentLine = "";
1243
1252
  /** Constructs a new instance. */
1244
1253
  constructor(options = { allowCR: false }) {
1245
1254
  super({
1246
1255
  transform: (chars, controller) => {
1247
- chars = __privateGet(this, _currentLine) + chars;
1256
+ chars = this.#currentLine + chars;
1248
1257
  while (true) {
1249
1258
  const lfIndex = chars.indexOf("\n");
1250
1259
  const crIndex = options.allowCR ? chars.indexOf("\r") : -1;
@@ -1259,19 +1268,17 @@ class TextLineStream extends TransformStream {
1259
1268
  controller.enqueue(chars.slice(0, endIndex));
1260
1269
  chars = chars.slice(lfIndex + 1);
1261
1270
  }
1262
- __privateSet(this, _currentLine, chars);
1271
+ this.#currentLine = chars;
1263
1272
  },
1264
1273
  flush: (controller) => {
1265
- if (__privateGet(this, _currentLine) === "")
1274
+ if (this.#currentLine === "")
1266
1275
  return;
1267
- const currentLine = options.allowCR && __privateGet(this, _currentLine).endsWith("\r") ? __privateGet(this, _currentLine).slice(0, -1) : __privateGet(this, _currentLine);
1276
+ const currentLine = options.allowCR && this.#currentLine.endsWith("\r") ? this.#currentLine.slice(0, -1) : this.#currentLine;
1268
1277
  controller.enqueue(currentLine);
1269
1278
  }
1270
1279
  });
1271
- __privateAdd(this, _currentLine, "");
1272
1280
  }
1273
1281
  }
1274
- _currentLine = new WeakMap();
1275
1282
  function stream$1(input) {
1276
1283
  let decoder = new TextDecoderStream();
1277
1284
  let split2 = new TextLineStream({ allowCR: true });
@@ -1313,19 +1320,17 @@ async function* events(res, signal) {
1313
1320
  continue;
1314
1321
  }
1315
1322
  let [field, value] = split(line.value) || [];
1316
- if (!field)
1317
- continue;
1318
1323
  if (field === "data") {
1319
- event || (event = {});
1324
+ event ||= {};
1320
1325
  event[field] = event[field] ? event[field] + "\n" + value : value;
1321
1326
  } else if (field === "event") {
1322
- event || (event = {});
1327
+ event ||= {};
1323
1328
  event[field] = value;
1324
1329
  } else if (field === "id") {
1325
- event || (event = {});
1326
- event[field] = +value || value;
1330
+ event ||= {};
1331
+ event[field] = String(+value) === value ? +value : value;
1327
1332
  } else if (field === "retry") {
1328
- event || (event = {});
1333
+ event ||= {};
1329
1334
  event[field] = +value || void 0;
1330
1335
  }
1331
1336
  }
@@ -1413,7 +1418,7 @@ async function open_stream() {
1413
1418
  function close_stream(stream_status, abort_controller) {
1414
1419
  if (stream_status) {
1415
1420
  stream_status.open = false;
1416
- abort_controller == null ? void 0 : abort_controller.abort();
1421
+ abort_controller?.abort();
1417
1422
  }
1418
1423
  }
1419
1424
  function apply_diff_stream(pending_diff_streams, event_id, data) {
@@ -1520,7 +1525,6 @@ function readable_stream(input, init = {}) {
1520
1525
  return instance;
1521
1526
  }
1522
1527
  function submit(endpoint, data = {}, event_data, trigger_id, all_events) {
1523
- var _a;
1524
1528
  try {
1525
1529
  let fire_event = function(event) {
1526
1530
  if (all_events || events_to_publish[event.type]) {
@@ -1590,13 +1594,13 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events) {
1590
1594
  let complete = false;
1591
1595
  let last_status = {};
1592
1596
  let url_params = typeof window !== "undefined" && typeof document !== "undefined" ? new URLSearchParams(window.location.search).toString() : "";
1593
- const events_to_publish = ((_a = options == null ? void 0 : options.events) == null ? void 0 : _a.reduce(
1597
+ const events_to_publish = options?.events?.reduce(
1594
1598
  (acc, event) => {
1595
1599
  acc[event] = true;
1596
1600
  return acc;
1597
1601
  },
1598
1602
  {}
1599
- )) || {};
1603
+ ) || {};
1600
1604
  async function cancel() {
1601
1605
  let reset_request = {};
1602
1606
  let cancel_request = {};
@@ -1651,405 +1655,410 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events) {
1651
1655
  fn_index
1652
1656
  });
1653
1657
  }
1654
- this.handle_blob(config.root, resolved_data, endpoint_info).then(
1655
- async (_payload) => {
1656
- var _a2;
1657
- let input_data = handle_payload(
1658
- _payload,
1659
- dependency,
1660
- config.components,
1661
- "input",
1662
- true
1663
- );
1664
- payload = {
1665
- data: input_data || [],
1666
- event_data,
1658
+ const job = this.handle_blob(
1659
+ config.root,
1660
+ resolved_data,
1661
+ endpoint_info
1662
+ ).then(async (_payload) => {
1663
+ let input_data = handle_payload(
1664
+ _payload,
1665
+ dependency,
1666
+ config.components,
1667
+ "input",
1668
+ true
1669
+ );
1670
+ payload = {
1671
+ data: input_data || [],
1672
+ event_data,
1673
+ fn_index,
1674
+ trigger_id
1675
+ };
1676
+ if (skip_queue(fn_index, config)) {
1677
+ fire_event({
1678
+ type: "status",
1679
+ endpoint: _endpoint,
1680
+ stage: "pending",
1681
+ queue: false,
1667
1682
  fn_index,
1668
- trigger_id
1669
- };
1670
- if (skip_queue(fn_index, config)) {
1671
- fire_event({
1672
- type: "status",
1673
- endpoint: _endpoint,
1674
- stage: "pending",
1675
- queue: false,
1676
- fn_index,
1677
- time: /* @__PURE__ */ new Date()
1678
- });
1679
- post_data2(
1680
- `${config.root}${api_prefix}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`,
1681
- {
1682
- ...payload,
1683
- session_hash
1684
- }
1685
- ).then(([output, status_code]) => {
1686
- const data2 = output.data;
1687
- if (status_code == 200) {
1688
- fire_event({
1689
- type: "data",
1690
- endpoint: _endpoint,
1691
- fn_index,
1692
- data: handle_payload(
1693
- data2,
1694
- dependency,
1695
- config.components,
1696
- "output",
1697
- options.with_null_state
1698
- ),
1699
- time: /* @__PURE__ */ new Date(),
1700
- event_data,
1701
- trigger_id
1702
- });
1703
- if (output.render_config) {
1704
- handle_render_config(output.render_config);
1705
- }
1706
- fire_event({
1707
- type: "status",
1708
- endpoint: _endpoint,
1709
- fn_index,
1710
- stage: "complete",
1711
- eta: output.average_duration,
1712
- queue: false,
1713
- time: /* @__PURE__ */ new Date()
1714
- });
1715
- } else {
1716
- fire_event({
1717
- type: "status",
1718
- stage: "error",
1719
- endpoint: _endpoint,
1720
- fn_index,
1721
- message: output.error,
1722
- queue: false,
1723
- time: /* @__PURE__ */ new Date()
1724
- });
1683
+ time: /* @__PURE__ */ new Date()
1684
+ });
1685
+ post_data2(
1686
+ `${config.root}${api_prefix}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`,
1687
+ {
1688
+ ...payload,
1689
+ session_hash
1690
+ }
1691
+ ).then(async ([output, status_code]) => {
1692
+ const data2 = output.data;
1693
+ if (status_code == 200) {
1694
+ fire_event({
1695
+ type: "data",
1696
+ endpoint: _endpoint,
1697
+ fn_index,
1698
+ data: handle_payload(
1699
+ data2,
1700
+ dependency,
1701
+ config.components,
1702
+ "output",
1703
+ options.with_null_state
1704
+ ),
1705
+ time: /* @__PURE__ */ new Date(),
1706
+ event_data,
1707
+ trigger_id
1708
+ });
1709
+ if (output.render_config) {
1710
+ await handle_render_config(output.render_config);
1725
1711
  }
1726
- }).catch((e) => {
1712
+ fire_event({
1713
+ type: "status",
1714
+ endpoint: _endpoint,
1715
+ fn_index,
1716
+ stage: "complete",
1717
+ eta: output.average_duration,
1718
+ queue: false,
1719
+ time: /* @__PURE__ */ new Date()
1720
+ });
1721
+ } else {
1727
1722
  fire_event({
1728
1723
  type: "status",
1729
1724
  stage: "error",
1730
- message: e.message,
1731
1725
  endpoint: _endpoint,
1732
1726
  fn_index,
1727
+ message: output.error,
1733
1728
  queue: false,
1734
1729
  time: /* @__PURE__ */ new Date()
1735
1730
  });
1736
- });
1737
- } else if (protocol == "sse") {
1731
+ }
1732
+ }).catch((e) => {
1738
1733
  fire_event({
1739
1734
  type: "status",
1740
- stage: "pending",
1741
- queue: true,
1735
+ stage: "error",
1736
+ message: e.message,
1742
1737
  endpoint: _endpoint,
1743
1738
  fn_index,
1739
+ queue: false,
1744
1740
  time: /* @__PURE__ */ new Date()
1745
1741
  });
1746
- var params = new URLSearchParams({
1747
- fn_index: fn_index.toString(),
1748
- session_hash
1749
- }).toString();
1750
- let url = new URL(
1751
- `${config.root}${api_prefix}/${SSE_URL}?${url_params ? url_params + "&" : ""}${params}`
1742
+ });
1743
+ } else if (protocol == "sse") {
1744
+ fire_event({
1745
+ type: "status",
1746
+ stage: "pending",
1747
+ queue: true,
1748
+ endpoint: _endpoint,
1749
+ fn_index,
1750
+ time: /* @__PURE__ */ new Date()
1751
+ });
1752
+ var params = new URLSearchParams({
1753
+ fn_index: fn_index.toString(),
1754
+ session_hash
1755
+ }).toString();
1756
+ let url = new URL(
1757
+ `${config.root}${api_prefix}/${SSE_URL}?${url_params ? url_params + "&" : ""}${params}`
1758
+ );
1759
+ if (this.jwt) {
1760
+ url.searchParams.set("__sign", this.jwt);
1761
+ }
1762
+ stream2 = this.stream(url);
1763
+ if (!stream2) {
1764
+ return Promise.reject(
1765
+ new Error("Cannot connect to SSE endpoint: " + url.toString())
1752
1766
  );
1753
- if (this.jwt) {
1754
- url.searchParams.set("__sign", this.jwt);
1755
- }
1756
- stream2 = this.stream(url);
1757
- if (!stream2) {
1758
- return Promise.reject(
1759
- new Error("Cannot connect to SSE endpoint: " + url.toString())
1760
- );
1761
- }
1762
- stream2.onmessage = async function(event) {
1763
- const _data = JSON.parse(event.data);
1764
- const { type, status, data: data2 } = handle_message(
1765
- _data,
1766
- last_status[fn_index]
1767
- );
1768
- if (type === "update" && status && !complete) {
1769
- fire_event({
1770
- type: "status",
1771
- endpoint: _endpoint,
1772
- fn_index,
1773
- time: /* @__PURE__ */ new Date(),
1774
- ...status
1775
- });
1776
- if (status.stage === "error") {
1777
- stream2 == null ? void 0 : stream2.close();
1778
- close();
1779
- }
1780
- } else if (type === "data") {
1781
- let [_, status2] = await post_data2(
1782
- `${config.root}${api_prefix}/queue/data`,
1783
- {
1784
- ...payload,
1785
- session_hash,
1786
- event_id
1787
- }
1788
- );
1789
- if (status2 !== 200) {
1790
- fire_event({
1791
- type: "status",
1792
- stage: "error",
1793
- message: BROKEN_CONNECTION_MSG,
1794
- queue: true,
1795
- endpoint: _endpoint,
1796
- fn_index,
1797
- time: /* @__PURE__ */ new Date()
1798
- });
1799
- stream2 == null ? void 0 : stream2.close();
1800
- close();
1801
- }
1802
- } else if (type === "complete") {
1803
- complete = status;
1804
- } else if (type === "log") {
1805
- fire_event({
1806
- type: "log",
1807
- title: data2.title,
1808
- log: data2.log,
1809
- level: data2.level,
1810
- endpoint: _endpoint,
1811
- duration: data2.duration,
1812
- visible: data2.visible,
1813
- fn_index
1814
- });
1815
- } else if (type === "generating" || type === "streaming") {
1816
- fire_event({
1817
- type: "status",
1818
- time: /* @__PURE__ */ new Date(),
1819
- ...status,
1820
- stage: status == null ? void 0 : status.stage,
1821
- queue: true,
1822
- endpoint: _endpoint,
1823
- fn_index
1824
- });
1825
- }
1826
- if (data2) {
1827
- fire_event({
1828
- type: "data",
1829
- time: /* @__PURE__ */ new Date(),
1830
- data: handle_payload(
1831
- data2.data,
1832
- dependency,
1833
- config.components,
1834
- "output",
1835
- options.with_null_state
1836
- ),
1837
- endpoint: _endpoint,
1838
- fn_index,
1839
- event_data,
1840
- trigger_id
1841
- });
1842
- if (complete) {
1843
- fire_event({
1844
- type: "status",
1845
- time: /* @__PURE__ */ new Date(),
1846
- ...complete,
1847
- stage: status == null ? void 0 : status.stage,
1848
- queue: true,
1849
- endpoint: _endpoint,
1850
- fn_index
1851
- });
1852
- stream2 == null ? void 0 : stream2.close();
1853
- close();
1854
- }
1767
+ }
1768
+ stream2.onmessage = async function(event) {
1769
+ const _data = JSON.parse(event.data);
1770
+ const { type, status, data: data2 } = handle_message(
1771
+ _data,
1772
+ last_status[fn_index]
1773
+ );
1774
+ if (type === "update" && status && !complete) {
1775
+ fire_event({
1776
+ type: "status",
1777
+ endpoint: _endpoint,
1778
+ fn_index,
1779
+ time: /* @__PURE__ */ new Date(),
1780
+ ...status
1781
+ });
1782
+ if (status.stage === "error") {
1783
+ stream2?.close();
1784
+ close();
1855
1785
  }
1856
- };
1857
- } else if (protocol == "sse_v1" || protocol == "sse_v2" || protocol == "sse_v2.1" || protocol == "sse_v3") {
1858
- fire_event({
1859
- type: "status",
1860
- stage: "pending",
1861
- queue: true,
1862
- endpoint: _endpoint,
1863
- fn_index,
1864
- time: /* @__PURE__ */ new Date()
1865
- });
1866
- let hostname = "";
1867
- if (typeof window !== "undefined" && typeof document !== "undefined") {
1868
- hostname = (_a2 = window == null ? void 0 : window.location) == null ? void 0 : _a2.hostname;
1869
- }
1870
- let hfhubdev = "dev.spaces.huggingface.tech";
1871
- const origin = hostname.includes(".dev.") ? `https://moon-${hostname.split(".")[1]}.${hfhubdev}` : `https://huggingface.co`;
1872
- const is_zerogpu_iframe = typeof window !== "undefined" && typeof document !== "undefined" && window.parent != window && window.supports_zerogpu_headers;
1873
- const zerogpu_auth_promise = is_zerogpu_iframe ? post_message("zerogpu-headers", origin) : Promise.resolve(null);
1874
- const post_data_promise = zerogpu_auth_promise.then((headers) => {
1875
- return post_data2(
1876
- `${config.root}${api_prefix}/${SSE_DATA_URL}?${url_params}`,
1786
+ } else if (type === "data") {
1787
+ let [_, status2] = await post_data2(
1788
+ `${config.root}${api_prefix}/queue/data`,
1877
1789
  {
1878
1790
  ...payload,
1879
- session_hash
1880
- },
1881
- headers
1791
+ session_hash,
1792
+ event_id
1793
+ }
1882
1794
  );
1883
- });
1884
- post_data_promise.then(async ([response, status]) => {
1885
- if (status === 503) {
1886
- fire_event({
1887
- type: "status",
1888
- stage: "error",
1889
- message: QUEUE_FULL_MSG,
1890
- queue: true,
1891
- endpoint: _endpoint,
1892
- fn_index,
1893
- time: /* @__PURE__ */ new Date()
1894
- });
1895
- } else if (status === 422) {
1795
+ if (status2 !== 200) {
1896
1796
  fire_event({
1897
1797
  type: "status",
1898
1798
  stage: "error",
1899
- message: response.detail,
1799
+ message: BROKEN_CONNECTION_MSG,
1900
1800
  queue: true,
1901
1801
  endpoint: _endpoint,
1902
1802
  fn_index,
1903
- code: "validation_error",
1904
1803
  time: /* @__PURE__ */ new Date()
1905
1804
  });
1805
+ stream2?.close();
1906
1806
  close();
1907
- } else if (status !== 200) {
1807
+ }
1808
+ } else if (type === "complete") {
1809
+ complete = status;
1810
+ } else if (type === "log") {
1811
+ fire_event({
1812
+ type: "log",
1813
+ title: data2.title,
1814
+ log: data2.log,
1815
+ level: data2.level,
1816
+ endpoint: _endpoint,
1817
+ duration: data2.duration,
1818
+ visible: data2.visible,
1819
+ fn_index
1820
+ });
1821
+ } else if (type === "generating" || type === "streaming") {
1822
+ fire_event({
1823
+ type: "status",
1824
+ time: /* @__PURE__ */ new Date(),
1825
+ ...status,
1826
+ stage: status?.stage,
1827
+ queue: true,
1828
+ endpoint: _endpoint,
1829
+ fn_index
1830
+ });
1831
+ }
1832
+ if (data2) {
1833
+ fire_event({
1834
+ type: "data",
1835
+ time: /* @__PURE__ */ new Date(),
1836
+ data: handle_payload(
1837
+ data2.data,
1838
+ dependency,
1839
+ config.components,
1840
+ "output",
1841
+ options.with_null_state
1842
+ ),
1843
+ endpoint: _endpoint,
1844
+ fn_index,
1845
+ event_data,
1846
+ trigger_id
1847
+ });
1848
+ if (complete) {
1908
1849
  fire_event({
1909
1850
  type: "status",
1910
- stage: "error",
1911
- broken: false,
1912
- message: response.detail,
1851
+ time: /* @__PURE__ */ new Date(),
1852
+ ...complete,
1853
+ stage: status?.stage,
1913
1854
  queue: true,
1914
1855
  endpoint: _endpoint,
1915
- fn_index,
1916
- time: /* @__PURE__ */ new Date()
1856
+ fn_index
1917
1857
  });
1918
- } else {
1919
- event_id = response.event_id;
1920
- event_id_final = event_id;
1921
- let callback = async function(_data) {
1922
- try {
1923
- const { type, status: status2, data: data2, original_msg } = handle_message(
1924
- _data,
1925
- last_status[fn_index]
1926
- );
1927
- if (type == "heartbeat") {
1928
- return;
1858
+ stream2?.close();
1859
+ close();
1860
+ }
1861
+ }
1862
+ };
1863
+ } else if (protocol == "sse_v1" || protocol == "sse_v2" || protocol == "sse_v2.1" || protocol == "sse_v3") {
1864
+ fire_event({
1865
+ type: "status",
1866
+ stage: "pending",
1867
+ queue: true,
1868
+ endpoint: _endpoint,
1869
+ fn_index,
1870
+ time: /* @__PURE__ */ new Date()
1871
+ });
1872
+ let hostname = "";
1873
+ if (typeof window !== "undefined" && typeof document !== "undefined") {
1874
+ hostname = window?.location?.hostname;
1875
+ }
1876
+ let hfhubdev = "dev.spaces.huggingface.tech";
1877
+ const origin = hostname.includes(".dev.") ? `https://moon-${hostname.split(".")[1]}.${hfhubdev}` : `https://huggingface.co`;
1878
+ const is_zerogpu_iframe = typeof window !== "undefined" && typeof document !== "undefined" && window.parent != window && window.supports_zerogpu_headers;
1879
+ const zerogpu_auth_promise = is_zerogpu_iframe ? post_message("zerogpu-headers", origin) : Promise.resolve(null);
1880
+ const post_data_promise = zerogpu_auth_promise.then((headers) => {
1881
+ return post_data2(
1882
+ `${config.root}${api_prefix}/${SSE_DATA_URL}?${url_params}`,
1883
+ {
1884
+ ...payload,
1885
+ session_hash
1886
+ },
1887
+ headers
1888
+ );
1889
+ });
1890
+ return post_data_promise.then(async ([response, status]) => {
1891
+ if (response.event_id) {
1892
+ event_id_final = response.event_id;
1893
+ }
1894
+ if (status === 503) {
1895
+ fire_event({
1896
+ type: "status",
1897
+ stage: "error",
1898
+ message: QUEUE_FULL_MSG,
1899
+ queue: true,
1900
+ endpoint: _endpoint,
1901
+ fn_index,
1902
+ time: /* @__PURE__ */ new Date(),
1903
+ visible: true
1904
+ });
1905
+ } else if (status === 422) {
1906
+ fire_event({
1907
+ type: "status",
1908
+ stage: "error",
1909
+ message: response.detail,
1910
+ queue: true,
1911
+ endpoint: _endpoint,
1912
+ fn_index,
1913
+ code: "validation_error",
1914
+ time: /* @__PURE__ */ new Date(),
1915
+ visible: true
1916
+ });
1917
+ close();
1918
+ } else if (status !== 200) {
1919
+ fire_event({
1920
+ type: "status",
1921
+ stage: "error",
1922
+ broken: false,
1923
+ message: response.detail,
1924
+ queue: true,
1925
+ endpoint: _endpoint,
1926
+ fn_index,
1927
+ time: /* @__PURE__ */ new Date(),
1928
+ visible: true
1929
+ });
1930
+ } else {
1931
+ event_id = response.event_id;
1932
+ event_id_final = event_id;
1933
+ let callback = async function(_data) {
1934
+ try {
1935
+ const { type, status: status2, data: data2, original_msg } = handle_message(
1936
+ _data,
1937
+ last_status[fn_index]
1938
+ );
1939
+ if (type == "heartbeat") {
1940
+ return;
1941
+ }
1942
+ if (type === "update" && status2 && !complete) {
1943
+ fire_event({
1944
+ type: "status",
1945
+ endpoint: _endpoint,
1946
+ fn_index,
1947
+ time: /* @__PURE__ */ new Date(),
1948
+ original_msg,
1949
+ ...status2
1950
+ });
1951
+ } else if (type === "complete") {
1952
+ complete = status2;
1953
+ } else if (type == "unexpected_error" || type == "broken_connection") {
1954
+ console.error("Unexpected error", status2?.message);
1955
+ const broken = type === "broken_connection";
1956
+ fire_event({
1957
+ type: "status",
1958
+ stage: "error",
1959
+ message: status2?.message || "An Unexpected Error Occurred!",
1960
+ queue: true,
1961
+ endpoint: _endpoint,
1962
+ broken,
1963
+ session_not_found: status2?.session_not_found,
1964
+ fn_index,
1965
+ time: /* @__PURE__ */ new Date()
1966
+ });
1967
+ } else if (type === "log") {
1968
+ fire_event({
1969
+ type: "log",
1970
+ title: data2.title,
1971
+ log: data2.log,
1972
+ level: data2.level,
1973
+ endpoint: _endpoint,
1974
+ duration: data2.duration,
1975
+ visible: data2.visible,
1976
+ fn_index
1977
+ });
1978
+ return;
1979
+ } else if (type === "generating" || type === "streaming") {
1980
+ fire_event({
1981
+ type: "status",
1982
+ time: /* @__PURE__ */ new Date(),
1983
+ ...status2,
1984
+ stage: status2?.stage,
1985
+ queue: true,
1986
+ endpoint: _endpoint,
1987
+ fn_index
1988
+ });
1989
+ if (data2 && dependency.connection !== "stream" && ["sse_v2", "sse_v2.1", "sse_v3"].includes(protocol)) {
1990
+ apply_diff_stream(pending_diff_streams, event_id, data2);
1929
1991
  }
1930
- if (type === "update" && status2 && !complete) {
1931
- fire_event({
1932
- type: "status",
1933
- endpoint: _endpoint,
1934
- fn_index,
1935
- time: /* @__PURE__ */ new Date(),
1936
- original_msg,
1937
- ...status2
1938
- });
1939
- } else if (type === "complete") {
1940
- complete = status2;
1941
- } else if (type == "unexpected_error" || type == "broken_connection") {
1942
- console.error("Unexpected error", status2 == null ? void 0 : status2.message);
1943
- const broken = type === "broken_connection";
1944
- fire_event({
1945
- type: "status",
1946
- stage: "error",
1947
- message: (status2 == null ? void 0 : status2.message) || "An Unexpected Error Occurred!",
1948
- queue: true,
1949
- endpoint: _endpoint,
1950
- broken,
1951
- session_not_found: status2 == null ? void 0 : status2.session_not_found,
1952
- fn_index,
1953
- time: /* @__PURE__ */ new Date()
1954
- });
1955
- } else if (type === "log") {
1956
- fire_event({
1957
- type: "log",
1958
- title: data2.title,
1959
- log: data2.log,
1960
- level: data2.level,
1961
- endpoint: _endpoint,
1962
- duration: data2.duration,
1963
- visible: data2.visible,
1964
- fn_index
1965
- });
1966
- return;
1967
- } else if (type === "generating" || type === "streaming") {
1992
+ }
1993
+ if (data2) {
1994
+ fire_event({
1995
+ type: "data",
1996
+ time: /* @__PURE__ */ new Date(),
1997
+ data: handle_payload(
1998
+ data2.data,
1999
+ dependency,
2000
+ config.components,
2001
+ "output",
2002
+ options.with_null_state
2003
+ ),
2004
+ endpoint: _endpoint,
2005
+ fn_index
2006
+ });
2007
+ if (data2.render_config) {
2008
+ await handle_render_config(data2.render_config);
2009
+ }
2010
+ if (complete) {
1968
2011
  fire_event({
1969
2012
  type: "status",
1970
2013
  time: /* @__PURE__ */ new Date(),
1971
- ...status2,
1972
- stage: status2 == null ? void 0 : status2.stage,
2014
+ ...complete,
2015
+ stage: status2?.stage,
1973
2016
  queue: true,
1974
2017
  endpoint: _endpoint,
1975
2018
  fn_index
1976
2019
  });
1977
- if (data2 && dependency.connection !== "stream" && ["sse_v2", "sse_v2.1", "sse_v3"].includes(protocol)) {
1978
- apply_diff_stream(pending_diff_streams, event_id, data2);
1979
- }
1980
- }
1981
- if (data2) {
1982
- fire_event({
1983
- type: "data",
1984
- time: /* @__PURE__ */ new Date(),
1985
- data: handle_payload(
1986
- data2.data,
1987
- dependency,
1988
- config.components,
1989
- "output",
1990
- options.with_null_state
1991
- ),
1992
- endpoint: _endpoint,
1993
- fn_index
1994
- });
1995
- if (data2.render_config) {
1996
- await handle_render_config(data2.render_config);
1997
- }
1998
- if (complete) {
1999
- fire_event({
2000
- type: "status",
2001
- time: /* @__PURE__ */ new Date(),
2002
- ...complete,
2003
- stage: status2 == null ? void 0 : status2.stage,
2004
- queue: true,
2005
- endpoint: _endpoint,
2006
- fn_index
2007
- });
2008
- close();
2009
- }
2020
+ close();
2010
2021
  }
2011
- if ((status2 == null ? void 0 : status2.stage) === "complete" || (status2 == null ? void 0 : status2.stage) === "error") {
2012
- if (event_callbacks[event_id]) {
2013
- delete event_callbacks[event_id];
2014
- }
2015
- if (event_id in pending_diff_streams) {
2016
- delete pending_diff_streams[event_id];
2017
- }
2022
+ }
2023
+ if (status2?.stage === "complete" || status2?.stage === "error") {
2024
+ if (event_callbacks[event_id]) {
2025
+ delete event_callbacks[event_id];
2018
2026
  }
2019
- } catch (e) {
2020
- console.error("Unexpected client exception", e);
2021
- fire_event({
2022
- type: "status",
2023
- stage: "error",
2024
- message: "An Unexpected Error Occurred!",
2025
- queue: true,
2026
- endpoint: _endpoint,
2027
- fn_index,
2028
- time: /* @__PURE__ */ new Date()
2029
- });
2030
- if (["sse_v2", "sse_v2.1", "sse_v3"].includes(protocol)) {
2031
- close_stream(stream_status, that.abort_controller);
2032
- stream_status.open = false;
2033
- close();
2027
+ if (event_id in pending_diff_streams) {
2028
+ delete pending_diff_streams[event_id];
2034
2029
  }
2035
2030
  }
2036
- };
2037
- if (event_id in pending_stream_messages) {
2038
- pending_stream_messages[event_id].forEach(
2039
- (msg) => callback(msg)
2040
- );
2041
- delete pending_stream_messages[event_id];
2042
- }
2043
- event_callbacks[event_id] = callback;
2044
- unclosed_events.add(event_id);
2045
- if (!stream_status.open) {
2046
- await this.open_stream();
2031
+ } catch (e) {
2032
+ console.error("Unexpected client exception", e);
2033
+ fire_event({
2034
+ type: "status",
2035
+ stage: "error",
2036
+ message: "An Unexpected Error Occurred!",
2037
+ queue: true,
2038
+ endpoint: _endpoint,
2039
+ fn_index,
2040
+ time: /* @__PURE__ */ new Date()
2041
+ });
2042
+ if (["sse_v2", "sse_v2.1", "sse_v3"].includes(protocol)) {
2043
+ close_stream(stream_status, that.abort_controller);
2044
+ stream_status.open = false;
2045
+ close();
2046
+ }
2047
2047
  }
2048
+ };
2049
+ if (event_id in pending_stream_messages) {
2050
+ pending_stream_messages[event_id].forEach((msg) => callback(msg));
2051
+ delete pending_stream_messages[event_id];
2048
2052
  }
2049
- });
2050
- }
2053
+ event_callbacks[event_id] = callback;
2054
+ unclosed_events.add(event_id);
2055
+ if (!stream_status.open) {
2056
+ await this.open_stream();
2057
+ }
2058
+ }
2059
+ });
2051
2060
  }
2052
- );
2061
+ });
2053
2062
  let done = false;
2054
2063
  const values = [];
2055
2064
  const resolvers = [];
@@ -2062,10 +2071,27 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events) {
2062
2071
  },
2063
2072
  return: async () => {
2064
2073
  close();
2065
- return next();
2074
+ return { value: void 0, done: true };
2066
2075
  },
2067
2076
  cancel,
2068
- event_id: event_id_cb
2077
+ send_chunk: (payload2) => {
2078
+ this.post_data(`${config.root}${api_prefix}/stream/${event_id_final}`, {
2079
+ ...payload2,
2080
+ session_hash: this.session_hash
2081
+ });
2082
+ },
2083
+ close_stream: () => {
2084
+ this.post_data(
2085
+ `${config.root}${api_prefix}/stream/${event_id_final}/close`,
2086
+ {}
2087
+ );
2088
+ close();
2089
+ },
2090
+ event_id: () => event_id_final,
2091
+ wait_for_id: async () => {
2092
+ await job;
2093
+ return event_id;
2094
+ }
2069
2095
  };
2070
2096
  return iterator;
2071
2097
  } catch (error) {
@@ -2102,61 +2128,28 @@ function get_endpoint_info(api_info, endpoint, api_map, config) {
2102
2128
  return { fn_index, endpoint_info, dependency };
2103
2129
  }
2104
2130
  class Client {
2105
- constructor(app_reference, options = { events: ["data"] }) {
2106
- __publicField(this, "app_reference");
2107
- __publicField(this, "options");
2108
- __publicField(this, "deep_link", null);
2109
- __publicField(this, "config");
2110
- __publicField(this, "api_prefix", "");
2111
- __publicField(this, "api_info");
2112
- __publicField(this, "api_map", {});
2113
- __publicField(this, "session_hash", Math.random().toString(36).substring(2));
2114
- __publicField(this, "jwt", false);
2115
- __publicField(this, "last_status", {});
2116
- __publicField(this, "cookies", null);
2117
- // streaming
2118
- __publicField(this, "stream_status", { open: false });
2119
- __publicField(this, "closed", false);
2120
- __publicField(this, "pending_stream_messages", {});
2121
- __publicField(this, "pending_diff_streams", {});
2122
- __publicField(this, "event_callbacks", {});
2123
- __publicField(this, "unclosed_events", /* @__PURE__ */ new Set());
2124
- __publicField(this, "heartbeat_event", null);
2125
- __publicField(this, "abort_controller", null);
2126
- __publicField(this, "stream_instance", null);
2127
- __publicField(this, "current_payload");
2128
- __publicField(this, "view_api");
2129
- __publicField(this, "upload_files");
2130
- __publicField(this, "upload");
2131
- __publicField(this, "handle_blob");
2132
- __publicField(this, "post_data");
2133
- __publicField(this, "submit");
2134
- __publicField(this, "predict");
2135
- __publicField(this, "open_stream");
2136
- __publicField(this, "resolve_config");
2137
- __publicField(this, "resolve_cookies");
2138
- var _a;
2139
- this.app_reference = app_reference;
2140
- this.deep_link = ((_a = options.query_params) == null ? void 0 : _a.deep_link) || null;
2141
- if (!options.events) {
2142
- options.events = ["data"];
2143
- }
2144
- this.options = options;
2145
- this.current_payload = {};
2146
- this.view_api = view_api.bind(this);
2147
- this.upload_files = upload_files.bind(this);
2148
- this.handle_blob = handle_blob.bind(this);
2149
- this.post_data = post_data.bind(this);
2150
- this.submit = submit.bind(this);
2151
- this.predict = predict.bind(this);
2152
- this.open_stream = open_stream.bind(this);
2153
- this.resolve_config = resolve_config.bind(this);
2154
- this.resolve_cookies = resolve_cookies.bind(this);
2155
- this.upload = upload.bind(this);
2156
- this.fetch = this.fetch.bind(this);
2157
- this.handle_space_success = this.handle_space_success.bind(this);
2158
- this.stream = this.stream.bind(this);
2159
- }
2131
+ app_reference;
2132
+ options;
2133
+ deep_link = null;
2134
+ config;
2135
+ api_prefix = "";
2136
+ api_info;
2137
+ api_map = {};
2138
+ session_hash = Math.random().toString(36).substring(2);
2139
+ jwt = false;
2140
+ last_status = {};
2141
+ cookies = null;
2142
+ // streaming
2143
+ stream_status = { open: false };
2144
+ closed = false;
2145
+ pending_stream_messages = {};
2146
+ pending_diff_streams = {};
2147
+ event_callbacks = {};
2148
+ unclosed_events = /* @__PURE__ */ new Set();
2149
+ heartbeat_event = null;
2150
+ abort_controller = null;
2151
+ stream_instance = null;
2152
+ current_payload;
2160
2153
  get_url_config(url = null) {
2161
2154
  if (!this.config) {
2162
2155
  throw new Error(CONFIG_ERROR_MSG);
@@ -2196,7 +2189,7 @@ class Client {
2196
2189
  };
2197
2190
  }
2198
2191
  fetch(input, init) {
2199
- const headers = new Headers((init == null ? void 0 : init.headers) || {});
2192
+ const headers = new Headers(init?.headers || {});
2200
2193
  if (this && this.cookies) {
2201
2194
  headers.append("Cookie", this.cookies);
2202
2195
  }
@@ -2228,8 +2221,39 @@ class Client {
2228
2221
  });
2229
2222
  return this.stream_instance;
2230
2223
  }
2224
+ view_api;
2225
+ upload_files;
2226
+ upload;
2227
+ handle_blob;
2228
+ post_data;
2229
+ submit;
2230
+ predict;
2231
+ open_stream;
2232
+ resolve_config;
2233
+ resolve_cookies;
2234
+ constructor(app_reference, options = { events: ["data"] }) {
2235
+ this.app_reference = app_reference;
2236
+ this.deep_link = options.query_params?.deep_link || null;
2237
+ if (!options.events) {
2238
+ options.events = ["data"];
2239
+ }
2240
+ this.options = options;
2241
+ this.current_payload = {};
2242
+ this.view_api = view_api.bind(this);
2243
+ this.upload_files = upload_files.bind(this);
2244
+ this.handle_blob = handle_blob.bind(this);
2245
+ this.post_data = post_data.bind(this);
2246
+ this.submit = submit.bind(this);
2247
+ this.predict = predict.bind(this);
2248
+ this.open_stream = open_stream.bind(this);
2249
+ this.resolve_config = resolve_config.bind(this);
2250
+ this.resolve_cookies = resolve_cookies.bind(this);
2251
+ this.upload = upload.bind(this);
2252
+ this.fetch = this.fetch.bind(this);
2253
+ this.handle_space_success = this.handle_space_success.bind(this);
2254
+ this.stream = this.stream.bind(this);
2255
+ }
2231
2256
  async init() {
2232
- var _a;
2233
2257
  if (this.options.auth) {
2234
2258
  await this.resolve_cookies();
2235
2259
  }
@@ -2237,7 +2261,7 @@ class Client {
2237
2261
  ({ config }) => this._resolve_heartbeat(config)
2238
2262
  );
2239
2263
  this.api_info = await this.view_api();
2240
- this.api_map = map_names_to_ids(((_a = this.config) == null ? void 0 : _a.dependencies) || []);
2264
+ this.api_map = map_names_to_ids(this.config?.dependencies || []);
2241
2265
  }
2242
2266
  async _resolve_heartbeat(_config) {
2243
2267
  if (_config) {
@@ -2359,7 +2383,6 @@ class Client {
2359
2383
  return this.prepare_return_obj();
2360
2384
  }
2361
2385
  async handle_space_success(status) {
2362
- var _a;
2363
2386
  if (!this) {
2364
2387
  throw new Error(CONFIG_ERROR_MSG);
2365
2388
  }
@@ -2368,7 +2391,7 @@ class Client {
2368
2391
  if (status.status === "running") {
2369
2392
  try {
2370
2393
  this.config = await this._resolve_config();
2371
- this.api_prefix = ((_a = this == null ? void 0 : this.config) == null ? void 0 : _a.api_prefix) || "";
2394
+ this.api_prefix = this?.config?.api_prefix || "";
2372
2395
  if (!this.config) {
2373
2396
  throw new Error(CONFIG_ERROR_MSG);
2374
2397
  }
@@ -2388,7 +2411,6 @@ class Client {
2388
2411
  }
2389
2412
  }
2390
2413
  async component_server(component_id, fn_name, data) {
2391
- var _a;
2392
2414
  if (!this.config) {
2393
2415
  throw new Error(CONFIG_ERROR_MSG);
2394
2416
  }
@@ -2402,7 +2424,7 @@ class Client {
2402
2424
  let component = this.config.components.find(
2403
2425
  (comp) => comp.id === component_id
2404
2426
  );
2405
- if ((_a = component == null ? void 0 : component.props) == null ? void 0 : _a.root_url) {
2427
+ if (component?.props?.root_url) {
2406
2428
  root_url = component.props.root_url;
2407
2429
  } else {
2408
2430
  root_url = this.config.root;