@slopit/adapter-jatos 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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aaron Steven White
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,303 @@
1
+ import { RecorderConfig, BaseRecorder, TrialConfig, CompletedTrial } from '@slopit/adapter-shared';
2
+ import { PlatformInfo, ResponseInfo, SlopitSession } from '@slopit/core';
3
+
4
+ /**
5
+ * Types for the JATOS adapter.
6
+ *
7
+ * @module
8
+ */
9
+
10
+ /**
11
+ * Configuration for JatosRecorder.
12
+ *
13
+ * Extends the base RecorderConfig with JATOS-specific options.
14
+ */
15
+ interface JatosRecorderConfig extends RecorderConfig {
16
+ /**
17
+ * Key used for slopit data in JATOS result data.
18
+ *
19
+ * @default "slopit"
20
+ */
21
+ resultKey?: string;
22
+ }
23
+ /**
24
+ * JATOS global module interface.
25
+ *
26
+ * This interface describes the jatos.js client library methods
27
+ * used by the adapter.
28
+ */
29
+ interface JatosModule {
30
+ /**
31
+ * Called when JATOS component is ready.
32
+ *
33
+ * @param callback - Function to execute when ready
34
+ */
35
+ onLoad(callback: () => void): void;
36
+ /**
37
+ * Submit result data for the component.
38
+ *
39
+ * @param data - Data object to submit
40
+ * @returns Promise that resolves when submission is complete
41
+ */
42
+ submitResultData(data: unknown): Promise<void>;
43
+ /**
44
+ * Append data to the result.
45
+ *
46
+ * @param data - JSON string to append
47
+ * @returns Promise that resolves when append is complete
48
+ */
49
+ appendResultData(data: string): Promise<void>;
50
+ /**
51
+ * End the study.
52
+ *
53
+ * @param successful - Whether the study ended successfully
54
+ * @param message - Optional message
55
+ */
56
+ endStudy(successful?: boolean, message?: string): void;
57
+ /**
58
+ * Start the next component in the study.
59
+ *
60
+ * @param message - Optional message to pass to the next component
61
+ */
62
+ startNextComponent(message?: string): void;
63
+ /**
64
+ * Shared data across components in the study.
65
+ */
66
+ studySessionData: Record<string, unknown>;
67
+ /**
68
+ * Result data for the current component.
69
+ */
70
+ componentResultData: unknown;
71
+ /**
72
+ * Result data for the entire study.
73
+ */
74
+ studyResultData: unknown;
75
+ /**
76
+ * Worker (participant) ID.
77
+ */
78
+ workerId: string;
79
+ /**
80
+ * Study ID.
81
+ */
82
+ studyId: string;
83
+ /**
84
+ * Batch ID.
85
+ */
86
+ batchId: string;
87
+ /**
88
+ * Current component ID.
89
+ */
90
+ componentId: string;
91
+ }
92
+ declare global {
93
+ interface Window {
94
+ jatos?: JatosModule;
95
+ }
96
+ }
97
+
98
+ /**
99
+ * JATOS SlopitRecorder implementation.
100
+ *
101
+ * @module
102
+ */
103
+
104
+ /**
105
+ * SlopitRecorder for JATOS experiments.
106
+ *
107
+ * Provides integration with JATOS for behavioral data capture and
108
+ * submission. Automatically detects JATOS worker and study IDs when available.
109
+ *
110
+ * @example
111
+ * ```typescript
112
+ * jatos.onLoad(function() {
113
+ * const recorder = new SlopitRecorder();
114
+ *
115
+ * // Run trials
116
+ * recorder.beginTrial("trial_1", textarea);
117
+ * // ... participant completes trial ...
118
+ * recorder.finishTrial(response);
119
+ *
120
+ * // Submit and end
121
+ * await recorder.submitToJatos();
122
+ * jatos.endStudy();
123
+ * });
124
+ * ```
125
+ */
126
+ declare class SlopitRecorder extends BaseRecorder {
127
+ private resultKey;
128
+ private discoveryCleanup;
129
+ /**
130
+ * Creates a new SlopitRecorder for JATOS.
131
+ *
132
+ * @param config - Recorder configuration
133
+ */
134
+ constructor(config?: JatosRecorderConfig);
135
+ /**
136
+ * Gets platform-specific information.
137
+ *
138
+ * @returns Platform information for session export
139
+ */
140
+ protected getPlatformInfo(): PlatformInfo;
141
+ /**
142
+ * Detects JATOS version.
143
+ *
144
+ * JATOS does not expose version information client-side, so this returns
145
+ * "unknown" unless overridden.
146
+ *
147
+ * @returns JATOS version string or "unknown"
148
+ */
149
+ private detectJatosVersion;
150
+ /**
151
+ * Gets the JATOS module from the window object.
152
+ *
153
+ * @returns JatosModule or undefined if not available
154
+ */
155
+ private getJatos;
156
+ /**
157
+ * Builds a TrialConfig from the provided parameters.
158
+ *
159
+ * @param trialId - Trial identifier
160
+ * @param element - Optional target element
161
+ * @param trialConfig - Optional additional configuration
162
+ * @returns TrialConfig compatible with base class
163
+ */
164
+ private buildTrialConfig;
165
+ /**
166
+ * Start capturing behavioral data for a trial.
167
+ *
168
+ * @param trialId - Unique identifier for the trial
169
+ * @param element - Target DOM element to monitor (optional)
170
+ * @param trialConfig - Additional trial configuration
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * const textarea = document.querySelector("textarea");
175
+ * recorder.beginTrial("trial_1", textarea, {
176
+ * trialType: "free-response",
177
+ * stimulus: { type: "text", content: "Describe your experience." },
178
+ * });
179
+ * ```
180
+ */
181
+ beginTrial(trialId: string, element?: HTMLElement, trialConfig?: Partial<TrialConfig>): void;
182
+ /**
183
+ * Start a trial with automatic element discovery.
184
+ *
185
+ * Uses a MutationObserver to watch for the target element to appear in
186
+ * the DOM. Useful when the element is created dynamically.
187
+ *
188
+ * @param trialId - Unique identifier for the trial
189
+ * @param selector - CSS selector for target element
190
+ * @param container - Container to search within (default: document)
191
+ *
192
+ * @example
193
+ * ```typescript
194
+ * recorder.startTrialWithSelector(
195
+ * "trial_1",
196
+ * "textarea.response-input",
197
+ * document.getElementById("trial-container")
198
+ * );
199
+ * ```
200
+ */
201
+ startTrialWithSelector(trialId: string, selector: string, container?: HTMLElement | Document): void;
202
+ /**
203
+ * Internal method to call the base class endTrial.
204
+ * This avoids TypeScript override signature issues.
205
+ */
206
+ private endTrialBase;
207
+ /**
208
+ * End the current trial.
209
+ *
210
+ * @param response - The participant's response (string or ResponseInfo)
211
+ * @returns The completed trial data
212
+ *
213
+ * @example
214
+ * ```typescript
215
+ * // With a text string
216
+ * const trial = recorder.finishTrial("The participant's response.");
217
+ *
218
+ * // With a ResponseInfo object
219
+ * const trial = recorder.finishTrial({
220
+ * type: "text",
221
+ * value: "Response text",
222
+ * characterCount: 13,
223
+ * wordCount: 2,
224
+ * });
225
+ * ```
226
+ */
227
+ finishTrial(response?: string | ResponseInfo): CompletedTrial;
228
+ /**
229
+ * Submit session data to JATOS.
230
+ *
231
+ * Call this at the end of the component or study to submit all captured
232
+ * behavioral data to JATOS.
233
+ *
234
+ * @returns Promise that resolves when data is submitted
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * // After all trials are complete
239
+ * await recorder.submitToJatos();
240
+ * jatos.endStudy();
241
+ * ```
242
+ */
243
+ submitToJatos(): Promise<void>;
244
+ /**
245
+ * Append session data to JATOS result.
246
+ *
247
+ * Use this for incremental data submission during long experiments.
248
+ * Unlike submitToJatos(), this appends to existing data rather than
249
+ * replacing it.
250
+ *
251
+ * @returns Promise that resolves when data is appended
252
+ *
253
+ * @example
254
+ * ```typescript
255
+ * // After each block of trials
256
+ * await recorder.appendToJatos();
257
+ * ```
258
+ */
259
+ appendToJatos(): Promise<void>;
260
+ /**
261
+ * Store session in JATOS study session data.
262
+ *
263
+ * Use this to share session data across components in a multi-component
264
+ * study. The data is stored in jatos.studySessionData and persists across
265
+ * component transitions.
266
+ *
267
+ * @example
268
+ * ```typescript
269
+ * // In component 1
270
+ * recorder.storeInStudySession();
271
+ * jatos.startNextComponent();
272
+ *
273
+ * // In component 2, retrieve with loadFromStudySession()
274
+ * ```
275
+ */
276
+ storeInStudySession(): void;
277
+ /**
278
+ * Load previous session data from JATOS study session.
279
+ *
280
+ * Use this in multi-component studies to retrieve session data stored
281
+ * by a previous component using storeInStudySession().
282
+ *
283
+ * @returns Previous session data or undefined if not found
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * // In a later component
288
+ * const previousSession = recorder.loadFromStudySession();
289
+ * if (previousSession !== undefined) {
290
+ * console.log(`Previous session had ${previousSession.trials.length} trials`);
291
+ * }
292
+ * ```
293
+ */
294
+ loadFromStudySession(): SlopitSession | undefined;
295
+ /**
296
+ * Get the result key used for JATOS data storage.
297
+ *
298
+ * @returns The result key string
299
+ */
300
+ getResultKey(): string;
301
+ }
302
+
303
+ export { type JatosModule, SlopitRecorder as JatosRecorder, type JatosRecorderConfig, SlopitRecorder };
package/dist/index.js ADDED
@@ -0,0 +1,305 @@
1
+ import { BaseRecorder, discoverElement } from '@slopit/adapter-shared';
2
+
3
+ // src/recorder.ts
4
+ var SlopitRecorder = class extends BaseRecorder {
5
+ resultKey;
6
+ discoveryCleanup = null;
7
+ /**
8
+ * Creates a new SlopitRecorder for JATOS.
9
+ *
10
+ * @param config - Recorder configuration
11
+ */
12
+ constructor(config) {
13
+ const jatos = typeof window !== "undefined" ? window.jatos : void 0;
14
+ const enhancedConfig = {};
15
+ const participantId = config?.participantId ?? jatos?.workerId;
16
+ if (participantId !== void 0) {
17
+ enhancedConfig.participantId = participantId;
18
+ }
19
+ const studyId = config?.studyId ?? jatos?.studyId;
20
+ if (studyId !== void 0) {
21
+ enhancedConfig.studyId = studyId;
22
+ }
23
+ if (config?.metadata !== void 0) {
24
+ enhancedConfig.metadata = config.metadata;
25
+ }
26
+ if (config?.behavioral !== void 0) {
27
+ enhancedConfig.behavioral = config.behavioral;
28
+ }
29
+ super(enhancedConfig);
30
+ this.resultKey = config?.resultKey ?? "slopit";
31
+ }
32
+ /**
33
+ * Gets platform-specific information.
34
+ *
35
+ * @returns Platform information for session export
36
+ */
37
+ getPlatformInfo() {
38
+ return {
39
+ name: "JATOS",
40
+ version: this.detectJatosVersion(),
41
+ adapterVersion: "0.1.0"
42
+ };
43
+ }
44
+ /**
45
+ * Detects JATOS version.
46
+ *
47
+ * JATOS does not expose version information client-side, so this returns
48
+ * "unknown" unless overridden.
49
+ *
50
+ * @returns JATOS version string or "unknown"
51
+ */
52
+ detectJatosVersion() {
53
+ return "unknown";
54
+ }
55
+ /**
56
+ * Gets the JATOS module from the window object.
57
+ *
58
+ * @returns JatosModule or undefined if not available
59
+ */
60
+ getJatos() {
61
+ return typeof window !== "undefined" ? window.jatos : void 0;
62
+ }
63
+ /**
64
+ * Builds a TrialConfig from the provided parameters.
65
+ *
66
+ * @param trialId - Trial identifier
67
+ * @param element - Optional target element
68
+ * @param trialConfig - Optional additional configuration
69
+ * @returns TrialConfig compatible with base class
70
+ */
71
+ buildTrialConfig(trialId, element, trialConfig) {
72
+ const config = {
73
+ trialId
74
+ };
75
+ if (element !== void 0) {
76
+ config.targetElement = element;
77
+ }
78
+ if (trialConfig?.trialIndex !== void 0) {
79
+ config.trialIndex = trialConfig.trialIndex;
80
+ }
81
+ if (trialConfig?.trialType !== void 0) {
82
+ config.trialType = trialConfig.trialType;
83
+ }
84
+ if (trialConfig?.stimulus !== void 0) {
85
+ config.stimulus = trialConfig.stimulus;
86
+ }
87
+ if (trialConfig?.targetSelector !== void 0) {
88
+ config.targetSelector = trialConfig.targetSelector;
89
+ }
90
+ return config;
91
+ }
92
+ /**
93
+ * Start capturing behavioral data for a trial.
94
+ *
95
+ * @param trialId - Unique identifier for the trial
96
+ * @param element - Target DOM element to monitor (optional)
97
+ * @param trialConfig - Additional trial configuration
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const textarea = document.querySelector("textarea");
102
+ * recorder.beginTrial("trial_1", textarea, {
103
+ * trialType: "free-response",
104
+ * stimulus: { type: "text", content: "Describe your experience." },
105
+ * });
106
+ * ```
107
+ */
108
+ beginTrial(trialId, element, trialConfig) {
109
+ const config = this.buildTrialConfig(trialId, element, trialConfig);
110
+ super.startTrial(config);
111
+ }
112
+ /**
113
+ * Start a trial with automatic element discovery.
114
+ *
115
+ * Uses a MutationObserver to watch for the target element to appear in
116
+ * the DOM. Useful when the element is created dynamically.
117
+ *
118
+ * @param trialId - Unique identifier for the trial
119
+ * @param selector - CSS selector for target element
120
+ * @param container - Container to search within (default: document)
121
+ *
122
+ * @example
123
+ * ```typescript
124
+ * recorder.startTrialWithSelector(
125
+ * "trial_1",
126
+ * "textarea.response-input",
127
+ * document.getElementById("trial-container")
128
+ * );
129
+ * ```
130
+ */
131
+ startTrialWithSelector(trialId, selector, container = document) {
132
+ const element = container.querySelector(selector);
133
+ if (element !== null) {
134
+ this.beginTrial(trialId, element);
135
+ } else {
136
+ const config = { trialId };
137
+ super.startTrial(config);
138
+ this.discoveryCleanup = discoverElement({
139
+ selector,
140
+ container,
141
+ timeout: 1e4,
142
+ onFound: (el) => {
143
+ if (this.capture !== null) {
144
+ return;
145
+ }
146
+ if (this.currentTrial !== null) {
147
+ this.attachCapture(el, this.currentTrial.startTime);
148
+ }
149
+ }
150
+ });
151
+ }
152
+ }
153
+ /**
154
+ * Internal method to call the base class endTrial.
155
+ * This avoids TypeScript override signature issues.
156
+ */
157
+ endTrialBase(response, platformData) {
158
+ return BaseRecorder.prototype.endTrial.call(this, response, platformData);
159
+ }
160
+ /**
161
+ * End the current trial.
162
+ *
163
+ * @param response - The participant's response (string or ResponseInfo)
164
+ * @returns The completed trial data
165
+ *
166
+ * @example
167
+ * ```typescript
168
+ * // With a text string
169
+ * const trial = recorder.finishTrial("The participant's response.");
170
+ *
171
+ * // With a ResponseInfo object
172
+ * const trial = recorder.finishTrial({
173
+ * type: "text",
174
+ * value: "Response text",
175
+ * characterCount: 13,
176
+ * wordCount: 2,
177
+ * });
178
+ * ```
179
+ */
180
+ finishTrial(response) {
181
+ if (this.discoveryCleanup !== null) {
182
+ this.discoveryCleanup();
183
+ this.discoveryCleanup = null;
184
+ }
185
+ const responseInfo = typeof response === "string" ? {
186
+ type: "text",
187
+ value: response,
188
+ characterCount: response.length,
189
+ wordCount: response.trim().split(/\s+/).filter((w) => w.length > 0).length
190
+ } : response;
191
+ return this.endTrialBase(responseInfo);
192
+ }
193
+ /**
194
+ * Submit session data to JATOS.
195
+ *
196
+ * Call this at the end of the component or study to submit all captured
197
+ * behavioral data to JATOS.
198
+ *
199
+ * @returns Promise that resolves when data is submitted
200
+ *
201
+ * @example
202
+ * ```typescript
203
+ * // After all trials are complete
204
+ * await recorder.submitToJatos();
205
+ * jatos.endStudy();
206
+ * ```
207
+ */
208
+ async submitToJatos() {
209
+ const jatos = this.getJatos();
210
+ if (jatos === void 0) {
211
+ console.warn("[slopit] JATOS not available, cannot submit data");
212
+ return;
213
+ }
214
+ const session = this.exportSession();
215
+ await jatos.submitResultData({
216
+ [this.resultKey]: session
217
+ });
218
+ }
219
+ /**
220
+ * Append session data to JATOS result.
221
+ *
222
+ * Use this for incremental data submission during long experiments.
223
+ * Unlike submitToJatos(), this appends to existing data rather than
224
+ * replacing it.
225
+ *
226
+ * @returns Promise that resolves when data is appended
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * // After each block of trials
231
+ * await recorder.appendToJatos();
232
+ * ```
233
+ */
234
+ async appendToJatos() {
235
+ const jatos = this.getJatos();
236
+ if (jatos === void 0) {
237
+ console.warn("[slopit] JATOS not available, cannot append data");
238
+ return;
239
+ }
240
+ const session = this.exportSession();
241
+ await jatos.appendResultData(
242
+ JSON.stringify({
243
+ [this.resultKey]: session
244
+ })
245
+ );
246
+ }
247
+ /**
248
+ * Store session in JATOS study session data.
249
+ *
250
+ * Use this to share session data across components in a multi-component
251
+ * study. The data is stored in jatos.studySessionData and persists across
252
+ * component transitions.
253
+ *
254
+ * @example
255
+ * ```typescript
256
+ * // In component 1
257
+ * recorder.storeInStudySession();
258
+ * jatos.startNextComponent();
259
+ *
260
+ * // In component 2, retrieve with loadFromStudySession()
261
+ * ```
262
+ */
263
+ storeInStudySession() {
264
+ const jatos = this.getJatos();
265
+ if (jatos !== void 0) {
266
+ jatos.studySessionData[this.resultKey] = this.exportSession();
267
+ }
268
+ }
269
+ /**
270
+ * Load previous session data from JATOS study session.
271
+ *
272
+ * Use this in multi-component studies to retrieve session data stored
273
+ * by a previous component using storeInStudySession().
274
+ *
275
+ * @returns Previous session data or undefined if not found
276
+ *
277
+ * @example
278
+ * ```typescript
279
+ * // In a later component
280
+ * const previousSession = recorder.loadFromStudySession();
281
+ * if (previousSession !== undefined) {
282
+ * console.log(`Previous session had ${previousSession.trials.length} trials`);
283
+ * }
284
+ * ```
285
+ */
286
+ loadFromStudySession() {
287
+ const jatos = this.getJatos();
288
+ if (jatos !== void 0 && jatos.studySessionData[this.resultKey] !== void 0) {
289
+ return jatos.studySessionData[this.resultKey];
290
+ }
291
+ return void 0;
292
+ }
293
+ /**
294
+ * Get the result key used for JATOS data storage.
295
+ *
296
+ * @returns The result key string
297
+ */
298
+ getResultKey() {
299
+ return this.resultKey;
300
+ }
301
+ };
302
+
303
+ export { SlopitRecorder as JatosRecorder, SlopitRecorder };
304
+ //# sourceMappingURL=index.js.map
305
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/recorder.ts"],"names":[],"mappings":";;;AAuCO,IAAM,cAAA,GAAN,cAA6B,YAAA,CAAa;AAAA,EACvC,SAAA;AAAA,EACA,gBAAA,GAAwC,IAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOhD,YAAY,MAAA,EAA8B;AAExC,IAAA,MAA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AAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBA,mBAAA,GAA4B;AAC1B,IAAA,MAAM,KAAA,GAAQ,KAAK,QAAA,EAAS;AAC5B,IAAA,IAAI,UAAU,MAAA,EAAW;AACvB,MAAA,KAAA,CAAM,gBAAA,CAAiB,IAAA,CAAK,SAAS,CAAA,GAAI,KAAK,aAAA,EAAc;AAAA,IAC9D;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,oBAAA,GAAkD;AAChD,IAAA,MAAM,KAAA,GAAQ,KAAK,QAAA,EAAS;AAC5B,IAAA,IAAI,UAAU,MAAA,IAAa,KAAA,CAAM,iBAAiB,IAAA,CAAK,SAAS,MAAM,MAAA,EAAW;AAC/E,MAAA,OAAO,KAAA,CAAM,gBAAA,CAAiB,IAAA,CAAK,SAAS,CAAA;AAAA,IAC9C;AACA,IAAA,OAAO,MAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,YAAA,GAAuB;AACrB,IAAA,OAAO,IAAA,CAAK,SAAA;AAAA,EACd;AACF","file":"index.js","sourcesContent":["/**\n * JATOS SlopitRecorder implementation.\n *\n * @module\n */\n\nimport {\n BaseRecorder,\n type CompletedTrial,\n type RecorderConfig,\n type TrialConfig,\n discoverElement,\n} from \"@slopit/adapter-shared\";\nimport type { PlatformInfo, ResponseInfo, SlopitSession } from \"@slopit/core\";\n\nimport type { JatosModule, JatosRecorderConfig } from \"./types.js\";\n\n/**\n * SlopitRecorder for JATOS experiments.\n *\n * Provides integration with JATOS for behavioral data capture and\n * submission. Automatically detects JATOS worker and study IDs when available.\n *\n * @example\n * ```typescript\n * jatos.onLoad(function() {\n * const recorder = new SlopitRecorder();\n *\n * // Run trials\n * recorder.beginTrial(\"trial_1\", textarea);\n * // ... participant completes trial ...\n * recorder.finishTrial(response);\n *\n * // Submit and end\n * await recorder.submitToJatos();\n * jatos.endStudy();\n * });\n * ```\n */\nexport class SlopitRecorder extends BaseRecorder {\n private resultKey: string;\n private discoveryCleanup: (() => void) | null = null;\n\n /**\n * Creates a new SlopitRecorder for JATOS.\n *\n * @param config - Recorder configuration\n */\n constructor(config?: JatosRecorderConfig) {\n // Auto-detect participantId and studyId from JATOS if available\n const jatos = typeof window !== \"undefined\" ? window.jatos : undefined;\n\n // Build enhanced config explicitly to satisfy exactOptionalPropertyTypes\n const enhancedConfig: RecorderConfig = {};\n\n // Use config value if provided, otherwise fall back to JATOS values\n const participantId = config?.participantId ?? jatos?.workerId;\n if (participantId !== undefined) {\n enhancedConfig.participantId = participantId;\n }\n\n const studyId = config?.studyId ?? jatos?.studyId;\n if (studyId !== undefined) {\n enhancedConfig.studyId = studyId;\n }\n\n if (config?.metadata !== undefined) {\n enhancedConfig.metadata = config.metadata;\n }\n\n if (config?.behavioral !== undefined) {\n enhancedConfig.behavioral = config.behavioral;\n }\n\n super(enhancedConfig);\n this.resultKey = config?.resultKey ?? \"slopit\";\n }\n\n /**\n * Gets platform-specific information.\n *\n * @returns Platform information for session export\n */\n protected override getPlatformInfo(): PlatformInfo {\n return {\n name: \"JATOS\",\n version: this.detectJatosVersion(),\n adapterVersion: \"0.1.0\",\n };\n }\n\n /**\n * Detects JATOS version.\n *\n * JATOS does not expose version information client-side, so this returns\n * \"unknown\" unless overridden.\n *\n * @returns JATOS version string or \"unknown\"\n */\n private detectJatosVersion(): string {\n // JATOS doesn't expose version client-side\n return \"unknown\";\n }\n\n /**\n * Gets the JATOS module from the window object.\n *\n * @returns JatosModule or undefined if not available\n */\n private getJatos(): JatosModule | undefined {\n return typeof window !== \"undefined\" ? window.jatos : undefined;\n }\n\n /**\n * Builds a TrialConfig from the provided parameters.\n *\n * @param trialId - Trial identifier\n * @param element - Optional target element\n * @param trialConfig - Optional additional configuration\n * @returns TrialConfig compatible with base class\n */\n private buildTrialConfig(\n trialId: string,\n element?: HTMLElement,\n trialConfig?: Partial<TrialConfig>,\n ): TrialConfig {\n const config: TrialConfig = {\n trialId,\n };\n\n if (element !== undefined) {\n config.targetElement = element;\n }\n\n if (trialConfig?.trialIndex !== undefined) {\n config.trialIndex = trialConfig.trialIndex;\n }\n\n if (trialConfig?.trialType !== undefined) {\n config.trialType = trialConfig.trialType;\n }\n\n if (trialConfig?.stimulus !== undefined) {\n config.stimulus = trialConfig.stimulus;\n }\n\n if (trialConfig?.targetSelector !== undefined) {\n config.targetSelector = trialConfig.targetSelector;\n }\n\n return config;\n }\n\n /**\n * Start capturing behavioral data for a trial.\n *\n * @param trialId - Unique identifier for the trial\n * @param element - Target DOM element to monitor (optional)\n * @param trialConfig - Additional trial configuration\n *\n * @example\n * ```typescript\n * const textarea = document.querySelector(\"textarea\");\n * recorder.beginTrial(\"trial_1\", textarea, {\n * trialType: \"free-response\",\n * stimulus: { type: \"text\", content: \"Describe your experience.\" },\n * });\n * ```\n */\n beginTrial(trialId: string, element?: HTMLElement, trialConfig?: Partial<TrialConfig>): void {\n const config = this.buildTrialConfig(trialId, element, trialConfig);\n super.startTrial(config);\n }\n\n /**\n * Start a trial with automatic element discovery.\n *\n * Uses a MutationObserver to watch for the target element to appear in\n * the DOM. Useful when the element is created dynamically.\n *\n * @param trialId - Unique identifier for the trial\n * @param selector - CSS selector for target element\n * @param container - Container to search within (default: document)\n *\n * @example\n * ```typescript\n * recorder.startTrialWithSelector(\n * \"trial_1\",\n * \"textarea.response-input\",\n * document.getElementById(\"trial-container\")\n * );\n * ```\n */\n startTrialWithSelector(\n trialId: string,\n selector: string,\n container: HTMLElement | Document = document,\n ): void {\n const element = container.querySelector(selector) as HTMLElement | null;\n\n if (element !== null) {\n this.beginTrial(trialId, element);\n } else {\n // Start trial without element, then attach when found\n const config: TrialConfig = { trialId };\n super.startTrial(config);\n\n this.discoveryCleanup = discoverElement({\n selector,\n container: container as HTMLElement,\n timeout: 10000,\n onFound: (el) => {\n if (this.capture !== null) {\n // If capture was already created by a previous attach, skip\n return;\n }\n if (this.currentTrial !== null) {\n this.attachCapture(el, this.currentTrial.startTime);\n }\n },\n });\n }\n }\n\n /**\n * Internal method to call the base class endTrial.\n * This avoids TypeScript override signature issues.\n */\n private endTrialBase(\n response?: ResponseInfo,\n platformData?: Record<string, unknown>,\n ): CompletedTrial {\n return BaseRecorder.prototype.endTrial.call(this, response, platformData) as CompletedTrial;\n }\n\n /**\n * End the current trial.\n *\n * @param response - The participant's response (string or ResponseInfo)\n * @returns The completed trial data\n *\n * @example\n * ```typescript\n * // With a text string\n * const trial = recorder.finishTrial(\"The participant's response.\");\n *\n * // With a ResponseInfo object\n * const trial = recorder.finishTrial({\n * type: \"text\",\n * value: \"Response text\",\n * characterCount: 13,\n * wordCount: 2,\n * });\n * ```\n */\n finishTrial(response?: string | ResponseInfo): CompletedTrial {\n // Clean up any pending discovery\n if (this.discoveryCleanup !== null) {\n this.discoveryCleanup();\n this.discoveryCleanup = null;\n }\n\n // Normalize response: convert string to ResponseInfo\n const responseInfo: ResponseInfo | undefined =\n typeof response === \"string\"\n ? {\n type: \"text\",\n value: response,\n characterCount: response.length,\n wordCount: response\n .trim()\n .split(/\\s+/)\n .filter((w) => w.length > 0).length,\n }\n : response;\n\n return this.endTrialBase(responseInfo);\n }\n\n /**\n * Submit session data to JATOS.\n *\n * Call this at the end of the component or study to submit all captured\n * behavioral data to JATOS.\n *\n * @returns Promise that resolves when data is submitted\n *\n * @example\n * ```typescript\n * // After all trials are complete\n * await recorder.submitToJatos();\n * jatos.endStudy();\n * ```\n */\n async submitToJatos(): Promise<void> {\n const jatos = this.getJatos();\n if (jatos === undefined) {\n console.warn(\"[slopit] JATOS not available, cannot submit data\");\n return;\n }\n\n const session = this.exportSession();\n await jatos.submitResultData({\n [this.resultKey]: session,\n });\n }\n\n /**\n * Append session data to JATOS result.\n *\n * Use this for incremental data submission during long experiments.\n * Unlike submitToJatos(), this appends to existing data rather than\n * replacing it.\n *\n * @returns Promise that resolves when data is appended\n *\n * @example\n * ```typescript\n * // After each block of trials\n * await recorder.appendToJatos();\n * ```\n */\n async appendToJatos(): Promise<void> {\n const jatos = this.getJatos();\n if (jatos === undefined) {\n console.warn(\"[slopit] JATOS not available, cannot append data\");\n return;\n }\n\n const session = this.exportSession();\n await jatos.appendResultData(\n JSON.stringify({\n [this.resultKey]: session,\n }),\n );\n }\n\n /**\n * Store session in JATOS study session data.\n *\n * Use this to share session data across components in a multi-component\n * study. The data is stored in jatos.studySessionData and persists across\n * component transitions.\n *\n * @example\n * ```typescript\n * // In component 1\n * recorder.storeInStudySession();\n * jatos.startNextComponent();\n *\n * // In component 2, retrieve with loadFromStudySession()\n * ```\n */\n storeInStudySession(): void {\n const jatos = this.getJatos();\n if (jatos !== undefined) {\n jatos.studySessionData[this.resultKey] = this.exportSession();\n }\n }\n\n /**\n * Load previous session data from JATOS study session.\n *\n * Use this in multi-component studies to retrieve session data stored\n * by a previous component using storeInStudySession().\n *\n * @returns Previous session data or undefined if not found\n *\n * @example\n * ```typescript\n * // In a later component\n * const previousSession = recorder.loadFromStudySession();\n * if (previousSession !== undefined) {\n * console.log(`Previous session had ${previousSession.trials.length} trials`);\n * }\n * ```\n */\n loadFromStudySession(): SlopitSession | undefined {\n const jatos = this.getJatos();\n if (jatos !== undefined && jatos.studySessionData[this.resultKey] !== undefined) {\n return jatos.studySessionData[this.resultKey] as SlopitSession;\n }\n return undefined;\n }\n\n /**\n * Get the result key used for JATOS data storage.\n *\n * @returns The result key string\n */\n getResultKey(): string {\n return this.resultKey;\n }\n}\n\n// Also export as JatosRecorder for clarity\nexport { SlopitRecorder as JatosRecorder };\n"]}
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@slopit/adapter-jatos",
3
+ "version": "0.1.0",
4
+ "description": "JATOS adapter for slopit behavioral capture",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": {
8
+ "name": "Aaron Steven White",
9
+ "email": "aaron.white@rochester.edu",
10
+ "url": "https://aaronstevenwhite.io"
11
+ },
12
+ "homepage": "https://slopit.github.io",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/slopit/slopit.git",
16
+ "directory": "packages/adapter-jatos"
17
+ },
18
+ "bugs": {
19
+ "url": "https://github.com/slopit/slopit/issues"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public"
23
+ },
24
+ "keywords": [
25
+ "jatos",
26
+ "behavioral-research",
27
+ "experiment",
28
+ "psychology",
29
+ "ai-detection",
30
+ "crowdsourcing"
31
+ ],
32
+ "exports": {
33
+ ".": {
34
+ "import": "./dist/index.js",
35
+ "types": "./dist/index.d.ts"
36
+ }
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "dependencies": {
42
+ "@slopit/adapter-shared": "0.1.0",
43
+ "@slopit/behavioral": "0.1.0",
44
+ "@slopit/core": "0.1.0"
45
+ },
46
+ "devDependencies": {
47
+ "jsdom": "^25.0.1",
48
+ "tsup": "^8.3.5",
49
+ "typescript": "^5.7.2",
50
+ "vitest": "^2.1.8"
51
+ },
52
+ "scripts": {
53
+ "build": "tsup",
54
+ "typecheck": "tsc --noEmit",
55
+ "test": "vitest run",
56
+ "test:watch": "vitest",
57
+ "clean": "rm -rf dist"
58
+ }
59
+ }