@widgetic/design-system 0.5.39 → 0.5.41
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/chat/Composer.svelte +381 -20
- 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/index.d.ts +1 -2
- package/dist/components/creator/index.js +2 -0
- package/dist/components/filters/FilterByButton.svelte +2 -1
- package/dist/components/index.d.ts +8 -7
- package/dist/components/index.js +8 -6
- package/dist/components/modals/BaseModal.svelte +14 -4
- package/dist/components/navigation/Breadcrumbs.svelte +1 -1
- package/dist/components/navigation/breadcrumbSlideIn.d.ts +6 -2
- package/dist/components/navigation/breadcrumbSlideIn.js +6 -6
- package/dist/components/search-input/SearchInput.svelte +282 -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 +12 -0
- package/dist/components/search-input/recentSearches.js +124 -0
- package/dist/components/toast/ToasterModal.svelte +8 -2
- package/dist/components/toast/ToasterModal.svelte.d.ts +2 -0
- 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/dropdown-menu/dropdown-menu-content.svelte +1 -1
- package/dist/components/ui/dropdown-menu/dropdown-menu-sub-content.svelte +1 -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/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/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 +58 -0
- 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/overlayMotion.d.ts +1 -0
- package/dist/utils/overlayMotion.js +3 -3
- package/dist/utils/reducedMotion.d.ts +9 -0
- package/dist/utils/reducedMotion.js +13 -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,310 @@
|
|
|
1
|
+
import { acquireTextareaOverlay } from "../textarea-overlay/textareaOverlay.js";
|
|
2
|
+
import { createCompletionController } from "./completionController.js";
|
|
3
|
+
import { interpretGhostKey } from "./completionKeys.js";
|
|
4
|
+
import { isTapOnGhost } from "./ghostTap.js";
|
|
5
|
+
import { completionHintMemory } from "./hintMemory.js";
|
|
6
|
+
/** The owner name of the ghost on the field's mirror layer. */
|
|
7
|
+
export const GHOST_OVERLAY_OWNER = 'ghost';
|
|
8
|
+
/** The ghost's colour: the muted text colour of the design system. A host may pass its own classes. */
|
|
9
|
+
export const DEFAULT_GHOST_CLASS = 'text-muted-foreground';
|
|
10
|
+
const defaultLog = (message, detail) => {
|
|
11
|
+
console.debug('[inline-completion]', message, detail ?? '');
|
|
12
|
+
};
|
|
13
|
+
/** Text for assistive technology only: out of sight, still read. */
|
|
14
|
+
const VISUALLY_HIDDEN_STYLE = 'position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0;';
|
|
15
|
+
export const inlineCompletion = (field, initialParams) => {
|
|
16
|
+
let params = initialParams;
|
|
17
|
+
let controller = null;
|
|
18
|
+
let overlay = null;
|
|
19
|
+
let statusNode = null;
|
|
20
|
+
// The ghost's text node on the mirror layer, so a tap can be matched against where its lines are.
|
|
21
|
+
let ghostNode = null;
|
|
22
|
+
let composing = false;
|
|
23
|
+
let destroyed = false;
|
|
24
|
+
// What the controller was last told: a different text at the next look means the page changed it without an event.
|
|
25
|
+
let lastSyncedValue = field.value;
|
|
26
|
+
// The placeholder, while it steps aside for a ghost in an empty box.
|
|
27
|
+
let placeholderHeldBack = null;
|
|
28
|
+
// Whether the screen reader was told about the ghost that shows (once per ghost, not at every keystroke that shortens it).
|
|
29
|
+
let announcedGhost = false;
|
|
30
|
+
const previousAutocomplete = field.getAttribute('aria-autocomplete');
|
|
31
|
+
field.setAttribute('aria-autocomplete', 'inline');
|
|
32
|
+
const log = (message, detail) => (params.log ?? defaultLog)(message, detail);
|
|
33
|
+
/** Runs a handler so that a failure in it is a log line, never an error in the page's own event handling. */
|
|
34
|
+
function guarded(what, run) {
|
|
35
|
+
try {
|
|
36
|
+
run();
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
log(`${what} failed`, { reason: error instanceof Error ? error.message : String(error) });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
function snapshot() {
|
|
43
|
+
return {
|
|
44
|
+
value: field.value,
|
|
45
|
+
selectionStart: field.selectionStart,
|
|
46
|
+
selectionEnd: field.selectionEnd,
|
|
47
|
+
focused: field.ownerDocument.activeElement === field,
|
|
48
|
+
composing,
|
|
49
|
+
disabled: field.disabled || params.disabled === true,
|
|
50
|
+
readOnly: field.readOnly
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Tells the controller what the field looks like now. */
|
|
54
|
+
function sync(overrides = {}) {
|
|
55
|
+
if (destroyed || !controller)
|
|
56
|
+
return;
|
|
57
|
+
const current = { ...snapshot(), ...overrides };
|
|
58
|
+
lastSyncedValue = current.value;
|
|
59
|
+
controller.update(current);
|
|
60
|
+
}
|
|
61
|
+
/** The placeholder would show under a ghost in an empty box: it steps aside while the ghost shows. */
|
|
62
|
+
function holdBackPlaceholder() {
|
|
63
|
+
if (placeholderHeldBack !== null)
|
|
64
|
+
return;
|
|
65
|
+
const placeholder = field.getAttribute('placeholder');
|
|
66
|
+
if (placeholder === null || placeholder === '')
|
|
67
|
+
return;
|
|
68
|
+
placeholderHeldBack = placeholder;
|
|
69
|
+
field.setAttribute('placeholder', '');
|
|
70
|
+
}
|
|
71
|
+
function restorePlaceholder() {
|
|
72
|
+
if (placeholderHeldBack === null)
|
|
73
|
+
return;
|
|
74
|
+
// A page that changed the placeholder meanwhile keeps its new one.
|
|
75
|
+
if (field.getAttribute('placeholder') === '')
|
|
76
|
+
field.setAttribute('placeholder', placeholderHeldBack);
|
|
77
|
+
placeholderHeldBack = null;
|
|
78
|
+
}
|
|
79
|
+
function announce(ghost) {
|
|
80
|
+
if (ghost === null) {
|
|
81
|
+
announcedGhost = false;
|
|
82
|
+
if (statusNode)
|
|
83
|
+
statusNode.textContent = '';
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
if (announcedGhost || !params.announce)
|
|
87
|
+
return;
|
|
88
|
+
announcedGhost = true;
|
|
89
|
+
if (!statusNode) {
|
|
90
|
+
statusNode = field.ownerDocument.createElement('span');
|
|
91
|
+
statusNode.setAttribute('role', 'status');
|
|
92
|
+
statusNode.setAttribute('aria-live', 'polite');
|
|
93
|
+
statusNode.setAttribute('data-inline-completion-status', '');
|
|
94
|
+
statusNode.style.cssText = VISUALLY_HIDDEN_STYLE;
|
|
95
|
+
field.insertAdjacentElement('afterend', statusNode);
|
|
96
|
+
}
|
|
97
|
+
statusNode.textContent = params.announce(ghost.text);
|
|
98
|
+
}
|
|
99
|
+
function paintGhost(ghost) {
|
|
100
|
+
if (!overlay)
|
|
101
|
+
return;
|
|
102
|
+
if (ghost === null) {
|
|
103
|
+
ghostNode = null;
|
|
104
|
+
overlay.setDecorations([]);
|
|
105
|
+
restorePlaceholder();
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
const caret = field.value.length;
|
|
109
|
+
const ghostText = ghost.text;
|
|
110
|
+
const ghostClass = params.ghostClass ?? DEFAULT_GHOST_CLASS;
|
|
111
|
+
overlay.setDecorations([
|
|
112
|
+
{
|
|
113
|
+
start: caret,
|
|
114
|
+
end: caret,
|
|
115
|
+
render: () => {
|
|
116
|
+
const node = field.ownerDocument.createElement('span');
|
|
117
|
+
node.setAttribute('data-inline-completion-ghost', '');
|
|
118
|
+
node.className = ghostClass;
|
|
119
|
+
node.textContent = ghostText;
|
|
120
|
+
ghostNode = node;
|
|
121
|
+
return node;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
]);
|
|
125
|
+
if (field.value.length === 0)
|
|
126
|
+
holdBackPlaceholder();
|
|
127
|
+
else
|
|
128
|
+
restorePlaceholder();
|
|
129
|
+
}
|
|
130
|
+
function handleChange(ghost) {
|
|
131
|
+
guarded('painting the ghost', () => {
|
|
132
|
+
paintGhost(ghost);
|
|
133
|
+
announce(ghost);
|
|
134
|
+
});
|
|
135
|
+
guarded('reporting the ghost', () => params.onChange?.(ghost));
|
|
136
|
+
}
|
|
137
|
+
/** Puts what the ghost said at the end of the text the way typing would: one undo step, a real `input` event. */
|
|
138
|
+
function insertAtEnd(text) {
|
|
139
|
+
const doc = field.ownerDocument;
|
|
140
|
+
const view = doc.defaultView;
|
|
141
|
+
// The field's own limit holds for a suggestion as it does for typing.
|
|
142
|
+
const room = field.maxLength >= 0
|
|
143
|
+
? Math.max(0, field.maxLength - field.value.length)
|
|
144
|
+
: Number.POSITIVE_INFINITY;
|
|
145
|
+
const insertion = room < text.length ? text.slice(0, room) : text;
|
|
146
|
+
if (insertion === '')
|
|
147
|
+
return;
|
|
148
|
+
const before = field.value.length;
|
|
149
|
+
field.setSelectionRange(before, before);
|
|
150
|
+
let inserted = false;
|
|
151
|
+
try {
|
|
152
|
+
inserted = doc.execCommand('insertText', false, insertion) && field.value.length > before;
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
inserted = false;
|
|
156
|
+
}
|
|
157
|
+
if (inserted)
|
|
158
|
+
return;
|
|
159
|
+
// A browser without `insertText` for fields: the text goes in directly and the page is told, as a keystroke would have.
|
|
160
|
+
field.setRangeText(insertion, before, before, 'end');
|
|
161
|
+
const InputEventConstructor = view?.InputEvent ?? InputEvent;
|
|
162
|
+
field.dispatchEvent(new InputEventConstructor('input', {
|
|
163
|
+
bubbles: true,
|
|
164
|
+
inputType: 'insertText',
|
|
165
|
+
data: insertion
|
|
166
|
+
}));
|
|
167
|
+
}
|
|
168
|
+
function acceptGhost() {
|
|
169
|
+
// Only a ghost that shows can be taken (the controller says the same; the note below is made before it is asked).
|
|
170
|
+
if (controller?.ghost == null)
|
|
171
|
+
return;
|
|
172
|
+
// Noted BEFORE the ghost is taken: taking it tells the host the ghost has gone (`onChange(null)`), and a host that keeps a line
|
|
173
|
+
// for the hint reads the note then, to give the line up. It is also made before the text goes in, which raises an `input` event
|
|
174
|
+
// that may bring the next suggestion straight from the cache in this very task, and a hint about to show for it must already
|
|
175
|
+
// find that the person has learned.
|
|
176
|
+
guarded('noting that a suggestion was taken', () => {
|
|
177
|
+
const memory = params.hintMemory === undefined ? completionHintMemory : params.hintMemory;
|
|
178
|
+
memory?.markLearned();
|
|
179
|
+
});
|
|
180
|
+
const accepted = controller.accept();
|
|
181
|
+
if (accepted)
|
|
182
|
+
insertAtEnd(accepted.insert);
|
|
183
|
+
}
|
|
184
|
+
function onKeyDown(event) {
|
|
185
|
+
guarded('reading a key', () => {
|
|
186
|
+
// Typed as a plain event so it can be registered on a field that is "a textarea or an input"; it is a keyboard event.
|
|
187
|
+
const action = interpretGhostKey(event, controller?.ghost != null);
|
|
188
|
+
if (action === 'accept') {
|
|
189
|
+
event.preventDefault();
|
|
190
|
+
acceptGhost();
|
|
191
|
+
}
|
|
192
|
+
else if (action === 'dismiss') {
|
|
193
|
+
// Esc that only sends the ghost away must not also close the dialog the box is in.
|
|
194
|
+
event.preventDefault();
|
|
195
|
+
event.stopPropagation();
|
|
196
|
+
controller?.dismiss();
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* A finger's tap on the ghost takes it (a phone has no Tab key). The ghost's layer ignores pointers, so the tap lands on the box;
|
|
202
|
+
* it counts as "on the ghost" when it is where one of the ghost's lines is. Any other touch, a pen and a mouse click only move the
|
|
203
|
+
* caret, as they always did.
|
|
204
|
+
*/
|
|
205
|
+
const onPointerUp = (event) => guarded('reading a tap', () => {
|
|
206
|
+
const pointer = event;
|
|
207
|
+
if (pointer.pointerType === 'touch' &&
|
|
208
|
+
controller?.ghost != null &&
|
|
209
|
+
ghostNode?.isConnected === true &&
|
|
210
|
+
isTapOnGhost(pointer.clientX, pointer.clientY, Array.from(ghostNode.getClientRects()))) {
|
|
211
|
+
// The controller logs the acceptance, as it does for Tab: one line per event.
|
|
212
|
+
acceptGhost();
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
sync();
|
|
216
|
+
});
|
|
217
|
+
const onInput = () => guarded('reading the text', () => sync());
|
|
218
|
+
const onCaretMoved = () => guarded('reading the caret', () => sync());
|
|
219
|
+
const onFocus = () => guarded('reading the focus', () => sync());
|
|
220
|
+
const onBlur = () => guarded('reading the focus', () => sync({ focused: false }));
|
|
221
|
+
const onCompositionStart = () => {
|
|
222
|
+
composing = true;
|
|
223
|
+
guarded('reading the composition', () => sync());
|
|
224
|
+
};
|
|
225
|
+
const onCompositionEnd = () => {
|
|
226
|
+
composing = false;
|
|
227
|
+
guarded('reading the composition', () => sync());
|
|
228
|
+
};
|
|
229
|
+
/** (Re)builds the controller for the current source; without a source there is none and the field behaves as if the action were absent. */
|
|
230
|
+
function buildController() {
|
|
231
|
+
const hadGhost = controller?.ghost != null;
|
|
232
|
+
controller?.destroy();
|
|
233
|
+
controller = null;
|
|
234
|
+
// The old controller is gone without a word (destroying one reports nothing): the ghost it left behind goes here.
|
|
235
|
+
if (hadGhost)
|
|
236
|
+
handleChange(null);
|
|
237
|
+
const source = params.source;
|
|
238
|
+
if (!source)
|
|
239
|
+
return;
|
|
240
|
+
controller = createCompletionController({
|
|
241
|
+
source,
|
|
242
|
+
debounceMs: params.debounceMs,
|
|
243
|
+
dismissalLimit: params.dismissalLimit,
|
|
244
|
+
onChange: handleChange,
|
|
245
|
+
onEvent: (event) => guarded('reporting an event', () => params.onEvent?.(event)),
|
|
246
|
+
log
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
overlay = acquireTextareaOverlay(field, GHOST_OVERLAY_OWNER);
|
|
250
|
+
buildController();
|
|
251
|
+
field.addEventListener('input', onInput);
|
|
252
|
+
field.addEventListener('keydown', onKeyDown);
|
|
253
|
+
field.addEventListener('keyup', onCaretMoved);
|
|
254
|
+
field.addEventListener('pointerup', onPointerUp);
|
|
255
|
+
field.addEventListener('select', onCaretMoved);
|
|
256
|
+
field.addEventListener('focus', onFocus);
|
|
257
|
+
field.addEventListener('blur', onBlur);
|
|
258
|
+
field.addEventListener('compositionstart', onCompositionStart);
|
|
259
|
+
field.addEventListener('compositionend', onCompositionEnd);
|
|
260
|
+
// A box that is already focused (the page focused it before the action ran) gets its first suggestion without waiting for a key.
|
|
261
|
+
guarded('the first look', () => sync());
|
|
262
|
+
return {
|
|
263
|
+
update(nextParams) {
|
|
264
|
+
if (destroyed)
|
|
265
|
+
return;
|
|
266
|
+
const sourceChanged = nextParams.source !== params.source;
|
|
267
|
+
params = nextParams;
|
|
268
|
+
if (sourceChanged)
|
|
269
|
+
buildController();
|
|
270
|
+
// The page's own writes to the box (a sent message clears it) raise no event: they are noticed here, after the page has
|
|
271
|
+
// finished its own update, and start the box over without counting the vanished ghost as one the person typed past.
|
|
272
|
+
queueMicrotask(() => {
|
|
273
|
+
if (destroyed)
|
|
274
|
+
return;
|
|
275
|
+
guarded('reading the page text', () => {
|
|
276
|
+
if (field.value !== lastSyncedValue)
|
|
277
|
+
controller?.reset();
|
|
278
|
+
sync();
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
},
|
|
282
|
+
destroy() {
|
|
283
|
+
destroyed = true;
|
|
284
|
+
field.removeEventListener('input', onInput);
|
|
285
|
+
field.removeEventListener('keydown', onKeyDown);
|
|
286
|
+
field.removeEventListener('keyup', onCaretMoved);
|
|
287
|
+
field.removeEventListener('pointerup', onPointerUp);
|
|
288
|
+
field.removeEventListener('select', onCaretMoved);
|
|
289
|
+
field.removeEventListener('focus', onFocus);
|
|
290
|
+
field.removeEventListener('blur', onBlur);
|
|
291
|
+
field.removeEventListener('compositionstart', onCompositionStart);
|
|
292
|
+
field.removeEventListener('compositionend', onCompositionEnd);
|
|
293
|
+
const hadGhost = controller?.ghost != null;
|
|
294
|
+
controller?.destroy();
|
|
295
|
+
controller = null;
|
|
296
|
+
restorePlaceholder();
|
|
297
|
+
overlay?.release();
|
|
298
|
+
overlay = null;
|
|
299
|
+
statusNode?.remove();
|
|
300
|
+
statusNode = null;
|
|
301
|
+
// A host that shows a hint beside the box is told it is gone.
|
|
302
|
+
if (hadGhost)
|
|
303
|
+
guarded('reporting the ghost', () => params.onChange?.(null));
|
|
304
|
+
if (previousAutocomplete === null)
|
|
305
|
+
field.removeAttribute('aria-autocomplete');
|
|
306
|
+
else
|
|
307
|
+
field.setAttribute('aria-autocomplete', previousAutocomplete);
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Counts what happened to the ghosts a box showed (shown, accepted, accepted from further down the list, dismissed, typed past) and
|
|
3
|
+
* reports the counts to the gateway in batches, so the nightly measurement of whether Jev's order is what people reach for has numbers
|
|
4
|
+
* to work from. Counts only: no text, ever.
|
|
5
|
+
*
|
|
6
|
+
* A batch goes out when enough ghosts were shown, when the box has been quiet long enough, and when the page is being left (with
|
|
7
|
+
* `keepalive`, so the browser finishes the request after the page is gone). A report that fails is dropped: a measurement is not worth
|
|
8
|
+
* a retry loop. Nothing here rejects.
|
|
9
|
+
*/
|
|
10
|
+
import type { InlineCompletionEvent, InlineCompletionLogger } from './completionTypes.ts';
|
|
11
|
+
import { type MaybePromise } from './promiseLike.ts';
|
|
12
|
+
/** How many ghosts are shown before the counts go out at once. */
|
|
13
|
+
export declare const DEFAULT_FLUSH_AFTER_SHOWN = 25;
|
|
14
|
+
/** How long after the first event of a batch the counts go out when the box is quiet (a person who stops typing is not a page left). */
|
|
15
|
+
export declare const DEFAULT_FLUSH_DELAY_MS = 20000;
|
|
16
|
+
/** The most one reported count may be: the gateway reads no more than this per item. */
|
|
17
|
+
export declare const MAX_REPORTED_COUNT = 1000;
|
|
18
|
+
export interface OutcomeReporterOptions {
|
|
19
|
+
/** The surface the boxes belong to, as the gateway names them (`creator_chat`, `selector_note`, `embed_assistant`, `workflow_assistant`). */
|
|
20
|
+
surface: string;
|
|
21
|
+
/** The route to POST the counts to. */
|
|
22
|
+
url: string | (() => string);
|
|
23
|
+
/**
|
|
24
|
+
* Headers besides `Content-Type`. Read at every report. May be asynchronous (a token refreshed on demand); a host that answers at
|
|
25
|
+
* once keeps a report made as the page is left inside that very turn, which is what lets `keepalive` finish it.
|
|
26
|
+
*/
|
|
27
|
+
headers?: () => MaybePromise<Record<string, string>>;
|
|
28
|
+
/** The `fetch` to use: the host's own, with the person's credentials. Defaults to the global one. */
|
|
29
|
+
fetchImpl?: typeof fetch;
|
|
30
|
+
credentials?: RequestCredentials;
|
|
31
|
+
flushAfterShown?: number;
|
|
32
|
+
flushDelayMs?: number;
|
|
33
|
+
/** Runs `callback` after `delayMs` and returns a function that cancels it. Default `setTimeout`; a test passes its own. */
|
|
34
|
+
schedule?: (callback: () => void, delayMs: number) => () => void;
|
|
35
|
+
/** Report when the page is hidden or left. Default true; a test turns it off. */
|
|
36
|
+
reportOnPageHide?: boolean;
|
|
37
|
+
log?: InlineCompletionLogger;
|
|
38
|
+
}
|
|
39
|
+
export interface OutcomeReporter {
|
|
40
|
+
/** Counts one event. Events whose answer did not come from Jev or the catalog's own order are not measured and are ignored. */
|
|
41
|
+
record(event: InlineCompletionEvent): void;
|
|
42
|
+
/** Sends what is counted now. `keepalive` lets the request outlive the page. */
|
|
43
|
+
flush(options?: {
|
|
44
|
+
keepalive?: boolean;
|
|
45
|
+
}): Promise<void>;
|
|
46
|
+
/** Sends what is counted, and stops listening to the page. */
|
|
47
|
+
destroy(): void;
|
|
48
|
+
}
|
|
49
|
+
export declare function createOutcomeReporter(options: OutcomeReporterOptions): OutcomeReporter;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { isPromiseLike } from "./promiseLike.js";
|
|
2
|
+
/** How many ghosts are shown before the counts go out at once. */
|
|
3
|
+
export const DEFAULT_FLUSH_AFTER_SHOWN = 25;
|
|
4
|
+
/** How long after the first event of a batch the counts go out when the box is quiet (a person who stops typing is not a page left). */
|
|
5
|
+
export const DEFAULT_FLUSH_DELAY_MS = 20_000;
|
|
6
|
+
/** The most one reported count may be: the gateway reads no more than this per item. */
|
|
7
|
+
export const MAX_REPORTED_COUNT = 1000;
|
|
8
|
+
function emptyTally() {
|
|
9
|
+
return { shown: 0, accepted: 0, acceptedNotFirst: 0, dismissed: 0, diverged: 0 };
|
|
10
|
+
}
|
|
11
|
+
function defaultSchedule(callback, delayMs) {
|
|
12
|
+
const timer = setTimeout(callback, delayMs);
|
|
13
|
+
return () => clearTimeout(timer);
|
|
14
|
+
}
|
|
15
|
+
export function createOutcomeReporter(options) {
|
|
16
|
+
const flushAfterShown = Math.max(1, Math.floor(options.flushAfterShown ?? DEFAULT_FLUSH_AFTER_SHOWN));
|
|
17
|
+
const flushDelayMs = Math.max(0, options.flushDelayMs ?? DEFAULT_FLUSH_DELAY_MS);
|
|
18
|
+
const schedule = options.schedule ?? defaultSchedule;
|
|
19
|
+
const log = options.log ?? (() => { });
|
|
20
|
+
const tallies = new Map();
|
|
21
|
+
let shownSinceFlush = 0;
|
|
22
|
+
let cancelTimer = null;
|
|
23
|
+
let reportedFailure = false;
|
|
24
|
+
let destroyed = false;
|
|
25
|
+
function tallyFor(source) {
|
|
26
|
+
let tally = tallies.get(source);
|
|
27
|
+
if (!tally) {
|
|
28
|
+
tally = emptyTally();
|
|
29
|
+
tallies.set(source, tally);
|
|
30
|
+
}
|
|
31
|
+
return tally;
|
|
32
|
+
}
|
|
33
|
+
async function flush(flushOptions = {}) {
|
|
34
|
+
cancelTimer?.();
|
|
35
|
+
cancelTimer = null;
|
|
36
|
+
if (tallies.size === 0)
|
|
37
|
+
return;
|
|
38
|
+
const cap = (count) => Math.min(MAX_REPORTED_COUNT, count);
|
|
39
|
+
const items = [...tallies].map(([source, tally]) => ({
|
|
40
|
+
surface: options.surface,
|
|
41
|
+
source,
|
|
42
|
+
shown: cap(tally.shown),
|
|
43
|
+
accepted: cap(tally.accepted),
|
|
44
|
+
acceptedNotFirst: cap(Math.min(tally.acceptedNotFirst, tally.accepted)),
|
|
45
|
+
dismissed: cap(tally.dismissed),
|
|
46
|
+
diverged: cap(tally.diverged)
|
|
47
|
+
}));
|
|
48
|
+
tallies.clear();
|
|
49
|
+
shownSinceFlush = 0;
|
|
50
|
+
const fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
51
|
+
if (typeof fetchImpl !== 'function')
|
|
52
|
+
return;
|
|
53
|
+
try {
|
|
54
|
+
const url = typeof options.url === 'function' ? options.url() : options.url;
|
|
55
|
+
const given = options.headers?.();
|
|
56
|
+
// Only a host that answers late is waited for: one that answers at once keeps the report in this very turn.
|
|
57
|
+
const hostHeaders = (isPromiseLike(given) ? await given : given) ?? {};
|
|
58
|
+
const response = await fetchImpl(url, {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
credentials: options.credentials ?? 'include',
|
|
61
|
+
// The body is JSON whatever the host adds: its headers cannot change that.
|
|
62
|
+
headers: { ...hostHeaders, 'Content-Type': 'application/json' },
|
|
63
|
+
body: JSON.stringify({ items }),
|
|
64
|
+
keepalive: flushOptions.keepalive === true
|
|
65
|
+
});
|
|
66
|
+
if (!response.ok && !reportedFailure) {
|
|
67
|
+
reportedFailure = true;
|
|
68
|
+
log('outcome report refused', { status: response.status });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (!reportedFailure) {
|
|
73
|
+
reportedFailure = true;
|
|
74
|
+
log('outcome report failed', {
|
|
75
|
+
reason: error instanceof Error ? error.message : String(error)
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function record(event) {
|
|
81
|
+
if (destroyed || event.type === 'paused')
|
|
82
|
+
return;
|
|
83
|
+
if (event.source !== 'jev' && event.source !== 'prior')
|
|
84
|
+
return;
|
|
85
|
+
const tally = tallyFor(event.source);
|
|
86
|
+
switch (event.type) {
|
|
87
|
+
case 'shown':
|
|
88
|
+
tally.shown += 1;
|
|
89
|
+
shownSinceFlush += 1;
|
|
90
|
+
break;
|
|
91
|
+
case 'accepted':
|
|
92
|
+
tally.accepted += 1;
|
|
93
|
+
if (event.rank > 0)
|
|
94
|
+
tally.acceptedNotFirst += 1;
|
|
95
|
+
break;
|
|
96
|
+
case 'dismissed':
|
|
97
|
+
tally.dismissed += 1;
|
|
98
|
+
break;
|
|
99
|
+
case 'diverged':
|
|
100
|
+
tally.diverged += 1;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
if (shownSinceFlush >= flushAfterShown) {
|
|
104
|
+
void flush();
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
// A quiet box still reports: the first event of a batch starts the clock.
|
|
108
|
+
cancelTimer ??= schedule(() => {
|
|
109
|
+
cancelTimer = null;
|
|
110
|
+
void flush();
|
|
111
|
+
}, flushDelayMs);
|
|
112
|
+
}
|
|
113
|
+
// A page that is hidden or left may never run another timer: the counts go out now, and outlive the page.
|
|
114
|
+
const pageTarget = options.reportOnPageHide === false
|
|
115
|
+
? undefined
|
|
116
|
+
: globalThis.document;
|
|
117
|
+
const onVisibilityChange = () => {
|
|
118
|
+
if (pageTarget?.visibilityState === 'hidden')
|
|
119
|
+
void flush({ keepalive: true });
|
|
120
|
+
};
|
|
121
|
+
const onPageHide = () => {
|
|
122
|
+
void flush({ keepalive: true });
|
|
123
|
+
};
|
|
124
|
+
const pageWindow = pageTarget?.defaultView ?? null;
|
|
125
|
+
pageTarget?.addEventListener('visibilitychange', onVisibilityChange);
|
|
126
|
+
pageWindow?.addEventListener('pagehide', onPageHide);
|
|
127
|
+
return {
|
|
128
|
+
record,
|
|
129
|
+
flush,
|
|
130
|
+
destroy() {
|
|
131
|
+
if (destroyed)
|
|
132
|
+
return;
|
|
133
|
+
void flush({ keepalive: true });
|
|
134
|
+
destroyed = true;
|
|
135
|
+
pageTarget?.removeEventListener('visibilitychange', onVisibilityChange);
|
|
136
|
+
pageWindow?.removeEventListener('pagehide', onPageHide);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a host's own function may give: its answer at once, or a promise of it (a token that is refreshed on demand). The two parts of the
|
|
3
|
+
* engine that call such a function (the source that asks the gateway and the reporter of outcomes) wait only for a promise, so a host
|
|
4
|
+
* that answers at once keeps the request in the very turn it was asked in.
|
|
5
|
+
*/
|
|
6
|
+
/** A value the host gave now, or one it will give later. */
|
|
7
|
+
export type MaybePromise<T> = T | PromiseLike<T>;
|
|
8
|
+
/** Whether a host's function gave its answer later (a promise) rather than at once. */
|
|
9
|
+
export declare function isPromiseLike<T>(value: MaybePromise<T>): value is PromiseLike<T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a host's own function may give: its answer at once, or a promise of it (a token that is refreshed on demand). The two parts of the
|
|
3
|
+
* engine that call such a function (the source that asks the gateway and the reporter of outcomes) wait only for a promise, so a host
|
|
4
|
+
* that answers at once keeps the request in the very turn it was asked in.
|
|
5
|
+
*/
|
|
6
|
+
/** Whether a host's function gave its answer later (a promise) rather than at once. */
|
|
7
|
+
export function isPromiseLike(value) {
|
|
8
|
+
return typeof value?.then === 'function';
|
|
9
|
+
}
|