-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsuggestEdit.js
More file actions
67 lines (59 loc) · 2.21 KB
/
suggestEdit.js
File metadata and controls
67 lines (59 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
'use strict';
const padMessageHandler = require('ep_etherpad-lite/node/handler/PadMessageHandler');
const {Builder} = require('ep_etherpad-lite/static/js/Builder');
const log4js = require('ep_etherpad-lite/node_modules/log4js');
const logger = log4js.getLogger('ep_ai_chat:suggest');
const suggestEdit = async (pad, edit, deps) => {
const {requesterAuthorId, aiAuthorId, aiAuthorName, commentManager, shared, io} = deps;
const currentText = pad.text();
const idx = currentText.indexOf(edit.findText);
if (idx === -1) {
return {success: false, error: `Text not found: "${edit.findText.substring(0, 100)}"`};
}
let commentId;
let comment;
try {
commentId = shared.generateCommentId();
comment = {
commentId,
name: aiAuthorName,
author: aiAuthorId,
text: edit.explanation || 'AI suggestion',
changeFrom: edit.findText,
changeTo: edit.replaceText,
timestamp: Date.now(),
};
await commentManager.bulkAddComments(pad.id, [comment]);
} catch (err) {
logger.error(`Failed to persist suggestion comment: ${err.message}`);
return {success: false, error: `comment persistence failed: ${err.message}`};
}
try {
const builder = new Builder(currentText.length);
const before = currentText.substring(0, idx);
const match = edit.findText;
const after = currentText.substring(idx + match.length);
if (before.length) builder.keepText(before);
const attribs = [['comment', commentId]];
if (requesterAuthorId) {
attribs.push(['ep_ai_chat:requestedBy', requesterAuthorId]);
}
builder.keepText(match, attribs, pad.pool);
if (after.length) builder.keepText(after);
const changeset = builder.toString();
await pad.appendRevision(changeset, aiAuthorId);
await padMessageHandler.updatePadClients(pad);
} catch (err) {
logger.error(`Failed to anchor suggestion changeset: ${err.message}`);
return {success: false, error: `anchor failed: ${err.message}`};
}
if (io) {
try {
io.to(pad.id).emit('pushAddComment', commentId, comment);
} catch (err) {
logger.warn(`pushAddComment broadcast failed: ${err.message}`);
}
}
return {success: true, commentId};
};
exports.suggestEdit = suggestEdit;