@adecore/intelligent-ui 0.39.0 → 0.40.0

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/syntax.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { safeKey, UiBudget, UiFailure } from './budget.ts';
2
2
  import { parseUiExpression, type UiExpression } from './expression.ts';
3
+ import { rethrowBudget } from './rethrow.ts';
3
4
 
4
5
  export interface UiDiagnostic {
5
6
  code: string;
@@ -35,6 +36,7 @@ const WARNING_CODES = new Set(['refused_prop', 'renamed_prop', 'too_many_series'
35
36
 
36
37
  export function uiDiagnostic(error: unknown, start: number, end: number, nodeId?: string): UiDiagnostic {
37
38
  const code = error instanceof UiFailure ? error.code : 'invalid_syntax';
39
+
38
40
  return {
39
41
  code,
40
42
  ...(WARNING_CODES.has(code) ? { severity: 'warning' as const } : {}),
@@ -59,6 +61,7 @@ class UiParser {
59
61
  this.id = id;
60
62
  this.final = final;
61
63
  this.budget = budget;
64
+
62
65
  if (source.length > budget.limits.characters) {
63
66
  throw new UiFailure('budget_exceeded', 'The block is too long.');
64
67
  }
@@ -66,10 +69,12 @@ class UiParser {
66
69
 
67
70
  parse(): UiSyntax {
68
71
  this.declarations();
72
+
69
73
  while (this.position < this.source.length) {
70
74
  this.budget.step();
71
75
  this.budget.depth(this.stack.length);
72
76
  const start = this.position;
77
+
73
78
  try {
74
79
  if (this.source[this.position] === '<') {
75
80
  this.tag();
@@ -77,16 +82,20 @@ class UiParser {
77
82
  const body = this.balanced();
78
83
  const node = this.node('$text', start);
79
84
  node.complete = body.complete;
85
+
80
86
  if (body.complete) {
81
87
  node.expression = parseUiExpression(body.value, this.budget);
82
88
  }
89
+
83
90
  this.append(node);
84
91
  } else {
85
92
  while (this.position < this.source.length && !['<', '{'].includes(this.source[this.position])) {
86
93
  this.position += 1;
87
94
  this.budget.step();
88
95
  }
96
+
89
97
  const value = this.source.slice(start, this.position);
98
+
90
99
  if (value.trim() || !/[\r\n]/.test(value)) {
91
100
  const node = this.node('$text', start);
92
101
  node.text = value;
@@ -94,52 +103,63 @@ class UiParser {
94
103
  }
95
104
  }
96
105
  } catch (error) {
97
- if (error instanceof UiFailure && error.code === 'budget_exceeded') {
98
- throw error;
99
- }
106
+ rethrowBudget(error);
100
107
  this.diagnose(error, start);
108
+
101
109
  if (this.position <= start) {
102
110
  this.position = start + 1;
103
111
  }
112
+
104
113
  while (this.position < this.source.length && this.source[this.position] !== '\n' && this.source[this.position] !== '<') {
105
114
  this.position += 1;
106
115
  this.budget.step();
107
116
  }
108
117
  }
109
118
  }
119
+
110
120
  for (const node of this.stack) {
111
121
  node.end = this.source.length;
122
+
112
123
  if (this.final) {
113
124
  this.diagnose(new UiFailure('unclosed_tag', `The ${node.type} tag is not closed.`), node.start, node.id);
114
125
  }
115
126
  }
127
+
116
128
  return this.result;
117
129
  }
118
130
 
119
131
  private declarations(): void {
120
132
  while (true) {
121
133
  this.whitespace();
134
+
122
135
  if (this.source[this.position] !== '$') {
123
136
  return;
124
137
  }
138
+
125
139
  const start = this.position;
126
140
  const declaration = /^\$([A-Za-z_][A-Za-z_\d]*)\s*=/.exec(this.source.slice(start));
141
+
127
142
  if (!declaration) {
128
143
  if (this.final) {
129
144
  this.diagnose(new UiFailure('invalid_declaration', 'Expected $name = value.'), start);
130
145
  }
146
+
131
147
  this.position = this.source.length;
148
+
132
149
  return;
133
150
  }
151
+
134
152
  const name = `$${safeKey(declaration[1])}`;
135
153
  this.position += declaration[0].length;
136
154
  const valueStart = this.position;
137
155
  let depth = 0;
138
156
  let quote = '';
139
157
  let escaped = false;
158
+
140
159
  while (this.position < this.source.length) {
141
160
  this.budget.step();
142
161
  const character = this.source[this.position];
162
+
143
163
  if (quote) {
144
164
  if (escaped) {
145
165
  escaped = false;
@@ -158,18 +178,20 @@ class UiParser {
158
178
  } else if (character === '\n' && depth <= 0) {
159
179
  break;
160
180
  }
181
+
161
182
  this.position += 1;
162
183
  }
184
+
163
185
  try {
164
186
  if (Object.hasOwn(this.result.declarations, name)) {
165
187
  throw new UiFailure('duplicate_declaration', `Duplicate variable ${name}.`);
166
188
  }
189
+
167
190
  const value = this.source.slice(valueStart, this.position).trim();
168
191
  this.result.declarations[name] = parseUiExpression(value, this.budget);
169
192
  } catch (error) {
170
- if (error instanceof UiFailure && error.code === 'budget_exceeded') {
171
- throw error;
172
- }
193
+ rethrowBudget(error);
194
+
173
195
  if (this.final || this.position < this.source.length) {
174
196
  this.diagnose(error, start);
175
197
  }
@@ -180,48 +202,66 @@ class UiParser {
180
202
  private tag(): void {
181
203
  const start = this.position++;
182
204
  const closing = this.source[this.position] === '/';
205
+
183
206
  if (closing) {
184
207
  this.position += 1;
185
208
  }
209
+
186
210
  const match = /^[A-Za-z][A-Za-z\d]*/.exec(this.source.slice(this.position));
211
+
187
212
  if (!match) {
188
213
  if (!this.final && this.position === this.source.length) {
189
214
  return;
190
215
  }
216
+
191
217
  throw new UiFailure('invalid_tag', 'Expected a component name.');
192
218
  }
219
+
193
220
  this.position += match[0].length;
221
+
194
222
  if (closing) {
195
223
  this.whitespace();
224
+
196
225
  if (!this.finish('>')) {
197
226
  return;
198
227
  }
228
+
199
229
  const index = this.stack.findLastIndex((node) => node.type === match[0]);
230
+
200
231
  if (index === -1) {
201
232
  throw new UiFailure('unmatched_tag', `Unexpected closing tag ${match[0]}.`);
202
233
  }
234
+
203
235
  for (const node of this.stack.splice(index)) {
204
236
  node.end = this.position;
205
237
  node.complete = node.type === match[0];
238
+
206
239
  if (!node.complete) {
207
240
  this.diagnose(new UiFailure('unclosed_tag', `The ${node.type} tag is not closed.`), node.start, node.id);
208
241
  }
209
242
  }
243
+
210
244
  return;
211
245
  }
246
+
212
247
  const node = this.node(match[0], start);
213
248
  node.complete = false;
214
249
  this.append(node);
250
+
215
251
  while (this.position < this.source.length) {
216
252
  this.whitespace();
253
+
217
254
  if (this.source.startsWith('/>', this.position)) {
218
255
  this.position += 2;
219
256
  node.complete = true;
220
257
  node.end = this.position;
258
+
221
259
  return;
222
260
  }
261
+
223
262
  if (this.source[this.position] === '>') {
224
263
  this.position += 1;
264
+
225
265
  if (node.type === 'CodeBlock') {
226
266
  const contentStart = this.position;
227
267
  const close = this.source.indexOf('</CodeBlock>', contentStart);
@@ -229,39 +269,48 @@ class UiParser {
229
269
  const text = this.node('$text', contentStart);
230
270
  text.text = this.source.slice(contentStart, this.position);
231
271
  node.children.push(text);
272
+
232
273
  if (close !== -1) {
233
274
  this.position += '</CodeBlock>'.length;
234
275
  node.complete = true;
235
276
  } else if (this.final) {
236
277
  this.diagnose(new UiFailure('unclosed_tag', 'The CodeBlock tag is not closed.'), start, node.id);
237
278
  }
279
+
238
280
  node.end = this.position;
281
+
239
282
  return;
240
283
  }
284
+
241
285
  // A tag that lost a prop and has no closing tag anywhere after it was meant to close itself.
242
286
  if (node.invalid && this.final && !this.closesLater(node.type)) {
243
287
  node.complete = true;
244
288
  node.end = this.position;
289
+
245
290
  return;
246
291
  }
292
+
247
293
  this.stack.push(node);
294
+
248
295
  return;
249
296
  }
297
+
250
298
  const propStart = this.position;
299
+
251
300
  try {
252
301
  if (!this.prop(node)) {
253
302
  return;
254
303
  }
255
304
  } catch (error) {
256
- if (error instanceof UiFailure && error.code === 'budget_exceeded') {
257
- throw error;
258
- }
305
+ rethrowBudget(error);
259
306
  this.diagnose(error, propStart, node.id);
260
307
  node.invalid = true;
261
308
  this.skipToken();
262
309
  }
263
310
  }
311
+
264
312
  node.end = this.position;
313
+
265
314
  if (this.final) {
266
315
  this.diagnose(new UiFailure('unclosed_tag', `The ${node.type} tag is incomplete.`), start, node.id);
267
316
  }
@@ -273,77 +322,101 @@ class UiParser {
273
322
  if (this.position === this.source.length) {
274
323
  return false;
275
324
  }
325
+
276
326
  const prop = /^[A-Za-z_][A-Za-z_\d]*/.exec(this.source.slice(this.position));
327
+
277
328
  if (!prop) {
278
329
  throw new UiFailure('invalid_prop', 'Expected a named prop.');
279
330
  }
331
+
280
332
  const name = safeKey(prop[0]);
281
333
  this.position += name.length;
282
334
  this.whitespace();
335
+
283
336
  if (this.position === this.source.length && !this.final) {
284
337
  return false;
285
338
  }
339
+
286
340
  if (this.source[this.position] !== '=') {
287
341
  // A bare name is true, as in JSX.
288
342
  if (Object.hasOwn(node.props, name)) {
289
343
  throw new UiFailure('duplicate_prop', `Duplicate prop ${name}.`);
290
344
  }
345
+
291
346
  node.props[name] = parseUiExpression('true', this.budget);
347
+
292
348
  return true;
293
349
  }
350
+
294
351
  this.position += 1;
295
352
  this.whitespace();
296
353
  let value: string;
354
+
297
355
  if (this.source[this.position] === '{') {
298
356
  const body = this.balanced();
357
+
299
358
  if (!body.complete) {
300
359
  return false;
301
360
  }
361
+
302
362
  value = body.value;
303
363
  } else {
304
364
  const quote = this.source[this.position];
365
+
305
366
  if (quote !== '"' && quote !== "'") {
306
367
  if (!this.final && this.position === this.source.length) {
307
368
  return false;
308
369
  }
370
+
309
371
  throw new UiFailure('invalid_prop', 'Use a quoted string or a braced expression.');
310
372
  }
373
+
311
374
  const valueStart = this.position++;
312
375
  let escaped = false;
313
376
  let closed = false;
377
+
314
378
  while (this.position < this.source.length) {
315
379
  this.budget.step();
316
380
  const character = this.source[this.position++];
381
+
317
382
  if (escaped) {
318
383
  escaped = false;
319
384
  } else if (character === '\\') {
320
385
  escaped = true;
321
386
  } else if (character === quote) {
322
387
  closed = true;
388
+
323
389
  break;
324
390
  }
325
391
  }
392
+
326
393
  if (!closed && !this.final) {
327
394
  value = this.source.slice(valueStart, this.position);
395
+
328
396
  if (escaped) {
329
397
  value = value.slice(0, -1);
330
398
  }
399
+
331
400
  try {
332
401
  node.props[name] = parseUiExpression(`${value}${quote}`, this.budget);
333
402
  } catch (error) {
334
- if (error instanceof UiFailure && error.code === 'budget_exceeded') {
335
- throw error;
336
- }
403
+ rethrowBudget(error);
337
404
  }
405
+
338
406
  node.end = this.position;
407
+
339
408
  return false;
340
409
  }
410
+
341
411
  value = this.source.slice(valueStart, this.position);
342
412
  }
413
+
343
414
  if (Object.hasOwn(node.props, name)) {
344
415
  throw new UiFailure('duplicate_prop', `Duplicate prop ${name}.`);
345
416
  }
417
+
346
418
  node.props[name] = parseUiExpression(value, this.budget);
419
+
347
420
  return true;
348
421
  }
349
422
 
@@ -351,9 +424,11 @@ class UiParser {
351
424
  private skipToken(): void {
352
425
  let quote = '';
353
426
  let depth = 0;
427
+
354
428
  while (this.position < this.source.length) {
355
429
  this.budget.step();
356
430
  const character = this.source[this.position];
431
+
357
432
  if (quote) {
358
433
  if (character === '\\') {
359
434
  this.position += 1;
@@ -369,6 +444,7 @@ class UiParser {
369
444
  } else if (depth === 0 && (/\s/.test(character) || character === '>' || this.source.startsWith('/>', this.position))) {
370
445
  break;
371
446
  }
447
+
372
448
  this.position += 1;
373
449
  }
374
450
  }
@@ -378,9 +454,11 @@ class UiParser {
378
454
  let depth = 1;
379
455
  let quote = '';
380
456
  let escaped = false;
457
+
381
458
  while (this.position < this.source.length) {
382
459
  this.budget.step();
383
460
  const character = this.source[this.position++];
461
+
384
462
  if (quote) {
385
463
  if (escaped) {
386
464
  escaped = false;
@@ -398,9 +476,11 @@ class UiParser {
398
476
  return { value: this.source.slice(start, this.position - 1), complete: true };
399
477
  }
400
478
  }
479
+
401
480
  if (this.final) {
402
481
  throw new UiFailure('incomplete_expression', 'The expression is not closed.');
403
482
  }
483
+
404
484
  return { value: this.source.slice(start), complete: false };
405
485
  }
406
486
 
@@ -408,6 +488,7 @@ class UiParser {
408
488
  private closesLater(type: string): boolean {
409
489
  const closing = new RegExp(`</${type}\\s*>`, 'g');
410
490
  closing.lastIndex = this.position;
491
+
411
492
  return closing.test(this.source);
412
493
  }
413
494
 
@@ -423,14 +504,18 @@ class UiParser {
423
504
  if (!this.final && this.position === this.source.length) {
424
505
  return false;
425
506
  }
507
+
426
508
  throw new UiFailure('invalid_syntax', `Expected ${value}.`);
427
509
  }
510
+
428
511
  this.position += 1;
512
+
429
513
  return true;
430
514
  }
431
515
 
432
516
  private node(type: string, start: number): UiSyntaxNode {
433
517
  this.budget.node();
518
+
434
519
  return { id: `${this.id}:${start}`, type, props: Object.create(null), children: [], complete: true, start, end: this.position };
435
520
  }
436
521
 
package/src/text.ts CHANGED
@@ -6,6 +6,7 @@ export function uiCompactCatalog(): string {
6
6
  return Object.entries(UI_CATALOG)
7
7
  .map(([name, entry]) => {
8
8
  const props = uiPropList(entry).join(',');
9
+
9
10
  return `${name}${props ? `: ${props}` : ''}`;
10
11
  })
11
12
  .join(' · ');
@@ -47,11 +48,13 @@ function uiGroupNames(detailHint: string | undefined): string {
47
48
  .map(([name, entry]) => uiRequirement(name, entry))
48
49
  .join(', ')}`
49
50
  );
51
+
50
52
  return `Components by group${detailHint ? ` (${detailHint})` : ''}, required props (text: words between the tags):\n${lines.join('\n')}`;
51
53
  }
52
54
 
53
55
  export function uiSessionNote({ fenceLanguage = UI_FENCE_LANGUAGE, catalog = 'props', detailHint }: UiTextOptions = {}): string {
54
56
  const components = catalog === 'names' ? uiGroupNames(detailHint) : `Catalog (? means optional props): ${uiCompactCatalog()}`;
57
+
55
58
  return `${uiSyntax(fenceLanguage)} Prose outside. Only named catalog props, no styling. $name = literal JSON; input value={$name}; Button action={@Set($name, literal)}, {@Reset($name)} or {@Reset()} changes local state only; braced expressions: @Count, @Filter(list,row,predicate), @Sum, @Join, @Round. Show/Each control children. Choices are shortcuts after finished work that the person may ignore. When your turn would end waiting on the person (a missing fact, a decision, a go-ahead for work you would otherwise do), ask with your own question tool if you have one, otherwise in prose, never with Choices. Choice labels and precise context stay visible and send once. Image: attachment or generated="latest", never URL/path. Model only when the person asks to see one. No code execution. State resets on reload. ${components}\nExample:\n${uiExample(fenceLanguage)}\n`;
56
59
  }
57
60
 
@@ -59,25 +62,31 @@ export function uiSessionNote({ fenceLanguage = UI_FENCE_LANGUAGE, catalog = 'pr
59
62
  export function uiGroupReference(group: UiGroupName): string {
60
63
  const components = uiGroupComponents(group).map(([name, entry]) => {
61
64
  const props = uiPropList(entry).join(', ');
65
+
62
66
  return `${name}: ${entry.description}${props ? ` Props: ${props}.` : ''}`;
63
67
  });
68
+
64
69
  return [UI_GROUPS[group].description, UI_GROUPS[group].rules, ...components].join('\n');
65
70
  }
66
71
 
67
72
  export function uiReferenceText({ fenceLanguage = UI_FENCE_LANGUAGE }: UiTextOptions = {}): string {
68
73
  const groups = (Object.keys(UI_GROUPS) as UiGroupName[]).map(uiGroupReference).join('\n\n');
74
+
69
75
  return `${uiSyntax(fenceLanguage)}\nExample:\n${uiExample(fenceLanguage)}\n\n${groups}`;
70
76
  }
71
77
 
72
78
  export function uiFallbackText(text: string, blocks: readonly UiBlock[]): string {
73
79
  let result = '';
74
80
  let position = 0;
81
+
75
82
  for (const block of [...blocks].sort((first, second) => first.start - second.start)) {
76
83
  if (block.start < position || block.end < block.start || block.end > text.length) {
77
84
  continue;
78
85
  }
86
+
79
87
  result += text.slice(position, block.start) + block.fallback + '\n';
80
88
  position = block.end;
81
89
  }
90
+
82
91
  return result + text.slice(position);
83
92
  }