@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
@@ -4,12 +4,13 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.loadGrammarDataAsync = exports.isGrammarDataLoaded = exports.STAGE2_WEIGHT = exports.STAGE1_WEIGHT = exports.MIN_STAGE1_SCORE = void 0;
7
+ exports.loadGrammarDataAsync = exports.isGrammarDataLoaded = exports.STAGE2_WEIGHT = exports.STAGE1_WEIGHT = exports.MIN_WINNER_MARGIN = exports.MIN_STAGE1_SCORE = void 0;
8
8
  exports.rankCandidates = rankCandidates;
9
9
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
10
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
11
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
12
12
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
13
+ var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
13
14
  var _ufo = require("../analytics/ufo");
14
15
  var _artifactsManifest = require("./artifacts-manifest");
15
16
  var _debugMode = require("./debug-mode");
@@ -28,6 +29,11 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
28
29
  */
29
30
  // ─── Types ──────────────────────────────────────────────────
30
31
 
32
+ /**
33
+ * A completion term is `(term, termType)`. `word` is the v1 default; `bigram`
34
+ * (2 tokens) and `phrase` (3 tokens) are multi-word units mined by the producer.
35
+ */
36
+
31
37
  /** Metadata returned by the grammar filter for debug logging in the caller. */
32
38
 
33
39
  // ─── Scoring Constants ──────────────────────────────────────
@@ -35,9 +41,25 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
35
41
  var ALPHA = 0.5;
36
42
  var BETA = 0.5;
37
43
  var NEUTRAL_SCORE = 0.5;
44
+ // Multiplicative Stage-1 boost applied to a multi-word term proportional to its
45
+ // (clamped [0,1]) NPMI association, so a strongly-collocated phrase outranks a
46
+ // coincidental word sequence of comparable freq/semantics. Tunable.
47
+ var ASSOCIATION_BOOST = 0.5;
38
48
  var STAGE1_WEIGHT = exports.STAGE1_WEIGHT = 0.35;
39
49
  var STAGE2_WEIGHT = exports.STAGE2_WEIGHT = 0.65;
40
50
  var MIN_STAGE1_SCORE = exports.MIN_STAGE1_SCORE = 0.35;
51
+ /**
52
+ * How far the leading candidate must sit above the runner-up on the blended
53
+ * score before a ghost is committed.
54
+ *
55
+ * Lives beside the weights it is measured in, because it is only meaningful on
56
+ * that scale — and because both the arbitration that applies it and the
57
+ * scheduler that decides when reading more tokens can no longer change the
58
+ * outcome have to use the same number. When those two drifted apart, the
59
+ * scheduler stopped early on a margin the arbitration did not recognise and the
60
+ * abstention that followed was invisible from either side.
61
+ */
62
+ var MIN_WINNER_MARGIN = exports.MIN_WINNER_MARGIN = 0.08;
41
63
  var L1_SESSION_CAP = 1.2;
42
64
  // Minimum prefix-payload max LM probability before Stage 2 activates.
43
65
  // Below this threshold the LM signal is too weak to suppress Stage 1 — finalScore
@@ -56,13 +78,38 @@ var posTags = new Map();
56
78
  * without suggesting these words to the user.
57
79
  */
58
80
  var ghostPosTags = {};
81
+ var grammarDataPromise;
59
82
 
60
83
  /**
61
84
  * Precomputed map from each POS tag to the set of allowed next POS tags, so
62
85
  * applyGrammarFilter never re-iterates the transition rules per call.
63
86
  */
64
87
  var precomputedAllowedByPos = new Map();
65
- var grammarDataPromise;
88
+ var isPosTagsRecord = function isPosTagsRecord(payload) {
89
+ if (payload == null || (0, _typeof2.default)(payload) !== 'object') {
90
+ return false;
91
+ }
92
+ return Object.values(payload).every(function (value) {
93
+ return Array.isArray(value) && value.every(function (entry) {
94
+ return typeof entry === 'string';
95
+ });
96
+ });
97
+ };
98
+ var isGrammarTransitions = function isGrammarTransitions(payload) {
99
+ if (payload == null || (0, _typeof2.default)(payload) !== 'object') {
100
+ return false;
101
+ }
102
+ var transitions = payload.transitions;
103
+ if (transitions == null || (0, _typeof2.default)(transitions) !== 'object') {
104
+ return false;
105
+ }
106
+ return Object.values(transitions).every(function (value) {
107
+ var allowed = value === null || value === void 0 ? void 0 : value.allowed;
108
+ return Array.isArray(allowed) && allowed.every(function (entry) {
109
+ return typeof entry === 'string';
110
+ });
111
+ });
112
+ };
66
113
  var setGrammarData = function setGrammarData(data) {
67
114
  var _data$grammarTransiti;
68
115
  posTags = new Map(Object.entries(data.posTags));
@@ -116,9 +163,27 @@ var loadGrammarDataAsync = exports.loadGrammarDataAsync = function loadGrammarDa
116
163
  }
117
164
  throw new Error("[scoring-pipeline] ".concat(_artifactsManifest.ARTIFACT_NAME.GRAMMAR_TRANSITIONS, " is missing its `transitions` wrapper"));
118
165
  case 3:
166
+ if (isGrammarTransitions(grammarTransitionsData)) {
167
+ _context.next = 4;
168
+ break;
169
+ }
170
+ throw new Error("[scoring-pipeline] ".concat(_artifactsManifest.ARTIFACT_NAME.GRAMMAR_TRANSITIONS, " payload shape was invalid"));
171
+ case 4:
172
+ if (isPosTagsRecord(posTagsData)) {
173
+ _context.next = 5;
174
+ break;
175
+ }
176
+ throw new Error("[scoring-pipeline] ".concat(_artifactsManifest.ARTIFACT_NAME.POS_TAGS, " payload shape was invalid"));
177
+ case 5:
178
+ if (isPosTagsRecord(ghostPosTagsData)) {
179
+ _context.next = 6;
180
+ break;
181
+ }
182
+ throw new Error("[scoring-pipeline] ".concat(_artifactsManifest.ARTIFACT_NAME.GHOST_POS_TAGS, " payload shape was invalid"));
183
+ case 6:
119
184
  setGrammarData({
120
- posTags: posTagsData !== null && posTagsData !== void 0 ? posTagsData : {},
121
- ghostPosTags: ghostPosTagsData !== null && ghostPosTagsData !== void 0 ? ghostPosTagsData : {},
185
+ posTags: posTagsData,
186
+ ghostPosTags: ghostPosTagsData,
122
187
  grammarTransitions: grammarTransitionsData
123
188
  });
124
189
  (0, _ufo.succeedExp)(_ufo.EXPERIENCE_NAME.LOAD_GRAMMAR, 'singleton', _objectSpread({
@@ -136,10 +201,10 @@ var loadGrammarDataAsync = exports.loadGrammarDataAsync = function loadGrammarDa
136
201
  transitionCount: precomputedAllowedByPos.size
137
202
  });
138
203
  }
139
- _context.next = 5;
204
+ _context.next = 8;
140
205
  break;
141
- case 4:
142
- _context.prev = 4;
206
+ case 7:
207
+ _context.prev = 7;
143
208
  _t = _context["catch"](1);
144
209
  (0, _ufo.failExp)(_ufo.EXPERIENCE_NAME.LOAD_GRAMMAR, 'singleton', _objectSpread({
145
210
  isLocalLLM: isLocalLLM,
@@ -150,11 +215,11 @@ var loadGrammarDataAsync = exports.loadGrammarDataAsync = function loadGrammarDa
150
215
  // Allow a later call to retry the load rather than caching the failure.
151
216
  grammarDataPromise = undefined;
152
217
  throw _t;
153
- case 5:
218
+ case 8:
154
219
  case "end":
155
220
  return _context.stop();
156
221
  }
157
- }, _callee, null, [[1, 4]]);
222
+ }, _callee, null, [[1, 7]]);
158
223
  }))();
159
224
  return grammarDataPromise;
160
225
  };
@@ -253,7 +318,16 @@ function applyGrammarFilter(candidates, previousWord) {
253
318
  try {
254
319
  for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
255
320
  var entry = _step2.value;
256
- var candidateTags = posTags.get(entry.candidate.word.toLowerCase());
321
+ // The producer validates a multi-word term's *internal* grammar offline
322
+ // (boundary-clean, noun-headed, AUP-passed — spec §9), but nothing judges
323
+ // the transition into it. Exempting multi-word terms therefore let a noun
324
+ // this filter had just rejected re-enter wrapped in a bigram — "be" [AUX]
325
+ // dropped "quality" and then displayed "quality assurance". Only the term's
326
+ // first word takes part in the transition, so gate every term on that.
327
+ var surface = entry.candidate.word;
328
+ var firstSpace = surface.indexOf(' ');
329
+ var transitionWord = firstSpace === -1 ? surface : surface.slice(0, firstSpace);
330
+ var candidateTags = posTags.get(transitionWord.toLowerCase());
257
331
 
258
332
  // If candidate has no tags (unknown word), let it pass to be safe
259
333
  if (!candidateTags || candidateTags.length === 0) {
@@ -301,19 +375,34 @@ function getLmScore(word, lmLogits) {
301
375
 
302
376
  // ─── Public API ─────────────────────────────────────────────
303
377
 
304
- function rankCandidates(candidates, contextVector, getWordVector, lmLogits, maxTenantFreq, previousWord) {
378
+ /**
379
+ * Apply Stage 1, grammar, and nullable context-local LM evidence to candidates.
380
+ */
381
+ function rankCandidates(candidates, contextVector, getWordVector, lmLogits, maxFreqByType, previousWord, getCanonicalLmEvidence) {
305
382
  var _grammarMeta$dropped;
306
383
  // Stage 1
307
384
  var stage1Results = candidates.map(function (candidate) {
308
- var _scoreStage = scoreStage1(candidate, contextVector, getWordVector, maxTenantFreq),
385
+ var _candidate$termType;
386
+ var termType = (_candidate$termType = candidate.termType) !== null && _candidate$termType !== void 0 ? _candidate$termType : 'word';
387
+ // Normalize freq within the candidate's own term-type population so a
388
+ // phrase (whose absolute counts are far below top unigrams) isn't crushed
389
+ // by the unigram max.
390
+ var maxFreq = maxFreqByType[termType] || 1;
391
+ var _scoreStage = scoreStage1(candidate, contextVector, getWordVector, maxFreq),
309
392
  semanticScore = _scoreStage.semanticScore,
310
393
  freqScore = _scoreStage.freqScore,
311
394
  stage1Score = _scoreStage.stage1Score;
395
+ // Multi-word units get an NPMI association boost (Stage-1 only).
396
+ var adjustedStage1 = stage1Score;
397
+ if (termType !== 'word' && typeof candidate.association === 'number') {
398
+ var assoc = Math.max(0, Math.min(1, candidate.association));
399
+ adjustedStage1 = stage1Score * (1 + ASSOCIATION_BOOST * assoc);
400
+ }
312
401
  return {
313
402
  candidate: candidate,
314
403
  semanticScore: semanticScore,
315
404
  freqScore: freqScore,
316
- stage1Score: stage1Score
405
+ stage1Score: adjustedStage1
317
406
  };
318
407
  });
319
408
  var stage1Survivors = [];
@@ -346,26 +435,82 @@ function rankCandidates(candidates, contextVector, getWordVector, lmLogits, maxT
346
435
  var values = Object.values(lmLogits);
347
436
  if (values.length > 0) lmMax = Math.max.apply(Math, values);
348
437
  }
438
+ var canonicalEvidenceBySurface = new Map();
439
+ var maxLmRankingValueByTierAndContext = new Map();
440
+ var _iterator4 = _createForOfIteratorHelper(filtered),
441
+ _step4;
442
+ try {
443
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
444
+ var _getCanonicalLmEviden;
445
+ var _entry = _step4.value;
446
+ var evidence = (_getCanonicalLmEviden = getCanonicalLmEvidence === null || getCanonicalLmEvidence === void 0 ? void 0 : getCanonicalLmEvidence(_entry.candidate.word)) !== null && _getCanonicalLmEviden !== void 0 ? _getCanonicalLmEviden : null;
447
+ if (!evidence) {
448
+ continue;
449
+ }
450
+ canonicalEvidenceBySurface.set(_entry.candidate.word, evidence);
451
+ var rankingValue = evidence.source === 'canonical-full-surface' ? evidence.meanTokenLogProbability : evidence.rawLogit;
452
+ if (rankingValue !== undefined && Number.isFinite(rankingValue)) {
453
+ var _maxLmRankingValueByT;
454
+ var key = "".concat(evidence.source, "\0").concat(evidence.contextKey);
455
+ var current = (_maxLmRankingValueByT = maxLmRankingValueByTierAndContext.get(key)) !== null && _maxLmRankingValueByT !== void 0 ? _maxLmRankingValueByT : -Infinity;
456
+ if (rankingValue > current) {
457
+ maxLmRankingValueByTierAndContext.set(key, rankingValue);
458
+ }
459
+ }
460
+ }
461
+ } catch (err) {
462
+ _iterator4.e(err);
463
+ } finally {
464
+ _iterator4.f();
465
+ }
349
466
  var scored = filtered.map(function (entry) {
467
+ var _entry$candidate$term, _canonicalEvidenceByS;
468
+ var termType = (_entry$candidate$term = entry.candidate.termType) !== null && _entry$candidate$term !== void 0 ? _entry$candidate$term : 'word';
469
+ var isMultiWord = termType !== 'word';
350
470
  var lmScore = 0;
351
471
  var finalScore = entry.stage1Score;
352
- if (lmLogits && lmMax >= LM_GATE_THRESHOLD) {
472
+ var lmEvidence = (_canonicalEvidenceByS = canonicalEvidenceBySurface.get(entry.candidate.word)) !== null && _canonicalEvidenceByS !== void 0 ? _canonicalEvidenceByS : null;
473
+ if (lmEvidence) {
474
+ var rankingValue = lmEvidence.source === 'canonical-full-surface' ? lmEvidence.meanTokenLogProbability : lmEvidence.rawLogit;
475
+ var maxRankingValue = maxLmRankingValueByTierAndContext.get("".concat(lmEvidence.source, "\0").concat(lmEvidence.contextKey));
476
+ if (rankingValue !== undefined && maxRankingValue !== undefined) {
477
+ lmEvidence = _objectSpread(_objectSpread({}, lmEvidence), {}, {
478
+ score: Math.exp(rankingValue - maxRankingValue)
479
+ });
480
+ }
481
+ }
482
+ if (lmEvidence) {
483
+ lmScore = lmEvidence.score;
484
+ finalScore = STAGE1_WEIGHT * entry.stage1Score + STAGE2_WEIGHT * lmScore;
485
+ } else if (!isMultiWord && lmLogits && lmMax >= LM_GATE_THRESHOLD) {
486
+ // Backward-compatible network slow-lane path.
353
487
  var rawLm = getLmScore(entry.candidate.word, lmLogits);
354
488
  if (rawLm !== 0) {
355
489
  var logitDiff = Math.log(rawLm) - Math.log(lmMax);
356
490
  lmScore = Math.exp(logitDiff);
491
+ lmEvidence = {
492
+ contextKey: 'network',
493
+ score: lmScore,
494
+ separatorKind: 'whitespace',
495
+ source: 'network-logit',
496
+ totalSurfaceCharCount: entry.candidate.word.length,
497
+ // The legacy BE word map does not report canonical token depth.
498
+ totalTokenCount: 0,
499
+ verifiedCharCount: 0,
500
+ verifiedTokenCount: 0
501
+ };
502
+ finalScore = STAGE1_WEIGHT * entry.stage1Score + STAGE2_WEIGHT * lmScore;
357
503
  }
358
- // Words absent from the prefix-filtered payload get lmScore = 0,
359
- // not 0.05, so they don't outrank genuine LM predictions.
360
-
361
- finalScore = STAGE1_WEIGHT * entry.stage1Score + STAGE2_WEIGHT * lmScore;
362
504
  }
363
505
  return {
364
506
  word: entry.candidate.word,
365
507
  freqScore: entry.freqScore,
366
508
  semanticScore: entry.semanticScore,
367
509
  lmScore: lmScore,
368
- finalScore: finalScore
510
+ lmEvidence: lmEvidence,
511
+ finalScore: finalScore,
512
+ stage1Score: entry.stage1Score,
513
+ termType: termType
369
514
  };
370
515
  });
371
516
  scored.sort(function (a, b) {
@@ -4,12 +4,13 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
4
4
  Object.defineProperty(exports, "__esModule", {
5
5
  value: true
6
6
  });
7
- exports.setDefaultSlowLaneClient = exports.isWordBoundary = exports.getStoredLmLogits = exports.getStoredContextVector = exports.createSlowLaneClient = void 0;
7
+ exports.setDefaultSlowLaneClient = exports.requestProgressiveSurfaceScores = exports.primeBoundaryLm = exports.isWordBoundary = exports.isCanonicalSurfaceScoringSupported = exports.getSurfaceScore = exports.getStoredLmLogits = exports.getStoredContextVector = exports.getStoredContextInput = exports.getProgressiveSurfaceEvidence = exports.getDefaultSlowLaneClientStatus = exports.getCanonicalSurfaceTokenIds = exports.getCanonicalSurfaceCount = exports.getBoundaryLmState = exports.createSlowLaneClient = exports.clearDefaultSlowLaneClient = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
9
  var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
10
10
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
11
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
12
12
  var _ufo = require("../analytics/ufo");
13
+ var _artifactLoader = require("./artifact-loader");
13
14
  var _debugMode = require("./debug-mode");
14
15
  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; }
15
16
  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) { (0, _defineProperty2.default)(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; } /**
@@ -30,8 +31,6 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
30
31
 
31
32
  // eslint-disable-next-line require-unicode-regexp
32
33
  var WORD_BOUNDARY_CHARS = /[\s.,;:!?]/;
33
- // eslint-disable-next-line require-unicode-regexp
34
- var TRAILING_SLASH_REGEX = /\/$/;
35
34
  var DEFAULT_DEBOUNCE_MS = 300;
36
35
 
37
36
  /**
@@ -68,6 +67,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
68
67
  };
69
68
  var debounceTimer = null;
70
69
  var lastRequestedText = '';
70
+ var storedContextInput = null;
71
71
  var storedContextVector = null;
72
72
  var storedLmLogits = null;
73
73
  var requestSeq = 0;
@@ -84,7 +84,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
84
84
  }
85
85
  return _context.abrupt("return");
86
86
  case 1:
87
- url = "".concat(baseUrl.replace(TRAILING_SLASH_REGEX, '')).concat(endpoint);
87
+ url = (0, _artifactLoader.buildAutocompleteGatewayUrl)(endpoint, baseUrl);
88
88
  payload = {
89
89
  text: text,
90
90
  session_id: sessionId
@@ -95,16 +95,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
95
95
  }, surface ? {
96
96
  surface: surface
97
97
  } : {}));
98
- if ((0, _debugMode.isAutocompleteDebugEnabled)()) {
99
- // eslint-disable-next-line no-console
100
- console.groupCollapsed("%c[SlowLane] %c\uD83D\uDCE4 Sending context | ".concat(text.length, " chars"), 'color: #9c27b0; font-weight: bold;', 'color: inherit;');
101
- text.split('\n').forEach(function (line, i, arr) {
102
- // eslint-disable-next-line no-console
103
- console.log(" ".concat(i === arr.length - 1 ? '▶' : ' ', " ").concat(line));
104
- });
105
- // eslint-disable-next-line no-console
106
- console.groupEnd();
107
- }
98
+ (0, _debugMode.ctcTag)('signal', "\uD83D\uDCE4 network context sent \xB7 ".concat(text.length, " chars"), _debugMode.CTC_STYLES.section);
108
99
  _context.prev = 2;
109
100
  _context.next = 3;
110
101
  return fetchFn(url, {
@@ -118,6 +109,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
118
109
  _context.next = 4;
119
110
  break;
120
111
  }
112
+ storedContextInput = null;
121
113
  storedContextVector = null;
122
114
  storedLmLogits = null;
123
115
  (0, _ufo.failExp)(_ufo.EXPERIENCE_NAME.SLOW_LANE_FETCH, requestId, _objectSpread({
@@ -127,10 +119,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
127
119
  }, surface ? {
128
120
  surface: surface
129
121
  } : {}));
130
- if ((0, _debugMode.isAutocompleteDebugEnabled)()) {
131
- // eslint-disable-next-line no-console
132
- console.log("%c[SlowLane] %c\u274C Request failed (".concat(res.status, ")"), 'color: #9c27b0; font-weight: bold;', 'color: #f44336;');
133
- }
122
+ (0, _debugMode.ctcTag)('signal', "\u274C network request failed (".concat(res.status, ")"), _debugMode.CTC_STYLES.bad);
134
123
  return _context.abrupt("return");
135
124
  case 4:
136
125
  _context.next = 5;
@@ -139,24 +128,17 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
139
128
  data = _context.sent;
140
129
  if (data.semantic_vector && Array.isArray(data.semantic_vector)) {
141
130
  storedContextVector = new Float32Array(data.semantic_vector);
131
+ storedContextInput = text;
142
132
  } else {
143
133
  storedContextVector = null;
134
+ storedContextInput = null;
144
135
  }
145
136
  if (data.lm_logits && (0, _typeof2.default)(data.lm_logits) === 'object') {
146
137
  storedLmLogits = data.lm_logits;
147
138
  } else {
148
139
  storedLmLogits = null;
149
140
  }
150
- if ((0, _debugMode.isAutocompleteDebugEnabled)()) {
151
- // eslint-disable-next-line no-console
152
- console.groupCollapsed("%c[SlowLane] %c\uD83D\uDCE5 Response received", 'color: #9c27b0; font-weight: bold;', 'color: inherit;');
153
- // eslint-disable-next-line no-console
154
- console.log(storedContextVector ? "\u2705 semantic_vector: ".concat(storedContextVector.length, " dims") : '❌ No semantic_vector');
155
- // eslint-disable-next-line no-console
156
- console.log(storedLmLogits ? "\u2705 lm_logits: ".concat(Object.keys(storedLmLogits).length, " tokens") : '❌ No lm_logits');
157
- // eslint-disable-next-line no-console
158
- console.groupEnd();
159
- }
141
+ (0, _debugMode.ctcTag)('signal', "\uD83D\uDCE5 network response \xB7 ".concat(storedContextVector ? "semantic \u2705 ".concat(storedContextVector.length, "d") : 'semantic ❌', " \xB7 ").concat(storedLmLogits ? "logits \u2705 ".concat(Object.keys(storedLmLogits).length, "tok") : 'logits ❌'), _debugMode.CTC_STYLES.section);
160
142
  (0, _ufo.succeedExp)(_ufo.EXPERIENCE_NAME.SLOW_LANE_FETCH, requestId, _objectSpread({
161
143
  textLength: text.length,
162
144
  hasVector: storedContextVector !== null,
@@ -176,6 +158,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
176
158
  case 6:
177
159
  _context.prev = 6;
178
160
  _t = _context["catch"](2);
161
+ storedContextInput = null;
179
162
  storedContextVector = null;
180
163
  storedLmLogits = null;
181
164
  (0, _ufo.failExp)(_ufo.EXPERIENCE_NAME.SLOW_LANE_FETCH, requestId, _objectSpread({
@@ -184,10 +167,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
184
167
  }, surface ? {
185
168
  surface: surface
186
169
  } : {}));
187
- if ((0, _debugMode.isAutocompleteDebugEnabled)()) {
188
- // eslint-disable-next-line no-console
189
- console.log('%c[SlowLane] %c❌ Network error — context cleared', 'color: #9c27b0; font-weight: bold;', 'color: #f44336;');
190
- }
170
+ (0, _debugMode.ctcTag)('signal', '❌ network error — context cleared', _debugMode.CTC_STYLES.bad);
191
171
  case 7:
192
172
  _context.prev = 7;
193
173
  if (inflightRequestId === requestId) {
@@ -225,6 +205,9 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
225
205
  };
226
206
  return {
227
207
  updateContext: updateContextDebounced,
208
+ getContextInput: function getContextInput() {
209
+ return storedContextInput;
210
+ },
228
211
  getContextVector: function getContextVector() {
229
212
  return storedContextVector;
230
213
  },
@@ -232,6 +215,7 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
232
215
  return storedLmLogits;
233
216
  },
234
217
  setContextVector: function setContextVector(vector) {
218
+ storedContextInput = null;
235
219
  storedContextVector = vector;
236
220
  },
237
221
  setLmLogits: function setLmLogits(logits) {
@@ -241,14 +225,93 @@ var createSlowLaneClient = exports.createSlowLaneClient = function createSlowLan
241
225
  };
242
226
  };
243
227
  var defaultSlowLaneClient = null;
244
- var setDefaultSlowLaneClient = exports.setDefaultSlowLaneClient = function setDefaultSlowLaneClient(client) {
228
+ var defaultSlowLaneClientRegistration = null;
229
+ var supportsCanonicalSurfaceScoring = function supportsCanonicalSurfaceScoring(client) {
230
+ return typeof (client === null || client === void 0 ? void 0 : client.primeBoundaryLm) === 'function' && typeof (client === null || client === void 0 ? void 0 : client.requestProgressiveSurfaceScores) === 'function';
231
+ };
232
+ var setDefaultSlowLaneClient = exports.setDefaultSlowLaneClient = function setDefaultSlowLaneClient(client, registration) {
245
233
  defaultSlowLaneClient = client;
234
+ defaultSlowLaneClientRegistration = client ? registration !== null && registration !== void 0 ? registration : {
235
+ clientId: 'untracked',
236
+ kind: supportsCanonicalSurfaceScoring(client) ? 'localLlm' : 'server'
237
+ } : null;
238
+ };
239
+
240
+ /**
241
+ * Clear the shared client only when the caller still owns the registration.
242
+ *
243
+ * Multiple editor instances can briefly overlap during a React/editor remount.
244
+ * An older instance must not disconnect the newer instance when its delayed
245
+ * teardown runs after the newer client has registered.
246
+ */
247
+ var clearDefaultSlowLaneClient = exports.clearDefaultSlowLaneClient = function clearDefaultSlowLaneClient(client) {
248
+ if (defaultSlowLaneClient !== client) {
249
+ return false;
250
+ }
251
+ defaultSlowLaneClient = null;
252
+ defaultSlowLaneClientRegistration = null;
253
+ return true;
254
+ };
255
+ var getDefaultSlowLaneClientStatus = exports.getDefaultSlowLaneClientStatus = function getDefaultSlowLaneClientStatus() {
256
+ var _defaultSlowLaneClien, _defaultSlowLaneClien2, _defaultSlowLaneClien3, _defaultSlowLaneClien4, _defaultSlowLaneClien5, _defaultSlowLaneClien6, _defaultSlowLaneClien7;
257
+ var canonicalScoringSupported = supportsCanonicalSurfaceScoring(defaultSlowLaneClient);
258
+ var isLocalClient = ((_defaultSlowLaneClien = defaultSlowLaneClientRegistration) === null || _defaultSlowLaneClien === void 0 ? void 0 : _defaultSlowLaneClien.kind) === 'localLlm';
259
+ return {
260
+ registered: defaultSlowLaneClient !== null,
261
+ clientId: (_defaultSlowLaneClien2 = (_defaultSlowLaneClien3 = defaultSlowLaneClientRegistration) === null || _defaultSlowLaneClien3 === void 0 ? void 0 : _defaultSlowLaneClien3.clientId) !== null && _defaultSlowLaneClien2 !== void 0 ? _defaultSlowLaneClien2 : null,
262
+ clientKind: (_defaultSlowLaneClien4 = (_defaultSlowLaneClien5 = defaultSlowLaneClientRegistration) === null || _defaultSlowLaneClien5 === void 0 ? void 0 : _defaultSlowLaneClien5.kind) !== null && _defaultSlowLaneClien4 !== void 0 ? _defaultSlowLaneClien4 : null,
263
+ canonicalScoringSupported: canonicalScoringSupported,
264
+ localModelReady: isLocalClient && typeof ((_defaultSlowLaneClien6 = defaultSlowLaneClient) === null || _defaultSlowLaneClien6 === void 0 ? void 0 : _defaultSlowLaneClien6.isReady) === 'function' ? defaultSlowLaneClient.isReady() : null,
265
+ canonicalSurfaceCount: canonicalScoringSupported && typeof ((_defaultSlowLaneClien7 = defaultSlowLaneClient) === null || _defaultSlowLaneClien7 === void 0 ? void 0 : _defaultSlowLaneClien7.getCanonicalSurfaceCount) === 'function' ? defaultSlowLaneClient.getCanonicalSurfaceCount() : null
266
+ };
246
267
  };
247
268
  var getStoredContextVector = exports.getStoredContextVector = function getStoredContextVector() {
248
- var _defaultSlowLaneClien, _defaultSlowLaneClien2;
249
- return (_defaultSlowLaneClien = (_defaultSlowLaneClien2 = defaultSlowLaneClient) === null || _defaultSlowLaneClien2 === void 0 ? void 0 : _defaultSlowLaneClien2.getContextVector()) !== null && _defaultSlowLaneClien !== void 0 ? _defaultSlowLaneClien : null;
269
+ var _defaultSlowLaneClien8, _defaultSlowLaneClien9;
270
+ return (_defaultSlowLaneClien8 = (_defaultSlowLaneClien9 = defaultSlowLaneClient) === null || _defaultSlowLaneClien9 === void 0 ? void 0 : _defaultSlowLaneClien9.getContextVector()) !== null && _defaultSlowLaneClien8 !== void 0 ? _defaultSlowLaneClien8 : null;
271
+ };
272
+ var getStoredContextInput = exports.getStoredContextInput = function getStoredContextInput() {
273
+ var _defaultSlowLaneClien0, _defaultSlowLaneClien1, _defaultSlowLaneClien10;
274
+ return (_defaultSlowLaneClien0 = (_defaultSlowLaneClien1 = defaultSlowLaneClient) === null || _defaultSlowLaneClien1 === void 0 || (_defaultSlowLaneClien10 = _defaultSlowLaneClien1.getContextInput) === null || _defaultSlowLaneClien10 === void 0 ? void 0 : _defaultSlowLaneClien10.call(_defaultSlowLaneClien1)) !== null && _defaultSlowLaneClien0 !== void 0 ? _defaultSlowLaneClien0 : null;
250
275
  };
251
276
  var getStoredLmLogits = exports.getStoredLmLogits = function getStoredLmLogits() {
252
- var _defaultSlowLaneClien3, _defaultSlowLaneClien4;
253
- return (_defaultSlowLaneClien3 = (_defaultSlowLaneClien4 = defaultSlowLaneClient) === null || _defaultSlowLaneClien4 === void 0 ? void 0 : _defaultSlowLaneClien4.getLmLogits()) !== null && _defaultSlowLaneClien3 !== void 0 ? _defaultSlowLaneClien3 : null;
277
+ var _defaultSlowLaneClien11, _defaultSlowLaneClien12;
278
+ return (_defaultSlowLaneClien11 = (_defaultSlowLaneClien12 = defaultSlowLaneClient) === null || _defaultSlowLaneClien12 === void 0 ? void 0 : _defaultSlowLaneClien12.getLmLogits()) !== null && _defaultSlowLaneClien11 !== void 0 ? _defaultSlowLaneClien11 : null;
279
+ };
280
+ var getBoundaryLmState = exports.getBoundaryLmState = function getBoundaryLmState(contextKey) {
281
+ var _defaultSlowLaneClien13, _defaultSlowLaneClien14, _defaultSlowLaneClien15;
282
+ return (_defaultSlowLaneClien13 = (_defaultSlowLaneClien14 = defaultSlowLaneClient) === null || _defaultSlowLaneClien14 === void 0 || (_defaultSlowLaneClien15 = _defaultSlowLaneClien14.getBoundaryLmState) === null || _defaultSlowLaneClien15 === void 0 ? void 0 : _defaultSlowLaneClien15.call(_defaultSlowLaneClien14, contextKey)) !== null && _defaultSlowLaneClien13 !== void 0 ? _defaultSlowLaneClien13 : null;
283
+ };
284
+ var getCanonicalSurfaceTokenIds = exports.getCanonicalSurfaceTokenIds = function getCanonicalSurfaceTokenIds(surface) {
285
+ var _defaultSlowLaneClien16, _defaultSlowLaneClien17, _defaultSlowLaneClien18;
286
+ return (_defaultSlowLaneClien16 = (_defaultSlowLaneClien17 = defaultSlowLaneClient) === null || _defaultSlowLaneClien17 === void 0 || (_defaultSlowLaneClien18 = _defaultSlowLaneClien17.getCanonicalSurfaceTokenIds) === null || _defaultSlowLaneClien18 === void 0 ? void 0 : _defaultSlowLaneClien18.call(_defaultSlowLaneClien17, surface)) !== null && _defaultSlowLaneClien16 !== void 0 ? _defaultSlowLaneClien16 : null;
287
+ };
288
+ var getSurfaceScore = exports.getSurfaceScore = function getSurfaceScore(contextKey, surface) {
289
+ var _defaultSlowLaneClien19, _defaultSlowLaneClien20, _defaultSlowLaneClien21;
290
+ return (_defaultSlowLaneClien19 = (_defaultSlowLaneClien20 = defaultSlowLaneClient) === null || _defaultSlowLaneClien20 === void 0 || (_defaultSlowLaneClien21 = _defaultSlowLaneClien20.getSurfaceScore) === null || _defaultSlowLaneClien21 === void 0 ? void 0 : _defaultSlowLaneClien21.call(_defaultSlowLaneClien20, contextKey, surface)) !== null && _defaultSlowLaneClien19 !== void 0 ? _defaultSlowLaneClien19 : null;
291
+ };
292
+ var getProgressiveSurfaceEvidence = exports.getProgressiveSurfaceEvidence = function getProgressiveSurfaceEvidence(contextKey, surface) {
293
+ var _defaultSlowLaneClien22, _defaultSlowLaneClien23, _defaultSlowLaneClien24;
294
+ return (_defaultSlowLaneClien22 = (_defaultSlowLaneClien23 = defaultSlowLaneClient) === null || _defaultSlowLaneClien23 === void 0 || (_defaultSlowLaneClien24 = _defaultSlowLaneClien23.getProgressiveSurfaceEvidence) === null || _defaultSlowLaneClien24 === void 0 ? void 0 : _defaultSlowLaneClien24.call(_defaultSlowLaneClien23, contextKey, surface)) !== null && _defaultSlowLaneClien22 !== void 0 ? _defaultSlowLaneClien22 : null;
295
+ };
296
+ var primeBoundaryLm = exports.primeBoundaryLm = function primeBoundaryLm(input) {
297
+ var _defaultSlowLaneClien25, _defaultSlowLaneClien26;
298
+ (_defaultSlowLaneClien25 = defaultSlowLaneClient) === null || _defaultSlowLaneClien25 === void 0 || (_defaultSlowLaneClien26 = _defaultSlowLaneClien25.primeBoundaryLm) === null || _defaultSlowLaneClien26 === void 0 || _defaultSlowLaneClien26.call(_defaultSlowLaneClien25, input);
299
+ };
300
+ var requestProgressiveSurfaceScores = exports.requestProgressiveSurfaceScores = function requestProgressiveSurfaceScores(input) {
301
+ var _defaultSlowLaneClien27, _defaultSlowLaneClien28;
302
+ (_defaultSlowLaneClien27 = defaultSlowLaneClient) === null || _defaultSlowLaneClien27 === void 0 || (_defaultSlowLaneClien28 = _defaultSlowLaneClien27.requestProgressiveSurfaceScores) === null || _defaultSlowLaneClien28 === void 0 || _defaultSlowLaneClien28.call(_defaultSlowLaneClien27, input);
303
+ };
304
+ var isCanonicalSurfaceScoringSupported = exports.isCanonicalSurfaceScoringSupported = function isCanonicalSurfaceScoringSupported() {
305
+ return supportsCanonicalSurfaceScoring(defaultSlowLaneClient);
306
+ };
307
+
308
+ /**
309
+ * How many surfaces the canonical token map currently holds. Goes from zero to
310
+ * its final size once the artifact lands, which is the only point at which
311
+ * `getCanonicalSurfaceTokenIds` starts answering — so callers memoising over
312
+ * that lookup can use this to detect the transition.
313
+ */
314
+ var getCanonicalSurfaceCount = exports.getCanonicalSurfaceCount = function getCanonicalSurfaceCount() {
315
+ var _defaultSlowLaneClien29, _defaultSlowLaneClien30, _defaultSlowLaneClien31;
316
+ return (_defaultSlowLaneClien29 = (_defaultSlowLaneClien30 = defaultSlowLaneClient) === null || _defaultSlowLaneClien30 === void 0 || (_defaultSlowLaneClien31 = _defaultSlowLaneClien30.getCanonicalSurfaceCount) === null || _defaultSlowLaneClien31 === void 0 ? void 0 : _defaultSlowLaneClien31.call(_defaultSlowLaneClien30)) !== null && _defaultSlowLaneClien29 !== void 0 ? _defaultSlowLaneClien29 : 0;
254
317
  };