@gradio/client 0.16.0 → 0.18.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 +57 -0
- package/README.md +49 -43
- package/dist/client.d.ts +63 -70
- package/dist/client.d.ts.map +1 -1
- package/dist/constants.d.ts +23 -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 -18
- 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 +1628 -1398
- package/dist/test/handlers.d.ts +3 -0
- package/dist/test/handlers.d.ts.map +1 -0
- package/dist/test/mock_eventsource.d.ts +2 -0
- package/dist/test/mock_eventsource.d.ts.map +1 -0
- package/dist/test/server.d.ts +2 -0
- package/dist/test/server.d.ts.map +1 -0
- package/dist/test/test_data.d.ts +76 -0
- package/dist/test/test_data.d.ts.map +1 -0
- package/dist/types.d.ts +153 -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 +10 -3
- package/src/client.ts +314 -1691
- package/src/constants.ts +27 -0
- package/src/globals.d.ts +2 -21
- package/src/helpers/api_info.ts +300 -0
- package/src/helpers/data.ts +133 -0
- package/src/helpers/init_helpers.ts +130 -0
- package/src/helpers/spaces.ts +197 -0
- package/src/index.ts +16 -10
- package/src/test/api_info.test.ts +456 -0
- package/src/test/data.test.ts +281 -0
- package/src/test/handlers.ts +438 -0
- package/src/test/init.test.ts +139 -0
- package/src/test/init_helpers.test.ts +94 -0
- package/src/test/mock_eventsource.ts +11 -0
- package/src/test/post_data.test.ts +45 -0
- package/src/test/server.ts +6 -0
- package/src/test/spaces.test.ts +145 -0
- package/src/test/stream.test.ts +67 -0
- package/src/test/test_data.ts +557 -0
- package/src/test/upload_files.test.ts +42 -0
- package/src/test/view_api.test.ts +53 -0
- package/src/types.ts +201 -59
- package/src/upload.ts +19 -15
- package/src/utils/duplicate.ts +104 -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 +175 -0
- package/src/utils/submit.ts +697 -0
- package/src/utils/upload_files.ts +51 -0
- package/src/utils/view_api.ts +67 -0
- package/src/vite-env.d.ts +1 -0
- package/tsconfig.json +15 -2
- package/vite.config.js +11 -17
- package/dist/utils.d.ts.map +0 -1
- package/src/client.node-test.ts +0 -172
- package/src/utils.ts +0 -314
@@ -0,0 +1,175 @@
|
|
1
|
+
import { BROKEN_CONNECTION_MSG } from "../constants";
|
2
|
+
import type { Client } from "../client";
|
3
|
+
|
4
|
+
export function open_stream(this: Client): void {
|
5
|
+
let {
|
6
|
+
event_callbacks,
|
7
|
+
unclosed_events,
|
8
|
+
pending_stream_messages,
|
9
|
+
stream_status,
|
10
|
+
config,
|
11
|
+
jwt
|
12
|
+
} = this;
|
13
|
+
|
14
|
+
if (!config) {
|
15
|
+
throw new Error("Could not resolve app config");
|
16
|
+
}
|
17
|
+
|
18
|
+
stream_status.open = true;
|
19
|
+
|
20
|
+
let stream: EventSource | null = null;
|
21
|
+
let params = new URLSearchParams({
|
22
|
+
session_hash: this.session_hash
|
23
|
+
}).toString();
|
24
|
+
|
25
|
+
let url = new URL(`${config.root}/queue/data?${params}`);
|
26
|
+
|
27
|
+
if (jwt) {
|
28
|
+
url.searchParams.set("__sign", jwt);
|
29
|
+
}
|
30
|
+
|
31
|
+
stream = this.stream_factory(url);
|
32
|
+
|
33
|
+
if (!stream) {
|
34
|
+
console.warn("Cannot connect to SSE endpoint: " + url.toString());
|
35
|
+
return;
|
36
|
+
}
|
37
|
+
|
38
|
+
stream.onmessage = async function (event: MessageEvent) {
|
39
|
+
let _data = JSON.parse(event.data);
|
40
|
+
if (_data.msg === "close_stream") {
|
41
|
+
close_stream(stream_status, stream);
|
42
|
+
return;
|
43
|
+
}
|
44
|
+
const event_id = _data.event_id;
|
45
|
+
if (!event_id) {
|
46
|
+
await Promise.all(
|
47
|
+
Object.keys(event_callbacks).map((event_id) =>
|
48
|
+
event_callbacks[event_id](_data)
|
49
|
+
)
|
50
|
+
);
|
51
|
+
} else if (event_callbacks[event_id] && config) {
|
52
|
+
if (
|
53
|
+
_data.msg === "process_completed" &&
|
54
|
+
["sse", "sse_v1", "sse_v2", "sse_v2.1"].includes(config.protocol)
|
55
|
+
) {
|
56
|
+
unclosed_events.delete(event_id);
|
57
|
+
if (unclosed_events.size === 0) {
|
58
|
+
close_stream(stream_status, stream);
|
59
|
+
}
|
60
|
+
}
|
61
|
+
let fn: (data: any) => void = event_callbacks[event_id];
|
62
|
+
|
63
|
+
if (typeof window !== "undefined") {
|
64
|
+
window.setTimeout(fn, 0, _data); // need to do this to put the event on the end of the event loop, so the browser can refresh between callbacks and not freeze in case of quick generations. See https://github.com/gradio-app/gradio/pull/7055
|
65
|
+
} else {
|
66
|
+
setImmediate(fn, _data);
|
67
|
+
}
|
68
|
+
} else {
|
69
|
+
if (!pending_stream_messages[event_id]) {
|
70
|
+
pending_stream_messages[event_id] = [];
|
71
|
+
}
|
72
|
+
pending_stream_messages[event_id].push(_data);
|
73
|
+
}
|
74
|
+
};
|
75
|
+
stream.onerror = async function () {
|
76
|
+
await Promise.all(
|
77
|
+
Object.keys(event_callbacks).map((event_id) =>
|
78
|
+
event_callbacks[event_id]({
|
79
|
+
msg: "unexpected_error",
|
80
|
+
message: BROKEN_CONNECTION_MSG
|
81
|
+
})
|
82
|
+
)
|
83
|
+
);
|
84
|
+
close_stream(stream_status, stream);
|
85
|
+
};
|
86
|
+
}
|
87
|
+
|
88
|
+
export function close_stream(
|
89
|
+
stream_status: { open: boolean },
|
90
|
+
stream: EventSource | null
|
91
|
+
): void {
|
92
|
+
if (stream_status && stream) {
|
93
|
+
stream_status.open = false;
|
94
|
+
stream?.close();
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
export function apply_diff_stream(
|
99
|
+
pending_diff_streams: Record<string, any[][]>,
|
100
|
+
event_id: string,
|
101
|
+
data: any
|
102
|
+
): void {
|
103
|
+
let is_first_generation = !pending_diff_streams[event_id];
|
104
|
+
if (is_first_generation) {
|
105
|
+
pending_diff_streams[event_id] = [];
|
106
|
+
data.data.forEach((value: any, i: number) => {
|
107
|
+
pending_diff_streams[event_id][i] = value;
|
108
|
+
});
|
109
|
+
} else {
|
110
|
+
data.data.forEach((value: any, i: number) => {
|
111
|
+
let new_data = apply_diff(pending_diff_streams[event_id][i], value);
|
112
|
+
pending_diff_streams[event_id][i] = new_data;
|
113
|
+
data.data[i] = new_data;
|
114
|
+
});
|
115
|
+
}
|
116
|
+
}
|
117
|
+
|
118
|
+
export function apply_diff(
|
119
|
+
obj: any,
|
120
|
+
diff: [string, (number | string)[], any][]
|
121
|
+
): any {
|
122
|
+
diff.forEach(([action, path, value]) => {
|
123
|
+
obj = apply_edit(obj, path, action, value);
|
124
|
+
});
|
125
|
+
|
126
|
+
return obj;
|
127
|
+
}
|
128
|
+
|
129
|
+
function apply_edit(
|
130
|
+
target: any,
|
131
|
+
path: (number | string)[],
|
132
|
+
action: string,
|
133
|
+
value: any
|
134
|
+
): any {
|
135
|
+
if (path.length === 0) {
|
136
|
+
if (action === "replace") {
|
137
|
+
return value;
|
138
|
+
} else if (action === "append") {
|
139
|
+
return target + value;
|
140
|
+
}
|
141
|
+
throw new Error(`Unsupported action: ${action}`);
|
142
|
+
}
|
143
|
+
|
144
|
+
let current = target;
|
145
|
+
for (let i = 0; i < path.length - 1; i++) {
|
146
|
+
current = current[path[i]];
|
147
|
+
}
|
148
|
+
|
149
|
+
const last_path = path[path.length - 1];
|
150
|
+
switch (action) {
|
151
|
+
case "replace":
|
152
|
+
current[last_path] = value;
|
153
|
+
break;
|
154
|
+
case "append":
|
155
|
+
current[last_path] += value;
|
156
|
+
break;
|
157
|
+
case "add":
|
158
|
+
if (Array.isArray(current)) {
|
159
|
+
current.splice(Number(last_path), 0, value);
|
160
|
+
} else {
|
161
|
+
current[last_path] = value;
|
162
|
+
}
|
163
|
+
break;
|
164
|
+
case "delete":
|
165
|
+
if (Array.isArray(current)) {
|
166
|
+
current.splice(Number(last_path), 1);
|
167
|
+
} else {
|
168
|
+
delete current[last_path];
|
169
|
+
}
|
170
|
+
break;
|
171
|
+
default:
|
172
|
+
throw new Error(`Unknown action: ${action}`);
|
173
|
+
}
|
174
|
+
return target;
|
175
|
+
}
|