@atlaskit/editor-plugin-autocomplete 0.1.0 → 0.3.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/CHANGELOG.md +16 -0
- package/afm-cc/tsconfig.json +2 -1
- package/afm-jira/tsconfig.json +2 -1
- package/afm-products/tsconfig.json +2 -1
- package/build/tsconfig.json +20 -0
- package/build/url-module.d.ts +8 -0
- package/dist/cjs/autocompletePlugin.js +18 -5
- package/dist/cjs/autocompletePluginType.js +5 -1
- package/dist/cjs/pm-plugins/autocomplete-plugin.js +368 -0
- package/dist/cjs/pm-plugins/ghost-text-decoration.js +39 -0
- package/dist/cjs/pm-plugins/scoring-pipeline.js +256 -0
- package/dist/cjs/pm-plugins/slow-lane-client.js +199 -0
- package/dist/cjs/pm-plugins/text-predictor.js +796 -0
- package/dist/es2019/autocompletePlugin.js +20 -6
- package/dist/es2019/autocompletePluginType.js +1 -0
- package/dist/es2019/pm-plugins/autocomplete-plugin.js +368 -0
- package/dist/es2019/pm-plugins/ghost-text-decoration.js +33 -0
- package/dist/es2019/pm-plugins/scoring-pipeline.js +221 -0
- package/dist/es2019/pm-plugins/slow-lane-client.js +157 -0
- package/dist/es2019/pm-plugins/text-predictor.js +631 -0
- package/dist/esm/autocompletePlugin.js +18 -5
- package/dist/esm/autocompletePluginType.js +1 -0
- package/dist/esm/pm-plugins/autocomplete-plugin.js +362 -0
- package/dist/esm/pm-plugins/ghost-text-decoration.js +33 -0
- package/dist/esm/pm-plugins/scoring-pipeline.js +252 -0
- package/dist/esm/pm-plugins/slow-lane-client.js +192 -0
- package/dist/esm/pm-plugins/text-predictor.js +793 -0
- package/dist/types/autocompletePluginType.d.ts +6 -3
- package/dist/types/pm-plugins/autocomplete-plugin.d.ts +36 -0
- package/dist/types/pm-plugins/ghost-text-decoration.d.ts +7 -0
- package/dist/types/pm-plugins/scoring-pipeline.d.ts +33 -0
- package/dist/types/pm-plugins/slow-lane-client.d.ts +46 -0
- package/dist/types/pm-plugins/text-predictor.d.ts +90 -0
- package/dist/types-ts4.5/autocompletePluginType.d.ts +6 -3
- package/dist/types-ts4.5/pm-plugins/autocomplete-plugin.d.ts +36 -0
- package/dist/types-ts4.5/pm-plugins/ghost-text-decoration.d.ts +7 -0
- package/dist/types-ts4.5/pm-plugins/scoring-pipeline.d.ts +33 -0
- package/dist/types-ts4.5/pm-plugins/slow-lane-client.d.ts +46 -0
- package/dist/types-ts4.5/pm-plugins/text-predictor.d.ts +90 -0
- package/package.json +2 -2
- package/src/autocompletePlugin.tsx +25 -5
- package/src/autocompletePluginType.ts +14 -3
- package/src/pm-plugins/autocomplete-plugin/package.json +15 -0
- package/src/pm-plugins/autocomplete-plugin.ts +443 -0
- package/src/pm-plugins/data/combined_l2_l3_pos_tags.json +73571 -3
- package/src/pm-plugins/data/ghost_pos_tags.json +43 -3
- package/src/pm-plugins/data/grammar_transitions_10k.json +46 -3
- package/src/pm-plugins/data/l3_vocabulary.json +20002 -3
- package/src/pm-plugins/data/vocabulary_10k.json +38794 -3
- package/src/pm-plugins/data/word_index_10k.json +7760 -3
- package/src/pm-plugins/ghost-text-decoration.ts +44 -0
- package/src/pm-plugins/scoring-pipeline.ts +294 -0
- package/src/pm-plugins/slow-lane-client/package.json +15 -0
- package/src/pm-plugins/slow-lane-client.ts +222 -0
- package/src/pm-plugins/text-predictor/package.json +15 -0
- package/src/pm-plugins/text-predictor.ts +780 -0
- package/tsconfig.app.json +12 -3
- package/tsconfig.json +4 -1
|
@@ -0,0 +1,362 @@
|
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
2
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
3
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
4
|
+
// url: prefix is an Atlaspack/Parcel directive that resolves this file as an emitted
|
|
5
|
+
// asset URL (content-hashed). For rspack, this is handled via staticAssetsLoader.
|
|
6
|
+
// eslint-disable-next-line @repo/internal/import/no-unresolved
|
|
7
|
+
import wordVectorsUrl from 'url:./data/word-vectors_10k.bin';
|
|
8
|
+
import { SafePlugin } from '@atlaskit/editor-common/safe-plugin';
|
|
9
|
+
import { keydownHandler } from '@atlaskit/editor-prosemirror/keymap';
|
|
10
|
+
import { PluginKey } from '@atlaskit/editor-prosemirror/state';
|
|
11
|
+
import { DecorationSet } from '@atlaskit/editor-prosemirror/view';
|
|
12
|
+
import { createGhostTextDecorationSet } from './ghost-text-decoration';
|
|
13
|
+
import { createSlowLaneClient, setDefaultSlowLaneClient, isWordBoundary } from './slow-lane-client';
|
|
14
|
+
import { predict, loadDefaultVocabulary, loadVectorsAsync, incrementSessionFreq, ingestDocumentPage } from './text-predictor';
|
|
15
|
+
var SLOW_LANE_ENDPOINT = '/gateway/api/v1/autocomplete/typeahead-encodings';
|
|
16
|
+
export var autocompletePluginKey = new PluginKey('autocomplete');
|
|
17
|
+
var DEBOUNCE_MS = 150;
|
|
18
|
+
var createInitialState = function createInitialState() {
|
|
19
|
+
return {
|
|
20
|
+
ghostText: '',
|
|
21
|
+
ghostPosition: -1,
|
|
22
|
+
decorationSet: DecorationSet.empty
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Extract text content before the cursor from the current document.
|
|
28
|
+
* Returns the last ~200 characters for context.
|
|
29
|
+
*/
|
|
30
|
+
var getTextBeforeCursor = function getTextBeforeCursor(state) {
|
|
31
|
+
var $from = state.selection.$from;
|
|
32
|
+
var maxChars = 200;
|
|
33
|
+
|
|
34
|
+
// 1. Get the perfectly flattened text of the current block up to the cursor
|
|
35
|
+
var blockNode = $from.parent;
|
|
36
|
+
var offsetInBlock = $from.parentOffset;
|
|
37
|
+
var blockText = blockNode.textContent.slice(0, offsetInBlock);
|
|
38
|
+
if (blockText.length >= maxChars) {
|
|
39
|
+
return blockText.slice(-maxChars);
|
|
40
|
+
}
|
|
41
|
+
var fullText = blockText;
|
|
42
|
+
|
|
43
|
+
// 2. Walk backwards through previous blocks
|
|
44
|
+
var depth = $from.depth - 1;
|
|
45
|
+
while (fullText.length < maxChars && depth >= 0) {
|
|
46
|
+
var parentNode = $from.node(depth);
|
|
47
|
+
var indexInParent = $from.index(depth);
|
|
48
|
+
for (var i = indexInParent - 1; i >= 0 && fullText.length < maxChars; i--) {
|
|
49
|
+
var sibling = parentNode.child(i);
|
|
50
|
+
var siblingText = sibling.textContent;
|
|
51
|
+
fullText = siblingText + '\n' + fullText;
|
|
52
|
+
}
|
|
53
|
+
depth--;
|
|
54
|
+
}
|
|
55
|
+
return fullText.slice(-maxChars);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set the autocomplete state via a transaction metadata.
|
|
60
|
+
*/
|
|
61
|
+
var setAutocompleteMeta = function setAutocompleteMeta(tr, meta) {
|
|
62
|
+
return tr.setMeta(autocompletePluginKey, meta);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Apply a ghost text suggestion to the editor state.
|
|
67
|
+
*/
|
|
68
|
+
var showGhostText = function showGhostText(view, text, position) {
|
|
69
|
+
var state = view.state,
|
|
70
|
+
dispatch = view.dispatch;
|
|
71
|
+
var decorationSet = createGhostTextDecorationSet(state, position, text);
|
|
72
|
+
var tr = setAutocompleteMeta(state.tr, {
|
|
73
|
+
ghostText: text,
|
|
74
|
+
ghostPosition: position,
|
|
75
|
+
decorationSet: decorationSet
|
|
76
|
+
});
|
|
77
|
+
dispatch(tr);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Clear the current ghost text from the editor.
|
|
82
|
+
*/
|
|
83
|
+
var clearGhostText = function clearGhostText(state, dispatch) {
|
|
84
|
+
var pluginState = autocompletePluginKey.getState(state);
|
|
85
|
+
if (!pluginState || !pluginState.ghostText) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
if (dispatch) {
|
|
89
|
+
var tr = setAutocompleteMeta(state.tr, {
|
|
90
|
+
ghostText: '',
|
|
91
|
+
ghostPosition: -1,
|
|
92
|
+
decorationSet: DecorationSet.empty
|
|
93
|
+
});
|
|
94
|
+
dispatch(tr);
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Accept the current ghost text suggestion and insert it into the document.
|
|
101
|
+
*/
|
|
102
|
+
var acceptGhostText = function acceptGhostText(state, dispatch) {
|
|
103
|
+
var pluginState = autocompletePluginKey.getState(state);
|
|
104
|
+
if (!pluginState || !pluginState.ghostText) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
if (dispatch) {
|
|
108
|
+
var ghostText = pluginState.ghostText,
|
|
109
|
+
ghostPosition = pluginState.ghostPosition;
|
|
110
|
+
var tr = state.tr.insertText(ghostText, ghostPosition);
|
|
111
|
+
tr = setAutocompleteMeta(tr, {
|
|
112
|
+
ghostText: '',
|
|
113
|
+
ghostPosition: -1,
|
|
114
|
+
decorationSet: DecorationSet.empty
|
|
115
|
+
});
|
|
116
|
+
dispatch(tr);
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Context provided to the autocomplete plugin on first editor focus.
|
|
123
|
+
* Text fields are selectively ingested to boost word-frequency scoring for
|
|
124
|
+
* predictions, giving words already present in the document/thread an L1
|
|
125
|
+
* priority boost.
|
|
126
|
+
*/
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Build the text payload for the slow-lane request by prepending any available
|
|
130
|
+
* comment context ahead of the live document text. This gives the backend
|
|
131
|
+
* model richer context about the thread the user is writing in.
|
|
132
|
+
*/
|
|
133
|
+
var buildSlowLaneText = function buildSlowLaneText(docText, context) {
|
|
134
|
+
var _context$siblingComme, _context$siblingComme2, _context$siblingComme3;
|
|
135
|
+
var lines = [];
|
|
136
|
+
if (context !== null && context !== void 0 && context.parentCommentContent) {
|
|
137
|
+
lines.push("comment: ".concat(context.parentCommentContent));
|
|
138
|
+
}
|
|
139
|
+
context === null || context === void 0 || (_context$siblingComme = context.siblingCommentsContents) === null || _context$siblingComme === void 0 || _context$siblingComme.forEach(function (sibling, index) {
|
|
140
|
+
lines.push("reply ".concat(index + 1, ": ").concat(sibling));
|
|
141
|
+
});
|
|
142
|
+
var nextReplyNumber = ((_context$siblingComme2 = context === null || context === void 0 || (_context$siblingComme3 = context.siblingCommentsContents) === null || _context$siblingComme3 === void 0 ? void 0 : _context$siblingComme3.length) !== null && _context$siblingComme2 !== void 0 ? _context$siblingComme2 : 0) + 1;
|
|
143
|
+
lines.push("reply ".concat(nextReplyNumber, ": ").concat(docText));
|
|
144
|
+
return lines.join('\n');
|
|
145
|
+
};
|
|
146
|
+
export var createAutocompletePlugin = function createAutocompletePlugin(options) {
|
|
147
|
+
var debounceTimer = null;
|
|
148
|
+
var hasIngestedPage = false;
|
|
149
|
+
var resolvedContext;
|
|
150
|
+
/**
|
|
151
|
+
* Set after accepting a suggestion so the next doc-change update
|
|
152
|
+
* skips scheduling a new prediction for the just-inserted text.
|
|
153
|
+
* Scoped to the factory so multiple editor instances don't share state.
|
|
154
|
+
*/
|
|
155
|
+
var justAccepted = false;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Stores the text-before-cursor snapshot at the moment the user dismissed
|
|
159
|
+
* a suggestion via Escape. While the context remains identical, we suppress
|
|
160
|
+
* re-showing the same suggestion. Resets to null as soon as the text changes.
|
|
161
|
+
*/
|
|
162
|
+
var dismissedContext = null;
|
|
163
|
+
var slowLaneClient = createSlowLaneClient({
|
|
164
|
+
baseUrl: '',
|
|
165
|
+
endpoint: SLOW_LANE_ENDPOINT
|
|
166
|
+
});
|
|
167
|
+
setDefaultSlowLaneClient(slowLaneClient);
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Schedule a prediction after a short debounce.
|
|
171
|
+
* Tier 1 predictions are synchronous (<0.1ms) but we still debounce
|
|
172
|
+
* to avoid unnecessary work on rapid keystrokes.
|
|
173
|
+
*/
|
|
174
|
+
var schedulePrediction = function schedulePrediction(view) {
|
|
175
|
+
if (debounceTimer) {
|
|
176
|
+
clearTimeout(debounceTimer);
|
|
177
|
+
}
|
|
178
|
+
debounceTimer = setTimeout(function () {
|
|
179
|
+
var state = view.state;
|
|
180
|
+
var selection = state.selection;
|
|
181
|
+
|
|
182
|
+
// Only predict for cursor selections (not range selections)
|
|
183
|
+
if (!selection.empty) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
var textBefore = getTextBeforeCursor(state);
|
|
187
|
+
|
|
188
|
+
// Suppress re-showing the same suggestion the user just dismissed.
|
|
189
|
+
// Once the text context changes (user types or deletes), this clears automatically.
|
|
190
|
+
if (textBefore === dismissedContext) {
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
dismissedContext = null;
|
|
194
|
+
|
|
195
|
+
// Don't predict if there's not enough context
|
|
196
|
+
if (textBefore.trim().length < 3) {
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Tier 1 prediction is synchronous -- no async needed
|
|
201
|
+
var prediction = predict(textBefore);
|
|
202
|
+
if (prediction && prediction.length > 0) {
|
|
203
|
+
showGhostText(view, prediction, selection.from);
|
|
204
|
+
}
|
|
205
|
+
}, DEBOUNCE_MS);
|
|
206
|
+
};
|
|
207
|
+
var maybeUpdateSessionFrequency = function maybeUpdateSessionFrequency(view, prevState) {
|
|
208
|
+
var newText = getTextBeforeCursor(view.state);
|
|
209
|
+
var prevText = getTextBeforeCursor(prevState);
|
|
210
|
+
if (newText.length <= prevText.length) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
var lastChar = newText[newText.length - 1];
|
|
214
|
+
// eslint-disable-next-line require-unicode-regexp
|
|
215
|
+
if (!/[\s.,;:!?]/.test(lastChar)) {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Only fire if the previous state did not already end on a boundary,
|
|
220
|
+
// so we don't double-count when multiple boundary chars are inserted.
|
|
221
|
+
var prevLastChar = prevText[prevText.length - 1];
|
|
222
|
+
// eslint-disable-next-line require-unicode-regexp
|
|
223
|
+
if (prevLastChar && /[\s.,;:!?]/.test(prevLastChar)) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
var beforeBoundary = newText.slice(0, -1).trimEnd();
|
|
227
|
+
var lastSpaceIdx = beforeBoundary.lastIndexOf(' ');
|
|
228
|
+
var completedWord = beforeBoundary.slice(lastSpaceIdx + 1).toLowerCase();
|
|
229
|
+
if (completedWord.length >= 2) {
|
|
230
|
+
incrementSessionFreq(completedWord);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
return new SafePlugin({
|
|
234
|
+
key: autocompletePluginKey,
|
|
235
|
+
state: {
|
|
236
|
+
init: function init() {
|
|
237
|
+
return createInitialState();
|
|
238
|
+
},
|
|
239
|
+
apply: function apply(tr, pluginState) {
|
|
240
|
+
var meta = tr.getMeta(autocompletePluginKey);
|
|
241
|
+
if (meta) {
|
|
242
|
+
return _objectSpread(_objectSpread({}, pluginState), meta);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// If the document changed, clear the ghost text
|
|
246
|
+
// (new prediction will be scheduled from view.update)
|
|
247
|
+
if (tr.docChanged) {
|
|
248
|
+
return _objectSpread(_objectSpread({}, pluginState), {}, {
|
|
249
|
+
ghostText: '',
|
|
250
|
+
ghostPosition: -1,
|
|
251
|
+
decorationSet: DecorationSet.empty
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// If selection changed without doc change, clear ghost text
|
|
256
|
+
if (tr.selectionSet && pluginState.ghostText) {
|
|
257
|
+
return _objectSpread(_objectSpread({}, pluginState), {}, {
|
|
258
|
+
ghostText: '',
|
|
259
|
+
ghostPosition: -1,
|
|
260
|
+
decorationSet: DecorationSet.empty
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
return pluginState;
|
|
264
|
+
}
|
|
265
|
+
},
|
|
266
|
+
props: {
|
|
267
|
+
decorations: function decorations(state) {
|
|
268
|
+
var _pluginState$decorati;
|
|
269
|
+
var pluginState = autocompletePluginKey.getState(state);
|
|
270
|
+
return (_pluginState$decorati = pluginState === null || pluginState === void 0 ? void 0 : pluginState.decorationSet) !== null && _pluginState$decorati !== void 0 ? _pluginState$decorati : DecorationSet.empty;
|
|
271
|
+
},
|
|
272
|
+
handleKeyDown: keydownHandler({
|
|
273
|
+
Tab: function Tab(state, dispatch) {
|
|
274
|
+
var accepted = acceptGhostText(state, dispatch);
|
|
275
|
+
if (accepted) justAccepted = true;
|
|
276
|
+
return accepted;
|
|
277
|
+
},
|
|
278
|
+
ArrowRight: function ArrowRight(state, dispatch) {
|
|
279
|
+
var accepted = acceptGhostText(state, dispatch);
|
|
280
|
+
if (accepted) justAccepted = true;
|
|
281
|
+
return accepted;
|
|
282
|
+
},
|
|
283
|
+
Escape: function Escape(state, dispatch) {
|
|
284
|
+
var didClear = clearGhostText(state, dispatch);
|
|
285
|
+
if (didClear) {
|
|
286
|
+
dismissedContext = getTextBeforeCursor(state);
|
|
287
|
+
}
|
|
288
|
+
return didClear;
|
|
289
|
+
}
|
|
290
|
+
}),
|
|
291
|
+
handleDOMEvents: {
|
|
292
|
+
blur: function blur(view) {
|
|
293
|
+
var pluginState = autocompletePluginKey.getState(view.state);
|
|
294
|
+
if (pluginState !== null && pluginState !== void 0 && pluginState.ghostText) {
|
|
295
|
+
clearGhostText(view.state, view.dispatch);
|
|
296
|
+
}
|
|
297
|
+
return false;
|
|
298
|
+
},
|
|
299
|
+
focus: function focus() {
|
|
300
|
+
loadDefaultVocabulary();
|
|
301
|
+
loadVectorsAsync({
|
|
302
|
+
vectorsUrl: wordVectorsUrl
|
|
303
|
+
}).catch(function () {});
|
|
304
|
+
if (!hasIngestedPage) {
|
|
305
|
+
hasIngestedPage = true;
|
|
306
|
+
if (options !== null && options !== void 0 && options.getContext) {
|
|
307
|
+
options.getContext().then(function (context) {
|
|
308
|
+
var _context$siblingComme4;
|
|
309
|
+
if (!context) {
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
resolvedContext = context;
|
|
313
|
+
if (context.fullPageContent) {
|
|
314
|
+
ingestDocumentPage(context.fullPageContent);
|
|
315
|
+
}
|
|
316
|
+
if (context.parentCommentContent) {
|
|
317
|
+
ingestDocumentPage(context.parentCommentContent);
|
|
318
|
+
}
|
|
319
|
+
(_context$siblingComme4 = context.siblingCommentsContents) === null || _context$siblingComme4 === void 0 || _context$siblingComme4.forEach(ingestDocumentPage);
|
|
320
|
+
}).catch(function () {});
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
return false;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
},
|
|
327
|
+
view: function view() {
|
|
328
|
+
return {
|
|
329
|
+
update: function update(view, prevState) {
|
|
330
|
+
if (!prevState.doc.eq(view.state.doc)) {
|
|
331
|
+
if (justAccepted) {
|
|
332
|
+
justAccepted = false;
|
|
333
|
+
|
|
334
|
+
// ✨ THE FIX: Memorize the text state right after acceptance.
|
|
335
|
+
// Any follow-up transactions will hit the 'dismissedContext'
|
|
336
|
+
// block and abort until the user actually types a new character!
|
|
337
|
+
dismissedContext = getTextBeforeCursor(view.state);
|
|
338
|
+
|
|
339
|
+
// Also clear any pending debounce timers from before the acceptance
|
|
340
|
+
if (debounceTimer) {
|
|
341
|
+
clearTimeout(debounceTimer);
|
|
342
|
+
}
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
maybeUpdateSessionFrequency(view, prevState);
|
|
346
|
+
var textBefore = getTextBeforeCursor(view.state);
|
|
347
|
+
if (isWordBoundary(textBefore)) {
|
|
348
|
+
slowLaneClient.updateContext(buildSlowLaneText(view.state.doc.textContent, resolvedContext));
|
|
349
|
+
}
|
|
350
|
+
schedulePrediction(view);
|
|
351
|
+
}
|
|
352
|
+
},
|
|
353
|
+
destroy: function destroy() {
|
|
354
|
+
if (debounceTimer) {
|
|
355
|
+
clearTimeout(debounceTimer);
|
|
356
|
+
}
|
|
357
|
+
setDefaultSlowLaneClient(null);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
});
|
|
362
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Decoration, DecorationSet } from '@atlaskit/editor-prosemirror/view';
|
|
2
|
+
var GHOST_TEXT_CLASS = 'autocomplete-ghost-text';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Creates a DecorationSet containing a ghost text widget at the given position.
|
|
6
|
+
* The ghost text is rendered as a styled <span> that appears after the cursor.
|
|
7
|
+
*/
|
|
8
|
+
export var createGhostTextDecorationSet = function createGhostTextDecorationSet(state, position, text) {
|
|
9
|
+
if (!text) {
|
|
10
|
+
return DecorationSet.empty;
|
|
11
|
+
}
|
|
12
|
+
var decoration = Decoration.widget(position, function () {
|
|
13
|
+
var container = document.createElement('span');
|
|
14
|
+
container.className = GHOST_TEXT_CLASS;
|
|
15
|
+
container.setAttribute('data-autocomplete-ghost', 'true');
|
|
16
|
+
container.style.color = '#999';
|
|
17
|
+
container.style.opacity = '0.6';
|
|
18
|
+
container.style.pointerEvents = 'none';
|
|
19
|
+
container.style.userSelect = 'none';
|
|
20
|
+
container.style.fontStyle = 'italic';
|
|
21
|
+
// U+200B (Zero Width Space) gives the browser a line-break opportunity
|
|
22
|
+
// immediately before the ghost text. This ensures the typed text before
|
|
23
|
+
// the span is never pushed to the next line by the ghost text's width —
|
|
24
|
+
// only the ghost text itself will wrap if it doesn't fit.
|
|
25
|
+
container.textContent = "\u200B" + text;
|
|
26
|
+
return container;
|
|
27
|
+
}, {
|
|
28
|
+
side: 1,
|
|
29
|
+
// Render after content at this position
|
|
30
|
+
key: 'autocomplete-ghost-text'
|
|
31
|
+
});
|
|
32
|
+
return DecorationSet.create(state.doc, [decoration]);
|
|
33
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
|
|
2
|
+
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
3
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
4
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
5
|
+
/**
|
|
6
|
+
* Scoring Pipeline: Stage 1 (Semantic + Frequency), Grammar Filter, Stage 2 (LM Re-ranking).
|
|
7
|
+
*
|
|
8
|
+
* Operates synchronously on pre-loaded data. Each stage gracefully degrades
|
|
9
|
+
* when its required data isn't available (cold → warm → full warm).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import posTagsData from './data/combined_l2_l3_pos_tags.json';
|
|
13
|
+
import ghostPosTagsData from './data/ghost_pos_tags.json';
|
|
14
|
+
import grammarTransitionsData from './data/grammar_transitions_10k.json';
|
|
15
|
+
|
|
16
|
+
// ─── Types ──────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
/** Metadata returned by the grammar filter for debug logging in the caller. */
|
|
19
|
+
|
|
20
|
+
// ─── Scoring Constants ──────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
var ALPHA = 0.5;
|
|
23
|
+
var BETA = 0.5;
|
|
24
|
+
var NEUTRAL_SCORE = 0.5;
|
|
25
|
+
var STAGE1_WEIGHT = 0.6;
|
|
26
|
+
var STAGE2_WEIGHT = 0.4;
|
|
27
|
+
var MIN_STAGE1_SCORE = 0.35;
|
|
28
|
+
var L1_SESSION_CAP = 1.2;
|
|
29
|
+
|
|
30
|
+
// ─── Grammar Data (loaded once on import) ───────────────────
|
|
31
|
+
|
|
32
|
+
var posTags = new Map(Object.entries(posTagsData));
|
|
33
|
+
var grammarTransitions = grammarTransitionsData;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Precomputed map from each POS tag to the set of allowed next POS tags.
|
|
37
|
+
* Built once at module load from grammarTransitions so applyGrammarFilter
|
|
38
|
+
* never re-iterates the transition rules per call.
|
|
39
|
+
*/
|
|
40
|
+
var precomputedAllowedByPos = new Map(Object.entries(grammarTransitions.transitions).map(function (_ref) {
|
|
41
|
+
var _ref2 = _slicedToArray(_ref, 2),
|
|
42
|
+
pos = _ref2[0],
|
|
43
|
+
rule = _ref2[1];
|
|
44
|
+
return [pos, new Set(rule.allowed)];
|
|
45
|
+
}));
|
|
46
|
+
|
|
47
|
+
// ─── Math ───────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
function cosineSimilarity(a, b) {
|
|
50
|
+
var dot = 0;
|
|
51
|
+
var normA = 0;
|
|
52
|
+
var normB = 0;
|
|
53
|
+
for (var i = 0; i < a.length; i++) {
|
|
54
|
+
dot += a[i] * b[i];
|
|
55
|
+
normA += a[i] * a[i];
|
|
56
|
+
normB += b[i] * b[i];
|
|
57
|
+
}
|
|
58
|
+
var dNormA = Math.sqrt(normA);
|
|
59
|
+
var dNormB = Math.sqrt(normB);
|
|
60
|
+
if (dNormA === 0 || dNormB === 0) {
|
|
61
|
+
return NEUTRAL_SCORE;
|
|
62
|
+
}
|
|
63
|
+
return (1 + dot / (dNormA * dNormB)) / 2;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// ─── Stage 1: Semantic + Frequency ──────────────────────────
|
|
67
|
+
|
|
68
|
+
function scoreStage1(candidate, contextVector, getWordVector, maxTenantFreq) {
|
|
69
|
+
// 1. Calculate Base Global Score (Normalized Log)
|
|
70
|
+
var maxPossibleLog = Math.log10(maxTenantFreq + 1);
|
|
71
|
+
|
|
72
|
+
// Diversity Adjustment
|
|
73
|
+
var diversityRaw = (Math.log10(candidate.tenantFreq + 1) * 0.50 + Math.log10(candidate.docFreq + 1) * 0.25 + Math.log10(candidate.authorFreq + 1) * 0.25) / maxPossibleLog;
|
|
74
|
+
var sessionMultiplier = candidate.sessionFreq > 0 ? 1 + Math.log10(candidate.sessionFreq + 1) * 2.5 : 1;
|
|
75
|
+
|
|
76
|
+
// Apply multiplier; capped at L1_SESSION_CAP (default 1.2) to prevent excessive over-indexing
|
|
77
|
+
var freqScore = Math.min(diversityRaw * sessionMultiplier, L1_SESSION_CAP);
|
|
78
|
+
|
|
79
|
+
// 3. Semantic Scoring
|
|
80
|
+
var semanticScore = NEUTRAL_SCORE;
|
|
81
|
+
if (contextVector) {
|
|
82
|
+
var wordVec = getWordVector(candidate.word);
|
|
83
|
+
semanticScore = wordVec ? cosineSimilarity(contextVector, wordVec) : NEUTRAL_SCORE;
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
semanticScore: semanticScore,
|
|
87
|
+
freqScore: freqScore,
|
|
88
|
+
stage1Score: ALPHA * semanticScore + BETA * freqScore
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─── Grammar Filter ─────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* GHOST POS DICTIONARY
|
|
96
|
+
* A hardcoded mapping of common structural English words that were stripped
|
|
97
|
+
* from the main domain vocabulary. This allows the grammar filter to understand
|
|
98
|
+
* context without suggesting these words to the user.
|
|
99
|
+
*/
|
|
100
|
+
var ghostPosTags = ghostPosTagsData;
|
|
101
|
+
function applyGrammarFilter(candidates, previousWord) {
|
|
102
|
+
if (!previousWord) return {
|
|
103
|
+
filtered: candidates,
|
|
104
|
+
grammarMeta: null
|
|
105
|
+
};
|
|
106
|
+
var lowerPrev = previousWord.toLowerCase();
|
|
107
|
+
var prevTags = ghostPosTags[lowerPrev] || posTags.get(lowerPrev);
|
|
108
|
+
if (!prevTags || prevTags.length === 0) {
|
|
109
|
+
return {
|
|
110
|
+
filtered: candidates,
|
|
111
|
+
grammarMeta: null
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
var allowedNextTags;
|
|
115
|
+
if (prevTags.length === 1) {
|
|
116
|
+
var _precomputedAllowedBy;
|
|
117
|
+
// Common case: single POS tag — reuse the precomputed Set directly (no allocation)
|
|
118
|
+
allowedNextTags = (_precomputedAllowedBy = precomputedAllowedByPos.get(prevTags[0])) !== null && _precomputedAllowedBy !== void 0 ? _precomputedAllowedBy : new Set();
|
|
119
|
+
} else {
|
|
120
|
+
allowedNextTags = new Set();
|
|
121
|
+
var _iterator = _createForOfIteratorHelper(prevTags),
|
|
122
|
+
_step;
|
|
123
|
+
try {
|
|
124
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
125
|
+
var pt = _step.value;
|
|
126
|
+
var allowed = precomputedAllowedByPos.get(pt);
|
|
127
|
+
if (allowed) allowed.forEach(function (tag) {
|
|
128
|
+
return allowedNextTags.add(tag);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
} catch (err) {
|
|
132
|
+
_iterator.e(err);
|
|
133
|
+
} finally {
|
|
134
|
+
_iterator.f();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
var filtered = [];
|
|
138
|
+
var dropped = [];
|
|
139
|
+
var _iterator2 = _createForOfIteratorHelper(candidates),
|
|
140
|
+
_step2;
|
|
141
|
+
try {
|
|
142
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
143
|
+
var entry = _step2.value;
|
|
144
|
+
var candidateTags = posTags.get(entry.candidate.word.toLowerCase());
|
|
145
|
+
|
|
146
|
+
// If candidate has no tags (unknown word), let it pass to be safe
|
|
147
|
+
if (!candidateTags || candidateTags.length === 0) {
|
|
148
|
+
filtered.push(entry);
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
if (candidateTags.some(function (ct) {
|
|
152
|
+
return allowedNextTags.has(ct);
|
|
153
|
+
})) {
|
|
154
|
+
filtered.push(entry);
|
|
155
|
+
} else {
|
|
156
|
+
dropped.push(entry.candidate.word);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} catch (err) {
|
|
160
|
+
_iterator2.e(err);
|
|
161
|
+
} finally {
|
|
162
|
+
_iterator2.f();
|
|
163
|
+
}
|
|
164
|
+
var finalFiltered = filtered.length > 0 ? filtered : candidates;
|
|
165
|
+
return {
|
|
166
|
+
filtered: finalFiltered,
|
|
167
|
+
grammarMeta: {
|
|
168
|
+
prevWord: lowerPrev,
|
|
169
|
+
prevTags: prevTags,
|
|
170
|
+
before: candidates.length,
|
|
171
|
+
after: finalFiltered.length,
|
|
172
|
+
dropped: filtered.length > 0 ? dropped : []
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ─── Stage 2: LM Re-ranking ────────────────────────────────
|
|
178
|
+
function getLmScore(word, lmLogits) {
|
|
179
|
+
if (!lmLogits) return 0;
|
|
180
|
+
|
|
181
|
+
// Look up the word directly! No more tokens.
|
|
182
|
+
var val = lmLogits[word.toLowerCase()];
|
|
183
|
+
if (typeof val === 'number') {
|
|
184
|
+
return val;
|
|
185
|
+
}
|
|
186
|
+
return 0;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ─── Public API ─────────────────────────────────────────────
|
|
190
|
+
|
|
191
|
+
export function rankCandidates(candidates, contextVector, getWordVector, lmLogits, maxTenantFreq, previousWord) {
|
|
192
|
+
// Stage 1
|
|
193
|
+
var stage1Results = candidates.map(function (candidate) {
|
|
194
|
+
var _scoreStage = scoreStage1(candidate, contextVector, getWordVector, maxTenantFreq),
|
|
195
|
+
semanticScore = _scoreStage.semanticScore,
|
|
196
|
+
freqScore = _scoreStage.freqScore,
|
|
197
|
+
stage1Score = _scoreStage.stage1Score;
|
|
198
|
+
return {
|
|
199
|
+
candidate: candidate,
|
|
200
|
+
semanticScore: semanticScore,
|
|
201
|
+
freqScore: freqScore,
|
|
202
|
+
stage1Score: stage1Score
|
|
203
|
+
};
|
|
204
|
+
});
|
|
205
|
+
var stage1Survivors = stage1Results.filter(function (entry) {
|
|
206
|
+
return entry.stage1Score >= MIN_STAGE1_SCORE;
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
// Grammar Filter
|
|
210
|
+
var _applyGrammarFilter = applyGrammarFilter(stage1Survivors, previousWord),
|
|
211
|
+
filtered = _applyGrammarFilter.filtered,
|
|
212
|
+
grammarMeta = _applyGrammarFilter.grammarMeta;
|
|
213
|
+
|
|
214
|
+
// Stage 2 + final assembly
|
|
215
|
+
var lmMax = 0;
|
|
216
|
+
if (lmLogits && Object.keys(lmLogits).length > 0) {
|
|
217
|
+
var values = Object.values(lmLogits);
|
|
218
|
+
lmMax = Math.max.apply(Math, values);
|
|
219
|
+
}
|
|
220
|
+
var scored = filtered.map(function (entry) {
|
|
221
|
+
var lmScore = 0;
|
|
222
|
+
var finalScore = entry.stage1Score;
|
|
223
|
+
if (lmLogits && Object.keys(lmLogits).length > 0) {
|
|
224
|
+
var rawLm = getLmScore(entry.candidate.word, lmLogits);
|
|
225
|
+
if (rawLm !== 0) {
|
|
226
|
+
// The word was in the top_k! Score it normally.
|
|
227
|
+
var logitDiff = Math.log(rawLm) - Math.log(lmMax);
|
|
228
|
+
lmScore = Math.exp(logitDiff);
|
|
229
|
+
} else {
|
|
230
|
+
lmScore = 0.05;
|
|
231
|
+
}
|
|
232
|
+
finalScore = STAGE1_WEIGHT * entry.stage1Score + STAGE2_WEIGHT * lmScore;
|
|
233
|
+
}
|
|
234
|
+
return {
|
|
235
|
+
word: entry.candidate.word,
|
|
236
|
+
freqScore: entry.freqScore,
|
|
237
|
+
semanticScore: entry.semanticScore,
|
|
238
|
+
lmScore: lmScore,
|
|
239
|
+
finalScore: finalScore
|
|
240
|
+
};
|
|
241
|
+
});
|
|
242
|
+
scored.sort(function (a, b) {
|
|
243
|
+
if (b.finalScore !== a.finalScore) {
|
|
244
|
+
return b.finalScore - a.finalScore;
|
|
245
|
+
}
|
|
246
|
+
return a.word.length - b.word.length;
|
|
247
|
+
});
|
|
248
|
+
return {
|
|
249
|
+
candidates: scored,
|
|
250
|
+
grammarMeta: grammarMeta
|
|
251
|
+
};
|
|
252
|
+
}
|