@khanglvm/relay 0.22.0 → 0.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/docs/AGENT.md +1 -1
  2. package/package.json +1 -1
  3. package/src/cli/args.js +4 -2
  4. package/src/cli/ask.js +4 -3
  5. package/src/cli/boards.js +10 -7
  6. package/src/cli/git.js +5 -3
  7. package/src/cli/help.js +1 -0
  8. package/src/cli/io.js +21 -0
  9. package/src/cli/mcp.js +12 -1
  10. package/src/file-access.js +117 -0
  11. package/src/file-preview.js +1 -1
  12. package/src/mcp/boards.js +4 -3
  13. package/src/mcp/serve.js +4 -4
  14. package/src/mcp/store.js +2 -2
  15. package/src/mcp/tools.js +2 -1
  16. package/src/server/board.js +18 -13
  17. package/src/server/files.js +10 -0
  18. package/src/spec/blocks/a2ui.js +1 -1
  19. package/src/spec/blocks/chart.js +1 -1
  20. package/src/spec/blocks/code.js +4 -4
  21. package/src/spec/blocks/diagram.js +3 -3
  22. package/src/spec/blocks/git-conflict.js +2 -2
  23. package/src/spec/blocks/image.js +6 -5
  24. package/src/spec/blocks/markdown.js +2 -2
  25. package/src/spec/blocks/pdf.js +4 -10
  26. package/src/spec/blocks/table.js +5 -4
  27. package/src/spec/blocks/video.js +4 -10
  28. package/src/spec/blocks/widgets.js +6 -6
  29. package/src/spec/normalize.js +18 -11
  30. package/src/spec/pick.js +4 -4
  31. package/src/spec/sources.js +19 -24
  32. package/src/store.js +6 -1
  33. package/types/file-access.d.ts +62 -0
  34. package/types/mcp/boards.d.ts +2 -1
  35. package/types/mcp/serve.d.ts +5 -2
  36. package/types/mcp/store.d.ts +6 -3
  37. package/types/mcp/tools.d.ts +2 -0
  38. package/types/spec/blocks/a2ui.d.ts +1 -1
  39. package/types/spec/blocks/chart.d.ts +1 -1
  40. package/types/spec/blocks/code.d.ts +2 -2
  41. package/types/spec/blocks/diagram.d.ts +3 -3
  42. package/types/spec/blocks/git-conflict.d.ts +1 -1
  43. package/types/spec/blocks/image.d.ts +2 -2
  44. package/types/spec/blocks/markdown.d.ts +1 -1
  45. package/types/spec/blocks/pdf.d.ts +1 -1
  46. package/types/spec/blocks/table.d.ts +1 -1
  47. package/types/spec/blocks/video.d.ts +1 -1
  48. package/types/spec/blocks/widgets.d.ts +4 -4
  49. package/types/spec/normalize.d.ts +7 -1
  50. package/types/spec/pick.d.ts +1 -1
  51. package/types/spec/sources.d.ts +5 -4
  52. package/types/store.d.ts +8 -1
@@ -1,6 +1,7 @@
1
1
  // The image block, and the image reader that compare blocks share.
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
+ import { resolveReadable } from '../../file-access.js';
4
5
  import { CliError } from '../../util.js';
5
6
  import { asStr, clampInt } from '../sources.js';
6
7
  import { BLOCK_HEIGHT, IMAGE_MAX_BYTES, IMAGE_MIMES } from '../constants.js';
@@ -8,14 +9,14 @@ import { BLOCK_HEIGHT, IMAGE_MAX_BYTES, IMAGE_MIMES } from '../constants.js';
8
9
  // Resolve an image source (shared by the `image` and `compare` blocks): an
9
10
  // http(s)/data URL passes through; a local file is read + embedded as a data
10
11
  // URI (capped) so the board stays self-contained offline.
11
- export function resolveImageSrc(srcRaw, cwd, where, field) {
12
+ export function resolveImageSrc(srcRaw, files, where, field) {
12
13
  const src = asStr(srcRaw).trim();
13
14
  if (!src) throw new CliError(`${where}: ${field} needs a "src" (http(s)/data URL or local file path).`);
14
15
  if (/^(https?:|data:)/i.test(src)) return src;
15
- const p = path.resolve(cwd, src);
16
- const ext = path.extname(p).slice(1).toLowerCase();
16
+ const ext = path.extname(src).slice(1).toLowerCase();
17
17
  const mime = IMAGE_MIMES[ext];
18
18
  if (!mime) throw new CliError(`${where}: ${field}: unsupported image extension ".${ext}" — use ${Object.keys(IMAGE_MIMES).join('/')}, or an http(s)/data URL.`);
19
+ const { file: p } = resolveReadable(files, src, where, field);
19
20
  let buf;
20
21
  try { buf = fs.readFileSync(p); } catch { throw new CliError(`${where}: ${field}: cannot read image "${src}" (resolved: ${p})`); }
21
22
  if (buf.length > IMAGE_MAX_BYTES) throw new CliError(`${where}: ${field}: image "${src}" is ${(buf.length / 1024 / 1024).toFixed(1)}MB — max ${IMAGE_MAX_BYTES / 1024 / 1024}MB.`);
@@ -23,9 +24,9 @@ export function resolveImageSrc(srcRaw, cwd, where, field) {
23
24
  }
24
25
 
25
26
  // Normalizes one image block. `id` is the already-assigned block id.
26
- export function normalizeImageBlock(rawBlock, id, cwd, where, hasHeight) {
27
+ export function normalizeImageBlock(rawBlock, id, files, where, hasHeight) {
27
28
  const block = { id, type: 'image' };
28
- block.src = resolveImageSrc(rawBlock.src ?? rawBlock.file ?? rawBlock.url, cwd, where, 'image block');
29
+ block.src = resolveImageSrc(rawBlock.src ?? rawBlock.file ?? rawBlock.url, files, where, 'image block');
29
30
  if (rawBlock.alt !== undefined) block.alt = asStr(rawBlock.alt);
30
31
  if (hasHeight) block.height = clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, undefined);
31
32
  return block;
@@ -4,8 +4,8 @@ import { clampInt, readTextSource } from '../sources.js';
4
4
  import { BLOCK_HEIGHT } from '../constants.js';
5
5
 
6
6
  // Normalizes one markdown block. `id` is the already-assigned block id.
7
- export function normalizeMarkdownBlock(rawBlock, id, cwd, where, hasHeight) {
8
- const md = readTextSource(rawBlock, 'md', 'mdFile', cwd, where);
7
+ export function normalizeMarkdownBlock(rawBlock, id, files, where, hasHeight) {
8
+ const md = readTextSource(rawBlock, 'md', 'mdFile', files, where);
9
9
  if (!md.trim()) throw new CliError(`${where}: markdown block needs a non-empty "md" string or a readable "mdFile".`);
10
10
  const block = { id, type: 'markdown', md };
11
11
  if (hasHeight) block.height = clampInt(rawBlock.height, BLOCK_HEIGHT.min, BLOCK_HEIGHT.max, undefined);
@@ -1,12 +1,12 @@
1
1
  // The pdf block.
2
- import fs from 'node:fs';
3
2
  import path from 'node:path';
3
+ import { resolveReadable } from '../../file-access.js';
4
4
  import { CliError } from '../../util.js';
5
5
  import { asStr, clampInt } from '../sources.js';
6
6
  import { BLOCK_HEIGHT, PDF_MAX_BYTES, PDF_MIME } from '../constants.js';
7
7
 
8
8
  // Normalizes one pdf block. `id` is the already-assigned block id.
9
- export function normalizePdfBlock(rawBlock, id, cwd, where, hasHeight) {
9
+ export function normalizePdfBlock(rawBlock, id, files, where, hasHeight) {
10
10
  const src = asStr(rawBlock.src ?? rawBlock.file ?? rawBlock.url).trim();
11
11
  if (!src) throw new CliError(`${where}: pdf block needs a "src" (http(s) PDF URL or local .pdf file path).`);
12
12
  const block = { id, type: 'pdf' };
@@ -19,15 +19,9 @@ export function normalizePdfBlock(rawBlock, id, cwd, where, hasHeight) {
19
19
  if (!block.title) block.title = path.basename(src.split(/[?#]/)[0]) || 'PDF';
20
20
  return block;
21
21
  }
22
- const p = path.resolve(cwd, src);
23
- const ext = path.extname(p).slice(1).toLowerCase();
22
+ const ext = path.extname(src).slice(1).toLowerCase();
24
23
  if (ext !== 'pdf') throw new CliError(`${where}: unsupported pdf extension ".${ext}" — use a local .pdf file or an http(s) PDF URL.`);
25
- let stat;
26
- try {
27
- stat = fs.statSync(p);
28
- } catch {
29
- throw new CliError(`${where}: cannot read pdf "${src}" (resolved: ${p})`);
30
- }
24
+ const { file: p, stat } = resolveReadable(files, src, where, 'pdf');
31
25
  if (stat.size > PDF_MAX_BYTES) {
32
26
  throw new CliError(`${where}: pdf "${src}" is ${(stat.size / 1024 / 1024).toFixed(0)}MB — max ${PDF_MAX_BYTES / 1024 / 1024}MB.`);
33
27
  }
@@ -1,6 +1,7 @@
1
1
  // The table block: CSV, TSV and JSON row files, and normalization.
2
2
  import fs from 'node:fs';
3
3
  import path from 'node:path';
4
+ import { resolveReadable } from '../../file-access.js';
4
5
  import { CliError } from '../../util.js';
5
6
  import { BLOCK_HEIGHT, TEXT_FILE_MAX_BYTES } from '../constants.js';
6
7
  import { asStr, clampInt } from '../sources.js';
@@ -38,8 +39,8 @@ function parseDelimited(text, delim) {
38
39
  // Load table {columns, rows} from a local .csv/.tsv/.json file (rowsFile).
39
40
  // CSV/TSV: first row = header → column keys. JSON: an array of objects (columns
40
41
  // from the union of keys) or arrays (needs explicit "columns").
41
- function loadRowsFile(file, cwd, where) {
42
- const p = path.resolve(cwd, file);
42
+ function loadRowsFile(file, files, where) {
43
+ const { file: p } = resolveReadable(files, file, where, 'rowsFile');
43
44
  let buf;
44
45
  try { buf = fs.readFileSync(p); } catch { throw new CliError(`${where}: cannot read rowsFile "${file}" (resolved: ${p})`); }
45
46
  if (buf.length > TEXT_FILE_MAX_BYTES) {
@@ -71,12 +72,12 @@ function loadRowsFile(file, cwd, where) {
71
72
  }
72
73
 
73
74
  // Normalizes one table block. `id` is the already-assigned block id.
74
- export function normalizeTableBlock(rawBlock, id, cwd, where, hasHeight) {
75
+ export function normalizeTableBlock(rawBlock, id, files, where, hasHeight) {
75
76
  // Rows/columns may come from a local .csv/.tsv/.json file instead of inline.
76
77
  let rawColumns = rawBlock.columns;
77
78
  let rawRows = rawBlock.rows;
78
79
  if (typeof rawBlock.rowsFile === 'string' && rawBlock.rowsFile.trim() && !Array.isArray(rawRows)) {
79
- const loaded = loadRowsFile(rawBlock.rowsFile, cwd, where);
80
+ const loaded = loadRowsFile(rawBlock.rowsFile, files, where);
80
81
  rawRows = loaded.rows;
81
82
  if (!Array.isArray(rawColumns) || !rawColumns.length) rawColumns = loaded.columns;
82
83
  }
@@ -1,6 +1,6 @@
1
1
  // The video block: embed detection for YouTube and Vimeo, local and direct media.
2
- import fs from 'node:fs';
3
2
  import path from 'node:path';
3
+ import { resolveReadable } from '../../file-access.js';
4
4
  import { CliError } from '../../util.js';
5
5
  import { asStr, clampInt } from '../sources.js';
6
6
  import { BLOCK_HEIGHT, VIDEO_MAX_BYTES, VIDEO_MIMES } from '../constants.js';
@@ -24,7 +24,7 @@ function parseVideoEmbed(src) {
24
24
  }
25
25
 
26
26
  // Normalizes one video block. `id` is the already-assigned block id.
27
- export function normalizeVideoBlock(rawBlock, id, cwd, where, hasHeight) {
27
+ export function normalizeVideoBlock(rawBlock, id, files, where, hasHeight) {
28
28
  const src = asStr(rawBlock.src ?? rawBlock.file ?? rawBlock.url).trim();
29
29
  if (!src) throw new CliError(`${where}: video block needs a "src" (YouTube/Vimeo URL, http(s) media URL, or local file path).`);
30
30
  const block = { id, type: 'video' };
@@ -46,18 +46,12 @@ export function normalizeVideoBlock(rawBlock, id, cwd, where, hasHeight) {
46
46
  }
47
47
  // local file — kept as an absolute path the server streams (never embedded
48
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();
49
+ const ext = path.extname(src).slice(1).toLowerCase();
51
50
  const mime = VIDEO_MIMES[ext];
52
51
  if (!mime) {
53
52
  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
53
  }
55
- let stat;
56
- try {
57
- stat = fs.statSync(p);
58
- } catch {
59
- throw new CliError(`${where}: cannot read video "${src}" (resolved: ${p})`);
60
- }
54
+ const { file: p, stat } = resolveReadable(files, src, where, 'video');
61
55
  if (stat.size > VIDEO_MAX_BYTES) {
62
56
  throw new CliError(`${where}: video "${src}" is ${(stat.size / 1024 / 1024).toFixed(0)}MB — max ${VIDEO_MAX_BYTES / 1024 / 1024}MB.`);
63
57
  }
@@ -5,7 +5,7 @@ import { resolveImageSrc } from './image.js';
5
5
  import { BLOCK_HEIGHT } from '../constants.js';
6
6
 
7
7
  // Normalizes one kpi block. `id` is the already-assigned block id.
8
- export function normalizeKpiBlock(rawBlock, id, cwd, where, hasHeight) {
8
+ export function normalizeKpiBlock(rawBlock, id, files, where, hasHeight) {
9
9
  // Stat cards: a row of big-number metrics, each with an optional delta
10
10
  // (up/down tinted) and sublabel. For "revenue ↑12%" at a glance, no chart.
11
11
  const rawItems = Array.isArray(rawBlock.items) ? rawBlock.items : [];
@@ -25,7 +25,7 @@ export function normalizeKpiBlock(rawBlock, id, cwd, where, hasHeight) {
25
25
  }
26
26
 
27
27
  // Normalizes one typography block. `id` is the already-assigned block id.
28
- export function normalizeTypographyBlock(rawBlock, id, cwd, where, hasHeight) {
28
+ export function normalizeTypographyBlock(rawBlock, id, files, where, hasHeight) {
29
29
  // Type specimens: render sample text at given size/weight/font so a designer
30
30
  // can react to type choices the way they react to a palette.
31
31
  const raw = Array.isArray(rawBlock.specimens) ? rawBlock.specimens : (Array.isArray(rawBlock.samples) ? rawBlock.samples : []);
@@ -50,14 +50,14 @@ export function normalizeTypographyBlock(rawBlock, id, cwd, where, hasHeight) {
50
50
  }
51
51
 
52
52
  // Normalizes one compare block. `id` is the already-assigned block id.
53
- export function normalizeCompareBlock(rawBlock, id, cwd, where, hasHeight) {
53
+ export function normalizeCompareBlock(rawBlock, id, files, where, hasHeight) {
54
54
  // Before/after: two images with a draggable divider to compare a redesign.
55
55
  const beforeRaw = rawBlock.before && typeof rawBlock.before === 'object' ? rawBlock.before.src : (rawBlock.before ?? rawBlock.beforeSrc);
56
56
  const afterRaw = rawBlock.after && typeof rawBlock.after === 'object' ? rawBlock.after.src : (rawBlock.after ?? rawBlock.afterSrc);
57
57
  const block = {
58
58
  id, type: 'compare',
59
- before: resolveImageSrc(beforeRaw, cwd, where, '"before"'),
60
- after: resolveImageSrc(afterRaw, cwd, where, '"after"'),
59
+ before: resolveImageSrc(beforeRaw, files, where, '"before"'),
60
+ after: resolveImageSrc(afterRaw, files, where, '"after"'),
61
61
  };
62
62
  block.beforeLabel = asStr(rawBlock.beforeLabel ?? (rawBlock.before && rawBlock.before.label) ?? 'Before') || 'Before';
63
63
  block.afterLabel = asStr(rawBlock.afterLabel ?? (rawBlock.after && rawBlock.after.label) ?? 'After') || 'After';
@@ -66,7 +66,7 @@ export function normalizeCompareBlock(rawBlock, id, cwd, where, hasHeight) {
66
66
  }
67
67
 
68
68
  // Normalizes one palette block. `id` is the already-assigned block id.
69
- export function normalizePaletteBlock(rawBlock, id, cwd, where, hasHeight) {
69
+ export function normalizePaletteBlock(rawBlock, id, files, where, hasHeight) {
70
70
  // Accept either { palettes: [{name, colors:[…]}, …] } or a single-palette
71
71
  // shorthand { name?, colors:[…] }. Each palette needs a non-empty colors
72
72
  // array of CSS color strings (#rrggbb, rgb(), hsl(), named — kept as given).
@@ -1,5 +1,6 @@
1
1
  // Turns an agent-written spec into the normalized shape that boards,
2
2
  // the server and the MCP App read.
3
+ import { createFileAccess } from '../file-access.js';
3
4
  import { CliError } from '../util.js';
4
5
  import { asStr, clampInt, readBlockHtml, readHtml } from './sources.js';
5
6
  import { ALIASES, BLOCK_ALIASES, BLOCK_HEIGHT, BLOCK_TYPES, CHART_KINDS, HTML_HEIGHT, TYPES } from './constants.js';
@@ -8,7 +9,7 @@ import { normalizePick } from './pick.js';
8
9
 
9
10
  // Normalizes one block object. `id` is the already-assigned block id.
10
11
  // Returns the normalized block (with a guaranteed string `type` + `id`).
11
- function normalizeBlock(rawBlock, id, cwd, where) {
12
+ function normalizeBlock(rawBlock, id, files, where) {
12
13
  if (rawBlock === null || typeof rawBlock !== 'object' || Array.isArray(rawBlock)) {
13
14
  throw new CliError(`${where}: must be an object with a "type".`);
14
15
  }
@@ -59,9 +60,9 @@ function normalizeBlock(rawBlock, id, cwd, where) {
59
60
 
60
61
  // type === 'html'
61
62
  const normalizer = BLOCK_NORMALIZERS[type];
62
- if (normalizer) return normalizer(rawBlock, id, cwd, where, hasHeight);
63
+ if (normalizer) return normalizer(rawBlock, id, files, where, hasHeight);
63
64
 
64
- const html = readBlockHtml(rawBlock, cwd, where);
65
+ const html = readBlockHtml(rawBlock, files, where);
65
66
  if (!html) {
66
67
  throw new CliError(`${where}: html block needs an "html" string or readable "htmlFile".`);
67
68
  }
@@ -76,13 +77,13 @@ function normalizeBlock(rawBlock, id, cwd, where) {
76
77
  // Builds the normalized block list for a scope (board or question).
77
78
  // Legacy html/htmlFile become a PREPENDED html block; ids are assigned in
78
79
  // final order with `prefix` ('' for board → b1,b2…; '<qid>-' for questions).
79
- function buildBlocks(rawObj, cwd, where, prefix) {
80
+ function buildBlocks(rawObj, files, where, prefix) {
80
81
  const blocks = [];
81
82
  let n = 0;
82
83
  const nextId = () => `${prefix}b${++n}`;
83
84
 
84
85
  // Legacy html/htmlFile → a single prepended html block. htmlHeight applies.
85
- const legacyHtml = readHtml(rawObj, cwd, where);
86
+ const legacyHtml = readHtml(rawObj, files, where);
86
87
  if (legacyHtml) {
87
88
  const def = prefix ? HTML_HEIGHT.questionDefault : HTML_HEIGHT.boardDefault;
88
89
  blocks.push({
@@ -96,7 +97,7 @@ function buildBlocks(rawObj, cwd, where, prefix) {
96
97
  if (rawObj.blocks !== undefined && rawObj.blocks !== null) {
97
98
  if (!Array.isArray(rawObj.blocks)) throw new CliError(`${where}.blocks: must be an array.`);
98
99
  rawObj.blocks.forEach((b, i) => {
99
- const nb = normalizeBlock(b, nextId(), cwd, `${where}.blocks[${i}]`);
100
+ const nb = normalizeBlock(b, nextId(), files, `${where}.blocks[${i}]`);
100
101
  // Cross-block fields handled uniformly so every block type supports them:
101
102
  // `ref` = a stable name a markdown reference link can open in a modal;
102
103
  // `pins` = enable coordinate pin-comments on an image.
@@ -110,14 +111,20 @@ function buildBlocks(rawObj, cwd, where, prefix) {
110
111
  return blocks;
111
112
  }
112
113
 
113
- export function normalizeSpec(raw, { cwd = process.cwd() } = {}) {
114
+ /**
115
+ * @param {Record<string, any>} raw
116
+ * @param {{ cwd?: string, fileRoots?: string[] }} [options] `fileRoots` limits which local files the spec may read
117
+ * (default: `cwd` only); `[]` allows none, for a spec that came from a remote client.
118
+ */
119
+ export function normalizeSpec(raw, { cwd = process.cwd(), fileRoots = [cwd] } = {}) {
120
+ const files = createFileAccess({ cwd, roots: fileRoots });
114
121
  if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
115
122
  throw new CliError('Spec must be a JSON object. Run `rly agent` for the schema and examples.');
116
123
  }
117
124
  const spec = {
118
125
  title: asStr(raw.title).trim() || 'Relay',
119
126
  intro: asStr(raw.intro),
120
- blocks: buildBlocks(raw, cwd, 'board', ''),
127
+ blocks: buildBlocks(raw, files, 'board', ''),
121
128
  allowPartial: raw.allowPartial !== false,
122
129
  note: raw.note !== false,
123
130
  autoClose: raw.autoClose !== false,
@@ -160,7 +167,7 @@ export function normalizeSpec(raw, { cwd = process.cwd() } = {}) {
160
167
  note: rq.note === undefined
161
168
  ? (type === 'single' || type === 'rank' || type === 'checklist' || type === 'allocate')
162
169
  : rq.note === true,
163
- blocks: buildBlocks(rq, cwd, where, `${id}-`),
170
+ blocks: buildBlocks(rq, files, where, `${id}-`),
164
171
  placeholder: asStr(rq.placeholder),
165
172
  };
166
173
 
@@ -179,7 +186,7 @@ export function normalizeSpec(raw, { cwd = process.cwd() } = {}) {
179
186
  // Per-option visuals: any block type, rendered inside the option card
180
187
  // (ids <qid>-o<n>-b<m>). Lets a choice show its example instead of
181
188
  // making the user read-and-guess.
182
- const oblocks = buildBlocks(o, cwd, `${where}.options[${j}]`, `${id}-o${j + 1}-`);
189
+ const oblocks = buildBlocks(o, files, `${where}.options[${j}]`, `${id}-o${j + 1}-`);
183
190
  if (oblocks.length) out.blocks = oblocks;
184
191
  return out;
185
192
  }
@@ -198,7 +205,7 @@ export function normalizeSpec(raw, { cwd = process.cwd() } = {}) {
198
205
  }
199
206
  }
200
207
 
201
- if (type === 'pick') normalizePick(q, rq, where, cwd);
208
+ if (type === 'pick') normalizePick(q, rq, where, files);
202
209
 
203
210
  if (type === 'checklist') {
204
211
  // Per-item status control. Default Pass / Fail / N/A; override with
package/src/spec/pick.js CHANGED
@@ -15,7 +15,7 @@ function httpUrl(raw, where, field) {
15
15
  return url;
16
16
  }
17
17
 
18
- function normalizeTile(raw, j, where, cwd) {
18
+ function normalizeTile(raw, j, where, files) {
19
19
  if (typeof raw === 'string' || typeof raw === 'number') return { id: String(raw), title: String(raw) };
20
20
  if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
21
21
  throw new CliError(`${where}.items[${j}]: must be a string or {id, title, image?, video?, url?, caption?}.`);
@@ -28,7 +28,7 @@ function normalizeTile(raw, j, where, cwd) {
28
28
  const caption = asStr(raw.caption ?? raw.description).trim();
29
29
  if (caption) tile.caption = caption;
30
30
  const image = raw.image ?? raw.src ?? raw.img;
31
- if (image !== undefined && image !== null && image !== '') tile.image = resolveImageSrc(image, cwd, at, '"image"');
31
+ if (image !== undefined && image !== null && image !== '') tile.image = resolveImageSrc(image, files, at, '"image"');
32
32
  if (raw.video !== undefined && raw.video !== null && raw.video !== '') tile.video = httpUrl(raw.video, at, 'video');
33
33
  if (raw.url !== undefined && raw.url !== null && raw.url !== '') tile.url = httpUrl(raw.url, at, 'url');
34
34
  return tile;
@@ -40,11 +40,11 @@ function normalizeTile(raw, j, where, cwd) {
40
40
  * @param {Record<string, any>} q the question being built
41
41
  * @param {Record<string, any>} rq what the agent wrote
42
42
  */
43
- export function normalizePick(q, rq, where, cwd) {
43
+ export function normalizePick(q, rq, where, files) {
44
44
  const rawItems = Array.isArray(rq.items) ? rq.items : Array.isArray(rq.options) ? rq.options : Array.isArray(rq.choices) ? rq.choices : [];
45
45
  if (!rawItems.length) throw new CliError(`${where}: type "pick" needs "items", the tiles to choose among: [{id, title, image?, video?, url?, caption?}].`);
46
46
  if (rawItems.length > PICK.maxItems) throw new CliError(`${where}: a pick takes at most ${PICK.maxItems} items.`);
47
- q.items = rawItems.map((raw, j) => normalizeTile(raw, j, where, cwd));
47
+ q.items = rawItems.map((raw, j) => normalizeTile(raw, j, where, files));
48
48
  const seen = new Set();
49
49
  for (const tile of q.items) {
50
50
  if (seen.has(tile.id)) throw new CliError(`${where}: duplicate item id "${tile.id}".`);
@@ -1,6 +1,6 @@
1
1
  // Readers for text that a block takes inline or from a file next to the spec.
2
2
  import fs from 'node:fs';
3
- import path from 'node:path';
3
+ import { resolveReadable } from '../file-access.js';
4
4
  import { CliError } from '../util.js';
5
5
  import { TEXT_FILE_MAX_BYTES } from './constants.js';
6
6
 
@@ -12,40 +12,35 @@ export function clampInt(v, min, max, def) {
12
12
  return Math.min(max, Math.max(min, n));
13
13
  }
14
14
 
15
- export function readHtml(obj, cwd, where) {
15
+ /** Reads `htmlFile` through the file roots; an inline `html` string wins. */
16
+ export function readHtml(obj, files, where) {
16
17
  if (typeof obj.html === 'string' && obj.html.trim()) return obj.html;
17
- if (typeof obj.htmlFile === 'string' && obj.htmlFile.trim()) {
18
- const p = path.resolve(cwd, obj.htmlFile);
19
- try {
20
- return fs.readFileSync(p, 'utf8');
21
- } catch {
22
- throw new CliError(`${where}: cannot read htmlFile "${obj.htmlFile}" (resolved: ${p})`);
23
- }
24
- }
25
- return '';
18
+ return readHtmlFile(obj, files, where);
26
19
  }
27
20
 
28
21
  // Reads an html-block body from either an inline string or a file path.
29
- export function readBlockHtml(block, cwd, where) {
22
+ export function readBlockHtml(block, files, where) {
30
23
  if (typeof block.html === 'string' && block.html) return block.html;
31
- if (typeof block.htmlFile === 'string' && block.htmlFile.trim()) {
32
- const p = path.resolve(cwd, block.htmlFile);
33
- try {
34
- return fs.readFileSync(p, 'utf8');
35
- } catch {
36
- throw new CliError(`${where}: cannot read htmlFile "${block.htmlFile}" (resolved: ${p})`);
37
- }
24
+ return readHtmlFile(block, files, where);
25
+ }
26
+
27
+ function readHtmlFile(obj, files, where) {
28
+ if (typeof obj.htmlFile !== 'string' || !obj.htmlFile.trim()) return '';
29
+ const { file } = resolveReadable(files, obj.htmlFile, where, 'htmlFile');
30
+ try {
31
+ return fs.readFileSync(file, 'utf8');
32
+ } catch {
33
+ throw new CliError(`${where}: cannot read htmlFile "${obj.htmlFile}" (resolved: ${file})`);
38
34
  }
39
- return '';
40
35
  }
41
36
 
42
37
  // Reads a code/diff block body from an inline string field or a local file.
43
38
  // `inlineKey` is the inline field (e.g. "code"/"diff"); `fileKey` its file
44
39
  // twin (e.g. "codeFile"/"diffFile"). Returns '' when neither is present.
45
- export function readTextSource(block, inlineKey, fileKey, cwd, where) {
40
+ export function readTextSource(block, inlineKey, fileKey, files, where) {
46
41
  if (typeof block[inlineKey] === 'string' && block[inlineKey] !== '') return block[inlineKey];
47
42
  if (typeof block[fileKey] === 'string' && block[fileKey].trim()) {
48
- const p = path.resolve(cwd, block[fileKey]);
43
+ const { file: p } = resolveReadable(files, block[fileKey], where, fileKey);
49
44
  let buf;
50
45
  try {
51
46
  buf = fs.readFileSync(p);
@@ -60,13 +55,13 @@ export function readTextSource(block, inlineKey, fileKey, cwd, where) {
60
55
  return '';
61
56
  }
62
57
 
63
- export function readAnyTextSource(block, inlineKeys, fileKeys, cwd, where, label) {
58
+ export function readAnyTextSource(block, inlineKeys, fileKeys, files, where, label) {
64
59
  for (const key of inlineKeys) {
65
60
  if (typeof block[key] === 'string' && block[key] !== '') return { text: block[key], sourceKey: key };
66
61
  }
67
62
  for (const key of fileKeys) {
68
63
  if (typeof block[key] === 'string' && block[key].trim()) {
69
- const p = path.resolve(cwd, block[key]);
64
+ const { file: p } = resolveReadable(files, block[key], where, label);
70
65
  let buf;
71
66
  try {
72
67
  buf = fs.readFileSync(p);
package/src/store.js CHANGED
@@ -36,7 +36,11 @@ export function saveBoardArtifact(id, bytes, ext = 'png') {
36
36
  return target;
37
37
  }
38
38
 
39
- export function createBoard(spec) {
39
+ /**
40
+ * @param {any} spec a normalized spec
41
+ * @param {{ fileRoots?: string[] }} [options] the roots the spec was read under; the live server opens only files inside them
42
+ */
43
+ export function createBoard(spec, { fileRoots } = {}) {
40
44
  ensureDirs();
41
45
  const record = {
42
46
  id: newId(),
@@ -46,6 +50,7 @@ export function createBoard(spec) {
46
50
  // writes into the spec (markdown links, ~/… paths) resolve against this, so
47
51
  // the live server can open them in the user's default app (POST /api/open).
48
52
  cwd: process.cwd(),
53
+ fileRoots: fileRoots ?? [process.cwd()],
49
54
  spec,
50
55
  draft: null,
51
56
  result: null,
@@ -0,0 +1,62 @@
1
+ import fs from 'node:fs';
2
+ export type FileAccess = {
3
+ /**
4
+ * Directory that relative spec paths resolve against.
5
+ */
6
+ cwd: string;
7
+ /**
8
+ * Real paths a spec may read from; a root may be a single file.
9
+ */
10
+ roots: string[];
11
+ };
12
+ /**
13
+ * @typedef {object} FileAccess
14
+ * @property {string} cwd Directory that relative spec paths resolve against.
15
+ * @property {string[]} roots Real paths a spec may read from; a root may be a single file.
16
+ */
17
+ /** Env var with extra roots, separated by the platform's path delimiter (":" on macOS and Linux). */
18
+ export declare const FILE_ROOTS_ENV = "RLY_FILE_ROOTS";
19
+ /** Expands a leading `~` to the home directory. */
20
+ export declare function expandHome(p: any): any;
21
+ /** Real paths of the roots that exist; a root that does not exist grants nothing. */
22
+ export declare function realRoots(list: any, cwd: any): any[];
23
+ /** Roots named by `RLY_FILE_ROOTS`. */
24
+ export declare function envFileRoots(env?: NodeJS.ProcessEnv): string[];
25
+ /**
26
+ * @param {{ cwd?: string, roots?: string[] }} [options] `roots` of `[]` means no local file may be read.
27
+ * @returns {FileAccess}
28
+ */
29
+ export declare function createFileAccess({ cwd, roots }?: {
30
+ cwd?: string;
31
+ roots?: string[];
32
+ }): FileAccess;
33
+ /** True when `target` is a root or lies under one. Both are real paths. */
34
+ export declare function isInsideRoots(target: any, roots: any): any;
35
+ /**
36
+ * Resolves a path a spec names and says whether it may be read.
37
+ * @param {FileAccess} access
38
+ * @param {unknown} raw the path as written; `~` and relative paths are expanded
39
+ * @param {{ allowDirectory?: boolean }} [options] directories are refused unless a caller opens them in an app
40
+ * @returns {{ ok: boolean, file?: string, stat?: fs.Stats, reason?: 'invalid' | 'outside' | 'missing' | 'not-file', resolved?: string }} `file` and `stat` when ok; otherwise `reason` and the real path it was refused at
41
+ */
42
+ export declare function checkReadable(access: FileAccess, raw: unknown, { allowDirectory }?: {
43
+ allowDirectory?: boolean;
44
+ }): {
45
+ ok: boolean;
46
+ file?: string;
47
+ stat?: fs.Stats;
48
+ reason?: 'invalid' | 'outside' | 'missing' | 'not-file';
49
+ resolved?: string;
50
+ };
51
+ /**
52
+ * The real path of a file a spec names, or a CliError worded for the agent.
53
+ * @param {FileAccess} access
54
+ * @param {unknown} raw
55
+ * @param {string} where which block, e.g. `board.blocks[0]`
56
+ * @param {string} field which field, e.g. `codeFile`
57
+ * @returns {{ file: string, stat: fs.Stats }}
58
+ */
59
+ export declare function resolveReadable(access: FileAccess, raw: unknown, where: string, field: string): {
60
+ file: string;
61
+ stat: fs.Stats;
62
+ };
@@ -5,10 +5,11 @@ export declare const modeOf: (spec: any) => "ask" | "show";
5
5
  * CliError so the tool can answer with an isError result the model can fix.
6
6
  * @param {import('./store.js').RelayStore} store
7
7
  * @param {Record<string, any>} args
8
- * @param {{ displayOnly?: boolean }} [options]
8
+ * @param {{ displayOnly?: boolean, fileRoots?: string[] }} [options] `fileRoots` are the only places a spec may read local files from; none by default
9
9
  */
10
10
  export declare function createMcpBoard(store: import('./store.js').RelayStore, args: Record<string, any>, options?: {
11
11
  displayOnly?: boolean;
12
+ fileRoots?: string[];
12
13
  }): Promise<{
13
14
  record: import("./store.js").BoardRecord;
14
15
  spec: {
@@ -1,5 +1,7 @@
1
1
  /** Serves MCP over this process's stdin and stdout until the host closes stdin. */
2
- export declare function runMcp(): Promise<any>;
2
+ export declare function runMcp({ fileRoots }?: {
3
+ fileRoots?: any[];
4
+ }): Promise<any>;
3
5
  /** A browser Origin is trusted when it is local or the one the operator allowed. A client with no Origin is not a browser. */
4
6
  export declare function originAllowed(origin: any, allowOrigin: any): boolean;
5
7
  /** Constant-time string comparison for the bearer token. */
@@ -8,8 +10,9 @@ export declare function safeEqual(a: any, b: any): boolean;
8
10
  * Serves MCP over Streamable HTTP. Localhost by default; a bearer token and an
9
11
  * allowed Origin are the controls for anything wider.
10
12
  */
11
- export declare function runMcpHttp({ port, host, token, allowOrigin }?: {
13
+ export declare function runMcpHttp({ port, host, token, allowOrigin, fileRoots }?: {
12
14
  allowOrigin?: string;
15
+ fileRoots?: any[];
13
16
  host?: string;
14
17
  port?: number;
15
18
  token?: string;
@@ -18,7 +18,9 @@ export type BoardRecord = {
18
18
  events?: any[];
19
19
  };
20
20
  export type RelayStore = {
21
- create: (spec: any) => BoardRecord | Promise<BoardRecord>;
21
+ create: (spec: any, options?: {
22
+ fileRoots?: string[];
23
+ }) => BoardRecord | Promise<BoardRecord>;
22
24
  get: (id: string) => BoardRecord | null | Promise<BoardRecord | null>;
23
25
  update: (id: string, patch: Partial<BoardRecord>) => BoardRecord | null | Promise<BoardRecord | null>;
24
26
  };
@@ -32,17 +34,18 @@ export type RelayStore = {
32
34
  */
33
35
  /**
34
36
  * @typedef {object} RelayStore
35
- * @property {(spec: any) => BoardRecord | Promise<BoardRecord>} create
37
+ * @property {(spec: any, options?: { fileRoots?: string[] }) => BoardRecord | Promise<BoardRecord>} create
36
38
  * @property {(id: string) => BoardRecord | null | Promise<BoardRecord | null>} get
37
39
  * @property {(id: string, patch: Partial<BoardRecord>) => BoardRecord | null | Promise<BoardRecord | null>} update
38
40
  */
39
41
  /** The default store: one JSON file per board under the relay home, shared with the CLI. */
40
42
  export declare function fileStore(): {
41
- create: (spec: any) => {
43
+ create: (spec: any, options: any) => {
42
44
  id: string;
43
45
  createdAt: string;
44
46
  title: any;
45
47
  cwd: string;
48
+ fileRoots: string[];
46
49
  spec: any;
47
50
  draft: any;
48
51
  result: any;
@@ -4,6 +4,7 @@
4
4
  * @param {any} server an McpServer
5
5
  * @param {object} [options]
6
6
  * @param {import('./store.js').RelayStore} [options.store] where boards live; defaults to the relay home on disk
7
+ * @param {string[]} [options.fileRoots] folders a spec may read local files from (`codeFile`, images, `rowsFile`...); none by default, so a remote client cannot read the server's disk
7
8
  * @param {string | Buffer} [options.tokenKey] the secret behind board tokens; defaults to a key file in the relay home
8
9
  * @param {(name: string) => string} [options.assetUrl] URL of a vendor file, for hosts that can fetch it
9
10
  * @param {(boardId: string) => string} [options.linkUrl] a URL where a person can open the board outside the host
@@ -11,6 +12,7 @@
11
12
  */
12
13
  export declare function registerRelay(server: any, options?: {
13
14
  store?: import('./store.js').RelayStore;
15
+ fileRoots?: string[];
14
16
  tokenKey?: string | Buffer;
15
17
  assetUrl?: (name: string) => string;
16
18
  linkUrl?: (boardId: string) => string;
@@ -1,4 +1,4 @@
1
- export declare function normalizeA2uiBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
1
+ export declare function normalizeA2uiBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
2
2
  id: any;
3
3
  type: string;
4
4
  };
@@ -1,4 +1,4 @@
1
- export declare function normalizeChartBlock(raw: any, id: any, cwd: any, where: any, hasHeight: any): {
1
+ export declare function normalizeChartBlock(raw: any, id: any, files: any, where: any, hasHeight: any): {
2
2
  id: any;
3
3
  type: string;
4
4
  };
@@ -1,9 +1,9 @@
1
- export declare function normalizeCodeBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
1
+ export declare function normalizeCodeBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
2
2
  id: any;
3
3
  type: string;
4
4
  code: any;
5
5
  };
6
- export declare function normalizeDiffBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
6
+ export declare function normalizeDiffBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
7
7
  id: any;
8
8
  type: string;
9
9
  diff: any;
@@ -1,14 +1,14 @@
1
- export declare function normalizeMermaidBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
1
+ export declare function normalizeMermaidBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
2
2
  id: any;
3
3
  type: string;
4
4
  code: string;
5
5
  };
6
- export declare function normalizeGraphvizBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
6
+ export declare function normalizeGraphvizBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
7
7
  id: any;
8
8
  type: string;
9
9
  dot: string;
10
10
  };
11
- export declare function normalizePlantumlBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
11
+ export declare function normalizePlantumlBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
12
12
  id: any;
13
13
  type: string;
14
14
  code: string;
@@ -1,4 +1,4 @@
1
- export declare function normalizeGitConflictBlock(rawBlock: any, id: any, cwd: any, where: any, hasHeight: any): {
1
+ export declare function normalizeGitConflictBlock(rawBlock: any, id: any, files: any, where: any, hasHeight: any): {
2
2
  id: any;
3
3
  type: string;
4
4
  filename: string;