Now replace the `addAllSearchThreads` and `loadMoreSearchThreads` functions with the improved versions:


Edit

        function addAllSearchThreads() {
            var threads = window._lastSearchThreads || [];
            var added = 0;
            threads.forEach(function(t) {
                if (t.thread_id !== currentThreadId && !contextThreads.some(function(ct) { return ct.threadId === t.thread_id; })) {
                    contextThreads.push({
                        threadId: t.thread_id,
                        subject: t.subject || '(No Subject)',
                        emailCount: t.email_count || 0
                    });
                    added++;
                }
            });
            updateContextCart();
            updateTokenEstimate();
            // Refresh to show "Added" labels
            var q = document.getElementById('context-search').value.trim();
            if (q) searchContext(q);
        }

        function loadMoreSearchThreads(query, currentCount, total) {
            var page = Math.floor(currentCount / 100) + 1;
            fetch('api.php?action=threads&search=' + encodeURIComponent(query) + '&limit=100&page=' + (page + 1))
                .then(function(r) { return r.json(); })
                .then(function(data) {
                    var newThreads = data.threads || [];
                    if (newThreads.length > 0) {
                        window._lastSearchThreads = (window._lastSearchThreads || []).concat(newThreads);
                        // Re-render with all threads
                        searchContext(query);
                    }
                });
        }

function addAllSearchThreads() {
            var threads = window._lastSearchThreads || [];
            var added = 0;
            threads.forEach(function(t) {
                if (t.thread_id !== currentThreadId && !contextThreads.some(function(ct) { return ct.threadId === t.thread_id; })) {
                    contextThreads.push({
                        threadId: t.thread_id,
                        subject: t.subject || '(No Subject)',
                        emailCount: t.email_count || 0
                    });
                    added++;
                }
            });
            updateContextCart();
            updateTokenEstimate();
            // Refresh to show "Added" labels
            var q = document.getElementById('context-search').value.trim();
            if (q) searchContext(q);
        }

        async function addAllSearchThreadsFull() {
            var query = window._lastSearchQuery || document.getElementById('context-search').value.trim();
            var total = window._lastSearchTotal || 0;
            if (!query || !total) return;

            var resultsEl = document.getElementById('context-search-results');
            var allThreads = [];
            var page = 1;
            var perPage = 100;
            var pages = Math.ceil(total / perPage);

            resultsEl.innerHTML = '
Fetching all ' + total + ' threads (page 1/' + pages + ')...
';

            try {
                while (page <= pages) {
                    resultsEl.innerHTML = '
Fetching all threads (page ' + page + '/' + pages + ')...
';
                    var resp = await fetch('api.php?action=threads&search=' + encodeURIComponent(query) + '&limit=' + perPage + '&page=' + page);
                    var data = await resp.json();
                    var threads = data.threads || [];
                    if (threads.length === 0) break;
                    allThreads = allThreads.concat(threads);
                    page++;
                }

                // Add all to context
                var added = 0;
                allThreads.forEach(function(t) {
                    if (t.thread_id !== currentThreadId && !contextThreads.some(function(ct) { return ct.threadId === t.thread_id; })) {
                        contextThreads.push({
                            threadId: t.thread_id,
                            subject: t.subject || '(No Subject)',
                            emailCount: t.email_count || 0
                        });
                        added++;
                    }
                });

                // Store all fetched threads
                window._lastSearchThreads = allThreads;

                updateContextCart();
                updateTokenEstimate();
                searchContext(query);
            } catch (err) {
                resultsEl.innerHTML = '
Error fetching all threads: ' + err.message + '
';
            }
        }