@gradio/client 0.15.1 → 0.17.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 +51 -0
- package/README.md +49 -43
- package/dist/client.d.ts +52 -70
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +17 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/helpers/api_info.d.ts +25 -0
- package/dist/helpers/api_info.d.ts.map +1 -0
- package/dist/helpers/data.d.ts +8 -0
- package/dist/helpers/data.d.ts.map +1 -0
- package/dist/{utils.d.ts → helpers/init_helpers.d.ts} +6 -17
- package/dist/helpers/init_helpers.d.ts.map +1 -0
- package/dist/helpers/spaces.d.ts +7 -0
- package/dist/helpers/spaces.d.ts.map +1 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1587 -1359
- package/dist/types.d.ts +152 -49
- package/dist/types.d.ts.map +1 -1
- package/dist/upload.d.ts +2 -2
- package/dist/upload.d.ts.map +1 -1
- package/dist/utils/duplicate.d.ts +4 -0
- package/dist/utils/duplicate.d.ts.map +1 -0
- package/dist/utils/handle_blob.d.ts +4 -0
- package/dist/utils/handle_blob.d.ts.map +1 -0
- package/dist/utils/post_data.d.ts +4 -0
- package/dist/utils/post_data.d.ts.map +1 -0
- package/dist/utils/predict.d.ts +4 -0
- package/dist/utils/predict.d.ts.map +1 -0
- package/dist/utils/stream.d.ts +8 -0
- package/dist/utils/stream.d.ts.map +1 -0
- package/dist/utils/submit.d.ts +4 -0
- package/dist/utils/submit.d.ts.map +1 -0
- package/dist/utils/upload_files.d.ts +4 -0
- package/dist/utils/upload_files.d.ts.map +1 -0
- package/dist/utils/view_api.d.ts +3 -0
- package/dist/utils/view_api.d.ts.map +1 -0
- package/dist/{wrapper-6f348d45.js → wrapper-CviSselG.js} +259 -17
- package/package.json +3 -2
- package/src/client.ts +290 -1652
- package/src/constants.ts +20 -0
- package/src/globals.d.ts +2 -21
- package/src/helpers/api_info.ts +300 -0
- package/src/helpers/data.ts +115 -0
- package/src/helpers/init_helpers.ts +134 -0
- package/src/helpers/spaces.ts +192 -0
- package/src/index.ts +16 -10
- package/src/types.ts +200 -59
- package/src/upload.ts +23 -15
- package/src/{client.node-test.ts → utils/client.node-test.ts} +11 -10
- package/src/utils/duplicate.ts +87 -0
- package/src/utils/handle_blob.ts +47 -0
- package/src/utils/post_data.ts +37 -0
- package/src/utils/predict.ts +56 -0
- package/src/utils/stream.ts +166 -0
- package/src/utils/submit.ts +689 -0
- package/src/utils/upload_files.ts +46 -0
- package/src/utils/view_api.ts +70 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +15 -2
- package/vite.config.js +4 -17
- package/dist/utils.d.ts.map +0 -1
- package/src/utils.ts +0 -300
@@ -0,0 +1,689 @@
|
|
1
|
+
/* eslint-disable complexity */
|
2
|
+
import type {
|
3
|
+
Status,
|
4
|
+
Payload,
|
5
|
+
EventType,
|
6
|
+
ListenerMap,
|
7
|
+
SubmitReturn,
|
8
|
+
EventListener,
|
9
|
+
Event,
|
10
|
+
JsApiData,
|
11
|
+
EndpointInfo,
|
12
|
+
ApiInfo,
|
13
|
+
Config,
|
14
|
+
Dependency
|
15
|
+
} from "../types";
|
16
|
+
|
17
|
+
import { skip_queue, post_message } from "../helpers/data";
|
18
|
+
import { resolve_root } from "../helpers/init_helpers";
|
19
|
+
import { handle_message, process_endpoint } from "../helpers/api_info";
|
20
|
+
import { BROKEN_CONNECTION_MSG, QUEUE_FULL_MSG } from "../constants";
|
21
|
+
import { apply_diff_stream, close_stream } from "./stream";
|
22
|
+
import { Client } from "../client";
|
23
|
+
|
24
|
+
export function submit(
|
25
|
+
this: Client,
|
26
|
+
endpoint: string | number,
|
27
|
+
data: unknown[],
|
28
|
+
event_data?: unknown,
|
29
|
+
trigger_id?: number | null
|
30
|
+
): SubmitReturn {
|
31
|
+
try {
|
32
|
+
const { hf_token } = this.options;
|
33
|
+
const {
|
34
|
+
fetch_implementation,
|
35
|
+
app_reference,
|
36
|
+
config,
|
37
|
+
session_hash,
|
38
|
+
api_info,
|
39
|
+
api_map,
|
40
|
+
stream_status,
|
41
|
+
pending_stream_messages,
|
42
|
+
pending_diff_streams,
|
43
|
+
event_callbacks,
|
44
|
+
unclosed_events,
|
45
|
+
post_data
|
46
|
+
} = this;
|
47
|
+
|
48
|
+
if (!api_info) throw new Error("No API found");
|
49
|
+
if (!config) throw new Error("Could not resolve app config");
|
50
|
+
|
51
|
+
let { fn_index, endpoint_info, dependency } = get_endpoint_info(
|
52
|
+
api_info,
|
53
|
+
endpoint,
|
54
|
+
api_map,
|
55
|
+
config
|
56
|
+
);
|
57
|
+
|
58
|
+
let websocket: WebSocket;
|
59
|
+
let event_source: EventSource | null;
|
60
|
+
let protocol = config.protocol ?? "ws";
|
61
|
+
|
62
|
+
const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
|
63
|
+
let payload: Payload;
|
64
|
+
let event_id: string | null = null;
|
65
|
+
let complete: Status | undefined | false = false;
|
66
|
+
const listener_map: ListenerMap<EventType> = {};
|
67
|
+
let last_status: Record<string, Status["stage"]> = {};
|
68
|
+
let url_params =
|
69
|
+
typeof window !== "undefined"
|
70
|
+
? new URLSearchParams(window.location.search).toString()
|
71
|
+
: "";
|
72
|
+
|
73
|
+
// event subscription methods
|
74
|
+
function fire_event<K extends EventType>(event: Event<K>): void {
|
75
|
+
const narrowed_listener_map: ListenerMap<K> = listener_map;
|
76
|
+
const listeners = narrowed_listener_map[event.type] || [];
|
77
|
+
listeners?.forEach((l) => l(event));
|
78
|
+
}
|
79
|
+
|
80
|
+
function on<K extends EventType>(
|
81
|
+
eventType: K,
|
82
|
+
listener: EventListener<K>
|
83
|
+
): SubmitReturn {
|
84
|
+
const narrowed_listener_map: ListenerMap<K> = listener_map;
|
85
|
+
const listeners = narrowed_listener_map[eventType] || [];
|
86
|
+
narrowed_listener_map[eventType] = listeners;
|
87
|
+
listeners?.push(listener);
|
88
|
+
|
89
|
+
return { on, off, cancel, destroy };
|
90
|
+
}
|
91
|
+
|
92
|
+
function off<K extends EventType>(
|
93
|
+
eventType: K,
|
94
|
+
listener: EventListener<K>
|
95
|
+
): SubmitReturn {
|
96
|
+
const narrowed_listener_map: ListenerMap<K> = listener_map;
|
97
|
+
let listeners = narrowed_listener_map[eventType] || [];
|
98
|
+
listeners = listeners?.filter((l) => l !== listener);
|
99
|
+
narrowed_listener_map[eventType] = listeners;
|
100
|
+
return { on, off, cancel, destroy };
|
101
|
+
}
|
102
|
+
|
103
|
+
async function cancel(): Promise<void> {
|
104
|
+
const _status: Status = {
|
105
|
+
stage: "complete",
|
106
|
+
queue: false,
|
107
|
+
time: new Date()
|
108
|
+
};
|
109
|
+
complete = _status;
|
110
|
+
fire_event({
|
111
|
+
..._status,
|
112
|
+
type: "status",
|
113
|
+
endpoint: _endpoint,
|
114
|
+
fn_index: fn_index
|
115
|
+
});
|
116
|
+
|
117
|
+
let cancel_request = {};
|
118
|
+
if (protocol === "ws") {
|
119
|
+
if (websocket && websocket.readyState === 0) {
|
120
|
+
websocket.addEventListener("open", () => {
|
121
|
+
websocket.close();
|
122
|
+
});
|
123
|
+
} else {
|
124
|
+
websocket.close();
|
125
|
+
}
|
126
|
+
cancel_request = { fn_index, session_hash };
|
127
|
+
} else {
|
128
|
+
event_source?.close();
|
129
|
+
cancel_request = { event_id };
|
130
|
+
}
|
131
|
+
|
132
|
+
try {
|
133
|
+
if (!config) {
|
134
|
+
throw new Error("Could not resolve app config");
|
135
|
+
}
|
136
|
+
|
137
|
+
await fetch_implementation(`${config.root}/reset`, {
|
138
|
+
headers: { "Content-Type": "application/json" },
|
139
|
+
method: "POST",
|
140
|
+
body: JSON.stringify(cancel_request)
|
141
|
+
});
|
142
|
+
} catch (e) {
|
143
|
+
console.warn(
|
144
|
+
"The `/reset` endpoint could not be called. Subsequent endpoint results may be unreliable."
|
145
|
+
);
|
146
|
+
}
|
147
|
+
}
|
148
|
+
|
149
|
+
function destroy(): void {
|
150
|
+
for (const event_type in listener_map) {
|
151
|
+
listener_map &&
|
152
|
+
listener_map[event_type as "data" | "status"]?.forEach((fn) => {
|
153
|
+
off(event_type as "data" | "status", fn);
|
154
|
+
});
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
this.handle_blob(`${config.root}`, data, endpoint_info).then(
|
159
|
+
async (_payload) => {
|
160
|
+
payload = {
|
161
|
+
data: _payload || [],
|
162
|
+
event_data,
|
163
|
+
fn_index,
|
164
|
+
trigger_id
|
165
|
+
};
|
166
|
+
if (skip_queue(fn_index, config)) {
|
167
|
+
fire_event({
|
168
|
+
type: "status",
|
169
|
+
endpoint: _endpoint,
|
170
|
+
stage: "pending",
|
171
|
+
queue: false,
|
172
|
+
fn_index,
|
173
|
+
time: new Date()
|
174
|
+
});
|
175
|
+
|
176
|
+
post_data(
|
177
|
+
`${config.root}/run${
|
178
|
+
_endpoint.startsWith("/") ? _endpoint : `/${_endpoint}`
|
179
|
+
}${url_params ? "?" + url_params : ""}`,
|
180
|
+
{
|
181
|
+
...payload,
|
182
|
+
session_hash
|
183
|
+
}
|
184
|
+
)
|
185
|
+
.then(([output, status_code]: any) => {
|
186
|
+
const data = output.data;
|
187
|
+
if (status_code == 200) {
|
188
|
+
fire_event({
|
189
|
+
type: "data",
|
190
|
+
endpoint: _endpoint,
|
191
|
+
fn_index,
|
192
|
+
data: data,
|
193
|
+
time: new Date(),
|
194
|
+
event_data,
|
195
|
+
trigger_id
|
196
|
+
});
|
197
|
+
|
198
|
+
fire_event({
|
199
|
+
type: "status",
|
200
|
+
endpoint: _endpoint,
|
201
|
+
fn_index,
|
202
|
+
stage: "complete",
|
203
|
+
eta: output.average_duration,
|
204
|
+
queue: false,
|
205
|
+
time: new Date()
|
206
|
+
});
|
207
|
+
} else {
|
208
|
+
fire_event({
|
209
|
+
type: "status",
|
210
|
+
stage: "error",
|
211
|
+
endpoint: _endpoint,
|
212
|
+
fn_index,
|
213
|
+
message: output.error,
|
214
|
+
queue: false,
|
215
|
+
time: new Date()
|
216
|
+
});
|
217
|
+
}
|
218
|
+
})
|
219
|
+
.catch((e) => {
|
220
|
+
fire_event({
|
221
|
+
type: "status",
|
222
|
+
stage: "error",
|
223
|
+
message: e.message,
|
224
|
+
endpoint: _endpoint,
|
225
|
+
fn_index,
|
226
|
+
queue: false,
|
227
|
+
time: new Date()
|
228
|
+
});
|
229
|
+
});
|
230
|
+
} else if (protocol == "ws") {
|
231
|
+
const { ws_protocol, host } = await process_endpoint(
|
232
|
+
app_reference,
|
233
|
+
hf_token
|
234
|
+
);
|
235
|
+
|
236
|
+
fire_event({
|
237
|
+
type: "status",
|
238
|
+
stage: "pending",
|
239
|
+
queue: true,
|
240
|
+
endpoint: _endpoint,
|
241
|
+
fn_index,
|
242
|
+
time: new Date()
|
243
|
+
});
|
244
|
+
|
245
|
+
let url = new URL(
|
246
|
+
`${ws_protocol}://${resolve_root(
|
247
|
+
host,
|
248
|
+
config.path as string,
|
249
|
+
true
|
250
|
+
)}/queue/join${url_params ? "?" + url_params : ""}`
|
251
|
+
);
|
252
|
+
|
253
|
+
if (this.jwt) {
|
254
|
+
url.searchParams.set("__sign", this.jwt);
|
255
|
+
}
|
256
|
+
|
257
|
+
websocket = new WebSocket(url);
|
258
|
+
|
259
|
+
websocket.onclose = (evt) => {
|
260
|
+
if (!evt.wasClean) {
|
261
|
+
fire_event({
|
262
|
+
type: "status",
|
263
|
+
stage: "error",
|
264
|
+
broken: true,
|
265
|
+
message: BROKEN_CONNECTION_MSG,
|
266
|
+
queue: true,
|
267
|
+
endpoint: _endpoint,
|
268
|
+
fn_index,
|
269
|
+
time: new Date()
|
270
|
+
});
|
271
|
+
}
|
272
|
+
};
|
273
|
+
|
274
|
+
websocket.onmessage = function (event) {
|
275
|
+
const _data = JSON.parse(event.data);
|
276
|
+
const { type, status, data } = handle_message(
|
277
|
+
_data,
|
278
|
+
last_status[fn_index]
|
279
|
+
);
|
280
|
+
|
281
|
+
if (type === "update" && status && !complete) {
|
282
|
+
// call 'status' listeners
|
283
|
+
fire_event({
|
284
|
+
type: "status",
|
285
|
+
endpoint: _endpoint,
|
286
|
+
fn_index,
|
287
|
+
time: new Date(),
|
288
|
+
...status
|
289
|
+
});
|
290
|
+
if (status.stage === "error") {
|
291
|
+
websocket.close();
|
292
|
+
}
|
293
|
+
} else if (type === "hash") {
|
294
|
+
websocket.send(JSON.stringify({ fn_index, session_hash }));
|
295
|
+
return;
|
296
|
+
} else if (type === "data") {
|
297
|
+
websocket.send(JSON.stringify({ ...payload, session_hash }));
|
298
|
+
} else if (type === "complete") {
|
299
|
+
complete = status;
|
300
|
+
} else if (type === "log") {
|
301
|
+
fire_event({
|
302
|
+
type: "log",
|
303
|
+
log: data.log,
|
304
|
+
level: data.level,
|
305
|
+
endpoint: _endpoint,
|
306
|
+
fn_index
|
307
|
+
});
|
308
|
+
} else if (type === "generating") {
|
309
|
+
fire_event({
|
310
|
+
type: "status",
|
311
|
+
time: new Date(),
|
312
|
+
...status,
|
313
|
+
stage: status?.stage!,
|
314
|
+
queue: true,
|
315
|
+
endpoint: _endpoint,
|
316
|
+
fn_index
|
317
|
+
});
|
318
|
+
}
|
319
|
+
if (data) {
|
320
|
+
fire_event({
|
321
|
+
type: "data",
|
322
|
+
time: new Date(),
|
323
|
+
data: data.data,
|
324
|
+
endpoint: _endpoint,
|
325
|
+
fn_index,
|
326
|
+
event_data,
|
327
|
+
trigger_id
|
328
|
+
});
|
329
|
+
|
330
|
+
if (complete) {
|
331
|
+
fire_event({
|
332
|
+
type: "status",
|
333
|
+
time: new Date(),
|
334
|
+
...complete,
|
335
|
+
stage: status?.stage!,
|
336
|
+
queue: true,
|
337
|
+
endpoint: _endpoint,
|
338
|
+
fn_index
|
339
|
+
});
|
340
|
+
websocket.close();
|
341
|
+
}
|
342
|
+
}
|
343
|
+
};
|
344
|
+
|
345
|
+
// different ws contract for gradio versions older than 3.6.0
|
346
|
+
//@ts-ignore
|
347
|
+
if (semiver(config.version || "2.0.0", "3.6") < 0) {
|
348
|
+
addEventListener("open", () =>
|
349
|
+
websocket.send(JSON.stringify({ hash: session_hash }))
|
350
|
+
);
|
351
|
+
}
|
352
|
+
} else if (protocol == "sse") {
|
353
|
+
fire_event({
|
354
|
+
type: "status",
|
355
|
+
stage: "pending",
|
356
|
+
queue: true,
|
357
|
+
endpoint: _endpoint,
|
358
|
+
fn_index,
|
359
|
+
time: new Date()
|
360
|
+
});
|
361
|
+
var params = new URLSearchParams({
|
362
|
+
fn_index: fn_index.toString(),
|
363
|
+
session_hash: session_hash
|
364
|
+
}).toString();
|
365
|
+
let url = new URL(
|
366
|
+
`${config.root}/queue/join?${
|
367
|
+
url_params ? url_params + "&" : ""
|
368
|
+
}${params}`
|
369
|
+
);
|
370
|
+
|
371
|
+
event_source = this.eventSource_factory(url);
|
372
|
+
|
373
|
+
if (!event_source) {
|
374
|
+
throw new Error(
|
375
|
+
"Cannot connect to sse endpoint: " + url.toString()
|
376
|
+
);
|
377
|
+
}
|
378
|
+
|
379
|
+
event_source.onmessage = async function (event) {
|
380
|
+
const _data = JSON.parse(event.data);
|
381
|
+
const { type, status, data } = handle_message(
|
382
|
+
_data,
|
383
|
+
last_status[fn_index]
|
384
|
+
);
|
385
|
+
|
386
|
+
if (type === "update" && status && !complete) {
|
387
|
+
// call 'status' listeners
|
388
|
+
fire_event({
|
389
|
+
type: "status",
|
390
|
+
endpoint: _endpoint,
|
391
|
+
fn_index,
|
392
|
+
time: new Date(),
|
393
|
+
...status
|
394
|
+
});
|
395
|
+
if (status.stage === "error") {
|
396
|
+
event_source?.close();
|
397
|
+
}
|
398
|
+
} else if (type === "data") {
|
399
|
+
event_id = _data.event_id as string;
|
400
|
+
let [_, status] = await post_data(`${config.root}/queue/data`, {
|
401
|
+
...payload,
|
402
|
+
session_hash,
|
403
|
+
event_id
|
404
|
+
});
|
405
|
+
if (status !== 200) {
|
406
|
+
fire_event({
|
407
|
+
type: "status",
|
408
|
+
stage: "error",
|
409
|
+
message: BROKEN_CONNECTION_MSG,
|
410
|
+
queue: true,
|
411
|
+
endpoint: _endpoint,
|
412
|
+
fn_index,
|
413
|
+
time: new Date()
|
414
|
+
});
|
415
|
+
event_source?.close();
|
416
|
+
}
|
417
|
+
} else if (type === "complete") {
|
418
|
+
complete = status;
|
419
|
+
} else if (type === "log") {
|
420
|
+
fire_event({
|
421
|
+
type: "log",
|
422
|
+
log: data.log,
|
423
|
+
level: data.level,
|
424
|
+
endpoint: _endpoint,
|
425
|
+
fn_index
|
426
|
+
});
|
427
|
+
} else if (type === "generating") {
|
428
|
+
fire_event({
|
429
|
+
type: "status",
|
430
|
+
time: new Date(),
|
431
|
+
...status,
|
432
|
+
stage: status?.stage!,
|
433
|
+
queue: true,
|
434
|
+
endpoint: _endpoint,
|
435
|
+
fn_index
|
436
|
+
});
|
437
|
+
}
|
438
|
+
if (data) {
|
439
|
+
fire_event({
|
440
|
+
type: "data",
|
441
|
+
time: new Date(),
|
442
|
+
data: data.data,
|
443
|
+
endpoint: _endpoint,
|
444
|
+
fn_index,
|
445
|
+
event_data,
|
446
|
+
trigger_id
|
447
|
+
});
|
448
|
+
|
449
|
+
if (complete) {
|
450
|
+
fire_event({
|
451
|
+
type: "status",
|
452
|
+
time: new Date(),
|
453
|
+
...complete,
|
454
|
+
stage: status?.stage!,
|
455
|
+
queue: true,
|
456
|
+
endpoint: _endpoint,
|
457
|
+
fn_index
|
458
|
+
});
|
459
|
+
event_source?.close();
|
460
|
+
}
|
461
|
+
}
|
462
|
+
};
|
463
|
+
} else if (
|
464
|
+
protocol == "sse_v1" ||
|
465
|
+
protocol == "sse_v2" ||
|
466
|
+
protocol == "sse_v2.1" ||
|
467
|
+
protocol == "sse_v3"
|
468
|
+
) {
|
469
|
+
// latest API format. v2 introduces sending diffs for intermediate outputs in generative functions, which makes payloads lighter.
|
470
|
+
// v3 only closes the stream when the backend sends the close stream message.
|
471
|
+
fire_event({
|
472
|
+
type: "status",
|
473
|
+
stage: "pending",
|
474
|
+
queue: true,
|
475
|
+
endpoint: _endpoint,
|
476
|
+
fn_index,
|
477
|
+
time: new Date()
|
478
|
+
});
|
479
|
+
let hostname = window.location.hostname;
|
480
|
+
let hfhubdev = "dev.spaces.huggingface.tech";
|
481
|
+
const origin = hostname.includes(".dev.")
|
482
|
+
? `https://moon-${hostname.split(".")[1]}.${hfhubdev}`
|
483
|
+
: `https://huggingface.co`;
|
484
|
+
const zerogpu_auth_promise =
|
485
|
+
dependency.zerogpu && window.parent != window && config.space_id
|
486
|
+
? post_message<Headers>("zerogpu-headers", origin)
|
487
|
+
: Promise.resolve(null);
|
488
|
+
const post_data_promise = zerogpu_auth_promise.then((headers) => {
|
489
|
+
return post_data(
|
490
|
+
`${config.root}/queue/join?${url_params}`,
|
491
|
+
{
|
492
|
+
...payload,
|
493
|
+
session_hash
|
494
|
+
},
|
495
|
+
headers
|
496
|
+
);
|
497
|
+
});
|
498
|
+
post_data_promise.then(([response, status]: any) => {
|
499
|
+
if (status === 503) {
|
500
|
+
fire_event({
|
501
|
+
type: "status",
|
502
|
+
stage: "error",
|
503
|
+
message: QUEUE_FULL_MSG,
|
504
|
+
queue: true,
|
505
|
+
endpoint: _endpoint,
|
506
|
+
fn_index,
|
507
|
+
time: new Date()
|
508
|
+
});
|
509
|
+
} else if (status !== 200) {
|
510
|
+
fire_event({
|
511
|
+
type: "status",
|
512
|
+
stage: "error",
|
513
|
+
message: BROKEN_CONNECTION_MSG,
|
514
|
+
queue: true,
|
515
|
+
endpoint: _endpoint,
|
516
|
+
fn_index,
|
517
|
+
time: new Date()
|
518
|
+
});
|
519
|
+
} else {
|
520
|
+
event_id = response.event_id as string;
|
521
|
+
let callback = async function (_data: object): Promise<void> {
|
522
|
+
try {
|
523
|
+
const { type, status, data } = handle_message(
|
524
|
+
_data,
|
525
|
+
last_status[fn_index]
|
526
|
+
);
|
527
|
+
|
528
|
+
if (type == "heartbeat") {
|
529
|
+
return;
|
530
|
+
}
|
531
|
+
|
532
|
+
if (type === "update" && status && !complete) {
|
533
|
+
// call 'status' listeners
|
534
|
+
fire_event({
|
535
|
+
type: "status",
|
536
|
+
endpoint: _endpoint,
|
537
|
+
fn_index,
|
538
|
+
time: new Date(),
|
539
|
+
...status
|
540
|
+
});
|
541
|
+
} else if (type === "complete") {
|
542
|
+
complete = status;
|
543
|
+
} else if (type == "unexpected_error") {
|
544
|
+
console.error("Unexpected error", status?.message);
|
545
|
+
fire_event({
|
546
|
+
type: "status",
|
547
|
+
stage: "error",
|
548
|
+
message:
|
549
|
+
status?.message || "An Unexpected Error Occurred!",
|
550
|
+
queue: true,
|
551
|
+
endpoint: _endpoint,
|
552
|
+
fn_index,
|
553
|
+
time: new Date()
|
554
|
+
});
|
555
|
+
} else if (type === "log") {
|
556
|
+
fire_event({
|
557
|
+
type: "log",
|
558
|
+
log: data.log,
|
559
|
+
level: data.level,
|
560
|
+
endpoint: _endpoint,
|
561
|
+
fn_index
|
562
|
+
});
|
563
|
+
return;
|
564
|
+
} else if (type === "generating") {
|
565
|
+
fire_event({
|
566
|
+
type: "status",
|
567
|
+
time: new Date(),
|
568
|
+
...status,
|
569
|
+
stage: status?.stage!,
|
570
|
+
queue: true,
|
571
|
+
endpoint: _endpoint,
|
572
|
+
fn_index
|
573
|
+
});
|
574
|
+
if (
|
575
|
+
data &&
|
576
|
+
["sse_v2", "sse_v2.1", "sse_v3"].includes(protocol)
|
577
|
+
) {
|
578
|
+
apply_diff_stream(pending_diff_streams, event_id!, data);
|
579
|
+
}
|
580
|
+
}
|
581
|
+
if (data) {
|
582
|
+
fire_event({
|
583
|
+
type: "data",
|
584
|
+
time: new Date(),
|
585
|
+
data: data.data,
|
586
|
+
endpoint: _endpoint,
|
587
|
+
fn_index
|
588
|
+
});
|
589
|
+
|
590
|
+
if (complete) {
|
591
|
+
fire_event({
|
592
|
+
type: "status",
|
593
|
+
time: new Date(),
|
594
|
+
...complete,
|
595
|
+
stage: status?.stage!,
|
596
|
+
queue: true,
|
597
|
+
endpoint: _endpoint,
|
598
|
+
fn_index
|
599
|
+
});
|
600
|
+
}
|
601
|
+
}
|
602
|
+
|
603
|
+
if (
|
604
|
+
status?.stage === "complete" ||
|
605
|
+
status?.stage === "error"
|
606
|
+
) {
|
607
|
+
if (event_callbacks[event_id!]) {
|
608
|
+
delete event_callbacks[event_id!];
|
609
|
+
}
|
610
|
+
if (event_id! in pending_diff_streams) {
|
611
|
+
delete pending_diff_streams[event_id!];
|
612
|
+
}
|
613
|
+
}
|
614
|
+
} catch (e) {
|
615
|
+
console.error("Unexpected client exception", e);
|
616
|
+
fire_event({
|
617
|
+
type: "status",
|
618
|
+
stage: "error",
|
619
|
+
message: "An Unexpected Error Occurred!",
|
620
|
+
queue: true,
|
621
|
+
endpoint: _endpoint,
|
622
|
+
fn_index,
|
623
|
+
time: new Date()
|
624
|
+
});
|
625
|
+
if (["sse_v2", "sse_v2.1"].includes(protocol)) {
|
626
|
+
close_stream(stream_status, event_source);
|
627
|
+
stream_status.open = false;
|
628
|
+
}
|
629
|
+
}
|
630
|
+
};
|
631
|
+
|
632
|
+
if (event_id in pending_stream_messages) {
|
633
|
+
pending_stream_messages[event_id].forEach((msg) =>
|
634
|
+
callback(msg)
|
635
|
+
);
|
636
|
+
delete pending_stream_messages[event_id];
|
637
|
+
}
|
638
|
+
// @ts-ignore
|
639
|
+
event_callbacks[event_id] = callback;
|
640
|
+
unclosed_events.add(event_id);
|
641
|
+
if (!stream_status.open) {
|
642
|
+
this.open_stream();
|
643
|
+
}
|
644
|
+
}
|
645
|
+
});
|
646
|
+
}
|
647
|
+
}
|
648
|
+
);
|
649
|
+
|
650
|
+
return { on, off, cancel, destroy };
|
651
|
+
} catch (error) {
|
652
|
+
console.error("Submit function encountered an error:", error);
|
653
|
+
throw error;
|
654
|
+
}
|
655
|
+
}
|
656
|
+
|
657
|
+
function get_endpoint_info(
|
658
|
+
api_info: ApiInfo<JsApiData>,
|
659
|
+
endpoint: string | number,
|
660
|
+
api_map: Record<string, number>,
|
661
|
+
config: Config
|
662
|
+
): {
|
663
|
+
fn_index: number;
|
664
|
+
endpoint_info: EndpointInfo<JsApiData>;
|
665
|
+
dependency: Dependency;
|
666
|
+
} {
|
667
|
+
let fn_index: number;
|
668
|
+
let endpoint_info: EndpointInfo<JsApiData>;
|
669
|
+
let dependency: Dependency;
|
670
|
+
|
671
|
+
if (typeof endpoint === "number") {
|
672
|
+
fn_index = endpoint;
|
673
|
+
endpoint_info = api_info.unnamed_endpoints[fn_index];
|
674
|
+
dependency = config.dependencies[endpoint];
|
675
|
+
} else {
|
676
|
+
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
677
|
+
|
678
|
+
fn_index = api_map[trimmed_endpoint];
|
679
|
+
endpoint_info = api_info.named_endpoints[endpoint.trim()];
|
680
|
+
dependency = config.dependencies[api_map[trimmed_endpoint]];
|
681
|
+
}
|
682
|
+
|
683
|
+
if (typeof fn_index !== "number") {
|
684
|
+
throw new Error(
|
685
|
+
"There is no endpoint matching that name of fn_index matching that number."
|
686
|
+
);
|
687
|
+
}
|
688
|
+
return { fn_index, endpoint_info, dependency };
|
689
|
+
}
|