@widgetic/design-system 0.5.40 → 0.5.42
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/README.md +73 -0
- package/dist/components/ContentRefreshOverlay.svelte +103 -0
- package/dist/components/ContentRefreshOverlay.svelte.d.ts +18 -0
- package/dist/components/EmptyState.svelte +6 -2
- package/dist/components/chat/Composer.svelte +380 -19
- package/dist/components/chat/Composer.svelte.d.ts +88 -1
- package/dist/components/chat/PlanCard.svelte +218 -88
- package/dist/components/chat/PlanCard.svelte.d.ts +11 -0
- package/dist/components/chat/QuestionCard.svelte +180 -70
- package/dist/components/chat/QuestionCard.svelte.d.ts +28 -2
- package/dist/components/chat/QuestionCardSummary.svelte +96 -0
- package/dist/components/chat/QuestionCardSummary.svelte.d.ts +20 -0
- package/dist/components/chat/QueueCard.svelte +185 -0
- package/dist/components/chat/QueueCard.svelte.d.ts +40 -0
- package/dist/components/chat/composerLogic.d.ts +31 -0
- package/dist/components/chat/composerLogic.js +37 -0
- package/dist/components/chat/composerReferences.d.ts +28 -0
- package/dist/components/chat/composerReferences.js +23 -0
- package/dist/components/chat/index.d.ts +8 -3
- package/dist/components/chat/index.js +10 -2
- package/dist/components/chat/questionCardLogic.d.ts +40 -3
- package/dist/components/chat/questionCardLogic.js +54 -3
- package/dist/components/chat/queueCardLogic.d.ts +7 -0
- package/dist/components/chat/queueCardLogic.js +5 -0
- package/dist/components/creator/CreatorBar.svelte +54 -1
- package/dist/components/creator/CreatorBar.svelte.d.ts +5 -0
- package/dist/components/creator/index.d.ts +1 -2
- package/dist/components/creator/index.js +2 -0
- package/dist/components/index.d.ts +8 -7
- package/dist/components/index.js +8 -6
- package/dist/components/modals/BaseModal.svelte +23 -4
- package/dist/components/modals/BaseModal.svelte.d.ts +5 -0
- package/dist/components/navigation/breadcrumbSlideIn.d.ts +6 -2
- package/dist/components/navigation/breadcrumbSlideIn.js +6 -6
- package/dist/components/search-input/SearchInput.svelte +317 -14
- package/dist/components/search-input/SearchInput.svelte.d.ts +3 -0
- package/dist/components/search-input/index.d.ts +1 -0
- package/dist/components/search-input/index.js +1 -0
- package/dist/components/search-input/index2.d.ts +4 -0
- package/dist/components/search-input/recentSearches.d.ts +16 -0
- package/dist/components/search-input/recentSearches.js +141 -0
- package/dist/components/shell/NavRow.svelte +53 -10
- package/dist/components/shell/NavRow.svelte.d.ts +23 -2
- package/dist/components/shell/NotificationRow.svelte +3 -2
- package/dist/components/shell/ProfileRow.svelte +6 -4
- package/dist/components/shell/ProfileRow.svelte.d.ts +2 -2
- package/dist/components/shell/SettingsDialog.svelte +23 -1
- package/dist/components/shell/WidgetRow.svelte +4 -3
- package/dist/components/shell/WidgetRow.svelte.d.ts +2 -2
- package/dist/components/toast/ToasterModal.svelte +8 -3
- package/dist/components/toast/ToasterModal.svelte.d.ts +2 -0
- package/dist/components/ui/accordion/accordion-trigger.svelte +19 -0
- package/dist/components/ui/button/button.svelte +4 -2
- package/dist/components/ui/chip/chip.svelte +39 -1
- package/dist/components/ui/chip/chip.svelte.d.ts +8 -0
- package/dist/components/ui/chip/chipProgress.d.ts +14 -0
- package/dist/components/ui/chip/chipProgress.js +21 -0
- package/dist/components/ui/dialog/dialog-content.svelte +24 -2
- package/dist/components/ui/dropdown-menu/dropdown-menu-content.svelte +2 -1
- package/dist/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte +2 -1
- package/dist/components/ui/hover-card/hover-card-content.svelte +2 -1
- package/dist/components/ui/icon-button/icon-button.svelte +2 -0
- package/dist/components/ui/icon-button/icon-button.svelte.d.ts +5 -0
- package/dist/components/ui/number-controls/index.d.ts +2 -0
- package/dist/components/ui/number-controls/index.js +2 -0
- package/dist/components/ui/number-controls/number-controls.svelte +530 -0
- package/dist/components/ui/number-controls/number-controls.svelte.d.ts +46 -0
- package/dist/components/ui/number-controls/numberControlsSteps.d.ts +17 -0
- package/dist/components/ui/number-controls/numberControlsSteps.js +23 -0
- package/dist/components/ui/number-controls/numberControlsValueArea.d.ts +29 -0
- package/dist/components/ui/number-controls/numberControlsValueArea.js +51 -0
- package/dist/components/ui/popover/popover-content.svelte +2 -1
- package/dist/components/ui/select/select-content.svelte +2 -1
- package/dist/components/ui/text-flip/index.d.ts +1 -0
- package/dist/components/ui/text-flip/index.js +1 -0
- package/dist/components/ui/text-flip/text-flip.svelte +103 -0
- package/dist/components/ui/text-flip/text-flip.svelte.d.ts +15 -0
- package/dist/components/ui/text-flip/textFlipLogic.d.ts +49 -0
- package/dist/components/ui/text-flip/textFlipLogic.js +151 -0
- package/dist/components/ui/tooltip/tooltip-content.svelte +1 -1
- package/dist/inline-completion/completionAnswer.d.ts +11 -0
- package/dist/inline-completion/completionAnswer.js +49 -0
- package/dist/inline-completion/completionCache.d.ts +24 -0
- package/dist/inline-completion/completionCache.js +47 -0
- package/dist/inline-completion/completionController.d.ts +61 -0
- package/dist/inline-completion/completionController.js +280 -0
- package/dist/inline-completion/completionKeys.d.ts +21 -0
- package/dist/inline-completion/completionKeys.js +19 -0
- package/dist/inline-completion/completionText.d.ts +53 -0
- package/dist/inline-completion/completionText.js +79 -0
- package/dist/inline-completion/completionTypes.d.ts +71 -0
- package/dist/inline-completion/completionTypes.js +5 -0
- package/dist/inline-completion/gatewayCompletion.d.ts +37 -0
- package/dist/inline-completion/gatewayCompletion.js +77 -0
- package/dist/inline-completion/ghostTap.d.ts +19 -0
- package/dist/inline-completion/ghostTap.js +20 -0
- package/dist/inline-completion/hintMemory.d.ts +43 -0
- package/dist/inline-completion/hintMemory.js +90 -0
- package/dist/inline-completion/httpSource.d.ts +40 -0
- package/dist/inline-completion/httpSource.js +149 -0
- package/dist/inline-completion/index.d.ts +30 -0
- package/dist/inline-completion/index.js +20 -0
- package/dist/inline-completion/inlineCompletionAction.d.ts +54 -0
- package/dist/inline-completion/inlineCompletionAction.js +310 -0
- package/dist/inline-completion/outcomeReporter.d.ts +49 -0
- package/dist/inline-completion/outcomeReporter.js +139 -0
- package/dist/inline-completion/promiseLike.d.ts +9 -0
- package/dist/inline-completion/promiseLike.js +9 -0
- package/dist/prompt-references/ReferenceMenu.svelte +344 -0
- package/dist/prompt-references/ReferenceMenu.svelte.d.ts +26 -0
- package/dist/prompt-references/index.d.ts +27 -0
- package/dist/prompt-references/index.js +20 -0
- package/dist/prompt-references/neutralizingSource.d.ts +21 -0
- package/dist/prompt-references/neutralizingSource.js +29 -0
- package/dist/prompt-references/promptReferences.d.ts +28 -0
- package/dist/prompt-references/promptReferences.js +766 -0
- package/dist/prompt-references/referenceBadge.d.ts +61 -0
- package/dist/prompt-references/referenceBadge.js +82 -0
- package/dist/prompt-references/referenceBadgePainter.d.ts +31 -0
- package/dist/prompt-references/referenceBadgePainter.js +106 -0
- package/dist/prompt-references/referenceEditing.d.ts +60 -0
- package/dist/prompt-references/referenceEditing.js +135 -0
- package/dist/prompt-references/referenceLabels.d.ts +56 -0
- package/dist/prompt-references/referenceLabels.js +215 -0
- package/dist/prompt-references/referenceMeasure.d.ts +12 -0
- package/dist/prompt-references/referenceMeasure.js +63 -0
- package/dist/prompt-references/referenceMenuHost.svelte.d.ts +29 -0
- package/dist/prompt-references/referenceMenuHost.svelte.js +43 -0
- package/dist/prompt-references/referenceMenuKeys.d.ts +26 -0
- package/dist/prompt-references/referenceMenuKeys.js +49 -0
- package/dist/prompt-references/referenceMenuPlacement.d.ts +63 -0
- package/dist/prompt-references/referenceMenuPlacement.js +66 -0
- package/dist/prompt-references/referenceTokens.d.ts +175 -0
- package/dist/prompt-references/referenceTokens.js +332 -0
- package/dist/prompt-references/referenceTrigger.d.ts +52 -0
- package/dist/prompt-references/referenceTrigger.js +100 -0
- package/dist/prompt-references/referenceTypes.d.ts +38 -0
- package/dist/prompt-references/referenceTypes.js +4 -0
- package/dist/prompt-references/sentReferences.d.ts +50 -0
- package/dist/prompt-references/sentReferences.js +122 -0
- package/dist/styles/tokens.css +126 -1
- package/dist/textarea-overlay/index.d.ts +9 -0
- package/dist/textarea-overlay/index.js +7 -0
- package/dist/textarea-overlay/mirrorSegments.d.ts +31 -0
- package/dist/textarea-overlay/mirrorSegments.js +47 -0
- package/dist/textarea-overlay/mirrorSizing.d.ts +39 -0
- package/dist/textarea-overlay/mirrorSizing.js +40 -0
- package/dist/textarea-overlay/textareaOverlay.d.ts +25 -0
- package/dist/textarea-overlay/textareaOverlay.js +277 -0
- package/dist/utils/modalEnterKey.d.ts +139 -0
- package/dist/utils/modalEnterKey.js +276 -0
- package/dist/utils/modalFocusTrap.d.ts +7 -0
- package/dist/utils/modalFocusTrap.js +22 -0
- package/dist/utils/overlayMotion.d.ts +10 -0
- package/dist/utils/overlayMotion.js +12 -3
- package/dist/utils/reducedMotion.d.ts +9 -0
- package/dist/utils/reducedMotion.js +13 -0
- package/dist/utils/revealOpeningSection.d.ts +51 -0
- package/dist/utils/revealOpeningSection.js +109 -0
- package/package.json +16 -1
- package/dist/components/ui/number-stepper/index.d.ts +0 -3
- package/dist/components/ui/number-stepper/index.js +0 -3
- package/dist/components/ui/number-stepper/number-stepper.svelte +0 -212
- package/dist/components/ui/number-stepper/number-stepper.svelte.d.ts +0 -24
- package/dist/components/ui/number-stepper/numberStepperSteps.d.ts +0 -26
- package/dist/components/ui/number-stepper/numberStepperSteps.js +0 -45
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The state machine of the inline completion ("ghost text"): given what the field looks like now, which ghost (if any) shows, when to ask
|
|
3
|
+
* the source, what to do with an answer that comes back late, and what Tab and Esc mean. No DOM and no timers of its own (the host hands
|
|
4
|
+
* the scheduler in), so Node's test runner drives it step by step.
|
|
5
|
+
*
|
|
6
|
+
* How one answer lives (a "session"): the source answers for the text `base`; the continuations are what could follow it. As the person
|
|
7
|
+
* types, the ghost is the rest of the first continuation they are still following (type-through, no request); when they type something
|
|
8
|
+
* a continuation does not say, the session ends with `diverged` and a new request is made for the new text.
|
|
9
|
+
*/
|
|
10
|
+
import { createCompletionCache, DEFAULT_CACHE_TTL_MS } from "./completionCache.js";
|
|
11
|
+
import { emptyAnswer, FAILED_REQUEST_TTL_MS, normalizeAnswer } from "./completionAnswer.js";
|
|
12
|
+
import { isGhostEligible, remainingAfterTyping, requestTextOf, MAX_REQUEST_TEXT_CHARS } from "./completionText.js";
|
|
13
|
+
/** The pause after the last keystroke before a request is made: long enough to skip the middle of a word, short enough to feel live. */
|
|
14
|
+
export const DEFAULT_DEBOUNCE_MS = 150;
|
|
15
|
+
/** How many dismissals in a row (Esc, with nothing accepted in between) stop the ghost for the rest of the session. */
|
|
16
|
+
export const DEFAULT_DISMISSAL_LIMIT = 3;
|
|
17
|
+
/** The ghost for `current` that follows `session`, `'completed'` when `current` already says all of a continuation, else null. */
|
|
18
|
+
function followSession(session, current) {
|
|
19
|
+
for (let rank = 0; rank < session.answer.continuations.length; rank += 1) {
|
|
20
|
+
const remaining = remainingAfterTyping(session.base, session.answer.continuations[rank], current);
|
|
21
|
+
if (remaining === null)
|
|
22
|
+
continue;
|
|
23
|
+
if (remaining === '')
|
|
24
|
+
return 'completed';
|
|
25
|
+
return { text: remaining, rank, source: session.answer.source };
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function defaultSchedule(callback, delayMs) {
|
|
30
|
+
const timer = setTimeout(callback, delayMs);
|
|
31
|
+
return () => clearTimeout(timer);
|
|
32
|
+
}
|
|
33
|
+
export function createCompletionController(options) {
|
|
34
|
+
const debounceMs = Math.max(0, options.debounceMs ?? DEFAULT_DEBOUNCE_MS);
|
|
35
|
+
const dismissalLimit = Math.max(1, Math.floor(options.dismissalLimit ?? DEFAULT_DISMISSAL_LIMIT));
|
|
36
|
+
const maxRequestChars = options.maxRequestChars ?? MAX_REQUEST_TEXT_CHARS;
|
|
37
|
+
const cache = options.cache ?? createCompletionCache();
|
|
38
|
+
const schedule = options.schedule ?? defaultSchedule;
|
|
39
|
+
const log = options.log ?? (() => { });
|
|
40
|
+
const report = (event) => options.onEvent?.(event);
|
|
41
|
+
let destroyed = false;
|
|
42
|
+
let paused = false;
|
|
43
|
+
let session = null;
|
|
44
|
+
let ghost = null;
|
|
45
|
+
// The last field the host told us about: an answer is judged against it, not against the text the request was made for.
|
|
46
|
+
let latestField = null;
|
|
47
|
+
let cancelTimer = null;
|
|
48
|
+
// The request text a pending timer will ask about (so the same text, seen again, does not restart the pause).
|
|
49
|
+
let pendingText = null;
|
|
50
|
+
let inFlight = null;
|
|
51
|
+
let consecutiveDismissals = 0;
|
|
52
|
+
// The text at which Esc was pressed: no new ghost until the text changes from it.
|
|
53
|
+
let dismissedAtText = null;
|
|
54
|
+
// Right after Tab the next request does not wait for the pause: the person just took a step and the next one is usually obvious.
|
|
55
|
+
let askWithoutPause = false;
|
|
56
|
+
// A failed request is logged once per distinct reason, not once per keystroke.
|
|
57
|
+
const reportedFailures = new Set();
|
|
58
|
+
function setGhost(next) {
|
|
59
|
+
const same = ghost === next ||
|
|
60
|
+
(ghost !== null &&
|
|
61
|
+
next !== null &&
|
|
62
|
+
ghost.text === next.text &&
|
|
63
|
+
ghost.rank === next.rank &&
|
|
64
|
+
ghost.source === next.source);
|
|
65
|
+
if (same)
|
|
66
|
+
return;
|
|
67
|
+
ghost = next;
|
|
68
|
+
options.onChange?.(next);
|
|
69
|
+
}
|
|
70
|
+
function cancelPendingTimer() {
|
|
71
|
+
cancelTimer?.();
|
|
72
|
+
cancelTimer = null;
|
|
73
|
+
pendingText = null;
|
|
74
|
+
}
|
|
75
|
+
function cancelPendingRequest() {
|
|
76
|
+
cancelPendingTimer();
|
|
77
|
+
if (inFlight) {
|
|
78
|
+
inFlight.abort.abort();
|
|
79
|
+
inFlight = null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/** Show what `session` says for `currentText`; ends the session (with `diverged` when a ghost was showing) when it says nothing. */
|
|
83
|
+
function applyFollowing(currentText) {
|
|
84
|
+
if (!session)
|
|
85
|
+
return false;
|
|
86
|
+
const next = followSession(session, currentText);
|
|
87
|
+
if (next !== null && next !== 'completed') {
|
|
88
|
+
setGhost(next);
|
|
89
|
+
if (!session.shownReported) {
|
|
90
|
+
session.shownReported = true;
|
|
91
|
+
report({ type: 'shown', source: next.source });
|
|
92
|
+
log('ghost shown', { source: next.source, rank: next.rank, chars: next.text.length });
|
|
93
|
+
}
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
if (next === 'completed') {
|
|
97
|
+
// The whole suggestion was typed out: the person followed it, which is neither a dismissal nor a divergence.
|
|
98
|
+
consecutiveDismissals = 0;
|
|
99
|
+
log('ghost typed in full');
|
|
100
|
+
}
|
|
101
|
+
else if (ghost !== null) {
|
|
102
|
+
report({ type: 'diverged', source: ghost.source });
|
|
103
|
+
log('ghost diverged', { source: ghost.source });
|
|
104
|
+
}
|
|
105
|
+
session = null;
|
|
106
|
+
setGhost(null);
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
/** Make `answer` the session for `base`, and show its ghost for the text the field holds now. */
|
|
110
|
+
function beginSession(base, answer, currentText) {
|
|
111
|
+
session = { base, answer, shownReported: false };
|
|
112
|
+
return applyFollowing(currentText);
|
|
113
|
+
}
|
|
114
|
+
/** Ask the source for the text the field holds now (called when the pause after the last keystroke is over). */
|
|
115
|
+
async function fire() {
|
|
116
|
+
const snapshot = latestField;
|
|
117
|
+
if (destroyed || !snapshot || !isGhostEligible(snapshot))
|
|
118
|
+
return;
|
|
119
|
+
const requestText = requestTextOf(snapshot.value, maxRequestChars);
|
|
120
|
+
inFlight?.abort.abort();
|
|
121
|
+
const abort = new AbortController();
|
|
122
|
+
inFlight = { abort, text: requestText };
|
|
123
|
+
let answer;
|
|
124
|
+
try {
|
|
125
|
+
const like = await options.source.suggest({ text: requestText, signal: abort.signal });
|
|
126
|
+
answer = normalizeAnswer(like, DEFAULT_CACHE_TTL_MS);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
if (abort.signal.aborted)
|
|
130
|
+
return;
|
|
131
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
132
|
+
if (!reportedFailures.has(reason)) {
|
|
133
|
+
reportedFailures.add(reason);
|
|
134
|
+
log('request failed', { reason });
|
|
135
|
+
}
|
|
136
|
+
answer = emptyAnswer(FAILED_REQUEST_TTL_MS);
|
|
137
|
+
}
|
|
138
|
+
// Dropped: a newer request went out, the person moved away (or paused the ghost), or the controller is gone. Each of those
|
|
139
|
+
// aborts this request's signal, so the signal alone says whether the answer is still wanted, and the field is as it was
|
|
140
|
+
// when the request left: eligible.
|
|
141
|
+
if (abort.signal.aborted)
|
|
142
|
+
return;
|
|
143
|
+
inFlight = null;
|
|
144
|
+
cache.set(requestText, answer, answer.ttlMs);
|
|
145
|
+
const current = latestField;
|
|
146
|
+
if (!current)
|
|
147
|
+
return;
|
|
148
|
+
// The person may have typed on since the request left: follow the answer from the text it was made for.
|
|
149
|
+
if (beginSession(snapshot.value, answer, current.value)) {
|
|
150
|
+
// A request made for a later keystroke has nothing to add to a ghost that already shows.
|
|
151
|
+
cancelPendingTimer();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
/** Look for a ghost for `text`: the answer already received if there is one, else ask once the person pauses. */
|
|
155
|
+
function requestFor(text) {
|
|
156
|
+
const withoutPause = askWithoutPause;
|
|
157
|
+
askWithoutPause = false;
|
|
158
|
+
const requestText = requestTextOf(text, maxRequestChars);
|
|
159
|
+
const cached = cache.get(requestText);
|
|
160
|
+
if (cached) {
|
|
161
|
+
cancelPendingRequest();
|
|
162
|
+
beginSession(text, cached, text);
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
// The same text again (a caret move, a key that changed nothing) must not restart the pause or send a second request.
|
|
166
|
+
if (inFlight?.text === requestText || (cancelTimer !== null && pendingText === requestText))
|
|
167
|
+
return;
|
|
168
|
+
cancelPendingTimer();
|
|
169
|
+
pendingText = requestText;
|
|
170
|
+
cancelTimer = schedule(() => {
|
|
171
|
+
cancelTimer = null;
|
|
172
|
+
pendingText = null;
|
|
173
|
+
fire().catch((error) => {
|
|
174
|
+
log('request handling failed', {
|
|
175
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
}, withoutPause ? 0 : debounceMs);
|
|
179
|
+
}
|
|
180
|
+
function update(field) {
|
|
181
|
+
if (destroyed)
|
|
182
|
+
return;
|
|
183
|
+
latestField = field;
|
|
184
|
+
if (paused || !isGhostEligible(field)) {
|
|
185
|
+
// Typing is not happening here (blur, caret moved, selection, composition) or the person asked not to be offered more: no
|
|
186
|
+
// ghost, no request. The answers are kept: the caret may come back to a text one fits.
|
|
187
|
+
setGhost(null);
|
|
188
|
+
cancelPendingRequest();
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const text = field.value;
|
|
192
|
+
if (dismissedAtText !== null) {
|
|
193
|
+
if (dismissedAtText === text) {
|
|
194
|
+
setGhost(null);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
dismissedAtText = null;
|
|
198
|
+
}
|
|
199
|
+
if (session && applyFollowing(text))
|
|
200
|
+
return;
|
|
201
|
+
// No ghost follows this text: look for one.
|
|
202
|
+
requestFor(text);
|
|
203
|
+
}
|
|
204
|
+
function accept() {
|
|
205
|
+
if (destroyed || ghost === null)
|
|
206
|
+
return null;
|
|
207
|
+
const accepted = ghost;
|
|
208
|
+
consecutiveDismissals = 0;
|
|
209
|
+
dismissedAtText = null;
|
|
210
|
+
report({ type: 'accepted', source: accepted.source, rank: accepted.rank });
|
|
211
|
+
log('ghost accepted', {
|
|
212
|
+
source: accepted.source,
|
|
213
|
+
rank: accepted.rank,
|
|
214
|
+
chars: accepted.text.length
|
|
215
|
+
});
|
|
216
|
+
session = null;
|
|
217
|
+
setGhost(null);
|
|
218
|
+
askWithoutPause = true;
|
|
219
|
+
return { insert: accepted.text, rank: accepted.rank };
|
|
220
|
+
}
|
|
221
|
+
function dismiss() {
|
|
222
|
+
if (destroyed || ghost === null)
|
|
223
|
+
return false;
|
|
224
|
+
const dismissed = ghost;
|
|
225
|
+
consecutiveDismissals += 1;
|
|
226
|
+
dismissedAtText = latestField?.value ?? null;
|
|
227
|
+
report({ type: 'dismissed', source: dismissed.source });
|
|
228
|
+
log('ghost dismissed', { source: dismissed.source, inARow: consecutiveDismissals });
|
|
229
|
+
session = null;
|
|
230
|
+
setGhost(null);
|
|
231
|
+
cancelPendingRequest();
|
|
232
|
+
if (consecutiveDismissals >= dismissalLimit && !paused) {
|
|
233
|
+
paused = true;
|
|
234
|
+
report({ type: 'paused' });
|
|
235
|
+
log('ghost paused', { dismissalLimit });
|
|
236
|
+
}
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
function resume() {
|
|
240
|
+
if (destroyed)
|
|
241
|
+
return;
|
|
242
|
+
paused = false;
|
|
243
|
+
consecutiveDismissals = 0;
|
|
244
|
+
dismissedAtText = null;
|
|
245
|
+
if (latestField)
|
|
246
|
+
update(latestField);
|
|
247
|
+
}
|
|
248
|
+
function reset() {
|
|
249
|
+
if (destroyed)
|
|
250
|
+
return;
|
|
251
|
+
cancelPendingRequest();
|
|
252
|
+
cache.clear();
|
|
253
|
+
session = null;
|
|
254
|
+
dismissedAtText = null;
|
|
255
|
+
askWithoutPause = false;
|
|
256
|
+
setGhost(null);
|
|
257
|
+
}
|
|
258
|
+
function destroy() {
|
|
259
|
+
if (destroyed)
|
|
260
|
+
return;
|
|
261
|
+
destroyed = true;
|
|
262
|
+
cancelPendingRequest();
|
|
263
|
+
session = null;
|
|
264
|
+
ghost = null;
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
update,
|
|
268
|
+
accept,
|
|
269
|
+
dismiss,
|
|
270
|
+
resume,
|
|
271
|
+
reset,
|
|
272
|
+
destroy,
|
|
273
|
+
get ghost() {
|
|
274
|
+
return ghost;
|
|
275
|
+
},
|
|
276
|
+
get paused() {
|
|
277
|
+
return paused;
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a key press means to the ghost, as a pure function so the one rule that matters is tested without a DOM: Tab and Esc are the
|
|
3
|
+
* ghost's ONLY while a ghost shows. With none showing, Tab is the browser's (focus moves on) and Esc is the page's (a dialog closes), so
|
|
4
|
+
* neither is ever swallowed for nothing.
|
|
5
|
+
*/
|
|
6
|
+
/** The part of a keyboard event the decision reads. */
|
|
7
|
+
export interface KeyPress {
|
|
8
|
+
key: string;
|
|
9
|
+
shiftKey: boolean;
|
|
10
|
+
ctrlKey: boolean;
|
|
11
|
+
altKey: boolean;
|
|
12
|
+
metaKey: boolean;
|
|
13
|
+
/** True while an IME composition is in progress: the keys belong to the composition then. */
|
|
14
|
+
isComposing: boolean;
|
|
15
|
+
}
|
|
16
|
+
export type GhostKeyAction = 'accept' | 'dismiss';
|
|
17
|
+
/**
|
|
18
|
+
* `accept` for a plain Tab and `dismiss` for Esc while a ghost shows; null for everything else (the key does what it always does).
|
|
19
|
+
* A Tab with Shift (focus backwards), Ctrl, Alt or Meta is never an acceptance.
|
|
20
|
+
*/
|
|
21
|
+
export declare function interpretGhostKey(press: KeyPress, ghostShowing: boolean): GhostKeyAction | null;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a key press means to the ghost, as a pure function so the one rule that matters is tested without a DOM: Tab and Esc are the
|
|
3
|
+
* ghost's ONLY while a ghost shows. With none showing, Tab is the browser's (focus moves on) and Esc is the page's (a dialog closes), so
|
|
4
|
+
* neither is ever swallowed for nothing.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* `accept` for a plain Tab and `dismiss` for Esc while a ghost shows; null for everything else (the key does what it always does).
|
|
8
|
+
* A Tab with Shift (focus backwards), Ctrl, Alt or Meta is never an acceptance.
|
|
9
|
+
*/
|
|
10
|
+
export function interpretGhostKey(press, ghostShowing) {
|
|
11
|
+
if (!ghostShowing || press.isComposing)
|
|
12
|
+
return null;
|
|
13
|
+
if (press.key === 'Tab') {
|
|
14
|
+
return press.shiftKey || press.ctrlKey || press.altKey || press.metaKey ? null : 'accept';
|
|
15
|
+
}
|
|
16
|
+
if (press.key === 'Escape')
|
|
17
|
+
return 'dismiss';
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text rules of the inline completion ("ghost text"): how a suggestion is cleaned before it is painted, how what the person types
|
|
3
|
+
* next is held against it (type-through), and what a request is allowed to carry. No DOM, so Node's test runner loads this file.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the gateway's own (`composerSuggestPolicy.ts`: `normalizeForMatch`): composed characters, runs of blanks as one
|
|
6
|
+
* space, case ignored. A person who types "V" where the suggestion says "v" is still following it, and so is one who types two spaces
|
|
7
|
+
* where it has one.
|
|
8
|
+
*/
|
|
9
|
+
/** The longest suggestion that is painted, in characters. The catalogs hold short phrases; anything longer is not one of theirs. */
|
|
10
|
+
export declare const MAX_GHOST_CHARS = 200;
|
|
11
|
+
/** The most characters of the box a request carries: the end of the text, up to the caret (the gateway reads far less of it). */
|
|
12
|
+
export declare const MAX_REQUEST_TEXT_CHARS = 2000;
|
|
13
|
+
/** Text with composed characters and every run of blanks (spaces, tabs, line breaks) as one space. Case is kept. */
|
|
14
|
+
export declare function collapseBlanks(text: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* A suggestion the way the box can paint it: one line, runs of blanks as one space, the end trimmed. A space at its START is kept: it is
|
|
17
|
+
* how a suggestion continues a word that is complete ("video player" then " with thumbnails"). Anything that is not text, holds nothing
|
|
18
|
+
* but blanks, or is longer than `maxChars` is not a suggestion: the result is the empty string.
|
|
19
|
+
*/
|
|
20
|
+
export declare function cleanGhost(raw: unknown, maxChars?: number): string;
|
|
21
|
+
/**
|
|
22
|
+
* What is left of a suggestion after the person typed on: the suggestion is `ghost`, which was made for a box holding `base`; the box now
|
|
23
|
+
* holds `current`.
|
|
24
|
+
*
|
|
25
|
+
* - The suggestion as a whole sentence is `base` plus `ghost`; the text now is `current`. When the text now is still the beginning of
|
|
26
|
+
* that sentence (case ignored, blanks collapsed) what remains is returned: a shorter ghost, and the empty string when the whole
|
|
27
|
+
* suggestion was typed.
|
|
28
|
+
* - `null` when it is not: the person typed something else, edited the text before the caret, or typed past the end.
|
|
29
|
+
*/
|
|
30
|
+
export declare function remainingAfterTyping(base: string, ghost: string, current: string): string | null;
|
|
31
|
+
/**
|
|
32
|
+
* The part of the box a request carries: its end, at most `maxChars` characters, never starting in the middle of a surrogate pair (half
|
|
33
|
+
* an emoji is not text).
|
|
34
|
+
*/
|
|
35
|
+
export declare function requestTextOf(value: string, maxChars?: number): string;
|
|
36
|
+
/** What the controller needs to know about the field to decide whether a ghost may show. */
|
|
37
|
+
export interface FieldSnapshot {
|
|
38
|
+
/** The whole text of the field. */
|
|
39
|
+
value: string;
|
|
40
|
+
selectionStart: number | null;
|
|
41
|
+
selectionEnd: number | null;
|
|
42
|
+
/** Whether the field has the focus. */
|
|
43
|
+
focused: boolean;
|
|
44
|
+
/** Whether an IME composition is in progress. */
|
|
45
|
+
composing: boolean;
|
|
46
|
+
disabled?: boolean;
|
|
47
|
+
readOnly?: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Whether a ghost may show for the field: focused, no composition, not disabled or read-only, and the caret collapsed at the very end
|
|
51
|
+
* of the text (a ghost is the rest of what is being typed, so it only makes sense where typing happens).
|
|
52
|
+
*/
|
|
53
|
+
export declare function isGhostEligible(field: FieldSnapshot): boolean;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure text rules of the inline completion ("ghost text"): how a suggestion is cleaned before it is painted, how what the person types
|
|
3
|
+
* next is held against it (type-through), and what a request is allowed to carry. No DOM, so Node's test runner loads this file.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the gateway's own (`composerSuggestPolicy.ts`: `normalizeForMatch`): composed characters, runs of blanks as one
|
|
6
|
+
* space, case ignored. A person who types "V" where the suggestion says "v" is still following it, and so is one who types two spaces
|
|
7
|
+
* where it has one.
|
|
8
|
+
*/
|
|
9
|
+
/** The longest suggestion that is painted, in characters. The catalogs hold short phrases; anything longer is not one of theirs. */
|
|
10
|
+
export const MAX_GHOST_CHARS = 200;
|
|
11
|
+
/** The most characters of the box a request carries: the end of the text, up to the caret (the gateway reads far less of it). */
|
|
12
|
+
export const MAX_REQUEST_TEXT_CHARS = 2000;
|
|
13
|
+
/** Text with composed characters and every run of blanks (spaces, tabs, line breaks) as one space. Case is kept. */
|
|
14
|
+
export function collapseBlanks(text) {
|
|
15
|
+
return text.normalize('NFC').replace(/\s+/g, ' ');
|
|
16
|
+
}
|
|
17
|
+
/** Whether two texts are the same letters, ignoring case. */
|
|
18
|
+
function sameLetters(left, right) {
|
|
19
|
+
return left.toLowerCase() === right.toLowerCase();
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A suggestion the way the box can paint it: one line, runs of blanks as one space, the end trimmed. A space at its START is kept: it is
|
|
23
|
+
* how a suggestion continues a word that is complete ("video player" then " with thumbnails"). Anything that is not text, holds nothing
|
|
24
|
+
* but blanks, or is longer than `maxChars` is not a suggestion: the result is the empty string.
|
|
25
|
+
*/
|
|
26
|
+
export function cleanGhost(raw, maxChars = MAX_GHOST_CHARS) {
|
|
27
|
+
if (typeof raw !== 'string')
|
|
28
|
+
return '';
|
|
29
|
+
// A control character other than a blank is not something to paint (the blanks are collapsed below).
|
|
30
|
+
// eslint-disable-next-line no-control-regex
|
|
31
|
+
if (/[\u0000-\u0008\u000e-\u001f\u007f]/.test(raw))
|
|
32
|
+
return '';
|
|
33
|
+
// Nothing but blanks collapses to one space and loses it with the rest of the end: the empty string, which is no suggestion.
|
|
34
|
+
const collapsed = collapseBlanks(raw).replace(/\s+$/, '');
|
|
35
|
+
return collapsed.length > Math.max(1, Math.floor(maxChars)) ? '' : collapsed;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* What is left of a suggestion after the person typed on: the suggestion is `ghost`, which was made for a box holding `base`; the box now
|
|
39
|
+
* holds `current`.
|
|
40
|
+
*
|
|
41
|
+
* - The suggestion as a whole sentence is `base` plus `ghost`; the text now is `current`. When the text now is still the beginning of
|
|
42
|
+
* that sentence (case ignored, blanks collapsed) what remains is returned: a shorter ghost, and the empty string when the whole
|
|
43
|
+
* suggestion was typed.
|
|
44
|
+
* - `null` when it is not: the person typed something else, edited the text before the caret, or typed past the end.
|
|
45
|
+
*/
|
|
46
|
+
export function remainingAfterTyping(base, ghost, current) {
|
|
47
|
+
if (!current.startsWith(base))
|
|
48
|
+
return null;
|
|
49
|
+
const sentence = collapseBlanks(base + ghost);
|
|
50
|
+
const typed = collapseBlanks(current);
|
|
51
|
+
// Typed past the end of the sentence: its beginning is shorter than what was typed, and `sameLetters` says they differ.
|
|
52
|
+
return sameLetters(sentence.slice(0, typed.length), typed) ? sentence.slice(typed.length) : null;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The part of the box a request carries: its end, at most `maxChars` characters, never starting in the middle of a surrogate pair (half
|
|
56
|
+
* an emoji is not text).
|
|
57
|
+
*/
|
|
58
|
+
export function requestTextOf(value, maxChars = MAX_REQUEST_TEXT_CHARS) {
|
|
59
|
+
const limit = Math.max(1, Math.floor(maxChars));
|
|
60
|
+
if (value.length <= limit)
|
|
61
|
+
return value;
|
|
62
|
+
let start = value.length - limit;
|
|
63
|
+
const unit = value.charCodeAt(start);
|
|
64
|
+
// A low surrogate at the cut belongs to the pair before it, which is cut off: drop it too.
|
|
65
|
+
if (unit >= 0xdc00 && unit <= 0xdfff)
|
|
66
|
+
start += 1;
|
|
67
|
+
return value.slice(start);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Whether a ghost may show for the field: focused, no composition, not disabled or read-only, and the caret collapsed at the very end
|
|
71
|
+
* of the text (a ghost is the rest of what is being typed, so it only makes sense where typing happens).
|
|
72
|
+
*/
|
|
73
|
+
export function isGhostEligible(field) {
|
|
74
|
+
if (!field.focused || field.composing || field.disabled === true || field.readOnly === true)
|
|
75
|
+
return false;
|
|
76
|
+
// A caret at the very end: both ends of the selection are the end of the text. A field that has no caret reports null, which is
|
|
77
|
+
// never the end of the text.
|
|
78
|
+
return field.selectionStart === field.value.length && field.selectionEnd === field.value.length;
|
|
79
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types of the inline completion ("ghost text"): what a source is asked, what it answers, and what the box reports back.
|
|
3
|
+
* Types only, so a host can import them without loading anything.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Where the order of an answer came from: `jev` (the gateway asked Jev), `prior` (the catalog's own order), `disabled` (nothing to
|
|
7
|
+
* show: the surface is off, the language has no catalog, or the request failed), `unknown` (a source that does not say).
|
|
8
|
+
*/
|
|
9
|
+
export type CompletionSourceName = 'jev' | 'prior' | 'disabled' | 'unknown';
|
|
10
|
+
/** What a source is asked: the end of the box's text (up to the caret) and a signal that fires when the answer is no longer wanted. */
|
|
11
|
+
export interface InlineCompletionRequest {
|
|
12
|
+
text: string;
|
|
13
|
+
signal: AbortSignal;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What a source may answer: the continuations alone (strings to append, best first), or those with how long the answer may be kept
|
|
17
|
+
* and where its order came from. Anything in `continuations` that is not a usable string is dropped by the engine.
|
|
18
|
+
*/
|
|
19
|
+
export type CompletionAnswerLike = readonly string[] | {
|
|
20
|
+
continuations: readonly unknown[];
|
|
21
|
+
ttlMs?: number;
|
|
22
|
+
source?: string;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Where suggestions come from. `suggest` resolves with the continuations of the text it was given. It should not reject; a rejection is
|
|
26
|
+
* the same as "nothing to show", logged once. A signal that fires means the answer is no longer wanted (the person typed on).
|
|
27
|
+
*/
|
|
28
|
+
export interface InlineCompletionSource {
|
|
29
|
+
suggest(request: InlineCompletionRequest): Promise<CompletionAnswerLike>;
|
|
30
|
+
}
|
|
31
|
+
/** One answer, after the engine cleaned it. */
|
|
32
|
+
export interface CompletionAnswer {
|
|
33
|
+
/** Continuations to append, best first: each one line, blanks collapsed, never empty. */
|
|
34
|
+
continuations: readonly string[];
|
|
35
|
+
/** How long the answer may be kept for the exact text it was made for. */
|
|
36
|
+
ttlMs: number;
|
|
37
|
+
source: CompletionSourceName;
|
|
38
|
+
}
|
|
39
|
+
/** The ghost that is showing: what is left of the continuation, its place in the answer, and where the answer's order came from. */
|
|
40
|
+
export interface GhostView {
|
|
41
|
+
text: string;
|
|
42
|
+
/** Zero for the first continuation of the answer; above zero when typing ruled the first out and a later one still fits. */
|
|
43
|
+
rank: number;
|
|
44
|
+
source: CompletionSourceName;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* What happened to the ghosts, one event per transition, for a host that measures them (`createOutcomeReporter`).
|
|
48
|
+
* - `shown`: a ghost appeared (once per answer, however often it shrinks or the caret leaves and returns).
|
|
49
|
+
* - `accepted`: Tab (or a tap) put it in the text; `rank` above zero means it was not the first continuation.
|
|
50
|
+
* - `dismissed`: Esc.
|
|
51
|
+
* - `diverged`: the person typed on, past what the ghost said (or edited the text before the caret).
|
|
52
|
+
* - `paused`: three dismissals in a row; no ghost shows again in this session.
|
|
53
|
+
*/
|
|
54
|
+
export type InlineCompletionEvent = {
|
|
55
|
+
type: 'shown';
|
|
56
|
+
source: CompletionSourceName;
|
|
57
|
+
} | {
|
|
58
|
+
type: 'accepted';
|
|
59
|
+
source: CompletionSourceName;
|
|
60
|
+
rank: number;
|
|
61
|
+
} | {
|
|
62
|
+
type: 'dismissed';
|
|
63
|
+
source: CompletionSourceName;
|
|
64
|
+
} | {
|
|
65
|
+
type: 'diverged';
|
|
66
|
+
source: CompletionSourceName;
|
|
67
|
+
} | {
|
|
68
|
+
type: 'paused';
|
|
69
|
+
};
|
|
70
|
+
/** A debug line: the engine says what it did, once per transition. */
|
|
71
|
+
export type InlineCompletionLogger = (message: string, detail?: Record<string, unknown>) => void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { InlineCompletionEvent, InlineCompletionLogger, InlineCompletionSource } from './completionTypes.ts';
|
|
2
|
+
import type { MaybePromise } from './promiseLike.ts';
|
|
3
|
+
/** The language a box is asked in when the host's own is not a language tag the gateway reads (two to thirty-five characters). */
|
|
4
|
+
export declare const FALLBACK_LOCALE = "en";
|
|
5
|
+
export interface GatewayCompletionOptions {
|
|
6
|
+
/** The surface the box belongs to, as the gateway names it (`embed_assistant`, `workflow_assistant`): what is counted is counted under it. */
|
|
7
|
+
surface: string;
|
|
8
|
+
/** The suggest route of the box. A function is read at every request. */
|
|
9
|
+
suggestUrl: string | (() => string);
|
|
10
|
+
/** The route the counts of what happened to the suggestions go to. Without it nothing is reported (the box still suggests). */
|
|
11
|
+
outcomeUrl?: string | (() => string);
|
|
12
|
+
/** Headers besides `Content-Type`, for the suggest route and the outcome route alike. May be asynchronous (a token refreshed on demand). */
|
|
13
|
+
headers?: () => MaybePromise<Record<string, string>>;
|
|
14
|
+
/** `credentials` of both requests. A host whose requests carry a token and not a cookie says `omit`. Default `include`, as the pieces below. */
|
|
15
|
+
credentials?: RequestCredentials;
|
|
16
|
+
/** The language of the box as a BCP 47 tag ("en", "pt-BR"), read at every request. */
|
|
17
|
+
locale: () => string;
|
|
18
|
+
/** What the box knows about itself (the gateway names the fields per surface), read at every request. Nothing, or no fields: no `context` is sent. */
|
|
19
|
+
context?: () => Record<string, unknown> | undefined;
|
|
20
|
+
/** The `fetch` to use: the host's own. Defaults to the global one. */
|
|
21
|
+
fetchImpl?: typeof fetch;
|
|
22
|
+
/** How long a request may take. Default as `createHttpCompletionSource`. */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
/** A debug line per kind of failure, once. Silent by default. */
|
|
25
|
+
log?: InlineCompletionLogger;
|
|
26
|
+
}
|
|
27
|
+
export interface GatewayCompletion {
|
|
28
|
+
/** What `use:inlineCompletion` asks for suggestions. */
|
|
29
|
+
source: InlineCompletionSource;
|
|
30
|
+
/** What `use:inlineCompletion` tells about what happened to them (`onEvent`). Counts them when there is an outcome route, else does nothing. */
|
|
31
|
+
onEvent: (event: InlineCompletionEvent) => void;
|
|
32
|
+
/** Reports what is counted, and stops listening to the page. For when the box goes away. Safe to call twice. */
|
|
33
|
+
destroy: () => void;
|
|
34
|
+
}
|
|
35
|
+
/** The language tag as the gateway wants it: the host's own when it is one, else English (a tag it refuses would make every request fail). */
|
|
36
|
+
export declare function localeForGateway(tag: unknown): string;
|
|
37
|
+
export declare function createGatewayCompletion(options: GatewayCompletionOptions): GatewayCompletion;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The ghost text of a box that one of the Widgetic gateway's suggest routes serves (the embed assistant's, the workflows assistant's):
|
|
3
|
+
* the source that asks the route and the counting of what happened to what it showed, joined, so a host wires a box with one call and
|
|
4
|
+
* gives only what it alone knows: the routes, the credentials, the language, and what the box knows about itself.
|
|
5
|
+
*
|
|
6
|
+
* The body of a request is `{ locale, text }` and, when the box has something to say about itself, `{ context }`. Both are read at every
|
|
7
|
+
* request, so a box whose language or context changes is always asked with its current one. A context that cannot be read is asked
|
|
8
|
+
* without (a suggestion without it is still a suggestion), and said once.
|
|
9
|
+
*
|
|
10
|
+
* Nothing here rejects, and nothing here shows anything: a box that gets no `outcomeUrl` suggests all the same and reports nothing.
|
|
11
|
+
*/
|
|
12
|
+
import { createHttpCompletionSource } from "./httpSource.js";
|
|
13
|
+
import { createOutcomeReporter } from "./outcomeReporter.js";
|
|
14
|
+
/** The language a box is asked in when the host's own is not a language tag the gateway reads (two to thirty-five characters). */
|
|
15
|
+
export const FALLBACK_LOCALE = 'en';
|
|
16
|
+
/** The shortest and the longest language tag the gateway accepts. */
|
|
17
|
+
const MIN_LOCALE_LENGTH = 2;
|
|
18
|
+
const MAX_LOCALE_LENGTH = 35;
|
|
19
|
+
/** The language tag as the gateway wants it: the host's own when it is one, else English (a tag it refuses would make every request fail). */
|
|
20
|
+
export function localeForGateway(tag) {
|
|
21
|
+
const trimmed = typeof tag === 'string' ? tag.trim() : '';
|
|
22
|
+
return trimmed.length >= MIN_LOCALE_LENGTH && trimmed.length <= MAX_LOCALE_LENGTH
|
|
23
|
+
? trimmed
|
|
24
|
+
: FALLBACK_LOCALE;
|
|
25
|
+
}
|
|
26
|
+
export function createGatewayCompletion(options) {
|
|
27
|
+
const log = options.log ?? (() => { });
|
|
28
|
+
// A context that cannot be read is said once, not at every keystroke.
|
|
29
|
+
let contextFailureReported = false;
|
|
30
|
+
function readContext() {
|
|
31
|
+
if (!options.context)
|
|
32
|
+
return undefined;
|
|
33
|
+
try {
|
|
34
|
+
const context = options.context();
|
|
35
|
+
return context && Object.keys(context).length > 0 ? context : undefined;
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
if (!contextFailureReported) {
|
|
39
|
+
contextFailureReported = true;
|
|
40
|
+
log('suggest context could not be read; the box is asked without it', {
|
|
41
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
const reporter = options.outcomeUrl
|
|
48
|
+
? createOutcomeReporter({
|
|
49
|
+
surface: options.surface,
|
|
50
|
+
url: options.outcomeUrl,
|
|
51
|
+
headers: options.headers,
|
|
52
|
+
credentials: options.credentials,
|
|
53
|
+
fetchImpl: options.fetchImpl,
|
|
54
|
+
log
|
|
55
|
+
})
|
|
56
|
+
: null;
|
|
57
|
+
const source = createHttpCompletionSource({
|
|
58
|
+
url: options.suggestUrl,
|
|
59
|
+
body: () => {
|
|
60
|
+
const context = readContext();
|
|
61
|
+
return {
|
|
62
|
+
locale: localeForGateway(options.locale()),
|
|
63
|
+
...(context ? { context } : {})
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
headers: options.headers,
|
|
67
|
+
credentials: options.credentials,
|
|
68
|
+
fetchImpl: options.fetchImpl,
|
|
69
|
+
timeoutMs: options.timeoutMs,
|
|
70
|
+
log
|
|
71
|
+
});
|
|
72
|
+
return {
|
|
73
|
+
source,
|
|
74
|
+
onEvent: (event) => reporter?.record(event),
|
|
75
|
+
destroy: () => reporter?.destroy()
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a finger's tap landed on the ghost. A phone has no Tab key, so touching the grey text takes it. The ghost lives on a layer
|
|
3
|
+
* that ignores pointers (the box underneath must keep getting them), so the tap arrives at the box itself: the action asks the
|
|
4
|
+
* page where the ghost's lines are, and this decides whether the tap was on one of them. Pure, so it runs under `node --test`.
|
|
5
|
+
*/
|
|
6
|
+
/** A box on the screen, in the viewport's coordinates (what `getClientRects()` and a pointer event both use). */
|
|
7
|
+
export interface TapRect {
|
|
8
|
+
left: number;
|
|
9
|
+
top: number;
|
|
10
|
+
right: number;
|
|
11
|
+
bottom: number;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* How far outside a line of the ghost a tap still counts: a fingertip is not a pixel, and the ghost's lines are one text line high.
|
|
15
|
+
* Small on purpose: a tap just beside the ghost is a tap on the typed text before it, which moves the caret.
|
|
16
|
+
*/
|
|
17
|
+
export declare const GHOST_TAP_SLOP_PX = 4;
|
|
18
|
+
/** True when the point (x, y) is on one of the ghost's lines, give or take the slop. A rect with no area (a ghost with no text) never counts. */
|
|
19
|
+
export declare function isTapOnGhost(x: number, y: number, rects: readonly TapRect[], slopPx?: number): boolean;
|