@workglow/util 0.0.84 → 0.0.86
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/README.md +32 -0
- package/dist/browser.js +232 -2
- package/dist/browser.js.map +10 -5
- package/dist/bun.js +232 -2
- package/dist/bun.js.map +10 -5
- package/dist/common.d.ts +4 -0
- package/dist/common.d.ts.map +1 -1
- package/dist/di/InputResolverRegistry.d.ts +58 -0
- package/dist/di/InputResolverRegistry.d.ts.map +1 -0
- package/dist/di/ServiceRegistry.d.ts +1 -1
- package/dist/di/ServiceRegistry.d.ts.map +1 -1
- package/dist/di/index.d.ts +1 -0
- package/dist/di/index.d.ts.map +1 -1
- package/dist/json-schema/JsonSchema.d.ts +1 -0
- package/dist/json-schema/JsonSchema.d.ts.map +1 -1
- package/dist/json-schema/SchemaValidation.d.ts +2 -2
- package/dist/json-schema/SchemaValidation.d.ts.map +1 -1
- package/dist/node.js +232 -2
- package/dist/node.js.map +10 -5
- package/dist/vector/Tensor.d.ts +59 -0
- package/dist/vector/Tensor.d.ts.map +1 -0
- package/dist/vector/TypedArray.d.ts +85 -0
- package/dist/vector/TypedArray.d.ts.map +1 -0
- package/dist/vector/VectorSimilarityUtils.d.ts +30 -0
- package/dist/vector/VectorSimilarityUtils.d.ts.map +1 -0
- package/dist/vector/VectorUtils.d.ts +31 -0
- package/dist/vector/VectorUtils.d.ts.map +1 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -108,6 +108,37 @@ container.register("UserService", UserService);
|
|
|
108
108
|
const userService = container.resolve<UserService>("UserService");
|
|
109
109
|
```
|
|
110
110
|
|
|
111
|
+
### Input Resolver Registry
|
|
112
|
+
|
|
113
|
+
The input resolver registry enables automatic resolution of string identifiers to object instances based on JSON Schema format annotations. This is used by the TaskRunner to resolve inputs like model names or repository IDs before task execution.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import {
|
|
117
|
+
registerInputResolver,
|
|
118
|
+
getInputResolvers,
|
|
119
|
+
INPUT_RESOLVERS,
|
|
120
|
+
} from "@workglow/util";
|
|
121
|
+
|
|
122
|
+
// Register a custom resolver for a format prefix
|
|
123
|
+
registerInputResolver("myformat", async (id, format, registry) => {
|
|
124
|
+
// id: the string value to resolve (e.g., "my-item-id")
|
|
125
|
+
// format: the full format string (e.g., "myformat:subtype")
|
|
126
|
+
// registry: ServiceRegistry for accessing other services
|
|
127
|
+
|
|
128
|
+
const myRepo = registry.get(MY_REPOSITORY_TOKEN);
|
|
129
|
+
const item = await myRepo.findById(id);
|
|
130
|
+
if (!item) {
|
|
131
|
+
throw new Error(`Item "${id}" not found`);
|
|
132
|
+
}
|
|
133
|
+
return item;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// Get all registered resolvers
|
|
137
|
+
const resolvers = getInputResolvers();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
When a task input schema includes a property with `format: "myformat:subtype"`, and the input value is a string, the resolver is called automatically to convert it to the resolved instance.
|
|
141
|
+
|
|
111
142
|
### Event System
|
|
112
143
|
|
|
113
144
|
```typescript
|
|
@@ -260,6 +291,7 @@ type User = z.infer<typeof userSchemaZod>;
|
|
|
260
291
|
- Decorator-based injection
|
|
261
292
|
- Singleton and transient lifetimes
|
|
262
293
|
- Circular dependency detection
|
|
294
|
+
- Input resolver registry for schema-based resolution
|
|
263
295
|
|
|
264
296
|
### Event System (`/events`)
|
|
265
297
|
|
package/dist/browser.js
CHANGED
|
@@ -77,6 +77,19 @@ class ServiceRegistry {
|
|
|
77
77
|
}
|
|
78
78
|
}
|
|
79
79
|
var globalServiceRegistry = new ServiceRegistry(globalContainer);
|
|
80
|
+
|
|
81
|
+
// src/di/InputResolverRegistry.ts
|
|
82
|
+
var INPUT_RESOLVERS = createServiceToken("task.input.resolvers");
|
|
83
|
+
if (!globalServiceRegistry.has(INPUT_RESOLVERS)) {
|
|
84
|
+
globalServiceRegistry.register(INPUT_RESOLVERS, () => new Map, true);
|
|
85
|
+
}
|
|
86
|
+
function getInputResolvers() {
|
|
87
|
+
return globalServiceRegistry.get(INPUT_RESOLVERS);
|
|
88
|
+
}
|
|
89
|
+
function registerInputResolver(formatPrefix, resolver) {
|
|
90
|
+
const resolvers = getInputResolvers();
|
|
91
|
+
resolvers.set(formatPrefix, resolver);
|
|
92
|
+
}
|
|
80
93
|
// src/events/EventEmitter.ts
|
|
81
94
|
class EventEmitter {
|
|
82
95
|
listeners = {};
|
|
@@ -963,7 +976,7 @@ function areObjectSchemasSemanticallyCompatible(sourceSchema, targetSchema) {
|
|
|
963
976
|
return areSemanticallyCompatible(sourceSchema, targetSchema);
|
|
964
977
|
}
|
|
965
978
|
// src/json-schema/SchemaValidation.ts
|
|
966
|
-
import { compileSchema } from "json-schema-library";
|
|
979
|
+
import { compileSchema } from "@sroussey/json-schema-library";
|
|
967
980
|
// src/utilities/Misc.ts
|
|
968
981
|
function forceArray(input) {
|
|
969
982
|
if (Array.isArray(input))
|
|
@@ -1337,6 +1350,208 @@ function objectOfArraysAsArrayOfObjects(data) {
|
|
|
1337
1350
|
}
|
|
1338
1351
|
});
|
|
1339
1352
|
}
|
|
1353
|
+
// src/vector/TypedArray.ts
|
|
1354
|
+
function isTypedArray(value) {
|
|
1355
|
+
return ArrayBuffer.isView(value) && !(value instanceof DataView);
|
|
1356
|
+
}
|
|
1357
|
+
var TypedArrayType = null;
|
|
1358
|
+
var TypedArraySchemaOptions = {
|
|
1359
|
+
...FromSchemaDefaultOptions,
|
|
1360
|
+
deserialize: [
|
|
1361
|
+
{
|
|
1362
|
+
pattern: { type: "array", format: "TypedArray:Float64Array" },
|
|
1363
|
+
output: Float64Array
|
|
1364
|
+
},
|
|
1365
|
+
{
|
|
1366
|
+
pattern: { type: "array", format: "TypedArray:Float32Array" },
|
|
1367
|
+
output: Float32Array
|
|
1368
|
+
},
|
|
1369
|
+
{
|
|
1370
|
+
pattern: { type: "array", format: "TypedArray:Float16Array" },
|
|
1371
|
+
output: Float16Array
|
|
1372
|
+
},
|
|
1373
|
+
{
|
|
1374
|
+
pattern: { type: "array", format: "TypedArray:Int16Array" },
|
|
1375
|
+
output: Int16Array
|
|
1376
|
+
},
|
|
1377
|
+
{
|
|
1378
|
+
pattern: { type: "array", format: "TypedArray:Int8Array" },
|
|
1379
|
+
output: Int8Array
|
|
1380
|
+
},
|
|
1381
|
+
{
|
|
1382
|
+
pattern: { type: "array", format: "TypedArray:Uint8Array" },
|
|
1383
|
+
output: Uint8Array
|
|
1384
|
+
},
|
|
1385
|
+
{
|
|
1386
|
+
pattern: { type: "array", format: "TypedArray:Uint16Array" },
|
|
1387
|
+
output: Uint16Array
|
|
1388
|
+
},
|
|
1389
|
+
{
|
|
1390
|
+
pattern: { type: "array", format: "TypedArray" },
|
|
1391
|
+
output: TypedArrayType
|
|
1392
|
+
}
|
|
1393
|
+
]
|
|
1394
|
+
};
|
|
1395
|
+
var TypedArraySchema = (annotations = {}) => {
|
|
1396
|
+
return {
|
|
1397
|
+
type: "array",
|
|
1398
|
+
format: "TypedArray",
|
|
1399
|
+
title: "Typed Array",
|
|
1400
|
+
description: "A typed array (Float32Array, Int8Array, etc.)",
|
|
1401
|
+
...annotations
|
|
1402
|
+
};
|
|
1403
|
+
};
|
|
1404
|
+
|
|
1405
|
+
// src/vector/Tensor.ts
|
|
1406
|
+
var TensorType = {
|
|
1407
|
+
FLOAT16: "float16",
|
|
1408
|
+
FLOAT32: "float32",
|
|
1409
|
+
FLOAT64: "float64",
|
|
1410
|
+
INT8: "int8",
|
|
1411
|
+
UINT8: "uint8",
|
|
1412
|
+
INT16: "int16",
|
|
1413
|
+
UINT16: "uint16"
|
|
1414
|
+
};
|
|
1415
|
+
var TensorSchema = (annotations = {}) => ({
|
|
1416
|
+
type: "object",
|
|
1417
|
+
properties: {
|
|
1418
|
+
type: {
|
|
1419
|
+
type: "string",
|
|
1420
|
+
enum: Object.values(TensorType),
|
|
1421
|
+
title: "Type",
|
|
1422
|
+
description: "The type of the tensor"
|
|
1423
|
+
},
|
|
1424
|
+
data: TypedArraySchema({
|
|
1425
|
+
title: "Data",
|
|
1426
|
+
description: "The data of the tensor"
|
|
1427
|
+
}),
|
|
1428
|
+
shape: {
|
|
1429
|
+
type: "array",
|
|
1430
|
+
items: { type: "number" },
|
|
1431
|
+
title: "Shape",
|
|
1432
|
+
description: "The shape of the tensor (dimensions)",
|
|
1433
|
+
minItems: 1,
|
|
1434
|
+
default: [1]
|
|
1435
|
+
},
|
|
1436
|
+
normalized: {
|
|
1437
|
+
type: "boolean",
|
|
1438
|
+
title: "Normalized",
|
|
1439
|
+
description: "Whether the tensor data is normalized",
|
|
1440
|
+
default: false
|
|
1441
|
+
}
|
|
1442
|
+
},
|
|
1443
|
+
required: ["data"],
|
|
1444
|
+
additionalProperties: false,
|
|
1445
|
+
...annotations
|
|
1446
|
+
});
|
|
1447
|
+
// src/vector/VectorSimilarityUtils.ts
|
|
1448
|
+
function cosineSimilarity(a, b) {
|
|
1449
|
+
if (a.length !== b.length) {
|
|
1450
|
+
throw new Error("Vectors must have the same length");
|
|
1451
|
+
}
|
|
1452
|
+
let dotProduct = 0;
|
|
1453
|
+
let normA = 0;
|
|
1454
|
+
let normB = 0;
|
|
1455
|
+
for (let i = 0;i < a.length; i++) {
|
|
1456
|
+
dotProduct += a[i] * b[i];
|
|
1457
|
+
normA += a[i] * a[i];
|
|
1458
|
+
normB += b[i] * b[i];
|
|
1459
|
+
}
|
|
1460
|
+
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
|
|
1461
|
+
if (denominator === 0) {
|
|
1462
|
+
return 0;
|
|
1463
|
+
}
|
|
1464
|
+
return dotProduct / denominator;
|
|
1465
|
+
}
|
|
1466
|
+
function jaccardSimilarity(a, b) {
|
|
1467
|
+
if (a.length !== b.length) {
|
|
1468
|
+
throw new Error("Vectors must have the same length");
|
|
1469
|
+
}
|
|
1470
|
+
let globalMin = a[0];
|
|
1471
|
+
for (let i = 0;i < a.length; i++) {
|
|
1472
|
+
globalMin = Math.min(globalMin, a[i], b[i]);
|
|
1473
|
+
}
|
|
1474
|
+
const shift = globalMin < 0 ? -globalMin : 0;
|
|
1475
|
+
let minSum = 0;
|
|
1476
|
+
let maxSum = 0;
|
|
1477
|
+
for (let i = 0;i < a.length; i++) {
|
|
1478
|
+
const shiftedA = a[i] + shift;
|
|
1479
|
+
const shiftedB = b[i] + shift;
|
|
1480
|
+
minSum += Math.min(shiftedA, shiftedB);
|
|
1481
|
+
maxSum += Math.max(shiftedA, shiftedB);
|
|
1482
|
+
}
|
|
1483
|
+
return maxSum === 0 ? 0 : minSum / maxSum;
|
|
1484
|
+
}
|
|
1485
|
+
function hammingDistance(a, b) {
|
|
1486
|
+
if (a.length !== b.length) {
|
|
1487
|
+
throw new Error("Vectors must have the same length");
|
|
1488
|
+
}
|
|
1489
|
+
let differences = 0;
|
|
1490
|
+
for (let i = 0;i < a.length; i++) {
|
|
1491
|
+
if (a[i] !== b[i]) {
|
|
1492
|
+
differences++;
|
|
1493
|
+
}
|
|
1494
|
+
}
|
|
1495
|
+
return differences / a.length;
|
|
1496
|
+
}
|
|
1497
|
+
function hammingSimilarity(a, b) {
|
|
1498
|
+
return 1 - hammingDistance(a, b);
|
|
1499
|
+
}
|
|
1500
|
+
// src/vector/VectorUtils.ts
|
|
1501
|
+
function magnitude(arr) {
|
|
1502
|
+
return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0));
|
|
1503
|
+
}
|
|
1504
|
+
function inner(arr1, arr2) {
|
|
1505
|
+
if (arr1.length !== arr2.length) {
|
|
1506
|
+
throw new Error("Vectors must have the same length to compute inner product.");
|
|
1507
|
+
}
|
|
1508
|
+
return arr1.reduce((acc, val, i) => acc + val * arr2[i], 0);
|
|
1509
|
+
}
|
|
1510
|
+
function normalize(vector, throwOnZero = true, float32 = false) {
|
|
1511
|
+
const mag = magnitude(vector);
|
|
1512
|
+
if (mag === 0) {
|
|
1513
|
+
if (throwOnZero) {
|
|
1514
|
+
throw new Error("Cannot normalize a zero vector.");
|
|
1515
|
+
}
|
|
1516
|
+
return vector;
|
|
1517
|
+
}
|
|
1518
|
+
const normalized = Array.from(vector).map((val) => Number(val) / mag);
|
|
1519
|
+
if (float32) {
|
|
1520
|
+
return new Float32Array(normalized);
|
|
1521
|
+
}
|
|
1522
|
+
if (vector instanceof Float64Array) {
|
|
1523
|
+
return new Float64Array(normalized);
|
|
1524
|
+
}
|
|
1525
|
+
if (vector instanceof Float16Array) {
|
|
1526
|
+
return new Float16Array(normalized);
|
|
1527
|
+
}
|
|
1528
|
+
if (vector instanceof Float32Array) {
|
|
1529
|
+
return new Float32Array(normalized);
|
|
1530
|
+
}
|
|
1531
|
+
if (vector instanceof Int8Array) {
|
|
1532
|
+
return new Int8Array(normalized);
|
|
1533
|
+
}
|
|
1534
|
+
if (vector instanceof Uint8Array) {
|
|
1535
|
+
return new Uint8Array(normalized);
|
|
1536
|
+
}
|
|
1537
|
+
if (vector instanceof Int16Array) {
|
|
1538
|
+
return new Int16Array(normalized);
|
|
1539
|
+
}
|
|
1540
|
+
if (vector instanceof Uint16Array) {
|
|
1541
|
+
return new Uint16Array(normalized);
|
|
1542
|
+
}
|
|
1543
|
+
return new Float32Array(normalized);
|
|
1544
|
+
}
|
|
1545
|
+
function normalizeNumberArray(values, throwOnZero = false) {
|
|
1546
|
+
const norm = magnitude(values);
|
|
1547
|
+
if (norm === 0) {
|
|
1548
|
+
if (throwOnZero) {
|
|
1549
|
+
throw new Error("Cannot normalize a zero vector.");
|
|
1550
|
+
}
|
|
1551
|
+
return values;
|
|
1552
|
+
}
|
|
1553
|
+
return values.map((v) => v / norm);
|
|
1554
|
+
}
|
|
1340
1555
|
// src/worker/WorkerManager.ts
|
|
1341
1556
|
function extractTransferables(obj) {
|
|
1342
1557
|
const transferables = [];
|
|
@@ -1661,16 +1876,27 @@ export {
|
|
|
1661
1876
|
sleep,
|
|
1662
1877
|
sha256,
|
|
1663
1878
|
serialize,
|
|
1879
|
+
registerInputResolver,
|
|
1664
1880
|
parseDataUri,
|
|
1665
1881
|
parentPort2 as parentPort,
|
|
1666
1882
|
objectOfArraysAsArrayOfObjects,
|
|
1883
|
+
normalizeNumberArray,
|
|
1884
|
+
normalize,
|
|
1667
1885
|
makeFingerprint,
|
|
1886
|
+
magnitude,
|
|
1887
|
+
jaccardSimilarity,
|
|
1888
|
+
isTypedArray,
|
|
1889
|
+
inner,
|
|
1890
|
+
hammingSimilarity,
|
|
1891
|
+
hammingDistance,
|
|
1668
1892
|
globalServiceRegistry,
|
|
1669
1893
|
globalContainer,
|
|
1894
|
+
getInputResolvers,
|
|
1670
1895
|
forceArray,
|
|
1671
1896
|
deepEqual,
|
|
1672
1897
|
decompress,
|
|
1673
1898
|
createServiceToken,
|
|
1899
|
+
cosineSimilarity,
|
|
1674
1900
|
convertImageDataToUseableForm,
|
|
1675
1901
|
compress,
|
|
1676
1902
|
compileSchema,
|
|
@@ -1682,9 +1908,13 @@ export {
|
|
|
1682
1908
|
Worker,
|
|
1683
1909
|
WORKER_SERVER,
|
|
1684
1910
|
WORKER_MANAGER,
|
|
1911
|
+
TypedArraySchema,
|
|
1912
|
+
TensorType,
|
|
1913
|
+
TensorSchema,
|
|
1685
1914
|
ServiceRegistry,
|
|
1686
1915
|
NodeDoesntExistError,
|
|
1687
1916
|
NodeAlreadyExistsError,
|
|
1917
|
+
INPUT_RESOLVERS,
|
|
1688
1918
|
Graph,
|
|
1689
1919
|
FromSchemaDefaultOptions,
|
|
1690
1920
|
EventEmitter,
|
|
@@ -1695,4 +1925,4 @@ export {
|
|
|
1695
1925
|
BaseError
|
|
1696
1926
|
};
|
|
1697
1927
|
|
|
1698
|
-
//# debugId=
|
|
1928
|
+
//# debugId=98769FE9D055D7AC64756E2164756E21
|