-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathrecord-demo.mjs
More file actions
262 lines (244 loc) · 7.57 KB
/
Copy pathrecord-demo.mjs
File metadata and controls
262 lines (244 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/env node
// Record a live demo of the CAO MCP Apps fleet UI.
//
// Boots the e2e harness server (e2e/server.mjs) — which serves the *built*
// single-file view bundles inside a real MCP-host iframe with canned fleet state
// and a live SSE feed — then drives Chromium through an operator flow
// (dashboard -> agent detail -> live event stream) while recording video and
// capturing screenshots, and (where gif-capable ffmpeg/gifski exists) exports an
// optimized GIF for the README/docs.
//
// Honest constraint: a true in-host render (Claude Desktop / Cursor / Goose)
// can't be captured headlessly, so this drives the *real* built bundles with
// live host data over the same postMessage/SSE contract the host uses — a
// faithful stand-in for the in-host experience.
//
// Artifacts (docs/media/): mcp-apps-demo.webm, mcp-apps-{dashboard,agent,
// event-stream}.png, and mcp-apps-demo.gif when a gif-capable encoder is found.
//
// Usage: npm run build:all && node scripts/record-demo.mjs
// Env overrides: CHROMIUM_BIN, FFMPEG_BIN, DEMO_PORT.
import { spawn, spawnSync } from "node:child_process";
import {
existsSync,
mkdirSync,
readdirSync,
renameSync,
rmSync,
} from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { chromium } from "@playwright/test";
const __dirname = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(__dirname, "..");
const REPO = resolve(ROOT, "..");
const PORT = Number(process.env.DEMO_PORT ?? 9889);
const BASE = `http://127.0.0.1:${PORT}`;
const OUT_DIR = resolve(REPO, "docs/media");
const TMP_DIR = resolve(ROOT, ".demo-tmp");
const CHROMIUM_BIN = process.env.CHROMIUM_BIN || "/opt/pw-browsers/chromium";
const FFMPEG_BIN =
process.env.FFMPEG_BIN || "/opt/pw-browsers/ffmpeg-1011/ffmpeg-linux";
const VIEWPORT = { width: 1320, height: 820 };
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function waitForServer(timeoutMs = 15000) {
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
try {
if ((await fetch(`${BASE}/host.html`)).ok) return;
} catch {
/* not up yet */
}
await sleep(200);
}
throw new Error("e2e harness server did not start");
}
async function emit(event) {
await fetch(`${BASE}/emit`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ event }),
}).catch(() => {});
}
function ffmpegHasGif(bin) {
try {
const r = spawnSync(bin, ["-hide_banner", "-encoders"], {
encoding: "utf8",
});
return r.status === 0 && /\bgif\b/.test(r.stdout || "");
} catch {
return false;
}
}
function run(bin, args) {
return new Promise((res, rej) => {
const p = spawn(bin, args, { stdio: "inherit" });
p.on("exit", (code) =>
code === 0 ? res() : rej(new Error(`${bin} exited ${code}`)),
);
p.on("error", rej);
});
}
async function main() {
if (!existsSync(CHROMIUM_BIN))
throw new Error(`chromium not found at ${CHROMIUM_BIN}`);
rmSync(TMP_DIR, { recursive: true, force: true });
mkdirSync(TMP_DIR, { recursive: true });
mkdirSync(OUT_DIR, { recursive: true });
const server = spawn("node", [resolve(ROOT, "e2e/server.mjs")], {
env: { ...process.env, E2E_PORT: String(PORT) },
stdio: "inherit",
});
try {
await waitForServer();
const browser = await chromium.launch({
executablePath: CHROMIUM_BIN,
headless: true,
args: [
"--no-sandbox",
"--disable-dev-shm-usage",
"--force-color-profile=srgb",
],
});
const context = await browser.newContext({
viewport: VIEWPORT,
recordVideo: { dir: TMP_DIR, size: VIEWPORT },
deviceScaleFactor: 1,
});
const page = await context.newPage();
const shot = (name) =>
page.screenshot({ path: resolve(OUT_DIR, `mcp-apps-${name}.png`) });
// 1) Operator dashboard — supervisor + workers, providers, live statuses.
await page.goto(`${BASE}/host.html?view=dashboard&fleet=rich`, {
waitUntil: "domcontentloaded",
});
await sleep(4000);
await shot("dashboard");
// 2) Drill into the supervisor (agent detail shows its delegation log).
await page.goto(`${BASE}/host.html?view=agent&fleet=rich`, {
waitUntil: "domcontentloaded",
});
await sleep(3500);
await shot("agent");
// 3) Live event stream — push governance events and watch it update.
await page.goto(`${BASE}/host.html?view=event-stream&fleet=rich`, {
waitUntil: "domcontentloaded",
});
await sleep(900);
const now = 1750000000000;
const events = [
{
id: "L1",
kind: "launch",
terminal_id: "sup-1",
session: "cao-feature-build",
ts: now,
},
{
id: "L2",
kind: "a2a_delegation",
terminal_id: "sup-1",
session: "cao-feature-build",
ts: now + 1,
},
{
id: "L3",
kind: "launch",
terminal_id: "dev-1",
session: "cao-feature-build",
ts: now + 2,
},
{
id: "L4",
kind: "file_mod",
terminal_id: "dev-1",
session: "cao-feature-build",
ts: now + 3,
},
{
id: "L5",
kind: "handoff",
terminal_id: "sup-1",
session: "cao-review",
ts: now + 4,
},
{
id: "L6",
kind: "a2a_delegation",
terminal_id: "sup-1",
session: "cao-review",
ts: now + 5,
},
{
id: "L7",
kind: "completion",
terminal_id: "test-1",
session: "cao-feature-build",
ts: now + 6,
},
{
id: "L8",
kind: "error",
terminal_id: "doc-1",
session: "cao-review",
ts: now + 7,
},
];
for (const ev of events) {
await emit(ev);
await sleep(900);
}
await sleep(1200);
await shot("event-stream");
await context.close(); // finalizes the .webm
await browser.close();
// Persist the webm first so a GIF-conversion failure never loses the capture.
const webm = readdirSync(TMP_DIR).find((f) => f.endsWith(".webm"));
if (!webm) throw new Error("no video captured");
const outWebm = resolve(OUT_DIR, "mcp-apps-demo.webm");
renameSync(resolve(TMP_DIR, webm), outWebm);
console.log(`[demo] wrote ${outWebm}`);
console.log(
`[demo] wrote screenshots: mcp-apps-{dashboard,agent,event-stream}.png`,
);
// Optional GIF — only when a gif-capable encoder is available. Playwright's
// bundled ffmpeg is a webm-only build (no gif encoder), so this is skipped
// here and runs on a dev/CI machine with full ffmpeg.
const outGif = resolve(OUT_DIR, "mcp-apps-demo.gif");
if (ffmpegHasGif(FFMPEG_BIN)) {
const palette = resolve(TMP_DIR, "palette.png");
const vf = "fps=10,scale=900:-1:flags=lanczos";
await run(FFMPEG_BIN, [
"-y",
"-i",
outWebm,
"-vf",
`${vf},palettegen`,
palette,
]);
await run(FFMPEG_BIN, [
"-y",
"-i",
outWebm,
"-i",
palette,
"-filter_complex",
`${vf}[x];[x][1:v]paletteuse`,
outGif,
]);
console.log(`[demo] wrote ${outGif}`);
} else {
console.warn(
`[demo] ${FFMPEG_BIN} has no gif encoder; skipped GIF. ` +
`Run on a machine with full ffmpeg (or gifski) to produce mcp-apps-demo.gif from the webm.`,
);
}
} finally {
server.kill("SIGTERM");
rmSync(TMP_DIR, { recursive: true, force: true });
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});