cwtools-shared 0.2.9 → 0.4.0

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 (65) hide show
  1. package/dist/debug/zz.d.ts +1 -0
  2. package/dist/debug/zz.js +12 -0
  3. package/dist/generated/mcpTools.d.ts +49 -4
  4. package/dist/generated/mcpTools.js +94 -49
  5. package/dist/generated/workshopDetection.d.ts +22 -0
  6. package/dist/generated/workshopDetection.js +50 -0
  7. package/dist/generated/workspaceDetectionProfiles.d.ts +2 -0
  8. package/dist/generated/workspaceDetectionProfiles.js +84 -0
  9. package/dist/generated/workspaceGameDetection.d.ts +39 -0
  10. package/dist/generated/workspaceGameDetection.js +424 -0
  11. package/dist/host/capabilities.d.ts +39 -0
  12. package/dist/host/capabilities.js +128 -0
  13. package/dist/host/diagnostics.d.ts +8 -1
  14. package/dist/host/filesystem.d.ts +2 -0
  15. package/dist/host/hostServices.d.ts +47 -0
  16. package/dist/host/indexing.d.ts +3 -0
  17. package/dist/host/lsp.d.ts +9 -0
  18. package/dist/host/lsp.js +27 -0
  19. package/dist/host/readiness.js +10 -0
  20. package/dist/host/vanillaCache.js +1 -1
  21. package/dist/index.d.ts +16 -1
  22. package/dist/index.js +22 -1
  23. package/dist/knowledge/interfaceKnowledge.d.ts +8 -0
  24. package/dist/knowledge/interfaceKnowledge.js +222 -0
  25. package/dist/project/descriptor.d.ts +12 -0
  26. package/dist/project/descriptor.js +69 -0
  27. package/dist/project/init.d.ts +11 -0
  28. package/dist/project/init.js +356 -0
  29. package/dist/project/knowledge.d.ts +6 -0
  30. package/dist/project/knowledge.js +20 -8
  31. package/dist/project/profile.d.ts +62 -7
  32. package/dist/project/profile.js +4 -3
  33. package/dist/project/profileBuilder.d.ts +31 -0
  34. package/dist/project/profileBuilder.js +989 -0
  35. package/dist/tools/archetypeArtifacts.d.ts +29 -0
  36. package/dist/tools/archetypeArtifacts.js +136 -0
  37. package/dist/tools/archetypeSlots.d.ts +32 -0
  38. package/dist/tools/archetypeSlots.js +257 -0
  39. package/dist/tools/archetypeTools.d.ts +4 -0
  40. package/dist/tools/archetypeTools.js +118 -0
  41. package/dist/tools/callHandler.d.ts +10 -0
  42. package/dist/tools/callHandler.js +76 -0
  43. package/dist/tools/catalog.d.ts +4094 -0
  44. package/dist/tools/catalog.js +5031 -0
  45. package/dist/tools/domainTools.d.ts +8 -0
  46. package/dist/tools/domainTools.js +222 -0
  47. package/dist/tools/mcpSchema.d.ts +20 -3
  48. package/dist/tools/mcpSchema.js +55 -7
  49. package/dist/tools/mediaWrites.d.ts +52 -0
  50. package/dist/tools/mediaWrites.js +322 -0
  51. package/dist/tools/names.d.ts +4 -4
  52. package/dist/tools/names.js +14 -1
  53. package/dist/tools/pdxWriteCore.d.ts +51 -0
  54. package/dist/tools/pdxWriteCore.js +628 -0
  55. package/dist/tools/pdxWrites.d.ts +88 -0
  56. package/dist/tools/pdxWrites.js +629 -0
  57. package/dist/tools/registry.d.ts +1 -0
  58. package/dist/tools/scopeBridge.d.ts +36 -0
  59. package/dist/tools/scopeBridge.js +173 -0
  60. package/dist/tools/symbols.js +22 -2
  61. package/dist/tools/targetingWeights.d.ts +206 -0
  62. package/dist/tools/targetingWeights.js +350 -0
  63. package/dist/tools/toolHandlers.d.ts +5 -1
  64. package/dist/tools/toolHandlers.js +146 -33
  65. package/package.json +1 -1
@@ -0,0 +1,628 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sha256Text = sha256Text;
4
+ exports.buildTypedPdxCandidate = buildTypedPdxCandidate;
5
+ const crypto_1 = require("crypto");
6
+ const MAX_DEPTH = 8;
7
+ const MAX_NODES = 256;
8
+ const MAX_TEXT = 4096;
9
+ const MAX_FILE_CHARS = 2000000;
10
+ const MAX_OVERRIDES = 64;
11
+ const IDENTIFIER = /^[A-Za-z_][A-Za-z0-9_.:@-]*$/;
12
+ const INLINE_SCRIPT = /^[A-Za-z0-9_.:@/-]+$/;
13
+ function fail(message) {
14
+ throw new Error('typedPdxWrite: ' + message);
15
+ }
16
+ function sha256Text(text) {
17
+ return (0, crypto_1.createHash)('sha256').update(text, 'utf8').digest('hex');
18
+ }
19
+ function asRecordValue(value) {
20
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
21
+ }
22
+ function assertIdentifier(value, label) {
23
+ if (typeof value !== 'string' || !IDENTIFIER.test(value))
24
+ fail(label + ' must be a PDX identifier');
25
+ }
26
+ function assertOnlyKeys(record, allowed, label) {
27
+ const extra = Object.keys(record).filter(key => !allowed.includes(key));
28
+ if (extra.length > 0)
29
+ fail(label + ' contains unsupported field(s): ' + extra.join(', '));
30
+ }
31
+ function validateValue(value, depth = 0, state = { nodes: 0 }) {
32
+ if (!value || typeof value !== 'object' || Array.isArray(value))
33
+ fail('PDX value must be an object');
34
+ if (depth > MAX_DEPTH || ++state.nodes > MAX_NODES)
35
+ fail('PDX value exceeds safety limits');
36
+ const record = value;
37
+ switch (record.kind) {
38
+ case 'identifier':
39
+ assertOnlyKeys(record, ['kind', 'value'], 'identifier value');
40
+ assertIdentifier(record.value, 'identifier');
41
+ return;
42
+ case 'string':
43
+ assertOnlyKeys(record, ['kind', 'value'], 'string value');
44
+ if (typeof record.value !== 'string' || record.value.length > MAX_TEXT
45
+ || [...record.value].some(char => char.charCodeAt(0) < 32 || char.charCodeAt(0) === 127))
46
+ fail('invalid string value');
47
+ return;
48
+ case 'number':
49
+ assertOnlyKeys(record, ['kind', 'value'], 'number value');
50
+ if (typeof record.value !== 'number' || !Number.isFinite(record.value))
51
+ fail('invalid number value');
52
+ return;
53
+ case 'boolean':
54
+ assertOnlyKeys(record, ['kind', 'value'], 'boolean value');
55
+ if (typeof record.value !== 'boolean')
56
+ fail('invalid boolean value');
57
+ return;
58
+ case 'list':
59
+ assertOnlyKeys(record, ['kind', 'values'], 'list value');
60
+ if (!Array.isArray(record.values))
61
+ fail('list values must be an array');
62
+ if (record.values.length > MAX_NODES)
63
+ fail('PDX value exceeds safety limits');
64
+ for (const item of record.values)
65
+ validateValue(item, depth + 1, state);
66
+ return;
67
+ case 'block':
68
+ assertOnlyKeys(record, ['kind', 'entries'], 'block value');
69
+ if (!Array.isArray(record.entries))
70
+ fail('block entries must be an array');
71
+ validateEntries(record.entries, depth + 1, state);
72
+ return;
73
+ default:
74
+ fail('unsupported PdxValue kind; raw code is forbidden');
75
+ }
76
+ }
77
+ function validateEntries(entries, depth = 0, state = { nodes: 0 }) {
78
+ if (entries.length > MAX_NODES)
79
+ fail('PDX entries exceed safety limits');
80
+ for (const entry of entries) {
81
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry))
82
+ fail('invalid PDX entry');
83
+ const record = entry;
84
+ assertOnlyKeys(record, ['key', 'value'], 'PDX entry');
85
+ assertIdentifier(record.key, 'PDX entry key');
86
+ validateValue(record.value, depth, state);
87
+ }
88
+ }
89
+ function validatePath(value, label) {
90
+ if (!Array.isArray(value) || value.length === 0)
91
+ fail(label + ' path must be a non-empty array');
92
+ if (value.length > MAX_DEPTH)
93
+ fail(label + ' path exceeds safety limit');
94
+ for (const segment of value) {
95
+ const record = asRecordValue(segment);
96
+ assertOnlyKeys(record, ['key', 'occurrence'], label + ' path segment');
97
+ assertIdentifier(record.key, label + ' path key');
98
+ if (record.occurrence !== undefined && (!Number.isSafeInteger(record.occurrence) || record.occurrence < 0)) {
99
+ fail(label + ' path occurrence must be a non-negative integer');
100
+ }
101
+ }
102
+ }
103
+ function quote(value) {
104
+ return '"' + value.replace(/\\/g, '\\\\').replace(/"/g, '\\"') + '"';
105
+ }
106
+ function renderValue(value, indent, newline, depth = 0) {
107
+ if (depth > MAX_DEPTH)
108
+ fail('render depth exceeded');
109
+ switch (value.kind) {
110
+ case 'identifier': return value.value;
111
+ case 'string': return quote(value.value);
112
+ case 'number': return String(value.value);
113
+ case 'boolean': return value.value ? 'yes' : 'no';
114
+ case 'list': return '{ ' + value.values.map(item => renderValue(item, indent, newline, depth + 1)).join(' ') + ' }';
115
+ case 'block': {
116
+ if (value.entries.length === 0)
117
+ return '{}';
118
+ const childIndent = indent + '\t';
119
+ return '{' + newline + value.entries
120
+ .map(entry => childIndent + entry.key + ' = ' + renderValue(entry.value, childIndent, newline, depth + 1))
121
+ .join(newline) + newline + indent + '}';
122
+ }
123
+ }
124
+ }
125
+ function skipTrivia(text, index) {
126
+ let i = index;
127
+ while (i < text.length) {
128
+ if (/\s/.test(text[i])) {
129
+ i++;
130
+ continue;
131
+ }
132
+ if (text[i] === '#') {
133
+ while (i < text.length && text[i] !== '\n')
134
+ i++;
135
+ continue;
136
+ }
137
+ break;
138
+ }
139
+ return i;
140
+ }
141
+ function readIdentifier(text, index) {
142
+ const match = /^[A-Za-z_][A-Za-z0-9_.:@-]*/.exec(text.slice(index));
143
+ return match ? { value: match[0], end: index + match[0].length } : undefined;
144
+ }
145
+ function skipString(text, index) {
146
+ let i = index + 1;
147
+ while (i < text.length && text[i] !== '"') {
148
+ if (text[i] === '\\')
149
+ i++;
150
+ i++;
151
+ }
152
+ return Math.min(i + 1, text.length);
153
+ }
154
+ function scanEntries(text, block) {
155
+ const entries = [];
156
+ let index = block.open + 1;
157
+ while (index < block.close) {
158
+ index = skipTrivia(text, index);
159
+ if (index >= block.close)
160
+ break;
161
+ const key = readIdentifier(text, index);
162
+ if (!key) {
163
+ index++;
164
+ continue;
165
+ }
166
+ const afterKey = skipTrivia(text, key.end);
167
+ if (text[afterKey] !== '=') {
168
+ index = key.end;
169
+ continue;
170
+ }
171
+ const valueStart = skipTrivia(text, afterKey + 1);
172
+ let valueEnd = valueStart;
173
+ if (text[valueStart] === '"') {
174
+ valueEnd = skipString(text, valueStart);
175
+ }
176
+ else if (text[valueStart] === '{') {
177
+ let depth = 0;
178
+ while (valueEnd < block.close) {
179
+ if (text[valueEnd] === '"') {
180
+ valueEnd = skipString(text, valueEnd);
181
+ continue;
182
+ }
183
+ if (text[valueEnd] === '#') {
184
+ while (valueEnd < block.close && text[valueEnd] !== '\n')
185
+ valueEnd++;
186
+ continue;
187
+ }
188
+ if (text[valueEnd] === '{')
189
+ depth++;
190
+ else if (text[valueEnd] === '}') {
191
+ depth--;
192
+ if (depth === 0) {
193
+ valueEnd++;
194
+ break;
195
+ }
196
+ }
197
+ valueEnd++;
198
+ }
199
+ }
200
+ else {
201
+ while (valueEnd < block.close && !/[\s}#]/.test(text[valueEnd]))
202
+ valueEnd++;
203
+ }
204
+ entries.push({ key: key.value, valueStart, valueEnd });
205
+ index = Math.max(valueEnd, key.end);
206
+ }
207
+ return entries;
208
+ }
209
+ function readQuoted(text, start, limit) {
210
+ let value = '';
211
+ for (let i = start + 1; i < limit; i++) {
212
+ const char = text[i];
213
+ if (char === '\\') {
214
+ const next = text[++i];
215
+ if (next === undefined)
216
+ return undefined;
217
+ value += next;
218
+ }
219
+ else if (char === '"') {
220
+ return value;
221
+ }
222
+ else {
223
+ value += char;
224
+ }
225
+ }
226
+ return undefined;
227
+ }
228
+ /**
229
+ * Definition identity for `kind = { id = name }` blocks (events, on_actions).
230
+ * Only immediate children count, so a nested `id` cannot rename the outer block.
231
+ */
232
+ function immediateId(text, open, close) {
233
+ let depth = 0;
234
+ let quoted = false;
235
+ let comment = false;
236
+ for (let i = open + 1; i < close; i++) {
237
+ const char = text[i];
238
+ if (comment) {
239
+ if (char === '\n')
240
+ comment = false;
241
+ continue;
242
+ }
243
+ if (quoted) {
244
+ if (char === '\\')
245
+ i++;
246
+ else if (char === '"')
247
+ quoted = false;
248
+ continue;
249
+ }
250
+ if (char === '"') {
251
+ quoted = true;
252
+ continue;
253
+ }
254
+ if (char === '#') {
255
+ comment = true;
256
+ continue;
257
+ }
258
+ if (char === '{') {
259
+ depth++;
260
+ continue;
261
+ }
262
+ if (char === '}') {
263
+ depth--;
264
+ continue;
265
+ }
266
+ if (depth !== 0)
267
+ continue;
268
+ const key = readIdentifier(text, i);
269
+ if (!key)
270
+ continue;
271
+ i = skipTrivia(text, key.end);
272
+ if (text[i] !== '=')
273
+ continue;
274
+ i = skipTrivia(text, i + 1);
275
+ if (key.value !== 'id')
276
+ continue;
277
+ if (text[i] === '"')
278
+ return readQuoted(text, i, close);
279
+ return readIdentifier(text, i)?.value;
280
+ }
281
+ return undefined;
282
+ }
283
+ function scanBlocks(text) {
284
+ const visit = (start, end) => {
285
+ const found = [];
286
+ let index = start;
287
+ while (index < end) {
288
+ index = skipTrivia(text, index);
289
+ if (index >= end)
290
+ break;
291
+ const key = readIdentifier(text, index);
292
+ if (!key) {
293
+ index++;
294
+ continue;
295
+ }
296
+ const afterKey = skipTrivia(text, key.end);
297
+ const valueStart = text[afterKey] === '=' ? skipTrivia(text, afterKey + 1) : afterKey;
298
+ if (text[valueStart] !== '{') {
299
+ // A scalar assignment is not a definition; step past it so a following
300
+ // block is not mistaken for one.
301
+ let cursor = valueStart;
302
+ while (cursor < end && text[cursor] !== '\n' && text[cursor] !== '}') {
303
+ cursor = text[cursor] === '"' ? skipString(text, cursor) : cursor + 1;
304
+ }
305
+ index = Math.max(cursor, key.end);
306
+ continue;
307
+ }
308
+ let depth = 0;
309
+ let close = valueStart;
310
+ while (close < end) {
311
+ if (text[close] === '"') {
312
+ close = skipString(text, close);
313
+ continue;
314
+ }
315
+ if (text[close] === '#') {
316
+ while (close < end && text[close] !== '\n')
317
+ close++;
318
+ continue;
319
+ }
320
+ if (text[close] === '{')
321
+ depth++;
322
+ else if (text[close] === '}') {
323
+ depth--;
324
+ if (depth === 0)
325
+ break;
326
+ }
327
+ close++;
328
+ }
329
+ if (close >= end)
330
+ break;
331
+ const lineStart = text.lastIndexOf('\n', index - 1) + 1;
332
+ const indent = /^[ \t]*/.exec(text.slice(lineStart, index))?.[0] ?? '';
333
+ found.push({
334
+ key: key.value,
335
+ symbol: immediateId(text, valueStart, close) ?? key.value,
336
+ start: lineStart,
337
+ open: valueStart,
338
+ close,
339
+ end: close + 1,
340
+ indent,
341
+ children: visit(valueStart + 1, close),
342
+ });
343
+ index = close + 1;
344
+ }
345
+ return found;
346
+ };
347
+ return visit(0, text.length);
348
+ }
349
+ function findUnique(blocks, name, label) {
350
+ const matches = blocks.filter(block => block.key === name || block.symbol === name);
351
+ if (matches.length === 0)
352
+ fail('could not find ' + label + ': ' + name);
353
+ if (matches.length > 1)
354
+ fail('ambiguous ' + label + ': ' + name + ' appears ' + matches.length + ' times');
355
+ return matches[0];
356
+ }
357
+ function findContainer(root, containerPath) {
358
+ if (containerPath !== undefined && !Array.isArray(containerPath))
359
+ fail('containerPath must be an array');
360
+ if ((containerPath?.length ?? 0) > MAX_DEPTH)
361
+ fail('containerPath exceeds safety limit');
362
+ let current = root;
363
+ for (const segment of containerPath ?? []) {
364
+ if (typeof segment !== 'string')
365
+ fail('containerPath segment must be a string');
366
+ current = findUnique(current.children, segment, 'container');
367
+ }
368
+ return current;
369
+ }
370
+ function appendEntry(text, block, entry, newline) {
371
+ const childIndent = block.indent + '\t';
372
+ const rendered = childIndent + entry.key + ' = ' + renderValue(entry.value, childIndent, newline);
373
+ const closeLineStart = text.lastIndexOf('\n', block.close - 1) + 1;
374
+ return text.slice(0, closeLineStart) + rendered + newline + text.slice(closeLineStart);
375
+ }
376
+ function applyPathEntry(text, root, path, action, value, newline) {
377
+ let current = root;
378
+ for (let index = 0; index < path.length - 1; index++) {
379
+ current = findUnique(current.children, path[index].key, 'container');
380
+ }
381
+ const last = path[path.length - 1];
382
+ const occurrence = last.occurrence ?? 0;
383
+ const target = scanEntries(text, current).filter(entry => entry.key === last.key)[occurrence];
384
+ if (action === 'set') {
385
+ if (!target)
386
+ fail('could not find field ' + last.key + ' (occurrence ' + occurrence + ')');
387
+ const rendered = renderValue(value, current.indent + '\t', newline);
388
+ return text.slice(0, target.valueStart) + rendered + text.slice(target.valueEnd);
389
+ }
390
+ if (action === 'delete') {
391
+ if (!target)
392
+ fail('could not find field ' + last.key + ' (occurrence ' + occurrence + ')');
393
+ const lineStart = text.lastIndexOf('\n', target.valueStart - 1) + 1;
394
+ if (/^[ \t]*[A-Za-z_][A-Za-z0-9_.:@-]*[ \t]*=/.test(text.slice(lineStart, target.valueStart))) {
395
+ // The field starts its own line: drop the whole line, not just the value.
396
+ const lineEnd = text.indexOf('\n', target.valueEnd);
397
+ const cut = lineEnd === -1 ? text.length : lineEnd + 1;
398
+ return text.slice(0, lineStart) + text.slice(cut);
399
+ }
400
+ return text.slice(0, target.valueStart) + text.slice(target.valueEnd);
401
+ }
402
+ if (target && text[target.valueStart] !== '{') {
403
+ fail('add_definition_field target ' + last.key + ' is not a block');
404
+ }
405
+ return appendEntry(text, current, value, newline);
406
+ }
407
+ function validateOverride(override) {
408
+ const record = asRecordValue(override);
409
+ assertOnlyKeys(record, ['action', 'path', 'value', 'entry'], 'clone override');
410
+ if (record.action !== 'set' && record.action !== 'delete' && record.action !== 'append')
411
+ fail('unsupported clone override action');
412
+ validatePath(record.path, 'clone override');
413
+ if (record.action === 'set')
414
+ validateValue(record.value);
415
+ if (record.action === 'append')
416
+ validateEntries([record.entry]);
417
+ }
418
+ function countChangedLines(before, after) {
419
+ const beforeLines = before.split(/\r?\n/).length;
420
+ const afterLines = after.split(/\r?\n/).length;
421
+ return { added: Math.max(0, afterLines - beforeLines), removed: Math.max(0, beforeLines - afterLines) };
422
+ }
423
+ function buildTypedPdxCandidate(filePath, text, operation, expectedHash) {
424
+ if (!filePath.toLowerCase().endsWith('.txt'))
425
+ fail('only .txt files are supported');
426
+ if (typeof text !== 'string' || text.length > MAX_FILE_CHARS)
427
+ fail('file is missing or oversized');
428
+ const beforeHash = sha256Text(text);
429
+ if (expectedHash !== undefined && expectedHash !== beforeHash)
430
+ fail('expectedHash does not match current content');
431
+ const newline = text.includes('\r\n') ? '\r\n' : '\n';
432
+ const blocks = scanBlocks(text);
433
+ const kind = typeof operation.operation === 'string' ? operation.operation : '';
434
+ let target;
435
+ let content;
436
+ let summary;
437
+ if (kind === 'clone_definition') {
438
+ assertOnlyKeys(operation, ['operation', 'source', 'newSymbol', 'overrides'], 'clone_definition');
439
+ const source = operation.source;
440
+ assertIdentifier(source, 'source');
441
+ target = findUnique(blocks, source, 'source');
442
+ assertIdentifier(operation.newSymbol, 'newSymbol');
443
+ if (blocks.some(block => block.key === operation.newSymbol || block.symbol === operation.newSymbol))
444
+ fail('duplicate target: ' + operation.newSymbol);
445
+ const overrides = operation.overrides ?? [];
446
+ if (!Array.isArray(overrides))
447
+ fail('clone_definition overrides must be an array');
448
+ if (overrides.length > MAX_OVERRIDES)
449
+ fail('clone_definition supports at most ' + MAX_OVERRIDES + ' overrides');
450
+ for (const override of overrides)
451
+ validateOverride(override);
452
+ let clone = text.slice(target.start, target.end);
453
+ let cloneRoot = scanBlocks(clone)[0];
454
+ if (!cloneRoot)
455
+ fail('could not scan cloned definition');
456
+ const identity = scanEntries(clone, cloneRoot).find(entry => entry.key === 'id');
457
+ if (identity) {
458
+ const current = clone.slice(identity.valueStart, identity.valueEnd);
459
+ if (current !== (current.startsWith('"') ? quote(source) : source))
460
+ fail('could not rewrite id field for ' + source);
461
+ const replacement = current.startsWith('"') ? quote(operation.newSymbol) : operation.newSymbol;
462
+ clone = clone.slice(0, identity.valueStart) + replacement + clone.slice(identity.valueEnd);
463
+ }
464
+ else {
465
+ const sourceName = readIdentifier(clone, 0);
466
+ if (!sourceName || sourceName.value !== source)
467
+ fail('could not rewrite definition key');
468
+ clone = operation.newSymbol + clone.slice(sourceName.end);
469
+ }
470
+ for (const override of overrides) {
471
+ cloneRoot = scanBlocks(clone)[0];
472
+ if (!cloneRoot)
473
+ fail('could not rescan cloned definition');
474
+ clone = applyPathEntry(clone, cloneRoot, override.path, override.action, override.value ?? override.entry, newline);
475
+ }
476
+ const tail = /^[ \t]*(?:#[^\r\n]*)?(?:\r?\n|$)/.exec(text.slice(target.end))?.[0] ?? '';
477
+ const insertion = target.end + tail.length;
478
+ const separator = insertion > 0 && (text[insertion - 1] === '\n' || text[insertion - 1] === '\r') ? '' : newline;
479
+ const suffix = text.slice(insertion);
480
+ const trailingSeparator = suffix.length > 0 || text.endsWith(newline) ? newline : '';
481
+ content = text.slice(0, insertion) + separator + clone + trailingSeparator + suffix;
482
+ summary = 'cloned ' + source + ' as ' + operation.newSymbol;
483
+ }
484
+ else {
485
+ assertIdentifier(operation.target, 'target');
486
+ target = findUnique(blocks, operation.target, 'target');
487
+ if (kind === 'add_event_call') {
488
+ assertOnlyKeys(operation, ['operation', 'target', 'callType', 'eventId', 'containerPath', 'days'], 'add_event_call');
489
+ assertIdentifier(operation.callType, 'callType');
490
+ assertIdentifier(operation.eventId, 'eventId');
491
+ if (operation.days !== undefined && (!Number.isSafeInteger(operation.days) || operation.days < 0)) {
492
+ fail('days must be a non-negative integer');
493
+ }
494
+ const entries = [{ key: 'id', value: { kind: 'identifier', value: operation.eventId } }];
495
+ if (operation.days !== undefined)
496
+ entries.push({ key: 'days', value: { kind: 'number', value: operation.days } });
497
+ content = appendEntry(text, findContainer(target, operation.containerPath), {
498
+ key: operation.callType,
499
+ value: { kind: 'block', entries },
500
+ }, newline);
501
+ summary = 'added ' + operation.callType + ' call to ' + operation.eventId;
502
+ }
503
+ else if (kind === 'add_event_option') {
504
+ assertOnlyKeys(operation, ['operation', 'target', 'name', 'fields'], 'add_event_option');
505
+ assertIdentifier(operation.name, 'option name');
506
+ const fields = (operation.fields ?? []);
507
+ validateEntries(fields);
508
+ if (text.slice(target.open, target.close).includes('name = ' + operation.name))
509
+ fail('duplicate option name: ' + operation.name);
510
+ content = appendEntry(text, target, {
511
+ key: 'option',
512
+ value: { kind: 'block', entries: [{ key: 'name', value: { kind: 'identifier', value: operation.name } }, ...fields] },
513
+ }, newline);
514
+ summary = 'added option ' + operation.name + ' to ' + operation.target;
515
+ }
516
+ else if (kind === 'append_trigger_condition') {
517
+ assertOnlyKeys(operation, ['operation', 'target', 'condition', 'containerPath'], 'append_trigger_condition');
518
+ const conditions = [operation.condition];
519
+ validateEntries(conditions);
520
+ const condition = conditions[0];
521
+ content = appendEntry(text, findContainer(target, operation.containerPath ?? ['trigger']), condition, newline);
522
+ summary = 'appended trigger condition ' + condition.key;
523
+ }
524
+ else if (kind === 'instantiate_inline_script') {
525
+ assertOnlyKeys(operation, ['operation', 'target', 'script', 'arguments', 'containerPath'], 'instantiate_inline_script');
526
+ if (typeof operation.script !== 'string' || !INLINE_SCRIPT.test(operation.script)) {
527
+ fail('inline script must be a safe project-relative identifier');
528
+ }
529
+ const argumentsList = (operation.arguments ?? []);
530
+ validateEntries(argumentsList);
531
+ content = appendEntry(text, findContainer(target, operation.containerPath), {
532
+ key: 'inline_script',
533
+ value: { kind: 'block', entries: [
534
+ { key: 'script', value: { kind: 'string', value: operation.script } },
535
+ ...argumentsList,
536
+ ] },
537
+ }, newline);
538
+ summary = 'instantiated inline script ' + operation.script;
539
+ }
540
+ else if (kind === 'set_definition_field' || kind === 'delete_definition_field' || kind === 'add_definition_field') {
541
+ const allowed = kind === 'set_definition_field'
542
+ ? ['operation', 'target', 'path', 'value']
543
+ : kind === 'delete_definition_field'
544
+ ? ['operation', 'target', 'path']
545
+ : ['operation', 'target', 'path', 'entry'];
546
+ assertOnlyKeys(operation, allowed, kind);
547
+ const pathValue = operation.path;
548
+ validatePath(pathValue, kind);
549
+ if (kind === 'set_definition_field') {
550
+ const fieldValue = operation.value;
551
+ validateValue(fieldValue);
552
+ content = applyPathEntry(text, target, pathValue, 'set', fieldValue, newline);
553
+ summary = 'set field on ' + operation.target;
554
+ }
555
+ else if (kind === 'delete_definition_field') {
556
+ content = applyPathEntry(text, target, pathValue, 'delete', undefined, newline);
557
+ summary = 'deleted field from ' + operation.target;
558
+ }
559
+ else {
560
+ const fieldEntries = [operation.entry];
561
+ validateEntries(fieldEntries);
562
+ const fieldEntry = fieldEntries[0];
563
+ content = applyPathEntry(text, target, pathValue, 'append', fieldEntry, newline);
564
+ summary = 'added field ' + fieldEntry.key + ' to ' + operation.target;
565
+ }
566
+ }
567
+ else if (kind === 'add_scripted_effect_call' || kind === 'add_scripted_trigger_call') {
568
+ assertOnlyKeys(operation, ['operation', 'target', 'script', 'arguments', 'containerPath'], kind);
569
+ assertIdentifier(operation.script, 'script');
570
+ const argumentsList = (operation.arguments ?? []);
571
+ validateEntries(argumentsList);
572
+ content = appendEntry(text, findContainer(target, operation.containerPath), {
573
+ key: operation.script,
574
+ value: { kind: 'block', entries: argumentsList },
575
+ }, newline);
576
+ summary = 'added ' + (kind === 'add_scripted_effect_call' ? 'scripted effect' : 'scripted trigger') + ' call ' + operation.script;
577
+ }
578
+ else if (kind === 'add_on_action_entry') {
579
+ assertOnlyKeys(operation, ['operation', 'target', 'entry'], 'add_on_action_entry');
580
+ const onActionEntries = [operation.entry];
581
+ validateEntries(onActionEntries);
582
+ const onActionEntry = onActionEntries[0];
583
+ if (onActionEntry.key === 'id')
584
+ fail('add_on_action_entry cannot append identity');
585
+ content = appendEntry(text, target, onActionEntry, newline);
586
+ summary = 'added on-action entry ' + onActionEntry.key;
587
+ }
588
+ else if (kind === 'bind_event_target' || kind === 'clear_event_target') {
589
+ assertOnlyKeys(operation, ['operation', 'target', 'eventTarget', 'containerPath'], kind);
590
+ assertIdentifier(operation.eventTarget, 'eventTarget');
591
+ content = appendEntry(text, findContainer(target, operation.containerPath), {
592
+ key: kind === 'bind_event_target' ? 'save_event_target_as' : 'clear_event_target',
593
+ value: { kind: 'identifier', value: operation.eventTarget },
594
+ }, newline);
595
+ summary = (kind === 'bind_event_target' ? 'bound' : 'cleared') + ' event target ' + operation.eventTarget;
596
+ }
597
+ else if (kind === 'add_variable_transition') {
598
+ assertOnlyKeys(operation, ['operation', 'target', 'transition', 'variable', 'value', 'containerPath'], 'add_variable_transition');
599
+ const transition = String(operation.transition);
600
+ if (!['set_variable', 'change_variable', 'multiply_variable', 'divide_variable'].includes(transition)) {
601
+ fail('unsupported variable transition');
602
+ }
603
+ assertIdentifier(operation.variable, 'variable');
604
+ validateValue(operation.value);
605
+ content = appendEntry(text, findContainer(target, operation.containerPath), {
606
+ key: transition,
607
+ value: { kind: 'block', entries: [
608
+ { key: 'which', value: { kind: 'identifier', value: operation.variable } },
609
+ { key: 'value', value: operation.value },
610
+ ] },
611
+ }, newline);
612
+ summary = 'added ' + transition + ' transition for ' + operation.variable;
613
+ }
614
+ else {
615
+ fail('unsupported typed PDX operation; raw code is forbidden');
616
+ }
617
+ }
618
+ return {
619
+ filePath,
620
+ beforeHash,
621
+ contentHash: sha256Text(content),
622
+ content,
623
+ summary,
624
+ targetRange: { start: target.start, end: target.end },
625
+ operation,
626
+ diff: countChangedLines(text, content),
627
+ };
628
+ }