@adecore/intelligent-ui 0.38.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/dist/compiler.js +10 -12
- package/dist/expression.js +18 -23
- package/dist/inputs.d.ts +1 -0
- package/dist/inputs.js +23 -20
- package/dist/links.js +6 -12
- package/dist/places.js +6 -12
- package/dist/rethrow.d.ts +1 -0
- package/dist/rethrow.js +7 -0
- package/dist/runtime.js +12 -12
- package/dist/syntax.js +5 -12
- package/package.json +4 -4
- package/src/actions.ts +7 -0
- package/src/budget.ts +9 -0
- package/src/compiler.ts +111 -15
- package/src/event-time.ts +14 -0
- package/src/event-warning.ts +2 -0
- package/src/expression.ts +146 -23
- package/src/fallback.ts +6 -0
- package/src/inputs.ts +37 -20
- package/src/links.ts +10 -13
- package/src/places.ts +9 -12
- package/src/prop-alias.ts +1 -0
- package/src/prop-list.ts +2 -0
- package/src/query.ts +15 -0
- package/src/rethrow.ts +8 -0
- package/src/runtime.ts +95 -19
- package/src/stream.ts +9 -0
- package/src/syntax.ts +97 -12
- package/src/text.ts +9 -0
package/src/compiler.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { unreadableEventTime } from './event-warning.ts';
|
|
|
5
5
|
import { uiNodeFallback, uiPlainText } from './fallback.ts';
|
|
6
6
|
import { aliasedProp } from './prop-alias.ts';
|
|
7
7
|
import { evaluateUiExpression, sameUiValue, type UiExpression, type UiValue } from './expression.ts';
|
|
8
|
+
import { rethrowBudget } from './rethrow.ts';
|
|
8
9
|
import { parseUiSyntax, uiDiagnostic, type UiDiagnostic, type UiSyntaxNode } from './syntax.ts';
|
|
9
10
|
import { type z } from 'zod';
|
|
10
11
|
|
|
@@ -19,20 +20,28 @@ export const UI_HOST_LIMITS = { queries: 8, links: 64, places: 50, refreshMillis
|
|
|
19
20
|
/* A Place that names an address, whose tag may hold quoted text with a `>` before it. */
|
|
20
21
|
const ADDRESSED_PLACE = /<Place\b(?:[^>"']|"[^"]*"|'[^']*')*?\baddress\s*=/;
|
|
21
22
|
|
|
23
|
+
let linkTag: RegExp | undefined;
|
|
24
|
+
|
|
22
25
|
/* A cheap test for whether `text` may declare a query or name a link target, before anything is read for it. */
|
|
23
26
|
export function uiMayReferenceHost(text: string): boolean {
|
|
24
27
|
if (text.includes('@Query')) {
|
|
25
28
|
return true;
|
|
26
29
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
.
|
|
30
|
-
|
|
30
|
+
|
|
31
|
+
linkTag ??= new RegExp(
|
|
32
|
+
`<(?:${Object.entries(UI_CATALOG)
|
|
33
|
+
.filter(([, entry]) => entry.group === 'links')
|
|
34
|
+
.map(([name]) => name)
|
|
35
|
+
.join('|')})\\b`
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return linkTag.test(text) || ADDRESSED_PLACE.test(text);
|
|
31
39
|
}
|
|
32
40
|
|
|
33
41
|
/* The children of a Map as its fallback numbers them: each Place that drew, also inside a Show or an Each, in order, and anything else as it is. */
|
|
34
42
|
function numberedPlaces(children: readonly UiNode[]): string {
|
|
35
43
|
let number = 0;
|
|
44
|
+
|
|
36
45
|
const visit = (nodes: readonly UiNode[]): string =>
|
|
37
46
|
nodes
|
|
38
47
|
.map((child) =>
|
|
@@ -43,6 +52,7 @@ function numberedPlaces(children: readonly UiNode[]): string {
|
|
|
43
52
|
: child.fallback
|
|
44
53
|
)
|
|
45
54
|
.join('');
|
|
55
|
+
|
|
46
56
|
return visit(children);
|
|
47
57
|
}
|
|
48
58
|
|
|
@@ -91,14 +101,7 @@ export interface UiCompileOptions {
|
|
|
91
101
|
now?: () => number;
|
|
92
102
|
}
|
|
93
103
|
|
|
94
|
-
|
|
95
|
-
function rethrowBudget(error: unknown): void {
|
|
96
|
-
if (error instanceof UiFailure && error.code === 'budget_exceeded') {
|
|
97
|
-
throw error;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function isQuery(expression: UiExpression): boolean {
|
|
104
|
+
function isQuery(expression: UiExpression): expression is Extract<UiExpression, { kind: 'call' }> {
|
|
102
105
|
return expression.kind === 'call' && expression.name === 'Query';
|
|
103
106
|
}
|
|
104
107
|
|
|
@@ -107,9 +110,11 @@ function checkQueryReferences(part: unknown, defaults: Readonly<Record<string, U
|
|
|
107
110
|
if (!part || typeof part !== 'object') {
|
|
108
111
|
return;
|
|
109
112
|
}
|
|
113
|
+
|
|
110
114
|
if ('kind' in part && part.kind === 'reference' && 'name' in part && !Object.hasOwn(defaults, String(part.name))) {
|
|
111
115
|
throw new UiFailure('invalid_query', 'Query arguments may reference only declared local state.');
|
|
112
116
|
}
|
|
117
|
+
|
|
113
118
|
for (const value of Object.values(part)) {
|
|
114
119
|
checkQueryReferences(value, defaults);
|
|
115
120
|
}
|
|
@@ -124,23 +129,30 @@ function compileQuery(
|
|
|
124
129
|
if (expression.args.length !== 2 || !isConstant(expression.args[0]) || expression.args[1].kind !== 'object') {
|
|
125
130
|
throw new UiFailure('invalid_query', '@Query needs a literal source and an argument record.');
|
|
126
131
|
}
|
|
132
|
+
|
|
127
133
|
const querySource = evaluateUiExpression(expression.args[0], {}, budget);
|
|
128
134
|
const argument = expression.args[1];
|
|
129
135
|
checkQueryReferences(argument, block.defaults);
|
|
130
136
|
const args = evaluateUiExpression(argument, block.defaults, budget);
|
|
137
|
+
|
|
131
138
|
if (typeof querySource !== 'string' || !args || Array.isArray(args) || typeof args !== 'object') {
|
|
132
139
|
throw new UiFailure('invalid_query', 'Invalid query source or argument record.');
|
|
133
140
|
}
|
|
141
|
+
|
|
134
142
|
if (!querySchemas || !Object.hasOwn(querySchemas, querySource)) {
|
|
135
143
|
throw new UiFailure('unknown_query', 'The host did not register this query source.');
|
|
136
144
|
}
|
|
145
|
+
|
|
137
146
|
const checked = querySchemas[querySource].safeParse(args);
|
|
147
|
+
|
|
138
148
|
if (!checked.success) {
|
|
139
149
|
throw new UiFailure('invalid_query', 'Query arguments do not match the host schema.');
|
|
140
150
|
}
|
|
151
|
+
|
|
141
152
|
if (Object.keys(block.queries).length >= UI_HOST_LIMITS.queries) {
|
|
142
153
|
throw new UiFailure('budget_exceeded', `A block may declare at most ${UI_HOST_LIMITS.queries} queries.`);
|
|
143
154
|
}
|
|
155
|
+
|
|
144
156
|
return { source: querySource, args, ...(!isConstant(argument) ? { expression: argument } : {}) };
|
|
145
157
|
}
|
|
146
158
|
|
|
@@ -173,6 +185,7 @@ function countChildren(children: readonly UiSyntaxNode[], kinds: readonly string
|
|
|
173
185
|
/* The keys besides `label` that hold a finite number in some row of a chart's data. */
|
|
174
186
|
function chartSeries(data: unknown): number {
|
|
175
187
|
const keys = new Set<string>();
|
|
188
|
+
|
|
176
189
|
for (const row of Array.isArray(data) ? data : []) {
|
|
177
190
|
for (const [key, value] of Object.entries((row ?? {}) as Record<string, unknown>)) {
|
|
178
191
|
if (key !== 'label' && typeof value === 'number' && Number.isFinite(value)) {
|
|
@@ -180,6 +193,7 @@ function chartSeries(data: unknown): number {
|
|
|
180
193
|
}
|
|
181
194
|
}
|
|
182
195
|
}
|
|
196
|
+
|
|
183
197
|
return keys.size;
|
|
184
198
|
}
|
|
185
199
|
|
|
@@ -190,6 +204,7 @@ function isLabelSource(child: UiSyntaxNode): boolean {
|
|
|
190
204
|
|
|
191
205
|
export function compileUiBlock(source: string, options: UiCompileOptions): UiBlock {
|
|
192
206
|
const budget = new UiBudget(options.limits, options.now);
|
|
207
|
+
|
|
193
208
|
const block: UiBlock = {
|
|
194
209
|
id: options.id,
|
|
195
210
|
catalogVersion: UI_CATALOG_VERSION,
|
|
@@ -202,21 +217,29 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
202
217
|
diagnostics: [],
|
|
203
218
|
fallback: ''
|
|
204
219
|
};
|
|
220
|
+
|
|
205
221
|
const diagnose = (error: unknown, start: number, end: number, nodeId?: string) => {
|
|
206
222
|
if (block.diagnostics.length < budget.limits.diagnostics) {
|
|
207
223
|
block.diagnostics.push(uiDiagnostic(error, start, end, nodeId));
|
|
208
224
|
}
|
|
209
225
|
};
|
|
226
|
+
|
|
227
|
+
const local = uiLocalState(block);
|
|
228
|
+
|
|
210
229
|
// A pressed action must set the same value whatever state it is pressed in, so a choice can check it.
|
|
211
230
|
const checkedAction = (action: UiExpression): UiExpression => {
|
|
212
|
-
uiActionTarget(action,
|
|
231
|
+
uiActionTarget(action, local);
|
|
232
|
+
|
|
213
233
|
if (action.kind === 'call' && action.name === 'Set' && !isConstant(action.args[1])) {
|
|
214
234
|
throw new UiFailure('refused_action', '@Set in an action takes a literal value.');
|
|
215
235
|
}
|
|
236
|
+
|
|
216
237
|
return action;
|
|
217
238
|
};
|
|
239
|
+
|
|
218
240
|
const compile = (syntax: UiSyntaxNode, depth: number, parent?: string): UiNode => {
|
|
219
241
|
budget.depth(depth);
|
|
242
|
+
|
|
220
243
|
const node: UiNode = {
|
|
221
244
|
id: syntax.id,
|
|
222
245
|
type: syntax.type,
|
|
@@ -229,11 +252,14 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
229
252
|
start: syntax.start,
|
|
230
253
|
end: syntax.end
|
|
231
254
|
};
|
|
255
|
+
|
|
232
256
|
let fallbackProps = node.props;
|
|
257
|
+
|
|
233
258
|
try {
|
|
234
259
|
if (syntax.type === '$text') {
|
|
235
260
|
if (syntax.expression) {
|
|
236
261
|
node.expressions.text = syntax.expression;
|
|
262
|
+
|
|
237
263
|
try {
|
|
238
264
|
node.fallback = uiPlainText(evaluateUiExpression(syntax.expression, block.defaults, budget));
|
|
239
265
|
} catch (error) {
|
|
@@ -243,20 +269,27 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
243
269
|
node.props.text = syntax.text ?? '';
|
|
244
270
|
node.fallback = syntax.text ?? '';
|
|
245
271
|
}
|
|
272
|
+
|
|
246
273
|
return node;
|
|
247
274
|
}
|
|
275
|
+
|
|
248
276
|
if (!isUiComponent(syntax.type)) {
|
|
249
277
|
throw new UiFailure('unknown_component', `Unknown component ${syntax.type}.`);
|
|
250
278
|
}
|
|
279
|
+
|
|
251
280
|
const entry = UI_CATALOG[syntax.type];
|
|
281
|
+
|
|
252
282
|
if ('parent' in entry) {
|
|
253
283
|
const parents: readonly string[] = [entry.parent].flat();
|
|
284
|
+
|
|
254
285
|
if (parent === undefined || !parents.includes(parent)) {
|
|
255
286
|
throw new UiFailure('invalid_parent', `${syntax.type} belongs inside ${parents.join(' or ')}.`);
|
|
256
287
|
}
|
|
257
288
|
}
|
|
289
|
+
|
|
258
290
|
if (parent && isUiComponent(parent)) {
|
|
259
291
|
const container = UI_CATALOG[parent];
|
|
292
|
+
|
|
260
293
|
if (
|
|
261
294
|
'children' in container &&
|
|
262
295
|
!(container.children as readonly string[]).includes(syntax.type) &&
|
|
@@ -266,18 +299,26 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
266
299
|
throw new UiFailure('invalid_child', `${syntax.type} is not a child of ${parent}.`);
|
|
267
300
|
}
|
|
268
301
|
}
|
|
302
|
+
|
|
269
303
|
const actionProp = uiActionProp(syntax.type);
|
|
304
|
+
|
|
270
305
|
for (const [written, expression] of Object.entries(syntax.props)) {
|
|
271
306
|
safeKey(written);
|
|
307
|
+
|
|
272
308
|
if (written === actionProp) {
|
|
273
309
|
node.expressions[written] = checkedAction(expression);
|
|
310
|
+
|
|
274
311
|
continue;
|
|
275
312
|
}
|
|
313
|
+
|
|
276
314
|
const key = aliasedProp(entry, written, syntax.props);
|
|
315
|
+
|
|
277
316
|
if (!Object.hasOwn(entry.schema.shape, key)) {
|
|
278
317
|
diagnose(new UiFailure('refused_prop', `${syntax.type} does not accept ${written}.`), syntax.start, syntax.end, syntax.id);
|
|
318
|
+
|
|
279
319
|
continue;
|
|
280
320
|
}
|
|
321
|
+
|
|
281
322
|
if (key !== written && syntax.complete) {
|
|
282
323
|
diagnose(
|
|
283
324
|
new UiFailure('renamed_prop', `${syntax.type} takes ${key}, not ${written}; read as ${key}.`),
|
|
@@ -286,29 +327,36 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
286
327
|
syntax.id
|
|
287
328
|
);
|
|
288
329
|
}
|
|
330
|
+
|
|
289
331
|
if (isConstant(expression)) {
|
|
290
332
|
node.props[key] = evaluateUiExpression(expression, {}, budget);
|
|
291
333
|
} else {
|
|
292
334
|
node.expressions[key] = expression;
|
|
293
|
-
|
|
335
|
+
|
|
336
|
+
if ('binding' in entry && entry.binding === key && expression.kind === 'reference' && local(expression.name)) {
|
|
294
337
|
node.bindings[key] = expression.name;
|
|
295
338
|
}
|
|
296
339
|
}
|
|
297
340
|
}
|
|
341
|
+
|
|
298
342
|
if (syntax.type === 'Image' && node.props.generated === 'latest' && options.latestAttachment) {
|
|
299
343
|
delete node.props.generated;
|
|
300
344
|
node.props.attachment = options.latestAttachment;
|
|
301
345
|
}
|
|
346
|
+
|
|
302
347
|
if (actionProp !== undefined && syntax.complete && !node.expressions[actionProp]) {
|
|
303
348
|
throw new UiFailure('invalid_props', `${syntax.type} needs ${actionProp}.`);
|
|
304
349
|
}
|
|
350
|
+
|
|
305
351
|
const evaluated: Record<string, UiValue> = { ...node.props };
|
|
306
352
|
fallbackProps = evaluated;
|
|
307
353
|
let unresolved = false;
|
|
354
|
+
|
|
308
355
|
for (const [key, expression] of Object.entries(node.expressions)) {
|
|
309
356
|
if (key === actionProp) {
|
|
310
357
|
continue;
|
|
311
358
|
}
|
|
359
|
+
|
|
312
360
|
try {
|
|
313
361
|
evaluated[key] = evaluateUiExpression(expression, block.defaults, budget);
|
|
314
362
|
} catch (error) {
|
|
@@ -316,17 +364,22 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
316
364
|
unresolved = true;
|
|
317
365
|
}
|
|
318
366
|
}
|
|
367
|
+
|
|
319
368
|
// Each locals and live query data are only available to the client evaluator.
|
|
320
369
|
if (!unresolved && syntax.complete) {
|
|
321
370
|
const parsed = entry.schema.safeParse(evaluated);
|
|
371
|
+
|
|
322
372
|
if (!parsed.success) {
|
|
323
373
|
throw new UiFailure('invalid_props', parsed.error.issues.map((issue) => `${issue.path.join('.')}: ${issue.message}`).join('; '));
|
|
324
374
|
}
|
|
325
375
|
}
|
|
376
|
+
|
|
326
377
|
const unreadable = !unresolved && syntax.complete && syntax.type === 'Event' ? unreadableEventTime(evaluated.at) : null;
|
|
378
|
+
|
|
327
379
|
if (unreadable !== null) {
|
|
328
380
|
diagnose(unreadable, syntax.start, syntax.end, syntax.id);
|
|
329
381
|
}
|
|
382
|
+
|
|
330
383
|
// The catalog promises six series, one per chart color; more still reach the values a person can open.
|
|
331
384
|
if (!unresolved && syntax.complete && syntax.type === 'Chart' && chartSeries(evaluated.data) > CHART_SERIES) {
|
|
332
385
|
diagnose(
|
|
@@ -336,9 +389,11 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
336
389
|
syntax.id
|
|
337
390
|
);
|
|
338
391
|
}
|
|
392
|
+
|
|
339
393
|
if (syntax.complete && 'count' in entry) {
|
|
340
394
|
const counted = countChildren(syntax.children, entry.children);
|
|
341
395
|
const limit = entry.count as { min: number; max?: number };
|
|
396
|
+
|
|
342
397
|
if (counted < limit.min || (limit.max !== undefined && counted > limit.max)) {
|
|
343
398
|
throw new UiFailure(
|
|
344
399
|
'invalid_props',
|
|
@@ -348,6 +403,7 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
348
403
|
);
|
|
349
404
|
}
|
|
350
405
|
}
|
|
406
|
+
|
|
351
407
|
// A Choice sends its label, so one without a label can never send; whether it closed is only known once final.
|
|
352
408
|
if (syntax.type === 'Choice' && syntax.complete && !syntax.children.some(isLabelSource)) {
|
|
353
409
|
throw new UiFailure('invalid_props', 'A choice needs a visible label.');
|
|
@@ -357,17 +413,21 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
357
413
|
node.error = error instanceof UiFailure ? error.code : 'invalid_props';
|
|
358
414
|
diagnose(error, syntax.start, syntax.end, syntax.id);
|
|
359
415
|
}
|
|
416
|
+
|
|
360
417
|
// The parser already diagnosed both, so they only decide how the node draws.
|
|
361
418
|
if (node.error === undefined && syntax.invalid) {
|
|
362
419
|
node.error = 'invalid_syntax';
|
|
363
420
|
}
|
|
421
|
+
|
|
364
422
|
if (node.error === undefined && syntax.type === 'Choice' && options.final && !syntax.complete) {
|
|
365
423
|
node.error = 'unclosed_tag';
|
|
366
424
|
}
|
|
425
|
+
|
|
367
426
|
const effectiveParent = syntax.type === 'Show' || syntax.type === 'Each' ? parent : syntax.type;
|
|
368
427
|
node.children = syntax.children.map((child) => compile(child, depth + 1, effectiveParent));
|
|
369
428
|
// A reader of the fallback has no control to look at, so the option a dropdown or a segmented control starts on says it is chosen.
|
|
370
429
|
const chosen = syntax.type === 'Select' || syntax.type === 'Segmented' ? fallbackProps.value : undefined;
|
|
430
|
+
|
|
371
431
|
const childText =
|
|
372
432
|
syntax.type === 'Map'
|
|
373
433
|
? numberedPlaces(node.children)
|
|
@@ -380,29 +440,36 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
380
440
|
: child.fallback
|
|
381
441
|
)
|
|
382
442
|
.join('');
|
|
443
|
+
|
|
383
444
|
node.fallback = uiNodeFallback(syntax.type, fallbackProps, childText);
|
|
445
|
+
|
|
384
446
|
// A part drawn as its text keeps the words written inside it, also where its kind reads only props.
|
|
385
447
|
if (node.error !== undefined && childText.trim() !== '' && !node.fallback.includes(childText.trim())) {
|
|
386
448
|
node.fallback += childText;
|
|
387
449
|
}
|
|
450
|
+
|
|
388
451
|
if (!node.fallback.trim()) {
|
|
389
452
|
node.fallback = source.slice(syntax.start, Math.min(syntax.end, syntax.start + 1024));
|
|
390
453
|
}
|
|
454
|
+
|
|
391
455
|
return node;
|
|
392
456
|
};
|
|
457
|
+
|
|
393
458
|
try {
|
|
394
459
|
const syntax = parseUiSyntax(source, options.id, options.final, budget);
|
|
395
460
|
block.diagnostics = syntax.diagnostics;
|
|
396
461
|
// Defaults first, so a query argument can read every one of them.
|
|
397
462
|
const declarations = Object.entries(syntax.declarations).sort(([, left], [, right]) => Number(isQuery(left)) - Number(isQuery(right)));
|
|
463
|
+
|
|
398
464
|
for (const [name, expression] of declarations) {
|
|
399
465
|
try {
|
|
400
|
-
if (expression
|
|
466
|
+
if (isQuery(expression)) {
|
|
401
467
|
block.queries[name] = compileQuery(expression, block, budget, options.querySchemas);
|
|
402
468
|
} else {
|
|
403
469
|
if (!isConstant(expression)) {
|
|
404
470
|
throw new UiFailure('invalid_default', 'State defaults must be literal JSON values.');
|
|
405
471
|
}
|
|
472
|
+
|
|
406
473
|
block.defaults[name] = evaluateUiExpression(expression, {}, budget);
|
|
407
474
|
}
|
|
408
475
|
} catch (error) {
|
|
@@ -410,7 +477,9 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
410
477
|
diagnose(error, 0, source.length);
|
|
411
478
|
}
|
|
412
479
|
}
|
|
480
|
+
|
|
413
481
|
block.nodes = syntax.nodes.map((node) => compile(node, 0));
|
|
482
|
+
|
|
414
483
|
block.fallback = block.nodes
|
|
415
484
|
.map((node) => node.fallback)
|
|
416
485
|
.join('')
|
|
@@ -422,9 +491,11 @@ export function compileUiBlock(source: string, options: UiCompileOptions): UiBlo
|
|
|
422
491
|
block.diagnostics = [uiDiagnostic(error, 0, source.length)];
|
|
423
492
|
block.fallback = source.slice(0, budget.limits.characters);
|
|
424
493
|
}
|
|
494
|
+
|
|
425
495
|
if (!block.fallback.trim()) {
|
|
426
496
|
block.fallback = source.slice(0, budget.limits.characters);
|
|
427
497
|
}
|
|
498
|
+
|
|
428
499
|
return block;
|
|
429
500
|
}
|
|
430
501
|
|
|
@@ -457,7 +528,9 @@ export class UiCompiler {
|
|
|
457
528
|
const language = this.options.fenceLanguage ?? UI_FENCE_LANGUAGE;
|
|
458
529
|
const diagnostics = this.options.limits?.diagnostics ?? UI_LIMITS.diagnostics;
|
|
459
530
|
const started = UI_REPLY_LIMITS.milliseconds > 0 ? now() : 0;
|
|
531
|
+
|
|
460
532
|
const nodeCount = (items: readonly UiNode[]): number => items.reduce((count, node) => count + 1 + nodeCount(node.children), 0);
|
|
533
|
+
|
|
461
534
|
const rejected = (start: number, codeStart: number, codeEnd: number, end: number, complete: boolean): UiBlock => ({
|
|
462
535
|
id: `${this.options.id}:ui:${start}`,
|
|
463
536
|
catalogVersion: UI_CATALOG_VERSION,
|
|
@@ -470,12 +543,15 @@ export class UiCompiler {
|
|
|
470
543
|
fallback: text.slice(codeStart, Math.min(codeEnd, codeStart + 4096)),
|
|
471
544
|
diagnostics: [uiDiagnostic(new UiFailure('budget_exceeded', 'This reply exceeded its UI budget; remaining fences stay text.'), start, end)]
|
|
472
545
|
});
|
|
546
|
+
|
|
473
547
|
const append = (start: number, codeStart: number, codeEnd: number, end: number, closed: boolean) => {
|
|
474
548
|
if (exhausted) {
|
|
475
549
|
return;
|
|
476
550
|
}
|
|
551
|
+
|
|
477
552
|
if (blocks.length >= UI_REPLY_LIMITS.blocks) {
|
|
478
553
|
const last = blocks.at(-1)!;
|
|
554
|
+
|
|
479
555
|
blocks[blocks.length - 1] = {
|
|
480
556
|
...last,
|
|
481
557
|
diagnostics: [
|
|
@@ -483,24 +559,32 @@ export class UiCompiler {
|
|
|
483
559
|
uiDiagnostic(new UiFailure('budget_exceeded', 'Further UI fences stay text because this reply has too many blocks.'), start, end)
|
|
484
560
|
]
|
|
485
561
|
};
|
|
562
|
+
|
|
486
563
|
exhausted = true;
|
|
564
|
+
|
|
487
565
|
return;
|
|
488
566
|
}
|
|
567
|
+
|
|
489
568
|
const complete = closed || update.final === true;
|
|
490
569
|
characters += codeEnd - codeStart;
|
|
570
|
+
|
|
491
571
|
if (characters > UI_REPLY_LIMITS.characters || (UI_REPLY_LIMITS.milliseconds > 0 && now() - started > UI_REPLY_LIMITS.milliseconds)) {
|
|
492
572
|
blocks.push(rejected(start, codeStart, codeEnd, end, complete));
|
|
493
573
|
exhausted = true;
|
|
574
|
+
|
|
494
575
|
return;
|
|
495
576
|
}
|
|
577
|
+
|
|
496
578
|
const source = text.slice(codeStart, codeEnd);
|
|
497
579
|
const cached = this.cache.get(start);
|
|
580
|
+
|
|
498
581
|
const unchanged =
|
|
499
582
|
!update.final &&
|
|
500
583
|
cached?.source === source &&
|
|
501
584
|
cached.block.end === end &&
|
|
502
585
|
cached.block.complete === complete &&
|
|
503
586
|
cached.latestAttachment === update.latestAttachment;
|
|
587
|
+
|
|
504
588
|
const block = unchanged
|
|
505
589
|
? cached.block
|
|
506
590
|
: compileUiBlock(source, {
|
|
@@ -509,22 +593,28 @@ export class UiCompiler {
|
|
|
509
593
|
id: `${this.options.id}:ui:${start}`,
|
|
510
594
|
final: complete
|
|
511
595
|
});
|
|
596
|
+
|
|
512
597
|
nodes += nodeCount(block.nodes);
|
|
598
|
+
|
|
513
599
|
if (nodes > UI_REPLY_LIMITS.nodes) {
|
|
514
600
|
blocks.push(rejected(start, codeStart, codeEnd, end, complete));
|
|
515
601
|
exhausted = true;
|
|
602
|
+
|
|
516
603
|
return;
|
|
517
604
|
}
|
|
605
|
+
|
|
518
606
|
block.start = start;
|
|
519
607
|
block.end = end;
|
|
520
608
|
blocks.push(block);
|
|
521
609
|
next.set(start, { source, block, latestAttachment: update.latestAttachment });
|
|
522
610
|
};
|
|
611
|
+
|
|
523
612
|
while (position < text.length && !exhausted) {
|
|
524
613
|
const newline = text.indexOf('\n', position);
|
|
525
614
|
const end = newline === -1 ? text.length : newline + 1;
|
|
526
615
|
const line = text.slice(position, end);
|
|
527
616
|
const opening = /^ {0,3}(`{3,}|~{3,})([^\r\n]*)/.exec(line);
|
|
617
|
+
|
|
528
618
|
if (!fence && opening) {
|
|
529
619
|
fence = {
|
|
530
620
|
marker: opening[1][0],
|
|
@@ -537,14 +627,19 @@ export class UiCompiler {
|
|
|
537
627
|
if (fence.ui) {
|
|
538
628
|
append(fence.start, fence.codeStart, position, position + line.length, true);
|
|
539
629
|
}
|
|
630
|
+
|
|
540
631
|
fence = null;
|
|
541
632
|
}
|
|
633
|
+
|
|
542
634
|
position = end;
|
|
543
635
|
}
|
|
636
|
+
|
|
544
637
|
if (fence?.ui) {
|
|
545
638
|
append(fence.start, fence.codeStart, text.length, text.length, false);
|
|
546
639
|
}
|
|
640
|
+
|
|
547
641
|
this.cache = next;
|
|
642
|
+
|
|
548
643
|
return blocks;
|
|
549
644
|
}
|
|
550
645
|
|
|
@@ -556,6 +651,7 @@ export class UiCompiler {
|
|
|
556
651
|
/* A cheap test for whether `text` opens a UI fence at all, before a compiler is worth creating. */
|
|
557
652
|
export function uiHasFence(text: string, fenceLanguage: string = UI_FENCE_LANGUAGE): boolean {
|
|
558
653
|
const language = fenceLanguage.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
654
|
+
|
|
559
655
|
return new RegExp(`^ {0,3}(?:\`{3,}|~{3,})[ \\t]*${language}[ \\t]*\\r?$`, 'm').test(text);
|
|
560
656
|
}
|
|
561
657
|
|
package/src/event-time.ts
CHANGED
|
@@ -19,21 +19,27 @@ function zoneOffset(zone: string): number | null {
|
|
|
19
19
|
if (zone === 'Z') {
|
|
20
20
|
return 0;
|
|
21
21
|
}
|
|
22
|
+
|
|
22
23
|
const [hours, minutes] = [Number(zone.slice(1, 3)), Number(zone.slice(4, 6))];
|
|
23
24
|
const offset = (zone[0] === '-' ? -1 : 1) * (hours * 60 + minutes);
|
|
25
|
+
|
|
24
26
|
return minutes > 59 || offset < -12 * 60 || offset > 14 * 60 ? null : offset;
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
/* The moment an Event's `at` names, or null when it is no `HH:MM[:SS]`, `YYYY-MM-DD` or ISO date-time with an optional offset. */
|
|
28
30
|
export function parseUiEventTime(at: string): UiEventTime | null {
|
|
29
31
|
const match = EVENT_TIME.exec(at);
|
|
32
|
+
|
|
30
33
|
if (match === null) {
|
|
31
34
|
return null;
|
|
32
35
|
}
|
|
36
|
+
|
|
33
37
|
const [, clockHour, clockMinute, clockSecond, year, month, day, hour, minute, second, zone] = match.map((part) => part as string | undefined);
|
|
38
|
+
|
|
34
39
|
if (clockHour !== undefined) {
|
|
35
40
|
// The day is not shown for a time, so any day will do.
|
|
36
41
|
const clockSeconds = Number(clockSecond ?? 0);
|
|
42
|
+
|
|
37
43
|
return validClock(Number(clockHour), Number(clockMinute), clockSeconds)
|
|
38
44
|
? {
|
|
39
45
|
kind: 'time',
|
|
@@ -43,21 +49,29 @@ export function parseUiEventTime(at: string): UiEventTime | null {
|
|
|
43
49
|
}
|
|
44
50
|
: null;
|
|
45
51
|
}
|
|
52
|
+
|
|
46
53
|
const [y, m, d] = [Number(year), Number(month), Number(day)];
|
|
47
54
|
const calendar = new Date(Date.UTC(y, m - 1, d));
|
|
55
|
+
|
|
48
56
|
if (calendar.getUTCFullYear() !== y || calendar.getUTCMonth() !== m - 1 || calendar.getUTCDate() !== d) {
|
|
49
57
|
return null;
|
|
50
58
|
}
|
|
59
|
+
|
|
51
60
|
if (hour === undefined) {
|
|
52
61
|
return { kind: 'date', date: calendar, written: true, seconds: false };
|
|
53
62
|
}
|
|
63
|
+
|
|
54
64
|
const [h, min, sec] = [Number(hour), Number(minute), Number(second ?? 0)];
|
|
65
|
+
|
|
55
66
|
if (!validClock(h, min, sec)) {
|
|
56
67
|
return null;
|
|
57
68
|
}
|
|
69
|
+
|
|
58
70
|
if (zone === undefined) {
|
|
59
71
|
return { kind: 'datetime', date: new Date(Date.UTC(y, m - 1, d, h, min, sec)), written: true, seconds: false };
|
|
60
72
|
}
|
|
73
|
+
|
|
61
74
|
const offset = zoneOffset(zone);
|
|
75
|
+
|
|
62
76
|
return offset === null ? null : { kind: 'datetime', date: new Date(Date.UTC(y, m - 1, d, h, min, sec) - offset * 60000), written: false, seconds: false };
|
|
63
77
|
}
|
package/src/event-warning.ts
CHANGED
|
@@ -6,7 +6,9 @@ export function unreadableEventTime(at: unknown): UiFailure | null {
|
|
|
6
6
|
if (typeof at !== 'string' || parseUiEventTime(at) !== null) {
|
|
7
7
|
return null;
|
|
8
8
|
}
|
|
9
|
+
|
|
9
10
|
const shown = at.length > 40 ? `${at.slice(0, 40)}...` : at;
|
|
11
|
+
|
|
10
12
|
return new UiFailure(
|
|
11
13
|
'unreadable_time',
|
|
12
14
|
`Event at="${shown}" is not HH:MM[:SS], YYYY-MM-DD or YYYY-MM-DDTHH:MM[:SS] with an optional Z or ±HH:MM, so it is shown as written.`
|