@slopit/core 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.
@@ -0,0 +1,849 @@
1
+ /**
2
+ * Modifier key states at time of keystroke.
3
+ */
4
+ interface ModifierState {
5
+ shift: boolean;
6
+ ctrl: boolean;
7
+ alt: boolean;
8
+ meta: boolean;
9
+ }
10
+ /**
11
+ * A single keystroke event.
12
+ */
13
+ interface KeystrokeEvent {
14
+ /**
15
+ * Time since trial start in milliseconds.
16
+ */
17
+ time: number;
18
+ /**
19
+ * Key value from KeyboardEvent.key.
20
+ * Examples: "a", "Enter", "Backspace", "Shift"
21
+ */
22
+ key: string;
23
+ /**
24
+ * Physical key code from KeyboardEvent.code.
25
+ * Examples: "KeyA", "Enter", "Backspace", "ShiftLeft"
26
+ */
27
+ code: string;
28
+ /**
29
+ * Event type.
30
+ */
31
+ event: "keydown" | "keyup";
32
+ /**
33
+ * Current text length at this moment.
34
+ * Captured from input element.
35
+ */
36
+ textLength?: number;
37
+ /**
38
+ * Modifier key states.
39
+ */
40
+ modifiers?: ModifierState;
41
+ }
42
+ /**
43
+ * A focus or visibility change event.
44
+ */
45
+ interface FocusEvent {
46
+ /**
47
+ * Time since trial start in milliseconds.
48
+ */
49
+ time: number;
50
+ /**
51
+ * Event type.
52
+ * - focus: window gained focus
53
+ * - blur: window lost focus
54
+ * - visibilitychange: document visibility changed
55
+ */
56
+ event: "focus" | "blur" | "visibilitychange";
57
+ /**
58
+ * For visibilitychange events, the new state.
59
+ */
60
+ visibility?: "visible" | "hidden";
61
+ /**
62
+ * For blur events, duration until refocus.
63
+ * Filled in retrospectively when focus returns.
64
+ */
65
+ blurDuration?: number;
66
+ }
67
+ /**
68
+ * A paste event.
69
+ */
70
+ interface PasteEvent {
71
+ /**
72
+ * Time since trial start in milliseconds.
73
+ */
74
+ time: number;
75
+ /**
76
+ * Length of pasted text in characters.
77
+ */
78
+ textLength: number;
79
+ /**
80
+ * First N characters of pasted text.
81
+ * For analysis purposes; default N=100.
82
+ */
83
+ textPreview?: string;
84
+ /**
85
+ * SHA-256 hash of full pasted text.
86
+ */
87
+ textHash?: string;
88
+ /**
89
+ * Number of keystrokes in preceding 2 seconds.
90
+ * Low value suggests copy from external source.
91
+ */
92
+ precedingKeystrokes: number;
93
+ /**
94
+ * Whether paste was blocked by configuration.
95
+ */
96
+ blocked: boolean;
97
+ }
98
+ /**
99
+ * Metrics computed from keystroke data.
100
+ */
101
+ interface KeystrokeMetrics {
102
+ /**
103
+ * Total number of keystrokes.
104
+ */
105
+ totalKeystrokes: number;
106
+ /**
107
+ * Number of printable character keystrokes.
108
+ */
109
+ printableKeystrokes: number;
110
+ /**
111
+ * Number of deletion keystrokes (Backspace, Delete).
112
+ */
113
+ deletions: number;
114
+ /**
115
+ * Mean inter-keystroke interval in milliseconds.
116
+ */
117
+ meanIKI: number;
118
+ /**
119
+ * Standard deviation of IKI.
120
+ */
121
+ stdIKI: number;
122
+ /**
123
+ * Median IKI.
124
+ */
125
+ medianIKI: number;
126
+ /**
127
+ * Number of pauses (IKI > threshold).
128
+ */
129
+ pauseCount: number;
130
+ /**
131
+ * Ratio of final characters to total keystrokes.
132
+ * Low values indicate heavy revision.
133
+ */
134
+ productProcessRatio: number;
135
+ }
136
+ /**
137
+ * Metrics computed from focus data.
138
+ */
139
+ interface FocusMetrics {
140
+ /**
141
+ * Number of blur events.
142
+ */
143
+ blurCount: number;
144
+ /**
145
+ * Total time in milliseconds with window blurred.
146
+ */
147
+ totalBlurDuration: number;
148
+ /**
149
+ * Number of visibility hidden events.
150
+ */
151
+ hiddenCount: number;
152
+ /**
153
+ * Total time in milliseconds with document hidden.
154
+ */
155
+ totalHiddenDuration: number;
156
+ }
157
+ /**
158
+ * Timing metrics.
159
+ */
160
+ interface TimingMetrics {
161
+ /**
162
+ * Time from trial start to first keystroke.
163
+ */
164
+ firstKeystrokeLatency?: number;
165
+ /**
166
+ * Time from trial start to response submission.
167
+ */
168
+ totalResponseTime: number;
169
+ /**
170
+ * Characters per minute (excluding deletions).
171
+ */
172
+ charactersPerMinute?: number;
173
+ }
174
+ /**
175
+ * Computed metrics from behavioral data.
176
+ * Calculated client-side for real-time use.
177
+ */
178
+ interface BehavioralMetrics {
179
+ /**
180
+ * Keystroke metrics.
181
+ */
182
+ keystroke?: KeystrokeMetrics;
183
+ /**
184
+ * Focus metrics.
185
+ */
186
+ focus?: FocusMetrics;
187
+ /**
188
+ * Timing metrics.
189
+ */
190
+ timing?: TimingMetrics;
191
+ /**
192
+ * Mouse metrics.
193
+ */
194
+ mouse?: MouseMetrics;
195
+ /**
196
+ * Scroll metrics.
197
+ */
198
+ scroll?: ScrollMetrics;
199
+ /**
200
+ * Input duration metrics.
201
+ */
202
+ inputDuration?: InputDurationMetrics;
203
+ }
204
+ /**
205
+ * Container for all behavioral capture data.
206
+ */
207
+ interface BehavioralData {
208
+ /**
209
+ * Keystroke events.
210
+ */
211
+ keystrokes?: KeystrokeEvent[];
212
+ /**
213
+ * Focus/visibility events.
214
+ */
215
+ focus?: FocusEvent[];
216
+ /**
217
+ * Paste events.
218
+ */
219
+ paste?: PasteEvent[];
220
+ /**
221
+ * Mouse events (optional, high volume).
222
+ */
223
+ mouse?: MouseEvent[];
224
+ /**
225
+ * Scroll events.
226
+ */
227
+ scroll?: ScrollEvent[];
228
+ /**
229
+ * Clipboard copy/cut events.
230
+ */
231
+ clipboard?: ClipboardCopyEvent[];
232
+ /**
233
+ * Input duration events.
234
+ */
235
+ inputDuration?: InputDurationEvent[];
236
+ /**
237
+ * Computed metrics from capture.
238
+ */
239
+ metrics?: BehavioralMetrics;
240
+ }
241
+ /**
242
+ * Mouse event (optional capture).
243
+ */
244
+ interface MouseEvent {
245
+ /**
246
+ * Time since trial start in milliseconds.
247
+ */
248
+ time: number;
249
+ /**
250
+ * Event type.
251
+ */
252
+ event: "mousemove" | "mousedown" | "mouseup" | "click";
253
+ /**
254
+ * X coordinate relative to viewport.
255
+ */
256
+ x: number;
257
+ /**
258
+ * Y coordinate relative to viewport.
259
+ */
260
+ y: number;
261
+ /**
262
+ * Velocity in pixels per millisecond (for mousemove events).
263
+ */
264
+ velocity?: number;
265
+ /**
266
+ * Distance traveled since last event in pixels (for mousemove).
267
+ */
268
+ distance?: number;
269
+ /**
270
+ * Time since last mouse event in milliseconds.
271
+ */
272
+ deltaTime?: number;
273
+ /**
274
+ * Whether this event is part of a drag operation.
275
+ */
276
+ isDragging?: boolean;
277
+ }
278
+ /**
279
+ * Scroll event for reading vs composing behavior detection.
280
+ */
281
+ interface ScrollEvent {
282
+ /**
283
+ * Time since trial start in milliseconds.
284
+ */
285
+ time: number;
286
+ /**
287
+ * Direction of scroll.
288
+ */
289
+ direction: "up" | "down";
290
+ /**
291
+ * Scroll delta in pixels.
292
+ */
293
+ deltaY: number;
294
+ /**
295
+ * Current scroll position from top.
296
+ */
297
+ scrollTop: number;
298
+ /**
299
+ * Scrollable element height.
300
+ */
301
+ scrollHeight: number;
302
+ /**
303
+ * Viewport height.
304
+ */
305
+ clientHeight: number;
306
+ /**
307
+ * Velocity in pixels per millisecond.
308
+ */
309
+ velocity?: number;
310
+ }
311
+ /**
312
+ * Clipboard event for copy/cut tracking with selection context.
313
+ */
314
+ interface ClipboardCopyEvent {
315
+ /**
316
+ * Time since trial start in milliseconds.
317
+ */
318
+ time: number;
319
+ /**
320
+ * Event type.
321
+ */
322
+ event: "copy" | "cut";
323
+ /**
324
+ * Length of selected text.
325
+ */
326
+ textLength: number;
327
+ /**
328
+ * First N characters of copied text.
329
+ */
330
+ textPreview?: string;
331
+ /**
332
+ * SHA-256 hash of copied text.
333
+ */
334
+ textHash?: string;
335
+ /**
336
+ * Selection start index in input element.
337
+ */
338
+ selectionStart?: number;
339
+ /**
340
+ * Selection end index in input element.
341
+ */
342
+ selectionEnd?: number;
343
+ }
344
+ /**
345
+ * Input duration event for per-field focus tracking.
346
+ */
347
+ interface InputDurationEvent {
348
+ /**
349
+ * Time since trial start when focus was gained.
350
+ */
351
+ focusTime: number;
352
+ /**
353
+ * Time since trial start when focus was lost.
354
+ */
355
+ blurTime: number;
356
+ /**
357
+ * Duration focused in milliseconds.
358
+ */
359
+ duration: number;
360
+ /**
361
+ * Element identifier (id or name attribute).
362
+ */
363
+ elementId?: string;
364
+ /**
365
+ * Number of keystrokes during focus.
366
+ */
367
+ keystrokesDuringFocus?: number;
368
+ /**
369
+ * Number of paste events during focus.
370
+ */
371
+ pastesDuringFocus?: number;
372
+ }
373
+ /**
374
+ * Metrics computed from mouse data.
375
+ */
376
+ interface MouseMetrics {
377
+ /**
378
+ * Total number of mouse events.
379
+ */
380
+ totalEvents: number;
381
+ /**
382
+ * Number of click events.
383
+ */
384
+ clickCount: number;
385
+ /**
386
+ * Mean mouse velocity in pixels per millisecond.
387
+ */
388
+ meanVelocity: number;
389
+ /**
390
+ * Total distance traveled by cursor in pixels.
391
+ */
392
+ totalDistance: number;
393
+ /**
394
+ * Total idle time with no mouse movement in milliseconds.
395
+ */
396
+ totalIdleTime: number;
397
+ /**
398
+ * Number of idle periods exceeding threshold.
399
+ */
400
+ idlePeriodCount: number;
401
+ }
402
+ /**
403
+ * Metrics computed from scroll data.
404
+ */
405
+ interface ScrollMetrics {
406
+ /**
407
+ * Total number of scroll events.
408
+ */
409
+ totalScrollEvents: number;
410
+ /**
411
+ * Total scroll distance in pixels.
412
+ */
413
+ totalScrollDistance: number;
414
+ /**
415
+ * Number of scroll direction changes.
416
+ */
417
+ directionChanges: number;
418
+ /**
419
+ * Mean scroll velocity.
420
+ */
421
+ meanScrollVelocity: number;
422
+ /**
423
+ * Time spent scrolling up vs down ratio.
424
+ */
425
+ upDownRatio: number;
426
+ }
427
+ /**
428
+ * Metrics computed from input duration data.
429
+ */
430
+ interface InputDurationMetrics {
431
+ /**
432
+ * Total focus duration across all inputs.
433
+ */
434
+ totalFocusDuration: number;
435
+ /**
436
+ * Mean focus duration per input session.
437
+ */
438
+ meanFocusDuration: number;
439
+ /**
440
+ * Number of focus sessions.
441
+ */
442
+ focusSessionCount: number;
443
+ /**
444
+ * Longest single focus session.
445
+ */
446
+ maxFocusDuration: number;
447
+ }
448
+
449
+ /**
450
+ * Severity level for flags.
451
+ */
452
+ type Severity = "info" | "low" | "medium" | "high";
453
+ /**
454
+ * Flag generated during data capture.
455
+ */
456
+ interface CaptureFlag {
457
+ /**
458
+ * Flag type identifier.
459
+ */
460
+ type: string;
461
+ /**
462
+ * Severity level.
463
+ */
464
+ severity: Severity;
465
+ /**
466
+ * Human-readable description.
467
+ */
468
+ message: string;
469
+ /**
470
+ * Timestamp when flag was generated.
471
+ */
472
+ timestamp: number;
473
+ /**
474
+ * Additional details.
475
+ */
476
+ details?: Record<string, unknown>;
477
+ }
478
+ /**
479
+ * Flag generated during server-side analysis.
480
+ */
481
+ interface AnalysisFlag {
482
+ /**
483
+ * Flag type identifier.
484
+ */
485
+ type: string;
486
+ /**
487
+ * Analyzer that generated this flag.
488
+ */
489
+ analyzer: string;
490
+ /**
491
+ * Severity level.
492
+ */
493
+ severity: Severity;
494
+ /**
495
+ * Human-readable description.
496
+ */
497
+ message: string;
498
+ /**
499
+ * Confidence score if applicable (0.0 to 1.0).
500
+ */
501
+ confidence?: number;
502
+ /**
503
+ * Evidence supporting this flag.
504
+ */
505
+ evidence?: Record<string, unknown>;
506
+ /**
507
+ * Related trial IDs if trial-specific.
508
+ */
509
+ trialIds?: string[];
510
+ }
511
+
512
+ /**
513
+ * Information about the experiment platform.
514
+ */
515
+ interface PlatformInfo {
516
+ /**
517
+ * Platform name.
518
+ * Known values: "jspsych", "labjs", "psychojs", "gorilla", "qualtrics", "custom"
519
+ */
520
+ name: string;
521
+ /**
522
+ * Platform version string.
523
+ */
524
+ version?: string;
525
+ /**
526
+ * Adapter version used for capture.
527
+ */
528
+ adapterVersion?: string;
529
+ }
530
+ /**
531
+ * Client environment information.
532
+ * Captured at session start.
533
+ */
534
+ interface EnvironmentInfo {
535
+ /**
536
+ * Browser user agent string.
537
+ */
538
+ userAgent: string;
539
+ /**
540
+ * Screen dimensions in pixels.
541
+ */
542
+ screenResolution: [width: number, height: number];
543
+ /**
544
+ * Viewport dimensions in pixels.
545
+ */
546
+ viewportSize: [width: number, height: number];
547
+ /**
548
+ * Device pixel ratio.
549
+ */
550
+ devicePixelRatio: number;
551
+ /**
552
+ * IANA timezone identifier.
553
+ * Example: "America/New_York"
554
+ */
555
+ timezone: string;
556
+ /**
557
+ * Browser language setting.
558
+ * Example: "en-US"
559
+ */
560
+ language: string;
561
+ /**
562
+ * Whether touch input is available.
563
+ */
564
+ touchCapable: boolean;
565
+ /**
566
+ * Estimated connection type if available.
567
+ * From Navigator.connection API.
568
+ */
569
+ connectionType?: string;
570
+ }
571
+ /**
572
+ * Session-level timing information.
573
+ */
574
+ interface SessionTiming {
575
+ /**
576
+ * Session start time.
577
+ * Unix timestamp in milliseconds.
578
+ */
579
+ startTime: number;
580
+ /**
581
+ * Session end time.
582
+ * Unix timestamp in milliseconds.
583
+ * Undefined if session incomplete.
584
+ */
585
+ endTime?: number;
586
+ /**
587
+ * Total duration in milliseconds.
588
+ * Computed as endTime - startTime.
589
+ */
590
+ duration?: number;
591
+ }
592
+ /**
593
+ * Information about the trial stimulus.
594
+ */
595
+ interface StimulusInfo {
596
+ /**
597
+ * Stimulus type.
598
+ */
599
+ type: "text" | "image" | "audio" | "video" | "html" | "other";
600
+ /**
601
+ * Stimulus content.
602
+ * For text: the text content.
603
+ * For media: URL or data URI.
604
+ * For HTML: the HTML string.
605
+ */
606
+ content?: string;
607
+ /**
608
+ * SHA-256 hash of content for deduplication.
609
+ */
610
+ contentHash?: string;
611
+ /**
612
+ * Additional stimulus parameters.
613
+ */
614
+ parameters?: Record<string, unknown>;
615
+ }
616
+ /**
617
+ * Span annotation for text highlighting tasks.
618
+ */
619
+ interface SpanAnnotation {
620
+ /**
621
+ * Start character index (inclusive).
622
+ */
623
+ start: number;
624
+ /**
625
+ * End character index (exclusive).
626
+ */
627
+ end: number;
628
+ /**
629
+ * Annotation label or category.
630
+ */
631
+ label: string;
632
+ /**
633
+ * Annotated text content.
634
+ */
635
+ text: string;
636
+ }
637
+ /**
638
+ * Participant response information.
639
+ */
640
+ interface ResponseInfo {
641
+ /**
642
+ * Response type.
643
+ */
644
+ type: "text" | "choice" | "multi-choice" | "slider" | "likert" | "annotation" | "other";
645
+ /**
646
+ * Response value.
647
+ * Type depends on response type:
648
+ * - text: string
649
+ * - choice: string or number
650
+ * - multi-choice: string[] or number[]
651
+ * - slider: number
652
+ * - likert: number
653
+ * - annotation: SpanAnnotation[]
654
+ * - other: unknown
655
+ */
656
+ value: unknown;
657
+ /**
658
+ * For text responses, character count.
659
+ */
660
+ characterCount?: number;
661
+ /**
662
+ * For text responses, word count.
663
+ */
664
+ wordCount?: number;
665
+ }
666
+ /**
667
+ * Data for a single trial.
668
+ */
669
+ interface SlopitTrial {
670
+ /**
671
+ * Unique trial identifier.
672
+ * Generated client-side or from platform.
673
+ */
674
+ trialId: string;
675
+ /**
676
+ * Zero-indexed trial position in session.
677
+ */
678
+ trialIndex: number;
679
+ /**
680
+ * Trial type identifier.
681
+ * Matches jsPsych trial.type or equivalent.
682
+ */
683
+ trialType?: string;
684
+ /**
685
+ * Trial start time.
686
+ * Unix timestamp in milliseconds.
687
+ */
688
+ startTime: number;
689
+ /**
690
+ * Trial end time.
691
+ * Unix timestamp in milliseconds.
692
+ */
693
+ endTime: number;
694
+ /**
695
+ * Response time in milliseconds.
696
+ * Time from trial start to response submission.
697
+ */
698
+ rt?: number;
699
+ /**
700
+ * Stimulus information.
701
+ */
702
+ stimulus?: StimulusInfo;
703
+ /**
704
+ * Participant response.
705
+ */
706
+ response?: ResponseInfo;
707
+ /**
708
+ * Behavioral capture data.
709
+ */
710
+ behavioral?: BehavioralData;
711
+ /**
712
+ * Platform-specific trial data.
713
+ * Passed through without modification.
714
+ */
715
+ platformData?: Record<string, unknown>;
716
+ /**
717
+ * Trial-level flags generated during capture.
718
+ */
719
+ captureFlags?: CaptureFlag[];
720
+ }
721
+ /**
722
+ * Session-level focus event.
723
+ */
724
+ interface SessionFocusEvent {
725
+ /**
726
+ * Unix timestamp in milliseconds.
727
+ */
728
+ timestamp: number;
729
+ /**
730
+ * Event type.
731
+ */
732
+ event: "focus" | "blur" | "visibilitychange";
733
+ /**
734
+ * Visibility state if applicable.
735
+ */
736
+ visibility?: "visible" | "hidden";
737
+ }
738
+ /**
739
+ * Error event.
740
+ */
741
+ interface ErrorEvent {
742
+ /**
743
+ * Unix timestamp in milliseconds.
744
+ */
745
+ timestamp: number;
746
+ /**
747
+ * Error message.
748
+ */
749
+ message: string;
750
+ /**
751
+ * Error stack trace if available.
752
+ */
753
+ stack?: string;
754
+ /**
755
+ * Error source (filename, line, column).
756
+ */
757
+ source?: string;
758
+ }
759
+ /**
760
+ * Custom event from platform or study.
761
+ */
762
+ interface CustomEvent {
763
+ /**
764
+ * Unix timestamp in milliseconds.
765
+ */
766
+ timestamp: number;
767
+ /**
768
+ * Event type identifier.
769
+ */
770
+ type: string;
771
+ /**
772
+ * Event data.
773
+ */
774
+ data?: Record<string, unknown>;
775
+ }
776
+ /**
777
+ * Events not tied to specific trials.
778
+ */
779
+ interface GlobalEvents {
780
+ /**
781
+ * Focus events at session level.
782
+ */
783
+ focus: SessionFocusEvent[];
784
+ /**
785
+ * Error events.
786
+ */
787
+ errors: ErrorEvent[];
788
+ /**
789
+ * Custom events from platform or study.
790
+ */
791
+ custom?: CustomEvent[];
792
+ }
793
+ /**
794
+ * Root container for a participant session.
795
+ *
796
+ * Contains all behavioral data, responses, and metadata
797
+ * collected during a single study session.
798
+ */
799
+ interface SlopitSession {
800
+ /**
801
+ * Schema version for forward compatibility.
802
+ * Current version: "1.0"
803
+ */
804
+ schemaVersion: "1.0";
805
+ /**
806
+ * Unique identifier for this session.
807
+ * Generated client-side using crypto.randomUUID().
808
+ */
809
+ sessionId: string;
810
+ /**
811
+ * Participant identifier from recruitment platform.
812
+ * For Prolific: PROLIFIC_PID
813
+ * For MTurk: workerId
814
+ * May be undefined for anonymous studies.
815
+ */
816
+ participantId?: string;
817
+ /**
818
+ * Study identifier.
819
+ * Matches JATOS study ID or equivalent.
820
+ */
821
+ studyId?: string;
822
+ /**
823
+ * Platform information.
824
+ */
825
+ platform: PlatformInfo;
826
+ /**
827
+ * Client environment information.
828
+ */
829
+ environment: EnvironmentInfo;
830
+ /**
831
+ * Session timing.
832
+ */
833
+ timing: SessionTiming;
834
+ /**
835
+ * Array of trial data.
836
+ */
837
+ trials: SlopitTrial[];
838
+ /**
839
+ * Events not tied to specific trials.
840
+ */
841
+ globalEvents: GlobalEvents;
842
+ /**
843
+ * Session-level metadata.
844
+ * Platform-specific or study-specific data.
845
+ */
846
+ metadata?: Record<string, unknown>;
847
+ }
848
+
849
+ export type { AnalysisFlag, BehavioralData, BehavioralMetrics, CaptureFlag, ClipboardCopyEvent, CustomEvent, EnvironmentInfo, ErrorEvent, FocusEvent, FocusMetrics, GlobalEvents, InputDurationEvent, InputDurationMetrics, KeystrokeEvent, KeystrokeMetrics, ModifierState, MouseEvent, MouseMetrics, PasteEvent, PlatformInfo, ResponseInfo, ScrollEvent, ScrollMetrics, SessionFocusEvent, SessionTiming, Severity, SlopitSession, SlopitTrial, SpanAnnotation, StimulusInfo, TimingMetrics };