@atlaskit/editor-plugin-autocomplete 8.0.0 → 9.1.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +108 -0
  2. package/dist/cjs/analytics/ufo.js +2 -1
  3. package/dist/cjs/pm-plugins/artifact-loader.js +135 -0
  4. package/dist/cjs/pm-plugins/artifacts-manifest.js +3 -0
  5. package/dist/cjs/pm-plugins/autocomplete-plugin.js +498 -160
  6. package/dist/cjs/pm-plugins/canonical-lm-scoring.js +371 -0
  7. package/dist/cjs/pm-plugins/debug-mode.js +105 -7
  8. package/dist/cjs/pm-plugins/ghost-text-decoration.js +12 -3
  9. package/dist/cjs/pm-plugins/local-slow-lane-client.js +1202 -453
  10. package/dist/cjs/pm-plugins/scoring-pipeline.js +164 -19
  11. package/dist/cjs/pm-plugins/slow-lane-client.js +100 -37
  12. package/dist/cjs/pm-plugins/text-predictor.js +2099 -309
  13. package/dist/es2019/analytics/ufo.js +2 -0
  14. package/dist/es2019/pm-plugins/artifact-loader.js +70 -0
  15. package/dist/es2019/pm-plugins/artifacts-manifest.js +3 -0
  16. package/dist/es2019/pm-plugins/autocomplete-plugin.js +489 -152
  17. package/dist/es2019/pm-plugins/canonical-lm-scoring.js +270 -0
  18. package/dist/es2019/pm-plugins/debug-mode.js +102 -7
  19. package/dist/es2019/pm-plugins/ghost-text-decoration.js +12 -3
  20. package/dist/es2019/pm-plugins/local-slow-lane-client.js +940 -326
  21. package/dist/es2019/pm-plugins/scoring-pipeline.js +137 -13
  22. package/dist/es2019/pm-plugins/slow-lane-client.js +94 -37
  23. package/dist/es2019/pm-plugins/text-predictor.js +1692 -200
  24. package/dist/esm/analytics/ufo.js +2 -1
  25. package/dist/esm/pm-plugins/artifact-loader.js +128 -0
  26. package/dist/esm/pm-plugins/artifacts-manifest.js +3 -0
  27. package/dist/esm/pm-plugins/autocomplete-plugin.js +501 -163
  28. package/dist/esm/pm-plugins/canonical-lm-scoring.js +364 -0
  29. package/dist/esm/pm-plugins/debug-mode.js +104 -6
  30. package/dist/esm/pm-plugins/ghost-text-decoration.js +12 -3
  31. package/dist/esm/pm-plugins/local-slow-lane-client.js +1200 -451
  32. package/dist/esm/pm-plugins/scoring-pipeline.js +163 -18
  33. package/dist/esm/pm-plugins/slow-lane-client.js +100 -37
  34. package/dist/esm/pm-plugins/text-predictor.js +2101 -313
  35. package/dist/types/analytics/ufo.d.ts +1 -0
  36. package/dist/types/pm-plugins/artifact-loader.d.ts +44 -0
  37. package/dist/types/pm-plugins/artifacts-manifest.d.ts +3 -0
  38. package/dist/types/pm-plugins/autocomplete-plugin.d.ts +39 -0
  39. package/dist/types/pm-plugins/canonical-lm-scoring.d.ts +158 -0
  40. package/dist/types/pm-plugins/debug-mode.d.ts +55 -1
  41. package/dist/types/pm-plugins/ghost-text-decoration.d.ts +5 -2
  42. package/dist/types/pm-plugins/local-slow-lane-client.d.ts +87 -54
  43. package/dist/types/pm-plugins/scoring-pipeline.d.ts +33 -1
  44. package/dist/types/pm-plugins/slow-lane-client.d.ts +51 -3
  45. package/dist/types/pm-plugins/text-predictor.d.ts +156 -3
  46. package/package.json +2 -2
  47. package/src/analytics/ufo.ts +5 -0
  48. package/src/pm-plugins/artifact-loader.ts +92 -0
  49. package/src/pm-plugins/artifacts-manifest.ts +3 -0
  50. package/src/pm-plugins/autocomplete-plugin.ts +584 -154
  51. package/src/pm-plugins/canonical-lm-scoring.ts +412 -0
  52. package/src/pm-plugins/debug-mode.ts +134 -7
  53. package/src/pm-plugins/ghost-text-decoration.ts +13 -4
  54. package/src/pm-plugins/local-slow-lane-client.ts +1302 -386
  55. package/src/pm-plugins/scoring-pipeline.ts +167 -17
  56. package/src/pm-plugins/slow-lane-client.ts +147 -62
  57. package/src/pm-plugins/text-predictor.ts +2119 -254
  58. package/CANONICAL_FIX__DO_NOT_USE_ME_A/package.json +0 -8
  59. package/CANONICAL_FIX__DO_NOT_USE_ME_B/package.json +0 -8
  60. package/CANONICAL_FIX__DO_NOT_USE_ME_C/package.json +0 -8
  61. package/dist/cjs/entry-points/src-pm-plugins-autocomplete-plugin.js +0 -18
  62. package/dist/cjs/entry-points/src-pm-plugins-slow-lane-client.js +0 -36
  63. package/dist/cjs/entry-points/src-pm-plugins-text-predictor.js +0 -66
  64. package/dist/es2019/entry-points/src-pm-plugins-autocomplete-plugin.js +0 -2
  65. package/dist/es2019/entry-points/src-pm-plugins-slow-lane-client.js +0 -2
  66. package/dist/es2019/entry-points/src-pm-plugins-text-predictor.js +0 -2
  67. package/dist/esm/entry-points/src-pm-plugins-autocomplete-plugin.js +0 -2
  68. package/dist/esm/entry-points/src-pm-plugins-slow-lane-client.js +0 -2
  69. package/dist/esm/entry-points/src-pm-plugins-text-predictor.js +0 -2
  70. package/dist/types/entry-points/src-pm-plugins-autocomplete-plugin.d.ts +0 -2
  71. package/dist/types/entry-points/src-pm-plugins-slow-lane-client.d.ts +0 -2
  72. package/dist/types/entry-points/src-pm-plugins-text-predictor.d.ts +0 -2
  73. package/scripts/gen_first_token_to_words.py +0 -170
  74. package/src/entry-points/src-pm-plugins-autocomplete-plugin.ts +0 -7
  75. package/src/entry-points/src-pm-plugins-slow-lane-client.ts +0 -13
  76. package/src/entry-points/src-pm-plugins-text-predictor.ts +0 -14
  77. package/src/pm-plugins/autocomplete-plugin/package.json +0 -8
  78. package/src/pm-plugins/slow-lane-client/package.json +0 -8
  79. package/src/pm-plugins/text-predictor/package.json +0 -8
@@ -0,0 +1,270 @@
1
+ import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
+ const createTokenTrieNode = () => ({
3
+ children: new Map()
4
+ });
5
+
6
+ /**
7
+ * Token trie for the producer's canonical leading-space token sequences.
8
+ * It lets the scorer expand a shared token prefix once for every surface below
9
+ * that node (`root cause`, `root directory`, ...), rather than guessing two
10
+ * arbitrary full surfaces from a broad one-character string prefix.
11
+ */
12
+ export class CanonicalSurfaceTokenTrie {
13
+ constructor(entries = []) {
14
+ _defineProperty(this, "root", createTokenTrieNode());
15
+ _defineProperty(this, "tokenIdsBySurface", new Map());
16
+ for (const [surface, tokenIds] of entries) {
17
+ this.insert(surface, tokenIds);
18
+ }
19
+ }
20
+ insert(surface, tokenIds) {
21
+ if (tokenIds.length === 0) {
22
+ return;
23
+ }
24
+ const normalizedSurface = surface.toLowerCase();
25
+ this.tokenIdsBySurface.set(normalizedSurface, [...tokenIds]);
26
+ let node = this.root;
27
+ for (const tokenId of tokenIds) {
28
+ let child = node.children.get(tokenId);
29
+ if (!child) {
30
+ child = createTokenTrieNode();
31
+ node.children.set(tokenId, child);
32
+ }
33
+ node = child;
34
+ }
35
+ }
36
+ getTokenIds(surface) {
37
+ const tokenIds = this.tokenIdsBySurface.get(surface.toLowerCase());
38
+ return tokenIds ? [...tokenIds] : null;
39
+ }
40
+ groupByScoredPrefix(candidates) {
41
+ const groups = new Map();
42
+ for (const candidate of candidates) {
43
+ var _groups$get;
44
+ if (candidate.scoredTokenCount <= 0 || candidate.scoredTokenCount >= candidate.tokenIds.length) {
45
+ continue;
46
+ }
47
+ const tokenPrefix = candidate.tokenIds.slice(0, candidate.scoredTokenCount);
48
+ let node = this.root;
49
+ for (const tokenId of tokenPrefix) {
50
+ node = node.children.get(tokenId);
51
+ if (!node) {
52
+ break;
53
+ }
54
+ }
55
+ if (!node) {
56
+ continue;
57
+ }
58
+ const key = tokenPrefix.join(',');
59
+ const group = (_groups$get = groups.get(key)) !== null && _groups$get !== void 0 ? _groups$get : {
60
+ surfaces: [],
61
+ tokenPrefix
62
+ };
63
+ group.surfaces.push(candidate.surface);
64
+ groups.set(key, group);
65
+ }
66
+ return Array.from(groups.values());
67
+ }
68
+ }
69
+ const WHITESPACE_REGEX = /\s/u;
70
+ const makeContextKey = (prompt, separatorKind) => `${separatorKind}\u0000${prompt}`;
71
+
72
+ /** The part of a candidate's context that depends only on where its surface starts. */
73
+
74
+ /**
75
+ * Per-pass store for the position-derived half of a candidate's context.
76
+ *
77
+ * Every unigram candidate for a keystroke shares one surface start, and the
78
+ * phrase candidates share a handful more, so without this the same slice,
79
+ * `trimEnd` and context key are rebuilt for each of the couple of hundred
80
+ * candidates. Valid only for a single `textBeforeCursor`.
81
+ */
82
+
83
+ export const createCanonicalContextPositionCache = () => new Map();
84
+
85
+ /**
86
+ * Derive the causal prompt and artifact eligibility from the exact, untrimmed
87
+ * pre-cursor text. The shipped artifact contains only tokenizations of
88
+ * `" " + surface`, so it is valid only when the surface is actually preceded
89
+ * by whitespace.
90
+ */
91
+ export const deriveCanonicalCandidateContext = (textBeforeCursor, matchedPrefixLen, surface, getTokenIds, surfaceStartOverride, positionCache) => {
92
+ const derivedSurfaceStart = textBeforeCursor.length - matchedPrefixLen;
93
+ const surfaceStart = Math.max(0, Math.min(textBeforeCursor.length, surfaceStartOverride !== null && surfaceStartOverride !== void 0 ? surfaceStartOverride : derivedSurfaceStart));
94
+ let position = positionCache === null || positionCache === void 0 ? void 0 : positionCache.get(surfaceStart);
95
+ if (position === undefined) {
96
+ const precedingChar = surfaceStart > 0 ? textBeforeCursor[surfaceStart - 1] : null;
97
+ const separatorKind = precedingChar === null ? 'document-start' : WHITESPACE_REGEX.test(precedingChar) ? 'whitespace' : 'non-space';
98
+ const contextBeforeSurface = textBeforeCursor.slice(0, surfaceStart).trimEnd();
99
+ position = {
100
+ contextBeforeSurface,
101
+ contextKey: makeContextKey(contextBeforeSurface, separatorKind),
102
+ separatorKind,
103
+ surfaceStart
104
+ };
105
+ positionCache === null || positionCache === void 0 ? void 0 : positionCache.set(surfaceStart, position);
106
+ }
107
+ return {
108
+ ...position,
109
+ canonicalTokenIds: position.separatorKind === 'whitespace' ? getTokenIds(surface) : null
110
+ };
111
+ };
112
+
113
+ /**
114
+ * Derive the next surface's leading-space context at an actual word boundary.
115
+ * This lets the local LM start its one-token prime on the space keystroke,
116
+ * before the user has typed the first character of the next word.
117
+ */
118
+ export const deriveWhitespaceBoundaryContext = textBeforeCursor => {
119
+ var _textBeforeCursor$at;
120
+ if (textBeforeCursor.length === 0 || !WHITESPACE_REGEX.test((_textBeforeCursor$at = textBeforeCursor.at(-1)) !== null && _textBeforeCursor$at !== void 0 ? _textBeforeCursor$at : '')) {
121
+ return null;
122
+ }
123
+ const prompt = textBeforeCursor.trimEnd();
124
+ if (prompt.length === 0) {
125
+ return null;
126
+ }
127
+ return {
128
+ contextKey: makeContextKey(prompt, 'whitespace'),
129
+ prompt
130
+ };
131
+ };
132
+
133
+ /**
134
+ * Return one prime per exact context, ordered from the latest surface start
135
+ * (normally the unigram window) to wider phrase windows.
136
+ */
137
+ export const selectBoundaryPrimeRequests = (familyKey, candidates, maxPrimes) => {
138
+ const byContext = new Map();
139
+ for (const candidate of candidates) {
140
+ if (candidate.canonicalTokenIds === null) {
141
+ continue;
142
+ }
143
+ const current = byContext.get(candidate.contextKey);
144
+ if (!current || candidate.surfaceStart > current.surfaceStart) {
145
+ byContext.set(candidate.contextKey, candidate);
146
+ }
147
+ }
148
+ return Array.from(byContext.values()).sort((a, b) => b.surfaceStart - a.surfaceStart).slice(0, Math.max(0, maxPrimes)).map((candidate, priority) => ({
149
+ familyKey,
150
+ contextKey: candidate.contextKey,
151
+ prompt: candidate.contextBeforeSurface,
152
+ priority
153
+ }));
154
+ };
155
+
156
+ /**
157
+ * `log(sum(exp(values)))`, shifted by the maximum so the sum cannot overflow or
158
+ * underflow to zero for the sequence log-likelihoods this is called with.
159
+ *
160
+ * Returns `-Infinity` for an empty or wholly non-finite input, which makes
161
+ * `exp(value - logSumExp(values))` evaluate to zero rather than `NaN`.
162
+ */
163
+ export const logSumExp = values => {
164
+ let max = -Infinity;
165
+ for (const value of values) {
166
+ if (Number.isFinite(value) && value > max) {
167
+ max = value;
168
+ }
169
+ }
170
+ if (!Number.isFinite(max)) {
171
+ return -Infinity;
172
+ }
173
+ let sumExp = 0;
174
+ for (const value of values) {
175
+ if (Number.isFinite(value)) {
176
+ sumExp += Math.exp(value - max);
177
+ }
178
+ }
179
+ return sumExp > 0 ? max + Math.log(sumExp) : -Infinity;
180
+ };
181
+
182
+ /** The two shift-and-sum terms of a distribution's log-partition. */
183
+
184
+ /**
185
+ * The log-partition depends only on the buffer, but scoring reads the same
186
+ * buffer once per candidate — so without this the two full-vocabulary passes
187
+ * below run N times per boundary for an answer that cannot change.
188
+ *
189
+ * Keying on the buffer itself is safe because captured logits are never
190
+ * written to again (see `CanonicalLogitProcessor.processLogits`, which copies
191
+ * out of WebLLM's reused buffer), and the weak reference lets a retired
192
+ * boundary's entry go with it.
193
+ */
194
+ const logPartitionTermsCache = new WeakMap();
195
+ const getLogPartitionTerms = logits => {
196
+ const cached = logPartitionTermsCache.get(logits);
197
+ if (cached !== undefined) {
198
+ return cached;
199
+ }
200
+ let maxLogit = -Infinity;
201
+ for (let i = 0; i < logits.length; i++) {
202
+ if (logits[i] > maxLogit) {
203
+ maxLogit = logits[i];
204
+ }
205
+ }
206
+ let terms = null;
207
+ if (Number.isFinite(maxLogit)) {
208
+ let sumExp = 0;
209
+ for (let i = 0; i < logits.length; i++) {
210
+ sumExp += Math.exp(logits[i] - maxLogit);
211
+ }
212
+ if (sumExp > 0) {
213
+ terms = {
214
+ maxLogit,
215
+ logSumTerm: Math.log(sumExp)
216
+ };
217
+ }
218
+ }
219
+ logPartitionTermsCache.set(logits, terms);
220
+ return terms;
221
+ };
222
+ export const logSoftmaxAt = (logits, target) => {
223
+ if (target < 0 || target >= logits.length) {
224
+ return -Infinity;
225
+ }
226
+ const terms = getLogPartitionTerms(logits);
227
+ if (terms === null) {
228
+ return -Infinity;
229
+ }
230
+ return logits[target] - terms.maxLogit - terms.logSumTerm;
231
+ };
232
+
233
+ /**
234
+ * One processor is registered for the causal model so the scheduler can read
235
+ * the next-token distribution off any forward pass.
236
+ *
237
+ * The scheduler drives decoding token by token — it prefills a prompt, then
238
+ * feeds each continuation token itself — so the processor never has to force
239
+ * the model onto a target path. It only has to hand back the distribution the
240
+ * forward pass produced, which it does without altering the logits.
241
+ */
242
+ export class CanonicalLogitProcessor {
243
+ constructor() {
244
+ _defineProperty(this, "captured", null);
245
+ _defineProperty(this, "processLogits", logits => {
246
+ // WebLLM reuses its logits buffer between forwards, so keep a private copy.
247
+ this.captured = new Float32Array(logits);
248
+ return logits;
249
+ });
250
+ _defineProperty(this, "processSampledToken", () => {
251
+ // The scheduler chooses the next token; WebLLM's sample is discarded.
252
+ });
253
+ _defineProperty(this, "resetState", () => {
254
+ // WebLLM resets the processor after the caller has armed a capture and
255
+ // immediately before it prefills a completion prompt, so this only ever
256
+ // clears a stale reading from the previous run.
257
+ this.captured = null;
258
+ });
259
+ _defineProperty(this, "startCapture", () => {
260
+ this.captured = null;
261
+ });
262
+ /**
263
+ * The captured distribution, or null when the forward pass never ran.
264
+ *
265
+ * `processLogits` already copied it, and every capture allocates afresh, so
266
+ * the buffer handed out here is never written to again.
267
+ */
268
+ _defineProperty(this, "getCapturedLogits", () => this.captured);
269
+ }
270
+ }
@@ -16,16 +16,83 @@
16
16
  * block uncategorized localStorage/sessionStorage/cookie writes in some products.
17
17
  */
18
18
 
19
- const hasUrlDebugFlag = () => {
19
+ /**
20
+ * Shared `%c` styles for all CTC console output, so every log — lifecycle,
21
+ * async signals, and per-prediction groups — reads consistently.
22
+ */
23
+ export const CTC_STYLES = {
24
+ brand: 'color: #00b8d9; font-weight: bold;',
25
+ section: 'color: #9c27b0; font-weight: bold;',
26
+ dim: 'color: #888; font-style: italic;',
27
+ good: 'color: #4caf50; font-weight: bold;',
28
+ warn: 'color: #ff9800; font-weight: bold;',
29
+ bad: 'color: #f44336; font-weight: bold;',
30
+ cold: 'color: #9e9e9e; font-weight: bold;',
31
+ lm: 'color: #e83e8c; font-weight: bold;',
32
+ body: 'color: inherit; font-weight: normal;'
33
+ };
34
+
35
+ /**
36
+ * Log one aligned section line inside a per-prediction group, e.g.
37
+ * `INPUT raw: "…"`. Label is padded so the bodies line up. No-ops unless
38
+ * debug is enabled, so callers don't need to guard.
39
+ */
40
+ export const ctcSection = (label, body) => {
41
+ if (!isAutocompleteDebugEnabled()) {
42
+ return;
43
+ }
44
+ // eslint-disable-next-line no-console
45
+ console.log(`%c${label.padEnd(10)}%c${body}`, CTC_STYLES.section, CTC_STYLES.body);
46
+ };
47
+
48
+ /**
49
+ * Log a lifecycle / async-signal line (outside the per-prediction groups),
50
+ * tagged `[CTC:<tag>]`. No-ops unless debug is enabled, so callers don't need to
51
+ * guard. Use tags like `init` (loads) and `signal` (slow-lane arrivals).
52
+ */
53
+ export const ctcTag = (tag, body, tagStyle = CTC_STYLES.brand) => {
54
+ if (!isAutocompleteDebugEnabled()) {
55
+ return;
56
+ }
57
+ // eslint-disable-next-line no-console
58
+ console.log(`%c[CTC:${tag}]%c ${body}`, tagStyle, CTC_STYLES.body);
59
+ };
60
+ const readUrlDebugFlag = () => {
20
61
  if (typeof window === 'undefined') {
21
- return false;
62
+ return {
63
+ enabled: false,
64
+ verbose: false
65
+ };
22
66
  }
23
67
  try {
24
- return new URLSearchParams(window.location.search).get('atlCtcDebug') === '1';
68
+ const value = new URLSearchParams(window.location.search).get('atlCtcDebug');
69
+ return {
70
+ enabled: value === '1' || value === 'verbose',
71
+ verbose: value === 'verbose'
72
+ };
25
73
  } catch {
26
- return false;
74
+ return {
75
+ enabled: false,
76
+ verbose: false
77
+ };
27
78
  }
28
79
  };
80
+ const printLegend = verbose => {
81
+ // eslint-disable-next-line no-console
82
+ console.groupCollapsed(`%c[CTC]%c debug enabled${verbose ? ' (verbose)' : ''} — Contextual Typeahead Completion`, CTC_STYLES.brand, CTC_STYLES.body);
83
+ // eslint-disable-next-line no-console
84
+ console.log('%cPipeline%c INPUT → SIGNALS → CANONICAL → GENERATE → SCORE → ARBITRATE → STABILIZE → CANDIDATES', CTC_STYLES.section, CTC_STYLES.body);
85
+ // eslint-disable-next-line no-console
86
+ console.log('%cEvidence%c Tier A = exact-context first token · prefix = one more decoded token · exact = every token of the surface scored · absent = Stage 1', CTC_STYLES.section, CTC_STYLES.body);
87
+ // eslint-disable-next-line no-console
88
+ console.log('%cLM queue%c one serialised GPU queue drains both: prompt prefills and single-token decode steps', CTC_STYLES.section, CTC_STYLES.body);
89
+ // eslint-disable-next-line no-console
90
+ console.log('%cPlanes%c [CTC:init] loads · [CTC:signal] semantic/network · [CTC:model] primes/exact · [CTC:model-cost] prefill/decode · [CTC:readiness] deadline progress', CTC_STYLES.section, CTC_STYLES.body);
91
+ // eslint-disable-next-line no-console
92
+ console.log('%cInspect%c __atlCtcDebug__.enable("verbose") · .disable() · .session() (L1 boosts, narrow to a family with .session("poll"))', CTC_STYLES.section, CTC_STYLES.body);
93
+ // eslint-disable-next-line no-console
94
+ console.groupEnd();
95
+ };
29
96
 
30
97
  // State lives on the window object (not a module closure) so duplicate copies of this
31
98
  // module across separate bundles/realms share one source of truth and the console API
@@ -35,15 +102,24 @@ const getDebugApi = () => {
35
102
  return undefined;
36
103
  }
37
104
  if (!window.__atlCtcDebug__) {
38
- let debugEnabled = hasUrlDebugFlag();
105
+ const initial = readUrlDebugFlag();
106
+ let debugEnabled = initial.enabled;
107
+ let debugVerbose = initial.verbose;
108
+ if (debugEnabled) {
109
+ printLegend(debugVerbose);
110
+ }
39
111
  window.__atlCtcDebug__ = {
40
- enable: () => {
112
+ enable: level => {
41
113
  debugEnabled = true;
114
+ debugVerbose = level === 'verbose';
115
+ printLegend(debugVerbose);
42
116
  },
43
117
  disable: () => {
44
118
  debugEnabled = false;
119
+ debugVerbose = false;
45
120
  },
46
- isEnabled: () => debugEnabled
121
+ isEnabled: () => debugEnabled,
122
+ isVerbose: () => debugEnabled && debugVerbose
47
123
  };
48
124
  }
49
125
  return window.__atlCtcDebug__;
@@ -52,6 +128,25 @@ export const isAutocompleteDebugEnabled = () => {
52
128
  var _getDebugApi$isEnable, _getDebugApi;
53
129
  return (_getDebugApi$isEnable = (_getDebugApi = getDebugApi()) === null || _getDebugApi === void 0 ? void 0 : _getDebugApi.isEnabled()) !== null && _getDebugApi$isEnable !== void 0 ? _getDebugApi$isEnable : false;
54
130
  };
131
+ export const isAutocompleteDebugVerbose = () => {
132
+ var _getDebugApi$isVerbos, _getDebugApi2;
133
+ return (_getDebugApi$isVerbos = (_getDebugApi2 = getDebugApi()) === null || _getDebugApi2 === void 0 ? void 0 : _getDebugApi2.isVerbose()) !== null && _getDebugApi$isVerbos !== void 0 ? _getDebugApi$isVerbos : false;
134
+ };
135
+
136
+ /**
137
+ * Hang the L1 session-boost snapshot off the console API.
138
+ *
139
+ * Unlike the log helpers this is available whether or not debug is enabled:
140
+ * inspecting state on demand is not logging, and asking someone to turn on
141
+ * logging and retype to find out what the session already holds defeats the
142
+ * point of being able to ask.
143
+ */
144
+ export const registerCtcSessionInspector = inspect => {
145
+ const api = getDebugApi();
146
+ if (api) {
147
+ api.session = inspect;
148
+ }
149
+ };
55
150
 
56
151
  // Eagerly install so the console API is available on load, regardless of call order.
57
152
  getDebugApi();
@@ -4,8 +4,11 @@ const GHOST_TEXT_CLASS = 'autocomplete-ghost-text';
4
4
  /**
5
5
  * Creates a DecorationSet containing a ghost text widget at the given position.
6
6
  * The ghost text is rendered as a styled <span> that appears after the cursor.
7
+ *
8
+ * Takes the document rather than the whole state so it can also be called from
9
+ * `apply`, where only the post-transaction doc exists.
7
10
  */
8
- export const createGhostTextDecorationSet = (state, position, text) => {
11
+ export const createGhostTextDecorationSet = (doc, position, text) => {
9
12
  if (!text) {
10
13
  return DecorationSet.empty;
11
14
  }
@@ -30,7 +33,13 @@ export const createGhostTextDecorationSet = (state, position, text) => {
30
33
  }, {
31
34
  side: 1,
32
35
  // Render after content at this position
33
- key: 'autocomplete-ghost-text'
36
+ // A matching key short-circuits `WidgetType.eq`, so ProseMirror reuses
37
+ // the rendered node and never calls `toDOM` again. Keying on the text
38
+ // keeps that reuse when nothing changed while still forcing a redraw
39
+ // when the ghost advances through a keystroke that confirmed it —
40
+ // otherwise the stale tail stays on screen and Tab inserts a
41
+ // character the user already typed.
42
+ key: `autocomplete-ghost-text:${text}`
34
43
  });
35
- return DecorationSet.create(state.doc, [decoration]);
44
+ return DecorationSet.create(doc, [decoration]);
36
45
  };