@slopit/behavioral 0.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.
- package/LICENSE +21 -0
- package/README.md +947 -0
- package/dist/index.d.ts +1822 -0
- package/dist/index.js +2748 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,1822 @@
|
|
|
1
|
+
import { KeystrokeEvent, FocusEvent, PasteEvent, InputDurationEvent, ClipboardCopyEvent, MouseEvent, ScrollEvent, BehavioralData, BehavioralMetrics, CaptureFlag, KeystrokeMetrics, FocusMetrics, TimingMetrics } from '@slopit/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for behavioral capture.
|
|
5
|
+
*/
|
|
6
|
+
interface BehavioralCaptureConfig {
|
|
7
|
+
/**
|
|
8
|
+
* Keystroke capture configuration.
|
|
9
|
+
*/
|
|
10
|
+
keystroke?: KeystrokeCaptureConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Focus/visibility capture configuration.
|
|
13
|
+
*/
|
|
14
|
+
focus?: FocusCaptureConfig;
|
|
15
|
+
/**
|
|
16
|
+
* Paste event capture configuration.
|
|
17
|
+
*/
|
|
18
|
+
paste?: PasteCaptureConfig;
|
|
19
|
+
/**
|
|
20
|
+
* Input duration capture configuration.
|
|
21
|
+
*/
|
|
22
|
+
inputDuration?: InputDurationCaptureConfig;
|
|
23
|
+
/**
|
|
24
|
+
* Clipboard capture configuration (paste, copy, cut).
|
|
25
|
+
*/
|
|
26
|
+
clipboard?: ClipboardCaptureConfig;
|
|
27
|
+
/**
|
|
28
|
+
* Mouse event capture configuration.
|
|
29
|
+
*/
|
|
30
|
+
mouse?: MouseCaptureConfig;
|
|
31
|
+
/**
|
|
32
|
+
* Scroll event capture configuration.
|
|
33
|
+
*/
|
|
34
|
+
scroll?: ScrollCaptureConfig;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Keystroke capture configuration.
|
|
38
|
+
*/
|
|
39
|
+
interface KeystrokeCaptureConfig {
|
|
40
|
+
/**
|
|
41
|
+
* Whether to capture keystrokes.
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
enabled?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Whether to capture keyup events in addition to keydown.
|
|
47
|
+
* @default true
|
|
48
|
+
*/
|
|
49
|
+
captureKeyUp?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Whether to include modifier key states.
|
|
52
|
+
* @default true
|
|
53
|
+
*/
|
|
54
|
+
includeModifiers?: boolean;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Focus capture configuration.
|
|
58
|
+
*/
|
|
59
|
+
interface FocusCaptureConfig {
|
|
60
|
+
/**
|
|
61
|
+
* Whether to capture focus events.
|
|
62
|
+
* @default true
|
|
63
|
+
*/
|
|
64
|
+
enabled?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Whether to use the Page Visibility API.
|
|
67
|
+
* @default true
|
|
68
|
+
*/
|
|
69
|
+
useVisibilityAPI?: boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Whether to use window blur/focus events.
|
|
72
|
+
* @default true
|
|
73
|
+
*/
|
|
74
|
+
useBlurFocus?: boolean;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Paste capture configuration.
|
|
78
|
+
*/
|
|
79
|
+
interface PasteCaptureConfig {
|
|
80
|
+
/**
|
|
81
|
+
* Whether to capture paste events.
|
|
82
|
+
* @default true
|
|
83
|
+
*/
|
|
84
|
+
enabled?: boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Whether to prevent paste operations.
|
|
87
|
+
* @default false
|
|
88
|
+
*/
|
|
89
|
+
prevent?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Message to show when paste is prevented.
|
|
92
|
+
*/
|
|
93
|
+
warnMessage?: string;
|
|
94
|
+
/**
|
|
95
|
+
* Whether to capture a preview of pasted text.
|
|
96
|
+
* @default true
|
|
97
|
+
*/
|
|
98
|
+
capturePreview?: boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Length of text preview to capture.
|
|
101
|
+
* @default 100
|
|
102
|
+
*/
|
|
103
|
+
previewLength?: number;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Input duration capture configuration.
|
|
107
|
+
*/
|
|
108
|
+
interface InputDurationCaptureConfig {
|
|
109
|
+
/**
|
|
110
|
+
* Whether to capture input duration events.
|
|
111
|
+
* @default true
|
|
112
|
+
*/
|
|
113
|
+
enabled?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Whether to count keystrokes during focus periods.
|
|
116
|
+
* @default true
|
|
117
|
+
*/
|
|
118
|
+
trackKeystrokes?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Whether to count paste events during focus periods.
|
|
121
|
+
* @default true
|
|
122
|
+
*/
|
|
123
|
+
trackPastes?: boolean;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Clipboard capture configuration for paste and copy/cut events.
|
|
127
|
+
*/
|
|
128
|
+
interface ClipboardCaptureConfig {
|
|
129
|
+
/**
|
|
130
|
+
* Whether clipboard capture is enabled.
|
|
131
|
+
* @default true
|
|
132
|
+
*/
|
|
133
|
+
enabled?: boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Whether to capture paste events.
|
|
136
|
+
* @default true
|
|
137
|
+
*/
|
|
138
|
+
capturePaste?: boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Whether to capture copy and cut events.
|
|
141
|
+
* @default true
|
|
142
|
+
*/
|
|
143
|
+
captureCopyCut?: boolean;
|
|
144
|
+
/**
|
|
145
|
+
* Whether to prevent paste operations.
|
|
146
|
+
* @default false
|
|
147
|
+
*/
|
|
148
|
+
prevent?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* Message to show when paste is prevented.
|
|
151
|
+
*/
|
|
152
|
+
warnMessage?: string;
|
|
153
|
+
/**
|
|
154
|
+
* Whether to capture a preview of clipboard text.
|
|
155
|
+
* @default true
|
|
156
|
+
*/
|
|
157
|
+
capturePreview?: boolean;
|
|
158
|
+
/**
|
|
159
|
+
* Length of text preview to capture.
|
|
160
|
+
* @default 100
|
|
161
|
+
*/
|
|
162
|
+
previewLength?: number;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Mouse capture configuration.
|
|
166
|
+
*/
|
|
167
|
+
interface MouseCaptureConfig {
|
|
168
|
+
/**
|
|
169
|
+
* Whether mouse capture is enabled.
|
|
170
|
+
* @default true
|
|
171
|
+
*/
|
|
172
|
+
enabled?: boolean;
|
|
173
|
+
/**
|
|
174
|
+
* Whether to capture click events (mousedown, mouseup, click).
|
|
175
|
+
* @default true
|
|
176
|
+
*/
|
|
177
|
+
captureClicks?: boolean;
|
|
178
|
+
/**
|
|
179
|
+
* Whether to capture mousemove events.
|
|
180
|
+
* @default true
|
|
181
|
+
*/
|
|
182
|
+
captureMovement?: boolean;
|
|
183
|
+
/**
|
|
184
|
+
* Throttle interval for mousemove events in milliseconds.
|
|
185
|
+
* Lower values capture more detail but produce more data.
|
|
186
|
+
* @default 50
|
|
187
|
+
*/
|
|
188
|
+
throttleMs?: number;
|
|
189
|
+
/**
|
|
190
|
+
* Threshold for detecting idle periods in milliseconds.
|
|
191
|
+
* Periods with no mouse activity exceeding this threshold are recorded.
|
|
192
|
+
* @default 5000
|
|
193
|
+
*/
|
|
194
|
+
idleThresholdMs?: number;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Scroll capture configuration.
|
|
198
|
+
*/
|
|
199
|
+
interface ScrollCaptureConfig {
|
|
200
|
+
/**
|
|
201
|
+
* Whether to capture scroll events.
|
|
202
|
+
* @default true
|
|
203
|
+
*/
|
|
204
|
+
enabled?: boolean;
|
|
205
|
+
/**
|
|
206
|
+
* Throttle interval for scroll events in milliseconds.
|
|
207
|
+
* @default 100
|
|
208
|
+
*/
|
|
209
|
+
throttleMs?: number;
|
|
210
|
+
/**
|
|
211
|
+
* Whether to capture scroll direction changes.
|
|
212
|
+
* @default true
|
|
213
|
+
*/
|
|
214
|
+
captureDirection?: boolean;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Event handlers for behavioral capture events.
|
|
218
|
+
*/
|
|
219
|
+
interface BehavioralCaptureEvents {
|
|
220
|
+
keystroke: (event: KeystrokeEvent) => void;
|
|
221
|
+
focus: (event: FocusEvent) => void;
|
|
222
|
+
paste: (event: PasteEvent) => void;
|
|
223
|
+
inputDuration: (event: InputDurationEvent) => void;
|
|
224
|
+
clipboard: (event: ClipboardCopyEvent) => void;
|
|
225
|
+
mouse: (event: MouseEvent) => void;
|
|
226
|
+
scroll: (event: ScrollEvent) => void;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Platform-agnostic behavioral data capture.
|
|
231
|
+
*
|
|
232
|
+
* Captures keystroke dynamics, focus events, and paste events
|
|
233
|
+
* from DOM elements. Works with any experiment framework or
|
|
234
|
+
* custom implementation.
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const capture = createBehavioralCapture({
|
|
239
|
+
* keystroke: { enabled: true },
|
|
240
|
+
* focus: { enabled: true },
|
|
241
|
+
* paste: { enabled: true, prevent: false },
|
|
242
|
+
* });
|
|
243
|
+
*
|
|
244
|
+
* const textarea = document.getElementById("response");
|
|
245
|
+
* capture.attach(textarea);
|
|
246
|
+
*
|
|
247
|
+
* // Later, retrieve data
|
|
248
|
+
* const data = capture.getData();
|
|
249
|
+
* const metrics = capture.getMetrics();
|
|
250
|
+
*
|
|
251
|
+
* // Cleanup
|
|
252
|
+
* capture.detach();
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
declare class BehavioralCapture {
|
|
256
|
+
private config;
|
|
257
|
+
private keystrokeCollector;
|
|
258
|
+
private focusCollector;
|
|
259
|
+
private clipboardCollector;
|
|
260
|
+
private mouseCollector;
|
|
261
|
+
private scrollCollector;
|
|
262
|
+
private inputDurationCollector;
|
|
263
|
+
private attachedElement;
|
|
264
|
+
private trialStartTime;
|
|
265
|
+
private listeners;
|
|
266
|
+
constructor(config: BehavioralCaptureConfig);
|
|
267
|
+
/**
|
|
268
|
+
* Attach capture to a DOM element.
|
|
269
|
+
*
|
|
270
|
+
* For text input capture, attach to the input or textarea element.
|
|
271
|
+
* For document-level focus tracking, attach to document.
|
|
272
|
+
*
|
|
273
|
+
* @param element - The element to attach to
|
|
274
|
+
* @param startTime - Optional trial start time (defaults to now)
|
|
275
|
+
*/
|
|
276
|
+
attach(element: HTMLElement | Document, startTime?: number): void;
|
|
277
|
+
/**
|
|
278
|
+
* Detach capture and clean up event listeners.
|
|
279
|
+
*/
|
|
280
|
+
detach(): void;
|
|
281
|
+
/**
|
|
282
|
+
* Reset all collected data without detaching.
|
|
283
|
+
*
|
|
284
|
+
* @param newStartTime - Optional new start time
|
|
285
|
+
*/
|
|
286
|
+
reset(newStartTime?: number): void;
|
|
287
|
+
/**
|
|
288
|
+
* Get all collected behavioral data.
|
|
289
|
+
*/
|
|
290
|
+
getData(): BehavioralData;
|
|
291
|
+
/**
|
|
292
|
+
* Get computed metrics from collected data.
|
|
293
|
+
*/
|
|
294
|
+
getMetrics(): BehavioralMetrics;
|
|
295
|
+
/**
|
|
296
|
+
* Get flags generated during capture.
|
|
297
|
+
*/
|
|
298
|
+
getFlags(): CaptureFlag[];
|
|
299
|
+
/**
|
|
300
|
+
* Subscribe to capture events.
|
|
301
|
+
*/
|
|
302
|
+
on<K extends keyof BehavioralCaptureEvents>(event: K, handler: BehavioralCaptureEvents[K]): void;
|
|
303
|
+
/**
|
|
304
|
+
* Unsubscribe from capture events.
|
|
305
|
+
*/
|
|
306
|
+
off<K extends keyof BehavioralCaptureEvents>(event: K, handler: BehavioralCaptureEvents[K]): void;
|
|
307
|
+
private emit;
|
|
308
|
+
private normalizeConfig;
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Factory function to create a BehavioralCapture instance.
|
|
312
|
+
*/
|
|
313
|
+
declare function createBehavioralCapture(config: BehavioralCaptureConfig): BehavioralCapture;
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Collects keystroke events from DOM elements.
|
|
317
|
+
*/
|
|
318
|
+
declare class KeystrokeCollector {
|
|
319
|
+
private config;
|
|
320
|
+
private onEvent?;
|
|
321
|
+
private events;
|
|
322
|
+
private element;
|
|
323
|
+
private startTime;
|
|
324
|
+
private firstKeystrokeTime;
|
|
325
|
+
private boundKeydown;
|
|
326
|
+
private boundKeyup;
|
|
327
|
+
constructor(config: Required<KeystrokeCaptureConfig>, startTime: number, onEvent?: ((event: KeystrokeEvent) => void) | undefined);
|
|
328
|
+
/**
|
|
329
|
+
* Attach collector to an element.
|
|
330
|
+
*/
|
|
331
|
+
attach(element: HTMLElement | Document): void;
|
|
332
|
+
/**
|
|
333
|
+
* Detach collector from element.
|
|
334
|
+
*/
|
|
335
|
+
detach(): void;
|
|
336
|
+
/**
|
|
337
|
+
* Reset collected data.
|
|
338
|
+
*/
|
|
339
|
+
reset(newStartTime?: number): void;
|
|
340
|
+
/**
|
|
341
|
+
* Get all collected events.
|
|
342
|
+
*/
|
|
343
|
+
getEvents(): KeystrokeEvent[];
|
|
344
|
+
/**
|
|
345
|
+
* Get time of first keystroke.
|
|
346
|
+
*/
|
|
347
|
+
getFirstKeystrokeTime(): number | undefined;
|
|
348
|
+
/**
|
|
349
|
+
* Get count of keystrokes in the last N milliseconds.
|
|
350
|
+
*/
|
|
351
|
+
getRecentCount(windowMs: number): number;
|
|
352
|
+
private handleKeydown;
|
|
353
|
+
private handleKeyup;
|
|
354
|
+
private recordEvent;
|
|
355
|
+
private getTextLength;
|
|
356
|
+
private getModifiers;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Collects focus and visibility events.
|
|
361
|
+
*/
|
|
362
|
+
declare class FocusCollector {
|
|
363
|
+
private config;
|
|
364
|
+
private onEvent?;
|
|
365
|
+
private events;
|
|
366
|
+
private startTime;
|
|
367
|
+
private lastBlurTime;
|
|
368
|
+
private lastHiddenTime;
|
|
369
|
+
private boundFocus;
|
|
370
|
+
private boundBlur;
|
|
371
|
+
private boundVisibility;
|
|
372
|
+
private attached;
|
|
373
|
+
constructor(config: Required<FocusCaptureConfig>, startTime: number, onEvent?: ((event: FocusEvent) => void) | undefined);
|
|
374
|
+
/**
|
|
375
|
+
* Attach collector to document.
|
|
376
|
+
*/
|
|
377
|
+
attach(_element: Document): void;
|
|
378
|
+
/**
|
|
379
|
+
* Detach collector.
|
|
380
|
+
*/
|
|
381
|
+
detach(): void;
|
|
382
|
+
/**
|
|
383
|
+
* Reset collected data.
|
|
384
|
+
*/
|
|
385
|
+
reset(newStartTime?: number): void;
|
|
386
|
+
/**
|
|
387
|
+
* Get all collected events.
|
|
388
|
+
*/
|
|
389
|
+
getEvents(): FocusEvent[];
|
|
390
|
+
private handleFocus;
|
|
391
|
+
private handleBlur;
|
|
392
|
+
private handleVisibilityChange;
|
|
393
|
+
private updateLastBlurDuration;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Collects paste events from DOM elements.
|
|
398
|
+
*/
|
|
399
|
+
declare class PasteCollector {
|
|
400
|
+
private config;
|
|
401
|
+
private getRecentKeystrokes;
|
|
402
|
+
private onEvent?;
|
|
403
|
+
private events;
|
|
404
|
+
private element;
|
|
405
|
+
private startTime;
|
|
406
|
+
private boundPaste;
|
|
407
|
+
constructor(config: Required<Omit<PasteCaptureConfig, "warnMessage">> & {
|
|
408
|
+
warnMessage?: string;
|
|
409
|
+
}, startTime: number, getRecentKeystrokes: () => number, onEvent?: ((event: PasteEvent) => void) | undefined);
|
|
410
|
+
/**
|
|
411
|
+
* Attach collector to an element.
|
|
412
|
+
*/
|
|
413
|
+
attach(element: HTMLElement | Document): void;
|
|
414
|
+
/**
|
|
415
|
+
* Detach collector from element.
|
|
416
|
+
*/
|
|
417
|
+
detach(): void;
|
|
418
|
+
/**
|
|
419
|
+
* Reset collected data.
|
|
420
|
+
*/
|
|
421
|
+
reset(newStartTime?: number): void;
|
|
422
|
+
/**
|
|
423
|
+
* Get all collected events.
|
|
424
|
+
*/
|
|
425
|
+
getEvents(): PasteEvent[];
|
|
426
|
+
private handlePaste;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Clipboard collector for paste and copy/cut events.
|
|
431
|
+
*
|
|
432
|
+
* @module
|
|
433
|
+
*/
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Collects clipboard events (paste, copy, cut) from DOM elements.
|
|
437
|
+
*
|
|
438
|
+
* This collector unifies paste event tracking with copy/cut event tracking,
|
|
439
|
+
* capturing selection context for copy/cut operations.
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```typescript
|
|
443
|
+
* const collector = new ClipboardCollector(
|
|
444
|
+
* {
|
|
445
|
+
* enabled: true,
|
|
446
|
+
* capturePaste: true,
|
|
447
|
+
* captureCopyCut: true,
|
|
448
|
+
* prevent: false,
|
|
449
|
+
* capturePreview: true,
|
|
450
|
+
* previewLength: 100,
|
|
451
|
+
* },
|
|
452
|
+
* performance.now(),
|
|
453
|
+
* () => keystrokeCollector.getRecentCount(2000),
|
|
454
|
+
* );
|
|
455
|
+
*
|
|
456
|
+
* collector.attach(document);
|
|
457
|
+
* // ... capture events
|
|
458
|
+
* const pasteEvents = collector.getPasteEvents();
|
|
459
|
+
* const copyEvents = collector.getCopyEvents();
|
|
460
|
+
* collector.detach();
|
|
461
|
+
* ```
|
|
462
|
+
*/
|
|
463
|
+
declare class ClipboardCollector {
|
|
464
|
+
private config;
|
|
465
|
+
private getRecentKeystrokes;
|
|
466
|
+
private onPasteEvent?;
|
|
467
|
+
private onCopyEvent?;
|
|
468
|
+
private pasteEvents;
|
|
469
|
+
private copyEvents;
|
|
470
|
+
private element;
|
|
471
|
+
private startTime;
|
|
472
|
+
private boundPaste;
|
|
473
|
+
private boundCopy;
|
|
474
|
+
private boundCut;
|
|
475
|
+
/**
|
|
476
|
+
* Creates a new ClipboardCollector.
|
|
477
|
+
*
|
|
478
|
+
* @param config - Configuration options for clipboard capture
|
|
479
|
+
* @param startTime - Reference time for event timestamps (typically performance.now())
|
|
480
|
+
* @param getRecentKeystrokes - Function returning keystroke count in recent window
|
|
481
|
+
* @param onPasteEvent - Optional callback invoked for each paste event
|
|
482
|
+
* @param onCopyEvent - Optional callback invoked for each copy/cut event
|
|
483
|
+
*/
|
|
484
|
+
constructor(config: Required<Omit<ClipboardCaptureConfig, "warnMessage">> & {
|
|
485
|
+
warnMessage?: string;
|
|
486
|
+
}, startTime: number, getRecentKeystrokes: () => number, onPasteEvent?: ((event: PasteEvent) => void) | undefined, onCopyEvent?: ((event: ClipboardCopyEvent) => void) | undefined);
|
|
487
|
+
/**
|
|
488
|
+
* Attach collector to an element.
|
|
489
|
+
*
|
|
490
|
+
* @param element - The DOM element or document to attach listeners to
|
|
491
|
+
*/
|
|
492
|
+
attach(element: HTMLElement | Document): void;
|
|
493
|
+
/**
|
|
494
|
+
* Detach collector from element.
|
|
495
|
+
*/
|
|
496
|
+
detach(): void;
|
|
497
|
+
/**
|
|
498
|
+
* Reset collected data.
|
|
499
|
+
*
|
|
500
|
+
* @param newStartTime - Optional new reference time for timestamps
|
|
501
|
+
*/
|
|
502
|
+
reset(newStartTime?: number): void;
|
|
503
|
+
/**
|
|
504
|
+
* Get all collected paste events.
|
|
505
|
+
*
|
|
506
|
+
* @returns Copy of paste events array
|
|
507
|
+
*/
|
|
508
|
+
getPasteEvents(): PasteEvent[];
|
|
509
|
+
/**
|
|
510
|
+
* Get all collected copy/cut events.
|
|
511
|
+
*
|
|
512
|
+
* @returns Copy of copy/cut events array
|
|
513
|
+
*/
|
|
514
|
+
getCopyEvents(): ClipboardCopyEvent[];
|
|
515
|
+
/**
|
|
516
|
+
* Get all clipboard events combined.
|
|
517
|
+
*
|
|
518
|
+
* @returns Array of all paste and copy/cut events, sorted by time
|
|
519
|
+
*/
|
|
520
|
+
getAllEvents(): Array<PasteEvent | ClipboardCopyEvent>;
|
|
521
|
+
private handlePaste;
|
|
522
|
+
private handleCopy;
|
|
523
|
+
private handleCut;
|
|
524
|
+
private handleCopyCut;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Compute metrics from keystroke events.
|
|
529
|
+
*
|
|
530
|
+
* @param keystrokes - Array of keystroke events
|
|
531
|
+
* @param pauseThreshold - Minimum IKI to count as a pause (default: 2000ms)
|
|
532
|
+
* @returns Computed keystroke metrics
|
|
533
|
+
*/
|
|
534
|
+
declare function computeKeystrokeMetrics(keystrokes: KeystrokeEvent[], pauseThreshold?: number): KeystrokeMetrics;
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Compute metrics from focus events.
|
|
538
|
+
*
|
|
539
|
+
* @param focusEvents - Array of focus events
|
|
540
|
+
* @returns Computed focus metrics
|
|
541
|
+
*/
|
|
542
|
+
declare function computeFocusMetrics(focusEvents: FocusEvent[]): FocusMetrics;
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Compute timing metrics from keystroke events.
|
|
546
|
+
*
|
|
547
|
+
* @param keystrokes - Array of keystroke events
|
|
548
|
+
* @param trialStartTime - Trial start time (for first keystroke latency)
|
|
549
|
+
* @param totalResponseTime - Total response time in milliseconds
|
|
550
|
+
* @returns Computed timing metrics
|
|
551
|
+
*/
|
|
552
|
+
declare function computeTimingMetrics(keystrokes: KeystrokeEvent[], totalResponseTime: number): TimingMetrics;
|
|
553
|
+
|
|
554
|
+
/**
|
|
555
|
+
* Base types for detection modules.
|
|
556
|
+
*
|
|
557
|
+
* @module
|
|
558
|
+
*/
|
|
559
|
+
/**
|
|
560
|
+
* Represents an individual detection indicator found during detection.
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* ```typescript
|
|
564
|
+
* const indicator: DetectionIndicator = {
|
|
565
|
+
* type: "clipboard-paste",
|
|
566
|
+
* selector: "#response-field",
|
|
567
|
+
* details: { pasteLength: 500 },
|
|
568
|
+
* };
|
|
569
|
+
* ```
|
|
570
|
+
*/
|
|
571
|
+
interface DetectionIndicator {
|
|
572
|
+
/** The type identifier for this indicator. */
|
|
573
|
+
type: string;
|
|
574
|
+
/** Optional CSS selector identifying the element where detection occurred. */
|
|
575
|
+
selector?: string;
|
|
576
|
+
/** Optional additional details about the detection. */
|
|
577
|
+
details?: Record<string, unknown>;
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* The result returned by a detector after running detection.
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
* ```typescript
|
|
584
|
+
* const result: DetectionResult = {
|
|
585
|
+
* detected: true,
|
|
586
|
+
* confidence: 0.85,
|
|
587
|
+
* message: "Large clipboard paste detected",
|
|
588
|
+
* indicators: [
|
|
589
|
+
* { type: "paste-size", details: { characters: 500 } },
|
|
590
|
+
* ],
|
|
591
|
+
* };
|
|
592
|
+
* ```
|
|
593
|
+
*/
|
|
594
|
+
interface DetectionResult {
|
|
595
|
+
/** Whether the detector found evidence of the behavior it monitors. */
|
|
596
|
+
detected: boolean;
|
|
597
|
+
/** Confidence score from 0 to 1, where 1 indicates highest confidence. */
|
|
598
|
+
confidence: number;
|
|
599
|
+
/** Human-readable message describing the detection result. */
|
|
600
|
+
message: string;
|
|
601
|
+
/** List of specific indicators that contributed to this result. */
|
|
602
|
+
indicators: DetectionIndicator[];
|
|
603
|
+
}
|
|
604
|
+
/**
|
|
605
|
+
* Interface for detection modules that identify specific behaviors.
|
|
606
|
+
*
|
|
607
|
+
* Detectors can operate in two modes:
|
|
608
|
+
* 1. One-shot detection via `detect()`
|
|
609
|
+
* 2. Continuous monitoring via `startMonitoring()` and `stopMonitoring()`
|
|
610
|
+
*
|
|
611
|
+
* @example
|
|
612
|
+
* ```typescript
|
|
613
|
+
* class PasteDetector implements Detector {
|
|
614
|
+
* readonly name = "paste-detector";
|
|
615
|
+
*
|
|
616
|
+
* detect(): DetectionResult {
|
|
617
|
+
* // Check for paste indicators
|
|
618
|
+
* return {
|
|
619
|
+
* detected: false,
|
|
620
|
+
* confidence: 0,
|
|
621
|
+
* message: "No paste detected",
|
|
622
|
+
* indicators: [],
|
|
623
|
+
* };
|
|
624
|
+
* }
|
|
625
|
+
* }
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
628
|
+
interface Detector {
|
|
629
|
+
/** Unique identifier for this detector. */
|
|
630
|
+
readonly name: string;
|
|
631
|
+
/**
|
|
632
|
+
* Performs detection and returns the result.
|
|
633
|
+
*
|
|
634
|
+
* @returns The detection result with confidence and indicators.
|
|
635
|
+
*/
|
|
636
|
+
detect(): DetectionResult;
|
|
637
|
+
/**
|
|
638
|
+
* Starts continuous monitoring for the behavior.
|
|
639
|
+
* Optional; not all detectors support continuous monitoring.
|
|
640
|
+
*
|
|
641
|
+
* @param element - Optional element to monitor (implementation-specific)
|
|
642
|
+
*/
|
|
643
|
+
startMonitoring?(element?: HTMLElement): void;
|
|
644
|
+
/**
|
|
645
|
+
* Stops continuous monitoring.
|
|
646
|
+
* Optional; not all detectors support continuous monitoring.
|
|
647
|
+
*/
|
|
648
|
+
stopMonitoring?(): void;
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* Detector for sudden text appearance without keystroke or paste source.
|
|
653
|
+
*
|
|
654
|
+
* @module
|
|
655
|
+
*/
|
|
656
|
+
|
|
657
|
+
/**
|
|
658
|
+
* Configuration for the text appearance detector.
|
|
659
|
+
*/
|
|
660
|
+
interface TextAppearanceDetectorConfig {
|
|
661
|
+
/**
|
|
662
|
+
* Minimum character count to flag as suspicious.
|
|
663
|
+
* @default 50
|
|
664
|
+
*/
|
|
665
|
+
minTextLength?: number;
|
|
666
|
+
/**
|
|
667
|
+
* Time window in milliseconds to check for keystroke activity.
|
|
668
|
+
* @default 2000
|
|
669
|
+
*/
|
|
670
|
+
timeWindowMs?: number;
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Callbacks for checking keystroke and paste activity.
|
|
674
|
+
*/
|
|
675
|
+
interface TextAppearanceCallbacks {
|
|
676
|
+
/**
|
|
677
|
+
* Returns the number of keystrokes in the recent time window.
|
|
678
|
+
*/
|
|
679
|
+
getRecentKeystrokes?: () => number;
|
|
680
|
+
/**
|
|
681
|
+
* Returns whether a paste occurred in the recent time window.
|
|
682
|
+
*/
|
|
683
|
+
hadRecentPaste?: () => boolean;
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Detects sudden text appearance without corresponding keystroke or paste events.
|
|
687
|
+
*
|
|
688
|
+
* This detector uses MutationObserver to watch for text changes in input fields,
|
|
689
|
+
* textareas, and contenteditable elements. When text appears faster than possible
|
|
690
|
+
* via normal typing (e.g., 50+ characters with no keystrokes), it flags the event
|
|
691
|
+
* as suspicious.
|
|
692
|
+
*
|
|
693
|
+
* @example
|
|
694
|
+
* ```typescript
|
|
695
|
+
* const detector = new TextAppearanceDetector(
|
|
696
|
+
* { minTextLength: 30, timeWindowMs: 1500 },
|
|
697
|
+
* {
|
|
698
|
+
* getRecentKeystrokes: () => keystrokeCollector.getRecentCount(1500),
|
|
699
|
+
* hadRecentPaste: () => pasteCollector.hadRecentPaste(1500),
|
|
700
|
+
* }
|
|
701
|
+
* );
|
|
702
|
+
*
|
|
703
|
+
* detector.startMonitoring(document.getElementById("response"));
|
|
704
|
+
* // ... user interaction ...
|
|
705
|
+
* const result = detector.detect();
|
|
706
|
+
* detector.stopMonitoring();
|
|
707
|
+
* ```
|
|
708
|
+
*/
|
|
709
|
+
declare class TextAppearanceDetector implements Detector {
|
|
710
|
+
readonly name = "text_appearance";
|
|
711
|
+
private observer;
|
|
712
|
+
private indicators;
|
|
713
|
+
private lastTextLength;
|
|
714
|
+
private keystrokeCallback;
|
|
715
|
+
private pasteCallback;
|
|
716
|
+
private config;
|
|
717
|
+
private startTime;
|
|
718
|
+
private monitoredElement;
|
|
719
|
+
private inputHandler;
|
|
720
|
+
/**
|
|
721
|
+
* Creates a new TextAppearanceDetector.
|
|
722
|
+
*
|
|
723
|
+
* @param config - Configuration options
|
|
724
|
+
* @param callbacks - Callbacks for checking keystroke and paste activity
|
|
725
|
+
*/
|
|
726
|
+
constructor(config?: TextAppearanceDetectorConfig, callbacks?: TextAppearanceCallbacks);
|
|
727
|
+
/**
|
|
728
|
+
* Returns detection results with all recorded indicators.
|
|
729
|
+
*
|
|
730
|
+
* @returns Detection result with indicators of sudden text appearance
|
|
731
|
+
*/
|
|
732
|
+
detect(): DetectionResult;
|
|
733
|
+
/**
|
|
734
|
+
* Starts monitoring an element for sudden text appearance.
|
|
735
|
+
*
|
|
736
|
+
* Watches for text changes in input/textarea elements and content changes
|
|
737
|
+
* in contenteditable elements via MutationObserver.
|
|
738
|
+
*
|
|
739
|
+
* @param element - The element to monitor
|
|
740
|
+
*/
|
|
741
|
+
startMonitoring(element: HTMLElement): void;
|
|
742
|
+
/**
|
|
743
|
+
* Stops monitoring and cleans up resources.
|
|
744
|
+
*/
|
|
745
|
+
stopMonitoring(): void;
|
|
746
|
+
/**
|
|
747
|
+
* Resets the detector state.
|
|
748
|
+
*
|
|
749
|
+
* @param newStartTime - Optional new start time for timing calculations
|
|
750
|
+
*/
|
|
751
|
+
reset(newStartTime?: number): void;
|
|
752
|
+
/**
|
|
753
|
+
* Returns the current list of indicators.
|
|
754
|
+
*
|
|
755
|
+
* @returns Copy of the indicators array
|
|
756
|
+
*/
|
|
757
|
+
getIndicators(): DetectionIndicator[];
|
|
758
|
+
private initializeTextLengths;
|
|
759
|
+
private handleInputEvent;
|
|
760
|
+
private handleMutations;
|
|
761
|
+
private checkTextChange;
|
|
762
|
+
private getSelector;
|
|
763
|
+
private calculateConfidence;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
/**
|
|
767
|
+
* Detector for AI assistant browser extension signatures.
|
|
768
|
+
*
|
|
769
|
+
* Scans the DOM and global window object for known patterns associated with
|
|
770
|
+
* browser extensions like Grammarly, Copilot, Quillbot, and other AI assistants.
|
|
771
|
+
*
|
|
772
|
+
* @module
|
|
773
|
+
*/
|
|
774
|
+
|
|
775
|
+
/**
|
|
776
|
+
* Monitor configuration for MutationObserver settings.
|
|
777
|
+
*/
|
|
778
|
+
interface MonitorConfig {
|
|
779
|
+
/**
|
|
780
|
+
* Whether to observe attribute changes.
|
|
781
|
+
* @default true
|
|
782
|
+
*/
|
|
783
|
+
attributes: boolean;
|
|
784
|
+
/**
|
|
785
|
+
* Whether to observe child node additions/removals.
|
|
786
|
+
* @default true
|
|
787
|
+
*/
|
|
788
|
+
childList: boolean;
|
|
789
|
+
/**
|
|
790
|
+
* Whether to observe the entire subtree.
|
|
791
|
+
* @default true
|
|
792
|
+
*/
|
|
793
|
+
subtree: boolean;
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Configuration for the ExtensionDetector.
|
|
797
|
+
*/
|
|
798
|
+
interface ExtensionDetectorConfig {
|
|
799
|
+
/**
|
|
800
|
+
* Additional DOM selectors to scan for extension signatures.
|
|
801
|
+
*/
|
|
802
|
+
customSelectors?: string[];
|
|
803
|
+
/**
|
|
804
|
+
* Additional global window properties to check.
|
|
805
|
+
*/
|
|
806
|
+
customGlobals?: string[];
|
|
807
|
+
/**
|
|
808
|
+
* Configuration for continuous monitoring via MutationObserver.
|
|
809
|
+
*/
|
|
810
|
+
monitor?: Partial<MonitorConfig>;
|
|
811
|
+
}
|
|
812
|
+
/**
|
|
813
|
+
* Detects AI assistant browser extension signatures.
|
|
814
|
+
*
|
|
815
|
+
* Scans for:
|
|
816
|
+
* - Known DOM elements injected by extensions (e.g., Grammarly shadow roots)
|
|
817
|
+
* - Global variables set by extensions
|
|
818
|
+
* - Custom patterns provided via configuration
|
|
819
|
+
*
|
|
820
|
+
* @example
|
|
821
|
+
* ```typescript
|
|
822
|
+
* const detector = new ExtensionDetector();
|
|
823
|
+
* const result = detector.detect();
|
|
824
|
+
*
|
|
825
|
+
* if (result.detected) {
|
|
826
|
+
* console.log("Extensions found:", result.indicators);
|
|
827
|
+
* }
|
|
828
|
+
* ```
|
|
829
|
+
*
|
|
830
|
+
* @example
|
|
831
|
+
* ```typescript
|
|
832
|
+
* // With continuous monitoring
|
|
833
|
+
* const detector = new ExtensionDetector({
|
|
834
|
+
* customSelectors: ['[data-my-extension]'],
|
|
835
|
+
* });
|
|
836
|
+
*
|
|
837
|
+
* detector.startMonitoring();
|
|
838
|
+
* // ... later
|
|
839
|
+
* detector.stopMonitoring();
|
|
840
|
+
* ```
|
|
841
|
+
*/
|
|
842
|
+
declare class ExtensionDetector implements Detector {
|
|
843
|
+
readonly name = "extension-detector";
|
|
844
|
+
private readonly config;
|
|
845
|
+
private readonly allSelectors;
|
|
846
|
+
private readonly allGlobals;
|
|
847
|
+
private observer;
|
|
848
|
+
private monitoredIndicators;
|
|
849
|
+
constructor(config?: ExtensionDetectorConfig);
|
|
850
|
+
/**
|
|
851
|
+
* Performs a one-shot detection scan for extension signatures.
|
|
852
|
+
*
|
|
853
|
+
* @returns Detection result with found indicators
|
|
854
|
+
*/
|
|
855
|
+
detect(): DetectionResult;
|
|
856
|
+
/**
|
|
857
|
+
* Starts continuous monitoring for dynamically injected extension elements.
|
|
858
|
+
*
|
|
859
|
+
* Uses MutationObserver to watch for DOM changes that match extension patterns.
|
|
860
|
+
*/
|
|
861
|
+
startMonitoring(): void;
|
|
862
|
+
/**
|
|
863
|
+
* Stops continuous monitoring and disconnects the MutationObserver.
|
|
864
|
+
*/
|
|
865
|
+
stopMonitoring(): void;
|
|
866
|
+
/**
|
|
867
|
+
* Gets all indicators detected during monitoring.
|
|
868
|
+
*
|
|
869
|
+
* @returns Array of detection indicators found during monitoring
|
|
870
|
+
*/
|
|
871
|
+
getMonitoredIndicators(): DetectionIndicator[];
|
|
872
|
+
/**
|
|
873
|
+
* Resets all collected monitoring data.
|
|
874
|
+
*/
|
|
875
|
+
reset(): void;
|
|
876
|
+
/**
|
|
877
|
+
* Queries the DOM for elements matching a selector.
|
|
878
|
+
*/
|
|
879
|
+
private queryElements;
|
|
880
|
+
/**
|
|
881
|
+
* Checks if a global variable exists on the window object.
|
|
882
|
+
*/
|
|
883
|
+
private hasGlobal;
|
|
884
|
+
/**
|
|
885
|
+
* Handles mutation events from the MutationObserver.
|
|
886
|
+
*/
|
|
887
|
+
private handleMutations;
|
|
888
|
+
/**
|
|
889
|
+
* Handles child list mutations, checking new nodes for extension patterns.
|
|
890
|
+
*/
|
|
891
|
+
private handleChildListMutation;
|
|
892
|
+
/**
|
|
893
|
+
* Handles attribute mutations, checking if changes match extension patterns.
|
|
894
|
+
*/
|
|
895
|
+
private handleAttributeMutation;
|
|
896
|
+
/**
|
|
897
|
+
* Safely checks if an element matches a selector.
|
|
898
|
+
*/
|
|
899
|
+
private elementMatchesSelector;
|
|
900
|
+
/**
|
|
901
|
+
* Safely queries for elements within a parent.
|
|
902
|
+
*/
|
|
903
|
+
private querySelectorSafe;
|
|
904
|
+
/**
|
|
905
|
+
* Adds an indicator to the monitored indicators list.
|
|
906
|
+
*/
|
|
907
|
+
private addMonitoredIndicator;
|
|
908
|
+
/**
|
|
909
|
+
* Calculates a confidence score based on indicators.
|
|
910
|
+
*/
|
|
911
|
+
private calculateConfidence;
|
|
912
|
+
/**
|
|
913
|
+
* Generates a human-readable message from indicators.
|
|
914
|
+
*/
|
|
915
|
+
private generateMessage;
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* Creates a new ExtensionDetector instance.
|
|
919
|
+
*
|
|
920
|
+
* @param config - Configuration options
|
|
921
|
+
* @returns New ExtensionDetector instance
|
|
922
|
+
*
|
|
923
|
+
* @example
|
|
924
|
+
* ```typescript
|
|
925
|
+
* const detector = createExtensionDetector({
|
|
926
|
+
* customSelectors: ['[data-custom-ai]'],
|
|
927
|
+
* });
|
|
928
|
+
* ```
|
|
929
|
+
*/
|
|
930
|
+
declare function createExtensionDetector(config?: ExtensionDetectorConfig): ExtensionDetector;
|
|
931
|
+
|
|
932
|
+
/**
|
|
933
|
+
* Types for the intervention module.
|
|
934
|
+
*
|
|
935
|
+
* @module
|
|
936
|
+
*/
|
|
937
|
+
/**
|
|
938
|
+
* The type of intervention to apply when a trigger condition is met.
|
|
939
|
+
*
|
|
940
|
+
* - `warning`: Display a warning message to the user
|
|
941
|
+
* - `friction`: Add friction (e.g., delay, extra confirmation)
|
|
942
|
+
* - `challenge`: Require the user to complete a challenge
|
|
943
|
+
* - `block`: Block the action entirely
|
|
944
|
+
*/
|
|
945
|
+
type InterventionType = "warning" | "friction" | "challenge" | "block";
|
|
946
|
+
/**
|
|
947
|
+
* Threshold configuration for paste-based triggers.
|
|
948
|
+
*/
|
|
949
|
+
interface PasteThreshold {
|
|
950
|
+
/** Minimum text length to trigger intervention. */
|
|
951
|
+
minLength: number;
|
|
952
|
+
/** Maximum preceding keystrokes allowed before paste. */
|
|
953
|
+
maxPrecedingKeystrokes: number;
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Threshold configuration for blur/focus-based triggers.
|
|
957
|
+
*/
|
|
958
|
+
interface BlurThreshold {
|
|
959
|
+
/** Number of blur events to trigger intervention. */
|
|
960
|
+
countThreshold: number;
|
|
961
|
+
/** Total blur duration in milliseconds to trigger intervention. */
|
|
962
|
+
durationThreshold: number;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Threshold configuration for idle-based triggers.
|
|
966
|
+
*/
|
|
967
|
+
interface IdleThreshold {
|
|
968
|
+
/** Duration of idle time in milliseconds to trigger intervention. */
|
|
969
|
+
durationMs: number;
|
|
970
|
+
}
|
|
971
|
+
/**
|
|
972
|
+
* Threshold configuration for typing speed triggers.
|
|
973
|
+
*/
|
|
974
|
+
interface TimingThreshold {
|
|
975
|
+
/** Maximum characters per second before triggering intervention. */
|
|
976
|
+
maxCharactersPerSecond: number;
|
|
977
|
+
}
|
|
978
|
+
/**
|
|
979
|
+
* Configuration for a single trigger that can invoke an intervention.
|
|
980
|
+
*
|
|
981
|
+
* @example
|
|
982
|
+
* ```typescript
|
|
983
|
+
* const pasteTrigger: TriggerConfig = {
|
|
984
|
+
* type: "paste",
|
|
985
|
+
* threshold: {
|
|
986
|
+
* paste: { minLength: 100, maxPrecedingKeystrokes: 5 },
|
|
987
|
+
* },
|
|
988
|
+
* intervention: "warning",
|
|
989
|
+
* };
|
|
990
|
+
* ```
|
|
991
|
+
*/
|
|
992
|
+
interface TriggerConfig {
|
|
993
|
+
/**
|
|
994
|
+
* The type of behavior that triggers this intervention.
|
|
995
|
+
*
|
|
996
|
+
* - `paste`: Triggered by paste events
|
|
997
|
+
* - `blur`: Triggered by window/tab blur events
|
|
998
|
+
* - `idle`: Triggered by user inactivity
|
|
999
|
+
* - `timing`: Triggered by typing speed anomalies
|
|
1000
|
+
* - `composite`: Triggered by a combination of factors
|
|
1001
|
+
*/
|
|
1002
|
+
type: "paste" | "blur" | "idle" | "timing" | "composite";
|
|
1003
|
+
/**
|
|
1004
|
+
* Threshold values for this trigger.
|
|
1005
|
+
* Only the relevant threshold for the trigger type needs to be specified.
|
|
1006
|
+
*/
|
|
1007
|
+
threshold: {
|
|
1008
|
+
paste?: PasteThreshold;
|
|
1009
|
+
blur?: BlurThreshold;
|
|
1010
|
+
idle?: IdleThreshold;
|
|
1011
|
+
timing?: TimingThreshold;
|
|
1012
|
+
};
|
|
1013
|
+
/** The intervention type to apply when this trigger fires. */
|
|
1014
|
+
intervention: InterventionType;
|
|
1015
|
+
}
|
|
1016
|
+
/**
|
|
1017
|
+
* Result returned when an intervention is triggered.
|
|
1018
|
+
*
|
|
1019
|
+
* @example
|
|
1020
|
+
* ```typescript
|
|
1021
|
+
* const result: InterventionResult = {
|
|
1022
|
+
* type: "warning",
|
|
1023
|
+
* triggered: true,
|
|
1024
|
+
* reason: "Large paste detected with minimal preceding keystrokes",
|
|
1025
|
+
* triggerConfig: { ... },
|
|
1026
|
+
* timestamp: 1234567890,
|
|
1027
|
+
* };
|
|
1028
|
+
* ```
|
|
1029
|
+
*/
|
|
1030
|
+
interface InterventionResult$1 {
|
|
1031
|
+
/** The type of intervention that was triggered. */
|
|
1032
|
+
type: InterventionType;
|
|
1033
|
+
/** Whether the intervention was actually triggered. */
|
|
1034
|
+
triggered: boolean;
|
|
1035
|
+
/** Human-readable reason explaining why the intervention was triggered. */
|
|
1036
|
+
reason: string;
|
|
1037
|
+
/** The trigger configuration that caused this intervention. */
|
|
1038
|
+
triggerConfig: TriggerConfig;
|
|
1039
|
+
/** Timestamp when the intervention was triggered (performance.now()). */
|
|
1040
|
+
timestamp: number;
|
|
1041
|
+
}
|
|
1042
|
+
/**
|
|
1043
|
+
* Configuration for the InterventionManager.
|
|
1044
|
+
*
|
|
1045
|
+
* @example
|
|
1046
|
+
* ```typescript
|
|
1047
|
+
* const config: InterventionManagerConfig = {
|
|
1048
|
+
* triggers: [
|
|
1049
|
+
* {
|
|
1050
|
+
* type: "paste",
|
|
1051
|
+
* threshold: { paste: { minLength: 100, maxPrecedingKeystrokes: 5 } },
|
|
1052
|
+
* intervention: "warning",
|
|
1053
|
+
* },
|
|
1054
|
+
* {
|
|
1055
|
+
* type: "blur",
|
|
1056
|
+
* threshold: { blur: { countThreshold: 3, durationThreshold: 10000 } },
|
|
1057
|
+
* intervention: "friction",
|
|
1058
|
+
* },
|
|
1059
|
+
* ],
|
|
1060
|
+
* escalate: true,
|
|
1061
|
+
* };
|
|
1062
|
+
* ```
|
|
1063
|
+
*/
|
|
1064
|
+
interface InterventionManagerConfig {
|
|
1065
|
+
/** Array of trigger configurations to evaluate. */
|
|
1066
|
+
triggers: TriggerConfig[];
|
|
1067
|
+
/**
|
|
1068
|
+
* Whether to escalate intervention level on repeated triggers.
|
|
1069
|
+
* When true, repeated triggers will escalate: warning -> friction -> challenge -> block.
|
|
1070
|
+
* @default false
|
|
1071
|
+
*/
|
|
1072
|
+
escalate?: boolean;
|
|
1073
|
+
}
|
|
1074
|
+
/**
|
|
1075
|
+
* Behavioral state passed to the InterventionManager for evaluation.
|
|
1076
|
+
*/
|
|
1077
|
+
interface BehavioralState {
|
|
1078
|
+
/** Array of paste events with text length and preceding keystroke count. */
|
|
1079
|
+
pasteEvents?: {
|
|
1080
|
+
textLength: number;
|
|
1081
|
+
precedingKeystrokes: number;
|
|
1082
|
+
}[];
|
|
1083
|
+
/** Number of blur events that have occurred. */
|
|
1084
|
+
blurCount?: number;
|
|
1085
|
+
/** Total duration of blur events in milliseconds. */
|
|
1086
|
+
totalBlurDuration?: number;
|
|
1087
|
+
/** Current idle duration in milliseconds. */
|
|
1088
|
+
idleDuration?: number;
|
|
1089
|
+
/** Current typing speed in characters per second. */
|
|
1090
|
+
charactersPerSecond?: number;
|
|
1091
|
+
}
|
|
1092
|
+
|
|
1093
|
+
/**
|
|
1094
|
+
* Manager for evaluating behavioral triggers and applying interventions.
|
|
1095
|
+
*
|
|
1096
|
+
* @module
|
|
1097
|
+
*/
|
|
1098
|
+
|
|
1099
|
+
/**
|
|
1100
|
+
* Manages behavioral interventions based on configurable triggers.
|
|
1101
|
+
*
|
|
1102
|
+
* The InterventionManager evaluates behavioral state against configured triggers
|
|
1103
|
+
* and fires interventions when thresholds are exceeded. It supports callback
|
|
1104
|
+
* registration for each intervention type and optional escalation on repeated
|
|
1105
|
+
* triggers.
|
|
1106
|
+
*
|
|
1107
|
+
* @example
|
|
1108
|
+
* ```typescript
|
|
1109
|
+
* const manager = new InterventionManager({
|
|
1110
|
+
* triggers: [
|
|
1111
|
+
* {
|
|
1112
|
+
* type: "paste",
|
|
1113
|
+
* threshold: { paste: { minLength: 100, maxPrecedingKeystrokes: 5 } },
|
|
1114
|
+
* intervention: "warning",
|
|
1115
|
+
* },
|
|
1116
|
+
* ],
|
|
1117
|
+
* escalate: true,
|
|
1118
|
+
* });
|
|
1119
|
+
*
|
|
1120
|
+
* manager.onIntervention("warning", (result) => {
|
|
1121
|
+
* console.log("Warning triggered:", result.reason);
|
|
1122
|
+
* });
|
|
1123
|
+
*
|
|
1124
|
+
* const result = manager.evaluate({
|
|
1125
|
+
* pasteEvents: [{ textLength: 500, precedingKeystrokes: 2 }],
|
|
1126
|
+
* });
|
|
1127
|
+
*
|
|
1128
|
+
* if (result) {
|
|
1129
|
+
* console.log("Intervention fired:", result.type);
|
|
1130
|
+
* }
|
|
1131
|
+
* ```
|
|
1132
|
+
*/
|
|
1133
|
+
declare class InterventionManager$1 {
|
|
1134
|
+
private readonly triggers;
|
|
1135
|
+
private readonly escalate;
|
|
1136
|
+
private interventionHistory;
|
|
1137
|
+
private callbacks;
|
|
1138
|
+
private triggerCounts;
|
|
1139
|
+
/**
|
|
1140
|
+
* Creates a new InterventionManager.
|
|
1141
|
+
*
|
|
1142
|
+
* @param config - Configuration with triggers and escalation settings
|
|
1143
|
+
*/
|
|
1144
|
+
constructor(config: InterventionManagerConfig);
|
|
1145
|
+
/**
|
|
1146
|
+
* Evaluates triggers against the current behavioral state.
|
|
1147
|
+
*
|
|
1148
|
+
* Checks each configured trigger against the provided state and returns
|
|
1149
|
+
* an InterventionResult if any trigger fires. Only the first matching
|
|
1150
|
+
* trigger is returned.
|
|
1151
|
+
*
|
|
1152
|
+
* @param state - Current behavioral state to evaluate
|
|
1153
|
+
* @returns InterventionResult if a trigger fires, null otherwise
|
|
1154
|
+
*
|
|
1155
|
+
* @example
|
|
1156
|
+
* ```typescript
|
|
1157
|
+
* const result = manager.evaluate({
|
|
1158
|
+
* pasteEvents: [{ textLength: 500, precedingKeystrokes: 2 }],
|
|
1159
|
+
* blurCount: 5,
|
|
1160
|
+
* totalBlurDuration: 15000,
|
|
1161
|
+
* });
|
|
1162
|
+
* ```
|
|
1163
|
+
*/
|
|
1164
|
+
evaluate(state: BehavioralState): InterventionResult$1 | null;
|
|
1165
|
+
/**
|
|
1166
|
+
* Registers a callback for a specific intervention type.
|
|
1167
|
+
*
|
|
1168
|
+
* Callbacks are invoked whenever an intervention of the specified type
|
|
1169
|
+
* is triggered during evaluation.
|
|
1170
|
+
*
|
|
1171
|
+
* @param type - The intervention type to listen for
|
|
1172
|
+
* @param callback - Function to call when the intervention fires
|
|
1173
|
+
*
|
|
1174
|
+
* @example
|
|
1175
|
+
* ```typescript
|
|
1176
|
+
* manager.onIntervention("warning", (result) => {
|
|
1177
|
+
* alert(result.reason);
|
|
1178
|
+
* });
|
|
1179
|
+
*
|
|
1180
|
+
* manager.onIntervention("block", (result) => {
|
|
1181
|
+
* disableSubmitButton();
|
|
1182
|
+
* });
|
|
1183
|
+
* ```
|
|
1184
|
+
*/
|
|
1185
|
+
onIntervention(type: InterventionType, callback: (result: InterventionResult$1) => void): void;
|
|
1186
|
+
/**
|
|
1187
|
+
* Returns the history of all triggered interventions.
|
|
1188
|
+
*
|
|
1189
|
+
* @returns Copy of the intervention history array
|
|
1190
|
+
*
|
|
1191
|
+
* @example
|
|
1192
|
+
* ```typescript
|
|
1193
|
+
* const history = manager.getHistory();
|
|
1194
|
+
* console.log(`Total interventions: ${history.length}`);
|
|
1195
|
+
* ```
|
|
1196
|
+
*/
|
|
1197
|
+
getHistory(): InterventionResult$1[];
|
|
1198
|
+
/**
|
|
1199
|
+
* Resets the intervention history and trigger counts.
|
|
1200
|
+
*
|
|
1201
|
+
* Call this method to clear all recorded interventions and reset
|
|
1202
|
+
* escalation counters.
|
|
1203
|
+
*
|
|
1204
|
+
* @example
|
|
1205
|
+
* ```typescript
|
|
1206
|
+
* manager.reset();
|
|
1207
|
+
* // History is now empty and escalation counters are reset
|
|
1208
|
+
* ```
|
|
1209
|
+
*/
|
|
1210
|
+
reset(): void;
|
|
1211
|
+
/**
|
|
1212
|
+
* Evaluates a single trigger against the behavioral state.
|
|
1213
|
+
*/
|
|
1214
|
+
private evaluateTrigger;
|
|
1215
|
+
/**
|
|
1216
|
+
* Evaluates a paste-based trigger.
|
|
1217
|
+
*/
|
|
1218
|
+
private evaluatePasteTrigger;
|
|
1219
|
+
/**
|
|
1220
|
+
* Evaluates a blur-based trigger.
|
|
1221
|
+
*/
|
|
1222
|
+
private evaluateBlurTrigger;
|
|
1223
|
+
/**
|
|
1224
|
+
* Evaluates an idle-based trigger.
|
|
1225
|
+
*/
|
|
1226
|
+
private evaluateIdleTrigger;
|
|
1227
|
+
/**
|
|
1228
|
+
* Evaluates a timing-based trigger.
|
|
1229
|
+
*/
|
|
1230
|
+
private evaluateTimingTrigger;
|
|
1231
|
+
/**
|
|
1232
|
+
* Evaluates a composite trigger (combination of multiple factors).
|
|
1233
|
+
*/
|
|
1234
|
+
private evaluateCompositeTrigger;
|
|
1235
|
+
/**
|
|
1236
|
+
* Creates an InterventionResult from a trigger.
|
|
1237
|
+
*/
|
|
1238
|
+
private createResult;
|
|
1239
|
+
/**
|
|
1240
|
+
* Applies escalation to an intervention result based on trigger count.
|
|
1241
|
+
*/
|
|
1242
|
+
private applyEscalation;
|
|
1243
|
+
/**
|
|
1244
|
+
* Fires callbacks for an intervention result.
|
|
1245
|
+
*/
|
|
1246
|
+
private fireCallbacks;
|
|
1247
|
+
}
|
|
1248
|
+
/**
|
|
1249
|
+
* Creates a new InterventionManager instance.
|
|
1250
|
+
*
|
|
1251
|
+
* @param config - Configuration with triggers and escalation settings
|
|
1252
|
+
* @returns New InterventionManager instance
|
|
1253
|
+
*
|
|
1254
|
+
* @example
|
|
1255
|
+
* ```typescript
|
|
1256
|
+
* const manager = createInterventionManager({
|
|
1257
|
+
* triggers: [
|
|
1258
|
+
* {
|
|
1259
|
+
* type: "paste",
|
|
1260
|
+
* threshold: { paste: { minLength: 100, maxPrecedingKeystrokes: 5 } },
|
|
1261
|
+
* intervention: "warning",
|
|
1262
|
+
* },
|
|
1263
|
+
* ],
|
|
1264
|
+
* });
|
|
1265
|
+
* ```
|
|
1266
|
+
*/
|
|
1267
|
+
declare function createInterventionManager(config: InterventionManagerConfig): InterventionManager$1;
|
|
1268
|
+
|
|
1269
|
+
/**
|
|
1270
|
+
* Challenge system for verifying human presence during behavioral capture.
|
|
1271
|
+
*
|
|
1272
|
+
* Challenges are interactive tasks that collect behavioral data while
|
|
1273
|
+
* verifying participant engagement. Researchers can implement custom
|
|
1274
|
+
* challenge renderers or use the provided implementations.
|
|
1275
|
+
*
|
|
1276
|
+
* @module
|
|
1277
|
+
*/
|
|
1278
|
+
/**
|
|
1279
|
+
* Available challenge types.
|
|
1280
|
+
*
|
|
1281
|
+
* - `audio_transcription`: User transcribes audio content
|
|
1282
|
+
* - `image_description`: User describes visual content
|
|
1283
|
+
* - `typing_test`: User types displayed text (captures natural rhythm)
|
|
1284
|
+
* - `memory_recall`: User answers questions about previously shown content
|
|
1285
|
+
* - `consistency_check`: User re-answers a previous question for comparison
|
|
1286
|
+
*/
|
|
1287
|
+
type ChallengeType = "audio_transcription" | "image_description" | "typing_test" | "memory_recall" | "consistency_check";
|
|
1288
|
+
/**
|
|
1289
|
+
* Configuration for a challenge.
|
|
1290
|
+
*/
|
|
1291
|
+
interface ChallengeConfig {
|
|
1292
|
+
/**
|
|
1293
|
+
* Type of challenge to present.
|
|
1294
|
+
*/
|
|
1295
|
+
type: ChallengeType;
|
|
1296
|
+
/**
|
|
1297
|
+
* Prompt or instructions to display to the participant.
|
|
1298
|
+
*/
|
|
1299
|
+
prompt: string;
|
|
1300
|
+
/**
|
|
1301
|
+
* Keywords expected in a valid response.
|
|
1302
|
+
* Used for automatic verification of memory recall and description challenges.
|
|
1303
|
+
*/
|
|
1304
|
+
expectedKeywords?: string[];
|
|
1305
|
+
/**
|
|
1306
|
+
* Time limit for completing the challenge in milliseconds.
|
|
1307
|
+
* When exceeded, the challenge should auto-submit with current response.
|
|
1308
|
+
*/
|
|
1309
|
+
timeLimit?: number;
|
|
1310
|
+
/**
|
|
1311
|
+
* Type-specific configuration data.
|
|
1312
|
+
*
|
|
1313
|
+
* For `typing_test`: `{ text: string }` - the text to type
|
|
1314
|
+
* For `audio_transcription`: `{ audioUrl: string }` - URL of audio file
|
|
1315
|
+
* For `image_description`: `{ imageUrl: string }` - URL of image
|
|
1316
|
+
* For `memory_recall`: `{ question: string, context?: string }`
|
|
1317
|
+
* For `consistency_check`: `{ originalQuestion: string, originalAnswer: string }`
|
|
1318
|
+
*/
|
|
1319
|
+
data?: Record<string, unknown>;
|
|
1320
|
+
}
|
|
1321
|
+
/**
|
|
1322
|
+
* Result from a completed challenge.
|
|
1323
|
+
*/
|
|
1324
|
+
interface ChallengeResult {
|
|
1325
|
+
/**
|
|
1326
|
+
* Whether the challenge was successfully completed.
|
|
1327
|
+
* For typing tests, this indicates accuracy threshold was met.
|
|
1328
|
+
* For keyword-based challenges, this indicates required keywords were present.
|
|
1329
|
+
*/
|
|
1330
|
+
passed: boolean;
|
|
1331
|
+
/**
|
|
1332
|
+
* The participant's response text.
|
|
1333
|
+
*/
|
|
1334
|
+
response: string;
|
|
1335
|
+
/**
|
|
1336
|
+
* Time taken to complete the challenge in milliseconds.
|
|
1337
|
+
*/
|
|
1338
|
+
completionTime: number;
|
|
1339
|
+
/**
|
|
1340
|
+
* Behavioral data captured during the challenge.
|
|
1341
|
+
* Contains keystroke events, timing patterns, and other captured signals.
|
|
1342
|
+
*/
|
|
1343
|
+
behavioralData?: unknown;
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Interface for challenge renderers.
|
|
1347
|
+
*
|
|
1348
|
+
* Researchers implement this interface to customize how challenges
|
|
1349
|
+
* are presented to participants. The library provides default
|
|
1350
|
+
* implementations for common challenge types.
|
|
1351
|
+
*
|
|
1352
|
+
* @example
|
|
1353
|
+
* ```typescript
|
|
1354
|
+
* class CustomChallenge implements ChallengeRenderer {
|
|
1355
|
+
* private cleanup: (() => void) | null = null;
|
|
1356
|
+
*
|
|
1357
|
+
* async render(
|
|
1358
|
+
* config: ChallengeConfig,
|
|
1359
|
+
* container: HTMLElement
|
|
1360
|
+
* ): Promise<ChallengeResult> {
|
|
1361
|
+
* // Create and append UI elements to container
|
|
1362
|
+
* // Set up event listeners and capture
|
|
1363
|
+
* // Return result when participant submits
|
|
1364
|
+
* }
|
|
1365
|
+
*
|
|
1366
|
+
* cleanup(): void {
|
|
1367
|
+
* // Remove elements and event listeners
|
|
1368
|
+
* }
|
|
1369
|
+
* }
|
|
1370
|
+
* ```
|
|
1371
|
+
*/
|
|
1372
|
+
interface ChallengeRenderer {
|
|
1373
|
+
/**
|
|
1374
|
+
* Render the challenge UI and wait for completion.
|
|
1375
|
+
*
|
|
1376
|
+
* @param config - Challenge configuration
|
|
1377
|
+
* @param container - DOM element to render the challenge into
|
|
1378
|
+
* @returns Promise that resolves with the challenge result when submitted
|
|
1379
|
+
*/
|
|
1380
|
+
render(config: ChallengeConfig, container: HTMLElement): Promise<ChallengeResult>;
|
|
1381
|
+
/**
|
|
1382
|
+
* Clean up the challenge UI and release resources.
|
|
1383
|
+
* Called after the challenge completes or is cancelled.
|
|
1384
|
+
*/
|
|
1385
|
+
cleanup(): void;
|
|
1386
|
+
}
|
|
1387
|
+
/**
|
|
1388
|
+
* Typing test challenge that captures natural typing rhythm.
|
|
1389
|
+
*
|
|
1390
|
+
* Displays text for the participant to type, capturing keystroke
|
|
1391
|
+
* timing patterns that can be compared against their regular typing
|
|
1392
|
+
* behavior. This helps verify the same person is responding throughout
|
|
1393
|
+
* the study.
|
|
1394
|
+
*
|
|
1395
|
+
* @example
|
|
1396
|
+
* ```typescript
|
|
1397
|
+
* const challenge = new TypingTestChallenge();
|
|
1398
|
+
* const result = await challenge.render(
|
|
1399
|
+
* {
|
|
1400
|
+
* type: "typing_test",
|
|
1401
|
+
* prompt: "Please type the following text exactly as shown:",
|
|
1402
|
+
* data: { text: "The quick brown fox jumps over the lazy dog." },
|
|
1403
|
+
* timeLimit: 60000,
|
|
1404
|
+
* },
|
|
1405
|
+
* document.getElementById("challenge-container")!
|
|
1406
|
+
* );
|
|
1407
|
+
* console.log(result.passed, result.behavioralData);
|
|
1408
|
+
* challenge.cleanup();
|
|
1409
|
+
* ```
|
|
1410
|
+
*/
|
|
1411
|
+
declare class TypingTestChallenge implements ChallengeRenderer {
|
|
1412
|
+
private container;
|
|
1413
|
+
private startTime;
|
|
1414
|
+
private keystrokes;
|
|
1415
|
+
private boundHandlers;
|
|
1416
|
+
/**
|
|
1417
|
+
* Render the typing test challenge.
|
|
1418
|
+
*
|
|
1419
|
+
* @param config - Challenge configuration with `data.text` containing the text to type
|
|
1420
|
+
* @param container - Container element for the challenge UI
|
|
1421
|
+
* @returns Promise resolving to challenge result with typing accuracy and keystroke data
|
|
1422
|
+
*/
|
|
1423
|
+
render(config: ChallengeConfig, container: HTMLElement): Promise<ChallengeResult>;
|
|
1424
|
+
/**
|
|
1425
|
+
* Clean up challenge UI and event listeners.
|
|
1426
|
+
*/
|
|
1427
|
+
cleanup(): void;
|
|
1428
|
+
/**
|
|
1429
|
+
* Calculate typing accuracy as a ratio (0-1).
|
|
1430
|
+
* Uses a simple character-by-character comparison with length normalization.
|
|
1431
|
+
*/
|
|
1432
|
+
private calculateAccuracy;
|
|
1433
|
+
}
|
|
1434
|
+
/**
|
|
1435
|
+
* Memory recall challenge that verifies participant engagement.
|
|
1436
|
+
*
|
|
1437
|
+
* Presents a question about previously shown content and checks
|
|
1438
|
+
* if the response contains expected keywords. Captures typing
|
|
1439
|
+
* behavior during the response.
|
|
1440
|
+
*
|
|
1441
|
+
* @example
|
|
1442
|
+
* ```typescript
|
|
1443
|
+
* const challenge = new MemoryRecallChallenge();
|
|
1444
|
+
* const result = await challenge.render(
|
|
1445
|
+
* {
|
|
1446
|
+
* type: "memory_recall",
|
|
1447
|
+
* prompt: "What color was the car in the image you saw earlier?",
|
|
1448
|
+
* expectedKeywords: ["red", "sedan"],
|
|
1449
|
+
* timeLimit: 30000,
|
|
1450
|
+
* },
|
|
1451
|
+
* document.getElementById("challenge-container")!
|
|
1452
|
+
* );
|
|
1453
|
+
* ```
|
|
1454
|
+
*/
|
|
1455
|
+
declare class MemoryRecallChallenge implements ChallengeRenderer {
|
|
1456
|
+
private container;
|
|
1457
|
+
private startTime;
|
|
1458
|
+
private keystrokes;
|
|
1459
|
+
private boundHandlers;
|
|
1460
|
+
/**
|
|
1461
|
+
* Render the memory recall challenge.
|
|
1462
|
+
*
|
|
1463
|
+
* @param config - Challenge configuration with optional `expectedKeywords`
|
|
1464
|
+
* @param container - Container element for the challenge UI
|
|
1465
|
+
* @returns Promise resolving to challenge result with keyword matching status
|
|
1466
|
+
*/
|
|
1467
|
+
render(config: ChallengeConfig, container: HTMLElement): Promise<ChallengeResult>;
|
|
1468
|
+
/**
|
|
1469
|
+
* Clean up challenge UI and event listeners.
|
|
1470
|
+
*/
|
|
1471
|
+
cleanup(): void;
|
|
1472
|
+
/**
|
|
1473
|
+
* Check if response contains all expected keywords.
|
|
1474
|
+
*/
|
|
1475
|
+
private checkKeywords;
|
|
1476
|
+
/**
|
|
1477
|
+
* Get list of keywords that were matched in the response.
|
|
1478
|
+
*/
|
|
1479
|
+
private getMatchedKeywords;
|
|
1480
|
+
}
|
|
1481
|
+
/**
|
|
1482
|
+
* Create a challenge renderer for the specified type.
|
|
1483
|
+
*
|
|
1484
|
+
* Returns a built-in renderer for supported types (typing_test, memory_recall),
|
|
1485
|
+
* or null for types that require custom implementation (audio_transcription,
|
|
1486
|
+
* image_description, consistency_check).
|
|
1487
|
+
*
|
|
1488
|
+
* @param type - The challenge type
|
|
1489
|
+
* @returns A challenge renderer instance, or null if type requires custom implementation
|
|
1490
|
+
*
|
|
1491
|
+
* @example
|
|
1492
|
+
* ```typescript
|
|
1493
|
+
* const challenge = createChallenge("typing_test");
|
|
1494
|
+
* if (challenge) {
|
|
1495
|
+
* const result = await challenge.render(config, container);
|
|
1496
|
+
* challenge.cleanup();
|
|
1497
|
+
* }
|
|
1498
|
+
* ```
|
|
1499
|
+
*/
|
|
1500
|
+
declare function createChallenge(type: ChallengeType): ChallengeRenderer | null;
|
|
1501
|
+
|
|
1502
|
+
/**
|
|
1503
|
+
* Generic wrapper for HTML input elements combining behavioral capture with interventions.
|
|
1504
|
+
*
|
|
1505
|
+
* @module
|
|
1506
|
+
*/
|
|
1507
|
+
|
|
1508
|
+
/**
|
|
1509
|
+
* Result of an intervention action.
|
|
1510
|
+
*/
|
|
1511
|
+
interface InterventionResult {
|
|
1512
|
+
/**
|
|
1513
|
+
* Unique identifier for the intervention.
|
|
1514
|
+
*/
|
|
1515
|
+
id: string;
|
|
1516
|
+
/**
|
|
1517
|
+
* Timestamp when the intervention was triggered.
|
|
1518
|
+
*/
|
|
1519
|
+
timestamp: number;
|
|
1520
|
+
/**
|
|
1521
|
+
* Type of intervention applied.
|
|
1522
|
+
*/
|
|
1523
|
+
type: string;
|
|
1524
|
+
/**
|
|
1525
|
+
* Whether the intervention was shown to the user.
|
|
1526
|
+
*/
|
|
1527
|
+
shown: boolean;
|
|
1528
|
+
/**
|
|
1529
|
+
* User's response to the intervention, if any.
|
|
1530
|
+
*/
|
|
1531
|
+
userResponse?: string;
|
|
1532
|
+
/**
|
|
1533
|
+
* Duration the intervention was displayed in milliseconds.
|
|
1534
|
+
*/
|
|
1535
|
+
displayDuration?: number;
|
|
1536
|
+
/**
|
|
1537
|
+
* Additional metadata about the intervention.
|
|
1538
|
+
*/
|
|
1539
|
+
metadata?: Record<string, unknown>;
|
|
1540
|
+
}
|
|
1541
|
+
/**
|
|
1542
|
+
* Interface for managing interventions during behavioral capture.
|
|
1543
|
+
*
|
|
1544
|
+
* Implementations can provide various intervention strategies such as
|
|
1545
|
+
* warnings, confirmations, or educational prompts.
|
|
1546
|
+
*/
|
|
1547
|
+
interface InterventionManager {
|
|
1548
|
+
/**
|
|
1549
|
+
* Unique name for this intervention manager.
|
|
1550
|
+
*/
|
|
1551
|
+
readonly name: string;
|
|
1552
|
+
/**
|
|
1553
|
+
* Evaluates whether an intervention should be triggered.
|
|
1554
|
+
*
|
|
1555
|
+
* @param data - Current behavioral data
|
|
1556
|
+
* @param flags - Current capture flags
|
|
1557
|
+
* @returns Whether an intervention should be shown
|
|
1558
|
+
*/
|
|
1559
|
+
shouldIntervene(data: BehavioralData, flags: CaptureFlag[]): boolean;
|
|
1560
|
+
/**
|
|
1561
|
+
* Executes the intervention and returns the result.
|
|
1562
|
+
*
|
|
1563
|
+
* @param data - Current behavioral data
|
|
1564
|
+
* @param flags - Current capture flags
|
|
1565
|
+
* @returns Promise resolving to the intervention result
|
|
1566
|
+
*/
|
|
1567
|
+
intervene(data: BehavioralData, flags: CaptureFlag[]): Promise<InterventionResult>;
|
|
1568
|
+
/**
|
|
1569
|
+
* Resets the intervention manager state.
|
|
1570
|
+
*/
|
|
1571
|
+
reset(): void;
|
|
1572
|
+
}
|
|
1573
|
+
/**
|
|
1574
|
+
* Event types emitted by the InputWrapper.
|
|
1575
|
+
*/
|
|
1576
|
+
type WrapperEvent = "data" | "flag" | "intervention" | "detection";
|
|
1577
|
+
/**
|
|
1578
|
+
* Configuration for InputWrapper.
|
|
1579
|
+
*/
|
|
1580
|
+
interface InputWrapperConfig {
|
|
1581
|
+
/**
|
|
1582
|
+
* Configuration for behavioral capture.
|
|
1583
|
+
*/
|
|
1584
|
+
capture?: BehavioralCaptureConfig;
|
|
1585
|
+
/**
|
|
1586
|
+
* Optional intervention manager for handling detected behaviors.
|
|
1587
|
+
*/
|
|
1588
|
+
interventionManager?: InterventionManager;
|
|
1589
|
+
/**
|
|
1590
|
+
* Array of detectors to run periodically.
|
|
1591
|
+
*/
|
|
1592
|
+
detectors?: Detector[];
|
|
1593
|
+
/**
|
|
1594
|
+
* Interval in milliseconds between detector runs.
|
|
1595
|
+
* @default 5000
|
|
1596
|
+
*/
|
|
1597
|
+
detectionInterval?: number;
|
|
1598
|
+
}
|
|
1599
|
+
/**
|
|
1600
|
+
* Extended behavioral data including flags, detection results, and interventions.
|
|
1601
|
+
*/
|
|
1602
|
+
interface EnhancedBehavioralData extends BehavioralData {
|
|
1603
|
+
/**
|
|
1604
|
+
* Flags generated during capture.
|
|
1605
|
+
*/
|
|
1606
|
+
flags: CaptureFlag[];
|
|
1607
|
+
/**
|
|
1608
|
+
* Results from all detector runs.
|
|
1609
|
+
*/
|
|
1610
|
+
detectionResults: DetectionResult[];
|
|
1611
|
+
/**
|
|
1612
|
+
* Results from all interventions.
|
|
1613
|
+
*/
|
|
1614
|
+
interventions: InterventionResult[];
|
|
1615
|
+
}
|
|
1616
|
+
/**
|
|
1617
|
+
* Event handler type for wrapper events.
|
|
1618
|
+
*/
|
|
1619
|
+
type WrapperEventHandler<T> = (data: T) => void;
|
|
1620
|
+
/**
|
|
1621
|
+
* Generic wrapper for HTML input elements combining behavioral capture with detection and intervention.
|
|
1622
|
+
*
|
|
1623
|
+
* The InputWrapper provides a unified interface for:
|
|
1624
|
+
* - Behavioral data capture via BehavioralCapture
|
|
1625
|
+
* - Periodic detection via configurable Detectors
|
|
1626
|
+
* - Intervention handling via InterventionManager
|
|
1627
|
+
* - Event-based notification of capture, detection, and intervention events
|
|
1628
|
+
*
|
|
1629
|
+
* @example
|
|
1630
|
+
* ```typescript
|
|
1631
|
+
* import { InputWrapper, createInputWrapper } from "@slopit/behavioral";
|
|
1632
|
+
*
|
|
1633
|
+
* // Create wrapper with default configuration
|
|
1634
|
+
* const wrapper = createInputWrapper({
|
|
1635
|
+
* capture: { keystroke: { enabled: true } },
|
|
1636
|
+
* detectionInterval: 3000,
|
|
1637
|
+
* });
|
|
1638
|
+
*
|
|
1639
|
+
* // Wrap an input element
|
|
1640
|
+
* const textarea = document.getElementById("response") as HTMLTextAreaElement;
|
|
1641
|
+
* wrapper.wrap(textarea);
|
|
1642
|
+
*
|
|
1643
|
+
* // Subscribe to events
|
|
1644
|
+
* wrapper.on("detection", (result) => {
|
|
1645
|
+
* console.log("Detection:", result);
|
|
1646
|
+
* });
|
|
1647
|
+
*
|
|
1648
|
+
* // Later, get all captured data
|
|
1649
|
+
* const data = wrapper.getData();
|
|
1650
|
+
*
|
|
1651
|
+
* // Cleanup
|
|
1652
|
+
* wrapper.unwrap();
|
|
1653
|
+
* ```
|
|
1654
|
+
*/
|
|
1655
|
+
declare class InputWrapper {
|
|
1656
|
+
private element;
|
|
1657
|
+
private capture;
|
|
1658
|
+
private interventionManager?;
|
|
1659
|
+
private detectors;
|
|
1660
|
+
private detectionInterval;
|
|
1661
|
+
private detectionTimer?;
|
|
1662
|
+
private detectionResults;
|
|
1663
|
+
private interventionResults;
|
|
1664
|
+
private listeners;
|
|
1665
|
+
private isInterventionInProgress;
|
|
1666
|
+
/**
|
|
1667
|
+
* Creates a new InputWrapper instance.
|
|
1668
|
+
*
|
|
1669
|
+
* @param config - Configuration options for the wrapper
|
|
1670
|
+
*/
|
|
1671
|
+
constructor(config?: InputWrapperConfig);
|
|
1672
|
+
/**
|
|
1673
|
+
* Wraps an input element with behavioral capture.
|
|
1674
|
+
*
|
|
1675
|
+
* Attaches event listeners to the element and starts detection
|
|
1676
|
+
* if detectors are configured.
|
|
1677
|
+
*
|
|
1678
|
+
* @param element - The textarea or input element to wrap
|
|
1679
|
+
* @throws {Error} If the wrapper is already attached to an element
|
|
1680
|
+
*
|
|
1681
|
+
* @example
|
|
1682
|
+
* ```typescript
|
|
1683
|
+
* const wrapper = createInputWrapper();
|
|
1684
|
+
* const input = document.querySelector("textarea");
|
|
1685
|
+
* wrapper.wrap(input);
|
|
1686
|
+
* ```
|
|
1687
|
+
*/
|
|
1688
|
+
wrap(element: HTMLTextAreaElement | HTMLInputElement): void;
|
|
1689
|
+
/**
|
|
1690
|
+
* Unwraps the element and cleans up all resources.
|
|
1691
|
+
*
|
|
1692
|
+
* Stops detection, detaches capture, and clears all internal state.
|
|
1693
|
+
*
|
|
1694
|
+
* @example
|
|
1695
|
+
* ```typescript
|
|
1696
|
+
* wrapper.unwrap();
|
|
1697
|
+
* // Can now wrap a different element
|
|
1698
|
+
* wrapper.wrap(anotherElement);
|
|
1699
|
+
* ```
|
|
1700
|
+
*/
|
|
1701
|
+
unwrap(): void;
|
|
1702
|
+
/**
|
|
1703
|
+
* Resets all collected data without unwrapping.
|
|
1704
|
+
*
|
|
1705
|
+
* Clears captured data, detection results, and intervention results
|
|
1706
|
+
* while maintaining the element attachment.
|
|
1707
|
+
*
|
|
1708
|
+
* @param newStartTime - Optional new start time for capture timing
|
|
1709
|
+
*/
|
|
1710
|
+
reset(newStartTime?: number): void;
|
|
1711
|
+
/**
|
|
1712
|
+
* Gets all captured data including flags, detection results, and interventions.
|
|
1713
|
+
*
|
|
1714
|
+
* @returns Enhanced behavioral data with all capture information
|
|
1715
|
+
*
|
|
1716
|
+
* @example
|
|
1717
|
+
* ```typescript
|
|
1718
|
+
* const data = wrapper.getData();
|
|
1719
|
+
* console.log("Keystrokes:", data.keystrokes?.length);
|
|
1720
|
+
* console.log("Flags:", data.flags.length);
|
|
1721
|
+
* console.log("Detections:", data.detectionResults.length);
|
|
1722
|
+
* ```
|
|
1723
|
+
*/
|
|
1724
|
+
getData(): EnhancedBehavioralData;
|
|
1725
|
+
/**
|
|
1726
|
+
* Gets flags generated during capture.
|
|
1727
|
+
*
|
|
1728
|
+
* @returns Array of capture flags
|
|
1729
|
+
*/
|
|
1730
|
+
getFlags(): CaptureFlag[];
|
|
1731
|
+
/**
|
|
1732
|
+
* Gets all detection results from detector runs.
|
|
1733
|
+
*
|
|
1734
|
+
* @returns Array of detection results
|
|
1735
|
+
*/
|
|
1736
|
+
getDetectionResults(): DetectionResult[];
|
|
1737
|
+
/**
|
|
1738
|
+
* Gets all intervention results.
|
|
1739
|
+
*
|
|
1740
|
+
* @returns Array of intervention results
|
|
1741
|
+
*/
|
|
1742
|
+
getInterventionResults(): InterventionResult[];
|
|
1743
|
+
/**
|
|
1744
|
+
* Manually triggers all detectors immediately.
|
|
1745
|
+
*
|
|
1746
|
+
* Useful for running detection on demand rather than waiting
|
|
1747
|
+
* for the next scheduled interval.
|
|
1748
|
+
*
|
|
1749
|
+
* @returns Array of detection results from this run
|
|
1750
|
+
*/
|
|
1751
|
+
runDetection(): DetectionResult[];
|
|
1752
|
+
/**
|
|
1753
|
+
* Subscribes to wrapper events.
|
|
1754
|
+
*
|
|
1755
|
+
* @param event - The event type to subscribe to
|
|
1756
|
+
* @param handler - The callback function to invoke
|
|
1757
|
+
*
|
|
1758
|
+
* @example
|
|
1759
|
+
* ```typescript
|
|
1760
|
+
* wrapper.on("flag", (flag) => {
|
|
1761
|
+
* console.log("Flag generated:", flag.type);
|
|
1762
|
+
* });
|
|
1763
|
+
*
|
|
1764
|
+
* wrapper.on("detection", (result) => {
|
|
1765
|
+
* if (result.detected) {
|
|
1766
|
+
* console.log("Detected:", result.message);
|
|
1767
|
+
* }
|
|
1768
|
+
* });
|
|
1769
|
+
* ```
|
|
1770
|
+
*/
|
|
1771
|
+
on(event: WrapperEvent, handler: WrapperEventHandler<unknown>): void;
|
|
1772
|
+
/**
|
|
1773
|
+
* Unsubscribes from wrapper events.
|
|
1774
|
+
*
|
|
1775
|
+
* @param event - The event type to unsubscribe from
|
|
1776
|
+
* @param handler - The callback function to remove
|
|
1777
|
+
*/
|
|
1778
|
+
off(event: WrapperEvent, handler: WrapperEventHandler<unknown>): void;
|
|
1779
|
+
/**
|
|
1780
|
+
* Starts the detection timer for periodic detector runs.
|
|
1781
|
+
*/
|
|
1782
|
+
private startDetection;
|
|
1783
|
+
/**
|
|
1784
|
+
* Stops the detection timer and monitoring.
|
|
1785
|
+
*/
|
|
1786
|
+
private stopDetection;
|
|
1787
|
+
/**
|
|
1788
|
+
* Sets up intervention checks on behavioral capture events.
|
|
1789
|
+
*/
|
|
1790
|
+
private setupInterventionChecks;
|
|
1791
|
+
/**
|
|
1792
|
+
* Checks if an intervention should be triggered and executes it.
|
|
1793
|
+
*/
|
|
1794
|
+
private checkIntervention;
|
|
1795
|
+
/**
|
|
1796
|
+
* Emits an event to all subscribed handlers.
|
|
1797
|
+
*
|
|
1798
|
+
* @param event - The event type
|
|
1799
|
+
* @param data - The event data
|
|
1800
|
+
*/
|
|
1801
|
+
private emit;
|
|
1802
|
+
}
|
|
1803
|
+
/**
|
|
1804
|
+
* Factory function to create an InputWrapper instance.
|
|
1805
|
+
*
|
|
1806
|
+
* @param config - Optional configuration for the wrapper
|
|
1807
|
+
* @returns A new InputWrapper instance
|
|
1808
|
+
*
|
|
1809
|
+
* @example
|
|
1810
|
+
* ```typescript
|
|
1811
|
+
* const wrapper = createInputWrapper({
|
|
1812
|
+
* capture: {
|
|
1813
|
+
* keystroke: { enabled: true },
|
|
1814
|
+
* paste: { enabled: true, prevent: false },
|
|
1815
|
+
* },
|
|
1816
|
+
* detectionInterval: 3000,
|
|
1817
|
+
* });
|
|
1818
|
+
* ```
|
|
1819
|
+
*/
|
|
1820
|
+
declare function createInputWrapper(config?: InputWrapperConfig): InputWrapper;
|
|
1821
|
+
|
|
1822
|
+
export { BehavioralCapture, type BehavioralCaptureConfig, type BehavioralCaptureEvents, type BehavioralState, type BlurThreshold, type ChallengeConfig, type ChallengeRenderer, type ChallengeResult, type ChallengeType, type ClipboardCaptureConfig, ClipboardCollector, type DetectionIndicator, type DetectionResult, type Detector, type EnhancedBehavioralData, ExtensionDetector, type ExtensionDetectorConfig, type FocusCaptureConfig, FocusCollector, type IdleThreshold, InputWrapper, type InputWrapperConfig, InterventionManager$1 as InterventionManager, type InterventionManagerConfig, type InterventionResult$1 as InterventionResult, type InterventionType, type KeystrokeCaptureConfig, KeystrokeCollector, MemoryRecallChallenge, type PasteCaptureConfig, PasteCollector, type PasteThreshold, type TextAppearanceCallbacks, TextAppearanceDetector, type TextAppearanceDetectorConfig, type TimingThreshold, type TriggerConfig, TypingTestChallenge, type WrapperEvent, type InterventionManager as WrapperInterventionManager, type InterventionResult as WrapperInterventionResult, computeFocusMetrics, computeKeystrokeMetrics, computeTimingMetrics, createBehavioralCapture, createChallenge, createExtensionDetector, createInputWrapper, createInterventionManager };
|