@playcraft/common 0.1.66 → 0.1.68

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 (174) hide show
  1. package/dist/agent-tools/replica-validation.d.ts +14 -14
  2. package/dist/agent-tools/video-model-proxy.d.ts +1 -1
  3. package/dist/agent-tools/video-replication.d.ts +4 -4
  4. package/dist/audio-library-stream.d.ts +65 -0
  5. package/dist/audio-library-stream.d.ts.map +1 -0
  6. package/dist/audio-library-stream.js +87 -0
  7. package/dist/audio-library-stream.js.map +1 -0
  8. package/dist/cloud-workspace/runtime/agent-command.d.ts +3 -3
  9. package/dist/cloud-workspace/runtime/agent-generation-provenance.js +1 -1
  10. package/dist/cloud-workspace/runtime/agent-generation-provenance.js.map +1 -1
  11. package/dist/cloud-workspace/runtime/agent-read.d.ts +6 -6
  12. package/dist/cloud-workspace/runtime/animation-editor.d.ts +27 -27
  13. package/dist/cloud-workspace/runtime/animation-quality.d.ts +1 -1
  14. package/dist/cloud-workspace/runtime/artifacts.d.ts +11 -11
  15. package/dist/cloud-workspace/runtime/asset-generation-bundle.d.ts +2 -2
  16. package/dist/cloud-workspace/runtime/asset-generation-tools.d.ts +58 -10
  17. package/dist/cloud-workspace/runtime/asset-generation-tools.d.ts.map +1 -1
  18. package/dist/cloud-workspace/runtime/asset-generation-tools.js +1 -1
  19. package/dist/cloud-workspace/runtime/asset-generation-tools.js.map +1 -1
  20. package/dist/cloud-workspace/runtime/asset-generation.d.ts +134 -37
  21. package/dist/cloud-workspace/runtime/asset-generation.d.ts.map +1 -1
  22. package/dist/cloud-workspace/runtime/asset-generation.js +24 -4
  23. package/dist/cloud-workspace/runtime/asset-generation.js.map +1 -1
  24. package/dist/cloud-workspace/runtime/asset-library-import.d.ts +31 -1
  25. package/dist/cloud-workspace/runtime/asset-library-import.d.ts.map +1 -1
  26. package/dist/cloud-workspace/runtime/asset-library-save.d.ts +12 -12
  27. package/dist/cloud-workspace/runtime/audio-adjustments.d.ts +13 -0
  28. package/dist/cloud-workspace/runtime/audio-adjustments.d.ts.map +1 -0
  29. package/dist/cloud-workspace/runtime/audio-adjustments.js +90 -0
  30. package/dist/cloud-workspace/runtime/audio-adjustments.js.map +1 -0
  31. package/dist/cloud-workspace/runtime/audio-asset-protocol.d.ts +664 -0
  32. package/dist/cloud-workspace/runtime/audio-asset-protocol.d.ts.map +1 -0
  33. package/dist/cloud-workspace/runtime/audio-asset-protocol.js +98 -0
  34. package/dist/cloud-workspace/runtime/audio-asset-protocol.js.map +1 -0
  35. package/dist/cloud-workspace/runtime/audio-delivery.d.ts +75 -0
  36. package/dist/cloud-workspace/runtime/audio-delivery.d.ts.map +1 -0
  37. package/dist/cloud-workspace/runtime/audio-delivery.js +122 -0
  38. package/dist/cloud-workspace/runtime/audio-delivery.js.map +1 -0
  39. package/dist/cloud-workspace/runtime/audio-document.d.ts +127 -0
  40. package/dist/cloud-workspace/runtime/audio-document.d.ts.map +1 -0
  41. package/dist/cloud-workspace/runtime/audio-document.js +293 -0
  42. package/dist/cloud-workspace/runtime/audio-document.js.map +1 -0
  43. package/dist/cloud-workspace/runtime/audio-dynamic-document.d.ts +68 -0
  44. package/dist/cloud-workspace/runtime/audio-dynamic-document.d.ts.map +1 -0
  45. package/dist/cloud-workspace/runtime/audio-dynamic-document.js +120 -0
  46. package/dist/cloud-workspace/runtime/audio-dynamic-document.js.map +1 -0
  47. package/dist/cloud-workspace/runtime/audio-export.d.ts +25 -0
  48. package/dist/cloud-workspace/runtime/audio-export.d.ts.map +1 -0
  49. package/dist/cloud-workspace/runtime/audio-export.js +21 -0
  50. package/dist/cloud-workspace/runtime/audio-export.js.map +1 -0
  51. package/dist/cloud-workspace/runtime/audio-hybrid-player.d.ts +22 -0
  52. package/dist/cloud-workspace/runtime/audio-hybrid-player.d.ts.map +1 -0
  53. package/dist/cloud-workspace/runtime/audio-hybrid-player.js +456 -0
  54. package/dist/cloud-workspace/runtime/audio-hybrid-player.js.map +1 -0
  55. package/dist/cloud-workspace/runtime/audio-library.d.ts +418 -0
  56. package/dist/cloud-workspace/runtime/audio-library.d.ts.map +1 -0
  57. package/dist/cloud-workspace/runtime/audio-library.js +155 -0
  58. package/dist/cloud-workspace/runtime/audio-library.js.map +1 -0
  59. package/dist/cloud-workspace/runtime/audio-player.d.ts +18 -0
  60. package/dist/cloud-workspace/runtime/audio-player.d.ts.map +1 -0
  61. package/dist/cloud-workspace/runtime/audio-player.js +98 -0
  62. package/dist/cloud-workspace/runtime/audio-player.js.map +1 -0
  63. package/dist/cloud-workspace/runtime/audio-prerender-player.d.ts +31 -0
  64. package/dist/cloud-workspace/runtime/audio-prerender-player.d.ts.map +1 -0
  65. package/dist/cloud-workspace/runtime/audio-prerender-player.js +178 -0
  66. package/dist/cloud-workspace/runtime/audio-prerender-player.js.map +1 -0
  67. package/dist/cloud-workspace/runtime/audio-synthesizer.d.ts +38 -0
  68. package/dist/cloud-workspace/runtime/audio-synthesizer.d.ts.map +1 -0
  69. package/dist/cloud-workspace/runtime/audio-synthesizer.js +342 -0
  70. package/dist/cloud-workspace/runtime/audio-synthesizer.js.map +1 -0
  71. package/dist/cloud-workspace/runtime/batch-runs.d.ts +2 -2
  72. package/dist/cloud-workspace/runtime/chat-realtime.d.ts +2 -2
  73. package/dist/cloud-workspace/runtime/composer-context.d.ts +122 -0
  74. package/dist/cloud-workspace/runtime/composer-context.d.ts.map +1 -1
  75. package/dist/cloud-workspace/runtime/composer-context.js +2 -0
  76. package/dist/cloud-workspace/runtime/composer-context.js.map +1 -1
  77. package/dist/cloud-workspace/runtime/delivery.d.ts +4 -4
  78. package/dist/cloud-workspace/runtime/events.d.ts +4 -4
  79. package/dist/cloud-workspace/runtime/execution-admission.d.ts +2 -2
  80. package/dist/cloud-workspace/runtime/execution-context.d.ts +112 -0
  81. package/dist/cloud-workspace/runtime/execution-context.d.ts.map +1 -0
  82. package/dist/cloud-workspace/runtime/execution-context.js +62 -0
  83. package/dist/cloud-workspace/runtime/execution-context.js.map +1 -0
  84. package/dist/cloud-workspace/runtime/index.d.ts +19 -0
  85. package/dist/cloud-workspace/runtime/index.d.ts.map +1 -1
  86. package/dist/cloud-workspace/runtime/index.js +19 -0
  87. package/dist/cloud-workspace/runtime/index.js.map +1 -1
  88. package/dist/cloud-workspace/runtime/level-editor-agent.d.ts +331 -0
  89. package/dist/cloud-workspace/runtime/level-editor-agent.d.ts.map +1 -0
  90. package/dist/cloud-workspace/runtime/level-editor-agent.js +172 -0
  91. package/dist/cloud-workspace/runtime/level-editor-agent.js.map +1 -0
  92. package/dist/cloud-workspace/runtime/level-editor-authoring.d.ts +125 -0
  93. package/dist/cloud-workspace/runtime/level-editor-authoring.d.ts.map +1 -0
  94. package/dist/cloud-workspace/runtime/level-editor-authoring.js +63 -0
  95. package/dist/cloud-workspace/runtime/level-editor-authoring.js.map +1 -0
  96. package/dist/cloud-workspace/runtime/level-editor-compiler.d.ts +260 -0
  97. package/dist/cloud-workspace/runtime/level-editor-compiler.d.ts.map +1 -0
  98. package/dist/cloud-workspace/runtime/level-editor-compiler.js +530 -0
  99. package/dist/cloud-workspace/runtime/level-editor-compiler.js.map +1 -0
  100. package/dist/cloud-workspace/runtime/level-editor-extension.d.ts +115 -0
  101. package/dist/cloud-workspace/runtime/level-editor-extension.d.ts.map +1 -0
  102. package/dist/cloud-workspace/runtime/level-editor-extension.js +107 -0
  103. package/dist/cloud-workspace/runtime/level-editor-extension.js.map +1 -0
  104. package/dist/cloud-workspace/runtime/level-editor-path.d.ts +12 -0
  105. package/dist/cloud-workspace/runtime/level-editor-path.d.ts.map +1 -0
  106. package/dist/cloud-workspace/runtime/level-editor-path.js +110 -0
  107. package/dist/cloud-workspace/runtime/level-editor-path.js.map +1 -0
  108. package/dist/cloud-workspace/runtime/level-editor-reducer.d.ts +30 -0
  109. package/dist/cloud-workspace/runtime/level-editor-reducer.d.ts.map +1 -0
  110. package/dist/cloud-workspace/runtime/level-editor-reducer.js +627 -0
  111. package/dist/cloud-workspace/runtime/level-editor-reducer.js.map +1 -0
  112. package/dist/cloud-workspace/runtime/level-editor-schema.d.ts +621 -0
  113. package/dist/cloud-workspace/runtime/level-editor-schema.d.ts.map +1 -0
  114. package/dist/cloud-workspace/runtime/level-editor-schema.js +409 -0
  115. package/dist/cloud-workspace/runtime/level-editor-schema.js.map +1 -0
  116. package/dist/cloud-workspace/runtime/level-editor-validation.d.ts +11 -0
  117. package/dist/cloud-workspace/runtime/level-editor-validation.d.ts.map +1 -0
  118. package/dist/cloud-workspace/runtime/level-editor-validation.js +128 -0
  119. package/dist/cloud-workspace/runtime/level-editor-validation.js.map +1 -0
  120. package/dist/cloud-workspace/runtime/level-editor.d.ts +7 -0
  121. package/dist/cloud-workspace/runtime/level-editor.d.ts.map +1 -0
  122. package/dist/cloud-workspace/runtime/level-editor.js +7 -0
  123. package/dist/cloud-workspace/runtime/level-editor.js.map +1 -0
  124. package/dist/cloud-workspace/runtime/library-attachments.d.ts +60 -0
  125. package/dist/cloud-workspace/runtime/library-attachments.d.ts.map +1 -1
  126. package/dist/cloud-workspace/runtime/library-attachments.js +4 -0
  127. package/dist/cloud-workspace/runtime/library-attachments.js.map +1 -1
  128. package/dist/cloud-workspace/runtime/model-build-inputs.d.ts +1 -1
  129. package/dist/cloud-workspace/runtime/model-production.d.ts +105 -87
  130. package/dist/cloud-workspace/runtime/model-production.d.ts.map +1 -1
  131. package/dist/cloud-workspace/runtime/model-quality.d.ts +79 -79
  132. package/dist/cloud-workspace/runtime/model-validation.d.ts +8 -8
  133. package/dist/cloud-workspace/runtime/operations.d.ts +417 -31
  134. package/dist/cloud-workspace/runtime/operations.d.ts.map +1 -1
  135. package/dist/cloud-workspace/runtime/operations.js +33 -6
  136. package/dist/cloud-workspace/runtime/operations.js.map +1 -1
  137. package/dist/cloud-workspace/runtime/parameter-agent.d.ts +4 -4
  138. package/dist/cloud-workspace/runtime/previews.d.ts +5 -5
  139. package/dist/cloud-workspace/runtime/score-document.d.ts +285 -0
  140. package/dist/cloud-workspace/runtime/score-document.d.ts.map +1 -0
  141. package/dist/cloud-workspace/runtime/score-document.js +114 -0
  142. package/dist/cloud-workspace/runtime/score-document.js.map +1 -0
  143. package/dist/cloud-workspace/runtime/score-editing.d.ts +39 -0
  144. package/dist/cloud-workspace/runtime/score-editing.d.ts.map +1 -0
  145. package/dist/cloud-workspace/runtime/score-editing.js +180 -0
  146. package/dist/cloud-workspace/runtime/score-editing.js.map +1 -0
  147. package/dist/cloud-workspace/runtime/score-merging.d.ts +4 -0
  148. package/dist/cloud-workspace/runtime/score-merging.d.ts.map +1 -0
  149. package/dist/cloud-workspace/runtime/score-merging.js +74 -0
  150. package/dist/cloud-workspace/runtime/score-merging.js.map +1 -0
  151. package/dist/cloud-workspace/runtime/score-midi.d.ts +7 -0
  152. package/dist/cloud-workspace/runtime/score-midi.d.ts.map +1 -0
  153. package/dist/cloud-workspace/runtime/score-midi.js +226 -0
  154. package/dist/cloud-workspace/runtime/score-midi.js.map +1 -0
  155. package/dist/cloud-workspace/runtime/score-synthesizer.d.ts +51 -0
  156. package/dist/cloud-workspace/runtime/score-synthesizer.d.ts.map +1 -0
  157. package/dist/cloud-workspace/runtime/score-synthesizer.js +388 -0
  158. package/dist/cloud-workspace/runtime/score-synthesizer.js.map +1 -0
  159. package/dist/cloud-workspace/runtime/skills.d.ts +2 -2
  160. package/dist/cloud-workspace/runtime/skills.d.ts.map +1 -1
  161. package/dist/cloud-workspace/runtime/workspace-notifications.d.ts +2 -2
  162. package/dist/cloud-workspace/runtime/workspace-ui.d.ts +10 -10
  163. package/dist/project-skills/local-snapshot.d.ts +5 -3
  164. package/dist/project-skills/local-snapshot.d.ts.map +1 -1
  165. package/dist/project-skills/local-snapshot.js +15 -4
  166. package/dist/project-skills/local-snapshot.js.map +1 -1
  167. package/dist/project-skills/protocol.d.ts +7 -3
  168. package/dist/project-skills/protocol.d.ts.map +1 -1
  169. package/dist/project-skills/protocol.js +19 -1
  170. package/dist/project-skills/protocol.js.map +1 -1
  171. package/dist/project-skills/types.d.ts +13 -0
  172. package/dist/project-skills/types.d.ts.map +1 -1
  173. package/dist/project-skills/types.js.map +1 -1
  174. package/package.json +5 -1
@@ -0,0 +1,627 @@
1
+ import { editLevelEditorPath, validateLevelEditorPath } from './level-editor-path.js';
2
+ import { z } from 'zod';
3
+ import { compileLevelEditorDefinition } from './level-editor-compiler.js';
4
+ import { isLevelEditorJson, LevelEditorError, LEVEL_EDITOR_TRANSACTION_MAX_BYTES, levelEditorCommandInvocationSchema, levelEditorCommandInputSchemas, } from './level-editor-schema.js';
5
+ import { levelEditorCanonicalJson, levelEditorSchemaAtPath, validateLevelEditorData, } from './level-editor-validation.js';
6
+ const own = (value, key) => !!value && typeof value === 'object' && Object.hasOwn(value, key);
7
+ const identity = z.union([z.string().min(1).max(128), z.number().finite()]);
8
+ function members(value, idField) {
9
+ if (!Array.isArray(value))
10
+ throw new LevelEditorError('level_editor_collection_not_array');
11
+ const ids = new Set();
12
+ for (const item of value) {
13
+ if (!item ||
14
+ typeof item !== 'object' ||
15
+ Array.isArray(item) ||
16
+ !identity.safeParse(item[idField]).success)
17
+ throw new LevelEditorError('level_editor_invalid_item_identity');
18
+ const token = levelEditorCanonicalJson(item[idField]);
19
+ if (ids.has(token))
20
+ throw new LevelEditorError('level_editor_duplicate_item_identity');
21
+ ids.add(token);
22
+ }
23
+ return value;
24
+ }
25
+ function resolve(definition, documents, binding, selection) {
26
+ if (!Object.hasOwn(documents, binding.documentId) ||
27
+ !isLevelEditorJson(documents[binding.documentId]))
28
+ throw new LevelEditorError('level_editor_document_missing');
29
+ let current = documents[binding.documentId];
30
+ const path = [];
31
+ for (const segment of binding.segments) {
32
+ if ('field' in segment) {
33
+ if (['__proto__', 'prototype', 'constructor'].includes(segment.field) ||
34
+ !own(current, segment.field) ||
35
+ Array.isArray(current))
36
+ throw new LevelEditorError('level_editor_binding_missing', [...path, segment.field]);
37
+ current = current[segment.field];
38
+ path.push(segment.field);
39
+ }
40
+ else {
41
+ const slot = Object.hasOwn(definition.selections, segment.item.selection)
42
+ ? definition.selections[segment.item.selection]
43
+ : undefined;
44
+ const collection = slot && Object.hasOwn(definition.collections, slot.collection)
45
+ ? definition.collections[slot.collection]
46
+ : undefined;
47
+ if (!collection || collection.idField !== segment.item.key)
48
+ throw new LevelEditorError('level_editor_selection_scope_mismatch', path);
49
+ const selected = Object.hasOwn(selection, segment.item.selection)
50
+ ? selection[segment.item.selection]
51
+ : undefined;
52
+ if (selected === undefined)
53
+ throw new LevelEditorError('level_editor_selection_empty', path);
54
+ const items = members(current, segment.item.key);
55
+ const index = items.findIndex((item) => item[segment.item.key] === selected);
56
+ if (index === -1)
57
+ throw new LevelEditorError('level_editor_target_missing', path);
58
+ current = items[index];
59
+ path.push(index);
60
+ }
61
+ }
62
+ return { documentId: binding.documentId, path, value: current };
63
+ }
64
+ export function resolveLevelEditorBinding(definition, documents, bindingId, selection) {
65
+ const binding = Object.hasOwn(definition.bindings, bindingId)
66
+ ? definition.bindings[bindingId]
67
+ : undefined;
68
+ if (!binding)
69
+ throw new LevelEditorError('level_editor_unknown_binding');
70
+ return resolve(definition, documents, binding, selection);
71
+ }
72
+ export function resolveLevelEditorCollection(definition, documents, collectionId, selection) {
73
+ const collection = Object.hasOwn(definition.collections, collectionId)
74
+ ? definition.collections[collectionId]
75
+ : undefined;
76
+ if (!collection)
77
+ throw new LevelEditorError('level_editor_unknown_collection');
78
+ const result = resolve(definition, documents, collection, selection);
79
+ return { ...result, value: members(result.value, collection.idField) };
80
+ }
81
+ export function evaluateLevelEditorCondition(condition, definition, documents, selection) {
82
+ if ('conditions' in condition)
83
+ return condition.op === 'all'
84
+ ? condition.conditions.every((child) => evaluateLevelEditorCondition(child, definition, documents, selection))
85
+ : condition.conditions.some((child) => evaluateLevelEditorCondition(child, definition, documents, selection));
86
+ if ('condition' in condition)
87
+ return !evaluateLevelEditorCondition(condition.condition, definition, documents, selection);
88
+ let value;
89
+ try {
90
+ value =
91
+ 'binding' in condition.source
92
+ ? resolveLevelEditorBinding(definition, documents, condition.source.binding, selection)
93
+ .value
94
+ : selection[condition.source.selection];
95
+ }
96
+ catch (error) {
97
+ if (!(error instanceof LevelEditorError))
98
+ throw error;
99
+ value = undefined;
100
+ }
101
+ if (condition.op === 'exists')
102
+ return value !== undefined;
103
+ if (condition.op === 'eq')
104
+ return value === condition.value;
105
+ return condition.values.some((item) => item === value);
106
+ }
107
+ function collectionLocations(definition, documents) {
108
+ const locations = [];
109
+ for (const collection of Object.values(definition.collections)) {
110
+ let pending = [
111
+ { value: documents[collection.documentId], path: [] },
112
+ ];
113
+ for (const segment of collection.segments) {
114
+ pending = pending.flatMap((item) => {
115
+ if ('field' in segment) {
116
+ if (!own(item.value, segment.field) || Array.isArray(item.value))
117
+ throw new LevelEditorError('level_editor_binding_missing', [
118
+ ...item.path,
119
+ segment.field,
120
+ ]);
121
+ return [{ value: item.value[segment.field], path: [...item.path, segment.field] }];
122
+ }
123
+ return members(item.value, segment.item.key).map((value, index) => ({
124
+ value,
125
+ path: [...item.path, index],
126
+ }));
127
+ });
128
+ }
129
+ for (const item of pending) {
130
+ members(item.value, collection.idField);
131
+ locations.push({
132
+ documentId: collection.documentId,
133
+ path: item.path,
134
+ idField: collection.idField,
135
+ });
136
+ }
137
+ }
138
+ return locations;
139
+ }
140
+ function assertReadOnlyPreserved(schema, original, candidate, documentId, locations, path = []) {
141
+ if (!schema || typeof schema !== 'object' || Array.isArray(schema) || original === undefined)
142
+ return;
143
+ const node = schema;
144
+ if (node.readOnly === true) {
145
+ if (levelEditorCanonicalJson(original) !== levelEditorCanonicalJson(candidate))
146
+ throw new LevelEditorError('level_editor_readonly', path);
147
+ return;
148
+ }
149
+ if (Array.isArray(original)) {
150
+ const next = Array.isArray(candidate) ? candidate : [];
151
+ const collection = locations.find((location) => location.documentId === documentId &&
152
+ location.path.length === path.length &&
153
+ location.path.every((part, index) => path[index] === part));
154
+ if (collection) {
155
+ const previousItems = new Map(members(original, collection.idField).map((item) => [
156
+ levelEditorCanonicalJson(item[collection.idField]),
157
+ item,
158
+ ]));
159
+ members(next, collection.idField).forEach((item, index) => {
160
+ const previous = previousItems.get(levelEditorCanonicalJson(item[collection.idField]));
161
+ if (previous)
162
+ assertReadOnlyPreserved(node.items, previous, item, documentId, locations, [
163
+ ...path,
164
+ index,
165
+ ]);
166
+ });
167
+ }
168
+ else
169
+ original.forEach((item, index) => assertReadOnlyPreserved(node.items, item, next[index], documentId, locations, [
170
+ ...path,
171
+ index,
172
+ ]));
173
+ return;
174
+ }
175
+ if (!original || typeof original !== 'object')
176
+ return;
177
+ const previous = original;
178
+ const next = candidate && typeof candidate === 'object' && !Array.isArray(candidate)
179
+ ? candidate
180
+ : {};
181
+ const properties = node.properties && typeof node.properties === 'object' && !Array.isArray(node.properties)
182
+ ? node.properties
183
+ : {};
184
+ for (const name of Object.keys(previous)) {
185
+ const childSchema = Object.hasOwn(properties, name)
186
+ ? properties[name]
187
+ : node.additionalProperties;
188
+ assertReadOnlyPreserved(childSchema, previous[name], Object.hasOwn(next, name) ? next[name] : undefined, documentId, locations, [...path, name]);
189
+ }
190
+ }
191
+ export function validateLevelEditorDocuments(definition, documents, schemas) {
192
+ const diagnostics = [];
193
+ for (const documentId of Object.keys(definition.documents)) {
194
+ if (!isLevelEditorJson(documents[documentId]))
195
+ diagnostics.push({
196
+ code: 'level_editor_invalid_data_json',
197
+ message: 'level_editor_invalid_data_json',
198
+ severity: 'error',
199
+ documentId,
200
+ });
201
+ else if (schemas)
202
+ diagnostics.push(...validateLevelEditorData(schemas[documentId], documents[documentId], documentId));
203
+ }
204
+ if (diagnostics.length)
205
+ return diagnostics;
206
+ try {
207
+ collectionLocations(definition, documents);
208
+ for (const command of Object.values(definition.commands)) {
209
+ if (command.operation !== 'grid.paint' && command.operation !== 'path.edit')
210
+ continue;
211
+ const target = command.operation === 'grid.paint'
212
+ ? definition.bindings[command.target]
213
+ : definition.collections[command.collection];
214
+ let states = [
215
+ { value: documents[target.documentId], selection: {} },
216
+ ];
217
+ for (const segment of target.segments)
218
+ states = states.flatMap((state) => {
219
+ if ('field' in segment) {
220
+ if (!own(state.value, segment.field))
221
+ throw new LevelEditorError('level_editor_binding_missing');
222
+ return [{ value: state.value[segment.field], selection: state.selection }];
223
+ }
224
+ return members(state.value, segment.item.key).map((item) => ({
225
+ value: item,
226
+ selection: {
227
+ ...state.selection,
228
+ [segment.item.selection]: item[segment.item.key],
229
+ },
230
+ }));
231
+ });
232
+ for (const state of states) {
233
+ if (command.operation === 'path.edit') {
234
+ const collection = definition.collections[command.collection];
235
+ const nodes = members(state.value, collection.idField);
236
+ const edges = resolveLevelEditorBinding(definition, documents, command.edges, state.selection).value;
237
+ validateLevelEditorPath(nodes, edges, collection.idField, command);
238
+ continue;
239
+ }
240
+ const rows = resolveLevelEditorBinding(definition, documents, command.dimensions.rows, state.selection).value;
241
+ const columns = resolveLevelEditorBinding(definition, documents, command.dimensions.columns, state.selection).value;
242
+ if (typeof rows !== 'number' ||
243
+ !Number.isInteger(rows) ||
244
+ rows < 1 ||
245
+ typeof columns !== 'number' ||
246
+ !Number.isInteger(columns) ||
247
+ columns < 1 ||
248
+ !Array.isArray(state.value) ||
249
+ state.value.length !== rows ||
250
+ state.value.some((row) => !Array.isArray(row) || row.length !== columns))
251
+ throw new LevelEditorError('level_editor_grid_dimensions');
252
+ }
253
+ }
254
+ }
255
+ catch (error) {
256
+ if (!(error instanceof LevelEditorError))
257
+ throw error;
258
+ diagnostics.push({
259
+ code: error.code,
260
+ message: error.code,
261
+ severity: 'error',
262
+ path: error.path,
263
+ });
264
+ }
265
+ return diagnostics;
266
+ }
267
+ export function applyLevelEditorCommand(definitionInput, documents, invocationInput, schemas) {
268
+ try {
269
+ return reduceLevelEditorCommand(definitionInput, documents, invocationInput, schemas);
270
+ }
271
+ catch (error) {
272
+ if (error instanceof z.ZodError)
273
+ throw new LevelEditorError('level_editor_invalid_command_input', error.issues[0]?.path.map((part) => (typeof part === 'number' ? part : String(part))));
274
+ throw error;
275
+ }
276
+ }
277
+ /** History is supplied by a trusted receipt, never by an engineering document or a write payload. */
278
+ export function applyLevelEditorHistory(definition, documents, entry, direction, schemas) {
279
+ if (!isLevelEditorJson(entry.before) || !isLevelEditorJson(entry.after))
280
+ throw new LevelEditorError('level_editor_history_invalid');
281
+ const forward = applyLevelEditorCommand(definition, { ...documents, [entry.documentId]: entry.before }, entry.command, schemas);
282
+ if (forward.documentId !== entry.documentId ||
283
+ levelEditorCanonicalJson(forward.data) !== levelEditorCanonicalJson(entry.after))
284
+ throw new LevelEditorError('level_editor_history_invalid');
285
+ const expected = direction === 'undo' ? entry.after : entry.before;
286
+ const data = structuredClone(direction === 'undo' ? entry.before : entry.after);
287
+ if (levelEditorCanonicalJson(documents[entry.documentId]) !== levelEditorCanonicalJson(expected))
288
+ throw new LevelEditorError('level_editor_history_conflict');
289
+ const nextDocuments = { ...documents, [entry.documentId]: data };
290
+ const errors = validateLevelEditorDocuments(definition, nextDocuments, schemas);
291
+ if (errors.length)
292
+ throw new LevelEditorError(errors[0].code, errors[0].path);
293
+ if (schemas)
294
+ assertReadOnlyPreserved(schemas[entry.documentId], documents[entry.documentId], data, entry.documentId, collectionLocations(definition, nextDocuments));
295
+ const changes = forward.changes.map((change) => {
296
+ let value = data;
297
+ for (const part of change.path) {
298
+ if (!own(value, part))
299
+ throw new LevelEditorError('level_editor_history_invalid');
300
+ value = value[part];
301
+ }
302
+ if (!isLevelEditorJson(value))
303
+ throw new LevelEditorError('level_editor_history_invalid');
304
+ return { path: [...change.path], op: 'set', value: structuredClone(value) };
305
+ });
306
+ return { documentId: entry.documentId, data, changes };
307
+ }
308
+ function reduceLevelEditorCommand(definitionInput, documents, invocationInput, schemas) {
309
+ const compiled = compileLevelEditorDefinition(definitionInput, schemas);
310
+ if (!compiled.ok)
311
+ throw new LevelEditorError(compiled.diagnostics[0].code, compiled.diagnostics[0].path);
312
+ const definition = compiled.definition;
313
+ if (!isLevelEditorJson(invocationInput) ||
314
+ new TextEncoder().encode(JSON.stringify(invocationInput)).byteLength >
315
+ LEVEL_EDITOR_TRANSACTION_MAX_BYTES)
316
+ throw new LevelEditorError('level_editor_transaction_budget');
317
+ const invocation = levelEditorCommandInvocationSchema.parse(invocationInput);
318
+ const command = Object.hasOwn(definition.commands, invocation.commandId)
319
+ ? definition.commands[invocation.commandId]
320
+ : undefined;
321
+ if (!command)
322
+ throw new LevelEditorError('level_editor_unknown_command');
323
+ for (const documentId of Object.keys(definition.documents)) {
324
+ if (!isLevelEditorJson(documents[documentId]))
325
+ throw new LevelEditorError('level_editor_invalid_data_json');
326
+ if (schemas) {
327
+ const errors = validateLevelEditorData(schemas[documentId], documents[documentId], documentId);
328
+ if (errors.length)
329
+ throw new LevelEditorError(errors[0].code, errors[0].path);
330
+ }
331
+ }
332
+ const locations = collectionLocations(definition, documents);
333
+ const changes = [];
334
+ let documentId;
335
+ let data;
336
+ const writable = (target, allowIdentity = false) => {
337
+ if (documentId && target.documentId !== documentId)
338
+ throw new LevelEditorError('level_editor_cross_document_command');
339
+ if (schemas && levelEditorSchemaAtPath(schemas[target.documentId], target.path).readOnly)
340
+ throw new LevelEditorError('level_editor_readonly', target.path);
341
+ if (!allowIdentity &&
342
+ locations.some((location) => location.documentId === target.documentId &&
343
+ target.path.length === location.path.length + 2 &&
344
+ location.path.every((part, index) => target.path[index] === part) &&
345
+ target.path.at(-1) === location.idField))
346
+ throw new LevelEditorError('level_editor_identity_immutable', target.path);
347
+ if (!documentId) {
348
+ documentId = target.documentId;
349
+ data = structuredClone(documents[documentId]);
350
+ }
351
+ };
352
+ const set = (target, value, allowIdentity = false) => {
353
+ writable(target, allowIdentity);
354
+ let parent = data;
355
+ for (const part of target.path.slice(0, -1)) {
356
+ if (!own(parent, part))
357
+ throw new LevelEditorError('level_editor_target_missing', target.path);
358
+ parent = parent[part];
359
+ }
360
+ const last = target.path.at(-1);
361
+ if (!own(parent, last))
362
+ throw new LevelEditorError('level_editor_target_missing', target.path);
363
+ parent[last] = structuredClone(value);
364
+ changes.push({ path: target.path, op: 'set', value: structuredClone(value) });
365
+ };
366
+ const binding = (name) => resolveLevelEditorBinding(definition, documents, name, invocation.selection);
367
+ if (command.operation === 'field.set') {
368
+ const input = levelEditorCommandInputSchemas['field.set'].parse(invocation.input);
369
+ if (!isLevelEditorJson(input.value) ||
370
+ (input.value !== null && typeof input.value === 'object'))
371
+ throw new LevelEditorError('level_editor_field_requires_primitive');
372
+ set(binding(command.target), input.value);
373
+ }
374
+ else if (command.operation === 'grid.paint') {
375
+ const input = levelEditorCommandInputSchemas['grid.paint'].parse(invocation.input);
376
+ const target = binding(command.target), rows = binding(command.dimensions.rows).value, columns = binding(command.dimensions.columns).value;
377
+ if (typeof rows !== 'number' ||
378
+ typeof columns !== 'number' ||
379
+ !Number.isInteger(rows) ||
380
+ !Number.isInteger(columns) ||
381
+ rows < 1 ||
382
+ columns < 1 ||
383
+ !Array.isArray(target.value) ||
384
+ target.value.length !== rows ||
385
+ target.value.some((row) => !Array.isArray(row) || row.length !== columns))
386
+ throw new LevelEditorError('level_editor_grid_dimensions');
387
+ const cells = new Set();
388
+ for (const cell of input.cells) {
389
+ if (cell.row >= rows || cell.column >= columns)
390
+ throw new LevelEditorError('level_editor_grid_out_of_bounds');
391
+ if (!isLevelEditorJson(cell.value) ||
392
+ !command.values.some((value) => levelEditorCanonicalJson(value) === levelEditorCanonicalJson(cell.value)))
393
+ throw new LevelEditorError('level_editor_grid_value');
394
+ const key = `${cell.row}:${cell.column}`;
395
+ if (cells.has(key))
396
+ throw new LevelEditorError('level_editor_duplicate_cell');
397
+ cells.add(key);
398
+ set({ ...target, path: [...target.path, cell.row, cell.column] }, cell.value);
399
+ }
400
+ }
401
+ else if (command.operation === 'path.edit') {
402
+ const input = levelEditorCommandInputSchemas['path.edit'].parse(invocation.input);
403
+ const collection = definition.collections[command.collection];
404
+ const target = resolveLevelEditorCollection(definition, documents, command.collection, invocation.selection);
405
+ const edgeTarget = binding(command.edges);
406
+ const next = editLevelEditorPath(target.value, edgeTarget.value, collection.idField, command, input, invocation.selection[command.selection]);
407
+ if (levelEditorCanonicalJson(target.value) !== levelEditorCanonicalJson(next.nodes))
408
+ set(target, next.nodes, true);
409
+ if (levelEditorCanonicalJson(edgeTarget.value) !== levelEditorCanonicalJson(next.edges))
410
+ set(edgeTarget, next.edges);
411
+ }
412
+ else if (command.operation === 'command.batch') {
413
+ if (validateLevelEditorData(command.inputSchema, invocation.input).length)
414
+ throw new LevelEditorError('level_editor_invalid_command_input');
415
+ const steps = command.steps.map((step) => ({
416
+ commandId: step.commandId,
417
+ selection: invocation.selection,
418
+ input: Object.fromEntries(Object.entries(step.input).map(([key, source]) => {
419
+ if ('input' in source && !Object.hasOwn(invocation.input, source.input))
420
+ throw new LevelEditorError('level_editor_command_input_missing');
421
+ return [key, 'input' in source ? invocation.input[source.input] : source.value];
422
+ })),
423
+ }));
424
+ const batch = applyLevelEditorCommandBatch(definition, documents, steps, schemas);
425
+ if (batch.results.length !== 1)
426
+ throw new LevelEditorError('level_editor_cross_document_command');
427
+ return batch.results[0];
428
+ }
429
+ else if (command.operation === 'object.transform') {
430
+ const input = levelEditorCommandInputSchemas['object.transform'].parse(invocation.input);
431
+ if (!Object.keys(input).length)
432
+ throw new LevelEditorError('level_editor_empty_transform');
433
+ for (const [coordinate, value] of Object.entries(input)) {
434
+ const target = command.bindings[coordinate];
435
+ if (!target)
436
+ throw new LevelEditorError('level_editor_unsupported_transform');
437
+ const resolved = binding(target);
438
+ if (typeof resolved.value !== 'number')
439
+ throw new LevelEditorError('level_editor_transform_type');
440
+ set(resolved, value);
441
+ }
442
+ }
443
+ else {
444
+ const collection = definition.collections[command.collection];
445
+ const target = resolveLevelEditorCollection(definition, documents, command.collection, invocation.selection);
446
+ const values = structuredClone(members(target.value, collection.idField));
447
+ const selectionName = command.selection ??
448
+ Object.entries(definition.selections).find(([, value]) => value.collection === command.collection)?.[0];
449
+ const selected = selectionName && invocation.selection[selectionName];
450
+ const index = values.findIndex((item) => item[collection.idField] === selected);
451
+ if (command.operation !== 'collection.insert' && index < 0)
452
+ throw new LevelEditorError('level_editor_target_missing');
453
+ if (command.operation === 'collection.duplicate') {
454
+ const { newId } = levelEditorCommandInputSchemas['collection.duplicate'].parse(invocation.input);
455
+ const item = structuredClone(values[index]);
456
+ item[collection.idField] = newId;
457
+ values.splice(index + 1, 0, item);
458
+ }
459
+ else if (command.operation === 'collection.delete') {
460
+ levelEditorCommandInputSchemas['collection.delete'].parse(invocation.input);
461
+ values.splice(index, 1);
462
+ }
463
+ else if (command.operation === 'collection.insert') {
464
+ const input = levelEditorCommandInputSchemas['collection.insert'].parse(invocation.input);
465
+ if (!isLevelEditorJson(input.item) ||
466
+ !input.item ||
467
+ typeof input.item !== 'object' ||
468
+ Array.isArray(input.item))
469
+ throw new LevelEditorError('level_editor_invalid_item');
470
+ const at = input.beforeId === undefined
471
+ ? values.length
472
+ : values.findIndex((item) => item[collection.idField] === input.beforeId);
473
+ if (at < 0)
474
+ throw new LevelEditorError('level_editor_target_missing');
475
+ values.splice(at, 0, input.item);
476
+ }
477
+ else {
478
+ const { beforeId } = levelEditorCommandInputSchemas['collection.reorder'].parse(invocation.input);
479
+ if (beforeId === selected)
480
+ throw new LevelEditorError('level_editor_reorder_same_target');
481
+ const [item] = values.splice(index, 1);
482
+ const at = beforeId === undefined || beforeId === null
483
+ ? values.length
484
+ : values.findIndex((value) => value[collection.idField] === beforeId);
485
+ if (at < 0)
486
+ throw new LevelEditorError('level_editor_target_missing');
487
+ values.splice(at, 0, item);
488
+ }
489
+ members(values, collection.idField);
490
+ set(target, values, true);
491
+ }
492
+ if (!documentId || data === undefined || !changes.length)
493
+ throw new LevelEditorError('level_editor_empty_command');
494
+ const nextDocuments = { ...documents, [documentId]: data };
495
+ const errors = validateLevelEditorDocuments(definition, nextDocuments, schemas);
496
+ if (errors.length)
497
+ throw new LevelEditorError(errors[0].code, errors[0].path);
498
+ if (schemas)
499
+ assertReadOnlyPreserved(schemas[documentId], documents[documentId], data, documentId, collectionLocations(definition, nextDocuments));
500
+ return { documentId, data, changes };
501
+ }
502
+ export function applyLevelEditorCommandBatch(definition, documents, commands, schemas) {
503
+ if (!commands.length || commands.length > 64)
504
+ throw new LevelEditorError('level_editor_batch_budget');
505
+ let current = { ...documents };
506
+ const changed = new Map();
507
+ let count = 0;
508
+ const compiled = compileLevelEditorDefinition(definition, schemas);
509
+ if (!compiled.ok)
510
+ throw new LevelEditorError(compiled.diagnostics[0].code);
511
+ const expand = (invocation, depth = 0) => {
512
+ if (depth > 8)
513
+ throw new LevelEditorError('level_editor_batch_budget');
514
+ const command = definition.commands[invocation.commandId];
515
+ if (command?.operation !== 'command.batch')
516
+ return [invocation];
517
+ if (validateLevelEditorData(command.inputSchema, invocation.input).length)
518
+ throw new LevelEditorError('level_editor_invalid_command_input');
519
+ const result = [];
520
+ for (const step of command.steps) {
521
+ const input = {};
522
+ for (const [key, source] of Object.entries(step.input)) {
523
+ if ('input' in source && !Object.hasOwn(invocation.input, source.input))
524
+ throw new LevelEditorError('level_editor_command_input_missing');
525
+ input[key] = 'input' in source ? invocation.input[source.input] : source.value;
526
+ }
527
+ result.push(...expand({ commandId: step.commandId, selection: invocation.selection, input }, depth + 1));
528
+ if (result.length > 64)
529
+ throw new LevelEditorError('level_editor_batch_budget');
530
+ }
531
+ return result;
532
+ };
533
+ const expanded = commands.flatMap((command) => expand(command));
534
+ if (expanded.length > 64)
535
+ throw new LevelEditorError('level_editor_batch_budget');
536
+ for (const command of expanded) {
537
+ const result = applyLevelEditorCommand(definition, current, command, schemas);
538
+ count += result.changes.length;
539
+ if (count > 2048)
540
+ throw new LevelEditorError('level_editor_batch_budget');
541
+ current = { ...current, [result.documentId]: result.data };
542
+ // Changes are replayed in order by format-aware writers; the final value at each path wins.
543
+ const previous = changed.get(result.documentId);
544
+ const changes = [...(previous?.changes ?? []), ...result.changes];
545
+ const paths = changes.filter((change, index) => !changes.some((other, otherIndex) => otherIndex !== index &&
546
+ other.path.length <= change.path.length &&
547
+ other.path.every((part, i) => change.path[i] === part) &&
548
+ (other.path.length < change.path.length || otherIndex < index)));
549
+ changed.set(result.documentId, {
550
+ ...result,
551
+ changes: paths.map((change) => {
552
+ let value = result.data;
553
+ for (const part of change.path) {
554
+ if (!own(value, part))
555
+ throw new LevelEditorError('level_editor_target_missing');
556
+ value = value[part];
557
+ }
558
+ return {
559
+ path: change.path,
560
+ op: 'set',
561
+ value: structuredClone(value),
562
+ };
563
+ }),
564
+ });
565
+ }
566
+ const diagnostics = validateLevelEditorDocuments(definition, current, schemas);
567
+ if (diagnostics.length)
568
+ throw new LevelEditorError(diagnostics[0].code, diagnostics[0].path);
569
+ if (schemas) {
570
+ const locations = collectionLocations(definition, current);
571
+ for (const id of changed.keys())
572
+ assertReadOnlyPreserved(schemas[id], documents[id], current[id], id, locations);
573
+ }
574
+ return { results: [...changed.values()] };
575
+ }
576
+ export function applyLevelEditorHistoryBatch(definition, documents, entry, direction, schemas) {
577
+ const ids = entry.documents.map((item) => item.documentId);
578
+ if (!ids.length ||
579
+ new Set(ids).size !== ids.length ||
580
+ entry.documents.some((item) => !isLevelEditorJson(item.before) || !isLevelEditorJson(item.after)))
581
+ throw new LevelEditorError('level_editor_history_invalid');
582
+ const before = {
583
+ ...documents,
584
+ ...Object.fromEntries(entry.documents.map((item) => [item.documentId, item.before])),
585
+ };
586
+ const forward = applyLevelEditorCommandBatch(definition, before, entry.commands, schemas);
587
+ if (forward.results.length !== ids.length ||
588
+ forward.results.some((result) => levelEditorCanonicalJson(result.data) !==
589
+ levelEditorCanonicalJson(entry.documents.find((item) => item.documentId === result.documentId)?.after)))
590
+ throw new LevelEditorError('level_editor_history_invalid');
591
+ const candidate = { ...documents };
592
+ for (const item of entry.documents) {
593
+ if (levelEditorCanonicalJson(documents[item.documentId]) !==
594
+ levelEditorCanonicalJson(direction === 'undo' ? item.after : item.before))
595
+ throw new LevelEditorError('level_editor_history_conflict');
596
+ candidate[item.documentId] = structuredClone(direction === 'undo' ? item.before : item.after);
597
+ }
598
+ const diagnostics = validateLevelEditorDocuments(definition, candidate, schemas);
599
+ if (diagnostics.length)
600
+ throw new LevelEditorError(diagnostics[0].code, diagnostics[0].path);
601
+ const locations = collectionLocations(definition, candidate);
602
+ return {
603
+ results: forward.results.map((result) => {
604
+ const data = candidate[result.documentId];
605
+ if (schemas)
606
+ assertReadOnlyPreserved(schemas[result.documentId], documents[result.documentId], data, result.documentId, locations);
607
+ return {
608
+ documentId: result.documentId,
609
+ data,
610
+ changes: result.changes.map((change) => {
611
+ let value = data;
612
+ for (const part of change.path) {
613
+ if (!own(value, part))
614
+ throw new LevelEditorError('level_editor_history_invalid');
615
+ value = value[part];
616
+ }
617
+ return {
618
+ path: change.path,
619
+ op: 'set',
620
+ value: structuredClone(value),
621
+ };
622
+ }),
623
+ };
624
+ }),
625
+ };
626
+ }
627
+ //# sourceMappingURL=level-editor-reducer.js.map