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 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/**
 * Interaction Tracker - Captures user interactions for behavioral analysis
 *
 * This module tracks user interactions with the annotation interface including:
 * - Clicks on annotation elements
 * - Focus changes between elements
 * - Scroll depth
 * - Keyboard shortcuts
 * - Navigation events
 * - AI assistance usage
 * - Annotation changes
 *
 * Events are batched and sent periodically to minimize network overhead.
 * Uses sendBeacon API for reliable delivery on page unload.
 */
class InteractionTracker {
    constructor() {
        this.events = [];
        this.focusStartTime = {};
        this.focusTime = {};
        this.scrollDepthMax = 0;
        this.currentInstanceId = null;
        this.previousInstanceId = null;
        this.flushInterval = 5000; // Flush every 5 seconds
        this.lastFlush = Date.now();
        this.isInitialized = false;
        this.debugMode = false;

        // Don't auto-init - wait for explicit init call or DOMContentLoaded
        if (document.readyState === 'loading') {
            document.addEventListener('DOMContentLoaded', () => this.init());
        } else {
            this.init();
        }
    }

    init() {
        if (this.isInitialized) return;
        this.isInitialized = true;

        // Track clicks on annotation elements
        document.addEventListener('click', (e) => this.trackClick(e), true);

        // Track focus changes
        document.addEventListener('focusin', (e) => this.trackFocusIn(e), true);
        document.addEventListener('focusout', (e) => this.trackFocusOut(e), true);

        // Track scroll depth
        window.addEventListener('scroll', () => this.trackScroll(), { passive: true });

        // Track keyboard shortcuts
        document.addEventListener('keydown', (e) => this.trackKeypress(e), true);

        // Flush on page unload
        window.addEventListener('beforeunload', () => this.flush(true));
        window.addEventListener('pagehide', () => this.flush(true));

        // Periodic flush
        this.flushTimer = setInterval(() => this.flush(false), this.flushInterval);

        if (this.debugMode) {
            console.log('[InteractionTracker] Initialized');
        }
    }

    /**
     * Set the current instance ID and notify about navigation
     * @param {string} instanceId - The new instance ID
     */
    setInstanceId(instanceId) {
        if (this.debugMode) {
            console.log(`[InteractionTracker] setInstanceId: ${instanceId}`);
        }

        // Flush events for previous instance
        if (this.currentInstanceId && this.currentInstanceId !== instanceId) {
            this.flush(true);
        }

        this.previousInstanceId = this.currentInstanceId;
        this.currentInstanceId = instanceId;

        // Reset scroll depth for new instance
        this.scrollDepthMax = 0;

        this.addEvent('navigation', 'instance_load', {
            instance_id: instanceId,
            from_instance: this.previousInstanceId
        });
    }

    /**
     * Track click events
     * @param {Event} e - Click event
     */
    trackClick(e) {
        const target = this.getTargetIdentifier(e.target);
        if (target) {
            this.addEvent('click', target, {
                x: e.clientX,
                y: e.clientY,
            });
        }
    }

    /**
     * Track focus entering an element
     * @param {Event} e - Focus event
     */
    trackFocusIn(e) {
        const target = this.getTargetIdentifier(e.target);
        if (target) {
            this.focusStartTime[target] = Date.now();
            this.addEvent('focus_in', target);
        }
    }

    /**
     * Track focus leaving an element
     * @param {Event} e - Focus event
     */
    trackFocusOut(e) {
        const target = this.getTargetIdentifier(e.target);
        if (target && this.focusStartTime[target]) {
            const duration = Date.now() - this.focusStartTime[target];
            this.focusTime[target] = (this.focusTime[target] || 0) + duration;
            delete this.focusStartTime[target];
            this.addEvent('focus_out', target, { duration_ms: duration });
        }
    }

    /**
     * Track scroll depth
     */
    trackScroll() {
        const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
        const scrollHeight = document.documentElement.scrollHeight - window.innerHeight;
        const scrollPercent = scrollHeight > 0 ? (scrollTop / scrollHeight) * 100 : 0;
        this.scrollDepthMax = Math.max(this.scrollDepthMax, scrollPercent);
    }

    /**
     * Track keyboard shortcuts
     * @param {Event} e - Keydown event
     */
    trackKeypress(e) {
        // Track annotation-related keypresses (number keys for keybindings)
        if (e.key >= '0' && e.key <= '9') {
            this.addEvent('keypress', `key:${e.key}`, {
                ctrl: e.ctrlKey,
                alt: e.altKey,
                shift: e.shiftKey,
            });
        }

        // Track navigation shortcuts
        if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
            this.addEvent('keypress', `nav:${e.key}`, {
                ctrl: e.ctrlKey,
                alt: e.altKey,
            });
        }

        // Track save shortcut (Ctrl/Cmd + S)
        if ((e.ctrlKey || e.metaKey) && e.key === 's') {
            this.addEvent('keypress', 'save:shortcut');
        }
    }

    /**
     * Track AI assistance request
     * @param {string} schemaName - The schema requesting AI help
     */
    trackAIRequest(schemaName) {
        this.addEvent('ai_request', `schema:${schemaName}`);

        // Also track via dedicated AI endpoint
        this.sendAIUsage('request', schemaName);
    }

    /**
     * Track AI assistance response
     * @param {string} schemaName - The schema that received help
     * @param {Array} suggestions - AI suggestions provided
     */
    trackAIResponse(schemaName, suggestions) {
        this.addEvent('ai_response', `schema:${schemaName}`, {
            suggestion_count: suggestions ? suggestions.length : 0,
            suggestions: suggestions
        });

        this.sendAIUsage('response', schemaName, { suggestions });
    }

    /**
     * Track user accepting AI suggestion
     * @param {string} schemaName - The schema
     * @param {string} acceptedValue - The value accepted
     */
    trackAIAccept(schemaName, acceptedValue) {
        this.addEvent('ai_accept', `schema:${schemaName}`, {
            accepted: acceptedValue
        });

        this.sendAIUsage('accept', schemaName, { accepted_value: acceptedValue });
    }

    /**
     * Track user rejecting AI suggestion
     * @param {string} schemaName - The schema
     */
    trackAIReject(schemaName) {
        this.addEvent('ai_reject', `schema:${schemaName}`);

        this.sendAIUsage('reject', schemaName);
    }

    /**
     * Track annotation change
     * @param {string} schemaName - Schema name
     * @param {string} labelName - Label name
     * @param {string} action - Action type (select, deselect, update, clear)
     * @param {*} oldValue - Previous value
     * @param {*} newValue - New value
     * @param {string} source - What triggered the change (user, ai_accept, keyboard, prefill)
     */
    trackAnnotationChange(schemaName, labelName, action, oldValue, newValue, source = 'user') {
        this.addEvent('annotation_change', `schema:${schemaName}`, {
            label: labelName,
            action: action,
            old_value: oldValue,
            new_value: newValue,
            source: source,
        });

        // Also send to dedicated annotation change endpoint for persistence
        this.sendAnnotationChange(schemaName, labelName, action, oldValue, newValue, source);
    }

    /**
     * Track navigation between instances
     * @param {string} action - Navigation action (next, prev, jump)
     * @param {string} fromInstance - Previous instance ID
     * @param {string} toInstance - New instance ID
     */
    trackNavigation(action, fromInstance, toInstance) {
        this.addEvent('navigation', action, {
            from_instance: fromInstance,
            to_instance: toInstance,
        });
    }

    /**
     * Track save action
     * @param {string} instanceId - Instance being saved
     */
    trackSave(instanceId) {
        this.addEvent('save', `instance:${instanceId || this.currentInstanceId}`);
    }

    /**
     * Track when an annotation becomes stale due to display logic changes.
     * Stale annotations are annotations for schemas that were hidden because
     * conditions changed (e.g., user changed a parent answer).
     *
     * @param {string} schemaName - Schema that became stale
     * @param {*} value - The value that is now stale
     * @param {string} reason - Why the schema became hidden (condition not met)
     */
    trackStaleAnnotation(schemaName, value, reason) {
        this.addEvent('annotation_stale', `schema:${schemaName}`, {
            stale_value: value,
            reason: reason,
            timestamp: new Date().toISOString()
        });
    }

    /**
     * Track display logic visibility changes.
     * @param {string} schemaName - Schema whose visibility changed
     * @param {boolean} visible - New visibility state
     * @param {string} reason - Reason for visibility change
     */
    trackDisplayLogicChange(schemaName, visible, reason) {
        this.addEvent('display_logic_change', `schema:${schemaName}`, {
            visible: visible,
            reason: reason,
            timestamp: new Date().toISOString()
        });
    }

    /**
     * Get a unique identifier for an element
     * @param {Element} element - DOM element
     * @returns {string|null} - Element identifier or null
     */
    getTargetIdentifier(element) {
        if (!element || !element.closest) return null;

        // Check for annotation labels (checkbox/radio inputs or their labels)
        const labelInput = element.closest('input[type="checkbox"], input[type="radio"]');
        if (labelInput) {
            const name = labelInput.name || '';
            const value = labelInput.value || '';
            if (name) {
                return `label:${name}:${value}`;
            }
        }

        // Check for label wrapper with data attributes
        const labelWrapper = element.closest('[data-label-name]');
        if (labelWrapper) {
            return `label:${labelWrapper.dataset.labelName}`;
        }

        // Check for schema elements
        const schema = element.closest('[data-schema-name]');
        if (schema) {
            return `schema:${schema.dataset.schemaName}`;
        }

        // Check for annotation schema containers
        const schemaContainer = element.closest('.annotation-schema');
        if (schemaContainer) {
            const schemaName = schemaContainer.id || schemaContainer.dataset.schema;
            if (schemaName) {
                return `schema:${schemaName}`;
            }
        }

        // Check for navigation buttons
        if (element.id === 'next_instance_button' || element.closest('#next_instance_button')) {
            return 'nav:next';
        }
        if (element.id === 'prev_instance_button' || element.closest('#prev_instance_button')) {
            return 'nav:prev';
        }
        if (element.id === 'save_button' || element.closest('#save_button')) {
            return 'nav:save';
        }

        // Check for AI assistant elements
        if (element.closest('.ai-assistant-button')) return 'ai:request';
        if (element.closest('.ai-suggestion')) return 'ai:suggestion';
        if (element.closest('.ai-assistant-panel')) return 'ai:panel';

        // Check for span annotation
        if (element.closest('.annotation-span')) return 'span:click';
        if (element.closest('.span-label-option')) return 'span:label';

        // Check for text inputs (textbox schemas)
        const textInput = element.closest('input[type="text"], textarea');
        if (textInput && textInput.name) {
            return `textbox:${textInput.name}`;
        }

        // Check for slider elements
        const slider = element.closest('input[type="range"]');
        if (slider && slider.name) {
            return `slider:${slider.name}`;
        }

        return null;
    }

    /**
     * Add an event to the queue
     * @param {string} eventType - Type of event
     * @param {string} target - Target identifier
     * @param {Object} metadata - Additional metadata
     */
    addEvent(eventType, target, metadata = {}) {
        const event = {
            event_type: eventType,
            timestamp: Date.now() / 1000,  // Unix timestamp in seconds
            client_timestamp: Date.now(),   // Milliseconds for latency analysis
            target: target,
            instance_id: this.currentInstanceId,
            metadata: metadata,
        };

        this.events.push(event);

        if (this.debugMode) {
            console.log('[InteractionTracker] Event:', event);
        }

        // Auto-flush if buffer is large
        if (this.events.length >= 50) {
            this.flush(false);
        }
    }

    /**
     * Flush events to the server
     * @param {boolean} isFinal - Whether this is a final flush (page unload)
     */
    async flush(isFinal) {
        if (this.events.length === 0 && Object.keys(this.focusTime).length === 0) {
            return;
        }

        const payload = {
            instance_id: this.currentInstanceId,
            events: [...this.events],
            focus_time: { ...this.focusTime },
            scroll_depth: this.scrollDepthMax,
        };

        // Clear local buffers
        this.events = [];
        this.focusTime = {};

        if (this.debugMode) {
            console.log('[InteractionTracker] Flushing:', payload);
        }

        if (isFinal) {
            // Use sendBeacon for reliable delivery on page unload
            const blob = new Blob([JSON.stringify(payload)], { type: 'application/json' });
            navigator.sendBeacon('/api/track_interactions', blob);
        } else {
            try {
                await fetch('/api/track_interactions', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(payload),
                });
            } catch (e) {
                if (this.debugMode) {
                    console.warn('[InteractionTracker] Failed to send interaction data:', e);
                }
            }
        }

        this.lastFlush = Date.now();
    }

    /**
     * Send AI usage event to dedicated endpoint
     * @param {string} eventType - Event type
     * @param {string} schemaName - Schema name
     * @param {Object} data - Additional data
     */
    async sendAIUsage(eventType, schemaName, data = {}) {
        try {
            await fetch('/api/track_ai_usage', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    instance_id: this.currentInstanceId,
                    schema_name: schemaName,
                    event_type: eventType,
                    ...data,
                }),
            });
        } catch (e) {
            if (this.debugMode) {
                console.warn('[InteractionTracker] Failed to send AI usage data:', e);
            }
        }
    }

    /**
     * Send annotation change to dedicated endpoint
     */
    async sendAnnotationChange(schemaName, labelName, action, oldValue, newValue, source) {
        try {
            await fetch('/api/track_annotation_change', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({
                    instance_id: this.currentInstanceId,
                    schema_name: schemaName,
                    label_name: labelName,
                    action: action,
                    old_value: oldValue,
                    new_value: newValue,
                    source: source,
                }),
            });
        } catch (e) {
            if (this.debugMode) {
                console.warn('[InteractionTracker] Failed to send annotation change:', e);
            }
        }
    }

    /**
     * Enable or disable debug mode
     * @param {boolean} enabled - Whether debug mode is enabled
     */
    setDebugMode(enabled) {
        this.debugMode = enabled;
        console.log(`[InteractionTracker] Debug mode: ${enabled ? 'enabled' : 'disabled'}`);
    }

    /**
     * Clean up tracker resources
     */
    destroy() {
        if (this.flushTimer) {
            clearInterval(this.flushTimer);
        }
        this.flush(true);
    }
}

// Create global instance
window.interactionTracker = new InteractionTracker();

// Expose for debugging
window.InteractionTracker = InteractionTracker;