@khanglvm/relay 0.19.0 → 0.20.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.
- package/README.md +11 -4
- package/bin/rly.js +1 -1
- package/package.json +4 -4
- 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 +91 -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 +1 -1
- package/src/link.js +57 -9
- 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 +12 -18
- package/src/{server.js → server/board.js} +16 -779
- 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/store.js +1 -1
- 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/index.d.ts +1 -1
- 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.d.ts → spec/schema.d.ts} +0 -20
- package/types/spec/sources.d.ts +14 -0
- package/types/ui/manifest.d.ts +62 -0
- package/src/cli.js +0 -1767
- package/src/mcp-ui/board.js +0 -1162
- package/src/spec.js +0 -1224
- package/src/ui/annotate.js +0 -1055
- package/src/ui/app.js +0 -1643
- package/src/ui/blocks.js +0 -3260
package/src/cli/wait.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
// rly wait and rly result: block for a board's answer, or read it.
|
|
2
|
+
import { controlUrl, isAlive, loadBoard, loadRunning } from '../store.js';
|
|
3
|
+
import { CliError, sleep } from '../util.js';
|
|
4
|
+
import { DEFAULT_WAIT_TIMEOUT_SEC } from './context.js';
|
|
5
|
+
import { exitCodeFor, mustLoad, printJson, printResult, runNotifyCmd } from './io.js';
|
|
6
|
+
|
|
7
|
+
// Best-effort fetch of /api/presence for a running board. Returns the parsed
|
|
8
|
+
// presence object, or null on any failure / timeout (500ms cap via
|
|
9
|
+
// AbortController). Never throws.
|
|
10
|
+
async function fetchPresence(url) {
|
|
11
|
+
if (!url) return null;
|
|
12
|
+
const controller = new AbortController();
|
|
13
|
+
const timer = setTimeout(() => controller.abort(), 500);
|
|
14
|
+
try {
|
|
15
|
+
const res = await fetch(new URL('/api/presence', url), { signal: controller.signal });
|
|
16
|
+
if (!res.ok) return null;
|
|
17
|
+
return await res.json();
|
|
18
|
+
} catch {
|
|
19
|
+
return null;
|
|
20
|
+
} finally {
|
|
21
|
+
clearTimeout(timer);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function cmdWait(args) {
|
|
26
|
+
const id = args._[0];
|
|
27
|
+
if (!id) throw new CliError('usage: rly wait <board-id> [--timeout <sec>] [--while-active] [--idle-grace <sec>] [--notify-cmd <cmd>]');
|
|
28
|
+
const timeoutSec = args.timeout !== undefined ? Math.max(0, Number.parseInt(args.timeout, 10) || 0) : DEFAULT_WAIT_TIMEOUT_SEC;
|
|
29
|
+
const whileActive = args.whileActive === true;
|
|
30
|
+
const idleGrace = args.idleGrace !== undefined ? Math.max(0, Number.parseInt(args.idleGrace, 10) || 0) : 180;
|
|
31
|
+
const notifyCmd = typeof args.notifyCmd === 'string' && args.notifyCmd.trim() ? args.notifyCmd : null;
|
|
32
|
+
let deadline = timeoutSec === 0 ? Number.POSITIVE_INFINITY : Date.now() + timeoutSec * 1000;
|
|
33
|
+
mustLoad(id);
|
|
34
|
+
|
|
35
|
+
// Push-wake: run the agent's --notify-cmd after a TERMINAL result, then print.
|
|
36
|
+
const finishResult = (result) => {
|
|
37
|
+
if (notifyCmd) runNotifyCmd(notifyCmd, result);
|
|
38
|
+
printResult(result);
|
|
39
|
+
return exitCodeFor(result.status);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
while (Date.now() < deadline) {
|
|
43
|
+
const record = mustLoad(id);
|
|
44
|
+
if (record.result && record.result.finishedAt) {
|
|
45
|
+
return finishResult(record.result);
|
|
46
|
+
}
|
|
47
|
+
const running = loadRunning(id);
|
|
48
|
+
if (!running || !isAlive(running.pid)) {
|
|
49
|
+
await sleep(700); // the result write may be racing the process exit
|
|
50
|
+
const again = loadBoard(id);
|
|
51
|
+
if (again?.result?.finishedAt) {
|
|
52
|
+
return finishResult(again.result);
|
|
53
|
+
}
|
|
54
|
+
printResult({
|
|
55
|
+
status: 'lost',
|
|
56
|
+
boardId: id,
|
|
57
|
+
draft: again?.draft ?? null,
|
|
58
|
+
error: 'board server exited without writing a result',
|
|
59
|
+
});
|
|
60
|
+
return 5;
|
|
61
|
+
}
|
|
62
|
+
if (Date.now() >= deadline) break;
|
|
63
|
+
await sleep(400);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Deadline hit while the board is still OPEN. Fetch presence (cheaply).
|
|
67
|
+
const running = loadRunning(id);
|
|
68
|
+
const presence = running && isAlive(running.pid) ? await fetchPresence(controlUrl(running)) : null;
|
|
69
|
+
|
|
70
|
+
// --while-active: if the user is present + recently active, EXTEND and keep
|
|
71
|
+
// waiting (repeat indefinitely while they stay active).
|
|
72
|
+
if (
|
|
73
|
+
whileActive &&
|
|
74
|
+
presence &&
|
|
75
|
+
presence.seen &&
|
|
76
|
+
(presence.visible || presence.focused) &&
|
|
77
|
+
presence.secondsSinceActivity < idleGrace
|
|
78
|
+
) {
|
|
79
|
+
deadline = Date.now() + Math.min(idleGrace, 120) * 1000;
|
|
80
|
+
return cmdWaitLoop(id, deadline, { whileActive, idleGrace, notifyCmd });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const out = {
|
|
84
|
+
status: 'wait-timeout',
|
|
85
|
+
boardId: id,
|
|
86
|
+
hint: `board is still open — run \`rly wait ${id}\` again, or \`rly result ${id}\` to peek at the live draft`,
|
|
87
|
+
};
|
|
88
|
+
if (presence) out.presence = presence;
|
|
89
|
+
printJson(out);
|
|
90
|
+
return 2;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Continuation loop for `rly wait --while-active` after a deadline extension.
|
|
94
|
+
// Identical waiting logic to cmdWait's main loop, then re-evaluates presence;
|
|
95
|
+
// extends again while the user stays active, otherwise emits wait-timeout.
|
|
96
|
+
async function cmdWaitLoop(id, deadline, opts) {
|
|
97
|
+
const { whileActive, idleGrace, notifyCmd } = opts;
|
|
98
|
+
const finishResult = (result) => {
|
|
99
|
+
if (notifyCmd) runNotifyCmd(notifyCmd, result);
|
|
100
|
+
printResult(result);
|
|
101
|
+
return exitCodeFor(result.status);
|
|
102
|
+
};
|
|
103
|
+
while (Date.now() < deadline) {
|
|
104
|
+
const record = mustLoad(id);
|
|
105
|
+
if (record.result && record.result.finishedAt) {
|
|
106
|
+
return finishResult(record.result);
|
|
107
|
+
}
|
|
108
|
+
const running = loadRunning(id);
|
|
109
|
+
if (!running || !isAlive(running.pid)) {
|
|
110
|
+
await sleep(700);
|
|
111
|
+
const again = loadBoard(id);
|
|
112
|
+
if (again?.result?.finishedAt) {
|
|
113
|
+
return finishResult(again.result);
|
|
114
|
+
}
|
|
115
|
+
printResult({
|
|
116
|
+
status: 'lost',
|
|
117
|
+
boardId: id,
|
|
118
|
+
draft: again?.draft ?? null,
|
|
119
|
+
error: 'board server exited without writing a result',
|
|
120
|
+
});
|
|
121
|
+
return 5;
|
|
122
|
+
}
|
|
123
|
+
if (Date.now() >= deadline) break;
|
|
124
|
+
await sleep(400);
|
|
125
|
+
}
|
|
126
|
+
const running = loadRunning(id);
|
|
127
|
+
const presence = running && isAlive(running.pid) ? await fetchPresence(controlUrl(running)) : null;
|
|
128
|
+
if (
|
|
129
|
+
whileActive &&
|
|
130
|
+
presence &&
|
|
131
|
+
presence.seen &&
|
|
132
|
+
(presence.visible || presence.focused) &&
|
|
133
|
+
presence.secondsSinceActivity < idleGrace
|
|
134
|
+
) {
|
|
135
|
+
const next = Date.now() + Math.min(idleGrace, 120) * 1000;
|
|
136
|
+
return cmdWaitLoop(id, next, opts);
|
|
137
|
+
}
|
|
138
|
+
const out = {
|
|
139
|
+
status: 'wait-timeout',
|
|
140
|
+
boardId: id,
|
|
141
|
+
hint: `board is still open — run \`rly wait ${id}\` again, or \`rly result ${id}\` to peek at the live draft`,
|
|
142
|
+
};
|
|
143
|
+
if (presence) out.presence = presence;
|
|
144
|
+
printJson(out);
|
|
145
|
+
return 2;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function cmdResult(args) {
|
|
149
|
+
const record = mustLoad(args._[0]);
|
|
150
|
+
const sideReviews = {
|
|
151
|
+
referenceOnly: true,
|
|
152
|
+
note: 'Side reviews are reference only. Wait for the board owner (or owner-authorized collaborator) to submit the final answer.',
|
|
153
|
+
submissions: Array.isArray(record.sideReviews) ? record.sideReviews : [],
|
|
154
|
+
drafts: record.reviewDrafts && typeof record.reviewDrafts === 'object' ? record.reviewDrafts : {},
|
|
155
|
+
};
|
|
156
|
+
if (record.result && record.result.finishedAt) {
|
|
157
|
+
printResult({ ...record.result, sideReviews });
|
|
158
|
+
return exitCodeFor(record.result.status);
|
|
159
|
+
}
|
|
160
|
+
const running = loadRunning(record.id);
|
|
161
|
+
if (running && isAlive(running.pid)) {
|
|
162
|
+
// While open, expose the real-time autosaved draft so agents can peek, plus
|
|
163
|
+
// best-effort presence (whether the user is still viewing/focused/active).
|
|
164
|
+
const out = { status: 'open', boardId: record.id, url: running.url, draft: record.draft ?? null, sideReviews };
|
|
165
|
+
const presence = await fetchPresence(controlUrl(running));
|
|
166
|
+
if (presence) out.presence = presence;
|
|
167
|
+
printResult(out); // draft can hold many annotations — sidecar it too
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
printResult({ status: 'lost', boardId: record.id, draft: record.draft ?? null, sideReviews });
|
|
171
|
+
return 5;
|
|
172
|
+
}
|
package/src/file-preview.js
CHANGED
package/src/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Public library entry (`import ... from '@khanglvm/relay'`). It exposes the
|
|
2
2
|
// spec checks and the board store without starting a server or a browser.
|
|
3
3
|
import { compileExpress, validateSurface } from './a2ui/compile.js';
|
|
4
|
-
import { normalizeSpec } from './spec.js';
|
|
4
|
+
import { normalizeSpec } from './spec/index.js';
|
|
5
5
|
import { createBoard as storeBoard } from './store.js';
|
|
6
6
|
|
|
7
7
|
/**
|
package/src/link.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// Where a board can be opened from other devices. By default a board lives on a
|
|
2
|
-
// loopback port. A link setting turns that into a URL a phone or another
|
|
3
|
-
// reach: through
|
|
4
|
-
// or through a
|
|
2
|
+
// loopback port. A link setting turns that into a URL a phone or another computer
|
|
3
|
+
// can reach: through a URL template for any reverse proxy or tunnel the user
|
|
4
|
+
// runs, or through a link-provider command that publishes the board's port and
|
|
5
|
+
// prints the https URL. `devlink` is one such provider, with a built-in preset.
|
|
5
6
|
// Nothing here is required; with no setting Relay behaves as before.
|
|
6
7
|
import fs from 'node:fs';
|
|
7
8
|
import os from 'node:os';
|
|
@@ -10,18 +11,23 @@ import { execFile } from 'node:child_process';
|
|
|
10
11
|
import { CliError } from './util.js';
|
|
11
12
|
|
|
12
13
|
const DEVLINK_TIMEOUT_MS = 30_000;
|
|
13
|
-
export const LINK_MODES = ['off', 'auto', 'devlink'];
|
|
14
|
+
export const LINK_MODES = ['off', 'auto', 'devlink', 'command'];
|
|
14
15
|
|
|
15
16
|
const LOOPBACK_HOST = /^(localhost|0\.0\.0\.0|127(?:\.\d{1,3}){3}|\[?::1?\]?)$/i;
|
|
16
17
|
|
|
17
18
|
export const isLoopbackHost = (host) => LOOPBACK_HOST.test(String(host || ''));
|
|
18
19
|
|
|
19
|
-
/** '
|
|
20
|
+
/** 'off' | 'auto' | 'devlink' | 'command:<publish command>' | an https URL template -> a normalized setting. */
|
|
20
21
|
export function parseLinkValue(raw, where = 'link') {
|
|
21
22
|
const value = String(raw ?? '').trim();
|
|
22
23
|
const lower = value.toLowerCase();
|
|
23
24
|
if (lower === 'off' || lower === 'local') return { mode: 'off' };
|
|
24
25
|
if (lower === 'auto' || lower === 'devlink') return { mode: lower };
|
|
26
|
+
if (/^command:/i.test(value)) {
|
|
27
|
+
const publish = value.slice('command:'.length).trim();
|
|
28
|
+
if (!publish) throw new CliError(`${where}: command: needs a command, for example command:my-publisher {port}.`);
|
|
29
|
+
return { mode: 'command', publish };
|
|
30
|
+
}
|
|
25
31
|
if (/^http:\/\//i.test(value)) throw new CliError(`${where}: use https://, because the board's share token travels in the URL.`);
|
|
26
32
|
if (/^https:\/\//i.test(value)) {
|
|
27
33
|
let host;
|
|
@@ -29,18 +35,20 @@ export function parseLinkValue(raw, where = 'link') {
|
|
|
29
35
|
if (isLoopbackHost(host)) throw new CliError(`${where}: ${host} only works on this machine. Use a host other devices can reach.`);
|
|
30
36
|
return { mode: 'url', template: value.replace(/\/+$/, '') };
|
|
31
37
|
}
|
|
32
|
-
throw new CliError(`${where}: use off, auto, devlink, or an https:// URL (with {port} for the board port).`);
|
|
38
|
+
throw new CliError(`${where}: use off, auto, devlink, command:<link provider command>, or an https:// URL (with {port} for the board port).`);
|
|
33
39
|
}
|
|
34
40
|
|
|
35
41
|
/**
|
|
36
|
-
* Picks the link setting. Highest first: --no-devlink, --devlink, --
|
|
37
|
-
* RLY_PUBLIC_URL, RLY_LINK, the saved config, then off.
|
|
42
|
+
* Picks the link setting. Highest first: --no-devlink, --devlink, --link-command,
|
|
43
|
+
* RLY_LINK_COMMAND, --public-url, RLY_PUBLIC_URL, RLY_LINK, the saved config, then off.
|
|
38
44
|
* @param {{ args?: Record<string, any>, env?: Record<string, string | undefined>, config?: Record<string, any> }} [input]
|
|
39
45
|
*/
|
|
40
46
|
export function linkSetting({ args = {}, env = process.env, config = {} } = {}) {
|
|
41
47
|
if (args.noDevlink === true) return { mode: 'off' };
|
|
42
48
|
if (args.devlink === true) return { mode: 'devlink' };
|
|
49
|
+
if (typeof args.linkCommand === 'string') return parseLinkValue(`command:${args.linkCommand}`, '--link-command');
|
|
43
50
|
if (typeof args.publicUrl === 'string') return parseLinkValue(args.publicUrl, '--public-url');
|
|
51
|
+
if (env.RLY_LINK_COMMAND && typeof args.publicUrl !== 'string') return parseLinkValue(`command:${env.RLY_LINK_COMMAND}`, 'RLY_LINK_COMMAND');
|
|
44
52
|
if (env.RLY_PUBLIC_URL) return parseLinkValue(env.RLY_PUBLIC_URL, 'RLY_PUBLIC_URL');
|
|
45
53
|
if (env.RLY_LINK) return parseLinkValue(env.RLY_LINK, 'RLY_LINK');
|
|
46
54
|
if (config.link) return parseLinkValue(config.link, 'config link');
|
|
@@ -48,7 +56,7 @@ export function linkSetting({ args = {}, env = process.env, config = {} } = {})
|
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
/** The setting in the one-string form `__serve` takes on its command line. */
|
|
51
|
-
export const serializeLink = (s) => (s.mode === 'url' ? s.template : s.mode);
|
|
59
|
+
export const serializeLink = (s) => (s.mode === 'url' ? s.template : s.mode === 'command' ? `command:${s.publish}` : s.mode);
|
|
52
60
|
|
|
53
61
|
/** devlink's own binary: RLY_DEVLINK_BIN, then PATH, then its usual install folder. */
|
|
54
62
|
export function findDevlink({ env = process.env, exists = fs.existsSync } = {}) {
|
|
@@ -70,6 +78,27 @@ export function pickDevlinkUrl(stdout) {
|
|
|
70
78
|
return parsed.origin;
|
|
71
79
|
}
|
|
72
80
|
|
|
81
|
+
/**
|
|
82
|
+
* The verified private https origin out of a link provider's output: a plain
|
|
83
|
+
* URL, or JSON with `url` or `records[].url`.
|
|
84
|
+
*/
|
|
85
|
+
export function pickLinkUrl(stdout, name = 'the link command') {
|
|
86
|
+
const text = String(stdout || '').trim();
|
|
87
|
+
let url = text.split('\n').pop().trim();
|
|
88
|
+
if (text.startsWith('{')) {
|
|
89
|
+
let data;
|
|
90
|
+
try { data = JSON.parse(text); } catch { throw new CliError(`${name} printed something that is not JSON.`, 1); }
|
|
91
|
+
url = typeof data.url === 'string' ? data.url : Array.isArray(data.records) ? data.records.map((r) => r && r.url).find((u) => typeof u === 'string') : '';
|
|
92
|
+
}
|
|
93
|
+
if (!url) throw new CliError(`${name} returned no URL for this board.`, 1);
|
|
94
|
+
let parsed;
|
|
95
|
+
try { parsed = new URL(url); } catch { throw new CliError(`${name} returned "${url}", which is not a URL.`, 1); }
|
|
96
|
+
if (parsed.protocol !== 'https:' || isLoopbackHost(parsed.hostname)) throw new CliError(`${name} returned ${url}, which is not a private https URL.`, 1);
|
|
97
|
+
return parsed.origin;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const argvFor = (command, port) => String(command).trim().split(/\s+/).map((part) => part.replaceAll('{port}', String(port)));
|
|
101
|
+
|
|
73
102
|
const runFile = (file, args) => new Promise((resolve, reject) => {
|
|
74
103
|
execFile(file, args, { timeout: DEVLINK_TIMEOUT_MS, maxBuffer: 1 << 20 }, (err, stdout, stderr) => {
|
|
75
104
|
if (err) reject(Object.assign(err, { stderr }));
|
|
@@ -89,6 +118,25 @@ export async function resolveLink(setting, port, { env = process.env, exec = run
|
|
|
89
118
|
const origin = new URL(setting.template.replace('{port}', String(port))).origin;
|
|
90
119
|
return { kind: 'url', origin, release: () => {} };
|
|
91
120
|
}
|
|
121
|
+
if (setting.mode === 'command') {
|
|
122
|
+
const [file, ...rest] = argvFor(setting.publish, port);
|
|
123
|
+
const hasPort = setting.publish.includes('{port}');
|
|
124
|
+
let out;
|
|
125
|
+
try {
|
|
126
|
+
out = await exec(file, hasPort ? rest : [...rest, String(port)]);
|
|
127
|
+
} catch (err) {
|
|
128
|
+
const detail = String((err && (err.stderr || err.message)) || err).trim().split('\n').pop();
|
|
129
|
+
throw new CliError(`the link command could not publish port ${port}: ${detail}`, 1);
|
|
130
|
+
}
|
|
131
|
+
const origin = pickLinkUrl(out.stdout);
|
|
132
|
+
const releaseCommand = env.RLY_LINK_RELEASE_COMMAND;
|
|
133
|
+
const release = () => {
|
|
134
|
+
if (!releaseCommand) return;
|
|
135
|
+
const [rfile, ...rrest] = argvFor(releaseCommand, port);
|
|
136
|
+
exec(rfile, releaseCommand.includes('{port}') ? rrest : [...rrest, String(port)]).catch(() => {});
|
|
137
|
+
};
|
|
138
|
+
return { kind: 'command', origin, release };
|
|
139
|
+
}
|
|
92
140
|
const bin = findDevlink({ env, exists });
|
|
93
141
|
if (!bin) {
|
|
94
142
|
if (setting.mode === 'auto') return null;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function blockCtx(questionId) {
|
|
2
|
+
return {
|
|
3
|
+
theme: effectiveTheme,
|
|
4
|
+
htmlSrc: (blockId) => htmlBlobSrc(blockId),
|
|
5
|
+
questionId: questionId == null ? null : questionId,
|
|
6
|
+
// No annotation during the streaming preview (it re-renders); on the final
|
|
7
|
+
// interactive render, wire the engine so blocks register their targets.
|
|
8
|
+
annotate: composing || (spec && spec.responseRequired === false) ? null : Annotate,
|
|
9
|
+
canComment: Boolean(spec && spec.responseRequired !== false),
|
|
10
|
+
saveArtifact: async ({ dataUrl, mime, width, height }) => {
|
|
11
|
+
const comma = typeof dataUrl === 'string' ? dataUrl.indexOf(',') : -1;
|
|
12
|
+
if (comma < 0) throw new Error('invalid image crop');
|
|
13
|
+
return { data: dataUrl.slice(comma + 1), mime: mime || 'image/png', width, height };
|
|
14
|
+
},
|
|
15
|
+
edits: state.blockEdits,
|
|
16
|
+
onBlockEdit: (blockId, codeOrNull) => {
|
|
17
|
+
if (codeOrNull === null || codeOrNull === undefined) delete state.blockEdits[blockId];
|
|
18
|
+
else state.blockEdits[blockId] = codeOrNull;
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
document.addEventListener('relay:block-edit', (e) => {
|
|
23
|
+
const d = e.detail || {};
|
|
24
|
+
if (!d.blockId) return;
|
|
25
|
+
if (d.value === null || d.value === undefined) delete state.blockEdits[d.blockId];
|
|
26
|
+
else state.blockEdits[d.blockId] = d.value;
|
|
27
|
+
});
|
|
28
|
+
function renderBlocks(container, blocks, questionId) {
|
|
29
|
+
if (!Array.isArray(blocks) || !blocks.length) return;
|
|
30
|
+
const target = el('div', { class: 'blocks' });
|
|
31
|
+
container.append(target);
|
|
32
|
+
if (!window.RelayBlocks) {
|
|
33
|
+
target.append(el('div', { class: 'blk' }, el('div', { class: 'blk-error' }, 'block failed to render')));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
Promise.resolve()
|
|
37
|
+
.then(() => window.RelayBlocks.render(target, blocks, blockCtx(questionId)))
|
|
38
|
+
.then(reportSize)
|
|
39
|
+
.catch(() => {
|
|
40
|
+
target.append(el('div', { class: 'blk' }, el('div', { class: 'blk-error' }, 'block failed to render')));
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
// ======================================================================
|
|
2
|
+
// MCP Apps postMessage / JSON-RPC bridge
|
|
3
|
+
// ======================================================================
|
|
4
|
+
const pending = new Map();
|
|
5
|
+
let rpcSeq = 0;
|
|
6
|
+
const notifyHandlers = Object.create(null);
|
|
7
|
+
function onNotify(method, fn) {
|
|
8
|
+
(notifyHandlers[method] || (notifyHandlers[method] = [])).push(fn);
|
|
9
|
+
}
|
|
10
|
+
function post(msg) {
|
|
11
|
+
try {
|
|
12
|
+
(window.parent && window.parent !== window ? window.parent : window).postMessage(msg, '*');
|
|
13
|
+
} catch {
|
|
14
|
+
// host frame gone — nothing we can do
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function request(method, params) {
|
|
18
|
+
const id = 'rly-' + (++rpcSeq);
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
pending.set(id, { resolve, reject });
|
|
21
|
+
post({ jsonrpc: '2.0', id, method, params: params || {} });
|
|
22
|
+
// Don't hang forever if a host ignores a method — resolve-less reject.
|
|
23
|
+
setTimeout(() => {
|
|
24
|
+
if (pending.has(id)) {
|
|
25
|
+
pending.delete(id);
|
|
26
|
+
reject(new Error('timeout: ' + method));
|
|
27
|
+
}
|
|
28
|
+
}, 15000);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function notify(method, params) {
|
|
32
|
+
post({ jsonrpc: '2.0', method, params: params || {} });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
window.addEventListener('message', (e) => {
|
|
36
|
+
const msg = e.data;
|
|
37
|
+
if (!msg || typeof msg !== 'object' || msg.jsonrpc !== '2.0') return;
|
|
38
|
+
// A response to one of our requests.
|
|
39
|
+
if (msg.id !== undefined && msg.id !== null && (('result' in msg) || ('error' in msg))) {
|
|
40
|
+
const p = pending.get(msg.id);
|
|
41
|
+
if (!p) return;
|
|
42
|
+
pending.delete(msg.id);
|
|
43
|
+
if (msg.error) p.reject(Object.assign(new Error(msg.error.message || 'rpc error'), { rpc: msg.error }));
|
|
44
|
+
else p.resolve(msg.result);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
// A notification or request FROM the host.
|
|
48
|
+
if (typeof msg.method === 'string') {
|
|
49
|
+
const hs = notifyHandlers[msg.method] || [];
|
|
50
|
+
for (const h of hs) {
|
|
51
|
+
try { h(msg.params || {}, msg); } catch { /* handler errors never break the bridge */ }
|
|
52
|
+
}
|
|
53
|
+
// Host requests we must acknowledge.
|
|
54
|
+
if (msg.id !== undefined && msg.id !== null) {
|
|
55
|
+
if (msg.method === 'ui/resource-teardown') post({ jsonrpc: '2.0', id: msg.id, result: {} });
|
|
56
|
+
else post({ jsonrpc: '2.0', id: msg.id, result: {} });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// ======================================================================
|
|
62
|
+
// theme + size
|
|
63
|
+
// ======================================================================
|
|
64
|
+
let host = {};
|
|
65
|
+
function effectiveTheme() {
|
|
66
|
+
if (host.theme === 'dark' || host.theme === 'light') return host.theme;
|
|
67
|
+
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
|
68
|
+
}
|
|
69
|
+
function applyTheme() {
|
|
70
|
+
const t = host.theme;
|
|
71
|
+
if (t === 'dark' || t === 'light') {
|
|
72
|
+
document.documentElement.dataset.theme = t;
|
|
73
|
+
// Pin color-scheme so the host's light-dark() style variables resolve to
|
|
74
|
+
// the side the host actually picked (not whatever the OS prefers).
|
|
75
|
+
document.documentElement.style.colorScheme = t;
|
|
76
|
+
} else {
|
|
77
|
+
delete document.documentElement.dataset.theme;
|
|
78
|
+
document.documentElement.style.colorScheme = 'light dark';
|
|
79
|
+
}
|
|
80
|
+
if (window.RelayBlocks && typeof RelayBlocks.onThemeChange === 'function') {
|
|
81
|
+
try { RelayBlocks.onThemeChange(effectiveTheme()); } catch { /* not rendered yet */ }
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// Full color-blend: map the host's standardized style variables (SEP-1865
|
|
86
|
+
// theming) onto relay's own custom properties so the board adopts the app's
|
|
87
|
+
// surfaces, text, borders, primary-action color and fonts — reading as part of
|
|
88
|
+
// Claude/Codex rather than a foreign page. Every mapping is conditional: a
|
|
89
|
+
// token the host omits keeps relay's own default, so it degrades gracefully on
|
|
90
|
+
// leaner hosts (where the warm terracotta identity simply stays).
|
|
91
|
+
const HOST_VAR_MAP = {
|
|
92
|
+
'--bg': '--color-background-primary',
|
|
93
|
+
'--card': '--color-background-secondary',
|
|
94
|
+
'--bg-sunken': '--color-background-tertiary',
|
|
95
|
+
'--fg': '--color-text-primary',
|
|
96
|
+
'--fg-2': '--color-text-secondary',
|
|
97
|
+
'--muted': '--color-text-tertiary',
|
|
98
|
+
'--border': '--color-border-primary',
|
|
99
|
+
'--border-strong': '--color-border-secondary',
|
|
100
|
+
'--accent': '--color-background-inverse',
|
|
101
|
+
'--accent-hover': '--color-background-inverse',
|
|
102
|
+
'--accent-fg': '--color-text-inverse',
|
|
103
|
+
'--accent-soft': '--color-background-tertiary',
|
|
104
|
+
'--danger': '--color-text-danger',
|
|
105
|
+
'--ok': '--color-text-success',
|
|
106
|
+
'--sans': '--font-sans',
|
|
107
|
+
'--mono': '--font-mono',
|
|
108
|
+
};
|
|
109
|
+
let hostFontStyleEl = null;
|
|
110
|
+
function adoptHostStyles() {
|
|
111
|
+
const styles = host && host.styles;
|
|
112
|
+
if (!styles || typeof styles !== 'object') return;
|
|
113
|
+
const v = styles.variables && typeof styles.variables === 'object' ? styles.variables : {};
|
|
114
|
+
const root = document.documentElement.style;
|
|
115
|
+
for (const [ours, theirs] of Object.entries(HOST_VAR_MAP)) {
|
|
116
|
+
if (typeof v[theirs] === 'string' && v[theirs]) root.setProperty(ours, v[theirs]);
|
|
117
|
+
}
|
|
118
|
+
const fonts = styles.css && typeof styles.css.fonts === 'string' ? styles.css.fonts : '';
|
|
119
|
+
if (fonts) {
|
|
120
|
+
if (!hostFontStyleEl) { hostFontStyleEl = document.createElement('style'); document.head.appendChild(hostFontStyleEl); }
|
|
121
|
+
hostFontStyleEl.textContent = fonts;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Platform awareness: reflect the host's surface so the board (and CSS) can
|
|
126
|
+
// adapt — a data-platform hint, a touch flag, and the host's safe-area insets
|
|
127
|
+
// applied as padding so content clears notches / home indicators on mobile.
|
|
128
|
+
function applyHostEnv() {
|
|
129
|
+
const root = document.documentElement;
|
|
130
|
+
if (host.platform === 'web' || host.platform === 'desktop' || host.platform === 'mobile') {
|
|
131
|
+
root.dataset.platform = host.platform;
|
|
132
|
+
}
|
|
133
|
+
root.classList.toggle('mcp-touch', Boolean(host.deviceCapabilities && host.deviceCapabilities.touch));
|
|
134
|
+
const sai = host.safeAreaInsets;
|
|
135
|
+
if (sai && typeof sai === 'object') {
|
|
136
|
+
const px = (n) => (Number.isFinite(n) ? n : 0) + 'px';
|
|
137
|
+
root.style.setProperty('--mcp-safe-top', px(sai.top));
|
|
138
|
+
root.style.setProperty('--mcp-safe-right', px(sai.right));
|
|
139
|
+
root.style.setProperty('--mcp-safe-bottom', px(sai.bottom));
|
|
140
|
+
root.style.setProperty('--mcp-safe-left', px(sai.left));
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
let sizeTimer = null;
|
|
145
|
+
function measureHeight() {
|
|
146
|
+
return Math.max(document.documentElement.scrollHeight, document.body ? document.body.scrollHeight : 0);
|
|
147
|
+
}
|
|
148
|
+
function sendSize() {
|
|
149
|
+
notify('ui/notifications/size-changed', { width: document.documentElement.scrollWidth, height: measureHeight() });
|
|
150
|
+
}
|
|
151
|
+
function reportSize() {
|
|
152
|
+
if (sizeTimer) return;
|
|
153
|
+
sizeTimer = setTimeout(() => { sizeTimer = null; sendSize(); }, 60);
|
|
154
|
+
}
|
|
155
|
+
// Immediate, un-debounced report — used when the board shrinks (e.g. after
|
|
156
|
+
// submit) so the host collapses the iframe right away instead of waiting.
|
|
157
|
+
function reportSizeNow() {
|
|
158
|
+
if (sizeTimer) { clearTimeout(sizeTimer); sizeTimer = null; }
|
|
159
|
+
sendSize();
|
|
160
|
+
}
|
|
161
|
+
window.addEventListener('resize', reportSize);
|
|
162
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
163
|
+
try { new ResizeObserver(reportSize).observe(document.documentElement); } catch { /* older host */ }
|
|
164
|
+
}
|
|
165
|
+
|