castle-web-cli 0.4.197 → 0.4.199

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 (62) hide show
  1. package/dist/agent-prompts.d.ts +1 -0
  2. package/dist/agent-prompts.js +9 -3
  3. package/dist/agent.js +25 -1
  4. package/dist/api.d.ts +1 -1
  5. package/dist/api.js +14 -3
  6. package/dist/byo-auth.js +4 -5
  7. package/dist/castle-host/host.js +215 -0
  8. package/dist/devSessionServer.js +40 -3
  9. package/dist/devSessionStorage.js +155 -0
  10. package/dist/filesChanged.d.ts +23 -0
  11. package/dist/filesChanged.js +15 -9
  12. package/dist/ide.d.ts +3 -0
  13. package/dist/ide.js +37 -25
  14. package/dist/init.js +1 -1
  15. package/dist/paramsFile.d.ts +80 -0
  16. package/dist/paramsFile.js +516 -0
  17. package/dist/paramsRegisterPlugin.d.ts +3 -0
  18. package/dist/paramsRegisterPlugin.js +81 -0
  19. package/dist/paramsTunes.d.ts +44 -0
  20. package/dist/paramsTunes.js +200 -0
  21. package/dist/paramsWrites.d.ts +30 -0
  22. package/dist/paramsWrites.js +138 -0
  23. package/dist/platformDoc.d.ts +1 -1
  24. package/dist/platformDoc.js +6 -20
  25. package/dist/save-deck.js +9 -1
  26. package/dist/serve.js +23 -15
  27. package/dist/shell/assets/index-DJSLmPmw.js +449 -0
  28. package/dist/shell/assets/index-uXcYGwQC.css +1 -0
  29. package/dist/shell/index.html +2 -2
  30. package/dist/textHash.d.ts +2 -0
  31. package/dist/textHash.js +28 -0
  32. package/kits/base/castle.json +1 -1
  33. package/kits/base/package-lock.json +1 -1
  34. package/kits/base/sdk/README.md +4 -0
  35. package/kits/base/sdk/castle.d.ts +4 -0
  36. package/kits/base/sdk/castle.js +2 -0
  37. package/kits/base/sdk/characters.d.ts +26 -0
  38. package/kits/base/sdk/characters.js +108 -0
  39. package/kits/base/sdk/commands.d.ts +70 -0
  40. package/kits/base/sdk/multiplayer.js +68 -0
  41. package/kits/base/sdk/paramsRegistry.d.ts +32 -0
  42. package/kits/base/sdk/paramsRegistry.js +100 -0
  43. package/kits/base/sdk/runtime.d.ts +7 -0
  44. package/kits/base/sdk/runtime.js +34 -0
  45. package/kits/base/sdk/server/storage.d.ts +77 -0
  46. package/kits/base/sdk/server/storage.js +272 -18
  47. package/kits/base/sdk/server/wrapper.d.ts +1 -1
  48. package/kits/physics-2d/behaviors/Collider.jsx +3 -3
  49. package/kits/physics-2d/behaviors/Joints.jsx +2 -2
  50. package/kits/physics-2d/castle.json +2 -2
  51. package/kits/physics-2d/editors/SceneEditor.jsx +5 -5
  52. package/kits/physics-2d/editors/pxArtTimeline.jsx +1 -1
  53. package/kits/physics-2d/editors/pxArtTimeline.module.css +73 -73
  54. package/kits/physics-2d/editors/themeEditor.module.css +17 -17
  55. package/kits/physics-2d/engine/castle-tokens.css +64 -0
  56. package/kits/physics-2d/engine/fields/fields.module.css +43 -37
  57. package/kits/physics-2d/engine/ui.jsx +1 -0
  58. package/kits/physics-2d/engine/ui.module.css +262 -288
  59. package/kits/physics-2d/package-lock.json +6 -1
  60. package/package.json +1 -1
  61. package/dist/shell/assets/index-MSHtKPXt.css +0 -1
  62. package/dist/shell/assets/index-yHGXRGlX.js +0 -449
@@ -1,3 +1,4 @@
1
+ import type { TunedSlot } from './paramsWrites.js';
1
2
  import type { ViteDevServer } from 'vite';
2
3
  export interface FileChangeEntry {
3
4
  /** Deck-relative POSIX path. */
@@ -10,6 +11,13 @@ export interface FileChangeEntry {
10
11
  * state has no other way to know.
11
12
  */
12
13
  forkChanged?: boolean;
14
+ /**
15
+ * Set when everything that reached this file since the last broadcast was a
16
+ * change of `PARAMS` values, with the new values. A consumer holding the
17
+ * running deck can assign these instead of reloading it; absent means the
18
+ * broadcast says nothing about what changed, which is most of them.
19
+ */
20
+ tuned?: TunedSlot[];
13
21
  /**
14
22
  * Deck-relative paths of modules affected via the Vite import graph: the
15
23
  * changed file itself (when it's part of the module graph) plus every
@@ -19,6 +27,14 @@ export interface FileChangeEntry {
19
27
  }
20
28
  export interface FilesChangedMessage {
21
29
  type: 'files_changed';
30
+ /**
31
+ * Which broadcast this is, counting from 1 for the life of the serve. A
32
+ * consumer that asks the deck to confirm something about a broadcast has to
33
+ * be able to tell the answer apart from the answer to the one before it: the
34
+ * path alone repeats, and a late reply about an older write would otherwise
35
+ * be read as a reply about this one.
36
+ */
37
+ seq: number;
22
38
  changes: FileChangeEntry[];
23
39
  /** Union of every change's path + affected set, deduped. */
24
40
  affected: string[];
@@ -34,4 +50,11 @@ export declare function installFilesChangedWatcher(opts: {
34
50
  invalidate: (paths?: string[]) => void;
35
51
  /** Records what was broadcast, so we can see what fires and when. */
36
52
  log?: (line: string) => void;
53
+ /**
54
+ * What this path's writes tuned since the last broadcast, or null. Asked at
55
+ * flush rather than at event time because coalescing is exactly what decides
56
+ * the answer: one of the merged writes being a code save makes the whole
57
+ * entry an ordinary change.
58
+ */
59
+ claimTunes?: (relPath: string, event: 'add' | 'change' | 'delete') => TunedSlot[] | null;
37
60
  }): void;
@@ -16,8 +16,9 @@ const IGNORED_SEGMENTS = new Set(['node_modules', '.castle', '.git']);
16
16
  const DEBOUNCE_MS = 100;
17
17
  const MAX_WAIT_MS = 500;
18
18
  export function installFilesChangedWatcher(opts) {
19
- const { vite, projectDir, broadcast, invalidate, log } = opts;
19
+ const { vite, projectDir, broadcast, invalidate, log, claimTunes } = opts;
20
20
  const pending = new Map();
21
+ let seq = 0;
21
22
  let debounceTimer = null;
22
23
  let maxTimer = null;
23
24
  const flush = () => {
@@ -30,12 +31,16 @@ export function installFilesChangedWatcher(opts) {
30
31
  if (pending.size === 0)
31
32
  return;
32
33
  const changes = [...pending.entries()]
33
- .map(([relPath, change]) => ({
34
- path: relPath,
35
- event: change.event,
36
- ...(change.forkChanged ? { forkChanged: true } : {}),
37
- affected: [...change.affected].sort(),
38
- }))
34
+ .map(([relPath, change]) => {
35
+ const tuned = claimTunes?.(relPath, change.event) ?? null;
36
+ return {
37
+ path: relPath,
38
+ event: change.event,
39
+ ...(change.forkChanged ? { forkChanged: true } : {}),
40
+ ...(tuned ? { tuned } : {}),
41
+ affected: [...change.affected].sort(),
42
+ };
43
+ })
39
44
  .sort((a, b) => a.path.localeCompare(b.path));
40
45
  pending.clear();
41
46
  // New/deleted files can be picked up by `import.meta.glob` importers whose
@@ -59,12 +64,13 @@ export function installFilesChangedWatcher(opts) {
59
64
  const affectedList = [...affected].sort();
60
65
  const loggedChanges = changes
61
66
  .slice(0, 5)
62
- .map((c) => `${c.event} ${c.path}${c.affected.length ? ` (+${c.affected.length})` : ''}`);
67
+ .map((c) => `${c.event} ${c.path}${c.affected.length ? ` (+${c.affected.length})` : ''}${c.tuned ? ' tuned' : ''}`);
63
68
  if (changes.length > loggedChanges.length) {
64
69
  loggedChanges.push(`and ${changes.length - loggedChanges.length} more`);
65
70
  }
66
71
  log?.(`[watch] ${loggedChanges.join(', ')} | affected ${affectedList.length}\n`);
67
- broadcast({ type: 'files_changed', changes, affected: affectedList });
72
+ seq += 1;
73
+ broadcast({ type: 'files_changed', seq, changes, affected: affectedList });
68
74
  };
69
75
  const schedule = () => {
70
76
  if (debounceTimer)
package/dist/ide.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import * as http from 'http';
2
2
  import { Duplex } from 'stream';
3
3
  import { COVER_FILE } from './localPaths.js';
4
+ import { type TuneLedger } from './paramsTunes.js';
4
5
  /** Cache only Vite's content-fingerprinted executable, style, and font output. */
5
6
  export declare function shellAssetCacheControl(asset: string): string;
6
7
  export declare const IDE_ASSET_PREFIX = "/__castle/ide/";
@@ -40,4 +41,6 @@ export declare function createIdeServer(opts: {
40
41
  invalidate?: (paths?: string[]) => void;
41
42
  shellBootstrap?: (req: http.IncomingMessage) => string;
42
43
  authorizeUpgrade?: (req: http.IncomingMessage, endpoint: 'pty') => boolean;
44
+ /** Records which writes were only a tune, for the files_changed broadcast. */
45
+ tuneLedger?: TuneLedger;
43
46
  }): IdeServer;
package/dist/ide.js CHANGED
@@ -17,13 +17,16 @@ import headlessPkg from '@xterm/headless';
17
17
  import { SerializeAddon } from '@xterm/addon-serialize';
18
18
  import { WebSocketServer } from 'ws';
19
19
  import { rawDataToString } from './rawData.js';
20
- import { IMPORTS_DIR, IMPORT_VERSION_FILE, importStatuses, updateImport, } from './imports.js';
20
+ import { IMPORTS_DIR, IMPORT_VERSION_FILE, importStatuses, updateImport } from './imports.js';
21
21
  import { FORKS_DIR, forkImportFile, forkStateOf, listOwnForks, ownForkFile, resolveForkedFile, revertImportFile, } from './forks.js';
22
22
  import { deckHidesFiles, readEditorConfig, resolveFileTypes, } from './editorConfig.js';
23
23
  import { UNSUPPORTED_MEDIA } from './unsupportedMedia.js';
24
24
  import { IMPORT_API_PREFIX, handleImportApi } from './importBrowse.js';
25
25
  import { readRequestBody, sendJson } from './httpJson.js';
26
26
  import { COVER_FILE } from './localPaths.js';
27
+ import { recordParamsWrite } from './paramsWrites.js';
28
+ import { applyDeckWrite } from './paramsTunes.js';
29
+ import { PARAMS_DECLARATION, PARAMS_EXTS } from './paramsFile.js';
27
30
  import { applyVersionRestore, createVersion, NotOnThisLine, pendingChanges, UnsavedChanges, versionSummaries, } from './versions.js';
28
31
  import { envForUserShell, installCliShims } from './byo-auth.js';
29
32
  const HeadlessTerminal = headlessPkg.Terminal;
@@ -53,9 +56,7 @@ const SHELL_MIME = {
53
56
  const IMMUTABLE_SHELL_ASSET = /^assets\/[^/\\]+-[A-Za-z0-9_-]{8}\.(?:css|js|woff2?|ttf)$/;
54
57
  /** Cache only Vite's content-fingerprinted executable, style, and font output. */
55
58
  export function shellAssetCacheControl(asset) {
56
- return IMMUTABLE_SHELL_ASSET.test(asset)
57
- ? 'public, max-age=31536000, immutable'
58
- : 'no-store';
59
+ return IMMUTABLE_SHELL_ASSET.test(asset) ? 'public, max-age=31536000, immutable' : 'no-store';
59
60
  }
60
61
  // Does the deck serve this root-level file itself? Vite serves both the deck
61
62
  // root and its `public/` dir at `/`, so either location counts.
@@ -378,15 +379,6 @@ function curatedDeckFiles(deckDir) {
378
379
  ...filterImportedFiles(deckDir, listed.filter(isImportPath)),
379
380
  ].sort((a, b) => a.localeCompare(b));
380
381
  }
381
- // The source extensions a `PARAMS` object can be read out of -- the same ones
382
- // the shell parses as JS/TS. Not a kit's vocabulary: `export const PARAMS` is a
383
- // JavaScript declaration, not a file type somebody declared.
384
- const PARAMS_EXTS = new Set(['.js', '.jsx', '.ts', '.tsx']);
385
- // Deliberately looser than the shell's parser, which also accepts a `const
386
- // PARAMS` that is never exported. A prefilter that were TIGHTER than the parser
387
- // would drop files the panel can actually edit; the shell re-parses each
388
- // candidate and discards the ones that only look like a match.
389
- const PARAMS_DECLARATION = /\bconst\s+PARAMS\b/;
390
382
  // Which of the deck's files declare params. Scanning here rather than in the
391
383
  // browser because a physics-2d deck carries a hundred-plus files under
392
384
  // `imports/`, and reading each one over HTTP to open a panel is the wrong cost.
@@ -526,19 +518,31 @@ function handleVersionRestore(deckDir, req, res, restart) {
526
518
  }
527
519
  });
528
520
  }
529
- function handleFilesWrite(deckDir, req, res) {
521
+ // `ifMatch` is optional and unchanged writes behave as they always did: a
522
+ // client that names the text it edited gets a 409 instead of silently writing
523
+ // over whatever landed in between, and the Parameters panel is the one that
524
+ // needs it (it splices against its last read and ignores external changes while
525
+ // its own write is in flight).
526
+ function handleFilesWrite(deckDir, req, res, ledger) {
530
527
  withMutationPath(deckDir, req, res, (target, body) => {
531
528
  if (typeof body.contents !== 'string') {
532
529
  return sendJson(res, 400, { error: 'File contents must be a string.' });
533
530
  }
534
- try {
535
- fs.mkdirSync(path.dirname(target.abs), { recursive: true });
536
- fs.writeFileSync(target.abs, body.contents, 'utf8');
537
- sendJson(res, 200, { ok: true, path: target.rel });
538
- }
539
- catch (err) {
540
- sendFailure(res, 'write', target.rel, err);
531
+ const written = applyDeckWrite({
532
+ abs: target.abs,
533
+ rel: target.rel,
534
+ contents: body.contents,
535
+ ifMatch: typeof body.ifMatch === 'string' ? body.ifMatch : undefined,
536
+ ledger,
537
+ });
538
+ if (!written.ok) {
539
+ return sendJson(res, written.conflict ? 409 : 500, {
540
+ error: written.error,
541
+ ...(written.conflict ? { conflict: true } : {}),
542
+ });
541
543
  }
544
+ recordParamsWrite(deckDir, target.rel, body.tuned);
545
+ sendJson(res, 200, { ok: true, path: target.rel });
542
546
  });
543
547
  }
544
548
  function handleFilesMkdir(deckDir, req, res) {
@@ -759,10 +763,16 @@ function handleFilesDelete(deckDir, req, res) {
759
763
  }
760
764
  });
761
765
  }
766
+ // The Play panel's Snap button. Off for everyone until the serve is told
767
+ // otherwise: castle-www sets this on the spawn for the users a server-side gate
768
+ // has opened it for, the same rollout shape as `planDefault` in agent.ts.
769
+ function playSnapEnabled() {
770
+ return process.env.CASTLE_PLAY_SNAP === 'on';
771
+ }
762
772
  // The builtin Files + code-editor backend: list / read / write deck files and
763
773
  // report kit-owned editor extensions. Paths are deck-relative; resolveDeckPath
764
774
  // rejects traversal and protected dirs.
765
- function handleFilesApi(deckDir, req, res, reqPath, restart, invalidate) {
775
+ function handleFilesApi(deckDir, req, res, reqPath, restart, invalidate, tuneLedger) {
766
776
  const action = reqPath.slice(FILES_API_PREFIX.length);
767
777
  if (action === 'info') {
768
778
  const config = readEditorConfig(deckDir);
@@ -781,6 +791,7 @@ function handleFilesApi(deckDir, req, res, reqPath, restart, invalidate) {
781
791
  // that hides nothing has nothing to reveal, and the globs themselves are
782
792
  // the serve's business, not the shell's.
783
793
  hidesFiles: deckHidesFiles(deckDir),
794
+ playSnap: playSnapEnabled(),
784
795
  // Formats the platform knows won't survive publishing. Sent alongside the
785
796
  // deck's own types rather than merged into them: these are not things the
786
797
  // deck understands, they are things this CLI refuses to let it ship.
@@ -928,7 +939,7 @@ function handleFilesApi(deckDir, req, res, reqPath, restart, invalidate) {
928
939
  return true;
929
940
  }
930
941
  if (action === 'write') {
931
- handleFilesWrite(deckDir, req, res);
942
+ handleFilesWrite(deckDir, req, res, tuneLedger);
932
943
  return true;
933
944
  }
934
945
  if (action === 'rename') {
@@ -1153,7 +1164,8 @@ export function createIdeServer(opts) {
1153
1164
  // The PTY is a root login shell — reject any upgrade that isn't same-origin (a
1154
1165
  // co-tenant reaching this port directly sends no browser Origin). Consumed either
1155
1166
  // way so a rejected upgrade never falls through to another handler.
1156
- if (!isSameOriginUpgrade(req) || (opts.authorizeUpgrade && !opts.authorizeUpgrade(req, 'pty'))) {
1167
+ if (!isSameOriginUpgrade(req) ||
1168
+ (opts.authorizeUpgrade && !opts.authorizeUpgrade(req, 'pty'))) {
1157
1169
  socket.write('HTTP/1.1 403 Forbidden\r\n\r\n');
1158
1170
  socket.destroy();
1159
1171
  return true;
@@ -1181,7 +1193,7 @@ export function createIdeServer(opts) {
1181
1193
  return handleCoverApi(deckDir, req, res);
1182
1194
  }
1183
1195
  if (reqPath.startsWith(FILES_API_PREFIX)) {
1184
- return handleFilesApi(deckDir, req, res, reqPath, opts.restart, opts.invalidate);
1196
+ return handleFilesApi(deckDir, req, res, reqPath, opts.restart, opts.invalidate, opts.tuneLedger);
1185
1197
  }
1186
1198
  if (reqPath.startsWith(IMPORT_API_PREFIX)) {
1187
1199
  return handleImportApi(deckDir, req, res, reqPath, opts.restart);
package/dist/init.js CHANGED
@@ -56,7 +56,7 @@ if (!(await mountEditorRoute())) startGame(initCard());
56
56
  // Registry version of castle-web-sdk to inject when scaffolding from a
57
57
  // globally-installed castle-web (not from inside the workspace). Bumped
58
58
  // alongside cli/sdk version bumps.
59
- const PUBLISHED_SDK_VERSION = '0.4.28';
59
+ const PUBLISHED_SDK_VERSION = '0.4.30';
60
60
  // Range, not a pin: `castle-web-fonts` is its own package precisely so a tenth
61
61
  // face can ship without a cli release, and a caret range means an existing
62
62
  // deck's next install picks it up. Nothing here has to move when it does.
@@ -0,0 +1,80 @@
1
+ /** The name a file declares its tunable values under. */
2
+ export declare const PARAMS_NAME = "PARAMS";
3
+ /** The optional sibling object that says how to RENDER them, keyed the same. */
4
+ export declare const PARAMS_META_NAME = "PARAMS_META";
5
+ export declare const PARAMS_EXTS: Set<string>;
6
+ export declare const PARAMS_DECLARATION: RegExp;
7
+ export type ParamValue = number | string | boolean;
8
+ export type ParamKind = 'number' | 'string' | 'color' | 'boolean' | 'readonly';
9
+ /** One choice of an `options` list, normalized from `'a'` or `{ value, label }`. */
10
+ export interface ParamOption {
11
+ value: string | number;
12
+ label: string;
13
+ }
14
+ export interface ParamMeta {
15
+ min?: number;
16
+ max?: number;
17
+ step?: number;
18
+ unit?: string;
19
+ options?: ParamOption[];
20
+ /** A media kind for the file picker: `image`, `audio`, `video`, `any`. */
21
+ file?: string;
22
+ hint?: string;
23
+ }
24
+ export interface ParamSlot {
25
+ /** Stable identity across re-parses: `speed`, or `player.speed` in a group. */
26
+ id: string;
27
+ key: string;
28
+ label: string;
29
+ kind: ParamKind;
30
+ /** Absent for a `readonly` slot -- there is no value we can round-trip. */
31
+ value?: ParamValue;
32
+ /** The value's source text, shown as-is for a `readonly` slot. */
33
+ source: string;
34
+ /** Set when `source` is arithmetic that folded into `value` -- `1 / 3`. */
35
+ folded?: true;
36
+ /** Which quote the string was written with, so rewriting keeps the style. */
37
+ quote?: "'" | '"';
38
+ from: number;
39
+ to: number;
40
+ /** 1-based line the value is written on, for a link into the code editor. */
41
+ line: number;
42
+ /** From `PARAMS_META`, if it names this slot. Never written back. */
43
+ meta?: ParamMeta;
44
+ }
45
+ /** A nested plain object renders as a titled group; `null` is the top level. */
46
+ export interface ParamGroup {
47
+ title: string | null;
48
+ slots: ParamSlot[];
49
+ }
50
+ export interface ParamsDoc {
51
+ /** Whether a PARAMS declaration exists at all -- drives the empty state. */
52
+ found: boolean;
53
+ groups: ParamGroup[];
54
+ }
55
+ export declare const EMPTY_PARAMS: ParamsDoc;
56
+ export declare function humanizeKey(key: string): string;
57
+ export declare function isHexColor(value: unknown): boolean;
58
+ export declare function parseParams(text: string): ParamsDoc;
59
+ export declare function findSlot(doc: ParamsDoc, id: string): ParamSlot | null;
60
+ /** One slot a write says it tuned -- the panel's marker and the serve's, same shape. */
61
+ export interface TunedValue {
62
+ id: string;
63
+ before: ParamValue;
64
+ after: ParamValue;
65
+ }
66
+ /**
67
+ * The same tuned slots with every `before` read off `doc` instead of off
68
+ * whatever the write was first built on.
69
+ *
70
+ * A write re-sent over somebody else's version did not move the value it set
71
+ * out to move. The marker answers "what did the person change this from", the
72
+ * router reads it, and a number the file never held is worse there than no
73
+ * entry at all -- so a slot the doc no longer has is dropped rather than
74
+ * carried forward on a guess.
75
+ */
76
+ export declare function rebaseTuned(tuned: TunedValue[], doc: ParamsDoc): TunedValue[];
77
+ export declare function editParam(text: string, doc: ParamsDoc, id: string, value: ParamValue): {
78
+ text: string;
79
+ doc: ParamsDoc;
80
+ };