castle-web-cli 0.4.193 → 0.4.195

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 (101) hide show
  1. package/dist/agent-prompts.d.ts +1 -0
  2. package/dist/agent-prompts.js +14 -5
  3. package/dist/agent.js +101 -4
  4. package/dist/api.d.ts +1 -0
  5. package/dist/api.js +5 -0
  6. package/dist/bundle.js +2 -1
  7. package/dist/castleJson.d.ts +1 -2
  8. package/dist/castleJson.js +0 -39
  9. package/dist/check.js +14 -6
  10. package/dist/devSessionFrameBudget.d.ts +14 -0
  11. package/dist/devSessionFrameBudget.js +64 -0
  12. package/dist/devSessionServer.d.ts +6 -0
  13. package/dist/devSessionServer.js +38 -1
  14. package/dist/editorConfig.d.ts +1 -1
  15. package/dist/editorConfig.js +12 -4
  16. package/dist/filesChanged.js +7 -3
  17. package/dist/ide.js +28 -59
  18. package/dist/importUpdateHook.d.ts +31 -0
  19. package/dist/importUpdateHook.js +135 -0
  20. package/dist/importUpdateHookRunner.d.ts +2 -0
  21. package/dist/importUpdateHookRunner.js +24 -0
  22. package/dist/imports.js +58 -10
  23. package/dist/index.js +58 -7
  24. package/dist/init.d.ts +1 -0
  25. package/dist/init.js +15 -9
  26. package/dist/multiplayerDoc.d.ts +1 -0
  27. package/dist/multiplayerDoc.js +228 -0
  28. package/dist/native/loop.js +29 -9
  29. package/dist/platformDoc.d.ts +1 -0
  30. package/dist/platformDoc.js +25 -6
  31. package/dist/remix.js +4 -1
  32. package/dist/scriptErrors.d.ts +23 -0
  33. package/dist/scriptErrors.js +147 -0
  34. package/dist/serve.js +366 -36
  35. package/dist/serverBundle.js +1 -1
  36. package/dist/shell/assets/index-kMH1qj6D.js +449 -0
  37. package/dist/shell/index.html +1 -1
  38. package/dist/versions.d.ts +14 -0
  39. package/dist/versions.js +21 -0
  40. package/dist/viteClientStub.d.ts +18 -0
  41. package/dist/viteClientStub.js +193 -0
  42. package/dist/vitePlugins.d.ts +1 -0
  43. package/dist/vitePlugins.js +18 -0
  44. package/kits/base/CLAUDE.md +3 -3
  45. package/kits/base/castle.json +1 -3
  46. package/kits/base/sdk/README.md +17 -0
  47. package/kits/multiplayer-2d/CLAUDE.md +9 -2
  48. package/kits/multiplayer-2d/blueprints/crate.scene +32 -4
  49. package/kits/multiplayer-2d/blueprints/other-player.scene +31 -4
  50. package/kits/multiplayer-2d/blueprints/player.scene +38 -6
  51. package/kits/multiplayer-2d/blueprints/wall.scene +25 -3
  52. package/kits/multiplayer-2d/castle.json +3 -12
  53. package/kits/multiplayer-2d/test/two-client-test.mjs +4 -4
  54. package/kits/multiplayer-3d/CLAUDE.md +5 -2
  55. package/kits/multiplayer-3d/castle.json +3 -11
  56. package/kits/multiplayer-3d/scenes/main.scene +128 -0
  57. package/kits/physics-2d/CLAUDE.md +16 -11
  58. package/kits/physics-2d/behaviors/Camera.jsx +5 -6
  59. package/kits/physics-2d/behaviors/Goal.jsx +5 -2
  60. package/kits/physics-2d/behaviors/Joints.jsx +4 -3
  61. package/kits/physics-2d/behaviors/Layout.jsx +120 -3
  62. package/kits/physics-2d/behaviors/Sprite.jsx +9 -9
  63. package/kits/physics-2d/behaviors/Style.jsx +22 -15
  64. package/kits/physics-2d/behaviors/Text.jsx +7 -6
  65. package/kits/physics-2d/blueprints/ball.scene +27 -5
  66. package/kits/physics-2d/blueprints/block.scene +16 -3
  67. package/kits/physics-2d/blueprints/cauldron.scene +3 -1
  68. package/kits/physics-2d/blueprints/crate.scene +23 -4
  69. package/kits/physics-2d/blueprints/goal.scene +12 -2
  70. package/kits/physics-2d/blueprints/text.scene +13 -3
  71. package/kits/physics-2d/castle.json +2 -11
  72. package/kits/physics-2d/editors/SceneEditor.jsx +68 -5
  73. package/kits/physics-2d/editors/SelectionOverlay.jsx +27 -20
  74. package/kits/physics-2d/editors/behaviorRegistry.js +8 -2
  75. package/kits/physics-2d/engine/ErrorOverlay.jsx +69 -0
  76. package/kits/physics-2d/engine/ScenePlayer.jsx +2 -67
  77. package/kits/physics-2d/engine/blueprint.js +111 -54
  78. package/kits/physics-2d/engine/blueprintCore.js +59 -0
  79. package/kits/physics-2d/engine/collider.js +28 -17
  80. package/kits/physics-2d/engine/layout.js +61 -0
  81. package/kits/physics-2d/engine/layoutOriginMigration.js +223 -0
  82. package/kits/physics-2d/engine/liveReload.js +5 -4
  83. package/kits/physics-2d/engine/physics/PhysicsSystem.js +11 -7
  84. package/kits/physics-2d/engine/physics/controls.js +4 -8
  85. package/kits/physics-2d/engine/physics/matterBridge.js +13 -8
  86. package/kits/physics-2d/engine/scene.js +38 -34
  87. package/kits/physics-2d/engine/spriteGeometry.js +17 -9
  88. package/kits/physics-2d/engine/tap.js +4 -3
  89. package/kits/physics-2d/scenes/main.scene +59 -6
  90. package/kits/physics-2d/scenes/sandbox.scene +69 -7
  91. package/kits/physics-2d/scripts/onUpdate.mjs +199 -0
  92. package/kits/physics-3d/castle.json +0 -11
  93. package/kits/real-time/castle.json +2 -8
  94. package/kits/real-time/code/server/persist.js +14 -0
  95. package/kits/turn-based/CLAUDE.md +18 -10
  96. package/kits/turn-based/blueprints/board.scene +3 -1
  97. package/kits/turn-based/blueprints/square.scene +3 -1
  98. package/kits/turn-based/castle.json +3 -14
  99. package/kits/turn-based/code/behaviors/Board.jsx +33 -21
  100. package/package.json +1 -1
  101. package/dist/shell/assets/index-ColHKJYj.js +0 -449
@@ -0,0 +1,147 @@
1
+ // Every failed module transform, remembered and announced.
2
+ //
3
+ // A syntax error in ANY module a deck's entry imports statically (a behavior in
4
+ // a kit's eager `import.meta.glob`, say) makes Vite answer that module request
5
+ // with a 500. The deck's entry then never evaluates, so nothing the deck itself
6
+ // installs -- no React error boundary, no console interception, no error
7
+ // overlay -- is there to report it. The editor's scene view goes blank and the
8
+ // play frame goes black with the only trace in the raw browser console.
9
+ //
10
+ // So the SERVE remembers instead, since it is the one place that always knows:
11
+ //
12
+ // - the error is observed from a connect ERROR middleware registered in the
13
+ // `configureServer` post hook. Vite's own `errorMiddleware` is added after
14
+ // every post hook runs, so ours sits between the transform middleware and
15
+ // Vite's: `next(err)` from a failed transform reaches us first, with the
16
+ // real error object (`id`, and `loc`/`frame` when the compiler set them --
17
+ // the fields Vite's own `prepareError` reads), and we hand it straight on
18
+ // so Vite's 500 response is unchanged. Verified against the oxc transform
19
+ // this Vite ships with, which sets `id` but leaves `loc` and `frame` empty
20
+ // and puts the position inside its formatted message instead.
21
+ // - this is deliberately NOT `server.hot.send` interception: the serve runs
22
+ // with `server.hmr: false`, so the client hot channel is a no-op and the
23
+ // `{ type: 'error' }` frame Vite sends there goes nowhere. It is also not a
24
+ // plugin `transform` wrapper, which would only see errors thrown by plugins
25
+ // that run after ours rather than "this module request failed".
26
+ // - success is observed from a `enforce: 'post'` `transform` hook, which only
27
+ // runs for a module whose whole transform chain worked.
28
+ //
29
+ // Consumers: the shell (a banner over the edit panel) and any deck frame,
30
+ // through the same `/__castle/ws` broadcast the rest of the serve uses, plus
31
+ // `GET /__castle/script-errors` for a frame that failed to boot and so has no
32
+ // socket of its own.
33
+ import * as path from 'path';
34
+ import { stripVTControlCharacters } from 'util';
35
+ // Only module requests. A 500 from anything else (an asset, a proxied route) is
36
+ // not a script that failed to compile, and claiming it would put a redbox over
37
+ // a deck that runs fine.
38
+ const SCRIPT_EXTS = ['.js', '.jsx', '.mjs', '.cjs', '.ts', '.tsx', '.mts', '.cts'];
39
+ // Big frames are source excerpts, not stacks -- a few lines. Cap anyway: this
40
+ // rides a WebSocket broadcast and an error in a generated file can be huge.
41
+ const FRAME_MAX = 2000;
42
+ // `…/behaviors/Ball.jsx:12:4` inside a compiler diagnostic. Vite's own
43
+ // `prepareError` reads `err.loc`, but the oxc transform this Vite ships with
44
+ // puts the position only in its formatted message, so the position has to be
45
+ // read back out of it when `loc` is absent.
46
+ const POSITION_RE = /([^\s[\]()]+\.(?:[cm]?[jt]sx?)):(\d+):(\d+)/;
47
+ // One line to put in a banner, out of a diagnostic that may be a whole framed
48
+ // excerpt. The oxc form is "Transform failed with N errors:" followed by
49
+ // "[PARSE_ERROR] Error: <the actual problem>"; esbuild and plugin errors are
50
+ // already a single line.
51
+ function summarize(text) {
52
+ const lines = text
53
+ .split('\n')
54
+ .map((line) => line.trim())
55
+ .filter((line) => line.length > 0);
56
+ const detail = lines.find((line) => /error/i.test(line) && !/^transform failed/i.test(line)) ?? lines[0];
57
+ return (detail ?? 'Failed to compile')
58
+ .replace(/^\[[A-Z_]+\]\s*/, '')
59
+ .replace(/^Error:\s*/, '')
60
+ .trim();
61
+ }
62
+ // `<root>/behaviors/Ball.jsx?t=1` -> `behaviors/Ball.jsx`. A file outside the
63
+ // deck (an import's node_modules, a linked SDK) keeps its absolute path: it is
64
+ // still the honest answer to "which file broke", and no consumer has to guess.
65
+ function deckRelative(projectDir, id) {
66
+ const clean = id.split('?')[0].split('#')[0];
67
+ // Virtual and internal ids (`\0`-prefixed, `/@vite/...`, `virtual:...`) name
68
+ // no file anyone can open, so there is nothing useful to report.
69
+ if (!clean || clean.includes('\0') || clean.startsWith('/@') || clean.includes(':\0'))
70
+ return null;
71
+ if (!SCRIPT_EXTS.some((ext) => clean.endsWith(ext)))
72
+ return null;
73
+ const rel = path.relative(projectDir, clean);
74
+ if (!rel || rel.startsWith('..') || path.isAbsolute(rel))
75
+ return clean;
76
+ return rel.split(path.sep).join('/');
77
+ }
78
+ export function createScriptErrors(projectDir, broadcast) {
79
+ const errors = new Map();
80
+ const snapshot = () => {
81
+ const out = {};
82
+ for (const [file, entry] of errors)
83
+ out[file] = entry;
84
+ return out;
85
+ };
86
+ return {
87
+ record(error) {
88
+ const err = (error ?? {});
89
+ const text = stripVTControlCharacters(typeof err.message === 'string' ? err.message : String(error));
90
+ const position = POSITION_RE.exec(text);
91
+ const id = err.id ?? err.loc?.file ?? '';
92
+ const file = id ? deckRelative(projectDir, id) : null;
93
+ if (!file)
94
+ return null;
95
+ // The whole diagnostic is the frame when the compiler did not hand one
96
+ // over separately: for oxc that text IS the framed source excerpt, which
97
+ // is the most useful thing a redbox can show.
98
+ const frame = typeof err.frame === 'string' && err.frame ? err.frame : text;
99
+ const entry = {
100
+ path: file,
101
+ message: summarize(text),
102
+ line: err.loc?.line ?? (position ? Number(position[2]) : undefined),
103
+ column: err.loc?.column ?? (position ? Number(position[3]) : undefined),
104
+ frame: frame.slice(0, FRAME_MAX),
105
+ };
106
+ errors.set(file, entry);
107
+ broadcast({ type: 'script_error', ...entry });
108
+ return entry;
109
+ },
110
+ clearId(id) {
111
+ const file = deckRelative(projectDir, id);
112
+ if (!file || !errors.has(file))
113
+ return;
114
+ errors.delete(file);
115
+ broadcast({ type: 'script_ok', path: file });
116
+ },
117
+ snapshot,
118
+ respond(res) {
119
+ res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' });
120
+ res.end(JSON.stringify({ errors: snapshot() }));
121
+ },
122
+ };
123
+ }
124
+ export function scriptErrorsPlugin(errors) {
125
+ return {
126
+ name: 'castle-script-errors',
127
+ // `post` so this transform only runs once every other plugin's has, which
128
+ // is exactly "this module compiled".
129
+ enforce: 'post',
130
+ transform(_code, id) {
131
+ errors.clearId(id);
132
+ return null;
133
+ },
134
+ configureServer(server) {
135
+ // Returning a function defers to the POST hook phase, which runs after
136
+ // Vite installed its transform middleware and before it installs its own
137
+ // error middleware -- so this handler is the first to see a transform
138
+ // error, and passing it on leaves Vite's 500 page untouched.
139
+ return () => {
140
+ server.middlewares.use((err, _req, _res, next) => {
141
+ errors.record(err);
142
+ next(err);
143
+ });
144
+ };
145
+ },
146
+ };
147
+ }