@fluidframework/ai-collab 2.33.2 → 2.40.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 (47) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/README.md +156 -0
  3. package/api-report/ai-collab.alpha.api.md +88 -0
  4. package/dist/aiCollabApi.d.ts +9 -0
  5. package/dist/aiCollabApi.d.ts.map +1 -1
  6. package/dist/aiCollabApi.js.map +1 -1
  7. package/dist/alpha.d.ts +12 -0
  8. package/dist/diffTypes.d.ts +200 -0
  9. package/dist/diffTypes.d.ts.map +1 -0
  10. package/dist/diffTypes.js +7 -0
  11. package/dist/diffTypes.js.map +1 -0
  12. package/dist/explicit-strategy/agentEditReducer.d.ts +25 -3
  13. package/dist/explicit-strategy/agentEditReducer.d.ts.map +1 -1
  14. package/dist/explicit-strategy/agentEditReducer.js +239 -15
  15. package/dist/explicit-strategy/agentEditReducer.js.map +1 -1
  16. package/dist/explicit-strategy/index.d.ts +3 -0
  17. package/dist/explicit-strategy/index.d.ts.map +1 -1
  18. package/dist/explicit-strategy/index.js +6 -2
  19. package/dist/explicit-strategy/index.js.map +1 -1
  20. package/dist/index.d.ts +1 -0
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/lib/aiCollabApi.d.ts +9 -0
  24. package/lib/aiCollabApi.d.ts.map +1 -1
  25. package/lib/aiCollabApi.js.map +1 -1
  26. package/lib/alpha.d.ts +12 -0
  27. package/lib/diffTypes.d.ts +200 -0
  28. package/lib/diffTypes.d.ts.map +1 -0
  29. package/lib/diffTypes.js +6 -0
  30. package/lib/diffTypes.js.map +1 -0
  31. package/lib/explicit-strategy/agentEditReducer.d.ts +25 -3
  32. package/lib/explicit-strategy/agentEditReducer.d.ts.map +1 -1
  33. package/lib/explicit-strategy/agentEditReducer.js +239 -18
  34. package/lib/explicit-strategy/agentEditReducer.js.map +1 -1
  35. package/lib/explicit-strategy/index.d.ts +3 -0
  36. package/lib/explicit-strategy/index.d.ts.map +1 -1
  37. package/lib/explicit-strategy/index.js +6 -2
  38. package/lib/explicit-strategy/index.js.map +1 -1
  39. package/lib/index.d.ts +1 -0
  40. package/lib/index.d.ts.map +1 -1
  41. package/lib/index.js.map +1 -1
  42. package/package.json +9 -9
  43. package/src/aiCollabApi.ts +10 -0
  44. package/src/diffTypes.ts +211 -0
  45. package/src/explicit-strategy/agentEditReducer.ts +296 -19
  46. package/src/explicit-strategy/index.ts +10 -2
  47. package/src/index.ts +15 -0
@@ -2,11 +2,33 @@
2
2
  * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
3
  * Licensed under the MIT License.
4
4
  */
5
- import { type TreeNode, type SimpleNodeSchema } from "@fluidframework/tree/internal";
6
- import { type TreeEdit } from "./agentEditTypes.js";
5
+ import { type TreeArrayNode, type TreeNode, type SimpleNodeSchema } from "@fluidframework/tree/internal";
6
+ import type { Diff } from "../diffTypes.js";
7
+ import { type TreeEdit, type Range, type TreeEditObject, type TreeEditValue } from "./agentEditTypes.js";
7
8
  import type { IdGenerator } from "./idGenerator.js";
9
+ /**
10
+ * Gets the schema identifier of the given content, including primitive values.
11
+ */
12
+ export declare function getSchemaIdentifier(content: TreeEditValue): string;
13
+ /**
14
+ * Converts a tree node from a {@link TreeEdit} to a {@link TreeEditObject} with the proper object IDs.
15
+ */
16
+ export declare function contentWithIds(content: TreeNode, idGenerator: IdGenerator): TreeEditObject;
8
17
  /**
9
18
  * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.
10
19
  */
11
- export declare function applyAgentEdit(treeEdit: TreeEdit, idGenerator: IdGenerator, definitionMap: ReadonlyMap<string, SimpleNodeSchema>, validator?: (edit: TreeNode) => void): TreeEdit;
20
+ export declare function applyAgentEdit(treeEdit: TreeEdit, idGenerator: IdGenerator, definitionMap: ReadonlyMap<string, SimpleNodeSchema>, validator?: (edit: TreeNode) => void): {
21
+ edit: TreeEdit;
22
+ diff: Diff;
23
+ };
24
+ interface RangeInfo {
25
+ array: TreeArrayNode;
26
+ startIndex: number;
27
+ endIndex: number;
28
+ }
29
+ /**
30
+ * Gets information about the range of nodes being targeted by an {@link Range}
31
+ */
32
+ export declare function getRangeInfo(range: Range, idGenerator: IdGenerator): RangeInfo;
33
+ export {};
12
34
  //# sourceMappingURL=agentEditReducer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"agentEditReducer.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAKN,KAAK,QAAQ,EAEb,KAAK,gBAAgB,EAOrB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,EACN,KAAK,QAAQ,EAUb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AA6DpD;;GAEG;AACH,wBAAgB,cAAc,CAC7B,QAAQ,EAAE,QAAQ,EAClB,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACpD,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,GAClC,QAAQ,CAwOV"}
1
+ {"version":3,"file":"agentEditReducer.d.ts","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH,OAAO,EAIN,KAAK,aAAa,EAClB,KAAK,QAAQ,EAEb,KAAK,gBAAgB,EAOrB,MAAM,+BAA+B,CAAC;AAGvC,OAAO,KAAK,EASX,IAAI,EACJ,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACN,KAAK,QAAQ,EAGb,KAAK,KAAK,EAGV,KAAK,cAAc,EACnB,KAAK,aAAa,EAMlB,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AA4BpD;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,aAAa,GAAG,MAAM,CA4BlE;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,WAAW,GAAG,cAAc,CAE1F;AAED;;GAEG;AACH,wBAAgB,cAAc,CAC7B,QAAQ,EAAE,QAAQ,EAClB,WAAW,EAAE,WAAW,EACxB,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,gBAAgB,CAAC,EACpD,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,QAAQ,KAAK,IAAI,GAClC;IAAE,IAAI,EAAE,QAAQ,CAAC;IAAC,IAAI,EAAE,IAAI,CAAA;CAAE,CAwPhC;AAkED,UAAU,SAAS;IAClB,KAAK,EAAE,aAAa,CAAC;IACrB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE,WAAW,GAAG,SAAS,CAW9E"}
@@ -4,7 +4,7 @@
4
4
  * Licensed under the MIT License.
5
5
  */
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.applyAgentEdit = void 0;
7
+ exports.getRangeInfo = exports.applyAgentEdit = exports.contentWithIds = exports.getSchemaIdentifier = void 0;
8
8
  const internal_1 = require("@fluidframework/core-utils/internal");
9
9
  const runtime_utils_1 = require("@fluidframework/runtime-utils");
10
10
  const internal_2 = require("@fluidframework/telemetry-utils/internal");
@@ -30,6 +30,9 @@ function populateDefaults(json, definitionMap) {
30
30
  }
31
31
  }
32
32
  }
33
+ /**
34
+ * Gets the schema identifier of the given content, including primitive values.
35
+ */
33
36
  function getSchemaIdentifier(content) {
34
37
  switch (typeof content) {
35
38
  case "boolean": {
@@ -58,9 +61,14 @@ function getSchemaIdentifier(content) {
58
61
  }
59
62
  }
60
63
  }
64
+ exports.getSchemaIdentifier = getSchemaIdentifier;
65
+ /**
66
+ * Converts a tree node from a {@link TreeEdit} to a {@link TreeEditObject} with the proper object IDs.
67
+ */
61
68
  function contentWithIds(content, idGenerator) {
62
69
  return JSON.parse((0, promptGeneration_js_1.toDecoratedJson)(idGenerator, content));
63
70
  }
71
+ exports.contentWithIds = contentWithIds;
64
72
  /**
65
73
  * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.
66
74
  */
@@ -83,8 +91,11 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
83
91
  validator?.(insertNode);
84
92
  array.insertAt(index, insertNode);
85
93
  return {
86
- ...treeEdit,
87
- content: contentWithIds(insertNode, idGenerator),
94
+ edit: {
95
+ ...treeEdit,
96
+ content: contentWithIds(insertNode, idGenerator),
97
+ },
98
+ diff: createInsertDiff(insertNode, treeEdit.explanation, idGenerator),
88
99
  };
89
100
  }
90
101
  }
@@ -92,6 +103,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
92
103
  }
93
104
  case "remove": {
94
105
  const source = treeEdit.source;
106
+ let diff;
95
107
  if (isObjectTarget(source)) {
96
108
  const node = getNodeFromTarget(source, idGenerator);
97
109
  const parentNode = internal_3.Tree.parent(node);
@@ -101,7 +113,9 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
101
113
  }
102
114
  else if (internal_3.Tree.schema(parentNode).kind === internal_3.NodeKind.Array) {
103
115
  const nodeIndex = internal_3.Tree.key(node);
104
- parentNode.removeAt(nodeIndex);
116
+ const parentArrayNode = parentNode;
117
+ diff = createRemoveDiff(treeEdit, idGenerator);
118
+ parentArrayNode.removeAt(nodeIndex);
105
119
  }
106
120
  else {
107
121
  const fieldKey = internal_3.Tree.key(node);
@@ -109,6 +123,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
109
123
  const fieldSchema = parentSchema.info[fieldKey] ??
110
124
  (0, utils_js_1.fail)("Expected field schema");
111
125
  if (fieldSchema instanceof internal_3.FieldSchema && fieldSchema.kind === internal_3.FieldKind.Optional) {
126
+ diff = createRemoveDiff(treeEdit, idGenerator);
112
127
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
113
128
  parentNode[fieldKey] = undefined;
114
129
  }
@@ -119,9 +134,13 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
119
134
  }
120
135
  else if (isRange(source)) {
121
136
  const { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);
137
+ diff = createRemoveDiff(treeEdit, idGenerator);
122
138
  array.removeRange(startIndex, endIndex);
123
139
  }
124
- return treeEdit;
140
+ else {
141
+ throw new internal_2.UsageError("Invalid source for remove edit");
142
+ }
143
+ return { edit: treeEdit, diff };
125
144
  }
126
145
  case "modify": {
127
146
  const node = getNodeFromTarget(treeEdit.target, idGenerator);
@@ -137,6 +156,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
137
156
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
138
157
  const schemaIdentifier = modification[agentEditTypes_js_1.typeField];
139
158
  let insertedObject;
159
+ const diff = createModifyDiff(treeEdit, idGenerator);
140
160
  // if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.
141
161
  if (isPrimitive(modification)) {
142
162
  try {
@@ -169,13 +189,9 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
169
189
  (0, internal_1.assert)(Array.isArray(field), 0xa75 /* the field must be an array node */);
170
190
  (0, internal_1.assert)(Array.isArray(constructedModification), 0xa76 /* the modification must be an array node */);
171
191
  field.removeRange(0);
172
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
173
- node[treeEdit.field] = constructedModification;
174
- }
175
- else {
176
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
177
- node[treeEdit.field] = constructedModification;
178
192
  }
193
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
194
+ node[treeEdit.field] = constructedModification;
179
195
  }
180
196
  // If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.
181
197
  else if (fieldSchema instanceof internal_3.FieldSchema) {
@@ -202,10 +218,13 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
202
218
  }
203
219
  }
204
220
  return insertedObject === undefined
205
- ? treeEdit
221
+ ? { edit: treeEdit, diff }
206
222
  : {
207
- ...treeEdit,
208
- modification: contentWithIds(insertedObject, idGenerator),
223
+ edit: {
224
+ ...treeEdit,
225
+ modification: contentWithIds(insertedObject, idGenerator),
226
+ },
227
+ diff,
209
228
  };
210
229
  }
211
230
  case "move": {
@@ -213,6 +232,7 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
213
232
  const source = treeEdit.source;
214
233
  const destination = treeEdit.destination;
215
234
  const { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(destination, idGenerator);
235
+ const diff = createMoveDiff(treeEdit, idGenerator);
216
236
  if (isObjectTarget(source)) {
217
237
  const sourceNode = getNodeFromTarget(source, idGenerator);
218
238
  const sourceIndex = internal_3.Tree.key(sourceNode);
@@ -249,7 +269,10 @@ function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator) {
249
269
  }
250
270
  destinationArrayNode.moveRangeToIndex(destinationIndex, sourceStartIndex, sourceEndIndex, array);
251
271
  }
252
- return treeEdit;
272
+ else {
273
+ throw new Error("Invalid source for move edit");
274
+ }
275
+ return { edit: treeEdit, diff };
253
276
  }
254
277
  default: {
255
278
  (0, utils_js_1.fail)("invalid tree edit");
@@ -309,6 +332,9 @@ function isObjectTarget(selection) {
309
332
  function isRange(selection) {
310
333
  return "from" in selection && "to" in selection;
311
334
  }
335
+ /**
336
+ * Gets information about the range of nodes being targeted by an {@link Range}
337
+ */
312
338
  function getRangeInfo(range, idGenerator) {
313
339
  const { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);
314
340
  const { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);
@@ -317,6 +343,7 @@ function getRangeInfo(range, idGenerator) {
317
343
  }
318
344
  return { array: arrayFrom, startIndex, endIndex };
319
345
  }
346
+ exports.getRangeInfo = getRangeInfo;
320
347
  function getPlaceInfo(place, idGenerator) {
321
348
  if (place.type === "arrayPlace") {
322
349
  const parent = idGenerator.getNode(place.parentId) ?? (0, utils_js_1.fail)("Expected parent node");
@@ -437,6 +464,203 @@ function assertObjectIdsExist(treeEdit, idGenerator) {
437
464
  }
438
465
  }
439
466
  }
467
+ const createNodePathRecursive = (node, idGenerator, currentPath) => {
468
+ if (node === undefined) {
469
+ return currentPath;
470
+ }
471
+ currentPath.push({
472
+ shortId: internal_3.Tree.shortId(node),
473
+ schemaIdentifier: internal_3.Tree.schema(node).identifier,
474
+ parentField: internal_3.Tree.key(node),
475
+ });
476
+ const parentNode = internal_3.Tree.parent(node);
477
+ return createNodePathRecursive(parentNode, idGenerator, currentPath);
478
+ };
479
+ /**
480
+ * Creates a diff for an Insert TreeEdit.
481
+ *
482
+ * @remarks
483
+ * This function is only invoked within the "insert" case block.
484
+ *
485
+ * This must only be called AFTER an insertion is made.
486
+ * It generates the insert diff after the node has been successfully inserted, as the node's index may
487
+ * be required to support undoing the insert operation, and we don't know that index until the insert has been made.
488
+ */
489
+ function createInsertDiff(newlyInsertedNode, aiExplanation, idGenerator) {
490
+ return {
491
+ type: "insert",
492
+ nodePath: createNodePathRecursive(newlyInsertedNode, idGenerator, []),
493
+ nodeContent: JSON.parse(JSON.stringify(newlyInsertedNode)),
494
+ aiExplanation,
495
+ };
496
+ }
497
+ /**
498
+ * Returns an object identical to the input except that the special 'objectIdKey' field (only intended for use by the LLM agent) is removed if present.
499
+ * @remarks The input object is not modified.
500
+ */
501
+ function removeAgentObjectIdField(oldValue) {
502
+ if (typeof oldValue === "object" && oldValue !== null && !Array.isArray(oldValue)) {
503
+ const { [agentEditTypes_js_1.objectIdKey]: _, ...rest } = oldValue;
504
+ return rest;
505
+ }
506
+ return oldValue;
507
+ }
508
+ /**
509
+ * Creates a diff for a Modify TreeEdit.
510
+ *
511
+ * @remarks
512
+ * This function must only be called BEFORE a modify edit is applied.
513
+ * For move operations, the diff is created before the node(s) have been successfully moved,
514
+ * since the original index is needed to restore the node(s) if the move operation need to undo.
515
+ */
516
+ function createModifyDiff(treeEdit, idGenerator) {
517
+ const targetNode = getNodeFromTarget(treeEdit.target, idGenerator);
518
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
519
+ const targetNodeAtField = targetNode[treeEdit.field];
520
+ if (isPrimitive(targetNodeAtField)) {
521
+ return {
522
+ type: "modify",
523
+ nodePath: createNodePathRecursive(targetNode, idGenerator, [
524
+ {
525
+ shortId: undefined,
526
+ parentField: treeEdit.field,
527
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
528
+ schemaIdentifier: getSchemaIdentifier(treeEdit.modification),
529
+ },
530
+ ]),
531
+ newValue: treeEdit.modification,
532
+ oldValue: targetNodeAtField,
533
+ aiExplanation: treeEdit.explanation,
534
+ };
535
+ }
536
+ return {
537
+ type: "modify",
538
+ nodePath: createNodePathRecursive(targetNodeAtField, idGenerator, []),
539
+ newValue: treeEdit.modification,
540
+ oldValue: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),
541
+ aiExplanation: treeEdit.explanation,
542
+ };
543
+ }
544
+ /**
545
+ * Creates a diff for a Remove TreeEdit.
546
+ *
547
+ * @remarks
548
+ * This function must only be called BEFORE a remove edit is applied.
549
+ * It generates the remove diff before the node has been successfully removed, as the node's index may
550
+ * be required to support undoing the remove operation, and we don't know that index until the remove has been made.
551
+ */
552
+ function createRemoveDiff(treeEdit, idGenerator) {
553
+ const source = treeEdit.source;
554
+ if (isObjectTarget(source)) {
555
+ const node = getNodeFromTarget(source, idGenerator);
556
+ const parentNode = internal_3.Tree.parent(node);
557
+ if (parentNode === undefined) {
558
+ throw new Error("Unexpectedly received a root node as the target of a remove edit");
559
+ }
560
+ else if (internal_3.Tree.schema(parentNode).kind === internal_3.NodeKind.Array) {
561
+ const nodeIndex = internal_3.Tree.key(node);
562
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
563
+ const targetRemovedNode = parentNode.at(nodeIndex);
564
+ if (isPrimitive(targetRemovedNode)) {
565
+ // Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly
566
+ throw new Error("Unexpectedly recieved a primitive node as the target of a remove edit");
567
+ }
568
+ return {
569
+ type: "remove",
570
+ removalType: "remove-array-single",
571
+ nodePath: createNodePathRecursive(targetRemovedNode, idGenerator, []),
572
+ aiExplanation: treeEdit.explanation,
573
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetRemovedNode))),
574
+ };
575
+ }
576
+ else {
577
+ const fieldKey = internal_3.Tree.key(node);
578
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
579
+ const targetNodeAtField = parentNode[fieldKey];
580
+ if (isPrimitive(targetNodeAtField)) {
581
+ // Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly
582
+ throw new Error("Unexpectedly recieved a primitive node as the target of a remove field edit");
583
+ }
584
+ return {
585
+ type: "remove",
586
+ removalType: "remove-node",
587
+ nodePath: createNodePathRecursive(targetNodeAtField, idGenerator, []),
588
+ aiExplanation: treeEdit.explanation,
589
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),
590
+ };
591
+ }
592
+ }
593
+ else if (isRange(source)) {
594
+ const { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);
595
+ const removedNodePaths = [];
596
+ const removedNodes = [];
597
+ for (let i = startIndex; i < endIndex; i++) {
598
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
599
+ const nodeToRemove = array.at(i);
600
+ if (!isPrimitive(nodeToRemove)) {
601
+ removedNodePaths.push(createNodePathRecursive(nodeToRemove, idGenerator, []));
602
+ removedNodes.push(nodeToRemove);
603
+ }
604
+ }
605
+ return {
606
+ type: "remove",
607
+ removalType: "remove-array-range",
608
+ nodePaths: removedNodePaths,
609
+ aiExplanation: treeEdit.explanation,
610
+ nodeContents: removedNodes.map((node) => removeAgentObjectIdField(JSON.parse(JSON.stringify(node)))),
611
+ };
612
+ }
613
+ else {
614
+ throw new Error("Invalid source encountered when trying to create diff for remove edit");
615
+ }
616
+ }
617
+ /**
618
+ * Creates a diff for a Move TreeEdit.
619
+ *
620
+ * @remarks
621
+ * This function must only be called BEFORE a move edit is applied.
622
+ * For move operations, the diff is created before the node(s) have been successfully moved,
623
+ * since the original index is needed to restore the node(s) if the move operation need to undo.
624
+ */
625
+ function createMoveDiff(treeEdit, idGenerator) {
626
+ const source = treeEdit.source;
627
+ const destination = treeEdit.destination;
628
+ const { array: destinationArrayNode } = getPlaceInfo(destination, idGenerator);
629
+ if (isObjectTarget(source)) {
630
+ const node = getNodeFromTarget(source, idGenerator);
631
+ return {
632
+ type: "move",
633
+ moveType: "move-single",
634
+ sourceNodePath: createNodePathRecursive(node, idGenerator, []),
635
+ destinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),
636
+ aiExplanation: treeEdit.explanation,
637
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(node))),
638
+ };
639
+ }
640
+ else if (isRange(source)) {
641
+ const { array, startIndex: sourceStartIndex, endIndex: sourceEndIndex, } = getRangeInfo(source, idGenerator);
642
+ const movedNodePaths = [];
643
+ const movedNodes = [];
644
+ for (let i = sourceStartIndex; i < sourceEndIndex; i++) {
645
+ const nodeToMove = array.at(i);
646
+ if (!isPrimitive(nodeToMove)) {
647
+ movedNodePaths.push(createNodePathRecursive(nodeToMove, idGenerator, []));
648
+ movedNodes.push(nodeToMove);
649
+ }
650
+ }
651
+ return {
652
+ type: "move",
653
+ moveType: "move-range",
654
+ sourceNodePaths: movedNodePaths,
655
+ destinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),
656
+ aiExplanation: treeEdit.explanation,
657
+ nodeContents: movedNodes.map((node) => removeAgentObjectIdField(JSON.parse(JSON.stringify(node)))),
658
+ };
659
+ }
660
+ else {
661
+ throw new Error("Invalid source for move edit");
662
+ }
663
+ }
440
664
  function getSimpleNodeSchema(node) {
441
665
  const treeNodeSchema = internal_3.Tree.schema(node);
442
666
  const simpleNodeSchema = treeNodeSchema;
@@ -1 +1 @@
1
- {"version":3,"file":"agentEditReducer.js","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,iEAA8D;AAC9D,uEAAoF;AACpF,4DAcuC;AACvC,6DAA8C;AAE9C,2DAW6B;AAG7B,+DAAwD;AACxD,yCAAkC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;IAEpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC5B,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAA,iBAAM,EACL,OAAO,IAAI,CAAC,6BAAS,CAAC,KAAK,QAAQ,EACnC,KAAK,CAAC,uDAAuD,CAC7D,CAAC;YACF,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,6BAAS,CAAC,CAAC,CAAC;YACtD,IAAA,iBAAM,EAAC,UAAU,EAAE,IAAI,KAAK,mBAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAClF,CAAC;IACF,CAAC;AACF,CAAC;AAED,SAAS,mBAAmB,CAAC,OAAsB;IAClD,QAAQ,OAAO,OAAO,EAAE,CAAC;QACxB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CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { isFluidError, UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tTree,\n\tNodeKind,\n\ttype ImplicitAllowedTypes,\n\ttype TreeArrayNode,\n\ttype TreeNode,\n\ttype TreeNodeSchema,\n\ttype SimpleNodeSchema,\n\tFieldKind,\n\tFieldSchema,\n\tnormalizeAllowedTypes,\n\ttype ImplicitFieldSchema,\n\ttype IterableTreeArrayContent,\n\tSchemaFactory,\n} from \"@fluidframework/tree/internal\";\nimport { closest } from \"fastest-levenshtein\";\n\nimport {\n\ttype TreeEdit,\n\ttype ObjectTarget,\n\ttype Selection,\n\ttype Range,\n\ttype ObjectPlace,\n\ttype ArrayPlace,\n\ttype TreeEditObject,\n\ttype TreeEditValue,\n\ttypeField,\n\ttype Modify,\n} from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport type { JsonValue } from \"./jsonTypes.js\";\nimport { toDecoratedJson } from \"./promptGeneration.js\";\nimport { fail } from \"./utils.js\";\n\nfunction populateDefaults(\n\tjson: JsonValue,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n): void {\n\tif (typeof json === \"object\") {\n\t\tif (json === null) {\n\t\t\treturn;\n\t\t}\n\t\tif (Array.isArray(json)) {\n\t\t\tfor (const element of json) {\n\t\t\t\tpopulateDefaults(element, definitionMap);\n\t\t\t}\n\t\t} else {\n\t\t\tassert(\n\t\t\t\ttypeof json[typeField] === \"string\",\n\t\t\t\t0xa73 /* The typeField must be present in new JSON content */,\n\t\t\t);\n\t\t\tconst nodeSchema = definitionMap.get(json[typeField]);\n\t\t\tassert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);\n\t\t}\n\t}\n}\n\nfunction getSchemaIdentifier(content: TreeEditValue): string | undefined {\n\tswitch (typeof content) {\n\t\tcase \"boolean\": {\n\t\t\treturn SchemaFactory.boolean.identifier;\n\t\t}\n\t\tcase \"number\": {\n\t\t\treturn SchemaFactory.number.identifier;\n\t\t}\n\t\tcase \"string\": {\n\t\t\treturn SchemaFactory.string.identifier;\n\t\t}\n\t\tcase \"object\": {\n\t\t\tif (content === null) {\n\t\t\t\treturn SchemaFactory.null.identifier;\n\t\t\t}\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\tthrow new UsageError(\"Arrays are not currently supported in this context\");\n\t\t\t}\n\t\t\tif (isFluidHandle(content)) {\n\t\t\t\treturn SchemaFactory.handle.identifier;\n\t\t\t}\n\t\t\treturn content[typeField];\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new UsageError(\"Unsupported content type\");\n\t\t}\n\t}\n}\n\nfunction contentWithIds(content: TreeNode, idGenerator: IdGenerator): TreeEditObject {\n\treturn JSON.parse(toDecoratedJson(idGenerator, content)) as TreeEditObject;\n}\n\n/**\n * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.\n */\nexport function applyAgentEdit(\n\ttreeEdit: TreeEdit,\n\tidGenerator: IdGenerator,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tvalidator?: (edit: TreeNode) => void,\n): TreeEdit {\n\tassertObjectIdsExist(treeEdit, idGenerator);\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tconst { array, index } = getPlaceInfo(treeEdit.destination, idGenerator);\n\n\t\t\tconst parentNodeSchema = Tree.schema(array);\n\t\t\tpopulateDefaults(treeEdit.content, definitionMap);\n\n\t\t\tconst schemaIdentifier = getSchemaIdentifier(treeEdit.content);\n\n\t\t\t// We assume that the parentNode for inserts edits are guaranteed to be an arrayNode.\n\t\t\tconst allowedTypes = [\n\t\t\t\t...normalizeAllowedTypes(parentNodeSchema.info as ImplicitAllowedTypes),\n\t\t\t];\n\n\t\t\tfor (const allowedType of allowedTypes.values()) {\n\t\t\t\tif (allowedType.identifier === schemaIdentifier && typeof allowedType === \"function\") {\n\t\t\t\t\tconst simpleNodeSchema = allowedType as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\t\tconst insertNode = new simpleNodeSchema(treeEdit.content);\n\t\t\t\t\tvalidator?.(insertNode);\n\t\t\t\t\tarray.insertAt(index, insertNode as unknown as IterableTreeArrayContent<never>);\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tcontent: contentWithIds(insertNode, idGenerator),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tfail(\"inserted node must be of an allowed type\");\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tconst source = treeEdit.source;\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst parentNode = Tree.parent(node);\n\t\t\t\t// Case for deleting rootNode\n\t\t\t\tif (parentNode === undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"The root is required, and cannot be removed. Please use modify edit instead.\",\n\t\t\t\t\t);\n\t\t\t\t} else if (Tree.schema(parentNode).kind === NodeKind.Array) {\n\t\t\t\t\tconst nodeIndex = Tree.key(node) as number;\n\t\t\t\t\t(parentNode as TreeArrayNode).removeAt(nodeIndex);\n\t\t\t\t} else {\n\t\t\t\t\tconst fieldKey = Tree.key(node);\n\t\t\t\t\tconst parentSchema = Tree.schema(parentNode);\n\t\t\t\t\tconst fieldSchema =\n\t\t\t\t\t\t(parentSchema.info as Record<string, ImplicitFieldSchema>)[fieldKey] ??\n\t\t\t\t\t\tfail(\"Expected field schema\");\n\t\t\t\t\tif (fieldSchema instanceof FieldSchema && fieldSchema.kind === FieldKind.Optional) {\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(parentNode as any)[fieldKey] = undefined;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${fieldKey} is required, and cannot be removed. Please use modify edit instead.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);\n\t\t\t\tarray.removeRange(startIndex, endIndex);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tconst node = getNodeFromTarget(treeEdit.target, idGenerator);\n\t\t\tconst { treeNodeSchema } = getSimpleNodeSchema(node);\n\n\t\t\tconst nodeFieldSchemas = treeNodeSchema.info as Record<string, ImplicitFieldSchema>;\n\n\t\t\tconst fieldSchema = nodeFieldSchemas[treeEdit.field];\n\n\t\t\t// If the LLM attempts to modify a field that does not exist in the target schema we generate a useful error message that can be used as part of the feedback loop.\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tconst errorMessage = createInvalidModifyFeedbackMsg(\n\t\t\t\t\ttreeEdit,\n\t\t\t\t\tnode,\n\t\t\t\t\t\"NONEXISTENT_FIELD\",\n\t\t\t\t);\n\t\t\t\tthrow new UsageError(errorMessage);\n\t\t\t}\n\n\t\t\tconst modification = treeEdit.modification;\n\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access\n\t\t\tconst schemaIdentifier = (modification as any)[typeField];\n\n\t\t\tlet insertedObject: TreeNode | undefined;\n\t\t\t// if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.\n\t\t\tif (isPrimitive(modification)) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (!isFluidError(error)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\t\t\t\t\t// If the LLM attempts to use the wrong type for a field, we generate a useful error message that can be used as part of the feedback loop.\n\t\t\t\t\tconst isInvalidTypeError =\n\t\t\t\t\t\terror.message.match(\n\t\t\t\t\t\t\t/The provided data is incompatible with all of the types allowed by the schema./,\n\t\t\t\t\t\t) !== null;\n\t\t\t\t\tif (isInvalidTypeError === true) {\n\t\t\t\t\t\tconst errorMessage = createInvalidModifyFeedbackMsg(\n\t\t\t\t\t\t\ttreeEdit,\n\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\"INVALID_TYPE\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthrow new UsageError(errorMessage);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the fieldSchema is a function we can grab the constructor and make an instance of that node.\n\t\t\telse if (typeof fieldSchema === \"function\") {\n\t\t\t\tconst simpleSchema = fieldSchema as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\tpopulateDefaults(modification, definitionMap);\n\t\t\t\tconst constructedModification = new simpleSchema(modification);\n\t\t\t\tvalidator?.(constructedModification);\n\t\t\t\tinsertedObject = constructedModification;\n\n\t\t\t\tif (Array.isArray(modification)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst field = (node as any)[treeEdit.field] as TreeArrayNode;\n\t\t\t\t\tassert(Array.isArray(field), 0xa75 /* the field must be an array node */);\n\t\t\t\t\tassert(\n\t\t\t\t\t\tArray.isArray(constructedModification),\n\t\t\t\t\t\t0xa76 /* the modification must be an array node */,\n\t\t\t\t\t);\n\t\t\t\t\tfield.removeRange(0);\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t} else {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.\n\t\t\telse if (fieldSchema instanceof FieldSchema) {\n\t\t\t\tif (fieldSchema.kind === FieldKind.Optional && modification === undefined) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = undefined;\n\t\t\t\t} else {\n\t\t\t\t\tfor (const allowedType of fieldSchema.allowedTypeSet.values()) {\n\t\t\t\t\t\tif (allowedType.identifier === schemaIdentifier) {\n\t\t\t\t\t\t\tif (typeof allowedType === \"function\") {\n\t\t\t\t\t\t\t\tconst simpleSchema = allowedType as unknown as new (\n\t\t\t\t\t\t\t\t\tdummy: unknown,\n\t\t\t\t\t\t\t\t) => TreeNode;\n\t\t\t\t\t\t\t\tconst constructedObject = new simpleSchema(modification);\n\t\t\t\t\t\t\t\tinsertedObject = constructedObject;\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = constructedObject;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn insertedObject === undefined\n\t\t\t\t? treeEdit\n\t\t\t\t: {\n\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\tmodification: contentWithIds(insertedObject, idGenerator),\n\t\t\t\t\t};\n\t\t}\n\t\tcase \"move\": {\n\t\t\t// TODO: need to add schema check for valid moves\n\t\t\tconst source = treeEdit.source;\n\t\t\tconst destination = treeEdit.destination;\n\t\t\tconst { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(\n\t\t\t\tdestination,\n\t\t\t\tidGenerator,\n\t\t\t);\n\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst sourceNode = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst sourceIndex = Tree.key(sourceNode) as number;\n\t\t\t\tconst sourceArrayNode = Tree.parent(sourceNode) as TreeArrayNode;\n\t\t\t\tconst sourceArraySchema = Tree.schema(sourceArrayNode);\n\t\t\t\tif (sourceArraySchema.kind !== NodeKind.Array) {\n\t\t\t\t\tthrow new UsageError(\"the source node must be within an arrayNode\");\n\t\t\t\t}\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tconst nodeToMove = sourceArrayNode.at(sourceIndex);\n\t\t\t\tassert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);\n\t\t\t\tif (isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\tsourceIndex,\n\t\t\t\t\t\tsourceIndex + 1,\n\t\t\t\t\t\tsourceArrayNode,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst {\n\t\t\t\t\tarray,\n\t\t\t\t\tstartIndex: sourceStartIndex,\n\t\t\t\t\tendIndex: sourceEndIndex,\n\t\t\t\t} = getRangeInfo(source, idGenerator);\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tfor (let i = sourceStartIndex; i < sourceEndIndex; i++) {\n\t\t\t\t\tconst nodeToMove = array.at(i);\n\t\t\t\t\tassert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);\n\t\t\t\t\tif (!isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\tdestinationIndex,\n\t\t\t\t\tsourceStartIndex,\n\t\t\t\t\tsourceEndIndex,\n\t\t\t\t\tarray,\n\t\t\t\t);\n\t\t\t}\n\t\t\treturn treeEdit;\n\t\t}\n\t\tdefault: {\n\t\t\tfail(\"invalid tree edit\");\n\t\t}\n\t}\n}\n\n/**\n * Produces a useful, context-rich error message to give as a response to the LLM when it has produced an {@link ModifyEdit} that either references a nonexistant field or an invalid type for the selected field.\n * @param errorType - The type of error message to produce. You must determine the error type before calling this function.\n * - `'NONEXISTENT_FIELD'` is used when the field does not exist in the node's schema.\n * - `'INVALID_TYPE'` is used when the field exists but the type of the modification is invalid.\n */\nfunction createInvalidModifyFeedbackMsg(\n\tmodifyEdit: Modify,\n\ttreeNode: TreeNode,\n\terrorType: \"NONEXISTENT_FIELD\" | \"INVALID_TYPE\",\n): string {\n\tconst { treeNodeSchema } = getSimpleNodeSchema(treeNode);\n\tconst nodeFieldSchemas = treeNodeSchema.info as Record<string, ImplicitFieldSchema>;\n\tconst messagePrefix = `You attempted an invalid modify edit on the node with id '${modifyEdit.target.target}' and schema '${treeNodeSchema.identifier}'.`;\n\tlet messageSuffix = \"\";\n\tconst getAllowedTypeIdentifiers = (fieldName: string): string[] => {\n\t\tconst targetFieldNodeSchema = nodeFieldSchemas[fieldName];\n\t\treturn targetFieldNodeSchema instanceof FieldSchema\n\t\t\t? [...targetFieldNodeSchema.allowedTypeSet.values()].map((schema) => schema.identifier)\n\t\t\t: [(targetFieldNodeSchema as TreeNodeSchema).identifier];\n\t};\n\n\tif (errorType === \"NONEXISTENT_FIELD\") {\n\t\tconst nodeFieldNames = Object.keys(nodeFieldSchemas);\n\t\tconst closestPossibleFieldMatch = closest(modifyEdit.field, nodeFieldNames);\n\t\tconst allowedTypeIdentifiers = getAllowedTypeIdentifiers(closestPossibleFieldMatch);\n\t\tconst closestPossibleMatchForFieldMessage = ` If you are sure you are trying to modify this node, did you mean to use the field \\`${closestPossibleFieldMatch}\\` which has the following set of allowed types: \\`[${allowedTypeIdentifiers.map((id) => `'${id}'`).join(\", \")}]\\`?`;\n\t\tmessageSuffix = ` The node's field you selected for modification \\`${modifyEdit.field}\\` does not exist in this node's schema. The set of available fields for this node are: \\`[${nodeFieldNames.map((field) => `'${field}'`).join(\", \")}]\\`.${closestPossibleMatchForFieldMessage}`;\n\t} else if (errorType === \"INVALID_TYPE\") {\n\t\tconst allowedTypeIdentifiers = getAllowedTypeIdentifiers(modifyEdit.field);\n\t\t// TODO: If the invalid modification is a new object, it won't be clear what part of the object is invalid for the given type. If we could give some more detailed guidance on what was wrong with the object it would be ideal.\n\t\tmessageSuffix = ` You cannot set the node's field \\`${modifyEdit.field}\\` to the value \\`${modifyEdit.modification}\\` with type \\`${typeof modifyEdit.modification}\\` because this type is incompatible with all of the types allowed by the field's schema. The set of allowed types are \\`[${allowedTypeIdentifiers.map((id) => `'${id}'`).join(\", \")}]\\`.`;\n\t}\n\n\treturn messagePrefix + messageSuffix;\n}\n\nfunction isNodeAllowedType(node: TreeNode, allowedTypes: TreeNodeSchema[]): boolean {\n\tfor (const allowedType of allowedTypes) {\n\t\tif (Tree.is(node, allowedType)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction isPrimitive(content: unknown): boolean {\n\treturn (\n\t\ttypeof content === \"number\" ||\n\t\ttypeof content === \"string\" ||\n\t\ttypeof content === \"boolean\" ||\n\t\tcontent === undefined ||\n\t\tcontent === null\n\t);\n}\n\nfunction isObjectTarget(selection: Selection): selection is ObjectTarget {\n\treturn Object.keys(selection).length === 1 && \"target\" in selection;\n}\n\nfunction isRange(selection: Selection): selection is Range {\n\treturn \"from\" in selection && \"to\" in selection;\n}\n\ninterface RangeInfo {\n\tarray: TreeArrayNode;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\nfunction getRangeInfo(range: Range, idGenerator: IdGenerator): RangeInfo {\n\tconst { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);\n\tconst { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);\n\n\tif (arrayFrom !== arrayTo) {\n\t\tthrow new UsageError(\n\t\t\t'The \"from\" node and \"to\" nodes of the range must be in the same parent array.',\n\t\t);\n\t}\n\n\treturn { array: arrayFrom, startIndex, endIndex };\n}\n\nfunction getPlaceInfo(\n\tplace: ObjectPlace | ArrayPlace,\n\tidGenerator: IdGenerator,\n): {\n\tarray: TreeArrayNode;\n\tindex: number;\n} {\n\tif (place.type === \"arrayPlace\") {\n\t\tconst parent = idGenerator.getNode(place.parentId) ?? fail(\"Expected parent node\");\n\t\tconst child = (parent as unknown as Record<string, unknown>)[place.field];\n\t\tif (child === undefined) {\n\t\t\tthrow new UsageError(`No child under field field`);\n\t\t}\n\t\tconst schema = Tree.schema(child as TreeNode);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: child as TreeArrayNode,\n\t\t\tindex: place.location === \"start\" ? 0 : (child as TreeArrayNode).length,\n\t\t};\n\t} else {\n\t\tconst node = getNodeFromTarget(place, idGenerator);\n\t\tconst nodeIndex = Tree.key(node);\n\t\tconst parent = Tree.parent(node);\n\t\tif (parent === undefined) {\n\t\t\tthrow new UsageError(\"TODO: root node target not supported\");\n\t\t}\n\t\tconst schema = Tree.schema(parent);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: parent as unknown as TreeArrayNode,\n\t\t\tindex: place.place === \"before\" ? (nodeIndex as number) : (nodeIndex as number) + 1,\n\t\t};\n\t}\n}\n\n/**\n * Returns the target node with the matching internal objectId using the provided {@link ObjectTarget}\n */\nfunction getNodeFromTarget(target: ObjectTarget, idGenerator: IdGenerator): TreeNode {\n\tconst node = idGenerator.getNode(target.target);\n\tassert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);\n\treturn node;\n}\n\n/**\n * Checks that the objectIds of the Tree Nodes within the givin the {@link TreeEdit} exist within the given {@link IdGenerator}\n *\n * @throws An {@link UsageError} if the objectIdKey does not exist in the {@link IdGenerator}\n */\nfunction assertObjectIdsExist(treeEdit: TreeEdit, idGenerator: IdGenerator): void {\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.target} does not exist`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.parentId} does not exist`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tthrow new UsageError(`objectIdKeys [${missingObjectIds}] does not exist`);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.source.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tif (idGenerator.getNode(treeEdit.target.target) === undefined) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.target.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"move\": {\n\t\t\tconst invalidObjectIds: string[] = [];\n\t\t\t// check the source\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tinvalidObjectIds.push(...missingObjectIds);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tinvalidObjectIds.push(treeEdit.source.target);\n\t\t\t}\n\n\t\t\t// check the destination\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.target);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.parentId);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (invalidObjectIds.length > 0) {\n\t\t\t\tthrow new UsageError(`objectIdKeys [${invalidObjectIds}] does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\ninterface SchemaInfo {\n\ttreeNodeSchema: TreeNodeSchema;\n\tsimpleNodeSchema: new (dummy: unknown) => TreeNode;\n}\n\nfunction getSimpleNodeSchema(node: TreeNode): SchemaInfo {\n\tconst treeNodeSchema = Tree.schema(node);\n\tconst simpleNodeSchema = treeNodeSchema as unknown as new (dummy: unknown) => TreeNode;\n\treturn { treeNodeSchema, simpleNodeSchema };\n}\n"]}
1
+ {"version":3,"file":"agentEditReducer.js","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,kEAA6D;AAC7D,iEAA8D;AAC9D,uEAAoF;AACpF,4DAcuC;AACvC,6DAA8C;AAc9C,2DAc6B;AAG7B,+DAAwD;AACxD,yCAAkC;AAElC,SAAS,gBAAgB,CACxB,IAAe,EACf,aAAoD;IAEpD,IAAI,OAAO,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC9B,IAAI,IAAI,KAAK,IAAI,EAAE,CAAC;YACnB,OAAO;QACR,CAAC;QACD,IAAI,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,CAAC;YACzB,KAAK,MAAM,OAAO,IAAI,IAAI,EAAE,CAAC;gBAC5B,gBAAgB,CAAC,OAAO,EAAE,aAAa,CAAC,CAAC;YAC1C,CAAC;QACF,CAAC;aAAM,CAAC;YACP,IAAA,iBAAM,EACL,OAAO,IAAI,CAAC,6BAAS,CAAC,KAAK,QAAQ,EACnC,KAAK,CAAC,uDAAuD,CAC7D,CAAC;YACF,MAAM,UAAU,GAAG,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,6BAAS,CAAC,CAAC,CAAC;YACtD,IAAA,iBAAM,EAAC,UAAU,EAAE,IAAI,KAAK,mBAAQ,CAAC,MAAM,EAAE,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAClF,CAAC;IACF,CAAC;AACF,CAAC;AAED;;GAEG;AACH,SAAgB,mBAAmB,CAAC,OAAsB;IACzD,QAAQ,OAAO,OAAO,EAAE,CAAC;QACxB,KAAK,SAAS,CAAC,CAAC,CAAC;YAChB,OAAO,wBAAa,CAAC,OAAO,CAAC,UAAU,CAAC;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EAAE,CAAC;AAC7C,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { assert } from \"@fluidframework/core-utils/internal\";\nimport { isFluidHandle } from \"@fluidframework/runtime-utils\";\nimport { isFluidError, UsageError } from \"@fluidframework/telemetry-utils/internal\";\nimport {\n\tTree,\n\tNodeKind,\n\ttype ImplicitAllowedTypes,\n\ttype TreeArrayNode,\n\ttype TreeNode,\n\ttype TreeNodeSchema,\n\ttype SimpleNodeSchema,\n\tFieldKind,\n\tFieldSchema,\n\tnormalizeAllowedTypes,\n\ttype ImplicitFieldSchema,\n\ttype IterableTreeArrayContent,\n\tSchemaFactory,\n} from \"@fluidframework/tree/internal\";\nimport { closest } from \"fastest-levenshtein\";\n\nimport type {\n\tArrayRangeRemoveDiff,\n\tArraySingleRemoveDiff,\n\tInsertDiff,\n\tModifyDiff,\n\tMoveRangeDiff,\n\tMoveSingleDiff,\n\tNodePath,\n\tRemoveNodeDiff,\n\tDiff,\n} from \"../diffTypes.js\";\n\nimport {\n\ttype TreeEdit,\n\ttype ObjectTarget,\n\ttype Selection,\n\ttype Range,\n\ttype ObjectPlace,\n\ttype ArrayPlace,\n\ttype TreeEditObject,\n\ttype TreeEditValue,\n\ttypeField,\n\ttype Modify,\n\ttype Remove,\n\ttype Move,\n\tobjectIdKey,\n} from \"./agentEditTypes.js\";\nimport type { IdGenerator } from \"./idGenerator.js\";\nimport type { JsonValue } from \"./jsonTypes.js\";\nimport { toDecoratedJson } from \"./promptGeneration.js\";\nimport { fail } from \"./utils.js\";\n\nfunction populateDefaults(\n\tjson: JsonValue,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n): void {\n\tif (typeof json === \"object\") {\n\t\tif (json === null) {\n\t\t\treturn;\n\t\t}\n\t\tif (Array.isArray(json)) {\n\t\t\tfor (const element of json) {\n\t\t\t\tpopulateDefaults(element, definitionMap);\n\t\t\t}\n\t\t} else {\n\t\t\tassert(\n\t\t\t\ttypeof json[typeField] === \"string\",\n\t\t\t\t0xa73 /* The typeField must be present in new JSON content */,\n\t\t\t);\n\t\t\tconst nodeSchema = definitionMap.get(json[typeField]);\n\t\t\tassert(nodeSchema?.kind === NodeKind.Object, 0xa74 /* Expected object schema */);\n\t\t}\n\t}\n}\n\n/**\n * Gets the schema identifier of the given content, including primitive values.\n */\nexport function getSchemaIdentifier(content: TreeEditValue): string {\n\tswitch (typeof content) {\n\t\tcase \"boolean\": {\n\t\t\treturn SchemaFactory.boolean.identifier;\n\t\t}\n\t\tcase \"number\": {\n\t\t\treturn SchemaFactory.number.identifier;\n\t\t}\n\t\tcase \"string\": {\n\t\t\treturn SchemaFactory.string.identifier;\n\t\t}\n\t\tcase \"object\": {\n\t\t\tif (content === null) {\n\t\t\t\treturn SchemaFactory.null.identifier;\n\t\t\t}\n\t\t\tif (Array.isArray(content)) {\n\t\t\t\tthrow new UsageError(\"Arrays are not currently supported in this context\");\n\t\t\t}\n\t\t\tif (isFluidHandle(content)) {\n\t\t\t\treturn SchemaFactory.handle.identifier;\n\t\t\t}\n\n\t\t\treturn content[typeField];\n\t\t}\n\t\tdefault: {\n\t\t\tthrow new UsageError(\"Unsupported content type\");\n\t\t}\n\t}\n}\n\n/**\n * Converts a tree node from a {@link TreeEdit} to a {@link TreeEditObject} with the proper object IDs.\n */\nexport function contentWithIds(content: TreeNode, idGenerator: IdGenerator): TreeEditObject {\n\treturn JSON.parse(toDecoratedJson(idGenerator, content)) as TreeEditObject;\n}\n\n/**\n * Manages applying the various types of {@link TreeEdit}'s to a a given {@link TreeNode}.\n */\nexport function applyAgentEdit(\n\ttreeEdit: TreeEdit,\n\tidGenerator: IdGenerator,\n\tdefinitionMap: ReadonlyMap<string, SimpleNodeSchema>,\n\tvalidator?: (edit: TreeNode) => void,\n): { edit: TreeEdit; diff: Diff } {\n\tassertObjectIdsExist(treeEdit, idGenerator);\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tconst { array, index } = getPlaceInfo(treeEdit.destination, idGenerator);\n\n\t\t\tconst parentNodeSchema = Tree.schema(array);\n\t\t\tpopulateDefaults(treeEdit.content, definitionMap);\n\n\t\t\tconst schemaIdentifier = getSchemaIdentifier(treeEdit.content);\n\n\t\t\t// We assume that the parentNode for inserts edits are guaranteed to be an arrayNode.\n\t\t\tconst allowedTypes = [\n\t\t\t\t...normalizeAllowedTypes(parentNodeSchema.info as ImplicitAllowedTypes),\n\t\t\t];\n\n\t\t\tfor (const allowedType of allowedTypes.values()) {\n\t\t\t\tif (allowedType.identifier === schemaIdentifier && typeof allowedType === \"function\") {\n\t\t\t\t\tconst simpleNodeSchema = allowedType as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\t\tconst insertNode = new simpleNodeSchema(treeEdit.content);\n\t\t\t\t\tvalidator?.(insertNode);\n\n\t\t\t\t\tarray.insertAt(index, insertNode as unknown as IterableTreeArrayContent<never>);\n\t\t\t\t\treturn {\n\t\t\t\t\t\tedit: {\n\t\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\t\tcontent: contentWithIds(insertNode, idGenerator),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdiff: createInsertDiff(insertNode, treeEdit.explanation, idGenerator),\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t\tfail(\"inserted node must be of an allowed type\");\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tconst source = treeEdit.source;\n\t\t\tlet diff: RemoveNodeDiff | ArraySingleRemoveDiff | ArrayRangeRemoveDiff;\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst parentNode = Tree.parent(node);\n\t\t\t\t// Case for deleting rootNode\n\t\t\t\tif (parentNode === undefined) {\n\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\"The root is required, and cannot be removed. Please use modify edit instead.\",\n\t\t\t\t\t);\n\t\t\t\t} else if (Tree.schema(parentNode).kind === NodeKind.Array) {\n\t\t\t\t\tconst nodeIndex = Tree.key(node) as number;\n\t\t\t\t\tconst parentArrayNode = parentNode as TreeArrayNode;\n\t\t\t\t\tdiff = createRemoveDiff(treeEdit, idGenerator);\n\t\t\t\t\tparentArrayNode.removeAt(nodeIndex);\n\t\t\t\t} else {\n\t\t\t\t\tconst fieldKey = Tree.key(node);\n\t\t\t\t\tconst parentSchema = Tree.schema(parentNode);\n\t\t\t\t\tconst fieldSchema =\n\t\t\t\t\t\t(parentSchema.info as Record<string, ImplicitFieldSchema>)[fieldKey] ??\n\t\t\t\t\t\tfail(\"Expected field schema\");\n\t\t\t\t\tif (fieldSchema instanceof FieldSchema && fieldSchema.kind === FieldKind.Optional) {\n\t\t\t\t\t\tdiff = createRemoveDiff(treeEdit, idGenerator);\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t(parentNode as any)[fieldKey] = undefined;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tthrow new UsageError(\n\t\t\t\t\t\t\t`${fieldKey} is required, and cannot be removed. Please use modify edit instead.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);\n\t\t\t\tdiff = createRemoveDiff(treeEdit, idGenerator);\n\t\t\t\tarray.removeRange(startIndex, endIndex);\n\t\t\t} else {\n\t\t\t\tthrow new UsageError(\"Invalid source for remove edit\");\n\t\t\t}\n\n\t\t\treturn { edit: treeEdit, diff };\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tconst node = getNodeFromTarget(treeEdit.target, idGenerator);\n\t\t\tconst { treeNodeSchema } = getSimpleNodeSchema(node);\n\n\t\t\tconst nodeFieldSchemas = treeNodeSchema.info as Record<string, ImplicitFieldSchema>;\n\n\t\t\tconst fieldSchema = nodeFieldSchemas[treeEdit.field];\n\n\t\t\t// If the LLM attempts to modify a field that does not exist in the target schema we generate a useful error message that can be used as part of the feedback loop.\n\t\t\tif (fieldSchema === undefined) {\n\t\t\t\tconst errorMessage = createInvalidModifyFeedbackMsg(\n\t\t\t\t\ttreeEdit,\n\t\t\t\t\tnode,\n\t\t\t\t\t\"NONEXISTENT_FIELD\",\n\t\t\t\t);\n\t\t\t\tthrow new UsageError(errorMessage);\n\t\t\t}\n\n\t\t\tconst modification = treeEdit.modification;\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access\n\t\t\tconst schemaIdentifier = (modification as any)[typeField];\n\n\t\t\tlet insertedObject: TreeNode | undefined;\n\t\t\tconst diff = createModifyDiff(treeEdit, idGenerator);\n\t\t\t// if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.\n\t\t\tif (isPrimitive(modification)) {\n\t\t\t\ttry {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t} catch (error) {\n\t\t\t\t\tif (!isFluidError(error)) {\n\t\t\t\t\t\tthrow error;\n\t\t\t\t\t}\n\t\t\t\t\t// If the LLM attempts to use the wrong type for a field, we generate a useful error message that can be used as part of the feedback loop.\n\t\t\t\t\tconst isInvalidTypeError =\n\t\t\t\t\t\terror.message.match(\n\t\t\t\t\t\t\t/The provided data is incompatible with all of the types allowed by the schema./,\n\t\t\t\t\t\t) !== null;\n\t\t\t\t\tif (isInvalidTypeError === true) {\n\t\t\t\t\t\tconst errorMessage = createInvalidModifyFeedbackMsg(\n\t\t\t\t\t\t\ttreeEdit,\n\t\t\t\t\t\t\tnode,\n\t\t\t\t\t\t\t\"INVALID_TYPE\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthrow new UsageError(errorMessage);\n\t\t\t\t\t}\n\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t}\n\t\t\t// If the fieldSchema is a function we can grab the constructor and make an instance of that node.\n\t\t\telse if (typeof fieldSchema === \"function\") {\n\t\t\t\tconst simpleSchema = fieldSchema as unknown as new (dummy: unknown) => TreeNode;\n\t\t\t\tpopulateDefaults(modification, definitionMap);\n\t\t\t\tconst constructedModification = new simpleSchema(modification);\n\t\t\t\tvalidator?.(constructedModification);\n\n\t\t\t\tinsertedObject = constructedModification;\n\n\t\t\t\tif (Array.isArray(modification)) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\tconst field = (node as any)[treeEdit.field] as TreeArrayNode;\n\t\t\t\t\tassert(Array.isArray(field), 0xa75 /* the field must be an array node */);\n\t\t\t\t\tassert(\n\t\t\t\t\t\tArray.isArray(constructedModification),\n\t\t\t\t\t\t0xa76 /* the modification must be an array node */,\n\t\t\t\t\t);\n\t\t\t\t\tfield.removeRange(0);\n\t\t\t\t}\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t(node as any)[treeEdit.field] = constructedModification;\n\t\t\t}\n\t\t\t// If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.\n\t\t\telse if (fieldSchema instanceof FieldSchema) {\n\t\t\t\tif (fieldSchema.kind === FieldKind.Optional && modification === undefined) {\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t(node as any)[treeEdit.field] = undefined;\n\t\t\t\t} else {\n\t\t\t\t\tfor (const allowedType of fieldSchema.allowedTypeSet.values()) {\n\t\t\t\t\t\tif (allowedType.identifier === schemaIdentifier) {\n\t\t\t\t\t\t\tif (typeof allowedType === \"function\") {\n\t\t\t\t\t\t\t\tconst simpleSchema = allowedType as unknown as new (\n\t\t\t\t\t\t\t\t\tdummy: unknown,\n\t\t\t\t\t\t\t\t) => TreeNode;\n\t\t\t\t\t\t\t\tconst constructedObject = new simpleSchema(modification);\n\t\t\t\t\t\t\t\tinsertedObject = constructedObject;\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = constructedObject;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\t\t\t\t\t\t(node as any)[treeEdit.field] = modification;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn insertedObject === undefined\n\t\t\t\t? { edit: treeEdit, diff }\n\t\t\t\t: {\n\t\t\t\t\t\tedit: {\n\t\t\t\t\t\t\t...treeEdit,\n\t\t\t\t\t\t\tmodification: contentWithIds(insertedObject, idGenerator),\n\t\t\t\t\t\t},\n\t\t\t\t\t\tdiff,\n\t\t\t\t\t};\n\t\t}\n\t\tcase \"move\": {\n\t\t\t// TODO: need to add schema check for valid moves\n\t\t\tconst source = treeEdit.source;\n\t\t\tconst destination = treeEdit.destination;\n\t\t\tconst { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(\n\t\t\t\tdestination,\n\t\t\t\tidGenerator,\n\t\t\t);\n\t\t\tconst diff: MoveSingleDiff | MoveRangeDiff = createMoveDiff(treeEdit, idGenerator);\n\t\t\tif (isObjectTarget(source)) {\n\t\t\t\tconst sourceNode = getNodeFromTarget(source, idGenerator);\n\t\t\t\tconst sourceIndex = Tree.key(sourceNode) as number;\n\t\t\t\tconst sourceArrayNode = Tree.parent(sourceNode) as TreeArrayNode;\n\t\t\t\tconst sourceArraySchema = Tree.schema(sourceArrayNode);\n\t\t\t\tif (sourceArraySchema.kind !== NodeKind.Array) {\n\t\t\t\t\tthrow new UsageError(\"the source node must be within an arrayNode\");\n\t\t\t\t}\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tconst nodeToMove = sourceArrayNode.at(sourceIndex);\n\t\t\t\tassert(nodeToMove !== undefined, 0xa77 /* node to move must exist */);\n\t\t\t\tif (isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\t\tdestinationIndex,\n\t\t\t\t\t\tsourceIndex,\n\t\t\t\t\t\tsourceIndex + 1,\n\t\t\t\t\t\tsourceArrayNode,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t}\n\t\t\t} else if (isRange(source)) {\n\t\t\t\tconst {\n\t\t\t\t\tarray,\n\t\t\t\t\tstartIndex: sourceStartIndex,\n\t\t\t\t\tendIndex: sourceEndIndex,\n\t\t\t\t} = getRangeInfo(source, idGenerator);\n\t\t\t\tconst destinationArraySchema = Tree.schema(destinationArrayNode);\n\t\t\t\tconst allowedTypes = [\n\t\t\t\t\t...normalizeAllowedTypes(destinationArraySchema.info as ImplicitAllowedTypes),\n\t\t\t\t];\n\t\t\t\tfor (let i = sourceStartIndex; i < sourceEndIndex; i++) {\n\t\t\t\t\tconst nodeToMove = array.at(i);\n\t\t\t\t\tassert(nodeToMove !== undefined, 0xa78 /* node to move must exist */);\n\t\t\t\t\tif (!isNodeAllowedType(nodeToMove as TreeNode, allowedTypes)) {\n\t\t\t\t\t\tthrow new UsageError(\"Illegal node type in destination array\");\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdestinationArrayNode.moveRangeToIndex(\n\t\t\t\t\tdestinationIndex,\n\t\t\t\t\tsourceStartIndex,\n\t\t\t\t\tsourceEndIndex,\n\t\t\t\t\tarray,\n\t\t\t\t);\n\t\t\t} else {\n\t\t\t\tthrow new Error(\"Invalid source for move edit\");\n\t\t\t}\n\t\t\treturn { edit: treeEdit, diff };\n\t\t}\n\t\tdefault: {\n\t\t\tfail(\"invalid tree edit\");\n\t\t}\n\t}\n}\n\n/**\n * Produces a useful, context-rich error message to give as a response to the LLM when it has produced an {@link ModifyEdit} that either references a nonexistant field or an invalid type for the selected field.\n * @param errorType - The type of error message to produce. You must determine the error type before calling this function.\n * - `'NONEXISTENT_FIELD'` is used when the field does not exist in the node's schema.\n * - `'INVALID_TYPE'` is used when the field exists but the type of the modification is invalid.\n */\nfunction createInvalidModifyFeedbackMsg(\n\tmodifyEdit: Modify,\n\ttreeNode: TreeNode,\n\terrorType: \"NONEXISTENT_FIELD\" | \"INVALID_TYPE\",\n): string {\n\tconst { treeNodeSchema } = getSimpleNodeSchema(treeNode);\n\tconst nodeFieldSchemas = treeNodeSchema.info as Record<string, ImplicitFieldSchema>;\n\tconst messagePrefix = `You attempted an invalid modify edit on the node with id '${modifyEdit.target.target}' and schema '${treeNodeSchema.identifier}'.`;\n\tlet messageSuffix = \"\";\n\tconst getAllowedTypeIdentifiers = (fieldName: string): string[] => {\n\t\tconst targetFieldNodeSchema = nodeFieldSchemas[fieldName];\n\t\treturn targetFieldNodeSchema instanceof FieldSchema\n\t\t\t? [...targetFieldNodeSchema.allowedTypeSet.values()].map((schema) => schema.identifier)\n\t\t\t: [(targetFieldNodeSchema as TreeNodeSchema).identifier];\n\t};\n\n\tif (errorType === \"NONEXISTENT_FIELD\") {\n\t\tconst nodeFieldNames = Object.keys(nodeFieldSchemas);\n\t\tconst closestPossibleFieldMatch = closest(modifyEdit.field, nodeFieldNames);\n\t\tconst allowedTypeIdentifiers = getAllowedTypeIdentifiers(closestPossibleFieldMatch);\n\t\tconst closestPossibleMatchForFieldMessage = ` If you are sure you are trying to modify this node, did you mean to use the field \\`${closestPossibleFieldMatch}\\` which has the following set of allowed types: \\`[${allowedTypeIdentifiers.map((id) => `'${id}'`).join(\", \")}]\\`?`;\n\t\tmessageSuffix = ` The node's field you selected for modification \\`${modifyEdit.field}\\` does not exist in this node's schema. The set of available fields for this node are: \\`[${nodeFieldNames.map((field) => `'${field}'`).join(\", \")}]\\`.${closestPossibleMatchForFieldMessage}`;\n\t} else if (errorType === \"INVALID_TYPE\") {\n\t\tconst allowedTypeIdentifiers = getAllowedTypeIdentifiers(modifyEdit.field);\n\t\t// TODO: If the invalid modification is a new object, it won't be clear what part of the object is invalid for the given type. If we could give some more detailed guidance on what was wrong with the object it would be ideal.\n\t\tmessageSuffix = ` You cannot set the node's field \\`${modifyEdit.field}\\` to the value \\`${modifyEdit.modification}\\` with type \\`${typeof modifyEdit.modification}\\` because this type is incompatible with all of the types allowed by the field's schema. The set of allowed types are \\`[${allowedTypeIdentifiers.map((id) => `'${id}'`).join(\", \")}]\\`.`;\n\t}\n\n\treturn messagePrefix + messageSuffix;\n}\n\nfunction isNodeAllowedType(node: TreeNode, allowedTypes: TreeNodeSchema[]): boolean {\n\tfor (const allowedType of allowedTypes) {\n\t\tif (Tree.is(node, allowedType)) {\n\t\t\treturn true;\n\t\t}\n\t}\n\treturn false;\n}\n\nfunction isPrimitive(content: unknown): boolean {\n\treturn (\n\t\ttypeof content === \"number\" ||\n\t\ttypeof content === \"string\" ||\n\t\ttypeof content === \"boolean\" ||\n\t\tcontent === undefined ||\n\t\tcontent === null\n\t);\n}\n\nfunction isObjectTarget(selection: Selection): selection is ObjectTarget {\n\treturn Object.keys(selection).length === 1 && \"target\" in selection;\n}\n\nfunction isRange(selection: Selection): selection is Range {\n\treturn \"from\" in selection && \"to\" in selection;\n}\n\ninterface RangeInfo {\n\tarray: TreeArrayNode;\n\tstartIndex: number;\n\tendIndex: number;\n}\n\n/**\n * Gets information about the range of nodes being targeted by an {@link Range}\n */\nexport function getRangeInfo(range: Range, idGenerator: IdGenerator): RangeInfo {\n\tconst { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);\n\tconst { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);\n\n\tif (arrayFrom !== arrayTo) {\n\t\tthrow new UsageError(\n\t\t\t'The \"from\" node and \"to\" nodes of the range must be in the same parent array.',\n\t\t);\n\t}\n\n\treturn { array: arrayFrom, startIndex, endIndex };\n}\n\nfunction getPlaceInfo(\n\tplace: ObjectPlace | ArrayPlace,\n\tidGenerator: IdGenerator,\n): {\n\tarray: TreeArrayNode;\n\tindex: number;\n} {\n\tif (place.type === \"arrayPlace\") {\n\t\tconst parent = idGenerator.getNode(place.parentId) ?? fail(\"Expected parent node\");\n\t\tconst child = (parent as unknown as Record<string, unknown>)[place.field];\n\t\tif (child === undefined) {\n\t\t\tthrow new UsageError(`No child under field field`);\n\t\t}\n\t\tconst schema = Tree.schema(child as TreeNode);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: child as TreeArrayNode,\n\t\t\tindex: place.location === \"start\" ? 0 : (child as TreeArrayNode).length,\n\t\t};\n\t} else {\n\t\tconst node = getNodeFromTarget(place, idGenerator);\n\t\tconst nodeIndex = Tree.key(node);\n\t\tconst parent = Tree.parent(node);\n\t\tif (parent === undefined) {\n\t\t\tthrow new UsageError(\"TODO: root node target not supported\");\n\t\t}\n\t\tconst schema = Tree.schema(parent);\n\t\tif (schema.kind !== NodeKind.Array) {\n\t\t\tthrow new UsageError(\"Expected child to be in an array node\");\n\t\t}\n\t\treturn {\n\t\t\tarray: parent as unknown as TreeArrayNode,\n\t\t\tindex: place.place === \"before\" ? (nodeIndex as number) : (nodeIndex as number) + 1,\n\t\t};\n\t}\n}\n\n/**\n * Returns the target node with the matching internal objectId using the provided {@link ObjectTarget}\n */\nfunction getNodeFromTarget(target: ObjectTarget, idGenerator: IdGenerator): TreeNode {\n\tconst node = idGenerator.getNode(target.target);\n\tassert(node !== undefined, 0xa79 /* objectId does not exist in nodeMap */);\n\treturn node;\n}\n\n/**\n * Checks that the objectIds of the Tree Nodes within the givin the {@link TreeEdit} exist within the given {@link IdGenerator}\n *\n * @throws An {@link UsageError} if the objectIdKey does not exist in the {@link IdGenerator}\n */\nfunction assertObjectIdsExist(treeEdit: TreeEdit, idGenerator: IdGenerator): void {\n\tswitch (treeEdit.type) {\n\t\tcase \"insert\": {\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.target} does not exist`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.destination.parentId} does not exist`);\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"remove\": {\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tthrow new UsageError(`objectIdKeys [${missingObjectIds}] does not exist`);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.source.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"modify\": {\n\t\t\tif (idGenerator.getNode(treeEdit.target.target) === undefined) {\n\t\t\t\tthrow new UsageError(`objectIdKey ${treeEdit.target.target} does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tcase \"move\": {\n\t\t\tconst invalidObjectIds: string[] = [];\n\t\t\t// check the source\n\t\t\tif (isRange(treeEdit.source)) {\n\t\t\t\tconst missingObjectIds = [\n\t\t\t\t\ttreeEdit.source.from.target,\n\t\t\t\t\ttreeEdit.source.to.target,\n\t\t\t\t].filter((id) => !idGenerator.getNode(id));\n\n\t\t\t\tif (missingObjectIds.length > 0) {\n\t\t\t\t\tinvalidObjectIds.push(...missingObjectIds);\n\t\t\t\t}\n\t\t\t} else if (\n\t\t\t\tisObjectTarget(treeEdit.source) &&\n\t\t\t\tidGenerator.getNode(treeEdit.source.target) === undefined\n\t\t\t) {\n\t\t\t\tinvalidObjectIds.push(treeEdit.source.target);\n\t\t\t}\n\n\t\t\t// check the destination\n\t\t\tif (treeEdit.destination.type === \"objectPlace\") {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.target) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.target);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tif (idGenerator.getNode(treeEdit.destination.parentId) === undefined) {\n\t\t\t\t\tinvalidObjectIds.push(treeEdit.destination.parentId);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (invalidObjectIds.length > 0) {\n\t\t\t\tthrow new UsageError(`objectIdKeys [${invalidObjectIds}] does not exist`);\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t\tdefault: {\n\t\t\tbreak;\n\t\t}\n\t}\n}\n\nconst createNodePathRecursive = (\n\tnode: TreeNode | undefined,\n\tidGenerator: IdGenerator,\n\tcurrentPath: NodePath,\n): NodePath => {\n\tif (node === undefined) {\n\t\treturn currentPath;\n\t}\n\n\tcurrentPath.push({\n\t\tshortId: Tree.shortId(node),\n\t\tschemaIdentifier: Tree.schema(node).identifier,\n\t\tparentField: Tree.key(node),\n\t});\n\n\tconst parentNode = Tree.parent(node);\n\treturn createNodePathRecursive(parentNode, idGenerator, currentPath);\n};\n\n/**\n * Creates a diff for an Insert TreeEdit.\n *\n * @remarks\n * This function is only invoked within the \"insert\" case block.\n *\n * This must only be called AFTER an insertion is made.\n * It generates the insert diff after the node has been successfully inserted, as the node's index may\n * be required to support undoing the insert operation, and we don't know that index until the insert has been made.\n */\nfunction createInsertDiff(\n\tnewlyInsertedNode: TreeNode,\n\taiExplanation: string,\n\tidGenerator: IdGenerator,\n): InsertDiff {\n\treturn {\n\t\ttype: \"insert\",\n\t\tnodePath: createNodePathRecursive(newlyInsertedNode, idGenerator, []),\n\t\tnodeContent: JSON.parse(JSON.stringify(newlyInsertedNode)),\n\t\taiExplanation,\n\t};\n}\n\n/**\n * Returns an object identical to the input except that the special 'objectIdKey' field (only intended for use by the LLM agent) is removed if present.\n * @remarks The input object is not modified.\n */\nfunction removeAgentObjectIdField(oldValue: unknown): unknown {\n\tif (typeof oldValue === \"object\" && oldValue !== null && !Array.isArray(oldValue)) {\n\t\tconst { [objectIdKey]: _, ...rest } = oldValue as Record<string, unknown>;\n\t\treturn rest;\n\t}\n\treturn oldValue;\n}\n\n/**\n * Creates a diff for a Modify TreeEdit.\n *\n * @remarks\n * This function must only be called BEFORE a modify edit is applied.\n * For move operations, the diff is created before the node(s) have been successfully moved,\n * since the original index is needed to restore the node(s) if the move operation need to undo.\n */\nfunction createModifyDiff(treeEdit: Modify, idGenerator: IdGenerator): ModifyDiff {\n\tconst targetNode = getNodeFromTarget(treeEdit.target, idGenerator);\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\tconst targetNodeAtField: unknown = (targetNode as any)[treeEdit.field];\n\n\tif (isPrimitive(targetNodeAtField)) {\n\t\treturn {\n\t\t\ttype: \"modify\",\n\t\t\tnodePath: createNodePathRecursive(targetNode, idGenerator, [\n\t\t\t\t{\n\t\t\t\t\tshortId: undefined,\n\t\t\t\t\tparentField: treeEdit.field,\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\t\tschemaIdentifier: getSchemaIdentifier(treeEdit.modification)!,\n\t\t\t\t},\n\t\t\t]),\n\t\t\tnewValue: treeEdit.modification,\n\t\t\toldValue: targetNodeAtField,\n\t\t\taiExplanation: treeEdit.explanation,\n\t\t};\n\t}\n\n\treturn {\n\t\ttype: \"modify\",\n\t\tnodePath: createNodePathRecursive(targetNodeAtField as TreeNode, idGenerator, []),\n\t\tnewValue: treeEdit.modification,\n\t\toldValue: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),\n\t\taiExplanation: treeEdit.explanation,\n\t};\n}\n\n/**\n * Creates a diff for a Remove TreeEdit.\n *\n * @remarks\n * This function must only be called BEFORE a remove edit is applied.\n * It generates the remove diff before the node has been successfully removed, as the node's index may\n * be required to support undoing the remove operation, and we don't know that index until the remove has been made.\n */\nfunction createRemoveDiff(\n\ttreeEdit: Remove,\n\tidGenerator: IdGenerator,\n): RemoveNodeDiff | ArraySingleRemoveDiff | ArrayRangeRemoveDiff {\n\tconst source = treeEdit.source;\n\tif (isObjectTarget(source)) {\n\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\tconst parentNode = Tree.parent(node);\n\t\tif (parentNode === undefined) {\n\t\t\tthrow new Error(\"Unexpectedly received a root node as the target of a remove edit\");\n\t\t} else if (Tree.schema(parentNode).kind === NodeKind.Array) {\n\t\t\tconst nodeIndex = Tree.key(node) as number;\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tconst targetRemovedNode = (parentNode as TreeArrayNode).at(nodeIndex)!;\n\n\t\t\tif (isPrimitive(targetRemovedNode)) {\n\t\t\t\t// Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Unexpectedly recieved a primitive node as the target of a remove edit\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"remove\",\n\t\t\t\tremovalType: \"remove-array-single\",\n\t\t\t\tnodePath: createNodePathRecursive(targetRemovedNode as TreeNode, idGenerator, []),\n\t\t\t\taiExplanation: treeEdit.explanation,\n\t\t\t\tnodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetRemovedNode))),\n\t\t\t};\n\t\t} else {\n\t\t\tconst fieldKey = Tree.key(node);\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access\n\t\t\tconst targetNodeAtField: unknown = (parentNode as any)[fieldKey];\n\n\t\t\tif (isPrimitive(targetNodeAtField)) {\n\t\t\t\t// Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly\n\t\t\t\tthrow new Error(\n\t\t\t\t\t\"Unexpectedly recieved a primitive node as the target of a remove field edit\",\n\t\t\t\t);\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\ttype: \"remove\",\n\t\t\t\tremovalType: \"remove-node\",\n\t\t\t\tnodePath: createNodePathRecursive(targetNodeAtField as TreeNode, idGenerator, []),\n\t\t\t\taiExplanation: treeEdit.explanation,\n\t\t\t\tnodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),\n\t\t\t};\n\t\t}\n\t} else if (isRange(source)) {\n\t\tconst { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);\n\t\tconst removedNodePaths: NodePath[] = [];\n\t\tconst removedNodes: TreeNode[] = [];\n\t\tfor (let i = startIndex; i < endIndex; i++) {\n\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\tconst nodeToRemove = array.at(i)!;\n\t\t\tif (!isPrimitive(nodeToRemove)) {\n\t\t\t\tremovedNodePaths.push(\n\t\t\t\t\tcreateNodePathRecursive(nodeToRemove as TreeNode, idGenerator, []),\n\t\t\t\t);\n\t\t\t\tremovedNodes.push(nodeToRemove as TreeNode);\n\t\t\t}\n\t\t}\n\t\treturn {\n\t\t\ttype: \"remove\",\n\t\t\tremovalType: \"remove-array-range\",\n\t\t\tnodePaths: removedNodePaths,\n\t\t\taiExplanation: treeEdit.explanation,\n\t\t\tnodeContents: removedNodes.map((node) =>\n\t\t\t\tremoveAgentObjectIdField(JSON.parse(JSON.stringify(node))),\n\t\t\t),\n\t\t};\n\t} else {\n\t\tthrow new Error(\"Invalid source encountered when trying to create diff for remove edit\");\n\t}\n}\n\n/**\n * Creates a diff for a Move TreeEdit.\n *\n * @remarks\n * This function must only be called BEFORE a move edit is applied.\n * For move operations, the diff is created before the node(s) have been successfully moved,\n * since the original index is needed to restore the node(s) if the move operation need to undo.\n */\nfunction createMoveDiff(\n\ttreeEdit: Move,\n\tidGenerator: IdGenerator,\n): MoveSingleDiff | MoveRangeDiff {\n\tconst source = treeEdit.source;\n\tconst destination = treeEdit.destination;\n\tconst { array: destinationArrayNode } = getPlaceInfo(destination, idGenerator);\n\n\tif (isObjectTarget(source)) {\n\t\tconst node = getNodeFromTarget(source, idGenerator);\n\t\treturn {\n\t\t\ttype: \"move\",\n\t\t\tmoveType: \"move-single\",\n\t\t\tsourceNodePath: createNodePathRecursive(node, idGenerator, []),\n\t\t\tdestinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),\n\t\t\taiExplanation: treeEdit.explanation,\n\t\t\tnodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(node))),\n\t\t};\n\t} else if (isRange(source)) {\n\t\tconst {\n\t\t\tarray,\n\t\t\tstartIndex: sourceStartIndex,\n\t\t\tendIndex: sourceEndIndex,\n\t\t} = getRangeInfo(source, idGenerator);\n\n\t\tconst movedNodePaths: NodePath[] = [];\n\t\tconst movedNodes: TreeNode[] = [];\n\t\tfor (let i = sourceStartIndex; i < sourceEndIndex; i++) {\n\t\t\tconst nodeToMove = array.at(i);\n\t\t\tif (!isPrimitive(nodeToMove)) {\n\t\t\t\tmovedNodePaths.push(createNodePathRecursive(nodeToMove as TreeNode, idGenerator, []));\n\t\t\t\tmovedNodes.push(nodeToMove as TreeNode);\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\ttype: \"move\",\n\t\t\tmoveType: \"move-range\",\n\t\t\tsourceNodePaths: movedNodePaths,\n\t\t\tdestinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),\n\t\t\taiExplanation: treeEdit.explanation,\n\t\t\tnodeContents: movedNodes.map((node) =>\n\t\t\t\tremoveAgentObjectIdField(JSON.parse(JSON.stringify(node))),\n\t\t\t),\n\t\t};\n\t} else {\n\t\tthrow new Error(\"Invalid source for move edit\");\n\t}\n}\n\ninterface SchemaInfo {\n\ttreeNodeSchema: TreeNodeSchema;\n\tsimpleNodeSchema: new (dummy: unknown) => TreeNode;\n}\n\nfunction getSimpleNodeSchema(node: TreeNode): SchemaInfo {\n\tconst treeNodeSchema = Tree.schema(node);\n\tconst simpleNodeSchema = treeNodeSchema as unknown as new (dummy: unknown) => TreeNode;\n\treturn { treeNodeSchema, simpleNodeSchema };\n}\n"]}