@prismshadow/a2ui 0.0.0-stage → 0.2.19
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/LICENSE +201 -0
- package/README.md +28 -2
- package/dist/index.d.ts +857 -0
- package/dist/index.js +3474 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -3
package/dist/index.js
ADDED
|
@@ -0,0 +1,3474 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var A2UI_LIMITS = {
|
|
3
|
+
/** `id` of a choice or form. */
|
|
4
|
+
idPattern: /^[a-z][a-z0-9_-]{0,31}$/,
|
|
5
|
+
/** `id` of a form field (no hyphen: it doubles as an answer key). */
|
|
6
|
+
fieldIdPattern: /^[a-z][a-z0-9_]{0,31}$/,
|
|
7
|
+
question: 120,
|
|
8
|
+
label: 60,
|
|
9
|
+
labelWarn: 40,
|
|
10
|
+
value: 500,
|
|
11
|
+
description: 160,
|
|
12
|
+
options: { min: 2, max: 7, warn: 5 },
|
|
13
|
+
fields: { min: 1, max: 6, warn: 4 },
|
|
14
|
+
// form fields
|
|
15
|
+
fieldHint: 80,
|
|
16
|
+
selectOptions: { min: 2, max: 30, warn: 12 },
|
|
17
|
+
textMaxLength: 500,
|
|
18
|
+
textareaMaxLength: 2e3,
|
|
19
|
+
pattern: 120,
|
|
20
|
+
steps: { min: 1, max: 15, warn: 10 },
|
|
21
|
+
title: 80,
|
|
22
|
+
placeholder: 80,
|
|
23
|
+
unit: 12,
|
|
24
|
+
submitLabel: 24,
|
|
25
|
+
stepText: 200,
|
|
26
|
+
stepCode: 2e3,
|
|
27
|
+
lang: 20,
|
|
28
|
+
calloutText: 400,
|
|
29
|
+
mermaid: { statements: 30, lines: 400 },
|
|
30
|
+
// weather
|
|
31
|
+
place: 60,
|
|
32
|
+
summary: 80,
|
|
33
|
+
source: 40,
|
|
34
|
+
windDirection: 6,
|
|
35
|
+
hourly: { min: 2, max: 24, warn: 12 },
|
|
36
|
+
daily: { min: 2, max: 7 },
|
|
37
|
+
// clock
|
|
38
|
+
zones: { min: 1, max: 4 },
|
|
39
|
+
zoneLabel: 24,
|
|
40
|
+
zone: 64,
|
|
41
|
+
clockTitle: 40,
|
|
42
|
+
// countdown
|
|
43
|
+
countdownLabel: 60,
|
|
44
|
+
// metrics
|
|
45
|
+
metricsTitle: 40,
|
|
46
|
+
metrics: { min: 1, max: 8, warn: 6 },
|
|
47
|
+
metricLabel: 40,
|
|
48
|
+
metricDetail: 60,
|
|
49
|
+
prefix: 4,
|
|
50
|
+
deltaLabel: 24,
|
|
51
|
+
history: { min: 2, max: 60 },
|
|
52
|
+
// chart
|
|
53
|
+
chartTitle: 60,
|
|
54
|
+
seriesLabel: 40,
|
|
55
|
+
categoryLabel: 40,
|
|
56
|
+
axisLabel: 24,
|
|
57
|
+
chartCategories: { min: 1, max: 60, warn: 24 },
|
|
58
|
+
pieSlices: { min: 2, max: 8, warn: 6 },
|
|
59
|
+
chartSeries: { min: 1, max: 6, warn: 4 },
|
|
60
|
+
scatterPoints: { min: 1, max: 500, warn: 200 },
|
|
61
|
+
edges: { min: 3, max: 61 },
|
|
62
|
+
// resources
|
|
63
|
+
host: 40,
|
|
64
|
+
disks: { min: 1, max: 6 },
|
|
65
|
+
diskPath: 40
|
|
66
|
+
};
|
|
67
|
+
var A2UI_SCORING = {
|
|
68
|
+
l2PerWarning: 15,
|
|
69
|
+
passTotal: 70
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/fences.ts
|
|
73
|
+
function splitLines(markdown) {
|
|
74
|
+
return markdown.split("\n").map((line) => line.endsWith("\r") ? line.slice(0, -1) : line);
|
|
75
|
+
}
|
|
76
|
+
var OPEN_FENCE = /^( {0,3})(`{3,}|~{3,})(.*)$/;
|
|
77
|
+
var CLOSE_FENCE = /^ {0,3}(`{3,}|~{3,})\s*$/;
|
|
78
|
+
function scanFences(markdown) {
|
|
79
|
+
const lines = splitLines(markdown);
|
|
80
|
+
const spans = [];
|
|
81
|
+
let open = null;
|
|
82
|
+
for (let i = 0; i < lines.length; i++) {
|
|
83
|
+
const line = lines[i] ?? "";
|
|
84
|
+
if (open === null) {
|
|
85
|
+
const m = OPEN_FENCE.exec(line);
|
|
86
|
+
if (!m) continue;
|
|
87
|
+
const run2 = m[2] ?? "";
|
|
88
|
+
const info = (m[3] ?? "").trim();
|
|
89
|
+
const char = run2.startsWith("~") ? "~" : "`";
|
|
90
|
+
if (char === "`" && info.includes("`")) continue;
|
|
91
|
+
open = {
|
|
92
|
+
char,
|
|
93
|
+
length: run2.length,
|
|
94
|
+
indent: (m[1] ?? "").length,
|
|
95
|
+
info,
|
|
96
|
+
lang: (info.split(/\s+/)[0] ?? "").toLowerCase(),
|
|
97
|
+
startLine: i + 1,
|
|
98
|
+
endLine: i + 1,
|
|
99
|
+
closed: false,
|
|
100
|
+
body: []
|
|
101
|
+
};
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
const close = CLOSE_FENCE.exec(line);
|
|
105
|
+
const run = close?.[1] ?? "";
|
|
106
|
+
if (close && run.startsWith(open.char) && run.length >= open.length) {
|
|
107
|
+
open.endLine = i + 1;
|
|
108
|
+
open.closed = true;
|
|
109
|
+
spans.push(open);
|
|
110
|
+
open = null;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
open.body.push(stripIndent(line, open.indent));
|
|
114
|
+
}
|
|
115
|
+
if (open !== null) {
|
|
116
|
+
open.endLine = lines.length;
|
|
117
|
+
spans.push(open);
|
|
118
|
+
}
|
|
119
|
+
return spans;
|
|
120
|
+
}
|
|
121
|
+
function stripIndent(line, indent) {
|
|
122
|
+
let n = 0;
|
|
123
|
+
while (n < indent && line[n] === " ") n++;
|
|
124
|
+
return line.slice(n);
|
|
125
|
+
}
|
|
126
|
+
function blocksFromFences(spans) {
|
|
127
|
+
const blocks = [];
|
|
128
|
+
for (const span2 of spans) {
|
|
129
|
+
if (span2.lang !== "a2ui" && span2.lang !== "mermaid") continue;
|
|
130
|
+
blocks.push({
|
|
131
|
+
index: blocks.length + 1,
|
|
132
|
+
fence: span2.lang === "a2ui" ? "a2ui" : "mermaid",
|
|
133
|
+
source: span2.body.join("\n"),
|
|
134
|
+
startLine: span2.startLine,
|
|
135
|
+
endLine: span2.endLine,
|
|
136
|
+
closed: span2.closed,
|
|
137
|
+
info: span2.info
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return blocks;
|
|
141
|
+
}
|
|
142
|
+
function findBlocks(markdown) {
|
|
143
|
+
return blocksFromFences(scanFences(markdown));
|
|
144
|
+
}
|
|
145
|
+
function fenceLineSet(spans) {
|
|
146
|
+
const set = /* @__PURE__ */ new Set();
|
|
147
|
+
for (const span2 of spans) {
|
|
148
|
+
for (let n = span2.startLine; n <= span2.endLine; n++) set.add(n);
|
|
149
|
+
}
|
|
150
|
+
return set;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/widgets.ts
|
|
154
|
+
var CONDITION_NAMES = {
|
|
155
|
+
en: {
|
|
156
|
+
clear: "Clear",
|
|
157
|
+
"partly-cloudy": "Partly cloudy",
|
|
158
|
+
cloudy: "Overcast",
|
|
159
|
+
fog: "Fog",
|
|
160
|
+
drizzle: "Drizzle",
|
|
161
|
+
rain: "Rain",
|
|
162
|
+
"heavy-rain": "Heavy rain",
|
|
163
|
+
thunder: "Thunderstorm",
|
|
164
|
+
snow: "Snow",
|
|
165
|
+
sleet: "Sleet",
|
|
166
|
+
wind: "Windy"
|
|
167
|
+
},
|
|
168
|
+
zh: {
|
|
169
|
+
clear: "\u6674",
|
|
170
|
+
"partly-cloudy": "\u591A\u4E91",
|
|
171
|
+
cloudy: "\u9634",
|
|
172
|
+
fog: "\u96FE",
|
|
173
|
+
drizzle: "\u5C0F\u96E8",
|
|
174
|
+
rain: "\u96E8",
|
|
175
|
+
"heavy-rain": "\u5927\u96E8",
|
|
176
|
+
thunder: "\u96F7\u96E8",
|
|
177
|
+
snow: "\u96EA",
|
|
178
|
+
sleet: "\u96E8\u5939\u96EA",
|
|
179
|
+
wind: "\u5927\u98CE"
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
var WORDS = {
|
|
183
|
+
en: {
|
|
184
|
+
used: (amount) => `${amount} used`,
|
|
185
|
+
remaining: (amount) => `${amount} left`,
|
|
186
|
+
today: "Today",
|
|
187
|
+
days: (n) => `${n} ${n === 1 ? "day" : "days"}`,
|
|
188
|
+
hours: (n) => `${n} ${n === 1 ? "hour" : "hours"}`,
|
|
189
|
+
minutes: (n) => `${n} min`,
|
|
190
|
+
seconds: (n) => `${n} sec`
|
|
191
|
+
},
|
|
192
|
+
zh: {
|
|
193
|
+
used: (amount) => `\u5DF2\u7528 ${amount}`,
|
|
194
|
+
remaining: (amount) => `\u5269\u4F59 ${amount}`,
|
|
195
|
+
today: "\u4ECA\u5929",
|
|
196
|
+
days: (n) => `${n} \u5929`,
|
|
197
|
+
hours: (n) => `${n} \u5C0F\u65F6`,
|
|
198
|
+
minutes: (n) => `${n} \u5206`,
|
|
199
|
+
seconds: (n) => `${n} \u79D2`
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
var INSTANT = /^(\d{4})-(\d{2})-(\d{2})(?:T(\d{2}):(\d{2})(?::(\d{2})(?:[.,](\d{1,9}))?)?(Z|[+-]\d{2}(?::?\d{2})?)?)?$/;
|
|
203
|
+
var DATE = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
204
|
+
function dateExists(year, month, day) {
|
|
205
|
+
const date = new Date(Date.UTC(year, month - 1, day));
|
|
206
|
+
return date.getUTCMonth() === month - 1 && date.getUTCDate() === day;
|
|
207
|
+
}
|
|
208
|
+
function parseA2uiInstant(text) {
|
|
209
|
+
const m = INSTANT.exec(text);
|
|
210
|
+
if (m === null) return null;
|
|
211
|
+
const [, y, mo, d, h, mi, s, fraction, zone] = m;
|
|
212
|
+
const year = Number(y);
|
|
213
|
+
const month = Number(mo);
|
|
214
|
+
const day = Number(d);
|
|
215
|
+
if (!dateExists(year, month, day)) return null;
|
|
216
|
+
if (h === void 0) return new Date(year, month - 1, day);
|
|
217
|
+
const hour = Number(h);
|
|
218
|
+
const minute = Number(mi);
|
|
219
|
+
const second = s === void 0 ? 0 : Number(s);
|
|
220
|
+
if (hour > 23 || minute > 59 || second > 59) return null;
|
|
221
|
+
const ms = fraction === void 0 ? 0 : Number(fraction.slice(0, 3).padEnd(3, "0"));
|
|
222
|
+
if (zone === void 0) return new Date(year, month - 1, day, hour, minute, second, ms);
|
|
223
|
+
let offset = 0;
|
|
224
|
+
if (zone !== "Z") {
|
|
225
|
+
const digits = zone.slice(1).replace(":", "");
|
|
226
|
+
const offsetHours = Number(digits.slice(0, 2));
|
|
227
|
+
const offsetMinutes = digits.length > 2 ? Number(digits.slice(2)) : 0;
|
|
228
|
+
if (offsetHours > 23 || offsetMinutes > 59) return null;
|
|
229
|
+
offset = (zone.startsWith("-") ? -1 : 1) * (offsetHours * 60 + offsetMinutes);
|
|
230
|
+
}
|
|
231
|
+
return new Date(Date.UTC(year, month - 1, day, hour, minute, second, ms) - offset * 6e4);
|
|
232
|
+
}
|
|
233
|
+
function localeOf(lang) {
|
|
234
|
+
return lang === "zh" ? "zh-CN" : "en-US";
|
|
235
|
+
}
|
|
236
|
+
function formatNumber(value, lang, decimals) {
|
|
237
|
+
const digits = decimals ?? (Number.isInteger(value) ? 0 : void 0);
|
|
238
|
+
return new Intl.NumberFormat(localeOf(lang), {
|
|
239
|
+
minimumFractionDigits: digits ?? 0,
|
|
240
|
+
maximumFractionDigits: digits ?? 2
|
|
241
|
+
}).format(value);
|
|
242
|
+
}
|
|
243
|
+
var TIGHT_UNIT = /^[%°′″]/;
|
|
244
|
+
function appendUnit(text, unit) {
|
|
245
|
+
if (unit === void 0 || unit === "") return text;
|
|
246
|
+
return TIGHT_UNIT.test(unit) ? `${text}${unit}` : `${text} ${unit}`;
|
|
247
|
+
}
|
|
248
|
+
function formatValue(value, unit, lang, decimals) {
|
|
249
|
+
return appendUnit(formatNumber(value, lang, decimals), unit);
|
|
250
|
+
}
|
|
251
|
+
function formatMetricValue(metric, lang) {
|
|
252
|
+
const number = formatNumber(metric.value, lang, metric.decimals);
|
|
253
|
+
return appendUnit(`${metric.prefix ?? ""}${number}`, metric.unit);
|
|
254
|
+
}
|
|
255
|
+
function resolveMetric(metric) {
|
|
256
|
+
const { value, max, warn, danger } = metric;
|
|
257
|
+
const kind = metric.kind ?? "reading";
|
|
258
|
+
const min = metric.min ?? 0;
|
|
259
|
+
const gauge = max === void 0 ? "none" : metric.gauge ?? (kind === "progress" ? "bar" : "ring");
|
|
260
|
+
const worse = metric.worse ?? (kind === "remaining" ? "low" : "high");
|
|
261
|
+
const resolved = { kind, gauge, worse };
|
|
262
|
+
if (max !== void 0 && max > min) {
|
|
263
|
+
resolved.share = Math.min(1, Math.max(0, (value - min) / (max - min)));
|
|
264
|
+
}
|
|
265
|
+
const past = (mark) => mark !== void 0 && (worse === "high" ? value >= mark : value <= mark);
|
|
266
|
+
if (kind === "progress" && max !== void 0 && value >= max) resolved.tone = "done";
|
|
267
|
+
else if (past(danger)) resolved.tone = "danger";
|
|
268
|
+
else if (past(warn)) resolved.tone = "attention";
|
|
269
|
+
return resolved;
|
|
270
|
+
}
|
|
271
|
+
function metricDetail(metric, lang) {
|
|
272
|
+
const { kind = "reading", max, prefix = "", unit, decimals } = metric;
|
|
273
|
+
if (kind === "reading" || max === void 0) return void 0;
|
|
274
|
+
const value = `${prefix}${formatNumber(metric.value, lang, decimals)}`;
|
|
275
|
+
const percent = unit === "%" && max === 100 && (metric.min ?? 0) === 0;
|
|
276
|
+
if (percent && kind === "progress") return void 0;
|
|
277
|
+
const whole = `${prefix}${formatNumber(max, lang, Number.isInteger(max) ? void 0 : decimals)}`;
|
|
278
|
+
const amount = appendUnit(percent ? value : `${value} / ${whole}`, unit);
|
|
279
|
+
if (kind === "progress") return amount;
|
|
280
|
+
return WORDS[lang][kind](amount);
|
|
281
|
+
}
|
|
282
|
+
function formatDelta(metric, lang) {
|
|
283
|
+
const { delta } = metric;
|
|
284
|
+
if (delta === void 0) return void 0;
|
|
285
|
+
const sign = delta > 0 ? "+" : delta < 0 ? "\u2212" : "";
|
|
286
|
+
const number = formatNumber(Math.abs(delta), lang, metric.decimals);
|
|
287
|
+
return appendUnit(`${sign}${metric.prefix ?? ""}${number}`, metric.unit);
|
|
288
|
+
}
|
|
289
|
+
function countdownParts(target, now) {
|
|
290
|
+
const left = Math.ceil((target.getTime() - now.getTime()) / 1e3);
|
|
291
|
+
if (!(left > 0)) return { days: 0, hours: 0, minutes: 0, seconds: 0, reached: true };
|
|
292
|
+
return {
|
|
293
|
+
days: Math.floor(left / 86400),
|
|
294
|
+
hours: Math.floor(left % 86400 / 3600),
|
|
295
|
+
minutes: Math.floor(left % 3600 / 60),
|
|
296
|
+
seconds: left % 60,
|
|
297
|
+
reached: false
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
function countdownText(parts, lang) {
|
|
301
|
+
if (parts.reached) return "";
|
|
302
|
+
const w = WORDS[lang];
|
|
303
|
+
const [first, second] = parts.days > 0 ? [w.days(parts.days), parts.hours > 0 ? w.hours(parts.hours) : ""] : parts.hours > 0 ? [w.hours(parts.hours), parts.minutes > 0 ? w.minutes(parts.minutes) : ""] : parts.minutes > 0 ? [w.minutes(parts.minutes), parts.seconds > 0 ? w.seconds(parts.seconds) : ""] : [w.seconds(parts.seconds), ""];
|
|
304
|
+
return second === "" ? first : `${first} ${second}`;
|
|
305
|
+
}
|
|
306
|
+
function conditionName(condition, lang) {
|
|
307
|
+
return CONDITION_NAMES[lang][condition];
|
|
308
|
+
}
|
|
309
|
+
function formatTemp(value, unit, withUnit, lang) {
|
|
310
|
+
return `${formatNumber(value, lang)}\xB0${withUnit ? unit ?? "C" : ""}`;
|
|
311
|
+
}
|
|
312
|
+
function zonedParts(date, timeZone) {
|
|
313
|
+
const parts = new Intl.DateTimeFormat("en-US", {
|
|
314
|
+
timeZone,
|
|
315
|
+
year: "numeric",
|
|
316
|
+
month: "2-digit",
|
|
317
|
+
day: "2-digit",
|
|
318
|
+
hour: "2-digit",
|
|
319
|
+
minute: "2-digit",
|
|
320
|
+
hourCycle: "h23"
|
|
321
|
+
}).formatToParts(date);
|
|
322
|
+
const part = (type) => parts.find((p) => p.type === type)?.value ?? "";
|
|
323
|
+
return {
|
|
324
|
+
year: part("year"),
|
|
325
|
+
month: part("month"),
|
|
326
|
+
day: part("day"),
|
|
327
|
+
hour: part("hour"),
|
|
328
|
+
minute: part("minute")
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function formatAsOf(asOf, lang, now, timeZone) {
|
|
332
|
+
const instant = parseA2uiInstant(asOf);
|
|
333
|
+
if (instant === null) return asOf;
|
|
334
|
+
const at = zonedParts(instant, timeZone);
|
|
335
|
+
const today = zonedParts(now, timeZone);
|
|
336
|
+
const time = `${at.hour}:${at.minute}`;
|
|
337
|
+
if (at.year !== today.year) return `${at.year}-${at.month}-${at.day} ${time}`;
|
|
338
|
+
if (at.month !== today.month || at.day !== today.day) return `${at.month}-${at.day} ${time}`;
|
|
339
|
+
return time;
|
|
340
|
+
}
|
|
341
|
+
function weekdayOf(date, lang, today) {
|
|
342
|
+
const day = date.slice(0, 10);
|
|
343
|
+
if (today !== void 0 && day === today.slice(0, 10)) return WORDS[lang].today;
|
|
344
|
+
const m = DATE.exec(day);
|
|
345
|
+
if (m === null) return date;
|
|
346
|
+
const utc = Date.UTC(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
|
|
347
|
+
return new Intl.DateTimeFormat(localeOf(lang), { weekday: "short", timeZone: "UTC" }).format(utc);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/fields.ts
|
|
351
|
+
var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
352
|
+
var join = (path, key) => path === "" ? key : `${path}.${key}`;
|
|
353
|
+
var length = (s) => [...s].length;
|
|
354
|
+
var present = (value) => value !== void 0 && value !== null;
|
|
355
|
+
var Issues = class {
|
|
356
|
+
list = [];
|
|
357
|
+
error(code, message, path, line) {
|
|
358
|
+
this.push("error", code, message, path, line);
|
|
359
|
+
}
|
|
360
|
+
warn(code, message, path) {
|
|
361
|
+
this.push("warning", code, message, path);
|
|
362
|
+
}
|
|
363
|
+
get hasError() {
|
|
364
|
+
return this.list.some((issue2) => issue2.level === "error");
|
|
365
|
+
}
|
|
366
|
+
push(level, code, message, path, line) {
|
|
367
|
+
const issue2 = { level, code, message };
|
|
368
|
+
if (path !== void 0) issue2.path = path;
|
|
369
|
+
if (line !== void 0) issue2.line = line;
|
|
370
|
+
this.list.push(issue2);
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
function readString(is, obj, key, path, rule) {
|
|
374
|
+
const value = obj[key];
|
|
375
|
+
const p = join(path, key);
|
|
376
|
+
if (value === void 0 || value === null) {
|
|
377
|
+
if (rule.required) is.error("missing_field", `\`${p}\` is required.`, p);
|
|
378
|
+
return void 0;
|
|
379
|
+
}
|
|
380
|
+
if (typeof value !== "string") {
|
|
381
|
+
is.error("wrong_type", `\`${p}\` must be a string.`, p);
|
|
382
|
+
return void 0;
|
|
383
|
+
}
|
|
384
|
+
if (value.trim() === "") {
|
|
385
|
+
if (rule.required) {
|
|
386
|
+
is.error("empty_string", `\`${p}\` is empty; write the text or drop the block.`, p);
|
|
387
|
+
}
|
|
388
|
+
return void 0;
|
|
389
|
+
}
|
|
390
|
+
const n = length(value);
|
|
391
|
+
if (n > rule.max) {
|
|
392
|
+
is.error(
|
|
393
|
+
"too_long",
|
|
394
|
+
`\`${p}\` is ${n} characters; the limit is ${rule.max}. Shorten it or move the detail into the prose.`,
|
|
395
|
+
p
|
|
396
|
+
);
|
|
397
|
+
}
|
|
398
|
+
if (rule.pattern && !rule.pattern.test(value)) {
|
|
399
|
+
is.error(
|
|
400
|
+
"invalid_format",
|
|
401
|
+
`\`${p}\` must match ${rule.pattern.source}${rule.patternHint ? ` (${rule.patternHint})` : ""}.`,
|
|
402
|
+
p
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
return value;
|
|
406
|
+
}
|
|
407
|
+
function readBoolean(is, obj, key, path) {
|
|
408
|
+
const value = obj[key];
|
|
409
|
+
if (value === void 0 || value === null) return void 0;
|
|
410
|
+
if (typeof value !== "boolean") {
|
|
411
|
+
is.error("wrong_type", `\`${join(path, key)}\` must be true or false.`, join(path, key));
|
|
412
|
+
return void 0;
|
|
413
|
+
}
|
|
414
|
+
return value;
|
|
415
|
+
}
|
|
416
|
+
function readNumber(is, obj, key, path, required = false) {
|
|
417
|
+
const value = obj[key];
|
|
418
|
+
const p = join(path, key);
|
|
419
|
+
if (value === void 0 || value === null) {
|
|
420
|
+
if (required) is.error("missing_field", `\`${p}\` is required: a number.`, p);
|
|
421
|
+
return void 0;
|
|
422
|
+
}
|
|
423
|
+
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
424
|
+
is.error("wrong_type", `\`${p}\` must be a number.`, p);
|
|
425
|
+
return void 0;
|
|
426
|
+
}
|
|
427
|
+
return value;
|
|
428
|
+
}
|
|
429
|
+
function readEnum(is, obj, key, path, values, required = true) {
|
|
430
|
+
const value = obj[key];
|
|
431
|
+
const p = join(path, key);
|
|
432
|
+
if (value === void 0 || value === null) {
|
|
433
|
+
if (required) {
|
|
434
|
+
is.error("missing_field", `\`${p}\` is required: one of ${values.join(", ")}.`, p);
|
|
435
|
+
}
|
|
436
|
+
return void 0;
|
|
437
|
+
}
|
|
438
|
+
if (typeof value !== "string" || !values.includes(value)) {
|
|
439
|
+
is.error("invalid_enum", `\`${p}\` must be one of ${values.join(", ")}.`, p);
|
|
440
|
+
return void 0;
|
|
441
|
+
}
|
|
442
|
+
return value;
|
|
443
|
+
}
|
|
444
|
+
function readArray(is, obj, key, path, rule) {
|
|
445
|
+
const value = obj[key];
|
|
446
|
+
const p = join(path, key);
|
|
447
|
+
if (value === void 0 || value === null) {
|
|
448
|
+
is.error("missing_field", `\`${p}\` is required.`, p);
|
|
449
|
+
return void 0;
|
|
450
|
+
}
|
|
451
|
+
if (!Array.isArray(value)) {
|
|
452
|
+
is.error("wrong_type", `\`${p}\` must be an array.`, p);
|
|
453
|
+
return void 0;
|
|
454
|
+
}
|
|
455
|
+
if (value.length < rule.min || value.length > rule.max) {
|
|
456
|
+
is.error(
|
|
457
|
+
"count_out_of_range",
|
|
458
|
+
`\`${p}\` has ${value.length} ${rule.what}; allowed ${rule.min}\u2013${rule.max}. ${value.length < rule.min ? "Add more, or answer in prose." : "Cut to the realistic ones, or split into two turns."}`,
|
|
459
|
+
p
|
|
460
|
+
);
|
|
461
|
+
} else if (rule.warn !== void 0 && rule.warnCode && value.length > rule.warn) {
|
|
462
|
+
is.warn(
|
|
463
|
+
rule.warnCode,
|
|
464
|
+
`\`${p}\` has ${value.length} ${rule.what}; more than ${rule.warn} is hard to take in at a glance.${rule.warnHint ? ` ${rule.warnHint}` : ""}`,
|
|
465
|
+
p
|
|
466
|
+
);
|
|
467
|
+
}
|
|
468
|
+
return value;
|
|
469
|
+
}
|
|
470
|
+
function readNumbers(is, obj, key, path, rule) {
|
|
471
|
+
const raw = readArray(is, obj, key, path, rule);
|
|
472
|
+
if (raw === void 0) return void 0;
|
|
473
|
+
const p = join(path, key);
|
|
474
|
+
return raw.map((entry, i) => {
|
|
475
|
+
const ep = `${p}[${i}]`;
|
|
476
|
+
if (entry === null && rule.nulls) return null;
|
|
477
|
+
if (typeof entry !== "number" || !Number.isFinite(entry)) {
|
|
478
|
+
is.error(
|
|
479
|
+
"wrong_type",
|
|
480
|
+
rule.nulls ? `\`${ep}\` must be a number, or null for a missing reading.` : `\`${ep}\` must be a number; leave out a reading you do not have.`,
|
|
481
|
+
ep
|
|
482
|
+
);
|
|
483
|
+
return Number.NaN;
|
|
484
|
+
}
|
|
485
|
+
const below = rule.atLeast !== void 0 && entry < rule.atLeast;
|
|
486
|
+
const above = rule.atMost !== void 0 && entry > rule.atMost;
|
|
487
|
+
if (below || above) {
|
|
488
|
+
is.error(
|
|
489
|
+
"range_invalid",
|
|
490
|
+
`\`${ep}\` is ${entry}; ${rule.rangeHint ?? "out of range"}. Fix the value.`,
|
|
491
|
+
ep
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
return entry;
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
function unknownFields(is, obj, known, path, what) {
|
|
498
|
+
for (const key of Object.keys(obj)) {
|
|
499
|
+
if (known.includes(key)) continue;
|
|
500
|
+
const p = join(path, key);
|
|
501
|
+
is.warn("unknown_field", `\`${p}\` is not a field of ${what}; it is ignored.`, p);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
var ID_HINT = "lower-case letters, digits, _ or -, starting with a letter, at most 32";
|
|
505
|
+
function readInstant(is, obj, key, path, required = false) {
|
|
506
|
+
const value = readString(is, obj, key, path, { required, max: Number.POSITIVE_INFINITY });
|
|
507
|
+
if (value === void 0 || parseA2uiInstant(value) !== null) return value;
|
|
508
|
+
const p = join(path, key);
|
|
509
|
+
is.error(
|
|
510
|
+
"invalid_datetime",
|
|
511
|
+
`\`${p}\` is "${value}", not a date-time; write ISO 8601 such as 2026-10-04T14:05+08:00 (Z or an offset; without one it is the viewer's local time), or 2026-10-04 for local midnight.`,
|
|
512
|
+
p
|
|
513
|
+
);
|
|
514
|
+
return void 0;
|
|
515
|
+
}
|
|
516
|
+
function readPercent(is, obj, key, path) {
|
|
517
|
+
const value = readNumber(is, obj, key, path);
|
|
518
|
+
if (value === void 0 || value >= 0 && value <= 100) return value;
|
|
519
|
+
const p = join(path, key);
|
|
520
|
+
is.error("range_invalid", `\`${p}\` is ${value}; a percentage is 0\u2013100. Fix the value.`, p);
|
|
521
|
+
return void 0;
|
|
522
|
+
}
|
|
523
|
+
function checkHighLow(is, high, low, path) {
|
|
524
|
+
if (high === void 0 || low === void 0 || high >= low) return;
|
|
525
|
+
const p = join(path, "high");
|
|
526
|
+
is.error(
|
|
527
|
+
"range_invalid",
|
|
528
|
+
`\`${p}\` ${high} is below \`${join(path, "low")}\` ${low}; swap them or fix the values.`,
|
|
529
|
+
p
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
// src/metric.ts
|
|
534
|
+
var METRIC_KINDS = ["reading", "used", "remaining", "progress"];
|
|
535
|
+
var GAUGES = ["ring", "bar", "none"];
|
|
536
|
+
var DIRECTIONS = ["high", "low"];
|
|
537
|
+
var METRIC_FIELDS = [
|
|
538
|
+
"label",
|
|
539
|
+
"value",
|
|
540
|
+
"max",
|
|
541
|
+
"min",
|
|
542
|
+
"kind",
|
|
543
|
+
"gauge",
|
|
544
|
+
"unit",
|
|
545
|
+
"prefix",
|
|
546
|
+
"decimals",
|
|
547
|
+
"worse",
|
|
548
|
+
"warn",
|
|
549
|
+
"danger",
|
|
550
|
+
"detail",
|
|
551
|
+
"delta",
|
|
552
|
+
"deltaLabel",
|
|
553
|
+
"history"
|
|
554
|
+
];
|
|
555
|
+
function readHistory(is, item, mp) {
|
|
556
|
+
const raw = readArray(is, item, "history", mp, { ...A2UI_LIMITS.history, what: "points" });
|
|
557
|
+
if (raw === void 0) return void 0;
|
|
558
|
+
const points = [];
|
|
559
|
+
raw.forEach((point, i) => {
|
|
560
|
+
if (typeof point === "number" && Number.isFinite(point)) {
|
|
561
|
+
points.push(point);
|
|
562
|
+
return;
|
|
563
|
+
}
|
|
564
|
+
const pp = `${mp}.history[${i}]`;
|
|
565
|
+
is.error("wrong_type", `\`${pp}\` must be a number; leave out a reading you do not have.`, pp);
|
|
566
|
+
});
|
|
567
|
+
return points;
|
|
568
|
+
}
|
|
569
|
+
function parseMetric(is, item, mp) {
|
|
570
|
+
if (!isObj(item)) {
|
|
571
|
+
is.error("wrong_type", `\`${mp}\` must be an object with a \`label\` and a \`value\`.`, mp);
|
|
572
|
+
return void 0;
|
|
573
|
+
}
|
|
574
|
+
const label = readString(is, item, "label", mp, {
|
|
575
|
+
required: true,
|
|
576
|
+
max: A2UI_LIMITS.metricLabel
|
|
577
|
+
});
|
|
578
|
+
const value = readNumber(is, item, "value", mp, true);
|
|
579
|
+
const max = readNumber(is, item, "max", mp);
|
|
580
|
+
const min = readNumber(is, item, "min", mp);
|
|
581
|
+
const kind = readEnum(is, item, "kind", mp, METRIC_KINDS, false);
|
|
582
|
+
const gauge = readEnum(is, item, "gauge", mp, GAUGES, false);
|
|
583
|
+
const unit = readString(is, item, "unit", mp, { max: A2UI_LIMITS.unit });
|
|
584
|
+
const prefix = readString(is, item, "prefix", mp, { max: A2UI_LIMITS.prefix });
|
|
585
|
+
const decimals = readNumber(is, item, "decimals", mp);
|
|
586
|
+
const worse = readEnum(is, item, "worse", mp, DIRECTIONS, false);
|
|
587
|
+
const warn = readNumber(is, item, "warn", mp);
|
|
588
|
+
const danger = readNumber(is, item, "danger", mp);
|
|
589
|
+
const detail = readString(is, item, "detail", mp, { max: A2UI_LIMITS.metricDetail });
|
|
590
|
+
const delta = readNumber(is, item, "delta", mp);
|
|
591
|
+
const deltaLabel = readString(is, item, "deltaLabel", mp, { max: A2UI_LIMITS.deltaLabel });
|
|
592
|
+
const history = present(item.history) ? readHistory(is, item, mp) : void 0;
|
|
593
|
+
unknownFields(is, item, METRIC_FIELDS, mp, "a metric");
|
|
594
|
+
if (!present(item.max)) {
|
|
595
|
+
if (kind === "used" || kind === "remaining" || kind === "progress") {
|
|
596
|
+
is.error(
|
|
597
|
+
"missing_field",
|
|
598
|
+
`\`${mp}.max\` is required for kind "${kind}"; add the whole the value is a part of.`,
|
|
599
|
+
`${mp}.max`
|
|
600
|
+
);
|
|
601
|
+
} else if (gauge === "ring" || gauge === "bar") {
|
|
602
|
+
is.error(
|
|
603
|
+
"missing_field",
|
|
604
|
+
`\`${mp}.max\` is required for a ${gauge} gauge; add it, or set gauge "none".`,
|
|
605
|
+
`${mp}.max`
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
if (max !== void 0 && (min ?? 0) >= max) {
|
|
610
|
+
is.error(
|
|
611
|
+
"range_invalid",
|
|
612
|
+
`\`${mp}\`: max ${max} is not above min ${min ?? "0 (the default)"}; fix the range.`,
|
|
613
|
+
min !== void 0 ? `${mp}.min` : `${mp}.max`
|
|
614
|
+
);
|
|
615
|
+
}
|
|
616
|
+
if (decimals !== void 0 && !(Number.isInteger(decimals) && decimals >= 0 && decimals <= 3)) {
|
|
617
|
+
is.error(
|
|
618
|
+
"range_invalid",
|
|
619
|
+
`\`${mp}.decimals\` is ${decimals}; use a whole number from 0 to 3.`,
|
|
620
|
+
`${mp}.decimals`
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
if (warn !== void 0 && danger !== void 0) {
|
|
624
|
+
const direction = worse ?? (kind === "remaining" ? "low" : "high");
|
|
625
|
+
const why = worse === void 0 ? ` (the default for kind "${kind ?? "reading"}")` : "";
|
|
626
|
+
if (direction === "high" && warn > danger) {
|
|
627
|
+
is.error(
|
|
628
|
+
"range_invalid",
|
|
629
|
+
`\`${mp}\`: warn ${warn} is above danger ${danger}, but worse is "high"${why} \u2014 a higher value is worse, so warn must be at most danger. Swap them, or set worse to "low".`,
|
|
630
|
+
`${mp}.warn`
|
|
631
|
+
);
|
|
632
|
+
} else if (direction === "low" && warn < danger) {
|
|
633
|
+
is.error(
|
|
634
|
+
"range_invalid",
|
|
635
|
+
`\`${mp}\`: warn ${warn} is below danger ${danger}, but worse is "low"${why} \u2014 a lower value is worse, so warn must be at least danger. Swap them, or set worse to "high".`,
|
|
636
|
+
`${mp}.warn`
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
if (deltaLabel !== void 0 && !present(item.delta)) {
|
|
641
|
+
is.warn(
|
|
642
|
+
"ignored_field",
|
|
643
|
+
`\`${mp}.deltaLabel\` without \`delta\` has no effect; add the change or remove the label.`,
|
|
644
|
+
`${mp}.deltaLabel`
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
if (label === void 0 || value === void 0) return void 0;
|
|
648
|
+
const metric = { label, value };
|
|
649
|
+
if (max !== void 0) metric.max = max;
|
|
650
|
+
if (min !== void 0) metric.min = min;
|
|
651
|
+
if (kind !== void 0) metric.kind = kind;
|
|
652
|
+
if (gauge !== void 0) metric.gauge = gauge;
|
|
653
|
+
if (unit !== void 0) metric.unit = unit;
|
|
654
|
+
if (prefix !== void 0) metric.prefix = prefix;
|
|
655
|
+
if (decimals !== void 0) metric.decimals = decimals;
|
|
656
|
+
if (worse !== void 0) metric.worse = worse;
|
|
657
|
+
if (warn !== void 0) metric.warn = warn;
|
|
658
|
+
if (danger !== void 0) metric.danger = danger;
|
|
659
|
+
if (detail !== void 0) metric.detail = detail;
|
|
660
|
+
if (delta !== void 0) metric.delta = delta;
|
|
661
|
+
if (deltaLabel !== void 0) metric.deltaLabel = deltaLabel;
|
|
662
|
+
if (history !== void 0) metric.history = history;
|
|
663
|
+
return metric;
|
|
664
|
+
}
|
|
665
|
+
var LINE = {
|
|
666
|
+
zh: { colon: "\uFF1A", tone: { attention: "\u6CE8\u610F", danger: "\u8B66\u544A", done: "\u5DF2\u5B8C\u6210" } },
|
|
667
|
+
en: { colon: ": ", tone: { attention: "warning", danger: "critical", done: "done" } }
|
|
668
|
+
};
|
|
669
|
+
function metricLine(item, lang) {
|
|
670
|
+
const t = LINE[lang];
|
|
671
|
+
let line = `- ${item.label}${t.colon}${formatMetricValue(item, lang)}`;
|
|
672
|
+
const detail = item.detail ?? metricDetail(item, lang);
|
|
673
|
+
if (detail !== void 0) line += ` \u2014 ${detail}`;
|
|
674
|
+
const delta = formatDelta(item, lang);
|
|
675
|
+
if (delta !== void 0) {
|
|
676
|
+
const change = item.deltaLabel ? `${delta} ${item.deltaLabel}` : delta;
|
|
677
|
+
line += lang === "zh" ? `\uFF08${change}\uFF09` : ` (${change})`;
|
|
678
|
+
}
|
|
679
|
+
const { tone } = resolveMetric(item);
|
|
680
|
+
if (tone !== void 0) line += ` \u2014 ${t.tone[tone]}`;
|
|
681
|
+
return line;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
// src/chart.ts
|
|
685
|
+
var A2UI_CHART_KINDS = [
|
|
686
|
+
"line",
|
|
687
|
+
"area",
|
|
688
|
+
"bar",
|
|
689
|
+
"pie",
|
|
690
|
+
"scatter",
|
|
691
|
+
"histogram",
|
|
692
|
+
"gauge"
|
|
693
|
+
];
|
|
694
|
+
var CHART_FIELDS = [
|
|
695
|
+
"type",
|
|
696
|
+
"kind",
|
|
697
|
+
"title",
|
|
698
|
+
"categories",
|
|
699
|
+
"edges",
|
|
700
|
+
"series",
|
|
701
|
+
"xLabel",
|
|
702
|
+
"yLabel",
|
|
703
|
+
"xUnit",
|
|
704
|
+
"yUnit",
|
|
705
|
+
"stacked",
|
|
706
|
+
"horizontal",
|
|
707
|
+
"donut",
|
|
708
|
+
"min",
|
|
709
|
+
"max",
|
|
710
|
+
"decimals",
|
|
711
|
+
"item",
|
|
712
|
+
"asOf"
|
|
713
|
+
];
|
|
714
|
+
var SERIES_FIELDS = ["label", "values", "points"];
|
|
715
|
+
var COMMON = ["type", "kind", "title", "asOf"];
|
|
716
|
+
var CARTESIAN = [
|
|
717
|
+
...COMMON,
|
|
718
|
+
"categories",
|
|
719
|
+
"series",
|
|
720
|
+
"xLabel",
|
|
721
|
+
"yLabel",
|
|
722
|
+
"yUnit",
|
|
723
|
+
"min",
|
|
724
|
+
"max",
|
|
725
|
+
"decimals"
|
|
726
|
+
];
|
|
727
|
+
var NUMERIC_X = [
|
|
728
|
+
...COMMON,
|
|
729
|
+
"series",
|
|
730
|
+
"xLabel",
|
|
731
|
+
"yLabel",
|
|
732
|
+
"xUnit",
|
|
733
|
+
"yUnit",
|
|
734
|
+
"min",
|
|
735
|
+
"max",
|
|
736
|
+
"decimals"
|
|
737
|
+
];
|
|
738
|
+
var KIND_FIELDS = {
|
|
739
|
+
line: CARTESIAN,
|
|
740
|
+
area: [...CARTESIAN, "stacked"],
|
|
741
|
+
bar: [...CARTESIAN, "stacked", "horizontal"],
|
|
742
|
+
pie: [...COMMON, "categories", "series", "yUnit", "decimals", "donut"],
|
|
743
|
+
scatter: NUMERIC_X,
|
|
744
|
+
histogram: [...NUMERIC_X, "edges"],
|
|
745
|
+
gauge: [...COMMON, "item"]
|
|
746
|
+
};
|
|
747
|
+
function ignoredReason(key, kind) {
|
|
748
|
+
if (kind === "gauge") {
|
|
749
|
+
return "a gauge shows its `item`; put the reading, its range and its unit there";
|
|
750
|
+
}
|
|
751
|
+
if (kind === "pie" && key !== "stacked" && key !== "horizontal" && key !== "item" && key !== "edges") {
|
|
752
|
+
return "a pie chart has no axes";
|
|
753
|
+
}
|
|
754
|
+
switch (key) {
|
|
755
|
+
case "stacked":
|
|
756
|
+
return "only bar and area charts stack";
|
|
757
|
+
case "horizontal":
|
|
758
|
+
return "only a bar chart lies on its side";
|
|
759
|
+
case "donut":
|
|
760
|
+
return "only a pie chart has a hole";
|
|
761
|
+
case "item":
|
|
762
|
+
return "only a gauge reads an item";
|
|
763
|
+
case "edges":
|
|
764
|
+
return "only a histogram has bin edges";
|
|
765
|
+
case "categories":
|
|
766
|
+
return kind === "scatter" ? "a scatter chart places `series[].points`" : "a histogram's bins come from `edges`";
|
|
767
|
+
default:
|
|
768
|
+
return "its x axis has labels, not numbers";
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
var WORDS2 = {
|
|
772
|
+
en: {
|
|
773
|
+
colon: ": ",
|
|
774
|
+
none: "no reading",
|
|
775
|
+
points: (n) => `${n} ${n === 1 ? "point" : "points"}`,
|
|
776
|
+
extent: (min, max, count2) => ` (min ${min}, max ${max}, ${count2})`,
|
|
777
|
+
share: (pct) => ` (${pct})`
|
|
778
|
+
},
|
|
779
|
+
zh: {
|
|
780
|
+
colon: "\uFF1A",
|
|
781
|
+
none: "\u65E0\u8BFB\u6570",
|
|
782
|
+
points: (n) => `${n} \u4E2A\u70B9`,
|
|
783
|
+
extent: (min, max, count2) => `\uFF08\u6700\u4F4E ${min}\uFF0C\u6700\u9AD8 ${max}\uFF0C${count2}\uFF09`,
|
|
784
|
+
share: (pct) => `\uFF08${pct}\uFF09`
|
|
785
|
+
}
|
|
786
|
+
};
|
|
787
|
+
var MAX_ROWS = 20;
|
|
788
|
+
function readDecimals(is, obj) {
|
|
789
|
+
const decimals = readNumber(is, obj, "decimals", "");
|
|
790
|
+
if (decimals === void 0 || Number.isInteger(decimals) && decimals >= 0 && decimals <= 3) {
|
|
791
|
+
return decimals;
|
|
792
|
+
}
|
|
793
|
+
is.error(
|
|
794
|
+
"range_invalid",
|
|
795
|
+
`\`decimals\` is ${decimals}; use a whole number from 0 to 3.`,
|
|
796
|
+
"decimals"
|
|
797
|
+
);
|
|
798
|
+
return void 0;
|
|
799
|
+
}
|
|
800
|
+
function readScale(is, obj, reads, spec) {
|
|
801
|
+
for (const key of ["xLabel", "yLabel"]) {
|
|
802
|
+
if (!reads.includes(key)) continue;
|
|
803
|
+
const value = readString(is, obj, key, "", { max: A2UI_LIMITS.axisLabel });
|
|
804
|
+
if (value !== void 0) spec[key] = value;
|
|
805
|
+
}
|
|
806
|
+
for (const key of ["xUnit", "yUnit"]) {
|
|
807
|
+
if (!reads.includes(key)) continue;
|
|
808
|
+
const value = readString(is, obj, key, "", { max: A2UI_LIMITS.unit });
|
|
809
|
+
if (value !== void 0) spec[key] = value;
|
|
810
|
+
}
|
|
811
|
+
if (reads.includes("min")) {
|
|
812
|
+
const min = readNumber(is, obj, "min", "");
|
|
813
|
+
const max = readNumber(is, obj, "max", "");
|
|
814
|
+
if (min !== void 0 && max !== void 0 && min >= max) {
|
|
815
|
+
is.error(
|
|
816
|
+
"range_invalid",
|
|
817
|
+
`\`min\` ${min} is not below \`max\` ${max}; fix the range, or leave both out to fit the data.`,
|
|
818
|
+
"min"
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
if (min !== void 0) spec.min = min;
|
|
822
|
+
if (max !== void 0) spec.max = max;
|
|
823
|
+
}
|
|
824
|
+
if (reads.includes("decimals")) {
|
|
825
|
+
const decimals = readDecimals(is, obj);
|
|
826
|
+
if (decimals !== void 0) spec.decimals = decimals;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
function readCategories(is, obj, rule, unique) {
|
|
830
|
+
const raw = readArray(is, obj, "categories", "", rule);
|
|
831
|
+
if (raw === void 0) return void 0;
|
|
832
|
+
const seen = /* @__PURE__ */ new Map();
|
|
833
|
+
return raw.map((entry, i) => {
|
|
834
|
+
const cp = `categories[${i}]`;
|
|
835
|
+
if (typeof entry !== "string") {
|
|
836
|
+
is.error("wrong_type", `\`${cp}\` must be a string.`, cp);
|
|
837
|
+
return "";
|
|
838
|
+
}
|
|
839
|
+
if (entry.trim() === "") {
|
|
840
|
+
is.error("empty_string", `\`${cp}\` is empty; name every category.`, cp);
|
|
841
|
+
return entry;
|
|
842
|
+
}
|
|
843
|
+
const n = length(entry);
|
|
844
|
+
if (n > A2UI_LIMITS.categoryLabel) {
|
|
845
|
+
is.error(
|
|
846
|
+
"too_long",
|
|
847
|
+
`\`${cp}\` is ${n} characters; the limit is ${A2UI_LIMITS.categoryLabel}. Shorten it.`,
|
|
848
|
+
cp
|
|
849
|
+
);
|
|
850
|
+
}
|
|
851
|
+
const first = seen.get(entry);
|
|
852
|
+
if (unique && first !== void 0) {
|
|
853
|
+
is.error(
|
|
854
|
+
"duplicate_label",
|
|
855
|
+
`\`${cp}\` repeats slice ${first + 1}'s name "${entry}"; name each slice once.`,
|
|
856
|
+
cp
|
|
857
|
+
);
|
|
858
|
+
} else {
|
|
859
|
+
seen.set(entry, i);
|
|
860
|
+
}
|
|
861
|
+
return entry;
|
|
862
|
+
});
|
|
863
|
+
}
|
|
864
|
+
function readSeriesArray(is, obj, kind) {
|
|
865
|
+
if (kind === "pie" || kind === "histogram") {
|
|
866
|
+
if (Array.isArray(obj.series) && obj.series.length > 1) {
|
|
867
|
+
is.error(
|
|
868
|
+
"count_out_of_range",
|
|
869
|
+
`\`series\` has ${obj.series.length} series; a ${kind} chart takes exactly one. ${kind === "pie" ? "Compare several with a bar chart." : "Draw one histogram per series."}`,
|
|
870
|
+
"series"
|
|
871
|
+
);
|
|
872
|
+
return void 0;
|
|
873
|
+
}
|
|
874
|
+
return readArray(is, obj, "series", "", { min: 1, max: 1, what: "series" });
|
|
875
|
+
}
|
|
876
|
+
return readArray(is, obj, "series", "", {
|
|
877
|
+
...A2UI_LIMITS.chartSeries,
|
|
878
|
+
warnCode: "chart_many_series",
|
|
879
|
+
warnHint: "Keep the series that matter, or split the chart in two.",
|
|
880
|
+
what: "series"
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
function readPoints(is, item, sp) {
|
|
884
|
+
const raw = readArray(is, item, "points", sp, {
|
|
885
|
+
...A2UI_LIMITS.scatterPoints,
|
|
886
|
+
warnCode: "scatter_many_points",
|
|
887
|
+
warnHint: "Sample the points, or draw the trend as a line chart.",
|
|
888
|
+
what: "points"
|
|
889
|
+
});
|
|
890
|
+
if (raw === void 0) return void 0;
|
|
891
|
+
const points = [];
|
|
892
|
+
raw.forEach((point, i) => {
|
|
893
|
+
const [x, y] = Array.isArray(point) ? point : [];
|
|
894
|
+
const finite = (v) => typeof v === "number" && Number.isFinite(v);
|
|
895
|
+
if (Array.isArray(point) && point.length === 2 && finite(x) && finite(y)) {
|
|
896
|
+
points.push([x, y]);
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
const pp = `${sp}.points[${i}]`;
|
|
900
|
+
is.error("wrong_type", `\`${pp}\` must be [x, y]: two numbers.`, pp);
|
|
901
|
+
});
|
|
902
|
+
return points;
|
|
903
|
+
}
|
|
904
|
+
function readValues(is, item, sp, rule) {
|
|
905
|
+
const { kind, expected } = rule;
|
|
906
|
+
const count2 = { min: 0, max: Number.POSITIVE_INFINITY, what: "values" };
|
|
907
|
+
let values;
|
|
908
|
+
if (kind === "pie" || kind === "histogram") {
|
|
909
|
+
values = readNumbers(is, item, "values", sp, {
|
|
910
|
+
...count2,
|
|
911
|
+
atLeast: 0,
|
|
912
|
+
rangeHint: kind === "pie" ? "a slice is at least 0" : "a count is at least 0"
|
|
913
|
+
});
|
|
914
|
+
} else if (rule.stacked) {
|
|
915
|
+
values = readNumbers(is, item, "values", sp, {
|
|
916
|
+
...count2,
|
|
917
|
+
nulls: true,
|
|
918
|
+
atLeast: 0,
|
|
919
|
+
rangeHint: "a stacked chart adds its series up, so every value is at least 0 \u2014 remove `stacked` to show a negative one"
|
|
920
|
+
});
|
|
921
|
+
} else {
|
|
922
|
+
values = readNumbers(is, item, "values", sp, { ...count2, nulls: true });
|
|
923
|
+
}
|
|
924
|
+
if (values === void 0) return void 0;
|
|
925
|
+
const vp = `${sp}.values`;
|
|
926
|
+
if (expected !== void 0 && values.length !== expected) {
|
|
927
|
+
is.error(
|
|
928
|
+
"length_mismatch",
|
|
929
|
+
kind === "histogram" ? `\`${vp}\` has ${values.length} counts for ${expected} bins (${expected + 1} edges); give one count per bin.` : `\`${vp}\` has ${values.length} values for ${expected} categories; give one value per category${kind === "pie" ? "" : " (null where there is no reading)"}.`,
|
|
930
|
+
vp
|
|
931
|
+
);
|
|
932
|
+
}
|
|
933
|
+
if (kind === "pie" && values.length > 0 && values.every((v) => v === 0)) {
|
|
934
|
+
is.error(
|
|
935
|
+
"range_invalid",
|
|
936
|
+
`\`${vp}\` is all 0; a pie needs at least one slice above 0 \u2014 say it in prose instead.`,
|
|
937
|
+
vp
|
|
938
|
+
);
|
|
939
|
+
}
|
|
940
|
+
return values;
|
|
941
|
+
}
|
|
942
|
+
function readSeries(is, obj, rule) {
|
|
943
|
+
const raw = readSeriesArray(is, obj, rule.kind);
|
|
944
|
+
if (raw === void 0) return void 0;
|
|
945
|
+
const scatter = rule.kind === "scatter";
|
|
946
|
+
const list = [];
|
|
947
|
+
const labels = /* @__PURE__ */ new Map();
|
|
948
|
+
raw.forEach((item, i) => {
|
|
949
|
+
const sp = `series[${i}]`;
|
|
950
|
+
if (!isObj(item)) {
|
|
951
|
+
is.error(
|
|
952
|
+
"wrong_type",
|
|
953
|
+
`\`${sp}\` must be an object with a \`label\` and \`${scatter ? "points" : "values"}\`.`,
|
|
954
|
+
sp
|
|
955
|
+
);
|
|
956
|
+
return;
|
|
957
|
+
}
|
|
958
|
+
const label = readString(is, item, "label", sp, {
|
|
959
|
+
required: true,
|
|
960
|
+
max: A2UI_LIMITS.seriesLabel
|
|
961
|
+
});
|
|
962
|
+
const other = scatter ? "values" : "points";
|
|
963
|
+
if (present(item[other])) {
|
|
964
|
+
is.warn(
|
|
965
|
+
"ignored_field",
|
|
966
|
+
`\`${sp}.${other}\` has no effect on a ${rule.kind} chart; ${scatter ? "give each point as [x, y] in `points`" : "give one value per category in `values`"}.`,
|
|
967
|
+
`${sp}.${other}`
|
|
968
|
+
);
|
|
969
|
+
}
|
|
970
|
+
unknownFields(is, item, SERIES_FIELDS, sp, "a series");
|
|
971
|
+
const points = scatter ? readPoints(is, item, sp) : void 0;
|
|
972
|
+
const values = scatter ? void 0 : readValues(is, item, sp, rule);
|
|
973
|
+
if (label === void 0) return;
|
|
974
|
+
const seen = labels.get(label);
|
|
975
|
+
if (seen !== void 0) {
|
|
976
|
+
is.error(
|
|
977
|
+
"duplicate_label",
|
|
978
|
+
`\`${sp}.label\` repeats series ${seen + 1}'s label "${label}"; labels must be unique.`,
|
|
979
|
+
`${sp}.label`
|
|
980
|
+
);
|
|
981
|
+
} else {
|
|
982
|
+
labels.set(label, i);
|
|
983
|
+
}
|
|
984
|
+
const series = { label };
|
|
985
|
+
if (values !== void 0) series.values = values;
|
|
986
|
+
if (points !== void 0) series.points = points;
|
|
987
|
+
list.push(series);
|
|
988
|
+
});
|
|
989
|
+
return list;
|
|
990
|
+
}
|
|
991
|
+
function readEdges(is, obj) {
|
|
992
|
+
const edges = readNumbers(is, obj, "edges", "", { ...A2UI_LIMITS.edges, what: "edges" });
|
|
993
|
+
if (edges === void 0 || edges.some(Number.isNaN)) return edges;
|
|
994
|
+
const i = edges.findIndex((edge, k) => k > 0 && !(edge > (edges[k - 1] ?? edge)));
|
|
995
|
+
if (i > 0) {
|
|
996
|
+
is.error(
|
|
997
|
+
"range_invalid",
|
|
998
|
+
`\`edges[${i}]\` ${edges[i]} is not above \`edges[${i - 1}]\` ${edges[i - 1]}; bin edges go strictly up, lowest first.`,
|
|
999
|
+
`edges[${i}]`
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
return edges;
|
|
1003
|
+
}
|
|
1004
|
+
function parseChart(is, obj) {
|
|
1005
|
+
const kind = readEnum(is, obj, "kind", "", A2UI_CHART_KINDS);
|
|
1006
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.chartTitle });
|
|
1007
|
+
const asOf = readInstant(is, obj, "asOf", "");
|
|
1008
|
+
unknownFields(is, obj, CHART_FIELDS, "", "a chart");
|
|
1009
|
+
if (kind === void 0) return void 0;
|
|
1010
|
+
const reads = KIND_FIELDS[kind];
|
|
1011
|
+
for (const key of CHART_FIELDS) {
|
|
1012
|
+
if (!present(obj[key]) || reads.includes(key)) continue;
|
|
1013
|
+
is.warn(
|
|
1014
|
+
"ignored_field",
|
|
1015
|
+
`\`${key}\` has no effect on a ${kind} chart (${ignoredReason(key, kind)}); remove it.`,
|
|
1016
|
+
key
|
|
1017
|
+
);
|
|
1018
|
+
}
|
|
1019
|
+
const spec = { type: "chart", kind };
|
|
1020
|
+
if (title !== void 0) spec.title = title;
|
|
1021
|
+
if (asOf !== void 0) spec.asOf = asOf;
|
|
1022
|
+
if (kind === "gauge") {
|
|
1023
|
+
if (!present(obj.item)) {
|
|
1024
|
+
is.error(
|
|
1025
|
+
"missing_field",
|
|
1026
|
+
'`item` is required for a gauge chart: one metrics item, such as {"label": "CPU", "value": 37, "max": 100, "unit": "%"}.',
|
|
1027
|
+
"item"
|
|
1028
|
+
);
|
|
1029
|
+
return void 0;
|
|
1030
|
+
}
|
|
1031
|
+
const item = parseMetric(is, obj.item, "item");
|
|
1032
|
+
if (item === void 0) return void 0;
|
|
1033
|
+
spec.item = item;
|
|
1034
|
+
return spec;
|
|
1035
|
+
}
|
|
1036
|
+
const stacked = reads.includes("stacked") ? readBoolean(is, obj, "stacked", "") : void 0;
|
|
1037
|
+
const horizontal = reads.includes("horizontal") ? readBoolean(is, obj, "horizontal", "") : void 0;
|
|
1038
|
+
const donut = reads.includes("donut") ? readBoolean(is, obj, "donut", "") : void 0;
|
|
1039
|
+
let categories;
|
|
1040
|
+
let edges;
|
|
1041
|
+
let expected;
|
|
1042
|
+
if (kind === "histogram") {
|
|
1043
|
+
edges = readEdges(is, obj);
|
|
1044
|
+
if (edges !== void 0) expected = edges.length - 1;
|
|
1045
|
+
} else if (kind !== "scatter") {
|
|
1046
|
+
categories = kind === "pie" ? readCategories(
|
|
1047
|
+
is,
|
|
1048
|
+
obj,
|
|
1049
|
+
{
|
|
1050
|
+
...A2UI_LIMITS.pieSlices,
|
|
1051
|
+
warnCode: "pie_many_slices",
|
|
1052
|
+
warnHint: 'Merge the small slices into one "Other" slice.',
|
|
1053
|
+
what: "slices"
|
|
1054
|
+
},
|
|
1055
|
+
true
|
|
1056
|
+
) : readCategories(
|
|
1057
|
+
is,
|
|
1058
|
+
obj,
|
|
1059
|
+
{
|
|
1060
|
+
...A2UI_LIMITS.chartCategories,
|
|
1061
|
+
warnCode: "chart_many_categories",
|
|
1062
|
+
warnHint: "Group them (weeks instead of days), or keep the recent ones.",
|
|
1063
|
+
what: "categories"
|
|
1064
|
+
},
|
|
1065
|
+
false
|
|
1066
|
+
);
|
|
1067
|
+
expected = categories?.length;
|
|
1068
|
+
}
|
|
1069
|
+
const series = readSeries(is, obj, { kind, expected, stacked: stacked === true });
|
|
1070
|
+
readScale(is, obj, reads, spec);
|
|
1071
|
+
if (series === void 0) return void 0;
|
|
1072
|
+
if (kind === "histogram" ? edges === void 0 : kind !== "scatter" && categories === void 0) {
|
|
1073
|
+
return void 0;
|
|
1074
|
+
}
|
|
1075
|
+
if (categories !== void 0) spec.categories = categories;
|
|
1076
|
+
if (edges !== void 0) spec.edges = edges;
|
|
1077
|
+
spec.series = series;
|
|
1078
|
+
if (stacked !== void 0) spec.stacked = stacked;
|
|
1079
|
+
if (horizontal !== void 0) spec.horizontal = horizontal;
|
|
1080
|
+
if (donut !== void 0) spec.donut = donut;
|
|
1081
|
+
return spec;
|
|
1082
|
+
}
|
|
1083
|
+
function seriesExtent(values) {
|
|
1084
|
+
let extent;
|
|
1085
|
+
for (const value of values) {
|
|
1086
|
+
if (value === null) continue;
|
|
1087
|
+
if (extent === void 0) {
|
|
1088
|
+
extent = { min: value, max: value, first: value, last: value, count: 1 };
|
|
1089
|
+
continue;
|
|
1090
|
+
}
|
|
1091
|
+
extent.min = Math.min(extent.min, value);
|
|
1092
|
+
extent.max = Math.max(extent.max, value);
|
|
1093
|
+
extent.last = value;
|
|
1094
|
+
extent.count += 1;
|
|
1095
|
+
}
|
|
1096
|
+
return extent;
|
|
1097
|
+
}
|
|
1098
|
+
function span(min, max, unit, lang, decimals) {
|
|
1099
|
+
if (min === max) return formatValue(min, unit, lang, decimals);
|
|
1100
|
+
return `${formatNumber(min, lang, decimals)}\u2013${formatValue(max, unit, lang, decimals)}`;
|
|
1101
|
+
}
|
|
1102
|
+
function rowLines(spec, rows, lang) {
|
|
1103
|
+
const t = WORDS2[lang];
|
|
1104
|
+
const series = spec.series ?? [];
|
|
1105
|
+
return rows.map((row, r) => {
|
|
1106
|
+
const cells = series.map((s) => {
|
|
1107
|
+
const value = s.values?.[r];
|
|
1108
|
+
const text = value === null || value === void 0 ? t.none : formatValue(value, spec.yUnit, lang, spec.decimals);
|
|
1109
|
+
return series.length === 1 ? text : `${s.label} ${text}`;
|
|
1110
|
+
});
|
|
1111
|
+
return `- ${row}${t.colon}${cells.join(" \xB7 ")}`;
|
|
1112
|
+
});
|
|
1113
|
+
}
|
|
1114
|
+
function summaryLines(spec, lang) {
|
|
1115
|
+
const t = WORDS2[lang];
|
|
1116
|
+
const value = (v, unit) => formatValue(v, unit, lang, spec.decimals);
|
|
1117
|
+
return (spec.series ?? []).map((s) => {
|
|
1118
|
+
const extent = seriesExtent(s.values ?? []);
|
|
1119
|
+
if (extent === void 0) return `- ${s.label}${t.colon}${t.none}`;
|
|
1120
|
+
const trend = `${formatNumber(extent.first, lang, spec.decimals)} \u2192 ${value(extent.last, spec.yUnit)}`;
|
|
1121
|
+
return `- ${s.label}${t.colon}${trend}${t.extent(value(extent.min), value(extent.max), t.points(extent.count))}`;
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
function pieLines(spec, lang) {
|
|
1125
|
+
const t = WORDS2[lang];
|
|
1126
|
+
const values = spec.series?.[0]?.values ?? [];
|
|
1127
|
+
const total = values.reduce((sum, v) => sum + (v ?? 0), 0);
|
|
1128
|
+
return (spec.categories ?? []).map((name, i) => {
|
|
1129
|
+
const v = values[i] ?? 0;
|
|
1130
|
+
const pct = total > 0 ? v / total * 100 : 0;
|
|
1131
|
+
const share = pct > 0 && pct < 0.5 ? "<1%" : `${formatNumber(Math.round(pct), lang)}%`;
|
|
1132
|
+
return `- ${name}${t.colon}${formatValue(v, spec.yUnit, lang, spec.decimals)}${t.share(share)}`;
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
function scatterLines(spec, lang) {
|
|
1136
|
+
const t = WORDS2[lang];
|
|
1137
|
+
return (spec.series ?? []).map((s) => {
|
|
1138
|
+
const points = s.points ?? [];
|
|
1139
|
+
const x = seriesExtent(points.map((p) => p[0]));
|
|
1140
|
+
const y = seriesExtent(points.map((p) => p[1]));
|
|
1141
|
+
const parts = [t.points(points.length)];
|
|
1142
|
+
if (x !== void 0)
|
|
1143
|
+
parts.push(`${spec.xLabel ?? "x"} ${span(x.min, x.max, spec.xUnit, lang)}`);
|
|
1144
|
+
if (y !== void 0) {
|
|
1145
|
+
parts.push(`${spec.yLabel ?? "y"} ${span(y.min, y.max, spec.yUnit, lang, spec.decimals)}`);
|
|
1146
|
+
}
|
|
1147
|
+
return `- ${s.label}${t.colon}${parts.join(" \xB7 ")}`;
|
|
1148
|
+
});
|
|
1149
|
+
}
|
|
1150
|
+
function chartMarkdown(spec, lang) {
|
|
1151
|
+
const out = [];
|
|
1152
|
+
if (spec.title) out.push(`**${spec.title}**`, "");
|
|
1153
|
+
switch (spec.kind) {
|
|
1154
|
+
case "gauge":
|
|
1155
|
+
if (spec.item !== void 0) out.push(metricLine(spec.item, lang));
|
|
1156
|
+
break;
|
|
1157
|
+
case "pie":
|
|
1158
|
+
out.push(...pieLines(spec, lang));
|
|
1159
|
+
break;
|
|
1160
|
+
case "scatter":
|
|
1161
|
+
out.push(...scatterLines(spec, lang));
|
|
1162
|
+
break;
|
|
1163
|
+
default: {
|
|
1164
|
+
const edges = spec.edges ?? [];
|
|
1165
|
+
const rows = spec.kind === "histogram" ? edges.slice(1).map((hi, i) => span(edges[i] ?? hi, hi, spec.xUnit, lang)) : spec.categories ?? [];
|
|
1166
|
+
out.push(...rows.length > MAX_ROWS ? summaryLines(spec, lang) : rowLines(spec, rows, lang));
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
return out;
|
|
1170
|
+
}
|
|
1171
|
+
|
|
1172
|
+
// src/options.ts
|
|
1173
|
+
var OPTION_FIELDS = ["label", "value", "description", "recommended"];
|
|
1174
|
+
function parseOptions(is, obj, key, path, count2 = A2UI_LIMITS.options) {
|
|
1175
|
+
const raw = readArray(is, obj, key, path, {
|
|
1176
|
+
...count2,
|
|
1177
|
+
warnCode: "too_many_options",
|
|
1178
|
+
what: "options"
|
|
1179
|
+
});
|
|
1180
|
+
if (raw === void 0) return void 0;
|
|
1181
|
+
const p = join(path, key);
|
|
1182
|
+
const options = [];
|
|
1183
|
+
const labels = /* @__PURE__ */ new Map();
|
|
1184
|
+
let recommended = 0;
|
|
1185
|
+
raw.forEach((item, i) => {
|
|
1186
|
+
const ip = `${p}[${i}]`;
|
|
1187
|
+
if (!isObj(item)) {
|
|
1188
|
+
is.error("wrong_type", `\`${ip}\` must be an object with a \`label\`.`, ip);
|
|
1189
|
+
return;
|
|
1190
|
+
}
|
|
1191
|
+
const label = readString(is, item, "label", ip, { required: true, max: A2UI_LIMITS.label });
|
|
1192
|
+
const value = readString(is, item, "value", ip, { max: A2UI_LIMITS.value });
|
|
1193
|
+
const description = readString(is, item, "description", ip, {
|
|
1194
|
+
max: A2UI_LIMITS.description
|
|
1195
|
+
});
|
|
1196
|
+
const rec = readBoolean(is, item, "recommended", ip);
|
|
1197
|
+
unknownFields(is, item, OPTION_FIELDS, ip, "an option");
|
|
1198
|
+
if (rec === true) recommended++;
|
|
1199
|
+
if (label === void 0) return;
|
|
1200
|
+
if (length(label) > A2UI_LIMITS.labelWarn) {
|
|
1201
|
+
is.warn(
|
|
1202
|
+
"option_label_long",
|
|
1203
|
+
`\`${ip}.label\` is longer than ${A2UI_LIMITS.labelWarn} characters; keep labels short and put the detail in \`description\`.`,
|
|
1204
|
+
`${ip}.label`
|
|
1205
|
+
);
|
|
1206
|
+
}
|
|
1207
|
+
const seen = labels.get(label);
|
|
1208
|
+
if (seen !== void 0) {
|
|
1209
|
+
is.error(
|
|
1210
|
+
"duplicate_label",
|
|
1211
|
+
`\`${ip}.label\` repeats option ${seen + 1}'s label "${label}"; labels must be unique.`,
|
|
1212
|
+
`${ip}.label`
|
|
1213
|
+
);
|
|
1214
|
+
} else {
|
|
1215
|
+
labels.set(label, i);
|
|
1216
|
+
}
|
|
1217
|
+
const option = { label };
|
|
1218
|
+
if (value !== void 0) option.value = value;
|
|
1219
|
+
if (description !== void 0) option.description = description;
|
|
1220
|
+
if (rec !== void 0) option.recommended = rec;
|
|
1221
|
+
options.push(option);
|
|
1222
|
+
});
|
|
1223
|
+
if (recommended > 1) {
|
|
1224
|
+
is.error(
|
|
1225
|
+
"multiple_recommended",
|
|
1226
|
+
`\`${p}\` marks ${recommended} options as recommended; at most one may be.`,
|
|
1227
|
+
p
|
|
1228
|
+
);
|
|
1229
|
+
}
|
|
1230
|
+
return options;
|
|
1231
|
+
}
|
|
1232
|
+
|
|
1233
|
+
// src/form.ts
|
|
1234
|
+
var FIELD_KINDS = [
|
|
1235
|
+
"text",
|
|
1236
|
+
"textarea",
|
|
1237
|
+
"number",
|
|
1238
|
+
"slider",
|
|
1239
|
+
"single",
|
|
1240
|
+
"multiple",
|
|
1241
|
+
"select",
|
|
1242
|
+
"switch",
|
|
1243
|
+
"date",
|
|
1244
|
+
"time",
|
|
1245
|
+
"datetime"
|
|
1246
|
+
];
|
|
1247
|
+
var A2UI_FIELD_FORMATS = {
|
|
1248
|
+
date: /^\d{4}-\d{2}-\d{2}$/,
|
|
1249
|
+
time: /^([01]\d|2[0-3]):[0-5]\d$/,
|
|
1250
|
+
datetime: /^\d{4}-\d{2}-\d{2}T([01]\d|2[0-3]):[0-5]\d$/
|
|
1251
|
+
};
|
|
1252
|
+
var FORMAT_RULES = {
|
|
1253
|
+
date: "YYYY-MM-DD",
|
|
1254
|
+
time: "HH:mm",
|
|
1255
|
+
datetime: "YYYY-MM-DD HH:mm"
|
|
1256
|
+
};
|
|
1257
|
+
var FORMAT_SPELLING = {
|
|
1258
|
+
date: "YYYY-MM-DD, a day that exists, such as 2026-10-20",
|
|
1259
|
+
time: "HH:mm on the 24-hour clock, such as 09:30",
|
|
1260
|
+
datetime: "YYYY-MM-DDTHH:mm, a day that exists, such as 2026-10-20T09:30"
|
|
1261
|
+
};
|
|
1262
|
+
var OPTION_KINDS = ["single", "multiple", "select"];
|
|
1263
|
+
var RANGE_KINDS = ["number", "slider"];
|
|
1264
|
+
var LENGTH_KINDS = ["text", "textarea"];
|
|
1265
|
+
var PATTERN_KINDS = ["text"];
|
|
1266
|
+
var PLACEHOLDER_KINDS = ["text", "textarea", "number", "select"];
|
|
1267
|
+
var KIND_ONLY = [
|
|
1268
|
+
{ keys: ["min", "max", "step", "unit"], kinds: RANGE_KINDS },
|
|
1269
|
+
{ keys: ["maxLength"], kinds: LENGTH_KINDS },
|
|
1270
|
+
{ keys: ["pattern"], kinds: PATTERN_KINDS }
|
|
1271
|
+
];
|
|
1272
|
+
var SELECT_COUNT = {
|
|
1273
|
+
...A2UI_LIMITS.selectOptions,
|
|
1274
|
+
warnHint: "Cut the list to the likely ones, or ask with a `text` field."
|
|
1275
|
+
};
|
|
1276
|
+
var FIELD_FIELDS = [
|
|
1277
|
+
"id",
|
|
1278
|
+
"label",
|
|
1279
|
+
"kind",
|
|
1280
|
+
"hint",
|
|
1281
|
+
"required",
|
|
1282
|
+
"options",
|
|
1283
|
+
"placeholder",
|
|
1284
|
+
"min",
|
|
1285
|
+
"max",
|
|
1286
|
+
"step",
|
|
1287
|
+
"unit",
|
|
1288
|
+
"maxLength",
|
|
1289
|
+
"pattern",
|
|
1290
|
+
"default"
|
|
1291
|
+
];
|
|
1292
|
+
var FORM_FIELDS = ["type", "id", "title", "fields", "submitLabel"];
|
|
1293
|
+
function kindNames(kinds) {
|
|
1294
|
+
const quoted = kinds.map((kind) => `"${kind}"`);
|
|
1295
|
+
const last = quoted.pop() ?? "";
|
|
1296
|
+
return `kind ${quoted.length > 0 ? `${quoted.join(", ")} or ${last}` : last}`;
|
|
1297
|
+
}
|
|
1298
|
+
var lengthCeiling = (kind) => kind === "textarea" ? A2UI_LIMITS.textareaMaxLength : A2UI_LIMITS.textMaxLength;
|
|
1299
|
+
var compiled = /* @__PURE__ */ new Map();
|
|
1300
|
+
function compilePattern(pattern) {
|
|
1301
|
+
let re = compiled.get(pattern);
|
|
1302
|
+
if (re === void 0) {
|
|
1303
|
+
try {
|
|
1304
|
+
new RegExp(pattern, "u");
|
|
1305
|
+
re = new RegExp(`^(?:${pattern})$`, "u");
|
|
1306
|
+
} catch {
|
|
1307
|
+
re = null;
|
|
1308
|
+
}
|
|
1309
|
+
compiled.set(pattern, re);
|
|
1310
|
+
}
|
|
1311
|
+
return re;
|
|
1312
|
+
}
|
|
1313
|
+
function formatHolds(kind, text) {
|
|
1314
|
+
if (!A2UI_FIELD_FORMATS[kind].test(text)) return false;
|
|
1315
|
+
return kind === "time" || parseA2uiInstant(text) !== null;
|
|
1316
|
+
}
|
|
1317
|
+
var isFormatKind = (kind) => kind === "date" || kind === "time" || kind === "datetime";
|
|
1318
|
+
function rangeText(min, max) {
|
|
1319
|
+
if (min !== void 0 && max !== void 0) return `${min}\u2013${max}`;
|
|
1320
|
+
if (min !== void 0) return `\u2265 ${min}`;
|
|
1321
|
+
if (max !== void 0) return `\u2264 ${max}`;
|
|
1322
|
+
return void 0;
|
|
1323
|
+
}
|
|
1324
|
+
function fieldRule(field) {
|
|
1325
|
+
if (field.kind === "number" || field.kind === "slider") return rangeText(field.min, field.max);
|
|
1326
|
+
if (field.kind === "text" || field.kind === "textarea") {
|
|
1327
|
+
return field.maxLength !== void 0 ? `\u2264 ${field.maxLength}` : void 0;
|
|
1328
|
+
}
|
|
1329
|
+
if (isFormatKind(field.kind)) return FORMAT_RULES[field.kind];
|
|
1330
|
+
return void 0;
|
|
1331
|
+
}
|
|
1332
|
+
var problem = (code, rule) => rule !== void 0 ? { code, rule } : { code };
|
|
1333
|
+
var labelsOf = (field) => (field.options ?? []).map((option) => option.label);
|
|
1334
|
+
function fieldProblem(field, answer) {
|
|
1335
|
+
if (field.kind === "switch") {
|
|
1336
|
+
return answer === void 0 || typeof answer === "boolean" ? null : problem("format");
|
|
1337
|
+
}
|
|
1338
|
+
const blank = answer === void 0 || typeof answer === "string" && answer.trim() === "" || Array.isArray(answer) && answer.length === 0;
|
|
1339
|
+
if (blank) return field.required === true ? problem("empty") : null;
|
|
1340
|
+
switch (field.kind) {
|
|
1341
|
+
case "single":
|
|
1342
|
+
case "select":
|
|
1343
|
+
return typeof answer === "string" && labelsOf(field).includes(answer) ? null : problem("not_an_option");
|
|
1344
|
+
case "multiple": {
|
|
1345
|
+
const labels = labelsOf(field);
|
|
1346
|
+
return Array.isArray(answer) && answer.every((label) => typeof label === "string" && labels.includes(label)) ? null : problem("not_an_option");
|
|
1347
|
+
}
|
|
1348
|
+
case "number":
|
|
1349
|
+
case "slider": {
|
|
1350
|
+
const value = typeof answer === "string" ? Number(answer.trim()) : Number.NaN;
|
|
1351
|
+
if (!Number.isFinite(value)) return problem("not_a_number", fieldRule(field));
|
|
1352
|
+
const below = field.min !== void 0 && value < field.min;
|
|
1353
|
+
const above = field.max !== void 0 && value > field.max;
|
|
1354
|
+
return below || above ? problem("out_of_range", fieldRule(field)) : null;
|
|
1355
|
+
}
|
|
1356
|
+
case "text":
|
|
1357
|
+
case "textarea": {
|
|
1358
|
+
if (typeof answer !== "string") return problem("format");
|
|
1359
|
+
const text = answer.trim();
|
|
1360
|
+
const limit = field.maxLength ?? lengthCeiling(field.kind);
|
|
1361
|
+
if (length(text) > limit) return problem("too_long", `\u2264 ${limit}`);
|
|
1362
|
+
if (field.kind === "text" && field.pattern !== void 0) {
|
|
1363
|
+
const re = compilePattern(field.pattern);
|
|
1364
|
+
if (re !== null && !re.test(text)) return problem("pattern");
|
|
1365
|
+
}
|
|
1366
|
+
return null;
|
|
1367
|
+
}
|
|
1368
|
+
case "date":
|
|
1369
|
+
case "time":
|
|
1370
|
+
case "datetime":
|
|
1371
|
+
return typeof answer === "string" && formatHolds(field.kind, answer) ? null : problem("format", FORMAT_RULES[field.kind]);
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
function initialAnswer(field) {
|
|
1375
|
+
const value = field.default;
|
|
1376
|
+
if (value === void 0) {
|
|
1377
|
+
if (field.kind === "slider" && field.min !== void 0) return String(field.min);
|
|
1378
|
+
if (field.kind === "switch") return false;
|
|
1379
|
+
return void 0;
|
|
1380
|
+
}
|
|
1381
|
+
if (typeof value === "number") return String(value);
|
|
1382
|
+
if (Array.isArray(value)) return labelsOf(field).filter((label) => value.includes(label));
|
|
1383
|
+
return value;
|
|
1384
|
+
}
|
|
1385
|
+
function expectedDefault(kind) {
|
|
1386
|
+
switch (kind) {
|
|
1387
|
+
case "number":
|
|
1388
|
+
case "slider":
|
|
1389
|
+
return "a number";
|
|
1390
|
+
case "single":
|
|
1391
|
+
case "select":
|
|
1392
|
+
return "one option label";
|
|
1393
|
+
case "multiple":
|
|
1394
|
+
return "a list of option labels";
|
|
1395
|
+
case "switch":
|
|
1396
|
+
return "true or false";
|
|
1397
|
+
default:
|
|
1398
|
+
return "a string";
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
function defaultAnswer(kind, raw) {
|
|
1402
|
+
switch (kind) {
|
|
1403
|
+
case "number":
|
|
1404
|
+
case "slider":
|
|
1405
|
+
return typeof raw === "number" && Number.isFinite(raw) ? String(raw) : void 0;
|
|
1406
|
+
case "multiple":
|
|
1407
|
+
return Array.isArray(raw) && raw.every((entry) => typeof entry === "string") ? [...raw] : void 0;
|
|
1408
|
+
case "switch":
|
|
1409
|
+
return typeof raw === "boolean" ? raw : void 0;
|
|
1410
|
+
default:
|
|
1411
|
+
return typeof raw === "string" ? raw : void 0;
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
function defaultReason(field, answer, found) {
|
|
1415
|
+
switch (found.code) {
|
|
1416
|
+
case "empty":
|
|
1417
|
+
return "is empty";
|
|
1418
|
+
case "not_a_number":
|
|
1419
|
+
case "out_of_range":
|
|
1420
|
+
return `is ${String(answer)}, outside ${found.rule ?? "the field's range"}`;
|
|
1421
|
+
case "too_long":
|
|
1422
|
+
return `is ${length(String(answer))} characters; the field takes ${found.rule ?? ""}`;
|
|
1423
|
+
case "pattern":
|
|
1424
|
+
return "does not match the field's `pattern`";
|
|
1425
|
+
case "format":
|
|
1426
|
+
return `must be written ${isFormatKind(field.kind) ? FORMAT_SPELLING[field.kind] : expectedDefault(field.kind)}`;
|
|
1427
|
+
case "not_an_option":
|
|
1428
|
+
return "names a label that is not one of the field's options";
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
function readDefault(is, item, fp, field) {
|
|
1432
|
+
if (!present(item.default)) return;
|
|
1433
|
+
const dp = `${fp}.default`;
|
|
1434
|
+
const fix = "; give a starting answer the field accepts, or leave `default` out.";
|
|
1435
|
+
const answer = defaultAnswer(field.kind, item.default);
|
|
1436
|
+
if (answer === void 0) {
|
|
1437
|
+
is.error(
|
|
1438
|
+
"default_invalid",
|
|
1439
|
+
`\`${dp}\` must be ${expectedDefault(field.kind)} for kind "${field.kind}"${fix}`,
|
|
1440
|
+
dp
|
|
1441
|
+
);
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
if (Array.isArray(answer) && new Set(answer).size !== answer.length) {
|
|
1445
|
+
is.error("default_invalid", `\`${dp}\` repeats a label${fix}`, dp);
|
|
1446
|
+
return;
|
|
1447
|
+
}
|
|
1448
|
+
const found = fieldProblem({ ...field, required: true }, answer);
|
|
1449
|
+
if (found !== null) {
|
|
1450
|
+
is.error("default_invalid", `\`${dp}\` ${defaultReason(field, answer, found)}${fix}`, dp);
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
field.default = Array.isArray(answer) ? answer : item.default;
|
|
1454
|
+
}
|
|
1455
|
+
function readFieldOptions(is, item, fp, kind) {
|
|
1456
|
+
const op = `${fp}.options`;
|
|
1457
|
+
if (kind === void 0) return void 0;
|
|
1458
|
+
if (!OPTION_KINDS.includes(kind)) {
|
|
1459
|
+
if (present(item.options)) {
|
|
1460
|
+
is.error(
|
|
1461
|
+
"options_forbidden",
|
|
1462
|
+
`\`${op}\` is not allowed for kind "${kind}"; use ${kindNames(OPTION_KINDS)} to offer options.`,
|
|
1463
|
+
op
|
|
1464
|
+
);
|
|
1465
|
+
}
|
|
1466
|
+
return void 0;
|
|
1467
|
+
}
|
|
1468
|
+
if (!present(item.options)) {
|
|
1469
|
+
is.error("options_required", `\`${op}\` is required for kind "${kind}".`, op);
|
|
1470
|
+
return void 0;
|
|
1471
|
+
}
|
|
1472
|
+
const options = parseOptions(
|
|
1473
|
+
is,
|
|
1474
|
+
item,
|
|
1475
|
+
"options",
|
|
1476
|
+
fp,
|
|
1477
|
+
kind === "select" ? SELECT_COUNT : void 0
|
|
1478
|
+
);
|
|
1479
|
+
if (kind === "select") {
|
|
1480
|
+
options?.forEach((option, i) => {
|
|
1481
|
+
if (option.description === void 0) return;
|
|
1482
|
+
is.warn(
|
|
1483
|
+
"ignored_field",
|
|
1484
|
+
`\`${op}[${i}].description\` has no effect on kind "select": its rows are one line; it is ignored.`,
|
|
1485
|
+
`${op}[${i}].description`
|
|
1486
|
+
);
|
|
1487
|
+
delete option.description;
|
|
1488
|
+
});
|
|
1489
|
+
}
|
|
1490
|
+
return options;
|
|
1491
|
+
}
|
|
1492
|
+
function checkKindRules(is, item, fp, kind, read) {
|
|
1493
|
+
for (const { keys, kinds } of KIND_ONLY) {
|
|
1494
|
+
if (kinds.includes(kind)) continue;
|
|
1495
|
+
for (const key of keys) {
|
|
1496
|
+
if (!present(item[key])) continue;
|
|
1497
|
+
is.error(
|
|
1498
|
+
"not_for_kind",
|
|
1499
|
+
`\`${fp}.${key}\` is only for ${kindNames(kinds)}, not "${kind}"; remove it or change the kind.`,
|
|
1500
|
+
`${fp}.${key}`
|
|
1501
|
+
);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
if (present(item.placeholder) && !PLACEHOLDER_KINDS.includes(kind)) {
|
|
1505
|
+
is.warn(
|
|
1506
|
+
"ignored_field",
|
|
1507
|
+
`\`${fp}.placeholder\` has no effect on kind "${kind}"; it is ignored.`,
|
|
1508
|
+
`${fp}.placeholder`
|
|
1509
|
+
);
|
|
1510
|
+
}
|
|
1511
|
+
if (RANGE_KINDS.includes(kind)) {
|
|
1512
|
+
if (kind === "slider") {
|
|
1513
|
+
for (const key of ["min", "max"]) {
|
|
1514
|
+
if (present(item[key])) continue;
|
|
1515
|
+
is.error(
|
|
1516
|
+
"missing_field",
|
|
1517
|
+
`\`${fp}.${key}\` is required for kind "slider": a slider moves between a min and a max.`,
|
|
1518
|
+
`${fp}.${key}`
|
|
1519
|
+
);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
if (read.min !== void 0 && read.max !== void 0 && read.min >= read.max) {
|
|
1523
|
+
is.error(
|
|
1524
|
+
"range_invalid",
|
|
1525
|
+
`\`${fp}\`: min ${read.min} must be less than max ${read.max}.`,
|
|
1526
|
+
`${fp}.min`
|
|
1527
|
+
);
|
|
1528
|
+
}
|
|
1529
|
+
if (read.step !== void 0 && read.step <= 0) {
|
|
1530
|
+
is.error("range_invalid", `\`${fp}.step\` must be greater than 0.`, `${fp}.step`);
|
|
1531
|
+
}
|
|
1532
|
+
}
|
|
1533
|
+
if (LENGTH_KINDS.includes(kind) && read.maxLength !== void 0) {
|
|
1534
|
+
const ceiling = lengthCeiling(kind);
|
|
1535
|
+
const n = read.maxLength;
|
|
1536
|
+
if (!Number.isInteger(n) || n < 1 || n > ceiling) {
|
|
1537
|
+
is.error(
|
|
1538
|
+
"range_invalid",
|
|
1539
|
+
`\`${fp}.maxLength\` is ${n}; for kind "${kind}" it is a whole number from 1 to ${ceiling}.`,
|
|
1540
|
+
`${fp}.maxLength`
|
|
1541
|
+
);
|
|
1542
|
+
}
|
|
1543
|
+
}
|
|
1544
|
+
if (PATTERN_KINDS.includes(kind) && read.pattern !== void 0) {
|
|
1545
|
+
if (compilePattern(read.pattern) === null) {
|
|
1546
|
+
is.error(
|
|
1547
|
+
"invalid_format",
|
|
1548
|
+
`\`${fp}.pattern\` is not a regular expression JavaScript can compile (u flag, no slashes, no flags); fix it, or drop it and say the format in \`hint\`.`,
|
|
1549
|
+
`${fp}.pattern`
|
|
1550
|
+
);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
function parseFormField(is, item, fp) {
|
|
1555
|
+
if (!isObj(item)) {
|
|
1556
|
+
is.error("wrong_type", `\`${fp}\` must be an object with \`id\`, \`label\` and \`kind\`.`, fp);
|
|
1557
|
+
return void 0;
|
|
1558
|
+
}
|
|
1559
|
+
const id = readString(is, item, "id", fp, {
|
|
1560
|
+
required: true,
|
|
1561
|
+
max: 32,
|
|
1562
|
+
pattern: A2UI_LIMITS.fieldIdPattern,
|
|
1563
|
+
patternHint: "lower-case letters, digits or _, starting with a letter, at most 32"
|
|
1564
|
+
});
|
|
1565
|
+
const label = readString(is, item, "label", fp, { required: true, max: A2UI_LIMITS.label });
|
|
1566
|
+
const kind = readEnum(is, item, "kind", fp, FIELD_KINDS);
|
|
1567
|
+
const hint = readString(is, item, "hint", fp, { max: A2UI_LIMITS.fieldHint });
|
|
1568
|
+
const required = readBoolean(is, item, "required", fp);
|
|
1569
|
+
const options = readFieldOptions(is, item, fp, kind);
|
|
1570
|
+
const placeholder = readString(is, item, "placeholder", fp, { max: A2UI_LIMITS.placeholder });
|
|
1571
|
+
const min = readNumber(is, item, "min", fp);
|
|
1572
|
+
const max = readNumber(is, item, "max", fp);
|
|
1573
|
+
const step = readNumber(is, item, "step", fp);
|
|
1574
|
+
const unit = readString(is, item, "unit", fp, { max: A2UI_LIMITS.unit });
|
|
1575
|
+
const maxLength = readNumber(is, item, "maxLength", fp);
|
|
1576
|
+
const pattern = readString(is, item, "pattern", fp, { max: A2UI_LIMITS.pattern });
|
|
1577
|
+
unknownFields(is, item, FIELD_FIELDS, fp, "a form field");
|
|
1578
|
+
if (kind !== void 0)
|
|
1579
|
+
checkKindRules(is, item, fp, kind, { min, max, step, maxLength, pattern });
|
|
1580
|
+
if (id === void 0 || label === void 0 || kind === void 0) return void 0;
|
|
1581
|
+
const field = { id, label, kind };
|
|
1582
|
+
if (hint !== void 0) field.hint = hint;
|
|
1583
|
+
if (required !== void 0) field.required = required;
|
|
1584
|
+
if (options !== void 0) field.options = options;
|
|
1585
|
+
if (placeholder !== void 0 && PLACEHOLDER_KINDS.includes(kind))
|
|
1586
|
+
field.placeholder = placeholder;
|
|
1587
|
+
if (RANGE_KINDS.includes(kind)) {
|
|
1588
|
+
if (min !== void 0) field.min = min;
|
|
1589
|
+
if (max !== void 0) field.max = max;
|
|
1590
|
+
if (step !== void 0) field.step = step;
|
|
1591
|
+
if (unit !== void 0) field.unit = unit;
|
|
1592
|
+
}
|
|
1593
|
+
if (maxLength !== void 0 && LENGTH_KINDS.includes(kind)) field.maxLength = maxLength;
|
|
1594
|
+
if (pattern !== void 0 && PATTERN_KINDS.includes(kind)) field.pattern = pattern;
|
|
1595
|
+
readDefault(is, item, fp, field);
|
|
1596
|
+
return field;
|
|
1597
|
+
}
|
|
1598
|
+
function parseForm(is, obj) {
|
|
1599
|
+
const id = readString(is, obj, "id", "", {
|
|
1600
|
+
max: 32,
|
|
1601
|
+
pattern: A2UI_LIMITS.idPattern,
|
|
1602
|
+
patternHint: ID_HINT
|
|
1603
|
+
});
|
|
1604
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.title });
|
|
1605
|
+
const raw = readArray(is, obj, "fields", "", {
|
|
1606
|
+
...A2UI_LIMITS.fields,
|
|
1607
|
+
warnCode: "form_too_many_fields",
|
|
1608
|
+
what: "fields"
|
|
1609
|
+
});
|
|
1610
|
+
const submitLabel = readString(is, obj, "submitLabel", "", { max: A2UI_LIMITS.submitLabel });
|
|
1611
|
+
unknownFields(is, obj, FORM_FIELDS, "", "a form");
|
|
1612
|
+
if (raw === void 0) return void 0;
|
|
1613
|
+
const fields = [];
|
|
1614
|
+
const ids = /* @__PURE__ */ new Map();
|
|
1615
|
+
raw.forEach((item, i) => {
|
|
1616
|
+
const field = parseFormField(is, item, `fields[${i}]`);
|
|
1617
|
+
if (field === void 0) return;
|
|
1618
|
+
const seen = ids.get(field.id);
|
|
1619
|
+
if (seen !== void 0) {
|
|
1620
|
+
is.error(
|
|
1621
|
+
"duplicate_id",
|
|
1622
|
+
`\`fields[${i}].id\` repeats field ${seen + 1}'s id "${field.id}"; ids must be unique.`,
|
|
1623
|
+
`fields[${i}].id`
|
|
1624
|
+
);
|
|
1625
|
+
} else {
|
|
1626
|
+
ids.set(field.id, i);
|
|
1627
|
+
}
|
|
1628
|
+
fields.push(field);
|
|
1629
|
+
});
|
|
1630
|
+
const spec = { type: "form", fields };
|
|
1631
|
+
if (id !== void 0) spec.id = id;
|
|
1632
|
+
if (title !== void 0) spec.title = title;
|
|
1633
|
+
if (submitLabel !== void 0) spec.submitLabel = submitLabel;
|
|
1634
|
+
return spec;
|
|
1635
|
+
}
|
|
1636
|
+
|
|
1637
|
+
// src/resources.ts
|
|
1638
|
+
var RESOURCES_FIELDS = [
|
|
1639
|
+
"type",
|
|
1640
|
+
"host",
|
|
1641
|
+
"cpu",
|
|
1642
|
+
"memory",
|
|
1643
|
+
"swap",
|
|
1644
|
+
"disks",
|
|
1645
|
+
"network",
|
|
1646
|
+
"load",
|
|
1647
|
+
"uptimeSec",
|
|
1648
|
+
"asOf"
|
|
1649
|
+
];
|
|
1650
|
+
var SECTIONS = ["cpu", "memory", "swap", "disks", "network", "load", "uptimeSec"];
|
|
1651
|
+
var CPU_FIELDS = ["percent", "history", "cores"];
|
|
1652
|
+
var MEMORY_FIELDS = ["used", "total", "unit", "history"];
|
|
1653
|
+
var SWAP_FIELDS = ["used", "total", "unit"];
|
|
1654
|
+
var DISK_FIELDS = ["path", "used", "total", "unit"];
|
|
1655
|
+
var NETWORK_FIELDS = ["unit", "rx", "tx"];
|
|
1656
|
+
var LOAD_FIELDS = ["one", "five", "fifteen", "cores"];
|
|
1657
|
+
var MEMORY_UNITS = ["GB", "MB"];
|
|
1658
|
+
var DISK_UNITS = ["GB", "TB"];
|
|
1659
|
+
var RATE_UNITS = ["KB/s", "MB/s", "Mbps"];
|
|
1660
|
+
var MARKS = {
|
|
1661
|
+
share: { warn: 80, danger: 95 },
|
|
1662
|
+
disk: { warn: 85, danger: 95 },
|
|
1663
|
+
loadPerCore: { warn: 1, danger: 2 }
|
|
1664
|
+
};
|
|
1665
|
+
function section(is, obj, key, example) {
|
|
1666
|
+
const value = obj[key];
|
|
1667
|
+
if (!present(value)) return void 0;
|
|
1668
|
+
if (isObj(value)) return value;
|
|
1669
|
+
is.error("wrong_type", `\`${key}\` must be an object, such as ${example}.`, key);
|
|
1670
|
+
return void 0;
|
|
1671
|
+
}
|
|
1672
|
+
function readCores(is, obj, path, required) {
|
|
1673
|
+
const cores = readNumber(is, obj, "cores", path, required);
|
|
1674
|
+
if (cores === void 0 || Number.isInteger(cores) && cores >= 1) return cores;
|
|
1675
|
+
const p = join(path, "cores");
|
|
1676
|
+
is.error(
|
|
1677
|
+
"range_invalid",
|
|
1678
|
+
`\`${p}\` is ${cores}; a core count is a whole number of at least 1.`,
|
|
1679
|
+
p
|
|
1680
|
+
);
|
|
1681
|
+
return void 0;
|
|
1682
|
+
}
|
|
1683
|
+
function readAmount(is, obj, key, path, required) {
|
|
1684
|
+
const value = readNumber(is, obj, key, path, required);
|
|
1685
|
+
if (value === void 0 || value >= 0) return value;
|
|
1686
|
+
const p = join(path, key);
|
|
1687
|
+
is.error("range_invalid", `\`${p}\` is ${value}; it is at least 0. Fix the value.`, p);
|
|
1688
|
+
return void 0;
|
|
1689
|
+
}
|
|
1690
|
+
function readUsage(is, obj, path) {
|
|
1691
|
+
const used = readAmount(is, obj, "used", path, true);
|
|
1692
|
+
const total = readNumber(is, obj, "total", path, true);
|
|
1693
|
+
if (total !== void 0 && total <= 0) {
|
|
1694
|
+
const p = join(path, "total");
|
|
1695
|
+
is.error(
|
|
1696
|
+
"range_invalid",
|
|
1697
|
+
`\`${p}\` is ${total}; the whole is above 0. Leave the section out when there is none.`,
|
|
1698
|
+
p
|
|
1699
|
+
);
|
|
1700
|
+
return void 0;
|
|
1701
|
+
}
|
|
1702
|
+
if (used === void 0 || total === void 0) return void 0;
|
|
1703
|
+
if (used > total) {
|
|
1704
|
+
const p = join(path, "used");
|
|
1705
|
+
is.error(
|
|
1706
|
+
"range_invalid",
|
|
1707
|
+
`\`${p}\` ${used} is above \`${join(path, "total")}\` ${total}; fix the values.`,
|
|
1708
|
+
p
|
|
1709
|
+
);
|
|
1710
|
+
}
|
|
1711
|
+
return { used, total };
|
|
1712
|
+
}
|
|
1713
|
+
function readPercentHistory(is, obj, path) {
|
|
1714
|
+
if (!present(obj.history)) return void 0;
|
|
1715
|
+
return readNumbers(is, obj, "history", path, {
|
|
1716
|
+
...A2UI_LIMITS.history,
|
|
1717
|
+
what: "points",
|
|
1718
|
+
atLeast: 0,
|
|
1719
|
+
atMost: 100,
|
|
1720
|
+
rangeHint: "a percentage is 0\u2013100"
|
|
1721
|
+
});
|
|
1722
|
+
}
|
|
1723
|
+
function parseCpu(is, obj) {
|
|
1724
|
+
const cpu = section(is, obj, "cpu", '{"percent": 37}');
|
|
1725
|
+
if (cpu === void 0) return void 0;
|
|
1726
|
+
const percent = readPercent(is, cpu, "percent", "cpu");
|
|
1727
|
+
if (!present(cpu.percent))
|
|
1728
|
+
is.error("missing_field", "`cpu.percent` is required: a number 0\u2013100.", "cpu.percent");
|
|
1729
|
+
const history = readPercentHistory(is, cpu, "cpu");
|
|
1730
|
+
const cores = readCores(is, cpu, "cpu", false);
|
|
1731
|
+
unknownFields(is, cpu, CPU_FIELDS, "cpu", "the cpu section");
|
|
1732
|
+
if (percent === void 0) return void 0;
|
|
1733
|
+
const out = { percent };
|
|
1734
|
+
if (history !== void 0) out.history = history;
|
|
1735
|
+
if (cores !== void 0) out.cores = cores;
|
|
1736
|
+
return out;
|
|
1737
|
+
}
|
|
1738
|
+
function parseMemory(is, obj) {
|
|
1739
|
+
const memory = section(is, obj, "memory", '{"used": 13.9, "total": 16}');
|
|
1740
|
+
if (memory === void 0) return void 0;
|
|
1741
|
+
const usage = readUsage(is, memory, "memory");
|
|
1742
|
+
const unit = readEnum(is, memory, "unit", "memory", MEMORY_UNITS, false);
|
|
1743
|
+
const history = readPercentHistory(is, memory, "memory");
|
|
1744
|
+
unknownFields(is, memory, MEMORY_FIELDS, "memory", "the memory section");
|
|
1745
|
+
if (usage === void 0) return void 0;
|
|
1746
|
+
const out = { ...usage };
|
|
1747
|
+
if (unit !== void 0) out.unit = unit;
|
|
1748
|
+
if (history !== void 0) out.history = history;
|
|
1749
|
+
return out;
|
|
1750
|
+
}
|
|
1751
|
+
function parseSwap(is, obj) {
|
|
1752
|
+
const swap = section(is, obj, "swap", '{"used": 0.5, "total": 2}');
|
|
1753
|
+
if (swap === void 0) return void 0;
|
|
1754
|
+
const usage = readUsage(is, swap, "swap");
|
|
1755
|
+
const unit = readEnum(is, swap, "unit", "swap", MEMORY_UNITS, false);
|
|
1756
|
+
unknownFields(is, swap, SWAP_FIELDS, "swap", "the swap section");
|
|
1757
|
+
if (usage === void 0) return void 0;
|
|
1758
|
+
const out = { ...usage };
|
|
1759
|
+
if (unit !== void 0) out.unit = unit;
|
|
1760
|
+
return out;
|
|
1761
|
+
}
|
|
1762
|
+
function parseDisks(is, obj) {
|
|
1763
|
+
if (!present(obj.disks)) return void 0;
|
|
1764
|
+
const raw = readArray(is, obj, "disks", "", { ...A2UI_LIMITS.disks, what: "disks" });
|
|
1765
|
+
if (raw === void 0) return void 0;
|
|
1766
|
+
const disks = [];
|
|
1767
|
+
const paths = /* @__PURE__ */ new Map();
|
|
1768
|
+
raw.forEach((item, i) => {
|
|
1769
|
+
const dp = `disks[${i}]`;
|
|
1770
|
+
if (!isObj(item)) {
|
|
1771
|
+
is.error(
|
|
1772
|
+
"wrong_type",
|
|
1773
|
+
`\`${dp}\` must be an object with \`path\`, \`used\` and \`total\`.`,
|
|
1774
|
+
dp
|
|
1775
|
+
);
|
|
1776
|
+
return;
|
|
1777
|
+
}
|
|
1778
|
+
const path = readString(is, item, "path", dp, { required: true, max: A2UI_LIMITS.diskPath });
|
|
1779
|
+
const usage = readUsage(is, item, dp);
|
|
1780
|
+
const unit = readEnum(is, item, "unit", dp, DISK_UNITS, false);
|
|
1781
|
+
unknownFields(is, item, DISK_FIELDS, dp, "a disk");
|
|
1782
|
+
if (path === void 0 || usage === void 0) return;
|
|
1783
|
+
const first = paths.get(path);
|
|
1784
|
+
if (first !== void 0) {
|
|
1785
|
+
is.error(
|
|
1786
|
+
"duplicate_label",
|
|
1787
|
+
`\`${dp}.path\` repeats disk ${first + 1}'s path "${path}"; list each disk once.`,
|
|
1788
|
+
`${dp}.path`
|
|
1789
|
+
);
|
|
1790
|
+
} else {
|
|
1791
|
+
paths.set(path, i);
|
|
1792
|
+
}
|
|
1793
|
+
const disk = { path, ...usage };
|
|
1794
|
+
if (unit !== void 0) disk.unit = unit;
|
|
1795
|
+
disks.push(disk);
|
|
1796
|
+
});
|
|
1797
|
+
return disks;
|
|
1798
|
+
}
|
|
1799
|
+
function parseNetwork(is, obj) {
|
|
1800
|
+
const network = section(is, obj, "network", '{"unit": "KB/s", "rx": [120, 98], "tx": [40, 35]}');
|
|
1801
|
+
if (network === void 0) return void 0;
|
|
1802
|
+
const unit = readEnum(is, network, "unit", "network", RATE_UNITS);
|
|
1803
|
+
const rule = {
|
|
1804
|
+
...A2UI_LIMITS.history,
|
|
1805
|
+
what: "readings",
|
|
1806
|
+
atLeast: 0,
|
|
1807
|
+
rangeHint: "a rate is at least 0"
|
|
1808
|
+
};
|
|
1809
|
+
const rx = readNumbers(is, network, "rx", "network", rule);
|
|
1810
|
+
const tx = readNumbers(is, network, "tx", "network", rule);
|
|
1811
|
+
unknownFields(is, network, NETWORK_FIELDS, "network", "the network section");
|
|
1812
|
+
if (rx !== void 0 && tx !== void 0 && rx.length !== tx.length) {
|
|
1813
|
+
is.error(
|
|
1814
|
+
"length_mismatch",
|
|
1815
|
+
`\`network.tx\` has ${tx.length} readings and \`network.rx\` ${rx.length}; give both the same readings, oldest first.`,
|
|
1816
|
+
"network.tx"
|
|
1817
|
+
);
|
|
1818
|
+
}
|
|
1819
|
+
if (unit === void 0 || rx === void 0 || tx === void 0) return void 0;
|
|
1820
|
+
return { unit, rx, tx };
|
|
1821
|
+
}
|
|
1822
|
+
function parseLoad(is, obj) {
|
|
1823
|
+
const load = section(is, obj, "load", '{"one": 1.2, "cores": 4}');
|
|
1824
|
+
if (load === void 0) return void 0;
|
|
1825
|
+
const one = readAmount(is, load, "one", "load", true);
|
|
1826
|
+
const five = readAmount(is, load, "five", "load", false);
|
|
1827
|
+
const fifteen = readAmount(is, load, "fifteen", "load", false);
|
|
1828
|
+
const cores = readCores(is, load, "load", true);
|
|
1829
|
+
unknownFields(is, load, LOAD_FIELDS, "load", "the load section");
|
|
1830
|
+
if (one === void 0 || cores === void 0) return void 0;
|
|
1831
|
+
const out = { one, cores };
|
|
1832
|
+
if (five !== void 0) out.five = five;
|
|
1833
|
+
if (fifteen !== void 0) out.fifteen = fifteen;
|
|
1834
|
+
return out;
|
|
1835
|
+
}
|
|
1836
|
+
function parseResources(is, obj) {
|
|
1837
|
+
const host = readString(is, obj, "host", "", { max: A2UI_LIMITS.host });
|
|
1838
|
+
const cpu = parseCpu(is, obj);
|
|
1839
|
+
const memory = parseMemory(is, obj);
|
|
1840
|
+
const swap = parseSwap(is, obj);
|
|
1841
|
+
const disks = parseDisks(is, obj);
|
|
1842
|
+
const network = parseNetwork(is, obj);
|
|
1843
|
+
const load = parseLoad(is, obj);
|
|
1844
|
+
const uptimeSec = readAmount(is, obj, "uptimeSec", "", false);
|
|
1845
|
+
const asOf = readInstant(is, obj, "asOf", "");
|
|
1846
|
+
unknownFields(is, obj, RESOURCES_FIELDS, "", "a resources block");
|
|
1847
|
+
if (!SECTIONS.some((key) => present(obj[key]))) {
|
|
1848
|
+
is.error(
|
|
1849
|
+
"missing_field",
|
|
1850
|
+
`A resources block needs at least one of ${SECTIONS.join(", ")}; add the readings you have (scripts/sysinfo.mjs prints them).`
|
|
1851
|
+
);
|
|
1852
|
+
return void 0;
|
|
1853
|
+
}
|
|
1854
|
+
const spec = { type: "resources" };
|
|
1855
|
+
if (host !== void 0) spec.host = host;
|
|
1856
|
+
if (cpu !== void 0) spec.cpu = cpu;
|
|
1857
|
+
if (memory !== void 0) spec.memory = memory;
|
|
1858
|
+
if (swap !== void 0) spec.swap = swap;
|
|
1859
|
+
if (disks !== void 0) spec.disks = disks;
|
|
1860
|
+
if (network !== void 0) spec.network = network;
|
|
1861
|
+
if (load !== void 0) spec.load = load;
|
|
1862
|
+
if (uptimeSec !== void 0) spec.uptimeSec = uptimeSec;
|
|
1863
|
+
if (asOf !== void 0) spec.asOf = asOf;
|
|
1864
|
+
return spec;
|
|
1865
|
+
}
|
|
1866
|
+
var TILE_WORDS = {
|
|
1867
|
+
en: {
|
|
1868
|
+
cores: (n) => `${n} ${n === 1 ? "core" : "cores"}`,
|
|
1869
|
+
days: "days",
|
|
1870
|
+
hours: "hours",
|
|
1871
|
+
minutes: "min"
|
|
1872
|
+
},
|
|
1873
|
+
zh: {
|
|
1874
|
+
cores: (n) => `${n} \u6838`,
|
|
1875
|
+
days: "\u5929",
|
|
1876
|
+
hours: "\u5C0F\u65F6",
|
|
1877
|
+
minutes: "\u5206\u949F"
|
|
1878
|
+
}
|
|
1879
|
+
};
|
|
1880
|
+
var round = (value, digits = 3) => Number(value.toFixed(digits));
|
|
1881
|
+
function usageMetric(usage, unit, marks) {
|
|
1882
|
+
return {
|
|
1883
|
+
value: usage.used,
|
|
1884
|
+
max: usage.total,
|
|
1885
|
+
kind: "used",
|
|
1886
|
+
unit,
|
|
1887
|
+
warn: round(usage.total * marks.warn / 100),
|
|
1888
|
+
danger: round(usage.total * marks.danger / 100)
|
|
1889
|
+
};
|
|
1890
|
+
}
|
|
1891
|
+
var diskMetric = (disk) => usageMetric(disk, disk.unit ?? "GB", MARKS.disk);
|
|
1892
|
+
function resourcesTiles(spec, lang) {
|
|
1893
|
+
const w = TILE_WORDS[lang];
|
|
1894
|
+
const tiles = [];
|
|
1895
|
+
if (spec.cpu !== void 0) {
|
|
1896
|
+
const { percent, history, cores } = spec.cpu;
|
|
1897
|
+
const metric = {
|
|
1898
|
+
value: percent,
|
|
1899
|
+
max: 100,
|
|
1900
|
+
unit: "%",
|
|
1901
|
+
warn: MARKS.share.warn,
|
|
1902
|
+
danger: MARKS.share.danger
|
|
1903
|
+
};
|
|
1904
|
+
if (cores !== void 0) metric.detail = w.cores(cores);
|
|
1905
|
+
if (history !== void 0) metric.history = history;
|
|
1906
|
+
tiles.push({ key: "cpu", metric });
|
|
1907
|
+
}
|
|
1908
|
+
if (spec.memory !== void 0) {
|
|
1909
|
+
const { history, total } = spec.memory;
|
|
1910
|
+
const metric = usageMetric(spec.memory, spec.memory.unit ?? "GB", MARKS.share);
|
|
1911
|
+
if (history !== void 0) metric.history = history.map((p) => round(p * total / 100));
|
|
1912
|
+
tiles.push({ key: "memory", metric });
|
|
1913
|
+
}
|
|
1914
|
+
if (spec.swap !== void 0) {
|
|
1915
|
+
tiles.push({
|
|
1916
|
+
key: "swap",
|
|
1917
|
+
metric: usageMetric(spec.swap, spec.swap.unit ?? "GB", MARKS.share)
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
if (spec.load !== void 0) {
|
|
1921
|
+
const { one, five, fifteen, cores } = spec.load;
|
|
1922
|
+
const averages = [one, five, fifteen].filter((v) => v !== void 0);
|
|
1923
|
+
const detail = [w.cores(cores)];
|
|
1924
|
+
if (averages.length > 1) detail.unshift(averages.map((v) => formatNumber(v, lang)).join(" / "));
|
|
1925
|
+
tiles.push({
|
|
1926
|
+
key: "load",
|
|
1927
|
+
metric: {
|
|
1928
|
+
value: one,
|
|
1929
|
+
max: cores * MARKS.loadPerCore.danger,
|
|
1930
|
+
warn: cores * MARKS.loadPerCore.warn,
|
|
1931
|
+
danger: cores * MARKS.loadPerCore.danger,
|
|
1932
|
+
detail: detail.join(" \xB7 ")
|
|
1933
|
+
}
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
if (spec.uptimeSec !== void 0) {
|
|
1937
|
+
const s = spec.uptimeSec;
|
|
1938
|
+
const metric = s >= 86400 ? { value: round(s / 86400, 1), unit: w.days } : s >= 3600 ? { value: round(s / 3600, 1), unit: w.hours } : { value: Math.floor(s / 60), unit: w.minutes };
|
|
1939
|
+
tiles.push({ key: "uptime", metric });
|
|
1940
|
+
}
|
|
1941
|
+
return tiles;
|
|
1942
|
+
}
|
|
1943
|
+
function toneOf(metric) {
|
|
1944
|
+
const { tone } = resolveMetric({ label: "", ...metric });
|
|
1945
|
+
return tone === "attention" || tone === "danger" ? tone : void 0;
|
|
1946
|
+
}
|
|
1947
|
+
function resolveResources(spec) {
|
|
1948
|
+
const resolved = { disks: [] };
|
|
1949
|
+
for (const tile of resourcesTiles(spec, "en")) {
|
|
1950
|
+
if (tile.key === "uptime") continue;
|
|
1951
|
+
const tone = toneOf(tile.metric);
|
|
1952
|
+
if (tone !== void 0) resolved[tile.key] = tone;
|
|
1953
|
+
}
|
|
1954
|
+
resolved.disks = (spec.disks ?? []).map((disk) => {
|
|
1955
|
+
const unit = disk.unit ?? "GB";
|
|
1956
|
+
const out = {
|
|
1957
|
+
path: disk.path,
|
|
1958
|
+
used: disk.used,
|
|
1959
|
+
total: disk.total,
|
|
1960
|
+
unit,
|
|
1961
|
+
share: disk.used / disk.total
|
|
1962
|
+
};
|
|
1963
|
+
const tone = toneOf(diskMetric(disk));
|
|
1964
|
+
if (tone !== void 0) out.tone = tone;
|
|
1965
|
+
return out;
|
|
1966
|
+
});
|
|
1967
|
+
return resolved;
|
|
1968
|
+
}
|
|
1969
|
+
var LABELS = {
|
|
1970
|
+
en: {
|
|
1971
|
+
cpu: "CPU",
|
|
1972
|
+
memory: "Memory",
|
|
1973
|
+
swap: "Swap",
|
|
1974
|
+
load: "Load",
|
|
1975
|
+
uptime: "Uptime",
|
|
1976
|
+
disk: "Disk",
|
|
1977
|
+
network: "Network",
|
|
1978
|
+
colon: ": ",
|
|
1979
|
+
over: (range, n) => `${range} over ${n} readings`
|
|
1980
|
+
},
|
|
1981
|
+
zh: {
|
|
1982
|
+
cpu: "CPU",
|
|
1983
|
+
memory: "\u5185\u5B58",
|
|
1984
|
+
swap: "\u4EA4\u6362",
|
|
1985
|
+
load: "\u8D1F\u8F7D",
|
|
1986
|
+
uptime: "\u8FD0\u884C\u65F6\u95F4",
|
|
1987
|
+
disk: "\u78C1\u76D8",
|
|
1988
|
+
network: "\u7F51\u7EDC",
|
|
1989
|
+
colon: "\uFF1A",
|
|
1990
|
+
over: (range, n) => `\u8FD1 ${n} \u6B21 ${range}`
|
|
1991
|
+
}
|
|
1992
|
+
};
|
|
1993
|
+
function percentRange(history, lang) {
|
|
1994
|
+
const min = Math.min(...history);
|
|
1995
|
+
const max = Math.max(...history);
|
|
1996
|
+
return min === max ? formatValue(min, "%", lang) : `${formatNumber(min, lang)}\u2013${formatValue(max, "%", lang)}`;
|
|
1997
|
+
}
|
|
1998
|
+
function resourcesMarkdown(spec, lang) {
|
|
1999
|
+
const t = LABELS[lang];
|
|
2000
|
+
const out = [];
|
|
2001
|
+
if (spec.host) out.push(`**${spec.host}**`, "");
|
|
2002
|
+
const tiles = new Map(resourcesTiles(spec, lang).map((tile) => [tile.key, tile.metric]));
|
|
2003
|
+
const line = (key, history) => {
|
|
2004
|
+
const metric = tiles.get(key);
|
|
2005
|
+
if (metric === void 0) return;
|
|
2006
|
+
const item = { label: t[key], ...metric };
|
|
2007
|
+
if (history !== void 0 && history.length > 0) {
|
|
2008
|
+
const own = item.detail ?? metricDetail(item, lang);
|
|
2009
|
+
const range = t.over(percentRange(history, lang), history.length);
|
|
2010
|
+
item.detail = own === void 0 ? range : `${own} \xB7 ${range}`;
|
|
2011
|
+
}
|
|
2012
|
+
out.push(metricLine(item, lang));
|
|
2013
|
+
};
|
|
2014
|
+
line("cpu", spec.cpu?.history);
|
|
2015
|
+
line("memory", spec.memory?.history);
|
|
2016
|
+
line("swap");
|
|
2017
|
+
for (const disk of spec.disks ?? []) {
|
|
2018
|
+
out.push(metricLine({ label: `${t.disk} ${disk.path}`, ...diskMetric(disk) }, lang));
|
|
2019
|
+
}
|
|
2020
|
+
if (spec.network !== void 0) {
|
|
2021
|
+
const { unit, rx, tx } = spec.network;
|
|
2022
|
+
const rate = (values) => formatValue(values.at(-1) ?? 0, unit, lang);
|
|
2023
|
+
out.push(`- ${t.network}${t.colon}\u2193 ${rate(rx)} \u2191 ${rate(tx)}`);
|
|
2024
|
+
}
|
|
2025
|
+
line("load");
|
|
2026
|
+
line("uptime");
|
|
2027
|
+
return out;
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
// src/catalog.ts
|
|
2031
|
+
var CHOICE_FIELDS = ["type", "id", "question", "options", "multiple", "allowOther"];
|
|
2032
|
+
function parseChoice(is, obj) {
|
|
2033
|
+
const id = readString(is, obj, "id", "", {
|
|
2034
|
+
max: 32,
|
|
2035
|
+
pattern: A2UI_LIMITS.idPattern,
|
|
2036
|
+
patternHint: ID_HINT
|
|
2037
|
+
});
|
|
2038
|
+
const question = readString(is, obj, "question", "", {
|
|
2039
|
+
required: true,
|
|
2040
|
+
max: A2UI_LIMITS.question
|
|
2041
|
+
});
|
|
2042
|
+
const options = parseOptions(is, obj, "options", "");
|
|
2043
|
+
const multiple = readBoolean(is, obj, "multiple", "");
|
|
2044
|
+
const allowOther = readBoolean(is, obj, "allowOther", "");
|
|
2045
|
+
unknownFields(is, obj, CHOICE_FIELDS, "", "a choice");
|
|
2046
|
+
if (question === void 0 || options === void 0) return void 0;
|
|
2047
|
+
const spec = { type: "choice", question, options };
|
|
2048
|
+
if (id !== void 0) spec.id = id;
|
|
2049
|
+
if (multiple !== void 0) spec.multiple = multiple;
|
|
2050
|
+
if (allowOther !== void 0) spec.allowOther = allowOther;
|
|
2051
|
+
return spec;
|
|
2052
|
+
}
|
|
2053
|
+
var STEP_FIELDS = ["text", "warning", "caution", "note", "code", "lang"];
|
|
2054
|
+
var STEPS_FIELDS = ["type", "title", "steps"];
|
|
2055
|
+
function parseSteps(is, obj) {
|
|
2056
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.title });
|
|
2057
|
+
const raw = readArray(is, obj, "steps", "", {
|
|
2058
|
+
...A2UI_LIMITS.steps,
|
|
2059
|
+
warnCode: "steps_too_many",
|
|
2060
|
+
what: "steps"
|
|
2061
|
+
});
|
|
2062
|
+
unknownFields(is, obj, STEPS_FIELDS, "", "a steps block");
|
|
2063
|
+
if (raw === void 0) return void 0;
|
|
2064
|
+
const steps = [];
|
|
2065
|
+
raw.forEach((item, i) => {
|
|
2066
|
+
const sp = `steps[${i}]`;
|
|
2067
|
+
if (!isObj(item)) {
|
|
2068
|
+
is.error("wrong_type", `\`${sp}\` must be an object with a \`text\`.`, sp);
|
|
2069
|
+
return;
|
|
2070
|
+
}
|
|
2071
|
+
const text = readString(is, item, "text", sp, { required: true, max: A2UI_LIMITS.stepText });
|
|
2072
|
+
const warning = readString(is, item, "warning", sp, { max: A2UI_LIMITS.stepText });
|
|
2073
|
+
const caution = readString(is, item, "caution", sp, { max: A2UI_LIMITS.stepText });
|
|
2074
|
+
const note = readString(is, item, "note", sp, { max: A2UI_LIMITS.stepText });
|
|
2075
|
+
const code = readString(is, item, "code", sp, { max: A2UI_LIMITS.stepCode });
|
|
2076
|
+
const lang = readString(is, item, "lang", sp, { max: A2UI_LIMITS.lang });
|
|
2077
|
+
if (lang !== void 0 && code === void 0) {
|
|
2078
|
+
is.warn("ignored_field", `\`${sp}.lang\` without \`code\` has no effect.`, `${sp}.lang`);
|
|
2079
|
+
}
|
|
2080
|
+
unknownFields(is, item, STEP_FIELDS, sp, "a step");
|
|
2081
|
+
if (text === void 0) return;
|
|
2082
|
+
const step = { text };
|
|
2083
|
+
if (warning !== void 0) step.warning = warning;
|
|
2084
|
+
if (caution !== void 0) step.caution = caution;
|
|
2085
|
+
if (note !== void 0) step.note = note;
|
|
2086
|
+
if (code !== void 0) step.code = code;
|
|
2087
|
+
if (lang !== void 0) step.lang = lang;
|
|
2088
|
+
steps.push(step);
|
|
2089
|
+
});
|
|
2090
|
+
const spec = { type: "steps", steps };
|
|
2091
|
+
if (title !== void 0) spec.title = title;
|
|
2092
|
+
return spec;
|
|
2093
|
+
}
|
|
2094
|
+
var TONES = ["note", "tip", "caution", "warning"];
|
|
2095
|
+
var CALLOUT_FIELDS = ["type", "tone", "title", "text"];
|
|
2096
|
+
function parseCallout(is, obj) {
|
|
2097
|
+
const tone = readEnum(is, obj, "tone", "", TONES);
|
|
2098
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.title });
|
|
2099
|
+
const text = readString(is, obj, "text", "", { required: true, max: A2UI_LIMITS.calloutText });
|
|
2100
|
+
unknownFields(is, obj, CALLOUT_FIELDS, "", "a callout");
|
|
2101
|
+
if (tone === void 0 || text === void 0) return void 0;
|
|
2102
|
+
const spec = { type: "callout", tone, text };
|
|
2103
|
+
if (title !== void 0) spec.title = title;
|
|
2104
|
+
return spec;
|
|
2105
|
+
}
|
|
2106
|
+
var WEATHER_CONDITIONS = [
|
|
2107
|
+
"clear",
|
|
2108
|
+
"partly-cloudy",
|
|
2109
|
+
"cloudy",
|
|
2110
|
+
"fog",
|
|
2111
|
+
"drizzle",
|
|
2112
|
+
"rain",
|
|
2113
|
+
"heavy-rain",
|
|
2114
|
+
"thunder",
|
|
2115
|
+
"snow",
|
|
2116
|
+
"sleet",
|
|
2117
|
+
"wind"
|
|
2118
|
+
];
|
|
2119
|
+
var TEMP_UNITS = ["C", "F"];
|
|
2120
|
+
var WIND_UNITS = ["km/h", "m/s", "mph"];
|
|
2121
|
+
var HOUR_TIME = /^([01]\d|2[0-3]):[0-5]\d$/;
|
|
2122
|
+
var DAY_DATE = /^\d{4}-\d{2}-\d{2}/;
|
|
2123
|
+
var HOUR_FIELDS = ["time", "temp", "condition", "precip", "night"];
|
|
2124
|
+
var DAY_FIELDS = ["date", "high", "low", "condition", "precip"];
|
|
2125
|
+
var WEATHER_FIELDS = [
|
|
2126
|
+
"type",
|
|
2127
|
+
"place",
|
|
2128
|
+
"condition",
|
|
2129
|
+
"temp",
|
|
2130
|
+
"unit",
|
|
2131
|
+
"night",
|
|
2132
|
+
"summary",
|
|
2133
|
+
"high",
|
|
2134
|
+
"low",
|
|
2135
|
+
"feelsLike",
|
|
2136
|
+
"humidity",
|
|
2137
|
+
"windSpeed",
|
|
2138
|
+
"windUnit",
|
|
2139
|
+
"windDirection",
|
|
2140
|
+
"hourly",
|
|
2141
|
+
"daily",
|
|
2142
|
+
"asOf",
|
|
2143
|
+
"source"
|
|
2144
|
+
];
|
|
2145
|
+
function parseHour(is, item, hp) {
|
|
2146
|
+
if (!isObj(item)) {
|
|
2147
|
+
is.error("wrong_type", `\`${hp}\` must be an object with \`time\` and \`temp\`.`, hp);
|
|
2148
|
+
return void 0;
|
|
2149
|
+
}
|
|
2150
|
+
const time = readString(is, item, "time", hp, { required: true, max: Number.POSITIVE_INFINITY });
|
|
2151
|
+
if (time !== void 0 && !HOUR_TIME.test(time) && !(time.includes("T") && parseA2uiInstant(time) !== null)) {
|
|
2152
|
+
is.error(
|
|
2153
|
+
"invalid_format",
|
|
2154
|
+
`\`${hp}.time\` must be HH:mm on the 24-hour clock, such as 14:00, or an ISO 8601 date-time such as 2026-10-04T14:00+08:00.`,
|
|
2155
|
+
`${hp}.time`
|
|
2156
|
+
);
|
|
2157
|
+
}
|
|
2158
|
+
const temp = readNumber(is, item, "temp", hp, true);
|
|
2159
|
+
const condition = readEnum(is, item, "condition", hp, WEATHER_CONDITIONS, false);
|
|
2160
|
+
const precip = readPercent(is, item, "precip", hp);
|
|
2161
|
+
const night = readBoolean(is, item, "night", hp);
|
|
2162
|
+
unknownFields(is, item, HOUR_FIELDS, hp, "an hour");
|
|
2163
|
+
if (time === void 0 || temp === void 0) return void 0;
|
|
2164
|
+
const hour = { time, temp };
|
|
2165
|
+
if (condition !== void 0) hour.condition = condition;
|
|
2166
|
+
if (precip !== void 0) hour.precip = precip;
|
|
2167
|
+
if (night !== void 0) hour.night = night;
|
|
2168
|
+
return hour;
|
|
2169
|
+
}
|
|
2170
|
+
function parseDay(is, item, dp) {
|
|
2171
|
+
if (!isObj(item)) {
|
|
2172
|
+
is.error(
|
|
2173
|
+
"wrong_type",
|
|
2174
|
+
`\`${dp}\` must be an object with \`date\`, \`high\`, \`low\` and \`condition\`.`,
|
|
2175
|
+
dp
|
|
2176
|
+
);
|
|
2177
|
+
return void 0;
|
|
2178
|
+
}
|
|
2179
|
+
const date = readString(is, item, "date", dp, { required: true, max: Number.POSITIVE_INFINITY });
|
|
2180
|
+
if (date !== void 0 && !(DAY_DATE.test(date) && parseA2uiInstant(date) !== null)) {
|
|
2181
|
+
is.error(
|
|
2182
|
+
"invalid_format",
|
|
2183
|
+
`\`${dp}.date\` must be a date that exists, written YYYY-MM-DD, such as 2026-10-05.`,
|
|
2184
|
+
`${dp}.date`
|
|
2185
|
+
);
|
|
2186
|
+
}
|
|
2187
|
+
const high = readNumber(is, item, "high", dp, true);
|
|
2188
|
+
const low = readNumber(is, item, "low", dp, true);
|
|
2189
|
+
const condition = readEnum(is, item, "condition", dp, WEATHER_CONDITIONS);
|
|
2190
|
+
const precip = readPercent(is, item, "precip", dp);
|
|
2191
|
+
unknownFields(is, item, DAY_FIELDS, dp, "a day");
|
|
2192
|
+
checkHighLow(is, high, low, dp);
|
|
2193
|
+
if (date === void 0 || high === void 0 || low === void 0 || condition === void 0) {
|
|
2194
|
+
return void 0;
|
|
2195
|
+
}
|
|
2196
|
+
const day = { date, high, low, condition };
|
|
2197
|
+
if (precip !== void 0) day.precip = precip;
|
|
2198
|
+
return day;
|
|
2199
|
+
}
|
|
2200
|
+
function parseWeather(is, obj) {
|
|
2201
|
+
const place = readString(is, obj, "place", "", { required: true, max: A2UI_LIMITS.place });
|
|
2202
|
+
const condition = readEnum(is, obj, "condition", "", WEATHER_CONDITIONS);
|
|
2203
|
+
const temp = readNumber(is, obj, "temp", "", true);
|
|
2204
|
+
const unit = readEnum(is, obj, "unit", "", TEMP_UNITS, false);
|
|
2205
|
+
const night = readBoolean(is, obj, "night", "");
|
|
2206
|
+
const summary = readString(is, obj, "summary", "", { max: A2UI_LIMITS.summary });
|
|
2207
|
+
const high = readNumber(is, obj, "high", "");
|
|
2208
|
+
const low = readNumber(is, obj, "low", "");
|
|
2209
|
+
const feelsLike = readNumber(is, obj, "feelsLike", "");
|
|
2210
|
+
const humidity = readPercent(is, obj, "humidity", "");
|
|
2211
|
+
let windSpeed = readNumber(is, obj, "windSpeed", "");
|
|
2212
|
+
if (windSpeed !== void 0 && windSpeed < 0) {
|
|
2213
|
+
is.error(
|
|
2214
|
+
"range_invalid",
|
|
2215
|
+
`\`windSpeed\` is ${windSpeed}; a speed is at least 0 \u2014 put the direction in \`windDirection\`.`,
|
|
2216
|
+
"windSpeed"
|
|
2217
|
+
);
|
|
2218
|
+
windSpeed = void 0;
|
|
2219
|
+
}
|
|
2220
|
+
const windUnit = readEnum(is, obj, "windUnit", "", WIND_UNITS, false);
|
|
2221
|
+
const windDirection = readString(is, obj, "windDirection", "", {
|
|
2222
|
+
max: A2UI_LIMITS.windDirection
|
|
2223
|
+
});
|
|
2224
|
+
if (!present(obj.windSpeed)) {
|
|
2225
|
+
for (const key of ["windUnit", "windDirection"]) {
|
|
2226
|
+
if (!present(obj[key])) continue;
|
|
2227
|
+
is.warn(
|
|
2228
|
+
"ignored_field",
|
|
2229
|
+
`\`${key}\` without \`windSpeed\` has no effect; add the speed or remove it.`,
|
|
2230
|
+
key
|
|
2231
|
+
);
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
let hourly;
|
|
2235
|
+
if (present(obj.hourly)) {
|
|
2236
|
+
const raw = readArray(is, obj, "hourly", "", {
|
|
2237
|
+
...A2UI_LIMITS.hourly,
|
|
2238
|
+
warnCode: "weather_hourly_long",
|
|
2239
|
+
warnHint: "Show the next 12 hours, or one entry every two hours.",
|
|
2240
|
+
what: "hours"
|
|
2241
|
+
});
|
|
2242
|
+
const list = [];
|
|
2243
|
+
raw?.forEach((item, i) => {
|
|
2244
|
+
const hour = parseHour(is, item, `hourly[${i}]`);
|
|
2245
|
+
if (hour !== void 0) list.push(hour);
|
|
2246
|
+
});
|
|
2247
|
+
hourly = list;
|
|
2248
|
+
}
|
|
2249
|
+
let daily;
|
|
2250
|
+
if (present(obj.daily)) {
|
|
2251
|
+
const raw = readArray(is, obj, "daily", "", { ...A2UI_LIMITS.daily, what: "days" });
|
|
2252
|
+
const list = [];
|
|
2253
|
+
raw?.forEach((item, i) => {
|
|
2254
|
+
const day = parseDay(is, item, `daily[${i}]`);
|
|
2255
|
+
if (day !== void 0) list.push(day);
|
|
2256
|
+
});
|
|
2257
|
+
daily = list;
|
|
2258
|
+
}
|
|
2259
|
+
const asOf = readInstant(is, obj, "asOf", "");
|
|
2260
|
+
const source = readString(is, obj, "source", "", { max: A2UI_LIMITS.source });
|
|
2261
|
+
unknownFields(is, obj, WEATHER_FIELDS, "", "a weather block");
|
|
2262
|
+
checkHighLow(is, high, low, "");
|
|
2263
|
+
if (place === void 0 || condition === void 0 || temp === void 0) return void 0;
|
|
2264
|
+
const spec = { type: "weather", place, condition, temp };
|
|
2265
|
+
if (unit !== void 0) spec.unit = unit;
|
|
2266
|
+
if (night !== void 0) spec.night = night;
|
|
2267
|
+
if (summary !== void 0) spec.summary = summary;
|
|
2268
|
+
if (high !== void 0) spec.high = high;
|
|
2269
|
+
if (low !== void 0) spec.low = low;
|
|
2270
|
+
if (feelsLike !== void 0) spec.feelsLike = feelsLike;
|
|
2271
|
+
if (humidity !== void 0) spec.humidity = humidity;
|
|
2272
|
+
if (windSpeed !== void 0) spec.windSpeed = windSpeed;
|
|
2273
|
+
if (windUnit !== void 0) spec.windUnit = windUnit;
|
|
2274
|
+
if (windDirection !== void 0) spec.windDirection = windDirection;
|
|
2275
|
+
if (hourly !== void 0) spec.hourly = hourly;
|
|
2276
|
+
if (daily !== void 0) spec.daily = daily;
|
|
2277
|
+
if (asOf !== void 0) spec.asOf = asOf;
|
|
2278
|
+
if (source !== void 0) spec.source = source;
|
|
2279
|
+
return spec;
|
|
2280
|
+
}
|
|
2281
|
+
var CLOCK_STYLES = ["digital", "analog", "both"];
|
|
2282
|
+
var HOUR_CYCLES = ["12", "24", "auto"];
|
|
2283
|
+
var ZONE_FIELDS = ["zone", "label"];
|
|
2284
|
+
var CLOCK_FIELDS = ["type", "title", "zones", "style", "hourCycle", "seconds", "date"];
|
|
2285
|
+
function canonicalZone(zone) {
|
|
2286
|
+
try {
|
|
2287
|
+
return new Intl.DateTimeFormat("en", { timeZone: zone }).resolvedOptions().timeZone;
|
|
2288
|
+
} catch {
|
|
2289
|
+
return null;
|
|
2290
|
+
}
|
|
2291
|
+
}
|
|
2292
|
+
function parseZones(is, obj) {
|
|
2293
|
+
const raw = readArray(is, obj, "zones", "", { ...A2UI_LIMITS.zones, what: "zones" });
|
|
2294
|
+
if (raw === void 0) return void 0;
|
|
2295
|
+
const zones = [];
|
|
2296
|
+
const seen = /* @__PURE__ */ new Map();
|
|
2297
|
+
raw.forEach((item, i) => {
|
|
2298
|
+
const zp = `zones[${i}]`;
|
|
2299
|
+
if (!isObj(item)) {
|
|
2300
|
+
is.error("wrong_type", `\`${zp}\` must be an object with a \`zone\`.`, zp);
|
|
2301
|
+
return;
|
|
2302
|
+
}
|
|
2303
|
+
const zone = readString(is, item, "zone", zp, { required: true, max: A2UI_LIMITS.zone });
|
|
2304
|
+
const label = readString(is, item, "label", zp, { max: A2UI_LIMITS.zoneLabel });
|
|
2305
|
+
unknownFields(is, item, ZONE_FIELDS, zp, "a clock zone");
|
|
2306
|
+
if (zone === void 0) return;
|
|
2307
|
+
const key = zone === "local" ? zone : canonicalZone(zone);
|
|
2308
|
+
if (key === null) {
|
|
2309
|
+
is.error(
|
|
2310
|
+
"invalid_timezone",
|
|
2311
|
+
`\`${zp}.zone\` "${zone}" is not a time zone; use an IANA name such as Asia/Shanghai, or local.`,
|
|
2312
|
+
`${zp}.zone`
|
|
2313
|
+
);
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
2316
|
+
const first = seen.get(key);
|
|
2317
|
+
if (first !== void 0) {
|
|
2318
|
+
is.error(
|
|
2319
|
+
"duplicate_zone",
|
|
2320
|
+
`\`${zp}.zone\` repeats zone ${first + 1} ("${zone}"); list each zone once.`,
|
|
2321
|
+
`${zp}.zone`
|
|
2322
|
+
);
|
|
2323
|
+
} else {
|
|
2324
|
+
seen.set(key, i);
|
|
2325
|
+
}
|
|
2326
|
+
const entry = { zone };
|
|
2327
|
+
if (label !== void 0) entry.label = label;
|
|
2328
|
+
zones.push(entry);
|
|
2329
|
+
});
|
|
2330
|
+
return zones;
|
|
2331
|
+
}
|
|
2332
|
+
function parseClock(is, obj) {
|
|
2333
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.clockTitle });
|
|
2334
|
+
const zones = present(obj.zones) ? parseZones(is, obj) : void 0;
|
|
2335
|
+
const style = readEnum(is, obj, "style", "", CLOCK_STYLES, false);
|
|
2336
|
+
const hourCycle = readEnum(is, obj, "hourCycle", "", HOUR_CYCLES, false);
|
|
2337
|
+
const seconds = readBoolean(is, obj, "seconds", "");
|
|
2338
|
+
const date = readBoolean(is, obj, "date", "");
|
|
2339
|
+
unknownFields(is, obj, CLOCK_FIELDS, "", "a clock");
|
|
2340
|
+
const spec = { type: "clock" };
|
|
2341
|
+
if (title !== void 0) spec.title = title;
|
|
2342
|
+
if (zones !== void 0) spec.zones = zones;
|
|
2343
|
+
if (style !== void 0) spec.style = style;
|
|
2344
|
+
if (hourCycle !== void 0) spec.hourCycle = hourCycle;
|
|
2345
|
+
if (seconds !== void 0) spec.seconds = seconds;
|
|
2346
|
+
if (date !== void 0) spec.date = date;
|
|
2347
|
+
return spec;
|
|
2348
|
+
}
|
|
2349
|
+
var COUNTDOWN_FIELDS = ["type", "to", "label", "doneLabel", "showTarget"];
|
|
2350
|
+
function parseCountdown(is, obj) {
|
|
2351
|
+
const to = readInstant(is, obj, "to", "", true);
|
|
2352
|
+
const label = readString(is, obj, "label", "", {
|
|
2353
|
+
required: true,
|
|
2354
|
+
max: A2UI_LIMITS.countdownLabel
|
|
2355
|
+
});
|
|
2356
|
+
const doneLabel = readString(is, obj, "doneLabel", "", { max: A2UI_LIMITS.countdownLabel });
|
|
2357
|
+
const showTarget = readBoolean(is, obj, "showTarget", "");
|
|
2358
|
+
unknownFields(is, obj, COUNTDOWN_FIELDS, "", "a countdown");
|
|
2359
|
+
if (to === void 0 || label === void 0) return void 0;
|
|
2360
|
+
const spec = { type: "countdown", to, label };
|
|
2361
|
+
if (doneLabel !== void 0) spec.doneLabel = doneLabel;
|
|
2362
|
+
if (showTarget !== void 0) spec.showTarget = showTarget;
|
|
2363
|
+
return spec;
|
|
2364
|
+
}
|
|
2365
|
+
var METRICS_FIELDS = ["type", "title", "items", "asOf"];
|
|
2366
|
+
function parseMetrics(is, obj) {
|
|
2367
|
+
const title = readString(is, obj, "title", "", { max: A2UI_LIMITS.metricsTitle });
|
|
2368
|
+
const raw = readArray(is, obj, "items", "", {
|
|
2369
|
+
...A2UI_LIMITS.metrics,
|
|
2370
|
+
warnCode: "metrics_too_many",
|
|
2371
|
+
warnHint: "Keep the readings that matter and say the rest in a sentence.",
|
|
2372
|
+
what: "items"
|
|
2373
|
+
});
|
|
2374
|
+
const asOf = readInstant(is, obj, "asOf", "");
|
|
2375
|
+
unknownFields(is, obj, METRICS_FIELDS, "", "a metrics block");
|
|
2376
|
+
if (raw === void 0) return void 0;
|
|
2377
|
+
const items = [];
|
|
2378
|
+
const labels = /* @__PURE__ */ new Map();
|
|
2379
|
+
raw.forEach((item, i) => {
|
|
2380
|
+
const metric = parseMetric(is, item, `items[${i}]`);
|
|
2381
|
+
if (metric === void 0) return;
|
|
2382
|
+
const seen = labels.get(metric.label);
|
|
2383
|
+
if (seen !== void 0) {
|
|
2384
|
+
is.error(
|
|
2385
|
+
"duplicate_label",
|
|
2386
|
+
`\`items[${i}].label\` repeats item ${seen + 1}'s label "${metric.label}"; labels must be unique.`,
|
|
2387
|
+
`items[${i}].label`
|
|
2388
|
+
);
|
|
2389
|
+
} else {
|
|
2390
|
+
labels.set(metric.label, i);
|
|
2391
|
+
}
|
|
2392
|
+
items.push(metric);
|
|
2393
|
+
});
|
|
2394
|
+
const spec = { type: "metrics", items };
|
|
2395
|
+
if (title !== void 0) spec.title = title;
|
|
2396
|
+
if (asOf !== void 0) spec.asOf = asOf;
|
|
2397
|
+
return spec;
|
|
2398
|
+
}
|
|
2399
|
+
function stripTrailingCommas(text) {
|
|
2400
|
+
let out = "";
|
|
2401
|
+
let inString = false;
|
|
2402
|
+
let changed = false;
|
|
2403
|
+
for (let i = 0; i < text.length; i++) {
|
|
2404
|
+
const ch = text[i] ?? "";
|
|
2405
|
+
if (inString) {
|
|
2406
|
+
out += ch;
|
|
2407
|
+
if (ch === "\\") {
|
|
2408
|
+
out += text[i + 1] ?? "";
|
|
2409
|
+
i++;
|
|
2410
|
+
} else if (ch === '"') {
|
|
2411
|
+
inString = false;
|
|
2412
|
+
}
|
|
2413
|
+
continue;
|
|
2414
|
+
}
|
|
2415
|
+
if (ch === '"') {
|
|
2416
|
+
inString = true;
|
|
2417
|
+
out += ch;
|
|
2418
|
+
continue;
|
|
2419
|
+
}
|
|
2420
|
+
if (ch === ",") {
|
|
2421
|
+
let j = i + 1;
|
|
2422
|
+
while (j < text.length && /\s/.test(text[j] ?? "")) j++;
|
|
2423
|
+
const next = text[j];
|
|
2424
|
+
if (next === "}" || next === "]") {
|
|
2425
|
+
changed = true;
|
|
2426
|
+
continue;
|
|
2427
|
+
}
|
|
2428
|
+
}
|
|
2429
|
+
out += ch;
|
|
2430
|
+
}
|
|
2431
|
+
return { text: out, changed };
|
|
2432
|
+
}
|
|
2433
|
+
function errorLine(err, text) {
|
|
2434
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2435
|
+
const m = /position (\d+)/.exec(message);
|
|
2436
|
+
if (!m) return void 0;
|
|
2437
|
+
const pos = Math.min(Number(m[1]), text.length);
|
|
2438
|
+
return text.slice(0, pos).split("\n").length;
|
|
2439
|
+
}
|
|
2440
|
+
function parseA2ui(source) {
|
|
2441
|
+
const is = new Issues();
|
|
2442
|
+
const text = source.replace(/\r\n?/g, "\n");
|
|
2443
|
+
if (text.trim() === "") {
|
|
2444
|
+
is.error("empty_block", "The a2ui fence is empty; it must hold one JSON object.");
|
|
2445
|
+
return { issues: is.list };
|
|
2446
|
+
}
|
|
2447
|
+
let data;
|
|
2448
|
+
try {
|
|
2449
|
+
data = JSON.parse(text);
|
|
2450
|
+
} catch (err) {
|
|
2451
|
+
const lenient = stripTrailingCommas(text);
|
|
2452
|
+
let recovered = false;
|
|
2453
|
+
if (lenient.changed) {
|
|
2454
|
+
try {
|
|
2455
|
+
data = JSON.parse(lenient.text);
|
|
2456
|
+
recovered = true;
|
|
2457
|
+
} catch {
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
if (!recovered) {
|
|
2461
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
2462
|
+
is.error(
|
|
2463
|
+
"invalid_json",
|
|
2464
|
+
`The block is not valid JSON (${detail}). Write one strict JSON object: double quotes, no comments, no trailing commas.`,
|
|
2465
|
+
void 0,
|
|
2466
|
+
errorLine(err, text)
|
|
2467
|
+
);
|
|
2468
|
+
return { issues: is.list };
|
|
2469
|
+
}
|
|
2470
|
+
is.warn(
|
|
2471
|
+
"json_trailing_comma",
|
|
2472
|
+
"A trailing comma was tolerated this time; strict JSON has none."
|
|
2473
|
+
);
|
|
2474
|
+
}
|
|
2475
|
+
if (!isObj(data)) {
|
|
2476
|
+
is.error(
|
|
2477
|
+
"not_object",
|
|
2478
|
+
"The block must be ONE JSON object \u2014 not an array, a string, or several objects."
|
|
2479
|
+
);
|
|
2480
|
+
return { issues: is.list };
|
|
2481
|
+
}
|
|
2482
|
+
const type = data.type;
|
|
2483
|
+
if (typeof type !== "string") {
|
|
2484
|
+
is.error(
|
|
2485
|
+
"missing_type",
|
|
2486
|
+
"`type` is required: one of choice, form, steps, callout, weather, clock, countdown, metrics, chart, resources.",
|
|
2487
|
+
"type"
|
|
2488
|
+
);
|
|
2489
|
+
return { issues: is.list };
|
|
2490
|
+
}
|
|
2491
|
+
let spec;
|
|
2492
|
+
switch (type) {
|
|
2493
|
+
case "choice":
|
|
2494
|
+
spec = parseChoice(is, data);
|
|
2495
|
+
break;
|
|
2496
|
+
case "form":
|
|
2497
|
+
spec = parseForm(is, data);
|
|
2498
|
+
break;
|
|
2499
|
+
case "steps":
|
|
2500
|
+
spec = parseSteps(is, data);
|
|
2501
|
+
break;
|
|
2502
|
+
case "callout":
|
|
2503
|
+
spec = parseCallout(is, data);
|
|
2504
|
+
break;
|
|
2505
|
+
case "weather":
|
|
2506
|
+
spec = parseWeather(is, data);
|
|
2507
|
+
break;
|
|
2508
|
+
case "clock":
|
|
2509
|
+
spec = parseClock(is, data);
|
|
2510
|
+
break;
|
|
2511
|
+
case "countdown":
|
|
2512
|
+
spec = parseCountdown(is, data);
|
|
2513
|
+
break;
|
|
2514
|
+
case "metrics":
|
|
2515
|
+
spec = parseMetrics(is, data);
|
|
2516
|
+
break;
|
|
2517
|
+
case "chart":
|
|
2518
|
+
spec = parseChart(is, data);
|
|
2519
|
+
break;
|
|
2520
|
+
case "resources":
|
|
2521
|
+
spec = parseResources(is, data);
|
|
2522
|
+
break;
|
|
2523
|
+
default:
|
|
2524
|
+
is.error(
|
|
2525
|
+
"unknown_type",
|
|
2526
|
+
`Unknown type "${type}"; the catalog has choice, form, steps, callout, weather, clock, countdown, metrics, chart and resources.`,
|
|
2527
|
+
"type"
|
|
2528
|
+
);
|
|
2529
|
+
return { issues: is.list, type };
|
|
2530
|
+
}
|
|
2531
|
+
const result = { issues: is.list, type };
|
|
2532
|
+
if (spec !== void 0 && !is.hasError) result.spec = spec;
|
|
2533
|
+
return result;
|
|
2534
|
+
}
|
|
2535
|
+
|
|
2536
|
+
// src/mermaid.ts
|
|
2537
|
+
var MERMAID_TYPES = [
|
|
2538
|
+
"flowchart",
|
|
2539
|
+
"graph",
|
|
2540
|
+
"sequenceDiagram",
|
|
2541
|
+
"stateDiagram",
|
|
2542
|
+
"stateDiagram-v2",
|
|
2543
|
+
"classDiagram",
|
|
2544
|
+
"erDiagram",
|
|
2545
|
+
"gantt",
|
|
2546
|
+
"journey",
|
|
2547
|
+
"pie",
|
|
2548
|
+
"mindmap",
|
|
2549
|
+
"timeline",
|
|
2550
|
+
"gitGraph",
|
|
2551
|
+
"quadrantChart",
|
|
2552
|
+
"xychart-beta",
|
|
2553
|
+
"requirementDiagram",
|
|
2554
|
+
"C4Context"
|
|
2555
|
+
];
|
|
2556
|
+
var MERMAID_TYPE_SET = new Set(MERMAID_TYPES);
|
|
2557
|
+
var BRACKET_CHECKED = /* @__PURE__ */ new Set([
|
|
2558
|
+
"flowchart",
|
|
2559
|
+
"graph",
|
|
2560
|
+
"stateDiagram",
|
|
2561
|
+
"stateDiagram-v2",
|
|
2562
|
+
"classDiagram",
|
|
2563
|
+
"erDiagram"
|
|
2564
|
+
]);
|
|
2565
|
+
var BRACES_PER_LINE = /* @__PURE__ */ new Set(["flowchart", "graph"]);
|
|
2566
|
+
var BRACES_PER_DOCUMENT = /* @__PURE__ */ new Set([
|
|
2567
|
+
"classDiagram",
|
|
2568
|
+
"stateDiagram",
|
|
2569
|
+
"stateDiagram-v2"
|
|
2570
|
+
]);
|
|
2571
|
+
var FORBIDDEN = [
|
|
2572
|
+
{ re: /^click\s/i, what: "a click binding" },
|
|
2573
|
+
{ re: /^link\s+\S+\s+"/, what: "a link binding" },
|
|
2574
|
+
{ re: /^callback\s/i, what: "a callback binding" },
|
|
2575
|
+
{ re: /\bhref\s*[="]/i, what: "an href" },
|
|
2576
|
+
{ re: /javascript:/i, what: "a javascript: URL" },
|
|
2577
|
+
{ re: /<script/i, what: "a script tag" }
|
|
2578
|
+
];
|
|
2579
|
+
var NOT_A_STATEMENT = /^(end|subgraph\b|style\b|classDef\b|linkStyle\b|direction\b|title\b|dateFormat\b|axisFormat\b|section\b|x-axis\b|y-axis\b|accTitle\b|accDescr\b)/;
|
|
2580
|
+
var issue = (level, code, message, line) => {
|
|
2581
|
+
const out = { level, code, message };
|
|
2582
|
+
if (line !== void 0) out.line = line;
|
|
2583
|
+
return out;
|
|
2584
|
+
};
|
|
2585
|
+
function unquoted(line) {
|
|
2586
|
+
let text = "";
|
|
2587
|
+
let inside = false;
|
|
2588
|
+
let quotes = 0;
|
|
2589
|
+
for (const ch of line) {
|
|
2590
|
+
if (ch === '"') {
|
|
2591
|
+
quotes++;
|
|
2592
|
+
inside = !inside;
|
|
2593
|
+
continue;
|
|
2594
|
+
}
|
|
2595
|
+
if (!inside) text += ch;
|
|
2596
|
+
}
|
|
2597
|
+
return { text, quotes };
|
|
2598
|
+
}
|
|
2599
|
+
var count = (text, ch) => text.split(ch).length - 1;
|
|
2600
|
+
function checkLineBalance(line, n, braces, issues) {
|
|
2601
|
+
const { text, quotes } = unquoted(line);
|
|
2602
|
+
if (quotes % 2 === 1) {
|
|
2603
|
+
issues.push(
|
|
2604
|
+
issue(
|
|
2605
|
+
"error",
|
|
2606
|
+
"mermaid_unbalanced",
|
|
2607
|
+
`Line ${n} has an odd number of double quotes; close the quoted label.`,
|
|
2608
|
+
n
|
|
2609
|
+
)
|
|
2610
|
+
);
|
|
2611
|
+
return { text };
|
|
2612
|
+
}
|
|
2613
|
+
const pairs = [
|
|
2614
|
+
["(", ")"],
|
|
2615
|
+
["[", "]"]
|
|
2616
|
+
];
|
|
2617
|
+
if (braces) pairs.push(["{", "}"]);
|
|
2618
|
+
for (const [open, close] of pairs) {
|
|
2619
|
+
const opens = count(text, open);
|
|
2620
|
+
const closes = count(text, close);
|
|
2621
|
+
if (opens === closes) continue;
|
|
2622
|
+
if (close === "]" && closes - opens === 1 && text.includes(">")) continue;
|
|
2623
|
+
issues.push(
|
|
2624
|
+
issue(
|
|
2625
|
+
"error",
|
|
2626
|
+
"mermaid_unbalanced",
|
|
2627
|
+
`Line ${n} has ${opens} "${open}" and ${closes} "${close}"; a label with brackets must be quoted, e.g. A["text (detail)"].`,
|
|
2628
|
+
n
|
|
2629
|
+
)
|
|
2630
|
+
);
|
|
2631
|
+
}
|
|
2632
|
+
return { text };
|
|
2633
|
+
}
|
|
2634
|
+
function checkMermaid(source) {
|
|
2635
|
+
const issues = [];
|
|
2636
|
+
const lines = splitLines(source);
|
|
2637
|
+
const firstIdx = lines.findIndex((line) => line.trim() !== "");
|
|
2638
|
+
if (firstIdx === -1) {
|
|
2639
|
+
issues.push(issue("error", "mermaid_empty", "The mermaid fence is empty; write a diagram."));
|
|
2640
|
+
return issues;
|
|
2641
|
+
}
|
|
2642
|
+
let start = 0;
|
|
2643
|
+
if ((lines[firstIdx] ?? "").trim() === "---") {
|
|
2644
|
+
const close = lines.findIndex((line, i) => i > firstIdx && line.trim() === "---");
|
|
2645
|
+
if (close === -1) {
|
|
2646
|
+
issues.push(
|
|
2647
|
+
issue(
|
|
2648
|
+
"error",
|
|
2649
|
+
"mermaid_unbalanced",
|
|
2650
|
+
"The frontmatter opened with --- is never closed; add the closing --- or drop it.",
|
|
2651
|
+
firstIdx + 1
|
|
2652
|
+
)
|
|
2653
|
+
);
|
|
2654
|
+
return issues;
|
|
2655
|
+
}
|
|
2656
|
+
for (let i = firstIdx + 1; i < close; i++) {
|
|
2657
|
+
const m = /^\s*([A-Za-z_][\w-]*)\s*:/.exec(lines[i] ?? "");
|
|
2658
|
+
if (m && m[1] !== "title") {
|
|
2659
|
+
issues.push(
|
|
2660
|
+
issue(
|
|
2661
|
+
"error",
|
|
2662
|
+
"mermaid_directive",
|
|
2663
|
+
`Frontmatter key \`${m[1]}\` is not allowed (only \`title\`); the app configures the diagram, not the model.`,
|
|
2664
|
+
i + 1
|
|
2665
|
+
)
|
|
2666
|
+
);
|
|
2667
|
+
}
|
|
2668
|
+
}
|
|
2669
|
+
start = close + 1;
|
|
2670
|
+
}
|
|
2671
|
+
let header;
|
|
2672
|
+
let statements = 0;
|
|
2673
|
+
let braceBalance = 0;
|
|
2674
|
+
for (let i = start; i < lines.length; i++) {
|
|
2675
|
+
const n = i + 1;
|
|
2676
|
+
const line = (lines[i] ?? "").trim();
|
|
2677
|
+
if (line === "") continue;
|
|
2678
|
+
if (line.includes("%%{")) {
|
|
2679
|
+
issues.push(
|
|
2680
|
+
issue(
|
|
2681
|
+
"error",
|
|
2682
|
+
"mermaid_directive",
|
|
2683
|
+
`Line ${n} carries an init directive (%%{ \u2026 }%%); remove it \u2014 the app sets the theme.`,
|
|
2684
|
+
n
|
|
2685
|
+
)
|
|
2686
|
+
);
|
|
2687
|
+
continue;
|
|
2688
|
+
}
|
|
2689
|
+
if (line.startsWith("%%")) continue;
|
|
2690
|
+
if (header === void 0) {
|
|
2691
|
+
header = /^([A-Za-z][\w-]*)/.exec(line)?.[1] ?? line;
|
|
2692
|
+
if (!MERMAID_TYPE_SET.has(header)) {
|
|
2693
|
+
issues.push(
|
|
2694
|
+
issue(
|
|
2695
|
+
"error",
|
|
2696
|
+
"mermaid_unknown_type",
|
|
2697
|
+
`"${header}" is not a supported diagram; start with one of ${MERMAID_TYPES.join(", ")}.`,
|
|
2698
|
+
n
|
|
2699
|
+
)
|
|
2700
|
+
);
|
|
2701
|
+
}
|
|
2702
|
+
continue;
|
|
2703
|
+
}
|
|
2704
|
+
for (const rule of FORBIDDEN) {
|
|
2705
|
+
if (rule.re.test(line)) {
|
|
2706
|
+
issues.push(
|
|
2707
|
+
issue(
|
|
2708
|
+
"error",
|
|
2709
|
+
"mermaid_forbidden",
|
|
2710
|
+
`Line ${n} carries ${rule.what}; links, clicks and scripts are not rendered \u2014 describe the target in the prose instead.`,
|
|
2711
|
+
n
|
|
2712
|
+
)
|
|
2713
|
+
);
|
|
2714
|
+
break;
|
|
2715
|
+
}
|
|
2716
|
+
}
|
|
2717
|
+
if (!NOT_A_STATEMENT.test(line)) statements++;
|
|
2718
|
+
if (BRACKET_CHECKED.has(header)) {
|
|
2719
|
+
const { text } = checkLineBalance(line, n, BRACES_PER_LINE.has(header), issues);
|
|
2720
|
+
if (BRACES_PER_DOCUMENT.has(header)) braceBalance += count(text, "{") - count(text, "}");
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
if (header === void 0) {
|
|
2724
|
+
issues.push(
|
|
2725
|
+
issue("error", "mermaid_empty", "The mermaid fence has no diagram header; write one.")
|
|
2726
|
+
);
|
|
2727
|
+
return issues;
|
|
2728
|
+
}
|
|
2729
|
+
if (braceBalance !== 0) {
|
|
2730
|
+
issues.push(
|
|
2731
|
+
issue(
|
|
2732
|
+
"error",
|
|
2733
|
+
"mermaid_unbalanced",
|
|
2734
|
+
`The diagram opens ${braceBalance > 0 ? braceBalance + " more { than }" : -braceBalance + " more } than {"}; close every block.`,
|
|
2735
|
+
lines.length
|
|
2736
|
+
)
|
|
2737
|
+
);
|
|
2738
|
+
}
|
|
2739
|
+
if (statements > A2UI_LIMITS.mermaid.statements || lines.length > A2UI_LIMITS.mermaid.lines) {
|
|
2740
|
+
issues.push(
|
|
2741
|
+
issue(
|
|
2742
|
+
"warning",
|
|
2743
|
+
"mermaid_too_big",
|
|
2744
|
+
`The diagram has ${statements} statements over ${lines.length} lines; more than ${A2UI_LIMITS.mermaid.statements} nodes or edges is hard to read \u2014 split it or show only the main path.`
|
|
2745
|
+
)
|
|
2746
|
+
);
|
|
2747
|
+
}
|
|
2748
|
+
return issues;
|
|
2749
|
+
}
|
|
2750
|
+
|
|
2751
|
+
// src/lang.ts
|
|
2752
|
+
var CJK = new RegExp("\\p{Script=Han}|\\p{Script=Hiragana}|\\p{Script=Katakana}|\\p{Script=Hangul}", "gu");
|
|
2753
|
+
var LATIN = new RegExp("\\p{Script=Latin}", "gu");
|
|
2754
|
+
function detectLang(markdown) {
|
|
2755
|
+
const fenced = fenceLineSet(scanFences(markdown));
|
|
2756
|
+
const text = splitLines(markdown).filter((_, i) => !fenced.has(i + 1)).join(" ").replace(/`+[^`]*`+/g, " ").replace(/https?:\/\/\S+/g, " ");
|
|
2757
|
+
const cjk = text.match(CJK)?.length ?? 0;
|
|
2758
|
+
const latin = text.match(LATIN)?.length ?? 0;
|
|
2759
|
+
return cjk > 0 && cjk * 2 >= latin ? "zh" : "en";
|
|
2760
|
+
}
|
|
2761
|
+
function resolveLang(markdown, lang) {
|
|
2762
|
+
return lang !== void 0 && lang !== "auto" ? lang : detectLang(markdown);
|
|
2763
|
+
}
|
|
2764
|
+
|
|
2765
|
+
// src/check.ts
|
|
2766
|
+
var HEADING = /^\s{0,3}#{1,6}(\s|$)/;
|
|
2767
|
+
var TABLE_ROW = /^\s*\|/;
|
|
2768
|
+
var THEMATIC_BREAK = /^\s{0,3}([-*_])(\s*\1){2,}\s*$/;
|
|
2769
|
+
function checkReply(markdown, opts = {}) {
|
|
2770
|
+
const lang = resolveLang(markdown, opts.lang);
|
|
2771
|
+
const lines = splitLines(markdown);
|
|
2772
|
+
const spans = scanFences(markdown);
|
|
2773
|
+
const blocks = blocksFromFences(spans);
|
|
2774
|
+
const fenced = fenceLineSet(spans);
|
|
2775
|
+
const issues = [];
|
|
2776
|
+
const summary = [];
|
|
2777
|
+
const typeOf = /* @__PURE__ */ new Map();
|
|
2778
|
+
const specOf = /* @__PURE__ */ new Map();
|
|
2779
|
+
for (const block of blocks) {
|
|
2780
|
+
const own = [];
|
|
2781
|
+
let type;
|
|
2782
|
+
if (!block.closed) {
|
|
2783
|
+
own.push({
|
|
2784
|
+
level: "error",
|
|
2785
|
+
code: "unclosed_fence",
|
|
2786
|
+
message: `The ${block.fence} fence opened on line ${block.startLine} is never closed; add the closing fence line.`,
|
|
2787
|
+
line: block.startLine
|
|
2788
|
+
});
|
|
2789
|
+
}
|
|
2790
|
+
let relative;
|
|
2791
|
+
if (block.fence === "a2ui") {
|
|
2792
|
+
const result = parseA2ui(block.source);
|
|
2793
|
+
type = result.spec?.type ?? result.type;
|
|
2794
|
+
relative = result.issues;
|
|
2795
|
+
if (result.spec !== void 0) specOf.set(block.index, result.spec);
|
|
2796
|
+
} else {
|
|
2797
|
+
relative = checkMermaid(block.source);
|
|
2798
|
+
}
|
|
2799
|
+
for (const issue2 of relative) {
|
|
2800
|
+
own.push(issue2.line !== void 0 ? { ...issue2, line: block.startLine + issue2.line } : issue2);
|
|
2801
|
+
}
|
|
2802
|
+
for (const issue2 of own) {
|
|
2803
|
+
issues.push({ ...issue2, block: block.index, scope: issue2.level === "error" ? "l1" : "l2" });
|
|
2804
|
+
}
|
|
2805
|
+
const entry = {
|
|
2806
|
+
index: block.index,
|
|
2807
|
+
fence: block.fence,
|
|
2808
|
+
ok: own.every((issue2) => issue2.level !== "error")
|
|
2809
|
+
};
|
|
2810
|
+
if (type !== void 0) entry.type = type;
|
|
2811
|
+
summary.push(entry);
|
|
2812
|
+
typeOf.set(block.index, type);
|
|
2813
|
+
}
|
|
2814
|
+
const l2 = (code, message, block, line) => {
|
|
2815
|
+
const issue2 = { level: "warning", code, message, scope: "l2" };
|
|
2816
|
+
if (block !== void 0) issue2.block = block;
|
|
2817
|
+
if (line !== void 0) issue2.line = line;
|
|
2818
|
+
issues.push(issue2);
|
|
2819
|
+
};
|
|
2820
|
+
const label = (index, fence) => fence === "mermaid" ? "mermaid" : typeOf.get(index) ?? "a2ui";
|
|
2821
|
+
const interactive = blocks.filter((b) => {
|
|
2822
|
+
const t = typeOf.get(b.index);
|
|
2823
|
+
return b.fence === "a2ui" && (t === "choice" || t === "form");
|
|
2824
|
+
});
|
|
2825
|
+
if (interactive.length > 2 || blocks.length > 4) {
|
|
2826
|
+
l2(
|
|
2827
|
+
"ui_overuse",
|
|
2828
|
+
`${blocks.length} blocks (${interactive.length} interactive) in one reply; keep to one or two blocks and at most two questions, or answer in prose.`
|
|
2829
|
+
);
|
|
2830
|
+
}
|
|
2831
|
+
for (const b of interactive) {
|
|
2832
|
+
if (lines.slice(b.endLine).some((line) => line.trim() !== "")) {
|
|
2833
|
+
l2(
|
|
2834
|
+
"interactive_not_last",
|
|
2835
|
+
`Content follows the ${label(b.index, b.fence)} block that ends on line ${b.endLine}; a question ends the turn \u2014 move the block to the end, or drop what follows.`,
|
|
2836
|
+
b.index,
|
|
2837
|
+
b.endLine
|
|
2838
|
+
);
|
|
2839
|
+
}
|
|
2840
|
+
}
|
|
2841
|
+
for (const b of blocks) {
|
|
2842
|
+
const spec = specOf.get(b.index);
|
|
2843
|
+
if (spec === void 0) continue;
|
|
2844
|
+
if ((spec.type === "weather" || spec.type === "metrics" || spec.type === "resources") && spec.asOf === void 0) {
|
|
2845
|
+
l2(
|
|
2846
|
+
"no_as_of",
|
|
2847
|
+
`The ${spec.type} block on line ${b.startLine} has no \`asOf\`; a snapshot says when it was taken \u2014 add the time the data was read (a script prints it).`,
|
|
2848
|
+
b.index,
|
|
2849
|
+
b.startLine
|
|
2850
|
+
);
|
|
2851
|
+
}
|
|
2852
|
+
}
|
|
2853
|
+
for (const b of blocks) {
|
|
2854
|
+
let i = b.startLine - 2;
|
|
2855
|
+
while (i >= 0 && (lines[i] ?? "").trim() === "") i--;
|
|
2856
|
+
const above = i >= 0 ? lines[i] ?? "" : "";
|
|
2857
|
+
const grounded = i >= 0 && !fenced.has(i + 1) && !HEADING.test(above) && !TABLE_ROW.test(above) && !THEMATIC_BREAK.test(above) && !above.trimStart().startsWith("<!--");
|
|
2858
|
+
if (!grounded) {
|
|
2859
|
+
l2(
|
|
2860
|
+
"ungrounded_block",
|
|
2861
|
+
`The ${label(b.index, b.fence)} block on line ${b.startLine} is not introduced; write one sentence before it that says what it is for.`,
|
|
2862
|
+
b.index,
|
|
2863
|
+
b.startLine
|
|
2864
|
+
);
|
|
2865
|
+
}
|
|
2866
|
+
}
|
|
2867
|
+
const errors = issues.filter((issue2) => issue2.level === "error").length;
|
|
2868
|
+
const warnings = issues.filter((issue2) => issue2.level === "warning").length;
|
|
2869
|
+
const l1Score = errors === 0 ? 100 : 0;
|
|
2870
|
+
const l2Score = Math.max(0, 100 - A2UI_SCORING.l2PerWarning * warnings);
|
|
2871
|
+
return {
|
|
2872
|
+
ok: errors === 0,
|
|
2873
|
+
blocks: summary,
|
|
2874
|
+
issues,
|
|
2875
|
+
score: { l1: l1Score, l2: l2Score, total: errors > 0 ? 0 : l2Score },
|
|
2876
|
+
lang
|
|
2877
|
+
};
|
|
2878
|
+
}
|
|
2879
|
+
|
|
2880
|
+
// src/fallback.ts
|
|
2881
|
+
var STR = {
|
|
2882
|
+
zh: {
|
|
2883
|
+
recommended: "\u63A8\u8350",
|
|
2884
|
+
multiple: "\u53EF\u591A\u9009\u3002",
|
|
2885
|
+
replyChoice: "\u56DE\u590D\u7F16\u53F7\u6216\u76F4\u63A5\u5199\u51FA\u4F60\u7684\u7B54\u6848\u3002",
|
|
2886
|
+
replyForm: "\u6309\u5B57\u6BB5\u9010\u884C\u56DE\u590D\u3002",
|
|
2887
|
+
required: "\u5FC5\u586B",
|
|
2888
|
+
yes: "\u662F",
|
|
2889
|
+
no: "\u5426",
|
|
2890
|
+
yesNo: "\u662F / \u5426",
|
|
2891
|
+
step: "\u6B65\u957F",
|
|
2892
|
+
byDefault: (value) => `\uFF08\u9ED8\u8BA4 ${value}\uFF09`,
|
|
2893
|
+
colon: "\uFF1A",
|
|
2894
|
+
sep: "\u3001",
|
|
2895
|
+
stepWarning: "\u8B66\u544A\uFF1A",
|
|
2896
|
+
stepCaution: "\u6CE8\u610F\uFF1A",
|
|
2897
|
+
stepNote: "\u8BF4\u660E\uFF1A",
|
|
2898
|
+
tone: { note: "\u8BF4\u660E", tip: "\u63D0\u793A", caution: "\u6CE8\u610F", warning: "\u8B66\u544A" },
|
|
2899
|
+
high: "\u6700\u9AD8",
|
|
2900
|
+
low: "\u6700\u4F4E",
|
|
2901
|
+
feelsLike: "\u4F53\u611F",
|
|
2902
|
+
humidity: "\u6E7F\u5EA6",
|
|
2903
|
+
wind: "\u98CE",
|
|
2904
|
+
hourly: "\u9010\u5C0F\u65F6\uFF1A",
|
|
2905
|
+
daily: "\u672A\u6765\u51E0\u5929\uFF1A",
|
|
2906
|
+
precip: "\u964D\u6C34 ",
|
|
2907
|
+
asOf: "\u6570\u636E\u65F6\u95F4",
|
|
2908
|
+
source: "\u6765\u6E90\uFF1A",
|
|
2909
|
+
localTime: "\u672C\u5730\u65F6\u95F4",
|
|
2910
|
+
left: (time) => `\u8FD8\u6709 ${time}`,
|
|
2911
|
+
reached: "\u5DF2\u5230"
|
|
2912
|
+
},
|
|
2913
|
+
en: {
|
|
2914
|
+
recommended: "recommended",
|
|
2915
|
+
multiple: "Pick all that apply.",
|
|
2916
|
+
replyChoice: "Reply with a number or your own answer.",
|
|
2917
|
+
replyForm: "Reply with one line per field.",
|
|
2918
|
+
required: "required",
|
|
2919
|
+
yes: "Yes",
|
|
2920
|
+
no: "No",
|
|
2921
|
+
yesNo: "yes / no",
|
|
2922
|
+
step: "step",
|
|
2923
|
+
byDefault: (value) => ` (default ${value})`,
|
|
2924
|
+
colon: ": ",
|
|
2925
|
+
sep: ", ",
|
|
2926
|
+
stepWarning: "WARNING:",
|
|
2927
|
+
stepCaution: "CAUTION:",
|
|
2928
|
+
stepNote: "NOTE:",
|
|
2929
|
+
tone: { note: "Note", tip: "Tip", caution: "Caution", warning: "Warning" },
|
|
2930
|
+
high: "H",
|
|
2931
|
+
low: "L",
|
|
2932
|
+
feelsLike: "Feels like",
|
|
2933
|
+
humidity: "Humidity",
|
|
2934
|
+
wind: "Wind",
|
|
2935
|
+
hourly: "Next hours: ",
|
|
2936
|
+
daily: "Next days:",
|
|
2937
|
+
precip: "",
|
|
2938
|
+
asOf: "As of",
|
|
2939
|
+
source: "Source: ",
|
|
2940
|
+
localTime: "Local time",
|
|
2941
|
+
left: (time) => `${time} left`,
|
|
2942
|
+
reached: "reached"
|
|
2943
|
+
}
|
|
2944
|
+
};
|
|
2945
|
+
var listSeparator = (lang) => STR[lang].sep;
|
|
2946
|
+
var answerColon = (lang) => STR[lang].colon;
|
|
2947
|
+
var switchAnswer = (on, lang) => on ? STR[lang].yes : STR[lang].no;
|
|
2948
|
+
function choiceMarkdown(spec, lang) {
|
|
2949
|
+
const t = STR[lang];
|
|
2950
|
+
const out = [`**${spec.question}**`, ""];
|
|
2951
|
+
spec.options.forEach((option, i) => {
|
|
2952
|
+
let line = `${i + 1}. ${option.label}`;
|
|
2953
|
+
if (option.description) line += ` \u2014 ${option.description}`;
|
|
2954
|
+
if (option.recommended) line += lang === "zh" ? `\uFF08${t.recommended}\uFF09` : ` (${t.recommended})`;
|
|
2955
|
+
out.push(line);
|
|
2956
|
+
});
|
|
2957
|
+
out.push(
|
|
2958
|
+
"",
|
|
2959
|
+
spec.multiple ? `${t.multiple}${lang === "zh" ? "" : " "}${t.replyChoice}` : t.replyChoice
|
|
2960
|
+
);
|
|
2961
|
+
return out;
|
|
2962
|
+
}
|
|
2963
|
+
function fieldDetail(field, lang) {
|
|
2964
|
+
const t = STR[lang];
|
|
2965
|
+
const detail = [];
|
|
2966
|
+
const rule = fieldRule(field);
|
|
2967
|
+
switch (field.kind) {
|
|
2968
|
+
case "single":
|
|
2969
|
+
case "multiple":
|
|
2970
|
+
case "select":
|
|
2971
|
+
detail.push((field.options ?? []).map((o) => o.label).join(" / "));
|
|
2972
|
+
break;
|
|
2973
|
+
case "number":
|
|
2974
|
+
case "slider": {
|
|
2975
|
+
const range = rule === void 0 ? field.unit : appendUnit(rule, field.unit);
|
|
2976
|
+
if (range) detail.push(range);
|
|
2977
|
+
if (field.kind === "slider" && field.step !== void 0) {
|
|
2978
|
+
detail.push(`${t.step} ${field.step}`);
|
|
2979
|
+
}
|
|
2980
|
+
break;
|
|
2981
|
+
}
|
|
2982
|
+
case "switch":
|
|
2983
|
+
detail.push(t.yesNo);
|
|
2984
|
+
break;
|
|
2985
|
+
case "date":
|
|
2986
|
+
case "time":
|
|
2987
|
+
case "datetime":
|
|
2988
|
+
if (rule !== void 0) detail.push(rule);
|
|
2989
|
+
break;
|
|
2990
|
+
}
|
|
2991
|
+
if (field.placeholder && field.kind !== "select") detail.push(field.placeholder);
|
|
2992
|
+
return detail;
|
|
2993
|
+
}
|
|
2994
|
+
function defaultText(field, lang) {
|
|
2995
|
+
if (field.default === void 0) return void 0;
|
|
2996
|
+
const answer = initialAnswer(field);
|
|
2997
|
+
if (typeof answer === "boolean") return (answer ? STR[lang].yes : STR[lang].no).toLowerCase();
|
|
2998
|
+
if (Array.isArray(answer)) return answer.join(listSeparator(lang));
|
|
2999
|
+
return field.kind === "datetime" ? answer?.replace("T", " ") : answer;
|
|
3000
|
+
}
|
|
3001
|
+
function formMarkdown(spec, lang) {
|
|
3002
|
+
const t = STR[lang];
|
|
3003
|
+
const out = [];
|
|
3004
|
+
if (spec.title) out.push(`**${spec.title}**`, "");
|
|
3005
|
+
for (const field of spec.fields) {
|
|
3006
|
+
let line = `- **${field.label}**`;
|
|
3007
|
+
if (field.required) line += lang === "zh" ? `\uFF08${t.required}\uFF09` : ` (${t.required})`;
|
|
3008
|
+
const detail = fieldDetail(field, lang);
|
|
3009
|
+
if (detail.length > 0) line += `${t.colon}${detail.join(" ")}`;
|
|
3010
|
+
const fallback = defaultText(field, lang);
|
|
3011
|
+
if (fallback !== void 0) line += t.byDefault(fallback);
|
|
3012
|
+
if (field.hint) line += ` \u2014 ${field.hint}`;
|
|
3013
|
+
out.push(line);
|
|
3014
|
+
}
|
|
3015
|
+
out.push("", t.replyForm);
|
|
3016
|
+
return out;
|
|
3017
|
+
}
|
|
3018
|
+
function stepsMarkdown(spec, lang) {
|
|
3019
|
+
const t = STR[lang];
|
|
3020
|
+
const out = [];
|
|
3021
|
+
if (spec.title) out.push(`**${spec.title}**`, "");
|
|
3022
|
+
spec.steps.forEach((step, i) => {
|
|
3023
|
+
const marker = `${i + 1}. `;
|
|
3024
|
+
const indent = " ".repeat(marker.length);
|
|
3025
|
+
const parts = [];
|
|
3026
|
+
if (step.warning) parts.push(`**${t.stepWarning}** ${step.warning}`);
|
|
3027
|
+
if (step.caution) parts.push(`**${t.stepCaution}** ${step.caution}`);
|
|
3028
|
+
parts.push(step.text);
|
|
3029
|
+
if (step.note) parts.push(`**${t.stepNote}** ${step.note}`);
|
|
3030
|
+
if (step.code !== void 0) {
|
|
3031
|
+
parts.push(["```" + (step.lang ?? ""), ...step.code.split("\n"), "```"].join("\n"));
|
|
3032
|
+
}
|
|
3033
|
+
parts.forEach((part, j) => {
|
|
3034
|
+
const partLines = part.split("\n");
|
|
3035
|
+
partLines.forEach((line, k) => {
|
|
3036
|
+
out.push(j === 0 && k === 0 ? marker + line : indent + line);
|
|
3037
|
+
});
|
|
3038
|
+
if (j < parts.length - 1) out.push("");
|
|
3039
|
+
});
|
|
3040
|
+
});
|
|
3041
|
+
return out;
|
|
3042
|
+
}
|
|
3043
|
+
function calloutMarkdown(spec, lang) {
|
|
3044
|
+
const t = STR[lang];
|
|
3045
|
+
const tone = t.tone[spec.tone];
|
|
3046
|
+
const body = spec.text.split("\n").map((line) => `> ${line}`);
|
|
3047
|
+
if (spec.title) {
|
|
3048
|
+
return [`> **${tone}${t.colon.trim()}${lang === "zh" ? "" : " "}${spec.title}**`, ">", ...body];
|
|
3049
|
+
}
|
|
3050
|
+
const first = body[0] ?? ">";
|
|
3051
|
+
return [`> **${tone}${t.colon.trim()}** ${first.slice(2)}`, ...body.slice(1)];
|
|
3052
|
+
}
|
|
3053
|
+
function hourOf(time, at) {
|
|
3054
|
+
if (!time.includes("T")) return time;
|
|
3055
|
+
const instant = parseA2uiInstant(time);
|
|
3056
|
+
if (instant === null) return time;
|
|
3057
|
+
const p = zonedParts(instant, at.timeZone);
|
|
3058
|
+
return `${p.hour}:${p.minute}`;
|
|
3059
|
+
}
|
|
3060
|
+
function weatherMarkdown(spec, lang, at) {
|
|
3061
|
+
const t = STR[lang];
|
|
3062
|
+
const temp = (value) => formatTemp(value, spec.unit, false, lang);
|
|
3063
|
+
const current = formatTemp(spec.temp, spec.unit, true, lang);
|
|
3064
|
+
const out = [`**${spec.place} \xB7 ${conditionName(spec.condition, lang)} ${current}**`];
|
|
3065
|
+
if (spec.summary) out.push("", spec.summary);
|
|
3066
|
+
const range = [];
|
|
3067
|
+
if (spec.high !== void 0) range.push(`${t.high} ${temp(spec.high)}`);
|
|
3068
|
+
if (spec.low !== void 0) range.push(`${t.low} ${temp(spec.low)}`);
|
|
3069
|
+
const details = [];
|
|
3070
|
+
if (range.length > 0) details.push(range.join(" / "));
|
|
3071
|
+
if (spec.feelsLike !== void 0) details.push(`${t.feelsLike} ${temp(spec.feelsLike)}`);
|
|
3072
|
+
if (spec.humidity !== void 0) {
|
|
3073
|
+
details.push(`${t.humidity} ${formatNumber(spec.humidity, lang)}%`);
|
|
3074
|
+
}
|
|
3075
|
+
if (spec.windSpeed !== void 0) {
|
|
3076
|
+
const speed = `${formatNumber(spec.windSpeed, lang)} ${spec.windUnit ?? "km/h"}`;
|
|
3077
|
+
details.push(`${t.wind} ${speed}${spec.windDirection ? ` ${spec.windDirection}` : ""}`);
|
|
3078
|
+
}
|
|
3079
|
+
if (details.length > 0) out.push("", details.join(" \xB7 "));
|
|
3080
|
+
if (spec.hourly) {
|
|
3081
|
+
const hours = spec.hourly.slice(0, 8).map((h) => `${hourOf(h.time, at)} ${temp(h.temp)}`);
|
|
3082
|
+
out.push("", `${t.hourly}${hours.join(" \xB7 ")}`);
|
|
3083
|
+
}
|
|
3084
|
+
if (spec.daily) {
|
|
3085
|
+
const p = zonedParts(at.now, at.timeZone);
|
|
3086
|
+
const today = `${p.year}-${p.month}-${p.day}`;
|
|
3087
|
+
out.push("", t.daily);
|
|
3088
|
+
for (const day of spec.daily) {
|
|
3089
|
+
const name = `${weekdayOf(day.date, lang, today)} ${conditionName(day.condition, lang)}`;
|
|
3090
|
+
let line = `- ${name} ${temp(day.high)} / ${temp(day.low)}`;
|
|
3091
|
+
if (day.precip !== void 0) line += ` \xB7 ${t.precip}${formatNumber(day.precip, lang)}%`;
|
|
3092
|
+
out.push(line);
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
const foot = [];
|
|
3096
|
+
if (spec.asOf) foot.push(`${t.asOf} ${formatAsOf(spec.asOf, lang, at.now, at.timeZone)}`);
|
|
3097
|
+
if (spec.source) foot.push(`${t.source}${spec.source}`);
|
|
3098
|
+
if (foot.length > 0) out.push("", foot.join(" \xB7 "));
|
|
3099
|
+
return out;
|
|
3100
|
+
}
|
|
3101
|
+
function clockMarkdown(spec, lang, at) {
|
|
3102
|
+
const t = STR[lang];
|
|
3103
|
+
const locale = localeOf(lang);
|
|
3104
|
+
const out = [];
|
|
3105
|
+
if (spec.title) out.push(`**${spec.title}**`, "");
|
|
3106
|
+
const hourCycle = spec.hourCycle === "12" ? "h12" : spec.hourCycle === "24" ? "h23" : void 0;
|
|
3107
|
+
const localZone = at.timeZone ?? new Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
3108
|
+
const zones = spec.zones ?? [{ zone: "local" }];
|
|
3109
|
+
for (const entry of zones) {
|
|
3110
|
+
const local = entry.zone === "local";
|
|
3111
|
+
const zone = local ? localZone : entry.zone;
|
|
3112
|
+
const city = zone.slice(zone.lastIndexOf("/") + 1).replace(/_/g, " ");
|
|
3113
|
+
const label = entry.label ?? (local ? t.localTime : city);
|
|
3114
|
+
const options = {
|
|
3115
|
+
hour: "2-digit",
|
|
3116
|
+
minute: "2-digit",
|
|
3117
|
+
timeZone: zone
|
|
3118
|
+
};
|
|
3119
|
+
if (hourCycle !== void 0) options.hourCycle = hourCycle;
|
|
3120
|
+
const time = new Intl.DateTimeFormat(locale, options).format(at.now);
|
|
3121
|
+
const weekday = new Intl.DateTimeFormat(locale, { weekday: "short", timeZone: zone }).format(
|
|
3122
|
+
at.now
|
|
3123
|
+
);
|
|
3124
|
+
out.push(
|
|
3125
|
+
lang === "zh" ? `- ${label}\uFF1A${time}\uFF08${weekday}\uFF0C${zone}\uFF09` : `- ${label}: ${time} (${weekday}, ${zone})`
|
|
3126
|
+
);
|
|
3127
|
+
}
|
|
3128
|
+
return out;
|
|
3129
|
+
}
|
|
3130
|
+
function countdownMarkdown(spec, lang, at) {
|
|
3131
|
+
const t = STR[lang];
|
|
3132
|
+
const target = parseA2uiInstant(spec.to);
|
|
3133
|
+
if (target === null) return [`**${spec.label}**${t.colon}${spec.to}`];
|
|
3134
|
+
const parts = countdownParts(target, at.now);
|
|
3135
|
+
const status = parts.reached ? spec.doneLabel ?? t.reached : t.left(countdownText(parts, lang));
|
|
3136
|
+
let line = `**${spec.label}**${t.colon}${status}`;
|
|
3137
|
+
if (spec.showTarget !== false) {
|
|
3138
|
+
const p = zonedParts(target, at.timeZone);
|
|
3139
|
+
const stamp = `${p.year}-${p.month}-${p.day} ${p.hour}:${p.minute}`;
|
|
3140
|
+
line += lang === "zh" ? `\uFF08${stamp}\uFF09` : ` (${stamp})`;
|
|
3141
|
+
}
|
|
3142
|
+
return [line];
|
|
3143
|
+
}
|
|
3144
|
+
function metricsMarkdown(spec, lang) {
|
|
3145
|
+
const out = [];
|
|
3146
|
+
if (spec.title) out.push(`**${spec.title}**`, "");
|
|
3147
|
+
for (const item of spec.items) out.push(metricLine(item, lang));
|
|
3148
|
+
return out;
|
|
3149
|
+
}
|
|
3150
|
+
function withAsOf(lines, asOf, lang, at) {
|
|
3151
|
+
if (!asOf) return lines;
|
|
3152
|
+
return [...lines, "", `${STR[lang].asOf} ${formatAsOf(asOf, lang, at.now, at.timeZone)}`];
|
|
3153
|
+
}
|
|
3154
|
+
function specToMarkdown(spec, lang, ctx = {}) {
|
|
3155
|
+
const at = { now: ctx.now ?? /* @__PURE__ */ new Date(), timeZone: ctx.timeZone };
|
|
3156
|
+
switch (spec.type) {
|
|
3157
|
+
case "choice":
|
|
3158
|
+
return choiceMarkdown(spec, lang).join("\n");
|
|
3159
|
+
case "form":
|
|
3160
|
+
return formMarkdown(spec, lang).join("\n");
|
|
3161
|
+
case "steps":
|
|
3162
|
+
return stepsMarkdown(spec, lang).join("\n");
|
|
3163
|
+
case "callout":
|
|
3164
|
+
return calloutMarkdown(spec, lang).join("\n");
|
|
3165
|
+
case "weather":
|
|
3166
|
+
return weatherMarkdown(spec, lang, at).join("\n");
|
|
3167
|
+
case "clock":
|
|
3168
|
+
return clockMarkdown(spec, lang, at).join("\n");
|
|
3169
|
+
case "countdown":
|
|
3170
|
+
return countdownMarkdown(spec, lang, at).join("\n");
|
|
3171
|
+
case "metrics":
|
|
3172
|
+
return withAsOf(metricsMarkdown(spec, lang), spec.asOf, lang, at).join("\n");
|
|
3173
|
+
case "chart":
|
|
3174
|
+
return withAsOf(chartMarkdown(spec, lang), spec.asOf, lang, at).join("\n");
|
|
3175
|
+
case "resources":
|
|
3176
|
+
return withAsOf(resourcesMarkdown(spec, lang), spec.asOf, lang, at).join("\n");
|
|
3177
|
+
}
|
|
3178
|
+
}
|
|
3179
|
+
function toFallbackMarkdown(markdown, opts = {}) {
|
|
3180
|
+
const lang = opts.lang ?? detectLang(markdown);
|
|
3181
|
+
const ctx = { now: opts.now ?? /* @__PURE__ */ new Date(), timeZone: opts.timeZone };
|
|
3182
|
+
const lines = splitLines(markdown);
|
|
3183
|
+
const out = [];
|
|
3184
|
+
let cursor = 0;
|
|
3185
|
+
for (const span2 of scanFences(markdown)) {
|
|
3186
|
+
if (span2.lang !== "a2ui" || !span2.closed) continue;
|
|
3187
|
+
const { spec } = parseA2ui(span2.body.join("\n"));
|
|
3188
|
+
if (spec === void 0) continue;
|
|
3189
|
+
for (; cursor < span2.startLine - 1; cursor++) out.push(lines[cursor] ?? "");
|
|
3190
|
+
out.push(...specToMarkdown(spec, lang, ctx).split("\n"));
|
|
3191
|
+
cursor = span2.endLine;
|
|
3192
|
+
}
|
|
3193
|
+
for (; cursor < lines.length; cursor++) out.push(lines[cursor] ?? "");
|
|
3194
|
+
return out.join("\n");
|
|
3195
|
+
}
|
|
3196
|
+
function choiceFillText(spec, picked, lang) {
|
|
3197
|
+
const byLabel = new Map(spec.options.map((o) => [o.label, o.value ?? o.label]));
|
|
3198
|
+
return picked.map((label) => byLabel.get(label) ?? label).filter((value) => value.trim() !== "").join(listSeparator(lang));
|
|
3199
|
+
}
|
|
3200
|
+
function fillAnswer(field, raw, lang) {
|
|
3201
|
+
if (field.kind === "switch") {
|
|
3202
|
+
return typeof raw === "boolean" ? switchAnswer(raw, lang) : void 0;
|
|
3203
|
+
}
|
|
3204
|
+
if (typeof raw === "boolean") return void 0;
|
|
3205
|
+
const byLabel = new Map((field.options ?? []).map((o) => [o.label, o.value ?? o.label]));
|
|
3206
|
+
const parts = (Array.isArray(raw) ? raw : [raw]).map((value) => value.trim()).filter((value) => value !== "").map((value) => byLabel.get(value) ?? value);
|
|
3207
|
+
if (parts.length === 0) return void 0;
|
|
3208
|
+
const answer = parts.join(listSeparator(lang));
|
|
3209
|
+
if (field.kind === "number" || field.kind === "slider") return appendUnit(answer, field.unit);
|
|
3210
|
+
return field.kind === "datetime" ? answer.replace("T", " ") : answer;
|
|
3211
|
+
}
|
|
3212
|
+
function formFillText(spec, answers, lang) {
|
|
3213
|
+
const colon = answerColon(lang);
|
|
3214
|
+
const lines = [];
|
|
3215
|
+
for (const field of spec.fields) {
|
|
3216
|
+
const raw = answers[field.id];
|
|
3217
|
+
if (raw === void 0) continue;
|
|
3218
|
+
const answer = fillAnswer(field, raw, lang);
|
|
3219
|
+
if (answer === void 0) continue;
|
|
3220
|
+
if (!answer.includes("\n")) {
|
|
3221
|
+
lines.push(`${field.label}${colon}${answer}`);
|
|
3222
|
+
continue;
|
|
3223
|
+
}
|
|
3224
|
+
lines.push(`${field.label}${colon.trimEnd()}`);
|
|
3225
|
+
for (const line of answer.split("\n"))
|
|
3226
|
+
lines.push(line.trim() === "" ? "" : ` ${line.trimEnd()}`);
|
|
3227
|
+
}
|
|
3228
|
+
return lines.join("\n");
|
|
3229
|
+
}
|
|
3230
|
+
|
|
3231
|
+
// src/fill-parse.ts
|
|
3232
|
+
var LANGS = ["en", "zh"];
|
|
3233
|
+
var COLONS = LANGS.map((lang) => answerColon(lang).trim());
|
|
3234
|
+
var SEPARATORS = LANGS.map((lang) => listSeparator(lang).trim());
|
|
3235
|
+
function spellingsOf(options) {
|
|
3236
|
+
const filled = options.map((option) => ({
|
|
3237
|
+
text: (option.value ?? option.label).trim(),
|
|
3238
|
+
label: option.label
|
|
3239
|
+
}));
|
|
3240
|
+
const labels = options.filter((option) => option.value !== void 0 && option.value.trim() !== option.label).map((option) => ({ text: option.label, label: option.label }));
|
|
3241
|
+
return [...filled, ...labels].filter((spelling) => spelling.text !== "");
|
|
3242
|
+
}
|
|
3243
|
+
function optionNamed(options, text) {
|
|
3244
|
+
return spellingsOf(options).find((spelling) => spelling.text === text)?.label;
|
|
3245
|
+
}
|
|
3246
|
+
function pastSeparator(text, at) {
|
|
3247
|
+
let i = at;
|
|
3248
|
+
while (i < text.length && /\s/u.test(text[i])) i += 1;
|
|
3249
|
+
const separator = SEPARATORS.find((each) => text.startsWith(each, i));
|
|
3250
|
+
if (separator === void 0) return -1;
|
|
3251
|
+
i += separator.length;
|
|
3252
|
+
while (i < text.length && /\s/u.test(text[i])) i += 1;
|
|
3253
|
+
return i;
|
|
3254
|
+
}
|
|
3255
|
+
function listedLabels(options, text) {
|
|
3256
|
+
const spellings = spellingsOf(options).sort((a, b) => b.text.length - a.text.length);
|
|
3257
|
+
const memo = /* @__PURE__ */ new Map();
|
|
3258
|
+
const from = (at) => {
|
|
3259
|
+
const known = memo.get(at);
|
|
3260
|
+
if (known !== void 0) return known;
|
|
3261
|
+
let found = null;
|
|
3262
|
+
for (const spelling of spellings) {
|
|
3263
|
+
if (!text.startsWith(spelling.text, at)) continue;
|
|
3264
|
+
const end = at + spelling.text.length;
|
|
3265
|
+
if (end === text.length) {
|
|
3266
|
+
found = [spelling.label];
|
|
3267
|
+
break;
|
|
3268
|
+
}
|
|
3269
|
+
const next = pastSeparator(text, end);
|
|
3270
|
+
const rest = next < 0 ? null : from(next);
|
|
3271
|
+
if (rest !== null) {
|
|
3272
|
+
found = [spelling.label, ...rest];
|
|
3273
|
+
break;
|
|
3274
|
+
}
|
|
3275
|
+
}
|
|
3276
|
+
memo.set(at, found);
|
|
3277
|
+
return found;
|
|
3278
|
+
};
|
|
3279
|
+
const picked = from(0);
|
|
3280
|
+
if (picked === null) return void 0;
|
|
3281
|
+
return options.map((option) => option.label).filter((label) => picked.includes(label));
|
|
3282
|
+
}
|
|
3283
|
+
function parseChoicePick(spec, text) {
|
|
3284
|
+
const answer = text.trim();
|
|
3285
|
+
if (answer === "") return null;
|
|
3286
|
+
const whole = optionNamed(spec.options, answer);
|
|
3287
|
+
if (whole !== void 0) return [whole];
|
|
3288
|
+
if (spec.multiple !== true) return null;
|
|
3289
|
+
return listedLabels(spec.options, answer) ?? null;
|
|
3290
|
+
}
|
|
3291
|
+
function headOf(fields, line) {
|
|
3292
|
+
if (/^\s/u.test(line)) return void 0;
|
|
3293
|
+
for (const field of fields) {
|
|
3294
|
+
if (!line.startsWith(field.label)) continue;
|
|
3295
|
+
const after = line.slice(field.label.length).trimStart();
|
|
3296
|
+
const colon = COLONS.find((each) => after.startsWith(each));
|
|
3297
|
+
if (colon !== void 0) return { field, rest: after.slice(colon.length).trim() };
|
|
3298
|
+
}
|
|
3299
|
+
return void 0;
|
|
3300
|
+
}
|
|
3301
|
+
function unindent(lines) {
|
|
3302
|
+
const kept = lines.map((line) => line.startsWith(" ") ? line.slice(2) : line.trimStart());
|
|
3303
|
+
return kept.join("\n").trim();
|
|
3304
|
+
}
|
|
3305
|
+
function answerTexts(spec, text) {
|
|
3306
|
+
const fields = [...spec.fields].sort((a, b) => b.label.length - a.label.length);
|
|
3307
|
+
const lines = splitLines(text);
|
|
3308
|
+
const out = /* @__PURE__ */ new Map();
|
|
3309
|
+
for (let i = 0; i < lines.length; i += 1) {
|
|
3310
|
+
const head = headOf(fields, lines[i]);
|
|
3311
|
+
if (head === void 0) continue;
|
|
3312
|
+
let answer = head.rest;
|
|
3313
|
+
if (answer === "") {
|
|
3314
|
+
const block = [];
|
|
3315
|
+
while (i + 1 < lines.length && /^(\s|$)/u.test(lines[i + 1])) {
|
|
3316
|
+
i += 1;
|
|
3317
|
+
block.push(lines[i]);
|
|
3318
|
+
}
|
|
3319
|
+
answer = unindent(block);
|
|
3320
|
+
}
|
|
3321
|
+
if (!out.has(head.field.id)) out.set(head.field.id, answer);
|
|
3322
|
+
}
|
|
3323
|
+
return out;
|
|
3324
|
+
}
|
|
3325
|
+
function switchState(text) {
|
|
3326
|
+
const word = text.toLowerCase();
|
|
3327
|
+
for (const lang of LANGS) {
|
|
3328
|
+
if (word === switchAnswer(true, lang).toLowerCase()) return true;
|
|
3329
|
+
if (word === switchAnswer(false, lang).toLowerCase()) return false;
|
|
3330
|
+
}
|
|
3331
|
+
return void 0;
|
|
3332
|
+
}
|
|
3333
|
+
function withoutUnit(text, unit) {
|
|
3334
|
+
if (unit === void 0 || unit === "" || !text.endsWith(unit)) return text;
|
|
3335
|
+
return text.slice(0, -unit.length).trim();
|
|
3336
|
+
}
|
|
3337
|
+
function answerFrom(field, text) {
|
|
3338
|
+
switch (field.kind) {
|
|
3339
|
+
case "switch":
|
|
3340
|
+
return switchState(text);
|
|
3341
|
+
case "single":
|
|
3342
|
+
case "select":
|
|
3343
|
+
return optionNamed(field.options ?? [], text);
|
|
3344
|
+
case "multiple":
|
|
3345
|
+
return listedLabels(field.options ?? [], text);
|
|
3346
|
+
case "number":
|
|
3347
|
+
case "slider":
|
|
3348
|
+
return withoutUnit(text, field.unit);
|
|
3349
|
+
case "datetime":
|
|
3350
|
+
return text.replace(/^(\S+)\s+(\S+)$/u, "$1T$2");
|
|
3351
|
+
default:
|
|
3352
|
+
return text;
|
|
3353
|
+
}
|
|
3354
|
+
}
|
|
3355
|
+
function holdsAnswer(answer) {
|
|
3356
|
+
if (typeof answer === "boolean") return true;
|
|
3357
|
+
return typeof answer === "string" ? answer.trim() !== "" : answer.length > 0;
|
|
3358
|
+
}
|
|
3359
|
+
function parseFormFill(spec, text) {
|
|
3360
|
+
const texts = answerTexts(spec, text);
|
|
3361
|
+
if (texts.size === 0) return null;
|
|
3362
|
+
const answers = {};
|
|
3363
|
+
for (const field of spec.fields) {
|
|
3364
|
+
const written = texts.get(field.id);
|
|
3365
|
+
if (written === void 0) continue;
|
|
3366
|
+
const answer = answerFrom(field, written);
|
|
3367
|
+
if (answer === void 0 || !holdsAnswer(answer)) continue;
|
|
3368
|
+
if (fieldProblem(field, answer) !== null) continue;
|
|
3369
|
+
answers[field.id] = answer;
|
|
3370
|
+
}
|
|
3371
|
+
return answers;
|
|
3372
|
+
}
|
|
3373
|
+
|
|
3374
|
+
// src/rubric.ts
|
|
3375
|
+
var A2UI_RUBRIC = {
|
|
3376
|
+
en: [
|
|
3377
|
+
"Would plain text have served the reader as well? If yes, remove the block.",
|
|
3378
|
+
"Does a sentence before each block say what it is for, so no block arrives unexplained?",
|
|
3379
|
+
"Is each component the right one: choice for one decision, form for several answers at once, steps for a procedure, callout for one warning or tip, mermaid for a structure or a flow, a chart for numbers with a shape (a trend, a comparison, a share, a spread), a widget (weather, clock, countdown, metrics, resources) for a reading taken in at a glance?",
|
|
3380
|
+
"Do the options cover the realistic answers, with one marked recommended when you have a view, and is the question the last thing in the reply?",
|
|
3381
|
+
"Can the reader take everything in at a glance: at most 5 options, 4 fields, 10 steps, one or two blocks?",
|
|
3382
|
+
"Did you keep UI out of a plain fact, a conversational turn or an emotional moment?",
|
|
3383
|
+
"Is every number in a widget or a chart real \u2014 read by a script, a tool or the user, with its `asOf` time when it is a snapshot \u2014 and never invented or rounded into a guess?"
|
|
3384
|
+
],
|
|
3385
|
+
zh: [
|
|
3386
|
+
"\u6362\u6210\u7EAF\u6587\u672C\u662F\u5426\u540C\u6837\u6E05\u695A\uFF1F\u5982\u679C\u662F\uFF0C\u5220\u6389\u8FD9\u4E2A\u7EC4\u4EF6\u3002",
|
|
3387
|
+
"\u6BCF\u4E2A\u7EC4\u4EF6\u524D\u9762\u662F\u5426\u6709\u4E00\u53E5\u8BDD\u8BF4\u660E\u5B83\u662F\u5E72\u4EC0\u4E48\u7684\uFF0C\u6CA1\u6709\u7EC4\u4EF6\u662F\u7A81\u7136\u51FA\u73B0\u7684\uFF1F",
|
|
3388
|
+
"\u7EC4\u4EF6\u9009\u5BF9\u4E86\u5417\uFF1Achoice \u7528\u4E8E\u4E00\u4E2A\u51B3\u5B9A\uFF0Cform \u7528\u4E8E\u4E00\u6B21\u56DE\u7B54\u591A\u4E2A\u95EE\u9898\uFF0Csteps \u7528\u4E8E\u64CD\u4F5C\u6B65\u9AA4\uFF0Ccallout \u7528\u4E8E\u4E00\u6761\u8B66\u544A\u6216\u63D0\u793A\uFF0Cmermaid \u7528\u4E8E\u7ED3\u6784\u6216\u6D41\u7A0B\uFF0Cchart \u7528\u4E8E\u6709\u5F62\u72B6\u7684\u6570\u5B57\uFF08\u8D8B\u52BF\u3001\u5BF9\u6BD4\u3001\u5360\u6BD4\u3001\u5206\u5E03\uFF09\uFF0C\u5C0F\u7EC4\u4EF6\uFF08weather\u3001clock\u3001countdown\u3001metrics\u3001resources\uFF09\u7528\u4E8E\u4E00\u773C\u770B\u61C2\u7684\u8BFB\u6570\uFF1F",
|
|
3389
|
+
"\u9009\u9879\u662F\u5426\u8986\u76D6\u4E86\u73B0\u5B9E\u4E2D\u7684\u7B54\u6848\uFF0C\u6709\u503E\u5411\u65F6\u662F\u5426\u6807\u4E86\u4E00\u4E2A\u63A8\u8350\u9879\uFF0C\u95EE\u9898\u662F\u5426\u653E\u5728\u56DE\u590D\u7684\u6700\u540E\uFF1F",
|
|
3390
|
+
"\u8BFB\u8005\u80FD\u5426\u4E00\u773C\u770B\u5B8C\uFF1A\u9009\u9879\u4E0D\u8D85\u8FC7 5 \u4E2A\u3001\u5B57\u6BB5\u4E0D\u8D85\u8FC7 4 \u4E2A\u3001\u6B65\u9AA4\u4E0D\u8D85\u8FC7 10 \u6B65\u3001\u7EC4\u4EF6\u4E0D\u8D85\u8FC7\u4E00\u4E24\u4E2A\uFF1F",
|
|
3391
|
+
"\u5BF9\u4E8E\u4E00\u53E5\u8BDD\u7684\u4E8B\u5B9E\u3001\u95F2\u804A\u6216\u60C5\u7EEA\u6027\u7684\u5BF9\u8BDD\uFF0C\u662F\u5426\u6CA1\u6709\u4F7F\u7528\u7EC4\u4EF6\uFF1F",
|
|
3392
|
+
"\u5C0F\u7EC4\u4EF6\u548C\u56FE\u8868\u91CC\u7684\u6BCF\u4E2A\u6570\u5B57\u662F\u5426\u90FD\u662F\u771F\u5B9E\u7684\u2014\u2014\u6765\u81EA\u811A\u672C\u3001\u5DE5\u5177\u6216\u7528\u6237\uFF0C\u5FEB\u7167\u5E26\u6709 `asOf` \u65F6\u95F4\u2014\u2014\u800C\u4E0D\u662F\u7F16\u9020\u6216\u731C\u6D4B\uFF1F"
|
|
3393
|
+
]
|
|
3394
|
+
};
|
|
3395
|
+
|
|
3396
|
+
// src/report.ts
|
|
3397
|
+
var plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
3398
|
+
function where(issue2) {
|
|
3399
|
+
const parts = [];
|
|
3400
|
+
if (issue2.block !== void 0) parts.push(`block ${issue2.block}`);
|
|
3401
|
+
if (issue2.line !== void 0) parts.push(`line ${issue2.line}`);
|
|
3402
|
+
if (issue2.path !== void 0) parts.push(issue2.path);
|
|
3403
|
+
return parts.length > 0 ? `[${parts.join(" ")}] ` : "";
|
|
3404
|
+
}
|
|
3405
|
+
function formatReport(report, opts = {}) {
|
|
3406
|
+
const errors = report.issues.filter((issue2) => issue2.level === "error");
|
|
3407
|
+
const warnings = report.issues.filter((issue2) => issue2.level === "warning");
|
|
3408
|
+
const out = [
|
|
3409
|
+
`a2ui check: ${plural(report.blocks.length, "block")}, ${plural(errors.length, "error")}, ${plural(warnings.length, "warning")} (lang ${report.lang})`
|
|
3410
|
+
];
|
|
3411
|
+
for (const block of report.blocks) {
|
|
3412
|
+
out.push(
|
|
3413
|
+
` block ${block.index}: ${block.fence}${block.type !== void 0 ? ` ${block.type}` : ""} \u2014 ${block.ok ? "ok" : "ERROR"}`
|
|
3414
|
+
);
|
|
3415
|
+
}
|
|
3416
|
+
if (report.blocks.length === 0) out.push(" no a2ui or mermaid blocks");
|
|
3417
|
+
const section2 = (title, list) => {
|
|
3418
|
+
if (list.length === 0) return;
|
|
3419
|
+
out.push("", title);
|
|
3420
|
+
for (const issue2 of list) out.push(` ${where(issue2)}${issue2.code}: ${issue2.message}`);
|
|
3421
|
+
};
|
|
3422
|
+
section2("errors (fix before sending):", errors);
|
|
3423
|
+
section2("warnings \u2014 blocks (L2):", warnings);
|
|
3424
|
+
const s = report.score;
|
|
3425
|
+
const verdict = errors.length > 0 ? "total is 0 while any error remains" : s.total < A2UI_SCORING.passTotal ? `below the ${A2UI_SCORING.passTotal} gate; fix the warnings that matter` : "pass";
|
|
3426
|
+
out.push("", `score: L1 ${s.l1} L2 ${s.l2} total ${s.total} (${verdict})`);
|
|
3427
|
+
if (opts.rubric) {
|
|
3428
|
+
out.push("", "self-review before sending:");
|
|
3429
|
+
A2UI_RUBRIC[report.lang].forEach((question, i) => out.push(` ${i + 1}. ${question}`));
|
|
3430
|
+
}
|
|
3431
|
+
return out.join("\n");
|
|
3432
|
+
}
|
|
3433
|
+
export {
|
|
3434
|
+
A2UI_CHART_KINDS,
|
|
3435
|
+
A2UI_FIELD_FORMATS,
|
|
3436
|
+
A2UI_LIMITS,
|
|
3437
|
+
A2UI_RUBRIC,
|
|
3438
|
+
A2UI_SCORING,
|
|
3439
|
+
MERMAID_TYPES,
|
|
3440
|
+
checkMermaid,
|
|
3441
|
+
checkReply,
|
|
3442
|
+
choiceFillText,
|
|
3443
|
+
conditionName,
|
|
3444
|
+
countdownParts,
|
|
3445
|
+
countdownText,
|
|
3446
|
+
detectLang,
|
|
3447
|
+
fieldProblem,
|
|
3448
|
+
fieldRule,
|
|
3449
|
+
findBlocks,
|
|
3450
|
+
formFillText,
|
|
3451
|
+
formatAsOf,
|
|
3452
|
+
formatDelta,
|
|
3453
|
+
formatMetricValue,
|
|
3454
|
+
formatNumber,
|
|
3455
|
+
formatReport,
|
|
3456
|
+
formatTemp,
|
|
3457
|
+
formatValue,
|
|
3458
|
+
initialAnswer,
|
|
3459
|
+
listSeparator,
|
|
3460
|
+
localeOf,
|
|
3461
|
+
metricDetail,
|
|
3462
|
+
parseA2ui,
|
|
3463
|
+
parseA2uiInstant,
|
|
3464
|
+
parseChoicePick,
|
|
3465
|
+
parseFormFill,
|
|
3466
|
+
resolveMetric,
|
|
3467
|
+
resolveResources,
|
|
3468
|
+
resourcesTiles,
|
|
3469
|
+
seriesExtent,
|
|
3470
|
+
specToMarkdown,
|
|
3471
|
+
toFallbackMarkdown,
|
|
3472
|
+
weekdayOf
|
|
3473
|
+
};
|
|
3474
|
+
//# sourceMappingURL=index.js.map
|