castle-web-cli 0.4.208 → 0.4.209

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 (53) hide show
  1. package/dist/api.d.ts +5 -0
  2. package/dist/castle-host/host.js +151 -0
  3. package/dist/castleJson.d.ts +4 -0
  4. package/dist/castleJson.js +3 -1
  5. package/dist/devSessionServer.js +1 -0
  6. package/dist/editorConfig.d.ts +12 -0
  7. package/dist/editorConfig.js +72 -6
  8. package/dist/init.js +1 -1
  9. package/dist/native/loop.js +1 -1
  10. package/dist/native/playtest-executor.js +36 -4
  11. package/dist/native/playtest-remote.d.ts +1 -0
  12. package/dist/native/playtest-remote.js +61 -5
  13. package/dist/native/playtest.d.ts +35 -2
  14. package/dist/native/playtest.js +162 -18
  15. package/dist/platformDoc.d.ts +1 -0
  16. package/dist/platformDoc.js +24 -3
  17. package/dist/playUrl.d.ts +3 -0
  18. package/dist/playUrl.js +12 -0
  19. package/dist/save-deck.js +1 -0
  20. package/dist/shell/assets/{index-b_bebkRb.css → index-BCrgYh76.css} +1 -1
  21. package/dist/shell/assets/{index-C6SXXPwm.js → index-DzLbNBc7.js} +68 -68
  22. package/dist/shell/index.html +2 -2
  23. package/kits/base/CLAUDE.md +1 -0
  24. package/kits/base/castle.json +1 -1
  25. package/kits/base/sdk/README.md +70 -7
  26. package/kits/base/sdk/castle.d.ts +1 -0
  27. package/kits/base/sdk/commands.d.ts +48 -0
  28. package/kits/base/sdk/commands.js +25 -0
  29. package/kits/base/sdk/multiplayer.d.ts +3 -0
  30. package/kits/base/sdk/multiplayer.js +13 -0
  31. package/kits/base/sdk/multiplayerVoice.d.ts +67 -0
  32. package/kits/base/sdk/multiplayerVoice.js +309 -0
  33. package/kits/base/sdk/transport.d.ts +2 -1
  34. package/kits/base/sdk/transport.js +15 -1
  35. package/kits/multiplayer-2d/castle.json +2 -2
  36. package/kits/multiplayer-2d/code/systems/multiplayer.js +7 -1
  37. package/kits/multiplayer-3d/castle.json +2 -2
  38. package/kits/multiplayer-3d/code/systems/multiplayer.js +8 -4
  39. package/kits/physics-2d/castle.json +1 -1
  40. package/kits/physics-2d/editors/SceneEditor.jsx +18 -4
  41. package/kits/physics-2d/systems/media.js +3 -5
  42. package/kits/physics-3d/castle.json +2 -2
  43. package/kits/physics-3d/engine3d/PxModelEditor.jsx +2 -2
  44. package/kits/physics-3d/engine3d/pxmodel.js +7 -0
  45. package/kits/real-time/CLAUDE.md +3 -0
  46. package/kits/real-time/castle.json +1 -1
  47. package/kits/real-time/code/client/connection.js +17 -2
  48. package/kits/real-time/code/client/gameHooks.js +5 -0
  49. package/kits/real-time/code/client/joinOverlay.js +23 -5
  50. package/kits/real-time/code/client/messages.js +4 -0
  51. package/kits/real-time/code/server/persist.js +19 -10
  52. package/kits/real-time/code/server/session.js +19 -0
  53. package/package.json +2 -2
package/dist/api.d.ts CHANGED
@@ -21,6 +21,8 @@ export type BrowserOperation = {
21
21
  durationMs?: number;
22
22
  }>;
23
23
  screenshots: number[];
24
+ input?: 'touch';
25
+ playFile?: string;
24
26
  } | {
25
27
  kind: 'cover';
26
28
  };
@@ -142,6 +144,9 @@ export declare function createCauldronServerBundleUploadConfig(deckId: string):
142
144
  export interface CauldronServerConfig {
143
145
  tickRate: number;
144
146
  maxPlayers: number;
147
+ voice: {
148
+ enabled: boolean;
149
+ };
145
150
  }
146
151
  export interface CauldronServerBundleResult {
147
152
  deckId: string;
@@ -40,6 +40,10 @@ const COMMAND_NAMES = [
40
40
  "portal.info",
41
41
  "haptics.play",
42
42
  "multiplayer.getSession",
43
+ "voice.getState",
44
+ "voice.requestJoin",
45
+ "voice.leave",
46
+ "voice.setMix",
43
47
  "characters.open",
44
48
  "characters.send",
45
49
  "characters.status",
@@ -79,6 +83,29 @@ function isCommandName(value) {
79
83
  function isPlatformCommand(command) {
80
84
  return PLATFORM_COMMAND_NAMES.includes(command);
81
85
  }
86
+ // What a host posts to the deck (web: postMessage to the iframe; mobile:
87
+ // window.__castleSdkHost.receive) for a voice state change or a new set of speakers.
88
+ export function voiceEventEnvelope(event) {
89
+ return { castleSdk: 1, voice: event };
90
+ }
91
+ const CAULDRON_VOICE_TOKEN_QUERY = `query CastleCauldronVoiceToken($sessionId: ID!, $deckId: ID!, $env: String) {
92
+ getCauldronVoiceToken(sessionId: $sessionId, deckId: $deckId, env: $env) {
93
+ available
94
+ reason
95
+ url
96
+ token
97
+ roomName
98
+ blockedUserIds
99
+ }
100
+ }`;
101
+ // For a host's voice implementation: a LiveKit token for the session's room, issued
102
+ // only if the signed-in player may be in it. Tokens last about a minute and only gate
103
+ // the join, so fetch one right before connecting.
104
+ export async function fetchCauldronVoiceToken(gql, ctx, sessionId, env) {
105
+ const deckId = requireDeckId(ctx, "voice.requestJoin");
106
+ const data = await graphql(gql, CAULDRON_VOICE_TOKEN_QUERY, { sessionId, deckId, env: env ?? null }, "voice.requestJoin");
107
+ return data.getCauldronVoiceToken;
108
+ }
82
109
  class HostCommandError extends Error {
83
110
  code;
84
111
  command;
@@ -151,6 +178,14 @@ function runCommand(ctx, command, params, caps, options) {
151
178
  return passHas(ctx, params, caps);
152
179
  case "multiplayer.getSession":
153
180
  return multiplayerGetSession(ctx, params, caps);
181
+ case "voice.getState":
182
+ return voiceGetState(ctx, params, caps);
183
+ case "voice.requestJoin":
184
+ return voiceRequestJoin(ctx, params, caps, options.signal);
185
+ case "voice.leave":
186
+ return voiceLeave(ctx, params, caps);
187
+ case "voice.setMix":
188
+ return Promise.resolve(voiceSetMix(ctx, params, caps));
154
189
  case "characters.open":
155
190
  return charactersOpen(ctx, params, gql);
156
191
  case "characters.send":
@@ -265,6 +300,122 @@ async function passHas(ctx, params, caps) {
265
300
  const match = (data.passesForDeck ?? []).find((p) => p?.passId === passId);
266
301
  return { hasPass: match?.isActive === true };
267
302
  }
303
+ // A session id is ghost's: a deck id plus a short suffix, or a nanoid.
304
+ const VOICE_SESSION_ID_MAX_LENGTH = 256;
305
+ // A room holds at most 24 players; anything past this is a confused deck.
306
+ const VOICE_MIX_MAX_ENTRIES = 64;
307
+ const VOICE_JOIN_STATUSES = ["joined", "declined", "unavailable"];
308
+ // After the player declines, a deck asking again within this long gets "declined"
309
+ // without another prompt, so a deck can't nag.
310
+ export const VOICE_DECLINE_COOLDOWN_MS = 10_000;
311
+ // Keys a deck could use to reach Object.prototype through a plain object.
312
+ const RESERVED_MIX_KEYS = new Set(["__proto__", "constructor", "prototype"]);
313
+ const voiceJoinsInFlight = new Map();
314
+ const voiceDeclinedAt = new Map();
315
+ const HOST_VOICE_STATES = ["unavailable", "off", "joining", "live"];
316
+ function voiceSessionId(params, command) {
317
+ const sessionId = asString(params.sessionId, "sessionId", command);
318
+ if (sessionId.length > VOICE_SESSION_ID_MAX_LENGTH) {
319
+ throw new HostCommandError("INVALID_ARGUMENT", `Castle command ${command} requires a valid sessionId.`, command);
320
+ }
321
+ return sessionId;
322
+ }
323
+ async function voiceGetState(ctx, params, caps) {
324
+ const sessionId = voiceSessionId(params, "voice.getState");
325
+ if (!caps.voice)
326
+ return { state: "unavailable" };
327
+ const state = await caps.voice.getState(sessionId, ctx);
328
+ return { state: HOST_VOICE_STATES.includes(state) ? state : "unavailable" };
329
+ }
330
+ async function voiceRequestJoin(ctx, params, caps, signal) {
331
+ const sessionId = voiceSessionId(params, "voice.requestJoin");
332
+ const voice = caps.voice;
333
+ if (!voice)
334
+ return { status: "unavailable" };
335
+ const key = `${ctx.deckId ?? ""}\u0000${sessionId}`;
336
+ const declinedAt = voiceDeclinedAt.get(key);
337
+ if (declinedAt !== undefined) {
338
+ if (Date.now() - declinedAt < VOICE_DECLINE_COOLDOWN_MS)
339
+ return { status: "declined" };
340
+ voiceDeclinedAt.delete(key);
341
+ }
342
+ let inFlight = voiceJoinsInFlight.get(key);
343
+ while (inFlight?.signal?.aborted) {
344
+ // A withdrawn prompt the host hasn't closed yet: this is a new ask, so let the
345
+ // old one finish and prompt afresh rather than hand back its "unavailable".
346
+ await inFlight.request.catch(() => undefined);
347
+ if (signal?.aborted)
348
+ return { status: "unavailable" };
349
+ inFlight = voiceJoinsInFlight.get(key);
350
+ }
351
+ if (inFlight)
352
+ return inFlight.request;
353
+ const request = (async () => {
354
+ const outcome = await voice.requestJoin(sessionId, ctx, signal);
355
+ if (signal?.aborted) {
356
+ // The deck withdrew the request while the player was choosing.
357
+ if (outcome === "joined")
358
+ await voice.leave(sessionId, ctx);
359
+ return { status: "unavailable" };
360
+ }
361
+ const status = VOICE_JOIN_STATUSES.includes(outcome)
362
+ ? outcome
363
+ : "declined";
364
+ if (status === "declined") {
365
+ pruneVoiceDeclines();
366
+ voiceDeclinedAt.set(key, Date.now());
367
+ }
368
+ else {
369
+ voiceDeclinedAt.delete(key);
370
+ }
371
+ return { status };
372
+ })().finally(() => {
373
+ if (voiceJoinsInFlight.get(key)?.request === request)
374
+ voiceJoinsInFlight.delete(key);
375
+ });
376
+ voiceJoinsInFlight.set(key, { request, signal });
377
+ return request;
378
+ }
379
+ function pruneVoiceDeclines() {
380
+ const now = Date.now();
381
+ for (const [key, declinedAt] of voiceDeclinedAt) {
382
+ if (now - declinedAt >= VOICE_DECLINE_COOLDOWN_MS)
383
+ voiceDeclinedAt.delete(key);
384
+ }
385
+ }
386
+ async function voiceLeave(ctx, params, caps) {
387
+ const sessionId = voiceSessionId(params, "voice.leave");
388
+ await caps.voice?.leave(sessionId, ctx);
389
+ return { ok: true };
390
+ }
391
+ function voiceSetMix(ctx, params, caps) {
392
+ const sessionId = voiceSessionId(params, "voice.setMix");
393
+ const raw = params.mix;
394
+ if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
395
+ throw new HostCommandError("INVALID_ARGUMENT", "Castle command voice.setMix requires mix.", "voice.setMix");
396
+ }
397
+ const entries = Object.entries(raw).slice(0, VOICE_MIX_MAX_ENTRIES);
398
+ const mix = Object.create(null);
399
+ for (const [userId, entry] of entries) {
400
+ if (!userId || userId.length > 64 || RESERVED_MIX_KEYS.has(userId))
401
+ continue;
402
+ if (typeof entry !== "object" || entry === null)
403
+ continue;
404
+ const record = entry;
405
+ mix[userId] = {
406
+ gain: finiteOr(record.gain, 1, 0, 1),
407
+ pan: finiteOr(record.pan, 0, -1, 1),
408
+ };
409
+ }
410
+ if (caps.voice && Object.keys(mix).length > 0)
411
+ caps.voice.setMix(sessionId, mix, ctx);
412
+ return { ok: true };
413
+ }
414
+ function finiteOr(value, fallback, min, max) {
415
+ return typeof value === "number" && Number.isFinite(value)
416
+ ? Math.min(max, Math.max(min, value))
417
+ : fallback;
418
+ }
268
419
  async function passesOffer(ctx, params, caps) {
269
420
  const passId = asString(params.passId, "passId", "pass.offer");
270
421
  // Hosts without bricks support (dev CLI — no handler at all) get a normalized
@@ -27,6 +27,9 @@ export interface CastleJson {
27
27
  tickRate?: number;
28
28
  maxPlayers?: number;
29
29
  };
30
+ voice?: {
31
+ enabled?: boolean;
32
+ };
30
33
  importWhenRemixed?: boolean;
31
34
  starterScene?: string;
32
35
  [key: string]: unknown;
@@ -40,6 +43,7 @@ export interface ResolvedServerConfig {
40
43
  entry: string;
41
44
  tickRate: number;
42
45
  maxPlayers: number;
46
+ voiceEnabled: boolean;
43
47
  }
44
48
  export declare function resolveServerConfig(dir: string): ResolvedServerConfig | null;
45
49
  export declare function deckStarterScene(dir: string): string;
@@ -40,7 +40,8 @@ const DEFAULT_SERVER_ENTRIES = [
40
40
  'code/server/index.mjs',
41
41
  ];
42
42
  export function resolveServerConfig(dir) {
43
- const server = readCastleJson(dir)?.server;
43
+ const castleJson = readCastleJson(dir);
44
+ const server = castleJson?.server;
44
45
  const declared = typeof server?.main === 'string' ? server.main.trim() : '';
45
46
  const entry = declared ||
46
47
  DEFAULT_SERVER_ENTRIES.find((candidate) => fs.existsSync(path.join(dir, candidate)));
@@ -50,6 +51,7 @@ export function resolveServerConfig(dir) {
50
51
  entry,
51
52
  tickRate: clampNumber(server?.tickRate, DEFAULT_SERVER_TICK_RATE, 0, SERVER_TICK_RATE_CAP),
52
53
  maxPlayers: Math.floor(clampNumber(server?.maxPlayers, DEFAULT_SERVER_MAX_PLAYERS, 1, SERVER_MAX_PLAYERS_CAP)),
54
+ voiceEnabled: castleJson?.voice?.enabled !== false,
53
55
  };
54
56
  }
55
57
  function isSafeServerEntry(dir, entry) {
@@ -543,6 +543,7 @@ class DevSessionServerImpl {
543
543
  entry: 'server/index.js',
544
544
  tickRate: 20,
545
545
  maxPlayers: 8,
546
+ voiceEnabled: true,
546
547
  });
547
548
  }
548
549
  sendPlatform(frame) {
@@ -33,7 +33,19 @@ export interface EditorConfig {
33
33
  fileTypes?: FileTypeConfig[];
34
34
  extensions?: string[];
35
35
  defaultPlayFile?: string;
36
+ defaultPlayFileRefused?: {
37
+ value: string;
38
+ reason: string;
39
+ };
36
40
  }
41
+ export declare const MAX_PLAY_FILE_LENGTH = 512;
42
+ export declare function normalizePlayFile(deckDir: string, raw: string): {
43
+ ok: true;
44
+ path: string;
45
+ } | {
46
+ ok: false;
47
+ reason: string;
48
+ };
37
49
  export declare function readEditorConfig(deckDir: string): EditorConfig;
38
50
  export declare function deckHidesFiles(deckDir: string): boolean;
39
51
  export declare function resolveFileTypes(deckDir: string): ResolvedFileType[] | null;
@@ -14,6 +14,7 @@
14
14
  // only. `defaultPlayFile` falls back to an import's when the deck names none --
15
15
  // see `importedDefaultPlayFile`.
16
16
  import * as fs from 'fs';
17
+ import * as os from 'os';
17
18
  import * as path from 'path';
18
19
  import { readCastleJson } from './castleJson.js';
19
20
  import { IMPORTS_DIR } from './imports.js';
@@ -24,6 +25,61 @@ export function dataModeOf(type) {
24
25
  return type.data;
25
26
  return null;
26
27
  }
28
+ // A play file as the Play panel, the playtest and the cloud browser worker all
29
+ // take it: a normalized deck-relative POSIX path. The worker and Ghost refuse
30
+ // anything else outright (castle-sandboxes/ghost-server validPlayFile), and a
31
+ // kit looks the file up by exact key, so a loose spelling is put in this form
32
+ // once, here, or refused with the reason.
33
+ export const MAX_PLAY_FILE_LENGTH = 512;
34
+ const LONE_SURROGATE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;
35
+ const within = (child, parent) => {
36
+ const rel = path.relative(parent, child);
37
+ return rel === '' || (!rel.startsWith(`..${path.sep}`) && rel !== '..' && !path.isAbsolute(rel));
38
+ };
39
+ // Directories an absolute declaration must not point into: the deck and the
40
+ // home dir, each as given and as its real path (/tmp vs /private/tmp). A home
41
+ // of `/` would claim every leading-slash path, so it is left out.
42
+ function filesystemRoots(deckRoot) {
43
+ const real = (dir) => {
44
+ try {
45
+ return fs.realpathSync(dir);
46
+ }
47
+ catch {
48
+ return dir;
49
+ }
50
+ };
51
+ const home = path.resolve(os.homedir());
52
+ const dirs = [deckRoot, real(deckRoot)];
53
+ if (home !== path.parse(home).root)
54
+ dirs.push(home, real(home));
55
+ return dirs.filter((dir) => dir !== path.parse(dir).root);
56
+ }
57
+ export function normalizePlayFile(deckDir, raw) {
58
+ const value = raw.trim();
59
+ if (!value)
60
+ return { ok: false, reason: 'it is empty' };
61
+ if ([...value].some((c) => c.charCodeAt(0) < 0x20 || c === '\u007f')) {
62
+ return { ok: false, reason: 'it contains a control character' };
63
+ }
64
+ if (value.includes('\\'))
65
+ return { ok: false, reason: 'it contains a backslash' };
66
+ if (LONE_SURROGATE.test(value))
67
+ return { ok: false, reason: 'it contains an unpaired surrogate' };
68
+ const root = path.resolve(deckDir);
69
+ if (path.isAbsolute(value) && filesystemRoots(root).some((dir) => within(value, dir))) {
70
+ return { ok: false, reason: 'it is an absolute filesystem path, not a path inside the deck' };
71
+ }
72
+ const inside = path.relative(root, path.resolve(root, value.replace(/^\/+/, '')));
73
+ if (!inside)
74
+ return { ok: false, reason: 'it names the deck folder, not a file' };
75
+ if (!within(path.join(root, inside), root))
76
+ return { ok: false, reason: 'it points outside the deck' };
77
+ const posix = inside.split(path.sep).join('/');
78
+ if (posix.length > MAX_PLAY_FILE_LENGTH) {
79
+ return { ok: false, reason: `it is longer than ${MAX_PLAY_FILE_LENGTH} characters` };
80
+ }
81
+ return { ok: true, path: posix };
82
+ }
27
83
  // An array with no usable entries stays an (empty) array rather than becoming
28
84
  // undefined: "I declare no file types" is a real answer, distinct from "I say
29
85
  // nothing about file types", and only the second one defers to the imports.
@@ -55,11 +111,20 @@ export function readEditorConfig(deckDir) {
55
111
  hiddenPaths: asStringArray(editor.hiddenPaths),
56
112
  fileTypes: asFileTypes(editor.fileTypes),
57
113
  extensions: asStringArray(editor.extensions) ?? asStringArray(data.editorExtensions),
58
- defaultPlayFile: typeof editor.defaultPlayFile === 'string'
59
- ? editor.defaultPlayFile
60
- : importedDefaultPlayFile(deckDir),
114
+ ...(typeof editor.defaultPlayFile === 'string'
115
+ ? ownDefaultPlayFile(deckDir, editor.defaultPlayFile)
116
+ : { defaultPlayFile: importedDefaultPlayFile(deckDir) }),
61
117
  };
62
118
  }
119
+ // An empty string keeps its old meaning, "no start file", with nothing refused.
120
+ function ownDefaultPlayFile(deckDir, raw) {
121
+ if (!raw.trim())
122
+ return {};
123
+ const normalized = normalizePlayFile(deckDir, raw);
124
+ return normalized.ok
125
+ ? { defaultPlayFile: normalized.path }
126
+ : { defaultPlayFileRefused: { value: raw, reason: normalized.reason } };
127
+ }
63
128
  // Which scene the play panel opens with, when the deck names none itself. Only a
64
129
  // scene kit has the concept, and a deck that imported one has its scenes without
65
130
  // having said so -- without this the panel loads the kit's runtime with no scene
@@ -74,10 +139,11 @@ function importedDefaultPlayFile(deckDir) {
74
139
  const declared = value && typeof value === 'object'
75
140
  ? value.defaultPlayFile
76
141
  : undefined;
77
- if (typeof declared !== 'string' || !declared.trim())
142
+ if (typeof declared !== 'string')
78
143
  continue;
79
- if (fs.existsSync(path.join(deckDir, declared.trim())))
80
- return declared.trim();
144
+ const normalized = normalizePlayFile(deckDir, declared);
145
+ if (normalized.ok && fs.existsSync(path.join(deckDir, normalized.path)))
146
+ return normalized.path;
81
147
  }
82
148
  return undefined;
83
149
  }
package/dist/init.js CHANGED
@@ -54,7 +54,7 @@ if (!(await mountEditorRoute())) startGame(initCard());
54
54
  // Registry version of castle-web-sdk to inject when scaffolding from a
55
55
  // globally-installed castle-web (not from inside the workspace). Bumped
56
56
  // alongside cli/sdk version bumps.
57
- const PUBLISHED_SDK_VERSION = '0.4.35';
57
+ const PUBLISHED_SDK_VERSION = '0.4.36';
58
58
  // Range, not a pin: `castle-web-fonts` is its own package precisely so a tenth
59
59
  // face can ship without a cli release, and a caret range means an existing
60
60
  // deck's next install picks it up. Nothing here has to move when it does.
@@ -25,7 +25,7 @@ const MAX_ITERATIONS = 40;
25
25
  // PLAYTEST_MAX_SHOTS frames each -- at most 24 -- small enough to list in
26
26
  // full for the task card, with no truncation needed.
27
27
  function playtestFrameList(frames) {
28
- return [...frames].sort();
28
+ return [...frames].sort((a, b) => a.localeCompare(b, "en", { numeric: true }));
29
29
  }
30
30
  // -- run logging ----------------------------------------------------------------
31
31
  // Structured JSONL, one event per line, appended (task retry attempts share
@@ -1,6 +1,6 @@
1
1
  // The real (Playwright-backed) PlaytestExecutor: navigates a fresh
2
2
  // browser context + page to the served deck, settles, replays a timeline of
3
- // mouse/keyboard primitives and screenshot captures on one wall clock, and
3
+ // mouse/touch/keyboard primitives and screenshot captures on one wall clock, and
4
4
  // reports console/pageerror output plus rAF-tick capture-health diagnostics.
5
5
  // See native/playtest.ts for the browser-free types this implements and
6
6
  // native/playtest-browser.ts for the warm-Chromium lifecycle this borrows a
@@ -65,7 +65,25 @@ function attachConsoleCapture(page, navStart, boundaries) {
65
65
  page.on("pageerror", (err) => push("pageerror", err.message));
66
66
  return log;
67
67
  }
68
- async function runTimeline(page, input) {
68
+ // Playwright's touchscreen only taps, so a touch drag goes through CDP --
69
+ // the same Input.dispatchTouchEvent that touchscreen.tap itself sends. A
70
+ // finger has no hover: Chromium rejects a touchMove or touchEnd with no touch
71
+ // in progress ("Must send a TouchStart first"), failing the whole call, so a
72
+ // move or up with no finger down is dropped rather than sent. One finger only:
73
+ // Chromium delivers a second down while the finger is down as a touchmove.
74
+ function touchDispatcher(cdp) {
75
+ let down = false;
76
+ return async (type, x, y) => {
77
+ if (type !== "touchStart" && !down)
78
+ return;
79
+ down = type !== "touchEnd";
80
+ await cdp.send("Input.dispatchTouchEvent", {
81
+ type,
82
+ touchPoints: type === "touchEnd" ? [] : [{ x, y }],
83
+ });
84
+ };
85
+ }
86
+ async function runTimeline(page, input, touch) {
69
87
  const sessionStart = Date.now();
70
88
  const frames = [];
71
89
  let framesRequested = 0;
@@ -87,6 +105,15 @@ async function runTimeline(page, input) {
87
105
  await page.mouse.move(event.x, event.y);
88
106
  await page.mouse.up();
89
107
  break;
108
+ case "touchstart":
109
+ await touch("touchStart", event.x, event.y);
110
+ break;
111
+ case "touchmove":
112
+ await touch("touchMove", event.x, event.y);
113
+ break;
114
+ case "touchend":
115
+ await touch("touchEnd", event.x, event.y);
116
+ break;
90
117
  case "keydown":
91
118
  await page.keyboard.down(event.key);
92
119
  break;
@@ -114,7 +141,11 @@ async function measureDiagnostics(page, wallMs) {
114
141
  async function runSessionOnBrowser(browser, input) {
115
142
  const navStart = Date.now();
116
143
  const boundaries = { settleStartRel: null, sessionStartRel: null };
117
- const context = await browser.newContext({ viewport: PLAYTEST_VIEWPORT });
144
+ const touchInput = input.input === "touch";
145
+ const context = await browser.newContext({
146
+ viewport: PLAYTEST_VIEWPORT,
147
+ ...(touchInput ? { hasTouch: true, isMobile: true } : {}),
148
+ });
118
149
  let consoleLog = [];
119
150
  try {
120
151
  const page = await context.newPage();
@@ -134,7 +165,8 @@ async function runSessionOnBrowser(browser, input) {
134
165
  };
135
166
  requestAnimationFrame(tick);
136
167
  });
137
- const { frames, framesRequested, wallMs } = await runTimeline(page, input);
168
+ const touch = touchInput ? touchDispatcher(await context.newCDPSession(page)) : null;
169
+ const { frames, framesRequested, wallMs } = await runTimeline(page, input, touch);
138
170
  const diagnostics = await measureDiagnostics(page, wallMs);
139
171
  diagnostics.framesRequested = framesRequested;
140
172
  diagnostics.framesCaptured = frames.length;
@@ -1,3 +1,4 @@
1
1
  import { type PlaytestExecutor } from './playtest.js';
2
2
  export declare function contentGrantFromBrowserUrl(value: string): string | null;
3
+ export declare const REMOTE_TOUCH_UNAVAILABLE = "touch input is not available in the cloud playtest browser yet, so nothing was run and this call was not counted -- use input \"mouse\", and say that touch was not checked";
3
4
  export declare function createRemotePlaytestExecutor(browserContentUrl: () => string | null): PlaytestExecutor;
@@ -29,7 +29,26 @@ export function contentGrantFromBrowserUrl(value) {
29
29
  return null;
30
30
  }
31
31
  }
32
- function operationFromTimeline(input) {
32
+ // Ghost and the browser worker validate a playtest operation key by key, so
33
+ // `input`/`playFile` are sent only when a call needs them, and a refusal of
34
+ // those keys is remembered for the rest of the serve. Ghost answers every
35
+ // validation failure with BROWSER_INVALID_OPERATION; only its unknown-key
36
+ // message, "operation is invalid", is that refusal -- a bad coordinate or
37
+ // timestamp is the call's own mistake and must not switch the extension off.
38
+ // (A worker that refuses the keys after Ghost accepted them fails the job as a
39
+ // generic BROWSER_SERVICE_FAILED, which cannot be told apart, so the host
40
+ // rollout must reach the workers before Ghost.)
41
+ function refusedExtension(error) {
42
+ const { message, extensions } = (error ?? {});
43
+ return (extensions?.code === 'BROWSER_INVALID_OPERATION' &&
44
+ typeof message === 'string' &&
45
+ message.startsWith('operation is invalid ['));
46
+ }
47
+ export const REMOTE_TOUCH_UNAVAILABLE = "touch input is not available in the cloud playtest browser yet, so nothing was run and this call was not counted -- use input \"mouse\", and say that touch was not checked";
48
+ function wantsExtension(input) {
49
+ return input.input === 'touch' || Boolean(input.playFile);
50
+ }
51
+ function operationFromTimeline(input, extended) {
33
52
  const actions = [];
34
53
  const screenshots = [];
35
54
  const keyDown = new Map();
@@ -59,9 +78,9 @@ function operationFromTimeline(input) {
59
78
  continue;
60
79
  actions.push({
61
80
  t: event.t,
62
- type: event.kind === 'mousedown'
81
+ type: event.kind === 'mousedown' || event.kind === 'touchstart'
63
82
  ? 'down'
64
- : event.kind === 'mouseup'
83
+ : event.kind === 'mouseup' || event.kind === 'touchend'
65
84
  ? 'up'
66
85
  : 'move',
67
86
  x: event.x,
@@ -70,7 +89,14 @@ function operationFromTimeline(input) {
70
89
  }
71
90
  }
72
91
  actions.sort((a, b) => a.t - b.t);
73
- return { kind: 'playtest', durationMs: input.durationMs, actions, screenshots };
92
+ return {
93
+ kind: 'playtest',
94
+ durationMs: input.durationMs,
95
+ actions,
96
+ screenshots,
97
+ ...(extended && input.input === 'touch' ? { input: 'touch' } : {}),
98
+ ...(extended && input.playFile ? { playFile: input.playFile } : {}),
99
+ };
74
100
  }
75
101
  function isRecord(value) {
76
102
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
@@ -123,6 +149,24 @@ function validRemoteResult(result) {
123
149
  Boolean(result.diagnostics));
124
150
  }
125
151
  export function createRemotePlaytestExecutor(browserContentUrl) {
152
+ let extensionRefused = false;
153
+ const submit = async (grant, input) => {
154
+ if (wantsExtension(input) && !extensionRefused) {
155
+ try {
156
+ const result = await api.runBrowserJob(grant, operationFromTimeline(input, true), input.signal);
157
+ return { result: result, degraded: false };
158
+ }
159
+ catch (error) {
160
+ if (!refusedExtension(error))
161
+ throw error;
162
+ extensionRefused = true;
163
+ }
164
+ }
165
+ if (input.input === 'touch')
166
+ return { refusedTouch: true };
167
+ const result = await api.runBrowserJob(grant, operationFromTimeline(input, false), input.signal);
168
+ return { result: result, degraded: Boolean(input.playFile) };
169
+ };
126
170
  return {
127
171
  async runSession(input) {
128
172
  const contentUrl = browserContentUrl();
@@ -130,7 +174,10 @@ export function createRemotePlaytestExecutor(browserContentUrl) {
130
174
  if (!grant)
131
175
  return failed(input, 'the serve could not mint a browser content grant');
132
176
  try {
133
- const result = (await api.runBrowserJob(grant, operationFromTimeline(input), input.signal));
177
+ const submitted = await submit(grant, input);
178
+ if ('refusedTouch' in submitted)
179
+ return { ...failed(input, REMOTE_TOUCH_UNAVAILABLE), unsupported: true };
180
+ const { result, degraded } = submitted;
134
181
  if (!result.ok) {
135
182
  if (!validFailureDiagnostics(result)) {
136
183
  return failed(input, 'browser worker returned invalid failure diagnostics');
@@ -151,6 +198,15 @@ export function createRemotePlaytestExecutor(browserContentUrl) {
151
198
  })),
152
199
  console: result.console,
153
200
  diagnostics: result.diagnostics,
201
+ ...(degraded
202
+ ? {
203
+ environment: {
204
+ playFile: null,
205
+ input: 'mouse',
206
+ note: `the cloud playtest browser cannot open a play file yet, so it loaded the entry point instead of ${input.playFile}. What the user's Play panel shows may differ.`,
207
+ },
208
+ }
209
+ : {}),
154
210
  };
155
211
  }
156
212
  catch (error) {