Now replace the `searchContext` function to increase limits, add total count, and add "Add All" button:


Edit

function searchContext(query) {
            var resultsEl = document.getElementById('context-search-results');
            resultsEl.innerHTML = '
Searching...
';

            // Search contacts and threads in parallel
            Promise.all([
                fetch('api.php?action=contacts&search=' + encodeURIComponent(query) + '&limit=5').then(function(r) { return r.json(); }),
                fetch('api.php?action=threads&search=' + encodeURIComponent(query) + '&limit=5').then(function(r) { return r.json(); })
            ]).then(function(results) {
                var contacts = results[0].contacts || [];
                var threads = results[1].threads || [];
                var html = '';

                if (contacts.length > 0) {
                    html += '
CONTACTS
';
                    contacts.forEach(function(c) {
                        html += '
';
                        html += '
';
                        html += '' + escapeHtml(c.name || c.email) + '';
                        html += '
' + escapeHtml(c.email || '') + ' | ' + (c.email_count || 0) + ' emails
';
                        html += '
';
                        html += '<button class="context-add-btn" onclick="loadContactThreads(' + (c.id || c.contact_id) + ', \'' + escapeHtml(c.name || '') + '\')">View Threads</button>';
                        html += '
';
                    });
                }

                if (threads.length > 0) {
                    html += '
THREADS
';
                    threads.forEach(function(t) {
                        var already = contextThreads.some(function(ct) { return ct.threadId === t.thread_id; });
                        var isCurrent = t.thread_id === currentThreadId;
                        html += '
';
                        html += '
';
                        html += '' + escapeHtml(t.subject || '(No Subject)') + '';
                        html += '
' + (t.email_count || 0) + ' emails | ' + escapeHtml(t.contact_name || '') + '
';
                        html += '
';
                        if (isCurrent) {
                            html += 'Current';
                        } else if (already) {
                            html += 'Added';
                        } else {
                            html += '<button class="context-add-btn" onclick="addThreadToContext(\'' + t.thread_id + '\', \'' + escapeHtml(t.subject || '').replace(/'/g, "\\'") + '\', ' + (t.email_count || 0) + ')">+ Add</button>';
                        }
                        html += '
';
                    });
                }

                if (!html) {
                    html = '
No results found
';
                }

                resultsEl.innerHTML = html;
            }).catch(function(err) {
                resultsEl.innerHTML = '
Search failed
';
            });
        }

function searchContext(query) {
            var resultsEl = document.getElementById('context-search-results');
            resultsEl.innerHTML = '
Searching...
';

            // Search contacts and threads in parallel — higher limit for threads
            Promise.all([
                fetch('api.php?action=contacts&search=' + encodeURIComponent(query) + '&limit=10').then(function(r) { return r.json(); }),
                fetch('api.php?action=threads&search=' + encodeURIComponent(query) + '&limit=100').then(function(r) { return r.json(); })
            ]).then(function(results) {
                var contacts = results[0].contacts || [];
                var threads = results[1].threads || [];
                var threadTotal = (results[1].pagination && results[1].pagination.total) || threads.length;
                var html = '';

                if (contacts.length > 0) {
                    html += '
CONTACTS (' + contacts.length + ')
';
                    contacts.forEach(function(c) {
                        html += '
';
                        html += '
';
                        html += '' + escapeHtml(c.name || c.email) + '';
                        html += '
' + escapeHtml(c.email || '') + ' | ' + (c.email_count || 0) + ' emails
';
                        html += '
';
                        html += '<button class="context-add-btn" onclick="loadContactThreads(' + (c.id || c.contact_id) + ', \'' + escapeHtml(c.name || '') + '\')">View Threads</button>';
                        html += '
';
                    });
                }

                if (threads.length > 0) {
                    // Store for "Add All" functionality
                    window._lastSearchThreads = threads;

                    html += '
THREADS (' + threadTotal + ' found' + (threadTotal > threads.length ? ', showing ' + threads.length : '') + ')
';

                    // "Add All" button
                    var notAddedCount = threads.filter(function(t) {
                        return t.thread_id !== currentThreadId && !contextThreads.some(function(ct) { return ct.threadId === t.thread_id; });
                    }).length;
                    if (notAddedCount > 1) {
                        html += '
<button class="context-add-btn" onclick="addAllSearchThreads()" style="width:100%;text-align:center;padding:6px;font-weight:600;">+ Add All ' + notAddedCount + ' Threads</button>
';
                    }

                    threads.forEach(function(t) {
                        var already = contextThreads.some(function(ct) { return ct.threadId === t.thread_id; });
                        var isCurrent = t.thread_id === currentThreadId;
                        html += '
';
                        html += '
';
                        html += '' + escapeHtml(t.subject || '(No Subject)') + '';
                        html += '
' + (t.email_count || 0) + ' emails | ' + escapeHtml(t.contact_name || '') + '
';
                        html += '
';
                        if (isCurrent) {
                            html += 'Current';
                        } else if (already) {
                            html += 'Added';
                        } else {
                            html += '<button class="context-add-btn" onclick="addThreadToContext(\'' + t.thread_id + '\', \'' + escapeHtml(t.subject || '').replace(/'/g, "\\'") + '\', ' + (t.email_count || 0) + ')">+ Add</button>';
                        }
                        html += '
';
                    });

                    // If more threads exist, show "Load More" button
                    if (threadTotal > threads.length) {
                        html += '
<button class="context-add-btn" onclick="loadMoreSearchThreads(\'' + escapeHtml(query).replace(/'/g, "\\'") + '\', ' + threads.length + ', ' + threadTotal + ')" style="width:100%;text-align:center;padding:6px;color:var(--text-secondary);">Load More (' + (threadTotal - threads.length) + ' remaining)</button>
';
                    }
                }

                if (!html) {
                    html = '
No results found
';
                }

                resultsEl.innerHTML = html;
            }).catch(function(err) {
                resultsEl.innerHTML = '
Search failed
';
            });
        }

        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);
                    }
                });
        }