@workglow/util 0.0.122 → 0.0.123
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 +1 -954
- package/dist/browser.js.map +3 -17
- package/dist/bun.js +1 -938
- package/dist/bun.js.map +3 -17
- package/dist/common.d.ts +0 -10
- package/dist/common.d.ts.map +1 -1
- package/dist/compress-browser.d.ts +7 -0
- package/dist/compress-browser.d.ts.map +1 -0
- package/dist/compress-browser.js +18 -0
- package/dist/compress-browser.js.map +10 -0
- package/dist/compress-node.d.ts +7 -0
- package/dist/compress-node.d.ts.map +1 -0
- package/dist/compress-node.js +25 -0
- package/dist/compress-node.js.map +10 -0
- package/dist/graph-entry.d.ts +7 -0
- package/dist/graph-entry.d.ts.map +1 -0
- package/dist/graph-entry.js +539 -0
- package/dist/graph-entry.js.map +15 -0
- package/dist/json-schema/SchemaUtils.d.ts +58 -0
- package/dist/json-schema/SchemaUtils.d.ts.map +1 -0
- package/dist/json-schema/SchemaValidation.d.ts +8 -0
- package/dist/json-schema/SchemaValidation.d.ts.map +1 -0
- package/dist/media-browser.d.ts +8 -0
- package/dist/media-browser.d.ts.map +1 -0
- package/dist/media-browser.js +73 -0
- package/dist/media-browser.js.map +11 -0
- package/dist/media-node.d.ts +8 -0
- package/dist/media-node.d.ts.map +1 -0
- package/dist/media-node.js +50 -0
- package/dist/media-node.js.map +11 -0
- package/dist/node.js +1 -938
- package/dist/node.js.map +3 -17
- package/dist/schema-entry.d.ts +17 -0
- package/dist/schema-entry.d.ts.map +1 -0
- package/dist/schema-entry.js +774 -0
- package/dist/schema-entry.js.map +18 -0
- package/dist/types.d.ts +0 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +41 -4
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
// src/json-schema/FromSchema.ts
|
|
2
|
+
var FromSchemaDefaultOptions = {
|
|
3
|
+
parseNotKeyword: true,
|
|
4
|
+
parseIfThenElseKeywords: true,
|
|
5
|
+
keepDefaultedPropertiesOptional: true,
|
|
6
|
+
references: false,
|
|
7
|
+
deserialize: false
|
|
8
|
+
};
|
|
9
|
+
// src/json-schema/SchemaUtils.ts
|
|
10
|
+
function areFormatStringsCompatible(sourceFormat, targetFormat) {
|
|
11
|
+
const formatPattern = /^[a-zA-Z][a-zA-Z0-9_-]*(?::[a-zA-Z][a-zA-Z0-9_-]*)?$/;
|
|
12
|
+
if (!formatPattern.test(sourceFormat) || !formatPattern.test(targetFormat)) {
|
|
13
|
+
return "incompatible";
|
|
14
|
+
}
|
|
15
|
+
const [sourceName, sourceNarrow] = sourceFormat.split(":");
|
|
16
|
+
const [targetName, targetNarrow] = targetFormat.split(":");
|
|
17
|
+
if (sourceName !== targetName) {
|
|
18
|
+
return "incompatible";
|
|
19
|
+
}
|
|
20
|
+
if (!sourceNarrow && !targetNarrow) {
|
|
21
|
+
return "static";
|
|
22
|
+
}
|
|
23
|
+
if (sourceNarrow && !targetNarrow) {
|
|
24
|
+
return "static";
|
|
25
|
+
}
|
|
26
|
+
if (!sourceNarrow && targetNarrow) {
|
|
27
|
+
return "runtime";
|
|
28
|
+
}
|
|
29
|
+
if (sourceNarrow === targetNarrow) {
|
|
30
|
+
return "static";
|
|
31
|
+
}
|
|
32
|
+
return "incompatible";
|
|
33
|
+
}
|
|
34
|
+
function isTypeStaticallyCompatible(sourceType, targetType) {
|
|
35
|
+
if (!targetType) {
|
|
36
|
+
return true;
|
|
37
|
+
}
|
|
38
|
+
if (!sourceType) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
const sourceTypes = Array.isArray(sourceType) ? sourceType : [sourceType];
|
|
42
|
+
const targetTypes = Array.isArray(targetType) ? targetType : [targetType];
|
|
43
|
+
return sourceTypes.some((st) => targetTypes.includes(st));
|
|
44
|
+
}
|
|
45
|
+
function mergeAllOfSchemas(schemas) {
|
|
46
|
+
if (schemas.length === 0)
|
|
47
|
+
return null;
|
|
48
|
+
if (schemas.length === 1)
|
|
49
|
+
return schemas[0];
|
|
50
|
+
let merged = {};
|
|
51
|
+
for (const schema of schemas) {
|
|
52
|
+
if (typeof schema === "boolean") {
|
|
53
|
+
if (schema === false)
|
|
54
|
+
return false;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const schemaObj = schema;
|
|
58
|
+
if (schemaObj.type !== undefined) {
|
|
59
|
+
if (merged.type === undefined) {
|
|
60
|
+
merged.type = schemaObj.type;
|
|
61
|
+
} else if (merged.type !== schemaObj.type) {
|
|
62
|
+
const mergedTypes = Array.isArray(merged.type) ? merged.type : [merged.type];
|
|
63
|
+
const schemaTypes = Array.isArray(schemaObj.type) ? schemaObj.type : [schemaObj.type];
|
|
64
|
+
const commonTypes = mergedTypes.filter((t) => schemaTypes.includes(t));
|
|
65
|
+
if (commonTypes.length === 0) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
merged.type = commonTypes.length === 1 ? commonTypes[0] : commonTypes;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const schemaFormat = schemaObj.format;
|
|
72
|
+
const mergedFormat = merged.format;
|
|
73
|
+
if (schemaFormat) {
|
|
74
|
+
if (!mergedFormat) {
|
|
75
|
+
merged.format = schemaFormat;
|
|
76
|
+
} else {
|
|
77
|
+
const formatCompat = areFormatStringsCompatible(mergedFormat, schemaFormat);
|
|
78
|
+
if (formatCompat === "incompatible") {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const mergedHasNarrow = mergedFormat.includes(":");
|
|
82
|
+
const schemaHasNarrow = schemaFormat.includes(":");
|
|
83
|
+
if (schemaHasNarrow && !mergedHasNarrow) {
|
|
84
|
+
merged.format = schemaFormat;
|
|
85
|
+
} else if (!schemaHasNarrow && mergedHasNarrow) {} else if (mergedFormat !== schemaFormat) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (schemaObj.properties && typeof schemaObj.properties === "object") {
|
|
91
|
+
if (!merged.properties) {
|
|
92
|
+
merged.properties = {};
|
|
93
|
+
}
|
|
94
|
+
const mergedProps = merged.properties;
|
|
95
|
+
const schemaProps = schemaObj.properties;
|
|
96
|
+
for (const [key, value] of Object.entries(schemaProps)) {
|
|
97
|
+
if (mergedProps[key]) {
|
|
98
|
+
const nestedMerged = mergeAllOfSchemas([mergedProps[key], value]);
|
|
99
|
+
if (nestedMerged === null || nestedMerged === false) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
mergedProps[key] = nestedMerged;
|
|
103
|
+
} else {
|
|
104
|
+
mergedProps[key] = value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (schemaObj.required && Array.isArray(schemaObj.required)) {
|
|
109
|
+
if (!merged.required) {
|
|
110
|
+
merged.required = [];
|
|
111
|
+
}
|
|
112
|
+
const mergedRequired = merged.required;
|
|
113
|
+
const schemaRequired = schemaObj.required;
|
|
114
|
+
merged.required = mergedRequired.filter((r) => schemaRequired.includes(r));
|
|
115
|
+
}
|
|
116
|
+
if (schemaObj.additionalProperties !== undefined) {
|
|
117
|
+
if (merged.additionalProperties === undefined) {
|
|
118
|
+
merged.additionalProperties = schemaObj.additionalProperties;
|
|
119
|
+
} else if (merged.additionalProperties === true && schemaObj.additionalProperties === false) {
|
|
120
|
+
merged.additionalProperties = false;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (schemaObj.items !== undefined) {
|
|
124
|
+
if (merged.items === undefined) {
|
|
125
|
+
merged.items = schemaObj.items;
|
|
126
|
+
} else {
|
|
127
|
+
const mergedItems = mergeAllOfSchemas([
|
|
128
|
+
merged.items,
|
|
129
|
+
schemaObj.items
|
|
130
|
+
]);
|
|
131
|
+
if (mergedItems === null || mergedItems === false) {
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
merged.items = mergedItems;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return merged;
|
|
139
|
+
}
|
|
140
|
+
function isCompatibleWithUnion(sourceSchema, unionSchemas) {
|
|
141
|
+
let hasStatic = false;
|
|
142
|
+
let hasRuntime = false;
|
|
143
|
+
for (const unionSchema of unionSchemas) {
|
|
144
|
+
const compatibility = areSemanticallyCompatible(sourceSchema, unionSchema);
|
|
145
|
+
if (compatibility === "static") {
|
|
146
|
+
hasStatic = true;
|
|
147
|
+
} else if (compatibility === "runtime") {
|
|
148
|
+
hasRuntime = true;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (hasStatic)
|
|
152
|
+
return "static";
|
|
153
|
+
if (hasRuntime)
|
|
154
|
+
return "runtime";
|
|
155
|
+
return "incompatible";
|
|
156
|
+
}
|
|
157
|
+
function areSemanticallyCompatible(sourceSchema, targetSchema) {
|
|
158
|
+
if (sourceSchema === undefined || targetSchema === undefined) {
|
|
159
|
+
return "incompatible";
|
|
160
|
+
}
|
|
161
|
+
if (typeof targetSchema === "boolean") {
|
|
162
|
+
if (targetSchema === false)
|
|
163
|
+
return "incompatible";
|
|
164
|
+
if (targetSchema === true)
|
|
165
|
+
return "static";
|
|
166
|
+
return "incompatible";
|
|
167
|
+
}
|
|
168
|
+
if (typeof sourceSchema === "boolean") {
|
|
169
|
+
if (sourceSchema === false)
|
|
170
|
+
return "incompatible";
|
|
171
|
+
if (sourceSchema === true)
|
|
172
|
+
return "runtime";
|
|
173
|
+
}
|
|
174
|
+
if (sourceSchema.allOf && Array.isArray(sourceSchema.allOf)) {
|
|
175
|
+
const mergedSchema = mergeAllOfSchemas(sourceSchema.allOf);
|
|
176
|
+
if (mergedSchema === null || mergedSchema === false) {
|
|
177
|
+
return "incompatible";
|
|
178
|
+
}
|
|
179
|
+
return areSemanticallyCompatible(mergedSchema, targetSchema);
|
|
180
|
+
}
|
|
181
|
+
const sourceType = sourceSchema.type;
|
|
182
|
+
const targetType = targetSchema.type;
|
|
183
|
+
if (sourceSchema.oneOf && Array.isArray(sourceSchema.oneOf)) {
|
|
184
|
+
let hasStatic = false;
|
|
185
|
+
let hasRuntime = false;
|
|
186
|
+
for (const sourceOption of sourceSchema.oneOf) {
|
|
187
|
+
const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
|
|
188
|
+
if (compatibility === "static") {
|
|
189
|
+
hasStatic = true;
|
|
190
|
+
} else if (compatibility === "runtime") {
|
|
191
|
+
hasRuntime = true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (hasRuntime)
|
|
195
|
+
return "runtime";
|
|
196
|
+
if (hasStatic)
|
|
197
|
+
return "static";
|
|
198
|
+
return "incompatible";
|
|
199
|
+
}
|
|
200
|
+
if (sourceSchema.anyOf && Array.isArray(sourceSchema.anyOf)) {
|
|
201
|
+
let hasStatic = false;
|
|
202
|
+
let hasRuntime = false;
|
|
203
|
+
for (const sourceOption of sourceSchema.anyOf) {
|
|
204
|
+
const compatibility = areSemanticallyCompatible(sourceOption, targetSchema);
|
|
205
|
+
if (compatibility === "static") {
|
|
206
|
+
hasStatic = true;
|
|
207
|
+
} else if (compatibility === "runtime") {
|
|
208
|
+
hasRuntime = true;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (hasRuntime)
|
|
212
|
+
return "runtime";
|
|
213
|
+
if (hasStatic)
|
|
214
|
+
return "static";
|
|
215
|
+
return "incompatible";
|
|
216
|
+
}
|
|
217
|
+
if (targetSchema.oneOf && Array.isArray(targetSchema.oneOf)) {
|
|
218
|
+
return isCompatibleWithUnion(sourceSchema, targetSchema.oneOf);
|
|
219
|
+
}
|
|
220
|
+
if (targetSchema.anyOf && Array.isArray(targetSchema.anyOf)) {
|
|
221
|
+
return isCompatibleWithUnion(sourceSchema, targetSchema.anyOf);
|
|
222
|
+
}
|
|
223
|
+
if (targetSchema.allOf && Array.isArray(targetSchema.allOf)) {
|
|
224
|
+
let hasStatic = false;
|
|
225
|
+
let hasRuntime = false;
|
|
226
|
+
for (const allOfSchema of targetSchema.allOf) {
|
|
227
|
+
const compatibility = areSemanticallyCompatible(sourceSchema, allOfSchema);
|
|
228
|
+
if (compatibility === "incompatible") {
|
|
229
|
+
return "incompatible";
|
|
230
|
+
} else if (compatibility === "static") {
|
|
231
|
+
hasStatic = true;
|
|
232
|
+
} else if (compatibility === "runtime") {
|
|
233
|
+
hasRuntime = true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (hasRuntime)
|
|
237
|
+
return "runtime";
|
|
238
|
+
if (hasStatic)
|
|
239
|
+
return "static";
|
|
240
|
+
return "incompatible";
|
|
241
|
+
}
|
|
242
|
+
if (sourceType === "object" && targetType === "object") {
|
|
243
|
+
const sourceProperties = sourceSchema.properties;
|
|
244
|
+
const targetProperties = targetSchema.properties;
|
|
245
|
+
if (!targetProperties) {
|
|
246
|
+
return "static";
|
|
247
|
+
}
|
|
248
|
+
if (!sourceProperties) {
|
|
249
|
+
if (targetSchema.additionalProperties === false) {
|
|
250
|
+
return "incompatible";
|
|
251
|
+
}
|
|
252
|
+
return "static";
|
|
253
|
+
}
|
|
254
|
+
const targetRequired = targetSchema.required || [];
|
|
255
|
+
let hasStatic = true;
|
|
256
|
+
let hasRuntime = false;
|
|
257
|
+
for (const propName of targetRequired) {
|
|
258
|
+
const targetProp = targetProperties?.[propName];
|
|
259
|
+
const sourceProp = sourceProperties?.[propName];
|
|
260
|
+
if (!sourceProp) {
|
|
261
|
+
return "incompatible";
|
|
262
|
+
}
|
|
263
|
+
if (targetProp) {
|
|
264
|
+
const propCompatibility = areSemanticallyCompatible(sourceProp, targetProp);
|
|
265
|
+
if (propCompatibility === "incompatible") {
|
|
266
|
+
return "incompatible";
|
|
267
|
+
} else if (propCompatibility === "runtime") {
|
|
268
|
+
hasRuntime = true;
|
|
269
|
+
hasStatic = false;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (targetSchema.additionalProperties === false) {
|
|
274
|
+
const sourcePropNames = Object.keys(sourceProperties);
|
|
275
|
+
const targetPropNames = Object.keys(targetProperties);
|
|
276
|
+
const extraProps = sourcePropNames.filter((name) => !targetPropNames.includes(name));
|
|
277
|
+
if (extraProps.length > 0) {
|
|
278
|
+
return "incompatible";
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (hasRuntime)
|
|
282
|
+
return "runtime";
|
|
283
|
+
return "static";
|
|
284
|
+
}
|
|
285
|
+
if (sourceType === "array" && targetType === "array") {
|
|
286
|
+
const sourceFormat2 = sourceSchema?.format;
|
|
287
|
+
const targetFormat2 = targetSchema?.format;
|
|
288
|
+
let formatCompatibility = null;
|
|
289
|
+
if (sourceFormat2 && targetFormat2) {
|
|
290
|
+
formatCompatibility = areFormatStringsCompatible(sourceFormat2, targetFormat2);
|
|
291
|
+
if (formatCompatibility === "incompatible") {
|
|
292
|
+
return "incompatible";
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
if (sourceFormat2 && !targetFormat2) {
|
|
296
|
+
return "static";
|
|
297
|
+
}
|
|
298
|
+
if (!sourceFormat2 && targetFormat2) {
|
|
299
|
+
return "incompatible";
|
|
300
|
+
}
|
|
301
|
+
const sourceItems = sourceSchema.items;
|
|
302
|
+
const targetItems = targetSchema.items;
|
|
303
|
+
if (sourceItems && typeof sourceItems === "object" && !Array.isArray(sourceItems) && targetItems && typeof targetItems === "object" && !Array.isArray(targetItems)) {
|
|
304
|
+
const itemsCompatibility = areSemanticallyCompatible(sourceItems, targetItems);
|
|
305
|
+
if (formatCompatibility === "runtime") {
|
|
306
|
+
return "runtime";
|
|
307
|
+
}
|
|
308
|
+
return itemsCompatibility;
|
|
309
|
+
}
|
|
310
|
+
if (!targetItems) {
|
|
311
|
+
return "static";
|
|
312
|
+
}
|
|
313
|
+
if (!sourceItems) {
|
|
314
|
+
return "incompatible";
|
|
315
|
+
}
|
|
316
|
+
if (Array.isArray(targetItems)) {
|
|
317
|
+
return isCompatibleWithUnion(sourceItems, targetItems);
|
|
318
|
+
}
|
|
319
|
+
return "static";
|
|
320
|
+
}
|
|
321
|
+
if (!sourceType) {
|
|
322
|
+
const targetFormat2 = targetSchema?.format;
|
|
323
|
+
if (targetFormat2) {
|
|
324
|
+
return "runtime";
|
|
325
|
+
}
|
|
326
|
+
return "static";
|
|
327
|
+
}
|
|
328
|
+
if (!targetType) {
|
|
329
|
+
const targetFormat2 = targetSchema?.format;
|
|
330
|
+
if (targetFormat2) {
|
|
331
|
+
const sourceFormat2 = sourceSchema?.format;
|
|
332
|
+
if (!sourceFormat2) {
|
|
333
|
+
return "incompatible";
|
|
334
|
+
}
|
|
335
|
+
return areFormatStringsCompatible(sourceFormat2, targetFormat2);
|
|
336
|
+
}
|
|
337
|
+
return "static";
|
|
338
|
+
}
|
|
339
|
+
if (!isTypeStaticallyCompatible(sourceType, targetType)) {
|
|
340
|
+
return "incompatible";
|
|
341
|
+
}
|
|
342
|
+
const sourceFormat = sourceSchema?.format;
|
|
343
|
+
const targetFormat = targetSchema?.format;
|
|
344
|
+
if (sourceFormat && targetFormat) {
|
|
345
|
+
return areFormatStringsCompatible(sourceFormat, targetFormat);
|
|
346
|
+
}
|
|
347
|
+
if (sourceFormat && !targetFormat) {
|
|
348
|
+
return "static";
|
|
349
|
+
}
|
|
350
|
+
if (!sourceFormat && targetFormat) {
|
|
351
|
+
return "incompatible";
|
|
352
|
+
}
|
|
353
|
+
return "static";
|
|
354
|
+
}
|
|
355
|
+
function areObjectSchemasSemanticallyCompatible(sourceSchema, targetSchema) {
|
|
356
|
+
return areSemanticallyCompatible(sourceSchema, targetSchema);
|
|
357
|
+
}
|
|
358
|
+
// src/json-schema/SchemaValidation.ts
|
|
359
|
+
import { compileSchema } from "@sroussey/json-schema-library";
|
|
360
|
+
// src/json-schema/parsePartialJson.ts
|
|
361
|
+
function parsePartialJson(text) {
|
|
362
|
+
const trimmed = text.trim();
|
|
363
|
+
if (!trimmed)
|
|
364
|
+
return;
|
|
365
|
+
try {
|
|
366
|
+
const result = JSON.parse(trimmed);
|
|
367
|
+
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
return;
|
|
371
|
+
} catch {}
|
|
372
|
+
if (trimmed[0] !== "{")
|
|
373
|
+
return;
|
|
374
|
+
const repaired = repairJson(trimmed);
|
|
375
|
+
if (repaired === undefined)
|
|
376
|
+
return;
|
|
377
|
+
try {
|
|
378
|
+
const result = JSON.parse(repaired);
|
|
379
|
+
if (typeof result === "object" && result !== null && !Array.isArray(result)) {
|
|
380
|
+
return result;
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
} catch {
|
|
384
|
+
return;
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
function repairJson(text) {
|
|
388
|
+
let result = "";
|
|
389
|
+
let i = 0;
|
|
390
|
+
const len = text.length;
|
|
391
|
+
const stack = [];
|
|
392
|
+
let inString = false;
|
|
393
|
+
let escaped = false;
|
|
394
|
+
let lastSafeEnd = 0;
|
|
395
|
+
while (i < len) {
|
|
396
|
+
const ch = text[i];
|
|
397
|
+
if (escaped) {
|
|
398
|
+
escaped = false;
|
|
399
|
+
result += ch;
|
|
400
|
+
i++;
|
|
401
|
+
continue;
|
|
402
|
+
}
|
|
403
|
+
if (ch === "\\") {
|
|
404
|
+
escaped = true;
|
|
405
|
+
result += ch;
|
|
406
|
+
i++;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (inString) {
|
|
410
|
+
if (ch === '"') {
|
|
411
|
+
inString = false;
|
|
412
|
+
result += ch;
|
|
413
|
+
i++;
|
|
414
|
+
lastSafeEnd = result.length;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
result += ch;
|
|
418
|
+
i++;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
switch (ch) {
|
|
422
|
+
case '"':
|
|
423
|
+
inString = true;
|
|
424
|
+
result += ch;
|
|
425
|
+
i++;
|
|
426
|
+
break;
|
|
427
|
+
case "{":
|
|
428
|
+
stack.push("}");
|
|
429
|
+
result += ch;
|
|
430
|
+
i++;
|
|
431
|
+
break;
|
|
432
|
+
case "[":
|
|
433
|
+
stack.push("]");
|
|
434
|
+
result += ch;
|
|
435
|
+
i++;
|
|
436
|
+
break;
|
|
437
|
+
case "}":
|
|
438
|
+
if (stack.length > 0 && stack[stack.length - 1] === "}") {
|
|
439
|
+
stack.pop();
|
|
440
|
+
result += ch;
|
|
441
|
+
i++;
|
|
442
|
+
lastSafeEnd = result.length;
|
|
443
|
+
} else {
|
|
444
|
+
return closeStack(result, stack);
|
|
445
|
+
}
|
|
446
|
+
break;
|
|
447
|
+
case "]":
|
|
448
|
+
if (stack.length > 0 && stack[stack.length - 1] === "]") {
|
|
449
|
+
stack.pop();
|
|
450
|
+
result += ch;
|
|
451
|
+
i++;
|
|
452
|
+
lastSafeEnd = result.length;
|
|
453
|
+
} else {
|
|
454
|
+
return closeStack(result, stack);
|
|
455
|
+
}
|
|
456
|
+
break;
|
|
457
|
+
default:
|
|
458
|
+
result += ch;
|
|
459
|
+
i++;
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
if (inString) {
|
|
464
|
+
result += '"';
|
|
465
|
+
}
|
|
466
|
+
if (stack.length === 0) {
|
|
467
|
+
return result;
|
|
468
|
+
}
|
|
469
|
+
return closeStack(cleanTrailing(result), stack);
|
|
470
|
+
}
|
|
471
|
+
function cleanTrailing(text) {
|
|
472
|
+
let s = text.trimEnd();
|
|
473
|
+
let changed = true;
|
|
474
|
+
while (changed) {
|
|
475
|
+
changed = false;
|
|
476
|
+
const trimmed = s.trimEnd();
|
|
477
|
+
if (trimmed.endsWith(",")) {
|
|
478
|
+
s = trimmed.slice(0, -1);
|
|
479
|
+
changed = true;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
482
|
+
if (trimmed.endsWith(":")) {
|
|
483
|
+
const withoutColon = trimmed.slice(0, -1).trimEnd();
|
|
484
|
+
if (withoutColon.endsWith('"')) {
|
|
485
|
+
const keyStart = withoutColon.lastIndexOf('"', withoutColon.length - 2);
|
|
486
|
+
if (keyStart >= 0) {
|
|
487
|
+
let before = withoutColon.slice(0, keyStart).trimEnd();
|
|
488
|
+
if (before.endsWith(",")) {
|
|
489
|
+
before = before.slice(0, -1);
|
|
490
|
+
}
|
|
491
|
+
s = before;
|
|
492
|
+
changed = true;
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
s = withoutColon;
|
|
497
|
+
changed = true;
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
const bareTokenMatch = trimmed.match(/,\s*"[^"]*"\s*:\s*(?:tru|fal|nul|true|false|null|[\d.eE+-]+)$/);
|
|
501
|
+
if (bareTokenMatch) {
|
|
502
|
+
const valueStr = trimmed.slice(trimmed.lastIndexOf(":") + 1).trim();
|
|
503
|
+
try {
|
|
504
|
+
JSON.parse(valueStr);
|
|
505
|
+
} catch {
|
|
506
|
+
s = trimmed.slice(0, bareTokenMatch.index).trimEnd();
|
|
507
|
+
if (s.endsWith(","))
|
|
508
|
+
s = s.slice(0, -1);
|
|
509
|
+
changed = true;
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return s;
|
|
515
|
+
}
|
|
516
|
+
function closeStack(text, stack) {
|
|
517
|
+
let result = text;
|
|
518
|
+
for (let i = stack.length - 1;i >= 0; i--) {
|
|
519
|
+
result += stack[i];
|
|
520
|
+
}
|
|
521
|
+
return result;
|
|
522
|
+
}
|
|
523
|
+
// src/vector/TypedArray.ts
|
|
524
|
+
function isTypedArray(value) {
|
|
525
|
+
return ArrayBuffer.isView(value) && !(value instanceof DataView);
|
|
526
|
+
}
|
|
527
|
+
var TypedArrayType = null;
|
|
528
|
+
var TypedArraySchemaOptions = {
|
|
529
|
+
...FromSchemaDefaultOptions,
|
|
530
|
+
deserialize: [
|
|
531
|
+
{
|
|
532
|
+
pattern: { type: "array", format: "TypedArray:Float64Array" },
|
|
533
|
+
output: Float64Array
|
|
534
|
+
},
|
|
535
|
+
{
|
|
536
|
+
pattern: { type: "array", format: "TypedArray:Float32Array" },
|
|
537
|
+
output: Float32Array
|
|
538
|
+
},
|
|
539
|
+
{
|
|
540
|
+
pattern: { type: "array", format: "TypedArray:Float16Array" },
|
|
541
|
+
output: Float16Array
|
|
542
|
+
},
|
|
543
|
+
{
|
|
544
|
+
pattern: { type: "array", format: "TypedArray:Int16Array" },
|
|
545
|
+
output: Int16Array
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
pattern: { type: "array", format: "TypedArray:Int8Array" },
|
|
549
|
+
output: Int8Array
|
|
550
|
+
},
|
|
551
|
+
{
|
|
552
|
+
pattern: { type: "array", format: "TypedArray:Uint8Array" },
|
|
553
|
+
output: Uint8Array
|
|
554
|
+
},
|
|
555
|
+
{
|
|
556
|
+
pattern: { type: "array", format: "TypedArray:Uint16Array" },
|
|
557
|
+
output: Uint16Array
|
|
558
|
+
},
|
|
559
|
+
{
|
|
560
|
+
pattern: { type: "array", format: "TypedArray" },
|
|
561
|
+
output: TypedArrayType
|
|
562
|
+
}
|
|
563
|
+
]
|
|
564
|
+
};
|
|
565
|
+
var TypedArraySchema = (annotations = {}) => {
|
|
566
|
+
return {
|
|
567
|
+
type: "array",
|
|
568
|
+
format: "TypedArray",
|
|
569
|
+
title: "Typed Array",
|
|
570
|
+
description: "A typed array (Float32Array, Int8Array, etc.)",
|
|
571
|
+
...annotations
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
// src/vector/Tensor.ts
|
|
576
|
+
var TensorType = {
|
|
577
|
+
FLOAT16: "float16",
|
|
578
|
+
FLOAT32: "float32",
|
|
579
|
+
FLOAT64: "float64",
|
|
580
|
+
INT8: "int8",
|
|
581
|
+
UINT8: "uint8",
|
|
582
|
+
INT16: "int16",
|
|
583
|
+
UINT16: "uint16"
|
|
584
|
+
};
|
|
585
|
+
var TensorSchema = (annotations = {}) => ({
|
|
586
|
+
type: "object",
|
|
587
|
+
properties: {
|
|
588
|
+
type: {
|
|
589
|
+
type: "string",
|
|
590
|
+
enum: Object.values(TensorType),
|
|
591
|
+
title: "Type",
|
|
592
|
+
description: "The type of the tensor"
|
|
593
|
+
},
|
|
594
|
+
data: TypedArraySchema({
|
|
595
|
+
title: "Data",
|
|
596
|
+
description: "The data of the tensor"
|
|
597
|
+
}),
|
|
598
|
+
shape: {
|
|
599
|
+
type: "array",
|
|
600
|
+
items: { type: "number" },
|
|
601
|
+
title: "Shape",
|
|
602
|
+
description: "The shape of the tensor (dimensions)",
|
|
603
|
+
minItems: 1,
|
|
604
|
+
default: [1]
|
|
605
|
+
},
|
|
606
|
+
normalized: {
|
|
607
|
+
type: "boolean",
|
|
608
|
+
title: "Normalized",
|
|
609
|
+
description: "Whether the tensor data is normalized",
|
|
610
|
+
default: false
|
|
611
|
+
}
|
|
612
|
+
},
|
|
613
|
+
required: ["data"],
|
|
614
|
+
additionalProperties: false,
|
|
615
|
+
...annotations
|
|
616
|
+
});
|
|
617
|
+
// src/vector/TypedArrayUtils.ts
|
|
618
|
+
var WIDTH_RANK = {
|
|
619
|
+
Float64Array: 6,
|
|
620
|
+
Float32Array: 5,
|
|
621
|
+
Float16Array: 4,
|
|
622
|
+
Int16Array: 3,
|
|
623
|
+
Uint16Array: 3,
|
|
624
|
+
Int8Array: 2,
|
|
625
|
+
Uint8Array: 2
|
|
626
|
+
};
|
|
627
|
+
function getWidthRank(arr) {
|
|
628
|
+
return WIDTH_RANK[arr.constructor.name] ?? 0;
|
|
629
|
+
}
|
|
630
|
+
function widestConstructor(sources) {
|
|
631
|
+
let best = sources[0];
|
|
632
|
+
for (let i = 1;i < sources.length; i++) {
|
|
633
|
+
if (getWidthRank(sources[i]) > getWidthRank(best))
|
|
634
|
+
best = sources[i];
|
|
635
|
+
}
|
|
636
|
+
return best.constructor;
|
|
637
|
+
}
|
|
638
|
+
function createTypedArrayFrom(sources, values) {
|
|
639
|
+
const Ctor = widestConstructor(sources);
|
|
640
|
+
const result = new Ctor(values.length);
|
|
641
|
+
for (let i = 0;i < values.length; i++)
|
|
642
|
+
result[i] = values[i];
|
|
643
|
+
return result;
|
|
644
|
+
}
|
|
645
|
+
// src/vector/VectorSimilarityUtils.ts
|
|
646
|
+
function cosineSimilarity(a, b) {
|
|
647
|
+
if (a.length !== b.length) {
|
|
648
|
+
throw new Error("Vectors must have the same length");
|
|
649
|
+
}
|
|
650
|
+
let dotProduct = 0;
|
|
651
|
+
let normA = 0;
|
|
652
|
+
let normB = 0;
|
|
653
|
+
for (let i = 0;i < a.length; i++) {
|
|
654
|
+
dotProduct += a[i] * b[i];
|
|
655
|
+
normA += a[i] * a[i];
|
|
656
|
+
normB += b[i] * b[i];
|
|
657
|
+
}
|
|
658
|
+
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
|
|
659
|
+
if (denominator === 0) {
|
|
660
|
+
return 0;
|
|
661
|
+
}
|
|
662
|
+
return dotProduct / denominator;
|
|
663
|
+
}
|
|
664
|
+
function jaccardSimilarity(a, b) {
|
|
665
|
+
if (a.length !== b.length) {
|
|
666
|
+
throw new Error("Vectors must have the same length");
|
|
667
|
+
}
|
|
668
|
+
let globalMin = a[0];
|
|
669
|
+
for (let i = 0;i < a.length; i++) {
|
|
670
|
+
globalMin = Math.min(globalMin, a[i], b[i]);
|
|
671
|
+
}
|
|
672
|
+
const shift = globalMin < 0 ? -globalMin : 0;
|
|
673
|
+
let minSum = 0;
|
|
674
|
+
let maxSum = 0;
|
|
675
|
+
for (let i = 0;i < a.length; i++) {
|
|
676
|
+
const shiftedA = a[i] + shift;
|
|
677
|
+
const shiftedB = b[i] + shift;
|
|
678
|
+
minSum += Math.min(shiftedA, shiftedB);
|
|
679
|
+
maxSum += Math.max(shiftedA, shiftedB);
|
|
680
|
+
}
|
|
681
|
+
return maxSum === 0 ? 0 : minSum / maxSum;
|
|
682
|
+
}
|
|
683
|
+
function hammingDistance(a, b) {
|
|
684
|
+
if (a.length !== b.length) {
|
|
685
|
+
throw new Error("Vectors must have the same length");
|
|
686
|
+
}
|
|
687
|
+
let differences = 0;
|
|
688
|
+
for (let i = 0;i < a.length; i++) {
|
|
689
|
+
if (a[i] !== b[i]) {
|
|
690
|
+
differences++;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return differences / a.length;
|
|
694
|
+
}
|
|
695
|
+
function hammingSimilarity(a, b) {
|
|
696
|
+
return 1 - hammingDistance(a, b);
|
|
697
|
+
}
|
|
698
|
+
// src/vector/VectorUtils.ts
|
|
699
|
+
function magnitude(arr) {
|
|
700
|
+
return Math.sqrt(arr.reduce((acc, val) => acc + val * val, 0));
|
|
701
|
+
}
|
|
702
|
+
function inner(arr1, arr2) {
|
|
703
|
+
if (arr1.length !== arr2.length) {
|
|
704
|
+
throw new Error("Vectors must have the same length to compute inner product.");
|
|
705
|
+
}
|
|
706
|
+
return arr1.reduce((acc, val, i) => acc + val * arr2[i], 0);
|
|
707
|
+
}
|
|
708
|
+
function normalize(vector, throwOnZero = true, float32 = false) {
|
|
709
|
+
const mag = magnitude(vector);
|
|
710
|
+
if (mag === 0) {
|
|
711
|
+
if (throwOnZero) {
|
|
712
|
+
throw new Error("Cannot normalize a zero vector.");
|
|
713
|
+
}
|
|
714
|
+
return vector;
|
|
715
|
+
}
|
|
716
|
+
const normalized = Array.from(vector).map((val) => Number(val) / mag);
|
|
717
|
+
if (float32) {
|
|
718
|
+
return new Float32Array(normalized);
|
|
719
|
+
}
|
|
720
|
+
if (vector instanceof Float64Array) {
|
|
721
|
+
return new Float64Array(normalized);
|
|
722
|
+
}
|
|
723
|
+
if (vector instanceof Float16Array) {
|
|
724
|
+
return new Float16Array(normalized);
|
|
725
|
+
}
|
|
726
|
+
if (vector instanceof Float32Array) {
|
|
727
|
+
return new Float32Array(normalized);
|
|
728
|
+
}
|
|
729
|
+
if (vector instanceof Int8Array) {
|
|
730
|
+
return new Int8Array(normalized);
|
|
731
|
+
}
|
|
732
|
+
if (vector instanceof Uint8Array) {
|
|
733
|
+
return new Uint8Array(normalized);
|
|
734
|
+
}
|
|
735
|
+
if (vector instanceof Int16Array) {
|
|
736
|
+
return new Int16Array(normalized);
|
|
737
|
+
}
|
|
738
|
+
if (vector instanceof Uint16Array) {
|
|
739
|
+
return new Uint16Array(normalized);
|
|
740
|
+
}
|
|
741
|
+
return new Float32Array(normalized);
|
|
742
|
+
}
|
|
743
|
+
function normalizeNumberArray(values, throwOnZero = false) {
|
|
744
|
+
const norm = magnitude(values);
|
|
745
|
+
if (norm === 0) {
|
|
746
|
+
if (throwOnZero) {
|
|
747
|
+
throw new Error("Cannot normalize a zero vector.");
|
|
748
|
+
}
|
|
749
|
+
return values;
|
|
750
|
+
}
|
|
751
|
+
return values.map((v) => v / norm);
|
|
752
|
+
}
|
|
753
|
+
export {
|
|
754
|
+
parsePartialJson,
|
|
755
|
+
normalizeNumberArray,
|
|
756
|
+
normalize,
|
|
757
|
+
magnitude,
|
|
758
|
+
jaccardSimilarity,
|
|
759
|
+
isTypedArray,
|
|
760
|
+
inner,
|
|
761
|
+
hammingSimilarity,
|
|
762
|
+
hammingDistance,
|
|
763
|
+
createTypedArrayFrom,
|
|
764
|
+
cosineSimilarity,
|
|
765
|
+
compileSchema,
|
|
766
|
+
areSemanticallyCompatible,
|
|
767
|
+
areObjectSchemasSemanticallyCompatible,
|
|
768
|
+
TypedArraySchema,
|
|
769
|
+
TensorType,
|
|
770
|
+
TensorSchema,
|
|
771
|
+
FromSchemaDefaultOptions
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
//# debugId=56A406BAD97EBABB64756E2164756E21
|