@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"}
@@ -7,7 +7,7 @@ import { isFluidHandle } from "@fluidframework/runtime-utils";
7
7
  import { isFluidError, UsageError } from "@fluidframework/telemetry-utils/internal";
8
8
  import { Tree, NodeKind, FieldKind, FieldSchema, normalizeAllowedTypes, SchemaFactory, } from "@fluidframework/tree/internal";
9
9
  import { closest } from "fastest-levenshtein";
10
- import { typeField, } from "./agentEditTypes.js";
10
+ import { typeField, objectIdKey, } from "./agentEditTypes.js";
11
11
  import { toDecoratedJson } from "./promptGeneration.js";
12
12
  import { fail } from "./utils.js";
13
13
  function populateDefaults(json, definitionMap) {
@@ -27,7 +27,10 @@ function populateDefaults(json, definitionMap) {
27
27
  }
28
28
  }
29
29
  }
30
- function getSchemaIdentifier(content) {
30
+ /**
31
+ * Gets the schema identifier of the given content, including primitive values.
32
+ */
33
+ export function getSchemaIdentifier(content) {
31
34
  switch (typeof content) {
32
35
  case "boolean": {
33
36
  return SchemaFactory.boolean.identifier;
@@ -55,7 +58,10 @@ function getSchemaIdentifier(content) {
55
58
  }
56
59
  }
57
60
  }
58
- function contentWithIds(content, idGenerator) {
61
+ /**
62
+ * Converts a tree node from a {@link TreeEdit} to a {@link TreeEditObject} with the proper object IDs.
63
+ */
64
+ export function contentWithIds(content, idGenerator) {
59
65
  return JSON.parse(toDecoratedJson(idGenerator, content));
60
66
  }
61
67
  /**
@@ -80,8 +86,11 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
80
86
  validator?.(insertNode);
81
87
  array.insertAt(index, insertNode);
82
88
  return {
83
- ...treeEdit,
84
- content: contentWithIds(insertNode, idGenerator),
89
+ edit: {
90
+ ...treeEdit,
91
+ content: contentWithIds(insertNode, idGenerator),
92
+ },
93
+ diff: createInsertDiff(insertNode, treeEdit.explanation, idGenerator),
85
94
  };
86
95
  }
87
96
  }
@@ -89,6 +98,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
89
98
  }
90
99
  case "remove": {
91
100
  const source = treeEdit.source;
101
+ let diff;
92
102
  if (isObjectTarget(source)) {
93
103
  const node = getNodeFromTarget(source, idGenerator);
94
104
  const parentNode = Tree.parent(node);
@@ -98,7 +108,9 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
98
108
  }
99
109
  else if (Tree.schema(parentNode).kind === NodeKind.Array) {
100
110
  const nodeIndex = Tree.key(node);
101
- parentNode.removeAt(nodeIndex);
111
+ const parentArrayNode = parentNode;
112
+ diff = createRemoveDiff(treeEdit, idGenerator);
113
+ parentArrayNode.removeAt(nodeIndex);
102
114
  }
103
115
  else {
104
116
  const fieldKey = Tree.key(node);
@@ -106,6 +118,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
106
118
  const fieldSchema = parentSchema.info[fieldKey] ??
107
119
  fail("Expected field schema");
108
120
  if (fieldSchema instanceof FieldSchema && fieldSchema.kind === FieldKind.Optional) {
121
+ diff = createRemoveDiff(treeEdit, idGenerator);
109
122
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
110
123
  parentNode[fieldKey] = undefined;
111
124
  }
@@ -116,9 +129,13 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
116
129
  }
117
130
  else if (isRange(source)) {
118
131
  const { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);
132
+ diff = createRemoveDiff(treeEdit, idGenerator);
119
133
  array.removeRange(startIndex, endIndex);
120
134
  }
121
- return treeEdit;
135
+ else {
136
+ throw new UsageError("Invalid source for remove edit");
137
+ }
138
+ return { edit: treeEdit, diff };
122
139
  }
123
140
  case "modify": {
124
141
  const node = getNodeFromTarget(treeEdit.target, idGenerator);
@@ -134,6 +151,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
134
151
  // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
135
152
  const schemaIdentifier = modification[typeField];
136
153
  let insertedObject;
154
+ const diff = createModifyDiff(treeEdit, idGenerator);
137
155
  // if fieldSchema is a LeafnodeSchema, we can check that it's a valid type and set the field.
138
156
  if (isPrimitive(modification)) {
139
157
  try {
@@ -166,13 +184,9 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
166
184
  assert(Array.isArray(field), 0xa75 /* the field must be an array node */);
167
185
  assert(Array.isArray(constructedModification), 0xa76 /* the modification must be an array node */);
168
186
  field.removeRange(0);
169
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
170
- node[treeEdit.field] = constructedModification;
171
- }
172
- else {
173
- // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
174
- node[treeEdit.field] = constructedModification;
175
187
  }
188
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
189
+ node[treeEdit.field] = constructedModification;
176
190
  }
177
191
  // If the fieldSchema is of type FieldSchema, we can check its allowed types and set the field.
178
192
  else if (fieldSchema instanceof FieldSchema) {
@@ -199,10 +213,13 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
199
213
  }
200
214
  }
201
215
  return insertedObject === undefined
202
- ? treeEdit
216
+ ? { edit: treeEdit, diff }
203
217
  : {
204
- ...treeEdit,
205
- modification: contentWithIds(insertedObject, idGenerator),
218
+ edit: {
219
+ ...treeEdit,
220
+ modification: contentWithIds(insertedObject, idGenerator),
221
+ },
222
+ diff,
206
223
  };
207
224
  }
208
225
  case "move": {
@@ -210,6 +227,7 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
210
227
  const source = treeEdit.source;
211
228
  const destination = treeEdit.destination;
212
229
  const { array: destinationArrayNode, index: destinationIndex } = getPlaceInfo(destination, idGenerator);
230
+ const diff = createMoveDiff(treeEdit, idGenerator);
213
231
  if (isObjectTarget(source)) {
214
232
  const sourceNode = getNodeFromTarget(source, idGenerator);
215
233
  const sourceIndex = Tree.key(sourceNode);
@@ -246,7 +264,10 @@ export function applyAgentEdit(treeEdit, idGenerator, definitionMap, validator)
246
264
  }
247
265
  destinationArrayNode.moveRangeToIndex(destinationIndex, sourceStartIndex, sourceEndIndex, array);
248
266
  }
249
- return treeEdit;
267
+ else {
268
+ throw new Error("Invalid source for move edit");
269
+ }
270
+ return { edit: treeEdit, diff };
250
271
  }
251
272
  default: {
252
273
  fail("invalid tree edit");
@@ -305,7 +326,10 @@ function isObjectTarget(selection) {
305
326
  function isRange(selection) {
306
327
  return "from" in selection && "to" in selection;
307
328
  }
308
- function getRangeInfo(range, idGenerator) {
329
+ /**
330
+ * Gets information about the range of nodes being targeted by an {@link Range}
331
+ */
332
+ export function getRangeInfo(range, idGenerator) {
309
333
  const { array: arrayFrom, index: startIndex } = getPlaceInfo(range.from, idGenerator);
310
334
  const { array: arrayTo, index: endIndex } = getPlaceInfo(range.to, idGenerator);
311
335
  if (arrayFrom !== arrayTo) {
@@ -433,6 +457,203 @@ function assertObjectIdsExist(treeEdit, idGenerator) {
433
457
  }
434
458
  }
435
459
  }
460
+ const createNodePathRecursive = (node, idGenerator, currentPath) => {
461
+ if (node === undefined) {
462
+ return currentPath;
463
+ }
464
+ currentPath.push({
465
+ shortId: Tree.shortId(node),
466
+ schemaIdentifier: Tree.schema(node).identifier,
467
+ parentField: Tree.key(node),
468
+ });
469
+ const parentNode = Tree.parent(node);
470
+ return createNodePathRecursive(parentNode, idGenerator, currentPath);
471
+ };
472
+ /**
473
+ * Creates a diff for an Insert TreeEdit.
474
+ *
475
+ * @remarks
476
+ * This function is only invoked within the "insert" case block.
477
+ *
478
+ * This must only be called AFTER an insertion is made.
479
+ * It generates the insert diff after the node has been successfully inserted, as the node's index may
480
+ * be required to support undoing the insert operation, and we don't know that index until the insert has been made.
481
+ */
482
+ function createInsertDiff(newlyInsertedNode, aiExplanation, idGenerator) {
483
+ return {
484
+ type: "insert",
485
+ nodePath: createNodePathRecursive(newlyInsertedNode, idGenerator, []),
486
+ nodeContent: JSON.parse(JSON.stringify(newlyInsertedNode)),
487
+ aiExplanation,
488
+ };
489
+ }
490
+ /**
491
+ * 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.
492
+ * @remarks The input object is not modified.
493
+ */
494
+ function removeAgentObjectIdField(oldValue) {
495
+ if (typeof oldValue === "object" && oldValue !== null && !Array.isArray(oldValue)) {
496
+ const { [objectIdKey]: _, ...rest } = oldValue;
497
+ return rest;
498
+ }
499
+ return oldValue;
500
+ }
501
+ /**
502
+ * Creates a diff for a Modify TreeEdit.
503
+ *
504
+ * @remarks
505
+ * This function must only be called BEFORE a modify edit is applied.
506
+ * For move operations, the diff is created before the node(s) have been successfully moved,
507
+ * since the original index is needed to restore the node(s) if the move operation need to undo.
508
+ */
509
+ function createModifyDiff(treeEdit, idGenerator) {
510
+ const targetNode = getNodeFromTarget(treeEdit.target, idGenerator);
511
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
512
+ const targetNodeAtField = targetNode[treeEdit.field];
513
+ if (isPrimitive(targetNodeAtField)) {
514
+ return {
515
+ type: "modify",
516
+ nodePath: createNodePathRecursive(targetNode, idGenerator, [
517
+ {
518
+ shortId: undefined,
519
+ parentField: treeEdit.field,
520
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
521
+ schemaIdentifier: getSchemaIdentifier(treeEdit.modification),
522
+ },
523
+ ]),
524
+ newValue: treeEdit.modification,
525
+ oldValue: targetNodeAtField,
526
+ aiExplanation: treeEdit.explanation,
527
+ };
528
+ }
529
+ return {
530
+ type: "modify",
531
+ nodePath: createNodePathRecursive(targetNodeAtField, idGenerator, []),
532
+ newValue: treeEdit.modification,
533
+ oldValue: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),
534
+ aiExplanation: treeEdit.explanation,
535
+ };
536
+ }
537
+ /**
538
+ * Creates a diff for a Remove TreeEdit.
539
+ *
540
+ * @remarks
541
+ * This function must only be called BEFORE a remove edit is applied.
542
+ * It generates the remove diff before the node has been successfully removed, as the node's index may
543
+ * be required to support undoing the remove operation, and we don't know that index until the remove has been made.
544
+ */
545
+ function createRemoveDiff(treeEdit, idGenerator) {
546
+ const source = treeEdit.source;
547
+ if (isObjectTarget(source)) {
548
+ const node = getNodeFromTarget(source, idGenerator);
549
+ const parentNode = Tree.parent(node);
550
+ if (parentNode === undefined) {
551
+ throw new Error("Unexpectedly received a root node as the target of a remove edit");
552
+ }
553
+ else if (Tree.schema(parentNode).kind === NodeKind.Array) {
554
+ const nodeIndex = Tree.key(node);
555
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
556
+ const targetRemovedNode = parentNode.at(nodeIndex);
557
+ if (isPrimitive(targetRemovedNode)) {
558
+ // Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly
559
+ throw new Error("Unexpectedly recieved a primitive node as the target of a remove edit");
560
+ }
561
+ return {
562
+ type: "remove",
563
+ removalType: "remove-array-single",
564
+ nodePath: createNodePathRecursive(targetRemovedNode, idGenerator, []),
565
+ aiExplanation: treeEdit.explanation,
566
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetRemovedNode))),
567
+ };
568
+ }
569
+ else {
570
+ const fieldKey = Tree.key(node);
571
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
572
+ const targetNodeAtField = parentNode[fieldKey];
573
+ if (isPrimitive(targetNodeAtField)) {
574
+ // Note that this cause should not be possible, still putting the error here in case things change so that this function is updated properly
575
+ throw new Error("Unexpectedly recieved a primitive node as the target of a remove field edit");
576
+ }
577
+ return {
578
+ type: "remove",
579
+ removalType: "remove-node",
580
+ nodePath: createNodePathRecursive(targetNodeAtField, idGenerator, []),
581
+ aiExplanation: treeEdit.explanation,
582
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(targetNodeAtField))),
583
+ };
584
+ }
585
+ }
586
+ else if (isRange(source)) {
587
+ const { array, startIndex, endIndex } = getRangeInfo(source, idGenerator);
588
+ const removedNodePaths = [];
589
+ const removedNodes = [];
590
+ for (let i = startIndex; i < endIndex; i++) {
591
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
592
+ const nodeToRemove = array.at(i);
593
+ if (!isPrimitive(nodeToRemove)) {
594
+ removedNodePaths.push(createNodePathRecursive(nodeToRemove, idGenerator, []));
595
+ removedNodes.push(nodeToRemove);
596
+ }
597
+ }
598
+ return {
599
+ type: "remove",
600
+ removalType: "remove-array-range",
601
+ nodePaths: removedNodePaths,
602
+ aiExplanation: treeEdit.explanation,
603
+ nodeContents: removedNodes.map((node) => removeAgentObjectIdField(JSON.parse(JSON.stringify(node)))),
604
+ };
605
+ }
606
+ else {
607
+ throw new Error("Invalid source encountered when trying to create diff for remove edit");
608
+ }
609
+ }
610
+ /**
611
+ * Creates a diff for a Move TreeEdit.
612
+ *
613
+ * @remarks
614
+ * This function must only be called BEFORE a move edit is applied.
615
+ * For move operations, the diff is created before the node(s) have been successfully moved,
616
+ * since the original index is needed to restore the node(s) if the move operation need to undo.
617
+ */
618
+ function createMoveDiff(treeEdit, idGenerator) {
619
+ const source = treeEdit.source;
620
+ const destination = treeEdit.destination;
621
+ const { array: destinationArrayNode } = getPlaceInfo(destination, idGenerator);
622
+ if (isObjectTarget(source)) {
623
+ const node = getNodeFromTarget(source, idGenerator);
624
+ return {
625
+ type: "move",
626
+ moveType: "move-single",
627
+ sourceNodePath: createNodePathRecursive(node, idGenerator, []),
628
+ destinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),
629
+ aiExplanation: treeEdit.explanation,
630
+ nodeContent: removeAgentObjectIdField(JSON.parse(JSON.stringify(node))),
631
+ };
632
+ }
633
+ else if (isRange(source)) {
634
+ const { array, startIndex: sourceStartIndex, endIndex: sourceEndIndex, } = getRangeInfo(source, idGenerator);
635
+ const movedNodePaths = [];
636
+ const movedNodes = [];
637
+ for (let i = sourceStartIndex; i < sourceEndIndex; i++) {
638
+ const nodeToMove = array.at(i);
639
+ if (!isPrimitive(nodeToMove)) {
640
+ movedNodePaths.push(createNodePathRecursive(nodeToMove, idGenerator, []));
641
+ movedNodes.push(nodeToMove);
642
+ }
643
+ }
644
+ return {
645
+ type: "move",
646
+ moveType: "move-range",
647
+ sourceNodePaths: movedNodePaths,
648
+ destinationNodePath: createNodePathRecursive(destinationArrayNode, idGenerator, []),
649
+ aiExplanation: treeEdit.explanation,
650
+ nodeContents: movedNodes.map((node) => removeAgentObjectIdField(JSON.parse(JSON.stringify(node)))),
651
+ };
652
+ }
653
+ else {
654
+ throw new Error("Invalid source for move edit");
655
+ }
656
+ }
436
657
  function getSimpleNodeSchema(node) {
437
658
  const treeNodeSchema = Tree.schema(node);
438
659
  const simpleNodeSchema = treeNodeSchema;
@@ -1 +1 @@
1
- {"version":3,"file":"agentEditReducer.js","sourceRoot":"","sources":["../../src/explicit-strategy/agentEditReducer.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,MAAM,EAAE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AK,SAAS,EACxD,CAAC;gBACF,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;YAC/C,CAAC;YAED,wBAAwB;YACxB,IAAI,QAAQ,CAAC,WAAW,CAAC,IAAI,KAAK,aAAa,EAAE,CAAC;gBACjD,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;oBACpE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;gBACpD,CAAC;YACF,CAAC;iBAAM,CAAC;gBACP,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,SAAS,EAAE,CAAC;oBACtE,gBAAgB,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC;gBACtD,CAAC;YACF,CAAC;YACD,IAAI,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,UAAU,CAAC,iBAAiB,gBAAgB,kBAAkB,CAAC,CAAC;YAC3E,CAAC;YACD,MAAM;QACP,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACT,MAAM;QACP,CAAC;IACF,CAAC;AACF,CAAC;AAOD,SAAS,mBAAmB,CAAC,IAAc;IAC1C,MAAM,cAAc,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;IACzC,MAAM,gBAAgB,GAAG,cAA6D,CAAC;IACvF,OAAO,EAAE,cAAc,EAAE,gBAAgB,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 {\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/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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"]}