@workglow/util 0.0.85 → 0.0.87

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
@@ -78,6 +78,19 @@ class ServiceRegistry {
78
78
  }
79
79
  }
80
80
  var globalServiceRegistry = new ServiceRegistry(globalContainer);
81
+
82
+ // src/di/InputResolverRegistry.ts
83
+ var INPUT_RESOLVERS = createServiceToken("task.input.resolvers");
84
+ if (!globalServiceRegistry.has(INPUT_RESOLVERS)) {
85
+ globalServiceRegistry.register(INPUT_RESOLVERS, () => new Map, true);
86
+ }
87
+ function getInputResolvers() {
88
+ return globalServiceRegistry.get(INPUT_RESOLVERS);
89
+ }
90
+ function registerInputResolver(formatPrefix, resolver) {
91
+ const resolvers = getInputResolvers();
92
+ resolvers.set(formatPrefix, resolver);
93
+ }
81
94
  // src/events/EventEmitter.ts
82
95
  class EventEmitter {
83
96
  listeners = {};
@@ -964,7 +977,7 @@ function areObjectSchemasSemanticallyCompatible(sourceSchema, targetSchema) {
964
977
  return areSemanticallyCompatible(sourceSchema, targetSchema);
965
978
  }
966
979
  // src/json-schema/SchemaValidation.ts
967
- import { compileSchema } from "json-schema-library";
980
+ import { compileSchema } from "@sroussey/json-schema-library";
968
981
  // src/utilities/Misc.ts
969
982
  function forceArray(input) {
970
983
  if (Array.isArray(input))
@@ -1338,6 +1351,208 @@ function objectOfArraysAsArrayOfObjects(data) {
1338
1351
  }
1339
1352
  });
1340
1353
  }
1354
+ // src/vector/TypedArray.ts
1355
+ function isTypedArray(value) {
1356
+ return ArrayBuffer.isView(value) && !(value instanceof DataView);
1357
+ }
1358
+ var TypedArrayType = null;
1359
+ var TypedArraySchemaOptions = {
1360
+ ...FromSchemaDefaultOptions,
1361
+ deserialize: [
1362
+ {
1363
+ pattern: { type: "array", format: "TypedArray:Float64Array" },
1364
+ output: Float64Array
1365
+ },
1366
+ {
1367
+ pattern: { type: "array", format: "TypedArray:Float32Array" },
1368
+ output: Float32Array
1369
+ },
1370
+ {
1371
+ pattern: { type: "array", format: "TypedArray:Float16Array" },
1372
+ output: Float16Array
1373
+ },
1374
+ {
1375
+ pattern: { type: "array", format: "TypedArray:Int16Array" },
1376
+ output: Int16Array
1377
+ },
1378
+ {
1379
+ pattern: { type: "array", format: "TypedArray:Int8Array" },
1380
+ output: Int8Array
1381
+ },
1382
+ {
1383
+ pattern: { type: "array", format: "TypedArray:Uint8Array" },
1384
+ output: Uint8Array
1385
+ },
1386
+ {
1387
+ pattern: { type: "array", format: "TypedArray:Uint16Array" },
1388
+ output: Uint16Array
1389
+ },
1390
+ {
1391
+ pattern: { type: "array", format: "TypedArray" },
1392
+ output: TypedArrayType
1393
+ }
1394
+ ]
1395
+ };
1396
+ var TypedArraySchema = (annotations = {}) => {
1397
+ return {
1398
+ type: "array",
1399
+ format: "TypedArray",
1400
+ title: "Typed Array",
1401
+ description: "A typed array (Float32Array, Int8Array, etc.)",
1402
+ ...annotations
1403
+ };
1404
+ };
1405
+
1406
+ // src/vector/Tensor.ts
1407
+ var TensorType = {
1408
+ FLOAT16: "float16",
1409
+ FLOAT32: "float32",
1410
+ FLOAT64: "float64",
1411
+ INT8: "int8",
1412
+ UINT8: "uint8",
1413
+ INT16: "int16",
1414
+ UINT16: "uint16"
1415
+ };
1416
+ var TensorSchema = (annotations = {}) => ({
1417
+ type: "object",
1418
+ properties: {
1419
+ type: {
1420
+ type: "string",
1421
+ enum: Object.values(TensorType),
1422
+ title: "Type",
1423
+ description: "The type of the tensor"
1424
+ },
1425
+ data: TypedArraySchema({
1426
+ title: "Data",
1427
+ description: "The data of the tensor"
1428
+ }),
1429
+ shape: {
1430
+ type: "array",
1431
+ items: { type: "number" },
1432
+ title: "Shape",
1433
+ description: "The shape of the tensor (dimensions)",
1434
+ minItems: 1,
1435
+ default: [1]
1436
+ },
1437
+ normalized: {
1438
+ type: "boolean",
1439
+ title: "Normalized",
1440
+ description: "Whether the tensor data is normalized",
1441
+ default: false
1442
+ }
1443
+ },
1444
+ required: ["data"],
1445
+ additionalProperties: false,
1446
+ ...annotations
1447
+ });
1448
+ // src/vector/VectorSimilarityUtils.ts
1449
+ function cosineSimilarity(a, b) {
1450
+ if (a.length !== b.length) {
1451
+ throw new Error("Vectors must have the same length");
1452
+ }
1453
+ let dotProduct = 0;
1454
+ let normA = 0;
1455
+ let normB = 0;
1456
+ for (let i = 0;i < a.length; i++) {
1457
+ dotProduct += a[i] * b[i];
1458
+ normA += a[i] * a[i];
1459
+ normB += b[i] * b[i];
1460
+ }
1461
+ const denominator = Math.sqrt(normA) * Math.sqrt(normB);
1462
+ if (denominator === 0) {
1463
+ return 0;
1464
+ }
1465
+ return dotProduct / denominator;
1466
+ }
1467
+ function jaccardSimilarity(a, b) {
1468
+ if (a.length !== b.length) {
1469
+ throw new Error("Vectors must have the same length");
1470
+ }
1471
+ let globalMin = a[0];
1472
+ for (let i = 0;i < a.length; i++) {
1473
+ globalMin = Math.min(globalMin, a[i], b[i]);
1474
+ }
1475
+ const shift = globalMin < 0 ? -globalMin : 0;
1476
+ let minSum = 0;
1477
+ let maxSum = 0;
1478
+ for (let i = 0;i < a.length; i++) {
1479
+ const shiftedA = a[i] + shift;
1480
+ const shiftedB = b[i] + shift;
1481
+ minSum += Math.min(shiftedA, shiftedB);
1482
+ maxSum += Math.max(shiftedA, shiftedB);
1483
+ }
1484
+ return maxSum === 0 ? 0 : minSum / maxSum;
1485
+ }
1486
+ function hammingDistance(a, b) {
1487
+ if (a.length !== b.length) {
1488
+ throw new Error("Vectors must have the same length");
1489
+ }
1490
+ let differences = 0;
1491
+ for (let i = 0;i < a.length; i++) {
1492
+ if (a[i] !== b[i]) {
1493
+ differences++;
1494
+ }
1495
+ }
1496
+ return differences / a.length;
1497
+ }
1498
+ function hammingSimilarity(a, b) {
1499
+ return 1 - hammingDistance(a, b);
1500
+ }
1501
+ // src/vector/VectorUtils.ts
1502
+ function magnitude(arr) {
1503
+ return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0));
1504
+ }
1505
+ function inner(arr1, arr2) {
1506
+ if (arr1.length !== arr2.length) {
1507
+ throw new Error("Vectors must have the same length to compute inner product.");
1508
+ }
1509
+ return arr1.reduce((acc, val, i) => acc + val * arr2[i], 0);
1510
+ }
1511
+ function normalize(vector, throwOnZero = true, float32 = false) {
1512
+ const mag = magnitude(vector);
1513
+ if (mag === 0) {
1514
+ if (throwOnZero) {
1515
+ throw new Error("Cannot normalize a zero vector.");
1516
+ }
1517
+ return vector;
1518
+ }
1519
+ const normalized = Array.from(vector).map((val) => Number(val) / mag);
1520
+ if (float32) {
1521
+ return new Float32Array(normalized);
1522
+ }
1523
+ if (vector instanceof Float64Array) {
1524
+ return new Float64Array(normalized);
1525
+ }
1526
+ if (vector instanceof Float16Array) {
1527
+ return new Float16Array(normalized);
1528
+ }
1529
+ if (vector instanceof Float32Array) {
1530
+ return new Float32Array(normalized);
1531
+ }
1532
+ if (vector instanceof Int8Array) {
1533
+ return new Int8Array(normalized);
1534
+ }
1535
+ if (vector instanceof Uint8Array) {
1536
+ return new Uint8Array(normalized);
1537
+ }
1538
+ if (vector instanceof Int16Array) {
1539
+ return new Int16Array(normalized);
1540
+ }
1541
+ if (vector instanceof Uint16Array) {
1542
+ return new Uint16Array(normalized);
1543
+ }
1544
+ return new Float32Array(normalized);
1545
+ }
1546
+ function normalizeNumberArray(values, throwOnZero = false) {
1547
+ const norm = magnitude(values);
1548
+ if (norm === 0) {
1549
+ if (throwOnZero) {
1550
+ throw new Error("Cannot normalize a zero vector.");
1551
+ }
1552
+ return values;
1553
+ }
1554
+ return values.map((v) => v / norm);
1555
+ }
1341
1556
  // src/worker/WorkerManager.ts
1342
1557
  function extractTransferables(obj) {
1343
1558
  const transferables = [];
@@ -1642,16 +1857,27 @@ export {
1642
1857
  sleep,
1643
1858
  sha256,
1644
1859
  serialize,
1860
+ registerInputResolver,
1645
1861
  parseDataUri,
1646
1862
  parentPort2 as parentPort,
1647
1863
  objectOfArraysAsArrayOfObjects,
1864
+ normalizeNumberArray,
1865
+ normalize,
1648
1866
  makeFingerprint,
1867
+ magnitude,
1868
+ jaccardSimilarity,
1869
+ isTypedArray,
1870
+ inner,
1871
+ hammingSimilarity,
1872
+ hammingDistance,
1649
1873
  globalServiceRegistry,
1650
1874
  globalContainer,
1875
+ getInputResolvers,
1651
1876
  forceArray,
1652
1877
  deepEqual,
1653
1878
  decompress,
1654
1879
  createServiceToken,
1880
+ cosineSimilarity,
1655
1881
  convertImageDataToUseableForm,
1656
1882
  compress,
1657
1883
  compileSchema,
@@ -1663,9 +1889,13 @@ export {
1663
1889
  Worker,
1664
1890
  WORKER_SERVER,
1665
1891
  WORKER_MANAGER,
1892
+ TypedArraySchema,
1893
+ TensorType,
1894
+ TensorSchema,
1666
1895
  ServiceRegistry,
1667
1896
  NodeDoesntExistError,
1668
1897
  NodeAlreadyExistsError,
1898
+ INPUT_RESOLVERS,
1669
1899
  Graph,
1670
1900
  FromSchemaDefaultOptions,
1671
1901
  EventEmitter,
@@ -1676,4 +1906,4 @@ export {
1676
1906
  BaseError
1677
1907
  };
1678
1908
 
1679
- //# debugId=58B65371F37FF8D064756E2164756E21
1909
+ //# debugId=5C29A628C26F3F6064756E2164756E21