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