@gradio/client 2.4.0 → 2.5.1
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 +12 -0
- package/dist/browser.js +586 -341
- package/dist/client.d.ts.map +1 -1
- package/dist/index.cjs +344 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +337 -3
- package/dist/index.min.js +588 -343
- package/dist/types.d.ts +14 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/run_history.d.ts +90 -0
- package/dist/utils/run_history.d.ts.map +1 -0
- package/dist/utils/submit.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +10 -0
- package/src/index.ts +14 -0
- package/src/test/run_history.test.ts +412 -0
- package/src/types.ts +14 -0
- package/src/utils/run_history.ts +589 -0
- package/src/utils/submit.ts +62 -2
package/dist/client.d.ts.map
CHANGED
|
@@ -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;
|
|
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;AA4B5C,qBAAa,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,OAAO,CAAC,MAAM,CAAC,CAEjB;AAED;;;;;;;GAOG;AACH,wBAAsB,eAAe,CACpC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,gBAAgB,GACvB,OAAO,CAAC,MAAM,CAAC,CAEjB;AAED,MAAM,MAAM,cAAc,GAAG,MAAM,CAAC"}
|
package/dist/index.cjs
CHANGED
|
@@ -1429,6 +1429,307 @@ function readable_stream(input, init = {}) {
|
|
|
1429
1429
|
return instance;
|
|
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 restore_run_impl(config, run) {
|
|
1598
|
+
const dependency = config.dependencies.find((item) => item.id === run.fn_index || typeof item.api_name === "string" && `/${item.api_name.replace(/^\//, "")}` === run.api_name);
|
|
1599
|
+
if (!dependency) return false;
|
|
1600
|
+
const inputs = Array.isArray(run.inputs) ? run.inputs : Object.values(run.inputs ?? {});
|
|
1601
|
+
const outputs = Array.isArray(run.outputs) ? run.outputs : run.outputs === null || run.outputs === void 0 ? [] : [run.outputs];
|
|
1602
|
+
const restore = (ids, saved) => {
|
|
1603
|
+
for (const [index, id] of ids.entries()) {
|
|
1604
|
+
const component = config.components.find((item) => item.id === id);
|
|
1605
|
+
if (!component || index >= saved.length) continue;
|
|
1606
|
+
if (component.type === "state") continue;
|
|
1607
|
+
component.props.value = saved[index];
|
|
1608
|
+
}
|
|
1609
|
+
};
|
|
1610
|
+
restore(dependency.inputs, inputs);
|
|
1611
|
+
restore(dependency.outputs, outputs);
|
|
1612
|
+
return true;
|
|
1613
|
+
}
|
|
1614
|
+
function apply_run_history_replay_impl(config) {
|
|
1615
|
+
const run = consume_run_history_replay_impl(config);
|
|
1616
|
+
return run ? restore_run_impl(config, run) : false;
|
|
1617
|
+
}
|
|
1618
|
+
function start_run_history_impl(options) {
|
|
1619
|
+
const key = storage_key(options);
|
|
1620
|
+
if (!key) return null;
|
|
1621
|
+
prune_apps(key);
|
|
1622
|
+
const run = {
|
|
1623
|
+
id: make_id(),
|
|
1624
|
+
endpoint: options.endpoint,
|
|
1625
|
+
api_name: options.api_name,
|
|
1626
|
+
fn_index: options.fn_index,
|
|
1627
|
+
page: `${window.location.pathname}${window.location.search}`,
|
|
1628
|
+
inputs: clone_for_storage(options.inputs),
|
|
1629
|
+
outputs: null,
|
|
1630
|
+
...options.input_components ? { input_components: clone_for_storage(options.input_components) } : {},
|
|
1631
|
+
...options.output_components ? { output_components: clone_for_storage(options.output_components) } : {},
|
|
1632
|
+
status: "running",
|
|
1633
|
+
started_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
1634
|
+
};
|
|
1635
|
+
write_run_history(options, [run, ...read_run_history_impl(options)]);
|
|
1636
|
+
notify_run_history_change();
|
|
1637
|
+
return run.id;
|
|
1638
|
+
}
|
|
1639
|
+
function update_run_inputs_impl(scope, id, inputs) {
|
|
1640
|
+
if (!id) return;
|
|
1641
|
+
const runs = read_run_history_impl(scope);
|
|
1642
|
+
const run = runs.find((item) => item.id === id);
|
|
1643
|
+
if (!run) return;
|
|
1644
|
+
run.inputs = clone_for_storage(inputs);
|
|
1645
|
+
write_run_history(scope, runs);
|
|
1646
|
+
}
|
|
1647
|
+
function update_run_history_impl(scope, id, event) {
|
|
1648
|
+
if (!id) return;
|
|
1649
|
+
const runs = read_run_history_impl(scope);
|
|
1650
|
+
const run = runs.find((item) => item.id === id);
|
|
1651
|
+
if (!run) return;
|
|
1652
|
+
let finished = false;
|
|
1653
|
+
if (event.type === "data") {
|
|
1654
|
+
run.outputs = clone_for_storage(event.data);
|
|
1655
|
+
deferred_outputs.set(id, run.outputs);
|
|
1656
|
+
} else if (event.type === "status" && event.original_msg === "process_starts") mark_process_start(run, event.time);
|
|
1657
|
+
else if (event.type === "status" && (event.stage === "generating" || event.stage === "streaming")) run.streamed = true;
|
|
1658
|
+
else if (event.type === "status" && event.stage === "complete") {
|
|
1659
|
+
run.status = "completed";
|
|
1660
|
+
mark_complete(run, event);
|
|
1661
|
+
finished = true;
|
|
1662
|
+
} else if (event.type === "status" && event.stage === "error") {
|
|
1663
|
+
run.status = "failed";
|
|
1664
|
+
run.error = typeof event.message === "string" ? event.message : JSON.stringify(event.message || "Unknown error");
|
|
1665
|
+
mark_complete(run, event);
|
|
1666
|
+
finished = true;
|
|
1667
|
+
} else return;
|
|
1668
|
+
if (finished) {
|
|
1669
|
+
if (deferred_outputs.has(id)) run.outputs = deferred_outputs.get(id);
|
|
1670
|
+
deferred_outputs.delete(id);
|
|
1671
|
+
last_write.delete(id);
|
|
1672
|
+
} else if (run.streamed) {
|
|
1673
|
+
if (Date.now() - (last_write.get(id) ?? 0) < STREAM_WRITE_INTERVAL_MS) return;
|
|
1674
|
+
last_write.set(id, Date.now());
|
|
1675
|
+
deferred_outputs.delete(id);
|
|
1676
|
+
}
|
|
1677
|
+
write_run_history(scope, runs);
|
|
1678
|
+
}
|
|
1679
|
+
var STREAM_WRITE_INTERVAL_MS = 500;
|
|
1680
|
+
/** When each in-flight streamed run was last written. */
|
|
1681
|
+
var last_write = /* @__PURE__ */ new Map();
|
|
1682
|
+
/** The newest output of a streamed run that has not been written yet. */
|
|
1683
|
+
var deferred_outputs = /* @__PURE__ */ new Map();
|
|
1684
|
+
function mark_process_start(run, time) {
|
|
1685
|
+
if (run.process_started_at) return;
|
|
1686
|
+
const started = time || /* @__PURE__ */ new Date();
|
|
1687
|
+
run.process_started_at = started.toISOString();
|
|
1688
|
+
run.queued_ms = Math.max(0, started.getTime() - Date.parse(run.started_at));
|
|
1689
|
+
}
|
|
1690
|
+
function mark_complete(run, event) {
|
|
1691
|
+
const completed = event.time || /* @__PURE__ */ new Date();
|
|
1692
|
+
run.completed_at = completed.toISOString();
|
|
1693
|
+
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));
|
|
1694
|
+
}
|
|
1695
|
+
function read_run_history(scope) {
|
|
1696
|
+
return safely(() => read_run_history_impl(scope), []);
|
|
1697
|
+
}
|
|
1698
|
+
function clear_run_history(scope) {
|
|
1699
|
+
safely(() => clear_run_history_impl(scope), void 0);
|
|
1700
|
+
}
|
|
1701
|
+
function delete_run_history(scope, id) {
|
|
1702
|
+
safely(() => delete_run_history_impl(scope, id), void 0);
|
|
1703
|
+
}
|
|
1704
|
+
function stage_run_history_replay(scope, run) {
|
|
1705
|
+
safely(() => stage_run_history_replay_impl(scope, run), void 0);
|
|
1706
|
+
}
|
|
1707
|
+
function consume_run_history_replay(scope) {
|
|
1708
|
+
return safely(() => consume_run_history_replay_impl(scope), null);
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Applies the run staged by the history page, if this page load is the one it
|
|
1712
|
+
* was staged for. Every entry point that renders an app has to call this, or
|
|
1713
|
+
* "Load run" silently does nothing on that entry point.
|
|
1714
|
+
*
|
|
1715
|
+
* @returns whether a staged run was found and applied.
|
|
1716
|
+
*/
|
|
1717
|
+
function apply_run_history_replay(config) {
|
|
1718
|
+
return safely(() => apply_run_history_replay_impl(config), false);
|
|
1719
|
+
}
|
|
1720
|
+
function start_run_history(options) {
|
|
1721
|
+
return safely(() => start_run_history_impl(options), null);
|
|
1722
|
+
}
|
|
1723
|
+
function update_run_inputs(scope, id, inputs) {
|
|
1724
|
+
safely(() => update_run_inputs_impl(scope, id, inputs), void 0);
|
|
1725
|
+
}
|
|
1726
|
+
function update_run_history(scope, id, event) {
|
|
1727
|
+
safely(() => update_run_history_impl(scope, id, event), void 0);
|
|
1728
|
+
}
|
|
1729
|
+
function on_run_history_change(listener) {
|
|
1730
|
+
return safely(() => on_run_history_change_impl(() => safely(listener, void 0)), () => {});
|
|
1731
|
+
}
|
|
1732
|
+
//#endregion
|
|
1432
1733
|
//#region src/utils/submit.ts
|
|
1433
1734
|
function submit(endpoint, data = {}, event_data, trigger_id, all_events, additional_headers) {
|
|
1434
1735
|
try {
|
|
@@ -1440,9 +1741,35 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
|
|
|
1440
1741
|
if (!config) throw new Error("Could not resolve app config");
|
|
1441
1742
|
let { fn_index, endpoint_info, dependency } = get_endpoint_info(api_info, endpoint, api_map, config);
|
|
1442
1743
|
let resolved_data = map_data_to_params(data, endpoint_info);
|
|
1443
|
-
let stream;
|
|
1444
1744
|
let protocol = config.protocol ?? "ws";
|
|
1445
1745
|
if (protocol === "ws") throw new Error(WS_PROTOCOL_MSG);
|
|
1746
|
+
const history_endpoint = typeof dependency.api_name === "string" ? `/${dependency.api_name}` : endpoint;
|
|
1747
|
+
const history_api_name = typeof dependency.api_name === "string" ? `/${dependency.api_name}` : `Function ${fn_index}`;
|
|
1748
|
+
const component_metadata = (id) => {
|
|
1749
|
+
const component = config.components.find((item) => item.id === id);
|
|
1750
|
+
if (!component) return null;
|
|
1751
|
+
return {
|
|
1752
|
+
type: component.type,
|
|
1753
|
+
component_class_id: component.component_class_id,
|
|
1754
|
+
props: component.props
|
|
1755
|
+
};
|
|
1756
|
+
};
|
|
1757
|
+
const is_documented_endpoint = dependency.api_visibility === "public";
|
|
1758
|
+
const history_enabled = config.run_history !== false && this.options.record_history !== false;
|
|
1759
|
+
const history_scope = {
|
|
1760
|
+
app_id: config.app_id,
|
|
1761
|
+
username: config.username
|
|
1762
|
+
};
|
|
1763
|
+
const history_run_id = !history_enabled || !is_documented_endpoint ? null : start_run_history({
|
|
1764
|
+
...history_scope,
|
|
1765
|
+
endpoint: history_endpoint,
|
|
1766
|
+
api_name: history_api_name,
|
|
1767
|
+
fn_index,
|
|
1768
|
+
inputs: handle_payload(resolved_data, dependency, config.components, "input", true),
|
|
1769
|
+
input_components: dependency.inputs.map(component_metadata),
|
|
1770
|
+
output_components: dependency.outputs.map(component_metadata)
|
|
1771
|
+
});
|
|
1772
|
+
let stream;
|
|
1446
1773
|
let event_id_final = "";
|
|
1447
1774
|
const _endpoint = typeof endpoint === "number" ? "/predict" : endpoint;
|
|
1448
1775
|
let payload;
|
|
@@ -1455,6 +1782,7 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
|
|
|
1455
1782
|
return acc;
|
|
1456
1783
|
}, {}) || {};
|
|
1457
1784
|
function fire_event(event) {
|
|
1785
|
+
update_run_history(history_scope, history_run_id, event);
|
|
1458
1786
|
if (all_events || events_to_publish[event.type]) push_event(event);
|
|
1459
1787
|
}
|
|
1460
1788
|
async function cancel() {
|
|
@@ -1502,8 +1830,10 @@ function submit(endpoint, data = {}, event_data, trigger_id, all_events, additio
|
|
|
1502
1830
|
});
|
|
1503
1831
|
}
|
|
1504
1832
|
const job = this.handle_blob(config.root, resolved_data, endpoint_info).then(async (_payload) => {
|
|
1833
|
+
let input_data = handle_payload(_payload, dependency, config.components, "input", true);
|
|
1834
|
+
update_run_inputs(history_scope, history_run_id, input_data || []);
|
|
1505
1835
|
payload = {
|
|
1506
|
-
data:
|
|
1836
|
+
data: input_data || [],
|
|
1507
1837
|
event_data,
|
|
1508
1838
|
fn_index,
|
|
1509
1839
|
trigger_id,
|
|
@@ -2154,6 +2484,10 @@ var Client = class {
|
|
|
2154
2484
|
async config_success(_config) {
|
|
2155
2485
|
this.config = _config;
|
|
2156
2486
|
this.api_prefix = _config.api_prefix || "";
|
|
2487
|
+
if (_config.run_history === false) clear_run_history({
|
|
2488
|
+
app_id: _config.app_id,
|
|
2489
|
+
username: _config.username
|
|
2490
|
+
});
|
|
2157
2491
|
if (this.config.auth_required) return this.prepare_return_obj();
|
|
2158
2492
|
try {
|
|
2159
2493
|
this.api_info = await this.view_api();
|
|
@@ -2264,11 +2598,19 @@ async function duplicate_space(app_reference, options) {
|
|
|
2264
2598
|
exports.Client = Client;
|
|
2265
2599
|
exports.FileData = FileData;
|
|
2266
2600
|
exports.MISSING_CREDENTIALS_MSG = MISSING_CREDENTIALS_MSG;
|
|
2601
|
+
exports.apply_run_history_replay = apply_run_history_replay;
|
|
2602
|
+
exports.clear_run_history = clear_run_history;
|
|
2267
2603
|
exports.client = client;
|
|
2604
|
+
exports.consume_run_history_replay = consume_run_history_replay;
|
|
2605
|
+
exports.delete_run_history = delete_run_history;
|
|
2268
2606
|
exports.duplicate = duplicate_space;
|
|
2269
2607
|
exports.handle_file = handle_file;
|
|
2608
|
+
exports.on_run_history_change = on_run_history_change;
|
|
2270
2609
|
exports.predict = predict;
|
|
2271
2610
|
exports.prepare_files = prepare_files;
|
|
2611
|
+
exports.read_run_history = read_run_history;
|
|
2612
|
+
exports.run_history_url = run_history_url;
|
|
2613
|
+
exports.stage_run_history_replay = stage_run_history_replay;
|
|
2272
2614
|
exports.submit = submit;
|
|
2273
2615
|
exports.upload = upload;
|
|
2274
2616
|
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 { apply_run_history_replay, 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 ReplayTarget, 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";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"index.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