| @@ -1617,71 +1617,8 @@ | ||
| 1617 | 1617 | }); |
| 1618 | 1618 | |
| 1619 | 1619 | } |
| 1620 | 1620 | |
| 1621 | -// 3.2.3: Confirmation dialog shown when the user attempts to switch embedding | |
| 1622 | -// models after they've already embedded content with a different model. Mixing | |
| 1623 | -// embeddings from two models silently breaks similarity matching. | |
| 1624 | -function showEmbeddingSwitchWarning(data, newValue, onChoice) { | |
| 1625 | - $('#mxchat_embedding_switch_warning').remove(); | |
| 1626 | - | |
| 1627 | - const dimsBlock = data.dims_differ ? ` | |
| 1628 | - <div class="mxchat-embed-warn-dims"> | |
| 1629 | - <strong>Dimension mismatch:</strong> | |
| 1630 | - Existing vectors are ${data.active_dims}-dimensional, but ${data.new_label} | |
| 1631 | - produces ${data.new_dims}-dimensional vectors. | |
| 1632 | - If you use Pinecone, your index will reject queries entirely until you re-embed. | |
| 1633 | - </div>` : ''; | |
| 1634 | - | |
| 1635 | - const $modal = $(` | |
| 1636 | - <div id="mxchat_embedding_switch_warning" class="mxchat-embed-warn-overlay"> | |
| 1637 | - <div class="mxchat-embed-warn-dialog"> | |
| 1638 | - <div class="mxchat-embed-warn-header"> | |
| 1639 | - <span class="mxchat-embed-warn-icon">⚠️</span> | |
| 1640 | - <h3>Switching embedding models will break similarity matching</h3> | |
| 1641 | - </div> | |
| 1642 | - <div class="mxchat-embed-warn-body"> | |
| 1643 | - <p> | |
| 1644 | - Your knowledge base and actions are currently embedded with | |
| 1645 | - <code>${data.active_label}</code>. Switching to | |
| 1646 | - <code>${data.new_label}</code> means new queries are embedded with | |
| 1647 | - a different model than the stored vectors — the chatbot will return | |
| 1648 | - inaccurate results or fail to match anything. | |
| 1649 | - </p> | |
| 1650 | - ${dimsBlock} | |
| 1651 | - <p><strong>To switch safely:</strong></p> | |
| 1652 | - <ol> | |
| 1653 | - <li>Go to <em>Knowledge Base</em> and delete all existing entries.</li> | |
| 1654 | - <li>Go to <em>Actions</em> and delete all existing actions.</li> | |
| 1655 | - <li>Come back here, switch the model, then re-import your content and re-add your actions.</li> | |
| 1656 | - </ol> | |
| 1657 | - </div> | |
| 1658 | - <div class="mxchat-embed-warn-footer"> | |
| 1659 | - <button type="button" class="button button-secondary" id="mxchat_embed_warn_cancel">Cancel — keep ${data.active_label}</button> | |
| 1660 | - <button type="button" class="button button-primary mxchat-embed-warn-danger" id="mxchat_embed_warn_continue">Switch anyway (I'll handle it)</button> | |
| 1661 | - </div> | |
| 1662 | - </div> | |
| 1663 | - </div> | |
| 1664 | - `); | |
| 1665 | - | |
| 1666 | - $('body').append($modal); | |
| 1667 | - | |
| 1668 | - let resolved = false; | |
| 1669 | - function resolve(confirmed) { | |
| 1670 | - if (resolved) return; | |
| 1671 | - resolved = true; | |
| 1672 | - $modal.remove(); | |
| 1673 | - if (typeof onChoice === 'function') onChoice(confirmed); | |
| 1674 | - } | |
| 1675 | - | |
| 1676 | - // Click on the overlay background dismisses (cancel) | |
| 1677 | - $modal.on('click', function(e) { | |
| 1678 | - if (e.target === $modal[0]) resolve(false); | |
| 1679 | - }); | |
| 1680 | - $modal.find('#mxchat_embed_warn_cancel').on('click', function() { resolve(false); }); | |
| 1681 | - $modal.find('#mxchat_embed_warn_continue').on('click', function() { resolve(true); }); | |
| 1682 | -} | |
| 1683 | - | |
| 1684 | 1621 | // Embedding model selector - completely separate from chat model selector |
| 1685 | 1622 | function setupMxChatEmbeddingModelSelector() { |
| 1686 | 1623 | const $embeddingModelSelect = $('#embedding_model'); |
| 1687 | 1624 | |
| @@ -1862,53 +1799,25 @@ | ||
| 1862 | 1799 | const activeCategory = $('.mxchat-embedding-model-selector-categories .mxchat-embedding-model-category-btn.active').data('category'); |
| 1863 | 1800 | populateEmbeddingModelsGrid(searchTerm, activeCategory); |
| 1864 | 1801 | }); |
| 1865 | 1802 | |
| 1866 | - // 3.2.3: Commit a model change — same as the original click handler, just | |
| 1867 | - // factored out so it can be invoked from both the safe path and the | |
| 1868 | - // post-confirmation path. | |
| 1869 | - function commitEmbeddingModelChange(modelValue) { | |
| 1803 | + // Use a direct selector to avoid conflicts with other card elements | |
| 1804 | + $(document).on('click.embeddingModelSelector', '.mxchat-embedding-model-selector-grid .mxchat-embedding-model-selector-card', function(e) { | |
| 1805 | + e.stopPropagation(); // Prevent event bubbling | |
| 1806 | + const modelValue = $(this).data('value'); | |
| 1807 | + | |
| 1808 | + // Important: Only update this specific select element | |
| 1870 | 1809 | $embeddingModelSelect.val(modelValue); |
| 1810 | + | |
| 1811 | + // Manually trigger change only on this element | |
| 1871 | 1812 | const changeEvent = new Event('change', { bubbles: true }); |
| 1872 | 1813 | $embeddingModelSelect[0].dispatchEvent(changeEvent); |
| 1814 | + | |
| 1815 | + // Update button text | |
| 1873 | 1816 | updateButtonText(); |
| 1817 | + | |
| 1818 | + // Hide modal | |
| 1874 | 1819 | $('#' + embeddingModalId).hide(); |
| 1875 | - } | |
| 1876 | - | |
| 1877 | - // Use a direct selector to avoid conflicts with other card elements | |
| 1878 | - $(document).on('click.embeddingModelSelector', '.mxchat-embedding-model-selector-grid .mxchat-embedding-model-selector-card', function(e) { | |
| 1879 | - e.stopPropagation(); // Prevent event bubbling | |
| 1880 | - const modelValue = $(this).data('value'); | |
| 1881 | - const previousValue = $embeddingModelSelect.val(); | |
| 1882 | - | |
| 1883 | - // No-op if the user clicked the already-selected card | |
| 1884 | - if (modelValue === previousValue) { | |
| 1885 | - $('#' + embeddingModalId).hide(); | |
| 1886 | - return; | |
| 1887 | - } | |
| 1888 | - | |
| 1889 | - // 3.2.3: Preflight — if the user has content embedded with a different | |
| 1890 | - // model, surface a confirmation dialog before committing the switch. | |
| 1891 | - $.post(ajaxurl, { | |
| 1892 | - action: 'mxchat_check_embedding_switch', | |
| 1893 | - security: (typeof mxchatAdmin !== 'undefined' ? mxchatAdmin.nonce : ''), | |
| 1894 | - new_model: modelValue | |
| 1895 | - }).done(function(resp) { | |
| 1896 | - if (resp && resp.success && resp.data && resp.data.is_mismatch) { | |
| 1897 | - showEmbeddingSwitchWarning(resp.data, modelValue, function(confirmed) { | |
| 1898 | - if (confirmed) { | |
| 1899 | - commitEmbeddingModelChange(modelValue); | |
| 1900 | - } | |
| 1901 | - }); | |
| 1902 | - } else { | |
| 1903 | - commitEmbeddingModelChange(modelValue); | |
| 1904 | - } | |
| 1905 | - }).fail(function() { | |
| 1906 | - // If preflight fails, fall back to the original behavior so a network | |
| 1907 | - // hiccup doesn't block legitimate model switches. | |
| 1908 | - commitEmbeddingModelChange(modelValue); | |
| 1909 | - }); | |
| 1910 | - return; | |
| 1911 | 1820 | }); |
| 1912 | 1821 | |
| 1913 | 1822 | // Close modal when clicking outside - use namespaced events |
| 1914 | 1823 | $(window).on('click.embeddingModelSelector', function(event) { |