@platejs/ai 51.1.2 → 52.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/getEditorPrompt-BnVrgUl3.js +102 -0
- package/dist/index-CyVv31EV.d.ts +96 -0
- package/dist/index.d.ts +2 -22
- package/dist/index.js +47 -224
- package/dist/react/index.d.ts +466 -374
- package/dist/react/index.js +1246 -1894
- package/package.json +14 -22
- package/dist/getEditorPrompt-C6prTxft.d.mts +0 -54
- package/dist/getEditorPrompt-C6prTxft.d.ts +0 -54
- package/dist/index.d.mts +0 -22
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -193
- package/dist/index.mjs.map +0 -1
- package/dist/react/index.d.mts +0 -471
- package/dist/react/index.js.map +0 -1
- package/dist/react/index.mjs +0 -1965
- package/dist/react/index.mjs.map +0 -1
package/dist/react/index.js
CHANGED
|
@@ -1,1988 +1,1340 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/react/index.ts
|
|
31
|
-
var react_exports = {};
|
|
32
|
-
__export(react_exports, {
|
|
33
|
-
AIChatPlugin: () => AIChatPlugin,
|
|
34
|
-
AIPlugin: () => AIPlugin,
|
|
35
|
-
CopilotPlugin: () => CopilotPlugin,
|
|
36
|
-
acceptAIChat: () => acceptAIChat,
|
|
37
|
-
acceptAISuggestions: () => acceptAISuggestions,
|
|
38
|
-
acceptCopilot: () => acceptCopilot,
|
|
39
|
-
acceptCopilotNextWord: () => acceptCopilotNextWord,
|
|
40
|
-
aiCommentToRange: () => aiCommentToRange,
|
|
41
|
-
applyAISuggestions: () => applyAISuggestions,
|
|
42
|
-
callCompletionApi: () => callCompletionApi,
|
|
43
|
-
createFormattedBlocks: () => createFormattedBlocks,
|
|
44
|
-
escapeInput: () => escapeInput,
|
|
45
|
-
findTextRangeInBlock: () => findTextRangeInBlock,
|
|
46
|
-
getChunkTrimmed: () => getChunkTrimmed,
|
|
47
|
-
getCurrentBlockPath: () => getCurrentBlockPath,
|
|
48
|
-
getLastAssistantMessage: () => getLastAssistantMessage,
|
|
49
|
-
getListNode: () => getListNode,
|
|
50
|
-
getNextWord: () => getNextWord,
|
|
51
|
-
insertBelowAIChat: () => insertBelowAIChat,
|
|
52
|
-
insertBelowGenerate: () => insertBelowGenerate,
|
|
53
|
-
isCompleteCodeBlock: () => isCompleteCodeBlock,
|
|
54
|
-
isCompleteMath: () => isCompleteMath,
|
|
55
|
-
isSameNode: () => isSameNode,
|
|
56
|
-
nodesWithProps: () => nodesWithProps,
|
|
57
|
-
rejectAISuggestions: () => rejectAISuggestions,
|
|
58
|
-
removeAnchorAIChat: () => removeAnchorAIChat,
|
|
59
|
-
renderCopilotBelowNodes: () => renderCopilotBelowNodes,
|
|
60
|
-
replaceSelectionAIChat: () => replaceSelectionAIChat,
|
|
61
|
-
resetAIChat: () => resetAIChat,
|
|
62
|
-
streamDeserializeInlineMd: () => streamDeserializeInlineMd,
|
|
63
|
-
streamDeserializeMd: () => streamDeserializeMd,
|
|
64
|
-
streamInsertChunk: () => streamInsertChunk,
|
|
65
|
-
streamSerializeMd: () => streamSerializeMd,
|
|
66
|
-
submitAIChat: () => submitAIChat,
|
|
67
|
-
triggerCopilotSuggestion: () => triggerCopilotSuggestion,
|
|
68
|
-
useAIChatEditor: () => useAIChatEditor,
|
|
69
|
-
useChatChunk: () => useChatChunk,
|
|
70
|
-
useEditorChat: () => useEditorChat,
|
|
71
|
-
useLastAssistantMessage: () => useLastAssistantMessage,
|
|
72
|
-
withAIChat: () => withAIChat,
|
|
73
|
-
withCopilot: () => withCopilot,
|
|
74
|
-
withoutAbort: () => withoutAbort
|
|
75
|
-
});
|
|
76
|
-
module.exports = __toCommonJS(react_exports);
|
|
77
|
-
|
|
78
|
-
// src/react/ai/AIPlugin.ts
|
|
79
|
-
var import_react = require("platejs/react");
|
|
80
|
-
|
|
81
|
-
// src/lib/BaseAIPlugin.ts
|
|
82
|
-
var import_platejs3 = require("platejs");
|
|
83
|
-
|
|
84
|
-
// src/lib/transforms/insertAINodes.ts
|
|
85
|
-
var insertAINodes = (editor, nodes, {
|
|
86
|
-
target
|
|
87
|
-
} = {}) => {
|
|
88
|
-
if (!target && !editor.selection?.focus.path) return;
|
|
89
|
-
const aiNodes = nodes.map((node) => ({
|
|
90
|
-
...node,
|
|
91
|
-
ai: true
|
|
92
|
-
}));
|
|
93
|
-
editor.tf.withoutNormalizing(() => {
|
|
94
|
-
editor.tf.insertNodes(aiNodes, {
|
|
95
|
-
at: editor.api.end(target || editor.selection.focus.path),
|
|
96
|
-
select: true
|
|
97
|
-
});
|
|
98
|
-
editor.tf.collapse({ edge: "end" });
|
|
99
|
-
});
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
// src/lib/transforms/removeAIMarks.ts
|
|
103
|
-
var import_platejs = require("platejs");
|
|
104
|
-
var removeAIMarks = (editor, { at = [] } = {}) => {
|
|
105
|
-
const nodeType = (0, import_platejs.getPluginType)(editor, import_platejs.KEYS.ai);
|
|
106
|
-
editor.tf.unsetNodes(nodeType, {
|
|
107
|
-
at,
|
|
108
|
-
match: (n) => n[nodeType]
|
|
109
|
-
});
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// src/lib/transforms/removeAINodes.ts
|
|
113
|
-
var import_platejs2 = require("platejs");
|
|
114
|
-
var removeAINodes = (editor, { at = [] } = {}) => {
|
|
115
|
-
editor.tf.removeNodes({
|
|
116
|
-
at,
|
|
117
|
-
match: (n) => import_platejs2.TextApi.isText(n) && !!n.ai
|
|
118
|
-
});
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
// src/lib/transforms/undoAI.ts
|
|
122
|
-
var import_suggestion = require("@platejs/suggestion");
|
|
123
|
-
var undoAI = (editor) => {
|
|
124
|
-
const hasAINodeOrAISuggestion = editor.api.some({
|
|
125
|
-
at: [],
|
|
126
|
-
match: (n) => !!n.ai
|
|
127
|
-
}) || editor.api.some({
|
|
128
|
-
at: [],
|
|
129
|
-
match: (n) => !!n[(0, import_suggestion.getTransientSuggestionKey)()]
|
|
130
|
-
});
|
|
131
|
-
if (editor.history.undos.at(-1)?.ai && hasAINodeOrAISuggestion) {
|
|
132
|
-
editor.undo();
|
|
133
|
-
editor.history.redos.pop();
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
// src/lib/transforms/withAIBatch.ts
|
|
138
|
-
var withAIBatch = (editor, fn, {
|
|
139
|
-
split
|
|
140
|
-
} = {}) => {
|
|
141
|
-
if (split) {
|
|
142
|
-
editor.tf.withNewBatch(fn);
|
|
143
|
-
} else {
|
|
144
|
-
editor.tf.withMerging(fn);
|
|
145
|
-
}
|
|
146
|
-
const lastBatch = editor.history.undos?.at(-1);
|
|
147
|
-
if (lastBatch) {
|
|
148
|
-
lastBatch.ai = true;
|
|
149
|
-
}
|
|
150
|
-
};
|
|
1
|
+
import { n as BaseAIPlugin, r as withAIBatch, t as getEditorPrompt } from "../getEditorPrompt-BnVrgUl3.js";
|
|
2
|
+
import { ElementApi, KEYS, NodeApi, PathApi, RangeApi, TextApi, bindFirst, getPluginKey, getPluginType, isDefined, nanoid } from "platejs";
|
|
3
|
+
import { SkipSuggestionDeletes, acceptSuggestion, diffToSuggestions, getSuggestionKey, getTransientSuggestionKey, rejectSuggestion } from "@platejs/suggestion";
|
|
4
|
+
import { isSelecting } from "@platejs/selection";
|
|
5
|
+
import { MarkdownPlugin, deserializeInlineMd, deserializeMd, serializeInlineMd, serializeMd } from "@platejs/markdown";
|
|
6
|
+
import { createTPlatePlugin, getEditorPlugin, toPlatePlugin, useEditorPlugin, usePluginOption } from "platejs/react";
|
|
7
|
+
import { distance } from "fastest-levenshtein";
|
|
8
|
+
import { BlockSelectionPlugin, removeBlockSelectionNodes } from "@platejs/selection/react";
|
|
9
|
+
import { SuggestionPlugin } from "@platejs/suggestion/react";
|
|
10
|
+
import cloneDeep from "lodash/cloneDeep.js";
|
|
11
|
+
import React, { useEffect, useMemo, useRef } from "react";
|
|
12
|
+
import debounce from "lodash/debounce.js";
|
|
151
13
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
key: import_platejs3.KEYS.ai,
|
|
155
|
-
node: { isDecoration: false, isLeaf: true }
|
|
156
|
-
}).extendTransforms(({ editor }) => ({
|
|
157
|
-
insertNodes: (0, import_platejs3.bindFirst)(insertAINodes, editor),
|
|
158
|
-
removeMarks: (0, import_platejs3.bindFirst)(removeAIMarks, editor),
|
|
159
|
-
removeNodes: (0, import_platejs3.bindFirst)(removeAINodes, editor),
|
|
160
|
-
undo: (0, import_platejs3.bindFirst)(undoAI, editor)
|
|
161
|
-
}));
|
|
162
|
-
|
|
163
|
-
// src/lib/utils/getEditorPrompt.ts
|
|
164
|
-
var import_selection = require("@platejs/selection");
|
|
165
|
-
var import_platejs4 = require("platejs");
|
|
166
|
-
var createPromptFromConfig = (config, params) => {
|
|
167
|
-
const { isBlockSelecting, isSelecting: isSelecting3 } = params;
|
|
168
|
-
if (isBlockSelecting && config.blockSelecting) {
|
|
169
|
-
return config.blockSelecting ?? config.default;
|
|
170
|
-
}
|
|
171
|
-
if (isSelecting3 && config.selecting) {
|
|
172
|
-
return config.selecting ?? config.default;
|
|
173
|
-
}
|
|
174
|
-
return config.default;
|
|
175
|
-
};
|
|
176
|
-
var getEditorPrompt = (editor, {
|
|
177
|
-
prompt = ""
|
|
178
|
-
}) => {
|
|
179
|
-
const params = {
|
|
180
|
-
editor,
|
|
181
|
-
isBlockSelecting: editor.getOption(
|
|
182
|
-
{ key: import_platejs4.KEYS.blockSelection },
|
|
183
|
-
"isSelectingSome"
|
|
184
|
-
),
|
|
185
|
-
isSelecting: (0, import_selection.isSelecting)(editor)
|
|
186
|
-
};
|
|
187
|
-
let promptText = "";
|
|
188
|
-
if (typeof prompt === "function") {
|
|
189
|
-
promptText = prompt(params);
|
|
190
|
-
} else if (typeof prompt === "object") {
|
|
191
|
-
promptText = createPromptFromConfig(prompt, params);
|
|
192
|
-
} else {
|
|
193
|
-
promptText = prompt;
|
|
194
|
-
}
|
|
195
|
-
return promptText;
|
|
196
|
-
};
|
|
14
|
+
//#region src/react/ai/AIPlugin.ts
|
|
15
|
+
const AIPlugin = toPlatePlugin(BaseAIPlugin);
|
|
197
16
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
// src/react/ai/utils/aiCommentToRange.ts
|
|
202
|
-
var import_markdown = require("@platejs/markdown");
|
|
203
|
-
var import_platejs6 = require("platejs");
|
|
204
|
-
|
|
205
|
-
// src/react/ai/utils/findTextRangeInBlock.tsx
|
|
206
|
-
var import_fastest_levenshtein = require("fastest-levenshtein");
|
|
207
|
-
var import_platejs5 = require("platejs");
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/react/ai/utils/findTextRangeInBlock.tsx
|
|
208
19
|
function maxAllowedDistance(len) {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
20
|
+
if (len <= 2) return 0;
|
|
21
|
+
if (len <= 5) return 1;
|
|
22
|
+
if (len <= 10) return 2;
|
|
23
|
+
if (len <= 20) return 3;
|
|
24
|
+
return 5;
|
|
214
25
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
path: lastSegment.path
|
|
294
|
-
};
|
|
295
|
-
};
|
|
296
|
-
const anchor = findPoint(matchStart, false);
|
|
297
|
-
const focus = findPoint(matchEnd, true);
|
|
298
|
-
return {
|
|
299
|
-
anchor,
|
|
300
|
-
focus
|
|
301
|
-
};
|
|
26
|
+
/** Find text within a block, supporting fuzzy matching. */
|
|
27
|
+
function findTextRangeInBlock({ block, findText }) {
|
|
28
|
+
const [blockNode, blockPath] = block;
|
|
29
|
+
const textSegments = [];
|
|
30
|
+
let fullText = "";
|
|
31
|
+
for (const [textNode, textPath] of NodeApi.texts(blockNode)) {
|
|
32
|
+
const startOffset = fullText.length;
|
|
33
|
+
const absolutePath = [...blockPath, ...textPath];
|
|
34
|
+
textSegments.push({
|
|
35
|
+
offset: startOffset,
|
|
36
|
+
path: absolutePath,
|
|
37
|
+
text: textNode.text
|
|
38
|
+
});
|
|
39
|
+
fullText += textNode.text;
|
|
40
|
+
}
|
|
41
|
+
if (!fullText) return null;
|
|
42
|
+
let matchStart = fullText.indexOf(findText);
|
|
43
|
+
let matchEnd = matchStart >= 0 ? matchStart + findText.length : -1;
|
|
44
|
+
if (matchStart === -1) {
|
|
45
|
+
const maxDist = maxAllowedDistance(findText.length);
|
|
46
|
+
let bestMatch = {
|
|
47
|
+
distance: Number.POSITIVE_INFINITY,
|
|
48
|
+
end: -1,
|
|
49
|
+
start: -1
|
|
50
|
+
};
|
|
51
|
+
for (let i = 0; i <= fullText.length - findText.length; i++) for (let lenOffset = -maxDist; lenOffset <= maxDist; lenOffset++) {
|
|
52
|
+
const len = findText.length + lenOffset;
|
|
53
|
+
if (len <= 0 || i + len > fullText.length) continue;
|
|
54
|
+
const dist = distance(fullText.slice(i, i + len), findText);
|
|
55
|
+
if (dist <= maxDist && dist < bestMatch.distance) bestMatch = {
|
|
56
|
+
distance: dist,
|
|
57
|
+
end: i + len,
|
|
58
|
+
start: i
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
if (bestMatch.start !== -1) {
|
|
62
|
+
matchStart = bestMatch.start;
|
|
63
|
+
matchEnd = bestMatch.end;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (matchStart === -1) for (let prefixLen = findText.length - 1; prefixLen > 0; prefixLen--) {
|
|
67
|
+
const prefix = findText.slice(0, Math.max(0, prefixLen));
|
|
68
|
+
const idx = fullText.indexOf(prefix);
|
|
69
|
+
if (idx !== -1) {
|
|
70
|
+
matchStart = idx;
|
|
71
|
+
matchEnd = idx + prefixLen;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (matchStart === -1) return null;
|
|
76
|
+
const findPoint = (charOffset, isEnd = false) => {
|
|
77
|
+
if (!isEnd) {
|
|
78
|
+
for (const segment of textSegments) if (charOffset === segment.offset) return {
|
|
79
|
+
offset: 0,
|
|
80
|
+
path: segment.path
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
for (const segment of textSegments) {
|
|
84
|
+
const segmentEnd = segment.offset + segment.text.length;
|
|
85
|
+
if (charOffset >= segment.offset && charOffset <= segmentEnd) return {
|
|
86
|
+
offset: charOffset - segment.offset,
|
|
87
|
+
path: segment.path
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
const lastSegment = textSegments.at(-1);
|
|
91
|
+
if (!lastSegment) return {
|
|
92
|
+
offset: 0,
|
|
93
|
+
path: blockPath
|
|
94
|
+
};
|
|
95
|
+
return {
|
|
96
|
+
offset: lastSegment.text.length,
|
|
97
|
+
path: lastSegment.path
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
anchor: findPoint(matchStart, false),
|
|
102
|
+
focus: findPoint(matchEnd, true)
|
|
103
|
+
};
|
|
302
104
|
}
|
|
303
105
|
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/react/ai/utils/aiCommentToRange.ts
|
|
108
|
+
const aiCommentToRange = (editor, aiComment) => {
|
|
109
|
+
const { blockId, content } = aiComment;
|
|
110
|
+
const contentNodes = deserializeMd(editor, content);
|
|
111
|
+
let firstBlock;
|
|
112
|
+
const ranges = [];
|
|
113
|
+
contentNodes.forEach((node, index) => {
|
|
114
|
+
let currentBlock;
|
|
115
|
+
if (index === 0) {
|
|
116
|
+
firstBlock = editor.api.node({
|
|
117
|
+
id: blockId,
|
|
118
|
+
at: []
|
|
119
|
+
});
|
|
120
|
+
currentBlock = firstBlock;
|
|
121
|
+
} else {
|
|
122
|
+
if (!firstBlock) return;
|
|
123
|
+
const [_, firstBlockPath] = firstBlock;
|
|
124
|
+
const blockPath = [firstBlockPath[0] + index];
|
|
125
|
+
currentBlock = editor.api.node(blockPath);
|
|
126
|
+
}
|
|
127
|
+
if (!currentBlock) return;
|
|
128
|
+
const range = findTextRangeInBlock({
|
|
129
|
+
block: currentBlock,
|
|
130
|
+
findText: NodeApi.string(node)
|
|
131
|
+
});
|
|
132
|
+
if (!range) return;
|
|
133
|
+
ranges.push(range);
|
|
134
|
+
});
|
|
135
|
+
if (ranges.length === 0) return;
|
|
136
|
+
if (ranges.length > 1) {
|
|
137
|
+
const startRange = ranges[0];
|
|
138
|
+
const endRange = ranges.at(-1);
|
|
139
|
+
return {
|
|
140
|
+
anchor: startRange.anchor,
|
|
141
|
+
focus: endRange.focus
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (ranges.length === 1) return ranges[0];
|
|
341
145
|
};
|
|
342
146
|
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const description = {
|
|
363
|
-
createdAt: new Date(suggestionData.createdAt),
|
|
364
|
-
keyId: (0, import_suggestion2.getSuggestionKey)(suggestionData.id),
|
|
365
|
-
suggestionId: suggestionData.id,
|
|
366
|
-
type: suggestionData.type,
|
|
367
|
-
userId: suggestionData.userId
|
|
368
|
-
};
|
|
369
|
-
(0, import_suggestion2.acceptSuggestion)(editor, description);
|
|
370
|
-
});
|
|
371
|
-
editor.tf.unsetNodes([(0, import_suggestion2.getTransientSuggestionKey)()], {
|
|
372
|
-
at: [],
|
|
373
|
-
mode: "all",
|
|
374
|
-
match: (n) => !!n[(0, import_suggestion2.getTransientSuggestionKey)()]
|
|
375
|
-
});
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/react/ai-chat/utils/acceptAISuggestions.ts
|
|
149
|
+
const acceptAISuggestions = (editor) => {
|
|
150
|
+
editor.getApi(SuggestionPlugin).suggestion.nodes({ transient: true }).forEach(([suggestionNode]) => {
|
|
151
|
+
const suggestionData = editor.getApi(SuggestionPlugin).suggestion.suggestionData(suggestionNode);
|
|
152
|
+
if (!suggestionData) return;
|
|
153
|
+
acceptSuggestion(editor, {
|
|
154
|
+
createdAt: new Date(suggestionData.createdAt),
|
|
155
|
+
keyId: getSuggestionKey(suggestionData.id),
|
|
156
|
+
suggestionId: suggestionData.id,
|
|
157
|
+
type: suggestionData.type,
|
|
158
|
+
userId: suggestionData.userId
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
editor.tf.unsetNodes([getTransientSuggestionKey()], {
|
|
162
|
+
at: [],
|
|
163
|
+
mode: "all",
|
|
164
|
+
match: (n) => !!n[getTransientSuggestionKey()]
|
|
165
|
+
});
|
|
376
166
|
};
|
|
377
167
|
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
168
|
+
//#endregion
|
|
169
|
+
//#region src/react/ai-chat/transforms/acceptAIChat.ts
|
|
170
|
+
const acceptAIChat = (editor) => {
|
|
171
|
+
const mode = editor.getOption(AIChatPlugin, "mode");
|
|
172
|
+
if (mode === "insert") {
|
|
173
|
+
const { tf } = getEditorPlugin(editor, AIPlugin);
|
|
174
|
+
const api = editor.getApi({ key: KEYS.ai });
|
|
175
|
+
const lastAINodePath = api.aiChat.node({
|
|
176
|
+
at: [],
|
|
177
|
+
reverse: true
|
|
178
|
+
})[1];
|
|
179
|
+
withAIBatch(editor, () => {
|
|
180
|
+
tf.ai.removeMarks();
|
|
181
|
+
editor.getTransforms(AIChatPlugin).aiChat.removeAnchor();
|
|
182
|
+
});
|
|
183
|
+
api.aiChat.hide();
|
|
184
|
+
editor.tf.focus();
|
|
185
|
+
const focusPoint = editor.api.end(lastAINodePath);
|
|
186
|
+
editor.tf.setSelection({
|
|
187
|
+
anchor: focusPoint,
|
|
188
|
+
focus: focusPoint
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
if (mode === "chat") {
|
|
192
|
+
withAIBatch(editor, () => {
|
|
193
|
+
acceptAISuggestions(editor);
|
|
194
|
+
});
|
|
195
|
+
editor.getApi(AIChatPlugin).aiChat.hide();
|
|
196
|
+
}
|
|
403
197
|
};
|
|
404
198
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
}
|
|
446
|
-
if (isSameString && isSameSuggestion && node.id === diffNode.id) {
|
|
447
|
-
return;
|
|
448
|
-
}
|
|
449
|
-
editor.tf.replaceNodes(diffNode, {
|
|
450
|
-
at: path
|
|
451
|
-
});
|
|
452
|
-
});
|
|
453
|
-
editor.getApi(import_react4.BlockSelectionPlugin).blockSelection.set(diffNodes.map((node) => node.id));
|
|
454
|
-
setReplaceIds(diffNodes.map((node) => node.id));
|
|
455
|
-
} else {
|
|
456
|
-
const diffNodes = getDiffNodes(editor, content);
|
|
457
|
-
editor.tf.insertFragment(diffNodes);
|
|
458
|
-
const nodes = Array.from(
|
|
459
|
-
editor.api.nodes({
|
|
460
|
-
at: [],
|
|
461
|
-
mode: "lowest",
|
|
462
|
-
match: (n) => import_platejs8.TextApi.isText(n) && !!n[(0, import_suggestion3.getTransientSuggestionKey)()]
|
|
463
|
-
})
|
|
464
|
-
);
|
|
465
|
-
const range = editor.api.nodesRange(nodes);
|
|
466
|
-
editor.tf.setSelection(range);
|
|
467
|
-
return;
|
|
468
|
-
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/react/ai-chat/utils/applyAISuggestions.ts
|
|
201
|
+
const applyAISuggestions = (editor, content) => {
|
|
202
|
+
/** Conflict with block selection */
|
|
203
|
+
editor.getApi({ key: KEYS.cursorOverlay })?.cursorOverlay?.removeCursor("selection");
|
|
204
|
+
const isBlockSelecting = editor.getOption(BlockSelectionPlugin, "isSelectingSome");
|
|
205
|
+
const { chatNodes } = editor.getOptions(AIChatPlugin);
|
|
206
|
+
if (isBlockSelecting || editor.api.blocks({ mode: "highest" }).length > 1) {
|
|
207
|
+
const setReplaceIds = (ids) => {
|
|
208
|
+
editor.setOption(AIChatPlugin, "_replaceIds", ids);
|
|
209
|
+
};
|
|
210
|
+
if (editor.getOption(AIChatPlugin, "_replaceIds").length === 0) setReplaceIds(chatNodes.map((node) => node.id));
|
|
211
|
+
const diffNodes = getDiffNodes(editor, content);
|
|
212
|
+
const replaceNodes = Array.from(editor.api.nodes({
|
|
213
|
+
at: [],
|
|
214
|
+
match: (n) => ElementApi.isElement(n) && editor.getOption(AIChatPlugin, "_replaceIds").includes(n.id)
|
|
215
|
+
}));
|
|
216
|
+
replaceNodes.forEach(([node, path], index) => {
|
|
217
|
+
const replaceNode = node;
|
|
218
|
+
const diffNode = diffNodes[index];
|
|
219
|
+
const isSameString = SkipSuggestionDeletes(editor, replaceNode) === SkipSuggestionDeletes(editor, diffNode);
|
|
220
|
+
const isSameSuggestion = replaceNode.suggestion?.type === diffNode.suggestion?.type;
|
|
221
|
+
if (index === replaceNodes.length - 1 && diffNodes.length > replaceNodes.length) editor.tf.replaceNodes(diffNodes.slice(index), { at: path });
|
|
222
|
+
if (isSameString && isSameSuggestion && node.id === diffNode.id) return;
|
|
223
|
+
editor.tf.replaceNodes(diffNode, { at: path });
|
|
224
|
+
});
|
|
225
|
+
editor.getApi(BlockSelectionPlugin).blockSelection.set(diffNodes.map((node) => node.id));
|
|
226
|
+
setReplaceIds(diffNodes.map((node) => node.id));
|
|
227
|
+
} else {
|
|
228
|
+
const diffNodes = getDiffNodes(editor, content);
|
|
229
|
+
editor.tf.insertFragment(diffNodes);
|
|
230
|
+
const nodes = Array.from(editor.api.nodes({
|
|
231
|
+
at: [],
|
|
232
|
+
mode: "lowest",
|
|
233
|
+
match: (n) => TextApi.isText(n) && !!n[getTransientSuggestionKey()]
|
|
234
|
+
}));
|
|
235
|
+
const range = editor.api.nodesRange(nodes);
|
|
236
|
+
editor.tf.setSelection(range);
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
469
239
|
};
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
240
|
+
const withProps = (diffNodes, chatNodes) => diffNodes.map((node, index) => {
|
|
241
|
+
if (!ElementApi.isElement(node)) return node;
|
|
242
|
+
const originalNode = chatNodes?.[index];
|
|
243
|
+
return {
|
|
244
|
+
...node,
|
|
245
|
+
...originalNode ?? { id: nanoid() },
|
|
246
|
+
children: node.children
|
|
247
|
+
};
|
|
478
248
|
});
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
[(0, import_suggestion3.getTransientSuggestionKey)()]: true
|
|
490
|
-
};
|
|
249
|
+
const withTransient = (diffNodes) => diffNodes.map((node) => {
|
|
250
|
+
if (TextApi.isText(node)) return {
|
|
251
|
+
...node,
|
|
252
|
+
[getTransientSuggestionKey()]: true
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
...node,
|
|
256
|
+
children: withTransient(node.children),
|
|
257
|
+
[getTransientSuggestionKey()]: true
|
|
258
|
+
};
|
|
491
259
|
});
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
return {
|
|
515
|
-
...node,
|
|
516
|
-
children: withoutSuggestionAndComments(node.children)
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
return node;
|
|
260
|
+
const withoutSuggestionAndComments = (nodes) => nodes.map((node) => {
|
|
261
|
+
if (TextApi.isText(node)) {
|
|
262
|
+
if (node[KEYS.suggestion] || node[KEYS.comment]) return { text: node.text };
|
|
263
|
+
return node;
|
|
264
|
+
}
|
|
265
|
+
if (ElementApi.isElement(node)) {
|
|
266
|
+
if (node[KEYS.suggestion]) {
|
|
267
|
+
const nodeWithoutSuggestion = {};
|
|
268
|
+
Object.keys(node).forEach((key) => {
|
|
269
|
+
if (key !== KEYS.suggestion && !key.startsWith(KEYS.suggestion)) nodeWithoutSuggestion[key] = node[key];
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
...nodeWithoutSuggestion,
|
|
273
|
+
children: withoutSuggestionAndComments(node.children)
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
return {
|
|
277
|
+
...node,
|
|
278
|
+
children: withoutSuggestionAndComments(node.children)
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return node;
|
|
520
282
|
});
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
const aiNodes = withProps((0, import_markdown2.deserializeMd)(editor, aiContent), chatNodes);
|
|
526
|
-
const diffNodes = withTransient(
|
|
527
|
-
(0, import_suggestion3.diffToSuggestions)(editor, chatNodes, aiNodes, {
|
|
528
|
-
ignoreProps: ["id", "listStart"]
|
|
529
|
-
})
|
|
530
|
-
);
|
|
531
|
-
return diffNodes;
|
|
283
|
+
const getDiffNodes = (editor, aiContent) => {
|
|
284
|
+
/** Original document nodes */
|
|
285
|
+
const chatNodes = withoutSuggestionAndComments(editor.getOption(AIChatPlugin, "chatNodes"));
|
|
286
|
+
return withTransient(diffToSuggestions(editor, chatNodes, withProps(deserializeMd(editor, aiContent), chatNodes), { ignoreProps: ["id", "listStart"] }));
|
|
532
287
|
};
|
|
533
288
|
|
|
534
|
-
|
|
535
|
-
|
|
289
|
+
//#endregion
|
|
290
|
+
//#region src/react/ai-chat/utils/getLastAssistantMessage.ts
|
|
536
291
|
function getLastAssistantMessage(editor) {
|
|
537
|
-
|
|
538
|
-
return messages?.findLast((message) => message.role === "assistant");
|
|
292
|
+
return editor.getOptions(AIChatPlugin).chat.messages?.findLast((message) => message.role === "assistant");
|
|
539
293
|
}
|
|
540
294
|
function useLastAssistantMessage() {
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
295
|
+
const toolName = usePluginOption(AIChatPlugin, "toolName");
|
|
296
|
+
const chat = usePluginOption(AIChatPlugin, "chat");
|
|
297
|
+
if (toolName === "comment") return;
|
|
298
|
+
return chat.messages?.findLast((message) => message.role === "assistant");
|
|
545
299
|
}
|
|
546
300
|
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
editor.tf.unsetNodes([(0, import_suggestion4.getTransientSuggestionKey)()], {
|
|
567
|
-
at: [],
|
|
568
|
-
mode: "all",
|
|
569
|
-
match: (n) => !!n[(0, import_suggestion4.getTransientSuggestionKey)()]
|
|
570
|
-
});
|
|
301
|
+
//#endregion
|
|
302
|
+
//#region src/react/ai-chat/utils/rejectAISuggestions.ts
|
|
303
|
+
const rejectAISuggestions = (editor) => {
|
|
304
|
+
editor.getApi(SuggestionPlugin).suggestion.nodes({ transient: true }).forEach(([suggestionNode]) => {
|
|
305
|
+
const suggestionData = editor.getApi(SuggestionPlugin).suggestion.suggestionData(suggestionNode);
|
|
306
|
+
if (!suggestionData) return;
|
|
307
|
+
rejectSuggestion(editor, {
|
|
308
|
+
createdAt: new Date(suggestionData.createdAt),
|
|
309
|
+
keyId: getSuggestionKey(suggestionData.id),
|
|
310
|
+
suggestionId: suggestionData.id,
|
|
311
|
+
type: suggestionData.type,
|
|
312
|
+
userId: suggestionData.userId
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
editor.tf.unsetNodes([getTransientSuggestionKey()], {
|
|
316
|
+
at: [],
|
|
317
|
+
mode: "all",
|
|
318
|
+
match: (n) => !!n[getTransientSuggestionKey()]
|
|
319
|
+
});
|
|
571
320
|
};
|
|
572
321
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
}
|
|
588
|
-
setOptions({
|
|
589
|
-
_replaceIds: [],
|
|
590
|
-
chatNodes: [],
|
|
591
|
-
mode: "insert",
|
|
592
|
-
toolName: null
|
|
593
|
-
});
|
|
594
|
-
if (undo) {
|
|
595
|
-
editor.getTransforms(AIPlugin).ai.undo();
|
|
596
|
-
}
|
|
322
|
+
//#endregion
|
|
323
|
+
//#region src/react/ai-chat/utils/resetAIChat.ts
|
|
324
|
+
const resetAIChat = (editor, { undo = true } = {}) => {
|
|
325
|
+
const { api, getOptions, setOptions } = getEditorPlugin(editor, { key: KEYS.aiChat });
|
|
326
|
+
api.aiChat.stop();
|
|
327
|
+
const chat = getOptions().chat;
|
|
328
|
+
if (chat.messages && chat.messages.length > 0) chat.setMessages?.([]);
|
|
329
|
+
setOptions({
|
|
330
|
+
_replaceIds: [],
|
|
331
|
+
chatNodes: [],
|
|
332
|
+
mode: "insert",
|
|
333
|
+
toolName: null
|
|
334
|
+
});
|
|
335
|
+
if (undo) editor.getTransforms(AIPlugin).ai.undo();
|
|
597
336
|
};
|
|
598
337
|
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
const blocksRange = editor.api.nodesRange(blocks);
|
|
634
|
-
const promptText = getEditorPrompt(editor, {
|
|
635
|
-
prompt
|
|
636
|
-
});
|
|
637
|
-
const selection = blocks.length > 0 ? blocksRange : editor.selection;
|
|
638
|
-
let chatNodes;
|
|
639
|
-
if (blocks.length > 0) {
|
|
640
|
-
chatNodes = blocks.map((block) => block[0]);
|
|
641
|
-
} else {
|
|
642
|
-
const selectionBlocks = editor.api.blocks({ mode: "highest" });
|
|
643
|
-
if (selectionBlocks.length > 1) {
|
|
644
|
-
chatNodes = selectionBlocks.map((block) => block[0]);
|
|
645
|
-
} else {
|
|
646
|
-
chatNodes = editor.api.fragment();
|
|
647
|
-
}
|
|
648
|
-
}
|
|
649
|
-
setOption("chatNodes", chatNodes);
|
|
650
|
-
setOption("chatSelection", blocks.length > 0 ? null : editor.selection);
|
|
651
|
-
const ctx = {
|
|
652
|
-
children: editor.children,
|
|
653
|
-
selection: selection ?? null,
|
|
654
|
-
toolName
|
|
655
|
-
};
|
|
656
|
-
void chat.sendMessage?.(
|
|
657
|
-
{
|
|
658
|
-
text: promptText
|
|
659
|
-
},
|
|
660
|
-
{
|
|
661
|
-
body: {
|
|
662
|
-
ctx
|
|
663
|
-
},
|
|
664
|
-
...options
|
|
665
|
-
}
|
|
666
|
-
);
|
|
338
|
+
//#endregion
|
|
339
|
+
//#region src/react/ai-chat/utils/submitAIChat.ts
|
|
340
|
+
const submitAIChat = (editor, input, { mode, options, prompt, toolName: toolNameProps } = {}) => {
|
|
341
|
+
const { getOptions, setOption } = getEditorPlugin(editor, { key: KEYS.aiChat });
|
|
342
|
+
const { chat, toolName: toolNameOption } = getOptions();
|
|
343
|
+
const toolName = toolNameProps ?? toolNameOption ?? null;
|
|
344
|
+
if (!prompt && input?.length === 0) return;
|
|
345
|
+
if (!prompt) prompt = input;
|
|
346
|
+
if (!mode) mode = isSelecting(editor) ? "chat" : "insert";
|
|
347
|
+
if (mode === "insert") editor.getTransforms(AIPlugin).ai.undo();
|
|
348
|
+
setOption("mode", mode);
|
|
349
|
+
setOption("toolName", toolName);
|
|
350
|
+
const blocks = editor.getApi(BlockSelectionPlugin).blockSelection.getNodes();
|
|
351
|
+
const blocksRange = editor.api.nodesRange(blocks);
|
|
352
|
+
const promptText = getEditorPrompt(editor, { prompt });
|
|
353
|
+
const selection = blocks.length > 0 ? blocksRange : editor.selection;
|
|
354
|
+
let chatNodes;
|
|
355
|
+
if (blocks.length > 0) chatNodes = blocks.map((block) => block[0]);
|
|
356
|
+
else {
|
|
357
|
+
const selectionBlocks = editor.api.blocks({ mode: "highest" });
|
|
358
|
+
if (selectionBlocks.length > 1) chatNodes = selectionBlocks.map((block) => block[0]);
|
|
359
|
+
else chatNodes = editor.api.fragment();
|
|
360
|
+
}
|
|
361
|
+
setOption("chatNodes", chatNodes);
|
|
362
|
+
setOption("chatSelection", blocks.length > 0 ? null : editor.selection);
|
|
363
|
+
const ctx = {
|
|
364
|
+
children: editor.children,
|
|
365
|
+
selection: selection ?? null,
|
|
366
|
+
toolName
|
|
367
|
+
};
|
|
368
|
+
chat.sendMessage?.({ text: promptText }, {
|
|
369
|
+
body: { ctx },
|
|
370
|
+
...options
|
|
371
|
+
});
|
|
667
372
|
};
|
|
668
373
|
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
return blocks.map((block, index) => {
|
|
697
|
-
if (format === "single" && index > 0) {
|
|
698
|
-
return block;
|
|
699
|
-
}
|
|
700
|
-
return applyTextFormatting({
|
|
701
|
-
...block,
|
|
702
|
-
...blockProps
|
|
703
|
-
});
|
|
704
|
-
});
|
|
374
|
+
//#endregion
|
|
375
|
+
//#region src/react/ai-chat/transforms/replaceSelectionAIChat.ts
|
|
376
|
+
const createFormattedBlocks = ({ blocks, format, sourceBlock }) => {
|
|
377
|
+
if (format === "none") return cloneDeep(blocks);
|
|
378
|
+
const [sourceNode] = sourceBlock;
|
|
379
|
+
const firstTextEntry = NodeApi.firstText(sourceNode);
|
|
380
|
+
if (!firstTextEntry) return null;
|
|
381
|
+
const blockProps = NodeApi.extractProps(sourceNode);
|
|
382
|
+
const textProps = NodeApi.extractProps(firstTextEntry[0]);
|
|
383
|
+
const applyTextFormatting = (node) => {
|
|
384
|
+
if (TextApi.isText(node)) return {
|
|
385
|
+
...textProps,
|
|
386
|
+
...node
|
|
387
|
+
};
|
|
388
|
+
if (node.children) return {
|
|
389
|
+
...node,
|
|
390
|
+
children: node.children.map(applyTextFormatting)
|
|
391
|
+
};
|
|
392
|
+
return node;
|
|
393
|
+
};
|
|
394
|
+
return blocks.map((block, index) => {
|
|
395
|
+
if (format === "single" && index > 0) return block;
|
|
396
|
+
return applyTextFormatting({
|
|
397
|
+
...block,
|
|
398
|
+
...blockProps
|
|
399
|
+
});
|
|
400
|
+
});
|
|
705
401
|
};
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
format,
|
|
759
|
-
sourceBlock: selectedBlocks[0]
|
|
760
|
-
});
|
|
761
|
-
if (!formattedBlocks) return;
|
|
762
|
-
editor.tf.withoutNormalizing(() => {
|
|
763
|
-
(0, import_react10.removeBlockSelectionNodes)(editor);
|
|
764
|
-
editor.tf.withNewBatch(() => {
|
|
765
|
-
editor.getTransforms(import_react10.BlockSelectionPlugin).blockSelection.insertBlocksAndSelect(formattedBlocks, {
|
|
766
|
-
at: firstBlockPath
|
|
767
|
-
});
|
|
768
|
-
});
|
|
769
|
-
});
|
|
770
|
-
editor.getApi(import_react10.BlockSelectionPlugin).blockSelection.focus();
|
|
402
|
+
const replaceSelectionAIChat = (editor, sourceEditor, { format = "single" } = {}) => {
|
|
403
|
+
if (!sourceEditor || sourceEditor.api.isEmpty()) return;
|
|
404
|
+
const isBlockSelecting = editor.getOption(BlockSelectionPlugin, "isSelectingSome");
|
|
405
|
+
editor.getApi({ key: KEYS.ai }).aiChat.hide();
|
|
406
|
+
if (!isBlockSelecting) {
|
|
407
|
+
const firstBlock = editor.api.node({
|
|
408
|
+
block: true,
|
|
409
|
+
mode: "lowest"
|
|
410
|
+
});
|
|
411
|
+
if (firstBlock && editor.api.isSelected(firstBlock[1], { contains: true }) && format !== "none") {
|
|
412
|
+
const formattedBlocks$1 = createFormattedBlocks({
|
|
413
|
+
blocks: cloneDeep(sourceEditor.children),
|
|
414
|
+
format,
|
|
415
|
+
sourceBlock: firstBlock
|
|
416
|
+
});
|
|
417
|
+
if (!formattedBlocks$1) return;
|
|
418
|
+
/** When user selection is cover the whole code block */
|
|
419
|
+
if (firstBlock[0].type === KEYS.codeLine && sourceEditor.children[0].type === KEYS.codeBlock && sourceEditor.children.length === 1) editor.tf.insertFragment(formattedBlocks$1[0].children);
|
|
420
|
+
else editor.tf.insertFragment(formattedBlocks$1);
|
|
421
|
+
editor.tf.focus();
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
editor.tf.insertFragment(sourceEditor.children);
|
|
425
|
+
editor.tf.focus();
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
const selectedBlocks = editor.getApi(BlockSelectionPlugin).blockSelection.getNodes();
|
|
429
|
+
if (selectedBlocks.length === 0) return;
|
|
430
|
+
if (format === "none" || format === "single" && selectedBlocks.length > 1) {
|
|
431
|
+
editor.tf.withoutNormalizing(() => {
|
|
432
|
+
removeBlockSelectionNodes(editor);
|
|
433
|
+
editor.tf.withNewBatch(() => {
|
|
434
|
+
editor.getTransforms(BlockSelectionPlugin).blockSelection.insertBlocksAndSelect(cloneDeep(sourceEditor.children), { at: selectedBlocks[0][1] });
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
editor.getApi(BlockSelectionPlugin).blockSelection.focus();
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
const [, firstBlockPath] = selectedBlocks[0];
|
|
441
|
+
const formattedBlocks = createFormattedBlocks({
|
|
442
|
+
blocks: cloneDeep(sourceEditor.children),
|
|
443
|
+
format,
|
|
444
|
+
sourceBlock: selectedBlocks[0]
|
|
445
|
+
});
|
|
446
|
+
if (!formattedBlocks) return;
|
|
447
|
+
editor.tf.withoutNormalizing(() => {
|
|
448
|
+
removeBlockSelectionNodes(editor);
|
|
449
|
+
editor.tf.withNewBatch(() => {
|
|
450
|
+
editor.getTransforms(BlockSelectionPlugin).blockSelection.insertBlocksAndSelect(formattedBlocks, { at: firstBlockPath });
|
|
451
|
+
});
|
|
452
|
+
});
|
|
453
|
+
editor.getApi(BlockSelectionPlugin).blockSelection.focus();
|
|
771
454
|
};
|
|
772
455
|
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
editor.getApi(AIChatPlugin).aiChat.hide({ focus: false });
|
|
456
|
+
//#endregion
|
|
457
|
+
//#region src/react/ai-chat/transforms/insertBelowAIChat.ts
|
|
458
|
+
const insertBelowAIChat = (editor, sourceEditor, { format = "single" } = {}) => {
|
|
459
|
+
const { toolName } = editor.getOptions(AIChatPlugin);
|
|
460
|
+
if (toolName === "generate") return insertBelowGenerate(editor, sourceEditor, { format });
|
|
461
|
+
const selectedBlocks = editor.getApi(BlockSelectionPlugin).blockSelection.getNodes();
|
|
462
|
+
const selectedIds = editor.getOptions(BlockSelectionPlugin).selectedIds;
|
|
463
|
+
editor.getTransforms(AIPlugin).ai.undo();
|
|
464
|
+
const insertBlocksAndSelect = editor.getTransforms(BlockSelectionPlugin).blockSelection.insertBlocksAndSelect;
|
|
465
|
+
if (!selectedIds || selectedIds.size === 0) return;
|
|
466
|
+
const lastBlock = selectedBlocks.at(-1);
|
|
467
|
+
if (!lastBlock) return;
|
|
468
|
+
const nextPath = PathApi.next(lastBlock[1]);
|
|
469
|
+
insertBlocksAndSelect(selectedBlocks.map((block) => block[0]), {
|
|
470
|
+
at: nextPath,
|
|
471
|
+
insertedCallback: () => {
|
|
472
|
+
withAIBatch(editor, () => {
|
|
473
|
+
acceptAISuggestions(editor);
|
|
474
|
+
});
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
editor.getApi(AIChatPlugin).aiChat.hide({ focus: false });
|
|
796
478
|
};
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
}
|
|
842
|
-
const formattedBlocks = createFormattedBlocks({
|
|
843
|
-
blocks: (0, import_cloneDeep2.default)(sourceEditor.children),
|
|
844
|
-
format,
|
|
845
|
-
sourceBlock: currentBlock
|
|
846
|
-
});
|
|
847
|
-
if (!formattedBlocks) return;
|
|
848
|
-
insertBlocksAndSelect(formattedBlocks, {
|
|
849
|
-
at: import_platejs12.PathApi.next(endPath)
|
|
850
|
-
});
|
|
851
|
-
}
|
|
479
|
+
const insertBelowGenerate = (editor, sourceEditor, { format = "single" } = {}) => {
|
|
480
|
+
if (!sourceEditor || sourceEditor.api.isEmpty()) return;
|
|
481
|
+
const isBlockSelecting = editor.getOption(BlockSelectionPlugin, "isSelectingSome");
|
|
482
|
+
editor.getApi({ key: KEYS.ai }).aiChat.hide();
|
|
483
|
+
const insertBlocksAndSelect = editor.getTransforms(BlockSelectionPlugin).blockSelection.insertBlocksAndSelect;
|
|
484
|
+
if (isBlockSelecting) {
|
|
485
|
+
const selectedBlocks = editor.getApi(BlockSelectionPlugin).blockSelection.getNodes();
|
|
486
|
+
const selectedIds = editor.getOptions(BlockSelectionPlugin).selectedIds;
|
|
487
|
+
if (!selectedIds || selectedIds.size === 0) return;
|
|
488
|
+
const lastBlock = selectedBlocks.at(-1);
|
|
489
|
+
if (!lastBlock) return;
|
|
490
|
+
const nextPath = PathApi.next(lastBlock[1]);
|
|
491
|
+
if (format === "none") {
|
|
492
|
+
insertBlocksAndSelect(cloneDeep(sourceEditor.children), { at: nextPath });
|
|
493
|
+
return;
|
|
494
|
+
}
|
|
495
|
+
const formattedBlocks = createFormattedBlocks({
|
|
496
|
+
blocks: cloneDeep(sourceEditor.children),
|
|
497
|
+
format,
|
|
498
|
+
sourceBlock: lastBlock
|
|
499
|
+
});
|
|
500
|
+
if (!formattedBlocks) return;
|
|
501
|
+
insertBlocksAndSelect(formattedBlocks, { at: nextPath });
|
|
502
|
+
} else {
|
|
503
|
+
const [, end] = RangeApi.edges(editor.selection);
|
|
504
|
+
const endPath = [end.path[0]];
|
|
505
|
+
const currentBlock = editor.api.node({
|
|
506
|
+
at: endPath,
|
|
507
|
+
block: true,
|
|
508
|
+
mode: "lowest"
|
|
509
|
+
});
|
|
510
|
+
if (!currentBlock) return;
|
|
511
|
+
if (format === "none") {
|
|
512
|
+
insertBlocksAndSelect(cloneDeep(sourceEditor.children), { at: PathApi.next(endPath) });
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
const formattedBlocks = createFormattedBlocks({
|
|
516
|
+
blocks: cloneDeep(sourceEditor.children),
|
|
517
|
+
format,
|
|
518
|
+
sourceBlock: currentBlock
|
|
519
|
+
});
|
|
520
|
+
if (!formattedBlocks) return;
|
|
521
|
+
insertBlocksAndSelect(formattedBlocks, { at: PathApi.next(endPath) });
|
|
522
|
+
}
|
|
852
523
|
};
|
|
853
524
|
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
525
|
+
//#endregion
|
|
526
|
+
//#region src/react/ai-chat/transforms/removeAnchorAIChat.ts
|
|
527
|
+
const removeAnchorAIChat = (editor, options) => {
|
|
528
|
+
editor.tf.withoutSaving(() => {
|
|
529
|
+
editor.tf.removeNodes({
|
|
530
|
+
at: [],
|
|
531
|
+
match: (n) => ElementApi.isElement(n) && n.type === getPluginType(editor, KEYS.aiChat),
|
|
532
|
+
...options
|
|
533
|
+
});
|
|
534
|
+
});
|
|
864
535
|
};
|
|
865
536
|
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
if (fn()) return;
|
|
905
|
-
return insertText(text, options);
|
|
906
|
-
},
|
|
907
|
-
normalizeNode(entry) {
|
|
908
|
-
const [node, path] = entry;
|
|
909
|
-
if (node[import_platejs14.KEYS.ai] && !getOptions().open) {
|
|
910
|
-
tf.ai.removeMarks({ at: path });
|
|
911
|
-
return;
|
|
912
|
-
}
|
|
913
|
-
if (import_platejs14.ElementApi.isElement(node) && node.type === type && !getOptions().open) {
|
|
914
|
-
editor.getTransforms(AIChatPlugin).aiChat.removeAnchor({ at: path });
|
|
915
|
-
return;
|
|
916
|
-
}
|
|
917
|
-
return normalizeNode(entry);
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
};
|
|
537
|
+
//#endregion
|
|
538
|
+
//#region src/react/ai-chat/withAIChat.ts
|
|
539
|
+
const withAIChat = ({ api, editor, getOptions, tf: { insertText, normalizeNode }, type }) => {
|
|
540
|
+
const tf = editor.getTransforms(AIPlugin);
|
|
541
|
+
const matchesTrigger = (text) => {
|
|
542
|
+
const { trigger } = getOptions();
|
|
543
|
+
if (trigger instanceof RegExp) return trigger.test(text);
|
|
544
|
+
if (Array.isArray(trigger)) return trigger.includes(text);
|
|
545
|
+
return text === trigger;
|
|
546
|
+
};
|
|
547
|
+
return { transforms: {
|
|
548
|
+
insertText(text, options) {
|
|
549
|
+
const { triggerPreviousCharPattern, triggerQuery } = getOptions();
|
|
550
|
+
const fn = () => {
|
|
551
|
+
if (!editor.selection || !matchesTrigger(text) || triggerQuery && !triggerQuery(editor)) return;
|
|
552
|
+
const previousChar = editor.api.string(editor.api.range("before", editor.selection));
|
|
553
|
+
if (!triggerPreviousCharPattern?.test(previousChar)) return;
|
|
554
|
+
const nodeEntry = editor.api.block({ highest: true });
|
|
555
|
+
if (!nodeEntry || !editor.api.isEmpty(nodeEntry[0])) return;
|
|
556
|
+
api.aiChat.show();
|
|
557
|
+
return true;
|
|
558
|
+
};
|
|
559
|
+
if (fn()) return;
|
|
560
|
+
return insertText(text, options);
|
|
561
|
+
},
|
|
562
|
+
normalizeNode(entry) {
|
|
563
|
+
const [node, path] = entry;
|
|
564
|
+
if (node[KEYS.ai] && !getOptions().open) {
|
|
565
|
+
tf.ai.removeMarks({ at: path });
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
if (ElementApi.isElement(node) && node.type === type && !getOptions().open) {
|
|
569
|
+
editor.getTransforms(AIChatPlugin).aiChat.removeAnchor({ at: path });
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
return normalizeNode(entry);
|
|
573
|
+
}
|
|
574
|
+
} };
|
|
921
575
|
};
|
|
922
576
|
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
}
|
|
577
|
+
//#endregion
|
|
578
|
+
//#region src/react/ai-chat/AIChatPlugin.ts
|
|
579
|
+
const AIChatPlugin = createTPlatePlugin({
|
|
580
|
+
key: KEYS.aiChat,
|
|
581
|
+
dependencies: ["ai"],
|
|
582
|
+
node: { isElement: true },
|
|
583
|
+
options: {
|
|
584
|
+
_blockChunks: "",
|
|
585
|
+
_blockPath: null,
|
|
586
|
+
_mdxName: null,
|
|
587
|
+
_replaceIds: [],
|
|
588
|
+
aiEditor: null,
|
|
589
|
+
chat: { messages: [] },
|
|
590
|
+
chatNodes: [],
|
|
591
|
+
chatSelection: null,
|
|
592
|
+
experimental_lastTextId: null,
|
|
593
|
+
mode: "insert",
|
|
594
|
+
open: false,
|
|
595
|
+
streaming: false,
|
|
596
|
+
toolName: null,
|
|
597
|
+
trigger: " ",
|
|
598
|
+
triggerPreviousCharPattern: /^\s?$/
|
|
599
|
+
}
|
|
947
600
|
}).overrideEditor(withAIChat).extendApi(({ editor, getOption, getOptions, setOption, type }) => ({
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
body: {
|
|
993
|
-
ctx
|
|
994
|
-
}
|
|
995
|
-
});
|
|
996
|
-
},
|
|
997
|
-
stop: () => {
|
|
998
|
-
setOption("streaming", false);
|
|
999
|
-
getOptions().chat.stop?.();
|
|
1000
|
-
}
|
|
601
|
+
reset: bindFirst(resetAIChat, editor),
|
|
602
|
+
submit: bindFirst(submitAIChat, editor),
|
|
603
|
+
node: (options = {}) => {
|
|
604
|
+
const { anchor = false, streaming = false, ...rest } = options;
|
|
605
|
+
if (anchor) return editor.api.node({
|
|
606
|
+
at: [],
|
|
607
|
+
match: (n) => ElementApi.isElement(n) && n.type === type,
|
|
608
|
+
...rest
|
|
609
|
+
});
|
|
610
|
+
if (streaming) {
|
|
611
|
+
if (!getOption("streaming")) return;
|
|
612
|
+
const path = getOption("_blockPath");
|
|
613
|
+
if (!path) return;
|
|
614
|
+
return editor.api.node({
|
|
615
|
+
at: path,
|
|
616
|
+
mode: "lowest",
|
|
617
|
+
reverse: true,
|
|
618
|
+
match: (t) => !!t[getPluginType(editor, KEYS.ai)],
|
|
619
|
+
...rest
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
return editor.api.node({
|
|
623
|
+
match: (n) => n[getPluginType(editor, KEYS.ai)],
|
|
624
|
+
...rest
|
|
625
|
+
});
|
|
626
|
+
},
|
|
627
|
+
reload: () => {
|
|
628
|
+
const { chat, chatNodes, chatSelection } = getOptions();
|
|
629
|
+
editor.getTransforms(AIPlugin).ai.undo();
|
|
630
|
+
if (chatSelection) editor.tf.setSelection(chatSelection);
|
|
631
|
+
else editor.getApi(BlockSelectionPlugin).blockSelection.set(chatNodes.map((node) => node.id));
|
|
632
|
+
const blocks = editor.getApi(BlockSelectionPlugin).blockSelection.getNodes();
|
|
633
|
+
const selection = blocks.length > 0 ? editor.api.nodesRange(blocks) : editor.selection;
|
|
634
|
+
const ctx = {
|
|
635
|
+
children: editor.children,
|
|
636
|
+
selection: selection ?? null,
|
|
637
|
+
toolName: getOption("toolName")
|
|
638
|
+
};
|
|
639
|
+
chat.regenerate?.({ body: { ctx } });
|
|
640
|
+
},
|
|
641
|
+
stop: () => {
|
|
642
|
+
setOption("streaming", false);
|
|
643
|
+
getOptions().chat.stop?.();
|
|
644
|
+
}
|
|
1001
645
|
})).extendApi(({ api, editor, getOptions, setOption, tf }) => ({
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
lastBatch.ai = void 0;
|
|
1018
|
-
}
|
|
1019
|
-
tf.aiChat.removeAnchor();
|
|
1020
|
-
},
|
|
1021
|
-
show: () => {
|
|
1022
|
-
api.aiChat.reset();
|
|
1023
|
-
setOption("toolName", null);
|
|
1024
|
-
getOptions().chat.setMessages?.([]);
|
|
1025
|
-
setOption("open", true);
|
|
1026
|
-
}
|
|
646
|
+
hide: ({ focus = true, undo = true } = {}) => {
|
|
647
|
+
api.aiChat.reset({ undo });
|
|
648
|
+
setOption("open", false);
|
|
649
|
+
if (focus) if (editor.getOption(BlockSelectionPlugin, "isSelectingSome")) editor.getApi(BlockSelectionPlugin).blockSelection.focus();
|
|
650
|
+
else editor.tf.focus();
|
|
651
|
+
const lastBatch = editor.history.undos.at(-1);
|
|
652
|
+
if (lastBatch?.ai) lastBatch.ai = void 0;
|
|
653
|
+
tf.aiChat.removeAnchor();
|
|
654
|
+
},
|
|
655
|
+
show: () => {
|
|
656
|
+
api.aiChat.reset();
|
|
657
|
+
setOption("toolName", null);
|
|
658
|
+
getOptions().chat.setMessages?.([]);
|
|
659
|
+
setOption("open", true);
|
|
660
|
+
}
|
|
1027
661
|
})).extendTransforms(({ editor }) => ({
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
662
|
+
accept: bindFirst(acceptAIChat, editor),
|
|
663
|
+
insertBelow: bindFirst(insertBelowAIChat, editor),
|
|
664
|
+
removeAnchor: bindFirst(removeAnchorAIChat, editor),
|
|
665
|
+
replaceSelection: bindFirst(replaceSelectionAIChat, editor)
|
|
1032
666
|
}));
|
|
1033
667
|
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
setOption("aiEditor", editor);
|
|
1050
|
-
}, [editor, setOption]);
|
|
668
|
+
//#endregion
|
|
669
|
+
//#region src/react/ai-chat/hooks/useAIChatEditor.ts
|
|
670
|
+
/**
|
|
671
|
+
* Register an editor in the AI chat plugin, and deserializes the content into
|
|
672
|
+
* `editor.children` with block-level memoization.
|
|
673
|
+
*/
|
|
674
|
+
const useAIChatEditor = (editor, content, { parser } = {}) => {
|
|
675
|
+
const { setOption } = useEditorPlugin(AIChatPlugin);
|
|
676
|
+
editor.children = useMemo(() => editor.getApi(MarkdownPlugin).markdown.deserialize(content, {
|
|
677
|
+
memoize: true,
|
|
678
|
+
parser
|
|
679
|
+
}), [content]);
|
|
680
|
+
useEffect(() => {
|
|
681
|
+
setOption("aiEditor", editor);
|
|
682
|
+
}, [editor, setOption]);
|
|
1051
683
|
};
|
|
1052
684
|
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
return;
|
|
1083
|
-
}
|
|
1084
|
-
const chunk = content.slice(insertedTextRef.current.length);
|
|
1085
|
-
const nodes = [];
|
|
1086
|
-
if (chunk) {
|
|
1087
|
-
const isFirst = insertedTextRef.current === "";
|
|
1088
|
-
nodes.push({ text: chunk });
|
|
1089
|
-
onChunk({
|
|
1090
|
-
chunk,
|
|
1091
|
-
isFirst,
|
|
1092
|
-
nodes,
|
|
1093
|
-
text: content
|
|
1094
|
-
});
|
|
1095
|
-
}
|
|
1096
|
-
insertedTextRef.current = content;
|
|
1097
|
-
}, [content]);
|
|
685
|
+
//#endregion
|
|
686
|
+
//#region src/react/ai-chat/hooks/useChatChunk.ts
|
|
687
|
+
const useChatChunk = ({ onChunk, onFinish }) => {
|
|
688
|
+
const { status } = usePluginOption({ key: KEYS.aiChat }, "chat");
|
|
689
|
+
const isLoading = status === "streaming" || status === "submitted";
|
|
690
|
+
const content = useLastAssistantMessage()?.parts.find((part) => part.type === "text")?.text;
|
|
691
|
+
const insertedTextRef = useRef("");
|
|
692
|
+
const prevIsLoadingRef = useRef(isLoading);
|
|
693
|
+
useEffect(() => {
|
|
694
|
+
if (!isLoading) insertedTextRef.current = "";
|
|
695
|
+
if (prevIsLoadingRef.current && !isLoading) onFinish?.({ content: content ?? "" });
|
|
696
|
+
prevIsLoadingRef.current = isLoading;
|
|
697
|
+
}, [isLoading]);
|
|
698
|
+
useEffect(() => {
|
|
699
|
+
if (!content) return;
|
|
700
|
+
const chunk = content.slice(insertedTextRef.current.length);
|
|
701
|
+
const nodes = [];
|
|
702
|
+
if (chunk) {
|
|
703
|
+
const isFirst = insertedTextRef.current === "";
|
|
704
|
+
nodes.push({ text: chunk });
|
|
705
|
+
onChunk({
|
|
706
|
+
chunk,
|
|
707
|
+
isFirst,
|
|
708
|
+
nodes,
|
|
709
|
+
text: content
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
insertedTextRef.current = content;
|
|
713
|
+
}, [content]);
|
|
1098
714
|
};
|
|
1099
715
|
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
}
|
|
1126
|
-
if (onOpenCursor && editor.api.isCollapsed()) {
|
|
1127
|
-
onOpenCursor();
|
|
1128
|
-
return;
|
|
1129
|
-
}
|
|
1130
|
-
if (onOpenSelection && editor.api.isExpanded()) {
|
|
1131
|
-
onOpenSelection();
|
|
1132
|
-
return;
|
|
1133
|
-
}
|
|
1134
|
-
}
|
|
1135
|
-
}, [open]);
|
|
716
|
+
//#endregion
|
|
717
|
+
//#region src/react/ai-chat/hooks/useEditorChat.ts
|
|
718
|
+
const useEditorChat = ({ onOpenBlockSelection, onOpenChange, onOpenCursor, onOpenSelection }) => {
|
|
719
|
+
const { editor } = useEditorPlugin(AIChatPlugin);
|
|
720
|
+
const open = usePluginOption(AIChatPlugin, "open");
|
|
721
|
+
useEffect(() => {
|
|
722
|
+
onOpenChange?.(open);
|
|
723
|
+
if (open) {
|
|
724
|
+
if (onOpenBlockSelection) {
|
|
725
|
+
const blockSelectionApi = editor.getApi(BlockSelectionPlugin).blockSelection;
|
|
726
|
+
if (editor.getOption(BlockSelectionPlugin, "isSelectingSome")) {
|
|
727
|
+
onOpenBlockSelection(blockSelectionApi.getNodes());
|
|
728
|
+
return;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
if (onOpenCursor && editor.api.isCollapsed()) {
|
|
732
|
+
onOpenCursor();
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
if (onOpenSelection && editor.api.isExpanded()) {
|
|
736
|
+
onOpenSelection();
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
}, [open]);
|
|
1136
741
|
};
|
|
1137
742
|
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
// src/react/ai-chat/streaming/streamDeserializeMd.ts
|
|
1143
|
-
var import_markdown5 = require("@platejs/markdown");
|
|
1144
|
-
var import_platejs19 = require("platejs");
|
|
743
|
+
//#endregion
|
|
744
|
+
//#region src/react/ai-chat/streaming/streamDeserializeInlineMd.ts
|
|
745
|
+
const streamDeserializeInlineMd = (editor, text, options) => editor.getApi(MarkdownPlugin).markdown.deserializeInline(text, options);
|
|
1145
746
|
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
return chunk.slice(str.length);
|
|
1153
|
-
}
|
|
1154
|
-
return chunk.slice(0, chunk.length - str.length);
|
|
747
|
+
//#endregion
|
|
748
|
+
//#region src/react/ai-chat/streaming/utils/utils.ts
|
|
749
|
+
const getChunkTrimmed = (chunk, { direction = "right" } = {}) => {
|
|
750
|
+
const str = direction === "right" ? chunk.trimEnd() : chunk.trimStart();
|
|
751
|
+
if (direction === "right") return chunk.slice(str.length);
|
|
752
|
+
return chunk.slice(0, chunk.length - str.length);
|
|
1155
753
|
};
|
|
1156
754
|
function isCompleteCodeBlock(str) {
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
755
|
+
const trimmed = str.trim();
|
|
756
|
+
const startsWithCodeBlock = trimmed.startsWith("```");
|
|
757
|
+
const endsWithCodeBlock = trimmed.endsWith("```");
|
|
758
|
+
return startsWithCodeBlock && endsWithCodeBlock;
|
|
1161
759
|
}
|
|
1162
760
|
function isCompleteMath(str) {
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
761
|
+
const trimmed = str.trim();
|
|
762
|
+
const startsWithMath = trimmed.startsWith("$$");
|
|
763
|
+
const endsWithMath = trimmed.endsWith("$$");
|
|
764
|
+
return startsWithMath && endsWithMath;
|
|
1167
765
|
}
|
|
1168
766
|
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
return res;
|
|
767
|
+
//#endregion
|
|
768
|
+
//#region src/react/ai-chat/streaming/utils/escapeInput.ts
|
|
769
|
+
const escapeInput = (data) => {
|
|
770
|
+
let res = data;
|
|
771
|
+
if (data.startsWith("$$") && !data.startsWith("$$\n") && !isCompleteMath(data)) res = data.replace("$$", String.raw`\$\$`);
|
|
772
|
+
return res;
|
|
1176
773
|
};
|
|
1177
774
|
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
};
|
|
1192
|
-
}
|
|
1193
|
-
return node;
|
|
775
|
+
//#endregion
|
|
776
|
+
//#region src/react/ai-chat/streaming/utils/getListNode.ts
|
|
777
|
+
const getListNode = (editor, node) => {
|
|
778
|
+
if (node.listStyleType && node.listStart) {
|
|
779
|
+
const previousNode = editor.api.previous({ at: editor.selection?.focus })?.[0];
|
|
780
|
+
if (previousNode?.listStyleType && previousNode?.listStart) return node;
|
|
781
|
+
if (node.listStart === 1) return node;
|
|
782
|
+
return {
|
|
783
|
+
...node,
|
|
784
|
+
listRestartPolite: node.listStart
|
|
785
|
+
};
|
|
786
|
+
}
|
|
787
|
+
return node;
|
|
1194
788
|
};
|
|
1195
789
|
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
return node1[LIST_STYLE_TYPE] === node2[LIST_STYLE_TYPE];
|
|
1204
|
-
}
|
|
1205
|
-
return node1.type === node2.type;
|
|
790
|
+
//#endregion
|
|
791
|
+
//#region src/react/ai-chat/streaming/utils/isSameNode.ts
|
|
792
|
+
const LIST_STYLE_TYPE = "listStyleType";
|
|
793
|
+
const isSameNode = (editor, node1, node2) => {
|
|
794
|
+
if (node1.type !== editor.getType(KEYS.p) || node2.type !== editor.getType(KEYS.p)) return node1.type === node2.type;
|
|
795
|
+
if (isDefined(node1[LIST_STYLE_TYPE]) || isDefined(node2[LIST_STYLE_TYPE])) return node1[LIST_STYLE_TYPE] === node2[LIST_STYLE_TYPE];
|
|
796
|
+
return node1.type === node2.type;
|
|
1206
797
|
};
|
|
1207
798
|
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
text: node.text
|
|
1222
|
-
};
|
|
799
|
+
//#endregion
|
|
800
|
+
//#region src/react/ai-chat/streaming/utils/nodesWithProps.ts
|
|
801
|
+
const nodesWithProps = (editor, nodes, options) => nodes.map((node) => {
|
|
802
|
+
if (ElementApi.isElement(node)) return {
|
|
803
|
+
...getListNode(editor, node),
|
|
804
|
+
...options.elementProps,
|
|
805
|
+
children: nodesWithProps(editor, node.children, options)
|
|
806
|
+
};
|
|
807
|
+
return {
|
|
808
|
+
...options.textProps,
|
|
809
|
+
...node,
|
|
810
|
+
text: node.text
|
|
811
|
+
};
|
|
1223
812
|
});
|
|
1224
813
|
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
}
|
|
1268
|
-
if (unshiftNewLine && !isCodeBlockOrTable) {
|
|
1269
|
-
result.unshift({
|
|
1270
|
-
children: [{ text: "" }],
|
|
1271
|
-
type: import_platejs19.KEYS.p
|
|
1272
|
-
});
|
|
1273
|
-
}
|
|
1274
|
-
return result;
|
|
814
|
+
//#endregion
|
|
815
|
+
//#region src/react/ai-chat/streaming/streamDeserializeMd.ts
|
|
816
|
+
const statMdxTagRegex = /<([A-Za-z][A-Za-z0-9._:-]*)(?:\s[^>]*?)?(?<!\/)>/;
|
|
817
|
+
const streamDeserializeMd = (editor, data, options) => {
|
|
818
|
+
const input = escapeInput(data);
|
|
819
|
+
const value = withoutDeserializeInMdx(editor, input);
|
|
820
|
+
if (Array.isArray(value)) return value;
|
|
821
|
+
let blocks = [];
|
|
822
|
+
blocks = editor.getApi(MarkdownPlugin).markdown.deserialize(input, {
|
|
823
|
+
...options,
|
|
824
|
+
preserveEmptyParagraphs: false
|
|
825
|
+
});
|
|
826
|
+
const trimmedData = getChunkTrimmed(data);
|
|
827
|
+
const lastBlock = blocks.at(-1);
|
|
828
|
+
const addNewLine = trimmedData === "\n\n";
|
|
829
|
+
const unshiftNewLine = getChunkTrimmed(data, { direction: "left" }) === "\n\n";
|
|
830
|
+
const isCodeBlockOrTable = lastBlock?.type === "code_block" || lastBlock?.type === "table";
|
|
831
|
+
let result = blocks;
|
|
832
|
+
/**
|
|
833
|
+
* Deserialize the sting like `123\n\n` will be `123` base on markdown spec
|
|
834
|
+
* but we want to keep the `\n\n`
|
|
835
|
+
*/
|
|
836
|
+
if (lastBlock && !isCodeBlockOrTable && trimmedData.length > 0 && !addNewLine) {
|
|
837
|
+
const textNode = [{ text: trimmedData }];
|
|
838
|
+
const lastChild = lastBlock.children.at(-1);
|
|
839
|
+
/** It’s like normalizing and merging the text nodes. */
|
|
840
|
+
if (lastChild && TextApi.isText(lastChild) && Object.keys(lastChild).length === 1) {
|
|
841
|
+
lastBlock.children.pop();
|
|
842
|
+
const textNode$1 = [{ text: lastChild.text + trimmedData }];
|
|
843
|
+
lastBlock.children.push(...textNode$1);
|
|
844
|
+
} else lastBlock.children.push(...textNode);
|
|
845
|
+
result = [...blocks.slice(0, -1), lastBlock];
|
|
846
|
+
}
|
|
847
|
+
if (addNewLine && !isCodeBlockOrTable) result.push({
|
|
848
|
+
children: [{ text: "" }],
|
|
849
|
+
type: KEYS.p
|
|
850
|
+
});
|
|
851
|
+
if (unshiftNewLine && !isCodeBlockOrTable) result.unshift({
|
|
852
|
+
children: [{ text: "" }],
|
|
853
|
+
type: KEYS.p
|
|
854
|
+
});
|
|
855
|
+
return result;
|
|
1275
856
|
};
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
],
|
|
1291
|
-
type: (0, import_platejs19.getPluginType)(editor, import_platejs19.KEYS.p)
|
|
1292
|
-
}
|
|
1293
|
-
];
|
|
1294
|
-
}
|
|
1295
|
-
const newMdxName = statMdxTagRegex.exec(input)?.[1];
|
|
1296
|
-
if (input.startsWith(`<${newMdxName}`)) {
|
|
1297
|
-
editor.setOption(AIChatPlugin, "_mdxName", newMdxName ?? null);
|
|
1298
|
-
}
|
|
857
|
+
const withoutDeserializeInMdx = (editor, input) => {
|
|
858
|
+
const mdxName = editor.getOption(AIChatPlugin, "_mdxName");
|
|
859
|
+
if (mdxName) {
|
|
860
|
+
if (input.includes(`</${mdxName}>`)) {
|
|
861
|
+
editor.setOption(AIChatPlugin, "_mdxName", null);
|
|
862
|
+
return false;
|
|
863
|
+
}
|
|
864
|
+
return [{
|
|
865
|
+
children: [{ text: input }],
|
|
866
|
+
type: getPluginType(editor, KEYS.p)
|
|
867
|
+
}];
|
|
868
|
+
}
|
|
869
|
+
const newMdxName = statMdxTagRegex.exec(input)?.[1];
|
|
870
|
+
if (input.startsWith(`<${newMdxName}`)) editor.setOption(AIChatPlugin, "_mdxName", newMdxName ?? null);
|
|
1299
871
|
};
|
|
1300
872
|
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
trimmedText,
|
|
1333
|
-
value: [...value.slice(0, -1), newLastBlock]
|
|
1334
|
-
};
|
|
1335
|
-
}
|
|
1336
|
-
}
|
|
1337
|
-
return { trimmedText: "", value };
|
|
873
|
+
//#endregion
|
|
874
|
+
//#region src/react/ai-chat/streaming/streamSerializeMd.ts
|
|
875
|
+
const trimEndHeading = (editor, value) => {
|
|
876
|
+
const headingKeys = new Set([
|
|
877
|
+
KEYS.h1,
|
|
878
|
+
KEYS.h2,
|
|
879
|
+
KEYS.h3,
|
|
880
|
+
KEYS.h4,
|
|
881
|
+
KEYS.h5,
|
|
882
|
+
KEYS.h6
|
|
883
|
+
]);
|
|
884
|
+
const lastBlock = value.at(-1);
|
|
885
|
+
if (lastBlock && headingKeys.has(getPluginKey(editor, lastBlock.type) ?? lastBlock.type) && ElementApi.isElement(lastBlock)) {
|
|
886
|
+
const lastTextNode = lastBlock.children.at(-1);
|
|
887
|
+
if (TextApi.isText(lastTextNode)) {
|
|
888
|
+
const trimmedText = getChunkTrimmed(lastTextNode?.text);
|
|
889
|
+
const newChildren = [...lastBlock.children.slice(0, -1), { text: lastTextNode.text.trimEnd() }];
|
|
890
|
+
const newLastBlock = {
|
|
891
|
+
...lastBlock,
|
|
892
|
+
children: newChildren
|
|
893
|
+
};
|
|
894
|
+
return {
|
|
895
|
+
trimmedText,
|
|
896
|
+
value: [...value.slice(0, -1), newLastBlock]
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
return {
|
|
901
|
+
trimmedText: "",
|
|
902
|
+
value
|
|
903
|
+
};
|
|
1338
904
|
};
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
if (chunk.endsWith("\n\n")) {
|
|
1361
|
-
if (result === "\n") {
|
|
1362
|
-
result = "";
|
|
1363
|
-
} else if (result.endsWith("\n\n")) {
|
|
1364
|
-
result = result.slice(0, -1);
|
|
1365
|
-
}
|
|
1366
|
-
}
|
|
1367
|
-
result = result.replace(/\\([\\`*_{}\\[\]()#+\-\\.!~<>|$])/g, "$1");
|
|
1368
|
-
return result;
|
|
905
|
+
const streamSerializeMd = (editor, options, chunk) => {
|
|
906
|
+
const { value: optionsValue, ...restOptions } = options;
|
|
907
|
+
const { value } = trimEndHeading(editor, optionsValue ?? editor.children);
|
|
908
|
+
let result = "";
|
|
909
|
+
result = editor.getApi(MarkdownPlugin).markdown.serialize({
|
|
910
|
+
value,
|
|
911
|
+
...restOptions
|
|
912
|
+
});
|
|
913
|
+
const trimmedChunk = getChunkTrimmed(chunk);
|
|
914
|
+
if (isCompleteCodeBlock(result) && !chunk.endsWith("```")) result = result.trimEnd().slice(0, -3) + trimmedChunk;
|
|
915
|
+
if (isCompleteMath(result) && !chunk.endsWith("$$")) result = result.trimEnd().slice(0, -3) + trimmedChunk;
|
|
916
|
+
result = result.replace(/ /g, " ");
|
|
917
|
+
result = result.replace(/​/g, " ");
|
|
918
|
+
result = result.replace(/\u200B/g, "");
|
|
919
|
+
if (trimmedChunk !== "\n\n") result = result.trimEnd() + trimmedChunk;
|
|
920
|
+
if (chunk.endsWith("\n\n")) {
|
|
921
|
+
if (result === "\n") result = "";
|
|
922
|
+
else if (result.endsWith("\n\n")) result = result.slice(0, -1);
|
|
923
|
+
}
|
|
924
|
+
result = result.replace(/\\([\\`*_{}\\[\]()#+\-\\.!~<>|$])/g, "$1");
|
|
925
|
+
return result;
|
|
1369
926
|
};
|
|
1370
927
|
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
return result;
|
|
928
|
+
//#endregion
|
|
929
|
+
//#region src/react/ai-chat/streaming/streamInsertChunk.ts
|
|
930
|
+
const getNextPath = (path, length) => {
|
|
931
|
+
let result = path;
|
|
932
|
+
for (let i = 0; i < length; i++) result = PathApi.next(result);
|
|
933
|
+
return result;
|
|
1378
934
|
};
|
|
935
|
+
/** @experimental */
|
|
1379
936
|
function streamInsertChunk(editor, chunk, options = {}) {
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
},
|
|
1459
|
-
tempBlockChunks
|
|
1460
|
-
);
|
|
1461
|
-
editor.setOption(
|
|
1462
|
-
AIChatPlugin,
|
|
1463
|
-
"_blockChunks",
|
|
1464
|
-
// one block includes multiple children
|
|
1465
|
-
tempBlocks[0].type === (0, import_platejs21.getPluginType)(editor, import_platejs21.KEYS.codeBlock) || tempBlocks[0].type === (0, import_platejs21.getPluginType)(editor, import_platejs21.KEYS.table) || tempBlocks[0].type === (0, import_platejs21.getPluginType)(editor, import_platejs21.KEYS.equation) ? tempBlockChunks : serializedBlock2
|
|
1466
|
-
);
|
|
1467
|
-
}
|
|
1468
|
-
} else {
|
|
1469
|
-
const serializedBlock = streamSerializeMd(
|
|
1470
|
-
editor,
|
|
1471
|
-
{
|
|
1472
|
-
value: [tempBlocks[0]]
|
|
1473
|
-
},
|
|
1474
|
-
tempBlockChunks
|
|
1475
|
-
);
|
|
1476
|
-
editor.tf.replaceNodes(
|
|
1477
|
-
nodesWithProps(editor, [tempBlocks[0]], options),
|
|
1478
|
-
{
|
|
1479
|
-
at: _blockPath,
|
|
1480
|
-
select: true
|
|
1481
|
-
}
|
|
1482
|
-
);
|
|
1483
|
-
editor.setOption(AIChatPlugin, "_blockChunks", serializedBlock);
|
|
1484
|
-
}
|
|
1485
|
-
} else {
|
|
1486
|
-
editor.tf.replaceNodes(nodesWithProps(editor, [tempBlocks[0]], options), {
|
|
1487
|
-
at: _blockPath,
|
|
1488
|
-
select: true
|
|
1489
|
-
});
|
|
1490
|
-
if (tempBlocks.length > 1) {
|
|
1491
|
-
const newEndBlockPath = getNextPath(_blockPath, tempBlocks.length - 1);
|
|
1492
|
-
editor.tf.insertNodes(
|
|
1493
|
-
nodesWithProps(editor, tempBlocks.slice(1), options),
|
|
1494
|
-
{
|
|
1495
|
-
at: import_platejs21.PathApi.next(_blockPath),
|
|
1496
|
-
select: true
|
|
1497
|
-
}
|
|
1498
|
-
);
|
|
1499
|
-
editor.setOption(AIChatPlugin, "_blockPath", newEndBlockPath);
|
|
1500
|
-
const endBlock = editor.api.node(newEndBlockPath)[0];
|
|
1501
|
-
const serializedBlock = streamSerializeMd(
|
|
1502
|
-
editor,
|
|
1503
|
-
{
|
|
1504
|
-
value: [endBlock]
|
|
1505
|
-
},
|
|
1506
|
-
tempBlockChunks
|
|
1507
|
-
);
|
|
1508
|
-
editor.setOption(AIChatPlugin, "_blockChunks", serializedBlock);
|
|
1509
|
-
}
|
|
1510
|
-
}
|
|
1511
|
-
}
|
|
937
|
+
const { _blockChunks, _blockPath } = editor.getOptions(AIChatPlugin);
|
|
938
|
+
if (_blockPath === null) {
|
|
939
|
+
const blocks = streamDeserializeMd(editor, chunk);
|
|
940
|
+
const path = getCurrentBlockPath(editor);
|
|
941
|
+
const startBlock = editor.api.node(path)[0];
|
|
942
|
+
const startInEmptyParagraph = NodeApi.string(startBlock).length === 0 && startBlock.type === getPluginType(editor, KEYS.p);
|
|
943
|
+
if (startInEmptyParagraph) editor.tf.removeNodes({ at: path });
|
|
944
|
+
if (blocks.length > 0) {
|
|
945
|
+
editor.tf.insertNodes(nodesWithProps(editor, [blocks[0]], options), {
|
|
946
|
+
at: path,
|
|
947
|
+
nextBlock: !startInEmptyParagraph,
|
|
948
|
+
select: true
|
|
949
|
+
});
|
|
950
|
+
editor.setOption(AIChatPlugin, "_blockPath", getCurrentBlockPath(editor));
|
|
951
|
+
editor.setOption(AIChatPlugin, "_blockChunks", chunk);
|
|
952
|
+
if (blocks.length > 1) {
|
|
953
|
+
const nextBlocks = blocks.slice(1);
|
|
954
|
+
const nextPath = getCurrentBlockPath(editor);
|
|
955
|
+
editor.tf.insertNodes(nodesWithProps(editor, nextBlocks, options), {
|
|
956
|
+
at: nextPath,
|
|
957
|
+
nextBlock: true,
|
|
958
|
+
select: true
|
|
959
|
+
});
|
|
960
|
+
const lastBlock = editor.api.node(getNextPath(nextPath, nextBlocks.length));
|
|
961
|
+
editor.setOption(AIChatPlugin, "_blockPath", lastBlock[1]);
|
|
962
|
+
const lastBlockChunks = streamSerializeMd(editor, { value: [lastBlock[0]] }, chunk);
|
|
963
|
+
editor.setOption(AIChatPlugin, "_blockChunks", lastBlockChunks);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
} else {
|
|
967
|
+
const tempBlockChunks = _blockChunks + chunk;
|
|
968
|
+
const tempBlocks = streamDeserializeMd(editor, tempBlockChunks);
|
|
969
|
+
if (tempBlocks.length === 0) return console.warn(`unsupport md nodes: ${JSON.stringify(tempBlockChunks)}`);
|
|
970
|
+
if (tempBlocks.length === 1) {
|
|
971
|
+
const currentBlock = editor.api.node(_blockPath)[0];
|
|
972
|
+
if (isSameNode(editor, currentBlock, tempBlocks[0])) {
|
|
973
|
+
const chunkNodes = streamDeserializeInlineMd(editor, chunk);
|
|
974
|
+
editor.tf.insertNodes(nodesWithProps(editor, chunkNodes, options), {
|
|
975
|
+
at: editor.api.end(_blockPath),
|
|
976
|
+
select: true
|
|
977
|
+
});
|
|
978
|
+
const serializedBlock = streamSerializeMd(editor, { value: [editor.api.node(_blockPath)[0]] }, tempBlockChunks);
|
|
979
|
+
const blockText = NodeApi.string(tempBlocks[0]);
|
|
980
|
+
if (serializedBlock === tempBlockChunks && blockText === serializedBlock) editor.setOption(AIChatPlugin, "_blockChunks", tempBlockChunks);
|
|
981
|
+
else {
|
|
982
|
+
editor.tf.replaceNodes(nodesWithProps(editor, [tempBlocks[0]], options), {
|
|
983
|
+
at: _blockPath,
|
|
984
|
+
select: true
|
|
985
|
+
});
|
|
986
|
+
const serializedBlock$1 = streamSerializeMd(editor, { value: [tempBlocks[0]] }, tempBlockChunks);
|
|
987
|
+
editor.setOption(AIChatPlugin, "_blockChunks", tempBlocks[0].type === getPluginType(editor, KEYS.codeBlock) || tempBlocks[0].type === getPluginType(editor, KEYS.table) || tempBlocks[0].type === getPluginType(editor, KEYS.equation) ? tempBlockChunks : serializedBlock$1);
|
|
988
|
+
}
|
|
989
|
+
} else {
|
|
990
|
+
const serializedBlock = streamSerializeMd(editor, { value: [tempBlocks[0]] }, tempBlockChunks);
|
|
991
|
+
editor.tf.replaceNodes(nodesWithProps(editor, [tempBlocks[0]], options), {
|
|
992
|
+
at: _blockPath,
|
|
993
|
+
select: true
|
|
994
|
+
});
|
|
995
|
+
editor.setOption(AIChatPlugin, "_blockChunks", serializedBlock);
|
|
996
|
+
}
|
|
997
|
+
} else {
|
|
998
|
+
editor.tf.replaceNodes(nodesWithProps(editor, [tempBlocks[0]], options), {
|
|
999
|
+
at: _blockPath,
|
|
1000
|
+
select: true
|
|
1001
|
+
});
|
|
1002
|
+
if (tempBlocks.length > 1) {
|
|
1003
|
+
const newEndBlockPath = getNextPath(_blockPath, tempBlocks.length - 1);
|
|
1004
|
+
editor.tf.insertNodes(nodesWithProps(editor, tempBlocks.slice(1), options), {
|
|
1005
|
+
at: PathApi.next(_blockPath),
|
|
1006
|
+
select: true
|
|
1007
|
+
});
|
|
1008
|
+
editor.setOption(AIChatPlugin, "_blockPath", newEndBlockPath);
|
|
1009
|
+
const endBlock = editor.api.node(newEndBlockPath)[0];
|
|
1010
|
+
const serializedBlock = streamSerializeMd(editor, { value: [endBlock] }, tempBlockChunks);
|
|
1011
|
+
editor.setOption(AIChatPlugin, "_blockChunks", serializedBlock);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1512
1015
|
}
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
if (entry && (entry[0].type === (0, import_platejs21.getPluginType)(editor, import_platejs21.KEYS.columnGroup) || entry[0].type === (0, import_platejs21.getPluginType)(editor, import_platejs21.KEYS.table))) {
|
|
1524
|
-
return editor.api.above()?.[1] ?? path;
|
|
1525
|
-
}
|
|
1526
|
-
return path;
|
|
1016
|
+
const getCurrentBlockPath = (editor) => {
|
|
1017
|
+
const getAnchorPreviousPath = (editor$1) => {
|
|
1018
|
+
const anchorNode = editor$1.getApi(AIChatPlugin).aiChat.node({ anchor: true });
|
|
1019
|
+
if (anchorNode) return PathApi.previous(anchorNode[1]);
|
|
1020
|
+
};
|
|
1021
|
+
const getFocusPath = (editor$1) => editor$1.selection?.focus.path.slice(0, 1);
|
|
1022
|
+
const path = getAnchorPreviousPath(editor) ?? getFocusPath(editor) ?? [0];
|
|
1023
|
+
const entry = editor.api.node(path);
|
|
1024
|
+
if (entry && (entry[0].type === getPluginType(editor, KEYS.columnGroup) || entry[0].type === getPluginType(editor, KEYS.table))) return editor.api.above()?.[1] ?? path;
|
|
1025
|
+
return path;
|
|
1527
1026
|
};
|
|
1528
1027
|
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
// src/react/copilot/renderCopilotBelowNodes.tsx
|
|
1536
|
-
var import_react21 = __toESM(require("react"));
|
|
1537
|
-
var import_platejs22 = require("platejs");
|
|
1538
|
-
var import_react22 = require("platejs/react");
|
|
1539
|
-
var renderCopilotBelowNodes = ({
|
|
1540
|
-
editor
|
|
1541
|
-
}) => {
|
|
1542
|
-
const copilot = (0, import_react22.getEditorPlugin)(editor, {
|
|
1543
|
-
key: import_platejs22.KEYS.copilot
|
|
1544
|
-
});
|
|
1545
|
-
const { renderGhostText: GhostText } = copilot.getOptions();
|
|
1546
|
-
if (!GhostText) return;
|
|
1547
|
-
return ({ children }) => /* @__PURE__ */ import_react21.default.createElement(import_react21.default.Fragment, null, children, /* @__PURE__ */ import_react21.default.createElement(GhostText, null));
|
|
1028
|
+
//#endregion
|
|
1029
|
+
//#region src/react/copilot/renderCopilotBelowNodes.tsx
|
|
1030
|
+
const renderCopilotBelowNodes = ({ editor }) => {
|
|
1031
|
+
const { renderGhostText: GhostText } = getEditorPlugin(editor, { key: KEYS.copilot }).getOptions();
|
|
1032
|
+
if (!GhostText) return;
|
|
1033
|
+
return ({ children }) => /* @__PURE__ */ React.createElement(React.Fragment, null, children, /* @__PURE__ */ React.createElement(GhostText, null));
|
|
1548
1034
|
};
|
|
1549
1035
|
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
});
|
|
1557
|
-
if (!suggestionText?.length) return false;
|
|
1558
|
-
editor.tf.insertFragment((0, import_markdown7.deserializeInlineMd)(editor, suggestionText));
|
|
1036
|
+
//#endregion
|
|
1037
|
+
//#region src/react/copilot/transforms/acceptCopilot.ts
|
|
1038
|
+
const acceptCopilot = (editor) => {
|
|
1039
|
+
const { suggestionText } = editor.getOptions({ key: KEYS.copilot });
|
|
1040
|
+
if (!suggestionText?.length) return false;
|
|
1041
|
+
editor.tf.insertFragment(deserializeInlineMd(editor, suggestionText));
|
|
1559
1042
|
};
|
|
1560
1043
|
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
}).catch((error) => {
|
|
1606
|
-
throw error;
|
|
1607
|
-
});
|
|
1608
|
-
if (onResponse) {
|
|
1609
|
-
await onResponse(res);
|
|
1610
|
-
}
|
|
1611
|
-
if (!res.ok) {
|
|
1612
|
-
throw new Error(
|
|
1613
|
-
await res.text() || "Failed to fetch the chat response."
|
|
1614
|
-
);
|
|
1615
|
-
}
|
|
1616
|
-
if (!res.body) {
|
|
1617
|
-
throw new Error("The response body is empty.");
|
|
1618
|
-
}
|
|
1619
|
-
const { text } = await res.json();
|
|
1620
|
-
if (!text) {
|
|
1621
|
-
throw new Error("The response does not contain a text field.");
|
|
1622
|
-
}
|
|
1623
|
-
setCompletion(text);
|
|
1624
|
-
if (onFinish) {
|
|
1625
|
-
onFinish(prompt, text);
|
|
1626
|
-
}
|
|
1627
|
-
setAbortController(null);
|
|
1628
|
-
return text;
|
|
1629
|
-
} catch (error) {
|
|
1630
|
-
if (error.name === "AbortError") {
|
|
1631
|
-
setAbortController(null);
|
|
1632
|
-
return null;
|
|
1633
|
-
}
|
|
1634
|
-
if (error instanceof Error && onError) {
|
|
1635
|
-
onError(error);
|
|
1636
|
-
}
|
|
1637
|
-
setError(error);
|
|
1638
|
-
} finally {
|
|
1639
|
-
setLoading(false);
|
|
1640
|
-
}
|
|
1044
|
+
//#endregion
|
|
1045
|
+
//#region src/react/copilot/utils/callCompletionApi.ts
|
|
1046
|
+
const getOriginalFetch = () => fetch;
|
|
1047
|
+
async function callCompletionApi({ api = "/api/completion", body, credentials, fetch: fetch$1 = getOriginalFetch(), headers, prompt, setAbortController = () => {}, setCompletion = () => {}, setError = () => {}, setLoading = () => {}, onError, onFinish, onResponse }) {
|
|
1048
|
+
try {
|
|
1049
|
+
setLoading(true);
|
|
1050
|
+
setError(null);
|
|
1051
|
+
const abortController = new AbortController();
|
|
1052
|
+
setAbortController(abortController);
|
|
1053
|
+
setCompletion("");
|
|
1054
|
+
const res = await fetch$1(api, {
|
|
1055
|
+
body: JSON.stringify({
|
|
1056
|
+
prompt,
|
|
1057
|
+
...body
|
|
1058
|
+
}),
|
|
1059
|
+
credentials,
|
|
1060
|
+
headers: {
|
|
1061
|
+
"Content-Type": "application/json",
|
|
1062
|
+
...headers
|
|
1063
|
+
},
|
|
1064
|
+
method: "POST",
|
|
1065
|
+
signal: abortController.signal
|
|
1066
|
+
}).catch((error) => {
|
|
1067
|
+
throw error;
|
|
1068
|
+
});
|
|
1069
|
+
if (onResponse) await onResponse(res);
|
|
1070
|
+
if (!res.ok) throw new Error(await res.text() || "Failed to fetch the chat response.");
|
|
1071
|
+
if (!res.body) throw new Error("The response body is empty.");
|
|
1072
|
+
const { text } = await res.json();
|
|
1073
|
+
if (!text) throw new Error("The response does not contain a text field.");
|
|
1074
|
+
setCompletion(text);
|
|
1075
|
+
if (onFinish) onFinish(prompt, text);
|
|
1076
|
+
setAbortController(null);
|
|
1077
|
+
return text;
|
|
1078
|
+
} catch (error) {
|
|
1079
|
+
if (error.name === "AbortError") {
|
|
1080
|
+
setAbortController(null);
|
|
1081
|
+
return null;
|
|
1082
|
+
}
|
|
1083
|
+
if (error instanceof Error && onError) onError(error);
|
|
1084
|
+
setError(error);
|
|
1085
|
+
} finally {
|
|
1086
|
+
setLoading(false);
|
|
1087
|
+
}
|
|
1641
1088
|
}
|
|
1642
1089
|
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1090
|
+
//#endregion
|
|
1091
|
+
//#region src/react/copilot/utils/getNextWord.ts
|
|
1092
|
+
const nonSpaceRegex = /^\s*(\S)/;
|
|
1093
|
+
const cjkCharRegex = /[\u1100-\u11FF\u3040-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF]/;
|
|
1094
|
+
const cjkMatchRegex = /^(\s*)([\u1100-\u11FF\u3040-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF])([\u3000-\u303F\uFF00-\uFFEF])?/;
|
|
1095
|
+
const nonCjkMatchRegex = /^(\s*\S+?)(?=[\s\u1100-\u11FF\u3040-\u30FF\u3400-\u4DBF\u4E00-\u9FFF\uAC00-\uD7AF\uF900-\uFAFF]|$)/;
|
|
1096
|
+
const getNextWord = ({ text }) => {
|
|
1097
|
+
if (!text) return {
|
|
1098
|
+
firstWord: "",
|
|
1099
|
+
remainingText: ""
|
|
1100
|
+
};
|
|
1101
|
+
const nonSpaceMatch = nonSpaceRegex.exec(text);
|
|
1102
|
+
if (!nonSpaceMatch) return {
|
|
1103
|
+
firstWord: "",
|
|
1104
|
+
remainingText: ""
|
|
1105
|
+
};
|
|
1106
|
+
const firstNonSpaceChar = nonSpaceMatch[1];
|
|
1107
|
+
const isCJKChar = cjkCharRegex.test(firstNonSpaceChar);
|
|
1108
|
+
let firstWord;
|
|
1109
|
+
let remainingText;
|
|
1110
|
+
if (isCJKChar) {
|
|
1111
|
+
const match = cjkMatchRegex.exec(text);
|
|
1112
|
+
if (match) {
|
|
1113
|
+
const [, spaces = "", char = "", punctuation = ""] = match;
|
|
1114
|
+
firstWord = spaces + char + punctuation;
|
|
1115
|
+
remainingText = text.slice(firstWord.length);
|
|
1116
|
+
} else {
|
|
1117
|
+
firstWord = "";
|
|
1118
|
+
remainingText = text;
|
|
1119
|
+
}
|
|
1120
|
+
} else {
|
|
1121
|
+
const match = nonCjkMatchRegex.exec(text);
|
|
1122
|
+
if (match) {
|
|
1123
|
+
firstWord = match[0];
|
|
1124
|
+
remainingText = text.slice(firstWord.length);
|
|
1125
|
+
} else {
|
|
1126
|
+
firstWord = text;
|
|
1127
|
+
remainingText = "";
|
|
1128
|
+
}
|
|
1129
|
+
}
|
|
1130
|
+
return {
|
|
1131
|
+
firstWord,
|
|
1132
|
+
remainingText
|
|
1133
|
+
};
|
|
1677
1134
|
};
|
|
1678
1135
|
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1696
|
-
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
setCompletion: (completion) => setOption("completion", completion),
|
|
1705
|
-
setError: (error) => setOption("error", error),
|
|
1706
|
-
setLoading: (loading) => setOption("isLoading", loading),
|
|
1707
|
-
onError: (error) => {
|
|
1708
|
-
setOption("error", error);
|
|
1709
|
-
completeOptions?.onError?.(error);
|
|
1710
|
-
}
|
|
1711
|
-
});
|
|
1136
|
+
//#endregion
|
|
1137
|
+
//#region src/react/copilot/utils/triggerCopilotSuggestion.ts
|
|
1138
|
+
const triggerCopilotSuggestion = async (editor) => {
|
|
1139
|
+
const { api, getOptions, setOption } = getEditorPlugin(editor, { key: KEYS.copilot });
|
|
1140
|
+
const { completeOptions, getPrompt, isLoading, triggerQuery } = getOptions();
|
|
1141
|
+
if (isLoading || editor.getOptions({ key: KEYS.aiChat }).chat?.isLoading) return false;
|
|
1142
|
+
if (!triggerQuery({ editor })) return false;
|
|
1143
|
+
const prompt = getPrompt({ editor });
|
|
1144
|
+
if (prompt.length === 0) return false;
|
|
1145
|
+
api.copilot.stop();
|
|
1146
|
+
await callCompletionApi({
|
|
1147
|
+
prompt,
|
|
1148
|
+
onFinish: (_, completion) => {
|
|
1149
|
+
api.copilot.setBlockSuggestion({ text: completion });
|
|
1150
|
+
},
|
|
1151
|
+
...completeOptions,
|
|
1152
|
+
setAbortController: (controller) => setOption("abortController", controller),
|
|
1153
|
+
setCompletion: (completion) => setOption("completion", completion),
|
|
1154
|
+
setError: (error) => setOption("error", error),
|
|
1155
|
+
setLoading: (loading) => setOption("isLoading", loading),
|
|
1156
|
+
onError: (error) => {
|
|
1157
|
+
setOption("error", error);
|
|
1158
|
+
completeOptions?.onError?.(error);
|
|
1159
|
+
}
|
|
1160
|
+
});
|
|
1712
1161
|
};
|
|
1713
1162
|
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1163
|
+
//#endregion
|
|
1164
|
+
//#region src/react/copilot/utils/withoutAbort.ts
|
|
1165
|
+
const withoutAbort = (editor, fn) => {
|
|
1166
|
+
editor.setOption(CopilotPlugin, "shouldAbort", false);
|
|
1167
|
+
fn();
|
|
1168
|
+
editor.setOption(CopilotPlugin, "shouldAbort", true);
|
|
1719
1169
|
};
|
|
1720
1170
|
|
|
1721
|
-
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
|
|
1729
|
-
|
|
1730
|
-
|
|
1731
|
-
|
|
1732
|
-
text: remainingText
|
|
1733
|
-
});
|
|
1734
|
-
withoutAbort(editor, () => {
|
|
1735
|
-
editor.tf.insertFragment((0, import_markdown8.deserializeInlineMd)(editor, firstWord));
|
|
1736
|
-
});
|
|
1171
|
+
//#endregion
|
|
1172
|
+
//#region src/react/copilot/transforms/acceptCopilotNextWord.ts
|
|
1173
|
+
const acceptCopilotNextWord = (editor) => {
|
|
1174
|
+
const { api, getOptions } = getEditorPlugin(editor, { key: KEYS.copilot });
|
|
1175
|
+
const { getNextWord: getNextWord$1, suggestionText } = getOptions();
|
|
1176
|
+
if (!suggestionText?.length) return false;
|
|
1177
|
+
const { firstWord, remainingText } = getNextWord$1({ text: suggestionText });
|
|
1178
|
+
api.copilot.setBlockSuggestion({ text: remainingText });
|
|
1179
|
+
withoutAbort(editor, () => {
|
|
1180
|
+
editor.tf.insertFragment(deserializeInlineMd(editor, firstWord));
|
|
1181
|
+
});
|
|
1737
1182
|
};
|
|
1738
1183
|
|
|
1739
|
-
|
|
1740
|
-
|
|
1741
|
-
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
} else if (operation.type === "insert_text") {
|
|
1750
|
-
string += operation.text;
|
|
1751
|
-
}
|
|
1752
|
-
}
|
|
1753
|
-
return string;
|
|
1184
|
+
//#endregion
|
|
1185
|
+
//#region src/react/copilot/withCopilot.ts
|
|
1186
|
+
const getPatchString = (editor, operations) => {
|
|
1187
|
+
let string = "";
|
|
1188
|
+
for (const operation of operations) if (operation.type === "insert_node") {
|
|
1189
|
+
const node = operation.node;
|
|
1190
|
+
const text = serializeInlineMd(editor, { value: [node] });
|
|
1191
|
+
string += text;
|
|
1192
|
+
} else if (operation.type === "insert_text") string += operation.text;
|
|
1193
|
+
return string;
|
|
1754
1194
|
};
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
|
|
1814
|
-
const shouldInsertText = getPatchString(
|
|
1815
|
-
editor,
|
|
1816
|
-
lastUndos.operations
|
|
1817
|
-
);
|
|
1818
|
-
const newText = shouldInsertText + oldText;
|
|
1819
|
-
setOption("suggestionText", newText);
|
|
1820
|
-
undo();
|
|
1821
|
-
});
|
|
1822
|
-
return;
|
|
1823
|
-
}
|
|
1824
|
-
return undo();
|
|
1825
|
-
},
|
|
1826
|
-
writeHistory(stacks, batch) {
|
|
1827
|
-
if (!getOptions().isLoading) {
|
|
1828
|
-
batch.shouldAbort = getOptions().shouldAbort;
|
|
1829
|
-
}
|
|
1830
|
-
return writeHistory(stacks, batch);
|
|
1831
|
-
}
|
|
1832
|
-
}
|
|
1833
|
-
};
|
|
1195
|
+
const withCopilot = ({ api, editor, getOptions, setOption, tf: { apply, insertText, redo, setSelection, undo, writeHistory } }) => {
|
|
1196
|
+
let prevSelection = null;
|
|
1197
|
+
return { transforms: {
|
|
1198
|
+
apply(operation) {
|
|
1199
|
+
const { shouldAbort } = getOptions();
|
|
1200
|
+
if (shouldAbort) api.copilot.reject();
|
|
1201
|
+
apply(operation);
|
|
1202
|
+
},
|
|
1203
|
+
insertText(text, options) {
|
|
1204
|
+
const suggestionText = getOptions().suggestionText;
|
|
1205
|
+
if (suggestionText?.startsWith(text)) {
|
|
1206
|
+
withoutAbort(editor, () => {
|
|
1207
|
+
editor.tf.withoutMerging(() => {
|
|
1208
|
+
const newText = suggestionText?.slice(text.length);
|
|
1209
|
+
setOption("suggestionText", newText);
|
|
1210
|
+
insertText(text);
|
|
1211
|
+
});
|
|
1212
|
+
});
|
|
1213
|
+
return;
|
|
1214
|
+
}
|
|
1215
|
+
insertText(text, options);
|
|
1216
|
+
},
|
|
1217
|
+
redo() {
|
|
1218
|
+
if (!getOptions().suggestionText) return redo();
|
|
1219
|
+
const topRedo = editor.history.redos.at(-1);
|
|
1220
|
+
const prevSuggestion = getOptions().suggestionText;
|
|
1221
|
+
if (topRedo && topRedo.shouldAbort === false && prevSuggestion) {
|
|
1222
|
+
withoutAbort(editor, () => {
|
|
1223
|
+
const shouldRemoveText = getPatchString(editor, topRedo.operations);
|
|
1224
|
+
setOption("suggestionText", prevSuggestion.slice(shouldRemoveText.length));
|
|
1225
|
+
redo();
|
|
1226
|
+
});
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
return redo();
|
|
1230
|
+
},
|
|
1231
|
+
setSelection(props) {
|
|
1232
|
+
setSelection(props);
|
|
1233
|
+
if (editor.selection && (!prevSelection || !RangeApi.equals(prevSelection, editor.selection)) && getOptions().autoTriggerQuery({ editor }) && editor.api.isFocused()) api.copilot.triggerSuggestion();
|
|
1234
|
+
prevSelection = editor.selection;
|
|
1235
|
+
},
|
|
1236
|
+
undo() {
|
|
1237
|
+
if (!getOptions().suggestionText) return undo();
|
|
1238
|
+
const lastUndos = editor.history.undos.at(-1);
|
|
1239
|
+
const oldText = getOptions().suggestionText;
|
|
1240
|
+
if (lastUndos && lastUndos.shouldAbort === false && oldText) {
|
|
1241
|
+
withoutAbort(editor, () => {
|
|
1242
|
+
setOption("suggestionText", getPatchString(editor, lastUndos.operations) + oldText);
|
|
1243
|
+
undo();
|
|
1244
|
+
});
|
|
1245
|
+
return;
|
|
1246
|
+
}
|
|
1247
|
+
return undo();
|
|
1248
|
+
},
|
|
1249
|
+
writeHistory(stacks, batch) {
|
|
1250
|
+
if (!getOptions().isLoading) batch.shouldAbort = getOptions().shouldAbort;
|
|
1251
|
+
return writeHistory(stacks, batch);
|
|
1252
|
+
}
|
|
1253
|
+
} };
|
|
1834
1254
|
};
|
|
1835
1255
|
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
}
|
|
1918
|
-
};
|
|
1919
|
-
}
|
|
1920
|
-
).extendApi(({ api, getOptions, setOptions }) => ({
|
|
1921
|
-
reject: () => {
|
|
1922
|
-
if (!getOptions().suggestionText?.length) return false;
|
|
1923
|
-
api.copilot.stop();
|
|
1924
|
-
setOptions({
|
|
1925
|
-
completion: null,
|
|
1926
|
-
suggestionNodeId: null,
|
|
1927
|
-
suggestionText: null
|
|
1928
|
-
});
|
|
1929
|
-
}
|
|
1930
|
-
})).extend({
|
|
1931
|
-
render: {
|
|
1932
|
-
belowNodes: renderCopilotBelowNodes
|
|
1933
|
-
},
|
|
1934
|
-
shortcuts: {
|
|
1935
|
-
accept: {
|
|
1936
|
-
keys: "tab"
|
|
1937
|
-
},
|
|
1938
|
-
reject: {
|
|
1939
|
-
keys: "escape"
|
|
1940
|
-
}
|
|
1941
|
-
}
|
|
1256
|
+
//#endregion
|
|
1257
|
+
//#region src/react/copilot/CopilotPlugin.tsx
|
|
1258
|
+
const CopilotPlugin = createTPlatePlugin({
|
|
1259
|
+
key: KEYS.copilot,
|
|
1260
|
+
handlers: {
|
|
1261
|
+
onBlur: ({ api }) => {
|
|
1262
|
+
api.copilot.reject();
|
|
1263
|
+
},
|
|
1264
|
+
onMouseDown: ({ api }) => {
|
|
1265
|
+
api.copilot.reject();
|
|
1266
|
+
}
|
|
1267
|
+
},
|
|
1268
|
+
options: {
|
|
1269
|
+
abortController: null,
|
|
1270
|
+
completeOptions: {},
|
|
1271
|
+
completion: "",
|
|
1272
|
+
debounceDelay: 0,
|
|
1273
|
+
error: null,
|
|
1274
|
+
getNextWord,
|
|
1275
|
+
isLoading: false,
|
|
1276
|
+
renderGhostText: null,
|
|
1277
|
+
shouldAbort: true,
|
|
1278
|
+
suggestionNodeId: null,
|
|
1279
|
+
suggestionText: null,
|
|
1280
|
+
autoTriggerQuery: ({ editor }) => {
|
|
1281
|
+
if (editor.getOptions({ key: KEYS.copilot }).suggestionText) return false;
|
|
1282
|
+
if (editor.api.isEmpty(editor.selection, { block: true })) return false;
|
|
1283
|
+
const blockAbove = editor.api.block();
|
|
1284
|
+
if (!blockAbove) return false;
|
|
1285
|
+
return NodeApi.string(blockAbove[0]).at(-1) === " ";
|
|
1286
|
+
},
|
|
1287
|
+
getPrompt: ({ editor }) => {
|
|
1288
|
+
const contextEntry = editor.api.block({ highest: true });
|
|
1289
|
+
if (!contextEntry) return "";
|
|
1290
|
+
return serializeMd(editor, { value: [contextEntry[0]] });
|
|
1291
|
+
},
|
|
1292
|
+
triggerQuery: ({ editor }) => {
|
|
1293
|
+
if (editor.api.isExpanded()) return false;
|
|
1294
|
+
if (!editor.api.isAt({ end: true })) return false;
|
|
1295
|
+
return true;
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
}).overrideEditor(withCopilot).extendSelectors(({ getOptions }) => ({ isSuggested: (id) => getOptions().suggestionNodeId === id })).extendTransforms(({ editor }) => ({
|
|
1299
|
+
accept: bindFirst(acceptCopilot, editor),
|
|
1300
|
+
acceptNextWord: bindFirst(acceptCopilotNextWord, editor)
|
|
1301
|
+
})).extendApi(({ api, editor, getOptions, setOption, setOptions }) => {
|
|
1302
|
+
const debounceDelay = getOptions().debounceDelay;
|
|
1303
|
+
let triggerSuggestion = bindFirst(triggerCopilotSuggestion, editor);
|
|
1304
|
+
if (debounceDelay) triggerSuggestion = debounce(bindFirst(triggerCopilotSuggestion, editor), debounceDelay);
|
|
1305
|
+
return {
|
|
1306
|
+
triggerSuggestion,
|
|
1307
|
+
setBlockSuggestion: ({ id = getOptions().suggestionNodeId, text }) => {
|
|
1308
|
+
if (!id) id = editor.api.block()[0].id;
|
|
1309
|
+
setOptions({
|
|
1310
|
+
suggestionNodeId: id,
|
|
1311
|
+
suggestionText: text
|
|
1312
|
+
});
|
|
1313
|
+
},
|
|
1314
|
+
stop: () => {
|
|
1315
|
+
const { abortController } = getOptions();
|
|
1316
|
+
api.copilot.triggerSuggestion?.cancel();
|
|
1317
|
+
if (abortController) {
|
|
1318
|
+
abortController.abort();
|
|
1319
|
+
setOption("abortController", null);
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
};
|
|
1323
|
+
}).extendApi(({ api, getOptions, setOptions }) => ({ reject: () => {
|
|
1324
|
+
if (!getOptions().suggestionText?.length) return false;
|
|
1325
|
+
api.copilot.stop();
|
|
1326
|
+
setOptions({
|
|
1327
|
+
completion: null,
|
|
1328
|
+
suggestionNodeId: null,
|
|
1329
|
+
suggestionText: null
|
|
1330
|
+
});
|
|
1331
|
+
} })).extend({
|
|
1332
|
+
render: { belowNodes: renderCopilotBelowNodes },
|
|
1333
|
+
shortcuts: {
|
|
1334
|
+
accept: { keys: "tab" },
|
|
1335
|
+
reject: { keys: "escape" }
|
|
1336
|
+
}
|
|
1942
1337
|
});
|
|
1943
|
-
|
|
1944
|
-
|
|
1945
|
-
|
|
1946
|
-
AIPlugin,
|
|
1947
|
-
CopilotPlugin,
|
|
1948
|
-
acceptAIChat,
|
|
1949
|
-
acceptAISuggestions,
|
|
1950
|
-
acceptCopilot,
|
|
1951
|
-
acceptCopilotNextWord,
|
|
1952
|
-
aiCommentToRange,
|
|
1953
|
-
applyAISuggestions,
|
|
1954
|
-
callCompletionApi,
|
|
1955
|
-
createFormattedBlocks,
|
|
1956
|
-
escapeInput,
|
|
1957
|
-
findTextRangeInBlock,
|
|
1958
|
-
getChunkTrimmed,
|
|
1959
|
-
getCurrentBlockPath,
|
|
1960
|
-
getLastAssistantMessage,
|
|
1961
|
-
getListNode,
|
|
1962
|
-
getNextWord,
|
|
1963
|
-
insertBelowAIChat,
|
|
1964
|
-
insertBelowGenerate,
|
|
1965
|
-
isCompleteCodeBlock,
|
|
1966
|
-
isCompleteMath,
|
|
1967
|
-
isSameNode,
|
|
1968
|
-
nodesWithProps,
|
|
1969
|
-
rejectAISuggestions,
|
|
1970
|
-
removeAnchorAIChat,
|
|
1971
|
-
renderCopilotBelowNodes,
|
|
1972
|
-
replaceSelectionAIChat,
|
|
1973
|
-
resetAIChat,
|
|
1974
|
-
streamDeserializeInlineMd,
|
|
1975
|
-
streamDeserializeMd,
|
|
1976
|
-
streamInsertChunk,
|
|
1977
|
-
streamSerializeMd,
|
|
1978
|
-
submitAIChat,
|
|
1979
|
-
triggerCopilotSuggestion,
|
|
1980
|
-
useAIChatEditor,
|
|
1981
|
-
useChatChunk,
|
|
1982
|
-
useEditorChat,
|
|
1983
|
-
useLastAssistantMessage,
|
|
1984
|
-
withAIChat,
|
|
1985
|
-
withCopilot,
|
|
1986
|
-
withoutAbort
|
|
1987
|
-
});
|
|
1988
|
-
//# sourceMappingURL=index.js.map
|
|
1338
|
+
|
|
1339
|
+
//#endregion
|
|
1340
|
+
export { AIChatPlugin, AIPlugin, CopilotPlugin, acceptAIChat, acceptAISuggestions, acceptCopilot, acceptCopilotNextWord, aiCommentToRange, applyAISuggestions, callCompletionApi, createFormattedBlocks, escapeInput, findTextRangeInBlock, getChunkTrimmed, getCurrentBlockPath, getLastAssistantMessage, getListNode, getNextWord, insertBelowAIChat, insertBelowGenerate, isCompleteCodeBlock, isCompleteMath, isSameNode, nodesWithProps, rejectAISuggestions, removeAnchorAIChat, renderCopilotBelowNodes, replaceSelectionAIChat, resetAIChat, streamDeserializeInlineMd, streamDeserializeMd, streamInsertChunk, streamSerializeMd, submitAIChat, triggerCopilotSuggestion, useAIChatEditor, useChatChunk, useEditorChat, useLastAssistantMessage, withAIChat, withCopilot, withoutAbort };
|