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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
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
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
var fn = new Intl.Collator(0, { numeric: 1 }).compare;
function semiver(a, b, bool) {
  a = a.split(".");
  b = b.split(".");
  return fn(a[0], b[0]) || fn(a[1], b[1]) || (b[2] = b.slice(2).join("."), bool = /[.-]/.test(a[2] = a.slice(2).join(".")), bool == /[.-]/.test(b[2]) ? fn(a[2], b[2]) : bool ? -1 : 1);
}
function resolve_root(base_url, root_path, prioritize_base) {
  if (root_path.startsWith("http://") || root_path.startsWith("https://")) {
    return prioritize_base ? base_url : root_path;
  }
  return base_url + root_path;
}
function determine_protocol(endpoint) {
  if (endpoint.startsWith("http")) {
    const { protocol, host } = new URL(endpoint);
    if (host.endsWith("hf.space")) {
      return {
        ws_protocol: "wss",
        host,
        http_protocol: protocol
      };
    }
    return {
      ws_protocol: protocol === "https:" ? "wss" : "ws",
      http_protocol: protocol,
      host
    };
  } else if (endpoint.startsWith("file:")) {
    return {
      ws_protocol: "ws",
      http_protocol: "http:",
      host: "lite.local"
      // Special fake hostname only used for this case. This matches the hostname allowed in `is_self_host()` in `js/wasm/network/host.ts`.
    };
  }
  return {
    ws_protocol: "wss",
    http_protocol: "https:",
    host: endpoint
  };
}
const RE_SPACE_NAME = /^[^\/]*\/[^\/]*$/;
const RE_SPACE_DOMAIN = /.*hf\.space\/{0,1}$/;
async function process_endpoint(app_reference, token) {
  const headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  const _app_reference = app_reference.trim();
  if (RE_SPACE_NAME.test(_app_reference)) {
    try {
      const res = await fetch(
        `https://huggingface.co/api/spaces/${_app_reference}/host`,
        { headers }
      );
      if (res.status !== 200)
        throw new Error("Space metadata could not be loaded.");
      const _host = (await res.json()).host;
      return {
        space_id: app_reference,
        ...determine_protocol(_host)
      };
    } catch (e) {
      throw new Error("Space metadata could not be loaded." + e.message);
    }
  }
  if (RE_SPACE_DOMAIN.test(_app_reference)) {
    const { ws_protocol, http_protocol, host } = determine_protocol(_app_reference);
    return {
      space_id: host.replace(".hf.space", ""),
      ws_protocol,
      http_protocol,
      host
    };
  }
  return {
    space_id: false,
    ...determine_protocol(_app_reference)
  };
}
function map_names_to_ids(fns) {
  let apis = {};
  fns.forEach(({ api_name }, i) => {
    if (api_name)
      apis[api_name] = i;
  });
  return apis;
}
const RE_DISABLED_DISCUSSION = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
async function discussions_enabled(space_id) {
  try {
    const r = await fetch(
      `https://huggingface.co/api/spaces/${space_id}/discussions`,
      {
        method: "HEAD"
      }
    );
    const error = r.headers.get("x-error-message");
    if (error && RE_DISABLED_DISCUSSION.test(error))
      return false;
    return true;
  } catch (e) {
    return false;
  }
}
async function get_space_hardware(space_id, token) {
  const headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  try {
    const res = await fetch(
      `https://huggingface.co/api/spaces/${space_id}/runtime`,
      { headers }
    );
    if (res.status !== 200)
      throw new Error("Space hardware could not be obtained.");
    const { hardware } = await res.json();
    return hardware;
  } catch (e) {
    throw new Error(e.message);
  }
}
async function set_space_hardware(space_id, new_hardware, token) {
  const headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  try {
    const res = await fetch(
      `https://huggingface.co/api/spaces/${space_id}/hardware`,
      { headers, body: JSON.stringify(new_hardware) }
    );
    if (res.status !== 200)
      throw new Error(
        "Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
      );
    const { hardware } = await res.json();
    return hardware;
  } catch (e) {
    throw new Error(e.message);
  }
}
async function set_space_timeout(space_id, timeout, token) {
  const headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  try {
    const res = await fetch(
      `https://huggingface.co/api/spaces/${space_id}/hardware`,
      { headers, body: JSON.stringify({ seconds: timeout }) }
    );
    if (res.status !== 200)
      throw new Error(
        "Space hardware could not be set. Please ensure the space hardware provided is valid and that a Hugging Face token is passed in."
      );
    const { hardware } = await res.json();
    return hardware;
  } catch (e) {
    throw new Error(e.message);
  }
}
const hardware_types = [
  "cpu-basic",
  "cpu-upgrade",
  "t4-small",
  "t4-medium",
  "a10g-small",
  "a10g-large",
  "a100-large"
];
function normalise_file(file, server_url, proxy_url) {
  if (file == null) {
    return null;
  }
  if (Array.isArray(file)) {
    const normalized_file = [];
    for (const x of file) {
      if (x == null) {
        normalized_file.push(null);
      } else {
        normalized_file.push(normalise_file(x, server_url, proxy_url));
      }
    }
    return normalized_file;
  }
  if (file.is_stream) {
    if (proxy_url == null) {
      return new FileData({
        ...file,
        url: server_url + "/stream/" + file.path
      });
    }
    return new FileData({
      ...file,
      url: "/proxy=" + proxy_url + "stream/" + file.path
    });
  }
  return new FileData({
    ...file,
    url: get_fetchable_url_or_file(file.path, server_url, proxy_url)
  });
}
function is_url(str) {
  try {
    const url = new URL(str);
    return url.protocol === "http:" || url.protocol === "https:";
  } catch {
    return false;
  }
}
function get_fetchable_url_or_file(path, server_url, proxy_url) {
  if (path == null) {
    return proxy_url ? `/proxy=${proxy_url}file=` : `${server_url}/file=`;
  }
  if (is_url(path)) {
    return path;
  }
  return proxy_url ? `/proxy=${proxy_url}file=${path}` : `${server_url}/file=${path}`;
}
async function upload(file_data, root, upload_id, upload_fn = upload_files) {
  let files = (Array.isArray(file_data) ? file_data : [file_data]).map(
    (file_data2) => file_data2.blob
  );
  return await Promise.all(
    await upload_fn(root, files, void 0, upload_id).then(
      async (response) => {
        if (response.error) {
          throw new Error(response.error);
        } else {
          if (response.files) {
            return response.files.map((f, i) => {
              const file = new FileData({ ...file_data[i], path: f });
              return normalise_file(file, root, null);
            });
          }
          return [];
        }
      }
    )
  );
}
async function prepare_files(files, is_stream) {
  return files.map(
    (f, i) => new FileData({
      path: f.name,
      orig_name: f.name,
      blob: f,
      size: f.size,
      mime_type: f.type,
      is_stream
    })
  );
}
class FileData {
  constructor({
    path,
    url,
    orig_name,
    size,
    blob,
    is_stream,
    mime_type,
    alt_text
  }) {
    this.path = path;
    this.url = url;
    this.orig_name = orig_name;
    this.size = size;
    this.blob = url ? void 0 : blob;
    this.is_stream = is_stream;
    this.mime_type = mime_type;
    this.alt_text = alt_text;
  }
}
const QUEUE_FULL_MSG = "This application is too busy. Keep trying!";
const BROKEN_CONNECTION_MSG = "Connection errored out.";
let NodeBlob;
async function duplicate(app_reference, options) {
  const { hf_token, private: _private, hardware, timeout } = options;
  if (hardware && !hardware_types.includes(hardware)) {
    throw new Error(
      `Invalid hardware type provided. Valid types are: ${hardware_types.map((v) => `"${v}"`).join(",")}.`
    );
  }
  const headers = {
    Authorization: `Bearer ${hf_token}`
  };
  const user = (await (await fetch(`https://huggingface.co/api/whoami-v2`, {
    headers
  })).json()).name;
  const space_name = app_reference.split("/")[1];
  const body = {
    repository: `${user}/${space_name}`
  };
  if (_private) {
    body.private = true;
  }
  try {
    const response = await fetch(
      `https://huggingface.co/api/spaces/${app_reference}/duplicate`,
      {
        method: "POST",
        headers: { "Content-Type": "application/json", ...headers },
        body: JSON.stringify(body)
      }
    );
    if (response.status === 409) {
      return client(`${user}/${space_name}`, options);
    }
    const duplicated_space = await response.json();
    let original_hardware;
    if (!hardware) {
      original_hardware = await get_space_hardware(app_reference, hf_token);
    }
    const requested_hardware = hardware || original_hardware || "cpu-basic";
    await set_space_hardware(
      `${user}/${space_name}`,
      requested_hardware,
      hf_token
    );
    await set_space_timeout(`${user}/${space_name}`, timeout || 300, hf_token);
    return client(duplicated_space.url, options);
  } catch (e) {
    throw new Error(e);
  }
}
function api_factory(fetch_implementation, EventSource_factory) {
  return { post_data: post_data2, upload_files: upload_files2, client: client2, handle_blob: handle_blob2 };
  async function post_data2(url, body, token) {
    const headers = { "Content-Type": "application/json" };
    if (token) {
      headers.Authorization = `Bearer ${token}`;
    }
    try {
      var response = await fetch_implementation(url, {
        method: "POST",
        body: JSON.stringify(body),
        headers
      });
    } catch (e) {
      return [{ error: BROKEN_CONNECTION_MSG }, 500];
    }
    let output;
    let status;
    try {
      output = await response.json();
      status = response.status;
    } catch (e) {
      output = { error: `Could not parse server response: ${e}` };
      status = 500;
    }
    return [output, status];
  }
  async function upload_files2(root, files, token, upload_id) {
    const headers = {};
    if (token) {
      headers.Authorization = `Bearer ${token}`;
    }
    const chunkSize = 1e3;
    const uploadResponses = [];
    for (let i = 0; i < files.length; i += chunkSize) {
      const chunk = files.slice(i, i + chunkSize);
      const formData = new FormData();
      chunk.forEach((file) => {
        formData.append("files", file);
      });
      try {
        const upload_url = upload_id ? `${root}/upload?upload_id=${upload_id}` : `${root}/upload`;
        var response = await fetch_implementation(upload_url, {
          method: "POST",
          body: formData,
          headers
        });
      } catch (e) {
        return { error: BROKEN_CONNECTION_MSG };
      }
      const output = await response.json();
      uploadResponses.push(...output);
    }
    return { files: uploadResponses };
  }
  async function client2(app_reference, options = { normalise_files: true }) {
    return new Promise(async (res) => {
      const { status_callback, hf_token, normalise_files } = options;
      const return_obj = {
        predict,
        submit,
        view_api,
        component_server
      };
      const transform_files = normalise_files ?? true;
      if ((typeof window === "undefined" || !("WebSocket" in window)) && !global.Websocket) {
        const ws = await import("./wrapper-6f348d45.js");
        NodeBlob = (await import("node:buffer")).Blob;
        global.WebSocket = ws.WebSocket;
      }
      const { ws_protocol, http_protocol, host, space_id } = await process_endpoint(app_reference, hf_token);
      const session_hash = Math.random().toString(36).substring(2);
      const last_status = {};
      let stream_open = false;
      let pending_stream_messages = {};
      let event_stream = null;
      const event_callbacks = {};
      const unclosed_events = /* @__PURE__ */ new Set();
      let config;
      let api_map = {};
      let jwt = false;
      if (hf_token && space_id) {
        jwt = await get_jwt(space_id, hf_token);
      }
      async function config_success(_config) {
        config = _config;
        api_map = map_names_to_ids((_config == null ? void 0 : _config.dependencies) || []);
        if (config.auth_required) {
          return {
            config,
            ...return_obj
          };
        }
        try {
          api = await view_api(config);
        } catch (e) {
          console.error(`Could not get api details: ${e.message}`);
        }
        return {
          config,
          ...return_obj
        };
      }
      let api;
      async function handle_space_sucess(status) {
        if (status_callback)
          status_callback(status);
        if (status.status === "running")
          try {
            config = await resolve_config(
              fetch_implementation,
              `${http_protocol}//${host}`,
              hf_token
            );
            const _config = await config_success(config);
            res(_config);
          } catch (e) {
            console.error(e);
            if (status_callback) {
              status_callback({
                status: "error",
                message: "Could not load this space.",
                load_status: "error",
                detail: "NOT_FOUND"
              });
            }
          }
      }
      try {
        config = await resolve_config(
          fetch_implementation,
          `${http_protocol}//${host}`,
          hf_token
        );
        const _config = await config_success(config);
        res(_config);
      } catch (e) {
        console.error(e);
        if (space_id) {
          check_space_status(
            space_id,
            RE_SPACE_NAME.test(space_id) ? "space_name" : "subdomain",
            handle_space_sucess
          );
        } else {
          if (status_callback)
            status_callback({
              status: "error",
              message: "Could not load this space.",
              load_status: "error",
              detail: "NOT_FOUND"
            });
        }
      }
      function predict(endpoint, data, event_data) {
        let data_returned = false;
        let status_complete = false;
        let dependency;
        if (typeof endpoint === "number") {
          dependency = config.dependencies[endpoint];
        } else {
          const trimmed_endpoint = endpoint.replace(/^\//, "");
          dependency = config.dependencies[api_map[trimmed_endpoint]];
        }
        if (dependency.types.continuous) {
          throw new Error(
            "Cannot call predict on this function as it may run forever. Use submit instead"
          );
        }
        return new Promise((res2, rej) => {
          const app = submit(endpoint, data, event_data);
          let result;
          app.on("data", (d) => {
            if (status_complete) {
              app.destroy();
              res2(d);
            }
            data_returned = true;
            result = d;
          }).on("status", (status) => {
            if (status.stage === "error")
              rej(status);
            if (status.stage === "complete") {
              status_complete = true;
              if (data_returned) {
                app.destroy();
                res2(result);
              }
            }
          });
        });
      }
      function submit(endpoint, data, event_data, trigger_id = null) {
        let fn_index;
        let api_info;
        if (typeof endpoint === "number") {
          fn_index = endpoint;
          api_info = api.unnamed_endpoints[fn_index];
        } else {
          const trimmed_endpoint = endpoint.replace(/^\//, "");
          fn_index = api_map[trimmed_endpoint];
          api_info = api.named_endpoints[endpoint.trim()];
        }
        if (typeof fn_index !== "number") {
          throw new Error(
            "There is no endpoint matching that name of fn_index matching that number."
          );
        }
        let websocket;
        let eventSource;
        let protocol = config.protocol ?? "ws";
        const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
        let payload;
        let event_id = null;
        let complete = false;
        const listener_map = {};
        let url_params = "";
        if (typeof window !== "undefined") {
          url_params = new URLSearchParams(window.location.search).toString();
        }
        handle_blob2(`${config.root}`, data, api_info, hf_token).then(
          (_payload) => {
            payload = {
              data: _payload || [],
              event_data,
              fn_index,
              trigger_id
            };
            if (skip_queue(fn_index, config)) {
              fire_event({
                type: "status",
                endpoint: _endpoint,
                stage: "pending",
                queue: false,
                fn_index,
                time: /* @__PURE__ */ new Date()
              });
              post_data2(
                `${config.root}/run${_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`}${url_params ? "?" + url_params : ""}`,
                {
                  ...payload,
                  session_hash
                },
                hf_token
              ).then(([output, status_code]) => {
                const data2 = transform_files ? transform_output(
                  output.data,
                  api_info,
                  config.root,
                  config.root_url
                ) : output.data;
                if (status_code == 200) {
                  fire_event({
                    type: "data",
                    endpoint: _endpoint,
                    fn_index,
                    data: data2,
                    time: /* @__PURE__ */ new Date()
                  });
                  fire_event({
                    type: "status",
                    endpoint: _endpoint,
                    fn_index,
                    stage: "complete",
                    eta: output.average_duration,
                    queue: false,
                    time: /* @__PURE__ */ new Date()
                  });
                } else {
                  fire_event({
                    type: "status",
                    stage: "error",
                    endpoint: _endpoint,
                    fn_index,
                    message: output.error,
                    queue: false,
                    time: /* @__PURE__ */ new Date()
                  });
                }
              }).catch((e) => {
                fire_event({
                  type: "status",
                  stage: "error",
                  message: e.message,
                  endpoint: _endpoint,
                  fn_index,
                  queue: false,
                  time: /* @__PURE__ */ new Date()
                });
              });
            } else if (protocol == "ws") {
              fire_event({
                type: "status",
                stage: "pending",
                queue: true,
                endpoint: _endpoint,
                fn_index,
                time: /* @__PURE__ */ new Date()
              });
              let url = new URL(`${ws_protocol}://${resolve_root(
                host,
                config.path,
                true
              )}
							/queue/join${url_params ? "?" + url_params : ""}`);
              if (jwt) {
                url.searchParams.set("__sign", jwt);
              }
              websocket = new WebSocket(url);
              websocket.onclose = (evt) => {
                if (!evt.wasClean) {
                  fire_event({
                    type: "status",
                    stage: "error",
                    broken: true,
                    message: BROKEN_CONNECTION_MSG,
                    queue: true,
                    endpoint: _endpoint,
                    fn_index,
                    time: /* @__PURE__ */ new Date()
                  });
                }
              };
              websocket.onmessage = function(event) {
                const _data = JSON.parse(event.data);
                const { type, status, data: data2 } = handle_message(
                  _data,
                  last_status[fn_index]
                );
                if (type === "update" && status && !complete) {
                  fire_event({
                    type: "status",
                    endpoint: _endpoint,
                    fn_index,
                    time: /* @__PURE__ */ new Date(),
                    ...status
                  });
                  if (status.stage === "error") {
                    websocket.close();
                  }
                } else if (type === "hash") {
                  websocket.send(JSON.stringify({ fn_index, session_hash }));
                  return;
                } else if (type === "data") {
                  websocket.send(JSON.stringify({ ...payload, session_hash }));
                } else if (type === "complete") {
                  complete = status;
                } else if (type === "log") {
                  fire_event({
                    type: "log",
                    log: data2.log,
                    level: data2.level,
                    endpoint: _endpoint,
                    fn_index
                  });
                } else if (type === "generating") {
                  fire_event({
                    type: "status",
                    time: /* @__PURE__ */ new Date(),
                    ...status,
                    stage: status == null ? void 0 : status.stage,
                    queue: true,
                    endpoint: _endpoint,
                    fn_index
                  });
                }
                if (data2) {
                  fire_event({
                    type: "data",
                    time: /* @__PURE__ */ new Date(),
                    data: transform_files ? transform_output(
                      data2.data,
                      api_info,
                      config.root,
                      config.root_url
                    ) : data2.data,
                    endpoint: _endpoint,
                    fn_index
                  });
                  if (complete) {
                    fire_event({
                      type: "status",
                      time: /* @__PURE__ */ new Date(),
                      ...complete,
                      stage: status == null ? void 0 : status.stage,
                      queue: true,
                      endpoint: _endpoint,
                      fn_index
                    });
                    websocket.close();
                  }
                }
              };
              if (semiver(config.version || "2.0.0", "3.6") < 0) {
                addEventListener(
                  "open",
                  () => websocket.send(JSON.stringify({ hash: session_hash }))
                );
              }
            } else if (protocol == "sse") {
              fire_event({
                type: "status",
                stage: "pending",
                queue: true,
                endpoint: _endpoint,
                fn_index,
                time: /* @__PURE__ */ new Date()
              });
              var params = new URLSearchParams({
                fn_index: fn_index.toString(),
                session_hash
              }).toString();
              let url = new URL(
                `${config.root}/queue/join?${url_params ? url_params + "&" : ""}${params}`
              );
              eventSource = EventSource_factory(url.toString());
              eventSource.onmessage = async function(event) {
                const _data = JSON.parse(event.data);
                const { type, status, data: data2 } = handle_message(
                  _data,
                  last_status[fn_index]
                );
                if (type === "update" && status && !complete) {
                  fire_event({
                    type: "status",
                    endpoint: _endpoint,
                    fn_index,
                    time: /* @__PURE__ */ new Date(),
                    ...status
                  });
                  if (status.stage === "error") {
                    eventSource.close();
                  }
                } else if (type === "data") {
                  event_id = _data.event_id;
                  let [_, status2] = await post_data2(
                    `${config.root}/queue/data`,
                    {
                      ...payload,
                      session_hash,
                      event_id
                    },
                    hf_token
                  );
                  if (status2 !== 200) {
                    fire_event({
                      type: "status",
                      stage: "error",
                      message: BROKEN_CONNECTION_MSG,
                      queue: true,
                      endpoint: _endpoint,
                      fn_index,
                      time: /* @__PURE__ */ new Date()
                    });
                    eventSource.close();
                  }
                } else if (type === "complete") {
                  complete = status;
                } else if (type === "log") {
                  fire_event({
                    type: "log",
                    log: data2.log,
                    level: data2.level,
                    endpoint: _endpoint,
                    fn_index
                  });
                } else if (type === "generating") {
                  fire_event({
                    type: "status",
                    time: /* @__PURE__ */ new Date(),
                    ...status,
                    stage: status == null ? void 0 : status.stage,
                    queue: true,
                    endpoint: _endpoint,
                    fn_index
                  });
                }
                if (data2) {
                  fire_event({
                    type: "data",
                    time: /* @__PURE__ */ new Date(),
                    data: transform_files ? transform_output(
                      data2.data,
                      api_info,
                      config.root,
                      config.root_url
                    ) : data2.data,
                    endpoint: _endpoint,
                    fn_index
                  });
                  if (complete) {
                    fire_event({
                      type: "status",
                      time: /* @__PURE__ */ new Date(),
                      ...complete,
                      stage: status == null ? void 0 : status.stage,
                      queue: true,
                      endpoint: _endpoint,
                      fn_index
                    });
                    eventSource.close();
                  }
                }
              };
            } else if (protocol == "sse_v1") {
              fire_event({
                type: "status",
                stage: "pending",
                queue: true,
                endpoint: _endpoint,
                fn_index,
                time: /* @__PURE__ */ new Date()
              });
              post_data2(
                `${config.root}/queue/join?${url_params}`,
                {
                  ...payload,
                  session_hash
                },
                hf_token
              ).then(([response, status]) => {
                if (status === 503) {
                  fire_event({
                    type: "status",
                    stage: "error",
                    message: QUEUE_FULL_MSG,
                    queue: true,
                    endpoint: _endpoint,
                    fn_index,
                    time: /* @__PURE__ */ new Date()
                  });
                } else if (status !== 200) {
                  fire_event({
                    type: "status",
                    stage: "error",
                    message: BROKEN_CONNECTION_MSG,
                    queue: true,
                    endpoint: _endpoint,
                    fn_index,
                    time: /* @__PURE__ */ new Date()
                  });
                } else {
                  event_id = response.event_id;
                  let callback = async function(_data) {
                    try {
                      const { type, status: status2, data: data2 } = handle_message(
                        _data,
                        last_status[fn_index]
                      );
                      if (type == "heartbeat") {
                        return;
                      }
                      if (type === "update" && status2 && !complete) {
                        fire_event({
                          type: "status",
                          endpoint: _endpoint,
                          fn_index,
                          time: /* @__PURE__ */ new Date(),
                          ...status2
                        });
                      } else if (type === "complete") {
                        complete = status2;
                      } else if (type == "unexpected_error") {
                        console.error("Unexpected error", status2 == null ? void 0 : status2.message);
                        fire_event({
                          type: "status",
                          stage: "error",
                          message: (status2 == null ? void 0 : status2.message) || "An Unexpected Error Occurred!",
                          queue: true,
                          endpoint: _endpoint,
                          fn_index,
                          time: /* @__PURE__ */ new Date()
                        });
                      } else if (type === "log") {
                        fire_event({
                          type: "log",
                          log: data2.log,
                          level: data2.level,
                          endpoint: _endpoint,
                          fn_index
                        });
                        return;
                      } else if (type === "generating") {
                        fire_event({
                          type: "status",
                          time: /* @__PURE__ */ new Date(),
                          ...status2,
                          stage: status2 == null ? void 0 : status2.stage,
                          queue: true,
                          endpoint: _endpoint,
                          fn_index
                        });
                      }
                      if (data2) {
                        fire_event({
                          type: "data",
                          time: /* @__PURE__ */ new Date(),
                          data: transform_files ? transform_output(
                            data2.data,
                            api_info,
                            config.root,
                            config.root_url
                          ) : data2.data,
                          endpoint: _endpoint,
                          fn_index
                        });
                        if (complete) {
                          fire_event({
                            type: "status",
                            time: /* @__PURE__ */ new Date(),
                            ...complete,
                            stage: status2 == null ? void 0 : status2.stage,
                            queue: true,
                            endpoint: _endpoint,
                            fn_index
                          });
                        }
                      }
                      if ((status2 == null ? void 0 : status2.stage) === "complete" || (status2 == null ? void 0 : status2.stage) === "error") {
                        if (event_callbacks[event_id]) {
                          delete event_callbacks[event_id];
                        }
                      }
                    } catch (e) {
                      console.error("Unexpected client exception", e);
                      fire_event({
                        type: "status",
                        stage: "error",
                        message: "An Unexpected Error Occurred!",
                        queue: true,
                        endpoint: _endpoint,
                        fn_index,
                        time: /* @__PURE__ */ new Date()
                      });
                      close_stream();
                    }
                  };
                  if (event_id in pending_stream_messages) {
                    pending_stream_messages[event_id].forEach(
                      (msg) => callback(msg)
                    );
                    delete pending_stream_messages[event_id];
                  }
                  event_callbacks[event_id] = callback;
                  unclosed_events.add(event_id);
                  if (!stream_open) {
                    open_stream();
                  }
                }
              });
            }
          }
        );
        function fire_event(event) {
          const narrowed_listener_map = listener_map;
          const listeners = narrowed_listener_map[event.type] || [];
          listeners == null ? void 0 : listeners.forEach((l) => l(event));
        }
        function on(eventType, listener) {
          const narrowed_listener_map = listener_map;
          const listeners = narrowed_listener_map[eventType] || [];
          narrowed_listener_map[eventType] = listeners;
          listeners == null ? void 0 : listeners.push(listener);
          return { on, off, cancel, destroy };
        }
        function off(eventType, listener) {
          const narrowed_listener_map = listener_map;
          let listeners = narrowed_listener_map[eventType] || [];
          listeners = listeners == null ? void 0 : listeners.filter((l) => l !== listener);
          narrowed_listener_map[eventType] = listeners;
          return { on, off, cancel, destroy };
        }
        async function cancel() {
          const _status = {
            stage: "complete",
            queue: false,
            time: /* @__PURE__ */ new Date()
          };
          complete = _status;
          fire_event({
            ..._status,
            type: "status",
            endpoint: _endpoint,
            fn_index
          });
          let cancel_request = {};
          if (protocol === "ws") {
            if (websocket && websocket.readyState === 0) {
              websocket.addEventListener("open", () => {
                websocket.close();
              });
            } else {
              websocket.close();
            }
            cancel_request = { fn_index, session_hash };
          } else {
            eventSource.close();
            cancel_request = { event_id };
          }
          try {
            await fetch_implementation(`${config.root}/reset`, {
              headers: { "Content-Type": "application/json" },
              method: "POST",
              body: JSON.stringify(cancel_request)
            });
          } catch (e) {
            console.warn(
              "The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
            );
          }
        }
        function destroy() {
          for (const event_type in listener_map) {
            listener_map[event_type].forEach((fn2) => {
              off(event_type, fn2);
            });
          }
        }
        return {
          on,
          off,
          cancel,
          destroy
        };
      }
      function open_stream() {
        stream_open = true;
        let params = new URLSearchParams({
          session_hash
        }).toString();
        let url = new URL(`${config.root}/queue/data?${params}`);
        event_stream = EventSource_factory(url.toString());
        event_stream.onmessage = async function(event) {
          let _data = JSON.parse(event.data);
          const event_id = _data.event_id;
          if (!event_id) {
            await Promise.all(
              Object.keys(event_callbacks).map(
                (event_id2) => event_callbacks[event_id2](_data)
              )
            );
          } else if (event_callbacks[event_id]) {
            if (_data.msg === "process_completed") {
              unclosed_events.delete(event_id);
              if (unclosed_events.size === 0) {
                close_stream();
              }
            }
            let fn2 = event_callbacks[event_id];
            if (typeof window !== "undefined") {
              window.setTimeout(fn2, 0, _data);
            } else {
              setTimeout(fn2, 0, _data);
            }
          } else {
            if (!pending_stream_messages[event_id]) {
              pending_stream_messages[event_id] = [];
            }
            pending_stream_messages[event_id].push(_data);
          }
        };
        event_stream.onerror = async function(event) {
          await Promise.all(
            Object.keys(event_callbacks).map(
              (event_id) => event_callbacks[event_id]({
                msg: "unexpected_error",
                message: BROKEN_CONNECTION_MSG
              })
            )
          );
          close_stream();
        };
      }
      function close_stream() {
        stream_open = false;
        event_stream == null ? void 0 : event_stream.close();
      }
      async function component_server(component_id, fn_name, data) {
        var _a;
        const headers = { "Content-Type": "application/json" };
        if (hf_token) {
          headers.Authorization = `Bearer ${hf_token}`;
        }
        let root_url;
        let component = config.components.find(
          (comp) => comp.id === component_id
        );
        if ((_a = component == null ? void 0 : component.props) == null ? void 0 : _a.root_url) {
          root_url = component.props.root_url;
        } else {
          root_url = config.root;
        }
        const response = await fetch_implementation(
          `${root_url}/component_server/`,
          {
            method: "POST",
            body: JSON.stringify({
              data,
              component_id,
              fn_name,
              session_hash
            }),
            headers
          }
        );
        if (!response.ok) {
          throw new Error(
            "Could not connect to component server: " + response.statusText
          );
        }
        const output = await response.json();
        return output;
      }
      async function view_api(config2) {
        if (api)
          return api;
        const headers = { "Content-Type": "application/json" };
        if (hf_token) {
          headers.Authorization = `Bearer ${hf_token}`;
        }
        let response;
        if (semiver(config2.version || "2.0.0", "3.30") < 0) {
          response = await fetch_implementation(
            "https://gradio-space-api-fetcher-v2.hf.space/api",
            {
              method: "POST",
              body: JSON.stringify({
                serialize: false,
                config: JSON.stringify(config2)
              }),
              headers
            }
          );
        } else {
          response = await fetch_implementation(`${config2.root}/info`, {
            headers
          });
        }
        if (!response.ok) {
          throw new Error(BROKEN_CONNECTION_MSG);
        }
        let api_info = await response.json();
        if ("api" in api_info) {
          api_info = api_info.api;
        }
        if (api_info.named_endpoints["/predict"] && !api_info.unnamed_endpoints["0"]) {
          api_info.unnamed_endpoints[0] = api_info.named_endpoints["/predict"];
        }
        const x = transform_api_info(api_info, config2, api_map);
        return x;
      }
    });
  }
  async function handle_blob2(endpoint, data, api_info, token) {
    const blob_refs = await walk_and_store_blobs(
      data,
      void 0,
      [],
      true,
      api_info
    );
    return Promise.all(
      blob_refs.map(async ({ path, blob, type }) => {
        if (blob) {
          const file_url = (await upload_files2(endpoint, [blob], token)).files[0];
          return { path, file_url, type, name: blob == null ? void 0 : blob.name };
        }
        return { path, type };
      })
    ).then((r) => {
      r.forEach(({ path, file_url, type, name }) => {
        if (type === "Gallery") {
          update_object(data, file_url, path);
        } else if (file_url) {
          const file = new FileData({ path: file_url, orig_name: name });
          update_object(data, file, path);
        }
      });
      return data;
    });
  }
}
const { post_data, upload_files, client, handle_blob } = api_factory(
  fetch,
  (...args) => new EventSource(...args)
);
function transform_output(data, api_info, root_url, remote_url) {
  return data.map((d, i) => {
    var _a, _b, _c, _d;
    if (((_b = (_a = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _a[i]) == null ? void 0 : _b.component) === "File") {
      return normalise_file(d, root_url, remote_url);
    } else if (((_d = (_c = api_info == null ? void 0 : api_info.returns) == null ? void 0 : _c[i]) == null ? void 0 : _d.component) === "Gallery") {
      return d.map((img) => {
        return Array.isArray(img) ? [normalise_file(img[0], root_url, remote_url), img[1]] : [normalise_file(img, root_url, remote_url), null];
      });
    } else if (typeof d === "object" && d.path) {
      return normalise_file(d, root_url, remote_url);
    }
    return d;
  });
}
function get_type(type, component, serializer, signature_type) {
  switch (type.type) {
    case "string":
      return "string";
    case "boolean":
      return "boolean";
    case "number":
      return "number";
  }
  if (serializer === "JSONSerializable" || serializer === "StringSerializable") {
    return "any";
  } else if (serializer === "ListStringSerializable") {
    return "string[]";
  } else if (component === "Image") {
    return signature_type === "parameter" ? "Blob | File | Buffer" : "string";
  } else if (serializer === "FileSerializable") {
    if ((type == null ? void 0 : type.type) === "array") {
      return signature_type === "parameter" ? "(Blob | File | Buffer)[]" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]`;
    }
    return signature_type === "parameter" ? "Blob | File | Buffer" : `{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}`;
  } else if (serializer === "GallerySerializable") {
    return signature_type === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : `[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]`;
  }
}
function get_description(type, serializer) {
  if (serializer === "GallerySerializable") {
    return "array of [file, label] tuples";
  } else if (serializer === "ListStringSerializable") {
    return "array of strings";
  } else if (serializer === "FileSerializable") {
    return "array of files or single file";
  }
  return type.description;
}
function transform_api_info(api_info, config, api_map) {
  const new_data = {
    named_endpoints: {},
    unnamed_endpoints: {}
  };
  for (const key in api_info) {
    const cat = api_info[key];
    for (const endpoint in cat) {
      const dep_index = config.dependencies[endpoint] ? endpoint : api_map[endpoint.replace("/", "")];
      const info = cat[endpoint];
      new_data[key][endpoint] = {};
      new_data[key][endpoint].parameters = {};
      new_data[key][endpoint].returns = {};
      new_data[key][endpoint].type = config.dependencies[dep_index].types;
      new_data[key][endpoint].parameters = info.parameters.map(
        ({ label, component, type, serializer }) => ({
          label,
          component,
          type: get_type(type, component, serializer, "parameter"),
          description: get_description(type, serializer)
        })
      );
      new_data[key][endpoint].returns = info.returns.map(
        ({ label, component, type, serializer }) => ({
          label,
          component,
          type: get_type(type, component, serializer, "return"),
          description: get_description(type, serializer)
        })
      );
    }
  }
  return new_data;
}
async function get_jwt(space, token) {
  try {
    const r = await fetch(`https://huggingface.co/api/spaces/${space}/jwt`, {
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    const jwt = (await r.json()).token;
    return jwt || false;
  } catch (e) {
    console.error(e);
    return false;
  }
}
function update_object(object, newValue, stack) {
  while (stack.length > 1) {
    object = object[stack.shift()];
  }
  object[stack.shift()] = newValue;
}
async function walk_and_store_blobs(param, type = void 0, path = [], root = false, api_info = void 0) {
  if (Array.isArray(param)) {
    let blob_refs = [];
    await Promise.all(
      param.map(async (v, i) => {
        var _a;
        let new_path = path.slice();
        new_path.push(i);
        const array_refs = await walk_and_store_blobs(
          param[i],
          root ? ((_a = api_info == null ? void 0 : api_info.parameters[i]) == null ? void 0 : _a.component) || void 0 : type,
          new_path,
          false,
          api_info
        );
        blob_refs = blob_refs.concat(array_refs);
      })
    );
    return blob_refs;
  } else if (globalThis.Buffer && param instanceof globalThis.Buffer) {
    const is_image = type === "Image";
    return [
      {
        path,
        blob: is_image ? false : new NodeBlob([param]),
        type
      }
    ];
  } else if (typeof param === "object") {
    let blob_refs = [];
    for (let key in param) {
      if (param.hasOwnProperty(key)) {
        let new_path = path.slice();
        new_path.push(key);
        blob_refs = blob_refs.concat(
          await walk_and_store_blobs(
            param[key],
            void 0,
            new_path,
            false,
            api_info
          )
        );
      }
    }
    return blob_refs;
  }
  return [];
}
function skip_queue(id, config) {
  var _a, _b, _c, _d;
  return !(((_b = (_a = config == null ? void 0 : config.dependencies) == null ? void 0 : _a[id]) == null ? void 0 : _b.queue) === null ? config.enable_queue : (_d = (_c = config == null ? void 0 : config.dependencies) == null ? void 0 : _c[id]) == null ? void 0 : _d.queue) || false;
}
async function resolve_config(fetch_implementation, endpoint, token) {
  const headers = {};
  if (token) {
    headers.Authorization = `Bearer ${token}`;
  }
  if (typeof window !== "undefined" && window.gradio_config && location.origin !== "http://localhost:9876" && !window.gradio_config.dev_mode) {
    const path = window.gradio_config.root;
    const config = window.gradio_config;
    config.root = resolve_root(endpoint, config.root, false);
    return { ...config, path };
  } else if (endpoint) {
    let response = await fetch_implementation(`${endpoint}/config`, {
      headers
    });
    if (response.status === 200) {
      const config = await response.json();
      config.path = config.path ?? "";
      config.root = endpoint;
      return config;
    }
    throw new Error("Could not get config.");
  }
  throw new Error("No config or app endpoint found");
}
async function check_space_status(id, type, status_callback) {
  let endpoint = type === "subdomain" ? `https://huggingface.co/api/spaces/by-subdomain/${id}` : `https://huggingface.co/api/spaces/${id}`;
  let response;
  let _status;
  try {
    response = await fetch(endpoint);
    _status = response.status;
    if (_status !== 200) {
      throw new Error();
    }
    response = await response.json();
  } catch (e) {
    status_callback({
      status: "error",
      load_status: "error",
      message: "Could not get space status",
      detail: "NOT_FOUND"
    });
    return;
  }
  if (!response || _status !== 200)
    return;
  const {
    runtime: { stage },
    id: space_name
  } = response;
  switch (stage) {
    case "STOPPED":
    case "SLEEPING":
      status_callback({
        status: "sleeping",
        load_status: "pending",
        message: "Space is asleep. Waking it up...",
        detail: stage
      });
      setTimeout(() => {
        check_space_status(id, type, status_callback);
      }, 1e3);
      break;
    case "PAUSED":
      status_callback({
        status: "paused",
        load_status: "error",
        message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.",
        detail: stage,
        discussions_enabled: await discussions_enabled(space_name)
      });
      break;
    case "RUNNING":
    case "RUNNING_BUILDING":
      status_callback({
        status: "running",
        load_status: "complete",
        message: "",
        detail: stage
      });
      break;
    case "BUILDING":
      status_callback({
        status: "building",
        load_status: "pending",
        message: "Space is building...",
        detail: stage
      });
      setTimeout(() => {
        check_space_status(id, type, status_callback);
      }, 1e3);
      break;
    default:
      status_callback({
        status: "space_error",
        load_status: "error",
        message: "This space is experiencing an issue.",
        detail: stage,
        discussions_enabled: await discussions_enabled(space_name)
      });
      break;
  }
}
function handle_message(data, last_status) {
  const queue = true;
  switch (data.msg) {
    case "send_data":
      return { type: "data" };
    case "send_hash":
      return { type: "hash" };
    case "queue_full":
      return {
        type: "update",
        status: {
          queue,
          message: QUEUE_FULL_MSG,
          stage: "error",
          code: data.code,
          success: data.success
        }
      };
    case "heartbeat":
      return {
        type: "heartbeat"
      };
    case "unexpected_error":
      return {
        type: "unexpected_error",
        status: {
          queue,
          message: data.message,
          stage: "error",
          success: false
        }
      };
    case "estimation":
      return {
        type: "update",
        status: {
          queue,
          stage: last_status || "pending",
          code: data.code,
          size: data.queue_size,
          position: data.rank,
          eta: data.rank_eta,
          success: data.success
        }
      };
    case "progress":
      return {
        type: "update",
        status: {
          queue,
          stage: "pending",
          code: data.code,
          progress_data: data.progress_data,
          success: data.success
        }
      };
    case "log":
      return { type: "log", data };
    case "process_generating":
      return {
        type: "generating",
        status: {
          queue,
          message: !data.success ? data.output.error : null,
          stage: data.success ? "generating" : "error",
          code: data.code,
          progress_data: data.progress_data,
          eta: data.average_duration
        },
        data: data.success ? data.output : null
      };
    case "process_completed":
      if ("error" in data.output) {
        return {
          type: "update",
          status: {
            queue,
            message: data.output.error,
            stage: "error",
            code: data.code,
            success: data.success
          }
        };
      }
      return {
        type: "complete",
        status: {
          queue,
          message: !data.success ? data.output.error : void 0,
          stage: data.success ? "complete" : "error",
          code: data.code,
          progress_data: data.progress_data
        },
        data: data.success ? data.output : null
      };
    case "process_starts":
      return {
        type: "update",
        status: {
          queue,
          stage: "pending",
          code: data.code,
          size: data.rank,
          position: 0,
          success: data.success,
          eta: data.eta
        }
      };
  }
  return { type: "none", status: { stage: "error", queue } };
}
export {
  FileData,
  api_factory,
  client,
  duplicate,
  get_fetchable_url_or_file,
  normalise_file,
  post_data,
  prepare_files,
  upload,
  upload_files
};