@fluidframework/ai-collab 2.10.0-306579

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 (168) hide show
  1. package/.eslintrc.cjs +26 -0
  2. package/CHANGELOG.md +9 -0
  3. package/LICENSE +21 -0
  4. package/README.md +280 -0
  5. package/alpha.d.ts +11 -0
  6. package/api-extractor/api-extractor-lint-alpha.cjs.json +5 -0
  7. package/api-extractor/api-extractor-lint-alpha.esm.json +5 -0
  8. package/api-extractor/api-extractor-lint-bundle.json +5 -0
  9. package/api-extractor/api-extractor-lint-index.cjs.json +5 -0
  10. package/api-extractor/api-extractor-lint-index.esm.json +5 -0
  11. package/api-extractor/api-extractor-lint-public.cjs.json +5 -0
  12. package/api-extractor/api-extractor-lint-public.esm.json +5 -0
  13. package/api-extractor-lint.json +4 -0
  14. package/api-extractor.json +4 -0
  15. package/api-report/ai-collab.alpha.api.md +164 -0
  16. package/api-report/ai-collab.beta.api.md +7 -0
  17. package/api-report/ai-collab.public.api.md +7 -0
  18. package/biome.jsonc +4 -0
  19. package/dist/aiCollab.d.ts +65 -0
  20. package/dist/aiCollab.d.ts.map +1 -0
  21. package/dist/aiCollab.js +81 -0
  22. package/dist/aiCollab.js.map +1 -0
  23. package/dist/aiCollabApi.d.ts +173 -0
  24. package/dist/aiCollabApi.d.ts.map +1 -0
  25. package/dist/aiCollabApi.js +7 -0
  26. package/dist/aiCollabApi.js.map +1 -0
  27. package/dist/alpha.d.ts +41 -0
  28. package/dist/explicit-strategy/agentEditReducer.d.ts +12 -0
  29. package/dist/explicit-strategy/agentEditReducer.d.ts.map +1 -0
  30. package/dist/explicit-strategy/agentEditReducer.js +394 -0
  31. package/dist/explicit-strategy/agentEditReducer.js.map +1 -0
  32. package/dist/explicit-strategy/agentEditTypes.d.ts +158 -0
  33. package/dist/explicit-strategy/agentEditTypes.d.ts.map +1 -0
  34. package/dist/explicit-strategy/agentEditTypes.js +50 -0
  35. package/dist/explicit-strategy/agentEditTypes.js.map +1 -0
  36. package/dist/explicit-strategy/idGenerator.d.ts +22 -0
  37. package/dist/explicit-strategy/idGenerator.d.ts.map +1 -0
  38. package/dist/explicit-strategy/idGenerator.js +74 -0
  39. package/dist/explicit-strategy/idGenerator.js.map +1 -0
  40. package/dist/explicit-strategy/index.d.ts +51 -0
  41. package/dist/explicit-strategy/index.d.ts.map +1 -0
  42. package/dist/explicit-strategy/index.js +223 -0
  43. package/dist/explicit-strategy/index.js.map +1 -0
  44. package/dist/explicit-strategy/jsonTypes.d.ts +23 -0
  45. package/dist/explicit-strategy/jsonTypes.d.ts.map +1 -0
  46. package/dist/explicit-strategy/jsonTypes.js +7 -0
  47. package/dist/explicit-strategy/jsonTypes.js.map +1 -0
  48. package/dist/explicit-strategy/promptGeneration.d.ts +51 -0
  49. package/dist/explicit-strategy/promptGeneration.d.ts.map +1 -0
  50. package/dist/explicit-strategy/promptGeneration.js +218 -0
  51. package/dist/explicit-strategy/promptGeneration.js.map +1 -0
  52. package/dist/explicit-strategy/typeGeneration.d.ts +15 -0
  53. package/dist/explicit-strategy/typeGeneration.d.ts.map +1 -0
  54. package/dist/explicit-strategy/typeGeneration.js +264 -0
  55. package/dist/explicit-strategy/typeGeneration.js.map +1 -0
  56. package/dist/explicit-strategy/utils.d.ts +37 -0
  57. package/dist/explicit-strategy/utils.d.ts.map +1 -0
  58. package/dist/explicit-strategy/utils.js +47 -0
  59. package/dist/explicit-strategy/utils.js.map +1 -0
  60. package/dist/implicit-strategy/index.d.ts +8 -0
  61. package/dist/implicit-strategy/index.d.ts.map +1 -0
  62. package/dist/implicit-strategy/index.js +18 -0
  63. package/dist/implicit-strategy/index.js.map +1 -0
  64. package/dist/implicit-strategy/sharedTreeBranchManager.d.ts +63 -0
  65. package/dist/implicit-strategy/sharedTreeBranchManager.d.ts.map +1 -0
  66. package/dist/implicit-strategy/sharedTreeBranchManager.js +212 -0
  67. package/dist/implicit-strategy/sharedTreeBranchManager.js.map +1 -0
  68. package/dist/implicit-strategy/sharedTreeDiff.d.ts +102 -0
  69. package/dist/implicit-strategy/sharedTreeDiff.d.ts.map +1 -0
  70. package/dist/implicit-strategy/sharedTreeDiff.js +522 -0
  71. package/dist/implicit-strategy/sharedTreeDiff.js.map +1 -0
  72. package/dist/implicit-strategy/utils.d.ts +21 -0
  73. package/dist/implicit-strategy/utils.d.ts.map +1 -0
  74. package/dist/implicit-strategy/utils.js +49 -0
  75. package/dist/implicit-strategy/utils.js.map +1 -0
  76. package/dist/index.d.ts +16 -0
  77. package/dist/index.d.ts.map +1 -0
  78. package/dist/index.js +24 -0
  79. package/dist/index.js.map +1 -0
  80. package/dist/package.json +3 -0
  81. package/dist/public.d.ts +19 -0
  82. package/eslintrc.cjs +11 -0
  83. package/internal.d.ts +11 -0
  84. package/lib/aiCollab.d.ts +65 -0
  85. package/lib/aiCollab.d.ts.map +1 -0
  86. package/lib/aiCollab.js +77 -0
  87. package/lib/aiCollab.js.map +1 -0
  88. package/lib/aiCollabApi.d.ts +173 -0
  89. package/lib/aiCollabApi.d.ts.map +1 -0
  90. package/lib/aiCollabApi.js +6 -0
  91. package/lib/aiCollabApi.js.map +1 -0
  92. package/lib/alpha.d.ts +41 -0
  93. package/lib/explicit-strategy/agentEditReducer.d.ts +12 -0
  94. package/lib/explicit-strategy/agentEditReducer.d.ts.map +1 -0
  95. package/lib/explicit-strategy/agentEditReducer.js +390 -0
  96. package/lib/explicit-strategy/agentEditReducer.js.map +1 -0
  97. package/lib/explicit-strategy/agentEditTypes.d.ts +158 -0
  98. package/lib/explicit-strategy/agentEditTypes.d.ts.map +1 -0
  99. package/lib/explicit-strategy/agentEditTypes.js +47 -0
  100. package/lib/explicit-strategy/agentEditTypes.js.map +1 -0
  101. package/lib/explicit-strategy/idGenerator.d.ts +22 -0
  102. package/lib/explicit-strategy/idGenerator.d.ts.map +1 -0
  103. package/lib/explicit-strategy/idGenerator.js +70 -0
  104. package/lib/explicit-strategy/idGenerator.js.map +1 -0
  105. package/lib/explicit-strategy/index.d.ts +51 -0
  106. package/lib/explicit-strategy/index.d.ts.map +1 -0
  107. package/lib/explicit-strategy/index.js +219 -0
  108. package/lib/explicit-strategy/index.js.map +1 -0
  109. package/lib/explicit-strategy/jsonTypes.d.ts +23 -0
  110. package/lib/explicit-strategy/jsonTypes.d.ts.map +1 -0
  111. package/lib/explicit-strategy/jsonTypes.js +6 -0
  112. package/lib/explicit-strategy/jsonTypes.js.map +1 -0
  113. package/lib/explicit-strategy/promptGeneration.d.ts +51 -0
  114. package/lib/explicit-strategy/promptGeneration.d.ts.map +1 -0
  115. package/lib/explicit-strategy/promptGeneration.js +208 -0
  116. package/lib/explicit-strategy/promptGeneration.js.map +1 -0
  117. package/lib/explicit-strategy/typeGeneration.d.ts +15 -0
  118. package/lib/explicit-strategy/typeGeneration.d.ts.map +1 -0
  119. package/lib/explicit-strategy/typeGeneration.js +260 -0
  120. package/lib/explicit-strategy/typeGeneration.js.map +1 -0
  121. package/lib/explicit-strategy/utils.d.ts +37 -0
  122. package/lib/explicit-strategy/utils.d.ts.map +1 -0
  123. package/lib/explicit-strategy/utils.js +41 -0
  124. package/lib/explicit-strategy/utils.js.map +1 -0
  125. package/lib/implicit-strategy/index.d.ts +8 -0
  126. package/lib/implicit-strategy/index.d.ts.map +1 -0
  127. package/lib/implicit-strategy/index.js +8 -0
  128. package/lib/implicit-strategy/index.js.map +1 -0
  129. package/lib/implicit-strategy/sharedTreeBranchManager.d.ts +63 -0
  130. package/lib/implicit-strategy/sharedTreeBranchManager.d.ts.map +1 -0
  131. package/lib/implicit-strategy/sharedTreeBranchManager.js +213 -0
  132. package/lib/implicit-strategy/sharedTreeBranchManager.js.map +1 -0
  133. package/lib/implicit-strategy/sharedTreeDiff.d.ts +102 -0
  134. package/lib/implicit-strategy/sharedTreeDiff.d.ts.map +1 -0
  135. package/lib/implicit-strategy/sharedTreeDiff.js +515 -0
  136. package/lib/implicit-strategy/sharedTreeDiff.js.map +1 -0
  137. package/lib/implicit-strategy/utils.d.ts +21 -0
  138. package/lib/implicit-strategy/utils.d.ts.map +1 -0
  139. package/lib/implicit-strategy/utils.js +43 -0
  140. package/lib/implicit-strategy/utils.js.map +1 -0
  141. package/lib/index.d.ts +16 -0
  142. package/lib/index.d.ts.map +1 -0
  143. package/lib/index.js +15 -0
  144. package/lib/index.js.map +1 -0
  145. package/lib/public.d.ts +19 -0
  146. package/lib/tsdoc-metadata.json +11 -0
  147. package/mocharc.cjs +14 -0
  148. package/package.json +165 -0
  149. package/prettier.config.cjs +8 -0
  150. package/src/aiCollab.ts +86 -0
  151. package/src/aiCollabApi.ts +184 -0
  152. package/src/explicit-strategy/agentEditReducer.ts +498 -0
  153. package/src/explicit-strategy/agentEditTypes.ts +177 -0
  154. package/src/explicit-strategy/idGenerator.ts +90 -0
  155. package/src/explicit-strategy/index.ts +364 -0
  156. package/src/explicit-strategy/jsonTypes.ts +27 -0
  157. package/src/explicit-strategy/promptGeneration.ts +294 -0
  158. package/src/explicit-strategy/typeGeneration.ts +374 -0
  159. package/src/explicit-strategy/utils.ts +60 -0
  160. package/src/implicit-strategy/README.md +4 -0
  161. package/src/implicit-strategy/index.ts +21 -0
  162. package/src/implicit-strategy/sharedTreeBranchManager.ts +294 -0
  163. package/src/implicit-strategy/sharedTreeDiff.ts +735 -0
  164. package/src/implicit-strategy/utils.ts +54 -0
  165. package/src/index.ts +39 -0
  166. package/tsconfig.cjs.json +7 -0
  167. package/tsconfig.json +12 -0
  168. package/tsdoc.json +4 -0
@@ -0,0 +1,41 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * TBD
7
+ */
8
+ export function fail(message) {
9
+ throw new Error(message);
10
+ }
11
+ /**
12
+ * Map one iterable to another by transforming each element one at a time
13
+ * @param iterable - the iterable to transform
14
+ * @param map - the transformation function to run on each element of the iterable
15
+ * @returns a new iterable of elements which have been transformed by the `map` function
16
+ *
17
+ * @remarks originally from tree/src/util/utils.ts
18
+ */
19
+ export function* mapIterable(iterable, map) {
20
+ for (const t of iterable) {
21
+ yield map(t);
22
+ }
23
+ }
24
+ /**
25
+ * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.
26
+ * @param map - The map to query/update
27
+ * @param key - The key to lookup in the map
28
+ * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists
29
+ * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)
30
+ *
31
+ * @remarks originally from tree/src/util/utils.ts
32
+ */
33
+ export function getOrCreate(map, key, defaultValue) {
34
+ let value = map.get(key);
35
+ if (value === undefined) {
36
+ value = defaultValue(key);
37
+ map.set(key, value);
38
+ }
39
+ return value;
40
+ }
41
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/explicit-strategy/utils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAYH;;GAEG;AACH,MAAM,UAAU,IAAI,CAAC,OAAe;IACnC,MAAM,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC;AAC1B,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,SAAS,CAAC,CAAC,WAAW,CAC3B,QAAqB,EACrB,GAAgB;IAEhB,KAAK,MAAM,CAAC,IAAI,QAAQ,EAAE,CAAC;QAC1B,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC;IACd,CAAC;AACF,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAC1B,GAAoB,EACpB,GAAM,EACN,YAA2B;IAE3B,IAAI,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IACzB,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACzB,KAAK,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC;QAC1B,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IACrB,CAAC;IACD,OAAO,KAAK,CAAC;AACd,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\n/**\n * Subset of Map interface.\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport interface MapGetSet<K, V> {\n\tget(key: K): V | undefined;\n\tset(key: K, value: V): void;\n}\n\n/**\n * TBD\n */\nexport function fail(message: string): never {\n\tthrow new Error(message);\n}\n\n/**\n * Map one iterable to another by transforming each element one at a time\n * @param iterable - the iterable to transform\n * @param map - the transformation function to run on each element of the iterable\n * @returns a new iterable of elements which have been transformed by the `map` function\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function* mapIterable<T, U>(\n\titerable: Iterable<T>,\n\tmap: (t: T) => U,\n): IterableIterator<U> {\n\tfor (const t of iterable) {\n\t\tyield map(t);\n\t}\n}\n\n/**\n * Retrieve a value from a map with the given key, or create a new entry if the key is not in the map.\n * @param map - The map to query/update\n * @param key - The key to lookup in the map\n * @param defaultValue - a function which returns a default value. This is called and used to set an initial value for the given key in the map if none exists\n * @returns either the existing value for the given key, or the newly-created value (the result of `defaultValue`)\n *\n * @remarks originally from tree/src/util/utils.ts\n */\nexport function getOrCreate<K, V>(\n\tmap: MapGetSet<K, V>,\n\tkey: K,\n\tdefaultValue: (key: K) => V,\n): V {\n\tlet value = map.get(key);\n\tif (value === undefined) {\n\t\tvalue = defaultValue(key);\n\t\tmap.set(key, value);\n\t}\n\treturn value;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export { type DifferenceCreate, type DifferenceChange, type DifferenceMove, type DifferenceRemove, type Difference, type ObjectPath, type Options, sharedTreeDiff, createMergableIdDiffSeries, createMergableDiffSeries, } from "./sharedTreeDiff.js";
6
+ export { SharedTreeBranchManager } from "./sharedTreeBranchManager.js";
7
+ export { sharedTreeTraverse, isTreeArrayNode, isTreeMapNode } from "./utils.js";
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/implicit-strategy/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACN,KAAK,gBAAgB,EACrB,KAAK,gBAAgB,EACrB,KAAK,cAAc,EACnB,KAAK,gBAAgB,EACrB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,OAAO,EACZ,cAAc,EACd,0BAA0B,EAC1B,wBAAwB,GACxB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC"}
@@ -0,0 +1,8 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ export { sharedTreeDiff, createMergableIdDiffSeries, createMergableDiffSeries, } from "./sharedTreeDiff.js";
6
+ export { SharedTreeBranchManager } from "./sharedTreeBranchManager.js";
7
+ export { sharedTreeTraverse, isTreeArrayNode, isTreeMapNode } from "./utils.js";
8
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/implicit-strategy/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAQN,cAAc,EACd,0BAA0B,EAC1B,wBAAwB,GACxB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nexport {\n\ttype DifferenceCreate,\n\ttype DifferenceChange,\n\ttype DifferenceMove,\n\ttype DifferenceRemove,\n\ttype Difference,\n\ttype ObjectPath,\n\ttype Options,\n\tsharedTreeDiff,\n\tcreateMergableIdDiffSeries,\n\tcreateMergableDiffSeries,\n} from \"./sharedTreeDiff.js\";\n\nexport { SharedTreeBranchManager } from \"./sharedTreeBranchManager.js\";\n\nexport { sharedTreeTraverse, isTreeArrayNode, isTreeMapNode } from \"./utils.js\";\n"]}
@@ -0,0 +1,63 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import type { ImplicitFieldSchema, TreeArrayNode, TreeViewConfiguration } from "@fluidframework/tree";
6
+ import { type BranchableTree, type TreeBranchFork, type TreeViewAlpha } from "@fluidframework/tree/alpha";
7
+ import type { z } from "zod";
8
+ import { type Difference, type ObjectPath } from "./sharedTreeDiff.js";
9
+ /**
10
+ * Manages determining the differences between two branches of a SharedTree represented as an actual tree node or a plain javascript object
11
+ * and applies said differences to the original SharedTree branch.
12
+ *
13
+ * @alpha
14
+ */
15
+ export declare class SharedTreeBranchManager {
16
+ private readonly objectSchema?;
17
+ private readonly nodeIdAttributeName?;
18
+ constructor(params?: {
19
+ objectSchema?: z.Schema;
20
+ nodeIdAttributeName?: string;
21
+ });
22
+ /**
23
+ * Compares the differences between either two objects or a TreeNode and a plain object.
24
+ * TODO: Should allow comparing two tree nodes? Should we allow comparing two plain objects? Or just leave as tree node vs object?
25
+ */
26
+ compare(obj: Record<string, unknown> | TreeArrayNode, newObj: Record<string, unknown> | unknown[]): Difference[];
27
+ /**
28
+ * Produces a set of differences based on two versions of an object, applies the changes to the first one,
29
+ * and returns the set of differences.
30
+ */
31
+ mergeObject(obj: Record<string, unknown> | TreeArrayNode, llmResponse: Record<string, unknown> | unknown[]): Difference[];
32
+ /**
33
+ * produces a diff between two objects and merges the differences.
34
+ */
35
+ checkoutNewMergedBranch<T extends ImplicitFieldSchema>(treeView: TreeViewAlpha<T>, treeViewConfiguration: TreeViewConfiguration<T>, absolutePathToObjectNode: ObjectPath, llmResponse: Record<string, unknown> | unknown[]): {
36
+ differences: Difference[];
37
+ originalBranch: BranchableTree;
38
+ forkBranch: TreeBranchFork;
39
+ forkView: TreeViewAlpha<T>;
40
+ newBranchTargetNode: Record<string, unknown> | TreeArrayNode;
41
+ };
42
+ /**
43
+ * Creates a forked branch of a tree view.
44
+ */
45
+ checkoutNewMergedBranchV2<T extends ImplicitFieldSchema>(treeView: TreeViewAlpha<T>, treeViewConfiguration: TreeViewConfiguration<T>, absolutePathToObjectNode: ObjectPath): {
46
+ originalBranch: BranchableTree;
47
+ forkBranch: TreeBranchFork;
48
+ forkView: TreeViewAlpha<T>;
49
+ newBranchTargetNode: Record<string, unknown> | TreeArrayNode;
50
+ };
51
+ /**
52
+ * Handles applying an array of differences to an object in the proper order and making any necessary adjustments as each diff
53
+ * is applied.
54
+ *
55
+ * @returns an array of differences that were not applied due to some kind of conflict or error.
56
+ */
57
+ mergeDiffs(diffs: Difference[], objectToUpdate: Record<string, unknown> | TreeArrayNode): Set<Difference>;
58
+ /**
59
+ * Applies an individual diff to the objectToUpdate.
60
+ */
61
+ applyDiff(diff: Difference, objectToUpdate: Record<string, unknown> | TreeArrayNode): boolean;
62
+ }
63
+ //# sourceMappingURL=sharedTreeBranchManager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sharedTreeBranchManager.d.ts","sourceRoot":"","sources":["../../src/implicit-strategy/sharedTreeBranchManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EACX,mBAAmB,EACnB,aAAa,EAEb,qBAAqB,EACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAIN,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,aAAa,EAElB,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAE7B,OAAO,EAIN,KAAK,UAAU,EACf,KAAK,UAAU,EACf,MAAM,qBAAqB,CAAC;AAG7B;;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,CAAC,CAAC,CAAC,EAC/C,wBAAwB,EAAE,UAAU,GAElC;QACF,cAAc,EAAE,cAAc,CAAC;QAC/B,UAAU,EAAE,cAAc,CAAC;QAC3B,QAAQ,EAAE,aAAa,CAAC,CAAC,CAAC,CAAC;QAC3B,mBAAmB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,CAAC;KAC7D;IAaD;;;;;OAKG;IACI,UAAU,CAChB,KAAK,EAAE,UAAU,EAAE,EACnB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,GACrD,GAAG,CAAC,UAAU,CAAC;IAclB;;OAEG;IACI,SAAS,CACf,IAAI,EAAE,UAAU,EAChB,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,aAAa,GACrD,OAAO;CA4FV"}
@@ -0,0 +1,213 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ import {
6
+ // TODO: Migrate to newer branching API (`TreeContext`)
7
+ // eslint-disable-next-line import/no-deprecated
8
+ getBranch,
9
+ // eslint-disable-next-line import/no-internal-modules -- This package depends on the branching APIs in Tree which are currently alpha
10
+ } from "@fluidframework/tree/alpha";
11
+ import { createMergableDiffSeries, createMergableIdDiffSeries, sharedTreeDiff, } from "./sharedTreeDiff.js";
12
+ import { isTreeMapNode, isTreeArrayNode, sharedTreeTraverse } from "./utils.js";
13
+ /**
14
+ * Manages determining the differences between two branches of a SharedTree represented as an actual tree node or a plain javascript object
15
+ * and applies said differences to the original SharedTree branch.
16
+ *
17
+ * @alpha
18
+ */
19
+ export class SharedTreeBranchManager {
20
+ constructor(params) {
21
+ this.objectSchema = params?.objectSchema;
22
+ this.nodeIdAttributeName = params?.nodeIdAttributeName;
23
+ }
24
+ /**
25
+ * Compares the differences between either two objects or a TreeNode and a plain object.
26
+ * TODO: Should allow comparing two tree nodes? Should we allow comparing two plain objects? Or just leave as tree node vs object?
27
+ */
28
+ compare(obj, newObj) {
29
+ // By validating that the incoming object matches the schema, we can confirm that any property
30
+ // deletions/updates/additions are valid.
31
+ if (this.objectSchema !== undefined) {
32
+ const res = this.objectSchema.safeParse(newObj);
33
+ if (res.success === false) {
34
+ throw new TypeError("Invalid data");
35
+ }
36
+ }
37
+ const diffTotality = sharedTreeDiff(obj, newObj, {
38
+ useObjectIds: this.nodeIdAttributeName === undefined
39
+ ? undefined
40
+ : { idAttributeName: this.nodeIdAttributeName },
41
+ cyclesFix: true,
42
+ });
43
+ if (this.nodeIdAttributeName !== undefined) {
44
+ return createMergableIdDiffSeries(obj, diffTotality, this.nodeIdAttributeName);
45
+ }
46
+ return createMergableDiffSeries(diffTotality);
47
+ }
48
+ /**
49
+ * Produces a set of differences based on two versions of an object, applies the changes to the first one,
50
+ * and returns the set of differences.
51
+ */
52
+ mergeObject(obj, llmResponse) {
53
+ const differences = this.compare(obj, llmResponse);
54
+ this.mergeDiffs(differences, obj);
55
+ return differences;
56
+ }
57
+ /**
58
+ * produces a diff between two objects and merges the differences.
59
+ */
60
+ checkoutNewMergedBranch(treeView, treeViewConfiguration, absolutePathToObjectNode, llmResponse) {
61
+ // eslint-disable-next-line import/no-deprecated
62
+ const originalBranch = getBranch(treeView);
63
+ const forkBranch = originalBranch.branch();
64
+ const forkView = forkBranch.viewWith(treeViewConfiguration);
65
+ console.log("traveling to absolute path from root:", absolutePathToObjectNode);
66
+ const newBranchTargetNode = sharedTreeTraverse(forkView.root, absolutePathToObjectNode);
67
+ console.log("initiating compare between old and new branch target nodes", { ...newBranchTargetNode }, { ...llmResponse });
68
+ console.log("newBranchTargetNode", { ...newBranchTargetNode });
69
+ console.log("llmResponse", { ...llmResponse });
70
+ const differences = this.compare(newBranchTargetNode, llmResponse);
71
+ // const differences = [];
72
+ this.mergeDiffs(differences, newBranchTargetNode);
73
+ return { differences, originalBranch, forkBranch, forkView, newBranchTargetNode };
74
+ }
75
+ /**
76
+ * Creates a forked branch of a tree view.
77
+ */
78
+ checkoutNewMergedBranchV2(treeView, treeViewConfiguration, absolutePathToObjectNode) {
79
+ // eslint-disable-next-line import/no-deprecated
80
+ const originalBranch = getBranch(treeView);
81
+ const forkBranch = originalBranch.branch();
82
+ const forkView = forkBranch.viewWith(treeViewConfiguration);
83
+ const newBranchTargetNode = sharedTreeTraverse(forkView.root, absolutePathToObjectNode);
84
+ // this.mergeDiffs(differences, newBranchTargetNode);
85
+ return { originalBranch, forkBranch, forkView, newBranchTargetNode };
86
+ }
87
+ /**
88
+ * Handles applying an array of differences to an object in the proper order and making any necessary adjustments as each diff
89
+ * is applied.
90
+ *
91
+ * @returns an array of differences that were not applied due to some kind of conflict or error.
92
+ */
93
+ mergeDiffs(diffs, objectToUpdate) {
94
+ const unappliedDiffs = new Set();
95
+ for (const diff of diffs) {
96
+ const isDiffApplied = this.applyDiff(diff, objectToUpdate);
97
+ if (isDiffApplied === false) {
98
+ unappliedDiffs.add(diff);
99
+ }
100
+ }
101
+ return unappliedDiffs;
102
+ }
103
+ /**
104
+ * Applies an individual diff to the objectToUpdate.
105
+ */
106
+ applyDiff(diff, objectToUpdate) {
107
+ const targetObject = getTargetObjectFromPath(diff.path, objectToUpdate);
108
+ if (isTreeMapNode(targetObject)) {
109
+ switch (diff.type) {
110
+ case "CHANGE":
111
+ case "CREATE": {
112
+ // This code is doing non-schema aware editing, which is not a supported feature of this API.
113
+ // Casting to any is a way to get this unsupported and rather unsafe operation to compile.
114
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
115
+ targetObject.set(diff.path[diff.path.length - 1], diff.value);
116
+ return true;
117
+ }
118
+ case "REMOVE": {
119
+ targetObject.delete(diff.path[diff.path.length - 1]);
120
+ return true;
121
+ }
122
+ default: {
123
+ throw new TypeError("Unsupported diff type for Map Tree Node");
124
+ }
125
+ }
126
+ }
127
+ else if (isTreeArrayNode(targetObject)) {
128
+ const targetIndex = diff.path[diff.path.length - 1];
129
+ const isTargetIndexValid = targetIndex >= 0 && targetIndex <= targetObject.length - 1;
130
+ switch (diff.type) {
131
+ case "CHANGE":
132
+ case "CREATE": {
133
+ if (isTargetIndexValid) {
134
+ // This code is doing non-schema aware editing, which is not a supported feature of this API.
135
+ // Casting to any is a way to get this unsupported and rather unsafe operation to compile.
136
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
137
+ targetObject.insertAt(targetIndex, diff.value);
138
+ return true;
139
+ }
140
+ else {
141
+ // This code is doing non-schema aware editing, which is not a supported feature of this API.
142
+ // Casting to any is a way to get this unsupported and rather unsafe operation to compile.
143
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
144
+ targetObject.insertAtEnd(diff.value);
145
+ console.warn("CREATE diff specified an invalid index, defaulting to pushing to end of array");
146
+ return false;
147
+ }
148
+ }
149
+ case "MOVE": {
150
+ if (isTargetIndexValid) {
151
+ if (diff.newIndex > targetIndex) {
152
+ // forward move must use i + 1
153
+ targetObject.moveToIndex(diff.newIndex + 1, targetIndex);
154
+ }
155
+ else if (diff.newIndex < targetIndex) {
156
+ // backwards move, using i directly is fine
157
+ targetObject.moveToIndex(diff.newIndex, targetIndex);
158
+ }
159
+ return true;
160
+ }
161
+ else {
162
+ console.warn("MOVE diff specified an invalid index, ignoring.");
163
+ return false;
164
+ }
165
+ }
166
+ case "REMOVE": {
167
+ if (isTargetIndexValid) {
168
+ targetObject.removeAt(targetIndex);
169
+ return true;
170
+ }
171
+ else {
172
+ console.warn("REMOVE diff specified an invalid index, ignoring.");
173
+ return false;
174
+ }
175
+ }
176
+ default: {
177
+ throw new TypeError("Unsupported diff type for Array Tree Node");
178
+ }
179
+ }
180
+ }
181
+ else if (typeof targetObject === "object" && targetObject !== null) {
182
+ switch (diff.type) {
183
+ case "CHANGE":
184
+ case "CREATE": {
185
+ targetObject[diff.path[diff.path.length - 1]] = diff.value;
186
+ return true;
187
+ }
188
+ case "REMOVE": {
189
+ // We can't use the delete keyword on a tree node.
190
+ targetObject[diff.path[diff.path.length - 1]] = undefined;
191
+ return false;
192
+ }
193
+ default: {
194
+ throw new TypeError("Unsupported diff type for Object Tree Node");
195
+ }
196
+ }
197
+ }
198
+ else {
199
+ throw new TypeError("Unsupported object type for diff application");
200
+ }
201
+ }
202
+ }
203
+ /**
204
+ * Returns the target object that the given diff should be applied to.
205
+ */
206
+ function getTargetObjectFromPath(path, object) {
207
+ let targetObject = object;
208
+ if (path.length > 1) {
209
+ targetObject = sharedTreeTraverse(object, path.slice(0, -1));
210
+ }
211
+ return targetObject;
212
+ }
213
+ //# sourceMappingURL=sharedTreeBranchManager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sharedTreeBranchManager.js","sourceRoot":"","sources":["../../src/implicit-strategy/sharedTreeBranchManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAQH,OAAO;AACN,uDAAuD;AACvD,gDAAgD;AAChD,SAAS;AAIT,sIAAsI;EACtI,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EACN,wBAAwB,EACxB,0BAA0B,EAC1B,cAAc,GAGd,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAEhF;;;;;GAKG;AACH,MAAM,OAAO,uBAAuB;IAInC,YAAmB,MAAkE;QACpF,IAAI,CAAC,YAAY,GAAG,MAAM,EAAE,YAAY,CAAC;QACzC,IAAI,CAAC,mBAAmB,GAAG,MAAM,EAAE,mBAAmB,CAAC;IACxD,CAAC;IAED;;;OAGG;IACI,OAAO,CACb,GAA4C,EAC5C,MAA2C;QAE3C,8FAA8F;QAC9F,yCAAyC;QACzC,IAAI,IAAI,CAAC,YAAY,KAAK,SAAS,EAAE,CAAC;YACrC,MAAM,GAAG,GAAG,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC;YAChD,IAAI,GAAG,CAA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CAAC;IACD,OAAO,YAAY,CAAC;AACrB,CAAC","sourcesContent":["/*!\n * Copyright (c) Microsoft Corporation and contributors. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport type {\n\tImplicitFieldSchema,\n\tTreeArrayNode,\n\tTreeMapNode,\n\tTreeViewConfiguration,\n} from \"@fluidframework/tree\";\nimport {\n\t// TODO: Migrate to newer branching API (`TreeContext`)\n\t// eslint-disable-next-line import/no-deprecated\n\tgetBranch,\n\ttype BranchableTree,\n\ttype TreeBranchFork,\n\ttype TreeViewAlpha,\n\t// eslint-disable-next-line import/no-internal-modules -- This package depends on the branching APIs in Tree which are currently alpha\n} from \"@fluidframework/tree/alpha\";\nimport type { z } from \"zod\";\n\nimport {\n\tcreateMergableDiffSeries,\n\tcreateMergableIdDiffSeries,\n\tsharedTreeDiff,\n\ttype Difference,\n\ttype ObjectPath,\n} from \"./sharedTreeDiff.js\";\nimport { isTreeMapNode, isTreeArrayNode, sharedTreeTraverse } from \"./utils.js\";\n\n/**\n * Manages determining the differences between two branches of a SharedTree represented as an actual tree node or a plain javascript object\n * and applies said differences to the original SharedTree branch.\n *\n * @alpha\n */\nexport class SharedTreeBranchManager {\n\tprivate readonly objectSchema?: z.Schema | undefined;\n\tprivate readonly nodeIdAttributeName?: string | undefined;\n\n\tpublic constructor(params?: { objectSchema?: z.Schema; nodeIdAttributeName?: string }) {\n\t\tthis.objectSchema = params?.objectSchema;\n\t\tthis.nodeIdAttributeName = params?.nodeIdAttributeName;\n\t}\n\n\t/**\n\t * Compares the differences between either two objects or a TreeNode and a plain object.\n\t * TODO: Should allow comparing two tree nodes? Should we allow comparing two plain objects? Or just leave as tree node vs object?\n\t */\n\tpublic compare(\n\t\tobj: Record<string, unknown> | TreeArrayNode,\n\t\tnewObj: Record<string, unknown> | unknown[],\n\t): Difference[] {\n\t\t// By validating that the incoming object matches the schema, we can confirm that any property\n\t\t// deletions/updates/additions are valid.\n\t\tif (this.objectSchema !== undefined) {\n\t\t\tconst res = this.objectSchema.safeParse(newObj);\n\t\t\tif (res.success === false) {\n\t\t\t\tthrow new TypeError(\"Invalid data\");\n\t\t\t}\n\t\t}\n\n\t\tconst diffTotality = sharedTreeDiff(obj as Record<string, unknown> | unknown[], newObj, {\n\t\t\tuseObjectIds:\n\t\t\t\tthis.nodeIdAttributeName === undefined\n\t\t\t\t\t? undefined\n\t\t\t\t\t: { idAttributeName: this.nodeIdAttributeName },\n\t\t\tcyclesFix: true,\n\t\t});\n\n\t\tif (this.nodeIdAttributeName !== undefined) {\n\t\t\treturn createMergableIdDiffSeries(obj, diffTotality, this.nodeIdAttributeName);\n\t\t}\n\n\t\treturn createMergableDiffSeries(diffTotality);\n\t}\n\n\t/**\n\t * Produces a set of differences based on two versions of an object, applies the changes to the first one,\n\t * and returns the set of differences.\n\t */\n\tpublic mergeObject(\n\t\tobj: Record<string, unknown> | TreeArrayNode,\n\t\tllmResponse: Record<string, unknown> | unknown[],\n\t): Difference[] {\n\t\tconst differences = this.compare(obj, llmResponse);\n\t\tthis.mergeDiffs(differences, obj);\n\t\treturn differences;\n\t}\n\n\t/**\n\t * produces a diff between two objects and merges the differences.\n\t */\n\tpublic checkoutNewMergedBranch<T extends ImplicitFieldSchema>(\n\t\ttreeView: TreeViewAlpha<T>,\n\t\ttreeViewConfiguration: TreeViewConfiguration<T>,\n\t\tabsolutePathToObjectNode: ObjectPath,\n\t\tllmResponse: Record<string, unknown> | unknown[],\n\t): {\n\t\tdifferences: Difference[];\n\t\toriginalBranch: BranchableTree;\n\t\tforkBranch: TreeBranchFork;\n\t\tforkView: TreeViewAlpha<T>;\n\t\tnewBranchTargetNode: Record<string, unknown> | TreeArrayNode;\n\t} {\n\t\t// eslint-disable-next-line import/no-deprecated\n\t\tconst originalBranch = getBranch(treeView);\n\t\tconst forkBranch = originalBranch.branch();\n\t\tconst forkView = forkBranch.viewWith(treeViewConfiguration) as TreeViewAlpha<T>;\n\n\t\tconsole.log(\"traveling to absolute path from root:\", absolutePathToObjectNode);\n\t\tconst newBranchTargetNode = sharedTreeTraverse(\n\t\t\tforkView.root as unknown as TreeMapNode | TreeArrayNode | Record<string, unknown>,\n\t\t\tabsolutePathToObjectNode,\n\t\t) as Record<string, unknown> | TreeArrayNode;\n\n\t\tconsole.log(\n\t\t\t\"initiating compare between old and new branch target nodes\",\n\t\t\t{ ...newBranchTargetNode },\n\t\t\t{ ...llmResponse },\n\t\t);\n\t\tconsole.log(\"newBranchTargetNode\", { ...newBranchTargetNode });\n\t\tconsole.log(\"llmResponse\", { ...llmResponse });\n\n\t\tconst differences = this.compare(newBranchTargetNode, llmResponse);\n\t\t// const differences = [];\n\t\tthis.mergeDiffs(differences, newBranchTargetNode);\n\n\t\treturn { differences, originalBranch, forkBranch, forkView, newBranchTargetNode };\n\t}\n\n\t/**\n\t * Creates a forked branch of a tree view.\n\t */\n\tpublic checkoutNewMergedBranchV2<T extends ImplicitFieldSchema>(\n\t\ttreeView: TreeViewAlpha<T>,\n\t\ttreeViewConfiguration: TreeViewConfiguration<T>,\n\t\tabsolutePathToObjectNode: ObjectPath,\n\t\t// differences: Difference[],\n\t): {\n\t\toriginalBranch: BranchableTree;\n\t\tforkBranch: TreeBranchFork;\n\t\tforkView: TreeViewAlpha<T>;\n\t\tnewBranchTargetNode: Record<string, unknown> | TreeArrayNode;\n\t} {\n\t\t// eslint-disable-next-line import/no-deprecated\n\t\tconst originalBranch = getBranch(treeView);\n\t\tconst forkBranch = originalBranch.branch();\n\t\tconst forkView = forkBranch.viewWith(treeViewConfiguration) as TreeViewAlpha<T>;\n\t\tconst newBranchTargetNode = sharedTreeTraverse(\n\t\t\tforkView.root as TreeMapNode | TreeArrayNode | Record<string, unknown>,\n\t\t\tabsolutePathToObjectNode,\n\t\t) as Record<string, unknown> | TreeArrayNode;\n\t\t// this.mergeDiffs(differences, newBranchTargetNode);\n\t\treturn { originalBranch, forkBranch, forkView, newBranchTargetNode };\n\t}\n\n\t/**\n\t * Handles applying an array of differences to an object in the proper order and making any necessary adjustments as each diff\n\t * is applied.\n\t *\n\t * @returns an array of differences that were not applied due to some kind of conflict or error.\n\t */\n\tpublic mergeDiffs(\n\t\tdiffs: Difference[],\n\t\tobjectToUpdate: Record<string, unknown> | TreeArrayNode,\n\t): Set<Difference> {\n\t\tconst unappliedDiffs = new Set<Difference>();\n\n\t\tfor (const diff of diffs) {\n\t\t\tconst isDiffApplied = this.applyDiff(diff, objectToUpdate);\n\n\t\t\tif (isDiffApplied === false) {\n\t\t\t\tunappliedDiffs.add(diff);\n\t\t\t}\n\t\t}\n\n\t\treturn unappliedDiffs;\n\t}\n\n\t/**\n\t * Applies an individual diff to the objectToUpdate.\n\t */\n\tpublic applyDiff(\n\t\tdiff: Difference,\n\t\tobjectToUpdate: Record<string, unknown> | TreeArrayNode,\n\t): boolean {\n\t\tconst targetObject: unknown = getTargetObjectFromPath(diff.path, objectToUpdate);\n\n\t\tif (isTreeMapNode(targetObject)) {\n\t\t\tswitch (diff.type) {\n\t\t\t\tcase \"CHANGE\":\n\t\t\t\tcase \"CREATE\": {\n\t\t\t\t\t// This code is doing non-schema aware editing, which is not a supported feature of this API.\n\t\t\t\t\t// Casting to any is a way to get this unsupported and rather unsafe operation to compile.\n\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any\n\t\t\t\t\ttargetObject.set(diff.path[diff.path.length - 1] as string, diff.value as any);\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tcase \"REMOVE\": {\n\t\t\t\t\ttargetObject.delete(diff.path[diff.path.length - 1] as string);\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tthrow new TypeError(\"Unsupported diff type for Map Tree Node\");\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (isTreeArrayNode(targetObject)) {\n\t\t\tconst targetIndex = diff.path[diff.path.length - 1] as number;\n\t\t\tconst isTargetIndexValid = targetIndex >= 0 && targetIndex <= targetObject.length - 1;\n\t\t\tswitch (diff.type) {\n\t\t\t\tcase \"CHANGE\":\n\t\t\t\tcase \"CREATE\": {\n\t\t\t\t\tif (isTargetIndexValid) {\n\t\t\t\t\t\t// This code is doing non-schema aware editing, which is not a supported feature of this API.\n\t\t\t\t\t\t// Casting to any is a way to get this unsupported and rather unsafe operation to compile.\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any\n\t\t\t\t\t\ttargetObject.insertAt(targetIndex, diff.value as any);\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// This code is doing non-schema aware editing, which is not a supported feature of this API.\n\t\t\t\t\t\t// Casting to any is a way to get this unsupported and rather unsafe operation to compile.\n\t\t\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any\n\t\t\t\t\t\ttargetObject.insertAtEnd(diff.value as any);\n\t\t\t\t\t\tconsole.warn(\n\t\t\t\t\t\t\t\"CREATE diff specified an invalid index, defaulting to pushing to end of array\",\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcase \"MOVE\": {\n\t\t\t\t\tif (isTargetIndexValid) {\n\t\t\t\t\t\tif (diff.newIndex > targetIndex) {\n\t\t\t\t\t\t\t// forward move must use i + 1\n\t\t\t\t\t\t\ttargetObject.moveToIndex(diff.newIndex + 1, targetIndex);\n\t\t\t\t\t\t} else if (diff.newIndex < targetIndex) {\n\t\t\t\t\t\t\t// backwards move, using i directly is fine\n\t\t\t\t\t\t\ttargetObject.moveToIndex(diff.newIndex, targetIndex);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.warn(\"MOVE diff specified an invalid index, ignoring.\");\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tcase \"REMOVE\": {\n\t\t\t\t\tif (isTargetIndexValid) {\n\t\t\t\t\t\ttargetObject.removeAt(targetIndex);\n\t\t\t\t\t\treturn true;\n\t\t\t\t\t} else {\n\t\t\t\t\t\tconsole.warn(\"REMOVE diff specified an invalid index, ignoring.\");\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tthrow new TypeError(\"Unsupported diff type for Array Tree Node\");\n\t\t\t\t}\n\t\t\t}\n\t\t} else if (typeof targetObject === \"object\" && targetObject !== null) {\n\t\t\tswitch (diff.type) {\n\t\t\t\tcase \"CHANGE\":\n\t\t\t\tcase \"CREATE\": {\n\t\t\t\t\ttargetObject[diff.path[diff.path.length - 1] as string] = diff.value;\n\t\t\t\t\treturn true;\n\t\t\t\t}\n\t\t\t\tcase \"REMOVE\": {\n\t\t\t\t\t// We can't use the delete keyword on a tree node.\n\t\t\t\t\ttargetObject[diff.path[diff.path.length - 1] as string] = undefined;\n\t\t\t\t\treturn false;\n\t\t\t\t}\n\t\t\t\tdefault: {\n\t\t\t\t\tthrow new TypeError(\"Unsupported diff type for Object Tree Node\");\n\t\t\t\t}\n\t\t\t}\n\t\t} else {\n\t\t\tthrow new TypeError(\"Unsupported object type for diff application\");\n\t\t}\n\t}\n}\n\n/**\n * Returns the target object that the given diff should be applied to.\n */\nfunction getTargetObjectFromPath(\n\tpath: ObjectPath,\n\tobject: Record<string, unknown> | TreeArrayNode,\n): unknown {\n\tlet targetObject: unknown = object;\n\tif (path.length > 1) {\n\t\ttargetObject = sharedTreeTraverse(object, path.slice(0, -1));\n\t}\n\treturn targetObject;\n}\n"]}
@@ -0,0 +1,102 @@
1
+ /*!
2
+ * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+ /**
6
+ * Represents a path through a tree of objects.
7
+ * number values represent array indices whereas string values represent object keys.
8
+ *
9
+ * @alpha
10
+ */
11
+ export type ObjectPath = (string | number)[];
12
+ /**
13
+ * Represents a create operation between two branches of a tree.
14
+ * Meaning that an attribute (a shared tree node) was identified as being created.
15
+ *
16
+ * @alpha
17
+ */
18
+ export interface DifferenceCreate {
19
+ type: "CREATE";
20
+ path: ObjectPath;
21
+ value: unknown;
22
+ }
23
+ /**
24
+ * Represents a remove operation between two branches of a tree.
25
+ * Meaning that an attribute (a shared tree node) was identified as being deleted.
26
+ * When using object ids, removes are idenitified by an object with a given id no longer existing.
27
+ *
28
+ * @alpha
29
+ */
30
+ export interface DifferenceRemove {
31
+ type: "REMOVE";
32
+ path: ObjectPath;
33
+ oldValue: unknown;
34
+ objectId?: string | number | undefined;
35
+ }
36
+ /**
37
+ * Represents a change operation between two branches of a tree.
38
+ * Meaning that an attribute (a shared tree node) was identified as being changed from one value to another.
39
+ *
40
+ * @alpha
41
+ */
42
+ export interface DifferenceChange {
43
+ type: "CHANGE";
44
+ path: ObjectPath;
45
+ value: unknown;
46
+ oldValue: unknown;
47
+ objectId?: string | number | undefined;
48
+ }
49
+ /**
50
+ * Represents a move operation between two branches of a tree.
51
+ * Meaning that an object (shared tree node) was identified as being moved from one index to another based on its unique id.
52
+ *
53
+ * @alpha
54
+ */
55
+ export interface DifferenceMove {
56
+ type: "MOVE";
57
+ path: ObjectPath;
58
+ newIndex: number;
59
+ value: unknown;
60
+ objectId?: string | number | undefined;
61
+ }
62
+ /**
63
+ * Union for all possible difference types.
64
+ *
65
+ * @alpha
66
+ */
67
+ export type Difference = DifferenceCreate | DifferenceRemove | DifferenceChange | DifferenceMove;
68
+ /**
69
+ * Options for tree diffing.
70
+ * @alpha
71
+ */
72
+ export interface Options {
73
+ cyclesFix: boolean;
74
+ useObjectIds?: {
75
+ idAttributeName: string;
76
+ } | undefined;
77
+ }
78
+ /**
79
+ * Compares two objects and returns an array of differences between them.
80
+ *
81
+ * @alpha
82
+ */
83
+ export declare function sharedTreeDiff(obj: Record<string, unknown> | unknown[], newObj: Record<string, unknown> | unknown[], options?: Options, _stack?: (Record<string, unknown> | unknown[])[]): Difference[];
84
+ /**
85
+ * Creates a set of mergeable diffs from a series of diffs produced by {@link sharedTreeDiff}
86
+ * that are using the object ID strategy. These diffs don't need any modifications to be applied to the old object.
87
+ *
88
+ * @alpha
89
+ */
90
+ export declare function createMergableIdDiffSeries(oldObject: unknown, diffs: Difference[], idAttributeName: string | number): Difference[];
91
+ /**
92
+ * Creates a set of mergeable diffs from a series of diffs produced by {@link sharedTreeDiff}
93
+ * that AREN'T using the object ID strategy. These diffs don't need any modifications to be applied to the old object.
94
+ *
95
+ * @alpha
96
+ */
97
+ export declare function createMergableDiffSeries(diffs: Difference[]): Difference[];
98
+ /**
99
+ * Determines if a given difference is on an array.
100
+ */
101
+ export declare function isDiffOnArray(diff: Difference): boolean;
102
+ //# sourceMappingURL=sharedTreeDiff.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sharedTreeDiff.d.ts","sourceRoot":"","sources":["../../src/implicit-strategy/sharedTreeDiff.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH;;;;;GAKG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;CACf;AAED;;;;;;GAMG;AACH,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACvC;AAED;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAChC,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,OAAO,CAAC;IACf,QAAQ,EAAE,OAAO,CAAC;IAClB,QAAQ,CAAC,EAAE,MAAM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