@llm4ts/flow 3.0.0 → 3.2.1

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.
Files changed (61) hide show
  1. package/dist/Activity.d.ts +1 -1
  2. package/dist/Activity.d.ts.map +1 -1
  3. package/dist/Activity.js +20 -0
  4. package/dist/Activity.js.map +1 -1
  5. package/dist/Pack.d.ts +11 -0
  6. package/dist/Pack.d.ts.map +1 -1
  7. package/dist/Pack.js +2 -0
  8. package/dist/Pack.js.map +1 -1
  9. package/dist/endpoint/Auth.d.ts +194 -0
  10. package/dist/endpoint/Auth.d.ts.map +1 -0
  11. package/dist/endpoint/Auth.js +302 -0
  12. package/dist/endpoint/Auth.js.map +1 -0
  13. package/dist/endpoint/Endpoints.d.ts +21 -0
  14. package/dist/endpoint/Endpoints.d.ts.map +1 -0
  15. package/dist/endpoint/Endpoints.js +40 -0
  16. package/dist/endpoint/Endpoints.js.map +1 -0
  17. package/dist/endpoint/Transport.d.ts +90 -0
  18. package/dist/endpoint/Transport.d.ts.map +1 -0
  19. package/dist/endpoint/Transport.js +306 -0
  20. package/dist/endpoint/Transport.js.map +1 -0
  21. package/dist/tools/Builtin.d.ts +5 -0
  22. package/dist/tools/Builtin.d.ts.map +1 -0
  23. package/dist/tools/Builtin.js +7 -0
  24. package/dist/tools/Builtin.js.map +1 -0
  25. package/dist/tools/Diff.d.ts +8 -0
  26. package/dist/tools/Diff.d.ts.map +1 -0
  27. package/dist/tools/Diff.js +45 -0
  28. package/dist/tools/Diff.js.map +1 -0
  29. package/dist/tools/HttpCall.d.ts +14 -0
  30. package/dist/tools/HttpCall.d.ts.map +1 -0
  31. package/dist/tools/HttpCall.js +134 -0
  32. package/dist/tools/HttpCall.js.map +1 -0
  33. package/dist/tools/JsonSchemaCheck.d.ts +36 -0
  34. package/dist/tools/JsonSchemaCheck.d.ts.map +1 -0
  35. package/dist/tools/JsonSchemaCheck.js +342 -0
  36. package/dist/tools/JsonSchemaCheck.js.map +1 -0
  37. package/dist/tools/JsonValidate.d.ts +11 -0
  38. package/dist/tools/JsonValidate.d.ts.map +1 -0
  39. package/dist/tools/JsonValidate.js +84 -0
  40. package/dist/tools/JsonValidate.js.map +1 -0
  41. package/dist/tools/Tool.d.ts +126 -0
  42. package/dist/tools/Tool.d.ts.map +1 -0
  43. package/dist/tools/Tool.js +218 -0
  44. package/dist/tools/Tool.js.map +1 -0
  45. package/dist/tools/TreeDiff.d.ts +42 -0
  46. package/dist/tools/TreeDiff.d.ts.map +1 -0
  47. package/dist/tools/TreeDiff.js +276 -0
  48. package/dist/tools/TreeDiff.js.map +1 -0
  49. package/package.json +12 -2
  50. package/src/Activity.ts +22 -0
  51. package/src/Pack.ts +16 -0
  52. package/src/endpoint/Auth.ts +446 -0
  53. package/src/endpoint/Endpoints.ts +60 -0
  54. package/src/endpoint/Transport.ts +456 -0
  55. package/src/tools/Builtin.ts +10 -0
  56. package/src/tools/Diff.ts +61 -0
  57. package/src/tools/HttpCall.ts +160 -0
  58. package/src/tools/JsonSchemaCheck.ts +457 -0
  59. package/src/tools/JsonValidate.ts +106 -0
  60. package/src/tools/Tool.ts +351 -0
  61. package/src/tools/TreeDiff.ts +386 -0
@@ -0,0 +1,386 @@
1
+ /**
2
+ * A deep structural compare of two JSON values that reports every difference
3
+ * with a JSONPath-style location, for tools that check an actual payload
4
+ * against an expected one.
5
+ *
6
+ * Arrays compare under one of three orderings: by position (`ordered`), by an
7
+ * identity field of their object elements (`per-key`), or as a multiset of
8
+ * canonical JSON (`unordered`). Paths named in `ignore` are skipped on both
9
+ * sides, so neither a missing nor an unexpected nor a changed value is
10
+ * reported for them.
11
+ */
12
+
13
+ export type DiffOrdering = "ordered" | "per-key" | "unordered"
14
+
15
+ export type DiffKind = "missing" | "unexpected" | "changed" | "type"
16
+
17
+ export interface DiffIssue {
18
+ readonly path: string
19
+ readonly kind: DiffKind
20
+ readonly detail: string
21
+ }
22
+
23
+ export interface TreeDiffOptions {
24
+ readonly ordering: DiffOrdering
25
+ /**
26
+ * Dot paths to skip, with `*` matching any one segment: `meta.requestId`,
27
+ * `items.*.updatedAt`. An array index is a segment (`items.2`); under
28
+ * `per-key` an element's segment is its identity (`items.42` or
29
+ * `items.id=42`).
30
+ */
31
+ readonly ignore: ReadonlyArray<string>
32
+ /**
33
+ * For `per-key`: the first of these fields present in an array element
34
+ * names its identity. Defaults to `["id", "key", "name", "code"]`.
35
+ */
36
+ readonly keyFields?: ReadonlyArray<string>
37
+ }
38
+
39
+ const defaultKeyFields: ReadonlyArray<string> = ["id", "key", "name", "code"]
40
+
41
+ type JsonRecord = Record<string, unknown>
42
+
43
+ const isRecord = (value: unknown): value is JsonRecord =>
44
+ typeof value === "object" && value !== null && !Array.isArray(value)
45
+
46
+ const jsonTypeOf = (value: unknown): string => {
47
+ if (value === null) return "null"
48
+ if (Array.isArray(value)) return "array"
49
+ return typeof value
50
+ }
51
+
52
+ /** Serialises a value as compact JSON with object keys sorted. */
53
+ export const canonicalJson = (value: unknown): string => {
54
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`
55
+ if (isRecord(value)) {
56
+ const entries = Object.keys(value)
57
+ .filter((key) => value[key] !== undefined)
58
+ .sort()
59
+ .map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key])}`)
60
+ return `{${entries.join(",")}}`
61
+ }
62
+ const text = JSON.stringify(value)
63
+ return text === undefined ? "null" : text
64
+ }
65
+
66
+ const renderLimit = 80
67
+
68
+ const render = (value: unknown): string => {
69
+ const text = canonicalJson(value)
70
+ return text.length > renderLimit ? `${text.slice(0, renderLimit - 1)}…` : text
71
+ }
72
+
73
+ type Segment =
74
+ | { readonly kind: "key"; readonly name: string }
75
+ | { readonly kind: "index"; readonly index: number }
76
+ | { readonly kind: "identity"; readonly field: string; readonly value: unknown }
77
+
78
+ const identifier = /^[A-Za-z_$][A-Za-z0-9_$]*$/
79
+
80
+ const plainKeyValue = /^[A-Za-z0-9_.:-]+$/
81
+
82
+ const renderKeyValue = (value: unknown): string =>
83
+ typeof value === "number" || (typeof value === "string" && plainKeyValue.test(value))
84
+ ? String(value)
85
+ : canonicalJson(value)
86
+
87
+ const renderPath = (segments: ReadonlyArray<Segment>): string =>
88
+ segments.reduce((path, segment) => {
89
+ switch (segment.kind) {
90
+ case "key":
91
+ return identifier.test(segment.name)
92
+ ? `${path}.${segment.name}`
93
+ : `${path}[${JSON.stringify(segment.name)}]`
94
+ case "index":
95
+ return `${path}[${segment.index}]`
96
+ case "identity":
97
+ return `${path}[${segment.field}=${renderKeyValue(segment.value)}]`
98
+ }
99
+ }, "$")
100
+
101
+ const segmentMatches = (pattern: string, segment: Segment): boolean => {
102
+ if (pattern === "*") return true
103
+ switch (segment.kind) {
104
+ case "key":
105
+ return pattern === segment.name
106
+ case "index":
107
+ return pattern === String(segment.index)
108
+ case "identity": {
109
+ const value = renderKeyValue(segment.value)
110
+ return pattern === value || pattern === `${segment.field}=${value}`
111
+ }
112
+ }
113
+ }
114
+
115
+ const pathMatches = (pattern: ReadonlyArray<string>, segments: ReadonlyArray<Segment>): boolean =>
116
+ pattern.length === segments.length &&
117
+ pattern.every((part, index) => segmentMatches(part, segments[index]))
118
+
119
+ interface Differ {
120
+ readonly ordering: DiffOrdering
121
+ readonly ignore: ReadonlyArray<ReadonlyArray<string>>
122
+ readonly keyFields: ReadonlyArray<string>
123
+ readonly issues: Array<DiffIssue>
124
+ }
125
+
126
+ const ignored = (differ: Differ, segments: ReadonlyArray<Segment>): boolean =>
127
+ differ.ignore.some((pattern) => pathMatches(pattern, segments))
128
+
129
+ const report = (
130
+ differ: Differ,
131
+ segments: ReadonlyArray<Segment>,
132
+ kind: DiffKind,
133
+ detail: string
134
+ ): void => {
135
+ differ.issues.push({ path: renderPath(segments), kind, detail })
136
+ }
137
+
138
+ /** Removes the ignored paths below `segments` from a value, for canonical matching. */
139
+ const strip = (differ: Differ, value: unknown, segments: ReadonlyArray<Segment>): unknown => {
140
+ if (Array.isArray(value)) {
141
+ return value.flatMap((item, index) => {
142
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "index", index }]
143
+ return ignored(differ, child) ? [] : [strip(differ, item, child)]
144
+ })
145
+ }
146
+ if (isRecord(value)) {
147
+ const out: JsonRecord = {}
148
+ for (const name of Object.keys(value)) {
149
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "key", name }]
150
+ if (!ignored(differ, child)) out[name] = strip(differ, value[name], child)
151
+ }
152
+ return out
153
+ }
154
+ return value
155
+ }
156
+
157
+ const identityOf = (
158
+ differ: Differ,
159
+ element: unknown
160
+ ): { readonly field: string; readonly value: unknown; readonly token: string } | undefined => {
161
+ if (!isRecord(element)) return undefined
162
+ for (const field of differ.keyFields) {
163
+ if (element[field] !== undefined) {
164
+ const value = element[field]
165
+ return { field, value, token: `${field}=${canonicalJson(value)}` }
166
+ }
167
+ }
168
+ return undefined
169
+ }
170
+
171
+ const diffOrdered = (
172
+ differ: Differ,
173
+ expected: ReadonlyArray<unknown>,
174
+ actual: ReadonlyArray<unknown>,
175
+ segments: ReadonlyArray<Segment>
176
+ ): void => {
177
+ const shared = Math.min(expected.length, actual.length)
178
+ for (let index = 0; index < shared; index++) {
179
+ diffAt(differ, expected[index], actual[index], [...segments, { kind: "index", index }])
180
+ }
181
+ for (let index = shared; index < expected.length; index++) {
182
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "index", index }]
183
+ if (ignored(differ, child)) continue
184
+ report(differ, child, "missing", `missing item ${render(expected[index])} at index ${index}`)
185
+ }
186
+ for (let index = shared; index < actual.length; index++) {
187
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "index", index }]
188
+ if (ignored(differ, child)) continue
189
+ report(
190
+ differ,
191
+ child,
192
+ "unexpected",
193
+ `unexpected item ${render(actual[index])} at index ${index}`
194
+ )
195
+ }
196
+ }
197
+
198
+ const diffPerKey = (
199
+ differ: Differ,
200
+ expected: ReadonlyArray<unknown>,
201
+ actual: ReadonlyArray<unknown>,
202
+ segments: ReadonlyArray<Segment>
203
+ ): void => {
204
+ const byIdentity = new Map<string, Array<number>>()
205
+ actual.forEach((element, index) => {
206
+ const identity = identityOf(differ, element)
207
+ if (identity === undefined) return
208
+ const queue = byIdentity.get(identity.token)
209
+ if (queue === undefined) byIdentity.set(identity.token, [index])
210
+ else queue.push(index)
211
+ })
212
+ const used = new Set<number>()
213
+
214
+ expected.forEach((element, index) => {
215
+ const identity = identityOf(differ, element)
216
+ if (identity === undefined) {
217
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "index", index }]
218
+ const counterpart = actual[index]
219
+ if (
220
+ index < actual.length &&
221
+ !used.has(index) &&
222
+ identityOf(differ, counterpart) === undefined
223
+ ) {
224
+ used.add(index)
225
+ diffAt(differ, element, counterpart, child)
226
+ } else if (!ignored(differ, child)) {
227
+ report(differ, child, "missing", `missing item ${render(element)} at index ${index}`)
228
+ }
229
+ return
230
+ }
231
+ const child: ReadonlyArray<Segment> = [
232
+ ...segments,
233
+ { kind: "identity", field: identity.field, value: identity.value }
234
+ ]
235
+ const match = byIdentity.get(identity.token)?.shift()
236
+ if (match === undefined) {
237
+ if (!ignored(differ, child)) {
238
+ report(
239
+ differ,
240
+ child,
241
+ "missing",
242
+ `missing item with ${identity.field}=${renderKeyValue(identity.value)}`
243
+ )
244
+ }
245
+ return
246
+ }
247
+ used.add(match)
248
+ diffAt(differ, element, actual[match], child)
249
+ })
250
+
251
+ actual.forEach((element, index) => {
252
+ if (used.has(index)) return
253
+ const identity = identityOf(differ, element)
254
+ const child: ReadonlyArray<Segment> =
255
+ identity === undefined
256
+ ? [...segments, { kind: "index", index }]
257
+ : [...segments, { kind: "identity", field: identity.field, value: identity.value }]
258
+ if (ignored(differ, child)) return
259
+ const detail =
260
+ identity === undefined
261
+ ? `unexpected item ${render(element)} at index ${index}`
262
+ : `unexpected item with ${identity.field}=${renderKeyValue(identity.value)}`
263
+ report(differ, child, "unexpected", detail)
264
+ })
265
+ }
266
+
267
+ const diffUnordered = (
268
+ differ: Differ,
269
+ expected: ReadonlyArray<unknown>,
270
+ actual: ReadonlyArray<unknown>,
271
+ segments: ReadonlyArray<Segment>
272
+ ): void => {
273
+ const keyOf = (element: unknown, index: number): string | undefined => {
274
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "index", index }]
275
+ return ignored(differ, child) ? undefined : canonicalJson(strip(differ, element, child))
276
+ }
277
+ const expectedKeys = expected.map(keyOf)
278
+ const actualKeys = actual.map(keyOf)
279
+ const count = (keys: ReadonlyArray<string | undefined>): Map<string, number> => {
280
+ const counts = new Map<string, number>()
281
+ for (const key of keys) {
282
+ if (key !== undefined) counts.set(key, (counts.get(key) ?? 0) + 1)
283
+ }
284
+ return counts
285
+ }
286
+ const leftover = (
287
+ keys: ReadonlyArray<string | undefined>,
288
+ available: Map<string, number>
289
+ ): ReadonlyArray<number> =>
290
+ keys.flatMap((key, index) => {
291
+ if (key === undefined) return []
292
+ const left = available.get(key) ?? 0
293
+ if (left === 0) return [index]
294
+ available.set(key, left - 1)
295
+ return []
296
+ })
297
+
298
+ for (const index of leftover(expectedKeys, count(actualKeys))) {
299
+ report(
300
+ differ,
301
+ [...segments, { kind: "index", index }],
302
+ "missing",
303
+ `missing item ${render(expected[index])}`
304
+ )
305
+ }
306
+ for (const index of leftover(actualKeys, count(expectedKeys))) {
307
+ report(
308
+ differ,
309
+ [...segments, { kind: "index", index }],
310
+ "unexpected",
311
+ `unexpected item ${render(actual[index])}`
312
+ )
313
+ }
314
+ }
315
+
316
+ const diffObjects = (
317
+ differ: Differ,
318
+ expected: JsonRecord,
319
+ actual: JsonRecord,
320
+ segments: ReadonlyArray<Segment>
321
+ ): void => {
322
+ const present = (record: JsonRecord, name: string): boolean =>
323
+ Object.prototype.hasOwnProperty.call(record, name) && record[name] !== undefined
324
+ for (const name of Object.keys(expected)) {
325
+ if (!present(expected, name)) continue
326
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "key", name }]
327
+ if (ignored(differ, child)) continue
328
+ if (present(actual, name)) diffAt(differ, expected[name], actual[name], child)
329
+ else report(differ, child, "missing", `missing property ${JSON.stringify(name)}`)
330
+ }
331
+ for (const name of Object.keys(actual)) {
332
+ if (!present(actual, name) || present(expected, name)) continue
333
+ const child: ReadonlyArray<Segment> = [...segments, { kind: "key", name }]
334
+ if (ignored(differ, child)) continue
335
+ report(differ, child, "unexpected", `unexpected property ${JSON.stringify(name)}`)
336
+ }
337
+ }
338
+
339
+ const diffAt = (
340
+ differ: Differ,
341
+ expected: unknown,
342
+ actual: unknown,
343
+ segments: ReadonlyArray<Segment>
344
+ ): void => {
345
+ if (ignored(differ, segments)) return
346
+ const expectedType = jsonTypeOf(expected)
347
+ const actualType = jsonTypeOf(actual)
348
+ if (expectedType !== actualType) {
349
+ report(differ, segments, "type", `expected ${expectedType}, found ${actualType}`)
350
+ return
351
+ }
352
+ if (Array.isArray(expected) && Array.isArray(actual)) {
353
+ switch (differ.ordering) {
354
+ case "ordered":
355
+ return diffOrdered(differ, expected, actual, segments)
356
+ case "per-key":
357
+ return diffPerKey(differ, expected, actual, segments)
358
+ case "unordered":
359
+ return diffUnordered(differ, expected, actual, segments)
360
+ }
361
+ }
362
+ if (isRecord(expected) && isRecord(actual)) return diffObjects(differ, expected, actual, segments)
363
+ if (expected !== actual) {
364
+ report(differ, segments, "changed", `expected ${render(expected)}, found ${render(actual)}`)
365
+ }
366
+ }
367
+
368
+ /**
369
+ * Compares `actual` against `expected` and returns every difference found,
370
+ * in document order: for objects the expected keys first, then the extra
371
+ * keys of `actual`; for arrays as the ordering dictates.
372
+ */
373
+ export const diffJson = (
374
+ expected: unknown,
375
+ actual: unknown,
376
+ options: TreeDiffOptions
377
+ ): ReadonlyArray<DiffIssue> => {
378
+ const differ: Differ = {
379
+ ordering: options.ordering,
380
+ ignore: options.ignore.map((pattern) => pattern.split(".")),
381
+ keyFields: options.keyFields ?? defaultKeyFields,
382
+ issues: []
383
+ }
384
+ diffAt(differ, expected, actual, [])
385
+ return differ.issues
386
+ }