coding-agent-search 0.5.2

Unified TUI search over local coding agent histories
Documentation
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/**
 * cass Archive Virtual List Component
 *
 * Efficient virtual scrolling that renders only visible items, enabling
 * smooth navigation through 10K+ search results without memory exhaustion.
 *
 * Uses a fixed-height item approach for O(1) scroll position calculations.
 */

/**
 * Virtual List for fixed-height items
 *
 * @example
 * const list = new VirtualList({
 *     container: document.getElementById('results'),
 *     itemHeight: 80,
 *     totalCount: results.length,
 *     renderItem: (index) => createResultCard(results[index]),
 *     overscan: 3
 * });
 */
export class VirtualList {
    /**
     * Create a new virtual list
     * @param {Object} options
     * @param {HTMLElement} options.container - Scroll container element
     * @param {number} options.itemHeight - Fixed height per item in pixels
     * @param {number} options.totalCount - Total number of items
     * @param {Function} options.renderItem - Function to render item at index
     * @param {number} [options.overscan=3] - Extra items to render above/below viewport
     * @param {Function} [options.onScrollEnd] - Callback when near end of list
     */
    constructor({ container, itemHeight, totalCount, renderItem, overscan = 3, onScrollEnd = null }) {
        this.container = container;
        this.itemHeight = itemHeight;
        this.totalCount = totalCount;
        this.renderItem = renderItem;
        this.overscan = overscan;
        this.onScrollEnd = onScrollEnd;

        this.scrollTop = 0;
        this.containerHeight = 0;
        this.items = new Map(); // index -> element
        this.lastVisibleRange = { start: -1, end: -1 };

        // Performance metrics
        this.metrics = {
            renders: 0,
            recycled: 0,
            created: 0,
        };
        this.destroyed = false;

        this._init();
    }

    /**
     * Initialize the virtual list
     * @private
     */
    _init() {
        // Create inner container for total height simulation
        this.inner = document.createElement('div');
        this.inner.className = 'virtual-list-inner';
        this.inner.style.height = `${this.totalCount * this.itemHeight}px`;
        this.inner.style.position = 'relative';
        this.inner.style.width = '100%';

        // Clear and set up container
        this.container.innerHTML = '';
        this.container.style.overflow = 'auto';
        this.container.style.position = 'relative';
        this.container.appendChild(this.inner);

        // Set up resize observer for responsive sizing
        this._resizeObserver = new ResizeObserver(() => this._onResize());
        this._resizeObserver.observe(this.container);

        // Throttled scroll handler
        this._scrollHandler = this._createThrottledHandler(() => this._onScroll(), 16);
        this.container.addEventListener('scroll', this._scrollHandler, { passive: true });

        // Initial render
        this._onResize();

        console.debug('[VirtualList] Initialized with', this.totalCount, 'items');
    }

    /**
     * Create a throttled event handler using requestAnimationFrame
     * @private
     */
    _createThrottledHandler(fn, wait) {
        let pending = false;
        return () => {
            if (this.destroyed || pending) {
                return;
            }
            if (!pending) {
                pending = true;
                requestAnimationFrame(() => {
                    if (this.destroyed) {
                        pending = false;
                        return;
                    }
                    fn();
                    pending = false;
                });
            }
        };
    }

    /**
     * Handle container resize
     * @private
     */
    _onResize() {
        if (this.destroyed) {
            return;
        }
        this.containerHeight = this.container.clientHeight;
        this._render();
    }

    /**
     * Handle scroll event
     * @private
     */
    _onScroll() {
        if (this.destroyed) {
            return;
        }
        this.scrollTop = this.container.scrollTop;
        this._render();

        // Check for scroll end callback
        if (this.onScrollEnd && this._isNearEnd()) {
            this.onScrollEnd();
        }
    }

    /**
     * Check if scrolled near the end
     * @private
     */
    _isNearEnd() {
        const totalHeight = this.totalCount * this.itemHeight;
        const remaining = totalHeight - this.scrollTop - this.containerHeight;
        return remaining < this.containerHeight * 2;
    }

    /**
     * Calculate visible range of items
     * @private
     */
    _getVisibleRange() {
        const startIndex = Math.max(0,
            Math.floor(this.scrollTop / this.itemHeight) - this.overscan
        );
        const endIndex = Math.min(this.totalCount,
            Math.ceil((this.scrollTop + this.containerHeight) / this.itemHeight) + this.overscan
        );
        return { start: startIndex, end: endIndex };
    }

    /**
     * Render visible items
     * @private
     */
    _render() {
        if (this.destroyed || !this.inner) {
            return;
        }
        const { start, end } = this._getVisibleRange();

        // Skip render if range unchanged
        if (start === this.lastVisibleRange.start && end === this.lastVisibleRange.end) {
            return;
        }

        this.lastVisibleRange = { start, end };
        this.metrics.renders++;

        const visible = new Set();

        // Add/update visible items
        for (let i = start; i < end; i++) {
            visible.add(i);

            if (!this.items.has(i)) {
                const element = this.renderItem(i);
                element.style.position = 'absolute';
                element.style.top = `${i * this.itemHeight}px`;
                element.style.left = '0';
                element.style.right = '0';
                element.style.height = `${this.itemHeight}px`;
                element.dataset.virtualIndex = i;

                this.inner.appendChild(element);
                this.items.set(i, element);
                this.metrics.created++;
            }
        }

        // Remove items no longer visible
        for (const [index, element] of this.items) {
            if (!visible.has(index)) {
                element.remove();
                this.items.delete(index);
                this.metrics.recycled++;
            }
        }

        console.debug(`[VirtualList] Rendering ${this.items.size} of ${this.totalCount} items (range: ${start}-${end})`);
    }

    /**
     * Update total item count
     * @param {number} newCount - New total count
     */
    updateTotalCount(newCount) {
        this.totalCount = newCount;
        this.inner.style.height = `${newCount * this.itemHeight}px`;

        // Force re-render to clean up out-of-range items
        this.lastVisibleRange = { start: -1, end: -1 };
        this._render();
    }

    /**
     * Scroll to a specific item index
     * @param {number} index - Item index to scroll to
     * @param {string} [align='start'] - Alignment: 'start' | 'center' | 'end'
     */
    scrollToIndex(index, align = 'start') {
        let targetTop = index * this.itemHeight;

        if (align === 'center') {
            targetTop = targetTop - (this.containerHeight / 2) + (this.itemHeight / 2);
        } else if (align === 'end') {
            targetTop = targetTop - this.containerHeight + this.itemHeight;
        }

        this.container.scrollTop = Math.max(0, targetTop);
        this.scrollTop = this.container.scrollTop;
        this._render();
    }

    /**
     * Force re-render all visible items
     */
    refresh() {
        // Remove all current items
        for (const [, element] of this.items) {
            element.remove();
        }
        this.items.clear();
        this.lastVisibleRange = { start: -1, end: -1 };
        this._render();
    }

    /**
     * Get the currently visible range
     * @returns {{start: number, end: number}} Visible item range
     */
    getVisibleRange() {
        return { ...this.lastVisibleRange };
    }

    /**
     * Get performance metrics
     * @returns {Object} Metrics object
     */
    getMetrics() {
        return { ...this.metrics };
    }

    /**
     * Clean up resources
     */
    destroy() {
        this.destroyed = true;

        if (this._resizeObserver) {
            this._resizeObserver.disconnect();
            this._resizeObserver = null;
        }

        if (this._scrollHandler) {
            this.container.removeEventListener('scroll', this._scrollHandler);
            this._scrollHandler = null;
        }

        for (const [, element] of this.items) {
            element.remove();
        }
        this.items.clear();

        if (this.inner) {
            this.inner.remove();
            this.inner = null;
        }

        console.debug('[VirtualList] Destroyed. Metrics:', this.metrics);
    }
}

/**
 * Virtual List for variable-height items
 *
 * Uses estimated heights and measures actual heights after rendering.
 * More expensive than fixed-height but handles dynamic content.
 *
 * @example
 * const list = new VariableHeightVirtualList({
 *     container: document.getElementById('messages'),
 *     totalCount: messages.length,
 *     estimatedItemHeight: 120,
 *     renderItem: (index) => createMessageElement(messages[index]),
 * });
 */
export class VariableHeightVirtualList {
    /**
     * Create a new variable-height virtual list
     * @param {Object} options
     * @param {HTMLElement} options.container - Scroll container element
     * @param {number} options.totalCount - Total number of items
     * @param {number} options.estimatedItemHeight - Estimated height per item
     * @param {Function} options.renderItem - Function to render item at index
     * @param {number} [options.overscan=5] - Extra items to render above/below viewport
     */
    constructor({ container, totalCount, estimatedItemHeight, renderItem, overscan = 5 }) {
        this.container = container;
        this.totalCount = totalCount;
        this.estimatedHeight = estimatedItemHeight;
        this.renderItem = renderItem;
        this.overscan = overscan;

        this.scrollTop = 0;
        this.containerHeight = 0;

        // Height tracking
        this.heights = new Map(); // index -> measured height
        this.positions = []; // cumulative positions

        // DOM tracking
        this.items = new Map(); // index -> element
        this.lastVisibleRange = { start: -1, end: -1 };
        this.destroyed = false;
        this._scrollFramePending = false;
        this._scrollFrameId = null;

        this._init();
    }

    /**
     * Initialize the virtual list
     * @private
     */
    _init() {
        // Calculate initial positions
        this._calculatePositions();

        // Create inner container
        this.inner = document.createElement('div');
        this.inner.className = 'virtual-list-inner variable-height';
        this.inner.style.position = 'relative';
        this.inner.style.width = '100%';
        this._updateTotalHeight();

        // Set up container
        this.container.innerHTML = '';
        this.container.style.overflow = 'auto';
        this.container.style.position = 'relative';
        this.container.appendChild(this.inner);

        // Set up resize observer
        this._resizeObserver = new ResizeObserver(() => this._onResize());
        this._resizeObserver.observe(this.container);

        // Scroll handler
        this._scrollHandler = () => {
            if (this.destroyed || this._scrollFramePending) {
                return;
            }

            this._scrollFramePending = true;
            this._scrollFrameId = requestAnimationFrame(() => {
                this._scrollFramePending = false;
                this._scrollFrameId = null;
                if (this.destroyed) {
                    return;
                }
                this._onScroll();
            });
        };
        this.container.addEventListener('scroll', this._scrollHandler, { passive: true });

        // Initial render
        this._onResize();

        console.debug('[VariableVirtualList] Initialized with', this.totalCount, 'items');
    }

    /**
     * Calculate cumulative positions based on known/estimated heights
     * @private
     */
    _calculatePositions() {
        this.positions = new Array(this.totalCount + 1);
        this.positions[0] = 0;

        for (let i = 0; i < this.totalCount; i++) {
            const height = this.heights.get(i) ?? this.estimatedHeight;
            this.positions[i + 1] = this.positions[i] + height;
        }
    }

    /**
     * Update total height based on positions
     * @private
     */
    _updateTotalHeight() {
        const totalHeight = this.positions[this.totalCount] ?? this.totalCount * this.estimatedHeight;
        this.inner.style.height = `${totalHeight}px`;
    }

    /**
     * Get item height (measured or estimated)
     * @private
     */
    _getItemHeight(index) {
        return this.heights.get(index) ?? this.estimatedHeight;
    }

    /**
     * Get item position
     * @private
     */
    _getItemPosition(index) {
        return this.positions[index] ?? index * this.estimatedHeight;
    }

    /**
     * Find item index at scroll position using binary search
     * @private
     */
    _findIndexAtPosition(scrollTop) {
        let low = 0;
        let high = this.totalCount - 1;

        while (low < high) {
            const mid = Math.floor((low + high + 1) / 2);
            if (this._getItemPosition(mid) <= scrollTop) {
                low = mid;
            } else {
                high = mid - 1;
            }
        }

        return low;
    }

    /**
     * Handle container resize
     * @private
     */
    _onResize() {
        if (this.destroyed) {
            return;
        }
        this.containerHeight = this.container.clientHeight;
        this._render();
    }

    /**
     * Handle scroll event
     * @private
     */
    _onScroll() {
        if (this.destroyed) {
            return;
        }
        this.scrollTop = this.container.scrollTop;
        this._render();
    }

    /**
     * Get visible range
     * @private
     */
    _getVisibleRange() {
        const startIndex = Math.max(0, this._findIndexAtPosition(this.scrollTop) - this.overscan);
        const endIndex = Math.min(
            this.totalCount,
            this._findIndexAtPosition(this.scrollTop + this.containerHeight) + this.overscan + 1
        );
        return { start: startIndex, end: endIndex };
    }

    /**
     * Render visible items
     * @private
     */
    _render() {
        if (this.destroyed || !this.inner) {
            return;
        }
        const { start, end } = this._getVisibleRange();

        // Skip if unchanged
        if (start === this.lastVisibleRange.start && end === this.lastVisibleRange.end) {
            return;
        }

        this.lastVisibleRange = { start, end };
        const visible = new Set();

        // Add/update visible items
        for (let i = start; i < end; i++) {
            visible.add(i);

            if (!this.items.has(i)) {
                const element = this.renderItem(i);
                element.style.position = 'absolute';
                element.style.top = `${this._getItemPosition(i)}px`;
                element.style.left = '0';
                element.style.right = '0';
                element.dataset.virtualIndex = i;

                this.inner.appendChild(element);
                this.items.set(i, element);

                // Measure actual height after render
                requestAnimationFrame(() => {
                    if (this.destroyed) {
                        return;
                    }
                    this._measureItem(i, element);
                });
            }
        }

        // Remove items no longer visible
        for (const [index, element] of this.items) {
            if (!visible.has(index)) {
                element.remove();
                this.items.delete(index);
            }
        }

        console.debug(`[VariableVirtualList] Rendering ${this.items.size} of ${this.totalCount} items`);
    }

    /**
     * Measure rendered item and update positions if needed
     * @private
     */
    _measureItem(index, element) {
        if (this.destroyed || !this.inner || !element?.isConnected) {
            return;
        }
        const measuredHeight = element.offsetHeight;
        const previousHeight = this.heights.get(index);

        if (previousHeight !== measuredHeight) {
            this.heights.set(index, measuredHeight);

            // Recalculate positions from this index forward
            for (let i = index; i < this.totalCount; i++) {
                const height = this.heights.get(i) ?? this.estimatedHeight;
                this.positions[i + 1] = this.positions[i] + height;
            }

            this._updateTotalHeight();

            // Update positions of rendered items after this one
            for (const [idx, el] of this.items) {
                if (idx > index) {
                    el.style.top = `${this._getItemPosition(idx)}px`;
                }
            }
        }
    }

    /**
     * Scroll to a specific item index
     * @param {number} index - Item index to scroll to
     * @param {string} [align='start'] - Alignment: 'start' | 'center' | 'end'
     */
    scrollToIndex(index, align = 'start') {
        let targetTop = this._getItemPosition(index);
        const itemHeight = this._getItemHeight(index);

        if (align === 'center') {
            targetTop = targetTop - (this.containerHeight / 2) + (itemHeight / 2);
        } else if (align === 'end') {
            targetTop = targetTop - this.containerHeight + itemHeight;
        }

        this.container.scrollTop = Math.max(0, targetTop);
        this.scrollTop = this.container.scrollTop;
        this._render();
    }

    /**
     * Update total item count
     * @param {number} newCount - New total count
     */
    updateTotalCount(newCount) {
        this.totalCount = newCount;
        this._calculatePositions();
        this._updateTotalHeight();
        this.lastVisibleRange = { start: -1, end: -1 };
        this._render();
    }

    /**
     * Force re-render all visible items
     */
    refresh() {
        for (const [, element] of this.items) {
            element.remove();
        }
        this.items.clear();
        this.lastVisibleRange = { start: -1, end: -1 };
        this._render();
    }

    /**
     * Clean up resources
     */
    destroy() {
        this.destroyed = true;

        if (this._resizeObserver) {
            this._resizeObserver.disconnect();
        }

        if (this._scrollHandler) {
            this.container.removeEventListener('scroll', this._scrollHandler);
        }

        if (this._scrollFrameId !== null && typeof cancelAnimationFrame === 'function') {
            cancelAnimationFrame(this._scrollFrameId);
            this._scrollFrameId = null;
        }
        this._scrollFramePending = false;

        for (const [, element] of this.items) {
            element.remove();
        }
        this.items.clear();

        if (this.inner) {
            this.inner.remove();
            this.inner = null;
        }

        console.debug('[VariableVirtualList] Destroyed');
    }
}

// Export default
export default {
    VirtualList,
    VariableHeightVirtualList,
};