Puppet Stage: a browser puppet that lip-syncs to local text-to-speech and takes stage directions, live or from a screenplay.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-face.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-face.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-face.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

Dan Quellhorst Converse: characters know they have bodies 52ca998 4 months, 1 week ago History
shim.js · 23.0 KB · 562 lines
1// DBBASIC adapter shim for the Puppet Stage frontend.
2//
3// Loaded BEFORE the verbatim stage.js. Translates the stage's native
4// protocol (SSE + /api/* + /characters/*) onto DBBASIC objects:
5// puppet_cues — polled cue queue (the object server has no SSE yet)
6// puppet_speak — TTS + Rhubarb, returns audio as a data: URI
7// Characters are embedded in the page (window.PUPPET_CHARACTERS), and the
8// screenplay runner executes client-side, broadcasting cues through the
9// queue so every open stage plays the same show.
10'use strict';
11(() => {
12 const CUES_URL = '/objects/puppet_cues';
13 const SPEAK_URL = '/objects/puppet_speak';
14 const POLL_MS = 350;
15 const CHARACTERS = window.PUPPET_CHARACTERS || {};
16 const EXAMPLES = window.PUPPET_EXAMPLES || {};
17 const AUDIO = window.PUPPET_AUDIO || { music: {}, sfx: {} };
18 const realFetch = window.fetch.bind(window);
19
20 const jsonResponse = (obj, status = 200) =>
21 new Response(JSON.stringify(obj), { status, headers: { 'Content-Type': 'application/json' } });
22
23 async function postCue(cue) {
24 const r = await realFetch(CUES_URL, {
25 method: 'POST',
26 headers: { 'Content-Type': 'application/json' },
27 body: JSON.stringify(cue),
28 });
29 return r.json();
30 }
31
32 async function renderSpeech(body) {
33 const r = await realFetch(SPEAK_URL, {
34 method: 'POST',
35 headers: { 'Content-Type': 'application/json' },
36 body: JSON.stringify(body),
37 });
38 return r.json();
39 }
40
41 // ---------------------------------------------------------- EventSource
42
43 class PollingEventSource {
44 constructor() {
45 this.onmessage = null;
46 this.seq = null;
47 this.chain = Promise.resolve(); // deliver cues in order
48 this.poll();
49 }
50 async poll() {
51 try {
52 const url = this.seq === null ? `${CUES_URL}?tail=1` : `${CUES_URL}?since=${this.seq}`;
53 const d = await (await realFetch(url)).json();
54 if (d.status === 'ok') {
55 if (this.seq !== null) {
56 for (const cue of d.cues) this.chain = this.chain.then(() => this.deliver(cue));
57 }
58 this.seq = d.seq;
59 }
60 } catch { /* server briefly unreachable; keep polling */ }
61 setTimeout(() => this.poll(), POLL_MS);
62 }
63 async deliver(rawCue) {
64 let cue = rawCue;
65 if (rawCue.type === 'say-text') {
66 const d = await renderSpeech(rawCue);
67 cue = d.status === 'ok'
68 ? { type: 'speak', audio: d.audio, timeline: d.timeline, duration: d.duration, text: d.text }
69 : { type: 'error', message: d.error || 'speech failed' };
70 if (cue.type === 'speak' && rawCue.frame) cue.frame = rawCue.frame;
71 if (cue.type === 'speak' && rawCue.actor) cue.actor = rawCue.actor;
72 }
73 if (this.onmessage) this.onmessage({ data: JSON.stringify(cue) });
74 }
75 }
76 window.EventSource = function () { return new PollingEventSource(); };
77
78 // ------------------------------------------------- screenplay (client)
79 //
80 // Grammar mirrors server.js parseScript/directionCue exactly — keep the
81 // two in sync when either changes.
82
83 const LAYOUT_FRAMES = {
84 single: ['main'],
85 split: ['left', 'right'],
86 stack: ['top', 'bottom'],
87 thirds: ['third-l', 'third-c', 'third-r'],
88 'pip-tr': ['main', 'pip'], 'pip-tl': ['main', 'pip'],
89 'pip-br': ['main', 'pip'], 'pip-bl': ['main', 'pip'],
90 };
91
92 function parseKV(str) {
93 const out = {};
94 const re = /([a-z][\w-]*):("([^"]*)"|\S+)/gi;
95 let m;
96 while ((m = re.exec(str))) out[m[1].toLowerCase()] = m[3] !== undefined ? m[3] : m[2];
97 return out;
98 }
99
100 // left/center|middle/right resolve to stage-percent positions (25/50/75,
101 // case-insensitive); anything else parses as a literal percent number.
102 // Mirrors server.js exactly. Does NOT apply to [enter/exit] left|right,
103 // which mean off-stage sides, not on-stage positions.
104 function parsePosWord(v) {
105 const w = String(v == null ? '' : v).toLowerCase();
106 if (w === 'left') return 25;
107 if (w === 'center' || w === 'middle') return 50;
108 if (w === 'right') return 75;
109 return parseFloat(v);
110 }
111
112 // Resolves a bare id/name to the frame or actor it refers to: a declared
113 // frame id or actor id wins as-is; otherwise, if `id` names a character
114 // who's been framed/placed (tracked in charLocs), resolve through that
115 // mapping. Falls back to `id` itself (unresolved). Mirrors server.js.
116 function resolveTarget(id, frameIds, actorIds, charLocs) {
117 if (frameIds.has(id) || actorIds.has(id)) return id;
118 const loc = charLocs && charLocs.get(id);
119 return loc ? loc.id : id;
120 }
121
122 // Speaker-prefix resolution shared by plain "<id>: text" lines and the
123 // bracket-line tolerance in parseScript: frame id -> actor id -> character
124 // name (via charLocs). Mirrors server.js's matchSpeaker exactly.
125 function matchSpeaker(text, frameIds, actorIds, charLocs) {
126 const m = text.match(/^([A-Za-z][\w-]*):\s*(.*)$/);
127 if (!m) return null;
128 if (frameIds.has(m[1])) return { frame: m[1], text: m[2] };
129 if (actorIds.has(m[1])) return { actor: m[1], text: m[2] };
130 const loc = charLocs && charLocs.get(m[1]);
131 if (loc) return loc.kind === 'frame' ? { frame: loc.id, text: m[2] } : { actor: loc.id, text: m[2] };
132 return null;
133 }
134
135 function directionCue(body, frameIds, actorIds, charLocs) {
136 frameIds = frameIds || new Set(['main']);
137 actorIds = actorIds || new Set();
138 charLocs = charLocs || new Map();
139 const parts = body.trim().split(/\s+/);
140 const head = parts[0].toLowerCase();
141
142 if (head === 'layout') {
143 const preset = (parts[1] || 'single').toLowerCase();
144 const ids = LAYOUT_FRAMES[preset] || ['main'];
145 frameIds.clear();
146 for (const id of ids) frameIds.add(id);
147 return { type: 'layout', preset };
148 }
149
150 if (head === 'frame') {
151 const id = parts[1];
152 const restStr = parts.slice(2).join(' ');
153 if (/^clear\b/i.test(restStr)) {
154 frameIds.delete(id);
155 return { type: 'frame-clear', id };
156 }
157 const kv = parseKV(restStr);
158 const cue = { type: 'frame', id };
159 if (kv.slot) cue.slot = kv.slot;
160 if (kv.bg) cue.bg = kv.bg;
161 if (kv.character) cue.character = kv.character;
162 if (kv.view) cue.view = kv.view;
163 if (kv.facing !== undefined) cue.facing = parseInt(kv.facing, 10);
164 if (kv.rect) cue.rect = kv.rect.split(',').map(Number);
165 frameIds.add(id);
166 if (kv.character) charLocs.set(kv.character, { kind: 'frame', id });
167 return cue;
168 }
169
170 if (head === 'scene') return { type: 'scene', bg: parts.slice(1).join(' ') };
171
172 if (head === 'show') {
173 const kv = parseKV(parts.slice(1).join(' '));
174 let kind = 'text', value = '';
175 for (const k of ['text', 'image', 'video']) {
176 if (kv[k] !== undefined) { kind = k; value = kv[k]; break; }
177 }
178 const cue = { type: 'content', kind, value };
179 if (kv.fit) cue.fit = kv.fit;
180 return cue;
181 }
182
183 if (head === 'lower-third') {
184 if ((parts[1] || '').toLowerCase() === 'clear' && !body.includes('"')) {
185 return { type: 'overlay-clear', id: 'lower-third' };
186 }
187 const strs = [...body.matchAll(/"([^"]*)"/g)].map((m) => m[1]);
188 const cue = { type: 'overlay', template: 'lower-third', id: 'lower-third', slots: { title: strs[0] || '', subtitle: strs[1] || '' } };
189 // Optional trailing `hold:<ms>` key: default 6000 (auto-dismiss after
190 // 6s) when omitted; `hold:0` means persist (omit hold entirely); any
191 // other value is that hold in ms. Mirrors server.js exactly.
192 const holdMatch = body.match(/\bhold:(\d+)\b/i);
193 const holdMs = holdMatch ? parseInt(holdMatch[1], 10) : 6000;
194 if (holdMs > 0) cue.hold = holdMs;
195 return cue;
196 }
197
198 if (head === 'walk' || head === 'enter' || head === 'exit') {
199 // walk's left/right are on-stage position words (25/75); enter/exit's
200 // left/right stay off-stage sides — handled separately below.
201 const arg = parts[parts.length - 1].toLowerCase();
202 let x = head === 'walk' ? parsePosWord(arg) : parseFloat(arg);
203 if (head === 'enter') x = 50;
204 else if (head === 'exit' && isNaN(x)) x = arg === 'left' ? -15 : 115;
205 const from = parts.includes('from') ? parts[parts.indexOf('from') + 1] || '' : null;
206 return { type: 'walk', x, from, jump: head === 'enter' ? from : null };
207 }
208 if (head === 'wait') return { type: 'wait', seconds: parseFloat(parts[1]) || 1 };
209 if (head === 'view' || head === 'zoom') return { type: 'view', mode: (parts[1] || 'body').toLowerCase() };
210 if (head === 'look') return { type: 'look', dir: parts[1] || 'front' };
211 if (head === 'emote') return { type: 'action', name: parts[1] || 'neutral' };
212 if (head === 'engine' || head === 'voice' || head === 'rate') {
213 return { type: 'setting', key: head, value: parts.slice(1).join(' ') };
214 }
215 if (head === 'captions') {
216 const arg = (parts[1] || 'on').toLowerCase();
217 return { type: 'captions', on: arg !== 'off' };
218 }
219 if (head === 'iris' || head === 'fade') {
220 const ms = parseInt(parts[2], 10);
221 return { type: 'transition', name: head, dir: (parts[1] || 'out').toLowerCase(), ms: isNaN(ms) ? 700 : ms };
222 }
223
224 if (head === 'place') {
225 // [place <what> at <x> [id:<id>] [scale:<n>] [behind]] — <x> accepts a
226 // percent number or left|center|middle|right (25/50/75).
227 const rest = parts.slice(1);
228 const what = rest[0];
229 const atIdx = rest.indexOf('at');
230 const x = atIdx >= 0 ? parsePosWord(rest[atIdx + 1]) : NaN;
231 const restStr = parts.slice(1).join(' ');
232 const kv = parseKV(restStr);
233 const id = kv.id || what;
234 const cue = { type: 'place', id, what, x };
235 if (kv.scale !== undefined) cue.scale = parseFloat(kv.scale);
236 if (/\bbehind\b/.test(restStr)) cue.behind = true;
237 actorIds.add(id);
238 // `what` names a character iff it's an embedded PUPPET_CHARACTERS key
239 // — track where it now lives so later `[<what> ...]`/`what: text`
240 // addresses it (mirrors server.js's fs.existsSync check).
241 if (CHARACTERS[what]) charLocs.set(what, { kind: 'actor', id });
242 return cue;
243 }
244
245 if (head === 'remove') {
246 const id = parts[1];
247 actorIds.delete(id);
248 return { type: 'remove', id };
249 }
250
251 if (head === 'wear') {
252 // [wear <target> <prop> [scale:<n>] [anchor:<name>]] — target resolves
253 // frame id -> actor id -> character name (see resolveTarget).
254 const target = resolveTarget(parts[1], frameIds, actorIds, charLocs);
255 const prop = parts[2];
256 const kv = parseKV(parts.slice(3).join(' '));
257 const cue = { type: 'wear', target, prop };
258 if (kv.scale !== undefined) cue.scale = parseFloat(kv.scale);
259 if (kv.anchor !== undefined) cue.anchor = kv.anchor;
260 return cue;
261 }
262
263 if (head === 'unwear') {
264 const target = resolveTarget(parts[1], frameIds, actorIds, charLocs);
265 const cue = { type: 'unwear', target };
266 if (parts[2]) cue.anchor = parts[2];
267 return cue;
268 }
269
270 if (head === 'music') {
271 const arg = (parts[1] || '').toLowerCase();
272 if (arg === 'off') return { type: 'music', off: true };
273 return { type: 'music', id: parts[1] };
274 }
275
276 if (head === 'sfx') return { type: 'sfx', id: parts[1] };
277
278 // Prop-actor directions, reached via the actorIds fallback below (e.g.
279 // [hoop1 move 40] recurses into directionCue("move 40", ...)).
280 if (head === 'move') return { type: 'move', x: parsePosWord(parts[1]) };
281 if (head === 'scale') return { type: 'scale', value: parseFloat(parts[1]) };
282 if (head === 'spin') return { type: 'spin' };
283 if (head === 'bounce') return { type: 'bounce' };
284
285 if (frameIds.has(head) && parts.length > 1) {
286 const sub = directionCue(parts.slice(1).join(' '), frameIds, actorIds, charLocs);
287 sub.frame = head;
288 return sub;
289 }
290
291 // Same, for a placed actor id (checked after frame ids, per spec).
292 if (actorIds.has(head) && parts.length > 1) {
293 const sub = directionCue(parts.slice(1).join(' '), frameIds, actorIds, charLocs);
294 sub.actor = head;
295 return sub;
296 }
297
298 // Same, for a character NAME that's been framed/placed (checked after
299 // frame ids and actor ids, per spec) — e.g. [rex sit] once rex occupies
300 // a frame or actor slot; resolves to whichever one it currently occupies.
301 {
302 const loc = charLocs.get(head);
303 if (loc && parts.length > 1) {
304 const sub = directionCue(parts.slice(1).join(' '), frameIds, actorIds, charLocs);
305 if (loc.kind === 'frame') sub.frame = loc.id; else sub.actor = loc.id;
306 return sub;
307 }
308 }
309
310 // [clear] — wipe placed actors/worn props/content tiles/overlays across
311 // every frame, on demand mid-script (mirrors server.js exactly).
312 if (head === 'clear') return { type: 'clear' };
313
314 return { type: 'action', name: head };
315 }
316
317 function parseScript(src) {
318 const cues = [];
319 const frameIds = new Set(['main']);
320 const actorIds = new Set(); // tracks ids declared by [place ...] as we go
321 // character name -> {kind:'frame'|'actor', id} it currently occupies —
322 // mirrors server.js's charLocs so writers can address characters by
323 // name once framed/placed; later assignments override earlier ones.
324 const charLocs = new Map();
325 for (const rawLine of src.split('\n')) {
326 const line = rawLine.trim();
327 if (!line || line.startsWith('#')) continue;
328 if (/^(\[[^\]]+\]\s*)+$/.test(line)) {
329 for (const m of line.matchAll(/\[([^\]]+)\]/g)) {
330 const inner = m[1];
331 // Tolerance: `[left: some words]` / `[<name>: words]` is a spoken
332 // line wearing brackets by mistake — unwrap it instead of
333 // treating it as an unknown direction, but only when the label
334 // actually resolves. Mirrors server.js.
335 const sp = matchSpeaker(inner, frameIds, actorIds, charLocs);
336 if (sp) {
337 const cue = { type: 'speak-line', text: sp.text, concurrent: null, source: line };
338 if (sp.frame) cue.frame = sp.frame;
339 if (sp.actor) cue.actor = sp.actor;
340 cues.push(cue);
341 } else {
342 cues.push({ ...directionCue(inner, frameIds, actorIds, charLocs), source: line });
343 }
344 }
345 continue;
346 }
347
348 let text = line;
349 let frame = null;
350 let actor = null;
351 const sp = matchSpeaker(text, frameIds, actorIds, charLocs);
352 if (sp) { frame = sp.frame || null; actor = sp.actor || null; text = sp.text; }
353
354 let concurrent = null;
355 const inline = text.match(/^\(([^)]+)\)\s*(.*)$/);
356 if (inline) {
357 concurrent = { ...directionCue(inline[1], frameIds, actorIds, charLocs), source: line };
358 text = inline[2];
359 if (frame) concurrent.frame = frame;
360 if (actor) concurrent.actor = actor;
361 }
362 const cue = { type: 'speak-line', text, concurrent, source: line };
363 if (frame) cue.frame = frame;
364 if (actor) cue.actor = actor;
365 cues.push(cue);
366 }
367 return cues;
368 }
369
370 const ACTION_SECONDS = { wave: 1.6, jump: 0.9, nod: 1.2, shake: 1.2, bow: 1.8, dance: 2.4, shrug: 1.1 };
371 const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
372 let scriptToken = 0;
373
374 // Single ordered pass over a parsed cue list that replays the same
375 // settings/frameChar/override-flag state runScript used to thread through
376 // its loop, attaching the fully-resolved TTS params to every speak-line
377 // cue as `cue.resolved`. Mirrors server.js's resolveSpeech so voices stay
378 // identical; pulling it out lets runScript render speech ahead of when a
379 // line is posted instead of only right before.
380 function resolveSpeech(cues, mainCharacter) {
381 const settings = { engine: 'say', voice: '', rate: 0 };
382 let engineOverridden = false;
383 let voiceOverridden = false;
384 // frame id -> character name; mirrors server.js's resolution so a
385 // screenplay sounds the same whether it runs against the real server or
386 // this shim. Seeded with the client's notion of who's in the main frame.
387 const frameChar = {};
388 if (mainCharacter) frameChar.main = mainCharacter;
389 // actor id -> character name, for actor-targeted speak-line cues
390 // (mirrors frameChar but seeded from [place] cues).
391 const actorChar = {};
392
393 for (const cue of cues) {
394 if (cue.type === 'setting') {
395 settings[cue.key] = cue.value;
396 if (cue.key === 'engine') engineOverridden = true;
397 if (cue.key === 'voice') voiceOverridden = true;
398 continue;
399 }
400 if (cue.type === 'frame' && cue.character) frameChar[cue.id] = cue.character;
401 if (cue.type === 'character') frameChar[cue.frame || 'main'] = cue.name;
402 if (cue.type === 'place') {
403 // `what` names a character iff it's an embedded PUPPET_CHARACTERS
404 // key; a prop actor placed over a stale id clears any earlier
405 // character mapping.
406 if (CHARACTERS[cue.what]) actorChar[cue.id] = cue.what;
407 else delete actorChar[cue.id];
408 }
409 if (cue.type === 'remove') delete actorChar[cue.id];
410
411 if (cue.type === 'speak-line') {
412 // Resolution: an explicit [engine]/[voice] direction always wins;
413 // otherwise use the declared voice of the character in this line's
414 // actor (if actor-targeted) or frame; otherwise fall back to the
415 // (default) settings.
416 const charName = cue.actor ? actorChar[cue.actor] : frameChar[cue.frame || 'main'];
417 const ch = CHARACTERS[charName];
418 const cv = ch && ch.manifest && ch.manifest.voice;
419 const engine = engineOverridden ? settings.engine : (cv && cv.engine) || settings.engine;
420 const voice = voiceOverridden ? settings.voice : (cv && cv.voice !== undefined ? cv.voice : settings.voice);
421 cue.resolved = { engine, voice, rate: settings.rate };
422 }
423 }
424 }
425
426 const RENDER_WINDOW = 4;
427
428 async function runScript(src, mainCharacter) {
429 const token = ++scriptToken;
430 const cues = parseScript(src);
431 resolveSpeech(cues, mainCharacter);
432 const speakCues = cues.filter((c) => c.type === 'speak-line');
433
434 // Bounded render-ahead: kick off renderSpeech for the k-th speak-line
435 // cue at most once, stashing the promise on the cue itself (learns the
436 // real duration; each stage re-renders the deterministic audio on
437 // receipt of the say-text cue). Safe to call repeatedly.
438 function ensureRender(k) {
439 if (k < 0 || k >= speakCues.length) return;
440 const c = speakCues[k];
441 if (!c._renderP) {
442 c._renderP = renderSpeech({ text: c.text, ...c.resolved });
443 c._renderP.catch(() => {}); // avoid unhandled-rejection noise; real errors surface at the await below
444 }
445 }
446
447 for (let k = 0; k < RENDER_WINDOW; k++) ensureRender(k);
448 let speakIndex = 0;
449
450 if (speakCues.length > 0) {
451 try {
452 await speakCues[0]._renderP;
453 } catch (e) {
454 // ignore here; the playback loop's per-line error handling will surface
455 // this once it reaches line 1
456 }
457 if (token !== scriptToken) return; // replaced while pre-rendering; don't start
458 }
459
460 await postCue({ type: 'script-start', lines: cues.map((c) => c.source) });
461
462 for (let i = 0; i < cues.length; i++) {
463 if (token !== scriptToken) break;
464 const cue = cues[i];
465 await postCue({ type: 'script-line', index: i });
466
467 if (cue.type === 'setting' || cue.type === 'wait') {
468 if (cue.type === 'wait') await sleep(cue.seconds * 1000);
469 continue;
470 }
471
472 if (cue.type === 'speak-line') {
473 const k = speakIndex++;
474 ensureRender(k); // in case it wasn't primed (e.g. window > remaining lines)
475 const d = await cue._renderP;
476 if (token !== scriptToken) break;
477 if (cue.concurrent) await postCue(cue.concurrent);
478 const sayCue = { type: 'say-text', text: cue.text, ...cue.resolved };
479 if (cue.frame) sayCue.frame = cue.frame;
480 if (cue.actor) sayCue.actor = cue.actor;
481 await postCue(sayCue);
482 await sleep(((d.duration || cue.text.split(/\s+/).length * 0.35) * 1000) + 400);
483 ensureRender(k + RENDER_WINDOW);
484 continue;
485 }
486
487 await postCue(cue);
488 if (cue.type === 'action') await sleep((ACTION_SECONDS[cue.name] || 1) * 1000);
489 else if (cue.type === 'walk') await sleep(1200);
490 else if (cue.type === 'transition') await sleep(cue.ms + 100);
491 else await sleep(400);
492 }
493 if (token === scriptToken) await postCue({ type: 'script-end' });
494 }
495
496 // -------------------------------------------------------- fetch bridge
497
498 window.fetch = async (url, opts = {}) => {
499 const u = String(url);
500
501 if (u === '/api/characters') {
502 return jsonResponse({ characters: Object.keys(CHARACTERS) });
503 }
504 if (u.startsWith('/characters/')) {
505 const [, , name, file] = u.split('/');
506 const ch = CHARACTERS[name];
507 if (!ch) return jsonResponse({ error: 'unknown character' }, 404);
508 if (file === 'manifest.json') return jsonResponse(ch.manifest);
509 return new Response(ch.svg, { status: 200, headers: { 'Content-Type': 'image/svg+xml' } });
510 }
511 if (u.startsWith('/api/voices')) {
512 const engine = new URLSearchParams(u.split('?')[1] || '').get('engine') || 'say';
513 const d = await (await realFetch(`${SPEAK_URL}?voices=1&engine=${engine}`)).json();
514 return jsonResponse({ voices: d.voices || [] });
515 }
516 if (u === '/api/say') {
517 const body = JSON.parse(opts.body || '{}');
518 const d = await postCue({ type: 'say-text', ...body });
519 return jsonResponse(d.status === 'ok' ? { ok: true } : { error: d.error }, d.status === 'ok' ? 200 : 500);
520 }
521 if (u === '/api/cue') {
522 const d = await postCue(JSON.parse(opts.body || '{}'));
523 return jsonResponse(d.status === 'ok' ? { ok: true } : { error: d.error }, d.status === 'ok' ? 200 : 500);
524 }
525 if (u === '/api/script') {
526 const body = JSON.parse(opts.body || '{}');
527 runScript(body.script || '', body.mainCharacter); // fire and forget
528 return jsonResponse({ ok: true });
529 }
530 if (u === '/api/script/stop') {
531 scriptToken++;
532 postCue({ type: 'script-end' });
533 return jsonResponse({ ok: true });
534 }
535 if (u === '/api/examples') {
536 return jsonResponse({ examples: Object.keys(EXAMPLES).sort() });
537 }
538 if (u.startsWith('/api/examples/')) {
539 const name = decodeURIComponent(u.slice('/api/examples/'.length));
540 const script = EXAMPLES[name];
541 if (script === undefined) return jsonResponse({ error: 'example not found' }, 404);
542 return jsonResponse({ name, script });
543 }
544 if (u === '/api/assets') {
545 // the package page has no image asset serving; the UI hides pickers
546 // that get empty lists back. Music/sfx JSON *is* embedded (small), so
547 // those two lists are real.
548 return jsonResponse({
549 backgrounds: [], props: [], overlays: [],
550 music: Object.keys(AUDIO.music || {}).sort(),
551 sfx: Object.keys(AUDIO.sfx || {}).sort(),
552 });
553 }
554 if (u.startsWith('/api/asset/music/') || u.startsWith('/api/asset/sfx/')) {
555 const [, , , kind, id] = u.split('/');
556 const data = AUDIO[kind] && AUDIO[kind][id];
557 if (!data) return jsonResponse({ error: 'asset not found' }, 404);
558 return jsonResponse(data);
559 }
560 return realFetch(url, opts);
561 };
562})();