@gradio/client 2.3.0 → 2.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -0
- package/README.md +4 -2
- package/dist/__vite-browser-external-Cgmn0awE.js +7 -0
- package/dist/browser.js +1672 -1902
- package/dist/client.d.ts +5 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +4 -0
- package/dist/constants.d.ts.map +1 -1
- package/dist/helpers/api_info.d.ts.map +1 -1
- package/dist/helpers/data.d.ts.map +1 -1
- package/dist/helpers/init_helpers.d.ts +12 -3
- package/dist/helpers/init_helpers.d.ts.map +1 -1
- package/dist/index.cjs +2274 -0
- package/dist/index.js +2155 -2449
- package/dist/index.min.js +1735 -0
- package/dist/types.d.ts +16 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/duplicate.d.ts.map +1 -1
- package/dist/utils/predict.d.ts.map +1 -1
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/submit.d.ts.map +1 -1
- package/dist/utils/view_api.d.ts.map +1 -1
- package/package.json +11 -4
- package/scripts/verify_dist.mjs +46 -0
- package/src/client.ts +46 -7
- package/src/constants.ts +10 -0
- package/src/helpers/api_info.ts +47 -19
- package/src/helpers/data.ts +8 -3
- package/src/helpers/init_helpers.ts +61 -14
- package/src/test/api_info.test.ts +105 -12
- package/src/test/data.test.ts +26 -3
- package/src/test/init.test.ts +21 -3
- package/src/test/init_helpers.test.ts +94 -1
- package/src/test/post_data.test.ts +21 -0
- package/src/test/refresh.test.ts +31 -0
- package/src/test/submit.test.ts +14 -0
- package/src/types.ts +16 -0
- package/src/utils/duplicate.ts +5 -1
- package/src/utils/post_data.ts +1 -1
- package/src/utils/predict.ts +34 -19
- package/src/utils/stream.ts +17 -4
- package/src/utils/submit.ts +43 -10
- package/src/utils/upload_files.ts +1 -1
- package/src/utils/view_api.ts +2 -3
- package/vite.config.ts +20 -4
- package/dist/__vite-browser-external-DYxpcVy9.js +0 -4
package/dist/browser.js
CHANGED
|
@@ -1,1960 +1,1730 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescriptor, r = Object.getOwnPropertyNames, i = Object.getPrototypeOf, a = Object.prototype.hasOwnProperty, o = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t.exports), s = (e, i, o, s) => {
|
|
3
|
+
if (i && typeof i == "object" || typeof i == "function") for (var c = r(i), l = 0, u = c.length, d; l < u; l++) d = c[l], !a.call(e, d) && d !== o && t(e, d, {
|
|
4
|
+
get: ((e) => i[e]).bind(null, d),
|
|
5
|
+
enumerable: !(s = n(i, d)) || s.enumerable
|
|
6
|
+
});
|
|
7
|
+
return e;
|
|
8
|
+
}, c = (n, r, a) => (a = n == null ? {} : e(i(n)), s(r || !n || !n.__esModule ? t(a, "default", {
|
|
9
|
+
value: n,
|
|
10
|
+
enumerable: !0
|
|
11
|
+
}) : a, n)), l = "host", u = "queue/data", d = "queue/join", f = "upload", p = "login", m = "config", h = "info", g = "runtime", _ = "sleeptime", v = "heartbeat", ee = "component_server", te = "reset", ne = "cancel", y = "app_id", re = "This application is currently busy. Please try again. ", b = "Connection errored out. ", x = "Could not resolve app config. ", S = "Could not get space status. ", C = "Could not get API info. ", w = "Space metadata could not be loaded. ", T = "Invalid URL. A full URL path is required.", E = "Not authorized to access this space. ", D = "Invalid credentials. Could not login. ", ie = "Login credentials are required to access this space.", O = "Could not access this app (received a 401 response). If it is a private Hugging Face Space, pass a valid Hugging Face token to the `token` option of `Client.connect`. You can generate a token at https://huggingface.co/settings/tokens.", k = (e, t) => `Space "${e}" could not be accessed (received a ${t} response from the Hugging Face API). Check that the Space name is spelled correctly and that the Space exists. If the Space is private, pass a valid Hugging Face token to the \`token\` option of \`Client.connect\`. You can generate a token at https://huggingface.co/settings/tokens.`, ae = "No API information is available for this app. This can happen when the app's `/info` endpoint cannot be reached, or when the app is running a legacy version of Gradio that is not supported by this client. ", oe = "This app appears to be running a legacy version of Gradio (3.x or earlier) that communicates over WebSockets, which is not supported by this version of @gradio/client. Please upgrade the app to a newer version of Gradio, or connect to it with @gradio/client version 0.x.", A = "File system access is only available in Node.js environments", j = "Root URL not found in client config", M = "Error uploading file";
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/helpers/init_helpers.ts
|
|
14
|
+
async function N(e, t, n) {
|
|
15
|
+
try {
|
|
16
|
+
return (await (await fetch(`https://huggingface.co/api/spaces/${e}/jwt`, { headers: {
|
|
17
|
+
Authorization: `Bearer ${t}`,
|
|
18
|
+
...n ? { Cookie: n } : {}
|
|
19
|
+
} })).json()).token || !1;
|
|
20
|
+
} catch {
|
|
21
|
+
return !1;
|
|
22
|
+
}
|
|
13
23
|
}
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
return e.forEach(({ api_name: t, id: n }) => {
|
|
17
|
-
t && (s[t] = n);
|
|
18
|
-
}), s;
|
|
24
|
+
function P(e) {
|
|
25
|
+
e.hf_token && !e.token && (e.token = e.hf_token, console.warn("The `hf_token` option has been renamed to `token`. Support for `hf_token` will be removed in a future version of @gradio/client."));
|
|
19
26
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
e,
|
|
26
|
-
this.deep_link ? K + "?deep_link=" + this.deep_link : K
|
|
27
|
-
);
|
|
28
|
-
const n = await this.fetch(t, {
|
|
29
|
-
headers: s,
|
|
30
|
-
credentials: "include"
|
|
31
|
-
}), o = await ve(n, !!this.options.auth);
|
|
32
|
-
o.root = e || o.root, window.gradio_config = {
|
|
33
|
-
...o,
|
|
34
|
-
current_page: window.gradio_config.current_page
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
return { ...window.gradio_config };
|
|
38
|
-
} else if (e) {
|
|
39
|
-
let t = ae(
|
|
40
|
-
e,
|
|
41
|
-
this.deep_link ? K + "?deep_link=" + this.deep_link : K
|
|
42
|
-
);
|
|
43
|
-
const n = await this.fetch(t, {
|
|
44
|
-
headers: s,
|
|
45
|
-
credentials: "include"
|
|
46
|
-
}), o = await ve(n, !!this.options.auth);
|
|
47
|
-
return o.root || (o.root = e), o;
|
|
48
|
-
}
|
|
49
|
-
throw new Error(C);
|
|
27
|
+
function F(e) {
|
|
28
|
+
let t = {};
|
|
29
|
+
return e.forEach(({ api_name: e, id: n }) => {
|
|
30
|
+
e && (t[e] = n);
|
|
31
|
+
}), t;
|
|
50
32
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
throw new Error(n || lt);
|
|
55
|
-
} else if (e?.status === 401 && s)
|
|
56
|
-
throw new Error(Ce);
|
|
57
|
-
if (e?.status === 200) {
|
|
58
|
-
let t = await e.json();
|
|
59
|
-
return t.dependencies?.forEach((n, o) => {
|
|
60
|
-
n.id === void 0 && (n.id = o);
|
|
61
|
-
}), t;
|
|
62
|
-
} else if (e?.status === 401)
|
|
63
|
-
throw new Error(ut);
|
|
64
|
-
throw new Error(C);
|
|
33
|
+
function I(e, t) {
|
|
34
|
+
let n = new URL(e, t), r = new URL(t);
|
|
35
|
+
return n.hostname === r.hostname ? (n.protocol = r.protocol, n.host = r.host, n.toString().replace(/\/$/, "")) : e;
|
|
65
36
|
}
|
|
66
|
-
async function
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
37
|
+
async function se(e, t = !0) {
|
|
38
|
+
let n = this.options.token ? { Authorization: `Bearer ${this.options.token}` } : {};
|
|
39
|
+
if (typeof window < "u" && window.gradio_config && location.origin !== "http://localhost:9876") {
|
|
40
|
+
if (t && window.gradio_config.current_page && (e = e.substring(0, e.lastIndexOf("/"))), window.gradio_config.dev_mode || typeof window < "u" && window?.BUILD_MODE === "dev") {
|
|
41
|
+
let t = W(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await L(await this.fetch(t, {
|
|
42
|
+
headers: n,
|
|
43
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
44
|
+
}), !!this.options.auth);
|
|
45
|
+
r.root = e || r.root, window.gradio_config = {
|
|
46
|
+
...r,
|
|
47
|
+
current_page: window.gradio_config.current_page
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
let r = { ...window.gradio_config };
|
|
51
|
+
return r.root = I(r.root, location.href), r;
|
|
52
|
+
} else if (e) {
|
|
53
|
+
let t = W(e, this.deep_link ? m + "?deep_link=" + this.deep_link : m), r = await L(await this.fetch(t, {
|
|
54
|
+
headers: n,
|
|
55
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
56
|
+
}), !!this.options.auth);
|
|
57
|
+
return r.root ||= e, r;
|
|
58
|
+
}
|
|
59
|
+
throw Error(x);
|
|
85
60
|
}
|
|
86
|
-
async function
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
61
|
+
async function L(e, t) {
|
|
62
|
+
if (e?.status === 401 && !t) {
|
|
63
|
+
let t = null;
|
|
64
|
+
try {
|
|
65
|
+
t = await e.json();
|
|
66
|
+
} catch {
|
|
67
|
+
throw Error(O);
|
|
68
|
+
}
|
|
69
|
+
let n = t?.detail?.auth_message;
|
|
70
|
+
throw Error(n || "Login credentials are required to access this space.");
|
|
71
|
+
} else if (e?.status === 401 && t) throw Error(D);
|
|
72
|
+
if (e?.status === 200) {
|
|
73
|
+
let t = await e.json();
|
|
74
|
+
return t.dependencies?.forEach((e, t) => {
|
|
75
|
+
e.id === void 0 && (e.id = t);
|
|
76
|
+
}), t;
|
|
77
|
+
} else if (e?.status === 401) throw Error(E);
|
|
78
|
+
throw Error(`${x}(received status ${e?.status} when fetching the app config)`);
|
|
100
79
|
}
|
|
101
|
-
function
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
ws_protocol: "wss",
|
|
112
|
-
http_protocol: "https:",
|
|
113
|
-
host: new URL(e).host
|
|
114
|
-
};
|
|
80
|
+
async function ce() {
|
|
81
|
+
let { http_protocol: e, host: t } = await U(this.app_reference, this.options.token);
|
|
82
|
+
try {
|
|
83
|
+
if (this.options.auth) {
|
|
84
|
+
let n = await R(e, t, this.options.auth, this.fetch, this.options.token, this.options.credentials);
|
|
85
|
+
n && this.set_cookies(n);
|
|
86
|
+
}
|
|
87
|
+
} catch (e) {
|
|
88
|
+
throw Error(e.message);
|
|
89
|
+
}
|
|
115
90
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const r = (await (await fetch(
|
|
130
|
-
`https://huggingface.co/api/spaces/${n}/${Ke}`,
|
|
131
|
-
{ headers: t }
|
|
132
|
-
)).json()).host;
|
|
133
|
-
return {
|
|
134
|
-
space_id: e,
|
|
135
|
-
...re(r)
|
|
136
|
-
};
|
|
137
|
-
} catch {
|
|
138
|
-
throw new Error(ue);
|
|
139
|
-
}
|
|
140
|
-
if (gt.test(n)) {
|
|
141
|
-
const { ws_protocol: o, http_protocol: r, host: i } = re(n);
|
|
142
|
-
return {
|
|
143
|
-
space_id: i.split("/")[0].replace(".hf.space", ""),
|
|
144
|
-
ws_protocol: o,
|
|
145
|
-
http_protocol: r,
|
|
146
|
-
host: i
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
return {
|
|
150
|
-
space_id: !1,
|
|
151
|
-
...re(n)
|
|
152
|
-
};
|
|
91
|
+
async function R(e, t, n, r, i, a) {
|
|
92
|
+
let o = new FormData();
|
|
93
|
+
o.append("username", n?.[0]), o.append("password", n?.[1]);
|
|
94
|
+
let s = {};
|
|
95
|
+
i && (s.Authorization = `Bearer ${i}`);
|
|
96
|
+
let c = await r(`${e}//${t}/${p}`, {
|
|
97
|
+
headers: s,
|
|
98
|
+
method: "POST",
|
|
99
|
+
body: o,
|
|
100
|
+
credentials: a ?? "same-origin"
|
|
101
|
+
});
|
|
102
|
+
if (c.status === 200) return c.headers.get("set-cookie");
|
|
103
|
+
throw c.status === 401 ? Error(D) : Error(w);
|
|
153
104
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
([r, { parameters: i, returns: a }]) => {
|
|
169
|
-
const u = s.dependencies.find(
|
|
170
|
-
(c) => c.api_name === r || c.api_name === r.replace("/", "")
|
|
171
|
-
)?.id || t[r.replace("/", "")] || -1, l = u !== -1 ? s.dependencies.find((c) => c.id == u)?.types : { generator: !1, cancel: !1 };
|
|
172
|
-
if (u !== -1 && s.dependencies.find((c) => c.id == u)?.inputs?.length !== i.length) {
|
|
173
|
-
const c = s.dependencies.find((g) => g.id == u).inputs.map(
|
|
174
|
-
(g) => s.components.find((w) => w.id === g)?.type
|
|
175
|
-
);
|
|
176
|
-
try {
|
|
177
|
-
c.forEach((g, w) => {
|
|
178
|
-
if (g === "state") {
|
|
179
|
-
const N = {
|
|
180
|
-
component: "state",
|
|
181
|
-
example: null,
|
|
182
|
-
parameter_default: null,
|
|
183
|
-
parameter_has_default: !0,
|
|
184
|
-
parameter_name: null,
|
|
185
|
-
hidden: !0
|
|
186
|
-
};
|
|
187
|
-
i.splice(w, 0, N);
|
|
188
|
-
}
|
|
189
|
-
});
|
|
190
|
-
} catch (g) {
|
|
191
|
-
console.error(g);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
const p = (c, g, w, N) => ({
|
|
195
|
-
...c,
|
|
196
|
-
description: yt(c?.type, w),
|
|
197
|
-
type: wt(c?.type, g, w, N) || ""
|
|
198
|
-
});
|
|
199
|
-
n[o][r] = {
|
|
200
|
-
parameters: i.map(
|
|
201
|
-
(c) => p(c, c?.component, c?.serializer, "parameter")
|
|
202
|
-
),
|
|
203
|
-
returns: a.map(
|
|
204
|
-
(c) => p(c, c?.component, c?.serializer, "return")
|
|
205
|
-
),
|
|
206
|
-
type: l
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
));
|
|
210
|
-
}), n;
|
|
105
|
+
function z(e) {
|
|
106
|
+
if (e.startsWith("http")) {
|
|
107
|
+
let { protocol: t, host: n, pathname: r } = new URL(e);
|
|
108
|
+
return {
|
|
109
|
+
ws_protocol: t === "https:" ? "wss" : "ws",
|
|
110
|
+
http_protocol: t,
|
|
111
|
+
host: n + (r === "/" ? "" : r)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
ws_protocol: "wss",
|
|
116
|
+
http_protocol: "https:",
|
|
117
|
+
host: new URL(e).host
|
|
118
|
+
};
|
|
211
119
|
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
120
|
+
var B = (e) => {
|
|
121
|
+
let t = [];
|
|
122
|
+
return e.split(/,(?=\s*[^\s=;]+=[^\s=;]+)/).forEach((e) => {
|
|
123
|
+
let [n, r] = e.split(";")[0].split("=");
|
|
124
|
+
n && r && t.push(`${n.trim()}=${r.trim()}`);
|
|
125
|
+
}), t;
|
|
126
|
+
}, V = /^[a-zA-Z0-9_\-\.]+\/[a-zA-Z0-9_\-\.]+$/, H = /.*hf\.space\/{0,1}.*$/;
|
|
127
|
+
async function U(e, t) {
|
|
128
|
+
let n = {};
|
|
129
|
+
t && (n.Authorization = `Bearer ${t}`);
|
|
130
|
+
let r = e.trim().replace(/\/$/, "");
|
|
131
|
+
if (V.test(r)) {
|
|
132
|
+
let e;
|
|
133
|
+
try {
|
|
134
|
+
e = await fetch(`https://huggingface.co/api/spaces/${r}/${l}`, { headers: n });
|
|
135
|
+
} catch {
|
|
136
|
+
throw Error(w);
|
|
137
|
+
}
|
|
138
|
+
if (e.status === 401 || e.status === 404) throw Error(k(r, e.status));
|
|
139
|
+
let t;
|
|
140
|
+
try {
|
|
141
|
+
t = (await e.json()).host;
|
|
142
|
+
} catch {
|
|
143
|
+
throw Error(w);
|
|
144
|
+
}
|
|
145
|
+
if (!t) throw Error(w);
|
|
146
|
+
return {
|
|
147
|
+
space_id: r,
|
|
148
|
+
...z(t)
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
if (H.test(r)) {
|
|
152
|
+
let { ws_protocol: e, http_protocol: t, host: n } = z(r);
|
|
153
|
+
return {
|
|
154
|
+
space_id: n.split("/")[0].replace(".hf.space", ""),
|
|
155
|
+
ws_protocol: e,
|
|
156
|
+
http_protocol: t,
|
|
157
|
+
host: n
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
space_id: !1,
|
|
162
|
+
...z(r)
|
|
163
|
+
};
|
|
232
164
|
}
|
|
233
|
-
|
|
234
|
-
|
|
165
|
+
var W = (...e) => {
|
|
166
|
+
try {
|
|
167
|
+
return e.reduce((e, t) => (e = e.replace(/\/+$/, ""), t = t.replace(/^\/+/, ""), new URL(t, e + "/").toString()));
|
|
168
|
+
} catch {
|
|
169
|
+
throw Error(T);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
function G(e, t, n) {
|
|
173
|
+
let r = {
|
|
174
|
+
named_endpoints: {},
|
|
175
|
+
unnamed_endpoints: {}
|
|
176
|
+
};
|
|
177
|
+
return Object.keys(e).forEach((i) => {
|
|
178
|
+
(i === "named_endpoints" || i === "unnamed_endpoints") && (r[i] = {}, Object.entries(e[i]).forEach(([e, a]) => {
|
|
179
|
+
let o = a?.parameters ?? [], s = a?.returns ?? [], c = t.dependencies.find((t) => t.api_name === e || t.api_name === e.replace("/", ""))?.id || n[e.replace("/", "")] || -1, l = c === -1 ? void 0 : t.dependencies.find((e) => e.id == c), u = c === -1 ? {
|
|
180
|
+
generator: !1,
|
|
181
|
+
cancel: !1
|
|
182
|
+
} : l?.types;
|
|
183
|
+
if (l && Array.isArray(l.inputs) && l.inputs.length !== o.length) {
|
|
184
|
+
let e = l.inputs.map((e) => t.components.find((t) => t.id === e)?.type);
|
|
185
|
+
try {
|
|
186
|
+
e.forEach((e, t) => {
|
|
187
|
+
e === "state" && o.splice(t, 0, {
|
|
188
|
+
component: "state",
|
|
189
|
+
example: null,
|
|
190
|
+
parameter_default: null,
|
|
191
|
+
parameter_has_default: !0,
|
|
192
|
+
parameter_name: null,
|
|
193
|
+
hidden: !0
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
} catch (e) {
|
|
197
|
+
console.error(e);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
let d = (e, t, n, r) => ({
|
|
201
|
+
...e,
|
|
202
|
+
description: ue(e?.type, n),
|
|
203
|
+
type: le(e?.type, t, n, r) || ""
|
|
204
|
+
});
|
|
205
|
+
r[i][e] = {
|
|
206
|
+
parameters: o.map((e) => d(e, e?.component, e?.serializer, "parameter")),
|
|
207
|
+
returns: s.map((e) => d(e, e?.component, e?.serializer, "return")),
|
|
208
|
+
type: u,
|
|
209
|
+
...a?.oauth_token ? { oauth_token: a.oauth_token } : {}
|
|
210
|
+
};
|
|
211
|
+
}));
|
|
212
|
+
}), r;
|
|
235
213
|
}
|
|
236
|
-
function
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
stage: "error",
|
|
249
|
-
code: e.code,
|
|
250
|
-
success: e.success
|
|
251
|
-
}
|
|
252
|
-
};
|
|
253
|
-
case "heartbeat":
|
|
254
|
-
return {
|
|
255
|
-
type: "heartbeat"
|
|
256
|
-
};
|
|
257
|
-
case "unexpected_error":
|
|
258
|
-
return {
|
|
259
|
-
type: "unexpected_error",
|
|
260
|
-
status: {
|
|
261
|
-
queue: !0,
|
|
262
|
-
message: e.message,
|
|
263
|
-
session_not_found: e.session_not_found,
|
|
264
|
-
stage: "error",
|
|
265
|
-
success: !1
|
|
266
|
-
}
|
|
267
|
-
};
|
|
268
|
-
case "broken_connection":
|
|
269
|
-
return {
|
|
270
|
-
type: "broken_connection",
|
|
271
|
-
status: {
|
|
272
|
-
queue: !0,
|
|
273
|
-
message: e.message,
|
|
274
|
-
stage: "error",
|
|
275
|
-
success: !1
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
case "estimation":
|
|
279
|
-
return {
|
|
280
|
-
type: "update",
|
|
281
|
-
status: {
|
|
282
|
-
queue: !0,
|
|
283
|
-
stage: s || "pending",
|
|
284
|
-
code: e.code,
|
|
285
|
-
size: e.queue_size,
|
|
286
|
-
position: e.rank,
|
|
287
|
-
eta: e.rank_eta,
|
|
288
|
-
success: e.success
|
|
289
|
-
}
|
|
290
|
-
};
|
|
291
|
-
case "progress":
|
|
292
|
-
return {
|
|
293
|
-
type: "update",
|
|
294
|
-
status: {
|
|
295
|
-
queue: !0,
|
|
296
|
-
stage: "pending",
|
|
297
|
-
code: e.code,
|
|
298
|
-
progress_data: e.progress_data,
|
|
299
|
-
success: e.success
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
case "log":
|
|
303
|
-
return { type: "log", data: e };
|
|
304
|
-
case "process_generating":
|
|
305
|
-
return {
|
|
306
|
-
type: "generating",
|
|
307
|
-
status: {
|
|
308
|
-
queue: !0,
|
|
309
|
-
message: e.success ? null : e.output.error,
|
|
310
|
-
stage: e.success ? "generating" : "error",
|
|
311
|
-
code: e.code,
|
|
312
|
-
progress_data: e.progress_data,
|
|
313
|
-
eta: e.average_duration,
|
|
314
|
-
changed_state_ids: e.success ? e.output.changed_state_ids : void 0
|
|
315
|
-
},
|
|
316
|
-
data: e.success ? e.output : null
|
|
317
|
-
};
|
|
318
|
-
case "process_streaming":
|
|
319
|
-
return {
|
|
320
|
-
type: "streaming",
|
|
321
|
-
status: {
|
|
322
|
-
queue: !0,
|
|
323
|
-
message: e.output.error,
|
|
324
|
-
stage: "streaming",
|
|
325
|
-
time_limit: e.time_limit,
|
|
326
|
-
code: e.code,
|
|
327
|
-
progress_data: e.progress_data,
|
|
328
|
-
eta: e.eta
|
|
329
|
-
},
|
|
330
|
-
data: e.output
|
|
331
|
-
};
|
|
332
|
-
case "process_completed":
|
|
333
|
-
return "error" in e.output ? {
|
|
334
|
-
type: "update",
|
|
335
|
-
status: {
|
|
336
|
-
queue: !0,
|
|
337
|
-
title: e.output.title ?? "Error",
|
|
338
|
-
message: e.output.error ?? "An error occurred",
|
|
339
|
-
visible: e.output.visible,
|
|
340
|
-
duration: e.output.duration,
|
|
341
|
-
stage: "error",
|
|
342
|
-
code: e.code,
|
|
343
|
-
success: e.success
|
|
344
|
-
}
|
|
345
|
-
} : {
|
|
346
|
-
type: "complete",
|
|
347
|
-
status: {
|
|
348
|
-
queue: !0,
|
|
349
|
-
message: e.success ? void 0 : e.output.error,
|
|
350
|
-
stage: e.success ? "complete" : "error",
|
|
351
|
-
code: e.code,
|
|
352
|
-
progress_data: e.progress_data,
|
|
353
|
-
changed_state_ids: e.success ? e.output.changed_state_ids : void 0,
|
|
354
|
-
used_cache: e.used_cache,
|
|
355
|
-
cache_duration: e.cache_duration,
|
|
356
|
-
avg_time: e.avg_time
|
|
357
|
-
},
|
|
358
|
-
data: e.success ? e.output : null
|
|
359
|
-
};
|
|
360
|
-
case "process_starts":
|
|
361
|
-
return {
|
|
362
|
-
type: "update",
|
|
363
|
-
status: {
|
|
364
|
-
queue: !0,
|
|
365
|
-
stage: "pending",
|
|
366
|
-
code: e.code,
|
|
367
|
-
size: e.rank,
|
|
368
|
-
position: 0,
|
|
369
|
-
success: e.success,
|
|
370
|
-
eta: e.eta
|
|
371
|
-
},
|
|
372
|
-
original_msg: "process_starts"
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
return { type: "none", status: { stage: "error", queue: !0 } };
|
|
214
|
+
function le(e, t, n, r) {
|
|
215
|
+
if (t === "Api") return e.type;
|
|
216
|
+
switch (e?.type) {
|
|
217
|
+
case "string": return "string";
|
|
218
|
+
case "boolean": return "boolean";
|
|
219
|
+
case "number": return "number";
|
|
220
|
+
}
|
|
221
|
+
if (n === "JSONSerializable" || n === "StringSerializable") return "any";
|
|
222
|
+
if (n === "ListStringSerializable") return "string[]";
|
|
223
|
+
if (t === "Image") return r === "parameter" ? "Blob | File | Buffer" : "string";
|
|
224
|
+
if (n === "FileSerializable") return e?.type === "array" ? r === "parameter" ? "(Blob | File | Buffer)[]" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}[]" : r === "parameter" ? "Blob | File | Buffer" : "{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}";
|
|
225
|
+
if (n === "GallerySerializable") return r === "parameter" ? "[(Blob | File | Buffer), (string | null)][]" : "[{ name: string; data: string; size?: number; is_file?: boolean; orig_name?: string}, (string | null))][]";
|
|
376
226
|
}
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
if (Array.isArray(e))
|
|
380
|
-
return s && t.length > 0 && e.length > t.length && console.warn("Too many arguments provided for the endpoint."), e;
|
|
381
|
-
const n = [], o = Object.keys(e);
|
|
382
|
-
return t.forEach((r, i) => {
|
|
383
|
-
if (e.hasOwnProperty(r.parameter_name))
|
|
384
|
-
n[i] = e[r.parameter_name];
|
|
385
|
-
else if (r.parameter_has_default)
|
|
386
|
-
n[i] = r.parameter_default;
|
|
387
|
-
else
|
|
388
|
-
throw new Error(
|
|
389
|
-
`No value provided for required parameter: ${r.parameter_name}`
|
|
390
|
-
);
|
|
391
|
-
}), o.forEach((r) => {
|
|
392
|
-
if (!t.some((i) => i.parameter_name === r))
|
|
393
|
-
throw new Error(
|
|
394
|
-
`Parameter \`${r}\` is not a valid keyword argument. Please refer to the API for usage.`
|
|
395
|
-
);
|
|
396
|
-
}), n.forEach((r, i) => {
|
|
397
|
-
if (r === void 0 && !t[i].parameter_has_default)
|
|
398
|
-
throw new Error(
|
|
399
|
-
`No value provided for required parameter: ${t[i].parameter_name}`
|
|
400
|
-
);
|
|
401
|
-
}), n;
|
|
402
|
-
};
|
|
403
|
-
async function vt() {
|
|
404
|
-
if (this.api_info) return this.api_info;
|
|
405
|
-
const { token: e } = this.options, { config: s } = this, t = { "Content-Type": "application/json" };
|
|
406
|
-
if (e && (t.Authorization = `Bearer ${e}`), !!s)
|
|
407
|
-
try {
|
|
408
|
-
let n, o;
|
|
409
|
-
if (typeof window < "u" && window.gradio_api_info)
|
|
410
|
-
o = window.gradio_api_info;
|
|
411
|
-
else {
|
|
412
|
-
const r = ae(s.root, this.api_prefix, Qe);
|
|
413
|
-
if (n = await this.fetch(r, {
|
|
414
|
-
headers: t,
|
|
415
|
-
credentials: "include"
|
|
416
|
-
}), !n.ok)
|
|
417
|
-
throw new Error(L);
|
|
418
|
-
o = await n.json();
|
|
419
|
-
}
|
|
420
|
-
return "api" in o && (o = o.api), o.named_endpoints["/predict"] && !o.unnamed_endpoints[0] && (o.unnamed_endpoints[0] = o.named_endpoints["/predict"]), mt(o, s, this.api_map);
|
|
421
|
-
} catch (n) {
|
|
422
|
-
throw new Error("Could not get API info. " + n.message);
|
|
423
|
-
}
|
|
227
|
+
function ue(e, t) {
|
|
228
|
+
return t === "GallerySerializable" ? "array of [file, label] tuples" : t === "ListStringSerializable" ? "array of strings" : t === "FileSerializable" ? "array of files or single file" : e?.description;
|
|
424
229
|
}
|
|
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
|
-
|
|
230
|
+
function de(e, t) {
|
|
231
|
+
switch (e.msg) {
|
|
232
|
+
case "send_data": return { type: "data" };
|
|
233
|
+
case "send_hash": return { type: "hash" };
|
|
234
|
+
case "queue_full": return {
|
|
235
|
+
type: "update",
|
|
236
|
+
status: {
|
|
237
|
+
queue: !0,
|
|
238
|
+
message: re,
|
|
239
|
+
stage: "error",
|
|
240
|
+
code: e.code,
|
|
241
|
+
success: e.success
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
case "heartbeat": return { type: "heartbeat" };
|
|
245
|
+
case "unexpected_error": return {
|
|
246
|
+
type: "unexpected_error",
|
|
247
|
+
status: {
|
|
248
|
+
queue: !0,
|
|
249
|
+
message: e.message,
|
|
250
|
+
session_not_found: e.session_not_found,
|
|
251
|
+
stage: "error",
|
|
252
|
+
success: !1
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
case "broken_connection": return {
|
|
256
|
+
type: "broken_connection",
|
|
257
|
+
status: {
|
|
258
|
+
queue: !0,
|
|
259
|
+
message: e.message,
|
|
260
|
+
stage: "error",
|
|
261
|
+
success: !1
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
case "estimation": return {
|
|
265
|
+
type: "update",
|
|
266
|
+
status: {
|
|
267
|
+
queue: !0,
|
|
268
|
+
stage: t || "pending",
|
|
269
|
+
code: e.code,
|
|
270
|
+
size: e.queue_size,
|
|
271
|
+
position: e.rank,
|
|
272
|
+
eta: e.rank_eta,
|
|
273
|
+
success: e.success
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
case "progress": return {
|
|
277
|
+
type: "update",
|
|
278
|
+
status: {
|
|
279
|
+
queue: !0,
|
|
280
|
+
stage: "pending",
|
|
281
|
+
code: e.code,
|
|
282
|
+
progress_data: e.progress_data,
|
|
283
|
+
success: e.success
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
case "log": return {
|
|
287
|
+
type: "log",
|
|
288
|
+
data: e
|
|
289
|
+
};
|
|
290
|
+
case "process_generating": return {
|
|
291
|
+
type: "generating",
|
|
292
|
+
status: {
|
|
293
|
+
queue: !0,
|
|
294
|
+
message: e.success ? null : e.output.error,
|
|
295
|
+
stage: e.success ? "generating" : "error",
|
|
296
|
+
code: e.code,
|
|
297
|
+
progress_data: e.progress_data,
|
|
298
|
+
eta: e.average_duration,
|
|
299
|
+
changed_state_ids: e.success ? e.output.changed_state_ids : void 0
|
|
300
|
+
},
|
|
301
|
+
data: e.success ? e.output : null
|
|
302
|
+
};
|
|
303
|
+
case "process_streaming": return {
|
|
304
|
+
type: "streaming",
|
|
305
|
+
status: {
|
|
306
|
+
queue: !0,
|
|
307
|
+
message: e.output.error,
|
|
308
|
+
stage: "streaming",
|
|
309
|
+
time_limit: e.time_limit,
|
|
310
|
+
code: e.code,
|
|
311
|
+
progress_data: e.progress_data,
|
|
312
|
+
changed_state_ids: e.output.changed_state_ids,
|
|
313
|
+
eta: e.eta
|
|
314
|
+
},
|
|
315
|
+
data: e.output
|
|
316
|
+
};
|
|
317
|
+
case "process_completed": return "error" in e.output ? {
|
|
318
|
+
type: "update",
|
|
319
|
+
status: {
|
|
320
|
+
queue: !0,
|
|
321
|
+
title: e.output.title ?? "Error",
|
|
322
|
+
message: e.output.error ?? "An error occurred",
|
|
323
|
+
visible: e.output.visible,
|
|
324
|
+
duration: e.output.duration,
|
|
325
|
+
stage: "error",
|
|
326
|
+
code: e.code,
|
|
327
|
+
success: e.success
|
|
328
|
+
}
|
|
329
|
+
} : {
|
|
330
|
+
type: "complete",
|
|
331
|
+
status: {
|
|
332
|
+
queue: !0,
|
|
333
|
+
message: e.success ? void 0 : e.output.error,
|
|
334
|
+
stage: e.success ? "complete" : "error",
|
|
335
|
+
code: e.code,
|
|
336
|
+
progress_data: e.progress_data,
|
|
337
|
+
changed_state_ids: e.success ? e.output.changed_state_ids : void 0,
|
|
338
|
+
used_cache: e.used_cache,
|
|
339
|
+
cache_duration: e.cache_duration,
|
|
340
|
+
avg_time: e.avg_time
|
|
341
|
+
},
|
|
342
|
+
data: e.success ? e.output : null
|
|
343
|
+
};
|
|
344
|
+
case "process_starts": return {
|
|
345
|
+
type: "update",
|
|
346
|
+
status: {
|
|
347
|
+
queue: !0,
|
|
348
|
+
stage: "pending",
|
|
349
|
+
code: e.code,
|
|
350
|
+
size: e.rank,
|
|
351
|
+
position: 0,
|
|
352
|
+
success: e.success,
|
|
353
|
+
eta: e.eta
|
|
354
|
+
},
|
|
355
|
+
original_msg: "process_starts"
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
return {
|
|
359
|
+
type: "none",
|
|
360
|
+
status: {
|
|
361
|
+
stage: "error",
|
|
362
|
+
queue: !0
|
|
363
|
+
}
|
|
364
|
+
};
|
|
454
365
|
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
366
|
+
var fe = (e = [], t) => {
|
|
367
|
+
let n = t ? t.parameters : [];
|
|
368
|
+
if (Array.isArray(e)) return t && n.length > 0 && e.length > n.length && console.warn("Too many arguments provided for the endpoint."), e;
|
|
369
|
+
let r = [], i = Object.keys(e);
|
|
370
|
+
return n.forEach((t, n) => {
|
|
371
|
+
if (e.hasOwnProperty(t.parameter_name)) r[n] = e[t.parameter_name];
|
|
372
|
+
else if (t.parameter_has_default) r[n] = t.parameter_default;
|
|
373
|
+
else throw Error(`No value provided for required parameter: ${t.parameter_name}`);
|
|
374
|
+
}), i.forEach((e) => {
|
|
375
|
+
if (!n.some((t) => t.parameter_name === e)) throw Error(`Parameter \`${e}\` is not a valid keyword argument. Please refer to the API for usage.`);
|
|
376
|
+
}), r.forEach((e, t) => {
|
|
377
|
+
if (e === void 0 && !n[t].parameter_has_default) throw Error(`No value provided for required parameter: ${n[t].parameter_name}`);
|
|
378
|
+
}), r;
|
|
468
379
|
};
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
380
|
+
//#endregion
|
|
381
|
+
//#region src/utils/view_api.ts
|
|
382
|
+
async function K() {
|
|
383
|
+
if (this.api_info) return this.api_info;
|
|
384
|
+
let { token: e } = this.options, { config: t } = this, n = {};
|
|
385
|
+
if (e && (n.Authorization = `Bearer ${e}`), t) try {
|
|
386
|
+
let e, r;
|
|
387
|
+
if (typeof window < "u" && window.gradio_api_info) r = window.gradio_api_info;
|
|
388
|
+
else {
|
|
389
|
+
let i = W(t.root, this.api_prefix, h);
|
|
390
|
+
if (e = await this.fetch(i, {
|
|
391
|
+
headers: n,
|
|
392
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
393
|
+
}), !e.ok) throw Error(b);
|
|
394
|
+
r = await e.json();
|
|
395
|
+
}
|
|
396
|
+
return "api" in r && (r = r.api), r.named_endpoints["/predict"] && !r.unnamed_endpoints[0] && (r.unnamed_endpoints[0] = r.named_endpoints["/predict"]), G(r, t, this.api_map);
|
|
397
|
+
} catch (e) {
|
|
398
|
+
throw Error("Could not get API info. " + e.message);
|
|
399
|
+
}
|
|
476
400
|
}
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region src/utils/upload_files.ts
|
|
403
|
+
async function q(e, t, n) {
|
|
404
|
+
let r = {};
|
|
405
|
+
this?.options?.token && (r.Authorization = `Bearer ${this.options.token}`);
|
|
406
|
+
let i = 1e3, a = [], o;
|
|
407
|
+
for (let s = 0; s < t.length; s += i) {
|
|
408
|
+
let c = t.slice(s, s + i), l = new FormData();
|
|
409
|
+
c.forEach((e) => {
|
|
410
|
+
l.append("files", e);
|
|
411
|
+
});
|
|
412
|
+
try {
|
|
413
|
+
let t = n ? `${e}${this.api_prefix}/${f}?upload_id=${n}` : `${e}${this.api_prefix}/${f}`;
|
|
414
|
+
o = await this.fetch(t, {
|
|
415
|
+
method: "POST",
|
|
416
|
+
body: l,
|
|
417
|
+
headers: r,
|
|
418
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
419
|
+
});
|
|
420
|
+
} catch (e) {
|
|
421
|
+
throw Error(b + e.message);
|
|
422
|
+
}
|
|
423
|
+
if (!o.ok) {
|
|
424
|
+
let e = await o.text();
|
|
425
|
+
return { error: `HTTP ${o.status}: ${e}` };
|
|
426
|
+
}
|
|
427
|
+
let u = await o.json();
|
|
428
|
+
u && a.push(...u);
|
|
429
|
+
}
|
|
430
|
+
return { files: a };
|
|
501
431
|
}
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
432
|
+
var pe = {
|
|
433
|
+
si: {
|
|
434
|
+
radix: 1e3,
|
|
435
|
+
unit: [
|
|
436
|
+
"b",
|
|
437
|
+
"kb",
|
|
438
|
+
"Mb",
|
|
439
|
+
"Gb",
|
|
440
|
+
"Tb",
|
|
441
|
+
"Pb",
|
|
442
|
+
"Eb",
|
|
443
|
+
"Zb",
|
|
444
|
+
"Yb"
|
|
445
|
+
]
|
|
446
|
+
},
|
|
447
|
+
iec: {
|
|
448
|
+
radix: 1024,
|
|
449
|
+
unit: [
|
|
450
|
+
"b",
|
|
451
|
+
"Kib",
|
|
452
|
+
"Mib",
|
|
453
|
+
"Gib",
|
|
454
|
+
"Tib",
|
|
455
|
+
"Pib",
|
|
456
|
+
"Eib",
|
|
457
|
+
"Zib",
|
|
458
|
+
"Yib"
|
|
459
|
+
]
|
|
460
|
+
},
|
|
461
|
+
jedec: {
|
|
462
|
+
radix: 1024,
|
|
463
|
+
unit: [
|
|
464
|
+
"b",
|
|
465
|
+
"Kb",
|
|
466
|
+
"Mb",
|
|
467
|
+
"Gb",
|
|
468
|
+
"Tb",
|
|
469
|
+
"Pb",
|
|
470
|
+
"Eb",
|
|
471
|
+
"Zb",
|
|
472
|
+
"Yb"
|
|
473
|
+
]
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
function me(e, t = 1, n = "jedec") {
|
|
477
|
+
e = Math.abs(e);
|
|
478
|
+
let { radix: r, unit: i } = pe[n] || pe.jedec, a = 0;
|
|
479
|
+
for (; e >= r;) e /= r, ++a;
|
|
480
|
+
return `${e.toFixed(t)} ${i[a]}`;
|
|
513
481
|
}
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
url: t,
|
|
528
|
-
orig_name: n,
|
|
529
|
-
size: o,
|
|
530
|
-
blob: r,
|
|
531
|
-
is_stream: i,
|
|
532
|
-
mime_type: a,
|
|
533
|
-
alt_text: u,
|
|
534
|
-
b64: l
|
|
535
|
-
}) {
|
|
536
|
-
this.path = s, this.url = t, this.orig_name = n, this.size = o, this.blob = t ? void 0 : r, this.is_stream = i, this.mime_type = a, this.alt_text = u, this.b64 = l;
|
|
537
|
-
}
|
|
482
|
+
//#endregion
|
|
483
|
+
//#region src/upload.ts
|
|
484
|
+
async function he(e, t, n, r) {
|
|
485
|
+
let i = (Array.isArray(e) ? e : [e]).map((e) => e.blob), a = i.filter((e) => e.size > (r ?? Infinity));
|
|
486
|
+
if (a.length) throw Error(`File(s) exceed the maximum allowed size of ${me(r || Infinity)}: ${a.map((e) => `"${e.name}"`).join(", ")}`);
|
|
487
|
+
return await Promise.all(await this.upload_files(t, i, n).then(async (n) => {
|
|
488
|
+
if (n.error) throw Error(n.error);
|
|
489
|
+
return n.files ? n.files.map((n, r) => new J({
|
|
490
|
+
...e[r],
|
|
491
|
+
path: n,
|
|
492
|
+
url: `${t}${this.api_prefix}/file=${n}`
|
|
493
|
+
})) : [];
|
|
494
|
+
}));
|
|
538
495
|
}
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
496
|
+
async function ge(e, t) {
|
|
497
|
+
return e.map((e) => new J({
|
|
498
|
+
path: e.name,
|
|
499
|
+
orig_name: e.name,
|
|
500
|
+
blob: e,
|
|
501
|
+
size: e.size,
|
|
502
|
+
mime_type: e.type,
|
|
503
|
+
is_stream: t
|
|
504
|
+
}));
|
|
547
505
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
506
|
+
var J = class {
|
|
507
|
+
path;
|
|
508
|
+
url;
|
|
509
|
+
orig_name;
|
|
510
|
+
size;
|
|
511
|
+
blob;
|
|
512
|
+
is_stream;
|
|
513
|
+
mime_type;
|
|
514
|
+
alt_text;
|
|
515
|
+
b64;
|
|
516
|
+
meta = { _type: "gradio.FileData" };
|
|
517
|
+
constructor({ path: e, url: t, orig_name: n, size: r, blob: i, is_stream: a, mime_type: o, alt_text: s, b64: c }) {
|
|
518
|
+
this.path = e, this.url = t, this.orig_name = n, this.size = r, this.blob = t ? void 0 : i, this.is_stream = a, this.mime_type = o, this.alt_text = s, this.b64 = c;
|
|
519
|
+
}
|
|
520
|
+
}, _e = class {
|
|
521
|
+
type;
|
|
522
|
+
command;
|
|
523
|
+
meta;
|
|
524
|
+
fileData;
|
|
525
|
+
constructor(e, t) {
|
|
526
|
+
this.type = "command", this.command = e, this.meta = t;
|
|
527
|
+
}
|
|
528
|
+
}, ve = typeof process < "u" && process.versions && process.versions.node;
|
|
529
|
+
function ye(e, t, n) {
|
|
530
|
+
for (; n.length > 1;) {
|
|
531
|
+
let t = n.shift();
|
|
532
|
+
if (typeof t == "string" || typeof t == "number") e = e[t];
|
|
533
|
+
else throw Error("Invalid key type");
|
|
534
|
+
}
|
|
535
|
+
let r = n.shift();
|
|
536
|
+
if (typeof r == "string" || typeof r == "number") e[r] = t;
|
|
537
|
+
else throw Error("Invalid key type");
|
|
562
538
|
}
|
|
563
|
-
async function
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
type: s
|
|
587
|
-
}
|
|
588
|
-
];
|
|
589
|
-
if (typeof e == "object" && e !== null) {
|
|
590
|
-
let r = [];
|
|
591
|
-
for (const i of Object.keys(e)) {
|
|
592
|
-
const a = [...t, i], u = e[i];
|
|
593
|
-
r = r.concat(
|
|
594
|
-
await ce(
|
|
595
|
-
u,
|
|
596
|
-
void 0,
|
|
597
|
-
a,
|
|
598
|
-
!1,
|
|
599
|
-
o
|
|
600
|
-
)
|
|
601
|
-
);
|
|
602
|
-
}
|
|
603
|
-
return r;
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
return [];
|
|
539
|
+
async function Y(e, t = void 0, n = [], r = !1, i = void 0) {
|
|
540
|
+
if (Array.isArray(e)) {
|
|
541
|
+
let a = [];
|
|
542
|
+
return await Promise.all(e.map(async (o, s) => {
|
|
543
|
+
let c = n.slice();
|
|
544
|
+
c.push(String(s));
|
|
545
|
+
let l = await Y(e[s], r ? i?.parameters[s]?.component || void 0 : t, c, !1, i);
|
|
546
|
+
a = a.concat(l);
|
|
547
|
+
})), a;
|
|
548
|
+
} else if (globalThis.Buffer && e instanceof globalThis.Buffer || e instanceof Blob) return [{
|
|
549
|
+
path: n,
|
|
550
|
+
blob: e instanceof Blob ? e : new Blob([e]),
|
|
551
|
+
type: t
|
|
552
|
+
}];
|
|
553
|
+
else if (typeof e == "object" && e) {
|
|
554
|
+
let t = [];
|
|
555
|
+
for (let r of Object.keys(e)) {
|
|
556
|
+
let a = [...n, r], o = e[r];
|
|
557
|
+
t = t.concat(await Y(o, void 0, a, !1, i));
|
|
558
|
+
}
|
|
559
|
+
return t;
|
|
560
|
+
}
|
|
561
|
+
return [];
|
|
607
562
|
}
|
|
608
|
-
function
|
|
609
|
-
|
|
610
|
-
|
|
563
|
+
function be(e, t) {
|
|
564
|
+
let n = t?.dependencies?.find((t) => t.id == e)?.queue;
|
|
565
|
+
return n == null ? !t.enable_queue : !n;
|
|
611
566
|
}
|
|
612
|
-
function
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
567
|
+
function xe(e, t) {
|
|
568
|
+
return new Promise((n, r) => {
|
|
569
|
+
let i = new MessageChannel();
|
|
570
|
+
i.port1.onmessage = (({ data: e }) => {
|
|
571
|
+
i.port1.close(), n(e);
|
|
572
|
+
}), window.parent.postMessage(e, t, [i.port2]);
|
|
573
|
+
});
|
|
619
574
|
}
|
|
620
|
-
function
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
return new Blob([e]);
|
|
638
|
-
if (e instanceof Buffer)
|
|
639
|
-
return new Blob([e]);
|
|
640
|
-
if (e instanceof Blob)
|
|
641
|
-
return e;
|
|
642
|
-
}
|
|
643
|
-
throw new Error(
|
|
644
|
-
"Invalid input: must be a URL, File, Blob, or Buffer object."
|
|
645
|
-
);
|
|
575
|
+
function Se(e) {
|
|
576
|
+
if (typeof e == "string") {
|
|
577
|
+
if (e.startsWith("http://") || e.startsWith("https://")) return {
|
|
578
|
+
path: e,
|
|
579
|
+
url: e,
|
|
580
|
+
orig_name: e.split("/").pop() ?? "unknown",
|
|
581
|
+
meta: { _type: "gradio.FileData" }
|
|
582
|
+
};
|
|
583
|
+
if (ve) return new _e("upload_file", {
|
|
584
|
+
path: e,
|
|
585
|
+
name: e,
|
|
586
|
+
orig_path: e
|
|
587
|
+
});
|
|
588
|
+
} else if (typeof File < "u" && e instanceof File) return e;
|
|
589
|
+
else if (globalThis.Buffer && e instanceof globalThis.Buffer) return new Blob([e]);
|
|
590
|
+
else if (e instanceof Blob) return e;
|
|
591
|
+
throw Error("Invalid input: must be a URL, File, Blob, or Buffer object.");
|
|
646
592
|
}
|
|
647
|
-
function
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
const c = e[i];
|
|
670
|
-
r.push(c), i++;
|
|
671
|
-
}
|
|
672
|
-
}
|
|
673
|
-
return r;
|
|
593
|
+
function X(e, t, n, r, i = !1) {
|
|
594
|
+
if (r === "input" && !i) throw Error("Invalid code path. Cannot skip state inputs for input.");
|
|
595
|
+
if (r === "output" && i) return e;
|
|
596
|
+
let a = [], o = 0, s = r === "input" ? t.inputs : t.outputs;
|
|
597
|
+
for (let t = 0; t < s.length; t++) {
|
|
598
|
+
let r = s[t];
|
|
599
|
+
if (n.find((e) => e.id === r)?.type === "state") {
|
|
600
|
+
if (i) if (e.length === s.length) {
|
|
601
|
+
let t = e[o];
|
|
602
|
+
a.push(t), o++;
|
|
603
|
+
} else a.push(null);
|
|
604
|
+
else {
|
|
605
|
+
o++;
|
|
606
|
+
continue;
|
|
607
|
+
}
|
|
608
|
+
continue;
|
|
609
|
+
} else {
|
|
610
|
+
let t = e[o];
|
|
611
|
+
a.push(t), o++;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
return a;
|
|
674
615
|
}
|
|
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
|
-
else if (a) {
|
|
700
|
-
const p = new Q({ path: a, orig_name: l });
|
|
701
|
-
$e(s, p, i);
|
|
702
|
-
}
|
|
703
|
-
}), s;
|
|
616
|
+
//#endregion
|
|
617
|
+
//#region src/utils/handle_blob.ts
|
|
618
|
+
async function Ce(e, t, n) {
|
|
619
|
+
let r = this;
|
|
620
|
+
await we(r, t);
|
|
621
|
+
let i = await Y(t, void 0, [], !0, n);
|
|
622
|
+
return (await Promise.all(i.map(async ({ path: t, blob: n, type: i }) => {
|
|
623
|
+
if (!n) return {
|
|
624
|
+
path: t,
|
|
625
|
+
type: i
|
|
626
|
+
};
|
|
627
|
+
let a = await r.upload_files(e, [n]);
|
|
628
|
+
return {
|
|
629
|
+
path: t,
|
|
630
|
+
file_url: a.files && a.files[0],
|
|
631
|
+
type: i,
|
|
632
|
+
name: typeof File < "u" && n instanceof File ? n?.name : void 0
|
|
633
|
+
};
|
|
634
|
+
}))).forEach(({ path: e, file_url: n, type: r, name: i }) => {
|
|
635
|
+
r === "Gallery" ? ye(t, n, e) : n && ye(t, new J({
|
|
636
|
+
path: n,
|
|
637
|
+
orig_name: i
|
|
638
|
+
}), e);
|
|
639
|
+
}), t;
|
|
704
640
|
}
|
|
705
|
-
async function
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
await Ie(e, s);
|
|
641
|
+
async function we(e, t) {
|
|
642
|
+
if (!(e.config?.root || e.config?.root_url)) throw Error(j);
|
|
643
|
+
await Te(e, t);
|
|
709
644
|
}
|
|
710
|
-
async function
|
|
711
|
-
|
|
712
|
-
s[n] instanceof je ? await Lt(e, s, n) : typeof s[n] == "object" && s[n] !== null && await Ie(e, s[n], [...t, n]);
|
|
645
|
+
async function Te(e, t, n = []) {
|
|
646
|
+
for (let r in t) t[r] instanceof _e ? await Ee(e, t, r) : typeof t[r] == "object" && t[r] !== null && await Te(e, t[r], [...n, r]);
|
|
713
647
|
}
|
|
714
|
-
async function
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
path: l,
|
|
732
|
-
orig_name: n.meta.name || ""
|
|
733
|
-
});
|
|
734
|
-
s[t] = p;
|
|
735
|
-
}
|
|
736
|
-
} catch (r) {
|
|
737
|
-
console.error(dt, r);
|
|
738
|
-
}
|
|
648
|
+
async function Ee(e, t, n) {
|
|
649
|
+
let r = t[n], i = e.config?.root || e.config?.root_url;
|
|
650
|
+
if (!i) throw Error(j);
|
|
651
|
+
try {
|
|
652
|
+
let a, o;
|
|
653
|
+
if (typeof process < "u" && process.versions && process.versions.node) {
|
|
654
|
+
let e = await import("./__vite-browser-external-Cgmn0awE.js").then((e) => /* @__PURE__ */ c(e.default, 1));
|
|
655
|
+
o = (await import("./__vite-browser-external-Cgmn0awE.js").then((e) => /* @__PURE__ */ c(e.default, 1))).resolve(process.cwd(), r.meta.path), a = await e.readFile(o);
|
|
656
|
+
} else throw Error(A);
|
|
657
|
+
let s = new Blob([a], { type: "application/octet-stream" }), l = await e.upload_files(i, [s]), u = l.files && l.files[0];
|
|
658
|
+
u && (t[n] = new J({
|
|
659
|
+
path: u,
|
|
660
|
+
orig_name: r.meta.name || ""
|
|
661
|
+
}));
|
|
662
|
+
} catch (e) {
|
|
663
|
+
console.error(M, e);
|
|
664
|
+
}
|
|
739
665
|
}
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
666
|
+
//#endregion
|
|
667
|
+
//#region src/utils/post_data.ts
|
|
668
|
+
async function De(e, t, n) {
|
|
669
|
+
let r = { "Content-Type": "application/json" };
|
|
670
|
+
this.options.token && (r.Authorization = `Bearer ${this.options.token}`);
|
|
671
|
+
try {
|
|
672
|
+
var i = await this.fetch(e, {
|
|
673
|
+
method: "POST",
|
|
674
|
+
body: JSON.stringify(t),
|
|
675
|
+
headers: {
|
|
676
|
+
...r,
|
|
677
|
+
...n
|
|
678
|
+
},
|
|
679
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
680
|
+
});
|
|
681
|
+
} catch {
|
|
682
|
+
return [{ error: b }, 500];
|
|
683
|
+
}
|
|
684
|
+
let a, o;
|
|
685
|
+
try {
|
|
686
|
+
a = await i.json(), o = i.status;
|
|
687
|
+
} catch (e) {
|
|
688
|
+
a = { error: `Could not parse server response: ${e}` }, o = 500;
|
|
689
|
+
}
|
|
690
|
+
return [a, o];
|
|
760
691
|
}
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
692
|
+
//#endregion
|
|
693
|
+
//#region src/utils/predict.ts
|
|
694
|
+
async function Oe(e, t = {}) {
|
|
695
|
+
let n = !1, r = !1;
|
|
696
|
+
if (!this.config) throw Error("Could not resolve app config");
|
|
697
|
+
if (typeof e == "number") this.config.dependencies.find((t) => t.id == e);
|
|
698
|
+
else {
|
|
699
|
+
let t = e.replace(/^\//, "");
|
|
700
|
+
this.config.dependencies.find((e) => e.id == this.api_map[t]);
|
|
701
|
+
}
|
|
702
|
+
let i = this.submit(e, t, null, null, !0), a;
|
|
703
|
+
for await (let e of i) {
|
|
704
|
+
if (e.type === "data" && (n = !0, a = e, r)) return a;
|
|
705
|
+
if (e.type === "status") {
|
|
706
|
+
if (e.stage === "error") {
|
|
707
|
+
let { message: t, ...n } = e, r = Error((typeof t == "string" ? t : t && JSON.stringify(t)) || "An unknown error occurred while making a prediction.");
|
|
708
|
+
throw Object.assign(r, n), r;
|
|
709
|
+
}
|
|
710
|
+
if (e.stage === "complete" && (r = !0, n)) return a;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return a;
|
|
779
714
|
}
|
|
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
|
-
break;
|
|
859
|
-
}
|
|
715
|
+
//#endregion
|
|
716
|
+
//#region src/helpers/spaces.ts
|
|
717
|
+
async function Z(e, t, n) {
|
|
718
|
+
let r = t === "subdomain" ? `https://huggingface.co/api/spaces/by-subdomain/${e}` : `https://huggingface.co/api/spaces/${e}`, i, a;
|
|
719
|
+
try {
|
|
720
|
+
if (i = await fetch(r), a = i.status, a !== 200) throw Error();
|
|
721
|
+
i = await i.json();
|
|
722
|
+
} catch {
|
|
723
|
+
n({
|
|
724
|
+
status: "error",
|
|
725
|
+
load_status: "error",
|
|
726
|
+
message: S,
|
|
727
|
+
detail: "NOT_FOUND"
|
|
728
|
+
});
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
if (!i || a !== 200) return;
|
|
732
|
+
let { runtime: { stage: o }, id: s } = i;
|
|
733
|
+
switch (o) {
|
|
734
|
+
case "STOPPED":
|
|
735
|
+
case "SLEEPING":
|
|
736
|
+
n({
|
|
737
|
+
status: "sleeping",
|
|
738
|
+
load_status: "pending",
|
|
739
|
+
message: "Space is asleep. Waking it up...",
|
|
740
|
+
detail: o
|
|
741
|
+
}), setTimeout(() => {
|
|
742
|
+
Z(e, t, n);
|
|
743
|
+
}, 1e3);
|
|
744
|
+
break;
|
|
745
|
+
case "PAUSED":
|
|
746
|
+
n({
|
|
747
|
+
status: "paused",
|
|
748
|
+
load_status: "error",
|
|
749
|
+
message: "This space has been paused by the author. If you would like to try this demo, consider duplicating the space.",
|
|
750
|
+
detail: o,
|
|
751
|
+
discussions_enabled: await je(s)
|
|
752
|
+
});
|
|
753
|
+
break;
|
|
754
|
+
case "RUNNING":
|
|
755
|
+
case "RUNNING_BUILDING":
|
|
756
|
+
n({
|
|
757
|
+
status: "running",
|
|
758
|
+
load_status: "complete",
|
|
759
|
+
message: "Space is running.",
|
|
760
|
+
detail: o
|
|
761
|
+
});
|
|
762
|
+
break;
|
|
763
|
+
case "BUILDING":
|
|
764
|
+
n({
|
|
765
|
+
status: "building",
|
|
766
|
+
load_status: "pending",
|
|
767
|
+
message: "Space is building...",
|
|
768
|
+
detail: o
|
|
769
|
+
}), setTimeout(() => {
|
|
770
|
+
Z(e, t, n);
|
|
771
|
+
}, 1e3);
|
|
772
|
+
break;
|
|
773
|
+
case "APP_STARTING":
|
|
774
|
+
n({
|
|
775
|
+
status: "starting",
|
|
776
|
+
load_status: "pending",
|
|
777
|
+
message: "Space is starting...",
|
|
778
|
+
detail: o
|
|
779
|
+
}), setTimeout(() => {
|
|
780
|
+
Z(e, t, n);
|
|
781
|
+
}, 1e3);
|
|
782
|
+
break;
|
|
783
|
+
default:
|
|
784
|
+
n({
|
|
785
|
+
status: "space_error",
|
|
786
|
+
load_status: "error",
|
|
787
|
+
message: "This space is experiencing an issue.",
|
|
788
|
+
detail: o,
|
|
789
|
+
discussions_enabled: await je(s)
|
|
790
|
+
});
|
|
791
|
+
break;
|
|
792
|
+
}
|
|
860
793
|
}
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
const s = await fetch(
|
|
879
|
-
`https://huggingface.co/api/spaces/${e}/discussions`,
|
|
880
|
-
{
|
|
881
|
-
method: "HEAD"
|
|
882
|
-
}
|
|
883
|
-
), t = s.headers.get("x-error-message");
|
|
884
|
-
return !(!s.ok || t && jt.test(t));
|
|
885
|
-
} catch {
|
|
886
|
-
return !1;
|
|
887
|
-
}
|
|
794
|
+
var ke = async (e, t) => {
|
|
795
|
+
let n = 0;
|
|
796
|
+
return new Promise((r) => {
|
|
797
|
+
Z(e, V.test(e) ? "space_name" : "subdomain", (i) => {
|
|
798
|
+
t(i), i.status === "running" || i.status === "error" || i.status === "paused" || i.status === "space_error" ? r() : (i.status === "sleeping" || i.status === "building") && (n < 12 ? (n++, setTimeout(() => {
|
|
799
|
+
ke(e, t).then(r);
|
|
800
|
+
}, 5e3)) : r());
|
|
801
|
+
});
|
|
802
|
+
});
|
|
803
|
+
}, Ae = /^(?=[^]*\b[dD]iscussions{0,1}\b)(?=[^]*\b[dD]isabled\b)[^]*$/;
|
|
804
|
+
async function je(e) {
|
|
805
|
+
try {
|
|
806
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/discussions`, { method: "HEAD" }), n = t.headers.get("x-error-message");
|
|
807
|
+
return !(!t.ok || n && Ae.test(n));
|
|
808
|
+
} catch {
|
|
809
|
+
return !1;
|
|
810
|
+
}
|
|
888
811
|
}
|
|
889
|
-
async function
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
return o.current;
|
|
901
|
-
} catch (n) {
|
|
902
|
-
throw new Error(n.message);
|
|
903
|
-
}
|
|
812
|
+
async function Me(e, t) {
|
|
813
|
+
let n = {};
|
|
814
|
+
t && (n.Authorization = `Bearer ${t}`);
|
|
815
|
+
try {
|
|
816
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/${g}`, { headers: n });
|
|
817
|
+
if (t.status !== 200) throw Error("Space hardware could not be obtained.");
|
|
818
|
+
let { hardware: r } = await t.json();
|
|
819
|
+
return r.current;
|
|
820
|
+
} catch (e) {
|
|
821
|
+
throw Error(e.message);
|
|
822
|
+
}
|
|
904
823
|
}
|
|
905
|
-
async function
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
);
|
|
924
|
-
return await r.json();
|
|
925
|
-
} catch (r) {
|
|
926
|
-
throw new Error(r.message);
|
|
927
|
-
}
|
|
824
|
+
async function Ne(e, t, n) {
|
|
825
|
+
let r = {};
|
|
826
|
+
n && (r.Authorization = `Bearer ${n}`);
|
|
827
|
+
let i = { seconds: t };
|
|
828
|
+
try {
|
|
829
|
+
let t = await fetch(`https://huggingface.co/api/spaces/${e}/${_}`, {
|
|
830
|
+
method: "POST",
|
|
831
|
+
headers: {
|
|
832
|
+
"Content-Type": "application/json",
|
|
833
|
+
...r
|
|
834
|
+
},
|
|
835
|
+
body: JSON.stringify(i)
|
|
836
|
+
});
|
|
837
|
+
if (t.status !== 200) throw Error("Could not set sleep timeout on duplicated Space. Please visit *ADD HF LINK TO SETTINGS* to set a timeout manually to reduce billing charges.");
|
|
838
|
+
return await t.json();
|
|
839
|
+
} catch (e) {
|
|
840
|
+
throw Error(e.message);
|
|
841
|
+
}
|
|
928
842
|
}
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
843
|
+
var Pe = [
|
|
844
|
+
"cpu-basic",
|
|
845
|
+
"cpu-upgrade",
|
|
846
|
+
"cpu-xl",
|
|
847
|
+
"t4-small",
|
|
848
|
+
"t4-medium",
|
|
849
|
+
"a10g-small",
|
|
850
|
+
"a10g-large",
|
|
851
|
+
"a10g-largex2",
|
|
852
|
+
"a10g-largex4",
|
|
853
|
+
"a100-large",
|
|
854
|
+
"zero-a10g",
|
|
855
|
+
"h100",
|
|
856
|
+
"h100x8"
|
|
943
857
|
];
|
|
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
|
-
body: JSON.stringify(w)
|
|
986
|
-
}
|
|
987
|
-
);
|
|
988
|
-
if (m.status === 409)
|
|
989
|
-
try {
|
|
990
|
-
return await Y.connect(`${c}/${g}`, s);
|
|
991
|
-
} catch (z) {
|
|
992
|
-
throw console.error("Failed to connect Client instance:", z), z;
|
|
993
|
-
}
|
|
994
|
-
else if (m.status !== 200)
|
|
995
|
-
throw new Error(m.statusText);
|
|
996
|
-
const H = await m.json();
|
|
997
|
-
return await Ut(`${c}/${g}`, r || 300, t), await Y.connect(
|
|
998
|
-
zt(H.url),
|
|
999
|
-
s
|
|
1000
|
-
);
|
|
1001
|
-
} catch (m) {
|
|
1002
|
-
throw new Error(m);
|
|
1003
|
-
}
|
|
1004
|
-
}
|
|
1005
|
-
function zt(e) {
|
|
1006
|
-
const s = /https:\/\/huggingface.co\/spaces\/([^/]+\/[^/]+)/, t = e.match(s);
|
|
1007
|
-
if (t)
|
|
1008
|
-
return t[1];
|
|
858
|
+
//#endregion
|
|
859
|
+
//#region src/utils/duplicate.ts
|
|
860
|
+
async function Fe(e, t) {
|
|
861
|
+
P(t);
|
|
862
|
+
let { token: n, private: r, hardware: i, timeout: a, auth: o } = t;
|
|
863
|
+
if (i && !Pe.includes(i)) throw Error(`Invalid hardware type provided. Valid types are: ${Pe.map((e) => `"${e}"`).join(",")}.`);
|
|
864
|
+
let { http_protocol: s, host: c } = await U(e, n), l = null;
|
|
865
|
+
if (o) {
|
|
866
|
+
let e = await R(s, c, o, fetch, void 0, t.credentials);
|
|
867
|
+
e && (l = B(e));
|
|
868
|
+
}
|
|
869
|
+
let u = {
|
|
870
|
+
Authorization: `Bearer ${n}`,
|
|
871
|
+
"Content-Type": "application/json",
|
|
872
|
+
...l ? { Cookie: l.join("; ") } : {}
|
|
873
|
+
}, d = (await (await fetch("https://huggingface.co/api/whoami-v2", { headers: u })).json()).name, f = e.split("/")[1], p = { repository: `${d}/${f}` };
|
|
874
|
+
r && (p.private = !0);
|
|
875
|
+
let m;
|
|
876
|
+
try {
|
|
877
|
+
i || (m = await Me(e, n));
|
|
878
|
+
} catch (e) {
|
|
879
|
+
throw Error(w + e.message);
|
|
880
|
+
}
|
|
881
|
+
p.hardware = i || m || "cpu-basic";
|
|
882
|
+
try {
|
|
883
|
+
let r = await fetch(`https://huggingface.co/api/spaces/${e}/duplicate`, {
|
|
884
|
+
method: "POST",
|
|
885
|
+
headers: u,
|
|
886
|
+
body: JSON.stringify(p)
|
|
887
|
+
});
|
|
888
|
+
if (r.status === 409) try {
|
|
889
|
+
return await $.connect(`${d}/${f}`, t);
|
|
890
|
+
} catch (e) {
|
|
891
|
+
throw console.error("Failed to connect Client instance:", e), e;
|
|
892
|
+
}
|
|
893
|
+
else if (r.status !== 200) throw Error(r.statusText);
|
|
894
|
+
let i = await r.json();
|
|
895
|
+
return await Ne(`${d}/${f}`, a || 300, n), await $.connect(Ie(i.url), t);
|
|
896
|
+
} catch (e) {
|
|
897
|
+
throw Error(e);
|
|
898
|
+
}
|
|
1009
899
|
}
|
|
1010
|
-
|
|
1011
|
-
let
|
|
1012
|
-
|
|
1013
|
-
return typeof window < "u" && typeof document < "u" && typeof window.addEventListener == "function";
|
|
900
|
+
function Ie(e) {
|
|
901
|
+
let t = e.match(/https:\/\/huggingface.co\/spaces\/([^/]+\/[^/]+)/);
|
|
902
|
+
if (t) return t[1];
|
|
1014
903
|
}
|
|
1015
|
-
|
|
1016
|
-
|
|
904
|
+
//#endregion
|
|
905
|
+
//#region src/helpers/zerogpu.ts
|
|
906
|
+
var Le = "supports-zerogpu-headers", Re = !1;
|
|
907
|
+
function ze() {
|
|
908
|
+
return typeof window < "u" && typeof document < "u" && typeof window.addEventListener == "function";
|
|
1017
909
|
}
|
|
1018
|
-
function
|
|
1019
|
-
|
|
1020
|
-
return;
|
|
1021
|
-
window.addEventListener("message", (s) => {
|
|
1022
|
-
s.data === Oe && (window.supports_zerogpu_headers = !0);
|
|
1023
|
-
}), Te = !0;
|
|
1024
|
-
const e = qe(window.location.hostname);
|
|
1025
|
-
e && window.parent !== window && window.parent.postMessage(Oe, e);
|
|
910
|
+
function Be(e) {
|
|
911
|
+
return e.includes(".dev.") ? `https://moon-${e.split(".")[1]}.dev.spaces.huggingface.tech` : e.endsWith(".hf.space") ? "https://huggingface.co" : null;
|
|
1026
912
|
}
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
const o = t.indexOf(`
|
|
1035
|
-
`), r = s.allowCR ? t.indexOf("\r") : -1;
|
|
1036
|
-
if (r !== -1 && r !== t.length - 1 && (o === -1 || o - 1 > r)) {
|
|
1037
|
-
n.enqueue(t.slice(0, r)), t = t.slice(r + 1);
|
|
1038
|
-
continue;
|
|
1039
|
-
}
|
|
1040
|
-
if (o === -1)
|
|
1041
|
-
break;
|
|
1042
|
-
const i = t[o - 1] === "\r" ? o - 1 : o;
|
|
1043
|
-
n.enqueue(t.slice(0, i)), t = t.slice(o + 1);
|
|
1044
|
-
}
|
|
1045
|
-
this.#e = t;
|
|
1046
|
-
},
|
|
1047
|
-
flush: (t) => {
|
|
1048
|
-
if (this.#e === "")
|
|
1049
|
-
return;
|
|
1050
|
-
const n = s.allowCR && this.#e.endsWith("\r") ? this.#e.slice(0, -1) : this.#e;
|
|
1051
|
-
t.enqueue(n);
|
|
1052
|
-
}
|
|
1053
|
-
});
|
|
1054
|
-
}
|
|
913
|
+
function Ve() {
|
|
914
|
+
if (!ze() || Re) return;
|
|
915
|
+
window.addEventListener("message", (e) => {
|
|
916
|
+
e.data === Le && (window.supports_zerogpu_headers = !0);
|
|
917
|
+
}), Re = !0;
|
|
918
|
+
let e = Be(window.location.hostname);
|
|
919
|
+
e && window.parent !== window && window.parent.postMessage(Le, e);
|
|
1055
920
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
921
|
+
//#endregion
|
|
922
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/deps/jsr.io/@std/streams/0.221.0/text_line_stream.js
|
|
923
|
+
var He = class extends TransformStream {
|
|
924
|
+
#e = "";
|
|
925
|
+
constructor(e = { allowCR: !1 }) {
|
|
926
|
+
super({
|
|
927
|
+
transform: (t, n) => {
|
|
928
|
+
for (t = this.#e + t;;) {
|
|
929
|
+
let r = t.indexOf("\n"), i = e.allowCR ? t.indexOf("\r") : -1;
|
|
930
|
+
if (i !== -1 && i !== t.length - 1 && (r === -1 || r - 1 > i)) {
|
|
931
|
+
n.enqueue(t.slice(0, i)), t = t.slice(i + 1);
|
|
932
|
+
continue;
|
|
933
|
+
}
|
|
934
|
+
if (r === -1) break;
|
|
935
|
+
let a = t[r - 1] === "\r" ? r - 1 : r;
|
|
936
|
+
n.enqueue(t.slice(0, a)), t = t.slice(r + 1);
|
|
937
|
+
}
|
|
938
|
+
this.#e = t;
|
|
939
|
+
},
|
|
940
|
+
flush: (t) => {
|
|
941
|
+
if (this.#e === "") return;
|
|
942
|
+
let n = e.allowCR && this.#e.endsWith("\r") ? this.#e.slice(0, -1) : this.#e;
|
|
943
|
+
t.enqueue(n);
|
|
944
|
+
}
|
|
945
|
+
});
|
|
946
|
+
}
|
|
947
|
+
};
|
|
948
|
+
//#endregion
|
|
949
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/utils.js
|
|
950
|
+
function Ue(e) {
|
|
951
|
+
let t = new TextDecoderStream(), n = new He({ allowCR: !0 });
|
|
952
|
+
return e.pipeThrough(t).pipeThrough(n);
|
|
1067
953
|
}
|
|
1068
|
-
function
|
|
1069
|
-
|
|
954
|
+
function We(e) {
|
|
955
|
+
let t = /[:]\s*/.exec(e), n = t && t.index;
|
|
956
|
+
if (n) return [e.substring(0, n), e.substring(n + t[0].length)];
|
|
1070
957
|
}
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
return;
|
|
1074
|
-
let t = Ft(e.body), n, o = t.getReader(), r;
|
|
1075
|
-
for (; ; ) {
|
|
1076
|
-
if (s && s.aborted)
|
|
1077
|
-
return o.cancel();
|
|
1078
|
-
if (n = await o.read(), n.done)
|
|
1079
|
-
return;
|
|
1080
|
-
if (!n.value) {
|
|
1081
|
-
r && (yield r), r = void 0;
|
|
1082
|
-
continue;
|
|
1083
|
-
}
|
|
1084
|
-
let [i, a] = Ht(n.value) || [];
|
|
1085
|
-
i === "data" ? (r ||= {}, r[i] = r[i] ? r[i] + `
|
|
1086
|
-
` + a : a) : i === "event" ? (r ||= {}, r[i] = a) : i === "id" ? (r ||= {}, r[i] = String(+a) === a ? +a : a) : i === "retry" && (r ||= {}, r[i] = +a || void 0);
|
|
1087
|
-
}
|
|
958
|
+
function Ge(e, t, n) {
|
|
959
|
+
e.get(t) || e.set(t, n);
|
|
1088
960
|
}
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
961
|
+
//#endregion
|
|
962
|
+
//#region ../../node_modules/.pnpm/fetch-event-stream@0.1.6/node_modules/fetch-event-stream/esm/mod.js
|
|
963
|
+
async function* Ke(e, t) {
|
|
964
|
+
if (!e.body) return;
|
|
965
|
+
let n = Ue(e.body), r, i = n.getReader(), a;
|
|
966
|
+
for (;;) {
|
|
967
|
+
if (t && t.aborted) return i.cancel();
|
|
968
|
+
if (r = await i.read(), r.done) return;
|
|
969
|
+
if (!r.value) {
|
|
970
|
+
a && (yield a), a = void 0;
|
|
971
|
+
continue;
|
|
972
|
+
}
|
|
973
|
+
let [e, n] = We(r.value) || [];
|
|
974
|
+
e === "data" ? (a ||= {}, a[e] = a[e] ? a[e] + "\n" + n : n) : e === "event" ? (a ||= {}, a[e] = n) : e === "id" ? (a ||= {}, a[e] = String(+n) === n ? +n : n) : e === "retry" && (a ||= {}, a[e] = +n || void 0);
|
|
975
|
+
}
|
|
1096
976
|
}
|
|
1097
|
-
async function
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
config: o,
|
|
1104
|
-
jwt: r
|
|
1105
|
-
} = this;
|
|
1106
|
-
const i = this;
|
|
1107
|
-
if (!o)
|
|
1108
|
-
throw new Error("Could not resolve app config");
|
|
1109
|
-
n.open = !0;
|
|
1110
|
-
let a = null, u = new URLSearchParams({
|
|
1111
|
-
session_hash: this.session_hash
|
|
1112
|
-
}).toString(), l = new URL(`${o.root}${this.api_prefix}/${xe}?${u}`);
|
|
1113
|
-
if (r && l.searchParams.set("__sign", r), a = this.stream(l), !a) {
|
|
1114
|
-
console.warn("Cannot connect to SSE endpoint: " + l.toString());
|
|
1115
|
-
return;
|
|
1116
|
-
}
|
|
1117
|
-
a.onmessage = async function(p) {
|
|
1118
|
-
let c = JSON.parse(p.data);
|
|
1119
|
-
if (c.msg === "close_stream") {
|
|
1120
|
-
de(n, i.abort_controller);
|
|
1121
|
-
return;
|
|
1122
|
-
}
|
|
1123
|
-
const g = c.event_id;
|
|
1124
|
-
if (!g)
|
|
1125
|
-
await Promise.all(
|
|
1126
|
-
Object.keys(e).map(
|
|
1127
|
-
(w) => e[w](c)
|
|
1128
|
-
)
|
|
1129
|
-
);
|
|
1130
|
-
else if (e[g] && o) {
|
|
1131
|
-
c.msg === "process_completed" && ["sse", "sse_v1", "sse_v2", "sse_v2.1", "sse_v3"].includes(
|
|
1132
|
-
o.protocol
|
|
1133
|
-
) && s.delete(g);
|
|
1134
|
-
let w = e[g];
|
|
1135
|
-
typeof window < "u" && typeof document < "u" ? setTimeout(w, 0, c) : w(c);
|
|
1136
|
-
} else
|
|
1137
|
-
t[g] || (t[g] = []), t[g].push(c);
|
|
1138
|
-
}, a.onerror = async function(p) {
|
|
1139
|
-
console.error(p), await Promise.all(
|
|
1140
|
-
Object.keys(e).map(
|
|
1141
|
-
(c) => e[c]({
|
|
1142
|
-
msg: "broken_connection",
|
|
1143
|
-
message: L
|
|
1144
|
-
})
|
|
1145
|
-
)
|
|
1146
|
-
);
|
|
1147
|
-
};
|
|
977
|
+
async function qe(e, t) {
|
|
978
|
+
let n = new Request(e, t);
|
|
979
|
+
Ge(n.headers, "Accept", "text/event-stream"), Ge(n.headers, "Content-Type", "application/json");
|
|
980
|
+
let r = await fetch(n);
|
|
981
|
+
if (!r.ok) throw r;
|
|
982
|
+
return Ke(r, n.signal);
|
|
1148
983
|
}
|
|
1149
|
-
|
|
1150
|
-
|
|
984
|
+
//#endregion
|
|
985
|
+
//#region src/utils/stream.ts
|
|
986
|
+
async function Je() {
|
|
987
|
+
let { event_callbacks: e, unclosed_events: t, pending_stream_messages: n, stream_status: r, config: i, jwt: a } = this, o = this;
|
|
988
|
+
if (!i) throw Error("Could not resolve app config");
|
|
989
|
+
r.open = !0;
|
|
990
|
+
let s = null, c = new URLSearchParams({ session_hash: this.session_hash }).toString(), l = new URL(`${i.root}${this.api_prefix}/${u}?${c}`);
|
|
991
|
+
if (a && l.searchParams.set("__sign", a), s = this.stream(l), !s) {
|
|
992
|
+
console.warn("Cannot connect to SSE endpoint: " + l.toString());
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
s.onmessage = async function(a) {
|
|
996
|
+
let s = JSON.parse(a.data);
|
|
997
|
+
if (s.msg === "close_stream") {
|
|
998
|
+
Q(r, o.abort_controller);
|
|
999
|
+
return;
|
|
1000
|
+
}
|
|
1001
|
+
let c = s.event_id;
|
|
1002
|
+
if (!c) await Promise.all(Object.keys(e).map((t) => e[t](s)));
|
|
1003
|
+
else if (e[c] && i) {
|
|
1004
|
+
s.msg === "process_completed" && [
|
|
1005
|
+
"sse",
|
|
1006
|
+
"sse_v1",
|
|
1007
|
+
"sse_v2",
|
|
1008
|
+
"sse_v2.1",
|
|
1009
|
+
"sse_v3"
|
|
1010
|
+
].includes(i.protocol) && t.delete(c);
|
|
1011
|
+
let n = e[c];
|
|
1012
|
+
typeof window < "u" && typeof document < "u" && document.visibilityState !== "hidden" ? setTimeout(n, 0, s) : n(s);
|
|
1013
|
+
} else n[c] || (n[c] = []), n[c].push(s);
|
|
1014
|
+
}, s.onerror = async function(t) {
|
|
1015
|
+
console.error(t), await Promise.all(Object.keys(e).map((t) => e[t]({
|
|
1016
|
+
msg: "broken_connection",
|
|
1017
|
+
message: b
|
|
1018
|
+
})));
|
|
1019
|
+
};
|
|
1151
1020
|
}
|
|
1152
|
-
function
|
|
1153
|
-
|
|
1154
|
-
e[s][r] = o;
|
|
1155
|
-
})) : t.data.forEach((o, r) => {
|
|
1156
|
-
let i = Vt(e[s][r], o);
|
|
1157
|
-
e[s][r] = i, t.data[r] = i;
|
|
1158
|
-
});
|
|
1021
|
+
function Q(e, t) {
|
|
1022
|
+
e && (e.open = !1, t?.abort());
|
|
1159
1023
|
}
|
|
1160
|
-
function
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1024
|
+
function Ye(e, t, n) {
|
|
1025
|
+
e[t] ? n.data.forEach((r, i) => {
|
|
1026
|
+
let a = Xe(i < e[t].length ? e[t][i] : null, r);
|
|
1027
|
+
e[t][i] = a, n.data[i] = a;
|
|
1028
|
+
}) : (e[t] = [], n.data.forEach((n, r) => {
|
|
1029
|
+
e[t][r] = n;
|
|
1030
|
+
}));
|
|
1164
1031
|
}
|
|
1165
|
-
function
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
if (t === "append")
|
|
1170
|
-
return e + n;
|
|
1171
|
-
throw new Error(`Unsupported action: ${t}`);
|
|
1172
|
-
}
|
|
1173
|
-
let o = e;
|
|
1174
|
-
for (let i = 0; i < s.length - 1; i++)
|
|
1175
|
-
o = o[s[i]];
|
|
1176
|
-
const r = s[s.length - 1];
|
|
1177
|
-
switch (t) {
|
|
1178
|
-
case "replace":
|
|
1179
|
-
o[r] = n;
|
|
1180
|
-
break;
|
|
1181
|
-
case "append":
|
|
1182
|
-
o[r] += n;
|
|
1183
|
-
break;
|
|
1184
|
-
case "add":
|
|
1185
|
-
Array.isArray(o) ? o.splice(Number(r), 0, n) : o[r] = n;
|
|
1186
|
-
break;
|
|
1187
|
-
case "delete":
|
|
1188
|
-
Array.isArray(o) ? o.splice(Number(r), 1) : delete o[r];
|
|
1189
|
-
break;
|
|
1190
|
-
default:
|
|
1191
|
-
throw new Error(`Unknown action: ${t}`);
|
|
1192
|
-
}
|
|
1193
|
-
return e;
|
|
1032
|
+
function Xe(e, t) {
|
|
1033
|
+
return t.forEach(([t, n, r]) => {
|
|
1034
|
+
e = Ze(e, n, t, r);
|
|
1035
|
+
}), e;
|
|
1194
1036
|
}
|
|
1195
|
-
function
|
|
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
|
-
t.readyState = t.OPEN;
|
|
1221
|
-
try {
|
|
1222
|
-
for await (const o of n)
|
|
1223
|
-
t.onmessage && t.onmessage(o);
|
|
1224
|
-
t.readyState = t.CLOSED;
|
|
1225
|
-
} catch (o) {
|
|
1226
|
-
t.onerror && t.onerror(o), t.readyState = t.CLOSED;
|
|
1227
|
-
}
|
|
1228
|
-
}).catch((n) => {
|
|
1229
|
-
console.error(n), t.onerror && t.onerror(n), t.readyState = t.CLOSED;
|
|
1230
|
-
}), t;
|
|
1037
|
+
function Ze(e, t, n, r) {
|
|
1038
|
+
if (t.length === 0) {
|
|
1039
|
+
if (n === "replace") return r;
|
|
1040
|
+
if (n === "append") return e + r;
|
|
1041
|
+
throw Error(`Unsupported action: ${n}`);
|
|
1042
|
+
}
|
|
1043
|
+
let i = e;
|
|
1044
|
+
for (let e = 0; e < t.length - 1; e++) i = i[t[e]];
|
|
1045
|
+
let a = t[t.length - 1];
|
|
1046
|
+
switch (n) {
|
|
1047
|
+
case "replace":
|
|
1048
|
+
i[a] = r;
|
|
1049
|
+
break;
|
|
1050
|
+
case "append":
|
|
1051
|
+
i[a] += r;
|
|
1052
|
+
break;
|
|
1053
|
+
case "add":
|
|
1054
|
+
Array.isArray(i) ? i.splice(Number(a), 0, r) : i[a] = r;
|
|
1055
|
+
break;
|
|
1056
|
+
case "delete":
|
|
1057
|
+
Array.isArray(i) ? i.splice(Number(a), 1) : delete i[a];
|
|
1058
|
+
break;
|
|
1059
|
+
default: throw Error(`Unknown action: ${n}`);
|
|
1060
|
+
}
|
|
1061
|
+
return e;
|
|
1231
1062
|
}
|
|
1232
|
-
function
|
|
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
|
-
if (!H) throw new Error("No API found");
|
|
1268
|
-
if (!f) throw new Error("Could not resolve app config");
|
|
1269
|
-
let { fn_index: d, endpoint_info: fe, dependency: B } = ts(
|
|
1270
|
-
H,
|
|
1271
|
-
e,
|
|
1272
|
-
z,
|
|
1273
|
-
f
|
|
1274
|
-
), Ge = bt(s, fe), j, A = f.protocol ?? "ws";
|
|
1275
|
-
if (A === "ws")
|
|
1276
|
-
throw new Error("WebSocket protocol is not supported in this version");
|
|
1277
|
-
let I = "", ss = () => I;
|
|
1278
|
-
const _ = typeof e == "number" ? "/predict" : e;
|
|
1279
|
-
let W, v = null, x = !1, _e = {}, G = typeof window < "u" && typeof document < "u" ? new URLSearchParams(window.location.search).toString() : "";
|
|
1280
|
-
const Me = J?.events?.reduce(
|
|
1281
|
-
(h, R) => (h[R] = !0, h),
|
|
1282
|
-
{}
|
|
1283
|
-
) || {};
|
|
1284
|
-
async function Fe() {
|
|
1285
|
-
let h = {}, R = {};
|
|
1286
|
-
h = { event_id: v }, R = { event_id: v, session_hash: m, fn_index: d };
|
|
1287
|
-
try {
|
|
1288
|
-
if (!f)
|
|
1289
|
-
throw new Error("Could not resolve app config");
|
|
1290
|
-
"event_id" in R && await w(`${f.root}${T}/${ot}`, {
|
|
1291
|
-
headers: { "Content-Type": "application/json" },
|
|
1292
|
-
method: "POST",
|
|
1293
|
-
body: JSON.stringify(R)
|
|
1294
|
-
}), await w(`${f.root}${T}/${nt}`, {
|
|
1295
|
-
headers: { "Content-Type": "application/json" },
|
|
1296
|
-
method: "POST",
|
|
1297
|
-
body: JSON.stringify(h)
|
|
1298
|
-
});
|
|
1299
|
-
} catch {
|
|
1300
|
-
console.warn(
|
|
1301
|
-
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
|
1302
|
-
);
|
|
1303
|
-
}
|
|
1304
|
-
}
|
|
1305
|
-
const He = async (h) => {
|
|
1306
|
-
await this._resolve_heartbeat(h);
|
|
1307
|
-
};
|
|
1308
|
-
async function ge(h) {
|
|
1309
|
-
if (!f) return;
|
|
1310
|
-
let R = h.render_id;
|
|
1311
|
-
f.components = [
|
|
1312
|
-
...f.components.filter((E) => E.props.rendered_in !== R),
|
|
1313
|
-
...h.components
|
|
1314
|
-
], f.dependencies = [
|
|
1315
|
-
...f.dependencies.filter((E) => E.rendered_in !== R),
|
|
1316
|
-
...h.dependencies
|
|
1317
|
-
];
|
|
1318
|
-
const Z = f.components.some((E) => E.type === "state"), y = f.dependencies.some(
|
|
1319
|
-
(E) => E.targets.some((U) => U[1] === "unload")
|
|
1320
|
-
);
|
|
1321
|
-
f.connect_heartbeat = Z || y, await He(f), i({
|
|
1322
|
-
type: "render",
|
|
1323
|
-
data: h,
|
|
1324
|
-
endpoint: _,
|
|
1325
|
-
fn_index: d
|
|
1326
|
-
});
|
|
1327
|
-
}
|
|
1328
|
-
const Je = this.handle_blob(
|
|
1329
|
-
f.root,
|
|
1330
|
-
Ge,
|
|
1331
|
-
fe
|
|
1332
|
-
).then(async (h) => {
|
|
1333
|
-
if (W = {
|
|
1334
|
-
data: V(
|
|
1335
|
-
h,
|
|
1336
|
-
B,
|
|
1337
|
-
f.components,
|
|
1338
|
-
"input",
|
|
1339
|
-
!0
|
|
1340
|
-
) || [],
|
|
1341
|
-
event_data: t,
|
|
1342
|
-
fn_index: d,
|
|
1343
|
-
trigger_id: n
|
|
1344
|
-
}, At(d, f))
|
|
1345
|
-
i({
|
|
1346
|
-
type: "status",
|
|
1347
|
-
endpoint: _,
|
|
1348
|
-
stage: "pending",
|
|
1349
|
-
queue: !1,
|
|
1350
|
-
fn_index: d,
|
|
1351
|
-
time: /* @__PURE__ */ new Date()
|
|
1352
|
-
}), ne(
|
|
1353
|
-
`${f.root}${T}/run${_.startsWith("/") ? _ : `/${_}`}${G ? "?" + G : ""}`,
|
|
1354
|
-
{
|
|
1355
|
-
...W,
|
|
1356
|
-
session_hash: m
|
|
1357
|
-
},
|
|
1358
|
-
he
|
|
1359
|
-
).then(async ([y, E]) => {
|
|
1360
|
-
const U = y.data;
|
|
1361
|
-
if (E == 200)
|
|
1362
|
-
i({
|
|
1363
|
-
type: "data",
|
|
1364
|
-
endpoint: _,
|
|
1365
|
-
fn_index: d,
|
|
1366
|
-
data: V(
|
|
1367
|
-
U,
|
|
1368
|
-
B,
|
|
1369
|
-
f.components,
|
|
1370
|
-
"output",
|
|
1371
|
-
J.with_null_state
|
|
1372
|
-
),
|
|
1373
|
-
time: /* @__PURE__ */ new Date(),
|
|
1374
|
-
event_data: t,
|
|
1375
|
-
trigger_id: n
|
|
1376
|
-
}), y.render_config && await ge(y.render_config), i({
|
|
1377
|
-
type: "status",
|
|
1378
|
-
endpoint: _,
|
|
1379
|
-
fn_index: d,
|
|
1380
|
-
stage: "complete",
|
|
1381
|
-
eta: y.average_duration,
|
|
1382
|
-
queue: !1,
|
|
1383
|
-
time: /* @__PURE__ */ new Date()
|
|
1384
|
-
});
|
|
1385
|
-
else {
|
|
1386
|
-
const O = y?.error === L;
|
|
1387
|
-
i({
|
|
1388
|
-
type: "status",
|
|
1389
|
-
stage: "error",
|
|
1390
|
-
endpoint: _,
|
|
1391
|
-
fn_index: d,
|
|
1392
|
-
message: y.error,
|
|
1393
|
-
broken: O,
|
|
1394
|
-
queue: !1,
|
|
1395
|
-
time: /* @__PURE__ */ new Date()
|
|
1396
|
-
});
|
|
1397
|
-
}
|
|
1398
|
-
}).catch((y) => {
|
|
1399
|
-
i({
|
|
1400
|
-
type: "status",
|
|
1401
|
-
stage: "error",
|
|
1402
|
-
message: y.message,
|
|
1403
|
-
endpoint: _,
|
|
1404
|
-
fn_index: d,
|
|
1405
|
-
queue: !1,
|
|
1406
|
-
time: /* @__PURE__ */ new Date()
|
|
1407
|
-
});
|
|
1408
|
-
});
|
|
1409
|
-
else if (A == "sse") {
|
|
1410
|
-
i({
|
|
1411
|
-
type: "status",
|
|
1412
|
-
stage: "pending",
|
|
1413
|
-
queue: !0,
|
|
1414
|
-
endpoint: _,
|
|
1415
|
-
fn_index: d,
|
|
1416
|
-
time: /* @__PURE__ */ new Date()
|
|
1417
|
-
});
|
|
1418
|
-
var Z = new URLSearchParams({
|
|
1419
|
-
fn_index: d.toString(),
|
|
1420
|
-
session_hash: m
|
|
1421
|
-
}).toString();
|
|
1422
|
-
let y = new URL(
|
|
1423
|
-
`${f.root}${T}/${xe}?${G ? G + "&" : ""}${Z}`
|
|
1424
|
-
);
|
|
1425
|
-
if (this.jwt && y.searchParams.set("__sign", this.jwt), j = this.stream(y), !j)
|
|
1426
|
-
return Promise.reject(
|
|
1427
|
-
new Error("Cannot connect to SSE endpoint: " + y.toString())
|
|
1428
|
-
);
|
|
1429
|
-
j.onmessage = async function(E) {
|
|
1430
|
-
const U = JSON.parse(E.data), { type: O, status: D, data: b } = Ee(
|
|
1431
|
-
U,
|
|
1432
|
-
_e[d]
|
|
1433
|
-
);
|
|
1434
|
-
if (O === "update" && D && !x)
|
|
1435
|
-
i({
|
|
1436
|
-
type: "status",
|
|
1437
|
-
endpoint: _,
|
|
1438
|
-
fn_index: d,
|
|
1439
|
-
time: /* @__PURE__ */ new Date(),
|
|
1440
|
-
...D
|
|
1441
|
-
}), D.stage === "error" && (j?.close(), a());
|
|
1442
|
-
else if (O === "data") {
|
|
1443
|
-
let [q, P] = await ne(
|
|
1444
|
-
`${f.root}${T}/queue/data`,
|
|
1445
|
-
{
|
|
1446
|
-
...W,
|
|
1447
|
-
session_hash: m,
|
|
1448
|
-
event_id: v
|
|
1449
|
-
}
|
|
1450
|
-
);
|
|
1451
|
-
P !== 200 && (i({
|
|
1452
|
-
type: "status",
|
|
1453
|
-
stage: "error",
|
|
1454
|
-
message: L,
|
|
1455
|
-
queue: !0,
|
|
1456
|
-
endpoint: _,
|
|
1457
|
-
fn_index: d,
|
|
1458
|
-
time: /* @__PURE__ */ new Date()
|
|
1459
|
-
}), j?.close(), a());
|
|
1460
|
-
} else O === "complete" ? x = D : O === "log" ? i({
|
|
1461
|
-
type: "log",
|
|
1462
|
-
title: b.title,
|
|
1463
|
-
log: b.log,
|
|
1464
|
-
level: b.level,
|
|
1465
|
-
endpoint: _,
|
|
1466
|
-
duration: b.duration,
|
|
1467
|
-
visible: b.visible,
|
|
1468
|
-
fn_index: d
|
|
1469
|
-
}) : (O === "generating" || O === "streaming") && i({
|
|
1470
|
-
type: "status",
|
|
1471
|
-
time: /* @__PURE__ */ new Date(),
|
|
1472
|
-
...D,
|
|
1473
|
-
stage: D?.stage,
|
|
1474
|
-
queue: !0,
|
|
1475
|
-
endpoint: _,
|
|
1476
|
-
fn_index: d
|
|
1477
|
-
});
|
|
1478
|
-
b && (i({
|
|
1479
|
-
type: "data",
|
|
1480
|
-
time: /* @__PURE__ */ new Date(),
|
|
1481
|
-
data: V(
|
|
1482
|
-
b.data,
|
|
1483
|
-
B,
|
|
1484
|
-
f.components,
|
|
1485
|
-
"output",
|
|
1486
|
-
J.with_null_state
|
|
1487
|
-
),
|
|
1488
|
-
endpoint: _,
|
|
1489
|
-
fn_index: d,
|
|
1490
|
-
event_data: t,
|
|
1491
|
-
trigger_id: n
|
|
1492
|
-
}), x && (i({
|
|
1493
|
-
type: "status",
|
|
1494
|
-
time: /* @__PURE__ */ new Date(),
|
|
1495
|
-
...x,
|
|
1496
|
-
stage: D?.stage,
|
|
1497
|
-
queue: !0,
|
|
1498
|
-
endpoint: _,
|
|
1499
|
-
fn_index: d
|
|
1500
|
-
}), j?.close(), a()));
|
|
1501
|
-
};
|
|
1502
|
-
} else if (A == "sse_v1" || A == "sse_v2" || A == "sse_v2.1" || A == "sse_v3") {
|
|
1503
|
-
i({
|
|
1504
|
-
type: "status",
|
|
1505
|
-
stage: "pending",
|
|
1506
|
-
queue: !0,
|
|
1507
|
-
endpoint: _,
|
|
1508
|
-
fn_index: d,
|
|
1509
|
-
time: /* @__PURE__ */ new Date()
|
|
1510
|
-
});
|
|
1511
|
-
let y = "";
|
|
1512
|
-
typeof window < "u" && typeof document < "u" && (y = window?.location?.hostname);
|
|
1513
|
-
const E = qe(y);
|
|
1514
|
-
return (typeof window < "u" && typeof document < "u" && window.parent != window && !!E && window.supports_zerogpu_headers ? xt("zerogpu-headers", E) : Promise.resolve(null)).then((b) => {
|
|
1515
|
-
const q = { ...he, ...b || {} };
|
|
1516
|
-
return ne(
|
|
1517
|
-
`${f.root}${T}/${Ve}?${G}`,
|
|
1518
|
-
{
|
|
1519
|
-
...W,
|
|
1520
|
-
session_hash: m
|
|
1521
|
-
},
|
|
1522
|
-
q
|
|
1523
|
-
);
|
|
1524
|
-
}).then(async ([b, q]) => {
|
|
1525
|
-
if (b.event_id && (I = b.event_id), q === 503)
|
|
1526
|
-
i({
|
|
1527
|
-
type: "status",
|
|
1528
|
-
stage: "error",
|
|
1529
|
-
message: De,
|
|
1530
|
-
queue: !0,
|
|
1531
|
-
endpoint: _,
|
|
1532
|
-
fn_index: d,
|
|
1533
|
-
time: /* @__PURE__ */ new Date(),
|
|
1534
|
-
visible: !0
|
|
1535
|
-
}), a();
|
|
1536
|
-
else if (q === 422)
|
|
1537
|
-
i({
|
|
1538
|
-
type: "status",
|
|
1539
|
-
stage: "error",
|
|
1540
|
-
message: b.detail,
|
|
1541
|
-
queue: !0,
|
|
1542
|
-
endpoint: _,
|
|
1543
|
-
fn_index: d,
|
|
1544
|
-
code: "validation_error",
|
|
1545
|
-
time: /* @__PURE__ */ new Date(),
|
|
1546
|
-
visible: !0
|
|
1547
|
-
}), a();
|
|
1548
|
-
else if (q !== 200) {
|
|
1549
|
-
const P = b?.error === L;
|
|
1550
|
-
i({
|
|
1551
|
-
type: "status",
|
|
1552
|
-
stage: "error",
|
|
1553
|
-
broken: P,
|
|
1554
|
-
message: P ? L : b.detail || b.error,
|
|
1555
|
-
queue: !0,
|
|
1556
|
-
endpoint: _,
|
|
1557
|
-
fn_index: d,
|
|
1558
|
-
time: /* @__PURE__ */ new Date(),
|
|
1559
|
-
visible: !0
|
|
1560
|
-
}), a();
|
|
1561
|
-
} else {
|
|
1562
|
-
v = b.event_id, I = v;
|
|
1563
|
-
let P = async function(ie) {
|
|
1564
|
-
try {
|
|
1565
|
-
const { type: S, status: $, data: k, original_msg: We } = Ee(
|
|
1566
|
-
ie,
|
|
1567
|
-
_e[d]
|
|
1568
|
-
);
|
|
1569
|
-
if (S == "heartbeat")
|
|
1570
|
-
return;
|
|
1571
|
-
if (S === "update" && $ && !x)
|
|
1572
|
-
i({
|
|
1573
|
-
type: "status",
|
|
1574
|
-
endpoint: _,
|
|
1575
|
-
fn_index: d,
|
|
1576
|
-
time: /* @__PURE__ */ new Date(),
|
|
1577
|
-
original_msg: We,
|
|
1578
|
-
...$
|
|
1579
|
-
});
|
|
1580
|
-
else if (S === "complete")
|
|
1581
|
-
x = $;
|
|
1582
|
-
else if (S == "unexpected_error" || S == "broken_connection") {
|
|
1583
|
-
console.error("Unexpected error", $?.message);
|
|
1584
|
-
const Ze = S === "broken_connection";
|
|
1585
|
-
i({
|
|
1586
|
-
type: "status",
|
|
1587
|
-
stage: "error",
|
|
1588
|
-
message: $?.message || "An Unexpected Error Occurred!",
|
|
1589
|
-
queue: !0,
|
|
1590
|
-
endpoint: _,
|
|
1591
|
-
broken: Ze,
|
|
1592
|
-
session_not_found: $?.session_not_found,
|
|
1593
|
-
fn_index: d,
|
|
1594
|
-
time: /* @__PURE__ */ new Date()
|
|
1595
|
-
});
|
|
1596
|
-
} else if (S === "log") {
|
|
1597
|
-
i({
|
|
1598
|
-
type: "log",
|
|
1599
|
-
title: k.title,
|
|
1600
|
-
log: k.log,
|
|
1601
|
-
level: k.level,
|
|
1602
|
-
endpoint: _,
|
|
1603
|
-
duration: k.duration,
|
|
1604
|
-
visible: k.visible,
|
|
1605
|
-
fn_index: d
|
|
1606
|
-
});
|
|
1607
|
-
return;
|
|
1608
|
-
} else (S === "generating" || S === "streaming") && (i({
|
|
1609
|
-
type: "status",
|
|
1610
|
-
time: /* @__PURE__ */ new Date(),
|
|
1611
|
-
...$,
|
|
1612
|
-
stage: $?.stage,
|
|
1613
|
-
queue: !0,
|
|
1614
|
-
endpoint: _,
|
|
1615
|
-
fn_index: d
|
|
1616
|
-
}), k && B.connection !== "stream" && ["sse_v2", "sse_v2.1", "sse_v3"].includes(A) && Kt(te, v, k));
|
|
1617
|
-
k && (i({
|
|
1618
|
-
type: "data",
|
|
1619
|
-
time: /* @__PURE__ */ new Date(),
|
|
1620
|
-
data: V(
|
|
1621
|
-
k.data,
|
|
1622
|
-
B,
|
|
1623
|
-
f.components,
|
|
1624
|
-
"output",
|
|
1625
|
-
J.with_null_state
|
|
1626
|
-
),
|
|
1627
|
-
endpoint: _,
|
|
1628
|
-
fn_index: d
|
|
1629
|
-
}), k.render_config && await ge(k.render_config), x && (i({
|
|
1630
|
-
type: "status",
|
|
1631
|
-
time: /* @__PURE__ */ new Date(),
|
|
1632
|
-
...x,
|
|
1633
|
-
stage: $?.stage,
|
|
1634
|
-
queue: !0,
|
|
1635
|
-
endpoint: _,
|
|
1636
|
-
fn_index: d
|
|
1637
|
-
}), a())), ($?.stage === "complete" || $?.stage === "error") && (se[v] && delete se[v], v in te && delete te[v], a());
|
|
1638
|
-
} catch (S) {
|
|
1639
|
-
console.error("Unexpected client exception", S), i({
|
|
1640
|
-
type: "status",
|
|
1641
|
-
stage: "error",
|
|
1642
|
-
message: "An Unexpected Error Occurred!",
|
|
1643
|
-
queue: !0,
|
|
1644
|
-
endpoint: _,
|
|
1645
|
-
fn_index: d,
|
|
1646
|
-
time: /* @__PURE__ */ new Date()
|
|
1647
|
-
}), ["sse_v2", "sse_v2.1", "sse_v3"].includes(A) && (de(X, Be.abort_controller), X.open = !1, a());
|
|
1648
|
-
}
|
|
1649
|
-
};
|
|
1650
|
-
v in ee && (ee[v].forEach((ie) => P(ie)), delete ee[v]), se[v] = P, ze.add(v), X.open || await this.open_stream();
|
|
1651
|
-
}
|
|
1652
|
-
});
|
|
1653
|
-
}
|
|
1654
|
-
});
|
|
1655
|
-
let me = !1;
|
|
1656
|
-
const oe = [], M = [], we = {
|
|
1657
|
-
[Symbol.asyncIterator]: () => we,
|
|
1658
|
-
next: c,
|
|
1659
|
-
throw: async (h) => (l(h), c()),
|
|
1660
|
-
return: async () => (a(), { value: void 0, done: !0 }),
|
|
1661
|
-
cancel: Fe,
|
|
1662
|
-
send_chunk: (h) => {
|
|
1663
|
-
this.post_data(`${f.root}${T}/stream/${I}`, {
|
|
1664
|
-
...h,
|
|
1665
|
-
session_hash: this.session_hash
|
|
1666
|
-
});
|
|
1667
|
-
},
|
|
1668
|
-
close_stream: () => {
|
|
1669
|
-
this.post_data(
|
|
1670
|
-
`${f.root}${T}/stream/${I}/close`,
|
|
1671
|
-
{}
|
|
1672
|
-
), a();
|
|
1673
|
-
},
|
|
1674
|
-
event_id: () => I,
|
|
1675
|
-
wait_for_id: async () => (await Je, v)
|
|
1676
|
-
};
|
|
1677
|
-
return we;
|
|
1678
|
-
} catch (i) {
|
|
1679
|
-
throw console.error("Submit function encountered an error:", i), i;
|
|
1680
|
-
}
|
|
1063
|
+
function Qe(e, t = {}) {
|
|
1064
|
+
let n = {
|
|
1065
|
+
close: () => {
|
|
1066
|
+
console.warn("Method not implemented.");
|
|
1067
|
+
},
|
|
1068
|
+
onerror: null,
|
|
1069
|
+
onmessage: null,
|
|
1070
|
+
onopen: null,
|
|
1071
|
+
readyState: 0,
|
|
1072
|
+
url: e.toString(),
|
|
1073
|
+
withCredentials: !1,
|
|
1074
|
+
CONNECTING: 0,
|
|
1075
|
+
OPEN: 1,
|
|
1076
|
+
CLOSED: 2,
|
|
1077
|
+
addEventListener: () => {
|
|
1078
|
+
throw Error("Method not implemented.");
|
|
1079
|
+
},
|
|
1080
|
+
dispatchEvent: () => {
|
|
1081
|
+
throw Error("Method not implemented.");
|
|
1082
|
+
},
|
|
1083
|
+
removeEventListener: () => {
|
|
1084
|
+
throw Error("Method not implemented.");
|
|
1085
|
+
}
|
|
1086
|
+
};
|
|
1087
|
+
return qe(e, t).then(async (e) => {
|
|
1088
|
+
n.readyState = n.OPEN;
|
|
1089
|
+
try {
|
|
1090
|
+
for await (let t of e) n.onmessage && n.onmessage(t);
|
|
1091
|
+
n.readyState = n.CLOSED;
|
|
1092
|
+
} catch (e) {
|
|
1093
|
+
n.onerror && n.onerror(e), n.readyState = n.CLOSED;
|
|
1094
|
+
}
|
|
1095
|
+
}).catch((e) => {
|
|
1096
|
+
console.error(e), n.onerror && n.onerror(e), n.readyState = n.CLOSED;
|
|
1097
|
+
}), n;
|
|
1681
1098
|
}
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1099
|
+
//#endregion
|
|
1100
|
+
//#region src/utils/submit.ts
|
|
1101
|
+
function $e(e, t = {}, n, r, i, a) {
|
|
1102
|
+
try {
|
|
1103
|
+
let { token: o } = this.options, { fetch: s, app_reference: c, config: l, session_hash: f, api_info: p, api_map: m, stream_status: h, pending_stream_messages: g, pending_diff_streams: _, event_callbacks: v, unclosed_events: ee, post_data: y, options: x, api_prefix: S } = this, C = a || { "x-gradio-user": "api" }, w = this;
|
|
1104
|
+
if (!p) throw Error(ae);
|
|
1105
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1106
|
+
let { fn_index: T, endpoint_info: E, dependency: D } = tt(p, e, m, l), ie = fe(t, E), O, k = l.protocol ?? "ws";
|
|
1107
|
+
if (k === "ws") throw Error(oe);
|
|
1108
|
+
let A = "", j = typeof e == "number" ? "/predict" : e, M, N = null, P = !1, F = {}, I = typeof window < "u" && typeof document < "u" ? new URLSearchParams(window.location.search).toString() : "", se = x?.events?.reduce((e, t) => (e[t] = !0, e), {}) || {};
|
|
1109
|
+
function L(e) {
|
|
1110
|
+
(i || se[e.type]) && ue(e);
|
|
1111
|
+
}
|
|
1112
|
+
async function ce() {
|
|
1113
|
+
let e = {}, t = {};
|
|
1114
|
+
e = { event_id: N }, t = {
|
|
1115
|
+
event_id: N,
|
|
1116
|
+
session_hash: f,
|
|
1117
|
+
fn_index: T
|
|
1118
|
+
};
|
|
1119
|
+
try {
|
|
1120
|
+
if (!l) throw Error("Could not resolve app config");
|
|
1121
|
+
"event_id" in t && await s(`${l.root}${S}/${ne}`, {
|
|
1122
|
+
headers: { "Content-Type": "application/json" },
|
|
1123
|
+
method: "POST",
|
|
1124
|
+
body: JSON.stringify(t)
|
|
1125
|
+
}), await s(`${l.root}${S}/${te}`, {
|
|
1126
|
+
headers: { "Content-Type": "application/json" },
|
|
1127
|
+
method: "POST",
|
|
1128
|
+
body: JSON.stringify(e)
|
|
1129
|
+
});
|
|
1130
|
+
} catch {
|
|
1131
|
+
console.warn("The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable.");
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
let R = async (e) => {
|
|
1135
|
+
await this._resolve_heartbeat(e);
|
|
1136
|
+
};
|
|
1137
|
+
async function z(e) {
|
|
1138
|
+
if (!l) return;
|
|
1139
|
+
let t = e.render_id;
|
|
1140
|
+
l.components = [...l.components.filter((e) => e.props.rendered_in !== t), ...e.components], l.dependencies = [...l.dependencies.filter((e) => e.rendered_in !== t), ...e.dependencies];
|
|
1141
|
+
let n = l.components.some((e) => e.type === "state"), r = l.dependencies.some((e) => e.targets.some((e) => e[1] === "unload"));
|
|
1142
|
+
l.connect_heartbeat = n || r, await R(l), L({
|
|
1143
|
+
type: "render",
|
|
1144
|
+
data: e,
|
|
1145
|
+
endpoint: j,
|
|
1146
|
+
fn_index: T
|
|
1147
|
+
});
|
|
1148
|
+
}
|
|
1149
|
+
let B = this.handle_blob(l.root, ie, E).then(async (e) => {
|
|
1150
|
+
if (M = {
|
|
1151
|
+
data: X(e, D, l.components, "input", !0) || [],
|
|
1152
|
+
event_data: n,
|
|
1153
|
+
fn_index: T,
|
|
1154
|
+
trigger_id: r,
|
|
1155
|
+
...x.oauth_token && E?.oauth_token ? { oauth_token: x.oauth_token } : {}
|
|
1156
|
+
}, be(T, l)) L({
|
|
1157
|
+
type: "status",
|
|
1158
|
+
endpoint: j,
|
|
1159
|
+
stage: "pending",
|
|
1160
|
+
queue: !1,
|
|
1161
|
+
fn_index: T,
|
|
1162
|
+
time: /* @__PURE__ */ new Date()
|
|
1163
|
+
}), y(`${l.root}${S}/run${j.startsWith("/") ? j : `/${j}`}${I ? "?" + I : ""}`, {
|
|
1164
|
+
...M,
|
|
1165
|
+
session_hash: f
|
|
1166
|
+
}, C).then(async ([e, t]) => {
|
|
1167
|
+
let i = e.data;
|
|
1168
|
+
if (t == 200) L({
|
|
1169
|
+
type: "data",
|
|
1170
|
+
endpoint: j,
|
|
1171
|
+
fn_index: T,
|
|
1172
|
+
data: X(i, D, l.components, "output", x.with_null_state),
|
|
1173
|
+
time: /* @__PURE__ */ new Date(),
|
|
1174
|
+
event_data: n,
|
|
1175
|
+
trigger_id: r
|
|
1176
|
+
}), e.render_config && await z(e.render_config), L({
|
|
1177
|
+
type: "status",
|
|
1178
|
+
endpoint: j,
|
|
1179
|
+
fn_index: T,
|
|
1180
|
+
stage: "complete",
|
|
1181
|
+
eta: e.average_duration,
|
|
1182
|
+
queue: !1,
|
|
1183
|
+
time: /* @__PURE__ */ new Date()
|
|
1184
|
+
});
|
|
1185
|
+
else {
|
|
1186
|
+
let t = e?.error === b;
|
|
1187
|
+
L({
|
|
1188
|
+
type: "status",
|
|
1189
|
+
stage: "error",
|
|
1190
|
+
endpoint: j,
|
|
1191
|
+
fn_index: T,
|
|
1192
|
+
message: e.error,
|
|
1193
|
+
broken: t,
|
|
1194
|
+
queue: !1,
|
|
1195
|
+
time: /* @__PURE__ */ new Date()
|
|
1196
|
+
});
|
|
1197
|
+
}
|
|
1198
|
+
}).catch((e) => {
|
|
1199
|
+
L({
|
|
1200
|
+
type: "status",
|
|
1201
|
+
stage: "error",
|
|
1202
|
+
message: e.message,
|
|
1203
|
+
endpoint: j,
|
|
1204
|
+
fn_index: T,
|
|
1205
|
+
queue: !1,
|
|
1206
|
+
time: /* @__PURE__ */ new Date()
|
|
1207
|
+
});
|
|
1208
|
+
});
|
|
1209
|
+
else if (k == "sse") {
|
|
1210
|
+
L({
|
|
1211
|
+
type: "status",
|
|
1212
|
+
stage: "pending",
|
|
1213
|
+
queue: !0,
|
|
1214
|
+
endpoint: j,
|
|
1215
|
+
fn_index: T,
|
|
1216
|
+
time: /* @__PURE__ */ new Date()
|
|
1217
|
+
});
|
|
1218
|
+
var t = new URLSearchParams({
|
|
1219
|
+
fn_index: T.toString(),
|
|
1220
|
+
session_hash: f
|
|
1221
|
+
}).toString();
|
|
1222
|
+
let e = new URL(`${l.root}${S}/${u}?${I ? I + "&" : ""}${t}`);
|
|
1223
|
+
if (this.jwt && e.searchParams.set("__sign", this.jwt), O = this.stream(e), !O) return Promise.reject(/* @__PURE__ */ Error("Cannot connect to SSE endpoint: " + e.toString()));
|
|
1224
|
+
O.onmessage = async function(e) {
|
|
1225
|
+
let { type: t, status: i, data: a } = de(JSON.parse(e.data), F[T]);
|
|
1226
|
+
if (t === "update" && i && !P) L({
|
|
1227
|
+
type: "status",
|
|
1228
|
+
endpoint: j,
|
|
1229
|
+
fn_index: T,
|
|
1230
|
+
time: /* @__PURE__ */ new Date(),
|
|
1231
|
+
...i
|
|
1232
|
+
}), i.stage === "error" && (O?.close(), W());
|
|
1233
|
+
else if (t === "data") {
|
|
1234
|
+
let [e, t] = await y(`${l.root}${S}/queue/data`, {
|
|
1235
|
+
...M,
|
|
1236
|
+
session_hash: f,
|
|
1237
|
+
event_id: N
|
|
1238
|
+
});
|
|
1239
|
+
t !== 200 && (L({
|
|
1240
|
+
type: "status",
|
|
1241
|
+
stage: "error",
|
|
1242
|
+
message: b,
|
|
1243
|
+
queue: !0,
|
|
1244
|
+
endpoint: j,
|
|
1245
|
+
fn_index: T,
|
|
1246
|
+
time: /* @__PURE__ */ new Date()
|
|
1247
|
+
}), O?.close(), W());
|
|
1248
|
+
} else t === "complete" ? P = i : t === "log" ? L({
|
|
1249
|
+
type: "log",
|
|
1250
|
+
title: a.title,
|
|
1251
|
+
log: a.log,
|
|
1252
|
+
level: a.level,
|
|
1253
|
+
endpoint: j,
|
|
1254
|
+
duration: a.duration,
|
|
1255
|
+
visible: a.visible,
|
|
1256
|
+
fn_index: T
|
|
1257
|
+
}) : (t === "generating" || t === "streaming") && L({
|
|
1258
|
+
type: "status",
|
|
1259
|
+
time: /* @__PURE__ */ new Date(),
|
|
1260
|
+
...i,
|
|
1261
|
+
stage: i?.stage,
|
|
1262
|
+
queue: !0,
|
|
1263
|
+
endpoint: j,
|
|
1264
|
+
fn_index: T
|
|
1265
|
+
});
|
|
1266
|
+
a && (L({
|
|
1267
|
+
type: "data",
|
|
1268
|
+
time: /* @__PURE__ */ new Date(),
|
|
1269
|
+
data: X(a.data, D, l.components, "output", x.with_null_state),
|
|
1270
|
+
endpoint: j,
|
|
1271
|
+
fn_index: T,
|
|
1272
|
+
event_data: n,
|
|
1273
|
+
trigger_id: r
|
|
1274
|
+
}), P && (L({
|
|
1275
|
+
type: "status",
|
|
1276
|
+
time: /* @__PURE__ */ new Date(),
|
|
1277
|
+
...P,
|
|
1278
|
+
stage: i?.stage,
|
|
1279
|
+
queue: !0,
|
|
1280
|
+
endpoint: j,
|
|
1281
|
+
fn_index: T
|
|
1282
|
+
}), O?.close(), W()));
|
|
1283
|
+
};
|
|
1284
|
+
} else if (k == "sse_v1" || k == "sse_v2" || k == "sse_v2.1" || k == "sse_v3") {
|
|
1285
|
+
L({
|
|
1286
|
+
type: "status",
|
|
1287
|
+
stage: "pending",
|
|
1288
|
+
queue: !0,
|
|
1289
|
+
endpoint: j,
|
|
1290
|
+
fn_index: T,
|
|
1291
|
+
time: /* @__PURE__ */ new Date()
|
|
1292
|
+
});
|
|
1293
|
+
let e = "";
|
|
1294
|
+
typeof window < "u" && typeof document < "u" && (e = window?.location?.hostname);
|
|
1295
|
+
let t = Be(e);
|
|
1296
|
+
return (typeof window < "u" && typeof document < "u" && window.parent != window && t && window.supports_zerogpu_headers ? xe("zerogpu-headers", t) : Promise.resolve(null)).then((e) => {
|
|
1297
|
+
let t = {
|
|
1298
|
+
...C,
|
|
1299
|
+
...e || {}
|
|
1300
|
+
};
|
|
1301
|
+
return y(`${l.root}${S}/${d}?${I}`, {
|
|
1302
|
+
...M,
|
|
1303
|
+
session_hash: f
|
|
1304
|
+
}, t);
|
|
1305
|
+
}).then(async ([e, t]) => {
|
|
1306
|
+
if (e.event_id && (A = e.event_id), t === 503) L({
|
|
1307
|
+
type: "status",
|
|
1308
|
+
stage: "error",
|
|
1309
|
+
message: re,
|
|
1310
|
+
queue: !0,
|
|
1311
|
+
endpoint: j,
|
|
1312
|
+
fn_index: T,
|
|
1313
|
+
time: /* @__PURE__ */ new Date(),
|
|
1314
|
+
visible: !0
|
|
1315
|
+
}), W();
|
|
1316
|
+
else if (t === 422) L({
|
|
1317
|
+
type: "status",
|
|
1318
|
+
stage: "error",
|
|
1319
|
+
message: e.detail,
|
|
1320
|
+
queue: !0,
|
|
1321
|
+
endpoint: j,
|
|
1322
|
+
fn_index: T,
|
|
1323
|
+
code: "validation_error",
|
|
1324
|
+
time: /* @__PURE__ */ new Date(),
|
|
1325
|
+
visible: !0
|
|
1326
|
+
}), W();
|
|
1327
|
+
else if (t !== 200) {
|
|
1328
|
+
let t = e?.error === b;
|
|
1329
|
+
L({
|
|
1330
|
+
type: "status",
|
|
1331
|
+
stage: "error",
|
|
1332
|
+
broken: t,
|
|
1333
|
+
message: t ? b : e.detail || e.error,
|
|
1334
|
+
queue: !0,
|
|
1335
|
+
endpoint: j,
|
|
1336
|
+
fn_index: T,
|
|
1337
|
+
time: /* @__PURE__ */ new Date(),
|
|
1338
|
+
visible: !0
|
|
1339
|
+
}), W();
|
|
1340
|
+
} else {
|
|
1341
|
+
N = e.event_id, A = N;
|
|
1342
|
+
let t = async function(e) {
|
|
1343
|
+
try {
|
|
1344
|
+
let { type: t, status: n, data: r, original_msg: i } = de(e, F[T]);
|
|
1345
|
+
if (t == "heartbeat") return;
|
|
1346
|
+
if (t === "update" && n && !P) L({
|
|
1347
|
+
type: "status",
|
|
1348
|
+
endpoint: j,
|
|
1349
|
+
fn_index: T,
|
|
1350
|
+
time: /* @__PURE__ */ new Date(),
|
|
1351
|
+
original_msg: i,
|
|
1352
|
+
...n
|
|
1353
|
+
});
|
|
1354
|
+
else if (t === "complete") P = n;
|
|
1355
|
+
else if (t == "unexpected_error" || t == "broken_connection") {
|
|
1356
|
+
console.error("Unexpected error", n?.message);
|
|
1357
|
+
let e = t === "broken_connection";
|
|
1358
|
+
L({
|
|
1359
|
+
type: "status",
|
|
1360
|
+
stage: "error",
|
|
1361
|
+
message: n?.message || "An Unexpected Error Occurred!",
|
|
1362
|
+
queue: !0,
|
|
1363
|
+
endpoint: j,
|
|
1364
|
+
broken: e,
|
|
1365
|
+
session_not_found: n?.session_not_found,
|
|
1366
|
+
fn_index: T,
|
|
1367
|
+
time: /* @__PURE__ */ new Date()
|
|
1368
|
+
});
|
|
1369
|
+
} else if (t === "log") {
|
|
1370
|
+
L({
|
|
1371
|
+
type: "log",
|
|
1372
|
+
title: r.title,
|
|
1373
|
+
log: r.log,
|
|
1374
|
+
level: r.level,
|
|
1375
|
+
endpoint: j,
|
|
1376
|
+
duration: r.duration,
|
|
1377
|
+
visible: r.visible,
|
|
1378
|
+
fn_index: T
|
|
1379
|
+
});
|
|
1380
|
+
return;
|
|
1381
|
+
} else (t === "generating" || t === "streaming") && (L({
|
|
1382
|
+
type: "status",
|
|
1383
|
+
time: /* @__PURE__ */ new Date(),
|
|
1384
|
+
...n,
|
|
1385
|
+
stage: n?.stage,
|
|
1386
|
+
queue: !0,
|
|
1387
|
+
endpoint: j,
|
|
1388
|
+
fn_index: T
|
|
1389
|
+
}), r && D.connection !== "stream" && [
|
|
1390
|
+
"sse_v2",
|
|
1391
|
+
"sse_v2.1",
|
|
1392
|
+
"sse_v3"
|
|
1393
|
+
].includes(k) && Ye(_, N, r));
|
|
1394
|
+
r && (L({
|
|
1395
|
+
type: "data",
|
|
1396
|
+
time: /* @__PURE__ */ new Date(),
|
|
1397
|
+
data: X(r.data, D, l.components, "output", x.with_null_state),
|
|
1398
|
+
endpoint: j,
|
|
1399
|
+
fn_index: T
|
|
1400
|
+
}), r.render_config && await z(r.render_config), P && (L({
|
|
1401
|
+
type: "status",
|
|
1402
|
+
time: /* @__PURE__ */ new Date(),
|
|
1403
|
+
...P,
|
|
1404
|
+
stage: n?.stage,
|
|
1405
|
+
queue: !0,
|
|
1406
|
+
endpoint: j,
|
|
1407
|
+
fn_index: T
|
|
1408
|
+
}), W())), (n?.stage === "complete" || n?.stage === "error") && (v[N] && delete v[N], N in _ && delete _[N], W());
|
|
1409
|
+
} catch (e) {
|
|
1410
|
+
console.error("Unexpected client exception", e), L({
|
|
1411
|
+
type: "status",
|
|
1412
|
+
stage: "error",
|
|
1413
|
+
message: "An Unexpected Error Occurred!",
|
|
1414
|
+
queue: !0,
|
|
1415
|
+
endpoint: j,
|
|
1416
|
+
fn_index: T,
|
|
1417
|
+
time: /* @__PURE__ */ new Date()
|
|
1418
|
+
}), [
|
|
1419
|
+
"sse_v2",
|
|
1420
|
+
"sse_v2.1",
|
|
1421
|
+
"sse_v3"
|
|
1422
|
+
].includes(k) && (Q(h, w.abort_controller), h.open = !1, W());
|
|
1423
|
+
}
|
|
1424
|
+
};
|
|
1425
|
+
N in g && (g[N].forEach((e) => t(e)), delete g[N]), v[N] = t, ee.add(N), h.open || await this.open_stream();
|
|
1426
|
+
}
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
B.catch((e) => {
|
|
1431
|
+
L({
|
|
1432
|
+
type: "status",
|
|
1433
|
+
stage: "error",
|
|
1434
|
+
message: e instanceof Error ? e.message : String(e),
|
|
1435
|
+
queue: !be(T, l),
|
|
1436
|
+
endpoint: j,
|
|
1437
|
+
fn_index: T,
|
|
1438
|
+
time: /* @__PURE__ */ new Date()
|
|
1439
|
+
}), W();
|
|
1440
|
+
});
|
|
1441
|
+
let V = !1, H = [], U = [];
|
|
1442
|
+
function W() {
|
|
1443
|
+
for (V = !0; U.length > 0;) U.shift()({
|
|
1444
|
+
value: void 0,
|
|
1445
|
+
done: !0
|
|
1446
|
+
});
|
|
1447
|
+
}
|
|
1448
|
+
function G(e) {
|
|
1449
|
+
U.length > 0 ? U.shift()(e) : H.push(e);
|
|
1450
|
+
}
|
|
1451
|
+
function le(e) {
|
|
1452
|
+
G(et(e)), W();
|
|
1453
|
+
}
|
|
1454
|
+
function ue(e) {
|
|
1455
|
+
G({
|
|
1456
|
+
value: e,
|
|
1457
|
+
done: !1
|
|
1458
|
+
});
|
|
1459
|
+
}
|
|
1460
|
+
function K() {
|
|
1461
|
+
return H.length > 0 ? Promise.resolve(H.shift()) : V ? Promise.resolve({
|
|
1462
|
+
value: void 0,
|
|
1463
|
+
done: !0
|
|
1464
|
+
}) : new Promise((e) => U.push(e));
|
|
1465
|
+
}
|
|
1466
|
+
let q = {
|
|
1467
|
+
[Symbol.asyncIterator]: () => q,
|
|
1468
|
+
next: K,
|
|
1469
|
+
throw: async (e) => (le(e), K()),
|
|
1470
|
+
return: async () => (W(), {
|
|
1471
|
+
value: void 0,
|
|
1472
|
+
done: !0
|
|
1473
|
+
}),
|
|
1474
|
+
cancel: ce,
|
|
1475
|
+
send_chunk: (e) => {
|
|
1476
|
+
this.post_data(`${l.root}${S}/stream/${A}`, {
|
|
1477
|
+
...e,
|
|
1478
|
+
session_hash: this.session_hash
|
|
1479
|
+
});
|
|
1480
|
+
},
|
|
1481
|
+
close_stream: () => {
|
|
1482
|
+
this.post_data(`${l.root}${S}/stream/${A}/close`, {}), W();
|
|
1483
|
+
},
|
|
1484
|
+
event_id: () => A,
|
|
1485
|
+
wait_for_id: async () => (await B, N)
|
|
1486
|
+
};
|
|
1487
|
+
return q;
|
|
1488
|
+
} catch (e) {
|
|
1489
|
+
throw console.error("Submit function encountered an error:", e), e;
|
|
1490
|
+
}
|
|
1686
1491
|
}
|
|
1687
|
-
function
|
|
1688
|
-
|
|
1689
|
-
if (typeof s == "number")
|
|
1690
|
-
o = s, r = e.unnamed_endpoints[o], i = n.dependencies.find((a) => a.id == s);
|
|
1691
|
-
else {
|
|
1692
|
-
const a = s.replace(/^\//, "");
|
|
1693
|
-
o = t[a], r = e.named_endpoints[s.trim()], i = n.dependencies.find(
|
|
1694
|
-
(u) => u.id == t[a]
|
|
1695
|
-
);
|
|
1696
|
-
}
|
|
1697
|
-
if (typeof o != "number")
|
|
1698
|
-
throw new Error(
|
|
1699
|
-
"There is no endpoint matching that name of fn_index matching that number."
|
|
1700
|
-
);
|
|
1701
|
-
return { fn_index: o, endpoint_info: r, dependency: i };
|
|
1492
|
+
function et(e) {
|
|
1493
|
+
return { then: (t, n) => n(e) };
|
|
1702
1494
|
}
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
pending_diff_streams = {};
|
|
1720
|
-
event_callbacks = {};
|
|
1721
|
-
unclosed_events = /* @__PURE__ */ new Set();
|
|
1722
|
-
heartbeat_event = null;
|
|
1723
|
-
abort_controller = null;
|
|
1724
|
-
stream_instance = null;
|
|
1725
|
-
current_payload;
|
|
1726
|
-
get_url_config(s = null) {
|
|
1727
|
-
if (!this.config)
|
|
1728
|
-
throw new Error(C);
|
|
1729
|
-
s === null && (s = window.location.href);
|
|
1730
|
-
const t = (i) => i.replace(/^\/+|\/+$/g, "");
|
|
1731
|
-
let n = t(new URL(this.config.root).pathname), o = t(new URL(s).pathname), r;
|
|
1732
|
-
return o.startsWith(n) ? r = t(o.substring(n.length)) : r = "", this.get_page_config(r);
|
|
1733
|
-
}
|
|
1734
|
-
get_page_config(s) {
|
|
1735
|
-
if (!this.config)
|
|
1736
|
-
throw new Error(C);
|
|
1737
|
-
let t = this.config;
|
|
1738
|
-
return s in t.page || (s = ""), {
|
|
1739
|
-
...t,
|
|
1740
|
-
current_page: s,
|
|
1741
|
-
layout: t.page[s].layout,
|
|
1742
|
-
components: t.components.filter(
|
|
1743
|
-
(n) => t.page[s].components.includes(n.id)
|
|
1744
|
-
),
|
|
1745
|
-
dependencies: this.config.dependencies.filter(
|
|
1746
|
-
(n) => t.page[s].dependencies.includes(n.id)
|
|
1747
|
-
)
|
|
1748
|
-
};
|
|
1749
|
-
}
|
|
1750
|
-
fetch(s, t) {
|
|
1751
|
-
const n = new Headers(t?.headers || {});
|
|
1752
|
-
return this && this.cookies && n.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((r, i) => {
|
|
1753
|
-
n.append(i, r);
|
|
1754
|
-
}), fetch(s, { ...t, headers: n });
|
|
1755
|
-
}
|
|
1756
|
-
stream(s) {
|
|
1757
|
-
const t = new Headers();
|
|
1758
|
-
return this && this.cookies && t.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((o, r) => {
|
|
1759
|
-
t.append(r, o);
|
|
1760
|
-
}), this && this.options.token && t.append("Authorization", `Bearer ${this.options.token}`), this.abort_controller = new AbortController(), this.stream_instance = Qt(s.toString(), {
|
|
1761
|
-
credentials: "include",
|
|
1762
|
-
headers: t,
|
|
1763
|
-
signal: this.abort_controller.signal
|
|
1764
|
-
}), this.stream_instance;
|
|
1765
|
-
}
|
|
1766
|
-
view_api;
|
|
1767
|
-
upload_files;
|
|
1768
|
-
upload;
|
|
1769
|
-
handle_blob;
|
|
1770
|
-
post_data;
|
|
1771
|
-
submit;
|
|
1772
|
-
predict;
|
|
1773
|
-
open_stream;
|
|
1774
|
-
resolve_config;
|
|
1775
|
-
resolve_cookies;
|
|
1776
|
-
constructor(s, t = { events: ["data"] }) {
|
|
1777
|
-
this.app_reference = s, this.deep_link = t.query_params?.deep_link || null, t.events || (t.events = ["data"]), this.options = t, this.current_payload = {}, t.cookies && (this.cookies = t.cookies), this.view_api = vt.bind(this), this.upload_files = Et.bind(this), this.handle_blob = Dt.bind(this), this.post_data = Nt.bind(this), this.submit = Xt.bind(this), this.predict = Pt.bind(this), this.open_stream = Zt.bind(this), this.resolve_config = ft.bind(this), this.resolve_cookies = _t.bind(this), this.upload = Ot.bind(this), this.fetch = this.fetch.bind(this), this.handle_space_success = this.handle_space_success.bind(this), this.stream = this.stream.bind(this);
|
|
1778
|
-
}
|
|
1779
|
-
async init() {
|
|
1780
|
-
Gt(), this.options.auth && await this.resolve_cookies(), await this._resolve_config().then(
|
|
1781
|
-
({ config: s }) => this._resolve_heartbeat(s)
|
|
1782
|
-
), this.api_info = await this.view_api(), this.api_map = ht(this.config?.dependencies || []);
|
|
1783
|
-
}
|
|
1784
|
-
async _resolve_heartbeat(s) {
|
|
1785
|
-
if (s && (this.config = s, this.api_prefix = s.api_prefix || "", this.config && this.config.connect_heartbeat && this.config.space_id && this.options.token && (this.jwt = await be(
|
|
1786
|
-
this.config.space_id,
|
|
1787
|
-
this.options.token,
|
|
1788
|
-
this.cookies
|
|
1789
|
-
))), s.space_id && this.options.token && (this.jwt = await be(s.space_id, this.options.token)), this.config && this.config.connect_heartbeat) {
|
|
1790
|
-
const t = new URL(
|
|
1791
|
-
`${this.config.root}${this.api_prefix}/${tt}/${this.session_hash}`
|
|
1792
|
-
);
|
|
1793
|
-
this.jwt && t.searchParams.set("__sign", this.jwt), this.heartbeat_event || (this.heartbeat_event = this.stream(t));
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
static async connect(s, t = {
|
|
1797
|
-
events: ["data"]
|
|
1798
|
-
}) {
|
|
1799
|
-
const n = new this(s, t);
|
|
1800
|
-
return t.session_hash && (n.session_hash = t.session_hash), await n.init(), n;
|
|
1801
|
-
}
|
|
1802
|
-
async reconnect() {
|
|
1803
|
-
const s = new URL(
|
|
1804
|
-
`${this.config.root}${this.api_prefix}/${it}`
|
|
1805
|
-
);
|
|
1806
|
-
let t;
|
|
1807
|
-
try {
|
|
1808
|
-
const n = await this.fetch(s);
|
|
1809
|
-
if (!n.ok)
|
|
1810
|
-
throw new Error();
|
|
1811
|
-
t = (await n.json()).app_id;
|
|
1812
|
-
} catch {
|
|
1813
|
-
return "broken";
|
|
1814
|
-
}
|
|
1815
|
-
return t !== this.config.app_id ? "changed" : "connected";
|
|
1816
|
-
}
|
|
1817
|
-
close() {
|
|
1818
|
-
this.closed = !0, de(this.stream_status, this.abort_controller);
|
|
1819
|
-
}
|
|
1820
|
-
set_current_payload(s) {
|
|
1821
|
-
this.current_payload = s;
|
|
1822
|
-
}
|
|
1823
|
-
static async duplicate(s, t = {
|
|
1824
|
-
events: ["data"]
|
|
1825
|
-
}) {
|
|
1826
|
-
return qt(s, t);
|
|
1827
|
-
}
|
|
1828
|
-
async _resolve_config() {
|
|
1829
|
-
const { http_protocol: s, host: t, space_id: n } = await pe(
|
|
1830
|
-
this.app_reference,
|
|
1831
|
-
this.options.token
|
|
1832
|
-
), { status_callback: o } = this.options;
|
|
1833
|
-
n && o && await Ue(n, o);
|
|
1834
|
-
let r;
|
|
1835
|
-
try {
|
|
1836
|
-
let i = `${s}//${t}`;
|
|
1837
|
-
if (r = await this.resolve_config(i), !r)
|
|
1838
|
-
throw new Error(C);
|
|
1839
|
-
return this.config_success(r);
|
|
1840
|
-
} catch (i) {
|
|
1841
|
-
if (n && o)
|
|
1842
|
-
F(
|
|
1843
|
-
n,
|
|
1844
|
-
le.test(n) ? "space_name" : "subdomain",
|
|
1845
|
-
this.handle_space_success
|
|
1846
|
-
);
|
|
1847
|
-
else
|
|
1848
|
-
throw o && o({
|
|
1849
|
-
status: "error",
|
|
1850
|
-
message: "Could not load this space.",
|
|
1851
|
-
load_status: "error",
|
|
1852
|
-
detail: "NOT_FOUND"
|
|
1853
|
-
}), Error(i);
|
|
1854
|
-
}
|
|
1855
|
-
}
|
|
1856
|
-
async config_success(s) {
|
|
1857
|
-
if (this.config = s, this.api_prefix = s.api_prefix || "", this.config.auth_required)
|
|
1858
|
-
return this.prepare_return_obj();
|
|
1859
|
-
try {
|
|
1860
|
-
this.api_info = await this.view_api();
|
|
1861
|
-
} catch (t) {
|
|
1862
|
-
console.error(at + t.message);
|
|
1863
|
-
}
|
|
1864
|
-
return this.prepare_return_obj();
|
|
1865
|
-
}
|
|
1866
|
-
async handle_space_success(s) {
|
|
1867
|
-
if (!this)
|
|
1868
|
-
throw new Error(C);
|
|
1869
|
-
const { status_callback: t } = this.options;
|
|
1870
|
-
if (t && t(s), s.status === "running")
|
|
1871
|
-
try {
|
|
1872
|
-
if (this.config = await this._resolve_config(), this.api_prefix = this?.config?.api_prefix || "", !this.config)
|
|
1873
|
-
throw new Error(C);
|
|
1874
|
-
return await this.config_success(this.config);
|
|
1875
|
-
} catch (n) {
|
|
1876
|
-
throw t && t({
|
|
1877
|
-
status: "error",
|
|
1878
|
-
message: "Could not load this space.",
|
|
1879
|
-
load_status: "error",
|
|
1880
|
-
detail: "NOT_FOUND"
|
|
1881
|
-
}), n;
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
|
-
async component_server(s, t, n) {
|
|
1885
|
-
if (!this.config)
|
|
1886
|
-
throw new Error(C);
|
|
1887
|
-
const o = {}, { token: r } = this.options, { session_hash: i } = this;
|
|
1888
|
-
r && (o.Authorization = `Bearer ${this.options.token}`);
|
|
1889
|
-
let a, u = this.config.components.find(
|
|
1890
|
-
(p) => p.id === s
|
|
1891
|
-
);
|
|
1892
|
-
u?.props?.root_url ? a = u.props.root_url : a = this.config.root;
|
|
1893
|
-
let l;
|
|
1894
|
-
if (typeof n == "object" && n !== null && "binary" in n) {
|
|
1895
|
-
const p = n;
|
|
1896
|
-
l = new FormData();
|
|
1897
|
-
for (const c in p.data)
|
|
1898
|
-
c !== "binary" && l.append(c, p.data[c]);
|
|
1899
|
-
l.set("component_id", s.toString()), l.set("fn_name", t), l.set("session_hash", i);
|
|
1900
|
-
} else
|
|
1901
|
-
l = JSON.stringify({
|
|
1902
|
-
data: n,
|
|
1903
|
-
component_id: s,
|
|
1904
|
-
fn_name: t,
|
|
1905
|
-
session_hash: i
|
|
1906
|
-
}), o["Content-Type"] = "application/json";
|
|
1907
|
-
r && (o.Authorization = `Bearer ${r}`);
|
|
1908
|
-
try {
|
|
1909
|
-
const p = await this.fetch(
|
|
1910
|
-
`${a}${this.api_prefix}/${st}/`,
|
|
1911
|
-
{
|
|
1912
|
-
method: "POST",
|
|
1913
|
-
body: l,
|
|
1914
|
-
headers: o,
|
|
1915
|
-
credentials: "include"
|
|
1916
|
-
}
|
|
1917
|
-
);
|
|
1918
|
-
if (!p.ok)
|
|
1919
|
-
throw new Error(
|
|
1920
|
-
"Could not connect to component server: " + p.statusText
|
|
1921
|
-
);
|
|
1922
|
-
return await p.json();
|
|
1923
|
-
} catch (p) {
|
|
1924
|
-
console.warn(p);
|
|
1925
|
-
}
|
|
1926
|
-
}
|
|
1927
|
-
set_cookies(s) {
|
|
1928
|
-
this.cookies = Pe(s).join("; ");
|
|
1929
|
-
}
|
|
1930
|
-
prepare_return_obj() {
|
|
1931
|
-
return {
|
|
1932
|
-
config: this.config,
|
|
1933
|
-
predict: this.predict,
|
|
1934
|
-
submit: this.submit,
|
|
1935
|
-
view_api: this.view_api,
|
|
1936
|
-
component_server: this.component_server
|
|
1937
|
-
};
|
|
1938
|
-
}
|
|
1495
|
+
function tt(e, t, n, r) {
|
|
1496
|
+
let i, a, o;
|
|
1497
|
+
if (typeof t == "number") i = t, a = e.unnamed_endpoints[i], o = r.dependencies.find((e) => e.id == t);
|
|
1498
|
+
else {
|
|
1499
|
+
let s = t.replace(/^\//, "");
|
|
1500
|
+
i = n[s], a = e.named_endpoints[t.trim()] ?? e.named_endpoints[`/${s}`], o = r.dependencies.find((e) => e.id == n[s]);
|
|
1501
|
+
}
|
|
1502
|
+
if (typeof i != "number" || !o) {
|
|
1503
|
+
let e = r.dependencies.filter((e) => e.api_name).map((e) => `"/${e.api_name}"`).join(", ");
|
|
1504
|
+
throw Error(`No endpoint matching ${JSON.stringify(t)} was found. ` + (e ? `Valid named endpoints are: ${e}. ` : "This app exposes no named endpoints. ") + "An fn_index (number) of an existing dependency can also be used.");
|
|
1505
|
+
}
|
|
1506
|
+
return {
|
|
1507
|
+
fn_index: i,
|
|
1508
|
+
endpoint_info: a,
|
|
1509
|
+
dependency: o
|
|
1510
|
+
};
|
|
1939
1511
|
}
|
|
1940
|
-
|
|
1941
|
-
|
|
1942
|
-
|
|
1943
|
-
|
|
1512
|
+
//#endregion
|
|
1513
|
+
//#region src/client.ts
|
|
1514
|
+
var $ = class {
|
|
1515
|
+
app_reference;
|
|
1516
|
+
options;
|
|
1517
|
+
deep_link = null;
|
|
1518
|
+
config;
|
|
1519
|
+
api_prefix = "";
|
|
1520
|
+
api_info;
|
|
1521
|
+
api_map = {};
|
|
1522
|
+
session_hash = Math.random().toString(36).substring(2);
|
|
1523
|
+
jwt = !1;
|
|
1524
|
+
last_status = {};
|
|
1525
|
+
cookies = null;
|
|
1526
|
+
stream_status = { open: !1 };
|
|
1527
|
+
closed = !1;
|
|
1528
|
+
pending_stream_messages = {};
|
|
1529
|
+
pending_diff_streams = {};
|
|
1530
|
+
event_callbacks = {};
|
|
1531
|
+
unclosed_events = /* @__PURE__ */ new Set();
|
|
1532
|
+
heartbeat_event = null;
|
|
1533
|
+
abort_controller = null;
|
|
1534
|
+
stream_instance = null;
|
|
1535
|
+
current_payload;
|
|
1536
|
+
get_url_config(e = null) {
|
|
1537
|
+
if (!this.config) throw Error(x);
|
|
1538
|
+
e === null && (e = window.location.href);
|
|
1539
|
+
let t = (e) => e.replace(/^\/+|\/+$/g, ""), n = t(new URL(this.config.root).pathname), r = t(new URL(e).pathname), i;
|
|
1540
|
+
return i = r.startsWith(n) ? t(r.substring(n.length)) : "", this.get_page_config(i);
|
|
1541
|
+
}
|
|
1542
|
+
get_page_config(e) {
|
|
1543
|
+
if (!this.config) throw Error(x);
|
|
1544
|
+
let t = this.config;
|
|
1545
|
+
return e in t.page || (e = ""), {
|
|
1546
|
+
...t,
|
|
1547
|
+
current_page: e,
|
|
1548
|
+
layout: t.page[e].layout,
|
|
1549
|
+
components: t.components.filter((n) => t.page[e].components.includes(n.id)),
|
|
1550
|
+
dependencies: this.config.dependencies.filter((n) => t.page[e].dependencies.includes(n.id))
|
|
1551
|
+
};
|
|
1552
|
+
}
|
|
1553
|
+
fetch(e, t) {
|
|
1554
|
+
let n = new Headers(t?.headers || {});
|
|
1555
|
+
return this && this.cookies && n.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, t) => {
|
|
1556
|
+
n.append(t, e);
|
|
1557
|
+
}), fetch(e, {
|
|
1558
|
+
...t,
|
|
1559
|
+
headers: n
|
|
1560
|
+
});
|
|
1561
|
+
}
|
|
1562
|
+
stream(e) {
|
|
1563
|
+
let t = new Headers();
|
|
1564
|
+
return this && this.cookies && t.append("Cookie", this.cookies), this && this.options.headers && new Headers(this.options.headers).forEach((e, n) => {
|
|
1565
|
+
t.append(n, e);
|
|
1566
|
+
}), this && this.options.token && t.append("Authorization", `Bearer ${this.options.token}`), this.abort_controller = new AbortController(), this.stream_instance = Qe(e.toString(), {
|
|
1567
|
+
credentials: this.options.credentials ?? "same-origin",
|
|
1568
|
+
headers: t,
|
|
1569
|
+
signal: this.abort_controller.signal
|
|
1570
|
+
}), this.stream_instance;
|
|
1571
|
+
}
|
|
1572
|
+
view_api;
|
|
1573
|
+
upload_files;
|
|
1574
|
+
upload;
|
|
1575
|
+
handle_blob;
|
|
1576
|
+
post_data;
|
|
1577
|
+
submit;
|
|
1578
|
+
predict;
|
|
1579
|
+
open_stream;
|
|
1580
|
+
resolve_config;
|
|
1581
|
+
resolve_cookies;
|
|
1582
|
+
constructor(e, t = { events: ["data"] }) {
|
|
1583
|
+
this.app_reference = e, this.deep_link = t.query_params?.deep_link || null, t.events ||= ["data"], P(t), this.options = t, this.current_payload = {}, t.cookies && (this.cookies = t.cookies), this.view_api = K.bind(this), this.upload_files = q.bind(this), this.handle_blob = Ce.bind(this), this.post_data = De.bind(this), this.submit = $e.bind(this), this.predict = Oe.bind(this), this.open_stream = Je.bind(this), this.resolve_config = se.bind(this), this.resolve_cookies = ce.bind(this), this.upload = he.bind(this), this.fetch = this.fetch.bind(this), this.handle_space_success = this.handle_space_success.bind(this), this.stream = this.stream.bind(this);
|
|
1584
|
+
}
|
|
1585
|
+
async init() {
|
|
1586
|
+
Ve(), this.options.auth && await this.resolve_cookies(), await this._resolve_config().then((e) => e?.config && this._resolve_heartbeat(e.config));
|
|
1587
|
+
try {
|
|
1588
|
+
this.api_info = await this.view_api();
|
|
1589
|
+
} catch (e) {
|
|
1590
|
+
console.error(e.message);
|
|
1591
|
+
}
|
|
1592
|
+
this.api_map = F(this.config?.dependencies || []);
|
|
1593
|
+
}
|
|
1594
|
+
async _resolve_heartbeat(e) {
|
|
1595
|
+
if (e && (this.config = e, this.api_prefix = e.api_prefix || "", this.config && this.config.connect_heartbeat && this.config.space_id && this.options.token && (this.jwt = await N(this.config.space_id, this.options.token, this.cookies))), e.space_id && this.options.token && (this.jwt = await N(e.space_id, this.options.token)), this.config && this.config.connect_heartbeat) {
|
|
1596
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${v}/${this.session_hash}`);
|
|
1597
|
+
this.jwt && e.searchParams.set("__sign", this.jwt), this.heartbeat_event ||= this.stream(e);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
static async connect(e, t = { events: ["data"] }) {
|
|
1601
|
+
let n = new this(e, t);
|
|
1602
|
+
return t.session_hash && (n.session_hash = t.session_hash), await n.init(), n;
|
|
1603
|
+
}
|
|
1604
|
+
async reconnect() {
|
|
1605
|
+
let e = new URL(`${this.config.root}${this.api_prefix}/${y}`), t;
|
|
1606
|
+
try {
|
|
1607
|
+
let n = await this.fetch(e);
|
|
1608
|
+
if (!n.ok) throw Error();
|
|
1609
|
+
t = (await n.json()).app_id;
|
|
1610
|
+
} catch {
|
|
1611
|
+
return "broken";
|
|
1612
|
+
}
|
|
1613
|
+
return t === this.config.app_id ? "connected" : "changed";
|
|
1614
|
+
}
|
|
1615
|
+
close() {
|
|
1616
|
+
this.closed = !0, Q(this.stream_status, this.abort_controller);
|
|
1617
|
+
}
|
|
1618
|
+
async refresh() {
|
|
1619
|
+
if (!this.config) throw Error(x);
|
|
1620
|
+
let e = await this.resolve_config(this.config.root, !1);
|
|
1621
|
+
if (!e) throw Error(x);
|
|
1622
|
+
this.config = e, this.api_prefix = e.api_prefix || "", this.api_map = F(e.dependencies || []);
|
|
1623
|
+
try {
|
|
1624
|
+
this.api_info = await this.view_api();
|
|
1625
|
+
} catch (e) {
|
|
1626
|
+
console.error(C + e.message);
|
|
1627
|
+
}
|
|
1628
|
+
return this.get_url_config();
|
|
1629
|
+
}
|
|
1630
|
+
set_current_payload(e) {
|
|
1631
|
+
this.current_payload = e;
|
|
1632
|
+
}
|
|
1633
|
+
static async duplicate(e, t = { events: ["data"] }) {
|
|
1634
|
+
return Fe(e, t);
|
|
1635
|
+
}
|
|
1636
|
+
async _resolve_config() {
|
|
1637
|
+
let { http_protocol: e, host: t, space_id: n } = await U(this.app_reference, this.options.token), { status_callback: r } = this.options;
|
|
1638
|
+
n && r && await ke(n, r);
|
|
1639
|
+
let i;
|
|
1640
|
+
try {
|
|
1641
|
+
let n = `${e}//${t}`;
|
|
1642
|
+
if (i = await this.resolve_config(n), !i) throw Error(x);
|
|
1643
|
+
return this.config_success(i);
|
|
1644
|
+
} catch (e) {
|
|
1645
|
+
if (n && r) Z(n, V.test(n) ? "space_name" : "subdomain", this.handle_space_success);
|
|
1646
|
+
else throw r && r({
|
|
1647
|
+
status: "error",
|
|
1648
|
+
message: "Could not load this space.",
|
|
1649
|
+
load_status: "error",
|
|
1650
|
+
detail: "NOT_FOUND"
|
|
1651
|
+
}), e instanceof Error ? e : Error(String(e));
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1654
|
+
async config_success(e) {
|
|
1655
|
+
if (this.config = e, this.api_prefix = e.api_prefix || "", this.config.auth_required) return this.prepare_return_obj();
|
|
1656
|
+
try {
|
|
1657
|
+
this.api_info = await this.view_api();
|
|
1658
|
+
} catch (e) {
|
|
1659
|
+
console.error(C + e.message);
|
|
1660
|
+
}
|
|
1661
|
+
return this.prepare_return_obj();
|
|
1662
|
+
}
|
|
1663
|
+
async handle_space_success(e) {
|
|
1664
|
+
if (!this) throw Error(x);
|
|
1665
|
+
let { status_callback: t } = this.options;
|
|
1666
|
+
if (t && t(e), e.status === "running") try {
|
|
1667
|
+
if (this.config = await this._resolve_config(), this.api_prefix = this?.config?.api_prefix || "", !this.config) throw Error(x);
|
|
1668
|
+
return await this.config_success(this.config);
|
|
1669
|
+
} catch (e) {
|
|
1670
|
+
throw t && t({
|
|
1671
|
+
status: "error",
|
|
1672
|
+
message: "Could not load this space.",
|
|
1673
|
+
load_status: "error",
|
|
1674
|
+
detail: "NOT_FOUND"
|
|
1675
|
+
}), e;
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
async component_server(e, t, n) {
|
|
1679
|
+
if (!this.config) throw Error(x);
|
|
1680
|
+
let r = {}, { token: i } = this.options, { session_hash: a } = this;
|
|
1681
|
+
i && (r.Authorization = `Bearer ${this.options.token}`);
|
|
1682
|
+
let o, s = this.config.components.find((t) => t.id === e);
|
|
1683
|
+
o = s?.props?.root_url ? s.props.root_url : this.config.root;
|
|
1684
|
+
let c;
|
|
1685
|
+
if (typeof n == "object" && n && "binary" in n) {
|
|
1686
|
+
let r = n;
|
|
1687
|
+
c = new FormData();
|
|
1688
|
+
for (let e in r.data) e !== "binary" && c.append(e, r.data[e]);
|
|
1689
|
+
c.set("component_id", e.toString()), c.set("fn_name", t), c.set("session_hash", a);
|
|
1690
|
+
} else c = JSON.stringify({
|
|
1691
|
+
data: n,
|
|
1692
|
+
component_id: e,
|
|
1693
|
+
fn_name: t,
|
|
1694
|
+
session_hash: a
|
|
1695
|
+
}), r["Content-Type"] = "application/json";
|
|
1696
|
+
i && (r.Authorization = `Bearer ${i}`);
|
|
1697
|
+
try {
|
|
1698
|
+
let e = await this.fetch(`${o}${this.api_prefix}/${ee}/`, {
|
|
1699
|
+
method: "POST",
|
|
1700
|
+
body: c,
|
|
1701
|
+
headers: r,
|
|
1702
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
1703
|
+
});
|
|
1704
|
+
if (!e.ok) throw Error("Could not connect to component server: " + e.statusText);
|
|
1705
|
+
return await e.json();
|
|
1706
|
+
} catch (e) {
|
|
1707
|
+
console.warn(e);
|
|
1708
|
+
}
|
|
1709
|
+
}
|
|
1710
|
+
set_cookies(e) {
|
|
1711
|
+
this.cookies = B(e).join("; ");
|
|
1712
|
+
}
|
|
1713
|
+
prepare_return_obj() {
|
|
1714
|
+
return {
|
|
1715
|
+
config: this.config,
|
|
1716
|
+
predict: this.predict,
|
|
1717
|
+
submit: this.submit,
|
|
1718
|
+
view_api: this.view_api,
|
|
1719
|
+
component_server: this.component_server
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
};
|
|
1723
|
+
async function nt(e, t = { events: ["data"] }) {
|
|
1724
|
+
return await $.connect(e, t);
|
|
1944
1725
|
}
|
|
1945
|
-
async function
|
|
1946
|
-
|
|
1726
|
+
async function rt(e, t) {
|
|
1727
|
+
return await $.duplicate(e, t);
|
|
1947
1728
|
}
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
Q as FileData,
|
|
1951
|
-
lt as MISSING_CREDENTIALS_MSG,
|
|
1952
|
-
is as client,
|
|
1953
|
-
rs as duplicate,
|
|
1954
|
-
os as handle_file,
|
|
1955
|
-
Pt as predict,
|
|
1956
|
-
ns as prepare_files,
|
|
1957
|
-
Xt as submit,
|
|
1958
|
-
Ot as upload,
|
|
1959
|
-
Et as upload_files
|
|
1960
|
-
};
|
|
1729
|
+
//#endregion
|
|
1730
|
+
export { $ as Client, J as FileData, ie as MISSING_CREDENTIALS_MSG, nt as client, rt as duplicate, Se as handle_file, Oe as predict, ge as prepare_files, $e as submit, o as t, he as upload, q as upload_files };
|