@liveblocks/react-tiptap 2.16.1-ai2 → 2.16.1-ai4
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/LiveblocksExtension.js +7 -11
- package/dist/LiveblocksExtension.js.map +1 -1
- package/dist/LiveblocksExtension.mjs +7 -11
- package/dist/LiveblocksExtension.mjs.map +1 -1
- package/dist/ai/AiExtension.js +186 -109
- package/dist/ai/AiExtension.js.map +1 -1
- package/dist/ai/AiExtension.mjs +187 -111
- package/dist/ai/AiExtension.mjs.map +1 -1
- package/dist/ai/AiToolbar.js +182 -89
- package/dist/ai/AiToolbar.js.map +1 -1
- package/dist/ai/AiToolbar.mjs +186 -93
- package/dist/ai/AiToolbar.mjs.map +1 -1
- package/dist/comments/FloatingComposer.js +2 -2
- package/dist/comments/FloatingComposer.js.map +1 -1
- package/dist/comments/FloatingComposer.mjs +2 -2
- package/dist/comments/FloatingComposer.mjs.map +1 -1
- package/dist/index.d.mts +57 -16
- package/dist/index.d.ts +57 -16
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/toolbar/Toolbar.js +1 -1
- package/dist/toolbar/Toolbar.js.map +1 -1
- package/dist/toolbar/Toolbar.mjs +1 -1
- package/dist/toolbar/Toolbar.mjs.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils.js +98 -6
- package/dist/utils.js.map +1 -1
- package/dist/utils.mjs +98 -7
- package/dist/utils.mjs.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.mjs +1 -1
- package/package.json +6 -6
- package/src/styles/index.css +115 -59
- package/src/styles/utils.css +6 -0
- package/styles.css +1 -1
- package/styles.css.map +1 -1
package/dist/ai/AiExtension.js
CHANGED
|
@@ -8,6 +8,7 @@ var view = require('@tiptap/pm/view');
|
|
|
8
8
|
var yProsemirror = require('y-prosemirror');
|
|
9
9
|
var yjs = require('yjs');
|
|
10
10
|
var types = require('../types.js');
|
|
11
|
+
var utils = require('../utils.js');
|
|
11
12
|
|
|
12
13
|
const DEFAULT_AI_NAME = "AI";
|
|
13
14
|
const DEFAULT_STATE = { phase: "closed" };
|
|
@@ -19,13 +20,52 @@ function getYjsBinding(editor) {
|
|
|
19
20
|
function getLiveblocksYjsProvider(editor) {
|
|
20
21
|
return editor.extensionStorage.liveblocksExtension?.provider;
|
|
21
22
|
}
|
|
23
|
+
function isContextualPromptDiffResponse(response) {
|
|
24
|
+
return response.type === "replace" || response.type === "insert";
|
|
25
|
+
}
|
|
26
|
+
function isResolveContextualPromptResponse(response) {
|
|
27
|
+
return typeof response === "object" && response !== null && typeof response.text === "string" && typeof response.type === "string" && ["insert", "replace", "other"].includes(response.type);
|
|
28
|
+
}
|
|
29
|
+
function getRevertTransaction(tr, editor, storage, doc) {
|
|
30
|
+
if (storage.snapshot) {
|
|
31
|
+
const binding = getYjsBinding(editor);
|
|
32
|
+
if (binding) {
|
|
33
|
+
binding.mapping.clear();
|
|
34
|
+
const docFromSnapshot = yjs.createDocFromSnapshot(
|
|
35
|
+
binding.doc,
|
|
36
|
+
storage.snapshot
|
|
37
|
+
);
|
|
38
|
+
const type = docFromSnapshot.getXmlFragment("default");
|
|
39
|
+
const fragmentContent = yProsemirror.yXmlFragmentToProseMirrorFragment(
|
|
40
|
+
type,
|
|
41
|
+
editor.state.schema
|
|
42
|
+
);
|
|
43
|
+
tr.setMeta("addToHistory", false);
|
|
44
|
+
tr.replace(
|
|
45
|
+
0,
|
|
46
|
+
editor.state.doc.content.size,
|
|
47
|
+
new model.Slice(model.Fragment.from(fragmentContent), 0, 0)
|
|
48
|
+
);
|
|
49
|
+
tr.setMeta(yProsemirror.ySyncPluginKey, {
|
|
50
|
+
snapshot: null,
|
|
51
|
+
prevSnapshot: null
|
|
52
|
+
});
|
|
53
|
+
if (doc) {
|
|
54
|
+
doc.gc = true;
|
|
55
|
+
}
|
|
56
|
+
storage.snapshot = void 0;
|
|
57
|
+
return tr;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
22
62
|
const AiExtension = core.Extension.create({
|
|
23
63
|
name: "liveblocksAi",
|
|
24
64
|
addOptions() {
|
|
25
65
|
return {
|
|
26
66
|
doc: void 0,
|
|
27
67
|
pud: void 0,
|
|
28
|
-
|
|
68
|
+
resolveContextualPrompt: () => Promise.reject(),
|
|
29
69
|
name: DEFAULT_AI_NAME
|
|
30
70
|
};
|
|
31
71
|
},
|
|
@@ -45,73 +85,74 @@ const AiExtension = core.Extension.create({
|
|
|
45
85
|
}
|
|
46
86
|
return true;
|
|
47
87
|
},
|
|
48
|
-
$
|
|
88
|
+
$acceptAiToolbarResponse: () => ({ tr, view }) => {
|
|
49
89
|
const currentState = this.storage.state;
|
|
50
90
|
if (currentState.phase !== "reviewing") {
|
|
51
91
|
return false;
|
|
52
92
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
93
|
+
if (isContextualPromptDiffResponse(currentState.response)) {
|
|
94
|
+
const binding = getYjsBinding(this.editor);
|
|
95
|
+
if (!binding) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
const fragmentContent = yProsemirror.yXmlFragmentToProseMirrorFragment(
|
|
99
|
+
binding.type,
|
|
100
|
+
this.editor.state.schema
|
|
101
|
+
);
|
|
102
|
+
tr.setMeta("addToHistory", false);
|
|
103
|
+
tr.replace(
|
|
104
|
+
0,
|
|
105
|
+
this.editor.state.doc.content.size,
|
|
106
|
+
new model.Slice(model.Fragment.from(fragmentContent), 0, 0)
|
|
107
|
+
);
|
|
108
|
+
tr.setMeta(yProsemirror.ySyncPluginKey, {
|
|
109
|
+
snapshot: null,
|
|
110
|
+
prevSnapshot: null
|
|
111
|
+
});
|
|
112
|
+
this.storage.snapshot = void 0;
|
|
113
|
+
} else {
|
|
114
|
+
const block = this.editor.schema.nodes.paragraph || Object.values(this.editor.schema.nodes).find(
|
|
115
|
+
(node) => node.isBlock
|
|
116
|
+
);
|
|
117
|
+
if (!block) {
|
|
118
|
+
throw new Error("Could not insert a new paragraph.");
|
|
119
|
+
}
|
|
120
|
+
const paragraph = block.create(
|
|
121
|
+
{},
|
|
122
|
+
this.editor.schema.text(currentState.response.text)
|
|
123
|
+
);
|
|
124
|
+
tr.insert(this.editor.state.selection.$to.end() + 1, paragraph);
|
|
125
|
+
view.dispatch(tr);
|
|
126
|
+
tr.setMeta("preventDispatch", true);
|
|
56
127
|
}
|
|
57
|
-
const fragmentContent = yProsemirror.yXmlFragmentToProseMirrorFragment(
|
|
58
|
-
binding.type,
|
|
59
|
-
this.editor.state.schema
|
|
60
|
-
);
|
|
61
|
-
tr.setMeta("addToHistory", false);
|
|
62
|
-
tr.replace(
|
|
63
|
-
0,
|
|
64
|
-
this.editor.state.doc.content.size,
|
|
65
|
-
new model.Slice(model.Fragment.from(fragmentContent), 0, 0)
|
|
66
|
-
);
|
|
67
|
-
tr.setMeta(yProsemirror.ySyncPluginKey, {
|
|
68
|
-
snapshot: null,
|
|
69
|
-
prevSnapshot: null
|
|
70
|
-
});
|
|
71
128
|
getLiveblocksYjsProvider(this.editor)?.unpause();
|
|
72
129
|
this.editor.setEditable(true);
|
|
73
|
-
this.storage.snapshot = void 0;
|
|
74
130
|
this.storage.state = { phase: "closed" };
|
|
75
131
|
return true;
|
|
76
132
|
},
|
|
77
|
-
$closeAiToolbar: () => ({ tr }) => {
|
|
133
|
+
$closeAiToolbar: () => ({ tr, view }) => {
|
|
78
134
|
const currentState = this.storage.state;
|
|
135
|
+
if (currentState.phase === "closed") {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
79
138
|
if (currentState.phase === "thinking") {
|
|
80
139
|
currentState.abortController.abort();
|
|
81
140
|
}
|
|
82
141
|
if (currentState.phase === "thinking" || currentState.phase === "reviewing") {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const fragmentContent = yProsemirror.yXmlFragmentToProseMirrorFragment(
|
|
93
|
-
type,
|
|
94
|
-
this.editor.state.schema
|
|
95
|
-
);
|
|
96
|
-
tr.setMeta("addToHistory", false);
|
|
97
|
-
tr.replace(
|
|
98
|
-
0,
|
|
99
|
-
this.editor.state.doc.content.size,
|
|
100
|
-
new model.Slice(model.Fragment.from(fragmentContent), 0, 0)
|
|
101
|
-
);
|
|
102
|
-
tr.setMeta(yProsemirror.ySyncPluginKey, {
|
|
103
|
-
snapshot: null,
|
|
104
|
-
prevSnapshot: null
|
|
105
|
-
});
|
|
106
|
-
getLiveblocksYjsProvider(this.editor)?.unpause();
|
|
107
|
-
if (this.options.doc) {
|
|
108
|
-
this.options.doc.gc = true;
|
|
109
|
-
}
|
|
110
|
-
this.storage.snapshot = void 0;
|
|
111
|
-
}
|
|
142
|
+
const revertTr = getRevertTransaction(
|
|
143
|
+
tr,
|
|
144
|
+
this.editor,
|
|
145
|
+
this.storage,
|
|
146
|
+
this.options.doc
|
|
147
|
+
);
|
|
148
|
+
if (revertTr) {
|
|
149
|
+
view.dispatch(revertTr);
|
|
150
|
+
tr.setMeta("preventDispatch", true);
|
|
112
151
|
}
|
|
113
|
-
this.editor.setEditable(true);
|
|
114
152
|
}
|
|
153
|
+
this.editor.commands.setTextSelection(currentState.initialSelection);
|
|
154
|
+
getLiveblocksYjsProvider(this.editor)?.unpause();
|
|
155
|
+
this.editor.setEditable(true);
|
|
115
156
|
this.storage.state = { phase: "closed" };
|
|
116
157
|
return true;
|
|
117
158
|
},
|
|
@@ -125,56 +166,84 @@ const AiExtension = core.Extension.create({
|
|
|
125
166
|
}
|
|
126
167
|
this.storage.state = {
|
|
127
168
|
phase: "asking",
|
|
169
|
+
initialSelection: {
|
|
170
|
+
from: this.editor.state.selection.from,
|
|
171
|
+
to: this.editor.state.selection.to
|
|
172
|
+
},
|
|
128
173
|
customPrompt: ""
|
|
129
174
|
};
|
|
130
175
|
return true;
|
|
131
176
|
},
|
|
132
|
-
$startAiToolbarThinking: (prompt) => () => {
|
|
177
|
+
$startAiToolbarThinking: (prompt, withPreviousResponse) => ({ tr, view }) => {
|
|
133
178
|
const currentState = this.storage.state;
|
|
134
179
|
if (currentState.phase === "thinking") {
|
|
135
180
|
return false;
|
|
136
181
|
}
|
|
137
|
-
if (currentState.phase === "reviewing") {
|
|
138
|
-
return this.editor.commands.$closeAiToolbar();
|
|
139
|
-
}
|
|
140
182
|
if (this.editor.isFocused) {
|
|
141
183
|
this.editor.commands.blur();
|
|
142
184
|
}
|
|
143
185
|
const abortController = new AbortController();
|
|
144
186
|
const provider = getLiveblocksYjsProvider(this.editor);
|
|
187
|
+
if (currentState.phase === "reviewing") {
|
|
188
|
+
const revertTr = getRevertTransaction(
|
|
189
|
+
tr,
|
|
190
|
+
this.editor,
|
|
191
|
+
this.storage,
|
|
192
|
+
this.options.doc
|
|
193
|
+
);
|
|
194
|
+
if (revertTr) {
|
|
195
|
+
view.dispatch(revertTr);
|
|
196
|
+
tr.setMeta("preventDispatch", true);
|
|
197
|
+
}
|
|
198
|
+
this.editor.commands.setTextSelection(
|
|
199
|
+
currentState.initialSelection
|
|
200
|
+
);
|
|
201
|
+
}
|
|
145
202
|
this.storage.state = {
|
|
146
203
|
phase: "thinking",
|
|
204
|
+
initialSelection: currentState.initialSelection ?? {
|
|
205
|
+
from: this.editor.state.selection.from,
|
|
206
|
+
to: this.editor.state.selection.to
|
|
207
|
+
},
|
|
147
208
|
customPrompt: currentState.customPrompt ?? "",
|
|
148
209
|
prompt,
|
|
149
|
-
abortController
|
|
210
|
+
abortController,
|
|
211
|
+
previousResponse: currentState.response
|
|
150
212
|
};
|
|
151
213
|
this.editor.setEditable(false);
|
|
152
214
|
core$1.autoRetry(
|
|
153
215
|
async () => {
|
|
154
216
|
await provider?.pause();
|
|
155
|
-
const
|
|
156
|
-
from: Math.max(this.editor.state.selection.to - 30, 0),
|
|
157
|
-
to: this.editor.state.selection.to
|
|
158
|
-
} : this.editor.state.selection;
|
|
159
|
-
return this.options.resolveAiPrompt({
|
|
217
|
+
const response = await this.options.resolveContextualPrompt({
|
|
160
218
|
prompt,
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
219
|
+
context: utils.getContextualPromptContext(this.editor, 3e3),
|
|
220
|
+
signal: abortController.signal,
|
|
221
|
+
previous: withPreviousResponse && currentState.phase === "reviewing" ? {
|
|
222
|
+
prompt: currentState.prompt,
|
|
223
|
+
response: {
|
|
224
|
+
type: currentState.response?.type,
|
|
225
|
+
text: currentState.response?.text
|
|
226
|
+
}
|
|
227
|
+
} : void 0
|
|
164
228
|
});
|
|
229
|
+
if (isResolveContextualPromptResponse(response)) {
|
|
230
|
+
return response;
|
|
231
|
+
} else {
|
|
232
|
+
throw new Error("Failed to resolve AI prompt.");
|
|
233
|
+
}
|
|
165
234
|
},
|
|
166
235
|
RESOLVE_AI_PROMPT_RETRY_ATTEMPTS,
|
|
167
236
|
RESOLVE_AI_PROMPT_RETRY_DELAYS,
|
|
168
237
|
(error) => {
|
|
169
238
|
return abortController.signal.aborted || error instanceof core$1.HttpError && error.status >= 400 && error.status < 500;
|
|
170
239
|
}
|
|
171
|
-
).then((
|
|
240
|
+
).then((response) => {
|
|
172
241
|
if (abortController.signal.aborted) {
|
|
173
242
|
return;
|
|
174
243
|
}
|
|
175
244
|
this.editor.commands._handleAiToolbarThinkingSuccess({
|
|
176
|
-
type:
|
|
177
|
-
text:
|
|
245
|
+
type: response.type,
|
|
246
|
+
text: response.text
|
|
178
247
|
});
|
|
179
248
|
}).catch((error) => {
|
|
180
249
|
if (abortController.signal.aborted) {
|
|
@@ -193,43 +262,64 @@ const AiExtension = core.Extension.create({
|
|
|
193
262
|
this.editor.setEditable(true);
|
|
194
263
|
this.storage.state = {
|
|
195
264
|
phase: "asking",
|
|
265
|
+
initialSelection: currentState.initialSelection,
|
|
196
266
|
customPrompt: currentState.prompt === currentState.customPrompt ? currentState.customPrompt : ""
|
|
197
267
|
};
|
|
198
268
|
return true;
|
|
199
269
|
},
|
|
200
|
-
|
|
270
|
+
_showAiToolbarReviewingDiff: () => () => {
|
|
271
|
+
if (this.storage.state.phase !== "reviewing") {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
274
|
+
if (!this.options.doc || !this.storage.snapshot) {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
const previousSnapshot = this.storage.snapshot ?? yjs.emptySnapshot;
|
|
278
|
+
const currentSnapshot = yjs.snapshot(this.options.doc);
|
|
279
|
+
if (yjs.equalSnapshots(previousSnapshot, currentSnapshot)) {
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
getYjsBinding(this.editor)?.renderSnapshot(
|
|
283
|
+
currentSnapshot,
|
|
284
|
+
previousSnapshot
|
|
285
|
+
);
|
|
286
|
+
return true;
|
|
287
|
+
},
|
|
288
|
+
_handleAiToolbarThinkingSuccess: (response) => ({ view, tr }) => {
|
|
201
289
|
const currentState = this.storage.state;
|
|
202
|
-
if (currentState.phase !== "thinking"
|
|
290
|
+
if (currentState.phase !== "thinking") {
|
|
203
291
|
return false;
|
|
204
292
|
}
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
to
|
|
213
|
-
};
|
|
214
|
-
const targetTo = output.type === "insert" ? to : from;
|
|
215
|
-
this.editor.commands.insertContentAt(contentTarget, output.text);
|
|
216
|
-
setTimeout(() => {
|
|
217
|
-
if (this.storage.snapshot) {
|
|
218
|
-
this.editor.commands._renderAiToolbarDiffInEditor(this.storage.snapshot);
|
|
219
|
-
}
|
|
220
|
-
}, 100);
|
|
221
|
-
selection = {
|
|
222
|
-
from,
|
|
223
|
-
to: targetTo + output.text.length
|
|
293
|
+
if (!isContextualPromptDiffResponse(response)) {
|
|
294
|
+
this.storage.state = {
|
|
295
|
+
phase: "reviewing",
|
|
296
|
+
initialSelection: currentState.initialSelection,
|
|
297
|
+
customPrompt: "",
|
|
298
|
+
prompt: currentState.prompt,
|
|
299
|
+
response
|
|
224
300
|
};
|
|
301
|
+
return true;
|
|
225
302
|
}
|
|
303
|
+
if (!this.options.doc) {
|
|
304
|
+
return false;
|
|
305
|
+
}
|
|
306
|
+
this.options.doc.gc = false;
|
|
307
|
+
this.storage.snapshot = yjs.snapshot(this.options.doc);
|
|
226
308
|
this.storage.state = {
|
|
227
309
|
phase: "reviewing",
|
|
310
|
+
initialSelection: currentState.initialSelection,
|
|
228
311
|
customPrompt: "",
|
|
229
312
|
prompt: currentState.prompt,
|
|
230
|
-
|
|
231
|
-
selection
|
|
313
|
+
response
|
|
232
314
|
};
|
|
315
|
+
tr.insertText(
|
|
316
|
+
response.text,
|
|
317
|
+
response.type === "insert" ? this.editor.state.selection.to : this.editor.state.selection.from,
|
|
318
|
+
this.editor.state.selection.to
|
|
319
|
+
);
|
|
320
|
+
view.dispatch(tr);
|
|
321
|
+
tr.setMeta("preventDispatch", true);
|
|
322
|
+
this.editor.commands._showAiToolbarReviewingDiff();
|
|
233
323
|
return true;
|
|
234
324
|
},
|
|
235
325
|
_handleAiToolbarThinkingError: (error) => () => {
|
|
@@ -238,10 +328,12 @@ const AiExtension = core.Extension.create({
|
|
|
238
328
|
return false;
|
|
239
329
|
}
|
|
240
330
|
this.editor.setEditable(true);
|
|
331
|
+
console.error(error);
|
|
241
332
|
this.storage.state = {
|
|
242
333
|
phase: "asking",
|
|
334
|
+
initialSelection: currentState.initialSelection,
|
|
243
335
|
customPrompt: currentState.prompt === currentState.customPrompt ? currentState.customPrompt : "",
|
|
244
|
-
error
|
|
336
|
+
error: error instanceof Error ? error : new Error(String(error), { cause: error })
|
|
245
337
|
};
|
|
246
338
|
return true;
|
|
247
339
|
},
|
|
@@ -252,22 +344,6 @@ const AiExtension = core.Extension.create({
|
|
|
252
344
|
}
|
|
253
345
|
this.storage.state.customPrompt = typeof customPrompt === "function" ? customPrompt(currentState.customPrompt) : customPrompt;
|
|
254
346
|
return true;
|
|
255
|
-
},
|
|
256
|
-
_renderAiToolbarDiffInEditor: (previous) => () => {
|
|
257
|
-
if (!this.options.doc) {
|
|
258
|
-
return false;
|
|
259
|
-
}
|
|
260
|
-
const previousSnapshot = previous ?? yjs.emptySnapshot;
|
|
261
|
-
const currentSnapshot = yjs.snapshot(this.options.doc);
|
|
262
|
-
if (yjs.equalSnapshots(previousSnapshot, currentSnapshot)) {
|
|
263
|
-
return true;
|
|
264
|
-
}
|
|
265
|
-
const binding = getYjsBinding(this.editor);
|
|
266
|
-
if (binding) {
|
|
267
|
-
binding.renderSnapshot(currentSnapshot, previousSnapshot);
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
return false;
|
|
271
347
|
}
|
|
272
348
|
};
|
|
273
349
|
},
|
|
@@ -277,10 +353,10 @@ const AiExtension = core.Extension.create({
|
|
|
277
353
|
key: types.AI_TOOLBAR_SELECTION_PLUGIN,
|
|
278
354
|
props: {
|
|
279
355
|
decorations: ({ doc, selection }) => {
|
|
280
|
-
if (this.storage.state.phase === "closed" || this.storage.state.phase === "reviewing" && this.storage.state.
|
|
356
|
+
if (this.storage.state.phase === "closed" || this.storage.state.phase === "reviewing" && isContextualPromptDiffResponse(this.storage.state.response)) {
|
|
281
357
|
return view.DecorationSet.create(doc, []);
|
|
282
358
|
}
|
|
283
|
-
const { from, to } =
|
|
359
|
+
const { from, to } = selection;
|
|
284
360
|
const decorations = [
|
|
285
361
|
view.Decoration.inline(from, to, {
|
|
286
362
|
class: "lb-root lb-selection lb-tiptap-active-selection"
|
|
@@ -296,4 +372,5 @@ const AiExtension = core.Extension.create({
|
|
|
296
372
|
|
|
297
373
|
exports.AiExtension = AiExtension;
|
|
298
374
|
exports.DEFAULT_STATE = DEFAULT_STATE;
|
|
375
|
+
exports.isContextualPromptDiffResponse = isContextualPromptDiffResponse;
|
|
299
376
|
//# sourceMappingURL=AiExtension.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AiExtension.js","sources":["../../src/ai/AiExtension.ts"],"sourcesContent":["import { autoRetry, HttpError } from \"@liveblocks/core\";\nimport type { LiveblocksYjsProvider } from \"@liveblocks/yjs\";\nimport type { CommandProps, Editor } from \"@tiptap/core\";\nimport { Extension } from \"@tiptap/core\";\nimport { Fragment, Slice } from \"@tiptap/pm/model\";\nimport { Plugin } from \"@tiptap/pm/state\";\nimport { Decoration, DecorationSet } from \"@tiptap/pm/view\";\nimport {\n ySyncPluginKey,\n yXmlFragmentToProseMirrorFragment,\n} from \"y-prosemirror\";\nimport type { Snapshot } from \"yjs\";\nimport {\n createDocFromSnapshot,\n emptySnapshot,\n equalSnapshots,\n snapshot,\n} from \"yjs\";\n\nimport {\n AI_TOOLBAR_SELECTION_PLUGIN,\n type AiCommands,\n type AiExtensionOptions,\n type AiExtensionStorage,\n type AiToolbarOutput,\n type AiToolbarState,\n type LiveblocksExtensionStorage,\n type YSyncPluginState,\n} from \"../types\";\n\nconst DEFAULT_AI_NAME = \"AI\";\nexport const DEFAULT_STATE: AiToolbarState = { phase: \"closed\" };\n\nconst RESOLVE_AI_PROMPT_RETRY_ATTEMPTS = 3;\nconst RESOLVE_AI_PROMPT_RETRY_DELAYS = [1000, 2000, 4000];\n\nfunction getYjsBinding(editor: Editor) {\n return (ySyncPluginKey.getState(editor.view.state) as YSyncPluginState)\n .binding;\n}\n\nfunction getLiveblocksYjsProvider(editor: Editor) {\n return (\n editor.extensionStorage.liveblocksExtension as\n | LiveblocksExtensionStorage\n | undefined\n )?.provider as LiveblocksYjsProvider | undefined;\n}\n\nexport const AiExtension = Extension.create<\n AiExtensionOptions,\n AiExtensionStorage\n>({\n name: \"liveblocksAi\",\n addOptions() {\n return {\n doc: undefined,\n pud: undefined,\n\n // The actual default resolver is set in LiveblocksExtension via AiExtension.configure()\n resolveAiPrompt: () => Promise.reject(),\n name: DEFAULT_AI_NAME,\n };\n },\n addStorage() {\n return {\n state: DEFAULT_STATE,\n name: this.options.name,\n };\n },\n addCommands() {\n return {\n askAi: (prompt) => () => {\n if (typeof prompt === \"string\") {\n (\n this.editor.commands as unknown as AiCommands\n ).$startAiToolbarThinking(prompt);\n } else {\n (\n this.editor.commands as unknown as AiCommands\n ).$openAiToolbarAsking();\n }\n\n return true;\n },\n\n $acceptAiToolbarOutput:\n () =>\n ({ tr }: CommandProps) => {\n const currentState = this.storage.state;\n if (currentState.phase !== \"reviewing\") {\n return false;\n }\n const binding = getYjsBinding(this.editor);\n if (!binding) {\n return false;\n }\n\n const fragmentContent = yXmlFragmentToProseMirrorFragment(\n binding.type,\n this.editor.state.schema\n );\n tr.setMeta(\"addToHistory\", false);\n tr.replace(\n 0,\n this.editor.state.doc.content.size,\n new Slice(Fragment.from(fragmentContent), 0, 0)\n );\n tr.setMeta(ySyncPluginKey, {\n snapshot: null,\n prevSnapshot: null,\n });\n\n // TODO: move this cleanup to somewhere that closeAIToolbar can share\n getLiveblocksYjsProvider(this.editor)?.unpause();\n this.editor.setEditable(true);\n this.storage.snapshot = undefined;\n this.storage.state = { phase: \"closed\" };\n return true;\n },\n\n $closeAiToolbar:\n () =>\n ({ tr }: CommandProps) => {\n const currentState = this.storage.state;\n\n // 1. If in \"thinking\" phase, cancel the current AI request\n if (currentState.phase === \"thinking\") {\n currentState.abortController.abort();\n }\n\n // 2. If in \"thinking\" or \"reviewing\" phase, revert the editor if possible and unblock it\n if (\n currentState.phase === \"thinking\" ||\n currentState.phase === \"reviewing\"\n ) {\n if (this.storage.snapshot) {\n const binding = getYjsBinding(this.editor);\n\n if (binding) {\n binding.mapping.clear();\n\n const docFromSnapshot = createDocFromSnapshot(\n binding.doc,\n this.storage.snapshot\n );\n const type = docFromSnapshot.getXmlFragment(\"default\"); // TODO: field\n const fragmentContent = yXmlFragmentToProseMirrorFragment(\n type,\n this.editor.state.schema\n );\n\n tr.setMeta(\"addToHistory\", false);\n tr.replace(\n 0,\n this.editor.state.doc.content.size,\n new Slice(Fragment.from(fragmentContent), 0, 0)\n );\n tr.setMeta(ySyncPluginKey, {\n snapshot: null,\n prevSnapshot: null,\n });\n\n getLiveblocksYjsProvider(this.editor)?.unpause();\n\n if (this.options.doc) {\n this.options.doc.gc = true;\n }\n\n this.storage.snapshot = undefined;\n }\n }\n\n this.editor.setEditable(true);\n }\n\n // 4. Set to \"closed\" phase\n this.storage.state = { phase: \"closed\" };\n\n return true;\n },\n\n $openAiToolbarAsking: () => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"closed\" phase, do nothing\n if (currentState.phase !== \"closed\") {\n return false;\n }\n\n // 2. Blur the editor if needed\n if (this.editor.isFocused) {\n this.editor.commands.blur();\n }\n\n // 3. Set to \"asking\" phase\n this.storage.state = {\n phase: \"asking\",\n\n // Initialize the custom prompt as empty\n customPrompt: \"\",\n };\n\n return true;\n },\n\n $startAiToolbarThinking: (prompt: string) => () => {\n const currentState = this.storage.state;\n\n // 1. If in \"thinking\" phase already, do nothing\n if (currentState.phase === \"thinking\") {\n return false;\n }\n\n if (currentState.phase === \"reviewing\") {\n // TODO: this is a retry, we should actually retry and start thinking again\n // Maybe not, it could be a refinement prompt\n return (\n this.editor.commands as unknown as AiCommands\n ).$closeAiToolbar();\n }\n\n // 2. Blur the editor if needed\n if (this.editor.isFocused) {\n this.editor.commands.blur();\n }\n\n const abortController = new AbortController();\n const provider = getLiveblocksYjsProvider(this.editor);\n\n // 3. Set to \"thinking\" phase\n this.storage.state = {\n phase: \"thinking\",\n customPrompt: currentState.customPrompt ?? \"\",\n prompt,\n abortController,\n };\n\n // 4. Block the editor\n this.editor.setEditable(false);\n\n // 5. Start the AI request\n autoRetry(\n async () => {\n await provider?.pause();\n const { from, to } = this.editor.state.selection.empty\n ? {\n // TODO: this is a hack to get the context around the selection, we need to improve this\n from: Math.max(this.editor.state.selection.to - 30, 0),\n to: this.editor.state.selection.to,\n }\n : this.editor.state.selection;\n\n return this.options.resolveAiPrompt({\n prompt,\n selectionText: this.editor.state.doc.textBetween(from, to, \" \"),\n /*\n TODO: This needs a maximum to avoid overloading context, for now I've arbitrailiry chosen 3000\n characters but this will need to be improved, probably using word boundary of some sort (languages can make that tricky)\n as well as choosing text around the selection, so before/after.\n */\n context: this.editor.getText().slice(0, 3_000),\n signal: abortController.signal,\n });\n },\n RESOLVE_AI_PROMPT_RETRY_ATTEMPTS,\n RESOLVE_AI_PROMPT_RETRY_DELAYS,\n\n (error) => {\n // Don't retry on 4xx errors or if the request was aborted\n return (\n abortController.signal.aborted ||\n (error instanceof HttpError &&\n error.status >= 400 &&\n error.status < 500)\n );\n }\n )\n .then((output) => {\n if (abortController.signal.aborted) {\n return;\n }\n\n // 5.a. If the AI request succeeds, set to \"reviewing\" phase with the output\n (\n this.editor.commands as unknown as AiCommands\n )._handleAiToolbarThinkingSuccess({\n type: output.type,\n text: output.content,\n });\n })\n .catch((error) => {\n if (abortController.signal.aborted) {\n return;\n }\n\n // 5.b. If the AI request fails, set to \"asking\" phase with error\n (\n this.editor.commands as unknown as AiCommands\n )._handleAiToolbarThinkingError(error as Error);\n });\n\n return true;\n },\n\n $cancelAiToolbarThinking: () => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\") {\n return false;\n }\n\n // 2. Cancel the current AI request\n currentState.abortController.abort();\n\n // 3. Unblock the editor\n this.editor.setEditable(true);\n\n // 4. Set to \"asking\" phase\n this.storage.state = {\n phase: \"asking\",\n // If the custom prompt is different than the prompt, reset it\n customPrompt:\n currentState.prompt === currentState.customPrompt\n ? currentState.customPrompt\n : \"\",\n };\n\n return true;\n },\n\n _handleAiToolbarThinkingSuccess: (output: AiToolbarOutput) => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\" || !this.options.doc) {\n return false;\n }\n\n let selection: { from: number; to: number } =\n this.editor.state.selection;\n\n // 2. If the output is a diff, apply it to the editor\n if ([\"modification\", \"insert\"].includes(output.type)) {\n this.options.doc.gc = false;\n this.storage.snapshot = snapshot(this.options.doc);\n\n const { empty, from, to } = this.editor.state.selection;\n\n // if the selection is empty, insert at the end of the selection\n const contentTarget =\n empty || output.type === \"insert\"\n ? to\n : {\n from,\n to,\n };\n\n // when inserting, use the \"to\" position, otherwise when modifing, use the \"from\" position\n const targetTo = output.type === \"insert\" ? to : from;\n\n // 2.a. Insert the output.\n this.editor.commands.insertContentAt(contentTarget, output.text);\n\n // 2.b. Diff the output in the editor\n // TODO: setTimeout will make this execute after the current transaction is committed, which is returned by the insert content command.\n setTimeout(() => {\n if (this.storage.snapshot) {\n (\n this.editor.commands as unknown as AiCommands\n )._renderAiToolbarDiffInEditor(this.storage.snapshot);\n }\n }, 100);\n\n selection = {\n from,\n to: targetTo + output.text.length, // take into account the new length with output\n };\n }\n\n // 3. Set to \"reviewing\" phase with the output\n this.storage.state = {\n phase: \"reviewing\",\n customPrompt: \"\",\n prompt: currentState.prompt,\n output,\n selection,\n };\n\n return true;\n },\n\n _handleAiToolbarThinkingError: (error: Error) => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\") {\n return false;\n }\n\n // 2. Unblock the editor\n this.editor.setEditable(true);\n\n // 3. Set to \"asking\" phase with error\n this.storage.state = {\n phase: \"asking\",\n // If the custom prompt is different than the prompt, reset it\n customPrompt:\n currentState.prompt === currentState.customPrompt\n ? currentState.customPrompt\n : \"\",\n // TODO: Improve error handling\n error,\n };\n\n return true;\n },\n\n _updateAiToolbarCustomPrompt:\n (customPrompt: string | ((currentCustomPrompt: string) => string)) =>\n () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in a phase with a custom prompt, do nothing\n if (typeof currentState.customPrompt !== \"string\") {\n return false;\n }\n\n // 2. Update the custom prompt\n this.storage.state.customPrompt =\n typeof customPrompt === \"function\"\n ? customPrompt(currentState.customPrompt)\n : customPrompt;\n\n return true;\n },\n\n _renderAiToolbarDiffInEditor: (previous?: Snapshot) => () => {\n if (!this.options.doc) {\n return false;\n }\n\n const previousSnapshot: Snapshot = previous ?? emptySnapshot;\n const currentSnapshot = snapshot(this.options.doc);\n\n if (equalSnapshots(previousSnapshot, currentSnapshot)) {\n return true;\n }\n\n const binding = getYjsBinding(this.editor);\n\n if (binding) {\n binding.renderSnapshot(currentSnapshot, previousSnapshot);\n return true;\n }\n\n return false;\n },\n };\n },\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: AI_TOOLBAR_SELECTION_PLUGIN,\n props: {\n decorations: ({ doc, selection }) => {\n if (\n this.storage.state.phase === \"closed\" ||\n // TODO: Don't show the selection when reviewing diff outputs\n (this.storage.state.phase === \"reviewing\" &&\n this.storage.state.output.type !== \"other\")\n ) {\n return DecorationSet.create(doc, []);\n }\n const { from, to } = this.storage.state.selection ?? selection;\n const decorations: Decoration[] = [\n Decoration.inline(from, to, {\n class: \"lb-root lb-selection lb-tiptap-active-selection\",\n }),\n ];\n return DecorationSet.create(doc, decorations);\n },\n },\n }),\n ];\n },\n});\n"],"names":["ySyncPluginKey","Extension","yXmlFragmentToProseMirrorFragment","Slice","Fragment","createDocFromSnapshot","autoRetry","HttpError","snapshot","emptySnapshot","equalSnapshots","Plugin","AI_TOOLBAR_SELECTION_PLUGIN","DecorationSet","Decoration"],"mappings":";;;;;;;;;;;AA8BA,MAAM,eAAkB,GAAA,IAAA,CAAA;AACX,MAAA,aAAA,GAAgC,EAAE,KAAA,EAAO,QAAS,GAAA;AAE/D,MAAM,gCAAmC,GAAA,CAAA,CAAA;AACzC,MAAM,8BAAiC,GAAA,CAAC,GAAM,EAAA,GAAA,EAAM,GAAI,CAAA,CAAA;AAExD,SAAS,cAAc,MAAgB,EAAA;AACrC,EAAA,OAAQA,2BAAe,CAAA,QAAA,CAAS,MAAO,CAAA,IAAA,CAAK,KAAK,CAC9C,CAAA,OAAA,CAAA;AACL,CAAA;AAEA,SAAS,yBAAyB,MAAgB,EAAA;AAChD,EACE,OAAA,MAAA,CAAO,iBAAiB,mBAGvB,EAAA,QAAA,CAAA;AACL,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,MAAO,CAAA,GAAA,EAAK,WAAW,CAAA,CAAA;AAAA,WAC9C;AAAA,SACF;AAAA,OACD,CAAA;AAAA,KACH,CAAA;AAAA,GACF;AACF,CAAC;;;;;"}
|
|
1
|
+
{"version":3,"file":"AiExtension.js","sources":["../../src/ai/AiExtension.ts"],"sourcesContent":["import {\n autoRetry,\n type ContextualPromptResponse,\n HttpError,\n} from \"@liveblocks/core\";\nimport type { LiveblocksYjsProvider } from \"@liveblocks/yjs\";\nimport type { CommandProps, Editor } from \"@tiptap/core\";\nimport { Extension } from \"@tiptap/core\";\nimport { Fragment, Slice } from \"@tiptap/pm/model\";\nimport type { Transaction } from \"@tiptap/pm/state\";\nimport { Plugin } from \"@tiptap/pm/state\";\nimport { Decoration, DecorationSet } from \"@tiptap/pm/view\";\nimport {\n ySyncPluginKey,\n yXmlFragmentToProseMirrorFragment,\n} from \"y-prosemirror\";\nimport type { Doc, Snapshot } from \"yjs\";\nimport {\n createDocFromSnapshot,\n emptySnapshot,\n equalSnapshots,\n snapshot as takeSnapshot,\n} from \"yjs\";\n\nimport {\n AI_TOOLBAR_SELECTION_PLUGIN,\n type AiCommands,\n type AiExtensionOptions,\n type AiExtensionStorage,\n type AiToolbarState,\n type LiveblocksExtensionStorage,\n type ResolveContextualPromptResponse,\n type YSyncPluginState,\n} from \"../types\";\nimport { getContextualPromptContext } from \"../utils\";\n\nconst DEFAULT_AI_NAME = \"AI\";\nexport const DEFAULT_STATE: AiToolbarState = { phase: \"closed\" };\n\nconst RESOLVE_AI_PROMPT_RETRY_ATTEMPTS = 3;\nconst RESOLVE_AI_PROMPT_RETRY_DELAYS = [1000, 2000, 4000];\n\nfunction getYjsBinding(editor: Editor) {\n return (ySyncPluginKey.getState(editor.view.state) as YSyncPluginState)\n .binding;\n}\n\nfunction getLiveblocksYjsProvider(editor: Editor) {\n return (\n editor.extensionStorage.liveblocksExtension as\n | LiveblocksExtensionStorage\n | undefined\n )?.provider as LiveblocksYjsProvider | undefined;\n}\n\nexport function isContextualPromptDiffResponse(\n response: ContextualPromptResponse\n): response is Extract<\n ContextualPromptResponse,\n { type: \"replace\" | \"insert\" }\n> {\n return response.type === \"replace\" || response.type === \"insert\";\n}\n\nfunction isResolveContextualPromptResponse(\n response: unknown\n): response is ResolveContextualPromptResponse {\n return (\n typeof response === \"object\" &&\n response !== null &&\n typeof (response as { text: unknown }).text === \"string\" &&\n typeof (response as { type: unknown }).type === \"string\" &&\n [\"insert\", \"replace\", \"other\"].includes((response as { type: string }).type)\n );\n}\n\nfunction getRevertTransaction(\n tr: Transaction,\n editor: Editor,\n storage: AiExtensionStorage,\n doc?: Doc\n): Transaction | null {\n if (storage.snapshot) {\n const binding = getYjsBinding(editor);\n\n if (binding) {\n binding.mapping.clear();\n\n const docFromSnapshot = createDocFromSnapshot(\n binding.doc,\n storage.snapshot\n );\n const type = docFromSnapshot.getXmlFragment(\"default\"); // TODO: field\n const fragmentContent = yXmlFragmentToProseMirrorFragment(\n type,\n editor.state.schema\n );\n\n tr.setMeta(\"addToHistory\", false);\n tr.replace(\n 0,\n editor.state.doc.content.size,\n new Slice(Fragment.from(fragmentContent), 0, 0)\n );\n tr.setMeta(ySyncPluginKey, {\n snapshot: null,\n prevSnapshot: null,\n });\n\n if (doc) {\n doc.gc = true;\n }\n\n storage.snapshot = undefined;\n\n return tr;\n }\n }\n return null;\n}\n\nexport const AiExtension = Extension.create<\n AiExtensionOptions,\n AiExtensionStorage\n>({\n name: \"liveblocksAi\",\n addOptions() {\n return {\n doc: undefined,\n pud: undefined,\n\n // The actual default resolver is set in LiveblocksExtension via AiExtension.configure()\n resolveContextualPrompt: () => Promise.reject(),\n name: DEFAULT_AI_NAME,\n };\n },\n addStorage() {\n return {\n state: DEFAULT_STATE,\n name: this.options.name,\n };\n },\n addCommands() {\n return {\n askAi: (prompt) => () => {\n if (typeof prompt === \"string\") {\n (\n this.editor.commands as unknown as AiCommands\n ).$startAiToolbarThinking(prompt);\n } else {\n (\n this.editor.commands as unknown as AiCommands\n ).$openAiToolbarAsking();\n }\n\n return true;\n },\n\n $acceptAiToolbarResponse:\n () =>\n ({ tr, view }: CommandProps) => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"reviewing\" phase, do nothing\n if (currentState.phase !== \"reviewing\") {\n return false;\n }\n\n // 2. Accept the response\n if (isContextualPromptDiffResponse(currentState.response)) {\n // 2.a. If the response is a diff, apply it definitely\n const binding = getYjsBinding(this.editor);\n if (!binding) {\n return false;\n }\n\n const fragmentContent = yXmlFragmentToProseMirrorFragment(\n binding.type,\n this.editor.state.schema\n );\n tr.setMeta(\"addToHistory\", false);\n tr.replace(\n 0,\n this.editor.state.doc.content.size,\n new Slice(Fragment.from(fragmentContent), 0, 0)\n );\n tr.setMeta(ySyncPluginKey, {\n snapshot: null,\n prevSnapshot: null,\n });\n\n this.storage.snapshot = undefined;\n } else {\n // 2.b. If the response is not a diff, insert it below the selection\n const block =\n this.editor.schema.nodes.paragraph ||\n Object.values(this.editor.schema.nodes).find(\n (node) => node.isBlock\n );\n\n if (!block) {\n throw new Error(\"Could not insert a new paragraph.\");\n }\n\n const paragraph = block.create(\n {},\n this.editor.schema.text(currentState.response.text)\n );\n\n tr.insert(this.editor.state.selection.$to.end() + 1, paragraph);\n view.dispatch(tr);\n // Prevent TipTap from dispatching this transaction, because we already did (this is a hack)\n tr.setMeta(\"preventDispatch\", true);\n }\n\n // 3. Unblock the editor\n getLiveblocksYjsProvider(this.editor)?.unpause();\n this.editor.setEditable(true);\n\n // 4. Set to \"closed\" phase\n this.storage.state = { phase: \"closed\" };\n\n return true;\n },\n\n $closeAiToolbar:\n () =>\n ({ tr, view }: CommandProps) => {\n const currentState = this.storage.state;\n\n // 1. If already in \"closed\" phase, do nothing\n if (currentState.phase === \"closed\") {\n return false;\n }\n\n // 2. If in \"thinking\" phase, cancel the current AI request\n if (currentState.phase === \"thinking\") {\n currentState.abortController.abort();\n }\n\n // 3. If in \"thinking\" or \"reviewing\" phase, revert the editor if possible and unblock it\n if (\n currentState.phase === \"thinking\" ||\n currentState.phase === \"reviewing\"\n ) {\n // Get the revert transaction and dispatch it\n const revertTr = getRevertTransaction(\n tr,\n this.editor,\n this.storage,\n this.options.doc\n );\n if (revertTr) {\n view.dispatch(revertTr);\n // Prevent TipTap from dispatching this transaction, because we already did (this is a hack)\n tr.setMeta(\"preventDispatch\", true);\n }\n }\n\n // 4. Restore the initial selection\n this.editor.commands.setTextSelection(currentState.initialSelection);\n\n // 5. Unblock the editor\n getLiveblocksYjsProvider(this.editor)?.unpause();\n this.editor.setEditable(true);\n\n // 6. Set to \"closed\" phase\n this.storage.state = { phase: \"closed\" };\n\n return true;\n },\n\n $openAiToolbarAsking: () => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"closed\" phase, do nothing\n if (currentState.phase !== \"closed\") {\n return false;\n }\n\n // 2. Blur the editor if needed\n if (this.editor.isFocused) {\n this.editor.commands.blur();\n }\n\n // 3. Set to \"asking\" phase\n this.storage.state = {\n phase: \"asking\",\n // Store the initial selection\n initialSelection: {\n from: this.editor.state.selection.from,\n to: this.editor.state.selection.to,\n },\n // Initialize the custom prompt as empty\n customPrompt: \"\",\n };\n\n return true;\n },\n\n $startAiToolbarThinking:\n (prompt: string, withPreviousResponse?: boolean) =>\n ({ tr, view }: CommandProps) => {\n const currentState = this.storage.state;\n\n // 1. If in \"thinking\" phase already, do nothing\n if (currentState.phase === \"thinking\") {\n return false;\n }\n\n // 2. Blur the editor if needed\n if (this.editor.isFocused) {\n this.editor.commands.blur();\n }\n\n const abortController = new AbortController();\n const provider = getLiveblocksYjsProvider(this.editor);\n\n // 3. If this is a retry or a refinement, revert the editor and restore the initial selection\n if (currentState.phase === \"reviewing\") {\n // 3.a. Revert the editor\n const revertTr = getRevertTransaction(\n tr,\n this.editor,\n this.storage,\n this.options.doc\n );\n if (revertTr) {\n // Important: in this scenario we do not unpause the provider\n view.dispatch(revertTr);\n // Prevent Tiptap from dispatching this transaction, because we already did (this is a hack)\n tr.setMeta(\"preventDispatch\", true);\n }\n\n // 3.b. Restore the initial selection\n this.editor.commands.setTextSelection(\n currentState.initialSelection\n );\n }\n\n // 4. Set to \"thinking\" phase\n this.storage.state = {\n phase: \"thinking\",\n // Store the initial selection if the toolbar is opened directly in the \"thinking\" phase\n initialSelection: currentState.initialSelection ?? {\n from: this.editor.state.selection.from,\n to: this.editor.state.selection.to,\n },\n // Initialize the custom prompt as empty if the toolbar is opened directly in the \"thinking\" phase\n customPrompt: currentState.customPrompt ?? \"\",\n prompt,\n abortController,\n previousResponse: currentState.response,\n };\n\n // 5. Block the editor\n this.editor.setEditable(false);\n\n // 6. Start the AI request\n autoRetry(\n async () => {\n await provider?.pause();\n\n // 6.a. Resolve the AI prompt\n const response = (await this.options.resolveContextualPrompt({\n prompt,\n context: getContextualPromptContext(this.editor, 3_000),\n signal: abortController.signal,\n previous:\n withPreviousResponse && currentState.phase === \"reviewing\"\n ? {\n prompt: currentState.prompt,\n response: {\n type: currentState.response?.type,\n text: currentState.response?.text,\n },\n }\n : undefined,\n })) as unknown;\n\n // 6.b. Validate the response\n if (isResolveContextualPromptResponse(response)) {\n return response;\n } else {\n throw new Error(\"Failed to resolve AI prompt.\");\n }\n },\n RESOLVE_AI_PROMPT_RETRY_ATTEMPTS,\n RESOLVE_AI_PROMPT_RETRY_DELAYS,\n\n (error) => {\n // Don't retry on 4xx errors or if the request was aborted\n return (\n abortController.signal.aborted ||\n (error instanceof HttpError &&\n error.status >= 400 &&\n error.status < 500)\n );\n }\n )\n .then((response) => {\n if (abortController.signal.aborted) {\n return;\n }\n\n // 6.a. If the AI request succeeds, set to \"reviewing\" phase with the response\n (\n this.editor.commands as unknown as AiCommands\n )._handleAiToolbarThinkingSuccess({\n type: response.type,\n text: response.text,\n });\n })\n .catch((error) => {\n if (abortController.signal.aborted) {\n return;\n }\n\n // 6.b. If the AI request fails, set to \"asking\" phase with error\n (\n this.editor.commands as unknown as AiCommands\n )._handleAiToolbarThinkingError(error);\n });\n\n return true;\n },\n\n $cancelAiToolbarThinking: () => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\") {\n return false;\n }\n\n // 2. Cancel the current AI request\n currentState.abortController.abort();\n\n // 3. Unblock the editor\n this.editor.setEditable(true);\n\n // 4. Set to \"asking\" phase\n this.storage.state = {\n phase: \"asking\",\n initialSelection: currentState.initialSelection,\n // If the custom prompt is different than the prompt, reset it\n customPrompt:\n currentState.prompt === currentState.customPrompt\n ? currentState.customPrompt\n : \"\",\n };\n\n return true;\n },\n\n _showAiToolbarReviewingDiff: () => () => {\n // The diff is applied right before moving to the \"reviewing\" phase\n if (this.storage.state.phase !== \"reviewing\") {\n return false;\n }\n\n if (!this.options.doc || !this.storage.snapshot) {\n return false;\n }\n\n const previousSnapshot: Snapshot =\n this.storage.snapshot ?? emptySnapshot;\n const currentSnapshot = takeSnapshot(this.options.doc);\n\n if (equalSnapshots(previousSnapshot, currentSnapshot)) {\n return true;\n }\n\n getYjsBinding(this.editor)?.renderSnapshot(\n currentSnapshot,\n previousSnapshot\n );\n\n return true;\n },\n\n _handleAiToolbarThinkingSuccess:\n (response: ContextualPromptResponse) =>\n ({ view, tr }: CommandProps) => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\") {\n return false;\n }\n\n // 2. If this is not a diff, the response will just be in the toolbar, set to \"reviewing\" phase directly with the response\n if (!isContextualPromptDiffResponse(response)) {\n this.storage.state = {\n phase: \"reviewing\",\n initialSelection: currentState.initialSelection,\n customPrompt: \"\",\n prompt: currentState.prompt,\n response,\n };\n\n return true;\n }\n\n if (!this.options.doc) {\n return false;\n }\n\n // 3. Otherwise, the response is a diff, apply it to the editor\n this.options.doc.gc = false;\n this.storage.snapshot = takeSnapshot(this.options.doc);\n\n // 4. Set to \"reviewing\" phase with the new range\n this.storage.state = {\n phase: \"reviewing\",\n initialSelection: currentState.initialSelection,\n customPrompt: \"\",\n prompt: currentState.prompt,\n response,\n };\n\n // 5. Insert the response's text\n tr.insertText(\n response.text,\n response.type === \"insert\"\n ? this.editor.state.selection.to\n : this.editor.state.selection.from,\n this.editor.state.selection.to\n );\n view.dispatch(tr);\n // Prevent Tiptap from dispatching this transaction, because we already did (this is a hack)\n tr.setMeta(\"preventDispatch\", true);\n\n // 6. Show the diff\n (\n this.editor.commands as unknown as AiCommands\n )._showAiToolbarReviewingDiff();\n\n // We moved to the \"reviewing\" phase, so even if `_showAiToolbarReviewingDiff`\n // returns `false` somehow, we still want to return `true`\n return true;\n },\n\n _handleAiToolbarThinkingError: (error: unknown) => () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in \"thinking\" phase, do nothing\n if (currentState.phase !== \"thinking\") {\n return false;\n }\n\n // 2. Unblock the editor\n this.editor.setEditable(true);\n\n // 3. Log the error\n console.error(error);\n\n // 4. Set to \"asking\" phase with error\n this.storage.state = {\n phase: \"asking\",\n initialSelection: currentState.initialSelection,\n // If the custom prompt is different than the prompt, reset it\n customPrompt:\n currentState.prompt === currentState.customPrompt\n ? currentState.customPrompt\n : \"\",\n // Pass the error so it can be displayed\n error:\n error instanceof Error\n ? error\n : new Error(String(error), { cause: error }),\n };\n\n return true;\n },\n\n _updateAiToolbarCustomPrompt:\n (customPrompt: string | ((currentCustomPrompt: string) => string)) =>\n () => {\n const currentState = this.storage.state;\n\n // 1. If NOT in a phase with a custom prompt, do nothing\n if (typeof currentState.customPrompt !== \"string\") {\n return false;\n }\n\n // 2. Update the custom prompt\n this.storage.state.customPrompt =\n typeof customPrompt === \"function\"\n ? customPrompt(currentState.customPrompt)\n : customPrompt;\n\n return true;\n },\n };\n },\n addProseMirrorPlugins() {\n return [\n new Plugin({\n key: AI_TOOLBAR_SELECTION_PLUGIN,\n props: {\n decorations: ({ doc, selection }) => {\n // Don't show the AI toolbar selection if the toolbar is closed or when reviewing diff responses\n if (\n this.storage.state.phase === \"closed\" ||\n (this.storage.state.phase === \"reviewing\" &&\n isContextualPromptDiffResponse(this.storage.state.response))\n ) {\n return DecorationSet.create(doc, []);\n }\n\n const { from, to } = selection;\n const decorations: Decoration[] = [\n Decoration.inline(from, to, {\n class: \"lb-root lb-selection lb-tiptap-active-selection\",\n }),\n ];\n\n return DecorationSet.create(doc, decorations);\n },\n },\n }),\n ];\n },\n});\n"],"names":["ySyncPluginKey","createDocFromSnapshot","yXmlFragmentToProseMirrorFragment","Slice","Fragment","Extension","autoRetry","getContextualPromptContext","HttpError","emptySnapshot","takeSnapshot","equalSnapshots","Plugin","AI_TOOLBAR_SELECTION_PLUGIN","DecorationSet","Decoration"],"mappings":";;;;;;;;;;;;AAoCA,MAAM,eAAkB,GAAA,IAAA,CAAA;AACX,MAAA,aAAA,GAAgC,EAAE,KAAA,EAAO,QAAS,GAAA;AAE/D,MAAM,gCAAmC,GAAA,CAAA,CAAA;AACzC,MAAM,8BAAiC,GAAA,CAAC,GAAM,EAAA,GAAA,EAAM,GAAI,CAAA,CAAA;AAExD,SAAS,cAAc,MAAgB,EAAA;AACrC,EAAA,OAAQA,2BAAe,CAAA,QAAA,CAAS,MAAO,CAAA,IAAA,CAAK,KAAK,CAC9C,CAAA,OAAA,CAAA;AACL,CAAA;AAEA,SAAS,yBAAyB,MAAgB,EAAA;AAChD,EACE,OAAA,MAAA,CAAO,iBAAiB,mBAGvB,EAAA,QAAA,CAAA;AACL,CAAA;AAEO,SAAS,+BACd,QAIA,EAAA;AACA,EAAA,OAAO,QAAS,CAAA,IAAA,KAAS,SAAa,IAAA,QAAA,CAAS,IAAS,KAAA,QAAA,CAAA;AAC1D,CAAA;AAEA,SAAS,kCACP,QAC6C,EAAA;AAC7C,EACE,OAAA,OAAO,aAAa,QACpB,IAAA,QAAA,KAAa,QACb,OAAQ,QAAA,CAA+B,SAAS,QAChD,IAAA,OAAQ,SAA+B,IAAS,KAAA,QAAA,IAChD,CAAC,QAAU,EAAA,SAAA,EAAW,OAAO,CAAE,CAAA,QAAA,CAAU,SAA8B,IAAI,CAAA,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