@gradio/client 2.5.1 → 2.6.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.
@@ -11,6 +11,7 @@ import type { GradioEvent, StatusMessage } from "../types";
11
11
  const KEY_ROOT = "gradio:run-history:";
12
12
  const STORAGE_PREFIX = `${KEY_ROOT}v2:`;
13
13
  const REPLAY_PREFIX = `${KEY_ROOT}replay:v2:`;
14
+ const DESTINATION_PREFIX = `${KEY_ROOT}destination:v1:`;
14
15
  const MAX_RUNS = 100;
15
16
  const MAX_APPS = 8;
16
17
 
@@ -26,6 +27,10 @@ export interface RunHistoryScope {
26
27
  username?: string | null;
27
28
  }
28
29
 
30
+ export type RunHistoryStorage =
31
+ | { type: "browser"; bucket_id?: string }
32
+ | { type: "bucket"; bucket_id: string };
33
+
29
34
  /**
30
35
  * Run history is a side effect of submitting, never the point of it, so no
31
36
  * failure in here may propagate into the caller and break the app. Every
@@ -115,6 +120,45 @@ function replay_key(scope: RunHistoryScope | null | undefined): string | null {
115
120
  return key ? key.replace(STORAGE_PREFIX, REPLAY_PREFIX) : null;
116
121
  }
117
122
 
123
+ function destination_key(
124
+ scope: RunHistoryScope | null | undefined
125
+ ): string | null {
126
+ const key = storage_key(scope);
127
+ return key ? key.replace(STORAGE_PREFIX, DESTINATION_PREFIX) : null;
128
+ }
129
+
130
+ function read_run_history_storage_impl(
131
+ scope: RunHistoryScope | null | undefined
132
+ ): RunHistoryStorage {
133
+ const key = destination_key(scope);
134
+ if (!key) return { type: "browser" };
135
+ try {
136
+ const value = JSON.parse(window.localStorage.getItem(key) || "null");
137
+ if (typeof value?.bucket_id === "string") {
138
+ return value.type === "bucket"
139
+ ? { type: "bucket", bucket_id: value.bucket_id }
140
+ : { type: "browser", bucket_id: value.bucket_id };
141
+ }
142
+ return { type: "browser" };
143
+ } catch {
144
+ return { type: "browser" };
145
+ }
146
+ }
147
+
148
+ function set_run_history_storage_impl(
149
+ scope: RunHistoryScope | null | undefined,
150
+ storage: RunHistoryStorage
151
+ ): void {
152
+ const key = destination_key(scope);
153
+ if (!key) return;
154
+ if (storage.type === "browser" && !storage.bucket_id) {
155
+ window.localStorage.removeItem(key);
156
+ } else {
157
+ window.localStorage.setItem(key, JSON.stringify(storage));
158
+ }
159
+ notify_run_history_change();
160
+ }
161
+
118
162
  /** When a run was most recently saved under a key, for deciding what to drop. */
119
163
  function last_saved_at(key: string): number {
120
164
  try {
@@ -140,7 +184,10 @@ function prune_apps(current_key: string): void {
140
184
  const stale = [
141
185
  // Keys written by an older layout can never be read again.
142
186
  ...keys.filter(
143
- (key) => !key.startsWith(STORAGE_PREFIX) && !key.startsWith(REPLAY_PREFIX)
187
+ (key) =>
188
+ !key.startsWith(STORAGE_PREFIX) &&
189
+ !key.startsWith(REPLAY_PREFIX) &&
190
+ !key.startsWith(DESTINATION_PREFIX)
144
191
  ),
145
192
  ...keys
146
193
  .filter((key) => key.startsWith(STORAGE_PREFIX) && key !== current_key)
@@ -365,6 +412,7 @@ function apply_run_history_replay_impl(
365
412
  }
366
413
 
367
414
  function start_run_history_impl(options: StartRunOptions): string | null {
415
+ if (read_run_history_storage_impl(options).type === "bucket") return null;
368
416
  const key = storage_key(options);
369
417
  if (!key) return null;
370
418
 
@@ -522,6 +570,21 @@ export function read_run_history(
522
570
  return safely(() => read_run_history_impl(scope), []);
523
571
  }
524
572
 
573
+ export function read_run_history_storage(
574
+ scope: RunHistoryScope | null | undefined
575
+ ): RunHistoryStorage {
576
+ return safely(() => read_run_history_storage_impl(scope), {
577
+ type: "browser"
578
+ });
579
+ }
580
+
581
+ export function set_run_history_storage(
582
+ scope: RunHistoryScope | null | undefined,
583
+ storage: RunHistoryStorage
584
+ ): void {
585
+ safely(() => set_run_history_storage_impl(scope, storage), undefined);
586
+ }
587
+
525
588
  export function clear_run_history(
526
589
  scope: RunHistoryScope | null | undefined
527
590
  ): void {
@@ -30,6 +30,7 @@ import {
30
30
  import { apply_diff_stream, close_stream } from "./stream";
31
31
  import { Client } from "../client";
32
32
  import {
33
+ read_run_history_storage,
33
34
  start_run_history,
34
35
  update_run_history,
35
36
  update_run_inputs
@@ -63,7 +64,7 @@ export function submit(
63
64
  api_prefix
64
65
  } = this;
65
66
 
66
- const addt_headers = additional_headers || { "x-gradio-user": "api" };
67
+ const base_headers = additional_headers || { "x-gradio-user": "api" };
67
68
 
68
69
  const that = this;
69
70
 
@@ -114,6 +115,13 @@ export function submit(
114
115
  const history_enabled =
115
116
  config.run_history !== false && this.options.record_history !== false;
116
117
  const history_scope = { app_id: config.app_id, username: config.username };
118
+ const history_storage = read_run_history_storage(history_scope);
119
+ const addt_headers = {
120
+ ...base_headers,
121
+ ...(history_enabled && history_storage.type === "bucket"
122
+ ? { "x-gradio-history-bucket": history_storage.bucket_id }
123
+ : {})
124
+ };
117
125
  const history_run_id =
118
126
  !history_enabled || !is_documented_endpoint
119
127
  ? null
@@ -385,7 +393,8 @@ export function submit(
385
393
  ...payload,
386
394
  session_hash,
387
395
  event_id
388
- }
396
+ },
397
+ addt_headers
389
398
  );
390
399
  if (status !== 200) {
391
400
  fire_event({