pinokiod 1.2.38 → 1.2.39

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