@workglow/util 0.0.89 → 0.0.91

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/dist/bun.js CHANGED
@@ -123,7 +123,7 @@ class EventEmitter {
123
123
  return this;
124
124
  }
125
125
  waitOn(event) {
126
- return new Promise((resolve, reject) => {
126
+ return new Promise((resolve) => {
127
127
  const listener = (...args) => {
128
128
  resolve(args);
129
129
  };
@@ -133,7 +133,7 @@ class EventEmitter {
133
133
  emit(event, ...args) {
134
134
  const listeners = this.listeners[event];
135
135
  if (listeners) {
136
- listeners.forEach(({ listener, once }) => {
136
+ listeners.forEach(({ listener }) => {
137
137
  listener(...args);
138
138
  });
139
139
  this.listeners[event] = listeners.filter((l) => !l.once);
@@ -1445,6 +1445,34 @@ var TensorSchema = (annotations = {}) => ({
1445
1445
  additionalProperties: false,
1446
1446
  ...annotations
1447
1447
  });
1448
+ // src/vector/TypedArrayUtils.ts
1449
+ var WIDTH_RANK = {
1450
+ Float64Array: 6,
1451
+ Float32Array: 5,
1452
+ Float16Array: 4,
1453
+ Int16Array: 3,
1454
+ Uint16Array: 3,
1455
+ Int8Array: 2,
1456
+ Uint8Array: 2
1457
+ };
1458
+ function getWidthRank(arr) {
1459
+ return WIDTH_RANK[arr.constructor.name] ?? 0;
1460
+ }
1461
+ function widestConstructor(sources) {
1462
+ let best = sources[0];
1463
+ for (let i = 1;i < sources.length; i++) {
1464
+ if (getWidthRank(sources[i]) > getWidthRank(best))
1465
+ best = sources[i];
1466
+ }
1467
+ return best.constructor;
1468
+ }
1469
+ function createTypedArrayFrom(sources, values) {
1470
+ const Ctor = widestConstructor(sources);
1471
+ const result = new Ctor(values.length);
1472
+ for (let i = 0;i < values.length; i++)
1473
+ result[i] = values[i];
1474
+ return result;
1475
+ }
1448
1476
  // src/vector/VectorSimilarityUtils.ts
1449
1477
  function cosineSimilarity(a, b) {
1450
1478
  if (a.length !== b.length) {
@@ -1656,6 +1684,80 @@ class WorkerManager {
1656
1684
  worker.postMessage(message, transferables);
1657
1685
  });
1658
1686
  }
1687
+ async* callWorkerStreamFunction(workerName, functionName, args, options) {
1688
+ const worker = this.workers.get(workerName);
1689
+ if (!worker)
1690
+ throw new Error(`Worker ${workerName} not found.`);
1691
+ await this.readyWorkers.get(workerName);
1692
+ const requestId = crypto.randomUUID();
1693
+ const queue = [];
1694
+ let waiting = null;
1695
+ const notify = () => {
1696
+ if (waiting) {
1697
+ const resolve = waiting;
1698
+ waiting = null;
1699
+ resolve();
1700
+ }
1701
+ };
1702
+ const handleMessage = (event) => {
1703
+ const { id, type, data } = event.data;
1704
+ if (id !== requestId)
1705
+ return;
1706
+ if (type === "stream_chunk") {
1707
+ queue.push({ kind: "event", data });
1708
+ notify();
1709
+ } else if (type === "complete") {
1710
+ queue.push({ kind: "done" });
1711
+ notify();
1712
+ } else if (type === "error") {
1713
+ queue.push({ kind: "error", error: new Error(data) });
1714
+ notify();
1715
+ }
1716
+ };
1717
+ const handleAbort = () => {
1718
+ worker.postMessage({ id: requestId, type: "abort" });
1719
+ };
1720
+ const cleanup = () => {
1721
+ worker.removeEventListener("message", handleMessage);
1722
+ options?.signal?.removeEventListener("abort", handleAbort);
1723
+ };
1724
+ worker.addEventListener("message", handleMessage);
1725
+ if (options?.signal) {
1726
+ if (options.signal.aborted) {
1727
+ cleanup();
1728
+ throw new Error("Operation aborted");
1729
+ }
1730
+ options.signal.addEventListener("abort", handleAbort, { once: true });
1731
+ }
1732
+ const message = { id: requestId, type: "call", functionName, args, stream: true };
1733
+ const transferables = extractTransferables(message);
1734
+ worker.postMessage(message, transferables);
1735
+ let completedNormally = false;
1736
+ try {
1737
+ while (true) {
1738
+ while (queue.length > 0) {
1739
+ const item = queue.shift();
1740
+ if (item.kind === "event") {
1741
+ yield item.data;
1742
+ } else if (item.kind === "done") {
1743
+ completedNormally = true;
1744
+ return;
1745
+ } else if (item.kind === "error") {
1746
+ completedNormally = true;
1747
+ throw item.error;
1748
+ }
1749
+ }
1750
+ await new Promise((resolve) => {
1751
+ waiting = resolve;
1752
+ });
1753
+ }
1754
+ } finally {
1755
+ if (!completedNormally) {
1756
+ worker.postMessage({ id: requestId, type: "abort" });
1757
+ }
1758
+ cleanup();
1759
+ }
1760
+ }
1659
1761
  }
1660
1762
  var WORKER_MANAGER = createServiceToken("worker.manager");
1661
1763
  globalServiceRegistry.register(WORKER_MANAGER, () => new WorkerManager, true);
@@ -1706,6 +1808,7 @@ class WorkerServer {
1706
1808
  });
1707
1809
  }
1708
1810
  functions = {};
1811
+ streamFunctions = {};
1709
1812
  requestControllers = new Map;
1710
1813
  completedRequests = new Set;
1711
1814
  postResult = (id, result) => {
@@ -1723,15 +1826,27 @@ class WorkerServer {
1723
1826
  this.completedRequests.add(id);
1724
1827
  postMessage({ id, type: "error", data: errorMessage });
1725
1828
  };
1829
+ postStreamChunk = (id, event) => {
1830
+ if (this.completedRequests.has(id)) {
1831
+ return;
1832
+ }
1833
+ postMessage({ id, type: "stream_chunk", data: event });
1834
+ };
1726
1835
  registerFunction(name, fn) {
1727
1836
  this.functions[name] = fn;
1728
1837
  }
1838
+ registerStreamFunction(name, fn) {
1839
+ this.streamFunctions[name] = fn;
1840
+ }
1729
1841
  async handleMessage(event) {
1730
- const { id, type, functionName, args } = event.data;
1842
+ const { id, type, functionName, args, stream } = event.data;
1731
1843
  if (type === "abort") {
1732
1844
  return await this.handleAbort(id);
1733
1845
  }
1734
1846
  if (type === "call") {
1847
+ if (stream) {
1848
+ return await this.handleStreamCall(id, functionName, args);
1849
+ }
1735
1850
  return await this.handleCall(id, functionName, args);
1736
1851
  }
1737
1852
  }
@@ -1768,6 +1883,48 @@ class WorkerServer {
1768
1883
  }, 1000);
1769
1884
  }
1770
1885
  }
1886
+ async handleStreamCall(id, functionName, [input, model]) {
1887
+ if (functionName in this.streamFunctions) {
1888
+ try {
1889
+ const abortController = new AbortController;
1890
+ this.requestControllers.set(id, abortController);
1891
+ const fn = this.streamFunctions[functionName];
1892
+ const iterable = fn(input, model, abortController.signal);
1893
+ for await (const event of iterable) {
1894
+ if (this.completedRequests.has(id))
1895
+ break;
1896
+ this.postStreamChunk(id, event);
1897
+ }
1898
+ this.postResult(id, undefined);
1899
+ } catch (error) {
1900
+ this.postError(id, error.message);
1901
+ } finally {
1902
+ this.requestControllers.delete(id);
1903
+ setTimeout(() => {
1904
+ this.completedRequests.delete(id);
1905
+ }, 1000);
1906
+ }
1907
+ } else if (functionName in this.functions) {
1908
+ try {
1909
+ const abortController = new AbortController;
1910
+ this.requestControllers.set(id, abortController);
1911
+ const fn = this.functions[functionName];
1912
+ const noopProgress = () => {};
1913
+ const result = await fn(input, model, noopProgress, abortController.signal);
1914
+ this.postStreamChunk(id, { type: "finish", data: result });
1915
+ this.postResult(id, undefined);
1916
+ } catch (error) {
1917
+ this.postError(id, error.message);
1918
+ } finally {
1919
+ this.requestControllers.delete(id);
1920
+ setTimeout(() => {
1921
+ this.completedRequests.delete(id);
1922
+ }, 1000);
1923
+ }
1924
+ } else {
1925
+ this.postError(id, `Function ${functionName} not found`);
1926
+ }
1927
+ }
1771
1928
  }
1772
1929
  var WORKER_SERVER = createServiceToken("worker.server");
1773
1930
  globalServiceRegistry.register(WORKER_SERVER, () => new WorkerServer, true);
@@ -1876,6 +2033,7 @@ export {
1876
2033
  forceArray,
1877
2034
  deepEqual,
1878
2035
  decompress,
2036
+ createTypedArrayFrom,
1879
2037
  createServiceToken,
1880
2038
  cosineSimilarity,
1881
2039
  convertImageDataToUseableForm,
@@ -1906,4 +2064,4 @@ export {
1906
2064
  BaseError
1907
2065
  };
1908
2066
 
1909
- //# debugId=5C29A628C26F3F6064756E2164756E21
2067
+ //# debugId=68D25BD2B19CF5FC64756E2164756E21