@xyne/workflow-sdk 3.2.52 → 3.2.54

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 (181) hide show
  1. package/dist/builder/index.d.ts +19 -2
  2. package/dist/builder/index.d.ts.map +1 -1
  3. package/dist/builder/index.js +17 -1
  4. package/dist/builder/index.js.map +1 -1
  5. package/dist/common/citation-ref.d.ts +4 -4
  6. package/dist/connectors/builtin/steps/compute.step.d.ts +56 -0
  7. package/dist/connectors/builtin/steps/compute.step.d.ts.map +1 -0
  8. package/dist/connectors/builtin/steps/compute.step.js +48 -0
  9. package/dist/connectors/builtin/steps/compute.step.js.map +1 -0
  10. package/dist/connectors/builtin/steps/conditional.step.d.ts +2 -2
  11. package/dist/connectors/builtin/steps/conditional.step.js +1 -1
  12. package/dist/connectors/builtin/steps/conditional.step.js.map +1 -1
  13. package/dist/connectors/builtin/steps/cycle.step.d.ts.map +1 -1
  14. package/dist/connectors/builtin/steps/cycle.step.js +5 -2
  15. package/dist/connectors/builtin/steps/cycle.step.js.map +1 -1
  16. package/dist/connectors/builtin/steps/dedup.step.js +2 -2
  17. package/dist/connectors/builtin/steps/dedup.step.js.map +1 -1
  18. package/dist/connectors/builtin/steps/loop.step.d.ts +6 -6
  19. package/dist/connectors/builtin/steps/loop.step.d.ts.map +1 -1
  20. package/dist/connectors/builtin/steps/loop.step.js +4 -2
  21. package/dist/connectors/builtin/steps/loop.step.js.map +1 -1
  22. package/dist/connectors/builtin/steps/map.step.d.ts +4 -4
  23. package/dist/connectors/builtin/steps/map.step.d.ts.map +1 -1
  24. package/dist/connectors/builtin/steps/map.step.js +4 -2
  25. package/dist/connectors/builtin/steps/map.step.js.map +1 -1
  26. package/dist/connectors/builtin/steps/parallel.step.d.ts +2 -2
  27. package/dist/connectors/builtin/steps/switch.step.js +1 -1
  28. package/dist/connectors/builtin/steps/switch.step.js.map +1 -1
  29. package/dist/connectors/builtin/steps/wait.step.d.ts +30 -24
  30. package/dist/connectors/builtin/steps/wait.step.d.ts.map +1 -1
  31. package/dist/connectors/builtin/steps/wait.step.js +18 -6
  32. package/dist/connectors/builtin/steps/wait.step.js.map +1 -1
  33. package/dist/conversational-flow/calls.d.ts +2 -2
  34. package/dist/conversational-flow/calls.d.ts.map +1 -1
  35. package/dist/conversational-flow/calls.js +40 -5
  36. package/dist/conversational-flow/calls.js.map +1 -1
  37. package/dist/conversational-flow/edit/calls.d.ts.map +1 -1
  38. package/dist/conversational-flow/edit/calls.js +8 -5
  39. package/dist/conversational-flow/edit/calls.js.map +1 -1
  40. package/dist/conversational-flow/edit/endings.d.ts +8 -3
  41. package/dist/conversational-flow/edit/endings.d.ts.map +1 -1
  42. package/dist/conversational-flow/edit/endings.js +17 -9
  43. package/dist/conversational-flow/edit/endings.js.map +1 -1
  44. package/dist/conversational-flow/edit/versions.d.ts.map +1 -1
  45. package/dist/conversational-flow/edit/versions.js +6 -7
  46. package/dist/conversational-flow/edit/versions.js.map +1 -1
  47. package/dist/conversational-flow/index.d.ts +2 -1
  48. package/dist/conversational-flow/index.d.ts.map +1 -1
  49. package/dist/conversational-flow/index.js +4 -0
  50. package/dist/conversational-flow/index.js.map +1 -1
  51. package/dist/conversational-flow/read.d.ts.map +1 -1
  52. package/dist/conversational-flow/read.js +20 -4
  53. package/dist/conversational-flow/read.js.map +1 -1
  54. package/dist/conversational-flow/settle.js +4 -7
  55. package/dist/conversational-flow/settle.js.map +1 -1
  56. package/dist/conversational-flow/types.d.ts +31 -8
  57. package/dist/conversational-flow/types.d.ts.map +1 -1
  58. package/dist/conversational-flow/types.js.map +1 -1
  59. package/dist/engine/condition-evaluator.d.ts +10 -2
  60. package/dist/engine/condition-evaluator.d.ts.map +1 -1
  61. package/dist/engine/condition-evaluator.js +36 -14
  62. package/dist/engine/condition-evaluator.js.map +1 -1
  63. package/dist/engine/config-validator.d.ts +64 -0
  64. package/dist/engine/config-validator.d.ts.map +1 -1
  65. package/dist/engine/config-validator.js +371 -30
  66. package/dist/engine/config-validator.js.map +1 -1
  67. package/dist/engine/resolve-expressions.d.ts +40 -0
  68. package/dist/engine/resolve-expressions.d.ts.map +1 -0
  69. package/dist/engine/resolve-expressions.js +186 -0
  70. package/dist/engine/resolve-expressions.js.map +1 -0
  71. package/dist/engine/variable-resolver.d.ts +27 -0
  72. package/dist/engine/variable-resolver.d.ts.map +1 -1
  73. package/dist/engine/variable-resolver.js +20 -3
  74. package/dist/engine/variable-resolver.js.map +1 -1
  75. package/dist/engine/workflow-executor.d.ts.map +1 -1
  76. package/dist/engine/workflow-executor.js +33 -1
  77. package/dist/engine/workflow-executor.js.map +1 -1
  78. package/dist/expression/catalogue.d.ts +36 -0
  79. package/dist/expression/catalogue.d.ts.map +1 -0
  80. package/dist/expression/catalogue.js +82 -0
  81. package/dist/expression/catalogue.js.map +1 -0
  82. package/dist/expression/form/model.d.ts +115 -0
  83. package/dist/expression/form/model.d.ts.map +1 -0
  84. package/dist/expression/form/model.js +61 -0
  85. package/dist/expression/form/model.js.map +1 -0
  86. package/dist/expression/form/read.d.ts +4 -0
  87. package/dist/expression/form/read.d.ts.map +1 -0
  88. package/dist/expression/form/read.js +283 -0
  89. package/dist/expression/form/read.js.map +1 -0
  90. package/dist/expression/form/write.d.ts +8 -0
  91. package/dist/expression/form/write.d.ts.map +1 -0
  92. package/dist/expression/form/write.js +124 -0
  93. package/dist/expression/form/write.js.map +1 -0
  94. package/dist/expression/format.d.ts +41 -0
  95. package/dist/expression/format.d.ts.map +1 -0
  96. package/dist/expression/format.js +84 -0
  97. package/dist/expression/format.js.map +1 -0
  98. package/dist/expression/index.d.ts +40 -0
  99. package/dist/expression/index.d.ts.map +1 -0
  100. package/dist/expression/index.js +35 -0
  101. package/dist/expression/index.js.map +1 -0
  102. package/dist/expression/infer.d.ts +40 -0
  103. package/dist/expression/infer.d.ts.map +1 -0
  104. package/dist/expression/infer.js +195 -0
  105. package/dist/expression/infer.js.map +1 -0
  106. package/dist/expression/parse.d.ts +60 -0
  107. package/dist/expression/parse.d.ts.map +1 -0
  108. package/dist/expression/parse.js +201 -0
  109. package/dist/expression/parse.js.map +1 -0
  110. package/dist/expression/signature.d.ts +41 -0
  111. package/dist/expression/signature.d.ts.map +1 -0
  112. package/dist/expression/signature.js +124 -0
  113. package/dist/expression/signature.js.map +1 -0
  114. package/dist/expression/signatures.d.ts +45 -0
  115. package/dist/expression/signatures.d.ts.map +1 -0
  116. package/dist/expression/signatures.js +113 -0
  117. package/dist/expression/signatures.js.map +1 -0
  118. package/dist/expression/types.d.ts +71 -0
  119. package/dist/expression/types.d.ts.map +1 -0
  120. package/dist/expression/types.js +139 -0
  121. package/dist/expression/types.js.map +1 -0
  122. package/dist/index.d.ts +21 -2
  123. package/dist/index.d.ts.map +1 -1
  124. package/dist/index.js +22 -1
  125. package/dist/index.js.map +1 -1
  126. package/dist/ingest/filter.d.ts +8 -2
  127. package/dist/ingest/filter.d.ts.map +1 -1
  128. package/dist/ingest/filter.js +8 -2
  129. package/dist/ingest/filter.js.map +1 -1
  130. package/dist/ingest/start-policy.d.ts +1 -1
  131. package/dist/ingest/start-policy.d.ts.map +1 -1
  132. package/dist/ingest/start-policy.js +12 -5
  133. package/dist/ingest/start-policy.js.map +1 -1
  134. package/dist/runtime/workflow-runtime.d.ts.map +1 -1
  135. package/dist/runtime/workflow-runtime.js +4 -2
  136. package/dist/runtime/workflow-runtime.js.map +1 -1
  137. package/dist/types/operators.d.ts +22 -0
  138. package/dist/types/operators.d.ts.map +1 -1
  139. package/dist/types/operators.js +6 -6
  140. package/dist/types/operators.js.map +1 -1
  141. package/dist/types/validation.d.ts +10 -1
  142. package/dist/types/validation.d.ts.map +1 -1
  143. package/dist/types/validation.js +9 -0
  144. package/dist/types/validation.js.map +1 -1
  145. package/dist/types/workflow-config.d.ts +4 -3
  146. package/dist/types/workflow-config.d.ts.map +1 -1
  147. package/dist/types/workflow-config.js +23 -8
  148. package/dist/types/workflow-config.js.map +1 -1
  149. package/dist/util/computed-value.d.ts +103 -0
  150. package/dist/util/computed-value.d.ts.map +1 -0
  151. package/dist/util/computed-value.js +453 -0
  152. package/dist/util/computed-value.js.map +1 -0
  153. package/dist/util/expression-format.d.ts +41 -0
  154. package/dist/util/expression-format.d.ts.map +1 -0
  155. package/dist/util/expression-format.js +84 -0
  156. package/dist/util/expression-format.js.map +1 -0
  157. package/dist/util/expression-functions.d.ts +35 -0
  158. package/dist/util/expression-functions.d.ts.map +1 -0
  159. package/dist/util/expression-functions.js +82 -0
  160. package/dist/util/expression-functions.js.map +1 -0
  161. package/dist/util/expression.d.ts +98 -0
  162. package/dist/util/expression.d.ts.map +1 -0
  163. package/dist/util/expression.js +395 -0
  164. package/dist/util/expression.js.map +1 -0
  165. package/dist/util/jsonata-signatures.d.ts +45 -0
  166. package/dist/util/jsonata-signatures.d.ts.map +1 -0
  167. package/dist/util/jsonata-signatures.js +113 -0
  168. package/dist/util/jsonata-signatures.js.map +1 -0
  169. package/dist/util/signature.d.ts +41 -0
  170. package/dist/util/signature.d.ts.map +1 -0
  171. package/dist/util/signature.js +124 -0
  172. package/dist/util/signature.js.map +1 -0
  173. package/dist/util/type-compat.d.ts +8 -28
  174. package/dist/util/type-compat.d.ts.map +1 -1
  175. package/dist/util/type-compat.js +22 -29
  176. package/dist/util/type-compat.js.map +1 -1
  177. package/dist/util/variable-ref.d.ts +24 -0
  178. package/dist/util/variable-ref.d.ts.map +1 -1
  179. package/dist/util/variable-ref.js +34 -9
  180. package/dist/util/variable-ref.js.map +1 -1
  181. package/package.json +6 -1
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Expressions: `{% … %}` over a workflow's data, in JSONata.
3
+ *
4
+ * Layered so the cheap half stands alone — `format.ts` recognises an expression
5
+ * and imports no parser, which is what lets the config schema and the UI handle
6
+ * one without the engine coming along.
7
+ *
8
+ * format the wire format, and where expressions appear in a config
9
+ * parse compile · cache · ast · the safety rules ← the jsonata import
10
+ * infer what an expression will produce
11
+ * types ExprType, and what may be assigned to what
12
+ * signature JSONata's signature notation, parsed
13
+ * signatures the generated table of builtin signatures
14
+ * catalogue the operations a menu offers
15
+ * form/ the subset a form can build and read back
16
+ *
17
+ * How the *engine* uses this lives in the engine — `resolve-expressions.ts` for
18
+ * a run, `config-validator.ts` for author time — because those depend on a
19
+ * workflow context, and the language must not.
20
+ */
21
+ export { EXPRESSION_REGEX, EXPRESSION_ROOTS, REF_OR_EXPRESSION_REGEX, collectExpressions, extractExpressionSource, hasExpression, isExpression, toExpressionValue, tokenizeExpressions, } from './format.js';
22
+ export { EXPRESSION_LIMITS, clearExpressionCache, compileExpression, expressionAst, inspectExpression, refsInExpression, } from './parse.js';
23
+ export { inferExpressionType } from './infer.js';
24
+ // The shared type vocabulary these rules are written in. Re-exported so a
25
+ // consumer of this module does not need a second barrel to turn a schema's
26
+ // `type` keyword into something `assignableToArgument` will take.
27
+ export { kindOfJsonType, kindsCompatible } from '../util/type-compat.js';
28
+ export { assignableToArgument, assignableToField, baseKind, elementType, sayType, unify, writeLiteralAs, } from './types.js';
29
+ export { parseSignature, paramAt } from './signature.js';
30
+ export { BUILTIN_SIGNATURES, builtinSignature, isBuiltin } from './signatures.js';
31
+ export { EXPRESSION_FUNCTIONS, expressionFunction, takesOf } from './catalogue.js';
32
+ export { COMPARISON_OPS, FORMABLE_FUNCTIONS, argumentSlots, isFormable, takesSeveral, wantsAt, } from './form/model.js';
33
+ export { writeComputation, writeLiteral, writeSelection, } from './form/write.js';
34
+ export { readComputation, } from './form/read.js';
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/expression/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,uBAAuB,EACvB,kBAAkB,EAClB,uBAAuB,EACvB,aAAa,EACb,YAAY,EACZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,aAAa,CAAC;AAGrB,OAAO,EACL,iBAAiB,EACjB,oBAAoB,EACpB,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGjD,0EAA0E;AAC1E,2EAA2E;AAC3E,kEAAkE;AAClE,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAGzE,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,QAAQ,EACR,WAAW,EACX,OAAO,EACP,KAAK,EACL,cAAc,GACf,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEzD,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAElF,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAGnF,OAAO,EACL,cAAc,EACd,kBAAkB,EAClB,aAAa,EACb,UAAU,EACV,YAAY,EACZ,OAAO,GACR,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,cAAc,GACf,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,eAAe,GAChB,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { type ExprType } from './types.js';
2
+ export interface TypeFault {
3
+ /** Author-facing, specific enough to show as-is. */
4
+ message: string;
5
+ /** 0-based offset into the expression source, when the parser gave one. */
6
+ position?: number;
7
+ }
8
+ export interface InferredExpression {
9
+ /** What the whole expression produces — `unknown` wherever it cannot be read. */
10
+ type: ExprType;
11
+ /** Arguments passed something their function cannot accept. */
12
+ faults: TypeFault[];
13
+ }
14
+ /**
15
+ * Work out what an expression produces, and what it misuses on the way.
16
+ *
17
+ * This is what makes the typing claim true *before* a run. Without it the
18
+ * engine leans entirely on JSONata's own signature checks (`T0410`, `T0412`) and
19
+ * on zod rejecting the result — both correct, both too late, and neither
20
+ * visible while the workflow is being written.
21
+ *
22
+ * ## It is only ever allowed to be silent, never wrong
23
+ *
24
+ * Every node it cannot read yields `unknown`, which {@link assignableToArgument}
25
+ * and {@link assignableToField} treat as compatible at any depth. A relative
26
+ * path inside a predicate, a lambda parameter, a `$$`, a regex, an unrecognised
27
+ * node from a future JSONata — all `unknown`. That asymmetry is deliberate: a
28
+ * missed mistake costs a runtime error the author would have had anyway, while a
29
+ * false one blocks a correct workflow from being saved. Expression typing is a
30
+ * courtesy on top of a dynamically-checked language, not a gate.
31
+ *
32
+ * ## Where the types come from
33
+ *
34
+ * References resolve through `lookup`, which the caller supplies — the validator
35
+ * reads JSON Schema, a test can hand over a map. This module never sees a
36
+ * schema, for the same reason `type-compat` does not: the two schema worlds
37
+ * (Zod here, JSON Schema across the wire) must not leak into the rule.
38
+ */
39
+ export declare function inferExpressionType(source: string, lookup: (path: string) => ExprType | undefined): InferredExpression;
40
+ //# sourceMappingURL=infer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infer.d.ts","sourceRoot":"","sources":["../../src/expression/infer.ts"],"names":[],"mappings":"AAYA,OAAO,EAA+D,KAAK,QAAQ,EAAE,MAAM,YAAY,CAAC;AAIxG,MAAM,WAAW,SAAS;IACxB,oDAAoD;IACpD,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,kBAAkB;IACjC,iFAAiF;IACjF,IAAI,EAAE,QAAQ,CAAC;IACf,+DAA+D;IAC/D,MAAM,EAAE,SAAS,EAAE,CAAC;CACrB;AA0BD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,QAAQ,GAAG,SAAS,GAC7C,kBAAkB,CAgJpB"}
@@ -0,0 +1,195 @@
1
+ import { ROOTS, compileExpression } from './parse.js';
2
+ import { builtinSignature } from './signatures.js';
3
+ import { paramAt, parseSignature } from './signature.js';
4
+ import { assignableToArgument, baseKind, elementType, sayType, unify } from './types.js';
5
+ /** Arithmetic that only makes sense on numbers. */
6
+ const NUMERIC_OPERATORS = new Set(['+', '-', '*', '/', '%']);
7
+ /** Comparisons and tests, whatever they are given. */
8
+ const BOOLEAN_OPERATORS = new Set(['=', '!=', '<', '<=', '>', '>=', 'in', 'and', 'or']);
9
+ function at(node) {
10
+ const position = node['position'];
11
+ return typeof position === 'number' ? { position } : {};
12
+ }
13
+ /** The dotted path a `path` node names, or null when it is not a plain one. */
14
+ function dottedPath(node) {
15
+ const steps = node['steps'];
16
+ if (!Array.isArray(steps) || steps.length === 0)
17
+ return null;
18
+ const names = [];
19
+ for (const step of steps) {
20
+ if (step === null || typeof step !== 'object')
21
+ return null;
22
+ const record = step;
23
+ if (record['type'] !== 'name' || typeof record['value'] !== 'string')
24
+ return null;
25
+ names.push(record['value']);
26
+ }
27
+ return ROOTS.has(names[0]) ? names.join('.') : null;
28
+ }
29
+ /**
30
+ * Work out what an expression produces, and what it misuses on the way.
31
+ *
32
+ * This is what makes the typing claim true *before* a run. Without it the
33
+ * engine leans entirely on JSONata's own signature checks (`T0410`, `T0412`) and
34
+ * on zod rejecting the result — both correct, both too late, and neither
35
+ * visible while the workflow is being written.
36
+ *
37
+ * ## It is only ever allowed to be silent, never wrong
38
+ *
39
+ * Every node it cannot read yields `unknown`, which {@link assignableToArgument}
40
+ * and {@link assignableToField} treat as compatible at any depth. A relative
41
+ * path inside a predicate, a lambda parameter, a `$$`, a regex, an unrecognised
42
+ * node from a future JSONata — all `unknown`. That asymmetry is deliberate: a
43
+ * missed mistake costs a runtime error the author would have had anyway, while a
44
+ * false one blocks a correct workflow from being saved. Expression typing is a
45
+ * courtesy on top of a dynamically-checked language, not a gate.
46
+ *
47
+ * ## Where the types come from
48
+ *
49
+ * References resolve through `lookup`, which the caller supplies — the validator
50
+ * reads JSON Schema, a test can hand over a map. This module never sees a
51
+ * schema, for the same reason `type-compat` does not: the two schema worlds
52
+ * (Zod here, JSON Schema across the wire) must not leak into the rule.
53
+ */
54
+ export function inferExpressionType(source, lookup) {
55
+ let ast;
56
+ try {
57
+ ast = compileExpression(source).ast();
58
+ }
59
+ catch {
60
+ return { type: 'unknown', faults: [] };
61
+ }
62
+ const faults = [];
63
+ const infer = (value) => {
64
+ if (value === null || typeof value !== 'object' || Array.isArray(value))
65
+ return 'unknown';
66
+ const node = value;
67
+ switch (node['type']) {
68
+ case 'number':
69
+ return 'number';
70
+ case 'string':
71
+ return 'string';
72
+ case 'value':
73
+ // `true` / `false` / `null`; only the first two have a type worth saying.
74
+ return node['value'] === null ? 'unknown' : 'boolean';
75
+ case 'path': {
76
+ const path = dottedPath(node);
77
+ return path === null ? 'unknown' : (lookup(path) ?? 'unknown');
78
+ }
79
+ case 'unary': {
80
+ if (node['value'] === '[') {
81
+ const items = node['expressions'];
82
+ if (!Array.isArray(items))
83
+ return 'array';
84
+ // JSONata flattens: `[theList]` is `theList`, and `[1..5]` is a flat
85
+ const inner = unify(items.map((item) => {
86
+ const type = infer(item);
87
+ return baseKind(type) === 'array' ? elementType(type) : type;
88
+ }));
89
+ return inner === 'unknown' ? 'array' : { array: inner };
90
+ }
91
+ if (node['value'] === '{')
92
+ return 'object';
93
+ if (node['value'] === '-')
94
+ return 'number';
95
+ return 'unknown';
96
+ }
97
+ case 'binary': {
98
+ const op = node['value'];
99
+ if (typeof op !== 'string')
100
+ return 'unknown';
101
+ if (op === '..')
102
+ return { array: 'number' };
103
+ if (op === '&')
104
+ return 'string';
105
+ if (BOOLEAN_OPERATORS.has(op)) {
106
+ infer(node['lhs']);
107
+ infer(node['rhs']);
108
+ return 'boolean';
109
+ }
110
+ if (NUMERIC_OPERATORS.has(op)) {
111
+ for (const side of ['lhs', 'rhs']) {
112
+ const operand = infer(node[side]);
113
+ if (!assignableToArgument(operand, 'number')) {
114
+ faults.push({
115
+ message: `"${op}" needs numbers, but one side is ${sayType(operand)}.`,
116
+ ...at(node),
117
+ });
118
+ }
119
+ }
120
+ return 'number';
121
+ }
122
+ return 'unknown';
123
+ }
124
+ case 'condition':
125
+ return unify([infer(node['then']), infer(node['else'])]);
126
+ case 'block': {
127
+ const parts = node['expressions'];
128
+ if (!Array.isArray(parts) || parts.length === 0)
129
+ return 'unknown';
130
+ // Every part runs, but only the last one is the answer.
131
+ for (const part of parts.slice(0, -1))
132
+ infer(part);
133
+ return infer(parts[parts.length - 1]);
134
+ }
135
+ case 'apply': {
136
+ // `a ~> $sum()` — the left side becomes the call's first argument.
137
+ const rhs = node['rhs'];
138
+ const piped = infer(node['lhs']);
139
+ if (rhs === null || typeof rhs !== 'object')
140
+ return 'unknown';
141
+ const record = rhs;
142
+ if (record['type'] === 'variable') {
143
+ return callType({ procedure: record, arguments: [] }, piped);
144
+ }
145
+ return callType(record, piped);
146
+ }
147
+ case 'function':
148
+ case 'partial':
149
+ return callType(node, undefined);
150
+ default:
151
+ // `lambda`, `variable`, `bind`, `regex`, `wildcard`, `descendant`,
152
+ return 'unknown';
153
+ }
154
+ };
155
+ /**
156
+ * A call's result, with every argument checked against the signature JSONata publishes for it.
157
+ */
158
+ const callType = (node, piped) => {
159
+ const procedure = node['procedure'];
160
+ const name = procedure !== null && typeof procedure === 'object'
161
+ ? procedure['value']
162
+ : undefined;
163
+ if (typeof name !== 'string')
164
+ return 'unknown';
165
+ const args = Array.isArray(node['arguments']) ? node['arguments'] : [];
166
+ // Walk every argument so faults inside them are still reported, but give a
167
+ const argTypes = args.map((argument) => {
168
+ const record = argument;
169
+ if (record !== null && typeof record === 'object' && record['type'] === 'lambda') {
170
+ return 'unknown';
171
+ }
172
+ return infer(argument);
173
+ });
174
+ const definition = builtinSignature(name);
175
+ const signature = definition === undefined ? null : parseSignature(definition);
176
+ if (signature === null)
177
+ return 'unknown'; // unknown name, or notation we cannot read
178
+ const supplied = piped === undefined ? argTypes : [piped, ...argTypes];
179
+ supplied.forEach((type, index) => {
180
+ const param = paramAt(signature, index);
181
+ if (param === undefined)
182
+ return;
183
+ if (assignableToArgument(type, param.type))
184
+ return;
185
+ const which = supplied.length > 1 ? ` for argument ${String(index + 1)}` : '';
186
+ faults.push({
187
+ message: `$${name} needs ${sayType(param.type)}${which}, but this is ${sayType(type)}.`,
188
+ ...at(node),
189
+ });
190
+ });
191
+ return signature.returns;
192
+ };
193
+ return { type: infer(ast), faults };
194
+ }
195
+ //# sourceMappingURL=infer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"infer.js","sourceRoot":"","sources":["../../src/expression/infer.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,QAAQ,EAAE,WAAW,EAAE,OAAO,EAAE,KAAK,EAAiB,MAAM,YAAY,CAAC;AAkBxG,mDAAmD;AACnD,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;AAC7D,sDAAsD;AACtD,MAAM,iBAAiB,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;AAExF,SAAS,EAAE,CAAC,IAA6B;IACvC,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC;IAClC,OAAO,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;A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AO,SAAS,CAAC,CAAC,2CAA2C;QAErF,MAAM,QAAQ,GAAG,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,GAAG,QAAQ,CAAC,CAAC;QACvE,QAAQ,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE;YAC/B,MAAM,KAAK,GAAG,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;YACxC,IAAI,KAAK,KAAK,SAAS;gBAAE,OAAO;YAChC,IAAI,oBAAoB,CAAC,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC;gBAAE,OAAO;YACnD,MAAM,KAAK,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,iBAAiB,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC9E,MAAM,CAAC,IAAI,CAAC;gBACV,OAAO,EAAE,IAAI,IAAI,UAAU,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,iBAAiB,OAAO,CAAC,IAAI,CAAC,GAAG;gBACvF,GAAG,EAAE,CAAC,IAAI,CAAC;aACZ,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;QACH,OAAO,SAAS,CAAC,OAAO,CAAC;IAC3B,CAAC,CAAC;IAEF,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC,GAAG,CAAC,EAAE,MAAM,EAAE,CAAC;AACtC,CAAC"}
@@ -0,0 +1,60 @@
1
+ import type { Expression, ExprNode } from 'jsonata';
2
+ /** Evaluation limits, applied at compile time and enforced by JSONata while evaluating. */
3
+ export declare const EXPRESSION_LIMITS: {
4
+ readonly timeout: 1000;
5
+ readonly stack: 200;
6
+ };
7
+ /** Compile an expression source, memoized. */
8
+ export declare function compileExpression(source: string): Expression;
9
+ /** The parsed form of an expression, or null when it does not compile. */
10
+ export declare function expressionAst(source: string): ExprNode | null;
11
+ /** Drop every cached expression. For tests and long-idle hosts. */
12
+ export declare function clearExpressionCache(): void;
13
+ export type ExpressionFaultKind = 'syntax' | 'namedFunction' | 'eval' | 'unknownFunction';
14
+ export interface ExpressionFault {
15
+ kind: ExpressionFaultKind;
16
+ /** Author-facing, already specific enough to show as-is. */
17
+ message: string;
18
+ /** 0-based offset into the expression source, when the parser gave one. */
19
+ position?: number;
20
+ }
21
+ /**
22
+ * Check an expression for anything that must never run, without evaluating it.
23
+ * Returns the first fault, or null when the expression is safe to keep.
24
+ *
25
+ * Two things are refused:
26
+ *
27
+ * **Naming a function.** A `lambda` is allowed only as a direct argument to a
28
+ * call — `$filter(xs, function($x){ ... })` — and nowhere else. That one rule is
29
+ * what makes evaluation terminate: an expression can only loop by calling
30
+ * itself, calling itself needs a name to call, and the only ways to get a name
31
+ * are to bind a lambda (`$f := function…`), to stash one in an array or object,
32
+ * or to apply a lambda literal to itself. Every one of those puts a `lambda`
33
+ * somewhere other than an argument slot, so all of them are caught — verified
34
+ * against each form. Inline predicates, which is what the list functions
35
+ * actually need, are untouched, and so is naming a plain *value*
36
+ * (`$total := $sum(…)`), which cannot recurse.
37
+ *
38
+ * **`$eval`.** It compiles a string as a new expression, which is the one hole
39
+ * through which everything above could be re-introduced. Matched on the
40
+ * variable name wherever it appears, so an alias (`$g := $eval`) or a reference
41
+ * (`$map(xs, $eval)`) is caught too, not just a direct call. AWS blocks it in
42
+ * their JSONata support for the same reason.
43
+ *
44
+ * **Unknown functions.** A name JSONata does not have, so that a typo is caught
45
+ * while writing rather than at `T1006`. This was skipped at first on the belief
46
+ * that the builtins could not be enumerated — they can: every name JSONata binds
47
+ * resolves to an object carrying its own signature, and
48
+ * `jsonata-signatures.ts` is generated from them. Checked after the two bans so
49
+ * that `$f(0)` beside a named function reports the named function, which is the
50
+ * more useful of the two complaints.
51
+ *
52
+ * Note this assumes nothing is registered beyond the builtins. If the engine
53
+ * ever calls `registerFunction`, those names have to be known here too, or
54
+ * correct expressions will be refused.
55
+ */
56
+ export declare function inspectExpression(source: string): ExpressionFault | null;
57
+ export declare const ROOTS: ReadonlySet<string>;
58
+ /** Dotted context paths an expression reads, e.g. */
59
+ export declare function refsInExpression(source: string): string[];
60
+ //# sourceMappingURL=parse.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../src/expression/parse.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,UAAU,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAMpD,2FAA2F;AAC3F,eAAO,MAAM,iBAAiB;;;CAAyC,CAAC;AAQxE,8CAA8C;AAC9C,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,CAe5D;AAED,0EAA0E;AAC1E,wBAAgB,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,GAAG,IAAI,CAM7D;AAED,mEAAmE;AACnE,wBAAgB,oBAAoB,IAAI,IAAI,CAE3C;AAID,MAAM,MAAM,mBAAmB,GAAG,QAAQ,GAAG,eAAe,GAAG,MAAM,GAAG,iBAAiB,CAAC;AAE1F,MAAM,WAAW,eAAe;IAC9B,IAAI,EAAE,mBAAmB,CAAC;IAC1B,4DAA4D;IAC5D,OAAO,EAAE,MAAM,CAAC;IAChB,2EAA2E;IAC3E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAgCD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,eAAe,GAAG,IAAI,CAgDxE;AAID,eAAO,MAAM,KAAK,EAAE,WAAW,CAAC,MAAM,CAA6B,CAAC;AAEpE,qDAAqD;AACrD,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CA0BzD"}
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Compiling an expression, and refusing the ones that are not safe to run. The
3
+ * only module that imports JSONata.
4
+ *
5
+ * Safety rests on two things. A `function(){}` literal is legal only as a
6
+ * direct call argument, which removes every route to recursion, and `$eval` is
7
+ * refused by name. `EXPRESSION_LIMITS` is the runtime guarantee behind both —
8
+ * measured, not assumed: a long expression throws D1012 at the timeout.
9
+ *
10
+ */
11
+ import jsonata from 'jsonata';
12
+ import { EXPRESSION_ROOTS } from './format.js';
13
+ import { isBuiltin } from './signatures.js';
14
+ // ─── Runtime limits ───
15
+ /** Evaluation limits, applied at compile time and enforced by JSONata while evaluating. */
16
+ export const EXPRESSION_LIMITS = { timeout: 1000, stack: 200 };
17
+ // ─── Compilation (cached) ───
18
+ /** Compiled-expression cache. */
19
+ const CACHE_MAX = 500;
20
+ const cache = new Map();
21
+ /** Compile an expression source, memoized. */
22
+ export function compileExpression(source) {
23
+ const hit = cache.get(source);
24
+ if (hit !== undefined) {
25
+ // Re-insert so iteration order stays least-recently-used first.
26
+ cache.delete(source);
27
+ cache.set(source, hit);
28
+ return hit;
29
+ }
30
+ const compiled = jsonata(source, EXPRESSION_LIMITS);
31
+ if (cache.size >= CACHE_MAX) {
32
+ const oldest = cache.keys().next();
33
+ if (!oldest.done)
34
+ cache.delete(oldest.value);
35
+ }
36
+ cache.set(source, compiled);
37
+ return compiled;
38
+ }
39
+ /** The parsed form of an expression, or null when it does not compile. */
40
+ export function expressionAst(source) {
41
+ try {
42
+ return compileExpression(source).ast();
43
+ }
44
+ catch {
45
+ return null;
46
+ }
47
+ }
48
+ /** Drop every cached expression. For tests and long-idle hosts. */
49
+ export function clearExpressionCache() {
50
+ cache.clear();
51
+ }
52
+ /**
53
+ * Walk every node of an AST, flagging which ones sit directly in a call's argument list.
54
+ */
55
+ function walkAst(node, inArgumentList, visit) {
56
+ if (node === null || typeof node !== 'object')
57
+ return;
58
+ if (Array.isArray(node)) {
59
+ for (const item of node)
60
+ walkAst(item, false, visit);
61
+ return;
62
+ }
63
+ const record = node;
64
+ visit(record, inArgumentList);
65
+ const type = record['type'];
66
+ for (const [key, value] of Object.entries(record)) {
67
+ const isCallArguments = key === 'arguments' &&
68
+ (type === 'function' || type === 'apply') &&
69
+ Array.isArray(value);
70
+ if (isCallArguments) {
71
+ for (const argument of value)
72
+ walkAst(argument, true, visit);
73
+ }
74
+ else {
75
+ walkAst(value, false, visit);
76
+ }
77
+ }
78
+ }
79
+ /**
80
+ * Check an expression for anything that must never run, without evaluating it.
81
+ * Returns the first fault, or null when the expression is safe to keep.
82
+ *
83
+ * Two things are refused:
84
+ *
85
+ * **Naming a function.** A `lambda` is allowed only as a direct argument to a
86
+ * call — `$filter(xs, function($x){ ... })` — and nowhere else. That one rule is
87
+ * what makes evaluation terminate: an expression can only loop by calling
88
+ * itself, calling itself needs a name to call, and the only ways to get a name
89
+ * are to bind a lambda (`$f := function…`), to stash one in an array or object,
90
+ * or to apply a lambda literal to itself. Every one of those puts a `lambda`
91
+ * somewhere other than an argument slot, so all of them are caught — verified
92
+ * against each form. Inline predicates, which is what the list functions
93
+ * actually need, are untouched, and so is naming a plain *value*
94
+ * (`$total := $sum(…)`), which cannot recurse.
95
+ *
96
+ * **`$eval`.** It compiles a string as a new expression, which is the one hole
97
+ * through which everything above could be re-introduced. Matched on the
98
+ * variable name wherever it appears, so an alias (`$g := $eval`) or a reference
99
+ * (`$map(xs, $eval)`) is caught too, not just a direct call. AWS blocks it in
100
+ * their JSONata support for the same reason.
101
+ *
102
+ * **Unknown functions.** A name JSONata does not have, so that a typo is caught
103
+ * while writing rather than at `T1006`. This was skipped at first on the belief
104
+ * that the builtins could not be enumerated — they can: every name JSONata binds
105
+ * resolves to an object carrying its own signature, and
106
+ * `jsonata-signatures.ts` is generated from them. Checked after the two bans so
107
+ * that `$f(0)` beside a named function reports the named function, which is the
108
+ * more useful of the two complaints.
109
+ *
110
+ * Note this assumes nothing is registered beyond the builtins. If the engine
111
+ * ever calls `registerFunction`, those names have to be known here too, or
112
+ * correct expressions will be refused.
113
+ */
114
+ export function inspectExpression(source) {
115
+ let ast;
116
+ try {
117
+ ast = compileExpression(source).ast();
118
+ }
119
+ catch (err) {
120
+ const error = err;
121
+ return {
122
+ kind: 'syntax',
123
+ message: error.message ?? 'This expression could not be read.',
124
+ ...(typeof error.position === 'number' ? { position: error.position } : {}),
125
+ };
126
+ }
127
+ let fault = null;
128
+ walkAst(ast, false, (node, inArgumentList) => {
129
+ if (fault)
130
+ return;
131
+ const position = node['position'];
132
+ const at = typeof position === 'number' ? { position } : {};
133
+ if (node['type'] === 'variable' && node['value'] === 'eval') {
134
+ fault = { kind: 'eval', message: '$eval is not available in expressions.', ...at };
135
+ return;
136
+ }
137
+ if (node['type'] === 'lambda' && !inArgumentList) {
138
+ fault = {
139
+ kind: 'namedFunction',
140
+ message: 'An expression cannot define a named function. Write the function directly where it is used, as in $filter(items, function($i){ $i.amount > 10 }).',
141
+ ...at,
142
+ };
143
+ }
144
+ });
145
+ if (fault)
146
+ return fault;
147
+ // A second pass, so a banned construct is reported before a name that only
148
+ walkAst(ast, false, (node) => {
149
+ if (fault || node['type'] !== 'function')
150
+ return;
151
+ const procedure = node['procedure'];
152
+ if (procedure === null || typeof procedure !== 'object')
153
+ return;
154
+ const name = procedure['value'];
155
+ if (typeof name !== 'string' || isBuiltin(name))
156
+ return;
157
+ const position = procedure['position'];
158
+ fault = {
159
+ kind: 'unknownFunction',
160
+ message: `$${name} is not a function.`,
161
+ ...(typeof position === 'number' ? { position } : {}),
162
+ };
163
+ });
164
+ return fault;
165
+ }
166
+ // ─── References inside an expression ───
167
+ export const ROOTS = new Set(EXPRESSION_ROOTS);
168
+ /** Dotted context paths an expression reads, e.g. */
169
+ export function refsInExpression(source) {
170
+ let ast;
171
+ try {
172
+ ast = compileExpression(source).ast();
173
+ }
174
+ catch {
175
+ return [];
176
+ }
177
+ const found = [];
178
+ walkAst(ast, false, (node) => {
179
+ if (node['type'] !== 'path')
180
+ return;
181
+ const steps = node['steps'];
182
+ if (!Array.isArray(steps) || steps.length === 0)
183
+ return;
184
+ const names = [];
185
+ for (const step of steps) {
186
+ if (step === null || typeof step !== 'object')
187
+ return;
188
+ const record = step;
189
+ if (record['type'] !== 'name' || typeof record['value'] !== 'string')
190
+ return;
191
+ names.push(record['value']);
192
+ }
193
+ if (!ROOTS.has(names[0]))
194
+ return;
195
+ const path = names.join('.');
196
+ if (!found.includes(path))
197
+ found.push(path);
198
+ });
199
+ return found;
200
+ }
201
+ //# sourceMappingURL=parse.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse.js","sourceRoot":"","sources":["../../src/expression/parse.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,yBAAyB;AAEzB,2FAA2F;AAC3F,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,EAAW,CAAC;AAExE,+BAA+B;AAE/B,iCAAiC;AACjC,MAAM,SAAS,GAAG,GAAG,CAAC;AACtB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAsB,CAAC;AAE5C,8CAA8C;AAC9C,MAAM,UAAU,iBAAiB,CAAC,MAAc;IAC9C,MAAM,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC9B,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;QACtB,gEAAgE;QAChE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QACrB,KAAK,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QACvB,OAAO,GAAG,CAAC;IACb,CAAC;IACD,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,EAAE,iBAAiB,CAAC,CAAC;IACpD,IAAI,KAAK,CAAC,IAAI,IAAI,SAAS,EAAE,CAAC;QAC5B,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,EAAE,C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41 @@
1
+ /**
2
+ * Reads JSONata's signature notation — `<a<n>:n>` is "a list of numbers, giving
3
+ * a number".
4
+ *
5
+ * Written here because JSONata's own parser is bundled with no import path to
6
+ * it, and no standalone package exists; the Go port wrote its own and the Rust
7
+ * one dropped signatures entirely. What *is* reachable, `signature.validate`,
8
+ * checks values, and author-time checking has types and no values.
9
+ *
10
+ * s n b l o a f string, number, boolean, null, object, array, function
11
+ * u j x some scalar, any JSON, anything
12
+ * a<T> a list of T — the distinction the checker rests on
13
+ * (sf) either; read as `unknown`
14
+ * - ? + from context, optional, variadic
15
+ */
16
+ import type { ExprType } from './types.js';
17
+ export interface SignatureParam {
18
+ type: ExprType;
19
+ /** Declared `?` — absent is fine. */
20
+ optional: boolean;
21
+ /** Declared `+` — this type, one or more times, to the end of the call. */
22
+ variadic: boolean;
23
+ /**
24
+ * Declared `-` — JSONata may supply this argument from the context when it is left out.
25
+ */
26
+ fromContext: boolean;
27
+ /** Declared `f` — this argument is a function. */
28
+ isFunction: boolean;
29
+ }
30
+ export interface Signature {
31
+ params: SignatureParam[];
32
+ /** `unknown` when the signature declares no return, as `<af>` does. */
33
+ returns: ExprType;
34
+ }
35
+ /** Read a signature, or null when it cannot be read. */
36
+ export declare function parseSignature(definition: string): Signature | null;
37
+ /**
38
+ * The parameter governing argument `index`, or undefined when the call has more arguments than the signature describes.
39
+ */
40
+ export declare function paramAt(signature: Signature, index: number): SignatureParam | undefined;
41
+ //# sourceMappingURL=signature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../../src/expression/signature.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,QAAQ,CAAC;IACf,qCAAqC;IACrC,QAAQ,EAAE,OAAO,CAAC;IAClB,2EAA2E;IAC3E,QAAQ,EAAE,OAAO,CAAC;IAClB;;OAEG;IACH,WAAW,EAAE,OAAO,CAAC;IACrB,kDAAkD;IAClD,UAAU,EAAE,OAAO,CAAC;CACrB;AAED,MAAM,WAAW,SAAS;IACxB,MAAM,EAAE,cAAc,EAAE,CAAC;IACzB,uEAAuE;IACvE,OAAO,EAAE,QAAQ,CAAC;CACnB;AAwED,wDAAwD;AACxD,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,SAAS,GAAG,IAAI,CAmCnE;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,SAAS,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,GAAG,SAAS,CAKvF"}