@khanglvm/relay 0.18.0 → 0.20.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/rly.js +1 -1
- package/package.json +39 -4
- package/schema/spec.v1.json +1368 -0
- package/src/a2ui/compile.js +2 -0
- package/src/cli/args.js +46 -0
- package/src/cli/ask.js +12 -0
- package/src/cli/boards.js +326 -0
- package/src/cli/config.js +56 -0
- package/src/cli/context.js +13 -0
- package/src/cli/git.js +183 -0
- package/src/cli/help.js +90 -0
- package/src/cli/index.js +103 -0
- package/src/cli/install.js +197 -0
- package/src/cli/io.js +186 -0
- package/src/cli/mcp.js +94 -0
- package/src/cli/share.js +45 -0
- package/src/cli/skill.js +192 -0
- package/src/cli/upgrade.js +130 -0
- package/src/cli/wait.js +172 -0
- package/src/file-preview.js +1 -1
- package/src/index.js +104 -0
- package/src/link.js +1 -0
- package/src/mcp-ui/board/block-ctx.js +43 -0
- package/src/mcp-ui/board/bridge.js +165 -0
- package/src/mcp-ui/board/controls.js +310 -0
- package/src/mcp-ui/board/core.js +20 -0
- package/src/mcp-ui/board/lifecycle.js +118 -0
- package/src/mcp-ui/board/normalize.js +95 -0
- package/src/mcp-ui/board/render.js +200 -0
- package/src/mcp-ui/board/state.js +106 -0
- package/src/mcp-ui/board/vendors.js +86 -0
- package/src/mcp.js +15 -21
- package/src/{server.js → server/board.js} +17 -781
- package/src/server/files.js +141 -0
- package/src/server/http.js +114 -0
- package/src/server/index.js +2 -0
- package/src/server/page.js +183 -0
- package/src/server/paths.js +7 -0
- package/src/server/result.js +58 -0
- package/src/server/reviews.js +41 -0
- package/src/server/sanitize.js +231 -0
- package/src/spec/blocks/a2ui.js +25 -0
- package/src/spec/blocks/chart.js +49 -0
- package/src/spec/blocks/code.js +38 -0
- package/src/spec/blocks/diagram.js +39 -0
- package/src/spec/blocks/git-conflict.js +106 -0
- package/src/spec/blocks/image.js +32 -0
- package/src/spec/blocks/index.js +33 -0
- package/src/spec/blocks/markdown.js +13 -0
- package/src/spec/blocks/pdf.js +38 -0
- package/src/spec/blocks/table.js +122 -0
- package/src/spec/blocks/video.js +68 -0
- package/src/spec/blocks/widgets.js +98 -0
- package/src/spec/constants.js +65 -0
- package/src/spec/index.js +5 -0
- package/src/spec/normalize.js +285 -0
- package/src/spec/ready.js +106 -0
- package/src/spec/schema.js +182 -0
- package/src/spec/sources.js +83 -0
- package/src/ui/annotate/api.js +149 -0
- package/src/ui/annotate/badges.js +77 -0
- package/src/ui/annotate/chrome.js +158 -0
- package/src/ui/annotate/core.js +134 -0
- package/src/ui/annotate/highlights.js +129 -0
- package/src/ui/annotate/mutations.js +127 -0
- package/src/ui/annotate/popover.js +206 -0
- package/src/ui/annotate/summary.js +67 -0
- package/src/ui/app/annotations.js +88 -0
- package/src/ui/app/autosave.js +44 -0
- package/src/ui/app/core.js +74 -0
- package/src/ui/app/drafts.js +201 -0
- package/src/ui/app/heartbeat.js +150 -0
- package/src/ui/app/iframe-bridge.js +64 -0
- package/src/ui/app/persistence.js +117 -0
- package/src/ui/app/prefilled.js +21 -0
- package/src/ui/app/presence.js +38 -0
- package/src/ui/app/question-color.js +67 -0
- package/src/ui/app/questions.js +360 -0
- package/src/ui/app/render.js +135 -0
- package/src/ui/app/share.js +108 -0
- package/src/ui/app/submit.js +109 -0
- package/src/ui/app/theme.js +63 -0
- package/src/ui/blocks/api.js +1 -0
- package/src/ui/blocks/chart.js +222 -0
- package/src/ui/blocks/code.js +138 -0
- package/src/ui/blocks/compare.js +82 -0
- package/src/ui/blocks/core.js +74 -0
- package/src/ui/blocks/diff.js +361 -0
- package/src/ui/blocks/dispatch.js +111 -0
- package/src/ui/blocks/file-links.js +180 -0
- package/src/ui/blocks/git-conflict.js +151 -0
- package/src/ui/blocks/graphviz.js +57 -0
- package/src/ui/blocks/html.js +45 -0
- package/src/ui/blocks/image.js +371 -0
- package/src/ui/blocks/kpi.js +26 -0
- package/src/ui/blocks/markdown.js +223 -0
- package/src/ui/blocks/mermaid.js +242 -0
- package/src/ui/blocks/palette.js +59 -0
- package/src/ui/blocks/pdf.js +21 -0
- package/src/ui/blocks/plantuml.js +152 -0
- package/src/ui/blocks/table.js +155 -0
- package/src/ui/blocks/typography.js +25 -0
- package/src/ui/blocks/vendor.js +43 -0
- package/src/ui/blocks/video.js +43 -0
- package/src/ui/blocks/viewer-fullscreen.js +146 -0
- package/src/ui/blocks/viewer.js +324 -0
- package/src/ui/kit.js +1 -0
- package/src/ui/manifest.js +140 -0
- package/types/a2ui/compile.d.ts +40 -0
- package/types/index.d.ts +181 -0
- package/types/mcp.d.ts +31 -0
- package/types/spec/blocks/a2ui.d.ts +4 -0
- package/types/spec/blocks/chart.d.ts +4 -0
- package/types/spec/blocks/code.d.ts +10 -0
- package/types/spec/blocks/diagram.d.ts +15 -0
- package/types/spec/blocks/git-conflict.d.ts +7 -0
- package/types/spec/blocks/image.d.ts +5 -0
- package/types/spec/blocks/index.d.ts +30 -0
- package/types/spec/blocks/markdown.d.ts +5 -0
- package/types/spec/blocks/pdf.d.ts +4 -0
- package/types/spec/blocks/table.d.ts +9 -0
- package/types/spec/blocks/video.d.ts +4 -0
- package/types/spec/blocks/widgets.d.ts +21 -0
- package/types/spec/constants.d.ts +67 -0
- package/types/spec/index.d.ts +4 -0
- package/types/spec/normalize.d.ts +17 -0
- package/types/spec/ready.d.ts +1 -0
- package/types/spec/schema.d.ts +1171 -0
- package/types/spec/sources.d.ts +14 -0
- package/types/store.d.ts +29 -0
- package/types/ui/manifest.d.ts +62 -0
- package/types/util.d.ts +6 -0
- package/src/cli.js +0 -1766
- package/src/mcp-ui/board.js +0 -1162
- package/src/spec.js +0 -1222
- package/src/ui/annotate.js +0 -1055
- package/src/ui/app.js +0 -1643
- package/src/ui/blocks.js +0 -3260
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
// The table block: CSV, TSV and JSON row files, and normalization.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { CliError } from '../../util.js';
|
|
5
|
+
import { BLOCK_HEIGHT, TEXT_FILE_MAX_BYTES } from '../constants.js';
|
|
6
|
+
import { asStr, clampInt } from '../sources.js';
|
|
7
|
+
|
|
8
|
+
// Minimal RFC-4180-ish CSV/TSV parser: handles quoted fields containing the
|
|
9
|
+
// delimiter, embedded newlines, and "" escaped quotes. Returns an array of rows
|
|
10
|
+
// (each an array of string cells). Library-free.
|
|
11
|
+
function parseDelimited(text, delim) {
|
|
12
|
+
const rows = [];
|
|
13
|
+
let row = [];
|
|
14
|
+
let field = '';
|
|
15
|
+
let inQ = false;
|
|
16
|
+
const pushField = () => { row.push(field); field = ''; };
|
|
17
|
+
const pushRow = () => { pushField(); rows.push(row); row = []; };
|
|
18
|
+
for (let i = 0; i < text.length; i++) {
|
|
19
|
+
const c = text[i];
|
|
20
|
+
if (inQ) {
|
|
21
|
+
if (c === '"') {
|
|
22
|
+
if (text[i + 1] === '"') { field += '"'; i++; } else inQ = false;
|
|
23
|
+
} else field += c;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (c === '"') inQ = true;
|
|
27
|
+
else if (c === delim) pushField();
|
|
28
|
+
else if (c === '\n') pushRow();
|
|
29
|
+
else if (c === '\r') { /* swallow CR (CRLF) */ }
|
|
30
|
+
else field += c;
|
|
31
|
+
}
|
|
32
|
+
if (field.length || row.length) pushRow();
|
|
33
|
+
// drop a single trailing empty row from a final newline
|
|
34
|
+
if (rows.length && rows[rows.length - 1].length === 1 && rows[rows.length - 1][0] === '') rows.pop();
|
|
35
|
+
return rows;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Load table {columns, rows} from a local .csv/.tsv/.json file (rowsFile).
|
|
39
|
+
// CSV/TSV: first row = header → column keys. JSON: an array of objects (columns
|
|
40
|
+
// from the union of keys) or arrays (needs explicit "columns").
|
|
41
|
+
function loadRowsFile(file, cwd, where) {
|
|
42
|
+
const p = path.resolve(cwd, file);
|
|
43
|
+
let buf;
|
|
44
|
+
try { buf = fs.readFileSync(p); } catch { throw new CliError(`${where}: cannot read rowsFile "${file}" (resolved: ${p})`); }
|
|
45
|
+
if (buf.length > TEXT_FILE_MAX_BYTES) {
|
|
46
|
+
throw new CliError(`${where}: rowsFile "${file}" is ${(buf.length / 1024).toFixed(0)}KB — max ${TEXT_FILE_MAX_BYTES / 1024}KB.`);
|
|
47
|
+
}
|
|
48
|
+
const text = buf.toString('utf8');
|
|
49
|
+
const ext = path.extname(p).slice(1).toLowerCase();
|
|
50
|
+
if (ext === 'json') {
|
|
51
|
+
let data;
|
|
52
|
+
try { data = JSON.parse(text); } catch (e) { throw new CliError(`${where}: rowsFile "${file}" JSON parse error: ${e.message}`); }
|
|
53
|
+
if (!Array.isArray(data)) throw new CliError(`${where}: rowsFile "${file}" JSON must be an array of rows.`);
|
|
54
|
+
const cols = [];
|
|
55
|
+
for (const r of data) {
|
|
56
|
+
if (r && typeof r === 'object' && !Array.isArray(r)) {
|
|
57
|
+
for (const k of Object.keys(r)) if (!cols.includes(k)) cols.push(k);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return { columns: cols, rows: data };
|
|
61
|
+
}
|
|
62
|
+
const parsed = parseDelimited(text, ext === 'tsv' ? '\t' : ',');
|
|
63
|
+
if (!parsed.length) return { columns: [], rows: [] };
|
|
64
|
+
const header = parsed[0].map((h) => String(h).trim());
|
|
65
|
+
const rows = parsed.slice(1).map((cells) => {
|
|
66
|
+
const obj = {};
|
|
67
|
+
header.forEach((h, i) => { obj[h] = cells[i] !== undefined ? cells[i] : ''; });
|
|
68
|
+
return obj;
|
|
69
|
+
});
|
|
70
|
+
return { columns: header, rows };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// Normalizes one table block. `id` is the already-assigned block id.
|
|
74
|
+
export function normalizeTableBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
75
|
+
// Rows/columns may come from a local .csv/.tsv/.json file instead of inline.
|
|
76
|
+
let rawColumns = rawBlock.columns;
|
|
77
|
+
let rawRows = rawBlock.rows;
|
|
78
|
+
if (typeof rawBlock.rowsFile === 'string' && rawBlock.rowsFile.trim() && !Array.isArray(rawRows)) {
|
|
79
|
+
const loaded = loadRowsFile(rawBlock.rowsFile, cwd, where);
|
|
80
|
+
rawRows = loaded.rows;
|
|
81
|
+
if (!Array.isArray(rawColumns) || !rawColumns.length) rawColumns = loaded.columns;
|
|
82
|
+
}
|
|
83
|
+
if (!Array.isArray(rawColumns) || rawColumns.length < 1) {
|
|
84
|
+
throw new CliError(`${where}: table needs a non-empty "columns" array (strings or {key,label,align?})${rawBlock.rowsFile ? ' — the rowsFile had no header/keys to infer them' : ''}.`);
|
|
85
|
+
}
|
|
86
|
+
if (!Array.isArray(rawRows)) {
|
|
87
|
+
throw new CliError(`${where}: table needs a "rows" array or a readable "rowsFile".`);
|
|
88
|
+
}
|
|
89
|
+
const columns = rawColumns.map((c, k) => {
|
|
90
|
+
if (typeof c === 'string' || typeof c === 'number') {
|
|
91
|
+
const key = String(c);
|
|
92
|
+
return { key, label: key };
|
|
93
|
+
}
|
|
94
|
+
if (c && typeof c === 'object' && !Array.isArray(c)) {
|
|
95
|
+
const key = asStr(c.key ?? c.label).trim();
|
|
96
|
+
if (!key) throw new CliError(`${where}.columns[${k}]: needs "key" or "label".`);
|
|
97
|
+
const col = { key, label: asStr(c.label ?? c.key) || key };
|
|
98
|
+
if (c.align !== undefined) col.align = asStr(c.align);
|
|
99
|
+
return col;
|
|
100
|
+
}
|
|
101
|
+
throw new CliError(`${where}.columns[${k}]: must be a string or {key, label, align?}.`);
|
|
102
|
+
});
|
|
103
|
+
// Normalize array rows into objects keyed by column key, so the client
|
|
104
|
+
// (and table-cell annotation values) always index rows the same way.
|
|
105
|
+
const rows = rawRows.map((r, ri) => {
|
|
106
|
+
if (Array.isArray(r)) {
|
|
107
|
+
const obj = {};
|
|
108
|
+
columns.forEach((col, ci) => {
|
|
109
|
+
obj[col.key] = r[ci];
|
|
110
|
+
});
|
|
111
|
+
return obj;
|
|
112
|
+
}
|
|
113
|
+
if (r && typeof r === 'object') return r;
|
|
114
|
+
throw new CliError(`${where}.rows[${ri}]: must be an array or an object.`);
|
|
115
|
+
});
|
|
116
|
+
const block = { id, type: 'table', columns, rows };
|
|
117
|
+
if (rawBlock.sortable === true) block.sortable = true;
|
|
118
|
+
if (rawBlock.filterable === true) block.filterable = true;
|
|
119
|
+
if (rawBlock.exportable === true) block.exportable = true;
|
|
120
|
+
if (hasHeight) block.height = clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, undefined);
|
|
121
|
+
return block;
|
|
122
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The video block: embed detection for YouTube and Vimeo, local and direct media.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { CliError } from '../../util.js';
|
|
5
|
+
import { asStr, clampInt } from '../sources.js';
|
|
6
|
+
import { BLOCK_HEIGHT, VIDEO_MAX_BYTES, VIDEO_MIMES } from '../constants.js';
|
|
7
|
+
|
|
8
|
+
// Recognizes a YouTube / Vimeo URL (or a bare YouTube id) and returns
|
|
9
|
+
// {provider, videoId, start} for an iframe embed, else null. Cross-platform —
|
|
10
|
+
// pure string parsing, no URL host assumptions beyond the known providers.
|
|
11
|
+
function parseVideoEmbed(src) {
|
|
12
|
+
const s = String(src).trim();
|
|
13
|
+
// bare 11-char YouTube id
|
|
14
|
+
if (/^[\w-]{11}$/.test(s)) return { provider: 'youtube', videoId: s, start: 0 };
|
|
15
|
+
let m;
|
|
16
|
+
if ((m = s.match(/(?:youtube\.com\/(?:watch\?(?:.*&)?v=|embed\/|shorts\/|v\/)|youtu\.be\/)([\w-]{11})/i))) {
|
|
17
|
+
const t = s.match(/[?&](?:t|start)=(\d+)/);
|
|
18
|
+
return { provider: 'youtube', videoId: m[1], start: t ? Number(t[1]) : 0 };
|
|
19
|
+
}
|
|
20
|
+
if ((m = s.match(/vimeo\.com\/(?:video\/)?(\d+)/i))) {
|
|
21
|
+
return { provider: 'vimeo', videoId: m[1], start: 0 };
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Normalizes one video block. `id` is the already-assigned block id.
|
|
27
|
+
export function normalizeVideoBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
28
|
+
const src = asStr(rawBlock.src ?? rawBlock.file ?? rawBlock.url).trim();
|
|
29
|
+
if (!src) throw new CliError(`${where}: video block needs a "src" (YouTube/Vimeo URL, http(s) media URL, or local file path).`);
|
|
30
|
+
const block = { id, type: 'video' };
|
|
31
|
+
if (rawBlock.title !== undefined) block.title = asStr(rawBlock.title);
|
|
32
|
+
if (rawBlock.alt !== undefined) block.title = asStr(rawBlock.alt);
|
|
33
|
+
if (hasHeight) block.height = clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, undefined);
|
|
34
|
+
const embed = parseVideoEmbed(src);
|
|
35
|
+
if (embed) {
|
|
36
|
+
block.provider = embed.provider;
|
|
37
|
+
block.videoId = embed.videoId;
|
|
38
|
+
if (embed.start) block.start = embed.start;
|
|
39
|
+
return block;
|
|
40
|
+
}
|
|
41
|
+
if (/^https?:/i.test(src)) {
|
|
42
|
+
block.src = src;
|
|
43
|
+
const ext = path.extname(src.split(/[?#]/)[0]).slice(1).toLowerCase();
|
|
44
|
+
if (VIDEO_MIMES[ext]) block.mime = VIDEO_MIMES[ext];
|
|
45
|
+
return block;
|
|
46
|
+
}
|
|
47
|
+
// local file — kept as an absolute path the server streams (never embedded
|
|
48
|
+
// in the payload). The client only learns a flag + mime via /video/b/<id>.
|
|
49
|
+
const p = path.resolve(cwd, src);
|
|
50
|
+
const ext = path.extname(p).slice(1).toLowerCase();
|
|
51
|
+
const mime = VIDEO_MIMES[ext];
|
|
52
|
+
if (!mime) {
|
|
53
|
+
throw new CliError(`${where}: unsupported video extension ".${ext}" — use ${Object.keys(VIDEO_MIMES).join('/')}, a YouTube/Vimeo URL, or an http(s) media URL.`);
|
|
54
|
+
}
|
|
55
|
+
let stat;
|
|
56
|
+
try {
|
|
57
|
+
stat = fs.statSync(p);
|
|
58
|
+
} catch {
|
|
59
|
+
throw new CliError(`${where}: cannot read video "${src}" (resolved: ${p})`);
|
|
60
|
+
}
|
|
61
|
+
if (stat.size > VIDEO_MAX_BYTES) {
|
|
62
|
+
throw new CliError(`${where}: video "${src}" is ${(stat.size / 1024 / 1024).toFixed(0)}MB — max ${VIDEO_MAX_BYTES / 1024 / 1024}MB.`);
|
|
63
|
+
}
|
|
64
|
+
block.file = p;
|
|
65
|
+
block.mime = mime;
|
|
66
|
+
if (!block.title) block.title = path.basename(p);
|
|
67
|
+
return block;
|
|
68
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// The small display blocks: kpi, typography, compare and palette.
|
|
2
|
+
import { CliError } from '../../util.js';
|
|
3
|
+
import { asStr, clampInt } from '../sources.js';
|
|
4
|
+
import { resolveImageSrc } from './image.js';
|
|
5
|
+
import { BLOCK_HEIGHT } from '../constants.js';
|
|
6
|
+
|
|
7
|
+
// Normalizes one kpi block. `id` is the already-assigned block id.
|
|
8
|
+
export function normalizeKpiBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
9
|
+
// Stat cards: a row of big-number metrics, each with an optional delta
|
|
10
|
+
// (up/down tinted) and sublabel. For "revenue ↑12%" at a glance, no chart.
|
|
11
|
+
const rawItems = Array.isArray(rawBlock.items) ? rawBlock.items : [];
|
|
12
|
+
if (!rawItems.length) throw new CliError(`${where}: kpi block needs a non-empty "items" array of {label, value, delta?, dir?, sub?}.`);
|
|
13
|
+
const items = rawItems.map((it, k) => {
|
|
14
|
+
if (it === null || typeof it !== 'object' || Array.isArray(it)) throw new CliError(`${where}.items[${k}]: must be an object {label, value, …}.`);
|
|
15
|
+
const out = { label: asStr(it.label), value: asStr(it.value) };
|
|
16
|
+
if (it.delta !== undefined && it.delta !== null && it.delta !== '') out.delta = asStr(it.delta);
|
|
17
|
+
const dir = asStr(it.dir ?? it.deltaDir).trim().toLowerCase();
|
|
18
|
+
if (dir === 'up' || dir === 'down' || dir === 'flat') out.dir = dir;
|
|
19
|
+
if (it.sub !== undefined) out.sub = asStr(it.sub);
|
|
20
|
+
return out;
|
|
21
|
+
});
|
|
22
|
+
const block = { id, type: 'kpi', items };
|
|
23
|
+
if (rawBlock.title !== undefined) block.title = asStr(rawBlock.title);
|
|
24
|
+
return block;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Normalizes one typography block. `id` is the already-assigned block id.
|
|
28
|
+
export function normalizeTypographyBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
29
|
+
// Type specimens: render sample text at given size/weight/font so a designer
|
|
30
|
+
// can react to type choices the way they react to a palette.
|
|
31
|
+
const raw = Array.isArray(rawBlock.specimens) ? rawBlock.specimens : (Array.isArray(rawBlock.samples) ? rawBlock.samples : []);
|
|
32
|
+
if (!raw.length) throw new CliError(`${where}: typography block needs a non-empty "specimens" array of {label?, size?, weight?, text?}.`);
|
|
33
|
+
const specimens = raw.map((s, k) => {
|
|
34
|
+
if (s === null || typeof s !== 'object' || Array.isArray(s)) throw new CliError(`${where}.specimens[${k}]: must be an object.`);
|
|
35
|
+
const out = { text: asStr(s.text ?? s.sample) || 'The quick brown fox jumps over the lazy dog' };
|
|
36
|
+
if (s.label !== undefined) out.label = asStr(s.label);
|
|
37
|
+
if (s.size !== undefined) out.size = asStr(s.size);
|
|
38
|
+
if (s.weight !== undefined) out.weight = asStr(s.weight);
|
|
39
|
+
if (s.font !== undefined) out.font = asStr(s.font);
|
|
40
|
+
const lh = s.lineHeight ?? s.leading;
|
|
41
|
+
if (lh !== undefined) out.lineHeight = asStr(lh);
|
|
42
|
+
const ls = s.letterSpacing ?? s.tracking;
|
|
43
|
+
if (ls !== undefined) out.letterSpacing = asStr(ls);
|
|
44
|
+
return out;
|
|
45
|
+
});
|
|
46
|
+
const block = { id, type: 'typography', specimens };
|
|
47
|
+
if (rawBlock.title !== undefined) block.title = asStr(rawBlock.title);
|
|
48
|
+
if (rawBlock.font !== undefined) block.font = asStr(rawBlock.font);
|
|
49
|
+
return block;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Normalizes one compare block. `id` is the already-assigned block id.
|
|
53
|
+
export function normalizeCompareBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
54
|
+
// Before/after: two images with a draggable divider to compare a redesign.
|
|
55
|
+
const beforeRaw = rawBlock.before && typeof rawBlock.before === 'object' ? rawBlock.before.src : (rawBlock.before ?? rawBlock.beforeSrc);
|
|
56
|
+
const afterRaw = rawBlock.after && typeof rawBlock.after === 'object' ? rawBlock.after.src : (rawBlock.after ?? rawBlock.afterSrc);
|
|
57
|
+
const block = {
|
|
58
|
+
id, type: 'compare',
|
|
59
|
+
before: resolveImageSrc(beforeRaw, cwd, where, '"before"'),
|
|
60
|
+
after: resolveImageSrc(afterRaw, cwd, where, '"after"'),
|
|
61
|
+
};
|
|
62
|
+
block.beforeLabel = asStr(rawBlock.beforeLabel ?? (rawBlock.before && rawBlock.before.label) ?? 'Before') || 'Before';
|
|
63
|
+
block.afterLabel = asStr(rawBlock.afterLabel ?? (rawBlock.after && rawBlock.after.label) ?? 'After') || 'After';
|
|
64
|
+
if (hasHeight) block.height = clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, undefined);
|
|
65
|
+
return block;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Normalizes one palette block. `id` is the already-assigned block id.
|
|
69
|
+
export function normalizePaletteBlock(rawBlock, id, cwd, where, hasHeight) {
|
|
70
|
+
// Accept either { palettes: [{name, colors:[…]}, …] } or a single-palette
|
|
71
|
+
// shorthand { name?, colors:[…] }. Each palette needs a non-empty colors
|
|
72
|
+
// array of CSS color strings (#rrggbb, rgb(), hsl(), named — kept as given).
|
|
73
|
+
const rawList = Array.isArray(rawBlock.palettes)
|
|
74
|
+
? rawBlock.palettes
|
|
75
|
+
: (Array.isArray(rawBlock.colors) ? [{ name: rawBlock.name, sub: rawBlock.sub, colors: rawBlock.colors }] : null);
|
|
76
|
+
if (!rawList || !rawList.length) {
|
|
77
|
+
throw new CliError(`${where}: palette block needs "palettes" (array) or a "colors" array.`);
|
|
78
|
+
}
|
|
79
|
+
const palettes = rawList.map((p, j) => {
|
|
80
|
+
if (!p || typeof p !== 'object') throw new CliError(`${where}.palettes[${j}]: must be an object with "colors".`);
|
|
81
|
+
const colors = (Array.isArray(p.colors) ? p.colors : [])
|
|
82
|
+
.map((c) => asStr(c).trim())
|
|
83
|
+
.filter(Boolean);
|
|
84
|
+
if (!colors.length) throw new CliError(`${where}.palettes[${j}]: needs a non-empty "colors" array.`);
|
|
85
|
+
const out = { colors };
|
|
86
|
+
if (p.name !== undefined) out.name = asStr(p.name);
|
|
87
|
+
const sub = p.sub ?? p.mood;
|
|
88
|
+
if (sub !== undefined) out.sub = asStr(sub);
|
|
89
|
+
const tag = p.badge ?? p.tag;
|
|
90
|
+
if (tag !== undefined) out.tag = asStr(tag);
|
|
91
|
+
if (p.tagTone !== undefined) out.tagTone = asStr(p.tagTone).trim().toLowerCase();
|
|
92
|
+
if (p.featured === true) out.featured = true;
|
|
93
|
+
return out;
|
|
94
|
+
});
|
|
95
|
+
const block = { id, type: 'palette', palettes };
|
|
96
|
+
if (rawBlock.title !== undefined) block.title = asStr(rawBlock.title);
|
|
97
|
+
return block;
|
|
98
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Limits, allowed values and aliases that the spec readers share.
|
|
2
|
+
|
|
3
|
+
export const TYPES = ['single', 'multi', 'yesno', 'text', 'textarea', 'scale', 'color', 'rank', 'checklist', 'allocate'];
|
|
4
|
+
|
|
5
|
+
export const ALIASES = {
|
|
6
|
+
radio: 'single',
|
|
7
|
+
choice: 'single',
|
|
8
|
+
select: 'single',
|
|
9
|
+
checkbox: 'multi',
|
|
10
|
+
checkboxes: 'multi',
|
|
11
|
+
boolean: 'yesno',
|
|
12
|
+
bool: 'yesno',
|
|
13
|
+
yn: 'yesno',
|
|
14
|
+
input: 'text',
|
|
15
|
+
string: 'text',
|
|
16
|
+
longtext: 'textarea',
|
|
17
|
+
long: 'textarea',
|
|
18
|
+
rating: 'scale',
|
|
19
|
+
likert: 'scale',
|
|
20
|
+
colour: 'color',
|
|
21
|
+
swatch: 'color',
|
|
22
|
+
ranking: 'rank',
|
|
23
|
+
order: 'rank',
|
|
24
|
+
ordering: 'rank',
|
|
25
|
+
prioritize: 'rank',
|
|
26
|
+
sort: 'rank',
|
|
27
|
+
signoff: 'checklist',
|
|
28
|
+
'sign-off': 'checklist',
|
|
29
|
+
qa: 'checklist',
|
|
30
|
+
allocation: 'allocate',
|
|
31
|
+
budget: 'allocate',
|
|
32
|
+
distribute: 'allocate',
|
|
33
|
+
points: 'allocate',
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
export const HTML_HEIGHT = { min: 100, max: 2400, boardDefault: 400, questionDefault: 360 };
|
|
37
|
+
|
|
38
|
+
// Block heights clamp to the same window; defaults vary per block type.
|
|
39
|
+
export const BLOCK_HEIGHT = { min: 100, max: 2400 };
|
|
40
|
+
export const BLOCK_TYPES = ['markdown', 'mermaid', 'graphviz', 'plantuml', 'chart', 'table', 'code', 'diff', 'git-conflict', 'video', 'pdf', 'html', 'image', 'palette', 'kpi', 'typography', 'compare', 'a2ui'];
|
|
41
|
+
export const CHART_KINDS = ['bar', 'line', 'pie', 'doughnut', 'radar', 'scatter'];
|
|
42
|
+
|
|
43
|
+
// code/diff blocks may load their text from a local file (like htmlFile). Caps
|
|
44
|
+
// keep a runaway file from bloating the board payload.
|
|
45
|
+
export const TEXT_FILE_MAX_BYTES = 512 * 1024;
|
|
46
|
+
|
|
47
|
+
// video blocks: a YouTube/Vimeo link embeds via iframe; an http(s) media URL or
|
|
48
|
+
// a local file plays in a <video> element (local files stream from the server,
|
|
49
|
+
// never embedded, so large clips don't bloat the board).
|
|
50
|
+
export const VIDEO_MIMES = {
|
|
51
|
+
mp4: 'video/mp4', m4v: 'video/x-m4v', webm: 'video/webm', ogv: 'video/ogg',
|
|
52
|
+
ogg: 'video/ogg', mov: 'video/quicktime', mkv: 'video/x-matroska',
|
|
53
|
+
};
|
|
54
|
+
export const VIDEO_MAX_BYTES = 512 * 1024 * 1024;
|
|
55
|
+
|
|
56
|
+
export const PDF_MIME = 'application/pdf';
|
|
57
|
+
export const PDF_MAX_BYTES = 512 * 1024 * 1024;
|
|
58
|
+
|
|
59
|
+
// image blocks: local files are embedded as data URIs at spec time (the page
|
|
60
|
+
// then loads them via /img/b/<id>), so boards stay self-contained offline.
|
|
61
|
+
export const IMAGE_MIMES = {
|
|
62
|
+
png: 'image/png', jpg: 'image/jpeg', jpeg: 'image/jpeg', gif: 'image/gif',
|
|
63
|
+
webp: 'image/webp', svg: 'image/svg+xml', avif: 'image/avif', bmp: 'image/bmp',
|
|
64
|
+
};
|
|
65
|
+
export const IMAGE_MAX_BYTES = 8 * 1024 * 1024;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// Public surface of the spec module: normalize, check and describe a board spec.
|
|
2
|
+
export { TYPES, BLOCK_TYPES } from './constants.js';
|
|
3
|
+
export { assertSpecReady } from './ready.js';
|
|
4
|
+
export { normalizeSpec, questionFromInline } from './normalize.js';
|
|
5
|
+
export { SPEC_SCHEMA } from './schema.js';
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
// Turns an agent-written spec into the normalized shape that boards,
|
|
2
|
+
// the server and the MCP App read.
|
|
3
|
+
import { CliError } from '../util.js';
|
|
4
|
+
import { asStr, clampInt, readBlockHtml, readHtml } from './sources.js';
|
|
5
|
+
import { ALIASES, BLOCK_HEIGHT, BLOCK_TYPES, HTML_HEIGHT, TYPES } from './constants.js';
|
|
6
|
+
import { BLOCK_NORMALIZERS } from './blocks/index.js';
|
|
7
|
+
|
|
8
|
+
// Normalizes one block object. `id` is the already-assigned block id.
|
|
9
|
+
// Returns the normalized block (with a guaranteed string `type` + `id`).
|
|
10
|
+
function normalizeBlock(rawBlock, id, cwd, where) {
|
|
11
|
+
if (rawBlock === null || typeof rawBlock !== 'object' || Array.isArray(rawBlock)) {
|
|
12
|
+
throw new CliError(`${where}: must be an object with a "type".`);
|
|
13
|
+
}
|
|
14
|
+
let type = asStr(rawBlock.type).trim().toLowerCase();
|
|
15
|
+
if (!type) throw new CliError(`${where}: missing "type". Valid: ${BLOCK_TYPES.join(', ')}.`);
|
|
16
|
+
if (!BLOCK_TYPES.includes(type)) {
|
|
17
|
+
throw new CliError(`${where}: unknown block type "${rawBlock.type}". Valid: ${BLOCK_TYPES.join(', ')}.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const hasHeight = rawBlock.height !== undefined && rawBlock.height !== null && rawBlock.height !== '';
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// type === 'html'
|
|
55
|
+
const normalizer = BLOCK_NORMALIZERS[type];
|
|
56
|
+
if (normalizer) return normalizer(rawBlock, id, cwd, where, hasHeight);
|
|
57
|
+
|
|
58
|
+
const html = readBlockHtml(rawBlock, cwd, where);
|
|
59
|
+
if (!html) {
|
|
60
|
+
throw new CliError(`${where}: html block needs an "html" string or readable "htmlFile".`);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
id,
|
|
64
|
+
type: 'html',
|
|
65
|
+
html,
|
|
66
|
+
height: clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, 360),
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Builds the normalized block list for a scope (board or question).
|
|
71
|
+
// Legacy html/htmlFile become a PREPENDED html block; ids are assigned in
|
|
72
|
+
// final order with `prefix` ('' for board → b1,b2…; '<qid>-' for questions).
|
|
73
|
+
function buildBlocks(rawObj, cwd, where, prefix) {
|
|
74
|
+
const blocks = [];
|
|
75
|
+
let n = 0;
|
|
76
|
+
const nextId = () => `${prefix}b${++n}`;
|
|
77
|
+
|
|
78
|
+
// Legacy html/htmlFile → a single prepended html block. htmlHeight applies.
|
|
79
|
+
const legacyHtml = readHtml(rawObj, cwd, where);
|
|
80
|
+
if (legacyHtml) {
|
|
81
|
+
const def = prefix ? HTML_HEIGHT.questionDefault : HTML_HEIGHT.boardDefault;
|
|
82
|
+
blocks.push({
|
|
83
|
+
id: nextId(),
|
|
84
|
+
type: 'html',
|
|
85
|
+
html: legacyHtml,
|
|
86
|
+
height: clampInt(rawObj.htmlHeight, HTML_HEIGHT.min, HTML_HEIGHT.max, def),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (rawObj.blocks !== undefined && rawObj.blocks !== null) {
|
|
91
|
+
if (!Array.isArray(rawObj.blocks)) throw new CliError(`${where}.blocks: must be an array.`);
|
|
92
|
+
rawObj.blocks.forEach((b, i) => {
|
|
93
|
+
const nb = normalizeBlock(b, nextId(), cwd, `${where}.blocks[${i}]`);
|
|
94
|
+
// Cross-block fields handled uniformly so every block type supports them:
|
|
95
|
+
// `ref` = a stable name a markdown reference link can open in a modal;
|
|
96
|
+
// `pins` = enable coordinate pin-comments on an image.
|
|
97
|
+
if (b && typeof b === 'object') {
|
|
98
|
+
if (typeof b.ref === 'string' && b.ref.trim()) nb.ref = b.ref.trim();
|
|
99
|
+
if (nb.type === 'image' && b.pins === true) nb.pins = true;
|
|
100
|
+
}
|
|
101
|
+
blocks.push(nb);
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return blocks;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function normalizeSpec(raw, { cwd = process.cwd() } = {}) {
|
|
108
|
+
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
|
|
109
|
+
throw new CliError('Spec must be a JSON object. Run `rly agent` for the schema and examples.');
|
|
110
|
+
}
|
|
111
|
+
const spec = {
|
|
112
|
+
title: asStr(raw.title).trim() || 'Relay',
|
|
113
|
+
intro: asStr(raw.intro),
|
|
114
|
+
blocks: buildBlocks(raw, cwd, 'board', ''),
|
|
115
|
+
allowPartial: raw.allowPartial !== false,
|
|
116
|
+
note: raw.note !== false,
|
|
117
|
+
autoClose: raw.autoClose !== false,
|
|
118
|
+
responseRequired: raw.responseRequired !== false,
|
|
119
|
+
questions: [],
|
|
120
|
+
submitLabel: '',
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
const rawQs = raw.questions == null ? [] : raw.questions;
|
|
124
|
+
if (!Array.isArray(rawQs)) throw new CliError('"questions" must be an array.');
|
|
125
|
+
|
|
126
|
+
const seen = new Set();
|
|
127
|
+
rawQs.forEach((rq, i) => {
|
|
128
|
+
const where = `questions[${i}]`;
|
|
129
|
+
if (rq === null || typeof rq !== 'object' || Array.isArray(rq)) {
|
|
130
|
+
throw new CliError(`${where}: must be an object.`);
|
|
131
|
+
}
|
|
132
|
+
const label = asStr(rq.label ?? rq.question ?? rq.text).trim();
|
|
133
|
+
if (!label) throw new CliError(`${where}: missing "label".`);
|
|
134
|
+
|
|
135
|
+
let type = asStr(rq.type).trim().toLowerCase() || 'text';
|
|
136
|
+
type = ALIASES[type] || type;
|
|
137
|
+
if (!TYPES.includes(type)) {
|
|
138
|
+
throw new CliError(`${where}: unknown type "${rq.type}". Valid: ${TYPES.join(', ')} (plus aliases like radio/checkbox/boolean/rating).`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const id = asStr(rq.id).trim() || `q${i + 1}`;
|
|
142
|
+
if (seen.has(id)) throw new CliError(`${where}: duplicate question id "${id}".`);
|
|
143
|
+
seen.add(id);
|
|
144
|
+
|
|
145
|
+
const q = {
|
|
146
|
+
id,
|
|
147
|
+
type,
|
|
148
|
+
label,
|
|
149
|
+
description: asStr(rq.description),
|
|
150
|
+
required: rq.required === true,
|
|
151
|
+
// Decision types (single/rank/checklist/allocate) show the optional
|
|
152
|
+
// per-answer note by default so the user can qualify their pick; other
|
|
153
|
+
// types stay opt-in. An explicit note:false turns it off.
|
|
154
|
+
note: rq.note === undefined
|
|
155
|
+
? (type === 'single' || type === 'rank' || type === 'checklist' || type === 'allocate')
|
|
156
|
+
: rq.note === true,
|
|
157
|
+
blocks: buildBlocks(rq, cwd, where, `${id}-`),
|
|
158
|
+
placeholder: asStr(rq.placeholder),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
if (type === 'single' || type === 'multi' || type === 'rank' || type === 'checklist' || type === 'allocate') {
|
|
162
|
+
const opts = Array.isArray(rq.options) ? rq.options : [];
|
|
163
|
+
q.options = opts.map((o, j) => {
|
|
164
|
+
if (typeof o === 'string' || typeof o === 'number') {
|
|
165
|
+
return { value: String(o), label: String(o) };
|
|
166
|
+
}
|
|
167
|
+
if (o && typeof o === 'object') {
|
|
168
|
+
const value = asStr(o.value ?? o.label).trim();
|
|
169
|
+
const olabel = asStr(o.label ?? o.value).trim();
|
|
170
|
+
if (!value) throw new CliError(`${where}.options[${j}]: needs "value" or "label".`);
|
|
171
|
+
const out = { value, label: olabel || value };
|
|
172
|
+
if (o.description) out.description = asStr(o.description);
|
|
173
|
+
// Per-option visuals: any block type, rendered inside the option card
|
|
174
|
+
// (ids <qid>-o<n>-b<m>). Lets a choice show its example instead of
|
|
175
|
+
// making the user read-and-guess.
|
|
176
|
+
const oblocks = buildBlocks(o, cwd, `${where}.options[${j}]`, `${id}-o${j + 1}-`);
|
|
177
|
+
if (oblocks.length) out.blocks = oblocks;
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
throw new CliError(`${where}.options[${j}]: must be a string or {value, label, description?, blocks?}.`);
|
|
181
|
+
});
|
|
182
|
+
const minOpts = type === 'rank' ? 2 : 1;
|
|
183
|
+
if (q.options.length < minOpts) {
|
|
184
|
+
throw new CliError(`${where}: type "${type}" needs at least ${minOpts} option${minOpts > 1 ? 's' : ''}.`);
|
|
185
|
+
}
|
|
186
|
+
// Radio (single) questions include an "Other" free-text option by default so
|
|
187
|
+
// the user is never boxed into the listed choices; opt out with other:false.
|
|
188
|
+
// Multi stays opt-in (checkbox lists are usually exhaustive on purpose).
|
|
189
|
+
// rank/checklist/allocate operate over a fixed set, so no "Other".
|
|
190
|
+
if (type === 'single' || type === 'multi') {
|
|
191
|
+
q.other = type === 'single' ? rq.other !== false : rq.other === true;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (type === 'checklist') {
|
|
196
|
+
// Per-item status control. Default Pass / Fail / N/A; override with
|
|
197
|
+
// "statuses" (strings or {value,label,tone?}). tone colors the chip.
|
|
198
|
+
const rawSt = Array.isArray(rq.statuses) && rq.statuses.length
|
|
199
|
+
? rq.statuses
|
|
200
|
+
: [{ value: 'pass', label: 'Pass', tone: 'ok' }, { value: 'fail', label: 'Fail', tone: 'bad' }, { value: 'na', label: 'N/A', tone: 'muted' }];
|
|
201
|
+
q.statuses = rawSt.map((s, k) => {
|
|
202
|
+
if (typeof s === 'string' || typeof s === 'number') {
|
|
203
|
+
const value = String(s).trim();
|
|
204
|
+
return { value, label: value === 'na' ? 'N/A' : value.charAt(0).toUpperCase() + value.slice(1) };
|
|
205
|
+
}
|
|
206
|
+
if (s && typeof s === 'object') {
|
|
207
|
+
const value = asStr(s.value ?? s.label).trim();
|
|
208
|
+
if (!value) throw new CliError(`${where}.statuses[${k}]: needs "value" or "label".`);
|
|
209
|
+
const out = { value, label: asStr(s.label ?? s.value) || value };
|
|
210
|
+
const tone = asStr(s.tone).trim().toLowerCase();
|
|
211
|
+
if (tone) out.tone = tone;
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
214
|
+
throw new CliError(`${where}.statuses[${k}]: must be a string or {value, label, tone?}.`);
|
|
215
|
+
});
|
|
216
|
+
if (q.statuses.length < 2) throw new CliError(`${where}: checklist needs ≥2 "statuses".`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (type === 'allocate') {
|
|
220
|
+
q.total = clampInt(rq.total, 1, 1000000, 100);
|
|
221
|
+
if (rq.unit !== undefined) q.unit = asStr(rq.unit);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (type === 'scale') {
|
|
225
|
+
q.min = clampInt(rq.min, 0, 9, 1);
|
|
226
|
+
q.max = clampInt(rq.max, q.min + 1, 10, Math.max(5, q.min + 1));
|
|
227
|
+
q.minLabel = asStr(rq.minLabel);
|
|
228
|
+
q.maxLabel = asStr(rq.maxLabel);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (type === 'color') {
|
|
232
|
+
// Optional preset swatches the user can click beside the native picker.
|
|
233
|
+
const presets = (Array.isArray(rq.presets) ? rq.presets : [])
|
|
234
|
+
.map((c) => asStr(c).trim())
|
|
235
|
+
.filter(Boolean);
|
|
236
|
+
if (presets.length) q.presets = presets;
|
|
237
|
+
// Richer "pick from a palette": labeled swatch cards. Each click selects
|
|
238
|
+
// that color as the answer and each card is individually commentable.
|
|
239
|
+
// Items: a color string, or {value|color, label?}. Any CSS color system.
|
|
240
|
+
if (Array.isArray(rq.palette) && rq.palette.length) {
|
|
241
|
+
q.palette = rq.palette.map((p, k) => {
|
|
242
|
+
if (typeof p === 'string' || typeof p === 'number') {
|
|
243
|
+
const value = String(p).trim();
|
|
244
|
+
return { value, label: value };
|
|
245
|
+
}
|
|
246
|
+
if (p && typeof p === 'object') {
|
|
247
|
+
const value = asStr(p.value ?? p.color).trim();
|
|
248
|
+
if (!value) throw new CliError(`${where}.palette[${k}]: needs a "value" or "color".`);
|
|
249
|
+
return { value, label: asStr(p.label ?? p.name) || value };
|
|
250
|
+
}
|
|
251
|
+
throw new CliError(`${where}.palette[${k}]: must be a color string or {value/color, label?}.`);
|
|
252
|
+
}).filter((p) => p.value);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (rq.default !== undefined) q.default = rq.default;
|
|
257
|
+
spec.questions.push(q);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
if (!spec.questions.length && !spec.blocks.length) {
|
|
261
|
+
throw new CliError('Spec needs "questions" and/or "blocks"/"html" — nothing to show.');
|
|
262
|
+
}
|
|
263
|
+
if (!spec.responseRequired && spec.questions.length) {
|
|
264
|
+
throw new CliError('responseRequired:false is display-only and cannot contain questions. Use responseRequired:true when answers are needed.');
|
|
265
|
+
}
|
|
266
|
+
spec.submitLabel = asStr(raw.submitLabel).trim() || (spec.questions.length ? 'Submit' : 'Acknowledge');
|
|
267
|
+
return spec;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Inline question syntax for `rly ask -q`: "[!]label::type::opt1,opt2"
|
|
271
|
+
// Leading "!" marks the question required. type defaults to "text".
|
|
272
|
+
export function questionFromInline(s, i) {
|
|
273
|
+
let body = asStr(s).trim();
|
|
274
|
+
let required = false;
|
|
275
|
+
if (body.startsWith('!')) {
|
|
276
|
+
required = true;
|
|
277
|
+
body = body.slice(1).trim();
|
|
278
|
+
}
|
|
279
|
+
const [label, type, options] = body.split('::').map((p) => p.trim());
|
|
280
|
+
if (!label) throw new CliError(`-q #${i + 1}: empty question text.`);
|
|
281
|
+
const q = { label, required };
|
|
282
|
+
if (type) q.type = type;
|
|
283
|
+
if (options) q.options = options.split(',').map((o) => o.trim()).filter(Boolean);
|
|
284
|
+
return q;
|
|
285
|
+
}
|