@tdesign-react/chat 1.0.1 → 1.0.2-alpha.1

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 (166) hide show
  1. package/es/_util/reactify.js +1 -1
  2. package/es/_util/useDynamicStyle.js +1 -1
  3. package/es/chat-actionbar/_example-js/base.d.ts +2 -0
  4. package/es/chat-actionbar/_example-js/custom.d.ts +2 -0
  5. package/es/chat-actionbar/_example-js/style.d.ts +2 -0
  6. package/es/chat-actionbar/index.js +1 -1
  7. package/es/chat-attachments/_example-js/base.d.ts +2 -0
  8. package/es/chat-attachments/_example-js/scroll-x.d.ts +2 -0
  9. package/es/chat-attachments/_example-js/scroll-y.d.ts +2 -0
  10. package/es/chat-attachments/index.js +1 -1
  11. package/es/chat-engine/_example-js/agui-basic.d.ts +10 -0
  12. package/es/chat-engine/_example-js/agui-comprehensive.d.ts +2 -0
  13. package/es/chat-engine/_example-js/agui-test.d.ts +11 -0
  14. package/es/chat-engine/_example-js/agui-toolcall.d.ts +5 -0
  15. package/es/chat-engine/_example-js/agui-videoclip.d.ts +8 -0
  16. package/es/chat-engine/_example-js/agui.d.ts +1 -0
  17. package/es/chat-engine/_example-js/basic.d.ts +9 -0
  18. package/es/chat-engine/_example-js/components/HotelCard.d.ts +3 -0
  19. package/es/chat-engine/_example-js/components/HumanInputForm.d.ts +6 -0
  20. package/es/chat-engine/_example-js/components/HumanInputResult.d.ts +3 -0
  21. package/es/chat-engine/_example-js/components/ItineraryCard.d.ts +3 -0
  22. package/es/chat-engine/_example-js/components/PlanningStatePanel.d.ts +4 -0
  23. package/es/chat-engine/_example-js/components/Toolcall.d.ts +4 -0
  24. package/es/chat-engine/_example-js/components/WeatherCard.d.ts +3 -0
  25. package/es/chat-engine/_example-js/components/login.d.ts +1 -0
  26. package/es/chat-engine/_example-js/comprehensive.d.ts +12 -0
  27. package/es/chat-engine/_example-js/custom-content.d.ts +1 -0
  28. package/es/chat-engine/_example-js/hookComponent.d.ts +1 -0
  29. package/es/chat-engine/_example-js/initial-messages.d.ts +9 -0
  30. package/es/chat-engine/_example-js/instance-methods.d.ts +13 -0
  31. package/es/chat-engine/_example-js/travel-actions.d.ts +91 -0
  32. package/es/chat-engine/_example-js/travelToolcall.d.ts +1 -0
  33. package/es/chat-engine/components/provider/agent-state.js +1 -1
  34. package/es/chat-engine/components/toolcall/index.js +1 -1
  35. package/es/chat-engine/components/toolcall/registry.js +1 -1
  36. package/es/chat-engine/components/toolcall/render.js +1 -1
  37. package/es/chat-engine/components/toolcall/types.js +1 -1
  38. package/es/chat-engine/core/adapters/agui/event-mapper.d.ts +104 -0
  39. package/es/chat-engine/core/adapters/agui/event-mapper.js +348 -0
  40. package/es/chat-engine/core/adapters/agui/event-mapper.js.map +1 -0
  41. package/es/chat-engine/core/adapters/agui/events.d.ts +1394 -0
  42. package/es/chat-engine/core/adapters/agui/events.js +181 -0
  43. package/es/chat-engine/core/adapters/agui/events.js.map +1 -0
  44. package/es/chat-engine/core/adapters/agui/index.d.ts +75 -0
  45. package/es/chat-engine/core/adapters/agui/index.js +165 -0
  46. package/es/chat-engine/core/adapters/agui/index.js.map +1 -0
  47. package/es/chat-engine/core/adapters/agui/state-manager.d.ts +99 -0
  48. package/es/chat-engine/core/adapters/agui/state-manager.js +168 -0
  49. package/es/chat-engine/core/adapters/agui/state-manager.js.map +1 -0
  50. package/es/chat-engine/core/adapters/agui/types.d.ts +760 -0
  51. package/es/chat-engine/core/adapters/agui/types.js +89 -0
  52. package/es/chat-engine/core/adapters/agui/types.js.map +1 -0
  53. package/es/chat-engine/core/adapters/agui/utils.d.ts +180 -0
  54. package/es/chat-engine/core/adapters/agui/utils.js +302 -0
  55. package/es/chat-engine/core/adapters/agui/utils.js.map +1 -0
  56. package/es/chat-engine/core/index.d.ts +132 -0
  57. package/es/chat-engine/core/index.js +597 -0
  58. package/es/chat-engine/core/index.js.map +1 -0
  59. package/es/chat-engine/core/processor/index.d.ts +20 -0
  60. package/es/chat-engine/core/processor/index.js +146 -0
  61. package/es/chat-engine/core/processor/index.js.map +1 -0
  62. package/es/chat-engine/core/server/batch-client.d.ts +20 -0
  63. package/es/chat-engine/core/server/batch-client.js +114 -0
  64. package/es/chat-engine/core/server/batch-client.js.map +1 -0
  65. package/es/chat-engine/core/server/connection-manager.d.ts +39 -0
  66. package/es/chat-engine/core/server/connection-manager.js +84 -0
  67. package/es/chat-engine/core/server/connection-manager.js.map +1 -0
  68. package/es/chat-engine/core/server/errors.d.ts +22 -0
  69. package/es/chat-engine/core/server/errors.js +80 -0
  70. package/es/chat-engine/core/server/errors.js.map +1 -0
  71. package/es/chat-engine/core/server/index.d.ts +11 -0
  72. package/es/chat-engine/core/server/index.js +26 -0
  73. package/es/chat-engine/core/server/index.js.map +1 -0
  74. package/es/chat-engine/core/server/llm-service.d.ts +44 -0
  75. package/es/chat-engine/core/server/llm-service.js +198 -0
  76. package/es/chat-engine/core/server/llm-service.js.map +1 -0
  77. package/es/chat-engine/core/server/sse-client.d.ts +77 -0
  78. package/es/chat-engine/core/server/sse-client.js +362 -0
  79. package/es/chat-engine/core/server/sse-client.js.map +1 -0
  80. package/es/chat-engine/core/server/sse-parser.d.ts +49 -0
  81. package/es/chat-engine/core/server/sse-parser.js +116 -0
  82. package/es/chat-engine/core/server/sse-parser.js.map +1 -0
  83. package/es/chat-engine/core/server/types.d.ts +54 -0
  84. package/es/chat-engine/core/server/types.js +28 -0
  85. package/es/chat-engine/core/server/types.js.map +1 -0
  86. package/es/chat-engine/core/store/message.d.ts +27 -0
  87. package/es/chat-engine/core/store/message.js +263 -0
  88. package/es/chat-engine/core/store/message.js.map +1 -0
  89. package/es/chat-engine/core/store/model.d.ts +8 -0
  90. package/es/chat-engine/core/store/model.js +65 -0
  91. package/es/chat-engine/core/store/model.js.map +1 -0
  92. package/es/chat-engine/core/store/reactiveState.d.ts +52 -0
  93. package/es/chat-engine/core/store/reactiveState.js +1359 -0
  94. package/es/chat-engine/core/store/reactiveState.js.map +1 -0
  95. package/es/chat-engine/core/type.d.ts +248 -0
  96. package/es/chat-engine/core/type.js +7 -0
  97. package/es/chat-engine/core/type.js.map +1 -0
  98. package/es/chat-engine/core/utils/eventEmitter.d.ts +10 -0
  99. package/es/chat-engine/core/utils/eventEmitter.js +67 -0
  100. package/es/chat-engine/core/utils/eventEmitter.js.map +1 -0
  101. package/es/chat-engine/core/utils/index.d.ts +32 -0
  102. package/es/chat-engine/core/utils/index.js +996 -0
  103. package/es/chat-engine/core/utils/index.js.map +1 -0
  104. package/es/chat-engine/core/utils/logger.d.ts +30 -0
  105. package/es/chat-engine/core/utils/logger.js +87 -0
  106. package/es/chat-engine/core/utils/logger.js.map +1 -0
  107. package/es/chat-engine/hooks/useAgentState.js +1 -1
  108. package/es/chat-engine/hooks/useAgentToolcall.js +1 -1
  109. package/es/chat-engine/hooks/useChat.js +1 -1
  110. package/es/chat-engine/index.js +1 -1
  111. package/es/chat-filecard/_example-js/base.d.ts +1 -0
  112. package/es/chat-filecard/index.js +1 -1
  113. package/es/chat-loading/_example-js/base.d.ts +2 -0
  114. package/es/chat-loading/_example-js/text.d.ts +2 -0
  115. package/es/chat-loading/index.js +1 -1
  116. package/es/chat-markdown/_example-js/base.d.ts +1 -0
  117. package/es/chat-markdown/_example-js/custom.d.ts +2 -0
  118. package/es/chat-markdown/_example-js/event.d.ts +1 -0
  119. package/es/chat-markdown/_example-js/footnote.d.ts +2 -0
  120. package/es/chat-markdown/_example-js/plugin.d.ts +2 -0
  121. package/es/chat-markdown/_example-js/theme.d.ts +2 -0
  122. package/es/chat-markdown/index.js +1 -1
  123. package/es/chat-message/_example-js/action.d.ts +1 -0
  124. package/es/chat-message/_example-js/base.d.ts +1 -0
  125. package/es/chat-message/_example-js/configure.d.ts +1 -0
  126. package/es/chat-message/_example-js/content.d.ts +1 -0
  127. package/es/chat-message/_example-js/custom.d.ts +1 -0
  128. package/es/chat-message/_example-js/handle-actions.d.ts +7 -0
  129. package/es/chat-message/_example-js/status.d.ts +1 -0
  130. package/es/chat-message/_example-js/think.d.ts +1 -0
  131. package/es/chat-message/index.js +1 -1
  132. package/es/chat-sender/_example-js/attachment.d.ts +2 -0
  133. package/es/chat-sender/_example-js/base.d.ts +2 -0
  134. package/es/chat-sender/_example-js/custom.d.ts +2 -0
  135. package/es/chat-sender/index.js +1 -1
  136. package/es/chat-thinking/_example-js/base.d.ts +1 -0
  137. package/es/chat-thinking/_example-js/style.d.ts +1 -0
  138. package/es/chat-thinking/index.js +1 -1
  139. package/es/chatbot/_example-js/agent.d.ts +1 -0
  140. package/es/chatbot/_example-js/agui.d.ts +13 -0
  141. package/es/chatbot/_example-js/backup/travel.d.ts +1 -0
  142. package/es/chatbot/_example-js/basic.d.ts +1 -0
  143. package/es/chatbot/_example-js/code.d.ts +1 -0
  144. package/es/chatbot/_example-js/components/ItineraryCard.d.ts +3 -0
  145. package/es/chatbot/_example-js/components/login.d.ts +1 -0
  146. package/es/chatbot/_example-js/comprehensive.d.ts +1 -0
  147. package/es/chatbot/_example-js/custom-content.d.ts +1 -0
  148. package/es/chatbot/_example-js/custom-merge.d.ts +1 -0
  149. package/es/chatbot/_example-js/custom.d.ts +1 -0
  150. package/es/chatbot/_example-js/docs.d.ts +1 -0
  151. package/es/chatbot/_example-js/image.d.ts +1 -0
  152. package/es/chatbot/_example-js/initial-messages.d.ts +9 -0
  153. package/es/chatbot/_example-js/instance-methods.d.ts +14 -0
  154. package/es/chatbot/_example-js/nostream.d.ts +1 -0
  155. package/es/chatbot/_example-js/quick-start.d.ts +9 -0
  156. package/es/chatbot/_example-js/research.d.ts +1 -0
  157. package/es/chatbot/_example-js/role-message-config.d.ts +22 -0
  158. package/es/chatbot/_example-js/sender-config.d.ts +20 -0
  159. package/es/chatbot/_example-js/service-config.d.ts +17 -0
  160. package/es/chatbot/_example-js/simple.d.ts +1 -0
  161. package/es/chatbot/_example-js/utils/messageRenderer.d.ts +5 -0
  162. package/es/chatbot/index.js +1 -1
  163. package/es/index.d.ts +0 -1
  164. package/es/index.js +1 -1
  165. package/es/style/index.js +1 -1
  166. package/package.json +4 -6
@@ -0,0 +1,996 @@
1
+ /**
2
+ * tdesign v1.0.1
3
+ * (c) 2025 tdesign
4
+ * @license MIT
5
+ */
6
+
7
+ import _typeof from '@babel/runtime/helpers/typeof';
8
+
9
+ /*!
10
+ * https://github.com/Starcounter-Jack/JSON-Patch
11
+ * (c) 2017-2022 Joachim Wester
12
+ * MIT licensed
13
+ */
14
+ var __extends = undefined && undefined.__extends || function () {
15
+ var _extendStatics = function extendStatics(d, b) {
16
+ _extendStatics = Object.setPrototypeOf || {
17
+ __proto__: []
18
+ } instanceof Array && function (d, b) {
19
+ d.__proto__ = b;
20
+ } || function (d, b) {
21
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
22
+ };
23
+ return _extendStatics(d, b);
24
+ };
25
+ return function (d, b) {
26
+ _extendStatics(d, b);
27
+ function __() {
28
+ this.constructor = d;
29
+ }
30
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
31
+ };
32
+ }();
33
+ var _hasOwnProperty = Object.prototype.hasOwnProperty;
34
+ function hasOwnProperty(obj, key) {
35
+ return _hasOwnProperty.call(obj, key);
36
+ }
37
+ function _objectKeys(obj) {
38
+ if (Array.isArray(obj)) {
39
+ var keys_1 = new Array(obj.length);
40
+ for (var k = 0; k < keys_1.length; k++) {
41
+ keys_1[k] = "" + k;
42
+ }
43
+ return keys_1;
44
+ }
45
+ if (Object.keys) {
46
+ return Object.keys(obj);
47
+ }
48
+ var keys = [];
49
+ for (var i in obj) {
50
+ if (hasOwnProperty(obj, i)) {
51
+ keys.push(i);
52
+ }
53
+ }
54
+ return keys;
55
+ }
56
+ ;
57
+ /**
58
+ * Deeply clone the object.
59
+ * https://jsperf.com/deep-copy-vs-json-stringify-json-parse/25 (recursiveDeepCopy)
60
+ * @param {any} obj value to clone
61
+ * @return {any} cloned obj
62
+ */
63
+ function _deepClone(obj) {
64
+ switch (_typeof(obj)) {
65
+ case "object":
66
+ return JSON.parse(JSON.stringify(obj));
67
+ //Faster than ES5 clone - http://jsperf.com/deep-cloning-of-objects/5
68
+ case "undefined":
69
+ return null;
70
+ //this is how JSON.stringify behaves for array items
71
+ default:
72
+ return obj;
73
+ //no need to clone primitives
74
+ }
75
+ }
76
+ //3x faster than cached /^\d+$/.test(str)
77
+ function isInteger(str) {
78
+ var i = 0;
79
+ var len = str.length;
80
+ var charCode;
81
+ while (i < len) {
82
+ charCode = str.charCodeAt(i);
83
+ if (charCode >= 48 && charCode <= 57) {
84
+ i++;
85
+ continue;
86
+ }
87
+ return false;
88
+ }
89
+ return true;
90
+ }
91
+ /**
92
+ * Escapes a json pointer path
93
+ * @param path The raw pointer
94
+ * @return the Escaped path
95
+ */
96
+ function escapePathComponent(path) {
97
+ if (path.indexOf('/') === -1 && path.indexOf('~') === -1) return path;
98
+ return path.replace(/~/g, '~0').replace(/\//g, '~1');
99
+ }
100
+ /**
101
+ * Unescapes a json pointer path
102
+ * @param path The escaped pointer
103
+ * @return The unescaped path
104
+ */
105
+ function unescapePathComponent(path) {
106
+ return path.replace(/~1/g, '/').replace(/~0/g, '~');
107
+ }
108
+ function _getPathRecursive(root, obj) {
109
+ var found;
110
+ for (var key in root) {
111
+ if (hasOwnProperty(root, key)) {
112
+ if (root[key] === obj) {
113
+ return escapePathComponent(key) + '/';
114
+ } else if (_typeof(root[key]) === 'object') {
115
+ found = _getPathRecursive(root[key], obj);
116
+ if (found != '') {
117
+ return escapePathComponent(key) + '/' + found;
118
+ }
119
+ }
120
+ }
121
+ }
122
+ return '';
123
+ }
124
+ function getPath(root, obj) {
125
+ if (root === obj) {
126
+ return '/';
127
+ }
128
+ var path = _getPathRecursive(root, obj);
129
+ if (path === '') {
130
+ throw new Error("Object not found in root");
131
+ }
132
+ return "/" + path;
133
+ }
134
+ /**
135
+ * Recursively checks whether an object has any undefined values inside.
136
+ */
137
+ function hasUndefined(obj) {
138
+ if (obj === undefined) {
139
+ return true;
140
+ }
141
+ if (obj) {
142
+ if (Array.isArray(obj)) {
143
+ for (var i_1 = 0, len = obj.length; i_1 < len; i_1++) {
144
+ if (hasUndefined(obj[i_1])) {
145
+ return true;
146
+ }
147
+ }
148
+ } else if (_typeof(obj) === "object") {
149
+ var objKeys = _objectKeys(obj);
150
+ var objKeysLength = objKeys.length;
151
+ for (var i = 0; i < objKeysLength; i++) {
152
+ if (hasUndefined(obj[objKeys[i]])) {
153
+ return true;
154
+ }
155
+ }
156
+ }
157
+ }
158
+ return false;
159
+ }
160
+ function patchErrorMessageFormatter(message, args) {
161
+ var messageParts = [message];
162
+ for (var key in args) {
163
+ var value = _typeof(args[key]) === 'object' ? JSON.stringify(args[key], null, 2) : args[key]; // pretty print
164
+ if (typeof value !== 'undefined') {
165
+ messageParts.push(key + ": " + value);
166
+ }
167
+ }
168
+ return messageParts.join('\n');
169
+ }
170
+ var PatchError = /** @class */function (_super) {
171
+ __extends(PatchError, _super);
172
+ function PatchError(message, name, index, operation, tree) {
173
+ var _newTarget = this.constructor;
174
+ var _this = _super.call(this, patchErrorMessageFormatter(message, {
175
+ name: name,
176
+ index: index,
177
+ operation: operation,
178
+ tree: tree
179
+ })) || this;
180
+ _this.name = name;
181
+ _this.index = index;
182
+ _this.operation = operation;
183
+ _this.tree = tree;
184
+ Object.setPrototypeOf(_this, _newTarget.prototype); // restore prototype chain, see https://stackoverflow.com/a/48342359
185
+ _this.message = patchErrorMessageFormatter(message, {
186
+ name: name,
187
+ index: index,
188
+ operation: operation,
189
+ tree: tree
190
+ });
191
+ return _this;
192
+ }
193
+ return PatchError;
194
+ }(Error);
195
+
196
+ var JsonPatchError = PatchError;
197
+ var deepClone = _deepClone;
198
+ /* We use a Javascript hash to store each
199
+ function. Each hash entry (property) uses
200
+ the operation identifiers specified in rfc6902.
201
+ In this way, we can map each patch operation
202
+ to its dedicated function in efficient way.
203
+ */
204
+ /* The operations applicable to an object */
205
+ var objOps = {
206
+ add: function add(obj, key, document) {
207
+ obj[key] = this.value;
208
+ return {
209
+ newDocument: document
210
+ };
211
+ },
212
+ remove: function remove(obj, key, document) {
213
+ var removed = obj[key];
214
+ delete obj[key];
215
+ return {
216
+ newDocument: document,
217
+ removed: removed
218
+ };
219
+ },
220
+ replace: function replace(obj, key, document) {
221
+ var removed = obj[key];
222
+ obj[key] = this.value;
223
+ return {
224
+ newDocument: document,
225
+ removed: removed
226
+ };
227
+ },
228
+ move: function move(obj, key, document) {
229
+ /* in case move target overwrites an existing value,
230
+ return the removed value, this can be taxing performance-wise,
231
+ and is potentially unneeded */
232
+ var removed = getValueByPointer(document, this.path);
233
+ if (removed) {
234
+ removed = _deepClone(removed);
235
+ }
236
+ var originalValue = applyOperation(document, {
237
+ op: "remove",
238
+ path: this.from
239
+ }).removed;
240
+ applyOperation(document, {
241
+ op: "add",
242
+ path: this.path,
243
+ value: originalValue
244
+ });
245
+ return {
246
+ newDocument: document,
247
+ removed: removed
248
+ };
249
+ },
250
+ copy: function copy(obj, key, document) {
251
+ var valueToCopy = getValueByPointer(document, this.from);
252
+ // enforce copy by value so further operations don't affect source (see issue #177)
253
+ applyOperation(document, {
254
+ op: "add",
255
+ path: this.path,
256
+ value: _deepClone(valueToCopy)
257
+ });
258
+ return {
259
+ newDocument: document
260
+ };
261
+ },
262
+ test: function test(obj, key, document) {
263
+ return {
264
+ newDocument: document,
265
+ test: _areEquals(obj[key], this.value)
266
+ };
267
+ },
268
+ _get: function _get(obj, key, document) {
269
+ this.value = obj[key];
270
+ return {
271
+ newDocument: document
272
+ };
273
+ }
274
+ };
275
+ /* The operations applicable to an array. Many are the same as for the object */
276
+ var arrOps = {
277
+ add: function add(arr, i, document) {
278
+ if (isInteger(i)) {
279
+ arr.splice(i, 0, this.value);
280
+ } else {
281
+ // array props
282
+ arr[i] = this.value;
283
+ }
284
+ // this may be needed when using '-' in an array
285
+ return {
286
+ newDocument: document,
287
+ index: i
288
+ };
289
+ },
290
+ remove: function remove(arr, i, document) {
291
+ var removedList = arr.splice(i, 1);
292
+ return {
293
+ newDocument: document,
294
+ removed: removedList[0]
295
+ };
296
+ },
297
+ replace: function replace(arr, i, document) {
298
+ var removed = arr[i];
299
+ arr[i] = this.value;
300
+ return {
301
+ newDocument: document,
302
+ removed: removed
303
+ };
304
+ },
305
+ move: objOps.move,
306
+ copy: objOps.copy,
307
+ test: objOps.test,
308
+ _get: objOps._get
309
+ };
310
+ /**
311
+ * Retrieves a value from a JSON document by a JSON pointer.
312
+ * Returns the value.
313
+ *
314
+ * @param document The document to get the value from
315
+ * @param pointer an escaped JSON pointer
316
+ * @return The retrieved value
317
+ */
318
+ function getValueByPointer(document, pointer) {
319
+ if (pointer == '') {
320
+ return document;
321
+ }
322
+ var getOriginalDestination = {
323
+ op: "_get",
324
+ path: pointer
325
+ };
326
+ applyOperation(document, getOriginalDestination);
327
+ return getOriginalDestination.value;
328
+ }
329
+ /**
330
+ * Apply a single JSON Patch Operation on a JSON document.
331
+ * Returns the {newDocument, result} of the operation.
332
+ * It modifies the `document` and `operation` objects - it gets the values by reference.
333
+ * If you would like to avoid touching your values, clone them:
334
+ * `jsonpatch.applyOperation(document, jsonpatch._deepClone(operation))`.
335
+ *
336
+ * @param document The document to patch
337
+ * @param operation The operation to apply
338
+ * @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
339
+ * @param mutateDocument Whether to mutate the original document or clone it before applying
340
+ * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
341
+ * @return `{newDocument, result}` after the operation
342
+ */
343
+ function applyOperation(document, operation, validateOperation, mutateDocument, banPrototypeModifications, index) {
344
+ if (validateOperation === void 0) {
345
+ validateOperation = false;
346
+ }
347
+ if (mutateDocument === void 0) {
348
+ mutateDocument = true;
349
+ }
350
+ if (banPrototypeModifications === void 0) {
351
+ banPrototypeModifications = true;
352
+ }
353
+ if (index === void 0) {
354
+ index = 0;
355
+ }
356
+ if (validateOperation) {
357
+ if (typeof validateOperation == 'function') {
358
+ validateOperation(operation, 0, document, operation.path);
359
+ } else {
360
+ validator(operation, 0);
361
+ }
362
+ }
363
+ /* ROOT OPERATIONS */
364
+ if (operation.path === "") {
365
+ var returnValue = {
366
+ newDocument: document
367
+ };
368
+ if (operation.op === 'add') {
369
+ returnValue.newDocument = operation.value;
370
+ return returnValue;
371
+ } else if (operation.op === 'replace') {
372
+ returnValue.newDocument = operation.value;
373
+ returnValue.removed = document; //document we removed
374
+ return returnValue;
375
+ } else if (operation.op === 'move' || operation.op === 'copy') {
376
+ // it's a move or copy to root
377
+ returnValue.newDocument = getValueByPointer(document, operation.from); // get the value by json-pointer in `from` field
378
+ if (operation.op === 'move') {
379
+ // report removed item
380
+ returnValue.removed = document;
381
+ }
382
+ return returnValue;
383
+ } else if (operation.op === 'test') {
384
+ returnValue.test = _areEquals(document, operation.value);
385
+ if (returnValue.test === false) {
386
+ throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document);
387
+ }
388
+ returnValue.newDocument = document;
389
+ return returnValue;
390
+ } else if (operation.op === 'remove') {
391
+ // a remove on root
392
+ returnValue.removed = document;
393
+ returnValue.newDocument = null;
394
+ return returnValue;
395
+ } else if (operation.op === '_get') {
396
+ operation.value = document;
397
+ return returnValue;
398
+ } else {
399
+ /* bad operation */
400
+ if (validateOperation) {
401
+ throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document);
402
+ } else {
403
+ return returnValue;
404
+ }
405
+ }
406
+ } /* END ROOT OPERATIONS */else {
407
+ if (!mutateDocument) {
408
+ document = _deepClone(document);
409
+ }
410
+ var path = operation.path || "";
411
+ var keys = path.split('/');
412
+ var obj = document;
413
+ var t = 1; //skip empty element - http://jsperf.com/to-shift-or-not-to-shift
414
+ var len = keys.length;
415
+ var existingPathFragment = undefined;
416
+ var key = void 0;
417
+ var validateFunction = void 0;
418
+ if (typeof validateOperation == 'function') {
419
+ validateFunction = validateOperation;
420
+ } else {
421
+ validateFunction = validator;
422
+ }
423
+ while (true) {
424
+ key = keys[t];
425
+ if (key && key.indexOf('~') != -1) {
426
+ key = unescapePathComponent(key);
427
+ }
428
+ if (banPrototypeModifications && (key == '__proto__' || key == 'prototype' && t > 0 && keys[t - 1] == 'constructor')) {
429
+ throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
430
+ }
431
+ if (validateOperation) {
432
+ if (existingPathFragment === undefined) {
433
+ if (obj[key] === undefined) {
434
+ existingPathFragment = keys.slice(0, t).join('/');
435
+ } else if (t == len - 1) {
436
+ existingPathFragment = operation.path;
437
+ }
438
+ if (existingPathFragment !== undefined) {
439
+ validateFunction(operation, 0, document, existingPathFragment);
440
+ }
441
+ }
442
+ }
443
+ t++;
444
+ if (Array.isArray(obj)) {
445
+ if (key === '-') {
446
+ key = obj.length;
447
+ } else {
448
+ if (validateOperation && !isInteger(key)) {
449
+ throw new JsonPatchError("Expected an unsigned base-10 integer value, making the new referenced value the array element with the zero-based index", "OPERATION_PATH_ILLEGAL_ARRAY_INDEX", index, operation, document);
450
+ } // only parse key when it's an integer for `arr.prop` to work
451
+ else if (isInteger(key)) {
452
+ key = ~~key;
453
+ }
454
+ }
455
+ if (t >= len) {
456
+ if (validateOperation && operation.op === "add" && key > obj.length) {
457
+ throw new JsonPatchError("The specified index MUST NOT be greater than the number of elements in the array", "OPERATION_VALUE_OUT_OF_BOUNDS", index, operation, document);
458
+ }
459
+ var returnValue = arrOps[operation.op].call(operation, obj, key, document); // Apply patch
460
+ if (returnValue.test === false) {
461
+ throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document);
462
+ }
463
+ return returnValue;
464
+ }
465
+ } else {
466
+ if (t >= len) {
467
+ var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
468
+ if (returnValue.test === false) {
469
+ throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document);
470
+ }
471
+ return returnValue;
472
+ }
473
+ }
474
+ obj = obj[key];
475
+ // If we have more keys in the path, but the next value isn't a non-null object,
476
+ // throw an OPERATION_PATH_UNRESOLVABLE error instead of iterating again.
477
+ if (validateOperation && t < len && (!obj || _typeof(obj) !== "object")) {
478
+ throw new JsonPatchError('Cannot perform operation at the desired path', 'OPERATION_PATH_UNRESOLVABLE', index, operation, document);
479
+ }
480
+ }
481
+ }
482
+ }
483
+ /**
484
+ * Apply a full JSON Patch array on a JSON document.
485
+ * Returns the {newDocument, result} of the patch.
486
+ * It modifies the `document` object and `patch` - it gets the values by reference.
487
+ * If you would like to avoid touching your values, clone them:
488
+ * `jsonpatch.applyPatch(document, jsonpatch._deepClone(patch))`.
489
+ *
490
+ * @param document The document to patch
491
+ * @param patch The patch to apply
492
+ * @param validateOperation `false` is without validation, `true` to use default jsonpatch's validation, or you can pass a `validateOperation` callback to be used for validation.
493
+ * @param mutateDocument Whether to mutate the original document or clone it before applying
494
+ * @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
495
+ * @return An array of `{newDocument, result}` after the patch
496
+ */
497
+ function applyPatch(document, patch, validateOperation, mutateDocument, banPrototypeModifications) {
498
+ if (mutateDocument === void 0) {
499
+ mutateDocument = true;
500
+ }
501
+ if (banPrototypeModifications === void 0) {
502
+ banPrototypeModifications = true;
503
+ }
504
+ if (validateOperation) {
505
+ if (!Array.isArray(patch)) {
506
+ throw new JsonPatchError('Patch sequence must be an array', 'SEQUENCE_NOT_AN_ARRAY');
507
+ }
508
+ }
509
+ if (!mutateDocument) {
510
+ document = _deepClone(document);
511
+ }
512
+ var results = new Array(patch.length);
513
+ for (var i = 0, length_1 = patch.length; i < length_1; i++) {
514
+ // we don't need to pass mutateDocument argument because if it was true, we already deep cloned the object, we'll just pass `true`
515
+ results[i] = applyOperation(document, patch[i], validateOperation, true, banPrototypeModifications, i);
516
+ document = results[i].newDocument; // in case root was replaced
517
+ }
518
+ results.newDocument = document;
519
+ return results;
520
+ }
521
+ /**
522
+ * Apply a single JSON Patch Operation on a JSON document.
523
+ * Returns the updated document.
524
+ * Suitable as a reducer.
525
+ *
526
+ * @param document The document to patch
527
+ * @param operation The operation to apply
528
+ * @return The updated document
529
+ */
530
+ function applyReducer(document, operation, index) {
531
+ var operationResult = applyOperation(document, operation);
532
+ if (operationResult.test === false) {
533
+ // failed test
534
+ throw new JsonPatchError("Test operation failed", 'TEST_OPERATION_FAILED', index, operation, document);
535
+ }
536
+ return operationResult.newDocument;
537
+ }
538
+ /**
539
+ * Validates a single operation. Called from `jsonpatch.validate`. Throws `JsonPatchError` in case of an error.
540
+ * @param {object} operation - operation object (patch)
541
+ * @param {number} index - index of operation in the sequence
542
+ * @param {object} [document] - object where the operation is supposed to be applied
543
+ * @param {string} [existingPathFragment] - comes along with `document`
544
+ */
545
+ function validator(operation, index, document, existingPathFragment) {
546
+ if (_typeof(operation) !== 'object' || operation === null || Array.isArray(operation)) {
547
+ throw new JsonPatchError('Operation is not an object', 'OPERATION_NOT_AN_OBJECT', index, operation, document);
548
+ } else if (!objOps[operation.op]) {
549
+ throw new JsonPatchError('Operation `op` property is not one of operations defined in RFC-6902', 'OPERATION_OP_INVALID', index, operation, document);
550
+ } else if (typeof operation.path !== 'string') {
551
+ throw new JsonPatchError('Operation `path` property is not a string', 'OPERATION_PATH_INVALID', index, operation, document);
552
+ } else if (operation.path.indexOf('/') !== 0 && operation.path.length > 0) {
553
+ // paths that aren't empty string should start with "/"
554
+ throw new JsonPatchError('Operation `path` property must start with "/"', 'OPERATION_PATH_INVALID', index, operation, document);
555
+ } else if ((operation.op === 'move' || operation.op === 'copy') && typeof operation.from !== 'string') {
556
+ throw new JsonPatchError('Operation `from` property is not present (applicable in `move` and `copy` operations)', 'OPERATION_FROM_REQUIRED', index, operation, document);
557
+ } else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && operation.value === undefined) {
558
+ throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_REQUIRED', index, operation, document);
559
+ } else if ((operation.op === 'add' || operation.op === 'replace' || operation.op === 'test') && hasUndefined(operation.value)) {
560
+ throw new JsonPatchError('Operation `value` property is not present (applicable in `add`, `replace` and `test` operations)', 'OPERATION_VALUE_CANNOT_CONTAIN_UNDEFINED', index, operation, document);
561
+ } else if (document) {
562
+ if (operation.op == "add") {
563
+ var pathLen = operation.path.split("/").length;
564
+ var existingPathLen = existingPathFragment.split("/").length;
565
+ if (pathLen !== existingPathLen + 1 && pathLen !== existingPathLen) {
566
+ throw new JsonPatchError('Cannot perform an `add` operation at the desired path', 'OPERATION_PATH_CANNOT_ADD', index, operation, document);
567
+ }
568
+ } else if (operation.op === 'replace' || operation.op === 'remove' || operation.op === '_get') {
569
+ if (operation.path !== existingPathFragment) {
570
+ throw new JsonPatchError('Cannot perform the operation at a path that does not exist', 'OPERATION_PATH_UNRESOLVABLE', index, operation, document);
571
+ }
572
+ } else if (operation.op === 'move' || operation.op === 'copy') {
573
+ var existingValue = {
574
+ op: "_get",
575
+ path: operation.from,
576
+ value: undefined
577
+ };
578
+ var error = validate([existingValue], document);
579
+ if (error && error.name === 'OPERATION_PATH_UNRESOLVABLE') {
580
+ throw new JsonPatchError('Cannot perform the operation from a path that does not exist', 'OPERATION_FROM_UNRESOLVABLE', index, operation, document);
581
+ }
582
+ }
583
+ }
584
+ }
585
+ /**
586
+ * Validates a sequence of operations. If `document` parameter is provided, the sequence is additionally validated against the object document.
587
+ * If error is encountered, returns a JsonPatchError object
588
+ * @param sequence
589
+ * @param document
590
+ * @returns {JsonPatchError|undefined}
591
+ */
592
+ function validate(sequence, document, externalValidator) {
593
+ try {
594
+ if (!Array.isArray(sequence)) {
595
+ throw new JsonPatchError('Patch sequence must be an array', 'SEQUENCE_NOT_AN_ARRAY');
596
+ }
597
+ if (document) {
598
+ //clone document and sequence so that we can safely try applying operations
599
+ applyPatch(_deepClone(document), _deepClone(sequence), externalValidator || true);
600
+ } else {
601
+ externalValidator = externalValidator || validator;
602
+ for (var i = 0; i < sequence.length; i++) {
603
+ externalValidator(sequence[i], i, document, undefined);
604
+ }
605
+ }
606
+ } catch (e) {
607
+ if (e instanceof JsonPatchError) {
608
+ return e;
609
+ } else {
610
+ throw e;
611
+ }
612
+ }
613
+ }
614
+ // based on https://github.com/epoberezkin/fast-deep-equal
615
+ // MIT License
616
+ // Copyright (c) 2017 Evgeny Poberezkin
617
+ // Permission is hereby granted, free of charge, to any person obtaining a copy
618
+ // of this software and associated documentation files (the "Software"), to deal
619
+ // in the Software without restriction, including without limitation the rights
620
+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
621
+ // copies of the Software, and to permit persons to whom the Software is
622
+ // furnished to do so, subject to the following conditions:
623
+ // The above copyright notice and this permission notice shall be included in all
624
+ // copies or substantial portions of the Software.
625
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
626
+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
627
+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
628
+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
629
+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
630
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
631
+ // SOFTWARE.
632
+ function _areEquals(a, b) {
633
+ if (a === b) return true;
634
+ if (a && b && _typeof(a) == 'object' && _typeof(b) == 'object') {
635
+ var arrA = Array.isArray(a),
636
+ arrB = Array.isArray(b),
637
+ i,
638
+ length,
639
+ key;
640
+ if (arrA && arrB) {
641
+ length = a.length;
642
+ if (length != b.length) return false;
643
+ for (i = length; i-- !== 0;) if (!_areEquals(a[i], b[i])) return false;
644
+ return true;
645
+ }
646
+ if (arrA != arrB) return false;
647
+ var keys = Object.keys(a);
648
+ length = keys.length;
649
+ if (length !== Object.keys(b).length) return false;
650
+ for (i = length; i-- !== 0;) if (!b.hasOwnProperty(keys[i])) return false;
651
+ for (i = length; i-- !== 0;) {
652
+ key = keys[i];
653
+ if (!_areEquals(a[key], b[key])) return false;
654
+ }
655
+ return true;
656
+ }
657
+ return a !== a && b !== b;
658
+ }
659
+ ;
660
+
661
+ var core = /*#__PURE__*/Object.freeze({
662
+ __proto__: null,
663
+ JsonPatchError: JsonPatchError,
664
+ deepClone: deepClone,
665
+ getValueByPointer: getValueByPointer,
666
+ applyOperation: applyOperation,
667
+ applyPatch: applyPatch,
668
+ applyReducer: applyReducer,
669
+ validator: validator,
670
+ validate: validate,
671
+ _areEquals: _areEquals
672
+ });
673
+
674
+ var beforeDict = new WeakMap();
675
+ var Mirror = /** @class */function () {
676
+ function Mirror(obj) {
677
+ this.observers = new Map();
678
+ this.obj = obj;
679
+ }
680
+ return Mirror;
681
+ }();
682
+ var ObserverInfo = /** @class */function () {
683
+ function ObserverInfo(callback, observer) {
684
+ this.callback = callback;
685
+ this.observer = observer;
686
+ }
687
+ return ObserverInfo;
688
+ }();
689
+ function getMirror(obj) {
690
+ return beforeDict.get(obj);
691
+ }
692
+ function getObserverFromMirror(mirror, callback) {
693
+ return mirror.observers.get(callback);
694
+ }
695
+ function removeObserverFromMirror(mirror, observer) {
696
+ mirror.observers["delete"](observer.callback);
697
+ }
698
+ /**
699
+ * Detach an observer from an object
700
+ */
701
+ function unobserve(root, observer) {
702
+ observer.unobserve();
703
+ }
704
+ /**
705
+ * Observes changes made to an object, which can then be retrieved using generate
706
+ */
707
+ function observe(obj, callback) {
708
+ var patches = [];
709
+ var observer;
710
+ var mirror = getMirror(obj);
711
+ if (!mirror) {
712
+ mirror = new Mirror(obj);
713
+ beforeDict.set(obj, mirror);
714
+ } else {
715
+ var observerInfo = getObserverFromMirror(mirror, callback);
716
+ observer = observerInfo && observerInfo.observer;
717
+ }
718
+ if (observer) {
719
+ return observer;
720
+ }
721
+ observer = {};
722
+ mirror.value = _deepClone(obj);
723
+ if (callback) {
724
+ observer.callback = callback;
725
+ observer.next = null;
726
+ var dirtyCheck = function dirtyCheck() {
727
+ generate(observer);
728
+ };
729
+ var fastCheck = function fastCheck() {
730
+ clearTimeout(observer.next);
731
+ observer.next = setTimeout(dirtyCheck);
732
+ };
733
+ if (typeof window !== 'undefined') {
734
+ //not Node
735
+ window.addEventListener('mouseup', fastCheck);
736
+ window.addEventListener('keyup', fastCheck);
737
+ window.addEventListener('mousedown', fastCheck);
738
+ window.addEventListener('keydown', fastCheck);
739
+ window.addEventListener('change', fastCheck);
740
+ }
741
+ }
742
+ observer.patches = patches;
743
+ observer.object = obj;
744
+ observer.unobserve = function () {
745
+ generate(observer);
746
+ clearTimeout(observer.next);
747
+ removeObserverFromMirror(mirror, observer);
748
+ if (typeof window !== 'undefined') {
749
+ window.removeEventListener('mouseup', fastCheck);
750
+ window.removeEventListener('keyup', fastCheck);
751
+ window.removeEventListener('mousedown', fastCheck);
752
+ window.removeEventListener('keydown', fastCheck);
753
+ window.removeEventListener('change', fastCheck);
754
+ }
755
+ };
756
+ mirror.observers.set(callback, new ObserverInfo(callback, observer));
757
+ return observer;
758
+ }
759
+ /**
760
+ * Generate an array of patches from an observer
761
+ */
762
+ function generate(observer, invertible) {
763
+ if (invertible === void 0) {
764
+ invertible = false;
765
+ }
766
+ var mirror = beforeDict.get(observer.object);
767
+ _generate(mirror.value, observer.object, observer.patches, "", invertible);
768
+ if (observer.patches.length) {
769
+ applyPatch(mirror.value, observer.patches);
770
+ }
771
+ var temp = observer.patches;
772
+ if (temp.length > 0) {
773
+ observer.patches = [];
774
+ if (observer.callback) {
775
+ observer.callback(temp);
776
+ }
777
+ }
778
+ return temp;
779
+ }
780
+ // Dirty check if obj is different from mirror, generate patches and update mirror
781
+ function _generate(mirror, obj, patches, path, invertible) {
782
+ if (obj === mirror) {
783
+ return;
784
+ }
785
+ if (typeof obj.toJSON === "function") {
786
+ obj = obj.toJSON();
787
+ }
788
+ var newKeys = _objectKeys(obj);
789
+ var oldKeys = _objectKeys(mirror);
790
+ var changed = false;
791
+ var deleted = false;
792
+ //if ever "move" operation is implemented here, make sure this test runs OK: "should not generate the same patch twice (move)"
793
+ for (var t = oldKeys.length - 1; t >= 0; t--) {
794
+ var key = oldKeys[t];
795
+ var oldVal = mirror[key];
796
+ if (hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
797
+ var newVal = obj[key];
798
+ if (_typeof(oldVal) == "object" && oldVal != null && _typeof(newVal) == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
799
+ _generate(oldVal, newVal, patches, path + "/" + escapePathComponent(key), invertible);
800
+ } else {
801
+ if (oldVal !== newVal) {
802
+ changed = true;
803
+ if (invertible) {
804
+ patches.push({
805
+ op: "test",
806
+ path: path + "/" + escapePathComponent(key),
807
+ value: _deepClone(oldVal)
808
+ });
809
+ }
810
+ patches.push({
811
+ op: "replace",
812
+ path: path + "/" + escapePathComponent(key),
813
+ value: _deepClone(newVal)
814
+ });
815
+ }
816
+ }
817
+ } else if (Array.isArray(mirror) === Array.isArray(obj)) {
818
+ if (invertible) {
819
+ patches.push({
820
+ op: "test",
821
+ path: path + "/" + escapePathComponent(key),
822
+ value: _deepClone(oldVal)
823
+ });
824
+ }
825
+ patches.push({
826
+ op: "remove",
827
+ path: path + "/" + escapePathComponent(key)
828
+ });
829
+ deleted = true; // property has been deleted
830
+ } else {
831
+ if (invertible) {
832
+ patches.push({
833
+ op: "test",
834
+ path: path,
835
+ value: mirror
836
+ });
837
+ }
838
+ patches.push({
839
+ op: "replace",
840
+ path: path,
841
+ value: obj
842
+ });
843
+ changed = true;
844
+ }
845
+ }
846
+ if (!deleted && newKeys.length == oldKeys.length) {
847
+ return;
848
+ }
849
+ for (var t = 0; t < newKeys.length; t++) {
850
+ var key = newKeys[t];
851
+ if (!hasOwnProperty(mirror, key) && obj[key] !== undefined) {
852
+ patches.push({
853
+ op: "add",
854
+ path: path + "/" + escapePathComponent(key),
855
+ value: _deepClone(obj[key])
856
+ });
857
+ }
858
+ }
859
+ }
860
+ /**
861
+ * Create an array of patches from the differences in two objects
862
+ */
863
+ function compare(tree1, tree2, invertible) {
864
+ if (invertible === void 0) {
865
+ invertible = false;
866
+ }
867
+ var patches = [];
868
+ _generate(tree1, tree2, patches, '', invertible);
869
+ return patches;
870
+ }
871
+
872
+ var duplex = /*#__PURE__*/Object.freeze({
873
+ __proto__: null,
874
+ unobserve: unobserve,
875
+ observe: observe,
876
+ generate: generate,
877
+ compare: compare
878
+ });
879
+
880
+ var index = Object.assign({}, core, duplex, {
881
+ JsonPatchError: PatchError,
882
+ deepClone: _deepClone,
883
+ escapePathComponent: escapePathComponent,
884
+ unescapePathComponent: unescapePathComponent
885
+ });
886
+
887
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
888
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
889
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
890
+ function applyJsonPatch(state, delta) {
891
+ try {
892
+ var result = applyPatch(state, delta, false, false);
893
+ return result.newDocument;
894
+ } catch (error) {
895
+ console.warn("JSON Patch\u64CD\u4F5C\u5931\u8D25\uFF0C\u8FD4\u56DE\u539F\u59CB\u72B6\u6001:", error);
896
+ return JSON.parse(JSON.stringify(state));
897
+ }
898
+ }
899
+ function safeParseJSON(value, fallbackValue, errorContext) {
900
+ if (typeof value !== "string") {
901
+ return value;
902
+ }
903
+ try {
904
+ return JSON.parse(value);
905
+ } catch (error) {
906
+ var context = errorContext ? " (".concat(errorContext, ")") : "";
907
+ console.warn("Failed to parse JSON".concat(context, ":"), error);
908
+ return fallbackValue !== void 0 ? fallbackValue : value;
909
+ }
910
+ }
911
+ function findTargetElement(event, selector) {
912
+ var selectors = Array.isArray(selector) ? selector : selector.split(",").map(function (s) {
913
+ return s.trim();
914
+ });
915
+ var eventPath = event.composedPath();
916
+ var _iterator = _createForOfIteratorHelper(eventPath),
917
+ _step;
918
+ try {
919
+ var _loop = function _loop() {
920
+ var el = _step.value;
921
+ if (el instanceof HTMLElement) {
922
+ if (selectors.some(function (sel) {
923
+ var _el$matches;
924
+ return (_el$matches = el.matches) === null || _el$matches === void 0 ? void 0 : _el$matches.call(el, sel);
925
+ })) {
926
+ return {
927
+ v: el
928
+ };
929
+ }
930
+ }
931
+ },
932
+ _ret;
933
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
934
+ _ret = _loop();
935
+ if (_ret) return _ret.v;
936
+ }
937
+ } catch (err) {
938
+ _iterator.e(err);
939
+ } finally {
940
+ _iterator.f();
941
+ }
942
+ return null;
943
+ }
944
+ function isUserMessage(message) {
945
+ return message.role === "user" && "content" in message;
946
+ }
947
+ function isAIMessage(message) {
948
+ return message.role === "assistant";
949
+ }
950
+ function isThinkingContent(content) {
951
+ return content.type === "thinking";
952
+ }
953
+ function isTextContent(content) {
954
+ return content.type === "text";
955
+ }
956
+ function isMarkdownContent(content) {
957
+ return content.type === "markdown";
958
+ }
959
+ function isImageContent(content) {
960
+ return content.type === "image";
961
+ }
962
+ function isSearchContent(content) {
963
+ return content.type === "search";
964
+ }
965
+ function isSuggestionContent(content) {
966
+ return content.type === "suggestion";
967
+ }
968
+ function isAttachmentContent(content) {
969
+ return content.type === "attachment";
970
+ }
971
+ function isToolCallContent(content) {
972
+ return content.type === "toolcall" || content.type.startsWith("toolcall-");
973
+ }
974
+ function isReasoningContent(content) {
975
+ return content.type === "reasoning";
976
+ }
977
+ function getMessageContentForCopy(message) {
978
+ if (!isAIMessage(message) || !message.content) {
979
+ return "";
980
+ }
981
+ return message.content.reduce(function (pre, item) {
982
+ var append = "";
983
+ if (isTextContent(item) || isMarkdownContent(item)) {
984
+ append = item.data;
985
+ } else if (isThinkingContent(item)) {
986
+ append = item.data.text || "";
987
+ }
988
+ if (!pre) {
989
+ return append;
990
+ }
991
+ return "".concat(pre, "\n").concat(append);
992
+ }, "");
993
+ }
994
+
995
+ export { applyJsonPatch, findTargetElement, getMessageContentForCopy, isAIMessage, isAttachmentContent, isImageContent, isMarkdownContent, isReasoningContent, isSearchContent, isSuggestionContent, isTextContent, isThinkingContent, isToolCallContent, isUserMessage, safeParseJSON };
996
+ //# sourceMappingURL=index.js.map