@adecore/intelligent-ui 0.39.0 → 0.40.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.
package/src/inputs.ts CHANGED
@@ -8,21 +8,38 @@ export function uiCompiledNodes(nodes: readonly UiNode[], into = new Map<string,
8
8
  into.set(node.id, node);
9
9
  uiCompiledNodes(node.children, into);
10
10
  }
11
+
11
12
  return into;
12
13
  }
13
14
 
15
+ /* Every node that drew, parents first; a node that failed or never closed hides its children too. */
16
+ export function eachDrawnNode(nodes: readonly UiViewNode[], visit: (node: UiViewNode) => void): void {
17
+ for (const node of nodes) {
18
+ if (node.error || !node.complete) {
19
+ continue;
20
+ }
21
+
22
+ visit(node);
23
+ eachDrawnNode(node.children, visit);
24
+ }
25
+ }
26
+
14
27
  /* Whether a control's value is one its own visible options offer. */
15
28
  function offered(node: UiViewNode): boolean {
16
29
  if (node.type === 'Segmented' || node.type === 'Select') {
17
30
  return node.children.some((option) => option.type === 'Option' && !option.error && option.props.value === node.props.value);
18
31
  }
32
+
19
33
  if (node.type === 'Map') {
20
34
  return node.children.some((place) => place.type === 'Place' && !place.error && place.props.value === node.props.value);
21
35
  }
36
+
22
37
  if (node.type === 'Checklist') {
23
38
  const options = node.children.filter((item) => item.type === 'Item' && !item.error).map((item) => item.props.value);
39
+
24
40
  return (node.props.value as unknown[]).every((value) => options.includes(value));
25
41
  }
42
+
26
43
  return true;
27
44
  }
28
45
 
@@ -33,9 +50,11 @@ function offered(node: UiViewNode): boolean {
33
50
  function actionValue(action: UiExpression, block: UiBlock): { target: string; value: UiValue } | undefined {
34
51
  try {
35
52
  const target = uiActionTarget(action, uiLocalState(block));
53
+
36
54
  if (target === null || action.kind !== 'call') {
37
55
  return undefined;
38
56
  }
57
+
39
58
  return { target, value: action.name === 'Set' ? evaluateUiExpression(action.args[1], {}) : block.defaults[target] };
40
59
  } catch {
41
60
  return undefined;
@@ -50,28 +69,26 @@ function actionValue(action: UiExpression, block: UiBlock): { target: string; va
50
69
  export function uiVisibleInputs(block: UiBlock, nodes: readonly UiViewNode[], scope: Readonly<Record<string, UiValue>>): Set<string> {
51
70
  const written = uiCompiledNodes(block.nodes);
52
71
  const inputs = new Set<string>();
53
- const visit = (views: readonly UiViewNode[]) => {
54
- for (const node of views) {
55
- if (node.error || !node.complete) {
56
- continue;
57
- }
58
- const source = written.get(node.sourceId ?? node.id);
59
- if (source && offered(node)) {
60
- for (const prop of Object.keys(node.bindings)) {
61
- if (source.bindings[prop]) {
62
- inputs.add(source.bindings[prop]);
63
- }
72
+
73
+ eachDrawnNode(nodes, (node) => {
74
+ const source = written.get(node.sourceId ?? node.id);
75
+
76
+ if (source && offered(node)) {
77
+ for (const prop of Object.keys(node.bindings)) {
78
+ if (source.bindings[prop]) {
79
+ inputs.add(source.bindings[prop]);
64
80
  }
65
81
  }
66
- const actionProp = uiActionProp(node.type);
67
- const action = source && actionProp ? source.expressions[actionProp] : undefined;
68
- const pressed = action && node.props.disabled !== true ? actionValue(action, block) : undefined;
69
- if (pressed && sameUiValue(pressed.value, scope[pressed.target])) {
70
- inputs.add(pressed.target);
71
- }
72
- visit(node.children);
73
82
  }
74
- };
75
- visit(nodes);
83
+
84
+ const actionProp = uiActionProp(node.type);
85
+ const action = source && actionProp ? source.expressions[actionProp] : undefined;
86
+ const pressed = action && node.props.disabled !== true ? actionValue(action, block) : undefined;
87
+
88
+ if (pressed && sameUiValue(pressed.value, scope[pressed.target])) {
89
+ inputs.add(pressed.target);
90
+ }
91
+ });
92
+
76
93
  return inputs;
77
94
  }
package/src/links.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { z } from 'zod';
2
2
  import type { UiLimits } from './budget.ts';
3
3
  import type { UiBlock } from './compiler.ts';
4
- import { evaluateUiBlock, type UiViewNode } from './runtime.ts';
4
+ import { eachDrawnNode } from './inputs.ts';
5
+ import { evaluateUiBlock } from './runtime.ts';
5
6
  import { uiValidatedState } from './query.ts';
6
7
 
7
8
  export const UiLinkTargetSchema = z.discriminatedUnion('type', [
@@ -37,18 +38,14 @@ export function uiLinkTargets(
37
38
  ): Record<string, UiLinkTarget> {
38
39
  const state = uiValidatedState(block, input, queries, limits);
39
40
  const targets: Record<string, UiLinkTarget> = Object.create(null);
40
- const visit = (nodes: readonly UiViewNode[]) => {
41
- for (const node of nodes) {
42
- if (node.error || !node.complete) {
43
- continue;
44
- }
45
- const checked = UiLinkTargetSchema.safeParse({ type: node.type, ...node.props });
46
- if (checked.success) {
47
- targets[node.id] = checked.data;
48
- }
49
- visit(node.children);
41
+
42
+ eachDrawnNode(evaluateUiBlock(block, state, limits).nodes, (node) => {
43
+ const checked = UiLinkTargetSchema.safeParse({ type: node.type, ...node.props });
44
+
45
+ if (checked.success) {
46
+ targets[node.id] = checked.data;
50
47
  }
51
- };
52
- visit(evaluateUiBlock(block, state, limits).nodes);
48
+ });
49
+
53
50
  return targets;
54
51
  }
package/src/places.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import type { UiLimits } from './budget.ts';
2
2
  import type { UiBlock } from './compiler.ts';
3
+ import { eachDrawnNode } from './inputs.ts';
3
4
  import { uiValidatedState } from './query.ts';
4
5
  import { evaluateUiBlock, type UiViewNode } from './runtime.ts';
5
6
 
@@ -30,18 +31,14 @@ export function uiPlaceQueries(
30
31
  ): Record<string, UiPlaceQuery> {
31
32
  const state = uiValidatedState(block, input, queries, limits);
32
33
  const places: Record<string, UiPlaceQuery> = Object.create(null);
33
- const visit = (nodes: readonly UiViewNode[]) => {
34
- for (const node of nodes) {
35
- if (node.error || !node.complete) {
36
- continue;
37
- }
38
- const { address } = node.props as { address?: string };
39
- if (node.type === 'Place' && typeof address === 'string' && address.trim() !== '') {
40
- places[node.id] = { address: address.trim(), name: nameOf(node) };
41
- }
42
- visit(node.children);
34
+
35
+ eachDrawnNode(evaluateUiBlock(block, state, limits).nodes, (node) => {
36
+ const { address } = node.props as { address?: string };
37
+
38
+ if (node.type === 'Place' && typeof address === 'string' && address.trim() !== '') {
39
+ places[node.id] = { address: address.trim(), name: nameOf(node) };
43
40
  }
44
- };
45
- visit(evaluateUiBlock(block, state, limits).nodes);
41
+ });
42
+
46
43
  return places;
47
44
  }
package/src/prop-alias.ts CHANGED
@@ -2,5 +2,6 @@
2
2
  export function aliasedProp(entry: object, written: string, props: Readonly<Record<string, unknown>>): string {
3
3
  const aliases = 'aliases' in entry ? (entry.aliases as Readonly<Record<string, string>>) : {};
4
4
  const target = Object.hasOwn(aliases, written) ? aliases[written] : undefined;
5
+
5
6
  return target === undefined || Object.hasOwn(props, target) ? written : target;
6
7
  }
package/src/prop-list.ts CHANGED
@@ -3,6 +3,7 @@ import type { z } from 'zod';
3
3
  /* The props a catalog entry accepts, its action first and an optional one marked with `?`. */
4
4
  export function uiPropList(entry: { readonly schema: { readonly shape: Readonly<Record<string, z.ZodType>> }; readonly action?: string }): string[] {
5
5
  const props = Object.entries(entry.schema.shape).map(([key, schema]) => `${key}${schema.isOptional() ? '?' : ''}`);
6
+
6
7
  return entry.action ? [entry.action, ...props] : props;
7
8
  }
8
9
 
@@ -21,5 +22,6 @@ export function uiRequirement(
21
22
  uiPropList(entry)
22
23
  .filter((prop) => !prop.endsWith('?'))
23
24
  .join(' ');
25
+
24
26
  return `${name}${required ? ` ${required}` : ''}${entry.text ? ` (text: ${entry.text})` : ''}`;
25
27
  }
package/src/query.ts CHANGED
@@ -15,28 +15,38 @@ export function uiValidatedState(
15
15
  if (!block.complete || block.catalogVersion !== UI_CATALOG_VERSION) {
16
16
  throw new UiFailure('invalid_block', 'Only completed supported blocks can resolve input.');
17
17
  }
18
+
18
19
  const values = copyUiValue(input, new UiBudget(limits)) as Record<string, UiValue>;
19
20
  const state = new UiState(block, limits);
21
+
20
22
  for (const [name, value] of Object.entries(queries)) {
21
23
  state.setQuery(name, value, block);
22
24
  }
25
+
23
26
  const allowed = uiInputValues(block, state);
27
+
24
28
  for (const [key, value] of Object.entries(values)) {
25
29
  if (!Object.hasOwn(allowed, key)) {
26
30
  throw new UiFailure('refused_binding', 'Only declared input bindings can be submitted.');
27
31
  }
32
+
28
33
  state.set(key, value);
29
34
  }
35
+
30
36
  const evaluated = evaluateUiBlock(block, state, limits);
37
+
31
38
  if (evaluated.diagnostics.some((diagnostic) => diagnostic.code === 'budget_exceeded')) {
32
39
  throw new UiFailure('budget_exceeded', 'The query inputs exceeded their evaluation budget.');
33
40
  }
41
+
34
42
  const visible = uiVisibleInputs(block, evaluated.nodes, state.scope());
43
+
35
44
  for (const [key, value] of Object.entries(values)) {
36
45
  if (!sameUiValue(value, allowed[key]) && !visible.has(key)) {
37
46
  throw new UiFailure('invalid_value', 'An input value is outside the visible control’s allowed values.');
38
47
  }
39
48
  }
49
+
40
50
  return state;
41
51
  }
42
52
 
@@ -55,8 +65,10 @@ export function uiQueryArguments(
55
65
  if (!Object.hasOwn(block.queries, name)) {
56
66
  throw new UiFailure('invalid_query', 'This block does not declare that query.');
57
67
  }
68
+
58
69
  const state = uiValidatedState(block, input, queries, limits);
59
70
  const query = block.queries[name];
71
+
60
72
  return query.expression
61
73
  ? (evaluateUiExpression(query.expression, state.scope(), new UiBudget(limits)) as Record<string, UiValue>)
62
74
  : (copyUiValue(query.args, new UiBudget(limits)) as Record<string, UiValue>);
@@ -64,10 +76,13 @@ export function uiQueryArguments(
64
76
 
65
77
  export function uiQueryFallback(block: UiBlock, queries: Readonly<Record<string, unknown>>): string {
66
78
  const state = new UiState(block);
79
+
67
80
  for (const [name, value] of Object.entries(queries)) {
68
81
  state.setQuery(name, value, block);
69
82
  }
83
+
70
84
  const text = (nodes: readonly UiViewNode[]): string =>
71
85
  nodes.map((node) => (node.error ? node.fallback : uiNodeFallback(node.type, node.props, text(node.children)))).join('');
86
+
72
87
  return text(evaluateUiBlock(block, state).nodes).trim() || block.fallback;
73
88
  }
package/src/rethrow.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { UiFailure } from './budget.ts';
2
+
3
+ /* A spent budget ends the whole block; every other failure only marks the part it came from. */
4
+ export function rethrowBudget(error: unknown): void {
5
+ if (error instanceof UiFailure && error.code === 'budget_exceeded') {
6
+ throw error;
7
+ }
8
+ }