castle-web-cli 0.4.157 → 0.4.159

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 (243) hide show
  1. package/dist/agent-failures.d.ts +1 -0
  2. package/dist/agent-failures.js +6 -4
  3. package/dist/agent-prompts.js +4 -0
  4. package/dist/agent.d.ts +11 -0
  5. package/dist/agent.js +175 -15
  6. package/dist/bundle.js +8 -2
  7. package/dist/castle-host/host.js +4 -2
  8. package/dist/castleJson.js +6 -1
  9. package/dist/check.d.ts +1 -0
  10. package/dist/check.js +25 -0
  11. package/dist/devSessionServer.js +31 -7
  12. package/dist/imports.js +1 -19
  13. package/dist/index.js +15 -1
  14. package/dist/init.d.ts +2 -2
  15. package/dist/init.js +78 -42
  16. package/dist/mcpPlaytest.d.ts +1 -0
  17. package/dist/mcpPlaytest.js +182 -0
  18. package/dist/metering.d.ts +24 -0
  19. package/dist/metering.js +87 -0
  20. package/dist/sdkResolve.d.ts +3 -0
  21. package/dist/sdkResolve.js +55 -0
  22. package/dist/serve.js +50 -4
  23. package/dist/serverBundle.js +8 -3
  24. package/dist/shell/assets/{index-DsDkYFvP.js → index-DMSoTyXh.js} +64 -64
  25. package/dist/shell/assets/{index-DwezawBs.css → index-DfZmu3XC.css} +1 -1
  26. package/dist/shell/index.html +2 -2
  27. package/dist/vitePlugins.d.ts +1 -0
  28. package/dist/vitePlugins.js +20 -0
  29. package/kits/base/CLAUDE.md +196 -0
  30. package/kits/{blank → base}/castle.json +5 -2
  31. package/kits/base/editorHost.js +172 -0
  32. package/kits/{blank → base}/node_modules/.package-lock.json +3 -2
  33. package/kits/{blank → base}/package-lock.json +7 -4
  34. package/kits/{blank → base}/package.json +4 -2
  35. package/kits/base/sdk/README.md +569 -0
  36. package/kits/base/sdk/castle.d.ts +27 -0
  37. package/kits/base/sdk/castle.js +16 -0
  38. package/kits/base/sdk/commands.d.ts +181 -0
  39. package/kits/base/sdk/commands.js +16 -0
  40. package/kits/base/sdk/context.d.ts +17 -0
  41. package/kits/base/sdk/context.js +15 -0
  42. package/kits/base/sdk/errors.d.ts +22 -0
  43. package/kits/base/sdk/errors.js +16 -0
  44. package/kits/base/sdk/files.d.ts +15 -0
  45. package/kits/base/sdk/files.js +52 -0
  46. package/kits/base/sdk/haptics.d.ts +6 -0
  47. package/kits/base/sdk/haptics.js +36 -0
  48. package/kits/base/sdk/leaderboard.d.ts +26 -0
  49. package/kits/base/sdk/leaderboard.js +296 -0
  50. package/kits/base/sdk/lifecycle.d.ts +4 -0
  51. package/kits/base/sdk/lifecycle.js +13 -0
  52. package/kits/base/sdk/multiplayer.d.ts +43 -0
  53. package/kits/base/sdk/multiplayer.js +464 -0
  54. package/kits/base/sdk/multiplayerProtocol.d.ts +51 -0
  55. package/kits/base/sdk/multiplayerProtocol.js +5 -0
  56. package/kits/base/sdk/passes.d.ts +7 -0
  57. package/kits/base/sdk/passes.js +84 -0
  58. package/kits/base/sdk/portal.d.ts +7 -0
  59. package/kits/base/sdk/portal.js +35 -0
  60. package/kits/base/sdk/runtime.d.ts +62 -0
  61. package/kits/base/sdk/runtime.js +970 -0
  62. package/kits/base/sdk/saveQueue.d.ts +27 -0
  63. package/kits/base/sdk/saveQueue.js +205 -0
  64. package/kits/base/sdk/server/platformHandle.d.ts +25 -0
  65. package/kits/base/sdk/server/platformHandle.js +1 -0
  66. package/kits/base/sdk/server/wrapper.d.ts +28 -0
  67. package/kits/base/sdk/server/wrapper.js +109 -0
  68. package/kits/base/sdk/storage.d.ts +16 -0
  69. package/kits/base/sdk/storage.js +323 -0
  70. package/kits/base/sdk/time.d.ts +17 -0
  71. package/kits/base/sdk/time.js +120 -0
  72. package/kits/base/sdk/transport.d.ts +17 -0
  73. package/kits/base/sdk/transport.js +141 -0
  74. package/kits/base/sdk/types.d.ts +3 -0
  75. package/kits/base/sdk/types.js +1 -0
  76. package/kits/base/sdk/user.d.ts +10 -0
  77. package/kits/base/sdk/user.js +45 -0
  78. package/kits/multiplayer-2d/.prettierrc +5 -0
  79. package/kits/multiplayer-2d/CLAUDE.md +129 -0
  80. package/kits/multiplayer-2d/behaviors/Box.jsx +92 -0
  81. package/kits/multiplayer-2d/behaviors/Player.jsx +86 -0
  82. package/kits/multiplayer-2d/blueprints/crate.scene +13 -0
  83. package/kits/multiplayer-2d/blueprints/other-player.scene +13 -0
  84. package/kits/multiplayer-2d/blueprints/player.scene +15 -0
  85. package/kits/multiplayer-2d/blueprints/wall.scene +12 -0
  86. package/kits/multiplayer-2d/castle.json +39 -0
  87. package/kits/multiplayer-2d/code/client/avatars.js +93 -0
  88. package/kits/multiplayer-2d/code/client/poses.js +168 -0
  89. package/kits/multiplayer-2d/code/server/index.js +26 -0
  90. package/kits/multiplayer-2d/code/server/limits.js +99 -0
  91. package/kits/multiplayer-2d/code/server/ownership.js +177 -0
  92. package/kits/multiplayer-2d/code/server/players.js +127 -0
  93. package/kits/multiplayer-2d/code/server/sceneBodies.js +98 -0
  94. package/kits/multiplayer-2d/code/server/world.js +337 -0
  95. package/kits/multiplayer-2d/code/systems/multiplayer.js +424 -0
  96. package/kits/multiplayer-2d/eslint.config.js +21 -0
  97. package/kits/multiplayer-2d/fonts.generated.js +9 -0
  98. package/kits/multiplayer-2d/index.html +25 -0
  99. package/kits/multiplayer-2d/main.jsx +10 -0
  100. package/kits/multiplayer-2d/package-lock.json +351 -0
  101. package/kits/multiplayer-2d/package.json +30 -0
  102. package/kits/multiplayer-2d/scenes/main.scene +20 -0
  103. package/kits/multiplayer-2d/test/two-client-test.mjs +341 -0
  104. package/kits/multiplayer-2d/theme.style +4 -0
  105. package/kits/multiplayer-3d/.prettierrc +5 -0
  106. package/kits/multiplayer-3d/CLAUDE.md +215 -0
  107. package/kits/multiplayer-3d/blueprints/ball.scene +21 -0
  108. package/kits/multiplayer-3d/blueprints/other-player.scene +21 -0
  109. package/kits/multiplayer-3d/castle.json +43 -0
  110. package/kits/multiplayer-3d/code/client/avatars.js +136 -0
  111. package/kits/multiplayer-3d/code/client/nameTags.js +101 -0
  112. package/kits/multiplayer-3d/code/client/poses.js +220 -0
  113. package/kits/multiplayer-3d/code/server/index.js +23 -0
  114. package/kits/multiplayer-3d/code/server/limits.js +107 -0
  115. package/kits/multiplayer-3d/code/server/ownership.js +157 -0
  116. package/kits/multiplayer-3d/code/server/players.js +114 -0
  117. package/kits/multiplayer-3d/code/server/sceneBodies.js +88 -0
  118. package/kits/multiplayer-3d/code/server/world.js +306 -0
  119. package/kits/multiplayer-3d/code/systems/multiplayer.js +427 -0
  120. package/kits/multiplayer-3d/eslint.config.js +20 -0
  121. package/kits/multiplayer-3d/fonts.generated.js +9 -0
  122. package/kits/multiplayer-3d/index.html +22 -0
  123. package/kits/multiplayer-3d/main.jsx +9 -0
  124. package/kits/multiplayer-3d/package-lock.json +368 -0
  125. package/kits/multiplayer-3d/package.json +32 -0
  126. package/kits/multiplayer-3d/scenes/main.scene +409 -0
  127. package/kits/multiplayer-3d/theme.style +3 -0
  128. package/kits/physics-2d/CLAUDE.md +6 -0
  129. package/kits/physics-2d/castle.json +7 -1
  130. package/kits/physics-2d/editors/behaviorRegistry.js +5 -0
  131. package/kits/physics-2d/editors/editorRegistry.jsx +3 -1
  132. package/kits/physics-2d/engine/files.js +9 -5
  133. package/kits/physics-2d/engine/systemRegistry.js +2 -0
  134. package/kits/physics-2d/engine/ui.module.css +11 -0
  135. package/kits/physics-2d/package-lock.json +8 -2
  136. package/kits/physics-2d/package.json +2 -2
  137. package/kits/physics-3d/CLAUDE.md +4 -4
  138. package/kits/physics-3d/behaviors/Shape.jsx +1 -1
  139. package/kits/physics-3d/blueprints/crate.scene +1 -1
  140. package/kits/physics-3d/blueprints/door.scene +4 -4
  141. package/kits/physics-3d/blueprints/pillar.scene +1 -1
  142. package/kits/physics-3d/blueprints/platform.scene +1 -1
  143. package/kits/physics-3d/blueprints/statue.scene +1 -1
  144. package/kits/physics-3d/blueprints/wall.scene +1 -1
  145. package/kits/physics-3d/castle.json +8 -11
  146. package/kits/physics-3d/docs/pxmodel-format.md +1 -1
  147. package/kits/physics-3d/engine3d/materials.js +3 -3
  148. package/kits/physics-3d/engine3d/modelFiles.js +4 -2
  149. package/kits/physics-3d/engine3d/world3d.js +1 -1
  150. package/kits/physics-3d/fonts.generated.js +9 -0
  151. package/kits/physics-3d/main.jsx +1 -1
  152. package/kits/physics-3d/package-lock.json +85 -2
  153. package/kits/physics-3d/package.json +6 -2
  154. package/kits/physics-3d/scenes/main.scene +1 -1
  155. package/kits/physics-3d/scenes/model-lab.scene +1 -1
  156. package/kits/real-time/.prettierrc +5 -0
  157. package/kits/real-time/CLAUDE.md +60 -0
  158. package/kits/real-time/castle.json +19 -0
  159. package/kits/real-time/code/client/clock.js +68 -0
  160. package/kits/real-time/code/client/connection.js +134 -0
  161. package/kits/real-time/code/client/gameHooks.js +50 -0
  162. package/kits/real-time/code/client/interpolate.js +82 -0
  163. package/kits/real-time/code/client/messages.js +55 -0
  164. package/kits/real-time/code/client/ownership.js +94 -0
  165. package/kits/real-time/code/client/smooth.js +54 -0
  166. package/kits/real-time/code/server/blockDelta.js +48 -0
  167. package/kits/real-time/code/server/gameHooks.js +47 -0
  168. package/kits/real-time/code/server/limits.js +35 -0
  169. package/kits/real-time/code/server/session.js +251 -0
  170. package/kits/real-time/eslint.config.js +22 -0
  171. package/kits/real-time/index.html +45 -0
  172. package/kits/real-time/package.json +14 -0
  173. package/kits/{tabletop → turn-based}/CLAUDE.md +42 -49
  174. package/kits/turn-based/blueprints/board.scene +15 -0
  175. package/kits/turn-based/blueprints/square.scene +25 -0
  176. package/kits/turn-based/castle.json +37 -0
  177. package/kits/turn-based/code/behaviors/Board.jsx +169 -0
  178. package/kits/turn-based/code/behaviors/Square.jsx +26 -0
  179. package/kits/turn-based/code/server/index.js +152 -0
  180. package/kits/turn-based/drawings/square.sprite +25 -0
  181. package/kits/turn-based/eslint.config.js +12 -0
  182. package/kits/turn-based/fonts.generated.js +9 -0
  183. package/kits/{tabletop → turn-based}/index.html +5 -3
  184. package/kits/{tabletop → turn-based}/main.jsx +2 -2
  185. package/kits/{tabletop → turn-based}/package-lock.json +17 -4
  186. package/kits/{tabletop → turn-based}/package.json +5 -2
  187. package/kits/turn-based/room.js +289 -0
  188. package/kits/turn-based/scenes/main.scene +227 -0
  189. package/kits/{tabletop → turn-based}/testing.js +131 -99
  190. package/kits/turn-based/theme.style +3 -0
  191. package/package.json +1 -1
  192. package/kits/blank/CLAUDE.md +0 -198
  193. package/kits/tabletop/castle.json +0 -34
  194. package/kits/tabletop/room.js +0 -269
  195. /package/kits/{blank → base}/index.html +0 -0
  196. /package/kits/{blank → base}/node_modules/castle-web-fonts/README.md +0 -0
  197. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.d.ts +0 -0
  198. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/catalog.js +0 -0
  199. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.d.ts +0 -0
  200. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/face.js +0 -0
  201. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.d.ts +0 -0
  202. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Bore.js +0 -0
  203. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.d.ts +0 -0
  204. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/CourierPrime.js +0 -0
  205. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.d.ts +0 -0
  206. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/DMSans.js +0 -0
  207. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.d.ts +0 -0
  208. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Glacier.js +0 -0
  209. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.d.ts +0 -0
  210. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/HelicoCentrica.js +0 -0
  211. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.d.ts +0 -0
  212. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Piazzolla.js +0 -0
  213. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.d.ts +0 -0
  214. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Synco.js +0 -0
  215. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.d.ts +0 -0
  216. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/Tektur.js +0 -0
  217. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.d.ts +0 -0
  218. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/faces/YatraOne.js +0 -0
  219. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.d.ts +0 -0
  220. /package/kits/{blank → base}/node_modules/castle-web-fonts/dist/index.js +0 -0
  221. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/CC-BY-ND-4.0.txt +0 -0
  222. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/FONTS.md +0 -0
  223. /package/kits/{blank → base}/node_modules/castle-web-fonts/licenses/OFL-1.1.txt +0 -0
  224. /package/kits/{blank → base}/node_modules/castle-web-fonts/package.json +0 -0
  225. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Bore.woff2 +0 -0
  226. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/CourierPrime.woff2 +0 -0
  227. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/DMSans.woff2 +0 -0
  228. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Glacier.woff2 +0 -0
  229. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/HelicoCentrica.woff2 +0 -0
  230. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Piazzolla.woff2 +0 -0
  231. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/SOURCE.txt +0 -0
  232. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Synco.woff2 +0 -0
  233. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/Tektur.woff2 +0 -0
  234. /package/kits/{blank → base}/node_modules/castle-web-fonts/woff2/YatraOne.woff2 +0 -0
  235. /package/kits/physics-3d/drawings/{crate.pxart → crate.sprite} +0 -0
  236. /package/kits/physics-3d/drawings/{door-edge.pxart → door-edge.sprite} +0 -0
  237. /package/kits/physics-3d/drawings/{door.pxart → door.sprite} +0 -0
  238. /package/kits/physics-3d/drawings/{face.pxart → face.sprite} +0 -0
  239. /package/kits/physics-3d/drawings/{floor.pxart → floor.sprite} +0 -0
  240. /package/kits/physics-3d/drawings/{platform.pxart → platform.sprite} +0 -0
  241. /package/kits/physics-3d/drawings/{statue.pxart → statue.sprite} +0 -0
  242. /package/kits/physics-3d/drawings/{wall.pxart → wall.sprite} +0 -0
  243. /package/kits/{tabletop → turn-based}/.prettierrc +0 -0
@@ -0,0 +1,55 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { readCastleJson } from './castleJson.js';
4
+ import { IMPORTS_DIR } from './imports.js';
5
+ // Where `castle-web-sdk` comes from now: an imported deck that says it provides
6
+ // it (`castle.json` `provides`), which in practice is the `base` kit every deck
7
+ // imports. The specifier deck code writes does NOT change -- a published deck,
8
+ // a doc, and every line already written keep saying `castle-web-sdk` -- so the
9
+ // move is a resolution detail rather than a rewrite of everything in the wild.
10
+ //
11
+ // A deck with no such import falls through to ordinary node resolution, which
12
+ // is what keeps decks predating the move (and the kits' own workspace checkouts)
13
+ // building against the npm package.
14
+ export const SDK_PACKAGE = 'castle-web-sdk';
15
+ // Subpath -> the file inside a provider's `sdk/` that answers it. Mirrors
16
+ // sdk/package.json `exports`; the kit copy is that package's dist, flattened one
17
+ // level (see scripts/copy-sdk-to-kit.mjs).
18
+ const SUBPATH_ENTRIES = {
19
+ '': 'castle.js',
20
+ '/server': 'server/wrapper.js',
21
+ };
22
+ function providersIn(deckDir) {
23
+ let aliases;
24
+ try {
25
+ aliases = fs.readdirSync(path.join(deckDir, IMPORTS_DIR)).sort();
26
+ }
27
+ catch {
28
+ return [];
29
+ }
30
+ return aliases.filter((alias) => {
31
+ const provides = readCastleJson(path.join(deckDir, IMPORTS_DIR, alias))?.provides;
32
+ return Array.isArray(provides) && provides.includes(SDK_PACKAGE);
33
+ });
34
+ }
35
+ // The file a `castle-web-sdk[/subpath]` specifier means in this deck, or null
36
+ // when no import provides it. Deck-root-absolute, so callers can hand it
37
+ // straight to vite or to node.
38
+ export function resolveSdkSpecifier(deckDir, specifier) {
39
+ if (specifier !== SDK_PACKAGE && !specifier.startsWith(`${SDK_PACKAGE}/`))
40
+ return null;
41
+ const entry = SUBPATH_ENTRIES[specifier.slice(SDK_PACKAGE.length)];
42
+ if (!entry)
43
+ return null;
44
+ for (const alias of providersIn(deckDir)) {
45
+ const file = path.join(deckDir, IMPORTS_DIR, alias, 'sdk', entry);
46
+ if (fs.existsSync(file))
47
+ return file;
48
+ }
49
+ return null;
50
+ }
51
+ // Whether this deck gets the SDK from an import rather than node_modules. What
52
+ // `init` asks before writing a dependency the deck does not need.
53
+ export function hasSdkProvider(deckDir) {
54
+ return resolveSdkSpecifier(deckDir, SDK_PACKAGE) !== null;
55
+ }
package/dist/serve.js CHANGED
@@ -8,7 +8,7 @@ import { WebSocketServer, WebSocket } from 'ws';
8
8
  import { isIP } from 'net';
9
9
  import { createIdeServer, FAVICON_FILES, FAVICON_LINK_TAGS } from './ide.js';
10
10
  import { createAgentServer } from './agent.js';
11
- import { sceneFilesPlugin, importsAliasPlugin, forksOverlayPlugin } from './vitePlugins.js';
11
+ import { sceneFilesPlugin, importsAliasPlugin, forksOverlayPlugin, sdkAliasPlugin, } from './vitePlugins.js';
12
12
  import { installFilesChangedWatcher } from './filesChanged.js';
13
13
  import { autoUpdateImports, IMPORTS_DIR, lockImportTree } from './imports.js';
14
14
  import { ensureAgentsSymlink } from './init.js';
@@ -118,6 +118,15 @@ function listenLocalContentServer(bindHost, upstreamHost, upstreamPort, role, se
118
118
  });
119
119
  }
120
120
  function proxyViteClientUpgrade(req, socket, head, upstreamHost, upstreamPort, headers) {
121
+ // Both ends live for the whole HMR session, and `pipe` does not forward
122
+ // 'error' -- so a reset on either side (a closed tab, a sleeping laptop)
123
+ // reaches a raw socket with no listener, which takes the serve process down.
124
+ let paired = null;
125
+ const teardown = () => {
126
+ socket.destroy();
127
+ paired?.destroy();
128
+ };
129
+ socket.on('error', teardown);
121
130
  const upstream = http.request({
122
131
  hostname: upstreamHost,
123
132
  port: upstreamPort,
@@ -126,6 +135,8 @@ function proxyViteClientUpgrade(req, socket, head, upstreamHost, upstreamPort, h
126
135
  headers,
127
136
  });
128
137
  upstream.on('upgrade', (response, upstreamSocket, upstreamHead) => {
138
+ paired = upstreamSocket;
139
+ upstreamSocket.on('error', teardown);
129
140
  const responseHeaders = [];
130
141
  for (let i = 0; i < response.rawHeaders.length; i += 2) {
131
142
  responseHeaders.push(`${response.rawHeaders[i]}: ${response.rawHeaders[i + 1]}`);
@@ -150,7 +161,7 @@ function proxyViteClientUpgrade(req, socket, head, upstreamHost, upstreamPort, h
150
161
  socket.write(`HTTP/1.1 ${response.statusCode || 502} ${response.statusMessage || ''}\r\n\r\n`);
151
162
  response.pipe(socket);
152
163
  });
153
- upstream.on('error', () => socket.destroy());
164
+ upstream.on('error', teardown);
154
165
  upstream.end();
155
166
  }
156
167
  export function isViteClientUpgrade(req) {
@@ -708,7 +719,11 @@ export async function serve(dir, options = {}) {
708
719
  // The WS server forwards `restart` to the browser, but it also needs the
709
720
  // Vite instance so it can drop transform caches first (see invalidateModuleCaches).
710
721
  const viteHolder = { vite: null };
711
- const { broadcast, handleUpgrade: handleCastleUpgrade } = startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security, devSession);
722
+ // Same late-binding trick as viteHolder: the control WS is up before
723
+ // createAgentServer exists, and `playtest_request` needs the agent server's
724
+ // warm Chromium and task store.
725
+ const playtestHolder = { run: null };
726
+ const { broadcast, handleUpgrade: handleCastleUpgrade } = startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security, devSession, playtestHolder);
712
727
  broadcastDevLog = broadcast;
713
728
  // The deck root `/` serves a split-view shell: the deck in an iframe plus a
714
729
  // toggle button for an xterm.js terminal. The terminal's PTY is lazy -- it
@@ -776,12 +791,14 @@ export async function serve(dir, options = {}) {
776
791
  authorizeUpgrade: (req) => security.consumeTicket(req, 'agent'),
777
792
  });
778
793
  process.on('exit', () => agentServer.shutdown());
794
+ playtestHolder.run = (req) => agentServer.runPlaytestFor(req);
779
795
  const vite = await createViteServer({
780
796
  root: projectDir,
781
797
  plugins: [
782
798
  castlePlugin(security, ideServer, agentServer, options.host),
783
799
  forksOverlayPlugin(),
784
800
  importsAliasPlugin(),
801
+ sdkAliasPlugin(),
785
802
  sceneFilesPlugin(),
786
803
  ],
787
804
  server: {
@@ -986,7 +1003,32 @@ async function serveDetached(projectDir, options) {
986
1003
  }
987
1004
  throw new Error(`Detached serve did not write ${serveJsonPath} within 15s; check ${logPath} for startup errors.`);
988
1005
  }
989
- function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security, multiplayer) {
1006
+ // Answered by the SERVE itself (the agent server owns the warm Chromium),
1007
+ // unlike screenshot_request which a browser tab answers -- so no
1008
+ // requestOrigins bookkeeping and no forwarding to other clients.
1009
+ function handlePlaytestRequest(ws, msg, playtestHolder) {
1010
+ const { requestId } = msg;
1011
+ const reply = (body) => {
1012
+ if (ws.readyState === WebSocket.OPEN) {
1013
+ ws.send(JSON.stringify({ type: 'playtest_response', requestId, ...body }));
1014
+ }
1015
+ };
1016
+ const run = playtestHolder.run;
1017
+ if (!run) {
1018
+ reply({ ok: false, output: 'Error: this serve has no agent server to playtest with.' });
1019
+ return;
1020
+ }
1021
+ void run({
1022
+ taskId: typeof msg.taskId === 'string' ? msg.taskId : undefined,
1023
+ args: (msg.args ?? {}),
1024
+ onProgress: (label) => {
1025
+ if (ws.readyState === WebSocket.OPEN) {
1026
+ ws.send(JSON.stringify({ type: 'playtest_progress', requestId, label }));
1027
+ }
1028
+ },
1029
+ }).then((result) => reply(result), (err) => reply({ ok: false, output: `Error: ${err instanceof Error ? err.message : String(err)}` }));
1030
+ }
1031
+ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security, multiplayer, playtestHolder) {
990
1032
  // The control socket is upgraded directly on Vite's HTTP server. Browser
991
1033
  // shell clients use the named path and a one-use ticket; pre-bridge SDKs use
992
1034
  // a separate ticket carrying their content role. The root path is accepted
@@ -1099,6 +1141,10 @@ function startWSServer(projectDir, logFile, screenshotsDir, viteHolder, security
1099
1141
  c.send(fwd);
1100
1142
  }
1101
1143
  }
1144
+ if (msg.type === 'playtest_request' && typeof msg.requestId === 'string') {
1145
+ handlePlaytestRequest(ws, msg, playtestHolder);
1146
+ return;
1147
+ }
1102
1148
  // Things one client asks the OTHERS to answer: a tab's canvas for
1103
1149
  // `screenshot`, and the browser's signed-in deck listing for
1104
1150
  // `list-decks` (a cloud sandbox holds no Castle token; the browser
@@ -3,7 +3,8 @@ import { createRequire } from 'module';
3
3
  import * as path from 'path';
4
4
  import { build } from 'vite';
5
5
  import { resolveServerConfig, } from './castleJson.js';
6
- import { forksOverlayPlugin, importsAliasPlugin } from './vitePlugins.js';
6
+ import { forksOverlayPlugin, importsAliasPlugin, sdkAliasPlugin } from './vitePlugins.js';
7
+ import { resolveSdkSpecifier, SDK_PACKAGE } from './sdkResolve.js';
7
8
  const WARN_BYTES = 1024 * 1024;
8
9
  const MAX_BYTES = 5 * 1024 * 1024;
9
10
  const VIRTUAL_ENTRY = '\0castle-server-entry';
@@ -33,7 +34,11 @@ export async function buildServerBundle(projectDir) {
33
34
  // Dev keeps readable code; the published artifact is minified and checked below
34
35
  // for the single-output-file contract.
35
36
  export async function bundleServerModule(projectDir, config, minify) {
36
- const sdkEntry = createRequire(path.join(projectDir, 'package.json')).resolve('castle-web-sdk/server');
37
+ // The synthesized entry names the file directly, so it has to resolve here
38
+ // rather than through the plugin: the deck's `base` import when it has one,
39
+ // node_modules for a deck predating the move.
40
+ const sdkEntry = resolveSdkSpecifier(projectDir, `${SDK_PACKAGE}/server`) ??
41
+ createRequire(path.join(projectDir, 'package.json')).resolve(`${SDK_PACKAGE}/server`);
37
42
  const authorEntry = path.resolve(projectDir, config.entry);
38
43
  const entryPlugin = {
39
44
  name: 'castle-server-entry',
@@ -52,7 +57,7 @@ export async function bundleServerModule(projectDir, config, minify) {
52
57
  root: projectDir,
53
58
  configFile: false,
54
59
  logLevel: 'silent',
55
- plugins: [forksOverlayPlugin(), importsAliasPlugin(), entryPlugin],
60
+ plugins: [forksOverlayPlugin(), importsAliasPlugin(), sdkAliasPlugin(), entryPlugin],
56
61
  ssr: { noExternal: true },
57
62
  build: {
58
63
  write: false,