@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/src/utils/submit.ts
CHANGED
|
@@ -19,11 +19,13 @@ import {
|
|
|
19
19
|
} from "../helpers/api_info";
|
|
20
20
|
import {
|
|
21
21
|
BROKEN_CONNECTION_MSG,
|
|
22
|
+
NO_API_INFO_MSG,
|
|
22
23
|
QUEUE_FULL_MSG,
|
|
23
24
|
SSE_URL,
|
|
24
25
|
SSE_DATA_URL,
|
|
25
26
|
RESET_URL,
|
|
26
|
-
CANCEL_URL
|
|
27
|
+
CANCEL_URL,
|
|
28
|
+
WS_PROTOCOL_MSG
|
|
27
29
|
} from "../constants";
|
|
28
30
|
import { apply_diff_stream, close_stream } from "./stream";
|
|
29
31
|
import { Client } from "../client";
|
|
@@ -60,7 +62,7 @@ export function submit(
|
|
|
60
62
|
|
|
61
63
|
const that = this;
|
|
62
64
|
|
|
63
|
-
if (!api_info) throw new Error(
|
|
65
|
+
if (!api_info) throw new Error(NO_API_INFO_MSG);
|
|
64
66
|
if (!config) throw new Error("Could not resolve app config");
|
|
65
67
|
|
|
66
68
|
let { fn_index, endpoint_info, dependency } = get_endpoint_info(
|
|
@@ -75,7 +77,7 @@ export function submit(
|
|
|
75
77
|
let stream: EventSource | null;
|
|
76
78
|
let protocol = config.protocol ?? "ws";
|
|
77
79
|
if (protocol === "ws") {
|
|
78
|
-
throw new Error(
|
|
80
|
+
throw new Error(WS_PROTOCOL_MSG);
|
|
79
81
|
}
|
|
80
82
|
let event_id_final = "";
|
|
81
83
|
let event_id_cb: () => string = () => event_id_final;
|
|
@@ -182,7 +184,10 @@ export function submit(
|
|
|
182
184
|
data: input_data || [],
|
|
183
185
|
event_data,
|
|
184
186
|
fn_index,
|
|
185
|
-
trigger_id
|
|
187
|
+
trigger_id,
|
|
188
|
+
...(options.oauth_token && endpoint_info?.oauth_token
|
|
189
|
+
? { oauth_token: options.oauth_token }
|
|
190
|
+
: {})
|
|
186
191
|
};
|
|
187
192
|
if (skip_queue(fn_index, config)) {
|
|
188
193
|
fire_event({
|
|
@@ -630,6 +635,22 @@ export function submit(
|
|
|
630
635
|
}
|
|
631
636
|
});
|
|
632
637
|
|
|
638
|
+
// Surface internal failures (e.g. malformed payloads or configs) as an
|
|
639
|
+
// error event instead of leaving the returned iterator hanging forever
|
|
640
|
+
// with an unhandled promise rejection.
|
|
641
|
+
job.catch((e) => {
|
|
642
|
+
fire_event({
|
|
643
|
+
type: "status",
|
|
644
|
+
stage: "error",
|
|
645
|
+
message: e instanceof Error ? e.message : String(e),
|
|
646
|
+
queue: !skip_queue(fn_index, config),
|
|
647
|
+
endpoint: _endpoint,
|
|
648
|
+
fn_index,
|
|
649
|
+
time: new Date()
|
|
650
|
+
});
|
|
651
|
+
close();
|
|
652
|
+
});
|
|
653
|
+
|
|
633
654
|
let done = false;
|
|
634
655
|
const values: (IteratorResult<GradioEvent> | PromiseLike<never>)[] = [];
|
|
635
656
|
const resolvers: ((
|
|
@@ -735,25 +756,37 @@ function get_endpoint_info(
|
|
|
735
756
|
} {
|
|
736
757
|
let fn_index: number;
|
|
737
758
|
let endpoint_info: EndpointInfo<JsApiData>;
|
|
738
|
-
let dependency: Dependency;
|
|
759
|
+
let dependency: Dependency | undefined;
|
|
739
760
|
|
|
740
761
|
if (typeof endpoint === "number") {
|
|
741
762
|
fn_index = endpoint;
|
|
742
763
|
endpoint_info = api_info.unnamed_endpoints[fn_index];
|
|
743
|
-
dependency = config.dependencies.find((dep) => dep.id == endpoint)
|
|
764
|
+
dependency = config.dependencies.find((dep) => dep.id == endpoint);
|
|
744
765
|
} else {
|
|
745
766
|
const trimmed_endpoint = endpoint.replace(/^\//, "");
|
|
746
767
|
|
|
747
768
|
fn_index = api_map[trimmed_endpoint];
|
|
748
|
-
|
|
769
|
+
// named endpoints are keyed with a leading slash in the API info, but
|
|
770
|
+
// accept endpoint names passed without one (e.g. "predict")
|
|
771
|
+
endpoint_info =
|
|
772
|
+
api_info.named_endpoints[endpoint.trim()] ??
|
|
773
|
+
api_info.named_endpoints[`/${trimmed_endpoint}`];
|
|
749
774
|
dependency = config.dependencies.find(
|
|
750
775
|
(dep) => dep.id == api_map[trimmed_endpoint]
|
|
751
|
-
)
|
|
776
|
+
);
|
|
752
777
|
}
|
|
753
778
|
|
|
754
|
-
if (typeof fn_index !== "number") {
|
|
779
|
+
if (typeof fn_index !== "number" || !dependency) {
|
|
780
|
+
const valid_endpoints = config.dependencies
|
|
781
|
+
.filter((dep) => dep.api_name)
|
|
782
|
+
.map((dep) => `"/${dep.api_name}"`)
|
|
783
|
+
.join(", ");
|
|
755
784
|
throw new Error(
|
|
756
|
-
|
|
785
|
+
`No endpoint matching ${JSON.stringify(endpoint)} was found. ` +
|
|
786
|
+
(valid_endpoints
|
|
787
|
+
? `Valid named endpoints are: ${valid_endpoints}. `
|
|
788
|
+
: "This app exposes no named endpoints. ") +
|
|
789
|
+
"An fn_index (number) of an existing dependency can also be used."
|
|
757
790
|
);
|
|
758
791
|
}
|
|
759
792
|
return { fn_index, endpoint_info, dependency };
|
package/src/utils/view_api.ts
CHANGED
|
@@ -12,8 +12,7 @@ export async function view_api(this: Client): Promise<any> {
|
|
|
12
12
|
|
|
13
13
|
const headers: {
|
|
14
14
|
Authorization?: string;
|
|
15
|
-
|
|
16
|
-
} = { "Content-Type": "application/json" };
|
|
15
|
+
} = {};
|
|
17
16
|
|
|
18
17
|
if (token) {
|
|
19
18
|
headers.Authorization = `Bearer ${token}`;
|
|
@@ -32,7 +31,7 @@ export async function view_api(this: Client): Promise<any> {
|
|
|
32
31
|
const url = join_urls(config.root, this.api_prefix, API_INFO_URL);
|
|
33
32
|
response = await this.fetch(url, {
|
|
34
33
|
headers,
|
|
35
|
-
credentials: "
|
|
34
|
+
credentials: this.options.credentials ?? "same-origin"
|
|
36
35
|
});
|
|
37
36
|
if (!response.ok) {
|
|
38
37
|
throw new Error(BROKEN_CONNECTION_MSG);
|
package/vite.config.ts
CHANGED
|
@@ -38,7 +38,12 @@ logger.info = (log, options) => {
|
|
|
38
38
|
};
|
|
39
39
|
export default defineConfig(({ mode }) => {
|
|
40
40
|
const production = mode === "production";
|
|
41
|
-
|
|
41
|
+
// Single-file, minified browser bundle served from CDNs at
|
|
42
|
+
// dist/index.min.js (see https://github.com/gradio-app/gradio/issues/10028)
|
|
43
|
+
const isCdnBuild = process.env.CDN_BUILD === "true";
|
|
44
|
+
// a CDN build is always a browser build (guards against CDN_BUILD=true
|
|
45
|
+
// without BROWSER_BUILD=true emitting es+cjs into the same file name)
|
|
46
|
+
const isBrowserBuild = process.env.BROWSER_BUILD === "true" || isCdnBuild;
|
|
42
47
|
|
|
43
48
|
if (mode === "preview") {
|
|
44
49
|
return {
|
|
@@ -50,15 +55,26 @@ export default defineConfig(({ mode }) => {
|
|
|
50
55
|
customLogger: logger,
|
|
51
56
|
build: {
|
|
52
57
|
emptyOutDir: false,
|
|
58
|
+
...(isCdnBuild ? { minify: true } : {}),
|
|
53
59
|
lib: {
|
|
54
60
|
entry: "src/index.ts",
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
// the node build also emits a CommonJS bundle (dist/index.cjs)
|
|
62
|
+
// so that require("@gradio/client") works on all supported
|
|
63
|
+
// node versions (see https://github.com/gradio-app/gradio/issues/9214)
|
|
64
|
+
formats: isBrowserBuild ? ["es"] : ["es", "cjs"],
|
|
65
|
+
fileName: (format) => {
|
|
66
|
+
if (isCdnBuild) return "index.min.js";
|
|
67
|
+
if (isBrowserBuild) return "browser.js";
|
|
68
|
+
return format === "cjs" ? "index.cjs" : "index.js";
|
|
69
|
+
}
|
|
57
70
|
},
|
|
58
71
|
rollupOptions: {
|
|
59
72
|
input: "src/index.ts",
|
|
60
73
|
output: {
|
|
61
|
-
dir: "dist"
|
|
74
|
+
dir: "dist",
|
|
75
|
+
// keep the CDN bundle as a single file so the documented
|
|
76
|
+
// <script type="module"> import works without extra chunks
|
|
77
|
+
inlineDynamicImports: isCdnBuild
|
|
62
78
|
}
|
|
63
79
|
}
|
|
64
80
|
},
|