@gradio/client 2.4.0 → 2.5.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.
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,OAAO,EACP,OAAO,EACP,aAAa,EACb,MAAM,EACN,gBAAgB,EAChB,YAAY,EACZ,SAAS,EACT,aAAa,EACb,WAAW,EACX,MAAM,EACN,cAAc,EAEd,cAAc,EACd,WAAW,EACX,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAU,QAAQ,EAAE,MAAM,UAAU,CAAC;AA2B5C,qBAAa,MAAM;IAClB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,aAAa,CAAC;IACvB,SAAS,EAAE,MAAM,GAAG,IAAI,CAAQ;IAEhC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC;IAC3B,UAAU,SAAM;IAChB,QAAQ,EAAE,OAAO,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;IACzC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IACrC,YAAY,EAAE,MAAM,CAA2C;IAC/D,GAAG,EAAE,MAAM,GAAG,KAAK,CAAS;IAC5B,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC,CAAM;IAElD,OAAO,CAAC,OAAO,CAAuB;IAGtC,aAAa;;MAAmB;IAChC,MAAM,UAAS;IACf,uBAAuB,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAM;IACtD,oBAAoB,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC,CAAM;IACnD,eAAe,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC,CAAM;IACxE,eAAe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ance;
1430
1430
  }
1431
1431
  //#endregion
1432
+ //#region src/utils/run_history.ts
1433
+ var KEY_ROOT = "gradio:run-history:";
1434
+ var STORAGE_PREFIX = `${KEY_ROOT}v2:`;
1435
+ var REPLAY_PREFIX = `${KEY_ROOT}replay:v2:`;
1436
+ var MAX_RUNS = 100;
1437
+ var MAX_APPS = 8;
1438
+ /**
1439
+ * Run history is a side effect of submitting, never the point of it, so no
1440
+ * failure in here may propagate into the caller and break the app. Every
1441
+ * exported function routes through this.
1442
+ */
1443
+ function safely(operation, fallback) {
1444
+ try {
1445
+ return operation();
1446
+ } catch (error) {
1447
+ console.warn("Could not update the run history.", error);
1448
+ return fallback;
1449
+ }
1450
+ }
1451
+ /**
1452
+ * The URL of the run history page for an app. `root` never carries a trailing
1453
+ * slash, so it cannot simply be concatenated with the path.
1454
+ */
1455
+ function run_history_url(root, api_prefix = "/gradio_api") {
1456
+ return `${root.replace(/\/+$/, "")}${api_prefix}/runs`;
1457
+ }
1458
+ function storage_key(scope) {
1459
+ if (typeof window === "undefined") return null;
1460
+ const app_id = scope?.app_id;
1461
+ if (app_id === null || app_id === void 0 || app_id === "") return null;
1462
+ try {
1463
+ if (!window.localStorage) return null;
1464
+ return `${STORAGE_PREFIX}${app_id}${scope?.username ? `:user:${encodeURIComponent(scope.username)}` : ""}`;
1465
+ } catch {
1466
+ return null;
1467
+ }
1468
+ }
1469
+ function replay_key(scope) {
1470
+ const key = storage_key(scope);
1471
+ return key ? key.replace(STORAGE_PREFIX, REPLAY_PREFIX) : null;
1472
+ }
1473
+ /** When a run was most recently saved under a key, for deciding what to drop. */
1474
+ function last_saved_at(key) {
1475
+ try {
1476
+ const runs = JSON.parse(window.localStorage.getItem(key) || "[]");
1477
+ return Array.isArray(runs) && runs.length ? Date.parse(runs[0]?.started_at) || 0 : 0;
1478
+ } catch {
1479
+ return 0;
1480
+ }
1481
+ }
1482
+ /**
1483
+ * Drops the histories of long-gone app instances. Every restart mints a new app
1484
+ * id, so without this the browser would keep every history an app ever had and
1485
+ * eventually run out of room for the current one.
1486
+ */
1487
+ function prune_apps(current_key) {
1488
+ const keys = Object.keys(window.localStorage).filter((key) => key.startsWith(KEY_ROOT));
1489
+ const stale = [...keys.filter((key) => !key.startsWith(STORAGE_PREFIX) && !key.startsWith(REPLAY_PREFIX)), ...keys.filter((key) => key.startsWith(STORAGE_PREFIX) && key !== current_key).sort((a, b) => last_saved_at(b) - last_saved_at(a)).slice(MAX_APPS - 1)];
1490
+ for (const key of stale) try {
1491
+ window.localStorage.removeItem(key);
1492
+ window.sessionStorage.removeItem(key.replace(STORAGE_PREFIX, REPLAY_PREFIX));
1493
+ } catch {}
1494
+ }
1495
+ function make_id() {
1496
+ if (typeof crypto !== "undefined" && crypto.randomUUID) return crypto.randomUUID();
1497
+ return `${Date.now()}-${Math.random().toString(36).slice(2)}`;
1498
+ }
1499
+ function clone_for_storage(value) {
1500
+ const seen = /* @__PURE__ */ new WeakSet();
1501
+ try {
1502
+ return JSON.parse(JSON.stringify(value, (_key, item) => {
1503
+ if (typeof item === "bigint") return item.toString();
1504
+ if (typeof item === "object" && item !== null) {
1505
+ if (seen.has(item)) return "[Circular]";
1506
+ seen.add(item);
1507
+ }
1508
+ return item;
1509
+ }));
1510
+ } catch {
1511
+ return "[Unserializable value]";
1512
+ }
1513
+ }
1514
+ function read_run_history_impl(scope) {
1515
+ const key = storage_key(scope);
1516
+ if (!key) return [];
1517
+ try {
1518
+ const value = JSON.parse(window.localStorage.getItem(key) || "[]");
1519
+ return Array.isArray(value) ? value : [];
1520
+ } catch {
1521
+ return [];
1522
+ }
1523
+ }
1524
+ function write_run_history(scope, runs) {
1525
+ const key = storage_key(scope);
1526
+ if (!key) return;
1527
+ let next = runs.slice(0, MAX_RUNS);
1528
+ let pruned = false;
1529
+ while (next.length > 0) try {
1530
+ window.localStorage.setItem(key, JSON.stringify(next));
1531
+ return;
1532
+ } catch {
1533
+ if (!pruned) {
1534
+ pruned = true;
1535
+ prune_apps(key);
1536
+ continue;
1537
+ }
1538
+ next = next.slice(0, -1);
1539
+ }
1540
+ try {
1541
+ window.localStorage.removeItem(key);
1542
+ } catch {}
1543
+ }
1544
+ function clear_run_history_impl(scope) {
1545
+ const key = storage_key(scope);
1546
+ if (!key) return;
1547
+ try {
1548
+ window.localStorage.removeItem(key);
1549
+ } catch {}
1550
+ notify_run_history_change();
1551
+ }
1552
+ function delete_run_history_impl(scope, id) {
1553
+ write_run_history(scope, read_run_history_impl(scope).filter((run) => run.id !== id));
1554
+ notify_run_history_change();
1555
+ }
1556
+ var CHANGE_EVENT = "gradio:run-history-change";
1557
+ function notify_run_history_change() {
1558
+ if (typeof window === "undefined") return;
1559
+ try {
1560
+ window.dispatchEvent(new Event(CHANGE_EVENT));
1561
+ } catch {}
1562
+ }
1563
+ /**
1564
+ * Subscribes to runs being added, deleted or cleared. `storage` covers other
1565
+ * tabs; the custom event covers this one, which `storage` never fires for.
1566
+ * Only counts change, not per-run progress, so listeners stay cheap.
1567
+ *
1568
+ * @returns a function that unsubscribes.
1569
+ */
1570
+ function on_run_history_change_impl(listener) {
1571
+ if (typeof window === "undefined") return () => {};
1572
+ window.addEventListener(CHANGE_EVENT, listener);
1573
+ window.addEventListener("storage", listener);
1574
+ return () => {
1575
+ window.removeEventListener(CHANGE_EVENT, listener);
1576
+ window.removeEventListener("storage", listener);
1577
+ };
1578
+ }
1579
+ function stage_run_history_replay_impl(scope, run) {
1580
+ const key = replay_key(scope);
1581
+ if (!key) return;
1582
+ try {
1583
+ window.sessionStorage.setItem(key, JSON.stringify(run));
1584
+ } catch {}
1585
+ }
1586
+ function consume_run_history_replay_impl(scope) {
1587
+ const key = replay_key(scope);
1588
+ if (!key) return null;
1589
+ try {
1590
+ const value = window.sessionStorage.getItem(key);
1591
+ window.sessionStorage.removeItem(key);
1592
+ return value ? JSON.parse(value) : null;
1593
+ } catch {
1594
+ return null;
1595
+ }
1596
+ }
1597
+ function start_run_history_impl(options) {
1598
+ const key = storage_key(options);
1599
+ if (!key) return null;
1600
+ prune_apps(key);
1601
+ const run = {
1602
+ id: make_id(),
1603
+ endpoint: options.endpoint,
1604
+ api_name: options.api_name,
1605
+ fn_index: options.fn_index,
1606
+ page: `${window.location.pathname}${window.location.search}`,
1607
+ inputs: clone_for_storage(options.inputs),
1608
+ outputs: null,
1609
+ ...options.input_components ? { input_components: clone_for_storage(options.input_components) } : {},
1610
+ ...options.output_components ? { output_components: clone_for_storage(options.output_components) } : {},
1611
+ status: "running",
1612
+ started_at: (/* @__PURE__ */ new Date()).toISOString()
1613
+ };
1614
+ write_run_history(options, [run, ...read_run_history_impl(options)]);
1615
+ notify_run_history_change();
1616
+ return run.id;
1617
+ }
1618
+ function update_run_inputs_impl(scope, id, inputs) {
1619
+ if (!id) return;
1620
+ const runs = read_run_history_impl(scope);
1621
+ const run = runs.find((item) => item.id === id);
1622
+ if (!run) return;
1623
+ run.inputs = clone_for_storage(inputs);
1624
+ write_run_history(scope, runs);
1625
+ }
1626
+ function update_run_history_impl(scope, id, event) {
1627
+ if (!id) return;
1628
+ const runs = read_run_history_impl(scope);
1629
+ const run = runs.find((item) => item.id === id);
1630
+ if (!run) return;
1631
+ let finished = false;
1632
+ if (event.type === "data") {
1633
+ run.outputs = clone_for_storage(event.data);
1634
+ deferred_outputs.set(id, run.outputs);
1635
+ } else if (event.type === "status" && event.original_msg === "process_starts") mark_process_start(run, event.time);
1636
+ else if (event.type === "status" && (event.stage === "generating" || event.stage === "streaming")) run.streamed = true;
1637
+ else if (event.type === "status" && event.stage === "complete") {
1638
+ run.status = "completed";
1639
+ mark_complete(run, event);
1640
+ finished = true;
1641
+ } else if (event.type === "status" && event.stage === "error") {
1642
+ run.status = "failed";
1643
+ run.error = typeof event.message === "string" ? event.message : JSON.stringify(event.message || "Unknown error");
1644
+ mark_complete(run, event);
1645
+ finished = true;
1646
+ } else return;
1647
+ if (finished) {
1648
+ if (deferred_outputs.has(id)) run.outputs = deferred_outputs.get(id);
1649
+ deferred_outputs.delete(id);
1650
+ last_write.delete(id);
1651
+ } else if (run.streamed) {
1652
+ if (Date.now() - (last_write.get(id) ?? 0) < STREAM_WRITE_INTERVAL_MS) return;
1653
+ last_write.set(id, Date.now());
1654
+ deferred_outputs.delete(id);
1655
+ }
1656
+ write_run_history(scope, runs);
1657
+ }
1658
+ var STREAM_WRITE_INTERVAL_MS = 500;
1659
+ /** When each in-flight streamed run was last written. */
1660
+ var last_write = /* @__PURE__ */ new Map();
1661
+ /** The newest output of a streamed run that has not been written yet. */
1662
+ var deferred_outputs = /* @__PURE__ */ new Map();
1663
+ function mark_process_start(run, time) {
1664
+ if (run.process_started_at) return;
1665
+ const started = time || /* @__PURE__ */ new Date();
1666
+ run.process_started_at = started.toISOString();
1667
+ run.queued_ms = Math.max(0, started.getTime() - Date.parse(run.started_at));
1668
+ }
1669
+ function mark_complete(run, event) {
1670
+ const completed = event.time || /* @__PURE__ */ new Date();
1671
+ run.completed_at = completed.toISOString();
1672
+ run.duration_ms = (!run.streamed && typeof event.cache_duration === "number" && event.cache_duration >= 0 ? event.cache_duration * 1e3 : null) ?? Math.max(0, completed.getTime() - Date.parse(run.process_started_at || run.started_at));
1673
+ }
1674
+ function read_run_history(scope) {
1675
+ return safely(() => read_run_history_impl(scope), []);
1676
+ }
1677
+ function clear_run_history(scope) {
1678
+ safely(() => clear_run_history_impl(scope), void 0);
1679
+ }
1680
+ function delete_run_history(scope, id) {
1681
+ safely(() => delete_run_history_impl(scope, id), void 0);
1682
+ }
1683
+ function stage_run_history_replay(scope, run) {
1684
+ safely(() => stage_run_history_replay_impl(scope, run), void 0);
1685
+ }
1686
+ function consume_run_history_replay(scope) {
1687
+ return safely(() => consume_run_history_replay_impl(scope), null);
1688
+ }
1689
+ function start_run_history(options) {
1690
+ return safely(() => start_run_history_impl(options), null);
1691
+ }
1692
+ function update_run_inputs(scope, id, inputs) {
1693
+ safely(() => update_run_inputs_impl(scope, id, inputs), void 0);
1694
+ }
1695
+ function update_run_history(scope, id, event) {
1696
+ safely(() => update_run_history_impl(scope, id, event), void 0);
1697
+ }
1698
+ function on_run_history_change(listener) {
1699
+ return safely(() => on_run_history_change_impl(() => safely(listener, void 0)), () => {});
1700
+ }
1701
+ //#endregion
1432
1702
  //#region src/utils/submit.ts
1433
1703
  function submit(endpoint, data = {}, event_data, trigger_id, all_events, additional_headers) {
1434
1704
  try {
@@ -1440,9 +1710,35 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
1440
1710
  if (!config) throw new Error("Could not resolve app config");
1441
1711
  let { fn_index, endpoint_info, dependency } = get_endpoint_info(api_info, endpoint, api_map, config);
1442
1712
  let resolved_data = map_data_to_params(data, endpoint_info);
1443
- let stream;
1444
1713
  let protocol = config.protocol ?? "ws";
1445
1714
  if (protocol === "ws") throw new Error(WS_PROTOCOL_MSG);
1715
+ const history_endpoint = typeof dependency.api_name === "string" ? `/${dependency.api_name}` : endpoint;
1716
+ const history_api_name = typeof dependency.api_name === "string" ? `/${dependency.api_name}` : `Function ${fn_index}`;
1717
+ const component_metadata = (id) => {
1718
+ const component = config.components.find((item) => item.id === id);
1719
+ if (!component) return null;
1720
+ return {
1721
+ type: component.type,
1722
+ component_class_id: component.component_class_id,
1723
+ props: component.props
1724
+ };
1725
+ };
1726
+ const is_documented_endpoint = dependency.api_visibility === "public";
1727
+ const history_enabled = config.run_history !== false && this.options.record_history !== false;
1728
+ const history_scope = {
1729
+ app_id: config.app_id,
1730
+ username: config.username
1731
+ };
1732
+ const history_run_id = !history_enabled || !is_documented_endpoint ? null : start_run_history({
1733
+ ...history_scope,
1734
+ endpoint: history_endpoint,
1735
+ api_name: history_api_name,
1736
+ fn_index,
1737
+ inputs: handle_payload(resolved_data, dependency, config.components, "input", true),
1738
+ input_components: dependency.inputs.map(component_metadata),
1739
+ output_components: dependency.outputs.map(component_metadata)
1740
+ });
1741
+ let stream;
1446
1742
  let event_id_final = "";
1447
1743
  const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
1448
1744
  let payload;
@@ -1455,6 +1751,7 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
1455
1751
  return acc;
1456
1752
  }, {}) || {};
1457
1753
  function fire_event(event) {
1754
+ update_run_history(history_scope, history_run_id, event);
1458
1755
  if (all_events || events_to_publish[event.type]) push_event(event);
1459
1756
  }
1460
1757
  async function cancel() {
@@ -1502,8 +1799,10 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
1502
1799
  });
1503
1800
  }
1504
1801
  const job = this.handle_blob(config.root, resolved_data, endpoint_info).then(async (_payload) => {
1802
+ let input_data = handle_payload(_payload, dependency, config.components, "input", true);
1803
+ update_run_inputs(history_scope, history_run_id, input_data || []);
1505
1804
  payload = {
1506
- data: handle_payload(_payload, dependency, config.components, "input", true) || [],
1805
+ data: input_data || [],
1507
1806
  event_data,
1508
1807
  fn_index,
1509
1808
  trigger_id,
@@ -2154,6 +2453,10 @@ var Client = class {
2154
2453
  async config_success(_config) {
2155
2454
  this.config = _config;
2156
2455
  this.api_prefix = _config.api_prefix || "";
2456
+ if (_config.run_history === false) clear_run_history({
2457
+ app_id: _config.app_id,
2458
+ username: _config.username
2459
+ });
2157
2460
  if (this.config.auth_required) return this.prepare_return_obj();
2158
2461
  try {
2159
2462
  this.api_info = await this.view_api();
@@ -2264,11 +2567,18 @@ async function duplicate_space(app_reference, options) {
2264
2567
  exports.Client = Client;
2265
2568
  exports.FileData = FileData;
2266
2569
  exports.MISSING_CREDENTIALS_MSG = MISSING_CREDENTIALS_MSG;
2570
+ exports.clear_run_history = clear_run_history;
2267
2571
  exports.client = client;
2572
+ exports.consume_run_history_replay = consume_run_history_replay;
2573
+ exports.delete_run_history = delete_run_history;
2268
2574
  exports.duplicate = duplicate_space;
2269
2575
  exports.handle_file = handle_file;
2576
+ exports.on_run_history_change = on_run_history_change;
2270
2577
  exports.predict = predict;
2271
2578
  exports.prepare_files = prepare_files;
2579
+ exports.read_run_history = read_run_history;
2580
+ exports.run_history_url = run_history_url;
2581
+ exports.stage_run_history_replay = stage_run_history_replay;
2272
2582
  exports.submit = submit;
2273
2583
  exports.upload = upload;
2274
2584
  exports.upload_files = upload_files;
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { submit } from "./utils/submit";
4
4
  export { upload_files } from "./utils/upload_files";
5
5
  export { FileData, upload, prepare_files } from "./upload";
6
6
  export { handle_file } from "./helpers/data";
7
+ export { clear_run_history, consume_run_history_replay, delete_run_history, on_run_history_change, read_run_history, run_history_url, stage_run_history_replay, type RunHistoryScope, type StoredRunComponent, type StoredRun } from "./utils/run_history";
7
8
  export type { SpaceStatus, StatusMessage, Status, client_return, UploadResponse, RenderMessage, LogMessage, Payload, Config, ValidationError } from "./types";
8
9
  export { MISSING_CREDENTIALS_MSG } from "./constants";
9
10
  export { client } from "./client";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AACxC,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,YAAY,EACX,WAAW,EACX,aAAa,EACb,MAAM,EACN,aAAa,EACb,cAAc,EACd,aAAa,EACb,UAAU,EACV,OAAO,EACP,MAAM,EACN,eAAe,EACf,MAAM,SAAS,CAAC;AAEjB,OAAO,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;AAGtD,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,eAAe,IAAI,SAAS,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"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